【问题标题】:How to determine if a point lies inside a Concave Hull (Alpha Shape)?如何确定一个点是否位于凹壳(Alpha 形状)内?
【发布时间】:2020-05-12 16:48:26
【问题描述】:

我在 2D 平面中有一组坐标,我希望从中构造一个凹壳(Alpha 形状)。在此之后,我需要确定某个点是在成型的船体内部还是外部。

虽然我可以使用附带的代码对凸壳实现这一点,但我还没有找到一种方法对凹壳做同样的事情。

# Detection in Convex Hulls
from scipy.spatial import Delaunay

hull = Delaunay(points)
return hull.find_simplex(test_point) >= 0

我怎样才能为凹形船体实现同样的效果?

【问题讨论】:

    标签: python graphics polygon


    【解决方案1】:

    使用 Alpha Shape 包。这是一个示例代码:

    import matplotlib.pyplot as plt
    from descartes import PolygonPatch
    import alphashape
    import random
    from shapely.geometry import Point
    
    points = [(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, 2.0) # Create the alpha shape
    
    # Plotting the alpha shape over the input data
    fig, ax = plt.subplots()
    ax.scatter(*zip(*points), c='green')
    ax.add_patch(PolygonPatch(alpha_shape, alpha=0.2))
    
    N = 10 # number of random points
    for i in range(N):
        x = round(random.uniform(0, 1), 2)
        y = round(random.uniform(0, 1), 2)
        point = Point(x,y) # analysis point
        if alpha_shape.contains(point) == True:
            plt.scatter(x,y,c='blue')
        else:
            plt.scatter(x,y,c='red')
    

    传说:

    1. 红色表示凹多边形外的点
    2. 在绿色中,顶点
    3. 蓝色表示凹多边形内的点

    【讨论】:

      【解决方案2】:

      Shapely 包含 point.within(polygon) 和 polygon.contains(point) 方法。

      【讨论】:

      • 我所知道的创建一个多边形的唯一方法是poly = MultiPoint(coords).convex_hull。这将创建一个凸包,而不是凹包。
      • shapely 中没有凹壳函数/方法。这里有一个讨论和一些代码:gist.github.com/dwyerk/10561690.
      猜你喜欢
      • 2011-10-13
      • 2019-08-01
      • 1970-01-01
      • 2021-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-25
      • 1970-01-01
      相关资源
      最近更新 更多