50 #include <OpenMesh/Core/Utils/Endian.hh> 51 #include <OpenMesh/Core/IO/IOManager.hh> 52 #include <OpenMesh/Core/IO/BinaryHelper.hh> 53 #include <OpenMesh/Core/IO/writer/OFFWriter.hh> 83 std::ofstream out(_filename.c_str(), (_opt.check(
Options::Binary) ? std::ios_base::binary | std::ios_base::out
84 : std::ios_base::out) );
86 return write(out, _be, _opt, _precision);
97 if ( !check( _be, _opt ) )
108 omerr() <<
"[OFFWriter] : cannot write to stream " 122 _os.precision(_precision);
126 write_binary(_os, _be, _opt) :
127 write_ascii(_os, _be, _opt));
142 unsigned int i, nV, nF;
150 std::vector<VertexHandle> vhandles;
154 _out << _be.n_vertices() <<
" ";
155 _out << _be.n_faces() <<
" ";
158 if (_opt.color_is_float())
163 for (i=0, nV=
int(_be.n_vertices()); i<nV; ++i)
169 _out << v[0] <<
" " << v[1] <<
" " << v[2];
172 if ( _opt.vertex_has_normal() ) {
174 _out <<
" " << n[0] <<
" " << n[1] <<
" " << n[2];
178 if ( _opt.vertex_has_color() ) {
179 if ( _opt.color_is_float() ) {
181 if ( _opt.color_has_alpha() ){
182 cAf = _be.colorAf(vh);
191 if ( _opt.color_has_alpha() ){
203 if (_opt.vertex_has_texcoord() ) {
204 t = _be.texcoord(vh);
205 _out <<
" " << t[0] <<
" " << t[1];
213 if (_be.is_triangle_mesh())
215 for (i=0, nF=
int(_be.n_faces()); i<nF; ++i)
219 _out << vhandles[0].idx() <<
" ";
220 _out << vhandles[1].idx() <<
" ";
221 _out << vhandles[2].idx();
224 if ( _opt.face_has_color() ){
225 if ( _opt.color_is_float() ) {
227 if ( _opt.color_has_alpha() ){
237 if ( _opt.color_has_alpha() ){
252 for (i=0, nF=
int(_be.n_faces()); i<nF; ++i)
254 nV = _be.get_vhandles(
FaceHandle(i), vhandles);
256 for (
size_t j=0; j<vhandles.size(); ++j)
257 _out << vhandles[j].idx() <<
" ";
260 if ( _opt.face_has_color() ){
261 if ( _opt.color_is_float() ) {
263 if ( _opt.color_has_alpha() ){
273 if ( _opt.color_has_alpha() ){
295 void _OFFWriter_::writeValue(std::ostream& _out,
int value)
const {
298 store(_out, tmp,
false);
301 void _OFFWriter_::writeValue(std::ostream& _out,
unsigned int value)
const {
304 store(_out, tmp,
false);
307 void _OFFWriter_::writeValue(std::ostream& _out,
float value)
const {
310 store(_out, tmp,
false);
318 unsigned int i, nV, nF;
324 std::vector<VertexHandle> vhandles;
327 writeValue(_out, (uint)_be.n_vertices() );
328 writeValue(_out, (uint) _be.n_faces() );
329 writeValue(_out, 0 );
332 for (i=0, nV=
int(_be.n_vertices()); i<nV; ++i)
338 writeValue(_out, v[0]);
339 writeValue(_out, v[1]);
340 writeValue(_out, v[2]);
343 if ( _opt.vertex_has_normal() ) {
345 writeValue(_out, n[0]);
346 writeValue(_out, n[1]);
347 writeValue(_out, n[2]);
350 if ( _opt.vertex_has_color() ) {
351 if ( _opt.color_is_float() ) {
352 cf = _be.colorAf(vh);
353 writeValue(_out, cf[0]);
354 writeValue(_out, cf[1]);
355 writeValue(_out, cf[2]);
357 if ( _opt.color_has_alpha() )
358 writeValue(_out, cf[3]);
361 writeValue(_out, c[0]);
362 writeValue(_out, c[1]);
363 writeValue(_out, c[2]);
365 if ( _opt.color_has_alpha() )
366 writeValue(_out, c[3]);
370 if (_opt.vertex_has_texcoord() ) {
371 t = _be.texcoord(vh);
372 writeValue(_out, t[0]);
373 writeValue(_out, t[1]);
379 if (_be.is_triangle_mesh())
381 for (i=0, nF=
int(_be.n_faces()); i<nF; ++i)
386 writeValue(_out, vhandles[0].idx());
387 writeValue(_out, vhandles[1].idx());
388 writeValue(_out, vhandles[2].idx());
391 if ( _opt.face_has_color() ){
392 if ( _opt.color_is_float() ) {
394 writeValue(_out, cf[0]);
395 writeValue(_out, cf[1]);
396 writeValue(_out, cf[2]);
398 if ( _opt.color_has_alpha() )
399 writeValue(_out, cf[3]);
402 writeValue(_out, c[0]);
403 writeValue(_out, c[1]);
404 writeValue(_out, c[2]);
406 if ( _opt.color_has_alpha() )
407 writeValue(_out, c[3]);
414 for (i=0, nF=
int(_be.n_faces()); i<nF; ++i)
417 nV = _be.get_vhandles(
FaceHandle(i), vhandles);
418 writeValue(_out, nV);
419 for (
size_t j=0; j<vhandles.size(); ++j)
420 writeValue(_out, vhandles[j].idx() );
423 if ( _opt.face_has_color() ){
424 if ( _opt.color_is_float() ) {
426 writeValue(_out, cf[0]);
427 writeValue(_out, cf[1]);
428 writeValue(_out, cf[2]);
430 if ( _opt.color_has_alpha() )
431 writeValue(_out, cf[3]);
434 writeValue(_out, c[0]);
435 writeValue(_out, c[1]);
436 writeValue(_out, c[2]);
438 if ( _opt.color_has_alpha() )
439 writeValue(_out, c[3]);
458 size_t _3floats(3*
sizeof(
float));
459 size_t _3ui(3*
sizeof(
unsigned int));
460 size_t _4ui(4*
sizeof(
unsigned int));
462 if ( !_opt.is_binary() )
466 size_t _3longs(3*
sizeof(
long));
470 data += _be.n_vertices() * _3floats;
473 if ( _opt.vertex_has_normal() && _be.has_vertex_normals() )
476 data += _be.n_vertices() * _3floats;
479 if ( _opt.vertex_has_color() && _be.has_vertex_colors() )
482 data += _be.n_vertices() * _3floats;
485 if ( _opt.vertex_has_texcoord() && _be.has_vertex_texcoords() )
487 size_t _2floats(2*
sizeof(
float));
489 data += _be.n_vertices() * _2floats;
493 if (_be.is_triangle_mesh())
495 data += _be.n_faces() * _4ui;
500 std::vector<VertexHandle> vhandles;
502 for (i=0, nF=
int(_be.n_faces()); i<nF; ++i)
503 data += _be.get_vhandles(
FaceHandle(i), vhandles) *
sizeof(
unsigned int);
508 if ( _opt.face_has_color() && _be.has_face_colors() ){
509 if ( _opt.color_has_alpha() )
510 data += _be.n_faces() * _4ui;
512 data += _be.n_faces() * _3ui;
Handle for a face entity.
Has (r) / store (w) vertex colors.
Has (r) / store (w) face colors.
bool write(const std::string &, BaseExporter &, Options, std::streamsize _precision=6) const override
Handle for a vertex entity.
_OFFWriter_ __OFFWriterInstance
Declare the single entity of the OFF writer.
bool register_module(BaseReader *_bl)
size_t binary_size(BaseExporter &_be, Options _opt) const override
Returns expected size of file if binary format is supported else 0.
Set options for reader/writer modules.
Has (r) / store (w) vertex normals.
_IOManager_ & IOManager()
Has (r) / store (w) face normals.
Has (r) / store (w) texture coordinates.