如何使用CGAL轻松检索两条相交多边形的相交线

文章讲述了如何利用CGAL库来查找两个相交多边形之间的交线,从第一个交点到最后一个交点。作者目前的实现是遍历交集多边形的边界点,但寻求更优方法。一种建议是将多边形线段插入2D排列中,找出度为4的顶点来标识交点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如何使用CGAL轻松检索两条相交多边形的相交线(从第一个交点到最后一个交点)。看到图像的澄清,绿线是我想要的。使用CGAL获取多边形相交线

Two intersecting polygons with intersection line

目前我使用下面的算法,在那里我得到的交集多边形,然后发现这是两个多边形的边界点,这应该是交叉点。这里是代码:

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

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无关。)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lst0426

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值