Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CGAL: Read vertices and triangles from mesh

Tags:

c++

mesh

cgal

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.

like image 504
Cheshire Cat Avatar asked Dec 07 '25 06:12

Cheshire Cat


1 Answers

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))];
    }
}
like image 120
Cheshire Cat Avatar answered Dec 08 '25 20:12

Cheshire Cat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!