Search code examples
c++meshcgal

CGAL: Read vertices and triangles from mesh


I just spend few hours with CGAL in Visual Studio C++ trying to understand how meshes work. What I want to get is an access to list of vertices and triangles (vertices in form of double[3], triangles in form of int[3]). Here is the script I am working on:

http://doc.cgal.org/latest/Surface_mesher/Surface_mesher_2mesh_a_3d_gray_image_8cpp-example.html

The point is - function CGAL::output_surface_facets_to_off (out, c2t3); outputs me a nice file in .off format (accessible by MeshLab), but I CAN'T DO ANY SIMILAR just by manipulating a c2t3 or tr variable. What I was expecting was something like:

c2t3.vertices (from 0 to N), and c2t3.triangles (from 0 to M) with values of triple of integers. What I've got was list of vertices, list of facets, list of cells, list of edges... and no way to get vertices numbers from facets any other way than finding for every vertice number in nonsorted list of vertices.

Can anybody solve my problem and point what I am doing wrong? Also, API of CGAL is very... raw. Digging with source was also very hardcore - so much I couldn't find output_surface function body.


Solution

  • Okay, I found the answer in Complex_2_in_triangulation_3_file_writer.h file. Apparently CGAL library is creating whole map of vertices and looking for facet vertices in this map. The code part:

    using CGAL::Surface_mesher::number_of_facets_on_surface;
    
    typedef typename C2t3::Triangulation Tr;
    typedef typename Tr::Finite_facets_iterator Finite_facets_iterator;
    typedef typename Tr::Finite_vertices_iterator Finite_vertices_iterator;
    typedef typename Tr::Facet Facet;
    typedef typename Tr::Edge Edge;
    typedef typename Tr::Vertex_handle Vertex_handle;
    
    std::map<Vertex_handle, int> V;
    int inum = 0;
    for(Finite_vertices_iterator vit = tr.finite_vertices_begin();
    vit != tr.finite_vertices_end();
    ++vit)
    {
        V[vit] = inum++;
    }
    
    for( Finite_facets_iterator fit = tr.finite_facets_begin();
    fit != tr.finite_facets_end(); ++fit)
    {
    const typename Tr::Cell_handle cell = fit->first;
    const int& index = fit->second;
        if (cell->is_facet_on_surface(index)==true)
        {
        const int index1 = V[cell->vertex(tr.vertex_triple_index(index, 0))];
        const int index2 = V[cell->vertex(tr.vertex_triple_index(index, 1))];
        const int index3 = V[cell->vertex(tr.vertex_triple_index(index, 2))];
        }
    }