BaseObjectData.cc 11 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-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/>.                                       *
*                                                                            *
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




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

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

#include "Types.hh"
55
#include <ACG/Scenegraph/SceneGraph.hh>
56
#include <OpenFlipper/common/BaseObjectCore.hh>
57
#include <OpenFlipper/common/GlobalOptions.hh>
58 59 60 61
#include <OpenFlipper/BasePlugin/PluginFunctions.hh>
#include <QDir>


Jan Möbius's avatar
 
Jan Möbius committed
62 63 64 65

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

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

67 68 69
BaseObjectData::BaseObjectData(const BaseObjectData& _object)
  : BaseObject(_object),
    path_("."),
70
    manipPlaced_(false),
71 72 73
    rootNode_(_object.rootNode_),
    separatorNode_(0),
    manipulatorNode_(0),
74
    materialNode_(0),
75 76
    boundingBoxNode_(0),
    stencilRefNode_(0)
77
{
78 79
  /// We have to create our own visualization nodes as we are a new object
  initializeScenegraphNodes();
80 81
}

82
BaseObjectData::BaseObjectData() :
Jan Möbius's avatar
 
Jan Möbius committed
83 84
  BaseObject(),
  path_("."),
85
  manipPlaced_(false),
86
  rootNode_( dynamic_cast< ACG::SceneGraph::SeparatorNode* > (PluginFunctions::getRootNode()) ),
Jan Möbius's avatar
 
Jan Möbius committed
87 88
  separatorNode_(0),
  manipulatorNode_(0),
89
  materialNode_(0),
90 91
  boundingBoxNode_(0),
  stencilRefNode_(0)
Jan Möbius's avatar
 
Jan Möbius committed
92
{
93
  initializeScenegraphNodes();
Jan Möbius's avatar
 
Jan Möbius committed
94 95 96 97 98 99
}

BaseObjectData::~BaseObjectData() {
  if ( separatorNode_ != 0 ) {
    separatorNode_->delete_subtree();
  }
100

Jan Möbius's avatar
 
Jan Möbius committed
101 102 103 104
}

void BaseObjectData::cleanup() {
  path_       = ".";
105

Jan Möbius's avatar
 
Jan Möbius committed
106 107 108 109 110 111 112 113
  // Delete everything below the seperator node on top of the object. This will remove the complete subtree.
  if ( separatorNode_ == 0 )
    std::cerr << "cleanup : separatorNode_ is already 0" << std::endl;
  else {
    separatorNode_->delete_subtree();
    separatorNode_   = 0;
    manipulatorNode_ = 0;
    materialNode_    = 0;
114
    boundingBoxNode_ = 0;
Jan Möbius's avatar
 
Jan Möbius committed
115 116
    additionalNodes_.clear();
  }
117

Jan Möbius's avatar
 
Jan Möbius committed
118
  BaseObject::cleanup();
119

120
  initializeScenegraphNodes();
Jan Möbius's avatar
 
Jan Möbius committed
121 122
}

123
void BaseObjectData::initializeScenegraphNodes() {
Jan Möbius's avatar
 
Jan Möbius committed
124 125 126
  // Create seperatorNode for Object only if it does not exist.
  if ( separatorNode_ == 0 )
    separatorNode_       = new SeparatorNode((BaseNode*)rootNode_,"NEW Object");
127
  else
Jan Möbius's avatar
 
Jan Möbius committed
128
    std::cerr << "Separator Node already exists. this should not happen!" << std::endl;
129

Jan Möbius's avatar
 
Jan Möbius committed
130
  if ( manipulatorNode_ == 0 ) {
131
    manipulatorNode_      = new QtTranslationManipulatorNode(baseNode(),"NEW ManipulatorNode");
132 133 134

    // Bind this manipulator to the current object
    manipulatorNode_->setIdentifier(id());
135
    manipulatorNode_->set_status( ACG::SceneGraph::TranslationManipulatorNode::Active );
Jan Möbius's avatar
 
Jan Möbius committed
136
  }
137
  else
Jan Möbius's avatar
 
Jan Möbius committed
138
    std::cerr << "Manipulator Node already exists. this should not happen!" << std::endl;
139 140 141 142 143
  if ( boundingBoxNode_ == 0)
  {
    boundingBoxNode_      = new BoundingBoxNode(manipulatorNode(),  "New Bounding Box");
    boundingBoxNode_->set_status( ACG::SceneGraph::BaseNode::HideNode );
  }
144 145
  if ( stencilRefNode_ == 0 )
  {
146
    stencilRefNode_       = new StencilRefNode(boundingBoxNode(),  "New Stencil Reference");
147 148
    stencilRefNode_->set_status( ACG::SceneGraph::BaseNode::HideNode );
  }
149
  if ( materialNode_ == 0 ) {
150
    materialNode_         = new MaterialNode(stencilRefNode(),  "New Material");
151 152 153 154
    
    QColor color;
    
    if ( OpenFlipper::Options::randomBaseColor() ){
Jan Möbius's avatar
Jan Möbius committed
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
      QColor bckgrnd = OpenFlipperSettings().value("Core/Gui/glViewer/defaultBackgroundColor").value<QColor>();
      int diff;
      
      do{
        color.setRgb(rand()%255, rand()%255, rand()%255);
        
        diff = (bckgrnd.red()   - color.red())  *(bckgrnd.red()   - color.red())
              +(bckgrnd.green() - color.green())*(bckgrnd.green() - color.green())
              +(bckgrnd.blue()  - color.blue()) *(bckgrnd.blue()  - color.blue());
      } while (diff < 70000);
    }
    else{
      color = OpenFlipper::Options::defaultBaseColor();
    }
    
    ACG::Vec4f colorV;
    colorV[0] = color.redF();
    colorV[1] = color.greenF();
    colorV[2] = color.blueF();
    colorV[3] = color.alphaF();
    
    materialNode_->set_base_color(colorV);
    materialNode_->set_color(colorV);
    
  }
Jan Möbius's avatar
 
Jan Möbius committed
181 182 183 184 185 186 187 188
}


// ===============================================================================
// Object visualization
// ===============================================================================

void BaseObjectData::show() {
189 190 191 192 193 194
  if ( !visible_ ) {
    separatorNode_->set_status( ACG::SceneGraph::BaseNode::Active  );
    visible_ = true;
    
    emit visibilityChanged( id() );
  }
Jan Möbius's avatar
 
Jan Möbius committed
195 196 197
}

void BaseObjectData::hide() {
198 199 200 201 202 203
  if ( visible_ ) {
    separatorNode_->set_status( ACG::SceneGraph::BaseNode::HideSubtree );
    visible_ = false;
    
    emit visibilityChanged( id() );
  }
Jan Möbius's avatar
 
Jan Möbius committed
204 205
}

206 207 208 209 210 211
bool BaseObjectData::visible(){
  return visible_;
}

void BaseObjectData::visible(bool _visible) {

212 213 214 215 216 217
  if ( visible_ != _visible ) {
    
    if (_visible)
      separatorNode_->set_status( ACG::SceneGraph::BaseNode::Active  );
    else
      separatorNode_->set_status( ACG::SceneGraph::BaseNode::HideSubtree );
218

219 220 221 222
    visible_ = _visible;
    
    emit visibilityChanged( id() );
  }
223 224
}

Jan Möbius's avatar
 
Jan Möbius committed
225
SeparatorNode* BaseObjectData::baseNode() {
226
  return separatorNode_;
Jan Möbius's avatar
 
Jan Möbius committed
227 228
}

229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
bool BaseObjectData::hasNode(BaseNode* _node) {

  QList< BaseNode* > list;
  list.push_back(separatorNode_);
  
  BaseNode* currentNode;
  while (!list.empty()) {
    currentNode = list.front();
    
    if ( currentNode->find(_node) == currentNode->childrenEnd() ) {
      for ( BaseNode::ChildIter child = currentNode->childrenBegin() ; child != currentNode->childrenEnd(); ++child )
        list.push_back(*child);
    } else 
      return true;
      
    list.pop_front();
  }
  
  return false;
  
}

Dirk Wilden's avatar
Dirk Wilden committed
251 252 253 254
BaseNode* BaseObjectData::primaryNode() {
  return separatorNode_;
}

255
QtTranslationManipulatorNode* BaseObjectData::manipulatorNode() {
256 257 258 259 260
  return manipulatorNode_;
}

ACG::SceneGraph::ShaderNode* BaseObjectData::shaderNode() {
  return 0;
Jan Möbius's avatar
 
Jan Möbius committed
261 262 263
}

MaterialNode* BaseObjectData::materialNode() {
264
  return materialNode_;
Jan Möbius's avatar
 
Jan Möbius committed
265 266
}

267 268 269 270
BoundingBoxNode* BaseObjectData::boundingBoxNode() {
  return boundingBoxNode_;
}

271 272 273 274
StencilRefNode* BaseObjectData::stencilRefNode() {
  return stencilRefNode_;
}

Jan Möbius's avatar
 
Jan Möbius committed
275 276 277 278 279 280 281 282
bool BaseObjectData::manipPlaced() {
  return manipPlaced_;
}

void BaseObjectData::manipPlaced( bool _placed ) {
  manipPlaced_ = _placed;
}

283 284
void BaseObjectData::getBoundingBox(ACG::Vec3d& bbmin, ACG::Vec3d& bbmax){

285
  // Single pass action, as the bounding box is not influenced by multipass traversal
286 287 288 289 290 291 292 293
  ACG::SceneGraph::BoundingBoxAction act;
  ACG::SceneGraph::traverse(separatorNode_, act);


  bbmin = (ACG::Vec3d) act.bbMin();
  bbmax = (ACG::Vec3d) act.bbMax();
}

294
void BaseObjectData::setObjectDrawMode(const ACG::SceneGraph::DrawModes::DrawMode _mode, bool _force) {
295 296
    
  // Set draw mode for this object
297
  ACG::SceneGraph::SetDrawModesAction actionActive(_mode, _force);
298 299 300 301 302
  
  // Traverse scenegraph in order to set new draw mode
  ACG::SceneGraph::traverse(primaryNode(), actionActive);
}

Jan Möbius's avatar
 
Jan Möbius committed
303
// ===============================================================================
304
// Picking
Jan Möbius's avatar
 
Jan Möbius committed
305 306 307
// ===============================================================================

bool BaseObjectData::picked( uint /* _node_idx */ ) {
308
 return false;
Jan Möbius's avatar
 
Jan Möbius committed
309 310
}

311 312 313 314 315 316 317
void BaseObjectData::enablePicking( bool /*_enable*/ ) {
}

bool BaseObjectData::pickingEnabled() {
  return true;
}

Jan Möbius's avatar
 
Jan Möbius committed
318 319 320
// ===============================================================================
// Content Nodes
// ===============================================================================
321
void BaseObjectData::update(UpdateType _type )
Jan Möbius's avatar
 
Jan Möbius committed
322 323 324 325 326 327 328 329 330 331 332 333 334
{

}

// ===============================================================================
// Additional Nodes
// ===============================================================================

bool BaseObjectData::hasAdditionalNode(QString _pluginName, QString _nodeName , int _id )
{
  QString index;
  index.setNum(_id);
  QString searchname = _pluginName+"#"+_nodeName+"#"+index;
335

Jan Möbius's avatar
 
Jan Möbius committed
336 337 338 339
  for ( uint i =0 ; i < additionalNodes_.size() ; ++i ) {
    if (additionalNodes_[i].second == searchname )
      return true;
  }
340

Jan Möbius's avatar
 
Jan Möbius committed
341 342 343
  return false;
}

344

Jan Möbius's avatar
 
Jan Möbius committed
345
//=============================================================================