如何在增强几何中适应自定义多边形类型

How to adapt a custom polygon type in boost geometry

我正在尝试将增强几何算法与我自己的自定义多边形类型结合使用。但是我收到编译器错误(在 Visual Studio 2019 Windows 10)。

我已经将我正在尝试做的事情简化为以下代码。

在my_custom_polygon.hpp

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/geometries.hpp>

using point = boost::geometry::model::d2::point_xy<double>;
using ring = boost::geometry::model::ring<point>;

struct MyPoly
{
    ring exteriorRing;
    std::vector<ring> interiorRings;
};

using polygon = MyPoly;
using multipolygon = std::vector<MyPoly>;
//using polygon = boost::geometry::model::polygon<point>;
//using multipolygon = boost::geometry::model::multi_polygon<polygon>;

namespace boost::geometry::traits
{
    template<> struct tag<MyPoly> { using type = polygon_tag; };
    template<> struct tag<std::vector<MyPoly>> { using type = multi_polygon_tag; };
    template<> struct ring_const_type<MyPoly> { using type = const ring; };
    template<> struct ring_mutable_type<MyPoly> { using type = ring; };
    template<> struct interior_const_type<MyPoly> { using type = const std::vector<ring>; };
    template<> struct interior_mutable_type<MyPoly> { using type = std::vector<ring>; };

    template<> struct exterior_ring<MyPoly>
    {
        static ring& get(MyPoly& poly) { return poly.exteriorRing; }
        static const ring& get(const MyPoly& poly) { return poly.exteriorRing; }
    };

    template<> struct interior_rings<MyPoly>
    {
        static std::vector<ring>& get(MyPoly& poly) { return poly.interiorRings; }
        static const std::vector<ring>& get(const MyPoly& poly) { return poly.interiorRings; }
    };
}

在my_custom_polygon.cpp

int main(int argc, const char** argv)
{
    const double buffer_distance = 1.0;
    const int points_per_circle = 36;
    boost::geometry::strategy::buffer::distance_symmetric<double> distance_strategy(buffer_distance);
    boost::geometry::strategy::buffer::join_round join_strategy(points_per_circle);
    boost::geometry::strategy::buffer::end_round end_strategy(points_per_circle);
    boost::geometry::strategy::buffer::point_circle circle_strategy(points_per_circle);
    boost::geometry::strategy::buffer::side_straight side_strategy;

    multipolygon result;

    point p{0.0, 0.0};

    boost::geometry::buffer(p, result,
            distance_strategy, side_strategy,
            join_strategy, end_strategy, circle_strategy);

    return 0
}

编译失败,第 70 行 boost/geometry/algorithms/detail/overlay/convert_ring.hpp 中出现错误 C2664

它说它不能将参数 2 从 'boost::geometry::model::ring<point,true,true,std::vector,std::allocator>' 转换为 'Geometry2 &'

但是,如果我将 .hpp 文件中注释掉的行用于多边形和多多边形类型,它就可以正常编译和运行。

我显然没有正确调整多边形。

有人有什么想法吗?

谢谢

阅读标题的第一个念头......哦,男孩:我们又来了:)

摆弄了一会儿(稍微清理了一下),发现原因是ring_{mutable,const}_type::type这个算法需要引用

This is making me think that earlier adaptations I made were less-than-optimal and leading to unnecessary ring copying, see e.g. Further problems in adapting a geometry object model using boost geometry and (How to) Create own polygon type in boost geometry and use multi_polygon type with it?

所以,事不宜迟:

Live On Coliru

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <iostream>

namespace bg = boost::geometry;

using Point = bg::model::d2::point_xy<double>;
using Ring  = bg::model::ring<Point>;
using Rings = std::vector<Ring>;

struct MyPoly {
    Ring  exteriorRing;
    Rings interiorRings;
};

using MyMultiPoly = std::vector<MyPoly>;

namespace boost::geometry::traits {
    template <> struct tag<MyPoly>                   { using type = polygon_tag;  };
    template <> struct ring_mutable_type<MyPoly>     { using type = Ring&;        };
    template <> struct ring_const_type<MyPoly>       { using type = const Ring&;  };
    template <> struct interior_mutable_type<MyPoly> { using type = Rings;        };
    template <> struct interior_const_type<MyPoly>   { using type = const Rings;  };

    template<> struct exterior_ring<MyPoly> {
        static auto& get(MyPoly& poly)       { return poly.exteriorRing; } 
        static auto& get(const MyPoly& poly) { return poly.exteriorRing; } 
    };

    template<> struct interior_rings<MyPoly> {
        static auto& get(MyPoly& poly)       { return poly.interiorRings; } 
        static auto& get(const MyPoly& poly) { return poly.interiorRings; } 
    };
} // namespace boost::geometry::traits

namespace boost::geometry::traits {
    template <> struct tag<MyMultiPoly> { using type = multi_polygon_tag; };
} // namespace boost::geometry::traits

int main() {
    MyMultiPoly result;
    Point p{0.0, 0.0};

    namespace bs = bg::strategy::buffer;
    const double buffer_distance   = 1.0;
    const int    points_per_circle = 36;

    bs::distance_symmetric<double> distance(buffer_distance);
    bs::join_round                 join(points_per_circle);
    bs::end_round                  end(points_per_circle);
    bs::point_circle               circle(points_per_circle);
    bs::side_straight              side;

    bg::buffer(p, result, distance, side, join, end, circle);

    std::cout << "result: " << bg::wkt(result) << "\n";
}

版画

result: MULTIPOLYGON(((1 0,0.984808 -0.173648,0.939693 -0.34202,0.866025 -0.5,0.766044 -0.642788,0.642788 -0.766044,0.5 -0.866025,0.34202 -0.939693,0.173648 -0.984808,6.12323e-17 -1,-0.173648 -0.984808,-0.34202 -0.939693,-0.5 -0.866025,-0.642788 -0.766044,-0.766044 -0.642788,-0.866025 -0.5,-0.939693 -0.34202,-0.984808 -0.173648,-1 -1.45473e-15,-0.984808 0.173648,-0.939693 0.34202,-0.866025 0.5,-0.766044 0.642788,-0.642788 0.766044,-0.5 0.866025,-0.34202 0.939693,-0.173648 0.984808,-2.84823e-15 1,0.173648 0.984808,0.34202 0.939693,0.5 0.866025,0.642788 0.766044,0.766044 0.642788,0.866025 0.5,0.939693 0.34202,0.984808 0.173648,1 0)))