2016-10-25 49 views
0

我正在尝试使用使用CGAL的二维三角测量,并创建一个obj文件。我能够创建一个2D三角测量。我现在想制作第三个坐标0,即z = 0,并从三角测量的结果中创建一个obj文件。 CGAL的样品看起来很混乱,我不知道该怎么做。写一个三角测量结果到CGAL中的OBJ文件

回答

1

这是我做到的。希望它能帮助别人。

// A modifier creating a triangle with the incremental builder. 
     template<class HDS> 
     class polyhedron_builder : public CGAL::Modifier_base<HDS> { 
     public: 
      std::vector<Triangulation>& t_; 
      polyhedron_builder(std::vector<Triangulation>& t) : t_(t) {} 
      void operator()(HDS& hds) { 
       typedef typename HDS::Vertex Vertex; 
       typedef typename Vertex::Point Point3; 

       // create a cgal incremental builder 
       CGAL::Polyhedron_incremental_builder_3<HDS> B(hds, true); 

       // calculte total vertices 
       int face_num = 0; 
       int vertice_num = 0; 
       for (auto& tri : t_) { 
        face_num += tri.number_of_faces(); 
        vertice_num += tri.number_of_vertices(); 
       } 

       std::cout << face_num << ", " << vertice_num << ", " << t_.size() << "\n"; 

       B.begin_surface(face_num, vertice_num); 

       // add the polyhedron vertices 
       for (auto& tri : t_) { 
        for (auto itr = tri.finite_vertices_begin(); itr != tri.finite_vertices_end(); ++itr) { 
         B.add_vertex(Point3(itr->point().x(), itr->point().y(), 0)); 
        } 
       } 


       // add the polyhedron triangles 
       for (auto& tri : t_) { 
        for (auto itr = tri.finite_faces_begin(); itr != tri.finite_faces_end(); ++itr) { 
         B.begin_facet(); 
         B.add_vertex_to_facet(itr->vertex(0)->info()); 
         B.add_vertex_to_facet(itr->vertex(1)->info()); 
         B.add_vertex_to_facet(itr->vertex(2)->info()); 
         B.end_facet(); 
        } 
       } 

       // finish up the surface 
       B.end_surface(); 
      } 
     }; 

     void OBJfile::write_obj_file(const std::string& filename) { 
      CGAL::Polyhedron_3<CGAL::Simple_cartesian<double>> polyhedron; 

      unsigned index = 0; 
      std::vector<Triangulation> t_vector; 

    // here, contours is an internal object that tracks the polygon outlines 
      for (auto& contour : contours_) { 
       Triangulation t; 
       std::vector < std::pair<Point, unsigned> > polygon; 
       for (auto& pt : contour) { 
        Point point(pt.x(), pt.y()); 
        polygon.push_back(std::make_pair(point, index++)); 
       } 
       triangulate(polygon, t); 
       t_vector.push_back(t); 

      } 

       polyhedron_builder<HalfedgeDS> builder(t_vector); 
       polyhedron.delegate(builder); 

      // write the polyhedron out as a .OFF file 
       std::ofstream os("test.obj"); 
       CGAL::File_writer_wavefront writer; 
       CGAL::generic_print_polyhedron(os, polyhedron, writer); 


      os.close(); 

     } 

     void OBJfile::triangulate(const std::vector<std::pair<Point, unsigned>>& polygon_points, Triangulation& t) { 

      auto begin = polygon_points.begin(); 
      auto end = polygon_points.end(); 
      //std::istream_iterator<Point> end; 
      t.insert(begin, end); 

     }