What I am trying to achieve is to get the intersection between line and a set of polygons with holes -> clip the lines by mask (set of polygons) -> result would be another lines. The question at CGAL: Intersection between a segment and a polygon? suggests using Polygon with two points to represent the line. With the help of the CGAL samples I came up with following snippet. My intention was to calculate part of the line which lies inside rectangle using intersection. However, result has 4 points, and it seems to be calculating intersection between polygon and a half-plane defined by the line.
Can anyone shed some light on this, please?
typedef CGAL::Exact_predicates_exact_constructions_kernel Kernel;
typedef Kernel::Point_2 Point_2;
typedef CGAL::Polygon_2<Kernel> Polygon_2;
typedef CGAL::Polygon_with_holes_2<Kernel> Polygon_with_holes_2;
typedef std::list<Polygon_with_holes_2> Pwh_list_2;
int main()
{
Polygon_2 P; // rectangle
P.push_back (Point_2 (10, 10));
P.push_back (Point_2 (20, 10));
P.push_back (Point_2 (20, 20));
P.push_back (Point_2 (10, 20));
Polygon_2 Q; // line
Q.push_back (Point_2 (0, 15));
Q.push_back (Point_2 (25, 15));
Pwh_list_2 symmR;
Pwh_list_2::const_iterator it;
CGAL::intersection (Q, P, std::back_inserter(symmR));
for (it = symmR.begin(); it != symmR.end(); ++it) {
std::cout << "--> ";
print_polygon_with_holes( *it);
}
getchar();
return 0;
}