Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/lib/coord/p_node.cc @ 7917

Last change on this file since 7917 was 7794, checked in by rennerc, 19 years ago

removed some output

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