26 using namespace StrUtils;
27 using namespace FileUtils;
35 template<
typename CoordType,
42 std::vector< std::vector< CoordType > > &points_coords,
43 std::vector< std::vector< CoordNType > > &normals_coords,
44 std::vector< std::vector< CoordTType > > &texture_coords,
45 std::vector< std::vector< CoordCType > > &vertex_color_coords,
46 std::vector< std::vector< IndexType > > &face_indices,
47 std::vector< std::vector< IndexType > > &texture_face_indices)
49 points_coords.clear();
50 normals_coords.clear();
51 texture_coords.clear();
52 vertex_color_coords.clear();
54 texture_face_indices.clear();
56 std::ifstream file(file_path);
62 std::vector< std::string > tokens;
65 bool vertices_have_color =
false, vertices_have_alpha =
false,
66 vertices_have_normal =
false, faces_have_texture =
false;
68 unsigned int vertex_dim = 3u, normal_dim = 3u, color_dim = 3u,
69 alpha_dim = 3u, tex_dim = 2u, offset, face_degree;
71 unsigned long nb_read_vertices = 0ul, nb_total_vertices = 0ul,
72 nb_read_edges = 0ul, nb_total_edges = 0ul,
73 nb_read_faces = 0ul, nb_total_faces = 0ul,
74 nb_face_texture_point = 0u;
85 tokens =
split(line,
"\t ");
86 tokens_size = tokens.size();
92 if(tokens[0].compare(
"element") == 0)
96 if(tokens[1].compare(
"vertex") == 0)
98 convert(tokens[2], nb_total_vertices);
101 else if(tokens[1].compare(
"edge") == 0)
103 convert(tokens[2], nb_total_edges);
106 else if(tokens[1].compare(
"face") == 0)
108 convert(tokens[2], nb_total_faces);
113 throw std::runtime_error(
"Reader::read_ply_file -> wrong tokens "
114 "number for header line.");
116 else if(tokens[0].compare(
"property") == 0)
124 if(tokens[2].compare(
"nx") == 0)
125 vertices_have_normal =
true;
126 if(tokens[2].compare(
"red") == 0)
127 vertices_have_color =
true;
128 if(tokens[2].compare(
"alpha") == 0)
129 vertices_have_alpha =
true;
137 if(tokens[4].compare(
"texcoord") == 0)
138 faces_have_texture =
true;
144 throw std::runtime_error(
"Reader::read_ply_file -> wrong tokens "
145 "number for header line.");
147 else if((tokens[0].compare(
"end_header") == 0))
153 while(nb_read_vertices < nb_total_vertices)
157 tokens =
split(line,
"\t ");
158 tokens_size = tokens.size();
165 else if(tokens_size == vertex_dim +
166 (vertices_have_normal ? normal_dim : 0) +
167 (vertices_have_color ? color_dim : 0) +
168 (vertices_have_alpha ? alpha_dim : 0))
170 std::vector< CoordType > point;
171 for(; i < vertex_dim; ++i)
174 point.push_back(coord);
176 points_coords.push_back(point);
178 if(vertices_have_normal)
180 std::vector< CoordNType > normal;
181 offset = vertex_dim + normal_dim;
182 for(; i < offset; ++i)
185 normal.push_back(n_coord);
187 normals_coords.push_back(normal);
190 if(vertices_have_color)
192 std::vector< CoordCType > color;
193 offset = vertex_dim + (vertices_have_normal ? normal_dim : 0) +
195 (vertices_have_alpha ? alpha_dim
197 for(; i < offset; ++i)
200 color.push_back(c_coord);
202 vertex_color_coords.push_back(color);
220 throw std::runtime_error(
221 std::string(
"Reader::read_ply_file -> vertex line '") + line +
222 std::string(
"'do not fit to the format."));
226 throw std::runtime_error(
227 "Reader::read_ply_file -> wrong number of vertices specified or "
228 "read error occured while parsing vertices.");
232 while(nb_read_faces < nb_total_faces)
236 tokens =
split(line,
"\t ");
237 tokens_size = tokens.size();
246 std::vector< IndexType >
face;
247 convert(tokens[0], face_degree);
248 offset = face_degree + 1;
250 if(tokens_size >= offset)
252 for(; i < offset; ++i)
255 face.push_back(f_ind);
257 face_indices.push_back(
face);
259 if(faces_have_texture)
264 std::vector< IndexType > face_textures;
265 for(
unsigned int j = 0; j < face_degree; ++i)
267 std::vector< CoordTType > tex_coord;
268 for(; i < tex_dim; ++i)
271 tex_coord.push_back(t_coord);
273 texture_coords.push_back(tex_coord);
274 face_textures.push_back(nb_face_texture_point++);
276 texture_face_indices.push_back(face_textures);
281 throw std::runtime_error(
282 "Reader::read_ply_file -> face line do not fit to the format.");
286 throw std::runtime_error(
287 "Reader::read_ply_file -> wrong number of faces specified or read "
288 "error occured while parsing faces.");
292 while(nb_read_edges < nb_total_edges)
296 tokens =
split(line,
"\t ");
297 tokens_size = tokens.size();
306 std::vector< IndexType >
edge;
313 edge.push_back(f_ind);
315 face_indices.push_back(
edge);
320 throw std::runtime_error(
321 "Reader::read_ply_file -> edge line do not fit to the format.");
325 throw std::runtime_error(
326 "Reader::read_ply_file -> wrong number of edges specified or read "
327 "error occured while parsing edges.");
334 throw std::runtime_error(
335 "Reader::read_ply_file -> input file failed to open.");