[Polygon] using polygon for linesegment intersection

Hello all, I was trying to use the Boost.Polygon library to test for line segment intersection. I define 2 polygons (lines) and use the Boolean operators of the polygon_set. However it gives an empty result: void TestBoostPolygonIntersectLineSegment() { using namespace boost::polygon::operators; typedef boost::polygon::polygon_data<int> Polygon; typedef boost::polygon::point_data<int> Point; typedef boost::polygon::polygon_set_data<int> PolygonSet; typedef std::vector<Polygon> PolygonVector; Point aPts1[] = { Point(0, 0), Point(10, 0) }; Point aPts2[] = { Point(5, 5), Point(5, -5) }; Polygon line1(aPts1, aPts1 + count_array(aPts1)); Polygon line2(aPts2, aPts2 + count_array(aPts2)); //both share point (5, 0) assert(boost::polygon::contains(line1, Point(5, 0))); assert(boost::polygon::contains(line2, Point(5, 0))); //eh..., expected it to be 1, but anyway assert(boost::polygon::size(line1) == 2); assert(boost::polygon::size(line2) == 2); PolygonSet ps1; PolygonSet ps2; ps1.insert(line1); ps2.insert(line2); PolygonSet ps3 = ps1 & ps2; //gives no result if (!ps3.empty()) //<--- { PolygonVector vec1; ps3.get(vec1); } } Also the documentation states that the size function gives the number of edges, but a line has only 1 edge: assert(boost::polygon::size(line1) == 2); //not 1?

gast128 wrote:
I was trying to use the Boost.Polygon library to test for line segment intersection. I define 2 polygons (lines) and use the Boolean operators of the polygon_set. However it gives an empty result:
A line segment does not a polygon make. There is an implied edge between the first and last point in a polygon (you can also make it explicit so that the first and last point are the same and the library will accept that also). In the case of two point the implied edge is the reverse of the edge you specified. The result is a figure that encloses no area. A polygon that encloses no area dissapears when it passes through a boolean operation. That is why your output is empty. The definition of a polygon is the area inside the boundary. The boundary itself is neither inside or outside, it is the boundary. That is why polygons that touch at the boundary are merged because their interiors touch along the common boundary.
Also the documentation states that the size function gives the number of edges, but a line has only 1 edge:
assert(boost::polygon::size(line1) == 2); //not 1?
The size function actually returns the number of points stored in the polygon. This may or may not be the number of edges of the polygon. It would be quite a bit more complicated to answer the question "given the points stored in the polygon currently, how many edges would the enclosed area have?" In your case it would be zero.
Internally Boost.Polygon has an implementation of line segment intersection, but its interface is an implementation detail and not a documented API. I want (and need) the ability to change the interface of this algorithm in the future. As it happens we are about to start a Google Summer of Code project to add line_segment and line_segment_set concepts to the Polygon API. This will allow you do directly get the line segment intersections by calling intersect on a set of line segments and getting the result back as pairs of intersecting segments populating a vector, for example. However, it will be at least three months before we can let people start using a beta version of the new API.
To directly do what you want with the current library you can access implementation details used by the Booleans algorithm for general polygons.
If you look in the file details/scan_arbitrary.hpp you will find a function definition:
template <typename iT>
static inline void validate_scan_divide_and_conquer(std::vector

ah thx for your extensive answer. It turns thus out that I didn't understand the concepts of a (closed) polygon very well. For now i 've implemented the line segment intersection myself and will probably replace it by Boost.Polygon when above gets introduced the official way.
participants (2)
-
gast128
-
Simonson, Lucanus J