MeshObjectT.cc 24.3 KB
Newer Older
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-2014 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/>.                                       *
*                                                                            *
33
34
35
\*===========================================================================*/

/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
36
37
38
39
40
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
41
\*===========================================================================*/
Jan Möbius's avatar
 
Jan Möbius committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59




  //=============================================================================
  //
  //  MyTypes
  //
  //=============================================================================

  #define MESHOBJECT_C


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

  #include <OpenFlipper/common/Types.hh>
  #include <ACG/Scenegraph/DrawModes.hh>
  #include <OpenFlipper/common/GlobalOptions.hh>
60
  #include <OpenFlipper/BasePlugin/PluginFunctions.hh>
Jan Möbius's avatar
 
Jan Möbius committed
61
62
63
64
65
66


  //== TYPEDEFS =================================================================

  //== CLASS DEFINITION =========================================================

67
68
  template < class MeshT  >
  MeshObject< MeshT >::MeshObject(const MeshObject& _object) :
Jan Möbius's avatar
 
Jan Möbius committed
69
70
71
72
73
74
75
76
      BaseObjectData(_object),
      statusNode_(0),
      areaNode_(0),
      handleNode_(0),
      featureNode_(0),
      meshNode_(0),
      textureNode_(0),
      shaderNode_(0),
77
      statusView_(0),
Jan Möbius's avatar
 
Jan Möbius committed
78
79
80
81
      triangle_bsp_(0)
  {
    init(_object.mesh_);

82
    
Jan Möbius's avatar
 
Jan Möbius committed
83
    setName( name() );
Jan Möbius's avatar
Jan Möbius committed
84

85
86
//     textures_     = _object.textures_;
//     textureNames_ = _object.textureNames_;
Jan Möbius's avatar
 
Jan Möbius committed
87
88
89
90
91
92
93
94
95
  }

  /** Constructor for Mesh Objects. This object class gets a Separator Node giving
  *  the root node to which it should be connected. The mesh is generated internally
  *  and all nodes for visualization will be added below the scenegraph node.\n
  *  You dont need to create an object of this type manually. Use
  *  PluginFunctions::addTriMesh or PluginFunctions::addPolyMesh instead. The
  *  objectDataType has to match the one of MeshT ( see Types.hh::DataType )
  */
96
  template < class MeshT  >
97
98
  MeshObject< MeshT >::MeshObject( DataType _typeId ) :
    BaseObjectData(),
Jan Möbius's avatar
 
Jan Möbius committed
99
100
101
102
103
104
105
106
    mesh_(0),
    statusNode_(0),
    areaNode_(0),
    handleNode_(0),
    featureNode_(0),
    meshNode_(0),
    textureNode_(0),
    shaderNode_(0),
107
    statusView_(0),
Jan Möbius's avatar
 
Jan Möbius committed
108
109
    triangle_bsp_(0)
  {
110
    setDataType(_typeId);
Jan Möbius's avatar
 
Jan Möbius committed
111
112
113
114
115
116
    init();
  }

  /** Destructor for Mesh Objects. The destructor deletes the mesh and all
  *  Scenegraph nodes associated with the mesh or the object.
  */
117
118
  template < class MeshT  >
  MeshObject< MeshT >::~MeshObject()
Jan Möbius's avatar
 
Jan Möbius committed
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
  {
    // Delete the data attached to this object ( this will remove all perObject data)
    // Not the best way to do it but it will work.
    // This is only necessary if people use references to the mesh below and
    // they do something with the mesh in the destructor of their
    // perObjectData.
    deleteData();

    // Delete the Mesh only, if this object contains a mesh
    if ( mesh_ != NULL)  {
      delete mesh_;
      mesh_ = NULL;
    } else {
      std::cerr << "Destructor error : Mesh already deleted" << std::endl;
    }

    if ( triangle_bsp_ != 0 )
      delete triangle_bsp_;
    triangle_bsp_ = 0;

    // No need to delete the scenegraph Nodes as this will be managed by baseplugin
    areaNode_    = 0;
    handleNode_  = 0;
    featureNode_ = 0;
    meshNode_    = 0;
    textureNode_ = 0;
    shaderNode_  = 0;
146
    statusView_  = 0;
Jan Möbius's avatar
 
Jan Möbius committed
147
148
149
150
151
  }

  /** Cleanup Function for Mesh Objects. Deletes the contents of the whole object and
  * calls MeshObject::init afterwards.
  */
152
153
  template < class MeshT  >
  void MeshObject< MeshT >::cleanup() {
Jan Möbius's avatar
 
Jan Möbius committed
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
    // Delete the Mesh only, if this object contains a mesh
    if ( mesh_ != NULL)  {
      delete mesh_;
      mesh_ = NULL;
    } else {
      std::cerr << "Cleanup error : Triangle Mesh already deleted" << std::endl;
    }

    if ( triangle_bsp_ != 0 )
      delete triangle_bsp_;
    triangle_bsp_ = 0;

    BaseObjectData::cleanup();

    statusNode_  = 0;
    areaNode_    = 0;
    handleNode_  = 0;
    featureNode_ = 0;
    textureNode_ = 0;
    shaderNode_  = 0;
    meshNode_    = 0;
175
    statusView_  = 0;
Jan Möbius's avatar
 
Jan Möbius committed
176
177
178
179
180
181
182
183

    init();

  }

  /** This function initalizes the mesh object. It creates the scenegraph nodes,
  *  the mesh and requests all required properties for the mesh.
  */
184
185
  template < class MeshT  >
  void MeshObject< MeshT >::init(MeshT* _mesh) {
Jan Möbius's avatar
 
Jan Möbius committed
186
187
188
189
190
191

    if ( _mesh == 0 )
      mesh_ = new MeshT();
    else
      mesh_ = new MeshT(*_mesh);

192
    // Prepare mesh and request required properties
Jan Möbius's avatar
 
Jan Möbius committed
193
194
195
196
197
198
199
200
    mesh_->request_vertex_normals();
    mesh_->request_face_normals();
    mesh_->request_vertex_status();
    mesh_->request_halfedge_status();
    mesh_->request_face_status();
    mesh_->request_edge_status();
    mesh_->request_vertex_colors();
    mesh_->request_face_colors();
201
202
203
204
205

    // Only initialize scenegraph nodes when we initialized a gui!!
    if ( OpenFlipper::Options::nogui() )
      return;

206
    // This should never happen!
207
    if ( manipulatorNode() == NULL)
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
      std::cerr << "Error when creating Mesh Object! manipulatorNode is NULL!" << std::endl;


    textureNode_ = new ACG::SceneGraph::EnvMapNode(materialNode(),"NEW TextureNode for ", true, GL_LINEAR_MIPMAP_LINEAR );

    shaderNode_  = new ACG::SceneGraph::ShaderNode(textureNode() , "NEW ShaderNode for ");

    meshNode_    = new ACG::SceneGraph::MeshNodeT<MeshT>(*mesh_, shaderNode_, "NEW MeshNode");

    QString shaderDir = OpenFlipper::Options::shaderDirStr() + OpenFlipper::Options::dirSeparator();

    std::string shaderDirectory = std::string( shaderDir.toUtf8() );
    shaderNode_->setShaderDir( shaderDirectory );


    if ( QFile( shaderDir + "Phong/Vertex.glsl").exists() && QFile( shaderDir + "Phong/Fragment.glsl" ).exists() )
      shaderNode_->setShader(ACG::SceneGraph::DrawModes::SOLID_PHONG_SHADED,"Phong/Vertex.glsl" , "Phong/Fragment.glsl" );
    else
      std::cerr << "Shader Files for Phong not found!" << std::endl;

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

229
    // Node showing selection
230
    statusNode_ = new ACG::SceneGraph::SelectionNodeT<MeshT>(*mesh_, 0, "NEW StatusNode for mesh " );
Jan Möbius's avatar
 
Jan Möbius committed
231
    statusNode_->set_point_size(4.0);
232
233
    statusNode_->set_color(ACG::Vec4f(1.0f,0.0f,0.0f,1.0f));
    statusNode_->set_base_color(ACG::Vec4f(1.0f,0.0f,0.0f,1.0f));
234
235
236
    // Status nodes are handled specially by their StatusViewNodeT parent which
    // is why they get a NONE draw mode.
    statusNode_->drawMode(ACG::SceneGraph::DrawModes::NONE);
Jan Möbius's avatar
 
Jan Möbius committed
237

238
    // Node showing modeling region
239
    areaNode_ = new ACG::SceneGraph::StatusNodeT<MeshT, AreaNodeMod<MeshT> >(*mesh_, 0, "NEW AreaNode for mesh ");
Jan Möbius's avatar
 
Jan Möbius committed
240
241
242
    areaNode_->set_round_points(true);
    areaNode_->enable_alpha_test(0.5);
    areaNode_->set_point_size(7.0);
243
    areaNode_->set_color(ACG::Vec4f(0.4f, 0.4f, 1.0f, 1.0f));
244
245
246
    // Status nodes are handled specially by their StatusViewNodeT parent which
    // is why they get a NONE draw mode.
    areaNode_->drawMode(ACG::SceneGraph::DrawModes::NONE);
Jan Möbius's avatar
 
Jan Möbius committed
247

248
    // Node showing handle region
249
    handleNode_ = new ACG::SceneGraph::StatusNodeT<MeshT, HandleNodeMod<MeshT> >(*mesh_, 0, "NEW HandleNode for mesh ");
Jan Möbius's avatar
 
Jan Möbius committed
250
251
252
253
    handleNode_->set_round_points(true);
    handleNode_->enable_alpha_test(0.5);
    handleNode_->set_line_width(2.0);
    handleNode_->set_point_size(7.0);
254
    handleNode_->set_color(ACG::Vec4f(0.2f, 1.0f, 0.2f, 1.0f));
255
256
257
    // Status nodes are handled specially by their StatusViewNodeT parent which
    // is why they get a NONE draw mode.
    handleNode_->drawMode(ACG::SceneGraph::DrawModes::NONE);
Jan Möbius's avatar
 
Jan Möbius committed
258

259
    // Node showing feature selection
260
    featureNode_ = new ACG::SceneGraph::StatusNodeT<MeshT, FeatureNodeMod<MeshT> >(*mesh_, 0, "NEW FeatureNode for mesh ");
Jan Möbius's avatar
 
Jan Möbius committed
261
262
263
264
    featureNode_->set_round_points(true);
    featureNode_->enable_alpha_test(0.5);
    featureNode_->set_line_width(2.0);
    featureNode_->set_point_size(7.0);
265
266
    featureNode_->set_color(ACG::Vec4f(1.0f, 0.2f, 1.0f, 1.0f));
    featureNode_->set_base_color(ACG::Vec4f(1.0f, 0.2f, 1.0f, 1.0f));
267
268
269
    // Status nodes are handled specially by their StatusViewNodeT parent which
    // is why they get a NONE draw mode.
    featureNode_->drawMode(ACG::SceneGraph::DrawModes::NONE);
Jan Möbius's avatar
 
Jan Möbius committed
270

271
272
273
274
275
276
277
    // Link the status nodes to the draw mesh of the mesh below them to reuse its buffers
    if (meshNode_) {
      statusNode_->setDrawMesh(meshNode_->getDrawMesh());
      featureNode_->setDrawMesh(meshNode_->getDrawMesh());
      areaNode_->setDrawMesh(meshNode_->getDrawMesh());
      handleNode_->setDrawMesh(meshNode_->getDrawMesh());
    }
Jan Möbius's avatar
 
Jan Möbius committed
278

279
280
281
282
283
284
    // Node rendering selections in correct order
    statusView_ = new ACG::SceneGraph::StatusViewNodeT<MeshT>(manipulatorNode(), "NEW StatusViewNode for mesh ",
      statusNode_,
      areaNode_,
      handleNode_,
      featureNode_);
Jan Möbius's avatar
 
Jan Möbius committed
285

286
287
288
289
290
    // make StatusViewNode parent of status nodes
    statusNode_->set_parent(statusView_);
    areaNode_->set_parent(statusView_);
    handleNode_->set_parent(statusView_);
    featureNode_->set_parent(statusView_);
Jan Möbius's avatar
 
Jan Möbius committed
291
292
293
294
295
296
297
298
299
300
301
302

    // Update all nodes
    update();
  }

  // ===============================================================================
  // Name/Path Handling
  // ===============================================================================

  /** Set the name of an object. All Scenegraph nodes are renamed too. It also calls
  * BaseObjectData::setName.
  */
303
304
  template < class MeshT  >
  void MeshObject< MeshT >::setName( QString _name ) {
Jan Möbius's avatar
 
Jan Möbius committed
305
306
    BaseObjectData::setName(_name);

307
308
309
310
    // No update when gui is not active
    if ( OpenFlipper::Options::nogui() )
      return;

Jan Möbius's avatar
 
Jan Möbius committed
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
    std::string nodename = std::string("StatusNode for mesh " + _name.toUtf8() );
    statusNode_->name( nodename );

    nodename = std::string("AreaNode for mesh   " + _name.toUtf8() );
    areaNode_->name( nodename );

    nodename = std::string("HandleNode for mesh " + _name.toUtf8() );
    handleNode_->name( nodename );

    nodename = std::string("FeatureNode for mesh " + _name.toUtf8() );
    featureNode_->name( nodename );

    nodename = std::string("TextureNode for mesh "+ _name.toUtf8() );
    textureNode_->name( nodename );

    nodename = std::string("ShaderNode for mesh "+ _name.toUtf8() );
    shaderNode_->name( nodename );

    nodename = std::string("MeshNode for mesh "     + _name.toUtf8() );
    meshNode_->name( nodename );
331
332
333

    nodename = std::string("StatusViewNode for mesh "     + _name.toUtf8() );
    statusView_->name( nodename );
Jan Möbius's avatar
 
Jan Möbius committed
334
335
336
337
338
339
340
341
342
  }

  // ===============================================================================
  // Content
  // ===============================================================================

  /** Get a pointer to the objects mesh.
  * @return Pointer to the mesh
  */
343
344
  template < class MeshT  >
  MeshT* MeshObject< MeshT >::mesh() {
Jan Möbius's avatar
 
Jan Möbius committed
345
346
347
348
349
350
351
    return mesh_;
  }

  /** Updates the visualization of the object. Calls MeshObject::updateGeometry,
  *  MeshObject::updateTopology, MeshObject::updateSelection and
  *  MeshObject::updateModelingRegions.
  */
352
  template < class MeshT  >
Dirk Wilden's avatar
Dirk Wilden committed
353
  void MeshObject< MeshT >::update(UpdateType _type) {
354
355
356
357
358
359
360

    // No update necessary if no gui
    if ( OpenFlipper::Options::nogui() )
      return;

    PluginFunctions::setMainGLContext();

361
    if ( _type.contains(UPDATE_ALL) || _type.contains(UPDATE_TOPOLOGY) ){
Dirk Wilden's avatar
Dirk Wilden committed
362
      updateGeometry();
363
      updateColor();
Dirk Wilden's avatar
Dirk Wilden committed
364
365
366
      updateTopology();
      updateSelection();
      updateFeatures();
367
      updateModelingRegions();
368
      updateTexture();
David Bommes's avatar
David Bommes committed
369
370
371
372
    }
    else 
    {
      if ( _type.contains(UPDATE_GEOMETRY) ) {
373
        updateGeometry();
David Bommes's avatar
David Bommes committed
374
375
      } 
      if ( _type.contains(UPDATE_SELECTION) ) {
376
377
378
        updateSelection();
        updateFeatures();
        updateModelingRegions();
David Bommes's avatar
David Bommes committed
379
380
      }
      if ( _type.contains(UPDATE_COLOR) ) {
381
        updateColor();
David Bommes's avatar
David Bommes committed
382
      }
383
384
385
      if ( _type.contains(UPDATE_TEXTURE)) {
        updateTexture();
      }
Jan Möbius's avatar
Jan Möbius committed
386
    }
Jan Möbius's avatar
 
Jan Möbius committed
387
388
389
  }

  /** Updates the selection scenegraph nodes */
390
391
  template < class MeshT  >
  void MeshObject< MeshT >::updateSelection() {
392

393
    if ( statusNode_ ){
394
      statusNode_->updateSelection();
395
    }
Jan Möbius's avatar
 
Jan Möbius committed
396
397
398
  }

  /** Updates the geometry information in the mesh scenegraph node */
399
400
  template < class MeshT  >
  void MeshObject< MeshT >::updateGeometry() {
401

402
    if ( meshNode_ ) {
403
      meshNode_->update_geometry();
404
405
406
407

      // Also update the selection nodes here.
      // These nodes store their positions based on the mesh.
      // So they would be at the wrong position afterwards
408
409
410
411
      statusNode_->updateGeometry();
      featureNode_->updateGeometry();
      areaNode_->updateGeometry();
      handleNode_->updateGeometry();
412

413
    }
414

415
    invalidateTriangleBsp();
Jan Möbius's avatar
 
Jan Möbius committed
416
417
  }

418
419
420
421
422
423
424
  /** Updates the color information in the mesh scenegraph node */
  template < class MeshT  >
  void MeshObject< MeshT >::updateColor() {
    if ( meshNode_ )
      meshNode_->update_color();
  }

Jan Möbius's avatar
 
Jan Möbius committed
425
  /** Updates the topology information in the mesh scenegraph node */
426
427
  template < class MeshT  >
  void MeshObject< MeshT >::updateTopology() {
428
    if ( meshNode_ ) {
429
      meshNode_->update_topology();
430
431
432
433
434
      statusNode_->updateTopology();
      featureNode_->updateTopology();
      areaNode_->updateTopology();
      handleNode_->updateTopology();
    }
435
    invalidateTriangleBsp();
Jan Möbius's avatar
 
Jan Möbius committed
436
437
438
  }

  /** Updates the modeling regions scenegraph nodes */
439
440
  template < class MeshT  >
  void MeshObject< MeshT >::updateModelingRegions() {
441
    if ( areaNode_ && handleNode_ ) {
442
443
      areaNode_->updateSelection();
      handleNode_->updateSelection();
444
    }
Jan Möbius's avatar
 
Jan Möbius committed
445
446
447
  }

  /** Updates the modeling regions scenegraph nodes */
448
449
  template < class MeshT  >
  void MeshObject< MeshT >::updateFeatures() {
450
    if ( featureNode_ )
451
      featureNode_->updateSelection();
452
453
454
455
456
457
  }

  /** Updates the modeling regions scenegraph nodes */
  template < class MeshT  >
  void MeshObject< MeshT >::updateTexture() {
      meshNode_->update_textures();
Jan Möbius's avatar
 
Jan Möbius committed
458
459
460
  }


461
462
463
  template < class MeshT  >
  BaseObject* MeshObject< MeshT >::copy() {
    MeshObject< MeshT >* object = new MeshObject< MeshT >(*this );
Jan Möbius's avatar
 
Jan Möbius committed
464
465
466
467
468
469
470
471
472
    return dynamic_cast< BaseObject* >(object);
  }

  // ===============================================================================
  // Visualization
  // ===============================================================================
  /** Returns a pointer to the texture node
  * @return Pointer to the texture node
  */
473
474
  template < class MeshT  >
  ACG::SceneGraph::EnvMapNode* MeshObject< MeshT >::textureNode() {
Jan Möbius's avatar
 
Jan Möbius committed
475
476
477
478
479
480
    return textureNode_;
  }

  /** Returns a pointer to the shader node
  * @return Pointer to the shader node
  */
481
482
  template < class MeshT  >
  ACG::SceneGraph::ShaderNode* MeshObject< MeshT >::shaderNode() {
Jan Möbius's avatar
 
Jan Möbius committed
483
484
485
486
487
    return shaderNode_;
  }

  /** Shows or hides the selections on the object
   */
488
489
  template < class MeshT  >
  void  MeshObject< MeshT >::hideSelection( bool _hide ) {
Jan Möbius's avatar
 
Jan Möbius committed
490
491
492
493
494
495
496
497
498

    if ( _hide ) {
      statusNode_->set_status( ACG::SceneGraph::BaseNode::HideNode );
    } else {
      statusNode_->set_status( ACG::SceneGraph::BaseNode::Active );
    }

  }

499
500
  template < class MeshT  >
  void  MeshObject< MeshT >::hideFeatures( bool _hide ) {
Jan Möbius's avatar
 
Jan Möbius committed
501
502
503
504
505
506
507
    if ( _hide ) {
      featureNode_->set_status( ACG::SceneGraph::BaseNode::HideNode );
    } else {
      featureNode_->set_status( ACG::SceneGraph::BaseNode::Active );
    }
  }

508
509
  template < class MeshT  >
  bool  MeshObject< MeshT >::featuresVisible( ) {
Jan Möbius's avatar
 
Jan Möbius committed
510
511
512
513
514
    return ( featureNode_->status() == ACG::SceneGraph::BaseNode::Active );
  }

  /** Shows or hides the areas on the object
   */
515
  template < class MeshT  >
516
517
518
  void  MeshObject< MeshT >::hideArea( StatusBits _bit, bool _hide ) {
    ACG::SceneGraph::BaseNode::StatusMode status;

Jan Möbius's avatar
 
Jan Möbius committed
519
    if ( _hide ) {
520
521
522
523
524
525
      status = ACG::SceneGraph::BaseNode::HideNode;
    }else
      status = ACG::SceneGraph::BaseNode::Active;

    if ( _bit & AREA ) {
      areaNode_->set_status( status );
Jan Möbius's avatar
 
Jan Möbius committed
526
    }
527
528
529
530

    if ( _bit & HANDLEAREA )
      handleNode_->set_status( status );

Jan Möbius's avatar
 
Jan Möbius committed
531
532
  }

533
534
  template < class MeshT  >
  bool MeshObject< MeshT >::selectionVisible() {
Jan Möbius's avatar
 
Jan Möbius committed
535
536
537
    return ( statusNode_->status() == ACG::SceneGraph::BaseNode::Active );
  }

538
  template < class MeshT  >
539
540
541
542
543
544
545
546
547
548
  bool MeshObject< MeshT >::areaVisible( StatusBits _bit ) {
    bool status = true;

    if ( _bit & AREA )
      status &= ( areaNode_->status()   == ACG::SceneGraph::BaseNode::Active );

    if ( _bit & HANDLEAREA )
      status &= ( handleNode_->status() == ACG::SceneGraph::BaseNode::Active );

    return status;
Jan Möbius's avatar
 
Jan Möbius committed
549
550
551
552
553
  }

  /** Returns a pointer to the mesh node
  * @return Pointer to the mesh node
  */
554
  template < class MeshT  >
555
  ACG::SceneGraph::MeshNodeT<MeshT>* MeshObject< MeshT >::meshNode() {
Jan Möbius's avatar
 
Jan Möbius committed
556
557
558
559
560
    return meshNode_;
  }

  /** Get the Bounding box size of this object
   */
561
562
  template < class MeshT  >
  void MeshObject< MeshT >::boundingBox( ACG::Vec3d& _bbMin , ACG::Vec3d& _bbMax ) {
563
    if ( meshNode_ ) {
564
      _bbMin = ACG::Vec3d(FLT_MAX, FLT_MAX, FLT_MAX);
565
      _bbMax = ACG::Vec3d(-FLT_MAX, -FLT_MAX, -FLT_MAX);
566
567
568
569
      meshNode_->boundingBox(_bbMin,_bbMax);
    } else {
      std::cerr << "Error: Bounding box computation via Scenegraph not available without gui" << std::endl;
    }
Jan Möbius's avatar
 
Jan Möbius committed
570
571
572
573
574
575
576
577
578
579
580
  }

  // ===============================================================================
  // Load/Save
  // ===============================================================================

  /** Load the mesh data from a file. The loader uses the OpenMesh object loaders and
  * therefore supports all mesh types supported by OpenMesh.
  *
  * @param _filename Full path of the file to load.
  */
581
582
  template < class MeshT  >
  bool MeshObject< MeshT >::loadMesh(QString _filename) {
Jan Möbius's avatar
 
Jan Möbius committed
583
584
585
586
587
588
589
590
591
592
593
594

    setFromFileName(_filename);

    // call the local function to update names
    setName( name() );

    std::string filename = std::string( _filename.toUtf8() );

    // load file
    bool ok = OpenMesh::IO::read_mesh( (*mesh()) , filename );
    if (!ok)
    {
595
      if ( dataType() == typeid("TriangleMesh") )
Jan Möbius's avatar
 
Jan Möbius committed
596
        std::cerr << "Main Application : Read error for Triangle Mesh at "<< filename << std::endl;
597
      if ( dataType() == typeid("PolyMesh") )
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
        std::cerr << "Main Application : Read error for Poly Mesh\n";
      return false;
    }

    mesh()->update_normals();

    update();

    show();

    return true;
  }

  // ===============================================================================
  // Object information
  // ===============================================================================

  /** Returns a string containing all information about the current object. This also
  * includes the information provided by BaseObjectData::getObjectinfo
  *
  * @return String containing the object information
  */
620
621
  template < class MeshT  >
  QString MeshObject< MeshT >::getObjectinfo() {
Jan Möbius's avatar
 
Jan Möbius committed
622
623
624
625
626
    QString output;

    output += "========================================================================\n";
    output += BaseObjectData::getObjectinfo();

627
    if ( dataType( typeId("TriangleMesh") ) )
Jan Möbius's avatar
 
Jan Möbius committed
628
629
      output += "Object Contains Triangle Mesh : ";

630
    if ( dataType( typeId("PolyMesh") ) )
Jan Möbius's avatar
 
Jan Möbius committed
631
632
633
634
635
636
637
638
639
640
641
642
643
644
      output += "Object Contains Poly Mesh : ";

    output += QString::number( mesh()->n_vertices() ) + " vertices, ";
    output += QString::number( mesh()->n_edges() ) += " edges ";
    output += QString::number( mesh()->n_faces() ) += " faces.\n";

    output += "========================================================================\n";
    return output;
  }

  // ===============================================================================
  // Picking
  // ===============================================================================

Jan Möbius's avatar
   
Jan Möbius committed
645
  /** Given an node index from PluginFunctions::scenegraphPick this function can be used to
Jan Möbius's avatar
 
Jan Möbius committed
646
647
648
649
650
  * check if the meshNode of the object has been picked.
  *
  * @param _node_idx Index of the picked mesh node
  * @return bool if the meshNode of this object is the picking target.
  */
651
652
  template < class MeshT  >
  bool MeshObject< MeshT >::picked( uint _node_idx ) {
Jan Möbius's avatar
 
Jan Möbius committed
653
654
655
656
    return ( _node_idx == meshNode_->id() );
  }


657
658
  template < class MeshT  >
  void MeshObject< MeshT >::enablePicking( bool _enable ) {
659
660
661
    if ( OpenFlipper::Options::nogui())
      return;

Jan Möbius's avatar
 
Jan Möbius committed
662
663
664
665
666
667
668
669
    meshNode_->enablePicking( _enable );
    areaNode_->enablePicking( _enable );
    handleNode_->enablePicking( _enable );
    featureNode_->enablePicking( _enable );
    textureNode_->enablePicking( _enable );
    shaderNode_->enablePicking( _enable );
  }

670
671
  template < class MeshT  >
  bool MeshObject< MeshT >::pickingEnabled() {
Jan Möbius's avatar
 
Jan Möbius committed
672
673
674
675
676
677
678
    return meshNode_->pickingEnabled();
  }

  // ===============================================================================
  // Octree
  // ===============================================================================

679
680
681
  template < class MeshT  >
  typename MeshObject< MeshT >::OMTriangleBSP*
  MeshObject< MeshT >::requestTriangleBsp() {
Jan Möbius's avatar
 
Jan Möbius committed
682

683
    if ( ! dataType( typeId("TriangleMesh") ) ) {
Jan Möbius's avatar
 
Jan Möbius committed
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
      std::cerr << "Bsps are only supported for triangle meshes." << std::endl;
      return 0;
    }

    // Create the tree if needed.
    if ( triangle_bsp_ == 0 )
    {
      // create Triangle BSP
      triangle_bsp_ = new OMTriangleBSP( *mesh() );

      // build Triangle BSP
      triangle_bsp_->reserve(mesh()->n_faces());

      typename MeshT::FIter f_it  = mesh()->faces_begin();
      typename MeshT::FIter f_end = mesh()->faces_end();

      for (; f_it!=f_end; ++f_it)
Jan Möbius's avatar
OM 3    
Jan Möbius committed
701
	     triangle_bsp_->push_back(*f_it);
Jan Möbius's avatar
 
Jan Möbius committed
702

Jan Möbius's avatar
Jan Möbius committed
703
      triangle_bsp_->build(10, 100); //max vertices per leaf 10, max depth 100
Jan Möbius's avatar
 
Jan Möbius committed
704
705
706
707
708
709
    }

    // return pointer to triangle bsp
    return triangle_bsp_;
  }

710
711
712
  template < class MeshT  >
  typename MeshObject< MeshT >::OMTriangleBSP*
  MeshObject< MeshT >::resetTriangleBsp() {
Jan Möbius's avatar
 
Jan Möbius committed
713
714
715
716
717
718
719
720
721
     if ( triangle_bsp_ != 0 )
     {
       delete triangle_bsp_;
       triangle_bsp_ = 0;
     }

     return requestTriangleBsp();
  }

722
  template < class MeshT  >
723
  void 
724
  MeshObject< MeshT >::invalidateTriangleBsp() {
725
726
727
728
729
730
731
     if ( triangle_bsp_ != 0 )
     {
       delete triangle_bsp_;
       triangle_bsp_ = 0;
     }
  }

Jan Möbius's avatar
 
Jan Möbius committed
732
733
734

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

Dirk Wilden's avatar
Dirk Wilden committed
735
736
737
738
739
740
  template < class MeshT  >
  BaseNode* 
  MeshObject< MeshT >::primaryNode(){
    return boundingBoxNode();
  } 

Jan Möbius's avatar
Jan Möbius committed
741
742
  //=============================================================================