简体   繁体   中英

Clipping lines with CGAL

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;
}

This code is defining a polygon for intersection, not a line:

  Polygon_2 Q; // line
  Q.push_back (Point_2 (0, 15));
  Q.push_back (Point_2 (25, 15)); 

With just two points, this is a degenerate polygon from one point to a second and back.

This image shows the configuration with the two overlapping edges curved and split apart a little so it is clearer what is going on. From that image, you can see why the result was a degenerate polygon with four vertices (from the four intersection points).

Note: when I ran this code, I got the following error message explaining what was going on.

CGAL warning: check violation!
Expression : false
File       : /usr/local/include/CGAL/Boolean_set_operations_2/Gps_polygon_validation.h
Line       : 263
Explanation: The polygon boundary self overlaps.

Searching, the documentation ( here and here ), I couldn't find a Polygon_2-Line_2 intersection function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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