OBJImporter.cc 32.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
*                                                                            *
*                              OpenFlipper                                   *
Jan Möbius's avatar
Jan Möbius committed
4
*      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen       *
Jan Möbius's avatar
Jan Möbius committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
*                           www.openflipper.org                              *
*                                                                            *
*--------------------------------------------------------------------------- *
*  This file is part of OpenFlipper.                                         *
*                                                                            *
*  OpenFlipper is free software: you can redistribute it and/or modify       *
*  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:                                                     *
*                                                                            *
*  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.                         *
*                                                                            *
*  OpenFlipper 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.                       *
*                                                                            *
*  You should have received a copy of the GNU LesserGeneral Public           *
*  License along with OpenFlipper. If not,                                   *
*  see <http://www.gnu.org/licenses/>.                                       *
*                                                                            *
Jan Möbius's avatar
Jan Möbius committed
33
34
35
\*===========================================================================*/

/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
36
37
38
39
40
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
Jan Möbius's avatar
Jan Möbius committed
41
42
\*===========================================================================*/

Dirk Wilden's avatar
Dirk Wilden committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "OBJImporter.hh"

#include <OpenMesh/Core/Utils/vector_cast.hh>

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

/// base class needs virtual destructor
OBJImporter::~OBJImporter(){
  
}

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

/// add a vertex with coordinate \c _point
VertexHandle OBJImporter::addVertex(const Vec3f& _point){
  vertices_.push_back( _point );
  
  return vertices_.size()-1;
}

Dirk Wilden's avatar
Dirk Wilden committed
64
/// get vertex with given index
Jan Möbius's avatar
Jan Möbius committed
65
Vec3f OBJImporter::vertex(unsigned int _index){
Dirk Wilden's avatar
Dirk Wilden committed
66
67
68
69
70
71
72
  
  if ( vertices_.size() > _index )
    return vertices_[ _index ];
  else
    return Vec3f();
}

Dirk Wilden's avatar
Dirk Wilden committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//-----------------------------------------------------------------------------

/// add texture coordinates
int OBJImporter::addTexCoord(const Vec2f& _coord){
  texCoords_.push_back( _coord );
  
  return texCoords_.size()-1;
}

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

/// add a normal
int OBJImporter::addNormal(const Vec3f& _normal){
  normals_.push_back( _normal );
  
  return normals_.size()-1;
}

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

Dirk Wilden's avatar
Dirk Wilden committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/// set degree U direction
void OBJImporter::setDegreeU(int _degree){
  degreeU_ = _degree;
}

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

/// set degree V direction
void OBJImporter::setDegreeV(int _degree){
  degreeV_ = _degree;
}


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

/// get current degree
int OBJImporter::degreeU(){
  return degreeU_;
}

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

/// get current degree
int OBJImporter::degreeV(){
  return degreeV_;
}

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

Dirk Wilden's avatar
Dirk Wilden committed
122
/// add a mesh
123
void OBJImporter::setObject(BaseObject* _object, int _groupId) {
Dirk Wilden's avatar
Dirk Wilden committed
124

125
126
127
    if((unsigned int)_groupId >= triMeshes_.size()) {
        std::cerr << "Error: Group does not exist!" << std::endl;
        return;
128
    }
Dirk Wilden's avatar
Dirk Wilden committed
129

130
    if(PluginFunctions::polyMeshObject(_object->id()) != NULL) {
Dirk Wilden's avatar
Dirk Wilden committed
131

132
133
        polyMeshes_[_groupId] = PluginFunctions::polyMeshObject(_object->id());
        addUsedVertices(_groupId);
Dirk Wilden's avatar
Dirk Wilden committed
134

135
    } else if(PluginFunctions::triMeshObject(_object->id()) != NULL) {
Dirk Wilden's avatar
Dirk Wilden committed
136

137
138
139
140
141
        triMeshes_[_groupId] = PluginFunctions::triMeshObject(_object->id());
        addUsedVertices(_groupId);
    }
#ifdef ENABLE_BSPLINECURVE_SUPPORT
    else if(PluginFunctions::bsplineCurveObject(PluginFunctions::baseObjectData(_object)) != NULL) {
Dirk Wilden's avatar
Dirk Wilden committed
142

143
144
145
146
147
148
149
150
151
152
        bSplineCurves_[_groupId] = PluginFunctions::bsplineCurveObject(PluginFunctions::baseObjectData(_object));
    }
#endif
#ifdef ENABLE_BSPLINESURFACE_SUPPORT
    else if(PluginFunctions::bsplineSurfaceObject(PluginFunctions::baseObjectData(_object)) != NULL) {

        bSplineSurfaces_[_groupId] = PluginFunctions::bsplineSurfaceObject(PluginFunctions::baseObjectData(_object));
    }
#endif
    else {
153
154
        std::cerr << "Error: Cannot add object. Type is unknown!" << std::endl;
    }
Dirk Wilden's avatar
Dirk Wilden committed
155
156
157
158
159
160
}

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

/// get id of the active object
int OBJImporter::currentObject(){
161
162

    return currentGroup_;
Dirk Wilden's avatar
Dirk Wilden committed
163
164
165
166
167
168
}

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

/// get the active polyMesh
PolyMesh* OBJImporter::currentPolyMesh(){
169

170
    return polyMeshes_[currentGroup_]->mesh();
Dirk Wilden's avatar
Dirk Wilden committed
171
172
173
174
175
176
}

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

/// get the active triMesh
TriMesh* OBJImporter::currentTriMesh(){
177

178
    return triMeshes_[currentGroup_]->mesh();
Dirk Wilden's avatar
Dirk Wilden committed
179
180
181
182
}

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

Dirk Wilden's avatar
Dirk Wilden committed
183
184
185
#ifdef ENABLE_BSPLINECURVE_SUPPORT

BSplineCurve* OBJImporter::currentCurve(){
186

187
    return bSplineCurves_[currentGroup_]->splineCurve();
Dirk Wilden's avatar
Dirk Wilden committed
188
189
190
191
192
193
194
195
196
}
  
#endif

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

#ifdef ENABLE_BSPLINESURFACE_SUPPORT

BSplineSurface* OBJImporter::currentSurface(){
197

198
    return bSplineSurfaces_[currentGroup_]->splineSurface();
Dirk Wilden's avatar
Dirk Wilden committed
199
200
201
202
203
204
}
  
#endif

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

205
/// add all vertices that are used to the mesh (in correct order)
206
void OBJImporter::addUsedVertices(int _groupId) {
Dirk Wilden's avatar
Dirk Wilden committed
207

208
    if (isTriangleMesh(_groupId)) {
Dirk Wilden's avatar
Dirk Wilden committed
209

210
        //handle triangle meshes
211
        TriMesh* curMesh = triMeshes_[_groupId]->mesh();
212
213
        if (curMesh == NULL)
            return;
214

215
        // add all vertices to the mesh
216

217
218
        if(usedVertices_.size() <= (unsigned int)_groupId) return;

219
220
        for(std::map<int, VertexHandle>::const_iterator it = usedVertices_[_groupId].begin();
                it != usedVertices_[_groupId].end(); ++it) {
221

222
            if (it->first >= (int)vertices_.size()) {
223
224
225
                std::cerr << "Error: Vertex ID too large" << std::endl;
                continue;
            }
Dirk Wilden's avatar
Dirk Wilden committed
226

227
228
            if (vertexMapTri_[_groupId].find(it->first) == vertexMapTri_[_groupId].end()) {
                vertexMapTri_[_groupId].insert(std::pair<int, TriMesh::VertexHandle>(it->first, curMesh->add_vertex((TriMesh::Point)vertices_[it->first])));
229
230
            }
        }
Dirk Wilden's avatar
Dirk Wilden committed
231

232
    } else if (isPolyMesh(_groupId)) {
233

234
        //handle triangle meshes
235
        PolyMesh* curMesh = polyMeshes_[_groupId]->mesh();
236
237
        if (curMesh == NULL)
            return;
238

239
        //add all vertices to the mesh
Dirk Wilden's avatar
Dirk Wilden committed
240

241
242
        if(usedVertices_.size() <= (unsigned int)currentObject()) return;

243
244
        for(std::map<int, VertexHandle>::const_iterator it = usedVertices_[_groupId].begin();
                it != usedVertices_[_groupId].end(); ++it) {
245

246
            if (it->first >= (int)vertices_.size()) {
247
                std::cerr << "Error: Vertex ID too large" << std::endl;
248
                continue;
249
250
            }

251
252
253
            if (vertexMapPoly_[_groupId].find(it->first) == vertexMapPoly_[_groupId].end()) {
                vertexMapPoly_[_groupId].insert(std::pair<int, PolyMesh::VertexHandle>(it->first, curMesh->add_vertex((PolyMesh::Point)vertices_[it->first])));
            }
254
        }
255
    }
Dirk Wilden's avatar
Dirk Wilden committed
256
257
258
259
260
261
262
263
264
265
266
267
268
269
}

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

/// set vertex texture coordinate
void OBJImporter::setVertexTexCoord(VertexHandle _vh, int _texCoordID){
  
  if ( isTriangleMesh( currentObject() ) ){
  
    //handle triangle meshes
    if ( !currentTriMesh() ) return;
    
    if ( _texCoordID < (int) texCoords_.size() ){
    
270
271
272
273
274
      //perhaps request texCoords for the mesh
      if ( !currentTriMesh()->has_vertex_texcoords2D() )
        currentTriMesh()->request_vertex_texcoords2D();
      
      //set the texCoords
275
276
      if ( vertexMapTri_[currentGroup_].find( _vh ) != vertexMapTri_[currentGroup_].end() )
        currentTriMesh()->set_texcoord2D( vertexMapTri_[currentGroup_][_vh], texCoords_[ _texCoordID ] );
Dirk Wilden's avatar
Dirk Wilden committed
277
278
279
280
281
282
283
284
285
286
287

    }else{
      std::cerr << "Error: TexCoord ID too large" << std::endl;
    }
  
  } else if ( isPolyMesh( currentObject() ) ){
    
    //handle poly meshes
    if ( !currentPolyMesh() ) return;
    
    if ( _texCoordID < (int) texCoords_.size() ){
288
289
290
291
292
293

      //perhaps request texCoords for the mesh
      if ( !currentPolyMesh()->has_vertex_texcoords2D() )
        currentPolyMesh()->request_vertex_texcoords2D();
      
      //set the texCoords
294
295
      if ( vertexMapPoly_[currentGroup_].find( _vh ) != vertexMapPoly_[currentGroup_].end() )
        currentPolyMesh()->set_texcoord2D( vertexMapPoly_[currentGroup_][_vh], texCoords_[ _texCoordID ] );
Dirk Wilden's avatar
Dirk Wilden committed
296
297
298
299
300
301
302
303
304
305

    }else{
      std::cerr << "Error: TexCoord ID too large" << std::endl;
    }
  }
}

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

/// set vertex normal
306
void OBJImporter::setNormal(int _index, int _normalID){
Dirk Wilden's avatar
Dirk Wilden committed
307
308
309
310
311
312
313
314
  
  if ( isTriangleMesh( currentObject() ) ){
  
    //handle triangle meshes
    if ( !currentTriMesh() ) return;
    
    if ( _normalID < (int) normals_.size() ){
    
315
316
      if ( vertexMapTri_[currentGroup_].find( _index ) != vertexMapTri_[currentGroup_].end() ){
        currentTriMesh()->set_normal( vertexMapTri_[currentGroup_][_index], (TriMesh::Point) normals_[ _normalID ] );
317
        objectOptions_[ currentGroup_ ] |= NORMALS;
Dirk Wilden's avatar
Dirk Wilden committed
318
319
320
321
322
323
324
325
326
327
328
329
330
      }

    }else{
      std::cerr << "Error: normal ID too large" << std::endl;
    }
  
  } else if ( isPolyMesh( currentObject() ) ){
    
    //handle poly meshes
    if ( !currentPolyMesh() ) return;
    
    if ( _normalID < (int) normals_.size() ){
    
331
332
      if ( vertexMapPoly_[currentGroup_].find( _index ) != vertexMapPoly_[currentGroup_].end() ){
        currentPolyMesh()->set_normal( vertexMapPoly_[currentGroup_][_index], (PolyMesh::Point) normals_[ _normalID ] );
333
        objectOptions_[ currentGroup_ ] |= NORMALS;
Dirk Wilden's avatar
Dirk Wilden committed
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
      }

    }else{
      std::cerr << "Error: normal ID too large" << std::endl;
    }
    
  }
}

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

/// add a face with indices _indices refering to vertices
void OBJImporter::addFace(const VHandles& _indices){
  
  if ( isTriangleMesh( currentObject() ) ){
  
    //handle triangle meshes
    if ( !currentTriMesh() ) return;
    
353
    addedFacesTri_[currentGroup_].clear();
Dirk Wilden's avatar
Dirk Wilden committed
354
355
356
    
    std::vector< TriMesh::VertexHandle > vertices;
    
Jan Möbius's avatar
Jan Möbius committed
357
    for (unsigned int i=0; i < _indices.size(); i++){
Dirk Wilden's avatar
Dirk Wilden committed
358

359
      if ( vertexMapTri_[currentGroup_].find( _indices[i] ) != vertexMapTri_[currentGroup_].end() ){
Dirk Wilden's avatar
Dirk Wilden committed
360
    
361
        vertices.push_back( vertexMapTri_[currentGroup_][ _indices[i] ] );
Dirk Wilden's avatar
Dirk Wilden committed
362
363

      }else{
Dirk Wilden's avatar
Dirk Wilden committed
364
        std::cerr << "Error: cannot add face. undefined index (" <<  _indices[i] << ")" << std::endl;
Dirk Wilden's avatar
Dirk Wilden committed
365
366
367
368
369
370
        return;
      }
    }
    
    size_t n_faces = currentTriMesh()->n_faces();
    
371
    OpenMesh::FaceHandle fh = currentTriMesh()->add_face( vertices );
Dirk Wilden's avatar
Dirk Wilden committed
372
373
    
    //remember all new faces
374
375
376
377
378
379
380
    if(!fh.is_valid()) {
    	// Store non-manifold face
    	invalidFaces_.push_back(vertices);

    } else {
    	// Store recently added face
    	for( size_t i=0; i < currentTriMesh()->n_faces()-n_faces; ++i )
381
    		addedFacesTri_[currentGroup_].push_back( TriMesh::FaceHandle(n_faces+i) );
382
    }
Dirk Wilden's avatar
Dirk Wilden committed
383
384
385
386
387
388
389
390
391
    
    
  } else if ( isPolyMesh( currentObject() ) ){
  
    //handle poly meshes
    if ( !currentPolyMesh() ) return;
    
    std::vector< PolyMesh::VertexHandle > vertices;
    
Jan Möbius's avatar
Jan Möbius committed
392
    for (unsigned int i=0; i < _indices.size(); i++){
Dirk Wilden's avatar
Dirk Wilden committed
393
      
394
      if ( vertexMapPoly_[currentGroup_].find( _indices[i] ) != vertexMapPoly_[currentGroup_].end() ){
Dirk Wilden's avatar
Dirk Wilden committed
395
    
396
        vertices.push_back( vertexMapPoly_[currentGroup_][ _indices[i] ] );
Dirk Wilden's avatar
Dirk Wilden committed
397
398

      }else{
Dirk Wilden's avatar
Dirk Wilden committed
399
        std::cerr << "Error: cannot add face. undefined index (" <<  _indices[i] << ")" << std::endl;
Dirk Wilden's avatar
Dirk Wilden committed
400
401
402
403
        return;
      }
    }
    
404
405
406
407
408
409
410
411
    OpenMesh::FaceHandle fh = currentPolyMesh()->add_face( vertices );

    if(!fh.is_valid()) {
    	// Store non-manifold face
    	invalidFaces_.push_back(vertices);
    } else {
    	addedFacePoly_ = fh;
    }
Dirk Wilden's avatar
Dirk Wilden committed
412
413
414
415
416
417
418
419
420
421
422
423
424
  }
}

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

/// add face and texture coordinates
void OBJImporter::addFace(const VHandles& _indices, const std::vector<int>& _face_texcoords){
  
  if ( isTriangleMesh( currentObject() ) ){
  
    //handle triangle meshes
    if ( !currentTriMesh() ) return;
    
425
    addedFacesTri_[currentGroup_].clear();
Dirk Wilden's avatar
Dirk Wilden committed
426
427
428
    
    std::vector< TriMesh::VertexHandle > vertices;
    
Jan Möbius's avatar
Jan Möbius committed
429
    for (unsigned int i=0; i < _indices.size(); i++){
Dirk Wilden's avatar
Dirk Wilden committed
430

431
      if ( vertexMapTri_[currentGroup_].find( _indices[i] ) != vertexMapTri_[currentGroup_].end() ){
Dirk Wilden's avatar
Dirk Wilden committed
432
    
433
        vertices.push_back( vertexMapTri_[currentGroup_][ _indices[i] ] );
Dirk Wilden's avatar
Dirk Wilden committed
434
435

      }else{
Dirk Wilden's avatar
Dirk Wilden committed
436
        std::cerr << "Error: cannot add face. undefined index (" <<  _indices[i] << ")" << std::endl;
Dirk Wilden's avatar
Dirk Wilden committed
437
438
439
440
441
442
        return;
      }
    }
    
    size_t n_faces = currentTriMesh()->n_faces();
    
443
    OpenMesh::FaceHandle fh = currentTriMesh()->add_face( vertices );
Dirk Wilden's avatar
Dirk Wilden committed
444
445
    
    //remember all new faces
446
447
448
    if(!fh.is_valid()) {
    	// Store non-manifold face
    	invalidFaces_.push_back(vertices);
Dirk Wilden's avatar
Dirk Wilden committed
449

450
    	std::cerr << "Error: Unable to add face " << std::endl;
Dirk Wilden's avatar
Dirk Wilden committed
451

452
    } else {
Dirk Wilden's avatar
Dirk Wilden committed
453

454
455
456
      // Store recently added face
      for( size_t i=0; i < currentTriMesh()->n_faces()-n_faces; ++i )
        addedFacesTri_[currentGroup_].push_back( TriMesh::FaceHandle(n_faces+i) );
Dirk Wilden's avatar
Dirk Wilden committed
457

458
459
460
461
462
      //perhaps request texCoords for the mesh
      if ( !currentTriMesh()->has_halfedge_texcoords2D() )
        currentTriMesh()->request_halfedge_texcoords2D();

      //now add texCoords
463

464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
      // get first halfedge handle
      TriMesh::HalfedgeHandle cur_heh = currentTriMesh()->halfedge_handle( addedFacesTri_[currentGroup_][0] );
      TriMesh::HalfedgeHandle end_heh = currentTriMesh()->prev_halfedge_handle(cur_heh);

      // find start heh
      while( currentTriMesh()->to_vertex_handle(cur_heh) != vertices[0] && cur_heh != end_heh )
        cur_heh = currentTriMesh()->next_halfedge_handle( cur_heh);

      for(unsigned int i=0; i<_face_texcoords.size(); ++i)
      {
        if ( _face_texcoords[i] < (int)texCoords_.size() ){

          PolyMesh::TexCoord2D tex = OpenMesh::vector_cast<TriMesh::TexCoord2D>( texCoords_[ _face_texcoords[i] ] );
          currentTriMesh()->set_texcoord2D(cur_heh, tex);

          cur_heh = currentTriMesh()->next_halfedge_handle(cur_heh);

        } else
          std::cerr << "Error: cannot set texture coordinates. undefined index." << std::endl;
      }
Dirk Wilden's avatar
Dirk Wilden committed
484
485
486
487
488
489
490
491
492
493
494

    }
    
    
  } else if ( isPolyMesh( currentObject() ) ){

    //handle poly meshes
    if ( !currentPolyMesh() ) return;
    
    std::vector< PolyMesh::VertexHandle > vertices;
    
Jan Möbius's avatar
Jan Möbius committed
495
    for (unsigned int i=0; i < _indices.size(); i++) {
Dirk Wilden's avatar
Dirk Wilden committed
496

497
      if ( vertexMapPoly_[currentGroup_].find( _indices[i] ) != vertexMapPoly_[currentGroup_].end() ){
Dirk Wilden's avatar
Dirk Wilden committed
498
    
499
        vertices.push_back( vertexMapPoly_[currentGroup_][ _indices[i] ] );
Dirk Wilden's avatar
Dirk Wilden committed
500

501
      } else {
502
        std::cerr << "Error: cannot add face poly mesh. undefined index(" <<  _indices[i] << ")" << std::endl;
503
        std::cerr << "Vertices in index map: " << vertexMapPoly_[currentGroup_].size() << std::endl;
Dirk Wilden's avatar
Dirk Wilden committed
504
505
506
507
        return;
      }
    }
    
508
509
510
511
512
    OpenMesh::FaceHandle fh = currentPolyMesh()->add_face( vertices );

    if(!fh.is_valid()) {
    	// Store non-manifold face
    	invalidFaces_.push_back(vertices);
513
    } else {
Dirk Wilden's avatar
Dirk Wilden committed
514
    
515
      addedFacePoly_ = fh;
516

517
518
519
      //perhaps request texCoords for the mesh
      if ( !currentPolyMesh()->has_halfedge_texcoords2D() )
        currentPolyMesh()->request_halfedge_texcoords2D();
Dirk Wilden's avatar
Dirk Wilden committed
520

521
      //now add texCoords
Dirk Wilden's avatar
Dirk Wilden committed
522

523
524
525
526
      if ( addedFacePoly_.is_valid() ) {
        // get first halfedge handle
        PolyMesh::HalfedgeHandle cur_heh = currentPolyMesh()->halfedge_handle( addedFacePoly_ );
        PolyMesh::HalfedgeHandle end_heh = currentPolyMesh()->prev_halfedge_handle(cur_heh);
Dirk Wilden's avatar
Dirk Wilden committed
527

528
529
530
        // find start heh
        while( currentPolyMesh()->to_vertex_handle(cur_heh) != vertices[0] && cur_heh != end_heh )
          cur_heh = currentPolyMesh()->next_halfedge_handle( cur_heh);
531

532
533
534
        for(unsigned int i=0; i<_face_texcoords.size(); ++i)
        {
          if ( _face_texcoords[i] < (int)texCoords_.size() ){
Dirk Wilden's avatar
Dirk Wilden committed
535

536
537
538
539
540
541
542
543
            PolyMesh::TexCoord2D tex = OpenMesh::vector_cast<PolyMesh::TexCoord2D>( texCoords_[ _face_texcoords[i] ] );
            currentPolyMesh()->set_texcoord2D(cur_heh, tex);

            cur_heh = currentPolyMesh()->next_halfedge_handle(cur_heh);

          }else
            std::cerr << "Error: cannot set texture coordinates. undefined index." << std::endl;
        }
544
      }
Dirk Wilden's avatar
Dirk Wilden committed
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
    }
  }
}

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

void OBJImporter::addMaterial(std::string _materialName){

  
  if ( isTriangleMesh( currentObject() ) ){
  
    //handle triangle meshes
    if ( !currentTriMesh() ) return;
    
  
    if ( materials_.find( _materialName ) != materials_.end() ){
      
      Material& mat = materials_[ _materialName ];

      //get textureIndex Property
      OpenMesh::FPropHandleT< int > indexProperty;

      if ( hasTexture( currentObject() ) ){

        bool textureAllowed = ! ( objectOptions_[ currentObject() ] & FORCE_NOTEXTURES );
        
        if ( textureAllowed ){
        
573
574
575
          // Add texture index property if it doesn't exist yet
          if (! currentTriMesh()->get_property_handle(indexProperty,TEXTUREINDEX) )
            currentTriMesh()->add_property(indexProperty,TEXTUREINDEX);
Dirk Wilden's avatar
Dirk Wilden committed
576
577
578
        }
      }

Jan Möbius's avatar
Jan Möbius committed
579
      for (unsigned int i=0; i < addedFacesTri_[currentGroup_].size(); i++){
Dirk Wilden's avatar
Dirk Wilden committed
580
581
582
583
584
      
        if ( mat.has_Kd() ) {
          bool colorAllowed = ! ( objectOptions_[ currentObject() ] & FORCE_NOCOLOR );
          
          if ( currentTriMesh()->has_face_colors() && colorAllowed ){
585
586
587
588
589
590
591
592
593
594
595

            TriMesh::Color color = OpenMesh::color_cast< OpenMesh::Vec4f >(mat.Kd() );

            // Get alpha if available
            if (mat.has_Tr() ) {
              color[3] = mat.Tr();
            } else {
              color[3] = 1.0;
            }

            currentTriMesh()->set_color(addedFacesTri_[currentGroup_][i], color  );
Dirk Wilden's avatar
Dirk Wilden committed
596
597
598
599
600
601
602
            objectOptions_[ currentObject() ] |= FACECOLOR;
          }
        }

        bool textureAllowed = ! ( objectOptions_[ currentObject() ] & FORCE_NOTEXTURES );

        // Set the texture index in the face index property
Mike Kremer's avatar
Mike Kremer committed
603
        if ( mat.has_Texture() ) {
Dirk Wilden's avatar
Dirk Wilden committed
604
605

          if ( hasTexture( currentObject() ) && textureAllowed )
606
            currentTriMesh()->property(indexProperty, addedFacesTri_[currentGroup_][i]) = mat.map_Kd_index();
Dirk Wilden's avatar
Dirk Wilden committed
607
608
609
610

        } else {
          // If we don't have the info, set it to no texture
          if ( hasTexture( currentObject() ) && textureAllowed )
611
            currentTriMesh()->property(indexProperty, addedFacesTri_[currentGroup_][i]) = 0;
Dirk Wilden's avatar
Dirk Wilden committed
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
        }
      }
    }
    
  } else if ( isPolyMesh( currentObject() ) ){
  
    //handle poly meshes
    if ( !currentPolyMesh() ) return;
    
    if ( materials_.find( _materialName ) != materials_.end() ){
      
      Material& mat = materials_[ _materialName ];
      
      //get textureIndex Property
      OpenMesh::FPropHandleT< int > indexProperty;

      if ( hasTexture( currentObject() ) ){

        bool textureAllowed = ! ( objectOptions_[ currentObject() ] & FORCE_NOTEXTURES );
        
        if ( textureAllowed ){

634
635
          if (! currentPolyMesh()->get_property_handle(indexProperty,TEXTUREINDEX) )
            currentPolyMesh()->add_property(indexProperty,TEXTUREINDEX);
Dirk Wilden's avatar
Dirk Wilden committed
636
637
638
639
640
641
        }
      }
      
      if ( mat.has_Kd() ) {
        bool colorAllowed = ! ( objectOptions_[ currentObject() ] & FORCE_NOCOLOR );
        
642
        if ( currentPolyMesh()->has_face_colors() && colorAllowed && addedFacePoly_.is_valid()  ){
643
644
645
646
647
648
649
650
651
652
653

          TriMesh::Color color = OpenMesh::color_cast< OpenMesh::Vec4f >(mat.Kd() );

          // Get alpha if available
          if (mat.has_Tr() ) {
            color[3] = mat.Tr();
          } else {
            color[3] = 1.0;
          }

          currentPolyMesh()->set_color(addedFacePoly_, color );
Dirk Wilden's avatar
Dirk Wilden committed
654
655
656
657
658
659
660
          objectOptions_[ currentObject() ] |= FACECOLOR;
        }
      }

      bool textureAllowed = ! ( objectOptions_[ currentObject() ] & FORCE_NOTEXTURES );

      // Set the texture index in the face index property
Mike Kremer's avatar
Mike Kremer committed
661
      if ( mat.has_Texture() ) {
Dirk Wilden's avatar
Dirk Wilden committed
662

663
        if ( hasTexture( currentObject() ) && textureAllowed && addedFacePoly_.is_valid())
Dirk Wilden's avatar
Dirk Wilden committed
664
665
666
667
          currentPolyMesh()->property(indexProperty, addedFacePoly_) = mat.map_Kd_index();
        
      } else {
        // If we don't have the info, set it to no texture
668
        if ( hasTexture( currentObject() ) && textureAllowed && addedFacePoly_.is_valid())
Dirk Wilden's avatar
Dirk Wilden committed
669
670
671
672
673
674
675
676
677
678
679
          currentPolyMesh()->property(indexProperty, addedFacePoly_) = 0;
      }
    }
  }
}

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

// force all meshes to be opened with specific type
void OBJImporter::forceMeshType( ObjectOptions _meshType ){
  
Jan Möbius's avatar
Jan Möbius committed
680
  for (unsigned int i=0; i < objectOptions_.size(); i++){
Dirk Wilden's avatar
Dirk Wilden committed
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
    
    bool isMesh = (objectOptions_[i] & TRIMESH) | (objectOptions_[i] & POLYMESH);
    bool correctType = objectOptions_[i] & _meshType;
    
    if ( isMesh && !correctType )
      objectOptions_[i] = _meshType;
  }
}

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

bool OBJImporter::isTriangleMesh(int _objectID){
  return objectOptions_[ _objectID ] & TRIMESH;
}

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

bool OBJImporter::isPolyMesh(int _objectID){
  return objectOptions_[ _objectID ] & POLYMESH;
}

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

Dirk Wilden's avatar
Dirk Wilden committed
704
705
706
707
708
709
710
711
712
713
714
715
bool OBJImporter::isCurve(int _objectID){
  return objectOptions_[ _objectID ] & CURVE;
}

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

bool OBJImporter::isSurface(int _objectID){
  return objectOptions_[ _objectID ] & SURFACE;
}

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

Dirk Wilden's avatar
Dirk Wilden committed
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
bool OBJImporter::hasNormals(int _objectID){
  
  return objectOptions_[ _objectID ] & NORMALS;
}

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

bool OBJImporter::hasTexture(int _objectID){
  
  return objectOptions_[ _objectID ] & TEXTURE;
}

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

bool OBJImporter::hasTextureCoords(int _objectID){
  return objectOptions_[ _objectID ] & TEXCOORDS;
}

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

Jan Möbius's avatar
Jan Möbius committed
736
unsigned int OBJImporter::n_vertices(){
Dirk Wilden's avatar
Dirk Wilden committed
737
738
739
740
741
  return vertices_.size();
}

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

Jan Möbius's avatar
Jan Möbius committed
742
unsigned int OBJImporter::n_normals(){
Dirk Wilden's avatar
Dirk Wilden committed
743
744
745
746
747
  return normals_.size();
}

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

Jan Möbius's avatar
Jan Möbius committed
748
unsigned int OBJImporter::n_texCoords(){
Dirk Wilden's avatar
Dirk Wilden committed
749
750
751
752
753
  return texCoords_.size();
}

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

Jan Möbius's avatar
Jan Möbius committed
754
unsigned int OBJImporter::objectCount(){
755
  return groupNames_.size();
Dirk Wilden's avatar
Dirk Wilden committed
756
757
758
759
}

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

760
761
BaseObject* OBJImporter::object(int _objectID) {

762
  if (objectCount() == 0 || objectCount() <= (unsigned int)_objectID)
763
    return NULL;
764
765
766
767
768
769
770

  if(triMeshes_[_objectID] != NULL)             return triMeshes_[_objectID];
  else if(polyMeshes_[_objectID] != NULL)       return polyMeshes_[_objectID];
  else if(bSplineCurves_[_objectID] != NULL)    return bSplineCurves_[_objectID];
  else if(bSplineSurfaces_[_objectID] != NULL)  return bSplineSurfaces_[_objectID];

  return NULL;
Dirk Wilden's avatar
Dirk Wilden committed
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
}

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

MaterialList& OBJImporter::materials(){
  return materials_;
}

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

QString OBJImporter::path(){
  return path_;
}

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

void OBJImporter::setPath(QString _path){
  path_ = _path;
}

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

793
794
795
796
797
798
799
void OBJImporter::setObjectOptions(ObjectOptions _options) {

    while((unsigned int)currentGroup_ >= objectOptions_.size()) {
        objectOptions_.push_back(POLYMESH);
    }

    objectOptions_[currentGroup_] = _options;
Dirk Wilden's avatar
Dirk Wilden committed
800
801
802
803
804
805
806
807
808
809
810
}

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

std::vector< OBJImporter::ObjectOptions >& OBJImporter::objectOptions(){
  return objectOptions_;
}

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

// check if object with given id has given option
Jan Möbius's avatar
Jan Möbius committed
811
bool OBJImporter::hasOption( unsigned int _id, ObjectOptions _option ){
Dirk Wilden's avatar
Dirk Wilden committed
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
  
  if (_id >= objectOptions_.size())
    return false;
  
  return objectOptions_[_id] & _option;
}

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

void OBJImporter::setObjectName(int _objectID, QString _name){

  BaseObject* obj = object( _objectID );
  
  if ( obj != 0 )
    obj->setName( _name );
}

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

831
832
833
834
835
836
837
838
839
int OBJImporter::addGroup(const QString& _groupName) {

    QString group = _groupName.trimmed();
    int id = groupId(group);
    if(id == -1) {
        groupNames_.push_back(group);
        vertexMapTri_.push_back(std::map<int,TriMesh::VertexHandle>());
        vertexMapPoly_.push_back(std::map<int,TriMesh::VertexHandle>());
        addedFacesTri_.push_back(std::vector< TriMesh::FaceHandle>());
840
841
842
843
844
845
846
847
848

        triMeshes_.push_back(NULL);
        polyMeshes_.push_back(NULL);
#ifdef ENABLE_BSPLINECURVE_SUPPORT
        bSplineCurves_.push_back(NULL);
#endif
#ifdef ENABLE_BSPLINESURFACE_SUPPORT
        bSplineSurfaces_.push_back(NULL);
#endif
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
        return groupNames_.size() - 1;
    }
    return id;
}

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

int OBJImporter::groupId(const QString& _groupName) const {

    for(unsigned int i = 0; i < groupNames_.size(); ++i) {
        if(groupNames_[i] == _groupName.trimmed()) return i;
    }
    return -1;
}

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

const QString OBJImporter::groupName(const int _grpId) const {

    if((unsigned int)_grpId < groupNames_.size()) {
        return groupNames_[_grpId];
    }
    return QString();
}

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

void OBJImporter::setGroupName(const int _grp, const QString& _name) {

    if((unsigned int)_grp >= groupNames_.size()) return;

    groupNames_[_grp] = _name;
Dirk Wilden's avatar
Dirk Wilden committed
881
882
883
884
}

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

885
886
887
888
889
890
891
892
893
894
void OBJImporter::setCurrentGroup(const int _current) {

    currentGroup_ = _current;
}

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

int OBJImporter::currentGroup() const {

    return currentGroup_;
Dirk Wilden's avatar
Dirk Wilden committed
895
896
897
898
}

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

899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
void OBJImporter::finish() {

	// Duplicate vertices of non-manifold faces
	// and add them as new isolated face
	if(invalidFaces_.empty()) return;

	if (isTriangleMesh(currentObject())) {

		// Handle triangle meshes
		if ( !currentTriMesh() ) return;

		for(std::vector<OMVHandles>::iterator it = invalidFaces_.begin();
				it != invalidFaces_.end(); ++it) {

			OMVHandles& vhandles = *it;

			// double vertices
			for (unsigned int j = 0; j < vhandles.size(); ++j)
			{
			  TriMesh::Point p = currentTriMesh()->point(vhandles[j]);
			  vhandles[j] = currentTriMesh()->add_vertex(p);
			  // DO STORE p, reference may not work since vertex array
			  // may be relocated after adding a new vertex !

			  // Mark vertices of failed face as non-manifold
			  if (currentTriMesh()->has_vertex_status()) {
				  currentTriMesh()->status(vhandles[j]).set_fixed_nonmanifold(true);
			  }
			}

			// add face
			OpenMesh::FaceHandle fh = currentTriMesh()->add_face(vhandles);

			// Mark failed face as non-manifold
			if (currentTriMesh()->has_face_status())
				currentTriMesh()->status(fh).set_fixed_nonmanifold(true);

			// Mark edges of failed face as non-two-manifold
			if (currentTriMesh()->has_edge_status()) {
				TriMesh::FaceEdgeIter fe_it = currentTriMesh()->fe_iter(fh);
				for(; fe_it; ++fe_it) {
					currentTriMesh()->status(fe_it).set_fixed_nonmanifold(true);
				}
			}
		}

	} else {

		// Handle Polymeshes
		if ( !currentPolyMesh() ) return;

		for(std::vector<OMVHandles>::iterator it = invalidFaces_.begin();
				it != invalidFaces_.end(); ++it) {

			OMVHandles& vhandles = *it;

			// double vertices
			for (unsigned int j = 0; j < vhandles.size(); ++j)
			{
			  PolyMesh::Point p = currentPolyMesh()->point(vhandles[j]);
			  vhandles[j] = currentPolyMesh()->add_vertex(p);
			  // DO STORE p, reference may not work since vertex array
			  // may be relocated after adding a new vertex !

			  // Mark vertices of failed face as non-manifold
			  if (currentPolyMesh()->has_vertex_status()) {
				  currentPolyMesh()->status(vhandles[j]).set_fixed_nonmanifold(true);
			  }
			}

			// add face
			OpenMesh::FaceHandle fh = currentPolyMesh()->add_face(vhandles);

			// Mark failed face as non-manifold
			if (currentPolyMesh()->has_face_status())
				currentPolyMesh()->status(fh).set_fixed_nonmanifold(true);

			// Mark edges of failed face as non-two-manifold
			if (currentPolyMesh()->has_edge_status()) {
				PolyMesh::FaceEdgeIter fe_it = currentPolyMesh()->fe_iter(fh);
				for(; fe_it; ++fe_it) {
					currentPolyMesh()->status(fe_it).set_fixed_nonmanifold(true);
				}
			}
		}
	}

	// Clear faces
	invalidFaces_.clear();
}

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

Dirk Wilden's avatar
Dirk Wilden committed
992
//used materials
Jan Möbius's avatar
Jan Möbius committed
993
const std::vector<std::string> OBJImporter::usedMaterials(unsigned int _objectID){
Dirk Wilden's avatar
Dirk Wilden committed
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008

  if (_objectID >= usedMaterials_.size())
    return std::vector<std::string>();
  else
    return usedMaterials_[ _objectID ];
}

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

void OBJImporter::useMaterial( std::string _materialName ){

  while( (int)usedMaterials_.size() - 1 < currentObject() )
    usedMaterials_.push_back( std::vector<std::string>() );
  
  //check that it is not added already
Jan Möbius's avatar
Jan Möbius committed
1009
  for (unsigned int i=0; i < usedMaterials_[ currentObject() ].size(); i++ )
Dirk Wilden's avatar
Dirk Wilden committed
1010
1011
1012
1013
1014
1015
1016
1017
    if ( usedMaterials_[ currentObject() ][i] == _materialName )
      return;
  
  usedMaterials_[ currentObject() ].push_back( _materialName );
  
  objectOptions_[ currentObject() ] |= TEXTURE;
}

1018
///used vertices
1019
void OBJImporter::useVertex(int _vertex_index){
1020

1021
    while(currentGroup_ >= (int)usedVertices_.size()) {
1022
        usedVertices_.push_back(std::map<int,VertexHandle>());
1023
1024
    }

1025
    usedVertices_[currentGroup_].insert(std::pair<int,VertexHandle>(_vertex_index,-1));
1026

1027
1028
1029
1030
//  while( (int)usedVertices_.size() - 1 < (int) objectOptions_.size() )
//    usedVertices_.push_back( std::set<VertexHandle>() );
//
//  usedVertices_[ objectOptions_.size() ].insert( _vertex );
1031
1032
}

Dirk Wilden's avatar
Dirk Wilden committed
1033
1034
//-----------------------------------------------------------------------------