Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8680 was 8490, checked in by patrick, 18 years ago

merged the bsp branche back to trunk

File size: 12.0 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"
[6341]22#include "stdincl.h"
[6498]23#include "synchronizeable.h"
[6042]24
[3804]25#include "vector.h"
[6616]26#include "quaternion.h"
[5770]27#include <list>
[3246]28
[5405]29// FORWARD DECLARATION
[4436]30class TiXmlElement;
[3246]31
[5006]32#define PNODE_ITERATION_DELTA    .001
[6054]33
[4765]34//! Parental linkage modes
35typedef enum
36{
[5769]37  // PARENTAL FOLLOWING
[6042]38  PNODE_LOCAL_ROTATE                   = 0x0001,    //!< Rotates all the children around their centers.
39  PNODE_ROTATE_MOVEMENT                = 0x0002,    //!< Moves all the children around the center of their parent, without the rotation around their own centers.
[4765]40
[6042]41  PNODE_MOVEMENT                       = 0x0004,    //!< Moves all children along with the parent.
42  // special linkage modes
43  PNODE_ALL                            = 0x0003,    //!< Moves all children around the center of their parent, and also rotates their centers
44  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]45
[5769]46
47  // REPARENTING
[6078]48  PNODE_REPARENT_TO_NULL               = 0x0010,    //!< Reparents to the Null, if the Parent is Removed. Meaning the Node wont have a parent anymore.
[6048]49  PNODE_REPARENT_TO_PARENTS_PARENT     = 0x0020,    //!< Reparents the Node to the parents (old) parent it the parent gets removed.
[6078]50  /////////////////////////////////////////////     //  ELSE: Reparents to the NullParent.
[6042]51  PNODE_REPARENT_DELETE_CHILDREN       = 0x0040,    //!< Deletes the Children of the node when This Node is Removed. (Use with care).
[6054]52  /// FIXME
53   PNODE_REPARENT_KEEP_POSITION         = 0x0080,    //!< Tries to keep the Position if the Node is reparented.
[5769]54
55
56  // DELETION
[6042]57  PNODE_PROHIBIT_CHILD_DELETE          = 0x0100,    //!< Prohibits the Children from being deleted if this Node gets deleted.
58  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]59  PNODE_REPARENT_CHILDREN_ON_REMOVE    = 0x0400,    //!< Reparents the Children of the Node if the Node gets Removed.
[6078]60  PNODE_REPARENT_ON_PARENTS_REMOVE     = 0x0800,    //!< The Node gets Reparented if its Parent gets removed. Child will be reparented according to the Reparenting-Rules.
[5769]61
[6042]62  // VISIBILITY/ACTIVITY
63  PNODE_HIDE_CHILDREN_IF_HIDDEN        = 0x1000,    //!< Prohibits the Children from being drawn if this node isn't visible. (used for Draw))
64  PNODE_HIDE_IF_PARENT_HIDDEN          = 0x2000,    //!< Prohibits the node from being drawn if the Parent is invisible.
65  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.)
66  PNODE_STATIC_NODE                    = 0x8000,    //!< Used for nodes that do not have any moving children, and that do not move.
67
[4765]68} PARENT_MODE;
69
[3450]70//! The default mode of the translation-binding.
[6042]71#define PNODE_PARENT_MODE_DEFAULT PNODE_ALL | \
72                                  PNODE_REPARENT_KEEP_POSITION
[3246]73
[4440]74
[3450]75//! Patent Node is a Engine to calculate the position of an Object in respect to the position of its parent.
[6498]76class PNode : virtual public BaseObject, virtual public Synchronizeable {
[3246]77 public:
[6078]78  PNode (PNode* parent = PNode::getNullParent(), long nodeFlags = PNODE_PARENT_MODE_DEFAULT);
[3365]79  virtual ~PNode ();
[3246]80
[6512]81  virtual void loadParams(const TiXmlElement* root);
[4436]82
[6424]83  void init();
84
[6078]85  // ACTIVATION //
[6142]86  inline void activateNode() { this->bActive = this->bRelCoorChanged = this->bRelDirChanged = true; };
[6054]87  inline void deactivateNode() { this->bActive = false; };
88  inline bool getNodeActiveState() { return this->bActive; };
89
[6078]90  // POSITION //
[3810]91  void setRelCoor (const Vector& relCoord);
[4610]92  void setRelCoor (float x, float y, float z);
[4992]93  void setRelCoorSoft(const Vector& relCoordSoft, float bias = 1.0);
94  void setRelCoorSoft(float x, float y, float z, float bias = 1.0);
[4836]95  /** @returns the relative position */
[5050]96  inline const Vector& getRelCoor () const { return this->prevRelCoordinate; };
[5113]97  /** @returns the Relative Coordinate Destination */
[5915]98  inline const Vector& getRelCoorSoft2D() const { return (this->toCoordinate)? *this->toCoordinate : this->relCoordinate; };
[3809]99  void setAbsCoor (const Vector& absCoord);
[4610]100  void setAbsCoor (float x, float y, float z);
[5406]101  void setAbsCoorSoft(const Vector& absCoordSoft, float bias = 1.0);
102  void setAbsCoorSoft(float x, float y, float z, float bias = 1.0);
[4836]103  /** @returns the absolute position */
[4372]104  inline const Vector& getAbsCoor () const { return this->absCoordinate; };
[8490]105  /** @returns the absolute position */
106  inline const Vector& getLastAbsCoor () const { return this->lastAbsCoordinate; };
[3809]107  void shiftCoor (const Vector& shift);
[5750]108  void shiftCoor (float x, float y, float z) { this->shiftCoor(Vector(x, y, z)); };
[3246]109
[6078]110  // SPEED //
111  /** @returns the Speed of the Node */
112  inline float getSpeed() const { return this->velocity.len(); };
113  /** @returns the Velocity of the Node */
114  inline const Vector& getVelocity() const { return this->velocity; };
115
116
117  // ROTATION //
[3810]118  void setRelDir (const Quaternion& relDir);
[6873]119  void setRelDir (float angle, float x, float y, float z);
[4992]120  void setRelDirSoft(const Quaternion& relDirSoft, float bias = 1.0);
[6873]121  void setRelDirSoft(float angle, float x, float y, float z, float bias = 1.0);
[4836]122  /** @returns the relative Direction */
[5050]123  inline const Quaternion& getRelDir () const { return this->prevRelDirection; };
[5113]124  /** @returns the Relative Directional Destination */
[5915]125  inline const Quaternion& getRelDirSoft2D() const { return (this->toDirection)? *this->toDirection : this->relDirection; };
[4836]126  /** @returns a Vector pointing into the relative Direction */
[5050]127  inline Vector getRelDirV() const { return this->prevRelDirection.apply(Vector(0,1,0)); };
[3810]128  void setAbsDir (const Quaternion& absDir);
[6873]129  void setAbsDir (float angle, float x, float y, float z);
[5414]130  void setAbsDirSoft(const Quaternion& absDirSoft, float bias = 1.0);
[6873]131  void setAbsDirSoft(float angle, float x, float y, float z, float bias = 1.0);
[5915]132  void shiftDir (const Quaternion& shift);
[4836]133  /** @returns the absolute Direction */
[4372]134  inline const Quaternion& getAbsDir () const { return this->absDirection; };
[4836]135  /** @returns a Vector pointing into the absolute Direction */
[4372]136  inline Vector getAbsDirV() const { return this->absDirection.apply(Vector(0,1,0)); };
[5915]137  /** @returns A Vector pointing into the forward direction (X) of the Node */
138  inline Vector getAbsDirX() const { return this->absDirection.apply(Vector(1,0,0)); };
139  /** @returns A Vector pointing into the upward direction (Y) of the Node */
140  inline Vector getAbsDirY() const { return this->absDirection.apply(Vector(0,1,0)); };
141  /** @returns A Vector pointing into the right direction (Z) of the Node */
142  inline Vector getAbsDirZ() const { return this->absDirection.apply(Vector(0,0,1)); };
[3246]143
[3644]144
[6078]145  // PARENTING //
[5382]146  void addChild (PNode* child);
[7221]147  void addChild (const std::string& childName);
[4993]148  void removeChild (PNode* child);
[5769]149  void removeNode();
[3537]150
[6695]151  PNode* seekNextAssignedPNode(PNode* node) const;
152
[6054]153  /** @param parent the new parent of this node */
[6048]154  inline void setParent (PNode* parent) { parent->addChild(this); };
[7221]155  void setParent (const std::string& parentName);
[4966]156  /** @returns the parent of this PNode */
[5387]157  inline PNode* getParent () const { return this->parent; };
158  /** @returns the List of Children of this PNode */
[6071]159  const std::list<PNode*>& getNodesChildren() const { return this->children; };
[4761]160
[5382]161  void setParentSoft(PNode* parentNode, float bias = 1.0);
[7221]162  void setParentSoft(const std::string& parentName, float bias = 1.0);
[4987]163
[6078]164  // PARENTING_MODE AND OTHER FLAGS //
[6054]165  void setParentMode (PARENT_MODE parentMode);
[7221]166  void setParentMode (const std::string& parentingMode);
[4836]167  /** @returns the Parenting mode of this node */
[6054]168  int getParentMode() const { return 0x000f & this->parentMode; };
[3246]169
[6078]170  void addNodeFlags(unsigned short nodeFlags);
171  void removeNodeFlags(unsigned short nodeFlags);
[6054]172
[6078]173  // NULL_PARENT //
[6074]174  /** @returns the NullParent, the (main) ROOT of the PNode Tree. If it does not yet exist, it will be created. */
[6078]175  static PNode* getNullParent()  { return (PNode::nullParent != NULL)? PNode::nullParent : PNode::createNullParent(); };
[6054]176
[6078]177  // UPDATING //
[5769]178  void updateNode (float dt);
[3246]179
[6078]180  // DEBUG //
[5769]181  void countChildNodes(int& nodes) const;
182  void debugNode (unsigned int depth = 1, unsigned int level = 0) const;
[5383]183  void debugDraw(unsigned int depth = 1, float size = 1.0, const Vector& color = Vector(1, 0, 0), unsigned int level = 0) const;
[3365]184
[6078]185  // HELPER_FUNCTIONS //
[7221]186  static const char* parentingModeToString(int parentingMode);
187  static PARENT_MODE stringToParentingMode(const std::string& parentingMode);
[7076]188  float distance(const PNode* node) const { return (this->getAbsCoor() - node->getAbsCoor()).len(); };
[5750]189
[4440]190 private:
[4987]191  /** tells the child that the parent's Coordinate has changed */
[4440]192  inline void parentCoorChanged () { this->bRelCoorChanged = true; }
[4987]193  /** tells the child that the parent's Direction has changed */
[4440]194  inline void parentDirChanged () { this->bRelDirChanged = true; }
[8186]195 public:
[4836]196  /** @returns the last calculated coordinate */
[4993]197  inline Vector getLastAbsCoor() { return this->lastAbsCoordinate; }
[8186]198 private:
[6078]199  static PNode* createNullParent();
[6048]200  void reparent();
[6075]201  bool checkIntegrity(const PNode* checkParent) const;
[6078]202  void eraseChild(PNode* child);
[3537]203
[3644]204 private:
[5770]205  bool               bRelCoorChanged;    //!< If Relative Coordinate has changed since last time we checked
206  bool               bRelDirChanged;     //!< If Relative Direction has changed since last time we checked
[3644]207
[5770]208  Vector             relCoordinate;      //!< coordinates relative to the parent
209  Vector             absCoordinate;      //!< absolute coordinates in the world ( from (0,0,0) )
210  Quaternion         relDirection;       //!< direction relative to the parent
211  Quaternion         absDirection;       //!< absolute direvtion in the world ( from (0,0,1) )
[4440]212
[5770]213  Vector             prevRelCoordinate;  //!< The last Relative Coordinate from the last update-Cycle.
214  Vector             lastAbsCoordinate;  //!< this is used for speedcalculation, it stores the last coordinate
215  Quaternion         prevRelDirection;   //!< The last Relative Direciton from the last update-Cycle.
216//  Quaternion         lastAbsDirection;
[4440]217
[5770]218  Vector             velocity;           //!< Saves the velocity.
[4987]219
[5770]220  Vector*            toCoordinate;       //!< a position to which to iterate. (This is used in conjunction with setParentSoft.and set*CoorSoft)
221  Quaternion*        toDirection;        //!< a direction to which to iterate. (This is used in conjunction with setParentSoft and set*DirSoft)
222  float              bias;               //!< how fast to iterate to the given position (default is 1)
[4987]223
[6042]224  PNode*             parent;             //!< a pointer to the parent node.
225  std::list<PNode*>  children;           //!< list of the children of this PNode.
[4440]226
[6042]227  bool               bActive;            //!< If the Node is Active (for cutting off the rest of the tree in update).
228  unsigned short     parentMode;         //!< the mode of the binding
[6074]229
230
231  static PNode*      nullParent;         //!< The ROOT of the main PNode Tree.
[6634]232
233  private:
234    float coorx;
235    float coory;
236    float coorz;
237
238    float rotw;
239    float rotx;
240    float roty;
241    float rotz;
[8490]242
[7954]243  private:
244    int relCoordinate_handle;
245    int relDirection_handle;
246    Vector relCoordinate_write;
247    Quaternion relDirection_write;
[8490]248
[7954]249  public:
250    virtual void varChangeHandler( std::list<int> & id );
[3246]251};
252
253#endif /* _P_NODE_H */
Note: See TracBrowser for help on using the repository browser.