27 using namespace StrUtils;
28 using namespace FileUtils;
34 template<
typename CoordType,
41 std::vector< std::vector< CoordType > > &points_coords,
42 std::vector< std::vector< CoordNType > > &normals_coords,
43 std::vector< std::vector< CoordTType > > &texture_coords,
44 std::vector< std::vector< CoordCType > > &vertex_color_coords,
45 std::vector< std::vector< IndexType > > &face_indices,
46 std::vector< std::vector< CoordCType > > &face_color_coords)
48 points_coords.clear();
49 normals_coords.clear();
50 texture_coords.clear();
52 vertex_color_coords.clear();
53 face_color_coords.clear();
55 std::ifstream file(file_path);
59 throw std::runtime_error(
60 "Reader::read_off_file -> failed to open input file.");
63 std::string line_str, word;
64 std::istringstream line_ss;
66 bool vertices_have_color =
false;
67 bool vertices_have_normals =
false;
68 bool vertices_have_texture =
false;
70 unsigned int vertex_dim = 3u,
74 unsigned int face_degree;
75 unsigned long nb_read_vertices = 0ul,
76 nb_total_vertices = 0ul;
77 unsigned long nb_total_edges = 0ul;
78 unsigned long nb_read_faces = 0ul,
90 if((word.size() < 3) || (word.substr(word.size() - 3) !=
"OFF"))
92 throw std::runtime_error(
"Reader::read_off_file -> file format is "
93 "unknown (no [ST][C][N]OFF header). Other "
94 "OFF formats are not implemented yet.");
97 vertices_have_normals = (word.size() >= 4) &&
98 (word.substr(word.size() - 4) ==
"NOFF");
100 vertices_have_color = (word ==
"COFF") || (word ==
"CNOFF") ||
101 (word ==
"STCOFF") || (word ==
"STCNOFF");
102 vertices_have_texture = (word.substr(0, 2) ==
"ST");
106 line_ss >> nb_total_vertices >> nb_total_faces >> nb_total_edges;
108 points_coords.reserve(nb_total_vertices);
109 face_indices.reserve(nb_total_faces);
110 if(vertices_have_normals)
111 normals_coords.reserve(nb_total_vertices);
112 if(vertices_have_color)
113 vertex_color_coords.reserve(nb_total_vertices);
114 if(vertices_have_texture)
115 texture_coords.reserve(nb_total_vertices);
118 std::vector< CoordType > point;
119 std::vector< CoordNType > normal;
120 std::vector< CoordCType > color;
121 std::vector< CoordTType > tex_coord;
122 while(nb_read_vertices < nb_total_vertices)
128 for(
unsigned int i = 0; i < vertex_dim; ++i)
131 point.push_back(coord);
133 points_coords.push_back(point);
136 if(vertices_have_normals)
139 for(
unsigned int i = 0; i < normal_dim; ++i)
142 normal.push_back(n_coord);
144 normals_coords.push_back(normal);
148 if(vertices_have_color)
151 for(
unsigned int i = 0; i < color_dim; ++i)
154 color.push_back(c_coord);
156 vertex_color_coords.push_back(color);
160 if(vertices_have_texture)
163 for(
unsigned int i = 0; i < texture_dim; ++i)
166 tex_coord.push_back(t_coord);
168 texture_coords.push_back(tex_coord);
175 throw std::runtime_error(
176 "Reader::read_off_file -> failed to read vertex line.");
181 std::vector< IndexType >
face;
182 while(nb_read_faces < nb_total_faces)
188 line_ss >> face_degree;
189 for(
unsigned int i = 0; i < face_degree; ++i)
192 face.push_back(f_ind);
194 face_indices.push_back(
face);
207 for(
unsigned int i = 0; i < color_dim; ++i)
210 color.push_back(c_coord);
216 face_color_coords.push_back(color);
223 throw std::runtime_error(
224 "Reader::read_off_file -> failed to read face line.");