Add implementation for operator << and >> (without normal)

This commit is contained in:
Andreas Fabri 2014-10-06 21:36:55 +02:00
parent 9097a6931e
commit 1d3f1d5545
3 changed files with 71 additions and 23 deletions

View File

@ -14,6 +14,10 @@ int main()
{ {
Mesh m; Mesh m;
std::cin >> m;
std::cout << m << std::endl;
m.clear();
vertex_descriptor v0 = m.add_vertex(K::Point_3(0,2,0)); vertex_descriptor v0 = m.add_vertex(K::Point_3(0,2,0));
vertex_descriptor v1 = m.add_vertex(K::Point_3(2,2,0)); vertex_descriptor v1 = m.add_vertex(K::Point_3(2,2,0));
vertex_descriptor v2 = m.add_vertex(K::Point_3(0,0,0)); vertex_descriptor v2 = m.add_vertex(K::Point_3(0,0,0));
@ -25,7 +29,6 @@ int main()
m.add_face(v2, v3, v4); m.add_face(v2, v3, v4);
// give each vertex a name, the default is empty // give each vertex a name, the default is empty
Mesh::Property_map<vertex_descriptor,std::string> name; Mesh::Property_map<vertex_descriptor,std::string> name;
bool created; bool created;
@ -59,6 +62,8 @@ int main()
// delete the string property again // delete the string property again
m.remove_property_map(name); m.remove_property_map(name);
std::cout << m <<std::endl;
return 0; return 0;
} }

View File

@ -66,13 +66,13 @@ bool read_off_binary(Surface_mesh<Point_3>& mesh,
Point_3 p; Point_3 p;
Vector_3 n, c; Vector_3 n, c;
Vector_2 t; Vector_2 t;
typename Mesh::Vertex_descriptor v; typename Mesh::Vertex_index v;
// properties // properties
Property_map<typename Mesh::Vertex_descriptor, Normal> normals; Mesh::Property_map<typename Mesh::Vertex_index, Normal> normals;
Property_map<typename Mesh::Vertex_descriptor, Texture_coordinate> texcoords; Mesh::Property_map<typename Mesh::Vertex_index, Texture_coordinate> texcoords;
if (has_normals) normals = mesh.template get_property_map<typename Mesh::Vertex_descriptor, Normal>("v:normal"); if (has_normals) normals = mesh.template get_or_add_property_map<typename Mesh::Vertex_index, Normal>("v:normal");
if (has_texcoords) texcoords = mesh.template get_property_map<typename Mesh::Vertex_descriptor, Texture_coordinate>("v:texcoord"); if (has_texcoords) texcoords = mesh.template get_or_add_property_map<typename Mesh::Vertex_index, Texture_coordinate>("v:texcoord");
// #Vertice, #Faces, #Edges // #Vertice, #Faces, #Edges
@ -107,7 +107,7 @@ bool read_off_binary(Surface_mesh<Point_3>& mesh,
// read faces: #N v[1] v[2] ... v[n-1] // read faces: #N v[1] v[2] ... v[n-1]
std::vector<typename Mesh::Vertex_descriptor> vertices; std::vector<typename Mesh::Vertex_index> vertices;
for (i=0; i<nF; ++i) for (i=0; i<nF; ++i)
{ {
internal::read(in, nV); internal::read(in, nV);
@ -115,7 +115,7 @@ bool read_off_binary(Surface_mesh<Point_3>& mesh,
for (j=0; j<nV; ++j) for (j=0; j<nV; ++j)
{ {
internal::read(in, idx); internal::read(in, idx);
vertices[j] = typename Mesh::Vertex_descriptor(idx); vertices[j] = typename Mesh::Vertex_index(idx);
} }
if(!mesh.add_face(vertices).is_valid()) { if(!mesh.add_face(vertices).is_valid()) {
// adding a face did not succeed, stop reading the rest // adding a face did not succeed, stop reading the rest
@ -142,14 +142,14 @@ bool read_off_ascii(Surface_mesh<Point_3>& mesh,
char line[100], *lp; char line[100], *lp;
unsigned int i, j, items, idx, nc; unsigned int i, j, items, idx, nc;
unsigned int nV, nF, nE; unsigned int nV, nF, nE;
typename Mesh::Vertex_descriptor v; typename Mesh::Vertex_index v;
// properties // properties
Property_map<typename Mesh::Vertex_descriptor, Normal> normals; Mesh::Property_map<typename Mesh::Vertex_index, Normal> normals;
Property_map<typename Mesh::Vertex_descriptor, Texture_coordinate> texcoords; Mesh::Property_map<typename Mesh::Vertex_index, Texture_coordinate> texcoords;
if (has_normals) normals = mesh.template get_property_map<typename Mesh::Vertex_descriptor, Normal>("v:normal"); if (has_normals) normals = mesh.template get_or_add_property_map<typename Mesh::Vertex_index, Normal>("v:normal");
if (has_texcoords) texcoords = mesh.template get_property_map<typename Mesh::Vertex_descriptor, Texture_coordinate>("v:texcoord"); if (has_texcoords) texcoords = mesh.template get_or_add_property_map<typename Mesh::Vertex_index, Texture_coordinate>("v:texcoord");
int c; int c;
do { do {
@ -206,7 +206,7 @@ bool read_off_ascii(Surface_mesh<Point_3>& mesh,
} }
// read faces: #N v[1] v[2] ... v[n-1] // read faces: #N v[1] v[2] ... v[n-1]
std::vector<typename Mesh::Vertex_descriptor> vertices; std::vector<typename Mesh::Vertex_index> vertices;
for (i=0; i<nF; ++i) for (i=0; i<nF; ++i)
{ {
// read line // read line
@ -228,7 +228,7 @@ bool read_off_ascii(Surface_mesh<Point_3>& mesh,
{ {
items = sscanf(lp, "%d%n", (int*)&idx, &nc); items = sscanf(lp, "%d%n", (int*)&idx, &nc);
CGAL_assertion(items == 1); CGAL_assertion(items == 1);
vertices[j] = typename Mesh::Vertex_descriptor(idx); vertices[j] = typename Mesh::Vertex_index(idx);
lp += nc; lp += nc;
} }
@ -343,8 +343,8 @@ bool write_off(const Surface_mesh<K>& mesh, const std::string& filename)
// vertices // vertices
Property_map<typename Mesh::Vertex_descriptor, Point_3> points Mesh::Property_map<typename Mesh::Vertex_index, Point_3> points
= mesh.template get_property_map<typename Mesh::Vertex_descriptor, Point_3>("v:point"); = mesh.template property_map<typename Mesh::Vertex_index, Point_3>("v:point");
for (typename Mesh::Vertex_iterator vit=mesh.vertices_begin(); vit!=mesh.vertices_end(); ++vit) for (typename Mesh::Vertex_iterator vit=mesh.vertices_begin(); vit!=mesh.vertices_end(); ++vit)
{ {
const Point_3& p = points[*vit]; const Point_3& p = points[*vit];

View File

@ -1955,8 +1955,6 @@ private: //--------------------------------------------------- property handling
} }
/// @cond CGAL_DOCUMENT_INTERNALS
/// returns a property map with key `I` and value type `T` with /// returns a property map with key `I` and value type `T` with
/// `name`. If no such property exists one is added with the default /// `name`. If no such property exists one is added with the default
/// value `t`. /// value `t`.
@ -1966,7 +1964,7 @@ private: //--------------------------------------------------- property handling
{ {
return (this->*boost::fusion::at_key<I>(pmap_)).template get_or_add<T>(name, t); return (this->*boost::fusion::at_key<I>(pmap_)).template get_or_add<T>(name, t);
} }
/// @endcond
/// removes property map `p`. The memory allocated for that property map is /// removes property map `p`. The memory allocated for that property map is
/// freed. /// freed.
@ -2091,8 +2089,28 @@ private: //------------------------------------------------------- private data
template <typename P> template <typename P>
std::ostream& operator<<(std::ostream& os, const Surface_mesh<P>& sm) std::ostream& operator<<(std::ostream& os, const Surface_mesh<P>& sm)
{ {
typedef Surface_mesh<P> Mesh;
typedef typename Mesh::Vertex_index Vertex_index;
typedef typename Mesh::Face_index Face_index;
os << "OFF\n" << sm.num_vertices() << " " << sm.num_faces() << " 0\n";
std::vector<int> reindex;
reindex.resize(sm.num_vertices());
int n = 0;
BOOST_FOREACH(Vertex_index v, sm.vertices()){
os << sm.point(v) << std::endl;
reindex[v]=n++;
}
BOOST_FOREACH(Face_index f, sm.faces()){
os << sm.degree(f);
BOOST_FOREACH(Vertex_index v, CGAL::vertices_around_face(sm.halfedge(f),sm)){
os << " " << reindex[v];
}
os << "\n";
}
return os; return os;
} }
/// \relates Surface_mesh /// \relates Surface_mesh
/// Extracts the surface mesh from an input stream in Ascii OFF format. /// Extracts the surface mesh from an input stream in Ascii OFF format.
/// If the vertices have the property "v:normal" it is also extracted from the stream. /// If the vertices have the property "v:normal" it is also extracted from the stream.
@ -2100,6 +2118,32 @@ private: //------------------------------------------------------- private data
template <typename P> template <typename P>
std::istream& operator>>(std::istream& is, Surface_mesh<P>& sm) std::istream& operator>>(std::istream& is, Surface_mesh<P>& sm)
{ {
typedef Surface_mesh<P> Mesh;
typedef typename Mesh::Vertex_index Vertex_index;
typedef typename Mesh::Face_index Face_index;
typedef typename Mesh::size_type size_type;
sm.clear();
int n, f, e;
std::string off;
is >> off;
assert(off == "OFF");
is >> n >> f >> e;
sm.reserve(n,2*f,e);
P p;
for(int i=0; i < n; i++){
is >> p;
sm.add_vertex(p);
}
std::vector<size_type> vr;
std::size_t d;
for(std::size_t i=0; i < f; i++){
is >> d;
vr.resize(d);
for(std::size_t j=0; j<d; j++){
is >> vr[j];
}
sm.add_face(vr);
}
return is; return is;
} }
@ -2363,12 +2407,11 @@ Surface_mesh<P>::add_face(const Range& r)
// test for topological errors // test for topological errors
for (i=0, ii=1; i<n; ++i, ++ii, ii%=n) for (i=0, ii=1; i<n; ++i, ++ii, ii%=n)
{ {
if ( !is_border(vertices[i]) ) if ( !(is_isolated(vertices[i]) || is_border(vertices[i]) ) )
{ {
std::cerr << "Surface_meshT::add_face: complex vertex " << vertices[i] << std::endl; std::cerr << "Surface_meshT::add_face: complex vertex " << vertices[i] << std::endl;
return Face_index(); return Face_index();
} }
halfedges[i] = halfedge(vertices[i], vertices[ii]); halfedges[i] = halfedge(vertices[i], vertices[ii]);
is_new[i] = !halfedges[i].is_valid(); is_new[i] = !halfedges[i].is_valid();
if (!is_new[i] && !is_border(halfedges[i])) if (!is_new[i] && !is_border(halfedges[i]))