如何使用CGAL轻松检索两条相交多边形的相交线(从第一个交点到最后一个交点)。看到图像的澄清,绿线是我想要的。使用CGAL获取多边形相交线
Two intersecting polygons with intersection line
目前我使用下面的算法,在那里我得到的交集多边形,然后发现这是两个多边形的边界点,这应该是交叉点。这里是代码:
代码语言:javascript复制Polygon_2 P,Q;
Pwh_list_2 intR;
Pwh_list_2::const_iterator it;
CGAL::intersection(P, Q, std::back_inserter(intR));
//Loop through intersection polygons
for (it = intR.begin(); it != intR.end(); it) {
boost::numeric::ublas::vector<double> firstIntersectPoint(3), lastIntersectPoint(3);
Polygon_2 Overlap = it->outer_boundary();
typename CGAL::Polygon_2<Kernel>::Vertex_iterator vit;
int pointNr = 1;
//Loop through points of intersection polygon to find first and last intersection point.
for (vit = Overlap.vertices_begin(); vit != Overlap.vertices_end(); vit) {
CGAL::Bounded_side bsideThis = P.bounded_side(*vit);
CGAL::Bounded_side bsideArg = Q.bounded_side(*vit);
if (bsideThis == CGAL::ON_BOUNDARY && bsideArg == CGAL::ON_BOUNDARY && pointNr == 1) {
firstIntersectPoint <<= 0, CGAL::to_double(vit->x()), CGAL::to_double(vit->y());
pointNr = 2;
}
else if (bsideThis == CGAL::ON_BOUNDARY && bsideArg == CGAL::ON_BOUNDARY && pointNr == 2) {
lastIntersectPoint <<= 0, CGAL::to_double(vit->x()), CGAL::to_double(vit->y());
pointNr = 2;
}
}
//RESULT
std::cout << firstIntersectPoint << std::endl;
std::cout << lastIntersectPoint << std::endl;
}
虽然这个作品,我不认为这是正确的方式去。有人可以告诉我这是否是正确的方法,或者指出如何更好地做到这一点。
来源
2017-08-02 D.J. Klomp A 回答 2 将两个多边形的线段插入到2D排列中。然后找到具有度4的顶点。注意,在一般情况下可能有2个以上;有可能是没有,有可能是1
代码语言:javascript复制std::list<X_monotone_curve_2> segments;
for (const auto& pgn : polygons) {
for (auto it = pgn.curves_begin(); it != pgn.curves_end(); it)
segments.push_back(*it);
}
Arrangement_2 arr;
insert(arr, segments.begin(), segments.end());
for (auto it = arr.begin_vertices(); it != arr.end_vertices(); it) {
if (4 == it->degree())
...
}
可以避开“段”名单的建设,而是直接将多边形细分成使用迭代器适配器的安排。 (这是纯粹的通用编程,与CGAL无关。)