这可以通过 Boost Polygon 实现。您需要polygon_set_data::get(),如果您从支持孔的多边形概念转换为不支持孔的多边形概念,它会为您进行孔破裂。请参阅:http://www.boost.org/doc/libs/1_65_0/libs/polygon/doc/gtl_polygon_set_concept.htm 了解更多详情。
下面是一个例子,我们先表示一个带孔的多边形,然后将其转换为只有一个环的简单多边形:
#include <boost/polygon/polygon.hpp>
namespace bp = boost::polygon;
int main(void)
{
using SimplePolygon = bp::polygon_data<int>;
using ComplexPolygon = bp::polygon_with_holes_data<int>;
using Point = bp::point_data<int>;
using PolygonSet = bp::polygon_set_data<int>;
using SimplePolygons = std::vector<bp::polygon_data<int>>;
using namespace boost::polygon::operators;
std::vector<Point> points{{5, 0}, {10, 5}, {5, 10}, {0, 5}};
ComplexPolygon p;
bp::set_points(p, points.begin(), points.end());
{
std::vector<Point> innerPoints{{4, 4}, {6, 4}, {6, 6}, {4, 6}};
std::vector<SimplePolygon> inner(1, SimplePolygon{});
bp::set_points(inner.front(), innerPoints.begin(), innerPoints.end());
bp::set_holes(p, inner.begin(), inner.end());
}
PolygonSet complexPolygons;
complexPolygons += p;
SimplePolygons simplePolygons;
complexPolygons.get<SimplePolygons>(simplePolygons);
std::cout << "Fractured:\n";
for (const auto& polygon : simplePolygons)
{
for (const Point& p : polygon)
{
std::cout << '\t' << std::to_string(p.x()) << ", " << std::to_string(p.y())
<< '\n';
}
}
return 0;
}