Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6461 was 6424, checked in by bensch, 19 years ago

orxonox/trunk: merged the branche network back to the trunk
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/network . -r 6351:HEAD
no conflicts

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