PluginFunctions.cc 15.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//=============================================================================
//
//                               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.
15
//
16 17 18 19
//  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.
20
//
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
//  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$
//
//=============================================================================




//=============================================================================
//
//  Plugin Functions
//
//=============================================================================

#include <OpenFlipper/common/Types.hh>

#include "PluginFunctions.hh"
44
#include "PluginFunctionsCore.hh"
45 46 47 48 49 50 51 52 53 54 55 56 57

namespace PluginFunctions {

 /** \brief DONT USE DIRECTLY!!
  *
  * This Pointer will be used to access the data trees root in the plugin. Never use it directly as
 *  the internal Structure will change. Use the Access functions instead */
static BaseObject* objectRoot_;

/** \brief DONT USE DIRECTLY!!
 *
 * This pointer is internally used to acces the examiner widget in the main apllication
 */
58
static std::vector< glViewer* > examiner_widgets_;
Jan Möbius's avatar
Jan Möbius committed
59 60

/// TODO : Remove this variable and implement multiView correctly here
61
static glViewer*  examiner_widget_;
Jan Möbius's avatar
Jan Möbius committed
62 63

static unsigned int activeExaminer_ = 0;
64 65 66 67 68 69 70

/** \brief DONT USE DIRECTLY!!
 *
 * This pointer is internally used to access the scenegraphs root node
 */
static SeparatorNode* root_node_;

71 72
static SeparatorNode* sceneGraph_root_node_;

73 74
static Viewer::ViewerProperties dummyProperties;

75 76 77 78
void setDataRoot( BaseObject* _root ) {
   objectRoot_ = _root;
}

79
void setViewers( std::vector< glViewer* > _examiner_widgets ) {
Jan Möbius's avatar
Jan Möbius committed
80 81 82 83
   PluginFunctions::examiner_widgets_ = _examiner_widgets;
   PluginFunctions::examiner_widget_ =  examiner_widgets_[0];
}

84
void setActiveExaminer( const unsigned int _id ) {
Jan Möbius's avatar
Jan Möbius committed
85
  activeExaminer_ = _id;
86 87
}

88 89 90 91
unsigned int activeExaminer( ) {
  return activeExaminer_;
}

92
void setRootNode( SeparatorNode* _root_node ) {
93 94 95
   PluginFunctions::root_node_ = _root_node;
}

96
void setSceneGraphRootNode( SeparatorNode* _root_node ) {
97 98 99
   PluginFunctions::sceneGraph_root_node_ = _root_node;
}

100
bool getPickedObject(const unsigned int _node_idx , BaseObjectData*& _object) {
101
  for ( ObjectIterator o_it(PluginFunctions::ALL_OBJECTS) ; o_it != PluginFunctions::objectsEnd(); ++o_it) {
102 103 104 105 106 107 108 109 110
    if ( o_it->picked( _node_idx ) ) {
      _object = *o_it;
      return true;
    }
  }
  return false;
}


111
bool getSourceIdentifiers( std::vector<int>& _identifiers  ) {
112 113
  _identifiers.clear();

114
  for ( ObjectIterator o_it(PluginFunctions::ALL_OBJECTS) ; o_it != PluginFunctions::objectsEnd(); ++o_it) {
115 116 117 118 119 120
    if ( o_it->source() )
      _identifiers.push_back ( o_it->id() );
  }
  return (_identifiers.size() >0 );
}

121
bool getTargetIdentifiers( std::vector<int>& _identifiers  ) {
122 123
  _identifiers.clear();

124
  for ( ObjectIterator o_it(PluginFunctions::ALL_OBJECTS) ; o_it != PluginFunctions::objectsEnd(); ++o_it) {
125 126 127 128 129 130 131 132 133 134
    if ( o_it->target() )
      _identifiers.push_back ( o_it->id() );
  }
  return (_identifiers.size() >0 );
}

// ===============================================================================
// Get objects
// ===============================================================================

135
bool getObject(  const int _identifier , BaseObject*& _object ) {
136

137 138
  if ( _identifier == -1 )
    return false;
139

140 141 142 143
  _object = objectRoot_->childExists( _identifier );
  return ( _object != 0 );
}

144
bool getObject(  const int _identifier , BaseObjectData*& _object ) {
145

146 147
  if ( _identifier == -1 )
    return false;
148

149 150 151 152 153 154 155 156 157
  BaseObject* object = objectRoot_->childExists( _identifier );
  _object = dynamic_cast< BaseObjectData* >(object);
  return ( _object != 0 );
}

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


158
bool deleteObject( const int _id ) {
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
  if ( _id == -1 )
    return false;

  // get the node
  BaseObject* object = objectRoot_->childExists(_id);

  if ( !object || object == objectRoot() ) {
    std::cerr << "Error while deleting object, does not exist!!" << std::endl;
    return false;
  }

  // remove the whole subtree below this item
  object->deleteSubtree();

  // remove the item itself from the parent
  object->parent()->removeChild(object);

  // delete it
  delete object;

  return true;
}

void deleteAll( ) {
  objectRoot()->deleteSubtree();
}

187
int copyObject( const int _id ) {
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
  if ( _id == -1 )
    return -1;

  // get the node
  BaseObject* object = objectRoot_->childExists(_id);

  if ( !object ) {
    std::cerr << "Error while copying object ... not found" << std::endl;
    return -1;
  }

  // remove the whole subtree below this item
  BaseObject* copy = object->copy();

  if ( copy == 0 ) {
    std::cerr << "Unable to create a copy of the object." << std::endl;
  }

  // Integrate into object tree
  copy->setParent( object->parent() );
  if ( object->parent() )
    object->parent()->appendChild(copy);
  else
    std::cerr << "Unable to add copy to object list" << std::endl;

  return copy->id();
}
215

216
bool object_exists(  const int _identifier ) {
217

218 219
  if ( _identifier == -1 )
    return false;
220

221 222 223 224 225 226
  BaseObject* object = objectRoot_->childExists( _identifier );
  return ( object != 0 );
}

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

227
bool getAllMeshes( std::vector<int>& _identifiers  ) {
228 229 230 231 232

  _identifiers.clear();

  // find changed manipulator
  for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS,DATA_TRIANGLE_MESH) ;
233
                                        o_it != PluginFunctions::objectsEnd(); ++o_it)  {
234 235 236 237 238 239 240 241 242 243 244 245
    _identifiers.push_back( o_it->id() );
  }

  return (_identifiers.size() > 0);
}

bool get_all_object_identifiers( std::vector<int>& _identifiers  ) {

  _identifiers.clear();

  // find changed manipulator
  for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS) ;
246
                                        o_it != PluginFunctions::objectsEnd(); ++o_it)  {
247 248 249 250 251 252 253
    _identifiers.push_back( o_it->id() );
  }

  return (_identifiers.size() > 0);
}

void setBackColor( OpenMesh::Vec4f _color) {
254
  for ( uint i = 0 ; i < examiner_widgets_.size(); ++i )
255
    PluginFunctions::viewerProperties(i).backgroundColor(_color);
256 257
}

258
QPoint mapToGlobal(const QPoint _point ) {
Jan Möbius's avatar
Jan Möbius committed
259
  return examiner_widgets_[activeExaminer_]->glMapToGlobal(_point);
260 261
}

262
QPoint mapToLocal( const QPoint _point ) {
Jan Möbius's avatar
Jan Möbius committed
263
  return examiner_widgets_[activeExaminer_]->glMapFromGlobal(_point);
264 265 266 267 268 269 270
}

/** Set the draw Mode of the examiner widget.\n
 *
 * The DrawModes are defined at ACG/Scenegraph/DrawModes.hh \n
 * They can be combined.
 */
271
void setDrawMode( const unsigned int _mode ) {
Jan Möbius's avatar
Jan Möbius committed
272 273
  examiner_widgets_[activeExaminer_]->drawMode(_mode);
  examiner_widgets_[activeExaminer_]->updateGL();
274 275 276 277 278 279 280 281
}

/** Get the current draw Mode of the examiner widget.\n
 *
 * The DrawModes are defined at ACG/Scenegraph/DrawModes.hh \n
 * They can be combined.
 */
unsigned int drawMode( ) {
Jan Möbius's avatar
Jan Möbius committed
282
  return examiner_widgets_[activeExaminer_]->drawMode();
283 284
}

285 286 287 288 289 290 291 292
void setGlobalDrawMode( const unsigned int _mode ) {
  for ( uint i = 0 ; i < examiner_widgets_.size(); ++i ) {
    examiner_widgets_[i]->drawMode(_mode);
    examiner_widgets_[i]->updateGL();
  }

}

293
bool scenegraphPick( ACG::SceneGraph::PickTarget _pickTarget, const QPoint &_mousePos, unsigned int &_nodeIdx, unsigned int &_targetIdx, ACG::Vec3d *_hitPointPtr=0 ) {
Jan Möbius's avatar
Jan Möbius committed
294 295 296 297

   return examiner_widgets_[activeExaminer_]->pick( _pickTarget,_mousePos,_nodeIdx,_targetIdx,_hitPointPtr );
}

298
bool scenegraphPick( const unsigned int _examiner, ACG::SceneGraph::PickTarget _pickTarget, const QPoint &_mousePos, unsigned int &_nodeIdx, unsigned int &_targetIdx, ACG::Vec3d *_hitPointPtr=0 ) {
Jan Möbius's avatar
Jan Möbius committed
299 300 301 302 303 304

  if ( _examiner >= examiner_widgets_.size() ) {
    std::cerr << "Wrong examiner id" << std::endl;
    return false;
  }
  return examiner_widgets_[_examiner]->pick( _pickTarget,_mousePos,_nodeIdx,_targetIdx,_hitPointPtr );
305 306 307 308 309
}


//Warning : Dont use template function as external static pointer for examiner widget is not resolved correctly!!
void traverse( ACG::SceneGraph::MouseEventAction  &_action ) {
310
   ACG::SceneGraph::traverse(sceneGraph_root_node_,
311
                             _action,viewerProperties().glState() );
312 313
}

Jan Möbius's avatar
Jan Möbius committed
314
//Warning : Dont use template function as external static pointer for examiner widget is not resolved correctly!!
315
void traverse( const unsigned int _examiner, ACG::SceneGraph::MouseEventAction  &_action ) {
Jan Möbius's avatar
Jan Möbius committed
316 317 318 319 320 321

  if ( _examiner >= examiner_widgets_.size() ) {
    std::cerr << "Wrong examiner id" << std::endl;
    return;
  }

322
  ACG::SceneGraph::traverse(sceneGraph_root_node_, _action,viewerProperties(_examiner).glState() );
Jan Möbius's avatar
Jan Möbius committed
323 324 325
}


326
const std::string & pickMode () {
327 328
  // No seperate draw modes available all should have the same so take first
  return examiner_widgets_[0]->pickMode();
329 330 331
}

void pickMode ( std::string _mode) {
Jan Möbius's avatar
Jan Möbius committed
332 333 334 335
  for ( uint i = 0 ; i < examiner_widgets_.size() ; ++i )
    examiner_widgets_[i]->pickMode(_mode);
}

336
void pickMode ( const unsigned int _examiner, std::string _mode) {
Jan Möbius's avatar
Jan Möbius committed
337 338 339 340 341 342
  if ( _examiner >= examiner_widgets_.size() ) {
    std::cerr << "Wrong examiner id" << std::endl;
    return;
  }

  examiner_widgets_[_examiner]->pickMode(_mode);
343 344
}

345 346
void actionMode ( Viewer::ActionMode _mode) {

347
  for ( uint i = 0 ; i < examiner_widgets_.size() ; ++i )
348
    viewerProperties(i).actionMode(_mode);
349 350 351
}

ACG::GLState&  glState() {
352
   return viewerProperties().glState();
353 354 355 356 357 358
}

void getCurrentViewImage(QImage& _image) {
  examiner_widget_->copyToImage( _image );
}

359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
Viewer::ActionMode actionMode() {
   return viewerProperties(activeExaminer_).actionMode();
}

Viewer::ViewerProperties& viewerProperties(int _id) {
  if ( _id >= (int)examiner_widgets_.size() ) {
    std::cerr << " Error, requested properties for non-existing Viewer!" << std::endl;
    return dummyProperties;
  }

  if ( _id == -1 )
    _id = activeExaminer_;

  return (*examiner_widgets_[_id]->properties());

374 375 376 377 378 379 380 381 382 383 384 385 386 387
}

void perspectiveProjection() {
  examiner_widget_->perspectiveProjection();
}

void orthographicProjection() {
  examiner_widget_->orthographicProjection();
}

void allowRotation(bool _mode) {
  examiner_widget_->allowRotation(_mode);
}

388 389 390 391
void setMainGLContext() {
  examiner_widget_->makeCurrent();
}

392
void viewingDirection(const ACG::Vec3d &_dir, const ACG::Vec3d &_up) {
393
  examiner_widgets_[activeExaminer_]->viewingDirection(_dir,_up);
394 395 396
}


397
void setScenePos(const ACG::Vec3d& _center,const double _radius) {
398
  examiner_widgets_[activeExaminer_]->setScenePos( _center, _radius );
399 400 401
}

void setScenePos(const ACG::Vec3d& _center) {
402
  examiner_widgets_[activeExaminer_]->setScenePos( _center, examiner_widgets_[activeExaminer_]->scene_radius() );
403 404 405
}

const ACG::Vec3d& sceneCenter() {
406
   return examiner_widgets_[activeExaminer_]->scene_center();
407 408 409
}

double sceneRadius() {
410
   return examiner_widgets_[activeExaminer_]->scene_radius();
411 412 413
}

void translate( const ACG::Vec3d &_vector ) {
414
  examiner_widgets_[activeExaminer_]->translate(_vector);
415 416
}

417
void rotate(const ACG::Vec3d&  _axis,
418
            const double       _angle,
419 420
            const ACG::Vec3d&  _center)
{
421
  examiner_widgets_[activeExaminer_]->rotate(_axis,_angle,_center);
422 423 424
}

void viewHome() {
425
  examiner_widgets_[activeExaminer_]->home();
426 427 428
}

void viewAll() {
429
  examiner_widgets_[activeExaminer_]->viewAll();
430 431 432
}

ACG::Vec3d viewingDirection() {
433
  return viewerProperties().glState().viewing_direction();
434 435 436
}

ACG::Vec3d eyePos() {
437
  return viewerProperties().glState().eye();
438 439 440
}

ACG::Vec3d upVector() {
441
  return viewerProperties().glState().up();
442 443 444
}


445 446 447 448
ACG::SceneGraph::BaseNode* getSceneGraphRootNode() {
   return PluginFunctions::sceneGraph_root_node_;
}

449 450 451 452
ACG::SceneGraph::BaseNode* getRootNode() {
   return PluginFunctions::root_node_;
}

453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
void addNode(ACG::SceneGraph::BaseNode* _node){
  if (PluginFunctions::root_node_)
    _node->set_parent( PluginFunctions::root_node_ );
}

void addGlobalNode(ACG::SceneGraph::BaseNode* _node){
  if (PluginFunctions::sceneGraph_root_node_){
    //set node as new parent for root's children
    while( PluginFunctions::sceneGraph_root_node_->nChildren() > 0 ){
      ACG::SceneGraph::BaseNode* child = *(PluginFunctions::sceneGraph_root_node_->childrenBegin());
      child->set_parent( _node );
    }

    _node->set_parent( PluginFunctions::sceneGraph_root_node_ );
  }
}

470
int objectCount() {
471 472 473 474 475

  int count = 0;

  // find changed manipulator
  for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS) ;
476
                                        o_it != PluginFunctions::objectsEnd(); ++o_it)  {
477 478 479 480 481 482 483
    ++count;
  }

  return ( count );

}

484
int targetCount() {
485 486 487 488 489

  int count = 0;

  // find changed manipulator
  for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS) ;
490
                                        o_it != PluginFunctions::objectsEnd(); ++o_it)  {
491 492 493 494 495 496
    ++count;
  }

  return ( count );
}

497
int sourceCount() {
498 499 500 501
  int count = 0;

  // find changed manipulator
  for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::SOURCE_OBJECTS) ;
502
                                        o_it != PluginFunctions::objectsEnd(); ++o_it)  {
503 504 505 506 507 508
    ++count;
  }

  return ( count );
}

509
int visibleCount() {
510 511 512 513
  int count = 0;

  // find changed manipulator
  for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS) ;
514
                                        o_it != PluginFunctions::objectsEnd(); ++o_it)  {
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
    if ( o_it->visible() )
    ++count;
  }

  return ( count );

}


/** Returns a vector containing all currently available objects
 *
 * @param _objects vector of all objects
 */
void get_all_objects( std::vector < BaseObjectData*>& _objects ) {

   _objects.clear();

  // find changed manipulator
  for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS,DATA_TRIANGLE_MESH) ;
534
                                        o_it != PluginFunctions::objectsEnd(); ++o_it)  {
535 536 537 538 539 540
    _objects.push_back( *o_it );
  }

}

///   Fly to point and viewing direction (animated).
541
void   flyTo (const ACG::Vec3d &_position, const ACG::Vec3d &_center, double _time) {
542
   examiner_widgets_[activeExaminer_]->flyTo(_position,_center,_time);
543 544 545 546 547 548 549 550 551 552
}


// ===============================================================================
// Getting data from objects and casting between them
// ===============================================================================

BaseObjectData* baseObjectData( BaseObject* _object ){
  if ( _object == 0 )
    return 0;
553

554 555 556 557 558 559
  return dynamic_cast< BaseObjectData* >(_object);
}

// ===============================================================================
// Get the root of the object structure
// ===============================================================================
Jan Möbius's avatar
Jan Möbius committed
560
BaseObject*& objectRoot() {
561 562 563 564
  return (objectRoot_);
}

}