OFFReader.cc 19.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
52
53
54
55
56
57
58
59
60
61
62
63
 *                                                                           *
\*===========================================================================*/


#define LINE_LEN 4096


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

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

//STL
64
#include <iostream>
Jan Möbius's avatar
Jan Möbius committed
65
66
67
#include <fstream>
#include <memory>

Jan Möbius's avatar
Jan Möbius committed
68
69
70
71
72
73
74
75
76
#if defined(OM_CC_MIPS)
#  include <ctype.h>
/// \bug Workaround for STLPORT 4.6: isspace seems not to be in namespace std!
#elif defined(_STLPORT_VERSION) && (_STLPORT_VERSION==0x460)
#  include <cctype>
#else
using std::isspace;
#endif

Jan Möbius's avatar
Jan Möbius committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#ifndef WIN32
#endif

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


namespace OpenMesh {
namespace IO {


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

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


_OFFReader_  __OFFReaderInstance;
_OFFReader_&  OFFReader() { return __OFFReaderInstance; }


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



100
101
102
_OFFReader_::_OFFReader_()
{
  IOManager().register_module(this);
Jan Möbius's avatar
Jan Möbius committed
103
104
105
106
107
108
}


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


109
110
bool
_OFFReader_::read(const std::string& _filename, BaseImporter& _bi,
Jan Möbius's avatar
Jan Möbius committed
111
112
                  Options& _opt)
{
113
114
  std::ifstream ifile(_filename.c_str(), (options_.is_binary() ? std::ios::binary | std::ios::in
                                                           : std::ios::in) );
Jan Möbius's avatar
Jan Möbius committed
115

116
  if (!ifile.is_open() || !ifile.good())
Jan Möbius's avatar
Jan Möbius committed
117
  {
118
    omerr() << "[OFFReader] : cannot not open file "
Jan Möbius's avatar
Jan Möbius committed
119
120
	  << _filename
	  << std::endl;
121

Jan Möbius's avatar
Jan Möbius committed
122
123
124
    return false;
  }

125
  assert(ifile);
Jan Möbius's avatar
Jan Möbius committed
126

127
  bool result = read(ifile, _bi, _opt);
Jan Möbius's avatar
Jan Möbius committed
128

129
  ifile.close();
Jan Möbius's avatar
Jan Möbius committed
130
131
132
133
134
135
  return result;
}

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


136
137
bool
_OFFReader_::read(std::istream& _in, BaseImporter& _bi, Options& _opt )
Jan Möbius's avatar
Jan Möbius committed
138
{
139
140
141
142
143
144
145
   if (!_in.good())
   {
     omerr() << "[OMReader] : cannot not use stream "
       << std::endl;
     return false;
   }

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

Jan Möbius's avatar
Jan Möbius committed
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165

   userOptions_ = _opt;

   // build options to be returned
   _opt.clear();

   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;

   //force user-choice for the alpha value when reading binary
   if ( options_.is_binary() && userOptions_.color_has_alpha() )
     options_ += Options::ColorAlpha;

    return (options_.is_binary() ?
166
167
 	   read_binary(_in, _bi, _opt, swap) :
	   read_ascii(_in, _bi, _opt));
Jan Möbius's avatar
Jan Möbius committed
168
169
170
171
172
173
174

}



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

175
bool
176
_OFFReader_::read_ascii(std::istream& _in, BaseImporter& _bi, Options& _opt) const
Jan Möbius's avatar
Jan Möbius committed
177
178
179
{


180
181
182
183
184
185
186
187
188
189
  unsigned int              i, j, k, l, idx;
  unsigned int              nV, nF, dummy;
  OpenMesh::Vec3f           v, n;
  OpenMesh::Vec2f           t;
  OpenMesh::Vec3i           c3;
  OpenMesh::Vec3f           c3f;
  OpenMesh::Vec4i           c4;
  OpenMesh::Vec4f           c4f;
  BaseImporter::VHandles    vhandles;
  VertexHandle              vh;
Martin Schultz's avatar
Martin Schultz committed
190
191
  std::stringstream         stream;
  std::string               trash;
Jan Möbius's avatar
Jan Möbius committed
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208

  // read header line
  std::string header;
  std::getline(_in,header);

  // + #Vertice, #Faces, #Edges
  _in >> nV;
  _in >> nF;
  _in >> dummy;

  _bi.reserve(nV, 3*nV, nF);

  // read vertices: coord [hcoord] [normal] [color] [texcoord]
  for (i=0; i<nV && !_in.eof(); ++i)
  {
    // Always read VERTEX
    _in >> v[0]; _in >> v[1]; _in >> v[2];
209

Jan Möbius's avatar
Jan Möbius committed
210
    vh = _bi.add_vertex(v);
211

Jan Möbius's avatar
Jan Möbius committed
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
    //perhaps read NORMAL
    if ( options_.vertex_has_normal() ){

      _in >> n[0]; _in >> n[1]; _in >> n[2];

      if ( userOptions_.vertex_has_normal() )
        _bi.set_normal(vh, n);
    }

    //take the rest of the line and check how colors are defined
    std::string line;
    std::getline(_in,line);

    int colorType = getColorType(line, options_.vertex_has_texcoord() );

227
228
    stream.str(line);
    stream.clear();
Jan Möbius's avatar
Jan Möbius committed
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248

    //perhaps read COLOR
    if ( options_.vertex_has_color() ){

      switch (colorType){
        case 0 : break; //no color
        case 1 : stream >> trash; break; //one int (isn't handled atm)
        case 2 : stream >> trash; stream >> trash; break; //corrupt format (ignore)
        // rgb int
        case 3 : stream >> c3[0];  stream >> c3[1];  stream >> c3[2];
            if ( userOptions_.vertex_has_color() )
              _bi.set_color( vh, Vec3uc( c3 ) );
            break;
        // rgba int
        case 4 : stream >> c4[0];  stream >> c4[1];  stream >> c4[2]; stream >> c4[3];
            if ( userOptions_.vertex_has_color() )
              _bi.set_color( vh, Vec4uc( c4 ) );
            break;
        // rgb floats
        case 5 : stream >> c3f[0];  stream >> c3f[1];  stream >> c3f[2];
249
250
251
252
            if ( userOptions_.vertex_has_color() ) {
              _bi.set_color( vh, c3f );
              _opt += Options::ColorFloat;
            }
Jan Möbius's avatar
Jan Möbius committed
253
254
255
            break;
        // rgba floats
        case 6 : stream >> c4f[0];  stream >> c4f[1];  stream >> c4f[2]; stream >> c4f[3];
256
257
258
259
            if ( userOptions_.vertex_has_color() ) {
              _bi.set_color( vh, c4f );
              _opt += Options::ColorFloat;
            }
Jan Möbius's avatar
Jan Möbius committed
260
261
            break;

262
        default:
Jan Möbius's avatar
Jan Möbius committed
263
            std::cerr << "Error in file format (colorType = " << colorType << ")\n";
Jan Möbius's avatar
Jan Möbius committed
264
            break;
Jan Möbius's avatar
Jan Möbius committed
265
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
      }
    }
    //perhaps read TEXTURE COORDs
    if ( options_.vertex_has_texcoord() ){
      stream >> t[0]; stream >> t[1];
      if ( userOptions_.vertex_has_texcoord() )
        _bi.set_texcoord(vh, t);
    }
  }

  // faces
  // #N <v1> <v2> .. <v(n-1)> [color spec]
  for (i=0; i<nF; ++i)
  {
    // 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);
    }
293
    else
Jan Möbius's avatar
Jan Möbius committed
294
295
296
297
298
299
300
301
    {
      vhandles.clear();
      for (j=0; j<nV; ++j)
      {
         _in >> idx;
         vhandles.push_back(VertexHandle(idx));
      }
    }
302

Jan Möbius's avatar
Jan Möbius committed
303
304
305
306
307
308
309
310
311
312
313
    FaceHandle fh = _bi.add_face(vhandles);

    //perhaps read face COLOR
    if ( options_.face_has_color() ){

      //take the rest of the line and check how colors are defined
      std::string line;
      std::getline(_in,line);

      int colorType = getColorType(line, false );

314
315
      stream.str(line);
      stream.clear();
Jan Möbius's avatar
Jan Möbius committed
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332

      switch (colorType){
        case 0 : break; //no color
        case 1 : stream >> trash; break; //one int (isn't handled atm)
        case 2 : stream >> trash; stream >> trash; break; //corrupt format (ignore)
        // rgb int
        case 3 : stream >> c3[0];  stream >> c3[1];  stream >> c3[2];
            if ( userOptions_.face_has_color() )
              _bi.set_color( fh, Vec3uc( c3 ) );
            break;
        // rgba int
        case 4 : stream >> c4[0];  stream >> c4[1];  stream >> c4[2]; stream >> c4[3];
            if ( userOptions_.face_has_color() )
              _bi.set_color( fh, Vec4uc( c4 ) );
            break;
        // rgb floats
        case 5 : stream >> c3f[0];  stream >> c3f[1];  stream >> c3f[2];
333
334
335
336
            if ( userOptions_.face_has_color() ) {
              _bi.set_color( fh, c3f );
              _opt += Options::ColorFloat;
            }
Jan Möbius's avatar
Jan Möbius committed
337
338
339
            break;
        // rgba floats
        case 6 : stream >> c4f[0];  stream >> c4f[1];  stream >> c4f[2]; stream >> c4f[3];
340
341
342
343
            if ( userOptions_.face_has_color() ) {
              _bi.set_color( fh, c4f );
              _opt += Options::ColorFloat;
            }
Jan Möbius's avatar
Jan Möbius committed
344
345
            break;

346
        default:
Jan Möbius's avatar
Jan Möbius committed
347
            std::cerr << "Error in file format (colorType = " << colorType << ")\n";
Jan Möbius's avatar
Jan Möbius committed
348
            break;
Jan Möbius's avatar
Jan Möbius committed
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
      }
    }
  }

  // File was successfully parsed.
  return true;
}


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

int _OFFReader_::getColorType(std::string& _line, bool _texCoordsAvailable) const
{
/*
  0 : no Color
  1 : one int (e.g colormap index)
  2 : two items (error!)
  3 : 3 ints
  4 : 3 ints
  5 : 3 floats
  6 : 4 floats

*/
	// Check if we have any additional information here
	if ( _line.size() < 1 )
		return 0;

    //first remove spaces at start/end of the line
377
    while (_line.size() > 0 && std::isspace(_line[0]))
Jan Möbius's avatar
Jan Möbius committed
378
      _line = _line.substr(1);
379
    while (_line.size() > 0 && std::isspace(_line[ _line.length()-1 ]))
380
      _line = _line.substr(0, _line.length()-1);
Jan Möbius's avatar
Jan Möbius committed
381
382
383
384
385
386
387
388
389
390
391

    //count the remaining items in the line
    size_t found;
    int count = 0;

    found=_line.find_first_of(" ");
    while (found!=std::string::npos){
      count++;
      found=_line.find_first_of(" ",found+1);
    }

392
    if (!_line.empty()) count++;
Jan Möbius's avatar
Jan Möbius committed
393
394
395
396
397
398
399

    if (_texCoordsAvailable) count -= 2;

    if (count == 3 || count == 4){
      //get first item
      found = _line.find(" ");
      std::string c1 = _line.substr (0,found);
400

Jan Möbius's avatar
Jan Möbius committed
401
402
403
404
405
406
407
408
409
410
      if (c1.find(".") != std::string::npos){
        if (count == 3)
          count = 5;
        else
          count = 6;
      }
    }
  return count;
}

411
void _OFFReader_::readValue(std::istream& _in, float& _value) const{
Jan Möbius's avatar
Jan Möbius committed
412
413
414
415
416
417
  float32_t tmp;

  restore( _in , tmp, false ); //assuming LSB byte order
  _value = tmp;
}

418
void _OFFReader_::readValue(std::istream& _in, int& _value) const{
Jan Möbius's avatar
Jan Möbius committed
419
420
421
422
423
424
  uint32_t tmp;

  restore( _in , tmp, false ); //assuming LSB byte order
  _value = tmp;
}

425
void _OFFReader_::readValue(std::istream& _in, unsigned int& _value) const{
Jan Möbius's avatar
Jan Möbius committed
426
427
428
429
430
431
  uint32_t tmp;

  restore( _in , tmp, false ); //assuming LSB byte order
  _value = tmp;
}

432
bool
433
_OFFReader_::read_binary(std::istream& _in, BaseImporter& _bi, Options& _opt, bool /*_swap*/) const
Jan Möbius's avatar
Jan Möbius committed
434
435
436
437
438
439
{
  unsigned int            i, j, k, l, idx;
  unsigned int            nV, nF, dummy;
  OpenMesh::Vec3f         v, n;
  OpenMesh::Vec3i         c;
  OpenMesh::Vec4i         cA;
440
441
  OpenMesh::Vec3f         cf;
  OpenMesh::Vec4f         cAf;
Jan Möbius's avatar
Jan Möbius committed
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
  OpenMesh::Vec2f         t;
  BaseImporter::VHandles  vhandles;
  VertexHandle            vh;

  // read header line
  std::string header;
  std::getline(_in,header);

  // + #Vertice, #Faces, #Edges
  readValue(_in, nV);
  readValue(_in, nF);
  readValue(_in, dummy);

  _bi.reserve(nV, 3*nV, nF);

457
  // read vertices: coord [hcoord] [normal] [color] [texcoord]
Jan Möbius's avatar
Jan Möbius committed
458
459
460
461
462
463
  for (i=0; i<nV && !_in.eof(); ++i)
  {
    // Always read Vertex
    readValue(_in, v[0]);
    readValue(_in, v[1]);
    readValue(_in, v[2]);
464

Jan Möbius's avatar
Jan Möbius committed
465
    vh = _bi.add_vertex(v);
466

Jan Möbius's avatar
Jan Möbius committed
467
468
469
470
471
472
473
474
    if ( options_.vertex_has_normal() ) {
      readValue(_in, n[0]);
      readValue(_in, n[1]);
      readValue(_in, n[2]);

      if ( userOptions_.vertex_has_normal() )
        _bi.set_normal(vh, n);
    }
475

Jan Möbius's avatar
Jan Möbius committed
476
    if ( options_.vertex_has_color() ) {
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
      if ( userOptions_.color_is_float() ) {
        _opt += Options::ColorFloat;
        //with alpha
        if ( options_.color_has_alpha() ){
          readValue(_in, cAf[0]);
          readValue(_in, cAf[1]);
          readValue(_in, cAf[2]);
          readValue(_in, cAf[3]);

          if ( userOptions_.vertex_has_color() )
            _bi.set_color( vh, cAf );
        }else{

          //without alpha
          readValue(_in, cf[0]);
          readValue(_in, cf[1]);
          readValue(_in, cf[2]);

          if ( userOptions_.vertex_has_color() )
            _bi.set_color( vh, cf );
        }
      } else {
        //with alpha
        if ( options_.color_has_alpha() ){
          readValue(_in, cA[0]);
          readValue(_in, cA[1]);
          readValue(_in, cA[2]);
          readValue(_in, cA[3]);

          if ( userOptions_.vertex_has_color() )
            _bi.set_color( vh, Vec4uc( cA ) );
        }else{
          //without alpha
          readValue(_in, c[0]);
          readValue(_in, c[1]);
          readValue(_in, c[2]);

          if ( userOptions_.vertex_has_color() )
            _bi.set_color( vh, Vec3uc( c ) );
        }
Jan Möbius's avatar
Jan Möbius committed
517
518
      }
    }
519

Jan Möbius's avatar
Jan Möbius committed
520
521
522
523
524
525
526
527
    if ( options_.vertex_has_texcoord()) {
      readValue(_in, t[0]);
      readValue(_in, t[1]);

      if ( userOptions_.vertex_has_texcoord() )
        _bi.set_texcoord(vh, t);
    }
  }
528

Jan Möbius's avatar
Jan Möbius committed
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
  // faces
  // #N <v1> <v2> .. <v(n-1)> [color spec]
  // So far color spec is unsupported!
  for (i=0; i<nF; ++i)
  {
    readValue(_in, nV);

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

      vhandles[0] = VertexHandle(j);
      vhandles[1] = VertexHandle(k);
      vhandles[2] = VertexHandle(l);
546
    } else {
Jan Möbius's avatar
Jan Möbius committed
547
548
549
550
551
552
553
      vhandles.clear();
      for (j=0; j<nV; ++j)
      {
         readValue(_in, idx);
         vhandles.push_back(VertexHandle(idx));
      }
    }
554

Jan Möbius's avatar
Jan Möbius committed
555
556
557
    FaceHandle fh = _bi.add_face(vhandles);

    //face color
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
    if ( _opt.face_has_color() ) {
      if ( userOptions_.color_is_float() ) {
        _opt += Options::ColorFloat;
        //with alpha
        if ( options_.color_has_alpha() ){
          readValue(_in, cAf[0]);
          readValue(_in, cAf[1]);
          readValue(_in, cAf[2]);
          readValue(_in, cAf[3]);

          if ( userOptions_.face_has_color() )
            _bi.set_color( fh , cAf );
        }else{
          //without alpha
          readValue(_in, cf[0]);
          readValue(_in, cf[1]);
          readValue(_in, cf[2]);

          if ( userOptions_.face_has_color() )
            _bi.set_color( fh, cf );
        }
      } else {
        //with alpha
        if ( options_.color_has_alpha() ){
          readValue(_in, cA[0]);
          readValue(_in, cA[1]);
          readValue(_in, cA[2]);
          readValue(_in, cA[3]);

          if ( userOptions_.face_has_color() )
            _bi.set_color( fh , Vec4uc( cA ) );
        }else{
          //without alpha
          readValue(_in, c[0]);
          readValue(_in, c[1]);
          readValue(_in, c[2]);

          if ( userOptions_.face_has_color() )
            _bi.set_color( fh, Vec3uc( c ) );
        }
Jan Möbius's avatar
Jan Möbius committed
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
      }
    }

  }

  // File was successfully parsed.
  return true;

}


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


bool _OFFReader_::can_u_read(const std::string& _filename) const
{
  // !!! Assuming BaseReader::can_u_parse( std::string& )
  // does not call BaseReader::read_magic()!!!
  if (BaseReader::can_u_read(_filename))
  {
    std::ifstream ifs(_filename.c_str());
    if (ifs.is_open() && can_u_read(ifs))
    {
      ifs.close();
      return true;
623
    }
Jan Möbius's avatar
Jan Möbius committed
624
625
626
627
628
629
630
  }
  return false;
}


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

631

Jan Möbius's avatar
Jan Möbius committed
632
633
634
635
636
637
638
639
640
bool _OFFReader_::can_u_read(std::istream& _is) const
{
  options_.cleanup();

  // read 1st line
  char line[LINE_LEN], *p;
  _is.getline(line, LINE_LEN);
  p = line;

Matthias Möller's avatar
Matthias Möller committed
641
  std::streamsize remainingChars = _is.gcount();
Jan Möbius's avatar
Jan Möbius committed
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661

  bool vertexDimensionTooHigh = false;

  // check header: [ST][C][N][4][n]OFF BINARY

  if ( ( remainingChars > 1 ) && ( p[0] == 'S' && p[1] == 'T') )
  { options_ += Options::VertexTexCoord; p += 2; remainingChars -= 2; }

  if ( ( remainingChars > 0 ) && ( p[0] == 'C') )
  { options_ += Options::VertexColor;
    options_ += Options::FaceColor; ++p; --remainingChars; }

  if ( ( remainingChars > 0 ) && ( p[0] == 'N') )
  { options_ += Options::VertexNormal; ++p; --remainingChars; }

  if ( ( remainingChars > 0 ) && (p[0] == '4' ) )
  { vertexDimensionTooHigh = true; ++p; --remainingChars; }

  if ( ( remainingChars > 0 ) && ( p[0] == 'n') )
  { vertexDimensionTooHigh = true; ++p; --remainingChars; }
662

Jan Möbius's avatar
Jan Möbius committed
663
664
665
666
  if ( ( remainingChars < 3 ) || (!(p[0] == 'O' && p[1] == 'F' && p[2] == 'F')  ) )
    return false;

  p += 4;
Jan Möbius's avatar
Jan Möbius committed
667
668
669
670

  // Detect possible garbage and make sure, we don't have an underflow
  if ( remainingChars >= 4 )
    remainingChars -= 4;
671
  else
Jan Möbius's avatar
Jan Möbius committed
672
    remainingChars = 0;
Jan Möbius's avatar
Jan Möbius committed
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688

  if ( ( remainingChars >= 6 ) && ( strncmp(p, "BINARY", 6) == 0 ) )
    options_+= Options::Binary;

  // vertex Dimensions != 3 are currently not supported
  if (vertexDimensionTooHigh)
    return false;

  return true;
}


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