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
Line 
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
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_SPATIAL_SEPARATION
17
18#include "quadtree_node.h"
19#include "quadtree.h"
20#include "material.h"
21#include "abstract_model.h"
22#include "list.h"
23#include "vector.h"
24
25using namespace std;
26
27
28/**
29 *  standard constructor
30*/
31QuadtreeNode::QuadtreeNode (sTriangleExt** triangles, int numTriangles,
32                            const float* pVertices, int numVertices,
33                            Quadtree* quadtree, QuadtreeNode* parent,
34                            Rectangle* rect, int treeDepth, const int maxDepth, int index
35                           )
36{
37  this->pTriangles = triangles;
38  this->numTriangles = numTriangles;
39  this->pVertices = pVertices;
40  this->numVertices = numVertices;
41
42  this->quadtree = quadtree;
43  this->parent = parent;
44
45  this->pDimension = rect;
46  this->treeDepth = treeDepth;
47  this->maxDepth = maxDepth;
48  this->indexNode = index;
49
50
51  /* debug output */
52  for( int i = 0; i < this->treeDepth; ++i)
53    PRINT(3)(" |");
54  PRINT(3)(" | +-| (Event) Separating Node Nr. %i Depth: %i/%i\n", this->indexNode, treeDepth, maxDepth);
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
61  this->init();
62}
63
64
65/**
66 *  standard constructor
67 */
68QuadtreeNode::QuadtreeNode(modelInfo* pModelInfo, Quadtree* quadtree, const int maxDepth)
69{
70  this->pModelInfo = pModelInfo;
71  this->quadtree = quadtree;
72
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;
77  this->numVertices = this->pModelInfo->numVertices;
78  for( int i = 0; i < this->pModelInfo->numTriangles; ++i)
79    this->pTriangles[i] = &this->pModelInfo->pTriangles[i];
80
81  this->treeDepth = 0;
82  this->maxDepth = maxDepth;
83  this->indexNode = 0;
84
85  /* debug output */
86  for( int i = 0; i < this->treeDepth; ++i)
87    PRINT(3)(" |");
88  PRINT(3)(" | +-| (Event) Separating Node Nr. %i Depth: %i/%i\n", this->indexNode, treeDepth, maxDepth);
89
90  this->pDimension = this->getDimFromModel();
91  this->init();
92}
93
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;
103
104  this->parent = NULL;
105  this->nodeA = NULL;
106  this->nodeB = NULL;
107  this->nodeC = NULL;
108  this->nodeD = NULL;
109  this->nodes = new QuadtreeNode*[4];
110
111  if( this->treeDepth < this->maxDepth)
112    this->separateNode();
113}
114
115
116/**
117 *  standard deconstructor
118 */
119QuadtreeNode::~QuadtreeNode ()
120{
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;
129}
130
131
132
133/**
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
136*/
137void QuadtreeNode::separateNode(float minLength)
138{
139  /* dimension calculation & limit checking */
140  if( minLength <= this->pDimension->getAxis())
141    return;
142
143  /* node separation */
144//   this->separateNode();
145//   this->nodeA->separateNode(minLength);
146//   this->nodeB->separateNode(minLength);
147//   this->nodeC->separateNode(minLength);
148//   this->nodeD->separateNode(minLength);
149}
150
151
152/**
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)
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]);
173          if( pVert[0] < rectCenter->x + this->offset && pVert[2] > rectCenter->z + this->offset)
174            listB->add(&this->pTriangles[i]);
175          if( pVert[0] < rectCenter->x + this->offset && pVert[2] < rectCenter->z + this->offset)
176            listC->add(&this->pTriangles[i]);
177          if( pVert[0] > rectCenter->x + this->offset && pVert[2] < rectCenter->z + this->offset)
178            listD->add(&this->pTriangles[i]);
179        }
180    }
181    for( int i = 0; i < treeDepth; ++i)
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());
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
190  int                            lenA;                                  //!< length array A
191  int                            lenB;                                  //!< length array B
192  int                            lenC;                                  //!< length array C
193  int                            lenD;                                  //!< length array D
194  tIterator<sTriangleExt*>*      iterator;                              //!< iterator for the list iterations
195  sTriangleExt**                 tempTri;                               //!< temp save place for triangle pointer
196  int                            counter;                               //!< counter for the while loops
197
198  lenA = listA->getSize();
199  lenB = listB->getSize();
200  lenC = listC->getSize();
201  lenD = listD->getSize();
202
203  pTriA = new sTriangleExt*[listA->getSize()];
204  pTriB = new sTriangleExt*[listB->getSize()];
205  pTriC = new sTriangleExt*[listC->getSize()];
206  pTriD = new sTriangleExt*[listD->getSize()];
207
208
209  counter = 0;
210  iterator = listA->getIterator();
211  tempTri = iterator->nextElement();
212  while( tempTri)
213    {
214      pTriA[counter] = *tempTri;
215      tempTri = iterator->nextElement();
216      ++counter;
217    }
218
219  counter = 0;
220  iterator = listB->getIterator();
221  tempTri = iterator->nextElement();
222  while( tempTri)
223    {
224      pTriB[counter] = *tempTri;
225      tempTri = iterator->nextElement();
226      ++counter;
227    }
228
229  counter = 0;
230  iterator = listC->getIterator();
231  tempTri = iterator->nextElement();
232  while( tempTri)
233    {
234      pTriC[counter] = *tempTri;
235      tempTri = iterator->nextElement();
236      ++counter;
237    }
238
239  counter = 0;
240  iterator = listD->getIterator();
241  tempTri = iterator->nextElement();
242  while( tempTri)
243    {
244      pTriD[counter] = *tempTri;
245      tempTri = iterator->nextElement();
246      ++counter;
247    }
248
249  /* now do cleanup */
250  delete listA;
251  delete listB;
252  delete listC;
253  delete listD;
254  delete iterator;
255
256
257  /* now create the rectangle dimensions */
258  Vector v;
259
260  v.x = this->pDimension->getCenter()->x + this->pDimension->getAxis() / 2.0f;
261  v.y = 0.0;
262  v.z = this->pDimension->getCenter()->z + this->pDimension->getAxis() / 2.0f;
263  Rectangle* rA = new Rectangle(v, this->pDimension->getAxis() / 2.0f);
264
265  v.z = this->pDimension->getCenter()->z - this->pDimension->getAxis() / 2.0f;
266  Rectangle* rB = new Rectangle(v, this->pDimension->getAxis() / 2.0f);
267
268  v.x = this->pDimension->getCenter()->x - this->pDimension->getAxis() / 2.0f;
269  Rectangle* rC = new Rectangle(v, this->pDimension->getAxis() / 2.0f);
270
271  v.z = this->pDimension->getCenter()->z + this->pDimension->getAxis() / 2.0f;
272  Rectangle* rD = new Rectangle(v, this->pDimension->getAxis() / 2.0f);
273
274  /* now propagate */
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);
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;
287}
288
289
290/**
291   \brief gets the maximal dimension of a model
292   \return the dimension of the AbstractModel as a Rectangle
293
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 */
299Rectangle* QuadtreeNode::getDimFromModel()
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
304
305  maxX = -999999; maxY = -999999;
306  minX =  999999; minY =  999999;
307  /* get maximal/minimal x/y */
308  for( int i = 0; i < this->numTriangles; ++i)
309  {
310    for( int j = 0; j < 3; ++j)
311    {
312
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    }
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
330  for( int i = 0; i < this->treeDepth; ++i)
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());
333  return rect;
334}
335
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.