我想使用Julia计算一组点的alpha形状(甚至只是凹面外壳)。在其他问题中,他们通过使用Delaunay tesselation Boundary enclosing a given set of points在python中解决了这个问题。
Julia中的这个包可以获得Delaunay tesselation https://github.com/JuliaGeometry/VoronoiDelaunay.jl (尽管我不确定它是否针对julia v0.7进行了更新)。我想知道是否已经有了julia v0.7的实现,可以得到呃alpha形状,甚至是一组点的凹面外壳。
或者,有没有一种方法可以高效地调用python (scipy.spatial.Delaunay)来完成这项工作?
发布于 2019-03-12 06:14:47
VoronoiDelaunay.jl适用于Julia1.0和1.1。它应该也适用于Julia 0.7。
VoronoiDelaunay.jl对坐标有一些数字限制,即(1.0+eps(), 2.0-eps()),因此您可能需要重新缩放数据点。
要使用您自己的点类型创建DelaunayTesselation,请确保您的类型是AbstractPoint2D的子类型,即<: AbstractPoint2D,并定义getx和gety方法。
我相信下面的示例代码使用DelaunayTesselation找到了一组点的凹壳,并绘制了结果。它基本上在this answer中使用相同的算法。您可以轻松地编辑代码以获得alpha形状。
我没有将一些代码片段包装到函数中。如果您需要高性能,请满足您的需求。我在检查点的相等性时使用了===,它实际上检查两个点是否是相同的对象(即内存中的地址)。如果你以某种方式在代码中破坏了这一部分,你可以扩展==并使用它来代替===。
using Random, VoronoiDelaunay, Plots
import Base.==
struct MyEdge{T<:AbstractPoint2D}
_a::T
_b::T
end
==(e1::MyEdge{T}, e2::MyEdge{T}) where {T<:AbstractPoint2D} = ((e1._a === e2._a) && (e1._b === e2._b)) || ((e1._b === e2._a) && (e2._b === e1._a))
###==(p1::T, p2::T) where {T<:AbstractPoint2D} = (getx(p1) == getx(p2)) && (gety(p1) == gety(p2))
### Create a Delaunay tesselation from random points
tess = DelaunayTessellation2D(46)
for _ in 1:23
push!(tess, Point2D(rand()+1, rand()+1))
end
edges = MyEdge[]
function add_edge!(edges, edge)
i = findfirst(e -> e == edge, edges)
if isnothing(i) # not found
push!(edges, edge)
else # found so not outer, remove this edge
deleteat!(edges, i)
end
end
for trig in tess
a, b, c = geta(trig), getb(trig), getc(trig)
add_edge!(edges, MyEdge(b, c))
add_edge!(edges, MyEdge(a, b))
add_edge!(edges, MyEdge(a, c))
end
### PLOT
x, y = Float64[], Float64[] # outer edges
for edge in edges
push!(x, getx(edge._a))
push!(x, getx(edge._b))
push!(x, NaN)
push!(y, gety(edge._a))
push!(y, gety(edge._b))
push!(y, NaN)
end
xall, yall = getplotxy(delaunayedges(tess)) # all the edges
plot(xall, yall, color=:blue, fmt=:svg, size=(400,400))
plot!(x, y, color=:red, linewidth=3, opacity=0.5)https://stackoverflow.com/questions/55104595
复制相似问题