PLYWriter.cc 16.7 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1
2
3
/*===========================================================================*\
 *                                                                           *
 *                               OpenMesh                                    *
Jan Möbius's avatar
Jan Möbius committed
4
 *      Copyright (C) 2001-2012 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/PLYWriter.hh>

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

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


namespace OpenMesh {
namespace IO {


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


// register the PLYLoader singleton with MeshLoader
_PLYWriter_  __PLYWriterInstance;
_PLYWriter_& PLYWriter() { return __PLYWriterInstance; }


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


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


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


bool
_PLYWriter_::
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
{
  // check exporter features
  if ( !check( _be, _opt ) )
    return false;


  // check writer features
89
90
91
92
93
94
95
  if ( _opt.check(Options::FaceNormal) ) {
    // Face normals are not supported
    // Uncheck these options and output message that
    // they are not written out even though they were requested
    _opt.unset(Options::FaceNormal);
    omerr() << "[PLYWriter] : Warning: Face normals are not supported and thus not exported! " << std::endl;
  }
96

97
98
99
100
101
102
103
  if ( _opt.check(Options::FaceColor) ) {
    // Face normals are not supported
    // Uncheck these options and output message that
    // they are not written out even though they were requested
    _opt.unset(Options::FaceColor);
    omerr() << "[PLYWriter] : Warning: Face colors are not supported and thus not exported! " << std::endl;
  }
Jan Möbius's avatar
Jan Möbius committed
104
105
106
107
108
109
110
111
112
113
114
115
116
117

  options_ = _opt;

  // 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() << "[PLYWriter] : cannot open file "
    << _filename
    << std::endl;
    return false;
  }

118
119
  if (!_opt.check(Options::Binary))
    out.precision(_precision);
120

Jan Möbius's avatar
Jan Möbius committed
121
122
123
124
125
126
127
128
129
130
  // 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;
}

131
132
133
134
135
//-----------------------------------------------------------------------------


bool
_PLYWriter_::
136
write(std::ostream& _os, BaseExporter& _be, Options _opt, std::streamsize _precision) const
137
138
139
140
141
142
143
144
145
146
147
148
{
  // check exporter features
  if ( !check( _be, _opt ) )
    return false;


  // check writer features
  if ( _opt.check(Options::FaceNormal) || _opt.check(Options::FaceColor) ) // not supported yet
    return false;

  options_ = _opt;

149

150
151
152
153
154
155
156
  if (!_os.good())
  {
    omerr() << "[PLYWriter] : cannot write to stream "
    << std::endl;
    return false;
  }

157
158
  if (!_opt.check(Options::Binary))
    _os.precision(_precision);
159

160
161
162
163
164
165
166
167
  // write to file
  bool result = (_opt.check(Options::Binary) ?
     write_binary(_os, _be, _opt) :
     write_ascii(_os, _be, _opt));

  return result;
}

Jan Möbius's avatar
Jan Möbius committed
168
169
170
171
172
173

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


bool
_PLYWriter_::
174
write_ascii(std::ostream& _out, BaseExporter& _be, Options _opt) const
Jan Möbius's avatar
Jan Möbius committed
175
176
177
178
179
180
{
  omlog() << "[PLYWriter] : write ascii file\n";


  unsigned int i, j, nV, nF;
  Vec3f v, n;
181
182
  OpenMesh::Vec3ui c;
  OpenMesh::Vec4ui cA;
183
  OpenMesh::Vec2f t;
Jan Möbius's avatar
Jan Möbius committed
184
185
186
187
188
189
190
191
  VertexHandle vh;
  std::vector<VertexHandle> vhandles;

  //writing header
  _out << "ply" << std::endl;
  _out << "format ascii 1.0" << std::endl;
  _out << "element vertex " << _be.n_vertices() << std::endl;

192
193
194
  _out << "property float x" << std::endl;
  _out << "property float y" << std::endl;
  _out << "property float z" << std::endl;
Jan Möbius's avatar
Jan Möbius committed
195

196
  if ( _opt.vertex_has_normal() ){
197
198
199
    _out << "property float nx" << std::endl;
    _out << "property float ny" << std::endl;
    _out << "property float nz" << std::endl;
200
201
  }

202
  if ( _opt.vertex_has_texcoord() ){
203
204
    _out << "property float u" << std::endl;
    _out << "property float v" << std::endl;
205
206
  }

Jan Möbius's avatar
Jan Möbius committed
207
  if ( _opt.vertex_has_color() ){
208
209
210
    _out << "property uchar red" << std::endl;
    _out << "property uchar green" << std::endl;
    _out << "property uchar blue" << std::endl;
Jan Möbius's avatar
Jan Möbius committed
211
212

    if ( _opt.color_has_alpha() )
213
      _out << "property uchar alpha" << std::endl;
Jan Möbius's avatar
Jan Möbius committed
214
215
216
  }

  _out << "element face " << _be.n_faces() << std::endl;
217
  _out << "property list uchar int vertex_indices" << std::endl;
Jan Möbius's avatar
Jan Möbius committed
218
219
220
221
222
223
224
225
226
227
228
  _out << "end_header" << std::endl;

  // vertex data (point, normals, colors, texcoords)
  for (i=0, nV=_be.n_vertices(); i<nV; ++i)
  {
    vh = VertexHandle(i);
    v  = _be.point(vh);

    //Vertex
    _out << v[0] << " " << v[1] << " " << v[2];

229
230
231
232
233
234
    // Vertex Normals
    if ( _opt.vertex_has_normal() ){
      n = _be.normal(vh);
      _out << " " << n[0] << " " << n[1] << " " << n[2];
    }

235
236
237
238
239
240
    // Vertex TexCoords
    if ( _opt.vertex_has_texcoord() ) {
    	t = _be.texcoord(vh);
    	_out << " " << t[0] << " " << t[1];
    }

Jan Möbius's avatar
Jan Möbius committed
241
242
243
244
    // VertexColor
    if ( _opt.vertex_has_color() ) {
      //with alpha
      if ( _opt.color_has_alpha() ){
245
246
        cA  = _be.colorAi(vh);
        _out << " " << cA;
Jan Möbius's avatar
Jan Möbius committed
247
248
      }else{
        //without alpha
249
250
        c  = _be.colori(vh);
        _out << " " << c;
Jan Möbius's avatar
Jan Möbius committed
251
252
253
254
255
      }
    }

    _out << "\n";
  }
256

Jan Möbius's avatar
Jan Möbius committed
257
258
259
260
261
262
  // faces (indices starting at 0)
  if (_be.is_triangle_mesh())
  {
    for (i=0, nF=_be.n_faces(); i<nF; ++i)
    {
      _be.get_vhandles(FaceHandle(i), vhandles);
263
      _out << 3 << " ";
Jan Möbius's avatar
Jan Möbius committed
264
      _out << vhandles[0].idx()  << " ";
265
      _out << vhandles[1].idx()  << " ";
Jan Möbius's avatar
Jan Möbius committed
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
      _out << vhandles[2].idx();

//       //face color
//       if ( _opt.face_has_color() ){
//         //with alpha
//         if ( _opt.color_has_alpha() ){
//           cA  = _be.colorA( FaceHandle(i) );
//           _out << " " << cA[0] << " " << cA[1] << " " << cA[2] << " " << cA[3];
//         }else{
//           //without alpha
//           c  = _be.color( FaceHandle(i) );
//           _out << " " << c[0] << " " << c[1] << " " << c[2];
//         }
//       }
      _out << "\n";
    }
  }
  else
  {
    for (i=0, nF=_be.n_faces(); i<nF; ++i)
    {
      nV = _be.get_vhandles(FaceHandle(i), vhandles);
      _out << nV << " ";
      for (j=0; j<vhandles.size(); ++j)
         _out << vhandles[j].idx() << " ";

//       //face color
//       if ( _opt.face_has_color() ){
//         //with alpha
//         if ( _opt.color_has_alpha() ){
//           cA  = _be.colorA( FaceHandle(i) );
//           _out << cA[0] << " " << cA[1] << " " << cA[2] << " " << cA[3];
//         }else{
//           //without alpha
//           c  = _be.color( FaceHandle(i) );
//           _out << c[0] << " " << c[1] << " " << c[2];
//         }
//       }

      _out << "\n";
    }
  }


  return true;
}


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

316
void _PLYWriter_::writeValue(ValueType _type, std::ostream& _out, int value) const {
Jan Möbius's avatar
Jan Möbius committed
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337

  uint32_t tmp32;
  uint8_t tmp8;

  switch (_type) {
    case ValueTypeINT:
    case ValueTypeINT32:
      tmp32 = value;
      store(_out, tmp32, options_.check(Options::MSB) );
      break;
//     case ValueTypeUINT8:
default :
      tmp8 = value;
      store(_out, tmp8, options_.check(Options::MSB) );
      break;
//     default :
//       std::cerr << "unsupported conversion type to int: " << _type << std::endl;
//       break;
  }
}

338
void _PLYWriter_::writeValue(ValueType _type, std::ostream& _out, unsigned int value) const {
Jan Möbius's avatar
Jan Möbius committed
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359

  uint32_t tmp32;
  uint8_t tmp8;

  switch (_type) {
    case ValueTypeINT:
    case ValueTypeINT32:
      tmp32 = value;
      store(_out, tmp32, options_.check(Options::MSB) );
      break;
//     case ValueTypeUINT8:
default :
      tmp8 = value;
      store(_out, tmp8, options_.check(Options::MSB) );
      break;
//     default :
//       std::cerr << "unsupported conversion type to int: " << _type << std::endl;
//       break;
  }
}

360
void _PLYWriter_::writeValue(ValueType _type, std::ostream& _out, float value) const {
Jan Möbius's avatar
Jan Möbius committed
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375

  float32_t tmp;

  switch (_type) {
    case ValueTypeFLOAT32:
    case ValueTypeFLOAT:
      tmp = value;
      store( _out , tmp, options_.check(Options::MSB) );
      break;
    default :
      std::cerr << "unsupported conversion type to float: " << _type << std::endl;
      break;
  }
}

376
bool
Jan Möbius's avatar
Jan Möbius committed
377
_PLYWriter_::
378
write_binary(std::ostream& _out, BaseExporter& _be, Options _opt) const
Jan Möbius's avatar
Jan Möbius committed
379
380
381
382
383
384
{
  omlog() << "[PLYWriter] : write binary file\n";

  unsigned int i, j, nV, nF;
  Vec3f v, n;
  Vec2f t;
385
  OpenMesh::Vec4uc c;
Jan Möbius's avatar
Jan Möbius committed
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
  VertexHandle vh;
  std::vector<VertexHandle> vhandles;

  //writing header
  _out << "ply" << std::endl;
  _out << "format ";

  if ( options_.check(Options::MSB) )
    _out << "binary_big_endian ";
  else
    _out << "binary_little_endian ";

  _out << "1.0" << std::endl;

  _out << "element vertex " << _be.n_vertices() << std::endl;

402
403
404
  _out << "property float x" << std::endl;
  _out << "property float y" << std::endl;
  _out << "property float z" << std::endl;
405

406
  if ( _opt.vertex_has_normal() ){
407
408
409
    _out << "property float nx" << std::endl;
    _out << "property float ny" << std::endl;
    _out << "property float nz" << std::endl;
410
  }
Jan Möbius's avatar
Jan Möbius committed
411

412
  if ( _opt.vertex_has_texcoord() ){
413
414
    _out << "property float u" << std::endl;
    _out << "property float v" << std::endl;
415
416
  }

Jan Möbius's avatar
Jan Möbius committed
417
  if ( _opt.vertex_has_color() ){
418
419
420
    _out << "property uchar red" << std::endl;
    _out << "property uchar green" << std::endl;
    _out << "property uchar blue" << std::endl;
Jan Möbius's avatar
Jan Möbius committed
421
422

    if ( _opt.color_has_alpha() )
423
      _out << "property uchar alpha" << std::endl;
Jan Möbius's avatar
Jan Möbius committed
424
425
426
  }

  _out << "element face " << _be.n_faces() << std::endl;
427
  _out << "property list uchar int vertex_indices" << std::endl;
Jan Möbius's avatar
Jan Möbius committed
428
  _out << "end_header" << std::endl;
429

Jan Möbius's avatar
Jan Möbius committed
430
431
432
433
434
  // vertex data (point, normals, texcoords)
  for (i=0, nV=_be.n_vertices(); i<nV; ++i)
  {
    vh = VertexHandle(i);
    v  = _be.point(vh);
435

Jan Möbius's avatar
Jan Möbius committed
436
437
438
439
    //vertex
    writeValue(ValueTypeFLOAT, _out, v[0]);
    writeValue(ValueTypeFLOAT, _out, v[1]);
    writeValue(ValueTypeFLOAT, _out, v[2]);
440

441
442
443
444
445
446
447
448
    // Vertex Normal
    if ( _opt.vertex_has_normal() ){
      n = _be.normal(vh);
      writeValue(ValueTypeFLOAT, _out, n[0]);
      writeValue(ValueTypeFLOAT, _out, n[1]);
      writeValue(ValueTypeFLOAT, _out, n[2]);
    }

449
450
451
452
453
454
455
    // Vertex TexCoords
    if ( _opt.vertex_has_texcoord() ) {
    	t = _be.texcoord(vh);
    	writeValue(ValueTypeFLOAT, _out, t[0]);
    	writeValue(ValueTypeFLOAT, _out, t[1]);
    }

Jan Möbius's avatar
Jan Möbius committed
456
457
458
    // vertex color
    if ( _opt.vertex_has_color() ) {
        c  = _be.colorA(vh);
459
460
461
        writeValue(ValueTypeUCHAR, _out, (int)c[0]);
        writeValue(ValueTypeUCHAR, _out, (int)c[1]);
        writeValue(ValueTypeUCHAR, _out, (int)c[2]);
Jan Möbius's avatar
Jan Möbius committed
462
463

        if ( _opt.color_has_alpha() )
464
          writeValue(ValueTypeUCHAR, _out, (int)c[3]);
Jan Möbius's avatar
Jan Möbius committed
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
    }
  }

  // faces (indices starting at 0)
  if (_be.is_triangle_mesh())
  {
    for (i=0, nF=_be.n_faces(); i<nF; ++i)
    {
      //face
      _be.get_vhandles(FaceHandle(i), vhandles);
      writeValue(ValueTypeUINT8, _out, 3);
      writeValue(ValueTypeINT32, _out, vhandles[0].idx());
      writeValue(ValueTypeINT32, _out, vhandles[1].idx());
      writeValue(ValueTypeINT32, _out, vhandles[2].idx());

//       //face color
//       if ( _opt.face_has_color() ){
//         c  = _be.colorA( FaceHandle(i) );
//         writeValue(_out, c[0]);
//         writeValue(_out, c[1]);
//         writeValue(_out, c[2]);
486
//
Jan Möbius's avatar
Jan Möbius committed
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
//         if ( _opt.color_has_alpha() )
//           writeValue(_out, c[3]);
//       }
    }
  }
  else
  {
    for (i=0, nF=_be.n_faces(); i<nF; ++i)
    {
      //face
      nV = _be.get_vhandles(FaceHandle(i), vhandles);
      writeValue(ValueTypeUINT8, _out, nV);
      for (j=0; j<vhandles.size(); ++j)
        writeValue(ValueTypeINT32, _out, vhandles[j].idx() );

//       //face color
//       if ( _opt.face_has_color() ){
//         c  = _be.colorA( FaceHandle(i) );
//         writeValue(_out, c[0]);
//         writeValue(_out, c[1]);
//         writeValue(_out, c[2]);
508
//
Jan Möbius's avatar
Jan Möbius committed
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
//         if ( _opt.color_has_alpha() )
//           writeValue(_out, c[3]);
//       }
    }
  }

  return true;
}

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


size_t
_PLYWriter_::
binary_size(BaseExporter& _be, Options _opt) const
{
  size_t header(0);
  size_t data(0);
  size_t _3longs(3*sizeof(long));
  size_t _3floats(3*sizeof(float));
  size_t _3ui(3*sizeof(unsigned int));
  size_t _4ui(4*sizeof(unsigned int));
531

Jan Möbius's avatar
Jan Möbius committed
532
533
534
535
536
537
538
539
540
541
542
543
544
545
  if ( !_opt.is_binary() )
    return 0;
  else
  {
    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;
  }
546

Jan Möbius's avatar
Jan Möbius committed
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
  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
  {
    unsigned int i, nV, nF;
    std::vector<VertexHandle> vhandles;

    for (i=0, nF=_be.n_faces(); i<nF; ++i)
    {
      nV = _be.get_vhandles(FaceHandle(i), vhandles);
      data += nV * sizeof(unsigned int);

    }
  }
577

Jan Möbius's avatar
Jan Möbius committed
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
  // 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
//=============================================================================