简体   繁体   English

Boost Geometry:多边形和盒子的交集

[英]Boost Geometry: intersection of a polygon and a box

I've been trying to code an algorithm using the Boost geometry library (trying to code a box counting algorithm if that matters) and part of it consists in handling a polygon. 我一直在尝试使用Boost几何库对算法进行编码(如果需要的话,尝试对盒计数算法进行编码),而其中的一部分在于处理多边形。 Though I'd like to compute the intersection of the boundary of the polygon with a large number of boxes (a meshing), its exterior ring if you prefer. 尽管我想计算多边形边界与大量盒子(网格)的交点,但如果愿意,可以使用其外环。 Strangely the functions intersects(box[i], polygon) intersects(box[i], exterior_ring(polygon)) and within(box[i],polygon) give me the same result. 奇怪的是,函数相交(box [i],多边形)相交(box [i],exterior_ring(多边形))和内(box [i],多边形)给我相同的结果。 For a box completely inside the polygon I should get true, false, true for example. 对于完全位于多边形内部的盒子,我应该得到true,false和true。 For one on the boundary true, true, false. 对于边界上的一个,真,真,假。 How come it doesn't compute it the way I think it should? 为什么它没有按照我认为的方式进行计算?

Your question is about 3 functions: 您的问题是关于3个功能的:

  1. intersects (box, polygon) 相交(框,多边形)
  2. intersects (box, ring) 相交(框,环)
  3. within (box, ring) 内(盒,环)

Let me start from #3. 让我从#3开始。 The function within only supports box-box and box-point input. 该函数只支持箱箱和箱点输入。 It means ring is implicitly converted to its bounding box, and the answers you get are correct (when one box is within another they are considered as intersecting as geometric shapes). 这意味着环被隐式转换为其边界框,并且您得到的答案是正确的(当一个框在另一个框内时,它们被视为与几何形状相交)。

For #2 it seems you want to get "false" even if box is within a ring. 对于#2,即使框在圆环内,您似乎也想获得“ false”。 It means you want to consider ring (contour) as a polyline (aka "linestring"). 这意味着您想将环(轮廓)视为多段线(也称为“线串”)。 You should explain to Boost.Geometry to consider a ring as a linestring. 您应该向Boost.Geometry进行解释,以将环视为线串。

To do so, you can probably "wrap" your contour (or ring) point container into some class and then register this class as a linestring (or as multi-linestring). 为此,您可能可以将轮廓(或环形)点容器“包装”到某个类中,然后将该类注册为线串(或多线串)。 The class itself could be very light-weight, simply keeping a pointer to the container and providing proper const-access. 该类本身可能非常轻巧,只需保持指向容器的指针并提供适当的const访问即可。 You register this class as a linestring using a macro BOOST_GEOMETRY_REGISTER_LINESTRING or BOOST_GEOMETRY_REGISTER_MULTI_LINESTRING . 您可以使用宏BOOST_GEOMETRY_REGISTER_LINESTRINGBOOST_GEOMETRY_REGISTER_MULTI_LINESTRING将其注册为线串。

After that you pass it to "intersects" roughly as intersects(my_linestring_wrapper(polygon), box_view(box[i])) . 之后,将其大致作为intersects(my_linestring_wrapper(polygon), box_view(box[i]))传递给“相交” intersects(my_linestring_wrapper(polygon), box_view(box[i])) Here the code box_view(box[i]) will return a light-weight object which behaves as a "ring" (a contour). 这里的代码box_view(box[i])将返回一个轻量级的对象,该对象的行为类似于“环”(轮廓)。

For #1 you can get box-box or ring-ring intersection. 对于#1,您可以获得Box-Box或Ring-ring相交。 To force the latter, you need to consider the box as a ring. 要强制使用后者,您需要将盒子视为环。 Standard way to achieve this in Boost.Geometry is to apply a proper "view" to the box. 在Boost.Geometry中实现此目的的标准方法是在框上应用适当的“视图”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM