Search code examples
c++c++11cgal

CGAL: can't chose iterator correctly


I build a convex hull 3d in CGAL library from point in stream .txt file and want to insert them into output .txt file by copying. The problem is about to chose iterator for std::copy that can sort out vertices in container Polyhedron_3 poly. Here is my code:

#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/point_generators_3.h>
#include <CGAL/algorithm.h>
#include <CGAL/Polyhedron_3.h>
#include <CGAL/convex_hull_3.h>
#include <vector>
#include <fstream>

typedef CGAL::Exact_predicates_inexact_constructions_kernel  K;
typedef CGAL::Polyhedron_3<K>                     Polyhedron_3;
typedef K::Segment_3                              Segment_3;
// define point creator
typedef K::Point_3                                Point_3;
typedef CGAL::Creator_uniform_3<double, Point_3>  PointCreator;
//a functor computing the plane containing a triangular facet
struct Plane_from_facet {
  Polyhedron_3::Plane_3 operator()(Polyhedron_3::Facet& f) {
      Polyhedron_3::Halfedge_handle h = f.halfedge();
      return Polyhedron_3::Plane_3( h->vertex()->point(),
                                    h->next()->vertex()->point(),
                                    h->opposite()->vertex()->point());
  }
};

using namespace std;

int main()
{

  std::ifstream fin("/Users/Arseniy/Documents/Course Work/Test Input.txt");
  std::ofstream fout("/Users/Arseniy/Documents/Course Work/Test Output.txt");

  CGAL::set_ascii_mode(fin);
  CGAL::set_ascii_mode(fout);

  std::istream_iterator< Point_3 >  in_start(fin );
  std::istream_iterator< Point_3 >  in_end;
  std::ostream_iterator< Point_3 >  out( fout, "\n" );

  std::vector<Point_3> points;
  std::copy(in_start, in_end, std::back_inserter(points));

  Polyhedron_3 poly;

  // compute convex hull of non-collinear points
  CGAL::convex_hull_3(points.begin(), points.end(), poly);

  cout << "The convex hull contains " << poly.size_of_vertices() << " vertices" << std::endl;
  cout << "The convex hull contains " << poly.size_of_facets() << " facets" << std::endl;

  // assign a plane equation to each polyhedron facet using functor Plane_from_facet
  std::transform(poly.facets_begin(), poly.facets_end(), poly.planes_begin(),Plane_from_facet());

  std::copy(poly.vertices_begin(), poly.vertices_end(), ostream_iterator<int>(fout, ", "));

  return 0;
}

I can't to determine type for ostream_iterator<>, it should be vertex_iterator, but its't.

Also i tried to describe

typedef Polyhedron::Vertex_iterator

But it's also a wrong decision.


Solution

  • This should do it:

    std::copy(poly.points_begin(), poly.points_end(), ostream_iterator<Point_3>(fout, ", "));