59 template <
class MeshT>
64 using Vertex =
typename Mesh::Vertex;
66 using Face =
typename Mesh::Face;
70 using Faces =
typename Mesh::Faces;
85 read(
const std::string& filename,
Mesh& mesh)
const
87 std::ifstream file(filename.c_str());
89 if (!file.is_open()) {
90 std::cerr <<
"Error in MeshIO::read: Could not open the file '" << filename
97 unsigned int line_number = 1;
98 int n_v = -1, n_he = -1, n_f = -1;
100 if (!std::getline(file, line) || line !=
"PCL half-edge mesh") {
101 std::cerr <<
"Error loading '" << filename <<
"' (line " << line_number
102 <<
"): Wrong file format.\n";
107 if (!std::getline(file, line)) {
108 std::cerr <<
"Error loading '" << filename <<
"'' (line " << line_number
109 <<
"): Number of vertices / half-edges / faces not found.\n";
113 std::istringstream iss(line);
114 if (!(iss >> n_v >> n_he >> n_f) || iss.good())
116 std::cerr <<
"Error loading '" << filename <<
"'' (line " << line_number
117 <<
"): Could not read the number of vertices / half-edges / faces.\n";
121 if (n_v < 0 || n_he < 0 || n_f < 0) {
122 std::cerr <<
"Error loading '" << filename <<
"'' (line " << line_number
123 <<
"): Invalid number of vertices / half-edges / faces.\n";
130 mesh.vertices_.reserve(n_v);
133 for (
int i = 0; i < n_v; ++i, ++line_number) {
134 if (!std::getline(file, line)) {
135 std::cerr <<
"Error loading '" << filename <<
"'' (line " << line_number
136 <<
"): Could not read the line.\n";
140 std::istringstream iss(line);
141 if (!(iss >> idx_ohe) || iss.good()) {
142 std::cerr <<
"Error loading '" << filename <<
"'' (line " << line_number
143 <<
"): Could not read the vertex.\n";
146 mesh.vertices_.push_back(
Vertex(idx_ohe));
152 mesh.half_edges_.reserve(n_he);
158 for (
int i = 0; i < n_he; ++i, ++line_number) {
159 if (!std::getline(file, line)) {
160 std::cerr <<
"Error loading '" << filename <<
"'' (line " << line_number
161 <<
"): Could not read the line.\n";
165 std::istringstream iss(line);
166 if (!(iss >> idx_tv >> idx_nhe >> idx_phe >> idx_f) || iss.good()) {
167 std::cerr <<
"Error loading '" << filename <<
"'' (line " << line_number
168 <<
"): Could not read the half-edge.\n";
171 mesh.half_edges_.push_back(
HalfEdge(idx_tv, idx_nhe, idx_phe, idx_f));
177 mesh.faces_.reserve(n_f);
180 for (
int i = 0; i < n_f; ++i, ++line_number) {
181 if (!std::getline(file, line)) {
182 std::cerr <<
"Error loading '" << filename <<
"'' (line " << line_number
183 <<
"): Could not read the line.\n";
187 std::istringstream iss(line);
188 if (!(iss >> idx_ihe) || iss.good()) {
189 std::cerr <<
"Error loading '" << filename <<
"'' (line " << line_number
190 <<
"): Could not read the face.\n";
193 mesh.faces_.push_back(
Face(idx_ihe));
198 if (Mesh::HasVertexData::value)
199 mesh.vertex_data_cloud_.resize(n_v);
200 if (Mesh::HasHalfEdgeData::value)
201 mesh.half_edge_data_cloud_.resize(n_he);
202 if (Mesh::HasEdgeData::value)
203 mesh.edge_data_cloud_.resize(n_he / 2);
204 if (Mesh::HasFaceData::value)
205 mesh.face_data_cloud_.resize(n_f);
216 write(
const std::string& filename,
const Mesh& mesh)
const
218 std::ofstream file(filename.c_str());
221 if (!file.is_open()) {
222 std::cerr <<
"Error in MeshIO::write: Could not open the file '" << filename
227 file <<
"PCL half-edge mesh\n";
228 file << mesh.sizeVertices() <<
" " << mesh.sizeHalfEdges() <<
" "
229 << mesh.sizeFaces() <<
"\n";
232 for (
const auto& vertex : mesh.vertices_) {
233 file << vertex.idx_outgoing_half_edge_ <<
"\n";
237 for (
const auto& edge : mesh.half_edges_) {
238 file << edge.idx_terminating_vertex_ <<
" " << edge.idx_next_half_edge_ <<
" "
239 << edge.idx_prev_half_edge_ <<
" " << edge.idx_face_ <<
"\n";
243 for (
const auto& face : mesh.faces_) {
244 file << face.idx_inner_half_edge_ <<
"\n";
Read / write the half-edge mesh from / to a file.
typename Mesh::VertexIndex VertexIndex
typename Mesh::FaceIndex FaceIndex
typename Mesh::HalfEdges HalfEdges
typename Mesh::HalfEdgeIndex HalfEdgeIndex
bool read(const std::string &filename, Mesh &mesh) const
Read the mesh from a file with the given filename.
typename Mesh::Faces Faces
MeshIO()=default
Constructor.
typename Mesh::Vertex Vertex
typename Mesh::Vertices Vertices
bool write(const std::string &filename, const Mesh &mesh) const
Write the mesh to a file with the given filename.
typename Mesh::HalfEdge HalfEdge
pcl::detail::MeshIndex< struct FaceIndexTag > FaceIndex
Index used to access elements in the half-edge mesh.
pcl::detail::MeshIndex< struct HalfEdgeIndexTag > HalfEdgeIndex
Index used to access elements in the half-edge mesh.
pcl::detail::MeshIndex< struct VertexIndexTag > VertexIndex
Index used to access elements in the half-edge mesh.