简体   繁体   English

Boost Geometry:联合多个多边形 C++

[英]Boost Geometry: Union multiple polygons C++

I have an array of several Boost.Geometry polygons, and I need to union them into a single polygon.我有几个 Boost.Geometry 多边形的数组,我需要将它们合并为一个多边形。 I have successfully implemented something that merges each successive polygon with the union of the previous two (just looping through them and unioning another polygon).我已经成功地实现了将每个连续的多边形与前两个的并集合并的东西(只是遍历它们并合并另一个多边形)。

multi_polygon polygons; // an array of initial polygons
multi_polygon border;   // the unioned polygons

for (polygon p : polygons) {
    // add another polygon each iteration
    multi_polygon tmp_poly;
    union_(border, p, tmp_poly);
    border = tmp_poly;
}

However, this takes quite a long time to execute.然而,这需要相当长的时间来执行。 I heard mention in a video that the assign function could be used for this, but it was not detailed how, and I couldn't find anything else about this.我在视频中听说可以使用assign功能,但没有详细说明如何使用,我找不到关于此的任何其他内容。 How can I speed up this process?我怎样才能加快这个过程?

This is not as efficient as the proposed answer, but I got some significant speedups from doing it this way without having to create a new algorithm from scratch.这不如建议的答案有效,但我通过这种方式获得了一些显着的加速,而无需从头开始创建新算法。

multi_polygon polygons; // an array of initial polygons
multi_polygon border;   // the unioned polygons
multi_polygon tmp_poly; // a temporary variable

for (const polygon &p : polygons) {
    // add another polygon each iteration
    union_(border, p, tmp_poly);
    border = tmp_poly;
    boost::geometry::clear(tmp_poly);

}

No, assign would only blindly add a polygon to a multi-polygon without merging them.不,assign 只会盲目地将多边形添加到多多边形而不合并它们。

Currently in Boost.Geometry (1.73) there is no algorithm for merging multiple polygons into a valid multi-polygon.目前在 Boost.Geometry (1.73) 中没有将多个多边形合并为一个有效的多边形的算法。

You could however speed up your algorithm.但是,您可以加快算法速度。 It makes sense only to merge polygons that are intersecting.只有合并相交的多边形才有意义。 Everything else that is not intersecting could simply be added to the resulting multi-polygon.其他所有不相交的东西都可以简单地添加到生成的多多边形中。 So you could:所以你可以:

  • calculate bounding boxes of all polygons ( boost::geometry::envelope() )计算所有多边形的边界框( boost::geometry::envelope()
  • put them (bounding box + id of a polygon) in the R-tree ( boost::geometry::index::rtree<> )将它们(边界框 + 多边形的 id)放入 R 树( boost::geometry::index::rtree<>
  • merge only those which bounding boxes intersect ( bgi::rtree<>::query() ), keeping the currently merged part (with bg::union_() ), tracking ids of polygons merged with it (eg with some bool flags stored together with polygons in a container) and iteratively expanding the bounding box of a current part ( bg::expand() )仅合并边界框相交的那些( bgi::rtree<>::query() ),保留当前合并的部分(使用bg::union_() ),跟踪与其合并的多边形的 id(例如,存储一些bool标志与容器中的多边形一起)并迭代扩展当前部分的边界框( bg::expand()
  • put all of the non-intersecting (non-mergeable) parts in a single multi-polygon将所有不相交(不可合并)的部分放在一个多多边形中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM