问题描述
我已经创建与升压几何形状的应用程序,它限定了自己的多边形类型 带有一些名为
的附加数据struct taggedPolygon_t : bg::model::polygon<point_t>
{
taggedPolygon_t() {}
std::string mask_;
};
其中包含一些附加信息。
我想实现的是升压::几何:: union_也处理这个附加 数据。在我的示例案例中,如果成员“掩码”像这样统一就足够了 或类似
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_ 函数的 strategy() 参数是 要走的路,但我被卡住了,因为似乎没有具体的文件 对于这种情况?
解决方法
这经常被要求,可悲的是,由于无法在几何实体(特别是点)中搭载元数据的概念。
点是默认可构造的,并使用访问器函数分配坐标。
这对于一般情况很重要:如果输出几何具有完全不同的类型。是的,特别是对于 union_
,您可能希望某些输入点复制构造,但可惜。
我之前在这里更详细地解释了这一点: