JuliaGeometry / VoronoiDelaunay.jl

Fast and robust Voronoi & Delaunay tessellation creation with Julia
Other
123 stars 26 forks source link

Missing delaunay edges #51

Open javieryu opened 4 years ago

javieryu commented 4 years ago

I am using delaunayedges to create a graph from the a random distribution of points, but in cases where voronoi cells only have one neighbouring voronoi cell (for instance 3 points on a line) then the there are no delaunay edges.

Working example

Here is an example where the proper delaunay edges appear: Selection_054

Not working

In this case each of the three point has zeros delaunay edges where they should have 1 or 2, and delaunayedges(tess) is an empty iterator.

Selection_053

This is the code I am using:

function generate_random_delaunay_graph(N::Int64)
    graph = SimpleGraph(N)

    tess = DelaunayTessellation()
    width = VoronoiDelaunay.max_coord - VoronoiDelaunay.min_coord
    a = Point2D[Point(VoronoiDelaunay.min_coord + rand() * width,
                      VoronoiDelaunay.min_coord + rand() * width) for i in 1:N]
    push!(tess, a)

    plt = plot()
    x, y = getplotxy(voronoiedges(tess))
    plot!(plt, x, y, c=:red, lab="voronoi edges")
    x, y = getplotxy(delaunayedges(tess))
    plot!(plt, x, y, c=:blue, lab="delaunayedges")
    display(plt)

    point_lookup = Dict(a[i] => i for i in 1:N)

    for edge in delaunayedges(tess)
        i = point_lookup[geta(edge)]
        j = point_lookup[getb(edge)]
        add_edge!(graph, i, j)
    end

    return graph, tess
end