bellockk / alphashape

Toolbox for constructing alpha shapes.
MIT License
258 stars 28 forks source link

optimizealpha() not working as expected #37

Open due23 opened 1 year ago

due23 commented 1 year ago

Description

From a set of points I am trying to find the best alpha shape so am not providing the alphashape function with an alpha value so that it can find the best one for my points ie. alphashape.alphashape(points). However, this generated an error from optimizealpha() when there should not have been one as this was a working example provided in the docs. I think there may be a bug in the code of the actual package so it needs to be updated given not even the provided example works.

What I Did

points_2d = [(0., 0.), (0., 1.), (1., 1.), (1., 0.),
          (0.5, 0.25), (0.5, 0.75), (0.25, 0.5), (0.75, 0.5)]
alpha_shape = alphashape.alphashape(points_2d)
fig, ax = plt.subplots()
ax.scatter(*zip(*points))
ax.add_patch(PolygonPatch(alpha_shape, alpha=.2))
plt.show()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[86], line 3
      1 points_2d = [(0., 0.), (0., 1.), (1., 1.), (1., 0.),
      2           (0.5, 0.25), (0.5, 0.75), (0.25, 0.5), (0.75, 0.5)]
----> 3 alpha_shape = alphashape.alphashape(points_2d)
      4 fig, ax = plt.subplots()
      5 ax.scatter(*zip(*points))

File ~/Documents/ALA/galah-env/lib/python3.10/site-packages/alphashape/alphashape.py:131, in alphashape(points, alpha)
    129     except ImportError:
    130         from .optimizealpha import optimizealpha
--> 131     alpha = optimizealpha(points)
    133 # Convert the points to a numpy array
    134 if USE_GP and isinstance(points, geopandas.geoseries.GeoSeries):

File ~/Documents/ALA/galah-env/lib/python3.10/site-packages/alphashape/optimizealpha.py:100, in optimizealpha(points, max_iterations, lower, upper, silent)
     97 test_alpha = (upper + lower) * .5
     99 # Update the bounds to include the solution space
--> 100 if _testalpha(points, test_alpha):
    101     lower = test_alpha
    102 else:

File ~/Documents/ALA/galah-env/lib/python3.10/site-packages/alphashape/optimizealpha.py:41, in _testalpha(points, alpha)
     39     if not isinstance(points, MultiPoint):
     40         points = MultiPoint(list(points))
---> 41     return all([polygon.intersects(point) for point in points])
     42 elif isinstance(polygon, trimesh.base.Trimesh):
     43     return len(polygon.faces) > 0 and all(
     44         trimesh.proximity.signed_distance(polygon, list(points)) >= 0)

TypeError: 'MultiPoint' object is not iterable
HanatoK commented 1 year ago

Could you try the fix I mentioned in https://github.com/bellockk/alphashape/issues/36?

due23 commented 1 year ago

Could you try the fix I mentioned in #36?

Hi! yes this did fix the issue, however I was hoping it may be updated in the actual package as I am writing a tutorial for researchers to use this package and many people may not know or understand how to change the package code