38 using namespace StrUtils;
39 using namespace FileUtils;
44 template<
typename CoordType,
51 std::vector< std::vector< CoordType > > &points_coords,
52 std::vector< std::vector< CoordNType > > &normals_coords,
53 std::vector< std::vector< CoordTType > >
55 std::vector< std::vector< CoordCType > > &vertex_color_coords,
56 std::vector< std::vector< IndexType > > &face_indices,
57 std::vector< std::vector< CoordCType > > &face_color_coords,
58 unsigned long nb_total_edges = 0)
60 const bool vertices_have_color =
61 (points_coords.size() == vertex_color_coords.size()),
62 vertices_have_normals =
63 (points_coords.size() == normals_coords.size()),
64 vertices_have_tex_coord =
65 (points_coords.size() == texture_coords.size()),
67 (face_indices.size() == face_color_coords.size());
69 std::ofstream file(file_path);
73 unsigned long nb_writen_vertices = 0,
75 static_cast< unsigned long >(points_coords.size()),
77 static_cast< unsigned long >(face_indices.size());
80 if(vertices_have_color)
82 if(vertices_have_normals)
84 if(vertices_have_tex_coord)
91 if(vertices_have_tex_coord)
99 if(vertices_have_normals)
101 if(vertices_have_tex_coord)
108 if(vertices_have_tex_coord)
114 file << mystring << std::endl;
116 if(nb_total_edges == 0)
117 nb_total_edges = nb_total_vertices + nb_total_faces - 2;
121 file << nb_total_vertices <<
" " << nb_total_faces <<
" " << nb_total_edges
123 file <<
"# #vertices #faces #edges" << std::endl;
124 file <<
"# generated by MEPP2 software" << std::endl;
128 typename std::vector< std::vector< CoordType > >::const_iterator it(
129 points_coords.begin()),
130 ite(points_coords.end());
131 typename std::vector< std::vector< CoordNType > >::const_iterator itn(
132 normals_coords.begin());
133 typename std::vector< std::vector< CoordCType > >::const_iterator itc(
134 vertex_color_coords.begin());
135 typename std::vector< std::vector< CoordTType > >::const_iterator itt(
136 texture_coords.begin());
137 typename std::vector< CoordType >::const_iterator itv, itve;
138 typename std::vector< CoordNType >::const_iterator itvn, itvne;
139 typename std::vector< CoordCType >::const_iterator itvc, itvce;
140 typename std::vector< CoordTType >::const_iterator itvt, itvte;
141 for(; it != ite; ++it)
144 for(itv = it->begin(), itve = it->end(); itv != itve; ++itv)
147 if(vertices_have_normals)
149 for(itvn = itn->begin(), itvne = itn->end(); itvn != itvne; ++itvn)
150 file << *itvn <<
" ";
154 if(vertices_have_color)
156 for(itvc = itc->begin(), itvce = itc->end(); itvc != itvce; ++itvc)
157 file << *itvc <<
" ";
161 if(vertices_have_tex_coord)
163 for(itvt = itt->begin(), itvte = itt->end(); itvt != itvte; ++itvt)
164 file << *itvt <<
" ";
169 nb_writen_vertices++;
172 assert(nb_writen_vertices == nb_total_vertices);
176 typename std::vector< std::vector< IndexType > >::const_iterator itf(
177 face_indices.begin()),
178 itfe(face_indices.end());
179 typename std::vector< std::vector< CoordCType > >::const_iterator itfc(
180 face_color_coords.begin());
182 typename std::vector< IndexType >::const_iterator itfindex, itfindexe;
183 typename std::vector< CoordCType >::const_iterator itffc, itffce;
184 for(; itf != itfe; ++itf)
186 file << itf->size() <<
" ";
188 for(itfindex = itf->begin(), itfindexe = itf->end();
189 itfindex != itfindexe;
191 file << *itfindex <<
" ";
193 if(faces_have_colors)
196 for(itffc = itfc->begin(), itffce = itfc->end(); itffc != itffce;
198 file << *itffc <<
" ";
209 throw std::runtime_error(
"writer_off_file: failed to open output file.");