Search code examples
c++inputcgal

Input for CGAL classes from a file


Is there an easy way to read points and segments in 2D, from the same file, using CGAL?

What should be the format for this file?


Solution

  • In CGAL operator<< is overloaded for kernel objects and streams. The format is, well, apparently undocumented but kind of obvious for simpler types.

    #include <CGAL/basic.h>
    #include <CGAL/Simple_cartesian.h>
    
    typedef CGAL::Simple_cartesian<double> K;
    
    int main()
    {
      K::Point_2 p;
      std::stringstream ss;
      ss << "2.05 3.05";
    
      ss >> p; // read from a stream
      std::cout << p << std::endl; // write to a stream
    
      K::Segment_2 s;
      ss.clear();
      ss << "2.3 4.2 4.2 2.3";
      ss >> s; // read a segment from a stream
      std::cout << s << std::endl; // write a segment to a stream
      return 0;
    }
    

    Looking at the code a CGAL::Polygon_2 expects input like this:

    "4 0 0 0 1 1 1 1 0"
    

    where the first number is the number of Points following and after that the points.