Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: some problems with the hash alg

File size: 13.0 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  this->nodeIter = 0;
104
105  this->parent = NULL;
106  this->nodeA = NULL;
107  this->nodeB = NULL;
108  this->nodeC = NULL;
109  this->nodeD = NULL;
110  this->nodes = new QuadtreeNode*[4];
111  for(int i = 0; i < 4; ++i)
112    this->nodes[i] = NULL;
113
114  if( this->treeDepth < this->maxDepth)
115    this->separateNode();
116}
117
118
119/**
120 *  standard deconstructor
121 */
122QuadtreeNode::~QuadtreeNode ()
123{
124  if( this->nodeA != NULL)
125    delete this->nodeA;
126  if( this->nodeB != NULL)
127    delete this->nodeB;
128  if( this->nodeC != NULL)
129    delete this->nodeC;
130  if( this->nodeD != NULL)
131    delete this->nodeD;
132}
133
134
135
136void QuadtreeNode::buildHashTable(QuadtreeNode** nodeList, int* index)
137{
138  int elCount = (int)pow(4, maxDepth - treeDepth) / 2;
139  int loopLimit = (this->nodeIter < elCount)?2:4;
140
141  /* is it a leaf? */
142  if( this->treeDepth < this->maxDepth)
143  {
144    for(int i = (this->nodeIter < elCount)?0:2; i < loopLimit; ++i)
145    {
146      for( int j = 0; j < this->treeDepth; ++j)
147        printf(" ");
148      printf("-> %i @ depth: %i, %i/%i \n", i, this->treeDepth, nodeIter, elCount);
149
150      this->nodes[i]->buildHashTable(nodeList, index);
151      ++this->nodeIter;
152    }
153  }
154  else
155  {
156    for( int j = 0; j < this->treeDepth; ++j)
157      printf(" ");
158    printf("        leaf, index: %i\n", *index);
159    nodeList[(*index)++] = this;
160  }
161}
162
163
164
165/**
166 *  gives the signal to separate the model into a quadtree
167 * @param treeDepth the max depth, the steps to go if treeDept == 0 leaf reached
168*/
169void QuadtreeNode::separateNode(float minLength)
170{
171  /* dimension calculation & limit checking */
172  if( minLength <= this->pDimension->getAxis())
173    return;
174
175  /* node separation */
176//   this->separateNode();
177//   this->nodeA->separateNode(minLength);
178//   this->nodeB->separateNode(minLength);
179//   this->nodeC->separateNode(minLength);
180//   this->nodeD->separateNode(minLength);
181}
182
183
184/**
185 *  gives the signal to separate the model into a quadtree
186 * @param treeDepth the max depth, the steps to go if treeDept == 0 leaf reached
187*/
188void QuadtreeNode::separateNode()
189{
190  tList<sTriangleExt*>*           listA = new tList<sTriangleExt*>();    //!< triangle list of nodeA
191  tList<sTriangleExt*>*           listB = new tList<sTriangleExt*>();    //!< triangle list of nodeB
192  tList<sTriangleExt*>*           listC = new tList<sTriangleExt*>();    //!< triangle list of nodeC
193  tList<sTriangleExt*>*           listD = new tList<sTriangleExt*>();    //!< triangle list of nodeD
194  const float*                    pVert;                                 //!< pointer to the vertices
195  const Vector*                   rectCenter;                            //!< vector to the center of the rect
196
197  rectCenter = this->pDimension->getCenter();
198  for( int i = 0; i < this->numTriangles; ++i)
199    {
200      for( int j = 0; j < 3; ++j)
201        {
202          pVert = &this->pVertices[this->pTriangles[i]->indexToVertices[j]];
203          if( pVert[0] > rectCenter->x + this->offset && pVert[2] > rectCenter->z + this->offset)
204            listA->add(&this->pTriangles[i]);
205          if( pVert[0] < rectCenter->x + this->offset && pVert[2] > rectCenter->z + this->offset)
206            listB->add(&this->pTriangles[i]);
207          if( pVert[0] < rectCenter->x + this->offset && pVert[2] < rectCenter->z + this->offset)
208            listC->add(&this->pTriangles[i]);
209          if( pVert[0] > rectCenter->x + this->offset && pVert[2] < rectCenter->z + this->offset)
210            listD->add(&this->pTriangles[i]);
211        }
212    }
213    for( int i = 0; i < treeDepth; ++i)
214      PRINT(3)(" |");
215    PRINT(3)(" | +-| (II) Quadtree Counts: A: %i, B: %i, C: %i, D: %i\n", listA->getSize(), listB->getSize(), listC->getSize(), listD->getSize());
216
217  /* Separating into to the triangle lists  */
218  sTriangleExt**                 pTriA;                                 //!< Triangle array A
219  sTriangleExt**                 pTriB;                                 //!< Triangle array B
220  sTriangleExt**                 pTriC;                                 //!< Triangle array C
221  sTriangleExt**                 pTriD;                                 //!< Triangle array D
222  int                            lenA;                                  //!< length array A
223  int                            lenB;                                  //!< length array B
224  int                            lenC;                                  //!< length array C
225  int                            lenD;                                  //!< length array D
226  tIterator<sTriangleExt*>*      iterator;                              //!< iterator for the list iterations
227  sTriangleExt**                 tempTri;                               //!< temp save place for triangle pointer
228  int                            counter;                               //!< counter for the while loops
229
230  lenA = listA->getSize();
231  lenB = listB->getSize();
232  lenC = listC->getSize();
233  lenD = listD->getSize();
234
235  pTriA = new sTriangleExt*[listA->getSize()];
236  pTriB = new sTriangleExt*[listB->getSize()];
237  pTriC = new sTriangleExt*[listC->getSize()];
238  pTriD = new sTriangleExt*[listD->getSize()];
239
240
241  counter = 0;
242  iterator = listA->getIterator();
243  tempTri = iterator->nextElement();
244  while( tempTri)
245    {
246      pTriA[counter] = *tempTri;
247      tempTri = iterator->nextElement();
248      ++counter;
249    }
250
251  counter = 0;
252  iterator = listB->getIterator();
253  tempTri = iterator->nextElement();
254  while( tempTri)
255    {
256      pTriB[counter] = *tempTri;
257      tempTri = iterator->nextElement();
258      ++counter;
259    }
260
261  counter = 0;
262  iterator = listC->getIterator();
263  tempTri = iterator->nextElement();
264  while( tempTri)
265    {
266      pTriC[counter] = *tempTri;
267      tempTri = iterator->nextElement();
268      ++counter;
269    }
270
271  counter = 0;
272  iterator = listD->getIterator();
273  tempTri = iterator->nextElement();
274  while( tempTri)
275    {
276      pTriD[counter] = *tempTri;
277      tempTri = iterator->nextElement();
278      ++counter;
279    }
280
281  /* now do cleanup */
282  delete listA;
283  delete listB;
284  delete listC;
285  delete listD;
286  delete iterator;
287
288
289  /* now create the rectangle dimensions */
290  Vector v;
291
292  v.x = this->pDimension->getCenter()->x + this->pDimension->getAxis() / 2.0f;
293  v.y = 0.0;
294  v.z = this->pDimension->getCenter()->z + this->pDimension->getAxis() / 2.0f;
295  Rectangle* rA = new Rectangle(v, this->pDimension->getAxis() / 2.0f);
296
297  v.z = this->pDimension->getCenter()->z - this->pDimension->getAxis() / 2.0f;
298  Rectangle* rB = new Rectangle(v, this->pDimension->getAxis() / 2.0f);
299
300  v.x = this->pDimension->getCenter()->x - this->pDimension->getAxis() / 2.0f;
301  Rectangle* rC = new Rectangle(v, this->pDimension->getAxis() / 2.0f);
302
303  v.z = this->pDimension->getCenter()->z + this->pDimension->getAxis() / 2.0f;
304  Rectangle* rD = new Rectangle(v, this->pDimension->getAxis() / 2.0f);
305
306  /* now propagate */
307  this->nodeA = new QuadtreeNode(pTriA, lenA, this->pVertices, this->numVertices, this->quadtree, this, rA, this->treeDepth + 1, this->maxDepth, (this->treeDepth + 1) * 10 + 0);
308
309  this->nodeB = new QuadtreeNode(pTriB, lenB, this->pVertices, this->numVertices, this->quadtree, this, rB, this->treeDepth + 1, this->maxDepth, (this->treeDepth + 1) * 10 + 1);
310
311  this->nodeC = new QuadtreeNode(pTriC, lenC, this->pVertices, this->numVertices, this->quadtree, this, rC, this->treeDepth + 1, this->maxDepth, (this->treeDepth + 1) * 10 + 2);
312
313  this->nodeD = new QuadtreeNode(pTriD, lenD, this->pVertices, this->numVertices, this->quadtree, this, rD, this->treeDepth + 1, this->maxDepth, (this->treeDepth + 1) * 10 + 3);
314  /* map the array references, this is for faster and automatical interfacing  \todo: use only array */
315  this->nodes[0] = this->nodeA;
316  this->nodes[1] = this->nodeB;
317  this->nodes[2] = this->nodeC;
318  this->nodes[3] = this->nodeD;
319}
320
321
322/**
323   \brief gets the maximal dimension of a model
324   \return the dimension of the AbstractModel as a Rectangle
325
326   The rectangle is x-z axis aligned. ATTENTION: if there are any vertices in the model, that exceed the
327   size of 999999.0, there probably will be some errors in the dimensions calculations. Write an email to
328   patrick@orxonox.ethz.ch if you will ever encounter a model bigger than 999999.0 units, I will realy be
329   happy to see orxonox used to extensivly :)
330 */
331Rectangle* QuadtreeNode::getDimFromModel()
332{
333  float            maxX, maxY;                       //!< the maximal coordinates axis
334  float            minX, minY;                       //!< minimal axis coorindates
335  const float*     pVertices;                        //!< pointer to the current vertices
336
337  maxX = -999999; maxY = -999999;
338  minX =  999999; minY =  999999;
339  /* get maximal/minimal x/y */
340  for( int i = 0; i < this->numTriangles; ++i)
341  {
342    for( int j = 0; j < 3; ++j)
343    {
344
345      pVertices = &this->pVertices[this->pTriangles[i]->indexToVertices[j]];
346      if( pVertices[0] > maxX)
347        maxX = pVertices[0];
348      if( pVertices[2] > maxY)
349        maxY = pVertices[2];
350
351      if( pVertices[0] < minX)
352        minX = pVertices[0];
353      if( pVertices[2] < minY)
354        minY = pVertices[2];
355    }
356  }
357
358  Rectangle* rect = new Rectangle();
359  rect->setCenter((maxX + minX) / 2.0f, 0.0f, (maxY + minY) / 2.0f); /* this is little strange, since y is in opengl the up vector */
360  rect->setAxis(fmax(((fabs(maxX) + fabs(minX)) / 2.0f), ((fabs(maxY) + fabs(minY)) / 2.0f)));
361
362  for( int i = 0; i < this->treeDepth; ++i)
363    PRINT(3)(" |");
364  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());
365  return rect;
366}
367
368
369/**
370 *  draws the debug quadtree boxes around the model
371 */
372void QuadtreeNode::drawTree(int depth, int drawMode) const
373{
374  if( depth == 0)
375  {
376    Vector t1 = *this->pDimension->getCenter();
377    float ax = this->pDimension->getAxis();
378    float h = 50.0f;
379
380    glBegin(GL_QUADS);
381    this->quadtree->getMaterial(this->indexNode)->select();
382    glVertex3f(t1.x + ax, h - depth * 10.0f, t1.z + ax);
383    glVertex3f(t1.x - ax, h - depth * 10.0f, t1.z + ax);
384    glVertex3f(t1.x - ax, h - depth * 10.0f, t1.z - ax);
385    glVertex3f(t1.x + ax, h - depth * 10.0f, t1.z - ax);
386    glEnd();
387  }
388  if( this->nodeA != NULL)
389    this->nodeA->drawTree(depth - 1, drawMode);
390  if( this->nodeB != NULL)
391    this->nodeB->drawTree(depth - 1, drawMode);
392  if( this->nodeC != NULL)
393    this->nodeC->drawTree(depth - 1, drawMode);
394  if( this->nodeD != NULL)
395    this->nodeD->drawTree(depth - 1, drawMode);
396}
Note: See TracBrowser for help on using the repository browser.