将三角测量结果写入CGAL中的OBJ文件

时间:2016-10-25 05:24:39

标签: cgal

我正在尝试使用CGAL进行2D三角测量,并创建一个obj文件。我能够创建2D三角测量。我现在想要使第3个坐标为0,即z = 0,并从三角测量的结果中创建一个obj文件。 CGAL的样本似乎很混乱,我不知道该怎么做。

1 个答案:

答案 0 :(得分:2)

我是这样做的。希望它可以帮助某人。   

    // 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);

        }