PLYReader.cc 39.7 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
 *                                                                           *
Mike Kremer's avatar
Mike Kremer committed
9
 *---------------------------------------------------------------------------*
Jan Möbius's avatar
Jan Möbius committed
10
11
12
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
 * This file is part of OpenMesh.                                            *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * 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

Jan Möbius's avatar
Jan Möbius committed
42
43
44
45
46
47
48
49
50
51
52
53
54
#define LINE_LEN 4096

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

// OpenMesh
#include <OpenMesh/Core/System/config.h>
#include <OpenMesh/Core/System/omstream.hh>
#include <OpenMesh/Core/IO/reader/PLYReader.hh>
#include <OpenMesh/Core/IO/IOManager.hh>
#include <OpenMesh/Core/Utils/color_cast.hh>

//STL
#include <fstream>
55
#include <iostream>
Jan Möbius's avatar
Jan Möbius committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <memory>

#ifndef WIN32
#endif

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


namespace OpenMesh {
namespace IO {

//=============================================================================

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


Mike Kremer's avatar
Mike Kremer committed
72
73
_PLYReader_ __PLYReaderInstance;
_PLYReader_& PLYReader() {
74
    return __PLYReaderInstance;
Mike Kremer's avatar
Mike Kremer committed
75
}
Jan Möbius's avatar
Jan Möbius committed
76
77
78
79

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


Mike Kremer's avatar
Mike Kremer committed
80
_PLYReader_::_PLYReader_() {
81
    IOManager().register_module(this);
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

    // Store sizes in byte of each property type
    scalar_size_[ValueTypeINT8] = 1;
    scalar_size_[ValueTypeUINT8] = 1;
    scalar_size_[ValueTypeINT16] = 2;
    scalar_size_[ValueTypeUINT16] = 2;
    scalar_size_[ValueTypeINT32] = 4;
    scalar_size_[ValueTypeUINT32] = 4;
    scalar_size_[ValueTypeFLOAT32] = 4;
    scalar_size_[ValueTypeFLOAT64] = 8;

    scalar_size_[ValueTypeCHAR] = 1;
    scalar_size_[ValueTypeUCHAR] = 1;
    scalar_size_[ValueTypeSHORT] = 2;
    scalar_size_[ValueTypeUSHORT] = 2;
    scalar_size_[ValueTypeINT] = 4;
    scalar_size_[ValueTypeUINT] = 4;
    scalar_size_[ValueTypeFLOAT] = 4;
    scalar_size_[ValueTypeDOUBLE] = 8;
Jan Möbius's avatar
Jan Möbius committed
101
102
103
104
105
}

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


Mike Kremer's avatar
Mike Kremer committed
106
bool _PLYReader_::read(const std::string& _filename, BaseImporter& _bi, Options& _opt) {
107

108
    std::fstream in(_filename.c_str(), (std::ios_base::binary | std::ios_base::in) );
Jan Möbius's avatar
Jan Möbius committed
109

110
111
112
113
    if (!in.is_open() || !in.good()) {
        omerr() << "[PLYReader] : cannot not open file " << _filename << std::endl;
        return false;
    }
Jan Möbius's avatar
Jan Möbius committed
114

115
    bool result = read(in, _bi, _opt);
Jan Möbius's avatar
Jan Möbius committed
116

117
118
    in.close();
    return result;
Jan Möbius's avatar
Jan Möbius committed
119
120
121
122
123
}

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


124
125
126
127
128
129
130
bool _PLYReader_::read(std::istream& _in, BaseImporter& _bi, Options& _opt) {

    if (!_in.good()) {
        omerr() << "[PLYReader] : cannot not use stream" << std::endl;
        return false;
    }

131
132
133
134
135
136
137
138
    // Reparse the header
    if (!can_u_read(_in)) {
        omerr() << "[PLYReader] : Unable to parse header\n";
        return false;
    }



139
140
    // filter relevant options for reading
    bool swap = _opt.check(Options::Swap);
Jan Möbius's avatar
Jan Möbius committed
141

142
    userOptions_ = _opt;
Jan Möbius's avatar
Jan Möbius committed
143

144
145
    // build options to be returned
    _opt.clear();
Jan Möbius's avatar
Jan Möbius committed
146

147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
    if (options_.vertex_has_normal() && userOptions_.vertex_has_normal()) {
        _opt += Options::VertexNormal;
    }
    if (options_.vertex_has_texcoord() && userOptions_.vertex_has_texcoord()) {
        _opt += Options::VertexTexCoord;
    }
    if (options_.vertex_has_color() && userOptions_.vertex_has_color()) {
        _opt += Options::VertexColor;
    }
    if (options_.face_has_color() && userOptions_.face_has_color()) {
        _opt += Options::FaceColor;
    }
    if (options_.is_binary()) {
        _opt += Options::Binary;
    }
162
163
164
    if (options_.color_is_float()) {
        _opt += Options::ColorFloat;
    }
165
166
167
    if (options_.check(Options::Custom) && userOptions_.check(Options::Custom)) {
        _opt += Options::Custom;
    }
Jan Möbius's avatar
Jan Möbius committed
168

169
170
171
    //    //force user-choice for the alpha value when reading binary
    //    if ( options_.is_binary() && userOptions_.color_has_alpha() )
    //      options_ += Options::ColorAlpha;
Jan Möbius's avatar
Jan Möbius committed
172

173
    return (options_.is_binary() ? read_binary(_in, _bi, swap, _opt) : read_ascii(_in, _bi, _opt));
Jan Möbius's avatar
Jan Möbius committed
174
175
176

}

Matthias Möller's avatar
Matthias Möller committed
177
178
179
template<typename T, typename Handle>
struct Handle2Prop;

180
template<typename T>
Matthias Möller's avatar
Matthias Möller committed
181
struct Handle2Prop<T,VertexHandle>
182
{
Matthias Möller's avatar
Matthias Möller committed
183
184
  typedef OpenMesh::VPropHandleT<T> PropT;
};
185

Matthias Möller's avatar
Matthias Möller committed
186
187
188
189
190
template<typename T>
struct Handle2Prop<T,FaceHandle>
{
  typedef OpenMesh::FPropHandleT<T> PropT;
};
191

192
193
194
//read and assign custom properties with the given type. Also creates property, if not exist
template<bool binary, typename T, typename Handle>
void _PLYReader_::readCreateCustomProperty(std::istream& _in, BaseImporter& _bi, Handle _h, const std::string& _propName, const _PLYReader_::ValueType _valueType, const _PLYReader_::ValueType _listType) const
195
{
196
  if (_listType == Unsupported) //no list type defined -> property is not a list
Matthias Möller's avatar
Matthias Möller committed
197
198
199
200
  {
    //get/add property
    typename Handle2Prop<T,Handle>::PropT prop;
    if (!_bi.kernel()->get_property_handle(prop,_propName))
201
    {
Matthias Möller's avatar
Matthias Möller committed
202
      _bi.kernel()->add_property(prop,_propName);
203
204
      _bi.kernel()->property(prop).set_persistent(true);
    }
Matthias Möller's avatar
Matthias Möller committed
205
206
207

    //read and assign
    T in;
208
    read(_valueType, _in, in, OpenMesh::GenProg::Bool2Type<binary>());
Matthias Möller's avatar
Matthias Möller committed
209
210
211
212
213
214
215
    _bi.kernel()->property(prop,_h) = in;
  }
  else
  {
    //get/add property
    typename Handle2Prop<std::vector<T>,Handle>::PropT prop;
    if (!_bi.kernel()->get_property_handle(prop,_propName))
216
    {
Matthias Möller's avatar
Matthias Möller committed
217
      _bi.kernel()->add_property(prop,_propName);
218
219
      _bi.kernel()->property(prop).set_persistent(true);
    }
Matthias Möller's avatar
Matthias Möller committed
220
221
222

    //init vector
    int numberOfValues;
223
    read(_listType, _in, numberOfValues, OpenMesh::GenProg::Bool2Type<binary>());
Matthias Möller's avatar
Matthias Möller committed
224
225
226
227
228
229
    std::vector<T> vec;
    vec.reserve(numberOfValues);
    //read and assign
    for (int i = 0; i < numberOfValues; ++i)
    {
      T in;
230
      read(_valueType, _in, in, OpenMesh::GenProg::Bool2Type<binary>());
Matthias Möller's avatar
Matthias Möller committed
231
232
233
234
235
236
      vec.push_back(in);
    }
    _bi.kernel()->property(prop,_h) = vec;
  }
}

237
238
template<bool binary, typename Handle>
void _PLYReader_::readCustomProperty(std::istream& _in, BaseImporter& _bi, Handle _h, const std::string& _propName, const _PLYReader_::ValueType _valueType, const _PLYReader_::ValueType _listIndexType) const
Matthias Möller's avatar
Matthias Möller committed
239
{
240
241
242
243
  switch (_valueType)
  {
  case ValueTypeINT8:
  case ValueTypeCHAR:
244
      readCreateCustomProperty<binary,signed char>(_in,_bi,_h,_propName,_valueType,_listIndexType);
245
      break;
246
247
  case ValueTypeUINT8:
  case ValueTypeUCHAR:
248
      readCreateCustomProperty<binary,unsigned char>(_in,_bi,_h,_propName,_valueType,_listIndexType);
249
      break;
250
251
  case ValueTypeINT16:
  case ValueTypeSHORT:
252
      readCreateCustomProperty<binary,short>(_in,_bi,_h,_propName,_valueType,_listIndexType);
253
254
255
      break;
  case ValueTypeUINT16:
  case ValueTypeUSHORT:
256
      readCreateCustomProperty<binary,unsigned short>(_in,_bi,_h,_propName,_valueType,_listIndexType);
257
258
259
      break;
  case ValueTypeINT32:
  case ValueTypeINT:
260
      readCreateCustomProperty<binary,int>(_in,_bi,_h,_propName,_valueType,_listIndexType);
261
262
263
      break;
  case ValueTypeUINT32:
  case ValueTypeUINT:
264
      readCreateCustomProperty<binary,unsigned int>(_in,_bi,_h,_propName,_valueType,_listIndexType);
265
266
267
      break;
  case ValueTypeFLOAT32:
  case ValueTypeFLOAT:
268
      readCreateCustomProperty<binary,float>(_in,_bi,_h,_propName,_valueType,_listIndexType);
269
270
271
      break;
  case ValueTypeFLOAT64:
  case ValueTypeDOUBLE:
272
      readCreateCustomProperty<binary,double>(_in,_bi,_h,_propName,_valueType,_listIndexType);
273
274
275
276
277
278
      break;
  default:
      std::cerr << "unsupported type" << std::endl;
      break;
  }
}
279
280


Jan Möbius's avatar
Jan Möbius committed
281
282
//-----------------------------------------------------------------------------

283
bool _PLYReader_::read_ascii(std::istream& _in, BaseImporter& _bi, const Options& _opt) const {
Mike Kremer's avatar
Mike Kremer committed
284

285
286
    unsigned int i, j, k, l, idx;
    unsigned int nV;
Jan Möbius's avatar
Jan Möbius committed
287
    OpenMesh::Vec3f v, n;
288
289
290
291
292
293
294
295
296
297
298
299
300
301
    std::string trash;
    OpenMesh::Vec2f t;
    OpenMesh::Vec4i c;
    float tmp;
    BaseImporter::VHandles vhandles;
    VertexHandle vh;

    _bi.reserve(vertexCount_, 3* vertexCount_ , faceCount_);

    if (vertexDimension_ != 3) {
        omerr() << "[PLYReader] : Only vertex dimension 3 is supported." << std::endl;
        return false;
    }

302
303
304
305
306
    const bool err_enabled = omerr().is_enabled();
    size_t complex_faces = 0;
    if (err_enabled)
      omerr().disable();

307
308
	for (std::vector<ElementInfo>::iterator e_it = elements_.begin(); e_it != elements_.end(); ++e_it)
	{
309
310
311
312
313
314
315
316
	        if (_in.eof()) {
			if (err_enabled)
				omerr().enable();

			omerr() << "Unexpected end of file while reading." << std::endl;
			return false;
		}

317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
		if (e_it->element_== VERTEX)
		{
			// read vertices:
			for (i = 0; i < e_it->count_ && !_in.eof(); ++i) {
				vh = _bi.add_vertex();

				v[0] = 0.0;
				v[1] = 0.0;
				v[2] = 0.0;

				n[0] = 0.0;
				n[1] = 0.0;
				n[2] = 0.0;

				t[0] = 0.0;
				t[1] = 0.0;

				c[0] = 0;
				c[1] = 0;
				c[2] = 0;
				c[3] = 255;

				for (size_t propertyIndex = 0; propertyIndex < e_it->properties_.size(); ++propertyIndex) {
					PropertyInfo prop = e_it->properties_[propertyIndex];
					switch (prop.property) {
					case XCOORD:
						_in >> v[0];
						break;
					case YCOORD:
						_in >> v[1];
						break;
					case ZCOORD:
						_in >> v[2];
						break;
					case XNORM:
						_in >> n[0];
						break;
					case YNORM:
						_in >> n[1];
						break;
					case ZNORM:
						_in >> n[2];
						break;
					case TEXX:
						_in >> t[0];
						break;
					case TEXY:
						_in >> t[1];
						break;
					case COLORRED:
						if (prop.value == ValueTypeFLOAT32 ||
							prop.value == ValueTypeFLOAT) {
							_in >> tmp;
							c[0] = static_cast<OpenMesh::Vec4i::value_type> (tmp * 255.0f);
						}
						else
							_in >> c[0];
						break;
					case COLORGREEN:
						if (prop.value == ValueTypeFLOAT32 ||
							prop.value == ValueTypeFLOAT) {
							_in >> tmp;
							c[1] = static_cast<OpenMesh::Vec4i::value_type> (tmp * 255.0f);
						}
						else
							_in >> c[1];
						break;
					case COLORBLUE:
						if (prop.value == ValueTypeFLOAT32 ||
							prop.value == ValueTypeFLOAT) {
							_in >> tmp;
							c[2] = static_cast<OpenMesh::Vec4i::value_type> (tmp * 255.0f);
						}
						else
							_in >> c[2];
						break;
					case COLORALPHA:
						if (prop.value == ValueTypeFLOAT32 ||
							prop.value == ValueTypeFLOAT) {
							_in >> tmp;
							c[3] = static_cast<OpenMesh::Vec4i::value_type> (tmp * 255.0f);
						}
						else
							_in >> c[3];
						break;
					case CUSTOM_PROP:
						if (_opt.check(Options::Custom))
							readCustomProperty<false>(_in, _bi, vh, prop.name, prop.value, prop.listIndexType);
						else
							_in >> trash;
						break;
					default:
						_in >> trash;
						break;
					}
				}

				_bi.set_point(vh, v);
				if (_opt.vertex_has_normal())
					_bi.set_normal(vh, n);
				if (_opt.vertex_has_texcoord())
					_bi.set_texcoord(vh, t);
				if (_opt.vertex_has_color())
					_bi.set_color(vh, Vec4uc(c));
			}
		}
		else if (e_it->element_ == FACE)
		{
			// faces
			for (i = 0; i < faceCount_ && !_in.eof(); ++i) {
				FaceHandle fh;
				for (size_t propertyIndex = 0; propertyIndex < e_it->properties_.size(); ++propertyIndex) {
					PropertyInfo prop = e_it->properties_[propertyIndex];
					switch (prop.property) {

					case VERTEX_INDICES:
						// nV = number of Vertices for current face
						_in >> nV;

						if (nV == 3) {
							vhandles.resize(3);
							_in >> j;
							_in >> k;
							_in >> l;

							vhandles[0] = VertexHandle(j);
							vhandles[1] = VertexHandle(k);
							vhandles[2] = VertexHandle(l);
						}
						else {
							vhandles.clear();
							for (j = 0; j < nV; ++j) {
								_in >> idx;
								vhandles.push_back(VertexHandle(idx));
							}
						}

						fh = _bi.add_face(vhandles);
						if (!fh.is_valid())
							++complex_faces;
						break;

					case CUSTOM_PROP:
						if (_opt.check(Options::Custom) && fh.is_valid())
							readCustomProperty<false>(_in, _bi, fh, prop.name, prop.value, prop.listIndexType);
						else
							_in >> trash;
						break;

					default:
						_in >> trash;
						break;
					}
				}

			}
		}
		else
		{
			// other elements
			for (i = 0; i < e_it->count_ && !_in.eof(); ++i) {
				for (size_t propertyIndex = 0; propertyIndex < e_it->properties_.size(); ++propertyIndex)
				{
					// just skip the values
					_in >> trash;
				}
			}
		}
485
486
487
488

		if(e_it->element_== FACE)
			// stop reading after the faces since additional elements are not preserved anyway
			break;
489
	}
490

Jan Möbius's avatar
Jan Möbius committed
491
    if (err_enabled) 
492
      omerr().enable();
Jan Möbius's avatar
Jan Möbius committed
493
494
495

    if (complex_faces)
      omerr() << complex_faces << "The reader encountered invalid faces, that could not be added.\n";
496

497
498
    // File was successfully parsed.
    return true;
Jan Möbius's avatar
Jan Möbius committed
499
500
501
502
}

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

503
bool _PLYReader_::read_binary(std::istream& _in, BaseImporter& _bi, bool /*_swap*/, const Options& _opt) const {
Mike Kremer's avatar
Mike Kremer committed
504

505
506
    OpenMesh::Vec3f        v, n;  // Vertex
    OpenMesh::Vec2f        t;  // TexCoords
507
    BaseImporter::VHandles vhandles;
508
509
510
    VertexHandle           vh;
    OpenMesh::Vec4i        c;  // Color
    float                  tmp;
511
	
512
513
    _bi.reserve(vertexCount_, 3* vertexCount_ , faceCount_);

514
515
516
517
518
    const bool err_enabled = omerr().is_enabled();
    size_t complex_faces = 0;
    if (err_enabled)
      omerr().disable();

519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
	for (std::vector<ElementInfo>::iterator e_it = elements_.begin(); e_it != elements_.end(); ++e_it)
	{
		if (e_it->element_ == VERTEX)
		{
			// read vertices:
			for (unsigned int i = 0; i < e_it->count_ && !_in.eof(); ++i) {
				vh = _bi.add_vertex();

				v[0] = 0.0;
				v[1] = 0.0;
				v[2] = 0.0;

				n[0] = 0.0;
				n[1] = 0.0;
				n[2] = 0.0;

				t[0] = 0.0;
				t[1] = 0.0;

				c[0] = 0;
				c[1] = 0;
				c[2] = 0;
				c[3] = 255;

				for (size_t propertyIndex = 0; propertyIndex < e_it->properties_.size(); ++propertyIndex) {
					PropertyInfo prop = e_it->properties_[propertyIndex];
					switch (prop.property) {
					case XCOORD:
						readValue(prop.value, _in, v[0]);
						break;
					case YCOORD:
						readValue(prop.value, _in, v[1]);
						break;
					case ZCOORD:
						readValue(prop.value, _in, v[2]);
						break;
					case XNORM:
						readValue(prop.value, _in, n[0]);
						break;
					case YNORM:
						readValue(prop.value, _in, n[1]);
						break;
					case ZNORM:
						readValue(prop.value, _in, n[2]);
						break;
					case TEXX:
						readValue(prop.value, _in, t[0]);
						break;
					case TEXY:
						readValue(prop.value, _in, t[1]);
						break;
					case COLORRED:
						if (prop.value == ValueTypeFLOAT32 ||
							prop.value == ValueTypeFLOAT) {
							readValue(prop.value, _in, tmp);

							c[0] = static_cast<OpenMesh::Vec4i::value_type> (tmp * 255.0f);
						}
						else
							readInteger(prop.value, _in, c[0]);

						break;
					case COLORGREEN:
						if (prop.value == ValueTypeFLOAT32 ||
							prop.value == ValueTypeFLOAT) {
							readValue(prop.value, _in, tmp);
							c[1] = static_cast<OpenMesh::Vec4i::value_type> (tmp * 255.0f);
						}
						else
							readInteger(prop.value, _in, c[1]);

						break;
					case COLORBLUE:
						if (prop.value == ValueTypeFLOAT32 ||
							prop.value == ValueTypeFLOAT) {
							readValue(prop.value, _in, tmp);
							c[2] = static_cast<OpenMesh::Vec4i::value_type> (tmp * 255.0f);
						}
						else
							readInteger(prop.value, _in, c[2]);

						break;
					case COLORALPHA:
						if (prop.value == ValueTypeFLOAT32 ||
							prop.value == ValueTypeFLOAT) {
							readValue(prop.value, _in, tmp);
							c[3] = static_cast<OpenMesh::Vec4i::value_type> (tmp * 255.0f);
						}
						else
							readInteger(prop.value, _in, c[3]);

						break;
					case CUSTOM_PROP:
						if (_opt.check(Options::Custom))
							readCustomProperty<true>(_in, _bi, vh, prop.name, prop.value, prop.listIndexType);
						else
							consume_input(_in, scalar_size_[prop.value]);
						break;
					default:
						// Read unsupported property
						consume_input(_in, scalar_size_[prop.value]);
						break;
					}

				}

				_bi.set_point(vh, v);
				if (_opt.vertex_has_normal())
					_bi.set_normal(vh, n);
				if (_opt.vertex_has_texcoord())
					_bi.set_texcoord(vh, t);
				if (_opt.vertex_has_color())
					_bi.set_color(vh, Vec4uc(c));
			}
		} 
		else if (e_it->element_ == FACE) {
			for (unsigned i = 0; i < e_it->count_ && !_in.eof(); ++i) {
				FaceHandle fh;
				for (size_t propertyIndex = 0; propertyIndex < e_it->properties_.size(); ++propertyIndex)
				{
					PropertyInfo prop = e_it->properties_[propertyIndex];
					switch (prop.property) {

					case VERTEX_INDICES:
						// nV = number of Vertices for current face
						unsigned int nV;
						readInteger(prop.listIndexType, _in, nV);

						if (nV == 3) {
							vhandles.resize(3);
							unsigned int j, k, l;
							readInteger(prop.value, _in, j);
							readInteger(prop.value, _in, k);
							readInteger(prop.value, _in, l);

							vhandles[0] = VertexHandle(j);
							vhandles[1] = VertexHandle(k);
							vhandles[2] = VertexHandle(l);
						}
						else {
							vhandles.clear();
							for (unsigned j = 0; j < nV; ++j) {
								unsigned int idx;
								readInteger(prop.value, _in, idx);
								vhandles.push_back(VertexHandle(idx));
							}
						}

						fh = _bi.add_face(vhandles);
						if (!fh.is_valid())
							++complex_faces;
						break;

					case CUSTOM_PROP:
						if (_opt.check(Options::Custom) && fh.is_valid())
							readCustomProperty<true>(_in, _bi, fh, prop.name, prop.value, prop.listIndexType);
						else
							consume_input(_in, scalar_size_[prop.value]);
						break;

					default:
						consume_input(_in, scalar_size_[prop.value]);
						break;
					}
				}
			}
		} 
		else { 
			for (unsigned int i = 0; i < e_it->count_ && !_in.eof(); ++i)
			{
				for (size_t propertyIndex = 0; propertyIndex < e_it->properties_.size(); ++propertyIndex)
				{
					PropertyInfo prop = e_it->properties_[propertyIndex];
					// skip element values
					consume_input(_in, scalar_size_[prop.value]);
				}
			}
		}

698
699
700
701
702
703
704
705
706
707
708
		if (_in.eof()) {
			if (err_enabled)
				omerr().enable();

			omerr() << "Unexpected end of file while reading." << std::endl;
			return false;
		}

		if (e_it->element_ == FACE)
			// stop reading after the faces since additional elements are not preserved anyway
			break; 
709
	}
Jan Möbius's avatar
Jan Möbius committed
710
    if (err_enabled) 
711
      omerr().enable();
Jan Möbius's avatar
Jan Möbius committed
712
713
714
715

   if (complex_faces)
      omerr() << complex_faces << "The reader encountered invalid faces, that could not be added.\n";

716

717
    return true;
Jan Möbius's avatar
Jan Möbius committed
718
719
720
}


721
722
723
//-----------------------------------------------------------------------------


724
void _PLYReader_::readValue(ValueType _type, std::istream& _in, float& _value) const {
725

726
727
728
729
730
731
732
733
734
735
736
737
    switch (_type) {
    case ValueTypeFLOAT32:
    case ValueTypeFLOAT:
        float32_t tmp;
        restore(_in, tmp, options_.check(Options::MSB));
        _value = tmp;
        break;
    default:
        _value = 0.0;
        std::cerr << "unsupported conversion type to float: " << _type << std::endl;
        break;
    }
738
739
}

740
741
742
743
744
745
746

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


void _PLYReader_::readValue(ValueType _type, std::istream& _in, double& _value) const {

    switch (_type) {
747

748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
        case ValueTypeFLOAT64:

        case ValueTypeDOUBLE:

            float64_t tmp;
            restore(_in, tmp, options_.check(Options::MSB));
            _value = tmp;

            break;

    default:

        _value = 0.0;
        std::cerr << "unsupported conversion type to double: " << _type << std::endl;

        break;
    }
}


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

770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
void _PLYReader_::readValue(ValueType _type, std::istream& _in, unsigned char& _value) const{
  unsigned int tmp;
  readValue(_type,_in,tmp);
  _value = tmp;
}

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

void _PLYReader_::readValue(ValueType _type, std::istream& _in, unsigned short& _value) const{
  unsigned int tmp;
  readValue(_type,_in,tmp);
  _value = tmp;
}

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

void _PLYReader_::readValue(ValueType _type, std::istream& _in, signed char& _value) const{
  int tmp;
  readValue(_type,_in,tmp);
  _value = tmp;
}
791

792
793
794
795
796
797
798
799
800
801
//-----------------------------------------------------------------------------

void _PLYReader_::readValue(ValueType _type, std::istream& _in, short& _value) const{
  int tmp;
  readValue(_type,_in,tmp);
  _value = tmp;
}


//-----------------------------------------------------------------------------
802
void _PLYReader_::readValue(ValueType _type, std::istream& _in, unsigned int& _value) const {
803

804
805
    uint32_t tmp_uint32_t;
    uint16_t tmp_uint16_t;
806
807
808
    uint8_t tmp_uchar;

    switch (_type) {
809
810
811
812

        case ValueTypeUINT:

        case ValueTypeUINT32:
813

814
815
816
            restore(_in, tmp_uint32_t, options_.check(Options::MSB));
            _value = tmp_uint32_t;

817
        break;
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842

        case ValueTypeUSHORT:

        case ValueTypeUINT16:

            restore(_in, tmp_uint16_t, options_.check(Options::MSB));
            _value = tmp_uint16_t;

            break;

        case ValueTypeUCHAR:

        case ValueTypeUINT8:

            restore(_in, tmp_uchar, options_.check(Options::MSB));
            _value = tmp_uchar;

            break;

        default:

            _value = 0;
            std::cerr << "unsupported conversion type to unsigned int: " << _type << std::endl;

            break;
843
    }
844
845
}

846
847
848
849

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


850
void _PLYReader_::readValue(ValueType _type, std::ist