Skip to content

Instantly share code, notes, and snippets.

@jhidding
Created September 15, 2018 15:50
Show Gist options
  • Save jhidding/4a2bc8f451e58d4b9d485b61ba7dedb5 to your computer and use it in GitHub Desktop.
Save jhidding/4a2bc8f451e58d4b9d485b61ba7dedb5 to your computer and use it in GitHub Desktop.
Write a CGAL mesh to wavefront OBJ
/* This takes a C2t3 from example 3.1 in CGAL manual on meshes.
The `vertices_begin()` method returns non-const iterator, so
we pass non-const reference. This should be fixed somehow.
*/
void write_to_obj(std::ostream &out, C2t3 &mesh)
{
// create a map from Vertex_handle to the vertex index as it is
// written into the OBJ file. Remember that OBJ indices start from 1.
std::map<C2t3::Vertex_handle, unsigned> vertex_map;
unsigned count = 1;
// Loop over vertices
for (auto v = mesh.vertices_begin();
v != mesh.vertices_end();
++v) {
// create entry into vertex_map
vertex_map[v] = count;
++count;
// write vertex
auto point = v->point();
out << "v " << point << " 0.0\n";
}
out << "\n";
// Map over facets. Each facet is a cell of the underlying
// Delaunay triangulation, and the vertex that is not part of
// this facet. We iterate all vertices of the cell except the one
// that is opposite.
for (auto f = mesh.facets_begin();
f != mesh.facets_end();
++f) {
out << "f";
for (unsigned j = 0; j < 4; ++j) {
if (j != f->second) {
out << " " << vertex_map[f->first->vertex(j)];
}
}
out << std::endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment