MeshObjectT.cc 21.2 KB
Newer Older
Jan Möbius's avatar
 
Jan Möbius committed
1
2
3
4
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//=============================================================================
//
//                               OpenFlipper
//        Copyright (C) 2008 by Computer Graphics Group, RWTH Aachen
//                           www.openflipper.org
//
//-----------------------------------------------------------------------------
//
//                                License
//
//  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.
//
//  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 Lesser General Public License
//  along with OpenFlipper.  If not, see <http://www.gnu.org/licenses/>.
//
//-----------------------------------------------------------------------------
//
//   $Revision$
//   $Author$
//   $Date$
//
//=============================================================================




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

  #define MESHOBJECT_C


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

  #include <OpenFlipper/common/Types.hh>
  #include <ACG/Scenegraph/DrawModes.hh>
  #include <OpenFlipper/common/GlobalOptions.hh>
49
  #include <OpenFlipper/BasePlugin/PluginFunctions.hh>
Jan Möbius's avatar
 
Jan Möbius committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70


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

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

  template < class MeshT , DataType objectDataType >
  MeshObject< MeshT , objectDataType >::MeshObject(const MeshObject& _object) :
      BaseObjectData(_object),
      statusNode_(0),
      areaNode_(0),
      handleNode_(0),
      featureNode_(0),
      meshNode_(0),
      textureNode_(0),
      shaderNode_(0),
      triangle_bsp_(0)
  {
    init(_object.mesh_);

    setName( name() );
Jan Möbius's avatar
Jan Möbius committed
71

72
73
//     textures_     = _object.textures_;
//     textureNames_ = _object.textureNames_;
Jan Möbius's avatar
 
Jan Möbius committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
122
123
124
125
126
127
128
129
130
131
132
133
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
  }

  /** 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 )
  */
  template < class MeshT , DataType objectDataType >
  MeshObject< MeshT , objectDataType >::MeshObject( SeparatorNode* _rootNode ) :
    BaseObjectData(_rootNode ),
    mesh_(0),
    statusNode_(0),
    areaNode_(0),
    handleNode_(0),
    featureNode_(0),
    meshNode_(0),
    textureNode_(0),
    shaderNode_(0),
    triangle_bsp_(0)
  {
    setDataType(objectDataType);
    init();
  }

  /** Destructor for Mesh Objects. The destructor deletes the mesh and all
  *  Scenegraph nodes associated with the mesh or the object.
  */
  template < class MeshT , DataType objectDataType >
  MeshObject< MeshT , objectDataType >::~MeshObject()
  {
    // 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;
  }

  /** Cleanup Function for Mesh Objects. Deletes the contents of the whole object and
  * calls MeshObject::init afterwards.
  */
  template < class MeshT , DataType objectDataType >
  void MeshObject< MeshT , objectDataType >::cleanup() {
    // 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;

    setDataType(objectDataType);

    init();

  }

  /** This function initalizes the mesh object. It creates the scenegraph nodes,
  *  the mesh and requests all required properties for the mesh.
  */
  template < class MeshT , DataType objectDataType >
  void MeshObject< MeshT , objectDataType >::init(MeshT* _mesh) {

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

    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();
    mesh_->request_vertex_texcoords2D();
    mesh_->request_halfedge_texcoords2D();
188
    mesh_->request_face_texture_index();
Jan Möbius's avatar
 
Jan Möbius committed
189

190
191
192
193
194
195

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


Jan Möbius's avatar
Jan Möbius committed
196
 	 if ( manipulatorNode() == NULL)
Jan Möbius's avatar
 
Jan Möbius committed
197
198
		std::cerr << "Error when creating Mesh Object! manipulatorNode is NULL!" << std::endl;

199
    // Node showing selection
Jan Möbius's avatar
 
Jan Möbius committed
200
201
202
203
    statusNode_ = new ACG::SceneGraph::SelectionNodeT<MeshT>(*mesh_,manipulatorNode(),"NEW StatusNode for mesh " );
    statusNode_->set_point_size(4.0);
    statusNode_->set_color(ACG::Vec4f(1.0,0.0,0.0,1.0));

204
    // Node showing modeling region
Jan Möbius's avatar
 
Jan Möbius committed
205
206
207
208
209
210
    areaNode_ = new ACG::SceneGraph::StatusNodeT<MeshT, AreaNodeMod<MeshT> >(*mesh_, manipulatorNode(), "NEW AreaNode for mesh ");
    areaNode_->set_round_points(true);
    areaNode_->enable_alpha_test(0.5);
    areaNode_->set_point_size(7.0);
    areaNode_->set_color(ACG::Vec4f(0.4, 0.4, 1.0, 1.0));

211
    // Node showing handle region
Jan Möbius's avatar
 
Jan Möbius committed
212
213
214
215
216
217
218
    handleNode_ = new ACG::SceneGraph::StatusNodeT<MeshT, HandleNodeMod<MeshT> >(*mesh_, manipulatorNode(), "NEW HandleNode for mesh ");
    handleNode_->set_round_points(true);
    handleNode_->enable_alpha_test(0.5);
    handleNode_->set_line_width(2.0);
    handleNode_->set_point_size(7.0);
    handleNode_->set_color(ACG::Vec4f(0.2, 1.0, 0.2, 1.0));

219
    // Node showing feature selection
Jan Möbius's avatar
 
Jan Möbius committed
220
221
222
223
224
225
226
227
    featureNode_ = new ACG::SceneGraph::StatusNodeT<MeshT, FeatureNodeMod<MeshT> >(*mesh_, manipulatorNode(), "NEW FeatureNode for mesh ");
    featureNode_->set_round_points(true);
    featureNode_->enable_alpha_test(0.5);
    featureNode_->set_line_width(2.0);
    featureNode_->set_point_size(7.0);
    featureNode_->set_color(ACG::Vec4f(1.0, 0.2, 1.0, 1.0));


228
    if ( materialNode() == NULL)
Jan Möbius's avatar
 
Jan Möbius committed
229
230
		std::cerr << "Error when creating Mesh Object! materialNode is NULL!" << std::endl;

231
    textureNode_ = new ACG::SceneGraph::EnvMapNode(materialNode(),"NEW TextureNode for ", true, GL_LINEAR_MIPMAP_LINEAR );
Jan Möbius's avatar
 
Jan Möbius committed
232
233
234

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

235
    meshNode_    = new ACG::SceneGraph::TriStripNodeT<MeshT>(*mesh_, shaderNode_, "NEW MeshNode");
Jan Möbius's avatar
 
Jan Möbius committed
236
237
238
239
240
241
242
243
244

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

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



245
246
247
248
249
250
    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
251

252
    if ( QFile( shaderDir + "Ward/Vertex.glsl").exists() && QFile( shaderDir + "Ward/Fragment.glsl" ).exists() ) {
Jan Möbius's avatar
 
Jan Möbius committed
253

254
255
256
      shaderNode_->setShader(ACG::SceneGraph::DrawModes::SOLID_SHADER,
                              "Ward/Vertex.glsl" ,
                              "Ward/Fragment.glsl" );
Jan Möbius's avatar
 
Jan Möbius committed
257

258
259
260
261
262
263
      // Ward shader uses 3 parameters so activate shader, set params and deactivate it again
      GLSL::PtrProgram shader = shaderNode_->getShader( ACG::SceneGraph::DrawModes::SOLID_SHADER );
      if ( shader == 0 ) {
        std::cerr << "Unable to get shader for shader mode" << std::endl;
      } else {
        shader->use();
Jan Möbius's avatar
 
Jan Möbius committed
264

265
266
267
        shader->setUniform("ward_specular" , 0.5f);
        shader->setUniform("ward_diffuse"  , 3.0f);
        shader->setUniform("ward_alpha"    , 0.2f);
Jan Möbius's avatar
 
Jan Möbius committed
268

269
270
271
272
273
        shader->disable();
      }

    } else
        std::cerr << "Shader Files for Ward not found!!" << std::endl;
Jan Möbius's avatar
 
Jan Möbius committed
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289

    // Update all nodes
    update();
  }

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

  /** Set the name of an object. All Scenegraph nodes are renamed too. It also calls
  * BaseObjectData::setName.
  */
  template < class MeshT , DataType objectDataType >
  void MeshObject< MeshT , objectDataType >::setName( QString _name ) {
    BaseObjectData::setName(_name);

290
291
292
293
    // No update when gui is not active
    if ( OpenFlipper::Options::nogui() )
      return;

Jan Möbius's avatar
 
Jan Möbius committed
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
    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 );
  }

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

  /** Get a pointer to the objects mesh.
  * @return Pointer to the mesh
  */
  template < class MeshT , DataType objectDataType >
  MeshT* MeshObject< MeshT , objectDataType >::mesh() {
    return mesh_;
  }

  /** Updates the visualization of the object. Calls MeshObject::updateGeometry,
  *  MeshObject::updateTopology, MeshObject::updateSelection and
  *  MeshObject::updateModelingRegions.
  */
  template < class MeshT , DataType objectDataType >
  void MeshObject< MeshT , objectDataType >::update() {
334
335
336
337
338
339
340

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

    PluginFunctions::setMainGLContext();

Jan Möbius's avatar
 
Jan Möbius committed
341
342
343
344
345
346
347
348
349
350
    updateGeometry();
    updateTopology();
    updateSelection();
    updateFeatures();
    updateModelingRegions();
  }

  /** Updates the selection scenegraph nodes */
  template < class MeshT , DataType objectDataType >
  void MeshObject< MeshT , objectDataType >::updateSelection() {
351
352
    if ( statusNode_ )
      statusNode_->update_cache();
Jan Möbius's avatar
 
Jan Möbius committed
353
354
355
356
357
  }

  /** Updates the geometry information in the mesh scenegraph node */
  template < class MeshT , DataType objectDataType >
  void MeshObject< MeshT , objectDataType >::updateGeometry() {
358
359
    if ( meshNode_ )
      meshNode_->update_geometry();
Jan Möbius's avatar
 
Jan Möbius committed
360
361
362
363
364
  }

  /** Updates the topology information in the mesh scenegraph node */
  template < class MeshT , DataType objectDataType >
  void MeshObject< MeshT , objectDataType >::updateTopology() {
365
366
367
368
    if ( meshNode_ ) {
      meshNode_->update_topology();
      meshNode_->update_strips();
    }
Jan Möbius's avatar
 
Jan Möbius committed
369
370
371
372
373
  }

  /** Updates the modeling regions scenegraph nodes */
  template < class MeshT , DataType objectDataType >
  void MeshObject< MeshT , objectDataType >::updateModelingRegions() {
374
375
376
377
    if ( areaNode_ && handleNode_ ) {
      areaNode_->update_cache();
      handleNode_->update_cache();
    }
Jan Möbius's avatar
 
Jan Möbius committed
378
379
380
381
382
  }

  /** Updates the modeling regions scenegraph nodes */
  template < class MeshT , DataType objectDataType >
  void MeshObject< MeshT , objectDataType >::updateFeatures() {
383
384
    if ( featureNode_ )
      featureNode_->update_cache();
Jan Möbius's avatar
 
Jan Möbius committed
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
  }


  template < class MeshT , DataType objectDataType >
  BaseObject* MeshObject< MeshT , objectDataType >::copy() {
    MeshObject< MeshT , objectDataType >* object = new MeshObject< MeshT , objectDataType >(*this);
    return dynamic_cast< BaseObject* >(object);
  }

  // ===============================================================================
  // Visualization
  // ===============================================================================
  /** Returns a pointer to the texture node
  * @return Pointer to the texture node
  */
  template < class MeshT , DataType objectDataType >
401
  ACG::SceneGraph::EnvMapNode* MeshObject< MeshT , objectDataType >::textureNode() {
Jan Möbius's avatar
 
Jan Möbius committed
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
    return textureNode_;
  }

  /** Returns a pointer to the shader node
  * @return Pointer to the shader node
  */
  template < class MeshT , DataType objectDataType >
  ACG::SceneGraph::ShaderNode* MeshObject< MeshT , objectDataType >::shaderNode() {
    return shaderNode_;
  }

  /** Shows or hides the selections on the object
   */
  template < class MeshT , DataType objectDataType >
  void  MeshObject< MeshT , objectDataType >::hideSelection( bool _hide ) {

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

  }

  template < class MeshT , DataType objectDataType >
  void  MeshObject< MeshT , objectDataType >::hideFeatures( bool _hide ) {
    if ( _hide ) {
      featureNode_->set_status( ACG::SceneGraph::BaseNode::HideNode );
    } else {
      featureNode_->set_status( ACG::SceneGraph::BaseNode::Active );
    }
  }

  template < class MeshT , DataType objectDataType >
  bool  MeshObject< MeshT , objectDataType >::featuresVisible( ) {
    return ( featureNode_->status() == ACG::SceneGraph::BaseNode::Active );
  }

  /** Shows or hides the areas on the object
   */
  template < class MeshT , DataType objectDataType >
  void  MeshObject< MeshT , objectDataType >::hideAreas( bool _hide ) {
    if ( _hide ) {
      areaNode_->set_status( ACG::SceneGraph::BaseNode::HideNode );
      handleNode_->set_status( ACG::SceneGraph::BaseNode::HideNode );
    } else {
      areaNode_->set_status( ACG::SceneGraph::BaseNode::Active );
      handleNode_->set_status( ACG::SceneGraph::BaseNode::Active );
    }
  }

  template < class MeshT , DataType objectDataType >
  bool MeshObject< MeshT , objectDataType >::selectionVisible() {
    return ( statusNode_->status() == ACG::SceneGraph::BaseNode::Active );
  }

  template < class MeshT , DataType objectDataType >
  bool MeshObject< MeshT , objectDataType >::areasVisible() {
    return ( areaNode_->status() == ACG::SceneGraph::BaseNode::Active );
  }

  /** Returns a pointer to the mesh node
  * @return Pointer to the mesh node
  */
  template < class MeshT , DataType objectDataType >
467
  ACG::SceneGraph::TriStripNodeT<MeshT>* MeshObject< MeshT , objectDataType >::meshNode() {
Jan Möbius's avatar
 
Jan Möbius committed
468
469
470
471
472
473
474
    return meshNode_;
  }

  /** Get the Bounding box size of this object
   */
  template < class MeshT , DataType objectDataType >
  void MeshObject< MeshT , objectDataType >::boundingBox( ACG::Vec3f& _bbMin , ACG::Vec3f& _bbMax ) {
475
476
477
478
479
480
481
    if ( meshNode_ ) {
      _bbMin = ACG::Vec3f(FLT_MAX, FLT_MAX, FLT_MAX);
      _bbMax = ACG::Vec3f(FLT_MIN, FLT_MIN, FLT_MIN);
      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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
  }

  // ===============================================================================
  // 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.
  */
  template < class MeshT , DataType objectDataType >
  bool MeshObject< MeshT , objectDataType >::loadMesh(QString _filename) {

    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)
    {
      if ( objectDataType == DATA_TRIANGLE_MESH )
        std::cerr << "Main Application : Read error for Triangle Mesh at "<< filename << std::endl;
      if ( objectDataType == DATA_POLY_MESH )
        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
  */
  template < class MeshT , DataType objectDataType >
  QString MeshObject< MeshT , objectDataType >::getObjectinfo() {
    QString output;

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

    if ( dataType( DATA_TRIANGLE_MESH ) )
      output += "Object Contains Triangle Mesh : ";

    if ( dataType( DATA_POLY_MESH ) )
      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
557
  /** Given an node index from PluginFunctions::scenegraphPick this function can be used to
Jan Möbius's avatar
 
Jan Möbius committed
558
559
560
561
562
563
564
565
566
567
568
569
570
  * 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.
  */
  template < class MeshT , DataType objectDataType >
  bool MeshObject< MeshT , objectDataType >::picked( uint _node_idx ) {
    return ( _node_idx == meshNode_->id() );
  }


  template < class MeshT , DataType objectDataType >
  void MeshObject< MeshT , objectDataType >::enablePicking( bool _enable ) {
571
572
573
    if ( OpenFlipper::Options::nogui())
      return;

Jan Möbius's avatar
 
Jan Möbius committed
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
    meshNode_->enablePicking( _enable );
    areaNode_->enablePicking( _enable );
    handleNode_->enablePicking( _enable );
    featureNode_->enablePicking( _enable );
    textureNode_->enablePicking( _enable );
    shaderNode_->enablePicking( _enable );
  }

  template < class MeshT , DataType objectDataType >
  bool MeshObject< MeshT , objectDataType >::pickingEnabled() {
    return meshNode_->pickingEnabled();
  }

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

  template < class MeshT , DataType objectDataType >
  typename MeshObject< MeshT , objectDataType >::OMTriangleBSP*
  MeshObject< MeshT , objectDataType >::requestTriangleBsp() {

    if ( ! dataType( DATA_TRIANGLE_MESH) ) {
      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)
613
	     triangle_bsp_->push_back(f_it.handle());
Jan Möbius's avatar
 
Jan Möbius committed
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636

      triangle_bsp_->build(10, 100);
    }

    // return pointer to triangle bsp
    return triangle_bsp_;
  }

  template < class MeshT , DataType objectDataType >
  typename MeshObject< MeshT , objectDataType >::OMTriangleBSP*
  MeshObject< MeshT , objectDataType >::resetTriangleBsp() {
     if ( triangle_bsp_ != 0 )
     {
       delete triangle_bsp_;
       triangle_bsp_ = 0;
     }

     return requestTriangleBsp();
  }


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