Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jul 21, 2005, 3:59:28 PM (19 years ago)
Author:
patrick
Message:

orxonox/trunk: started cleaning up the code a little and making the comments

File:
1 edited

Legend:

Unmodified
Added
Removed
  • orxonox/trunk/src/lib/graphics/spatial_separation/quadtree.cc

    r4921 r4922  
    6464     printf("node %i, %f, %f \n", i, this->nodes[i]->getDimension()->getCenter()->x, this->nodes[i]->getDimension()->getCenter()->z);
    6565   }
     66
     67   this->quadLength = this->nodes[0]->getDimension()->getAxis() * 2.0f;
     68   Rectangle* r = this->rootNode->getDimension();
     69
     70   Vector* offset = new Vector();
     71   float xOff = r->getCenter()->x - r->getAxis();
     72   float yOff = r->getCenter()->z - r->getAxis();
     73   this->offset->x = xOff;
     74   this->offset->z = yOff;
     75
     76   this->maxIndex = (int)pow(2, this->treeDepth);
    6677}
    6778
     
    7687  delete [] this->nodes;
    7788  delete this->rootNode;
     89  delete offset;
    7890}
    7991
    8092
    8193/**
     94  \brief this function rotates the array and mirrors it in the middle
     95  \param nodes: the nodes to translate
    8296
    8397  since the original matrix is counted from the right upper edge to the right lower one, we have to reorganize the elements
     
    108122}
    109123
     124/**
     125  \brief sorts the hash table using their positions
     126  \param nodes the nodes to use
    110127
     128 */
    111129void Quadtree::sortHashTable(QuadtreeNode** nodes)
    112130{
    113131  int                  len         = (int)pow(2, this->treeDepth);          //!< the length of a quadtree side
    114   float a,b;
    115   QuadtreeNode* tmpNode;
     132  float                a;                                                   //!< temp place for float a
     133  float                b;                                                   //!< temp place for float b
     134  QuadtreeNode*        tmpNode;                                             //!< tmp place for a QuadtreeNode
    116135
    117   for(int i = 0; i < len; ++i)
     136  for( int i = 0; i < len; ++i)
    118137  {
    119     for(int j = 0; j < len; ++j)
     138    for( int j = 0; j < len; ++j)
    120139    {
    121       for(int k = j + 1; k < len; ++k)
     140      for( int k = j + 1; k < len; ++k)
    122141      {
    123142        a = this->nodes[i * len + j]->getDimension()->getCenter()->z;
     
    131150        }
    132151      }
     152    }
     153  }
    133154
    134     }
    135 
    136   }
    137155}
    138156
    139157
     158/**
     159  \brief maps a position to a quadtree
     160  \param position the position so look for
     161  \return the quadtree
    140162
     163  this function will return the quadtree that contains the position
     164 */
    141165QuadtreeNode* Quadtree::getQuadtreeFromPosition(const Vector& position)
    142166{
    143   Vector v = position;
    144   Rectangle* r = this->rootNode->getDimension();
    145   float len = this->nodes[0]->getDimension()->getAxis() * 2.0f;
     167  /* shift into model space */
     168  Vector v = position - *this->offset;
     169  /* map */
     170  int i = (int)(v.x / quadLength);
     171  int j = (int)(v.z / quadLength);
    146172
    147   Vector edge;
    148   float xOff = r->getCenter()->x - r->getAxis();
    149   float yOff = r->getCenter()->z - r->getAxis();
    150   edge.x = xOff, edge.z = yOff;
    151 
    152   /* shift into model space */
    153   v = position - edge;
    154   /* map */
    155   int i = (int)(v.x / len);
    156   int j = (int)(v.z / len);
    157 
    158   int max = (int)pow(2, this->treeDepth);
    159 
    160   if( i < max && j < max)
    161   {
    162     printf("-----------\nthe array coordinates are: %i, %i\n", i, j);
    163     printf("position: %f,%f, center %f, %f\n", position.x, position.z, this->nodes[i + j * max]->getDimension()->getCenter()->x, this->nodes[i + j * max]->getDimension()->getCenter()->z);
    164 
    165     //this->nodes[i + j * max]->draw();
    166     this->nodes[i + j * max]->includesPoint(position);
    167   }
     173  /* check if object is still inside the terrain */
     174  if( i < this->maxIndex && j < this->maxIndex)
     175    this->nodes[i + j * this->maxIndex]->includesPoint(position);
    168176  else
    169     printf("object has left terrain\n");
     177    PRINTF(0)("Object has left terrain\n");
    170178}
    171179
     
    174182 *  draws the debug quadtree boxes around the model
    175183 */
    176 void Quadtree::drawTree(int depth, int drawMode) const
     184void Quadtree::drawTree() const
    177185{
    178   //this->rootNode->drawTree(depth, drawMode);
    179   for(int i = 0; i < (int)pow(4, this->treeDepth); ++i)
    180   {
    181     this->nodes[i]->draw();
    182   }
    183 
    184 
     186  this->rootNode->drawTree();
     187//   for(int i = 0; i < (int)pow(4, this->treeDepth); ++i)
     188//   {
     189//     this->nodes[i]->draw();
     190//   }
    185191}
Note: See TracChangeset for help on using the changeset viewer.