OFFWriter.cc 14.8 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1
/* ========================================================================= *
Jan Möbius's avatar
Jan Möbius committed
2
3
 *                                                                           *
 *                               OpenMesh                                    *
Jan Möbius's avatar
Jan Möbius committed
4
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
Jan Möbius's avatar
Typo    
Jan Möbius committed
5
 *           Department of Computer Graphics and Multimedia                  *
Jan Möbius's avatar
Jan Möbius committed
6
7
 *                          All rights reserved.                             *
 *                            www.openmesh.org                               *
Jan Möbius's avatar
Jan Möbius committed
8
 *                                                                           *
9
 *---------------------------------------------------------------------------*
Jan Möbius's avatar
Jan Möbius committed
10
11
 * This file is part of OpenMesh.                                            *
 *---------------------------------------------------------------------------*
12
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
Jan Möbius's avatar
Jan Möbius committed
39
40
 *                                                                           *
 * ========================================================================= */
41
42

/*===========================================================================*\
43
 *                                                                           *
44
45
 *   $Revision$                                                         *
 *   $Date$                   *
Jan Möbius's avatar
Jan Möbius committed
46
47
48
49
50
51
 *                                                                           *
\*===========================================================================*/


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

Matthias Möller's avatar
Matthias Möller committed
52
#include <fstream>
Jan Möbius's avatar
Jan Möbius committed
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
81
82
83
84
85
#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>

//=== 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_::
86
write(const std::string& _filename, BaseExporter& _be, Options _opt, std::streamsize _precision) const
Jan Möbius's avatar
Jan Möbius committed
87
{
Matthias Möller's avatar
Matthias Möller committed
88
89
  std::ofstream out(_filename.c_str(), (_opt.check(Options::Binary) ? std::ios_base::binary | std::ios_base::out
                                                           : std::ios_base::out) );
Jan Möbius's avatar
Jan Möbius committed
90

Matthias Möller's avatar
Matthias Möller committed
91
  return write(out, _be, _opt, _precision);
Jan Möbius's avatar
Jan Möbius committed
92
93
}

94
95
96
97
98
//-----------------------------------------------------------------------------


bool
_OFFWriter_::
99
write(std::ostream& _os, BaseExporter& _be, Options _opt, std::streamsize _precision) const
100
101
102
103
104
105
106
107
108
{
  // check exporter features
  if ( !check( _be, _opt ) )
    return false;


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


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
  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";

126
127
  if (!_opt.check(Options::Binary))
    _os.precision(_precision);
128
129
130
131
132
133
134
135
136
137

  // 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
138
139
140
141
142
143

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


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

Jan Möbius's avatar
Jan Möbius committed
147
  unsigned int i, nV, nF;
Jan Möbius's avatar
Jan Möbius committed
148
149
150
151
  Vec3f v, n;
  Vec2f t;
  OpenMesh::Vec3i c;
  OpenMesh::Vec4i cA;
152
153
  OpenMesh::Vec3f cf;
  OpenMesh::Vec4f cAf;
Jan Möbius's avatar
Jan Möbius committed
154
155
156
157
158
159
160
161
162
  VertexHandle vh;
  std::vector<VertexHandle> vhandles;


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

163
164
165
  if (_opt.color_is_float())
    _out << std::fixed;

Jan Möbius's avatar
Jan Möbius committed
166
167

  // vertex data (point, normals, colors, texcoords)
168
  for (i=0, nV=int(_be.n_vertices()); i<nV; ++i)
Jan Möbius's avatar
Jan Möbius committed
169
170
171
  {
    vh = VertexHandle(i);
    v  = _be.point(vh);
172

Jan Möbius's avatar
Jan Möbius committed
173
174
    //Vertex
    _out << v[0] << " " << v[1] << " " << v[2];
175

Jan Möbius's avatar
Jan Möbius committed
176
177
178
179
180
    // VertexNormal
    if ( _opt.vertex_has_normal() ) {
      n  = _be.normal(vh);
      _out << " " << n[0] << " " << n[1] << " " << n[2];
    }
181

Jan Möbius's avatar
Jan Möbius committed
182
183
    // VertexColor
    if ( _opt.vertex_has_color() ) {
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
      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
204
205
      }
    }
206

Jan Möbius's avatar
Jan Möbius committed
207
208
209
210
211
    // TexCoord
    if (_opt.vertex_has_texcoord() ) {
      t  = _be.texcoord(vh);
      _out << " " << t[0] << " " << t[1];
    }
212

213
    _out << '\n';
214

Jan Möbius's avatar
Jan Möbius committed
215
  }
216

Jan Möbius's avatar
Jan Möbius committed
217
218
219
  // faces (indices starting at 0)
  if (_be.is_triangle_mesh())
  {
220
    for (i=0, nF=int(_be.n_faces()); i<nF; ++i)
Jan Möbius's avatar
Jan Möbius committed
221
222
    {
      _be.get_vhandles(FaceHandle(i), vhandles);
223
      _out << 3 << " ";
Jan Möbius's avatar
Jan Möbius committed
224
      _out << vhandles[0].idx()  << " ";
225
      _out << vhandles[1].idx()  << " ";
Jan Möbius's avatar
Jan Möbius committed
226
227
228
229
      _out << vhandles[2].idx();

      //face color
      if ( _opt.face_has_color() ){
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
        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
250
251
        }
      }
252
      _out << '\n';
Jan Möbius's avatar
Jan Möbius committed
253
254
255
256
    }
  }
  else
  {
257
    for (i=0, nF=int(_be.n_faces()); i<nF; ++i)
Jan Möbius's avatar
Jan Möbius committed
258
259
260
    {
      nV = _be.get_vhandles(FaceHandle(i), vhandles);
      _out << nV << " ";
Jan Möbius's avatar
Jan Möbius committed
261
      for (size_t j=0; j<vhandles.size(); ++j)
Jan Möbius's avatar
Jan Möbius committed
262
263
264
265
	       _out << vhandles[j].idx() << " ";

      //face color
      if ( _opt.face_has_color() ){
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
        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
286
287
288
        }
      }

289
      _out << '\n';
Jan Möbius's avatar
Jan Möbius committed
290
291
292
293
294
295
296
297
298
299
    }
  }


  return true;
}


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

300
void _OFFWriter_::writeValue(std::ostream& _out, int value) const {
Jan Möbius's avatar
Jan Möbius committed
301
302
303
304
305

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

306
void _OFFWriter_::writeValue(std::ostream& _out, unsigned int value) const {
Jan Möbius's avatar
Jan Möbius committed
307
308
309
310
311

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

312
void _OFFWriter_::writeValue(std::ostream& _out, float value) const {
Jan Möbius's avatar
Jan Möbius committed
313
314
315
316
317

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

318
bool
Jan Möbius's avatar
Jan Möbius committed
319
_OFFWriter_::
320
write_binary(std::ostream& _out, BaseExporter& _be, Options _opt) const
Jan Möbius's avatar
Jan Möbius committed
321
322
{

Jan Möbius's avatar
Jan Möbius committed
323
  unsigned int i, nV, nF;
Jan Möbius's avatar
Jan Möbius committed
324
325
326
  Vec3f v, n;
  Vec2f t;
  OpenMesh::Vec4i c;
327
  OpenMesh::Vec4f cf;
Jan Möbius's avatar
Jan Möbius committed
328
329
330
331
332
333
334
335
336
  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)
337
  for (i=0, nV=int(_be.n_vertices()); i<nV; ++i)
Jan Möbius's avatar
Jan Möbius committed
338
339
340
  {
    vh = VertexHandle(i);
    v  = _be.point(vh);
341

Jan Möbius's avatar
Jan Möbius committed
342
343
344
345
    //vertex
    writeValue(_out, v[0]);
    writeValue(_out, v[1]);
    writeValue(_out, v[2]);
346

Jan Möbius's avatar
Jan Möbius committed
347
348
349
350
351
352
353
354
355
    // 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() ) {
356
357
358
359
360
361
362
363
364
      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
365
366
367
368
369
370
371
        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]);
372
      }
Jan Möbius's avatar
Jan Möbius committed
373
374
375
376
377
378
379
    }
    // texCoords
    if (_opt.vertex_has_texcoord() ) {
      t  = _be.texcoord(vh);
      writeValue(_out, t[0]);
      writeValue(_out, t[1]);
    }
380

Jan Möbius's avatar
Jan Möbius committed
381
382
383
384
385
  }

  // faces (indices starting at 0)
  if (_be.is_triangle_mesh())
  {
386
    for (i=0, nF=int(_be.n_faces()); i<nF; ++i)
Jan Möbius's avatar
Jan Möbius committed
387
388
389
390
391
392
393
394
395
396
    {
      //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() ){
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
        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
414
415
416
417
418
      }
    }
  }
  else
  {
419
    for (i=0, nF=int(_be.n_faces()); i<nF; ++i)
Jan Möbius's avatar
Jan Möbius committed
420
421
422
423
    {
      //face
      nV = _be.get_vhandles(FaceHandle(i), vhandles);
      writeValue(_out, nV);
Jan Möbius's avatar
Jan Möbius committed
424
      for (size_t j=0; j<vhandles.size(); ++j)
Jan Möbius's avatar
Jan Möbius committed
425
426
427
428
        writeValue(_out, vhandles[j].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
451
452
453
454
455
456
457
458
459
460
461
      }
    }
  }

  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
462

Jan Möbius's avatar
Jan Möbius committed
463
464
465
  size_t _3floats(3*sizeof(float));
  size_t _3ui(3*sizeof(unsigned int));
  size_t _4ui(4*sizeof(unsigned int));
466

Jan Möbius's avatar
Jan Möbius committed
467
468
469
470
  if ( !_opt.is_binary() )
    return 0;
  else
  {
Jan Möbius's avatar
Jan Möbius committed
471
472
    size_t _3longs(3*sizeof(long));

Jan Möbius's avatar
Jan Möbius committed
473
474
475
476
477
478
479
480
481
482
    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;
  }
483

Jan Möbius's avatar
Jan Möbius committed
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
  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
504
    unsigned int i, nF;
Jan Möbius's avatar
Jan Möbius committed
505
506
    std::vector<VertexHandle> vhandles;

507
    for (i=0, nF=int(_be.n_faces()); i<nF; ++i)
Jan Möbius's avatar
Jan Möbius committed
508
      data += _be.get_vhandles(FaceHandle(i), vhandles) * sizeof(unsigned int);
Jan Möbius's avatar
Jan Möbius committed
509
510

  }
511

Jan Möbius's avatar
Jan Möbius committed
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
  // 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
//=============================================================================