61 template <
class MeshT>
68 using Vertex =
typename Mesh::Vertex;
70 using Face =
typename Mesh::Face;
74 using Faces =
typename Mesh::Faces;
91 read (
const std::string& filename,
Mesh& mesh)
const
93 std::ifstream file (filename.c_str ());
97 std::cerr <<
"Error in MeshIO::read: Could not open the file '" << filename <<
"'\n";
103 unsigned int line_number = 1;
104 int n_v = -1, n_he = -1, n_f = -1;
106 if (!std::getline (file, line) || line !=
"PCL half-edge mesh")
108 std::cerr <<
"Error loading '" << filename <<
"' (line " << line_number <<
"): Wrong file format.\n";
113 if (!std::getline (file, line))
115 std::cerr <<
"Error loading '" << filename <<
"'' (line " << line_number <<
"): Number of vertices / half-edges / faces not found.\n";
119 std::istringstream iss (line);
120 if (!(iss >> n_v >> n_he >> n_f) || iss.good ())
122 std::cerr <<
"Error loading '" << filename <<
"'' (line " << line_number <<
"): Could not read the number of vertices / half-edges / faces.\n";
126 if (n_v < 0 || n_he < 0 || n_f < 0)
128 std::cerr <<
"Error loading '" << filename <<
"'' (line " << line_number <<
"): Invalid number of vertices / half-edges / faces.\n";
135 mesh.vertices_.reserve (n_v);
138 for (
int i=0; i<n_v; ++i, ++line_number)
140 if (!std::getline (file, line))
142 std::cerr <<
"Error loading '" << filename <<
"'' (line " << line_number <<
"): Could not read the line.\n";
146 std::istringstream iss (line);
147 if (!(iss >> idx_ohe) || iss.good ())
149 std::cerr <<
"Error loading '" << filename <<
"'' (line " << line_number <<
"): Could not read the vertex.\n";
152 mesh.vertices_.push_back (
Vertex (idx_ohe));
158 mesh.half_edges_.reserve (n_he);
164 for (
int i=0; i<n_he; ++i, ++line_number)
166 if (!std::getline (file, line))
168 std::cerr <<
"Error loading '" << filename <<
"'' (line " << line_number <<
"): Could not read the line.\n";
172 std::istringstream iss (line);
173 if (!(iss >> idx_tv >> idx_nhe >> idx_phe >> idx_f) || iss.good ())
175 std::cerr <<
"Error loading '" << filename <<
"'' (line " << line_number <<
"): Could not read the half-edge.\n";
178 mesh.half_edges_.push_back (
HalfEdge (idx_tv, idx_nhe, idx_phe, idx_f));
184 mesh.faces_.reserve (n_f);
187 for (
int i=0; i<n_f; ++i, ++line_number)
189 if (!std::getline (file, line))
191 std::cerr <<
"Error loading '" << filename <<
"'' (line " << line_number <<
"): Could not read the line.\n";
195 std::istringstream iss (line);
196 if (!(iss >> idx_ihe) || iss.good ())
198 std::cerr <<
"Error loading '" << filename <<
"'' (line " << line_number <<
"): Could not read the face.\n";
201 mesh.faces_.push_back (
Face (idx_ihe));
206 if (Mesh::HasVertexData::value) mesh.vertex_data_cloud_. resize (n_v);
207 if (Mesh::HasHalfEdgeData::value) mesh.half_edge_data_cloud_.resize (n_he);
208 if (Mesh::HasEdgeData::value) mesh.edge_data_cloud_. resize (n_he / 2);
209 if (Mesh::HasFaceData::value) mesh.face_data_cloud_. resize (n_f);
220 write (
const std::string& filename,
const Mesh& mesh)
const
222 std::ofstream file (filename.c_str ());
225 if (!file.is_open ())
227 std::cerr <<
"Error in MeshIO::write: Could not open the file '" << filename <<
"'\n";
231 file <<
"PCL half-edge mesh\n";
232 file << mesh.sizeVertices () <<
" "
233 << mesh.sizeHalfEdges () <<
" "
234 << mesh.sizeFaces () <<
"\n";
237 for (
typename Vertices::const_iterator it=mesh.vertices_.begin (); it!=mesh.vertices_.end (); ++it)
239 file << it->idx_outgoing_half_edge_ <<
"\n";
243 for (
typename HalfEdges::const_iterator it=mesh.half_edges_.begin (); it!=mesh.half_edges_.end (); ++it)
245 file << it->idx_terminating_vertex_ <<
" "
246 << it->idx_next_half_edge_ <<
" "
247 << it->idx_prev_half_edge_ <<
" "
248 << it->idx_face_ <<
"\n";
252 for (
typename Faces::const_iterator it=mesh.faces_.begin (); it!=mesh.faces_.end (); ++it)
254 file << it->idx_inner_half_edge_ <<
"\n";