OFFWriter.cc 15.1 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1
2
3
/*===========================================================================*\
 *                                                                           *
 *                               OpenMesh                                    *
4
 *      Copyright (C) 2001-2014 by Computer Graphics Group, RWTH Aachen      *
Jan Möbius's avatar
Jan Möbius committed
5
6
 *                           www.openmesh.org                                *
 *                                                                           *
7
 *---------------------------------------------------------------------------*
8
 *  This file is part of OpenMesh.                                           *
Jan Möbius's avatar
Jan Möbius committed
9
 *                                                                           *
10
 *  OpenMesh is free software: you can redistribute it and/or modify         *
11
12
13
14
 *  it under the terms of the GNU Lesser General Public License as           *
 *  published by the Free Software Foundation, either version 3 of           *
 *  the License, or (at your option) any later version with the              *
 *  following exceptions:                                                    *
Jan Möbius's avatar
Jan Möbius committed
15
 *                                                                           *
16
17
18
19
20
21
22
 *  If other files instantiate templates or use macros                       *
 *  or inline functions from this file, or you compile this file and         *
 *  link it with other files to produce an executable, this file does        *
 *  not by itself cause the resulting executable to be covered by the        *
 *  GNU Lesser General Public License. This exception does not however       *
 *  invalidate any other reasons why the executable file might be            *
 *  covered by the GNU Lesser General Public License.                        *
Jan Möbius's avatar
Jan Möbius committed
23
 *                                                                           *
24
25
26
27
 *  OpenMesh is distributed in the hope that it will be useful,              *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
 *  GNU Lesser General Public License for more details.                      *
Jan Möbius's avatar
Jan Möbius committed
28
 *                                                                           *
29
30
31
32
 *  You should have received a copy of the GNU LesserGeneral Public          *
 *  License along with OpenMesh.  If not,                                    *
 *  see <http://www.gnu.org/licenses/>.                                      *
 *                                                                           *
33
\*===========================================================================*/
34
35

/*===========================================================================*\
36
 *                                                                           *
37
38
 *   $Revision$                                                         *
 *   $Date$                   *
Jan Möbius's avatar
Jan Möbius committed
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
 *                                                                           *
\*===========================================================================*/


//== INCLUDES =================================================================


#include <OpenMesh/Core/System/config.h>
#include <OpenMesh/Core/System/omstream.hh>
#include <OpenMesh/Core/Utils/Endian.hh>
#include <OpenMesh/Core/IO/IOManager.hh>
#include <OpenMesh/Core/IO/BinaryHelper.hh>
#include <OpenMesh/Core/IO/writer/OFFWriter.hh>

#include <OpenMesh/Core/IO/SR_store.hh>

//=== NAMESPACES ==============================================================


namespace OpenMesh {
namespace IO {


//=== INSTANCIATE =============================================================


// register the OFFLoader singleton with MeshLoader
_OFFWriter_  __OFFWriterInstance;
_OFFWriter_& OFFWriter() { return __OFFWriterInstance; }


//=== IMPLEMENTATION ==========================================================


_OFFWriter_::_OFFWriter_() { IOManager().register_module(this); }


//-----------------------------------------------------------------------------


bool
_OFFWriter_::
81
write(const std::string& _filename, BaseExporter& _be, Options _opt, std::streamsize _precision) const
Jan Möbius's avatar
Jan Möbius committed
82
83
84
85
86
87
88
89
90
{
  // check exporter features
  if ( !check( _be, _opt ) )
    return false;


  // check writer features
  if ( _opt.check(Options::FaceNormal) ) // not supported by format
    return false;
91

Jan Möbius's avatar
Jan Möbius committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
  // open file
  std::fstream out(_filename.c_str(), (_opt.check(Options::Binary) ? std::ios_base::binary | std::ios_base::out
                                                         : std::ios_base::out) );
  if (!out)
  {
    omerr() << "[OFFWriter] : cannot open file "
	  << _filename
	  << std::endl;
    return false;
  }

  // write header line
  if (_opt.check(Options::VertexTexCoord)) out << "ST";
  if (_opt.check(Options::VertexColor) || _opt.check(Options::FaceColor))    out << "C";
  if (_opt.check(Options::VertexNormal))   out << "N";
  out << "OFF";
  if (_opt.check(Options::Binary)) out << " BINARY";
  out << "\n";


112
113
114
  if (!_opt.check(Options::Binary))
    out.precision(_precision);

Jan Möbius's avatar
Jan Möbius committed
115
116
117
118
119
120
121
122
123
124
125
  // write to file
  bool result = (_opt.check(Options::Binary) ?
		 write_binary(out, _be, _opt) :
		 write_ascii(out, _be, _opt));


  // return result
  out.close();
  return result;
}

126
127
128
129
130
//-----------------------------------------------------------------------------


bool
_OFFWriter_::
131
write(std::ostream& _os, BaseExporter& _be, Options _opt, std::streamsize _precision) const
132
133
134
135
136
137
138
139
140
{
  // check exporter features
  if ( !check( _be, _opt ) )
    return false;


  // check writer features
  if ( _opt.check(Options::FaceNormal) ) // not supported by format
    return false;
141
142


143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
  if (!_os.good())
  {
    omerr() << "[OFFWriter] : cannot write to stream "
	  << std::endl;
    return false;
  }

  // write header line
  if (_opt.check(Options::VertexTexCoord)) _os << "ST";
  if (_opt.check(Options::VertexColor) || _opt.check(Options::FaceColor))    _os << "C";
  if (_opt.check(Options::VertexNormal))   _os << "N";
  _os << "OFF";
  if (_opt.check(Options::Binary)) _os << " BINARY";
  _os << "\n";

158
159
  if (!_opt.check(Options::Binary))
    _os.precision(_precision);
160
161
162
163
164
165
166
167
168
169

  // write to file
  bool result = (_opt.check(Options::Binary) ?
		 write_binary(_os, _be, _opt) :
		 write_ascii(_os, _be, _opt));


  // return result
  return result;
}
Jan Möbius's avatar
Jan Möbius committed
170
171
172
173
174
175

//-----------------------------------------------------------------------------


bool
_OFFWriter_::
176
write_ascii(std::ostream& _out, BaseExporter& _be, Options _opt) const
Jan Möbius's avatar
Jan Möbius committed
177
178
{

Jan Möbius's avatar
Jan Möbius committed
179
  unsigned int i, nV, nF;
Jan Möbius's avatar
Jan Möbius committed
180
181
182
183
  Vec3f v, n;
  Vec2f t;
  OpenMesh::Vec3i c;
  OpenMesh::Vec4i cA;
184
185
  OpenMesh::Vec3f cf;
  OpenMesh::Vec4f cAf;
Jan Möbius's avatar
Jan Möbius committed
186
187
188
189
190
191
192
193
194
  VertexHandle vh;
  std::vector<VertexHandle> vhandles;


  // #vertices, #faces
  _out << _be.n_vertices() << " ";
  _out << _be.n_faces() << " ";
  _out << 0 << "\n";

195
196
197
  if (_opt.color_is_float())
    _out << std::fixed;

Jan Möbius's avatar
Jan Möbius committed
198
199

  // vertex data (point, normals, colors, texcoords)
200
  for (i=0, nV=int(_be.n_vertices()); i<nV; ++i)
Jan Möbius's avatar
Jan Möbius committed
201
202
203
  {
    vh = VertexHandle(i);
    v  = _be.point(vh);
204

Jan Möbius's avatar
Jan Möbius committed
205
206
    //Vertex
    _out << v[0] << " " << v[1] << " " << v[2];
207

Jan Möbius's avatar
Jan Möbius committed
208
209
210
211
212
    // VertexNormal
    if ( _opt.vertex_has_normal() ) {
      n  = _be.normal(vh);
      _out << " " << n[0] << " " << n[1] << " " << n[2];
    }
213

Jan Möbius's avatar
Jan Möbius committed
214
215
    // VertexColor
    if ( _opt.vertex_has_color() ) {
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
      if ( _opt.color_is_float() ) {
        //with alpha
        if ( _opt.color_has_alpha() ){
          cAf  = _be.colorAf(vh);
          _out << " " << cAf;
        }else{
          //without alpha
          cf  = _be.colorf(vh);
          _out << " " << cf;
        }
      } else {
        //with alpha
        if ( _opt.color_has_alpha() ){
          cA  = _be.colorA(vh);
          _out << " " << cA;
        }else{
          //without alpha
          c  = _be.color(vh);
          _out << " " << c;
        }
Jan Möbius's avatar
Jan Möbius committed
236
237
      }
    }
238

Jan Möbius's avatar
Jan Möbius committed
239
240
241
242
243
    // TexCoord
    if (_opt.vertex_has_texcoord() ) {
      t  = _be.texcoord(vh);
      _out << " " << t[0] << " " << t[1];
    }
244

Jan Möbius's avatar
Jan Möbius committed
245
    _out << "\n";
246

Jan Möbius's avatar
Jan Möbius committed
247
  }
248

Jan Möbius's avatar
Jan Möbius committed
249
250
251
  // faces (indices starting at 0)
  if (_be.is_triangle_mesh())
  {
252
    for (i=0, nF=int(_be.n_faces()); i<nF; ++i)
Jan Möbius's avatar
Jan Möbius committed
253
254
    {
      _be.get_vhandles(FaceHandle(i), vhandles);
255
      _out << 3 << " ";
Jan Möbius's avatar
Jan Möbius committed
256
      _out << vhandles[0].idx()  << " ";
257
      _out << vhandles[1].idx()  << " ";
Jan Möbius's avatar
Jan Möbius committed
258
259
260
261
      _out << vhandles[2].idx();

      //face color
      if ( _opt.face_has_color() ){
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
        if ( _opt.color_is_float() ) {
          //with alpha
          if ( _opt.color_has_alpha() ){
            cAf  = _be.colorAf( FaceHandle(i) );
            _out << " " << cAf;
          }else{
            //without alpha
            cf  = _be.colorf( FaceHandle(i) );
            _out << " " << cf;
          }
        } else {
          //with alpha
          if ( _opt.color_has_alpha() ){
            cA  = _be.colorA( FaceHandle(i) );
            _out << " " << cA;
          }else{
            //without alpha
            c  = _be.color( FaceHandle(i) );
            _out << " " << c;
          }
Jan Möbius's avatar
Jan Möbius committed
282
283
284
285
286
287
288
        }
      }
      _out << "\n";
    }
  }
  else
  {
289
    for (i=0, nF=int(_be.n_faces()); i<nF; ++i)
Jan Möbius's avatar
Jan Möbius committed
290
291
292
    {
      nV = _be.get_vhandles(FaceHandle(i), vhandles);
      _out << nV << " ";
Jan Möbius's avatar
Jan Möbius committed
293
      for (size_t j=0; j<vhandles.size(); ++j)
Jan Möbius's avatar
Jan Möbius committed
294
295
296
297
	       _out << vhandles[j].idx() << " ";

      //face color
      if ( _opt.face_has_color() ){
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
        if ( _opt.color_is_float() ) {
          //with alpha
          if ( _opt.color_has_alpha() ){
            cAf  = _be.colorAf( FaceHandle(i) );
            _out << " " << cAf;
          }else{
            //without alpha
            cf  = _be.colorf( FaceHandle(i) );
            _out << " " << cf;
          }
        } else {
          //with alpha
          if ( _opt.color_has_alpha() ){
            cA  = _be.colorA( FaceHandle(i) );
            _out << " " << cA;
          }else{
            //without alpha
            c  = _be.color( FaceHandle(i) );
            _out << " " << c;
          }
Jan Möbius's avatar
Jan Möbius committed
318
319
320
321
322
323
324
325
326
327
328
329
330
331
        }
      }

      _out << "\n";
    }
  }


  return true;
}


//-----------------------------------------------------------------------------

332
void _OFFWriter_::writeValue(std::ostream& _out, int value) const {
Jan Möbius's avatar
Jan Möbius committed
333
334
335
336
337

  uint32_t tmp = value;
  store(_out, tmp, false);
}

338
void _OFFWriter_::writeValue(std::ostream& _out, unsigned int value) const {
Jan Möbius's avatar
Jan Möbius committed
339
340
341
342
343

  uint32_t tmp = value;
  store(_out, tmp, false);
}

344
void _OFFWriter_::writeValue(std::ostream& _out, float value) const {
Jan Möbius's avatar
Jan Möbius committed
345
346
347
348
349

  float32_t tmp = value;
  store(_out, tmp, false);
}

350
bool
Jan Möbius's avatar
Jan Möbius committed
351
_OFFWriter_::
352
write_binary(std::ostream& _out, BaseExporter& _be, Options _opt) const
Jan Möbius's avatar
Jan Möbius committed
353
354
{

Jan Möbius's avatar
Jan Möbius committed
355
  unsigned int i, nV, nF;
Jan Möbius's avatar
Jan Möbius committed
356
357
358
  Vec3f v, n;
  Vec2f t;
  OpenMesh::Vec4i c;
359
  OpenMesh::Vec4f cf;
Jan Möbius's avatar
Jan Möbius committed
360
361
362
363
364
365
366
367
368
  VertexHandle vh;
  std::vector<VertexHandle> vhandles;

  // #vertices, #faces
  writeValue(_out, (uint)_be.n_vertices() );
  writeValue(_out, (uint) _be.n_faces() );
  writeValue(_out, 0 );

  // vertex data (point, normals, texcoords)
369
  for (i=0, nV=int(_be.n_vertices()); i<nV; ++i)
Jan Möbius's avatar
Jan Möbius committed
370
371
372
  {
    vh = VertexHandle(i);
    v  = _be.point(vh);
373

Jan Möbius's avatar
Jan Möbius committed
374
375
376
377
    //vertex
    writeValue(_out, v[0]);
    writeValue(_out, v[1]);
    writeValue(_out, v[2]);
378

Jan Möbius's avatar
Jan Möbius committed
379
380
381
382
383
384
385
386
387
    // vertex normal
    if ( _opt.vertex_has_normal() ) {
      n  = _be.normal(vh);
      writeValue(_out, n[0]);
      writeValue(_out, n[1]);
      writeValue(_out, n[2]);
    }
    // vertex color
    if ( _opt.vertex_has_color() ) {
388
389
390
391
392
393
394
395
396
      if ( _opt.color_is_float() ) {
        cf  = _be.colorAf(vh);
        writeValue(_out, cf[0]);
        writeValue(_out, cf[1]);
        writeValue(_out, cf[2]);

        if ( _opt.color_has_alpha() )
          writeValue(_out, cf[3]);
      } else {
Jan Möbius's avatar
Jan Möbius committed
397
398
399
400
401
402
403
        c  = _be.colorA(vh);
        writeValue(_out, c[0]);
        writeValue(_out, c[1]);
        writeValue(_out, c[2]);

        if ( _opt.color_has_alpha() )
          writeValue(_out, c[3]);
404
      }
Jan Möbius's avatar
Jan Möbius committed
405
406
407
408
409
410
411
    }
    // texCoords
    if (_opt.vertex_has_texcoord() ) {
      t  = _be.texcoord(vh);
      writeValue(_out, t[0]);
      writeValue(_out, t[1]);
    }
412

Jan Möbius's avatar
Jan Möbius committed
413
414
415
416
417
  }

  // faces (indices starting at 0)
  if (_be.is_triangle_mesh())
  {
418
    for (i=0, nF=int(_be.n_faces()); i<nF; ++i)
Jan Möbius's avatar
Jan Möbius committed
419
420
421
422
423
424
425
426
427
428
    {
      //face
      _be.get_vhandles(FaceHandle(i), vhandles);
      writeValue(_out, 3);
      writeValue(_out, vhandles[0].idx());
      writeValue(_out, vhandles[1].idx());
      writeValue(_out, vhandles[2].idx());

      //face color
      if ( _opt.face_has_color() ){
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
        if ( _opt.color_is_float() ) {
          cf  = _be.colorAf( FaceHandle(i) );
          writeValue(_out, cf[0]);
          writeValue(_out, cf[1]);
          writeValue(_out, cf[2]);

          if ( _opt.color_has_alpha() )
            writeValue(_out, cf[3]);
        } else {
          c  = _be.colorA( FaceHandle(i) );
          writeValue(_out, c[0]);
          writeValue(_out, c[1]);
          writeValue(_out, c[2]);

          if ( _opt.color_has_alpha() )
            writeValue(_out, c[3]);
        }
Jan Möbius's avatar
Jan Möbius committed
446
447
448
449
450
      }
    }
  }
  else
  {
451
    for (i=0, nF=int(_be.n_faces()); i<nF; ++i)
Jan Möbius's avatar
Jan Möbius committed
452
453
454
455
    {
      //face
      nV = _be.get_vhandles(FaceHandle(i), vhandles);
      writeValue(_out, nV);
Jan Möbius's avatar
Jan Möbius committed
456
      for (size_t j=0; j<vhandles.size(); ++j)
Jan Möbius's avatar
Jan Möbius committed
457
458
459
460
        writeValue(_out, vhandles[j].idx() );

      //face color
      if ( _opt.face_has_color() ){
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
        if ( _opt.color_is_float() ) {
          cf  = _be.colorAf( FaceHandle(i) );
          writeValue(_out, cf[0]);
          writeValue(_out, cf[1]);
          writeValue(_out, cf[2]);

          if ( _opt.color_has_alpha() )
            writeValue(_out, cf[3]);
        } else {
          c  = _be.colorA( FaceHandle(i) );
          writeValue(_out, c[0]);
          writeValue(_out, c[1]);
          writeValue(_out, c[2]);

          if ( _opt.color_has_alpha() )
            writeValue(_out, c[3]);
        }
Jan Möbius's avatar
Jan Möbius committed
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
      }
    }
  }

  return true;
}

// ----------------------------------------------------------------------------


size_t
_OFFWriter_::
binary_size(BaseExporter& _be, Options _opt) const
{
  size_t header(0);
  size_t data(0);
Jan Möbius's avatar
Jan Möbius committed
494

Jan Möbius's avatar
Jan Möbius committed
495
496
497
  size_t _3floats(3*sizeof(float));
  size_t _3ui(3*sizeof(unsigned int));
  size_t _4ui(4*sizeof(unsigned int));
498

Jan Möbius's avatar
Jan Möbius committed
499
500
501
502
  if ( !_opt.is_binary() )
    return 0;
  else
  {
Jan Möbius's avatar
Jan Möbius committed
503
504
    size_t _3longs(3*sizeof(long));

Jan Möbius's avatar
Jan Möbius committed
505
506
507
508
509
510
511
512
513
514
    header += 11;                             // 'OFF BINARY\n'
    header += _3longs;                        // #V #F #E
    data   += _be.n_vertices() * _3floats;    // vertex data
  }

  if ( _opt.vertex_has_normal() && _be.has_vertex_normals() )
  {
    header += 1; // N
    data   += _be.n_vertices() * _3floats;
  }
515

Jan Möbius's avatar
Jan Möbius committed
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
  if ( _opt.vertex_has_color() && _be.has_vertex_colors() )
  {
    header += 1; // C
    data   += _be.n_vertices() * _3floats;
  }

  if ( _opt.vertex_has_texcoord() && _be.has_vertex_texcoords() )
  {
    size_t _2floats(2*sizeof(float));
    header += 2; // ST
    data   += _be.n_vertices() * _2floats;
  }

  // topology
  if (_be.is_triangle_mesh())
  {
    data += _be.n_faces() * _4ui;
  }
  else
  {
Jan Möbius's avatar
Jan Möbius committed
536
    unsigned int i, nF;
Jan Möbius's avatar
Jan Möbius committed
537
538
    std::vector<VertexHandle> vhandles;

539
    for (i=0, nF=int(_be.n_faces()); i<nF; ++i)
Jan Möbius's avatar
Jan Möbius committed
540
      data += _be.get_vhandles(FaceHandle(i), vhandles) * sizeof(unsigned int);
Jan Möbius's avatar
Jan Möbius committed
541
542

  }
543

Jan Möbius's avatar
Jan Möbius committed
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
  // face colors
  if ( _opt.face_has_color() && _be.has_face_colors() ){
    if ( _opt.color_has_alpha() )
      data += _be.n_faces() * _4ui;
    else
      data += _be.n_faces() * _3ui;
  }

  return header+data;
}


//=============================================================================
} // namespace IO
} // namespace OpenMesh
//=============================================================================