Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/coord/p_node.h @ 6075

Last change on this file since 6075 was 6075, checked in by bensch, 18 years ago

orxonox/trunk: PNode rearange

File size: 10.6 KB
RevLine 
[4570]1/*!
[5391]2 * @file p_node.h
[6042]3 * @brief Definition of THE Parenting Node
[5391]4 *
5 *  parenting is how coordinates are handled in orxonox, meaning, that all coordinates
6 *  are representet relative to another parent node. this nodes build a parenting
7 *  tree of one-sided references (from up to down referenced).
8 *  Every node manages itself a list of childrens (of whos it is parent - easy...).
9 *
10 *  absCoordinate, absDirection have to be recalculated as soon as there was a change in
11 *  place or ortientation. this is only the case if
[6042]12 *  o bRelCoorChanged is true (so moved)
13 *  o bRelDirChanged is true (so changed)
[5391]14 *  this conditions make it cheaper to recalculate the tree (reduces redundant work).
15 */
[3246]16
17
18#ifndef _P_NODE_H
19#define _P_NODE_H
20
[3543]21#include "base_object.h"
[6042]22
[3804]23#include "vector.h"
[5770]24#include <list>
[3246]25
[5405]26// FORWARD DECLARATION
[4436]27class TiXmlElement;
[3246]28
[5006]29#define PNODE_ITERATION_DELTA    .001
[6054]30
[4765]31//! Parental linkage modes
32typedef enum
33{
[5769]34  // PARENTAL FOLLOWING
[6042]35  PNODE_LOCAL_ROTATE                   = 0x0001,    //!< Rotates all the children around their centers.
36  PNODE_ROTATE_MOVEMENT                = 0x0002,    //!< Moves all the children around the center of their parent, without the rotation around their own centers.
[4765]37
[6042]38  PNODE_MOVEMENT                       = 0x0004,    //!< Moves all children along with the parent.
39  // special linkage modes
40  PNODE_ALL                            = 0x0003,    //!< Moves all children around the center of their parent, and also rotates their centers
41  PNODE_ROTATE_AND_MOVE                = 0x0005,    //!< Rotates all children around their axis, and moves them as the Parent Moves, but does not rotate around the center of their parent.
[3565]42
[5769]43
44  // REPARENTING
[6042]45  PNODE_REPARENT_TO_NULLPARENT         = 0x0010,    //!< Reparents to the NullParent, if the Parent is Removed.
[6048]46  PNODE_REPARENT_TO_PARENTS_PARENT     = 0x0020,    //!< Reparents the Node to the parents (old) parent it the parent gets removed.
[6042]47  PNODE_REPARENT_DELETE_CHILDREN       = 0x0040,    //!< Deletes the Children of the node when This Node is Removed. (Use with care).
[6054]48  /// FIXME
49   PNODE_REPARENT_KEEP_POSITION         = 0x0080,    //!< Tries to keep the Position if the Node is reparented.
[5769]50
51
52  // DELETION
[6042]53  PNODE_PROHIBIT_CHILD_DELETE          = 0x0100,    //!< Prohibits the Children from being deleted if this Node gets deleted.
54  PNODE_PROHIBIT_DELETE_WITH_PARENT    = 0x0200,    //!< Prohibits the Node to be deleted if the Parent is. Child will be reparented according to the Repaenting-Rules
[6054]55  PNODE_REPARENT_CHILDREN_ON_REMOVE    = 0x0400,    //!< Reparents the Children of the Node if the Node gets Removed.
[5769]56
[6042]57  // VISIBILITY/ACTIVITY
58  PNODE_HIDE_CHILDREN_IF_HIDDEN        = 0x1000,    //!< Prohibits the Children from being drawn if this node isn't visible. (used for Draw))
59  PNODE_HIDE_IF_PARENT_HIDDEN          = 0x2000,    //!< Prohibits the node from being drawn if the Parent is invisible.
60  PNODE_UPDATE_CHILDREN_IF_INACTIVE    = 0x4000,    //!< Updates the Children of this Node even if the Parent is Inactive (note if this's parent is inactive children won't be updated.)
61  PNODE_STATIC_NODE                    = 0x8000,    //!< Used for nodes that do not have any moving children, and that do not move.
62
[4765]63} PARENT_MODE;
64
[3450]65//! The default mode of the translation-binding.
[6042]66#define PNODE_PARENT_MODE_DEFAULT PNODE_ALL | \
[6054]67                                  PNODE_REPARENT_CHILDREN_ON_REMOVE | \
[6042]68                                  PNODE_REPARENT_KEEP_POSITION
[3246]69
[4440]70
[3450]71//! Patent Node is a Engine to calculate the position of an Object in respect to the position of its parent.
[4382]72class PNode : virtual public BaseObject {
[3246]73 public:
[6074]74  PNode (PNode* parent = PNode::getNullParent());
[3365]75  virtual ~PNode ();
[3246]76
[4436]77  void loadParams(const TiXmlElement* root);
78
[6054]79  inline void activateNode() { this->bActive = true; };
80  inline void deactivateNode() { this->bActive = false; };
81  inline bool getNodeActiveState() { return this->bActive; };
82
[3810]83  void setRelCoor (const Vector& relCoord);
[4610]84  void setRelCoor (float x, float y, float z);
[4992]85  void setRelCoorSoft(const Vector& relCoordSoft, float bias = 1.0);
86  void setRelCoorSoft(float x, float y, float z, float bias = 1.0);
[4836]87  /** @returns the relative position */
[5050]88  inline const Vector& getRelCoor () const { return this->prevRelCoordinate; };
[5113]89  /** @returns the Relative Coordinate Destination */
[5915]90  inline const Vector& getRelCoorSoft2D() const { return (this->toCoordinate)? *this->toCoordinate : this->relCoordinate; };
[3809]91  void setAbsCoor (const Vector& absCoord);
[4610]92  void setAbsCoor (float x, float y, float z);
[5406]93  void setAbsCoorSoft(const Vector& absCoordSoft, float bias = 1.0);
94  void setAbsCoorSoft(float x, float y, float z, float bias = 1.0);
[4836]95  /** @returns the absolute position */
[4372]96  inline const Vector& getAbsCoor () const { return this->absCoordinate; };
[3809]97  void shiftCoor (const Vector& shift);
[5750]98  void shiftCoor (float x, float y, float z) { this->shiftCoor(Vector(x, y, z)); };
[3246]99
[3810]100  void setRelDir (const Quaternion& relDir);
[4771]101  void setRelDir (float x, float y, float z);
[4992]102  void setRelDirSoft(const Quaternion& relDirSoft, float bias = 1.0);
103  void setRelDirSoft(float x, float y, float z, float bias = 1.0);
[4836]104  /** @returns the relative Direction */
[5050]105  inline const Quaternion& getRelDir () const { return this->prevRelDirection; };
[5113]106  /** @returns the Relative Directional Destination */
[5915]107  inline const Quaternion& getRelDirSoft2D() const { return (this->toDirection)? *this->toDirection : this->relDirection; };
[4836]108  /** @returns a Vector pointing into the relative Direction */
[5050]109  inline Vector getRelDirV() const { return this->prevRelDirection.apply(Vector(0,1,0)); };
[3810]110  void setAbsDir (const Quaternion& absDir);
[4771]111  void setAbsDir (float x, float y, float z);
[5414]112  void setAbsDirSoft(const Quaternion& absDirSoft, float bias = 1.0);
113  void setAbsDirSoft(float x, float y, float z, float bias = 1.0);
[5915]114  void shiftDir (const Quaternion& shift);
[4836]115  /** @returns the absolute Direction */
[4372]116  inline const Quaternion& getAbsDir () const { return this->absDirection; };
[4836]117  /** @returns a Vector pointing into the absolute Direction */
[4372]118  inline Vector getAbsDirV() const { return this->absDirection.apply(Vector(0,1,0)); };
[5915]119  /** @returns A Vector pointing into the forward direction (X) of the Node */
120  inline Vector getAbsDirX() const { return this->absDirection.apply(Vector(1,0,0)); };
121  /** @returns A Vector pointing into the upward direction (Y) of the Node */
122  inline Vector getAbsDirY() const { return this->absDirection.apply(Vector(0,1,0)); };
123  /** @returns A Vector pointing into the right direction (Z) of the Node */
124  inline Vector getAbsDirZ() const { return this->absDirection.apply(Vector(0,0,1)); };
[3246]125
[4836]126  /** @returns the Speed of the Node */
[4993]127  inline float getSpeed() const { return this->velocity.len(); };
[4836]128  /** @returns the Velocity of the Node */
[4993]129  inline const Vector& getVelocity() const { return this->velocity; };
[3644]130
[4440]131
[5382]132  void addChild (PNode* child);
[4765]133  void addChild (const char* childName);
[4993]134  void removeChild (PNode* child);
[5769]135  void removeNode();
[3537]136
[6054]137  /** @param parent the new parent of this node */
[6048]138  inline void setParent (PNode* parent) { parent->addChild(this); };
[4987]139  void setParent (const char* parentName);
[4966]140  /** @returns the parent of this PNode */
[5387]141  inline PNode* getParent () const { return this->parent; };
142  /** @returns the List of Children of this PNode */
[6071]143  const std::list<PNode*>& getNodesChildren() const { return this->children; };
[4761]144
[5382]145  void setParentSoft(PNode* parentNode, float bias = 1.0);
146  void setParentSoft(const char* parentName, float bias = 1.0);
[4987]147
[6054]148  void setParentMode (PARENT_MODE parentMode);
[4765]149  void setParentMode (const char* parentingMode);
[4836]150  /** @returns the Parenting mode of this node */
[6054]151  int getParentMode() const { return 0x000f & this->parentMode; };
[3246]152
[6054]153  void addNodeModeFlags(unsigned short nodeFlags);
154  void removeNodeModeFlags(unsigned short nodeFlags);
155
[6074]156  /** @returns the NullParent, the (main) ROOT of the PNode Tree. If it does not yet exist, it will be created. */
157  static PNode* getNullParent()  { return (PNode::nullParent != NULL || (PNode::nullParent = new PNode(NULL)) != NULL)? PNode::nullParent : NULL; };
[6054]158
[5769]159  void updateNode (float dt);
[3246]160
[5769]161  void countChildNodes(int& nodes) const;
162  void debugNode (unsigned int depth = 1, unsigned int level = 0) const;
[5383]163  void debugDraw(unsigned int depth = 1, float size = 1.0, const Vector& color = Vector(1, 0, 0), unsigned int level = 0) const;
[3365]164
[4993]165
166  // helper functions //
167  static const char* parentingModeToChar(int parentingMode);
168  static PARENT_MODE charToParentingMode(const char* parentingMode);
[5750]169
170
[4440]171 private:
[6073]172  void init();
[4987]173  /** tells the child that the parent's Coordinate has changed */
[4440]174  inline void parentCoorChanged () { this->bRelCoorChanged = true; }
[4987]175  /** tells the child that the parent's Direction has changed */
[4440]176  inline void parentDirChanged () { this->bRelDirChanged = true; }
[4836]177  /** @returns the last calculated coordinate */
[4993]178  inline Vector getLastAbsCoor() { return this->lastAbsCoordinate; }
[3246]179
[6048]180  void reparent();
[6075]181  bool checkIntegrity(const PNode* checkParent) const;
[3537]182
[3644]183 private:
[5770]184  bool               bRelCoorChanged;    //!< If Relative Coordinate has changed since last time we checked
185  bool               bRelDirChanged;     //!< If Relative Direction has changed since last time we checked
[3644]186
[5770]187  Vector             relCoordinate;      //!< coordinates relative to the parent
188  Vector             absCoordinate;      //!< absolute coordinates in the world ( from (0,0,0) )
189  Quaternion         relDirection;       //!< direction relative to the parent
190  Quaternion         absDirection;       //!< absolute direvtion in the world ( from (0,0,1) )
[4440]191
[5770]192  Vector             prevRelCoordinate;  //!< The last Relative Coordinate from the last update-Cycle.
193  Vector             lastAbsCoordinate;  //!< this is used for speedcalculation, it stores the last coordinate
194  Quaternion         prevRelDirection;   //!< The last Relative Direciton from the last update-Cycle.
195//  Quaternion         lastAbsDirection;
[4440]196
[5770]197  Vector             velocity;           //!< Saves the velocity.
[4987]198
[5770]199  Vector*            toCoordinate;       //!< a position to which to iterate. (This is used in conjunction with setParentSoft.and set*CoorSoft)
200  Quaternion*        toDirection;        //!< a direction to which to iterate. (This is used in conjunction with setParentSoft and set*DirSoft)
201  float              bias;               //!< how fast to iterate to the given position (default is 1)
[4987]202
[6042]203  PNode*             parent;             //!< a pointer to the parent node.
204  std::list<PNode*>  children;           //!< list of the children of this PNode.
[4440]205
[6042]206  bool               bActive;            //!< If the Node is Active (for cutting off the rest of the tree in update).
207  unsigned short     parentMode;         //!< the mode of the binding
[6074]208
209
210  static PNode*      nullParent;         //!< The ROOT of the main PNode Tree.
[3246]211};
212
213#endif /* _P_NODE_H */
Note: See TracBrowser for help on using the repository browser.