Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/coord/p_node.cc @ 6629

Last change on this file since 6629 was 6624, checked in by bensch, 19 years ago

orxonox/trunk: Some missing stuff

File size: 32.2 KB
RevLine 
[4570]1/*
[3246]2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Patrick Boenzli
[5419]13   co-programmer: Benjamin Grauer
[3246]14*/
15
[3590]16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_PNODE
[3246]17
18#include "p_node.h"
[4761]19
20#include "load_param.h"
21#include "class_list.h"
22
[6078]23#include <algorithm>
[3860]24#include "compiler.h"
[3608]25#include "debug.h"
26
[6054]27#include "glincl.h"
[5406]28#include "color.h"
[3246]29
[6341]30#include "synchronizeable.h"
31
[6424]32#include "shell_command.h"
33SHELL_COMMAND(debugNode, PNode, debugNodeSC);
34
[3246]35using namespace std;
36
37
38/**
[6048]39 * @brief standard constructor
[6078]40 * @param parent the Parent of this Node. __NULL__ if __No Parent__ requested, PNode::getNullParent(), if connected to NullParent directly (default)
[6299]41 * @param nodeFlags all flags to set. THIS_WILL_OVERWRITE Default_Values.
[5420]42 */
[6078]43PNode::PNode (PNode* parent, long nodeFlags)
[3365]44{
[6074]45  this->setClassID(CL_PARENT_NODE, "PNode");
[3365]46
[6074]47  this->bRelCoorChanged = true;
48  this->bRelDirChanged = true;
49  this->parent = NULL;
[6078]50  this->parentMode = nodeFlags;
[6074]51  this->bActive = true;
[4993]52
[6074]53  // smooth-movers
54  this->toCoordinate = NULL;
55  this->toDirection = NULL;
56  this->bias = 1.0;
[6078]57
58  if (parent != NULL)
59    parent->addChild(this);
[3365]60}
61
[6074]62// NullParent Reference
63PNode* PNode::nullParent = NULL;
[6073]64
[3365]65/**
[6048]66 * @brief standard deconstructor
[5296]67 *
68 * There are two general ways to delete a PNode
69 * 1. delete instance;
70 *   -> result
71 *    delete this Node and all its children and children's children...
72 *    (danger if you still need the children's instance somewhere else!!)
73 *
[6073]74 * 2. instance->removeNode(); delete instance;
[5296]75 *   -> result:
76 *    moves its children to the NullParent
77 *    then deletes the Element.
78 */
[4570]79PNode::~PNode ()
[3365]80{
[6073]81  // remove the Node, delete it's children (if required).
82  std::list<PNode*>::iterator tmp = this->children.begin();
83  std::list<PNode*>::iterator deleteNode;
84  while(!this->children.empty())
85    while (tmp != this->children.end())
86    {
87      deleteNode = tmp;
[6142]88      tmp++;
[6078]89//      printf("TEST::%s(%s) %s\n", (*deleteNode)->getName(), (*deleteNode)->getClassName(), this->getName());
[6073]90      if ((this->parentMode & PNODE_PROHIBIT_CHILD_DELETE) ||
91          ((*deleteNode)->parentMode & PNODE_PROHIBIT_DELETE_WITH_PARENT))
92      {
[6078]93        if (this == PNode::nullParent && (*deleteNode)->parentMode & PNODE_REPARENT_TO_NULL)
[6073]94          delete (*deleteNode);
95        else
96          (*deleteNode)->reparent();
97      }
98      else
99        delete (*deleteNode);
100    }
101
[6071]102  if (this->parent != NULL)
103   {
[6078]104     this->parent->eraseChild(this);
[6071]105     this->parent = NULL;
106   }
107
[5296]108  // remove all other allocated memory.
[5088]109  if (this->toCoordinate != NULL)
110    delete this->toCoordinate;
111  if (this->toDirection != NULL)
112    delete this->toDirection;
[6075]113
114  if (this == PNode::nullParent)
115    PNode::nullParent = NULL;
[3365]116}
[3246]117
[5296]118
[4448]119/**
[6048]120 * @brief loads parameters of the PNode
[4836]121 * @param root the XML-element to load the properties of
[5420]122 */
[4436]123void PNode::loadParams(const TiXmlElement* root)
124{
[6512]125  BaseObject::loadParams(root);
[4610]126
[5671]127  LoadParam(root, "rel-coor", this, PNode, setRelCoor)
[4771]128      .describe("Sets The relative position of the Node to its parent.");
129
[5671]130  LoadParam(root, "abs-coor", this, PNode, setAbsCoor)
[4610]131      .describe("Sets The absolute Position of the Node.");
132
[5671]133  LoadParam(root, "rel-dir", this, PNode, setRelDir)
[4771]134      .describe("Sets The relative rotation of the Node to its parent.");
[4761]135
[5671]136  LoadParam(root, "abs-dir", this, PNode, setAbsDir)
[4771]137      .describe("Sets The absolute rotation of the Node.");
138
[5671]139  LoadParam(root, "parent", this, PNode, setParent)
[4761]140      .describe("the Name of the Parent of this PNode");
[4765]141
[5671]142  LoadParam(root, "parent-mode", this, PNode, setParentMode)
[4765]143      .describe("the mode to connect this node to its parent ()");
144
145  // cycling properties
[4785]146  if (root != NULL)
[4765]147  {
[5654]148    LOAD_PARAM_START_CYCLE(root, element);
[4785]149    {
[6074]150      LoadParam_CYCLE(element, "child", this, PNode, addChild)
[4785]151          .describe("adds a new Child to the current Node.");
[4765]152
[4785]153    }
[5654]154    LOAD_PARAM_END_CYCLE(element);
[4765]155  }
[4436]156}
[3365]157
[6424]158
[3365]159/**
[6424]160 *  init the pnode to a well definied state
161 *
162 * this function actualy only updates the PNode tree
163 */
164void PNode::init()
165{
166  /* just update all aboslute positions via timestep 0.001ms */
167  this->updateNode(0.001f);
168  this->updateNode(0.001f);
169}
170
171
172/**
[6048]173 * @brief set relative coordinates
[4836]174 * @param relCoord relative coordinates to its parent
[5420]175 *
176 *
177 * it is very importand, that you use this function, if you want to update the
178 * relCoordinates. If you don't use this, the PNode won't recognize, that something
179 * has changed and won't update the children Nodes.
180 */
[3810]181void PNode::setRelCoor (const Vector& relCoord)
[3675]182{
[5113]183  if (this->toCoordinate!= NULL)
184  {
185    delete this->toCoordinate;
186    this->toCoordinate = NULL;
187  }
188
[4993]189  this->relCoordinate = relCoord;
[3675]190  this->bRelCoorChanged = true;
191}
192
193/**
[6048]194 * @brief set relative coordinates
[4836]195 * @param x x-relative coordinates to its parent
196 * @param y y-relative coordinates to its parent
197 * @param z z-relative coordinates to its parent
[4993]198 * @see  void PNode::setRelCoor (const Vector& relCoord)
[5420]199 */
[4610]200void PNode::setRelCoor (float x, float y, float z)
201{
202  this->setRelCoor(Vector(x, y, z));
203}
204
[4992]205/**
[6048]206 * @brief sets a new relative position smoothely
[4992]207 * @param relCoordSoft the new Position to iterate to
208 * @param bias how fast to iterate to this position
209 */
210void PNode::setRelCoorSoft(const Vector& relCoordSoft, float bias)
[4987]211{
[4993]212  if (likely(this->toCoordinate == NULL))
213    this->toCoordinate = new Vector();
[4987]214
[4993]215  *this->toCoordinate = relCoordSoft;
[6616]216  this->toStep = 0.0f;
[4992]217  this->bias = bias;
[4987]218}
219
[4990]220
[4610]221/**
[6048]222 * @brief set relative coordinates smoothely
[4990]223 * @param x x-relative coordinates to its parent
224 * @param y y-relative coordinates to its parent
225 * @param z z-relative coordinates to its parent
[4993]226 * @see  void PNode::setRelCoorSoft (const Vector&, float)
[4990]227 */
[4992]228void PNode::setRelCoorSoft (float x, float y, float z, float bias)
[4990]229{
[4992]230  this->setRelCoorSoft(Vector(x, y, z), bias);
[4990]231}
232
[5382]233
[4990]234/**
[4836]235 * @param absCoord set absolute coordinate
[5091]236 */
[3809]237void PNode::setAbsCoor (const Vector& absCoord)
[3675]238{
[5113]239  if (this->toCoordinate!= NULL)
240  {
241    delete this->toCoordinate;
242    this->toCoordinate = NULL;
243  }
244
[4993]245  if( likely(this->parentMode & PNODE_MOVEMENT))
246  {
247      /* if you have set the absolute coordinates this overrides all other changes */
248    if (likely(this->parent != NULL))
249      this->relCoordinate = absCoord - parent->getAbsCoor ();
250    else
251      this->relCoordinate = absCoord;
252  }
253  if( this->parentMode & PNODE_ROTATE_MOVEMENT)
254  {
255    if (likely(this->parent != NULL))
256      this->relCoordinate = absCoord - parent->getAbsCoor ();
257    else
258      this->relCoordinate = absCoord;
259  }
260
261  this->bRelCoorChanged = true;
262//  this->absCoordinate = absCoord;
[3675]263}
264
[5382]265
[3675]266/**
[4836]267 * @param x x-coordinate.
268 * @param y y-coordinate.
269 * @param z z-coordinate.
[4987]270 * @see void PNode::setAbsCoor (const Vector& absCoord)
[4610]271 */
272void PNode::setAbsCoor(float x, float y, float z)
273{
274  this->setAbsCoor(Vector(x, y, z));
275}
276
[6054]277
[4610]278/**
[5406]279 * @param absCoord set absolute coordinate
280 * @todo check off
281 */
282void PNode::setAbsCoorSoft (const Vector& absCoordSoft, float bias)
283{
284  if (this->toCoordinate == NULL)
285    this->toCoordinate = new Vector;
286
287  if( likely(this->parentMode & PNODE_MOVEMENT))
288  {
289      /* if you have set the absolute coordinates this overrides all other changes */
290    if (likely(this->parent != NULL))
291      *this->toCoordinate = absCoordSoft - parent->getAbsCoor ();
292    else
293      *this->toCoordinate = absCoordSoft;
294  }
295  if( this->parentMode & PNODE_ROTATE_MOVEMENT)
296  {
297    if (likely(this->parent != NULL))
298      *this->toCoordinate = absCoordSoft - parent->getAbsCoor ();
299    else
300      *this->toCoordinate = absCoordSoft;
301  }
[6616]302  this->toStep = 0.0f;
[5406]303}
304
305
306/**
[6048]307 * @brief shift coordinate relative
[4836]308 * @param shift shift vector
[4987]309 *
[5420]310 * this function shifts the current coordinates about the vector shift. this is
311 * usefull because from some place else you can:
312 * PNode* someNode = ...;
313 * Vector objectMovement = calculateShift();
314 * someNode->shiftCoor(objectMovement);
315 *
316 * this is the internal method of:
317 * PNode* someNode = ...;
318 * Vector objectMovement = calculateShift();
319 * Vector currentCoor = someNode->getRelCoor();
320 * Vector newCoor = currentCoor + objectMovement;
321 * someNode->setRelCoor(newCoor);
322 *
[5382]323 */
[3809]324void PNode::shiftCoor (const Vector& shift)
[3683]325{
[4993]326  this->relCoordinate += shift;
327  this->bRelCoorChanged = true;
[3683]328}
329
[6054]330
[3683]331/**
[6048]332 * @brief set relative direction
[4836]333 * @param relDir to its parent
[5091]334 */
[3810]335void PNode::setRelDir (const Quaternion& relDir)
[3675]336{
[5113]337  if (this->toDirection!= NULL)
338  {
339    delete this->toDirection;
340    this->toDirection = NULL;
341  }
[4993]342  this->relDirection = relDir;
[5819]343
[3675]344  this->bRelCoorChanged = true;
345}
346
[6054]347
[3365]348/**
[4771]349 * @see void PNode::setRelDir (const Quaternion& relDir)
350 * @param x the x direction
351 * @param y the y direction
352 * @param z the z direction
353 *
354 * main difference is, that here you give a directional vector, that will be translated into a Quaternion
355 */
356void PNode::setRelDir (float x, float y, float z)
357{
358  this->setRelDir(Quaternion(Vector(x,y,z), Vector(0,1,0)));
359}
360
[4990]361
[4771]362/**
[6048]363 * @brief sets the Relative Direction of this node to its parent in a Smoothed way
[4990]364 * @param relDirSoft the direction to iterate to smoothely.
[4992]365 * @param bias how fast to iterate to the new Direction
[4990]366 */
[4992]367void PNode::setRelDirSoft(const Quaternion& relDirSoft, float bias)
[4990]368{
369  if (likely(this->toDirection == NULL))
370    this->toDirection = new Quaternion();
371
372  *this->toDirection = relDirSoft;
[4992]373  this->bias = bias;
[6616]374  this->toStep = 0.0f;
[5819]375  this->bRelDirChanged = true;
[4990]376}
377
[6054]378
[4990]379/**
380 * @see void PNode::setRelDirSoft (const Quaternion& relDir)
381 * @param x the x direction
382 * @param y the y direction
383 * @param z the z direction
384 *
385 * main difference is, that here you give a directional vector, that will be translated into a Quaternion
386 */
[4992]387void PNode::setRelDirSoft(float x, float y, float z, float bias)
[4990]388{
[4992]389  this->setRelDirSoft(Quaternion(Vector(x,y,z), Vector(0,1,0)), bias);
[4990]390}
391
[6054]392
[4990]393/**
[6048]394 * @brief sets the absolute direction
[4836]395 * @param absDir absolute coordinates
[5091]396 */
[3810]397void PNode::setAbsDir (const Quaternion& absDir)
[3675]398{
[5113]399  if (this->toDirection!= NULL)
400  {
401    delete this->toDirection;
402    this->toDirection = NULL;
403  }
404
[5001]405  if (likely(this->parent != NULL))
406    this->relDirection = absDir / this->parent->getAbsDir();
[4996]407  else
[5001]408   this->relDirection = absDir;
[4993]409
410  this->bRelDirChanged = true;
[3675]411}
412
[6054]413
[3675]414/**
[4771]415 * @see void PNode::setAbsDir (const Quaternion& relDir)
416 * @param x the x direction
417 * @param y the y direction
418 * @param z the z direction
419 *
420 * main difference is, that here you give a directional vector, that will be translated into a Quaternion
421 */
422void PNode::setAbsDir (float x, float y, float z)
423{
424  this->setAbsDir(Quaternion(Vector(x,y,z), Vector(0,1,0)));
425}
426
[6054]427
[4771]428/**
[6048]429 * @brief sets the absolute direction
[5414]430 * @param absDir absolute coordinates
[6048]431 * @param bias how fast to iterator to the new Position
[5414]432 */
433void PNode::setAbsDirSoft (const Quaternion& absDirSoft, float bias)
434{
435  if (this->toDirection == NULL)
436    this->toDirection = new Quaternion();
437
438  if (likely(this->parent != NULL))
439    *this->toDirection = absDirSoft / this->parent->getAbsDir();
440  else
441   *this->toDirection = absDirSoft;
442
443  this->bias = bias;
[6616]444  this->toStep = 0.0f;
[5915]445  this->bRelDirChanged = true;
[5414]446}
447
[6054]448
[5414]449/**
450 * @see void PNode::setAbsDir (const Quaternion& relDir)
451 * @param x the x direction
452 * @param y the y direction
453 * @param z the z direction
454 *
455 * main difference is, that here you give a directional vector, that will be translated into a Quaternion
456 */
457void PNode::setAbsDirSoft (float x, float y, float z, float bias)
458{
459  this->setAbsDirSoft(Quaternion(Vector(x,y,z), Vector(0,1,0)), bias);
460}
461
462
463/**
[6048]464 * @brief shift Direction
[5091]465 * @param shift the direction around which to shift.
466 */
[3802]467void PNode::shiftDir (const Quaternion& shift)
468{
[4993]469  this->relDirection = this->relDirection * shift;
[3802]470  this->bRelDirChanged = true;
471}
[3365]472
[6048]473
[3683]474/**
[6048]475 * @brief adds a child and makes this node to a parent
[5091]476 * @param child child reference
[4993]477 * use this to add a child to this node.
[5420]478 */
[5382]479void PNode::addChild (PNode* child)
[3365]480{
[4993]481  if( likely(child->parent != NULL))
[6078]482      child->parent->eraseChild(child);
[6075]483  if (this->checkIntegrity(child))
484  {
485    child->parent = this;
486    if (unlikely(this != NULL))
487      this->children.push_back(child);
488    child->parentCoorChanged();
489  }
490  else
491  {
492    PRINTF(1)("Tried to reparent to own child '%s::%s' to '%s::%s'.\n",
493              this->getClassName(), this->getName(), child->getClassName(), child->getName());
494    child->parent = NULL;
[6142]495    child->parentCoorChanged();
[6075]496  }
[3365]497}
498
[6048]499
[3365]500/**
[5091]501 * @see PNode::addChild(PNode* child);
[4765]502 * @param childName the name of the child to add to this PNode
503 */
504void PNode::addChild (const char* childName)
505{
506  PNode* childNode = dynamic_cast<PNode*>(ClassList::getObject(childName, CL_PARENT_NODE));
507  if (childNode != NULL)
508    this->addChild(childNode);
509}
510
[6048]511
[4765]512/**
[6048]513 * @brief removes a child from the node
[5091]514 * @param child the child to remove from this pNode.
[4993]515 *
[6054]516 * Children from pNode will not be lost, they are Reparented by the rules of the ParentMode
[5420]517 */
[4993]518void PNode::removeChild (PNode* child)
[3365]519{
[5214]520  if (child != NULL)
[5769]521   child->removeNode();
[3365]522}
523
[6054]524
[3365]525/**
[6075]526 * !! PRIVATE FUNCTION
[6299]527 * @brief reparents a node (happens on Parents Node delete or remove and Flags are set.)
[6075]528 */
529void PNode::reparent()
530{
[6078]531  if (this->parentMode & PNODE_REPARENT_TO_NULL)
532    this->setParent((PNode*)NULL);
[6075]533  else if (this->parentMode & PNODE_REPARENT_TO_PARENTS_PARENT && this->parent != NULL)
534    this->setParent(this->parent->getParent());
[6078]535  else
536    this->setParent(PNode::getNullParent());
[6075]537}
538
[6299]539/**
540 * ereases child from the nodes children
541 * @param chuld the child to remove
542 */
[6078]543void PNode::eraseChild(PNode* child)
544{
545  std::list<PNode*>::iterator childRemover = std::find(this->children.begin(), this->children.end(), child);
546  if(childRemover != this->children.end())
547    this->children.erase(childRemover);
548}
[6075]549
[6078]550
[6075]551/**
[6048]552 * @brief remove this pnode from the tree and adds all following to NullParent
[5420]553 *
554 * this can be the case, if an entity in the world is being destroyed.
555 */
[5769]556void PNode::removeNode()
[3537]557{
[6078]558  list<PNode*>::iterator child = this->children.begin();
559  list<PNode*>::iterator reparenter;
560  while (child != this->children.end())
[6054]561  {
[6078]562    reparenter = child;
563    child++;
564    if (this->parentMode & PNODE_REPARENT_CHILDREN_ON_REMOVE ||
565        (*reparenter)->parentMode & PNODE_REPARENT_ON_PARENTS_REMOVE)
[6142]566    {
567      printf("TEST----------------%s ---- %s\n", this->getClassName(), (*reparenter)->getClassName());
[6054]568      (*reparenter)->reparent();
[6078]569      printf("REPARENTED TO: %s::%s\n",(*reparenter)->getParent()->getClassName(),(*reparenter)->getParent()->getName());
[6054]570    }
571  }
[5214]572  if (this->parent != NULL)
[6142]573  {
[6078]574    this->parent->eraseChild(this);
[6142]575    this->parent = NULL;
576  }
[3537]577}
578
[3365]579
[4761]580/**
581 * @see PNode::setParent(PNode* parent);
582 * @param parentName the name of the Parent to set to this PNode
583 */
584void PNode::setParent (const char* parentName)
585{
586  PNode* parentNode = dynamic_cast<PNode*>(ClassList::getObject(parentName, CL_PARENT_NODE));
587  if (parentNode != NULL)
588    parentNode->addChild(this);
[6075]589  else
590    PRINTF(2)("Not Found PNode's (%s::%s) new Parent by Name: %s\n",
591              this->getClassName(), this->getName(), parentName);
[4761]592}
593
[6054]594
[4990]595/**
[6048]596 * @brief does the reparenting in a very smooth way
[4990]597 * @param parentNode the new Node to connect this node to.
[4993]598 * @param bias the speed to iterate to this new Positions
[4990]599 */
[5382]600void PNode::setParentSoft(PNode* parentNode, float bias)
[4987]601{
[5382]602  // return if the new parent and the old one match
[6075]603  if (this->parent == parentNode )
[4992]604    return;
[6075]605  if (parentNode == NULL)
606    parentNode = PNode::getNullParent();
[4992]607
[5382]608  // store the Valures to iterate to.
[4993]609  if (likely(this->toCoordinate == NULL))
[4989]610  {
[4993]611    this->toCoordinate = new Vector();
612    *this->toCoordinate = this->getRelCoor();
[4989]613  }
[4990]614  if (likely(this->toDirection == NULL))
615  {
616    this->toDirection = new Quaternion();
617    *this->toDirection = this->getRelDir();
618  }
[4992]619  this->bias = bias;
[6616]620  this->toStep = 0.0f;
[4987]621
[4990]622  Vector tmpV = this->getAbsCoor();
623  Quaternion tmpQ = this->getAbsDir();
[4987]624
625  parentNode->addChild(this);
626
[5382]627 if (this->parentMode & PNODE_ROTATE_MOVEMENT && this->parent != NULL)
628   this->relCoordinate = this->parent->getAbsDir().inverse().apply(tmpV - this->parent->getAbsCoor());
[5000]629 else
[5382]630   this->relCoordinate = tmpV - parentNode->getAbsCoor();
[4991]631
[5382]632 this->relDirection = tmpQ / parentNode->getAbsDir();
[4987]633}
634
[6054]635
[4993]636/**
[6048]637 * @brief does the reparenting in a very smooth way
[4993]638 * @param parentName the name of the Parent to reconnect to
639 * @param bias the speed to iterate to this new Positions
640 */
[5382]641void PNode::setParentSoft(const char* parentName, float bias)
[4987]642{
643  PNode* parentNode = dynamic_cast<PNode*>(ClassList::getObject(parentName, CL_PARENT_NODE));
644  if (parentNode != NULL)
[5382]645    this->setParentSoft(parentNode, bias);
[4987]646}
647
[6054]648/**
649 * @param parentMode sets the parentingMode of this Node
650 */
651void PNode::setParentMode(PARENT_MODE parentMode)
652{
[6307]653  this->parentMode = ((this->parentMode & 0xfff0) | parentMode);
[6054]654}
[6048]655
[3365]656/**
[6048]657 * @brief sets the mode of this parent manually
[4765]658 * @param parentMode a String representing this parentingMode
659 */
660void PNode::setParentMode (const char* parentingMode)
661{
[4993]662  this->setParentMode(PNode::charToParentingMode(parentingMode));
[4765]663}
[3537]664
[3365]665/**
[6075]666 * @brief adds special mode Flags to this PNode
667 * @see PARENT_MODE
668 * @param nodeFlags a compsition of PARENT_MODE-flags, split by the '|' (or) operator.
[6054]669 */
[6078]670void PNode::addNodeFlags(unsigned short nodeFlags)
[6054]671{
672  this->parentMode |= nodeFlags;
673}
674
[6075]675/**
676 * @brief removes special mode Flags to this PNode
677 * @see PARENT_MODE
678 * @param nodeFlags a compsition of PARENT_MODE-flags, split by the '|' (or) operator.
679 */
[6078]680void PNode::removeNodeFlags(unsigned short nodeFlags)
[6054]681{
682  this->parentMode &= !nodeFlags;
683}
684
[6078]685/**
686 * @returns the NullParent (and if needed creates it)
687 */
688PNode* PNode::createNullParent()
689{
690  if (likely(PNode::nullParent == NULL))
691  {
692    PNode::nullParent = new PNode(NULL, PNODE_PARENT_MODE_DEFAULT | PNODE_REPARENT_TO_NULL);
693    PNode::nullParent->setName("NullParent");
694  }
695  return PNode::nullParent;
696}
[6054]697
[6078]698
[6054]699/**
[6075]700 * !! PRIVATE FUNCTION
701 * @brief checks the upward integrity (e.g if PNode is somewhere up the Node tree.)
702 * @param checkParent the Parent to check.
703 * @returns true if the integrity-check succeeds, false otherwise.
704 *
705 * If there is a second occurence of checkParent before NULL, then a loop could get
706 * into the Tree, and we do not want this.
707 */
708bool PNode::checkIntegrity(const PNode* checkParent) const
709{
710  const PNode* parent = this;
711  while ( (parent = parent->getParent()) != NULL)
712    if (unlikely(parent == checkParent))
713      return false;
714  return true;
715}
716
717
718/**
[6048]719 * @brief updates the absCoordinate/absDirection
[4836]720 * @param dt The time passed since the last update
[5420]721 *
722 * this is used to go through the parent-tree to update all the absolute coordinates
723 * and directions. this update should be done by the engine, so you don't have to
724 * worry, normaly...
725 */
[5769]726void PNode::updateNode (float dt)
[3365]727{
[6075]728  if (!(this->parentMode & PNODE_STATIC_NODE))
729  {
730    if( likely(this->parent != NULL))
[4440]731    {
[6054]732        // movement for nodes with smoothMove enabled
733        if (unlikely(this->toCoordinate != NULL))
[4987]734        {
[6054]735          Vector moveVect = (*this->toCoordinate - this->relCoordinate) *fabsf(dt)*bias;
736          if (likely(moveVect.len() >= PNODE_ITERATION_DELTA))
737          {
738            this->shiftCoor(moveVect);
739          }
740          else
741          {
742            delete this->toCoordinate;
743            this->toCoordinate = NULL;
744            PRINTF(5)("SmoothMove of %s finished\n", this->getName());
745          }
[4987]746        }
[6054]747        if (unlikely(this->toDirection != NULL))
[4987]748        {
[6616]749          this->toStep += fabs(dt) * bias;
[6624]750          //printf("%s::%s %f\n", this->getClassName(), this->getName(), this->toStep );
[6616]751          Quaternion rotQuat = Quaternion::quatSlerp(this->relDirection,*this->toDirection, toStep);
[6054]752          if (this->relDirection.distance(rotQuat) >PNODE_ITERATION_DELTA)
753          {
754            this->relDirection = rotQuat;
755            this->bRelDirChanged;
756          }
757          else
758          {
759            delete this->toDirection;
760            this->toDirection = NULL;
[6075]761            PRINTF(5)("SmoothRotate of %s finished\n", this->getName());
[6054]762          }
[4987]763        }
[4990]764
[6054]765        // MAIN UPDATE /////////////////////////////////////
766        this->lastAbsCoordinate = this->absCoordinate;
[4145]767
[6075]768        PRINTF(5)("PNode::update - '%s::%s' - (%f, %f, %f)\n", this->getClassName(), this->getName(),
769                      this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z);
[3800]770
[4570]771
[6307]772        if(this->bRelDirChanged && this->parentMode & PNODE_LOCAL_ROTATE )
[6054]773        {
774          /* update the current absDirection - remember * means rotation around sth.*/
775          this->prevRelCoordinate = this->relCoordinate;
[6253]776          this->absDirection = parent->getAbsDir() * this->relDirection;
[6054]777        }
[4570]778
[6307]779        if(likely(this->bRelCoorChanged && this->parentMode & PNODE_MOVEMENT))
[6054]780        {
[6075]781        /* update the current absCoordinate */
[6307]782          this->prevRelCoordinate = this->relCoordinate;
783          this->absCoordinate = this->parent->getAbsCoor() + this->relCoordinate;
[6054]784        }
785        else if( this->parentMode & PNODE_ROTATE_MOVEMENT && this->bRelCoorChanged)
786        {
787          /* update the current absCoordinate */
[6307]788          this->prevRelCoordinate = this->relCoordinate;
789          this->absCoordinate = this->parent->getAbsCoor() + parent->getAbsDir().apply(this->relCoordinate);
[6054]790        }
791        /////////////////////////////////////////////////
[5007]792      }
[6075]793
794  else // Nodes without a Parent are handled faster :: MOST LIKELY THE NULLPARENT
[4440]795    {
[6075]796      PRINTF(4)("update ParentLess Node (%s::%s) - (%f, %f, %f)\n", this->getClassName(), this->getName(),
797                this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z);
[6054]798        if (this->bRelCoorChanged)
799        {
800          this->prevRelCoordinate = this->relCoordinate;
801          this->absCoordinate = this->relCoordinate;
802        }
803        if (this->bRelDirChanged)
804        {
805          this->prevRelDirection = this->relDirection;
806          this->absDirection = this->getAbsDir () * this->relDirection;
807        }
[5118]808      }
[4993]809    }
[3365]810
[6054]811    if(!this->children.empty() && (this->bActive || this->parentMode & PNODE_UPDATE_CHILDREN_IF_INACTIVE ))
[4993]812    {
[5770]813      list<PNode*>::iterator child;
814      for (child = this->children.begin(); child != this->children.end(); child ++)
[4574]815      {
816        /* if this node has changed, make sure, that all children are updated also */
[4993]817        if( likely(this->bRelCoorChanged))
[5770]818          (*child)->parentCoorChanged ();
[4993]819        if( likely(this->bRelDirChanged))
[5770]820          (*child)->parentDirChanged ();
[4993]821
[5770]822        (*child)->updateNode(dt);
[4993]823      }
[4440]824    }
[4993]825    this->velocity = (this->absCoordinate - this->lastAbsCoordinate) / dt;
826    this->bRelCoorChanged = false;
827    this->bRelDirChanged = false;
[3365]828}
829
[5769]830
[6075]831
832
833
834/*************
835 * DEBUGGING *
836 *************/
[6048]837/**
838 * @brief counts total amount the children walking through the entire tree.
839 * @param nodes the counter
840 */
[5769]841void PNode::countChildNodes(int& nodes) const
842{
843  nodes++;
[5770]844  list<PNode*>::const_iterator child;
845  for (child = this->children.begin(); child != this->children.end(); child ++)
846    (*child)->countChildNodes(nodes);
[5769]847}
848
849
[3450]850/**
[6048]851 * @brief displays some information about this pNode
[4836]852 * @param depth The deph into which to debug the children of this PNode to.
[5383]853 * (0: all children will be debugged, 1: only this PNode, 2: this and direct children, ...)
854 * @param level !! INTERNAL !! The n-th level of the Node we draw (this is internal and only for nice output).
[5420]855 */
[5769]856void PNode::debugNode(unsigned int depth, unsigned int level) const
[3365]857{
[4574]858  for (unsigned int i = 0; i < level; i++)
[4575]859    PRINT(0)(" |");
[5770]860  if (this->children.size() > 0)
[4575]861    PRINT(0)(" +");
862  else
863    PRINT(0)(" -");
[5769]864
865  int childNodeCount = 0;
866  this->countChildNodes(childNodeCount);
867
868  PRINT(0)("PNode(%s::%s) - absCoord: (%0.2f, %0.2f, %0.2f), relCoord(%0.2f, %0.2f, %0.2f), direction(%0.2f, %0.2f, %0.2f) - %s - %d childs\n",
[4574]869           this->getClassName(),
870           this->getName(),
871           this->absCoordinate.x,
872           this->absCoordinate.y,
873           this->absCoordinate.z,
874           this->relCoordinate.x,
875           this->relCoordinate.y,
[4993]876           this->relCoordinate.z,
[4996]877           this->getAbsDirV().x,
878           this->getAbsDirV().y,
879           this->getAbsDirV().z,
[5769]880           this->parentingModeToChar(parentMode),
881           childNodeCount);
[4574]882  if (depth >= 2 || depth == 0)
883  {
[5770]884    list<PNode*>::const_iterator child;
885    for (child = this->children.begin(); child != this->children.end(); child ++)
[4574]886    {
887      if (depth == 0)
[5770]888        (*child)->debugNode(0, level + 1);
[4574]889      else
[5770]890        (*child)->debugNode(depth - 1, level +1);
[4574]891    }
892  }
[3365]893}
894
[4570]895/**
[6048]896 * @brief displays the PNode at its position with its rotation as a cube.
[5383]897 * @param  depth The deph into which to debug the children of this PNode to.
898 * (0: all children will be displayed, 1: only this PNode, 2: this and direct children, ...)
899 * @param size the Size of the Box to draw.
900 * @param color the color of the Box to display.
901 * @param level !! INTERNAL !! The n-th level of the Node we draw (this is internal and only for nice output).
902 */
903void PNode::debugDraw(unsigned int depth, float size, const Vector& color, unsigned int level) const
[4570]904{
[5432]905  // if this is the first Element we draw
[5383]906  if (level == 0)
907  {
[5432]908    glPushAttrib(GL_ENABLE_BIT); // save the Enable-attributes
909    glMatrixMode(GL_MODELVIEW);  // goto the ModelView Matrix
[5383]910
[5432]911    glDisable(GL_LIGHTING);      // disable lighting (we do not need them for just lighting)
912    glDisable(GL_BLEND);         // ''
913    glDisable(GL_TEXTURE_2D);    // ''
[5438]914    glDisable(GL_DEPTH_TEST);    // ''
[5383]915  }
916
[5432]917  glPushMatrix();                // repush the Matrix-stack
[4570]918  /* translate */
919  glTranslatef (this->getAbsCoor ().x,
920                this->getAbsCoor ().y,
921                this->getAbsCoor ().z);
[4998]922//  this->getAbsDir ().matrix (matrix);
923
[5432]924  /* rotate */
[4998]925  Vector tmpRot = this->getAbsDir().getSpacialAxis();
[5432]926  glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
927  /* set the new Color */
[5008]928  glColor3f(color.x, color.y, color.z);
[5432]929  { /* draw a cube of size size */
[4570]930    glBegin(GL_LINE_STRIP);
[4995]931    glVertex3f(-.5*size, -.5*size,  -.5*size);
932    glVertex3f(+.5*size, -.5*size,  -.5*size);
933    glVertex3f(+.5*size, -.5*size,  +.5*size);
934    glVertex3f(-.5*size, -.5*size,  +.5*size);
935    glVertex3f(-.5*size, -.5*size,  -.5*size);
[4570]936    glEnd();
937    glBegin(GL_LINE_STRIP);
[4995]938    glVertex3f(-.5*size, +.5*size,  -.5*size);
939    glVertex3f(+.5*size, +.5*size,  -.5*size);
940    glVertex3f(+.5*size, +.5*size,  +.5*size);
941    glVertex3f(-.5*size, +.5*size,  +.5*size);
942    glVertex3f(-.5*size, +.5*size,  -.5*size);
[4570]943    glEnd();
[4995]944
[4570]945    glBegin(GL_LINES);
[4995]946    glVertex3f(-.5*size, -.5*size,  -.5*size);
947    glVertex3f(-.5*size, +.5*size,  -.5*size);
948    glVertex3f(+.5*size, -.5*size,  -.5*size);
949    glVertex3f(+.5*size, +.5*size,  -.5*size);
950    glVertex3f(+.5*size, -.5*size,  +.5*size);
951    glVertex3f(+.5*size, +.5*size,  +.5*size);
952    glVertex3f(-.5*size, -.5*size,  +.5*size);
953    glVertex3f(-.5*size, +.5*size,  +.5*size);
[4570]954    glEnd();
955  }
956
957  glPopMatrix();
[5007]958  if (depth >= 2 || depth == 0)
959  {
[5432]960    /* rotate the current color in HSV space around 20 degree */
[5115]961    Vector childColor =  Color::HSVtoRGB(Color::RGBtoHSV(color)+Vector(20,0,.0));
[5770]962    list<PNode*>::const_iterator child;
963    for (child = this->children.begin(); child != this->children.end(); child ++)
[5007]964    {
[5394]965      // drawing the Dependency graph
[6074]966     if (this != PNode::getNullParent())
[5394]967      {
968       glBegin(GL_LINES);
969       glColor3f(color.x, color.y, color.z);
970       glVertex3f(this->getAbsCoor ().x,
971                  this->getAbsCoor ().y,
972                  this->getAbsCoor ().z);
973        glColor3f(childColor.x, childColor.y, childColor.z);
[5770]974        glVertex3f((*child)->getAbsCoor ().x,
975                   (*child)->getAbsCoor ().y,
976                   (*child)->getAbsCoor ().z);
[5394]977        glEnd();
978      }
[5915]979
[5432]980      /* if we want to draw the children too */
981      if (depth == 0) /* -> all of them */
[5770]982        (*child)->debugDraw(0, size, childColor, level+1);
[5432]983      else            /* -> only the Next one */
[5770]984        (*child)->debugDraw(depth - 1, size, childColor, level +1);
[5007]985    }
986  }
[5383]987  if (level == 0)
[5432]988    glPopAttrib(); /* pop the saved attributes back out */
[4570]989}
[4993]990
991
992
993/////////////////////
994// HELPER_FUCTIONS //
995/////////////////////
996
997/**
[6048]998 * @brief converts a parentingMode into a string that is the name of it
[4993]999 * @param parentingMode the ParentingMode to convert
1000 * @return the converted string
1001 */
1002const char* PNode::parentingModeToChar(int parentingMode)
1003{
1004  if (parentingMode == PNODE_LOCAL_ROTATE)
1005    return "local-rotate";
1006  else if (parentingMode == PNODE_ROTATE_MOVEMENT)
1007    return "rotate-movement";
1008  else if (parentingMode == PNODE_MOVEMENT)
1009    return "movement";
1010  else if (parentingMode == PNODE_ALL)
1011    return "all";
1012  else if (parentingMode == PNODE_ROTATE_AND_MOVE)
1013    return "rotate-and-move";
1014}
1015
1016/**
[6048]1017 * @brief converts a parenting-mode-string into a int
[4993]1018 * @param parentingMode the string naming the parentingMode
1019 * @return the int corresponding to the named parentingMode
1020 */
1021PARENT_MODE PNode::charToParentingMode(const char* parentingMode)
1022{
1023  if (!strcmp(parentingMode, "local-rotate"))
1024    return (PNODE_LOCAL_ROTATE);
1025  else  if (!strcmp(parentingMode, "rotate-movement"))
1026    return (PNODE_ROTATE_MOVEMENT);
1027  else  if (!strcmp(parentingMode, "movement"))
1028    return (PNODE_MOVEMENT);
1029  else  if (!strcmp(parentingMode, "all"))
1030    return (PNODE_ALL);
1031  else  if (!strcmp(parentingMode, "rotate-and-move"))
1032    return (PNODE_ROTATE_AND_MOVE);
1033}
[6341]1034
1035/**
1036 * Writes data from network containing information about the state
1037 * @param data pointer to data
1038 * @param length length of data
1039 * @param sender hostID of sender
1040 */
1041int PNode::writeState( const byte * data, int length, int sender )
1042{
1043  SYNCHELP_READ_BEGIN();
1044
1045  SYNCHELP_READ_FKT( BaseObject::writeState );
1046
1047  char * parentName = NULL;
1048  SYNCHELP_READ_STRINGM( parentName );
1049
1050  if ( strcmp(parentName, "")==0 )
1051  {
1052    setParent( (char*)NULL );
1053  }
1054  else
1055  {
1056    setParent( parentName );
1057  }
1058
1059  delete[] parentName;
1060
1061  int parentMode;
1062  SYNCHELP_READ_INT( parentMode );
1063  this->setParentMode((PARENT_MODE)parentMode);
1064
1065  float f1, f2, f3, f4;
1066
1067  SYNCHELP_READ_FLOAT( f1 );
1068  SYNCHELP_READ_FLOAT( f2 );
1069  SYNCHELP_READ_FLOAT( f3 );
1070  this->setRelCoor( f1, f2, f3 );
[6498]1071  //this->setRelCoor( 5, 0, 0 );
[6341]1072
[6498]1073  /*SYNCHELP_READ_FLOAT( f1 );
[6341]1074  SYNCHELP_READ_FLOAT( f2 );
[6498]1075  SYNCHELP_READ_FLOAT( f3 );*/
[6341]1076  //this->setAbsCoor( f1, f2, f3 );
1077
1078  SYNCHELP_READ_FLOAT( f1 );
1079  SYNCHELP_READ_FLOAT( f2 );
1080  SYNCHELP_READ_FLOAT( f3 );
1081  SYNCHELP_READ_FLOAT( f4 );
1082  this->setRelDir( Quaternion( Vector(f2, f3, f4), f1 ) );
1083
[6498]1084  /*SYNCHELP_READ_FLOAT( f1 );
[6341]1085  SYNCHELP_READ_FLOAT( f2 );
1086  SYNCHELP_READ_FLOAT( f3 );
[6498]1087  SYNCHELP_READ_FLOAT( f4 );*/
[6341]1088  //this->setAbsDir( Quaternion( Vector(f2, f3, f4), f1 ) );
1089
1090  int n;
1091  char * childName;
1092
1093  SYNCHELP_READ_INT( n );
1094
1095  for (int i = 0; i<n; i++)
1096  {
1097    SYNCHELP_READ_STRINGM( childName );
[6498]1098    //PRINTF(0)("childname = %s\n", childName);
[6341]1099    addChild( childName );
1100    delete childName;
1101    childName = NULL;
1102  }
1103
1104  return SYNCHELP_READ_N;
1105}
1106
1107/**
1108 * data copied in data will bee sent to another host
1109 * @param data pointer to data
1110 * @param maxLength max length of data
1111 * @return the number of bytes writen
1112 */
1113int PNode::readState( byte * data, int maxLength )
1114{
1115  SYNCHELP_WRITE_BEGIN();
1116
1117  SYNCHELP_WRITE_FKT( BaseObject::readState );
1118
1119  if ( this->parent )
1120  {
1121    SYNCHELP_WRITE_STRING( parent->getName() );
1122  }
1123  else
1124  {
1125    SYNCHELP_WRITE_STRING( "" );
1126  }
1127
1128  SYNCHELP_WRITE_INT( this->parentMode );
1129
1130  SYNCHELP_WRITE_FLOAT( this->relCoordinate.x );
1131  SYNCHELP_WRITE_FLOAT( this->relCoordinate.y );
1132  SYNCHELP_WRITE_FLOAT( this->relCoordinate.z );
1133
[6498]1134  //PRINTF(0)("%s, %f, %f, %f\n", getClassName(), relCoordinate.x, relCoordinate.y, relCoordinate.z);
[6341]1135
[6498]1136  /*SYNCHELP_WRITE_FLOAT( this->absCoordinate.x );
[6341]1137  SYNCHELP_WRITE_FLOAT( this->absCoordinate.y );
[6498]1138  SYNCHELP_WRITE_FLOAT( this->absCoordinate.z );*/
[6341]1139
[6498]1140  //PRINTF(0)("%s, %f, %f, %f\n", getClassName(), absCoordinate.x, absCoordinate.y, absCoordinate.z);
[6341]1141
1142  SYNCHELP_WRITE_FLOAT( this->relDirection.w );
1143  SYNCHELP_WRITE_FLOAT( this->relDirection.v.x );
1144  SYNCHELP_WRITE_FLOAT( this->relDirection.v.y );
1145  SYNCHELP_WRITE_FLOAT( this->relDirection.v.z );
1146
[6498]1147  /*SYNCHELP_WRITE_FLOAT( this->absDirection.w );
[6341]1148  SYNCHELP_WRITE_FLOAT( this->absDirection.v.x );
1149  SYNCHELP_WRITE_FLOAT( this->absDirection.v.y );
[6498]1150  SYNCHELP_WRITE_FLOAT( this->absDirection.v.z );*/
[6341]1151
1152  int n = children.size();
1153  SYNCHELP_WRITE_INT( n );
1154
1155  for (std::list<PNode*>::const_iterator it = children.begin(); it!=children.end(); it++)
1156  {
1157    SYNCHELP_WRITE_STRING( (*it)->getName() );
1158  }
[6498]1159
[6341]1160  return SYNCHELP_WRITE_N;
1161}
Note: See TracBrowser for help on using the repository browser.