CGAL / cgal-swig-bindings

CGAL bindings using SWIG
Other
346 stars 93 forks source link

Alpha shape vertices do not seem to be in the correct order #252

Open MaxwellHogan opened 11 months ago

MaxwellHogan commented 11 months ago

I am trying to use the Alpha Shapes 2D alpha shapes methods to create a polygon based on a group of points. I have found that when I try to plot the alpha shapes it appears that the points are not organised in such a way that it will make it possible to properly plot the polygon. See the image below for clarification, Figure_1 The left plot shows the outcome when Alpha_shape_2 with the blue x showing the REGULAR finite vertices of the alpha shape. The right shows the outcome when using the weighted alpha shape method. In both examples you can see how the red polygon criss-crosses back and forth - which is the issue I am trying to resolve. Is this correct behaviour or is there something I can do to resolve this? The (mostly)full code is shown below:

my_points = {load my points from file}
my_points_weighted = {load my weighted points from file}

fig, (ax, ax1) = plt.subplots(1, 2)
# fig, ax = plt.subplots(figsize = (4,8))
ax.scatter(my_points[:,0], my_points[:,1], marker='x',c = 'orange')

## alpha shape
lst = [Point_2(p[0], p[1]) for p in my_points]
t = Alpha_shape_2(lst, 10000, GENERAL)
t.make_alpha_shape(lst)
optimal_alphas = t.find_optimal_alpha(1)
print(optimal_alphas.next())
print("alpha", t.set_alpha(optimal_alphas.next()))

## weighted alpha shape 
weighting = {load my point weightings from file}
lst_wp = [Weighted_point_2(Point_2(p[0], p[1]), weighting[int(p[1]),int(p[0])]) for p in my_points_weighted]
was = Weighted_alpha_shape_2()
was.make_alpha_shape(lst_wp)

# alpha_shape_vertices
# if t.classify(v) == REGULAR
alpha_vertices_weighted = np.array([(v.point().x(), v.point().y()) for v in was.finite_vertices() if was.classify(v) == REGULAR ], dtype=int)
alpha_vertices = np.array([(v.point().x(), v.point().y()) for v in t.finite_vertices() if t.classify(v) == REGULAR ], dtype=int)

poly = patches.Polygon(alpha_vertices, alpha = .3, color = 'r')
ax.scatter(alpha_vertices[:,0], alpha_vertices[:,1], marker='x')
ax.add_patch(poly)
ylim = ax.get_ylim()
ax.set_ylim(ylim[1], ylim[0])

poly = patches.Polygon(alpha_vertices_weighted, alpha = .3, color = 'r')
ax1.scatter(alpha_vertices_weighted[:,0], alpha_vertices_weighted[:,1], marker='x')
ax1.add_patch(poly)
ylim = ax1.get_ylim()
ax1.set_ylim(ylim[1], ylim[0])

plt.show()