多边形邻居列表中的多边形簇

时间:2020-06-12 14:15:26

标签: algorithm 3d polygon mesh

我已经实现了algorithm来为每个多边形生成一个相邻多边形的列表。实现效果很好。

现在,我打算生成一个多边形簇列表。每个群集包含共同的邻居:

Clusters sample

我有点困惑,想出一种算法将常见的邻居合并到集群中。是否有任何标准算法可以做到这一点?

2 个答案:

答案 0 :(得分:1)

这是经典的不相交集union-find问题。联合查找算法及其相关数据结构支持三种操作:

  • MakeSet(p)创建一个包含p的单例集。
  • 查找(p),其中p是宇宙的元素(此处是所有多边形的集合),返回表示包含p的整个集合的“可区分元素(多边形)”。例如,Find(MakeSet(p))= p。
  • Union(p,q)用这两个集合的并集替换包含p和q的集合,以便随后,Find(p)== Find(q)。如果p和q已经在同一集合中,则为空操作。

现在执行以下算法:

for each polygon p
  MakeSet(p)
for each polygon p
  for each polygon q that's a neighbor of p
    Union(p, q)
Let m be a map from polygons to lists of polygons, intitially empty
for each polygon p
  append p to map[Find(p)] 

现在地图中的值(多边形列表)就是您的答案。

具有按等级和折叠查找的并集的并集查找算法本质上是恒定时间(请参阅Wikipedia文章以了解Ackermann逆函数的理论细节),在实践中非常快,并且易于实现。所有的地图操作也是固定时间。

因此,该算法的运行速度(基本上)与多边形输入列表的总和成正比;尽可能快。

答案 1 :(得分:0)

通常使用depth-first searchbreadth-first search来完成。

对于BFS,您可以执行以下操作:

Create an empty queue, and assign all polygons to group -1. Set the current group to 0
While there are any polygons in group -1:
    Randomly grab a polygon which is in group -1 and add it to the queue
    While the queue is not empty:
        Grab the first polygon in the queue and assign it to the current group
        Find all of that polygon's neighbors
            For all of the neighbors, if they are in group -1, add them to the queue
        Remove the selected polygon from the queue
    Increment the current group

此算法完成后,每个多边形将分配给一组连接的组件。