Search code examples
c++cgal

How to create a Polyhedron_3 data structure from list of coordinates and topology in CGAL


I have a list of coordinates (an n-by-3 matrix, where n is the number of points), and a topology connectivity (an m-by-3 matrix, where m is the number of triangles).

I wish to create a CGAL::Polyhedron_3 class in CGAL using this information. How do I achieve that?

Here is my current code:

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iterator>

#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Random.h>
#include <CGAL/Polyhedron_3.h>
#include <CGAL/Polyhedron_items_with_id_3.h>
#include <CGAL/IO/Polyhedron_iostream.h>
#include <CGAL/Surface_mesh_shortest_path.h>
#include <CGAL/boost/graph/graph_traits_Polyhedron_3.h>
#include <CGAL/boost/graph/iterator.h>

typedef CGAL::Exact_predicates_inexact_constructions_kernel           Kernel;
typedef CGAL::Polyhedron_3<Kernel, CGAL::Polyhedron_items_with_id_3>  Polyhedron_3;
typedef Kernel::Point_3                                               Point_3;
typedef Polyhedron_3::HalfedgeDS                                      HalfedgeDS;
typedef Polyhedron_3::Vertex_iterator                                 Vertex_iterator;
typedef Polyhedron_3::Edge_iterator                                   Edge_iterator;
typedef Polyhedron_3::Facet_iterator                                  Facet_iterator;
typedef CGAL::Surface_mesh_shortest_path_traits<Kernel, Polyhedron_3> Traits;
typedef CGAL::Surface_mesh_shortest_path<Traits>                      Surface_mesh_shortest_path;
typedef boost::graph_traits<Polyhedron_3>                             Graph_traits;
typedef Graph_traits::vertex_iterator                                 vertex_iterator;
typedef Graph_traits::face_iterator                                   face_iterator;
typedef Polyhedron_3::Halfedge_around_facet_circulator                Halfedge_facet_circulator;

template <class HDS>
class Build_triangle : public CGAL::Modifier_base<HDS>
{
public:
    Build_triangle() {}
    void operator()(HDS & hds)
    {
        // Postcondition: hds is a valid polyhedral surface.
        CGAL::Polyhedron_incremental_builder_3<HDS> B(hds, true);
        B.begin_surface(5, 4, 16);
        typedef typename HDS::Vertex   Vertex;
        typedef typename Vertex::Point Point;

        B.add_vertex(Point(0.0, 0.0, 0.0));
        B.add_vertex(Point(1.0, 0.0, 0.0));
        B.add_vertex(Point(1.0, 1.0, 0.0));
        B.add_vertex(Point(0.0, 1.0, 0.0));
        B.add_vertex(Point(0.5, 0.5, 0.0));

        B.begin_facet();
        B.add_vertex_to_facet(0);
        B.add_vertex_to_facet(1);
        B.add_vertex_to_facet(4);
        B.end_facet();

        B.begin_facet();
        B.add_vertex_to_facet(1);
        B.add_vertex_to_facet(2);
        B.add_vertex_to_facet(4);
        B.end_facet();

        B.begin_facet();
        B.add_vertex_to_facet(2);
        B.add_vertex_to_facet(3);
        B.add_vertex_to_facet(4);
        B.end_facet();

        B.begin_facet();
        B.add_vertex_to_facet(3);
        B.add_vertex_to_facet(0);
        B.add_vertex_to_facet(4);
        B.end_facet();

        B.end_surface();
    }
};

int main(int argc, char *argv[])
{
    Polyhedron_3 P;
    Build_triangle<HalfedgeDS> triangle;
    P.delegate(triangle);

    CGAL_assertion(P.is_triangle(P.halfedges_begin()));

    return 0;
}

The CGAL_assertion assertion statement fails, which means I fail to generate the triangles correctly. Do I need to change the HalfedgeDS data structure? I have not been able to find a good example on how to do this properly.


Solution

  • According to the documentation the function is_triangle(h) returns true if the connected components of h is a triangle (not if the face is a triangle). You can use the function is_pure_triangle().