Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/preferences/src/lib/coord/p_node.cc @ 6483

Last change on this file since 6483 was 6341, checked in by bensch, 19 years ago

orxonox/trunk: merged the network branche back to the trunk, so we do not get away from each other to fast

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