如何扩展 boost geometry union_ 策略来处理带有附加数据的自己的多边形类型?

How to extend boost geometry union_ strategy to handle own polygon type with additional data?

我创建了一个带有 boost geometry 的应用程序,它定义了自己的多边形类型 一些额外的数据称为

struct taggedPolygon_t : bg::model::polygon<point_t>
{
    taggedPolygon_t() {}    
    std::string mask_;
};

其中包含一些附加信息。

我想要实现的是 boost::geometry::union_ 也处理这个额外的 数据。在我的示例中,如果成员“mask”像这样统一就足够了 或类似

resultPoly.mask = poly1.mask + poly2.mask;

我试图创建一个示例应用程序 - 但它在 Coliru 的限制下。

我想大家应该明白了吧?

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/algorithms/area.hpp>
#include <iostream>
namespace bg = boost::geometry;
using point_t = bg::model::d2::point_xy<double>;

struct taggedPolygon_t : bg::model::polygon<point_t>
{
    taggedPolygon_t() {}    
    std::string mask_;
};

namespace boost::geometry::traits
{
    template<> struct tag<taggedPolygon_t> { typedef polygon_tag type; };
    template<> struct ring_const_type<taggedPolygon_t> { typedef const bg::model::polygon<point_t>::ring_type& type; };
    template<> struct ring_mutable_type<taggedPolygon_t> { typedef bg::model::polygon<point_t>::ring_type& type; };
    template<> struct interior_const_type<taggedPolygon_t> { typedef const bg::model::polygon<point_t>::inner_container_type& type; };
    template<> struct interior_mutable_type<taggedPolygon_t> { typedef bg::model::polygon<point_t>::inner_container_type& type; };

    template<> struct exterior_ring<taggedPolygon_t>
    {
        static bg::model::polygon<point_t>::ring_type& get(bg::model::polygon<point_t>& p) {return p.outer(); }
        static bg::model::polygon<point_t>::ring_type const& get(bg::model::polygon<point_t> const& p) {return p.outer(); }
    };

    template<> struct interior_rings<taggedPolygon_t>
    {
        static bg::model::polygon<point_t>::inner_container_type& get(bg::model::polygon<point_t>& p) {return p.inners(); }
        static bg::model::polygon<point_t>::inner_container_type const& get(bg::model::polygon<point_t> const& p) {return p.inners(); }
    };
} // namespace boost::geometry::traits

using multiTaggedPolygon_t = bg::model::multi_polygon<taggedPolygon_t>;

int main() {
    multiTaggedPolygon_t polygons;
    bg::read_wkt("MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)), "
        "((20 35, 10 30, 10 10, 30 5, 45 20, 20 35),"
        "(30 20, 20 15, 20 25, 30 20)))", polygons);

    std::string reason;
    if (!bg::is_valid(polygons, reason)) {
        std::cout << "Correcting data: " << reason << "\n";
        bg::correct(polygons);
    }
    
    multiTaggedPolygon_t separated(std::move(polygons));
    
    multiTaggedPolygon_t newPolygons;
    for (auto& poly : separated)
    {
        // Unify polygons
        bg::union_(polygons, poly, newPolygons);
        std::swap(polygons, newPolygons);
        newPolygons.clear();
    }

    for (auto& p : polygons)
        std::cout << p.mask_ << std::endl;
}

http://coliru.stacked-crooked.com/a/ff15c25e4d672ebd

我认为 union_ 函数的 strategies() 参数应该是 要走的路,但我被困住了,因为似乎没有具体的文件 对于这种情况?

这经常被要求,可悲的是由于概念没有办法在几何实体(特别是点)中搭载元数据。

点是默认可构造的,并使用访问函数分配坐标。

这对于一般情况很重要:如果输出几何具有完全不同的类型。是的,特别是对于 union_,您可能希望某些输入点是 复制构造的 ,但是唉。

我之前在这里更详细地解释了这一点:

  • Losing data in custom point type after boost buffer call