55 #include <OpenMesh/Core/Utils/Endian.hh>
56 #include <OpenMesh/Core/IO/IOManager.hh>
57 #include <OpenMesh/Core/IO/BinaryHelper.hh>
58 #include <OpenMesh/Core/IO/writer/OFFWriter.hh>
88 std::ofstream out(_filename.c_str(), (_opt.check(
Options::Binary) ? std::ios_base::binary | std::ios_base::out
89 : std::ios_base::out) );
91 return write(out, _be, _opt, _precision);
102 if ( !check( _be, _opt ) )
113 omerr() <<
"[OFFWriter] : cannot write to stream "
127 _os.precision(_precision);
131 write_binary(_os, _be, _opt) :
132 write_ascii(_os, _be, _opt));
147 unsigned int i, nV, nF;
155 std::vector<VertexHandle> vhandles;
159 _out << _be.n_vertices() <<
" ";
160 _out << _be.n_faces() <<
" ";
163 if (_opt.color_is_float())
168 for (i=0, nV=
int(_be.n_vertices()); i<nV; ++i)
174 _out << v[0] <<
" " << v[1] <<
" " << v[2];
177 if ( _opt.vertex_has_normal() ) {
179 _out <<
" " << n[0] <<
" " << n[1] <<
" " << n[2];
183 if ( _opt.vertex_has_color() ) {
184 if ( _opt.color_is_float() ) {
186 if ( _opt.color_has_alpha() ){
187 cAf = _be.colorAf(vh);
196 if ( _opt.color_has_alpha() ){
208 if (_opt.vertex_has_texcoord() ) {
209 t = _be.texcoord(vh);
210 _out <<
" " << t[0] <<
" " << t[1];
218 if (_be.is_triangle_mesh())
220 for (i=0, nF=
int(_be.n_faces()); i<nF; ++i)
222 _be.get_vhandles(FaceHandle(i), vhandles);
224 _out << vhandles[0].idx() <<
" ";
225 _out << vhandles[1].idx() <<
" ";
226 _out << vhandles[2].idx();
229 if ( _opt.face_has_color() ){
230 if ( _opt.color_is_float() ) {
232 if ( _opt.color_has_alpha() ){
233 cAf = _be.colorAf( FaceHandle(i) );
237 cf = _be.colorf( FaceHandle(i) );
242 if ( _opt.color_has_alpha() ){
243 cA = _be.colorA( FaceHandle(i) );
247 c = _be.color( FaceHandle(i) );
257 for (i=0, nF=
int(_be.n_faces()); i<nF; ++i)
259 nV = _be.get_vhandles(FaceHandle(i), vhandles);
261 for (
size_t j=0; j<vhandles.size(); ++j)
262 _out << vhandles[j].idx() <<
" ";
265 if ( _opt.face_has_color() ){
266 if ( _opt.color_is_float() ) {
268 if ( _opt.color_has_alpha() ){
269 cAf = _be.colorAf( FaceHandle(i) );
273 cf = _be.colorf( FaceHandle(i) );
278 if ( _opt.color_has_alpha() ){
279 cA = _be.colorA( FaceHandle(i) );
283 c = _be.color( FaceHandle(i) );
300 void _OFFWriter_::writeValue(std::ostream& _out,
int value)
const {
303 store(_out, tmp,
false);
306 void _OFFWriter_::writeValue(std::ostream& _out,
unsigned int value)
const {
309 store(_out, tmp,
false);
312 void _OFFWriter_::writeValue(std::ostream& _out,
float value)
const {
315 store(_out, tmp,
false);
320 write_binary(std::ostream& _out,
BaseExporter& _be, Options _opt)
const
323 unsigned int i, nV, nF;
329 std::vector<VertexHandle> vhandles;
332 writeValue(_out, (uint)_be.n_vertices() );
333 writeValue(_out, (uint) _be.n_faces() );
334 writeValue(_out, 0 );
337 for (i=0, nV=
int(_be.n_vertices()); i<nV; ++i)
339 vh = VertexHandle(i);
343 writeValue(_out, v[0]);
344 writeValue(_out, v[1]);
345 writeValue(_out, v[2]);
348 if ( _opt.vertex_has_normal() ) {
350 writeValue(_out, n[0]);
351 writeValue(_out, n[1]);
352 writeValue(_out, n[2]);
355 if ( _opt.vertex_has_color() ) {
356 if ( _opt.color_is_float() ) {
357 cf = _be.colorAf(vh);
358 writeValue(_out, cf[0]);
359 writeValue(_out, cf[1]);
360 writeValue(_out, cf[2]);
362 if ( _opt.color_has_alpha() )
363 writeValue(_out, cf[3]);
366 writeValue(_out, c[0]);
367 writeValue(_out, c[1]);
368 writeValue(_out, c[2]);
370 if ( _opt.color_has_alpha() )
371 writeValue(_out, c[3]);
375 if (_opt.vertex_has_texcoord() ) {
376 t = _be.texcoord(vh);
377 writeValue(_out, t[0]);
378 writeValue(_out, t[1]);
384 if (_be.is_triangle_mesh())
386 for (i=0, nF=
int(_be.n_faces()); i<nF; ++i)
389 _be.get_vhandles(FaceHandle(i), vhandles);
391 writeValue(_out, vhandles[0].idx());
392 writeValue(_out, vhandles[1].idx());
393 writeValue(_out, vhandles[2].idx());
396 if ( _opt.face_has_color() ){
397 if ( _opt.color_is_float() ) {
398 cf = _be.colorAf( FaceHandle(i) );
399 writeValue(_out, cf[0]);
400 writeValue(_out, cf[1]);
401 writeValue(_out, cf[2]);
403 if ( _opt.color_has_alpha() )
404 writeValue(_out, cf[3]);
406 c = _be.colorA( FaceHandle(i) );
407 writeValue(_out, c[0]);
408 writeValue(_out, c[1]);
409 writeValue(_out, c[2]);
411 if ( _opt.color_has_alpha() )
412 writeValue(_out, c[3]);
419 for (i=0, nF=
int(_be.n_faces()); i<nF; ++i)
422 nV = _be.get_vhandles(FaceHandle(i), vhandles);
423 writeValue(_out, nV);
424 for (
size_t j=0; j<vhandles.size(); ++j)
425 writeValue(_out, vhandles[j].idx() );
428 if ( _opt.face_has_color() ){
429 if ( _opt.color_is_float() ) {
430 cf = _be.colorAf( FaceHandle(i) );
431 writeValue(_out, cf[0]);
432 writeValue(_out, cf[1]);
433 writeValue(_out, cf[2]);
435 if ( _opt.color_has_alpha() )
436 writeValue(_out, cf[3]);
438 c = _be.colorA( FaceHandle(i) );
439 writeValue(_out, c[0]);
440 writeValue(_out, c[1]);
441 writeValue(_out, c[2]);
443 if ( _opt.color_has_alpha() )
444 writeValue(_out, c[3]);
463 size_t _3floats(3*
sizeof(
float));
464 size_t _3ui(3*
sizeof(
unsigned int));
465 size_t _4ui(4*
sizeof(
unsigned int));
467 if ( !_opt.is_binary() )
471 size_t _3longs(3*
sizeof(
long));
475 data += _be.n_vertices() * _3floats;
478 if ( _opt.vertex_has_normal() && _be.has_vertex_normals() )
481 data += _be.n_vertices() * _3floats;
484 if ( _opt.vertex_has_color() && _be.has_vertex_colors() )
487 data += _be.n_vertices() * _3floats;
490 if ( _opt.vertex_has_texcoord() && _be.has_vertex_texcoords() )
492 size_t _2floats(2*
sizeof(
float));
494 data += _be.n_vertices() * _2floats;
498 if (_be.is_triangle_mesh())
500 data += _be.n_faces() * _4ui;
505 std::vector<VertexHandle> vhandles;
507 for (i=0, nF=
int(_be.n_faces()); i<nF; ++i)
508 data += _be.get_vhandles(
FaceHandle(i), vhandles) *
sizeof(
unsigned int);
513 if ( _opt.face_has_color() && _be.has_face_colors() ){
514 if ( _opt.color_has_alpha() )
515 data += _be.n_faces() * _4ui;
517 data += _be.n_faces() * _3ui;
size_t binary_size(BaseExporter &_be, Options _opt) const
Returns expected size of file if binary format is supported else 0.
Has (r) / store (w) vertex normals.
bool register_module(BaseReader *_bl)
Has (r) / store (w) face normals.
Has (r) / store (w) face colors.
Has (r) / store (w) texture coordinates.
Set options for reader/writer modules.
Handle for a vertex entity.
Handle for a face entity.
Has (r) / store (w) vertex colors.
bool write(const std::string &, BaseExporter &, Options, std::streamsize _precision=6) const
_OFFWriter_ __OFFWriterInstance
Declare the single entity of the OFF writer.
_IOManager_ & IOManager()