41 #ifndef PCL_GEOMETRY_MESH_IO_H 42 #define PCL_GEOMETRY_MESH_IO_H 62 template <
class MeshT>
69 typedef typename Mesh::Vertex
Vertex;
71 typedef typename Mesh::Face
Face;
75 typedef typename Mesh::Faces
Faces;
92 read (
const std::string& filename,
Mesh& mesh)
const 94 std::ifstream file (filename.c_str ());
98 std::cerr <<
"Error in MeshIO::read: Could not open the file '" << filename <<
"'\n";
104 unsigned int line_number = 1;
105 int n_v = -1, n_he = -1, n_f = -1;
107 if (!std::getline (file, line) || line.compare (
"PCL half-edge mesh") != 0)
109 std::cerr <<
"Error loading '" << filename <<
"' (line " << line_number <<
"): Wrong file format.\n";
114 if (!std::getline (file, line))
116 std::cerr <<
"Error loading '" << filename <<
"'' (line " << line_number <<
"): Number of vertices / half-edges / faces not found.\n";
120 std::istringstream iss (line);
121 if (!(iss >> n_v >> n_he >> n_f) || iss.good ())
123 std::cerr <<
"Error loading '" << filename <<
"'' (line " << line_number <<
"): Could not read the number of vertices / half-edges / faces.\n";
127 if (n_v < 0 || n_he < 0 || n_f < 0)
129 std::cerr <<
"Error loading '" << filename <<
"'' (line " << line_number <<
"): Invalid number of vertices / half-edges / faces.\n";
136 mesh.vertices_.reserve (n_v);
139 for (
int i=0; i<n_v; ++i, ++line_number)
141 if (!std::getline (file, line))
143 std::cerr <<
"Error loading '" << filename <<
"'' (line " << line_number <<
"): Could not read the line.\n";
147 std::istringstream iss (line);
148 if (!(iss >> idx_ohe) || iss.good ())
150 std::cerr <<
"Error loading '" << filename <<
"'' (line " << line_number <<
"): Could not read the vertex.\n";
153 mesh.vertices_.push_back (
Vertex (idx_ohe));
159 mesh.half_edges_.reserve (n_he);
165 for (
int i=0; i<n_he; ++i, ++line_number)
167 if (!std::getline (file, line))
169 std::cerr <<
"Error loading '" << filename <<
"'' (line " << line_number <<
"): Could not read the line.\n";
173 std::istringstream iss (line);
174 if (!(iss >> idx_tv >> idx_nhe >> idx_phe >> idx_f) || iss.good ())
176 std::cerr <<
"Error loading '" << filename <<
"'' (line " << line_number <<
"): Could not read the half-edge.\n";
179 mesh.half_edges_.push_back (
HalfEdge (idx_tv, idx_nhe, idx_phe, idx_f));
185 mesh.faces_.reserve (n_f);
188 for (
int i=0; i<n_f; ++i, ++line_number)
190 if (!std::getline (file, line))
192 std::cerr <<
"Error loading '" << filename <<
"'' (line " << line_number <<
"): Could not read the line.\n";
196 std::istringstream iss (line);
197 if (!(iss >> idx_ihe) || iss.good ())
199 std::cerr <<
"Error loading '" << filename <<
"'' (line " << line_number <<
"): Could not read the face.\n";
202 mesh.faces_.push_back (
Face (idx_ihe));
207 if (Mesh::HasVertexData::value) mesh.vertex_data_cloud_. resize (n_v);
208 if (Mesh::HasHalfEdgeData::value) mesh.half_edge_data_cloud_.resize (n_he);
209 if (Mesh::HasEdgeData::value) mesh.edge_data_cloud_. resize (n_he / 2);
210 if (Mesh::HasFaceData::value) mesh.face_data_cloud_. resize (n_f);
221 write (
const std::string& filename,
const Mesh& mesh)
const 223 std::ofstream file (filename.c_str ());
226 if (!file.is_open ())
228 std::cerr <<
"Error in MeshIO::write: Could not open the file '" << filename <<
"'\n";
232 file <<
"PCL half-edge mesh\n";
233 file << mesh.sizeVertices () <<
" " 234 << mesh.sizeHalfEdges () <<
" " 235 << mesh.sizeFaces () <<
"\n";
238 for (
typename Vertices::const_iterator it=mesh.vertices_.begin (); it!=mesh.vertices_.end (); ++it)
240 file << it->idx_outgoing_half_edge_ <<
"\n";
244 for (
typename HalfEdges::const_iterator it=mesh.half_edges_.begin (); it!=mesh.half_edges_.end (); ++it)
246 file << it->idx_terminating_vertex_ <<
" " 247 << it->idx_next_half_edge_ <<
" " 248 << it->idx_prev_half_edge_ <<
" " 249 << it->idx_face_ <<
"\n";
253 for (
typename Faces::const_iterator it=mesh.faces_.begin (); it!=mesh.faces_.end (); ++it)
255 file << it->idx_inner_half_edge_ <<
"\n";
265 #endif // PCL_GEOMETRY_MESH_IO_H
bool read(const std::string &filename, Mesh &mesh) const
Read the mesh from a file with the given filename.
Mesh::HalfEdges HalfEdges
This file defines compatibility wrappers for low level I/O functions.
Mesh::FaceIndex FaceIndex
Mesh::VertexIndex VertexIndex
bool write(const std::string &filename, const Mesh &mesh) const
Write the mesh to a file with the given filename.
Mesh::HalfEdgeIndex HalfEdgeIndex