Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/lib/graphics/spatial_separation/quadtree_node.cc @ 4907

Last change on this file since 4907 was 4907, checked in by patrick, 19 years ago

orxonox/trunk: hash table preparation for quadtree nodes lookup

File size: 12.2 KB
RevLine 
[4805]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: ...
14*/
15
[4808]16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_SPATIAL_SEPARATION
[4805]17
18#include "quadtree_node.h"
[4902]19#include "quadtree.h"
20#include "material.h"
[4868]21#include "abstract_model.h"
[4851]22#include "list.h"
23#include "vector.h"
[4805]24
25using namespace std;
26
27
28/**
[4836]29 *  standard constructor
[4805]30*/
[4887]31QuadtreeNode::QuadtreeNode (sTriangleExt** triangles, int numTriangles,
[4896]32                            const float* pVertices, int numVertices,
[4897]33                            Quadtree* quadtree, QuadtreeNode* parent,
[4900]34                            Rectangle* rect, int treeDepth, const int maxDepth, int index
[4897]35                           )
[4805]36{
[4867]37  this->pTriangles = triangles;
38  this->numTriangles = numTriangles;
[4868]39  this->pVertices = pVertices;
40  this->numVertices = numVertices;
[4899]41
[4867]42  this->quadtree = quadtree;
[4896]43  this->parent = parent;
[4899]44
[4897]45  this->pDimension = rect;
[4898]46  this->treeDepth = treeDepth;
47  this->maxDepth = maxDepth;
[4900]48  this->indexNode = index;
[4887]49
[4898]50
51  /* debug output */
52  for( int i = 0; i < this->treeDepth; ++i)
53    PRINT(3)(" |");
[4900]54  PRINT(3)(" | +-| (Event) Separating Node Nr. %i Depth: %i/%i\n", this->indexNode, treeDepth, maxDepth);
[4898]55
56  for( int i = 0; i < this->treeDepth; ++i)
57    PRINT(3)(" |");
58  PRINT(3)(" | +-| (II) Rectangle Center (%5.2f, %5.2f), half axis length: %5.2f\n",
59  this->pDimension->getCenter()->x, this->pDimension->getCenter()->z, this->pDimension->getAxis());
60
[4852]61  this->init();
[4805]62}
63
64
65/**
[4845]66 *  standard constructor
67 */
[4902]68QuadtreeNode::QuadtreeNode(modelInfo* pModelInfo, Quadtree* quadtree, const int maxDepth)
[4845]69{
70  this->pModelInfo = pModelInfo;
[4902]71  this->quadtree = quadtree;
[4887]72
[4851]73  /* create an array of triangle references */
74  this->pTriangles = new sTriangleExt*[this->pModelInfo->numTriangles];
75  this->numTriangles = this->pModelInfo->numTriangles;
76  this->pVertices = this->pModelInfo->pVertices;
[4868]77  this->numVertices = this->pModelInfo->numVertices;
[4851]78  for( int i = 0; i < this->pModelInfo->numTriangles; ++i)
79    this->pTriangles[i] = &this->pModelInfo->pTriangles[i];
[4900]80
[4887]81  this->treeDepth = 0;
[4898]82  this->maxDepth = maxDepth;
[4900]83  this->indexNode = 0;
[4852]84
[4898]85  /* debug output */
86  for( int i = 0; i < this->treeDepth; ++i)
87    PRINT(3)(" |");
[4900]88  PRINT(3)(" | +-| (Event) Separating Node Nr. %i Depth: %i/%i\n", this->indexNode, treeDepth, maxDepth);
[4898]89
[4897]90  this->pDimension = this->getDimFromModel();
[4852]91  this->init();
[4845]92}
93
[4852]94
95/**
96 * takes the rest of the initialisation process
97 */
98void QuadtreeNode::init()
99{
100  this->setClassID(CL_QUADTREE_NODE, "QuadtreeNode");
101
102  this->offset = 0.0f;
[4899]103
[4896]104  this->parent = NULL;
[4867]105  this->nodeA = NULL;
106  this->nodeB = NULL;
107  this->nodeC = NULL;
108  this->nodeD = NULL;
[4907]109  this->nodes = new QuadtreeNode*[4];
[4898]110
[4899]111  if( this->treeDepth < this->maxDepth)
112    this->separateNode();
[4852]113}
114
[4887]115
[4845]116/**
[4836]117 *  standard deconstructor
[4852]118 */
[4805]119QuadtreeNode::~QuadtreeNode ()
120{
[4867]121  if( this->nodeA != NULL)
122    delete this->nodeA;
123  if( this->nodeB != NULL)
124    delete this->nodeB;
125  if( this->nodeC != NULL)
126    delete this->nodeC;
127  if( this->nodeD != NULL)
128    delete this->nodeD;
[4805]129}
[4812]130
131
[4887]132
[4812]133/**
[4836]134 *  gives the signal to separate the model into a quadtree
135 * @param treeDepth the max depth, the steps to go if treeDept == 0 leaf reached
[4819]136*/
137void QuadtreeNode::separateNode(float minLength)
[4852]138{
[4888]139  /* dimension calculation & limit checking */
[4867]140  if( minLength <= this->pDimension->getAxis())
141    return;
[4887]142
[4888]143  /* node separation */
[4899]144//   this->separateNode();
145//   this->nodeA->separateNode(minLength);
146//   this->nodeB->separateNode(minLength);
147//   this->nodeC->separateNode(minLength);
148//   this->nodeD->separateNode(minLength);
[4852]149}
[4819]150
151
152/**
[4851]153 *  gives the signal to separate the model into a quadtree
154 * @param treeDepth the max depth, the steps to go if treeDept == 0 leaf reached
155*/
156void QuadtreeNode::separateNode()
157{
158  tList<sTriangleExt*>*           listA = new tList<sTriangleExt*>();    //!< triangle list of nodeA
159  tList<sTriangleExt*>*           listB = new tList<sTriangleExt*>();    //!< triangle list of nodeB
160  tList<sTriangleExt*>*           listC = new tList<sTriangleExt*>();    //!< triangle list of nodeC
161  tList<sTriangleExt*>*           listD = new tList<sTriangleExt*>();    //!< triangle list of nodeD
162  const float*                    pVert;                                 //!< pointer to the vertices
163  const Vector*                   rectCenter;                            //!< vector to the center of the rect
164
165  rectCenter = this->pDimension->getCenter();
166  for( int i = 0; i < this->numTriangles; ++i)
167    {
168      for( int j = 0; j < 3; ++j)
[4887]169        {
170          pVert = &this->pVertices[this->pTriangles[i]->indexToVertices[j]];
171          if( pVert[0] > rectCenter->x + this->offset && pVert[2] > rectCenter->z + this->offset)
172            listA->add(&this->pTriangles[i]);
[4900]173          if( pVert[0] < rectCenter->x + this->offset && pVert[2] > rectCenter->z + this->offset)
[4887]174            listB->add(&this->pTriangles[i]);
[4900]175          if( pVert[0] < rectCenter->x + this->offset && pVert[2] < rectCenter->z + this->offset)
[4887]176            listC->add(&this->pTriangles[i]);
[4900]177          if( pVert[0] > rectCenter->x + this->offset && pVert[2] < rectCenter->z + this->offset)
[4887]178            listD->add(&this->pTriangles[i]);
179        }
[4851]180    }
[4887]181    for( int i = 0; i < treeDepth; ++i)
[4888]182      PRINT(3)(" |");
183    PRINT(3)(" | +-| (II) Quadtree Counts: A: %i, B: %i, C: %i, D: %i\n", listA->getSize(), listB->getSize(), listC->getSize(), listD->getSize());
[4853]184
185  /* Separating into to the triangle lists  */
186  sTriangleExt**                 pTriA;                                 //!< Triangle array A
187  sTriangleExt**                 pTriB;                                 //!< Triangle array B
188  sTriangleExt**                 pTriC;                                 //!< Triangle array C
189  sTriangleExt**                 pTriD;                                 //!< Triangle array D
[4867]190  int                            lenA;                                  //!< length array A
191  int                            lenB;                                  //!< length array B
192  int                            lenC;                                  //!< length array C
193  int                            lenD;                                  //!< length array D
[4853]194  tIterator<sTriangleExt*>*      iterator;                              //!< iterator for the list iterations
[4855]195  sTriangleExt**                 tempTri;                               //!< temp save place for triangle pointer
[4854]196  int                            counter;                               //!< counter for the while loops
[4853]197
198  lenA = listA->getSize();
199  lenB = listB->getSize();
200  lenC = listC->getSize();
201  lenD = listD->getSize();
[4887]202
[4853]203  pTriA = new sTriangleExt*[listA->getSize()];
204  pTriB = new sTriangleExt*[listB->getSize()];
[4887]205  pTriC = new sTriangleExt*[listC->getSize()];
[4853]206  pTriD = new sTriangleExt*[listD->getSize()];
207
208
[4854]209  counter = 0;
[4853]210  iterator = listA->getIterator();
[4855]211  tempTri = iterator->nextElement();
[4854]212  while( tempTri)
213    {
[4855]214      pTriA[counter] = *tempTri;
215      tempTri = iterator->nextElement();
[4854]216      ++counter;
217    }
[4853]218
[4854]219  counter = 0;
220  iterator = listB->getIterator();
[4855]221  tempTri = iterator->nextElement();
[4854]222  while( tempTri)
223    {
[4855]224      pTriB[counter] = *tempTri;
225      tempTri = iterator->nextElement();
[4854]226      ++counter;
227    }
[4887]228
[4854]229  counter = 0;
230  iterator = listC->getIterator();
[4855]231  tempTri = iterator->nextElement();
[4854]232  while( tempTri)
233    {
[4855]234      pTriC[counter] = *tempTri;
235      tempTri = iterator->nextElement();
[4854]236      ++counter;
237    }
[4853]238
[4854]239  counter = 0;
240  iterator = listD->getIterator();
[4855]241  tempTri = iterator->nextElement();
[4854]242  while( tempTri)
243    {
[4855]244      pTriD[counter] = *tempTri;
245      tempTri = iterator->nextElement();
[4854]246      ++counter;
247    }
[4853]248
[4859]249  /* now do cleanup */
250  delete listA;
251  delete listB;
252  delete listC;
253  delete listD;
254  delete iterator;
255
[4897]256
257  /* now create the rectangle dimensions */
258  Vector v;
259
[4898]260  v.x = this->pDimension->getCenter()->x + this->pDimension->getAxis() / 2.0f;
[4900]261  v.y = 0.0;
[4898]262  v.z = this->pDimension->getCenter()->z + this->pDimension->getAxis() / 2.0f;
[4897]263  Rectangle* rA = new Rectangle(v, this->pDimension->getAxis() / 2.0f);
264
[4898]265  v.z = this->pDimension->getCenter()->z - this->pDimension->getAxis() / 2.0f;
[4897]266  Rectangle* rB = new Rectangle(v, this->pDimension->getAxis() / 2.0f);
267
[4898]268  v.x = this->pDimension->getCenter()->x - this->pDimension->getAxis() / 2.0f;
[4897]269  Rectangle* rC = new Rectangle(v, this->pDimension->getAxis() / 2.0f);
270
[4898]271  v.z = this->pDimension->getCenter()->z + this->pDimension->getAxis() / 2.0f;
[4897]272  Rectangle* rD = new Rectangle(v, this->pDimension->getAxis() / 2.0f);
273
[4859]274  /* now propagate */
[4900]275  this->nodeA = new QuadtreeNode(pTriA, lenA, this->pVertices, this->numVertices, this->quadtree, this, rA, this->treeDepth + 1, this->maxDepth, (this->treeDepth + 1) * 10 + 0);
276
277  this->nodeB = new QuadtreeNode(pTriB, lenB, this->pVertices, this->numVertices, this->quadtree, this, rB, this->treeDepth + 1, this->maxDepth, (this->treeDepth + 1) * 10 + 1);
278
279  this->nodeC = new QuadtreeNode(pTriC, lenC, this->pVertices, this->numVertices, this->quadtree, this, rC, this->treeDepth + 1, this->maxDepth, (this->treeDepth + 1) * 10 + 2);
280
281  this->nodeD = new QuadtreeNode(pTriD, lenD, this->pVertices, this->numVertices, this->quadtree, this, rD, this->treeDepth + 1, this->maxDepth, (this->treeDepth + 1) * 10 + 3);
[4907]282  /* map the array references, this is for faster and automatical interfacing  \todo: use only array */
283  this->nodes[0] = this->nodeA;
284  this->nodes[1] = this->nodeB;
285  this->nodes[2] = this->nodeC;
286  this->nodes[3] = this->nodeD;
[4851]287}
288
289
290/**
[4868]291   \brief gets the maximal dimension of a model
292   \return the dimension of the AbstractModel as a Rectangle
[4887]293
[4868]294   The rectangle is x-z axis aligned. ATTENTION: if there are any vertices in the model, that exceed the
295   size of 999999.0, there probably will be some errors in the dimensions calculations. Write an email to
296   patrick@orxonox.ethz.ch if you will ever encounter a model bigger than 999999.0 units, I will realy be
297   happy to see orxonox used to extensivly :)
298 */
[4897]299Rectangle* QuadtreeNode::getDimFromModel()
[4868]300{
301  float            maxX, maxY;                       //!< the maximal coordinates axis
302  float            minX, minY;                       //!< minimal axis coorindates
303  const float*     pVertices;                        //!< pointer to the current vertices
[4887]304
[4868]305  maxX = -999999; maxY = -999999;
306  minX =  999999; minY =  999999;
307  /* get maximal/minimal x/y */
[4887]308  for( int i = 0; i < this->numTriangles; ++i)
[4868]309  {
[4887]310    for( int j = 0; j < 3; ++j)
311    {
[4868]312
[4887]313      pVertices = &this->pVertices[this->pTriangles[i]->indexToVertices[j]];
314      if( pVertices[0] > maxX)
315        maxX = pVertices[0];
316      if( pVertices[2] > maxY)
317        maxY = pVertices[2];
318
319      if( pVertices[0] < minX)
320        minX = pVertices[0];
321      if( pVertices[2] < minY)
322        minY = pVertices[2];
323    }
[4868]324  }
325
326  Rectangle* rect = new Rectangle();
327  rect->setCenter((maxX + minX) / 2.0f, 0.0f, (maxY + minY) / 2.0f); /* this is little strange, since y is in opengl the up vector */
328  rect->setAxis(fmax(((fabs(maxX) + fabs(minX)) / 2.0f), ((fabs(maxY) + fabs(minY)) / 2.0f)));
329
[4887]330  for( int i = 0; i < this->treeDepth; ++i)
[4888]331    PRINT(3)(" |");
332  PRINT(3)(" | +-| (II) Rectangle Dimension  (%5.2f, %5.2f) to (%5.2f, %5.2f), Center (%5.2f, %5.2f)\n", minX, minY, maxX, maxY, rect->getCenter()->x, rect->getCenter()->z, rect->getAxis());
[4868]333  return rect;
334}
335
[4902]336
337/**
338 *  draws the debug quadtree boxes around the model
339 */
340void QuadtreeNode::drawTree(int depth, int drawMode) const
341{
342
343  Vector t1 = *this->pDimension->getCenter();
344  float ax = this->pDimension->getAxis();
345  float h = 50.0f;
346
347  glBegin(GL_QUADS);
348  this->quadtree->getMaterial(this->indexNode)->select();
349  glVertex3f(t1.x + ax, h - depth * 10.0f, t1.z + ax);
350  glVertex3f(t1.x - ax, h - depth * 10.0f, t1.z + ax);
351  glVertex3f(t1.x - ax, h - depth * 10.0f, t1.z - ax);
352  glVertex3f(t1.x + ax, h - depth * 10.0f, t1.z - ax);
353  glEnd();
354
355  if( this->nodeA != NULL)
356    this->nodeA->drawTree(depth - 1, drawMode);
357  if( this->nodeB != NULL)
358    this->nodeB->drawTree(depth - 1, drawMode);
359  if( this->nodeC != NULL)
360    this->nodeC->drawTree(depth - 1, drawMode);
361  if( this->nodeD != NULL)
362    this->nodeD->drawTree(depth - 1, drawMode);
363}
Note: See TracBrowser for help on using the repository browser.