OBJReader.cc 21.5 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
64
65
66
67
68
69
70
 *                                                                           *
\*===========================================================================*/


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


// OpenMesh
#include <OpenMesh/Core/IO/reader/OBJReader.hh>
#include <OpenMesh/Core/IO/IOManager.hh>
#include <OpenMesh/Core/Utils/vector_cast.hh>
#include <OpenMesh/Core/Utils/color_cast.hh>
// STL
#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

#ifndef WIN32
#endif

71
#include <fstream>
72

Jan Möbius's avatar
Jan Möbius committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//=== NAMESPACES ==============================================================


namespace OpenMesh {
namespace IO {


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


_OBJReader_  __OBJReaderInstance;
_OBJReader_& OBJReader() { return __OBJReaderInstance; }


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

89
90
91
92
93
//-----------------------------------------------------------------------------

void trimString( std::string& _string) {
  // Trim Both leading and trailing spaces

94
95
  size_t start = _string.find_first_not_of(" \t\r\n");
  size_t end   = _string.find_last_not_of(" \t\r\n");
96
97
98
99
100
101
102

  if(( std::string::npos == start ) || ( std::string::npos == end))
    _string = "";
  else
    _string = _string.substr( start, end-start+1 );
}

103
104
105
106
107
//-----------------------------------------------------------------------------

// remove duplicated indices from one face
void remove_duplicated_vertices(BaseImporter::VHandles& _indices)
{
108
109
110
111
112
  BaseImporter::VHandles::iterator endIter = _indices.end();
  for (BaseImporter::VHandles::iterator iter = _indices.begin(); iter != endIter; ++iter)
    endIter = std::remove(iter+1, endIter, *(iter));

  _indices.erase(endIter,_indices.end());
113
114
}

115
//-----------------------------------------------------------------------------
Jan Möbius's avatar
Jan Möbius committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132

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


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


bool
_OBJReader_::
read(const std::string& _filename, BaseImporter& _bi, Options& _opt)
{
  std::fstream in( _filename.c_str(), std::ios_base::in );

133
  if (!in.is_open() || !in.good())
Jan Möbius's avatar
Jan Möbius committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
  {
    omerr() << "[OBJReader] : cannot not open file "
          << _filename
          << std::endl;
    return false;
  }

  {
#if defined(WIN32)
    std::string::size_type dot = _filename.find_last_of("\\/");
#else
    std::string::size_type dot = _filename.rfind("/");
#endif
    path_ = (dot == std::string::npos)
      ? "./"
      : std::string(_filename.substr(0,dot+1));
  }

  bool result = read(in, _bi, _opt);

  in.close();
  return result;
}

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

bool
_OBJReader_::
read_material(std::fstream& _in)
{
  std::string line;
  std::string keyWrd;
166
  std::string textureName;
Jan Möbius's avatar
Jan Möbius committed
167

Martin Schultz's avatar
Martin Schultz committed
168
  std::stringstream  stream;
169

Jan Möbius's avatar
Jan Möbius committed
170
171
172
173
  std::string key;
  Material    mat;
  float       f1,f2,f3;
  bool        indef = false;
174
  int         textureId = 1;
Jan Möbius's avatar
Jan Möbius committed
175
176


Martin Habbecke's avatar
Martin Habbecke committed
177
  materials_.clear();
Jan Möbius's avatar
Jan Möbius committed
178
179
180
181
182
183
184
185
186
187
188
189
190
  mat.cleanup();

  while( _in && !_in.eof() )
  {
    std::getline(_in,line);
    if ( _in.bad() ){
      omerr() << "  Warning! Could not read file properly!\n";
      return false;
    }

    if ( line.empty() )
      continue;

191
192
    stream.str(line);
    stream.clear();
Jan Möbius's avatar
Jan Möbius committed
193
194
195

    stream >> keyWrd;

196
    if( ( isspace(line[0]) && line[0] != '\t' ) || line[0] == '#' )
Jan Möbius's avatar
Jan Möbius committed
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
    {
      if (indef && !key.empty() && mat.is_valid())
      {
        materials_[key] = mat;
        mat.cleanup();
      }
    }

    else if (keyWrd == "newmtl") // begin new material definition
    {
      stream >> key;
      indef = true;
    }

    else if (keyWrd == "Kd") // diffuse color
    {
      stream >> f1; stream >> f2; stream >> f3;

      if( !stream.fail() )
        mat.set_Kd(f1,f2,f3);
    }

    else if (keyWrd == "Ka") // ambient color
    {
      stream >> f1; stream >> f2; stream >> f3;

      if( !stream.fail() )
        mat.set_Ka(f1,f2,f3);
    }

    else if (keyWrd == "Ks") // specular color
    {
      stream >> f1; stream >> f2; stream >> f3;

      if( !stream.fail() )
        mat.set_Ks(f1,f2,f3);
    }
#if 0
    else if (keyWrd == "illum") // diffuse/specular shading model
    {
      ; // just skip this
    }

    else if (keyWrd == "Ns") // Shininess [0..200]
    {
      ; // just skip this
    }

    else if (keyWrd == "map_") // map images
    {
      // map_Ks, specular map
      // map_Ka, ambient map
      // map_Bump, bump map
      // map_d,  opacity map
      ; // just skip this
    }
#endif
254
    else if (keyWrd == "map_Kd" ) {
255
256
      // Get the rest of the line, removing leading or trailing spaces
      // This will define the filename of the texture
257
258
      std::getline(stream,textureName);
      trimString(textureName);
259
260
      if ( ! textureName.empty() )
        mat.set_map_Kd( textureName, textureId++ );
261
    }
Jan Möbius's avatar
Jan Möbius committed
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
    else if (keyWrd == "Tr") // transparency value
    {
      stream >> f1;

      if( !stream.fail() )
        mat.set_Tr(f1);
    }
    else if (keyWrd == "d") // transparency value
    {
      stream >> f1;

      if( !stream.fail() )
        mat.set_Tr(f1);
    }

    if ( _in && indef && mat.is_valid() && !key.empty())
      materials_[key] = mat;
  }
  return true;
}

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

bool
_OBJReader_::
287
read(std::istream& _in, BaseImporter& _bi, Options& _opt)
Jan Möbius's avatar
Jan Möbius committed
288
289
290
291
292
{

  std::string line;
  std::string keyWrd;

293
  float                     x, y, z, u, v, w;
294
  float                     r, g, b;
295
296
  BaseImporter::VHandles    vhandles;
  std::vector<Vec3f>        normals;
297
  std::vector<Vec3f>        colors;
298
299
  std::vector<Vec3f>        texcoords3d, face_texcoords3d;
  std::vector<Vec2f>        texcoords, face_texcoords;
300
  std::vector<VertexHandle> vertexHandles;
301

302
  std::string               matname;
Jan Möbius's avatar
Jan Möbius committed
303

Martin Schultz's avatar
Martin Schultz committed
304
  std::stringstream         stream, lineData, tmp;
305

Jan Möbius's avatar
Jan Möbius committed
306

307
308
309
310
311
312
313
  // Options supplied by the user
  Options userOptions = _opt;

  // Options collected via file parsing
  Options fileOptions;


Jan Möbius's avatar
Jan Möbius committed
314
315
316
317
318
319
320
321
  while( _in && !_in.eof() )
  {
    std::getline(_in,line);
    if ( _in.bad() ){
      omerr() << "  Warning! Could not read file properly!\n";
      return false;
    }

322
    // Trim Both leading and trailing spaces
323
    trimString(line);
324

Jan Möbius's avatar
Jan Möbius committed
325
326
327
328
329
    // comment
    if ( line.size() == 0 || line[0] == '#' || isspace(line[0]) ) {
      continue;
    }

330
331
    stream.str(line);
    stream.clear();
Jan Möbius's avatar
Jan Möbius committed
332
333
334
335
336
337
338
339

    stream >> keyWrd;

    // material file
    if (keyWrd == "mtllib")
    {
      std::string matFile;

Jan Möbius's avatar
Jan Möbius committed
340
341
342
343
      // Get the rest of the line, removing leading or trailing spaces
      // This will define the filename of the texture
      std::getline(stream,matFile);
      trimString(matFile);
Jan Möbius's avatar
Jan Möbius committed
344
345
346

      matFile = path_ + matFile;

Jan Möbius's avatar
Jan Möbius committed
347
      //omlog() << "Load material file " << matFile << std::endl;
Jan Möbius's avatar
Jan Möbius committed
348
349
350
351
352
353
354
355
356
357
358

      std::fstream matStream( matFile.c_str(), std::ios_base::in );

      if ( matStream ){

        if ( !read_material( matStream ) )
	        omerr() << "  Warning! Could not read file properly!\n";
        matStream.close();

      }else
	      omerr() << "  Warning! Material file '" << matFile << "' not found!\n";
359

Jan Möbius's avatar
Jan Möbius committed
360
      //omlog() << "  " << materials_.size() << " materials loaded.\n";
361

Jan Möbius's avatar
Jan Möbius committed
362
      for ( MaterialList::iterator material = materials_.begin(); material != materials_.end(); ++material )
363
      {
Jan Möbius's avatar
Jan Möbius committed
364
365
366
        // Save the texture information in a property
        if ( (*material).second.has_map_Kd() )
          _bi.add_texture_information( (*material).second.map_Kd_index() , (*material).second.map_Kd() );
367
368
      }

Jan Möbius's avatar
Jan Möbius committed
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
    }

    // usemtl
    else if (keyWrd == "usemtl")
    {
      stream >> matname;
      if (materials_.find(matname)==materials_.end())
      {
        omerr() << "Warning! Material '" << matname
              << "' not defined in material file.\n";
        matname="";
      }
    }

    // vertex
    else if (keyWrd == "v")
    {
      stream >> x; stream >> y; stream >> z;

      if ( !stream.fail() )
389
390
      {
        vertexHandles.push_back(_bi.add_vertex(OpenMesh::Vec3f(x,y,z)));
391
        stream >> r; stream >> g; stream >> b;
392

393
394
        if ( !stream.fail() )
        {
395
396
          if (  userOptions.vertex_has_color() ) {
            fileOptions += Options::VertexColor;
397
            colors.push_back(OpenMesh::Vec3f(r,g,b));
398
          }
399
        }
400
      }
Jan Möbius's avatar
Jan Möbius committed
401
402
403
404
405
406
407
    }

    // texture coord
    else if (keyWrd == "vt")
    {
      stream >> u; stream >> v;

408
409
410
411
412
413
414
415
      if ( !stream.fail()  ){
        if ( userOptions.vertex_has_texcoord() || userOptions.face_has_texcoord() ) {
          texcoords.push_back(OpenMesh::Vec2f(u, v));

          // Can be used for both!
          fileOptions += Options::VertexTexCoord;
          fileOptions += Options::FaceTexCoord;
        }
Jan Möbius's avatar
Jan Möbius committed
416

417
418
419
420
421
422
423
424
425
426
427
        //try to read the w component as it is optional
        stream >> w;
        if ( !stream.fail()  ){
          if ( userOptions.vertex_has_texcoord() || userOptions.face_has_texcoord() ) {
            texcoords3d.push_back(OpenMesh::Vec3f(u, v, w));

            // Can be used for both!
            fileOptions += Options::VertexTexCoord;
            fileOptions += Options::FaceTexCoord;
          }
        }
Jan Möbius's avatar
Jan Möbius committed
428

429
430
      }else{
        omerr() << "Only single 2D or 3D texture coordinate per vertex"
Jan Möbius's avatar
Jan Möbius committed
431
432
433
434
435
              << "allowed!" << std::endl;
        return false;
      }
    }

436
437
438
439
440
    // color per vertex
    else if (keyWrd == "vc")
    {
      stream >> r; stream >> g; stream >> b;

441
442
      if ( !stream.fail()   ){
        if ( userOptions.vertex_has_color() ) {
443
          colors.push_back(OpenMesh::Vec3f(r,g,b));
444
445
          fileOptions += Options::VertexColor;
        }
446
447
      }
    }
Jan Möbius's avatar
Jan Möbius committed
448
449
450
451
452
453

    // normal
    else if (keyWrd == "vn")
    {
      stream >> x; stream >> y; stream >> z;

454
455
456
457
458
      if ( !stream.fail() ) {
        if (userOptions.vertex_has_normal() ){
          normals.push_back(OpenMesh::Vec3f(x,y,z));
          fileOptions += Options::VertexNormal;
        }
Jan Möbius's avatar
Jan Möbius committed
459
460
461
462
463
464
465
466
467
468
469
      }
    }


    // face
    else if (keyWrd == "f")
    {
      int component(0), nV(0);
      int value;

      vhandles.clear();
470
      face_texcoords.clear();
Jan Möbius's avatar
Jan Möbius committed
471
472
473
474

      // read full line after detecting a face
      std::string faceLine;
      std::getline(stream,faceLine);
475
476
      lineData.str( faceLine );
      lineData.clear();
Jan Möbius's avatar
Jan Möbius committed
477

478
479
480
      FaceHandle fh;
      BaseImporter::VHandles faceVertices;

Jan Möbius's avatar
Jan Möbius committed
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
      // work on the line until nothing left to read
      while ( !lineData.eof() )
      {
        // read one block from the line ( vertex/texCoord/normal )
        std::string vertex;
        lineData >> vertex;

        do{

          //get the component (vertex/texCoord/normal)
          size_t found=vertex.find("/");

          // parts are seperated by '/' So if no '/' found its the last component
          if( found != std::string::npos ){

            // read the index value
497
498
            tmp.str( vertex.substr(0,found) );
            tmp.clear();
Jan Möbius's avatar
Jan Möbius committed
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520

            // If we get an empty string this property is undefined in the file
            if ( vertex.substr(0,found).empty() ) {
              // Switch to next field
              vertex = vertex.substr(found+1);

              // Now we are at the next component
              ++component;

              // Skip further processing of this component
              continue;
            }

            // Read current value
            tmp >> value;

            // remove the read part from the string
            vertex = vertex.substr(found+1);

          } else {

            // last component of the vertex, read it.
521
522
            tmp.str( vertex );
            tmp.clear();
Jan Möbius's avatar
Jan Möbius committed
523
524
            tmp >> value;

Jan Möbius's avatar
Jan Möbius committed
525
526
527
            // Clear vertex after finished reading the line
            vertex="";

Jan Möbius's avatar
Jan Möbius committed
528
529
530
531
532
533
534
535
536
537
538
            // Nothing to read here ( garbage at end of line )
            if ( tmp.fail() ) {
              continue;
            }
          }

          // store the component ( each component is referenced by the index here! )
          switch (component)
          {
            case 0: // vertex
              if ( value < 0 ) {
539
540
541
                // Calculation of index :
                // -1 is the last vertex in the list
                // As obj counts from 1 and not zero add +1
542
                value = int(_bi.n_vertices() + value + 1);
Jan Möbius's avatar
Jan Möbius committed
543
              }
544
              // Obj counts from 1 and not zero .. array counts from zero therefore -1
Jan Möbius's avatar
Jan Möbius committed
545
              vhandles.push_back(VertexHandle(value-1));
546
              faceVertices.push_back(VertexHandle(value-1));
547
              if (fileOptions.vertex_has_color() )
548
                _bi.set_color(vhandles.back(), colors[value-1]);
Jan Möbius's avatar
Jan Möbius committed
549
              break;
550
	      
Jan Möbius's avatar
Jan Möbius committed
551
552
            case 1: // texture coord
              if ( value < 0 ) {
553
554
555
                // Calculation of index :
                // -1 is the last vertex in the list
                // As obj counts from 1 and not zero add +1
556
                value = int(texcoords.size()) + value + 1;
Jan Möbius's avatar
Jan Möbius committed
557
558
              }
              assert(!vhandles.empty());
559
560
561
562
563
564
565


              if ( fileOptions.vertex_has_texcoord() && userOptions.vertex_has_texcoord() ) {

                if (!texcoords.empty() && (unsigned int) (value - 1) < texcoords.size()) {
                  // Obj counts from 1 and not zero .. array counts from zero therefore -1
                  _bi.set_texcoord(vhandles.back(), texcoords[value - 1]);
566
567
                  if(!texcoords3d.empty() && (unsigned int) (value -1) < texcoords3d.size())
                    _bi.set_texcoord(vhandles.back(), texcoords3d[value - 1]);
568
569
570
571
572
573
574
575
576
577
                } else {
                  omerr() << "Error setting Texture coordinates" << std::endl;
                }

                }

                if (fileOptions.face_has_texcoord() && userOptions.face_has_texcoord() ) {

                  if (!texcoords.empty() && (unsigned int) (value - 1) < texcoords.size()) {
                    face_texcoords.push_back( texcoords[value-1] );
578
579
                    if(!texcoords3d.empty() && (unsigned int) (value -1) < texcoords3d.size())
                      face_texcoords3d.push_back( texcoords3d[value-1] );
580
581
582
583
584
                  } else {
                    omerr() << "Error setting Texture coordinates" << std::endl;
                  }
                }

585

Jan Möbius's avatar
Jan Möbius committed
586
587
588
589
              break;

            case 2: // normal
              if ( value < 0 ) {
590
591
592
                // Calculation of index :
                // -1 is the last vertex in the list
                // As obj counts from 1 and not zero add +1
593
                value = int(normals.size()) + value + 1;
Jan Möbius's avatar
Jan Möbius committed
594
              }
595

596
              // Obj counts from 1 and not zero .. array counts from zero therefore -1
597
598
599
600
601
              if (fileOptions.vertex_has_normal() ) {
                assert(!vhandles.empty());
                assert((unsigned int)(value-1) < normals.size());
                _bi.set_normal(vhandles.back(), normals[value-1]);
              }
Jan Möbius's avatar
Jan Möbius committed
602
603
604
605
606
607
              break;
          }

          // Prepare for reading next component
          ++component;

Jan Möbius's avatar
Jan Möbius committed
608
609
          // Read until line does not contain any other info
        } while ( !vertex.empty() );
Jan Möbius's avatar
Jan Möbius committed
610
611
612
613

        component = 0;
        nV++;

614
      }
Jan Möbius's avatar
Jan Möbius committed
615

616
617
      // note that add_face can possibly triangulate the faces, which is why we have to
      // store the current number of faces first
Jan Möbius's avatar
Jan Möbius committed
618
      size_t n_faces = _bi.n_faces();
619
620
621
622
623
      remove_duplicated_vertices(faceVertices);

      //A minimum of three vertices are required.
      if (faceVertices.size() > 2)
        fh = _bi.add_face(faceVertices);
Jan Möbius's avatar
Jan Möbius committed
624

625
      if (!vhandles.empty() && fh.is_valid() )
626
      {
627
        _bi.add_face_texcoords(fh, vhandles[0], face_texcoords);
628
629
        _bi.add_face_texcoords(fh, vhandles[0], face_texcoords3d);
      }
630

631
      if ( !matname.empty()  )
Jan Möbius's avatar
Jan Möbius committed
632
633
634
635
      {
        std::vector<FaceHandle> newfaces;

        for( size_t i=0; i < _bi.n_faces()-n_faces; ++i )
636
          newfaces.push_back(FaceHandle(int(n_faces+i)));
Jan Möbius's avatar
Jan Möbius committed
637

638
639
640
641
642
        Material& mat = materials_[matname];

        if ( mat.has_Kd() ) {
          Vec3uc fc = color_cast<Vec3uc, Vec3f>(mat.Kd());

643
644
645
646
          if ( userOptions.face_has_color()) {

            for (std::vector<FaceHandle>::iterator it = newfaces.begin(); it != newfaces.end(); ++it)
              _bi.set_color(*it, fc);
647

648
649
            fileOptions += Options::FaceColor;
          }
650
651
652
653
        }

        // Set the texture index in the face index property
        if ( mat.has_map_Kd() ) {
Jan Möbius's avatar
Jan Möbius committed
654

655
656
657
658
659
660
661
662
          if (userOptions.face_has_texcoord()) {

            for (std::vector<FaceHandle>::iterator it = newfaces.begin(); it != newfaces.end(); ++it)
              _bi.set_face_texindex(*it, mat.map_Kd_index());

            fileOptions += Options::FaceTexCoord;

          }
Jan Möbius's avatar
Jan Möbius committed
663

664
        } else {
665

666
          // If we don't have the info, set it to no texture
667
668
669
670
671
672
          if (userOptions.face_has_texcoord()) {

            for (std::vector<FaceHandle>::iterator it = newfaces.begin(); it != newfaces.end(); ++it)
              _bi.set_face_texindex(*it, 0);

          }
673
        }
Jan Möbius's avatar
Jan Möbius committed
674

675
676
677
678
      } else {
        std::vector<FaceHandle> newfaces;

        for( size_t i=0; i < _bi.n_faces()-n_faces; ++i )
679
          newfaces.push_back(FaceHandle(int(n_faces+i)));
680
681

        // Set the texture index to zero as we don't have any information
682
683
684
        if ( userOptions.face_has_texcoord() )
          for (std::vector<FaceHandle>::iterator it = newfaces.begin(); it != newfaces.end(); ++it)
            _bi.set_face_texindex(*it, 0);
Jan Möbius's avatar
Jan Möbius committed
685
      }
686

Jan Möbius's avatar
Jan Möbius committed
687
    }
688

Jan Möbius's avatar
Jan Möbius committed
689
690
  }

691
692
  // If we do not have any faces,
  // assume this is a point cloud and read the normals and colors directly
693
  if (_bi.n_faces() == 0)
694
  {
695
    int i = 0;
696
697
    // add normal per vertex

698
699
700
701
702
703
    if (normals.size() == _bi.n_vertices()) {
      if ( fileOptions.vertex_has_normal() && userOptions.vertex_has_normal() ) {
        for (std::vector<VertexHandle>::iterator it = vertexHandles.begin(); it != vertexHandles.end(); ++it, i++)
          _bi.set_normal(*it, normals[i]);
      }
    }
704
705

    // add color per vertex
706
707
708
709
710
    i = 0;
    if (colors.size() >= _bi.n_vertices())
      if (fileOptions.vertex_has_color() && userOptions.vertex_has_color()) {
        for (std::vector<VertexHandle>::iterator it = vertexHandles.begin(); it != vertexHandles.end(); ++it, i++)
          _bi.set_color(*it, colors[i]);
711
712
713
714
      }

  }

Jan Möbius's avatar
Jan Möbius committed
715
716
717
  // Return, what we actually read
  _opt = fileOptions;

Jan Möbius's avatar
Jan Möbius committed
718
719
720
721
722
723
724
725
  return true;
}


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