什么是使用boost而不递归遍历Voronoi图边缘的好算法?
我知道它必须检查单元格中的无限边缘,然后检查其邻居并从那里重复,但我更喜欢不需要递归的方法,因为我正在处理大量数据。
这可能没有递归吗?
编辑,以获得更多说明:
这是一种获取所有边缘单元的方法:
voronoi_diagram vd;
boost::polygon::construct_voronoi(in.begin(), in.end(), &vd);
std::vector<const voronoi_diagram::cell_type *> edge_cells;
for(const voronoi_diagram::edge_type & e : vd.edges())
if (e.is_infinite())
edge_cells.push_back(e.cell());
上述方法的问题是它不会以任何特定的顺序遍历边缘单元格,例如顺时针方向。
递归实现会做类似于这个(仓促编写和未经测试的)代码的事情:
bool findNext(const voronoi_diagram::cell_type * c,
std::list<const voronoi_diagram::cell_type *> & list)
{
const voronoi_diagram::edge_type * e = c->incident_edge();
do
{
// Follow infinite edges, adding cells encountered along the way
if (e->is_infinite() && e->twin()->cell() != list.front() &&
e->twin()->cell() != list.back())
{
list.push_back(c);
return findNext(e->twin()->cell(), list);
}
else if (e->twin()->cell() == list.front())
{
list.push_back(c);
return true; // we reached the starting point, return
}
e = e->next();
} while (e != c->incident_edge());
return false;
}
// ...
std::list<const voronoi_diagram::cell_type *> edge_cells;
// ...
for(const voronoi_diagram::edge_type & e : vd.edges())
{
// find first infinite edge
if (e.is_infinite())
{
if (findNext(e.cell(), edge_cells))
break;
else
edge_cells.clear();
}
}
这将遍历 Voronoi 图的边缘,直到它回溯到第一个单元格,然后停止,一路填满堆栈。
非递归实现将对第二个示例进行建模,以顺时针或逆时针顺序生成边缘单元的列表,而不使用递归。