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