Export to OFF (2D & 3D triangulations)

This commit is contained in:
Clement Jamin 2014-06-16 11:44:06 +02:00
parent 9055e27175
commit 907f7bec01
1 changed files with 86 additions and 3 deletions

View File

@ -33,13 +33,12 @@ std::ostream &
operator<<(std::ostream & os, const typename Wrap::Point_d<K> & p)
{
typename K::Cartesian_const_iterator_d it = p.cartesian_begin();
os << "(" << *it;
os << *it;
++it;
for ( ; it != p.cartesian_end() ; ++it)
{
os << ", " << *it;
os << " " << *it;
}
os << ")";
return os;
}
@ -58,6 +57,90 @@ operator<<(std::ostream & os, const Triangulation_vertex<A, Data, B> & v)
return os;
}*/
template<typename Tr>
std::ostream &
export_triangulation_to_off(std::ostream & os, const Tr & tr)
{
typedef typename Tr::Vertex_const_handle Vertex_handle;
typedef typename Tr::Vertex_const_iterator Vertex_iterator;
typedef typename Tr::Finite_vertex_const_iterator Finite_vertex_iterator;
typedef typename Tr::Full_cell_const_handle Full_cell_handle;
typedef typename Tr::Finite_full_cell_const_iterator Finite_full_cell_iterator;
typedef typename Tr::Full_cell_const_iterator Full_cell_iterator;
typedef typename Tr::Full_cell Full_cell;
typedef typename Full_cell::Vertex_handle_const_iterator Full_cell_vertex_iterator;
if (tr.maximal_dimension() < 2 || tr.maximal_dimension() > 3)
{
std::cerr << "Warning: export_tds_to_off => dimension should be 2 or 3.";
os << "Warning: export_tds_to_off => dimension should be 2 or 3.";
return os;
}
size_t n = tr.number_of_vertices();
std::stringstream output;
// write the vertices
std::map<Vertex_handle, int> index_of_vertex;
int i = 0;
for(Finite_vertex_iterator it = tr.finite_vertices_begin();
it != tr.finite_vertices_end(); ++it, ++i)
{
output << *it;
if (tr.maximal_dimension() == 2)
output << " 0";
output << std::endl;
index_of_vertex[it.base()] = i;
}
CGAL_assertion( i == n );
size_t number_of_triangles = 0;
if (tr.maximal_dimension() == 2)
{
for (Finite_full_cell_iterator fch = tr.finite_full_cells_begin() ;
fch != tr.finite_full_cells_end() ; ++fch)
{
output << "3 ";
for (Full_cell_vertex_iterator vit = fch->vertices_begin() ;
vit != fch->vertices_end() ; ++vit)
{
output << index_of_vertex[*vit] << " ";
}
output << std::endl;
++number_of_triangles;
}
}
else if (tr.maximal_dimension() == 3)
{
// Parse boundary facets
for (Full_cell_iterator fch = tr.full_cells_begin() ;
fch != tr.full_cells_end() ; ++fch)
{
if (tr.is_infinite(fch))
{
output << "3 ";
for (Full_cell_vertex_iterator vit = fch->vertices_begin() ;
vit != fch->vertices_end() ; ++vit, ++i)
{
if (!tr.is_infinite(*vit))
output << index_of_vertex[*vit] << " ";
}
output << std::endl;
++number_of_triangles;
}
}
}
os << "OFF \n"
<< n << " "
<< number_of_triangles << " 0\n"
<< output.str();
return os;
}
} //namespace CGAL
#endif // CGAL_TRIANGULATION_IO_H