2

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. 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. For a box completely inside the polygon I should get true, false, true for example. For one on the boundary true, true, false. How come it doesn't compute it the way I think it should?

1 Answer 1

1

Your question is about 3 functions:

  1. intersects (box, polygon)
  2. intersects (box, ring)
  3. within (box, ring)

Let me start from #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. 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.

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. You register this class as a linestring using a macro BOOST_GEOMETRY_REGISTER_LINESTRING or BOOST_GEOMETRY_REGISTER_MULTI_LINESTRING.

After that you pass it to "intersects" roughly as 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).

For #1 you can get box-box or ring-ring intersection. 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.

6
  • Thank you for the contribution I'm seeing it more clearly, in my case your second point is the one I'm trying to implement, though I'm having trouble making use of your identity_view , I added the header #include <boost/geometry/views/identity_view.hpp>
    – Liam
    Nov 11, 2013 at 10:44
  • And using if(boost::geometry::intersects(my_box,boost::geometry::identity_view(boost::gemetry::exterior_ring(my_polygon))) gives me the following compilation error: error: missing template arguments before ‘(’ token Could it be that I need to add a template definition before making use of identity_view ?! There doesn't seem to be any documentation on this function in the boost geometry website.
    – Liam
    Nov 11, 2013 at 10:46
  • 1
    Thanks, I modified the answer to get rid of identity_view and suggested another solution. Nov 11, 2013 at 23:04
  • Thanks for the new suggestion Michael though I implemented something else for now, I'm using the boost::geometry::within function and checking both points of each box: within(my_box.min_corner,my_polygon) XOR within(my_box.max_corner,my_polygon) because for the box to intersect the boundary without being included in most cases we need one corner in and one corner out (my approximation). This is much faster and seems to work quite well for now. Thank you for the help.
    – Liam
    Nov 12, 2013 at 13:57
  • Just in case, your new solution would be to create a class derived from Linestring and add a constructor taking a polygon in argument, building the linestring on the external ring of the polygon?
    – Liam
    Nov 12, 2013 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.