Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6078 in orxonox.OLD for trunk/src/lib/coord


Ignore:
Timestamp:
Dec 13, 2005, 2:55:08 AM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: PNode compiles again
PNode is now handled with the erase function of stl, rather than remove (which is totally the wrong way when iterating through a list and delete elements)

Location:
trunk/src/lib/coord
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/coord/p_node.cc

    r6075 r6078  
    2121#include "class_list.h"
    2222
     23#include <algorithm>
    2324#include "compiler.h"
    2425#include "debug.h"
     
    3233/**
    3334 * @brief standard constructor
    34  */
    35 PNode::PNode (PNode* parent)
    36 {
    37   init();
    38   if (parent != NULL)
    39     parent->addChild(this);
    40 }
    41 
    42 /**
    43  * @brief initializes a PNode
    44  * @param parent the parent for this PNode
    45  */
    46 void PNode::init()
     35 * @param parent the Parent of this Node. __NULL__ if __No Parent__ requested, PNode::getNullParent(), if connected to NullParent directly (default)
     36 * @param nodeFlags all flags to set. THIS_WILL_OVERWRITE Default_Values. look at creation of NullParent for more Info.
     37 */
     38PNode::PNode (PNode* parent, long nodeFlags)
    4739{
    4840  this->setClassID(CL_PARENT_NODE, "PNode");
     
    5143  this->bRelDirChanged = true;
    5244  this->parent = NULL;
    53   this->parentMode = PNODE_PARENT_MODE_DEFAULT;
     45  this->parentMode = nodeFlags;
    5446  this->bActive = true;
    5547
     
    5850  this->toDirection = NULL;
    5951  this->bias = 1.0;
     52
     53  if (parent != NULL)
     54    parent->addChild(this);
    6055}
    6156
     
    8782      deleteNode = tmp;
    8883      ++tmp;
    89       printf("TEST::%s(%s) %s\n", (*deleteNode)->getName(), (*deleteNode)->getClassName(), this->getName());
     84//      printf("TEST::%s(%s) %s\n", (*deleteNode)->getName(), (*deleteNode)->getClassName(), this->getName());
    9085      if ((this->parentMode & PNODE_PROHIBIT_CHILD_DELETE) ||
    9186          ((*deleteNode)->parentMode & PNODE_PROHIBIT_DELETE_WITH_PARENT))
    9287      {
    93         if (this == PNode::nullParent && (*deleteNode)->parentMode & PNODE_REPARENT_TO_NULLPARENT)
     88        if (this == PNode::nullParent && (*deleteNode)->parentMode & PNODE_REPARENT_TO_NULL)
    9489          delete (*deleteNode);
    9590        else
     
    10297  if (this->parent != NULL)
    10398   {
    104      this->parent->children.remove(this);
     99     this->parent->eraseChild(this);
    105100     this->parent = NULL;
    106101   }
     
    462457{
    463458  if( likely(child->parent != NULL))
    464       child->parent->children.remove(child);
     459      child->parent->eraseChild(child);
    465460  if (this->checkIntegrity(child))
    466461  {
     
    510505void PNode::reparent()
    511506{
    512   if (this->parentMode & PNODE_REPARENT_TO_NULLPARENT)
    513     this->setParent(PNode::getNullParent());
     507  if (this->parentMode & PNODE_REPARENT_TO_NULL)
     508    this->setParent((PNode*)NULL);
    514509  else if (this->parentMode & PNODE_REPARENT_TO_PARENTS_PARENT && this->parent != NULL)
    515510    this->setParent(this->parent->getParent());
     511  else
     512    this->setParent(PNode::getNullParent());
     513}
     514
     515void PNode::eraseChild(PNode* child)
     516{
     517  std::list<PNode*>::iterator childRemover = std::find(this->children.begin(), this->children.end(), child);
     518  if(childRemover != this->children.end())
     519    this->children.erase(childRemover);
    516520}
    517521
     
    524528void PNode::removeNode()
    525529{
    526   if (this->parentMode & PNODE_REPARENT_CHILDREN_ON_REMOVE)
    527   {
    528     list<PNode*>::iterator child = this->children.begin();
    529     list<PNode*>::iterator reparenter;
    530     while (child != this->children.end())
    531     {
    532       reparenter = child;
    533       child++;
     530  list<PNode*>::iterator child = this->children.begin();
     531  list<PNode*>::iterator reparenter;
     532  while (child != this->children.end())
     533  {
     534    reparenter = child;
     535    child++;
     536    if (this->parentMode & PNODE_REPARENT_CHILDREN_ON_REMOVE ||
     537        (*reparenter)->parentMode & PNODE_REPARENT_ON_PARENTS_REMOVE)
     538    {      printf("TEST----------------%s ---- %s\n", this->getClassName(), (*reparenter)->getClassName());
    534539      (*reparenter)->reparent();
     540      printf("REPARENTED TO: %s::%s\n",(*reparenter)->getParent()->getClassName(),(*reparenter)->getParent()->getName());
    535541    }
    536542  }
    537543  if (this->parent != NULL)
    538     this->parent->children.remove(this);
     544    this->parent->eraseChild(this);
    539545}
    540546
     
    630636 * @param nodeFlags a compsition of PARENT_MODE-flags, split by the '|' (or) operator.
    631637 */
    632 void PNode::addNodeModeFlags(unsigned short nodeFlags)
     638void PNode::addNodeFlags(unsigned short nodeFlags)
    633639{
    634640  this->parentMode |= nodeFlags;
     
    640646 * @param nodeFlags a compsition of PARENT_MODE-flags, split by the '|' (or) operator.
    641647 */
    642 void PNode::removeNodeModeFlags(unsigned short nodeFlags)
     648void PNode::removeNodeFlags(unsigned short nodeFlags)
    643649{
    644650  this->parentMode &= !nodeFlags;
     651}
     652
     653/**
     654 * @returns the NullParent (and if needed creates it)
     655 */
     656PNode* PNode::createNullParent()
     657{
     658  if (likely(PNode::nullParent == NULL))
     659  {
     660    PNode::nullParent = new PNode(NULL, PNODE_PARENT_MODE_DEFAULT | PNODE_REPARENT_TO_NULL);
     661    PNode::nullParent->setName("NullParent");
     662  }
     663  return PNode::nullParent;
    645664}
    646665
  • trunk/src/lib/coord/p_node.h

    r6075 r6078  
    4343
    4444  // REPARENTING
    45   PNODE_REPARENT_TO_NULLPARENT         = 0x0010,    //!< Reparents to the NullParent, if the Parent is Removed.
     45  PNODE_REPARENT_TO_NULL               = 0x0010,    //!< Reparents to the Null, if the Parent is Removed. Meaning the Node wont have a parent anymore.
    4646  PNODE_REPARENT_TO_PARENTS_PARENT     = 0x0020,    //!< Reparents the Node to the parents (old) parent it the parent gets removed.
     47  /////////////////////////////////////////////     //  ELSE: Reparents to the NullParent.
    4748  PNODE_REPARENT_DELETE_CHILDREN       = 0x0040,    //!< Deletes the Children of the node when This Node is Removed. (Use with care).
    4849  /// FIXME
     
    5455  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
    5556  PNODE_REPARENT_CHILDREN_ON_REMOVE    = 0x0400,    //!< Reparents the Children of the Node if the Node gets Removed.
     57  PNODE_REPARENT_ON_PARENTS_REMOVE     = 0x0800,    //!< The Node gets Reparented if its Parent gets removed. Child will be reparented according to the Reparenting-Rules.
    5658
    5759  // VISIBILITY/ACTIVITY
     
    6567//! The default mode of the translation-binding.
    6668#define PNODE_PARENT_MODE_DEFAULT PNODE_ALL | \
    67                                   PNODE_REPARENT_CHILDREN_ON_REMOVE | \
    6869                                  PNODE_REPARENT_KEEP_POSITION
    6970
     
    7273class PNode : virtual public BaseObject {
    7374 public:
    74   PNode (PNode* parent = PNode::getNullParent());
     75  PNode (PNode* parent = PNode::getNullParent(), long nodeFlags = PNODE_PARENT_MODE_DEFAULT);
    7576  virtual ~PNode ();
    7677
    7778  void loadParams(const TiXmlElement* root);
    7879
     80  // ACTIVATION //
    7981  inline void activateNode() { this->bActive = true; };
    8082  inline void deactivateNode() { this->bActive = false; };
    8183  inline bool getNodeActiveState() { return this->bActive; };
    8284
     85  // POSITION //
    8386  void setRelCoor (const Vector& relCoord);
    8487  void setRelCoor (float x, float y, float z);
     
    98101  void shiftCoor (float x, float y, float z) { this->shiftCoor(Vector(x, y, z)); };
    99102
     103  // SPEED //
     104  /** @returns the Speed of the Node */
     105  inline float getSpeed() const { return this->velocity.len(); };
     106  /** @returns the Velocity of the Node */
     107  inline const Vector& getVelocity() const { return this->velocity; };
     108
     109
     110  // ROTATION //
    100111  void setRelDir (const Quaternion& relDir);
    101112  void setRelDir (float x, float y, float z);
     
    124135  inline Vector getAbsDirZ() const { return this->absDirection.apply(Vector(0,0,1)); };
    125136
    126   /** @returns the Speed of the Node */
    127   inline float getSpeed() const { return this->velocity.len(); };
    128   /** @returns the Velocity of the Node */
    129   inline const Vector& getVelocity() const { return this->velocity; };
    130 
    131 
     137
     138  // PARENTING //
    132139  void addChild (PNode* child);
    133140  void addChild (const char* childName);
     
    146153  void setParentSoft(const char* parentName, float bias = 1.0);
    147154
     155  // PARENTING_MODE AND OTHER FLAGS //
    148156  void setParentMode (PARENT_MODE parentMode);
    149157  void setParentMode (const char* parentingMode);
     
    151159  int getParentMode() const { return 0x000f & this->parentMode; };
    152160
    153   void addNodeModeFlags(unsigned short nodeFlags);
    154   void removeNodeModeFlags(unsigned short nodeFlags);
    155 
     161  void addNodeFlags(unsigned short nodeFlags);
     162  void removeNodeFlags(unsigned short nodeFlags);
     163
     164  // NULL_PARENT //
    156165  /** @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; };
    158 
     166  static PNode* getNullParent()  { return (PNode::nullParent != NULL)? PNode::nullParent : PNode::createNullParent(); };
     167
     168  // UPDATING //
    159169  void updateNode (float dt);
    160170
     171  // DEBUG //
    161172  void countChildNodes(int& nodes) const;
    162173  void debugNode (unsigned int depth = 1, unsigned int level = 0) const;
    163174  void debugDraw(unsigned int depth = 1, float size = 1.0, const Vector& color = Vector(1, 0, 0), unsigned int level = 0) const;
    164175
    165 
    166   // helper functions //
     176  // HELPER_FUNCTIONS //
    167177  static const char* parentingModeToChar(int parentingMode);
    168178  static PARENT_MODE charToParentingMode(const char* parentingMode);
     
    170180
    171181 private:
    172   void init();
    173182  /** tells the child that the parent's Coordinate has changed */
    174183  inline void parentCoorChanged () { this->bRelCoorChanged = true; }
     
    178187  inline Vector getLastAbsCoor() { return this->lastAbsCoordinate; }
    179188
     189  static PNode* createNullParent();
    180190  void reparent();
    181191  bool checkIntegrity(const PNode* checkParent) const;
     192  void eraseChild(PNode* child);
    182193
    183194 private:
Note: See TracChangeset for help on using the changeset viewer.