OMReader.cc 16.2 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
 *                                                                           *
\*===========================================================================*/


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


//STL
#include <fstream>

// OpenMesh
#include <OpenMesh/Core/System/config.h>
#include <OpenMesh/Core/System/omstream.hh>
#include <OpenMesh/Core/Utils/Endian.hh>
#include <OpenMesh/Core/IO/OMFormat.hh>
#include <OpenMesh/Core/IO/reader/OMReader.hh>


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


namespace OpenMesh {
namespace IO {


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


// register the OMReader singleton with MeshReader
_OMReader_  __OMReaderInstance;
_OMReader_&  OMReader() { return __OMReaderInstance; }



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


_OMReader_::_OMReader_()
{
78
  IOManager().register_module(this);
Jan Möbius's avatar
Jan Möbius committed
79
80
81
82
83
84
}


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


85
bool _OMReader_::read(const std::string& _filename, BaseImporter& _bi, Options& _opt)
Jan Möbius's avatar
Jan Möbius committed
86
87
{
  // check whether importer can give us an OpenMesh BaseKernel
88
89
  if (!_bi.kernel())
    return false;
Jan Möbius's avatar
Jan Möbius committed
90
91
92
93

  _opt += Options::Binary; // only binary format supported!

  // Open file
94
95
96
  std::ifstream ifs(_filename.c_str(), std::ios::binary);
  if (!ifs.is_open() || !ifs.good()) {
    omerr() << "[OMReader] : cannot not open file " << _filename << std::endl;
Jan Möbius's avatar
Jan Möbius committed
97
98
    return false;
  }
99

Jan Möbius's avatar
Jan Möbius committed
100
101
  // Pass stream to read method, remember result
  bool result = read(ifs, _bi, _opt);
102

Jan Möbius's avatar
Jan Möbius committed
103
104
  // close input stream
  ifs.close();
105

Jan Möbius's avatar
Jan Möbius committed
106
107
108
109
110
111
  return result;
}

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


112
bool _OMReader_::read(std::istream& _is, BaseImporter& _bi, Options& _opt)
Jan Möbius's avatar
Jan Möbius committed
113
{
114
  // check whether importer can give us an OpenMesh BaseKernel
115
116
  if (!_bi.kernel())
    return false;
117
118
119

  _opt += Options::Binary; // only binary format supported!

120
121
  if (!_is.good()) {
    omerr() << "[OMReader] : cannot read from stream " << std::endl;
122
123
124
125
126
127
    return false;
  }

  // Pass stream to read method, remember result
  bool result = read_binary(_is, _bi, _opt);

128
129
  if (result)
    _opt += Options::Binary;
Jan Möbius's avatar
Jan Möbius committed
130

131
  return result;
Jan Möbius's avatar
Jan Möbius committed
132
133
134
135
136
137
}



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

138
bool _OMReader_::read_ascii(std::istream& /* _is */, BaseImporter& /* _bi */, Options& /* _opt */) const
Jan Möbius's avatar
Jan Möbius committed
139
140
141
142
143
144
145
146
{
  // not supported yet!
  return false;
}


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

147
bool _OMReader_::read_binary(std::istream& _is, BaseImporter& _bi, Options& _opt) const
Jan Möbius's avatar
Jan Möbius committed
148
149
150
{
  bool swap = _opt.check(Options::Swap) || (Endian::local() == Endian::MSB);

151
  // Initialize byte counter
152
  bytes_ = 0;
Jan Möbius's avatar
Jan Möbius committed
153

154
  bytes_ += restore(_is, header_, swap);
Jan Möbius's avatar
Jan Möbius committed
155
156
157

  size_t data_bytes;

158
159
  while (!_is.eof()) {
    bytes_ += restore(_is, chunk_header_, swap);
Jan Möbius's avatar
Jan Möbius committed
160

161
    if (_is.eof())
Jan Möbius's avatar
Jan Möbius committed
162
163
164
      break;

    // Is this a named property restore the name
165
    if (chunk_header_.name_) {
Jan Möbius's avatar
Jan Möbius committed
166
      OMFormat::Chunk::PropertyName pn;
167
      bytes_ += restore(_is, property_name_, swap);
Jan Möbius's avatar
Jan Möbius committed
168
169
170
171
172
    }

    // Read in the property data. If it is an anonymous or unknown named
    // property, then skip data.
    data_bytes = bytes_;
173
    switch (chunk_header_.entity_) {
174
      case OMFormat::Chunk::Entity_Vertex:
175
176
177
        if (!read_binary_vertex_chunk(_is, _bi, _opt, swap))
          return false;
        break;
Jan Möbius's avatar
Jan Möbius committed
178
      case OMFormat::Chunk::Entity_Face:
179
180
181
        if (!read_binary_face_chunk(_is, _bi, _opt, swap))
          return false;
        break;
Jan Möbius's avatar
Jan Möbius committed
182
      case OMFormat::Chunk::Entity_Edge:
183
184
185
        if (!read_binary_edge_chunk(_is, _bi, _opt, swap))
          return false;
        break;
Jan Möbius's avatar
Jan Möbius committed
186
      case OMFormat::Chunk::Entity_Halfedge:
187
188
189
        if (!read_binary_halfedge_chunk(_is, _bi, _opt, swap))
          return false;
        break;
Jan Möbius's avatar
Jan Möbius committed
190
      case OMFormat::Chunk::Entity_Mesh:
191
192
193
        if (!read_binary_mesh_chunk(_is, _bi, _opt, swap))
          return false;
        break;
Jan Möbius's avatar
Jan Möbius committed
194
      default:
195
        return false;
Jan Möbius's avatar
Jan Möbius committed
196
197
198
199
200
201
202
203
204
205
206
    }
    data_bytes = bytes_ - data_bytes;
  }

  // File was successfully parsed.
  return true;
}


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

207
bool _OMReader_::can_u_read(const std::string& _filename) const
Jan Möbius's avatar
Jan Möbius committed
208
209
210
{
  // !!! Assuming BaseReader::can_u_parse( std::string& )
  // does not call BaseReader::read_magic()!!!
211
212
213
  if (this->BaseReader::can_u_read(_filename)) {
    std::ifstream ifile(_filename.c_str());
    if (ifile && can_u_read(ifile))
214
      return true;
Jan Möbius's avatar
Jan Möbius committed
215
216
217
218
219
  }
  return false;
}

//-----------------------------------------------------------------------------
220

221
bool _OMReader_::can_u_read(std::istream& _is) const
Jan Möbius's avatar
Jan Möbius committed
222
223
224
225
226
{
  std::vector<char> evt;
  evt.reserve(20);

  // read first 4 characters into a buffer
227
228
  while (evt.size() < 4)
    evt.push_back(static_cast<char>(_is.get()));
Jan Möbius's avatar
Jan Möbius committed
229

230
  // put back all read characters
Jan Möbius's avatar
Jan Möbius committed
231
  std::vector<char>::reverse_iterator it = evt.rbegin();
232
233
  while (it != evt.rend())
    _is.putback(*it++);
Jan Möbius's avatar
Jan Möbius committed
234
235

  // evaluate header information
236
  OMFormat::Header *hdr = (OMFormat::Header*) &evt[0];
Jan Möbius's avatar
Jan Möbius committed
237
238
239
240
241
242

  // first two characters must be 'OM'
  if (hdr->magic_[0] != 'O' || hdr->magic_[1] != 'M')
    return false;

  // 3rd characters defines the mesh type:
243
  switch (hdr->mesh_) {
Jan Möbius's avatar
Jan Möbius committed
244
245
246
247
248
249
250
    case 'T': // Triangle Mesh
    case 'Q': // Quad Mesh
    case 'P': // Polygonal Mesh
      break;
    default:  // ?
      return false;
  }
251

Jan Möbius's avatar
Jan Möbius committed
252
  // 4th characters encodes the version
253
  return supports(hdr->version_);
Jan Möbius's avatar
Jan Möbius committed
254
255
256
257
}

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

258
bool _OMReader_::supports(const OMFormat::uint8 /* version */) const
Jan Möbius's avatar
Jan Möbius committed
259
260
261
262
263
264
265
{
  return true;
}


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

266
bool _OMReader_::read_binary_vertex_chunk(std::istream &_is, BaseImporter &_bi, Options &_opt, bool _swap) const
Jan Möbius's avatar
Jan Möbius committed
267
268
269
{
  using OMFormat::Chunk;

270
  assert( chunk_header_.entity_ == Chunk::Entity_Vertex);
271

272
273
  OpenMesh::Vec3f v3f;
  OpenMesh::Vec2f v2f;
Jan Möbius's avatar
Jan Möbius committed
274
275
276
277
  OpenMesh::Vec3uc v3uc; // rgb

  OMFormat::Chunk::PropertyName custom_prop;

278
279
  size_t vidx = 0;
  switch (chunk_header_.type_) {
Jan Möbius's avatar
Jan Möbius committed
280
    case Chunk::Type_Pos:
281
      assert( OMFormat::dimensions(chunk_header_) == size_t(OpenMesh::Vec3f::dim()));
Jan Möbius's avatar
Jan Möbius committed
282

283
284
285
      for (; vidx < header_.n_vertices_ && !_is.eof(); ++vidx) {
        bytes_ += vector_restore(_is, v3f, _swap);
        _bi.add_vertex(v3f);
Jan Möbius's avatar
Jan Möbius committed
286
287
288
289
      }
      break;

    case Chunk::Type_Normal:
290
      assert( OMFormat::dimensions(chunk_header_) == size_t(OpenMesh::Vec3f::dim()));
Jan Möbius's avatar
Jan Möbius committed
291
292

      _opt += Options::VertexNormal;
293
294
295
      for (; vidx < header_.n_vertices_ && !_is.eof(); ++vidx) {
        bytes_ += vector_restore(_is, v3f, _swap);
        _bi.set_normal(VertexHandle(vidx), v3f);
Jan Möbius's avatar
Jan Möbius committed
296
297
298
299
      }
      break;

    case Chunk::Type_Texcoord:
300
      assert( OMFormat::dimensions(chunk_header_) == size_t(OpenMesh::Vec2f::dim()));
Jan Möbius's avatar
Jan Möbius committed
301
302

      _opt += Options::VertexTexCoord;
303
304
305
      for (; vidx < header_.n_vertices_ && !_is.eof(); ++vidx) {
        bytes_ += vector_restore(_is, v2f, _swap);
        _bi.set_texcoord(VertexHandle(vidx), v2f);
Jan Möbius's avatar
Jan Möbius committed
306
      }
307
      break;
Jan Möbius's avatar
Jan Möbius committed
308
309
310

    case Chunk::Type_Color:

311
      assert( OMFormat::dimensions(chunk_header_) == 3);
Jan Möbius's avatar
Jan Möbius committed
312
313

      _opt += Options::VertexColor;
314

315
316
317
      for (; vidx < header_.n_vertices_ && !_is.eof(); ++vidx) {
        bytes_ += vector_restore(_is, v3uc, _swap);
        _bi.set_color(VertexHandle(vidx), v3uc);
Jan Möbius's avatar
Jan Möbius committed
318
319
320
321
      }
      break;

    case Chunk::Type_Custom:
322

323
      bytes_ += restore_binary_custom_data(_is, _bi.kernel()->_get_vprop(property_name_), header_.n_vertices_, _swap);
Jan Möbius's avatar
Jan Möbius committed
324
325
326
327
328
329
330
331

      vidx = header_.n_vertices_;

      break;

    default: // skip unknown chunks
    {
      omerr() << "Unknown chunk type ignored!\n";
332
333
      size_t size_of = header_.n_vertices_ * OMFormat::vector_size(chunk_header_);
      _is.ignore(size_of);
Jan Möbius's avatar
Jan Möbius committed
334
335
336
337
338
339
340
341
342
343
344
      bytes_ += size_of;
    }
  }

  // all chunk data has been read..?!
  return vidx == header_.n_vertices_;
}


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

345
bool _OMReader_::read_binary_face_chunk(std::istream &_is, BaseImporter &_bi, Options &_opt, bool _swap) const
Jan Möbius's avatar
Jan Möbius committed
346
347
348
{
  using OMFormat::Chunk;

349
  assert( chunk_header_.entity_ == Chunk::Entity_Face);
Jan Möbius's avatar
Jan Möbius committed
350

351
352
  size_t fidx = 0;
  OpenMesh::Vec3f v3f;  // normal
Jan Möbius's avatar
Jan Möbius committed
353
354
  OpenMesh::Vec3uc v3uc; // rgb

355
356
357
358
359
360
361
362
363
364
365
366
367
  switch (chunk_header_.type_) {
    case Chunk::Type_Topology: {
      BaseImporter::VHandles vhandles;
      size_t nV = 0;
      size_t vidx = 0;

      switch (header_.mesh_) {
        case 'T':
          nV = 3;
          break;
        case 'Q':
          nV = 4;
          break;
Jan Möbius's avatar
Jan Möbius committed
368
      }
369

370
371
372
      for (; fidx < header_.n_faces_; ++fidx) {
        if (header_.mesh_ == 'P')
          bytes_ += restore(_is, nV, Chunk::Integer_16, _swap);
Jan Möbius's avatar
Jan Möbius committed
373

374
375
376
        vhandles.clear();
        for (size_t j = 0; j < nV; ++j) {
          bytes_ += restore(_is, vidx, Chunk::Integer_Size(chunk_header_.bits_), _swap);
Jan Möbius's avatar
Jan Möbius committed
377

378
379
          vhandles.push_back(VertexHandle(vidx));
        }
Jan Möbius's avatar
Jan Möbius committed
380

381
        _bi.add_face(vhandles);
Jan Möbius's avatar
Jan Möbius committed
382
383
      }
    }
384
      break;
Jan Möbius's avatar
Jan Möbius committed
385
386

    case Chunk::Type_Normal:
387
      assert( OMFormat::dimensions(chunk_header_) == size_t(OpenMesh::Vec3f::dim()));
Jan Möbius's avatar
Jan Möbius committed
388
389

      _opt += Options::FaceNormal;
390
391
392
      for (; fidx < header_.n_faces_ && !_is.eof(); ++fidx) {
        bytes_ += vector_restore(_is, v3f, _swap);
        _bi.set_normal(FaceHandle(fidx), v3f);
Jan Möbius's avatar
Jan Möbius committed
393
394
395
396
397
      }
      break;

    case Chunk::Type_Color:

398
      assert( OMFormat::dimensions(chunk_header_) == 3);
Jan Möbius's avatar
Jan Möbius committed
399

400
      _opt += Options::FaceColor;
401
402
403
      for (; fidx < header_.n_faces_ && !_is.eof(); ++fidx) {
        bytes_ += vector_restore(_is, v3uc, _swap);
        _bi.set_color(FaceHandle(fidx), v3uc);
Jan Möbius's avatar
Jan Möbius committed
404
405
406
407
      }
      break;

    case Chunk::Type_Custom:
408

409
      bytes_ += restore_binary_custom_data(_is, _bi.kernel()->_get_fprop(property_name_), header_.n_faces_, _swap);
Jan Möbius's avatar
Jan Möbius committed
410
411
412
413

      fidx = header_.n_faces_;

      break;
414

Jan Möbius's avatar
Jan Möbius committed
415
416
417
418
    default: // skip unknown chunks
    {
      omerr() << "Unknown chunk type ignore!\n";
      size_t size_of = OMFormat::chunk_data_size(header_, chunk_header_);
419
      _is.ignore(size_of);
420
      bytes_ += size_of;
Jan Möbius's avatar
Jan Möbius committed
421
422
423
424
425
426
427
428
    }
  }
  return fidx == header_.n_faces_;
}


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

429
bool _OMReader_::read_binary_edge_chunk(std::istream &_is, BaseImporter &_bi, Options &/*_opt */, bool _swap) const
Jan Möbius's avatar
Jan Möbius committed
430
431
432
{
  using OMFormat::Chunk;

433
  assert( chunk_header_.entity_ == Chunk::Entity_Edge);
Jan Möbius's avatar
Jan Möbius committed
434

435
  size_t b = bytes_;
Jan Möbius's avatar
Jan Möbius committed
436

437
  switch (chunk_header_.type_) {
438
    case Chunk::Type_Custom:
Jan Möbius's avatar
Jan Möbius committed
439

440
      bytes_ += restore_binary_custom_data(_is, _bi.kernel()->_get_eprop(property_name_), header_.n_edges_, _swap);
Jan Möbius's avatar
Jan Möbius committed
441
442
443
444
445
446

      break;

    default:
      // skip unknown type
      size_t size_of = OMFormat::chunk_data_size(header_, chunk_header_);
447
      _is.ignore(size_of);
Jan Möbius's avatar
Jan Möbius committed
448
449
450
451
452
453
454
455
456
      bytes_ += size_of;
  }

  return b < bytes_;
}


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

457
bool _OMReader_::read_binary_halfedge_chunk(std::istream &_is, BaseImporter &_bi, Options &/* _opt */, bool _swap) const
Jan Möbius's avatar
Jan Möbius committed
458
459
460
{
  using OMFormat::Chunk;

461
  assert( chunk_header_.entity_ == Chunk::Entity_Halfedge);
Jan Möbius's avatar
Jan Möbius committed
462
463
464

  size_t b = bytes_;

465
  switch (chunk_header_.type_) {
Jan Möbius's avatar
Jan Möbius committed
466
467
    case Chunk::Type_Custom:

468
      bytes_ += restore_binary_custom_data(_is, _bi.kernel()->_get_hprop(property_name_), 2 * header_.n_edges_, _swap);
Jan Möbius's avatar
Jan Möbius committed
469
470
471
472
473
474
      break;

    default:
      // skip unknown chunk
      omerr() << "Unknown chunk type ignored!\n";
      size_t size_of = OMFormat::chunk_data_size(header_, chunk_header_);
475
      _is.ignore(size_of);
Jan Möbius's avatar
Jan Möbius committed
476
477
478
479
480
481
482
483
484
      bytes_ += size_of;
  }

  return b < bytes_;
}


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

485
bool _OMReader_::read_binary_mesh_chunk(std::istream &_is, BaseImporter &_bi, Options & /* _opt */, bool _swap) const
Jan Möbius's avatar
Jan Möbius committed
486
487
488
{
  using OMFormat::Chunk;

489
  assert( chunk_header_.entity_ == Chunk::Entity_Mesh);
Jan Möbius's avatar
Jan Möbius committed
490
491
492

  size_t b = bytes_;

493
  switch (chunk_header_.type_) {
Jan Möbius's avatar
Jan Möbius committed
494
    case Chunk::Type_Custom:
495

496
      bytes_ += restore_binary_custom_data(_is, _bi.kernel()->_get_mprop(property_name_), 1, _swap);
497

Jan Möbius's avatar
Jan Möbius committed
498
499
500
501
502
      break;

    default:
      // skip unknown chunk
      size_t size_of = OMFormat::chunk_data_size(header_, chunk_header_);
503
      _is.ignore(size_of);
Jan Möbius's avatar
Jan Möbius committed
504
505
506
507
508
509
510
511
512
513
      bytes_ += size_of;
  }

  return b < bytes_;
}


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


514
size_t _OMReader_::restore_binary_custom_data(std::istream& _is, BaseProperty* _bp, size_t _n_elem, bool _swap) const
515
{
516
  assert( !_bp || (_bp->name() == property_name_));
Jan Möbius's avatar
Jan Möbius committed
517
518
519

  using OMFormat::Chunk;

520
521
  size_t bytes = 0;
  Chunk::esize_t block_size;
Jan Möbius's avatar
Jan Möbius committed
522
523
  Chunk::PropertyName custom_prop;

524
  bytes += restore(_is, block_size, OMFormat::Chunk::Integer_32, _swap);
Jan Möbius's avatar
Jan Möbius committed
525

526
527
  if (_bp) {
    size_t n_bytes = _bp->size_of(_n_elem);
Jan Möbius's avatar
Jan Möbius committed
528

529
530
    if (((n_bytes == BaseProperty::UnknownSize) || (n_bytes == block_size))
        && (_bp->element_size() == BaseProperty::UnknownSize || (_n_elem * _bp->element_size() == block_size))) {
Jan Möbius's avatar
Jan Möbius committed
531
#if defined(OM_DEBUG)
Jan Möbius's avatar
Jan Möbius committed
532
      size_t b;
Jan Möbius's avatar
Jan Möbius committed
533
534
      bytes += (b=_bp->restore( _is, _swap ));
#else
535
      bytes += _bp->restore(_is, _swap);
Jan Möbius's avatar
Jan Möbius committed
536
#endif
537

Jan Möbius's avatar
Jan Möbius committed
538
539
540
#if defined(OM_DEBUG)
      assert( block_size == b );
#endif
541
542

      assert( block_size == _bp->size_of());
Jan Möbius's avatar
Jan Möbius committed
543
544

      block_size = 0;
545
546
    } else {
      omerr() << "Warning! Property " << _bp->name() << " not loaded: " << "Mismatching data sizes!n";
Jan Möbius's avatar
Jan Möbius committed
547
548
549
    }
  }

550
551
552
  if (block_size) {
    _is.ignore(block_size);
    bytes += block_size;
Jan Möbius's avatar
Jan Möbius committed
553
554
555
556
557
558
559
560
561
562
563
564
  }

  return bytes;
}


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

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