Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/lib/collision_detection/obb_tree_node.cc @ 4615

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

orxonox/trunk: modified the cd subproject, so it loads an md2 file now. the polygons are rendered black, so the bounding box is not viewable! Must render them in another color:)

File size: 18.7 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_COLLISION
17
18#include "obb_tree_node.h"
19#include "list.h"
20#include "obb.h"
21#include "vector.h"
22#include "abstract_model.h"
23
24#include <math.h>
25
26
27#define WANT_STREAM
28#define WANT_MATH
29#define WANT_FSTREAM
30
31
32#include "include.h"
33#include "newmat.h"
34#include "newmatap.h"
35#include "newmatio.h"
36
37
38
39
40using namespace std;
41
42
43/**
44   \brief standard constructor
45*/
46OBBTreeNode::OBBTreeNode ()
47{
48   this->setClassID(CL_OBB_TREE_NODE, "OBBTreeNode");
49
50}
51
52
53/**
54   \brief standard deconstructor
55
56*/
57OBBTreeNode::~OBBTreeNode ()
58{
59  // delete what has to be deleted here
60}
61
62
63
64/**
65   \brief creates a new BVTree or BVTree partition
66   \param depth: how much more depth-steps to go: if == 1 don't go any deeper!
67   \param verticesList: the list of vertices of the object - each vertices triple is interpreted as a triangle
68*/
69void OBBTreeNode::spawnBVTree(const int depth, sVec3D *verticesList, const int length)
70{
71  this->depth = depth;
72
73  if( likely( this->depth > 0))
74  {
75    this->bvElement = this->createBox();
76    this->calculateBoxAttributes(this->bvElement, verticesList, length);
77    this->forkBox(this->bvElement);
78  }
79}
80
81
82OBB* OBBTreeNode::createBox()
83{
84  return new OBB();
85}
86
87
88void OBBTreeNode::calculateBoxAttributes(OBB* box, sVec3D* verticesList, int length)
89{
90  float     facelet[length];                         //!< surface area of the i'th triangle of the convex hull
91  float     face;                                    //!< surface area of the entire convex hull
92  Vector    centroid[length];                        //!< centroid of the i'th convex hull
93  Vector    center;                                  //!< the center of the entire hull
94  Vector    p, q, r;                                 //!< holder of the polygon data, much more conveniant to work with Vector than sVec3d
95  Vector    t1, t2;                                  //!< temporary values
96  float     covariance[3][3];                        //!< the covariance matrix
97
98  this->numOfVertices = length;
99  this->vertices = verticesList;
100  box->vertices = verticesList;
101  box->numOfVertices = length;
102
103
104  /* fist compute all the convex hull face/facelets and centroids */
105  for(int i = 0; i < length; i+=3)          /* FIX-ME-QUICK: hops of 3, array indiscontinuity*/
106    {
107      p = verticesList[i];
108      q = verticesList[i +1];
109      r = verticesList[i + 2];
110
111      t1 = p - q; t2 = p - r;
112
113      /* finding the facelet surface via cross-product */
114      facelet[i] = 0.5f * fabs( t1.cross(t2).len() );
115      /* update the entire convex hull surface */
116      face += facelet[i];
117
118      /* calculate the cetroid of the hull triangles */
119      centroid[i] = (p + q + r) * 1/3;
120      /* now calculate the centroid of the entire convex hull, weighted average of triangle centroids */
121      center += centroid[i] * facelet[i];
122    }
123  /* take the average of the centroid sum */
124  center /= face;
125
126
127
128  /* now calculate the covariance matrix - if not written in three for-loops, it would compute faster: minor */
129  for(int j = 0; j < 3; ++j)
130    {
131      for(int k = 0; k < 3; ++k)
132        {
133          for(int i = 0; i < length; i+=3)
134            {
135              p = verticesList[i];
136              q = verticesList[i +1];
137              r = verticesList[i + 2];
138
139              covariance[j][k] = facelet[i] / (12.0f * face) * (9.0f * centroid[i][j] * centroid[i][k] + p[j]* p[k] +
140                                                                q[j] * q[k] + r[j]*r[k]) - center[j] * center[k];
141            }
142        }
143    }
144
145    printf("\nVertex Data:\n");
146    for(int i = 0; i < length; i++)
147    {
148      printf("vertex %i: %f, %f, %f\n", i, verticesList[i][0], verticesList[i][1], verticesList[i][2]);
149    }
150
151  printf("\nCovariance Matrix:\n");
152  for(int j = 0; j < 3; ++j)
153    {
154      printf(" |");
155      for(int k = 0; k < 3; ++k)
156        {
157          printf(" \b%f ", covariance[j][k]);
158        }
159      printf(" |\n");
160    }
161  printf("center: %f, %f, %f\n\n", center.x, center.y, center.z);
162
163
164  for(int i = 0; i < 3; ++i)
165    {
166
167      box->covarianceMatrix[i][0] = covariance[i][0];
168      box->covarianceMatrix[i][1] = covariance[i][1];
169      box->covarianceMatrix[i][3] = covariance[i][2];
170    }
171  *box->center = center;
172
173
174  /* now getting spanning vectors of the sub-space:
175     the eigenvectors of a symmertric matrix, such as the
176     covarience matrix are mutually orthogonal.
177     after normalizing them, they can be used as a the basis
178     vectors
179  */
180  Matrix                V(3,3);                               //!< for eigenvectors
181  DiagonalMatrix        D(3);                                 //!< for eigenvalues
182  SymmetricMatrix       C(3);                                 //!< for the covariance symmetrical matrix
183  Vector**              axis = new Vector*[3];                //!< the references to the obb axis
184
185  C(1,1) = covariance[0][0];
186  C(1,2) = covariance[0][1];
187  C(1,3) = covariance[0][2];
188  C(2,1) = covariance[1][0];
189  C(2,2) = covariance[1][1];
190  C(2,3) = covariance[1][2];
191  C(3,1) = covariance[2][0];
192  C(3,2) = covariance[2][1];
193  C(3,3) = covariance[2][2];
194
195  Jacobi(C, D, V);                                            /* do the jacobi decomposition */
196
197  printf("\nwe got a result! YES: \n");
198
199  for(int j = 1; j < 4; ++j)
200  {
201    printf(" |");
202    for(int k = 1; k < 4; ++k)
203    {
204      printf(" \b%f ", V(j, k));
205    }
206    printf(" |\n");
207  }
208
209  axis[0] = new Vector(V(1, 1), V(2, 1), V(3, 1));
210  axis[1] = new Vector(V(1, 2), V(2, 2), V(3, 2));
211  axis[2] = new Vector(V(1, 3), V(2, 3), V(3, 3));
212  box->axis = axis;
213
214  printf("\neigenvector: %f, %f, %f\n", box->axis[0]->x, box->axis[0]->y, box->axis[0]->z);
215  printf("eigenvector: %f, %f, %f\n", box->axis[1]->x, box->axis[1]->y, box->axis[1]->z);
216  printf("eigenvector: %f, %f, %f\n", box->axis[2]->x, box->axis[2]->y, box->axis[2]->z);
217
218
219  /* now get the axis length */
220  Line                ax[3];                                 //!< the axis
221  float*              halfLength = new float[3];             //!< half length of the axis
222  float               tmpLength;                             //!< tmp save point for the length
223  Plane               p0(*box->axis[0], *box->center);       //!< the axis planes
224  Plane               p1(*box->axis[1], *box->center);
225  Plane               p2(*box->axis[2], *box->center);
226
227  halfLength[0] = -1.0f;
228  for(int j = 0; j < length; ++j)
229  {
230    tmpLength = fabs(p0.distancePoint(vertices[j]));
231    if( tmpLength > halfLength[0])
232      halfLength[0] = tmpLength;
233  }
234
235  halfLength[1] = -1.0f;
236  for(int j = 0; j < length; ++j)
237  {
238    tmpLength = fabs(p1.distancePoint(vertices[j]));
239    if( tmpLength > halfLength[1])
240      halfLength[1] = tmpLength;
241  }
242
243  halfLength[2] = -1.0f;
244  for(int j = 0; j < length; ++j)
245  {
246    tmpLength = fabs(p2.distancePoint(vertices[j]));
247    if( tmpLength > halfLength[2])
248      halfLength[2] = tmpLength;
249  }
250
251  box->halfLength = halfLength;
252
253
254  printf("\nwe got length: \n");
255  for(int i = 0; i < 3; ++i)
256    printf("length[%i] = %f\n", i, box->halfLength[i]);
257}
258
259
260
261/**
262  \brief this separates an ob-box in the middle
263  \param box: the box to separate
264
265  this will separate the box into to smaller boxes. the separation is done along the middle of the longest axis
266 */
267void OBBTreeNode::forkBox(OBB* box)
268{
269  /* get the longest axis of the box */
270  float               aLength = -1.0f;                     //!< the length of the longest axis
271  int                 axisIndex = 0;                       //!< this is the nr of the longest axis
272
273  for(int i = 0; i < 3; ++i)
274  {
275    if( aLength < box->halfLength[i])
276    {
277      aLength = box->halfLength[i];
278      axisIndex = i;
279    }
280  }
281
282  printf("\nlongest axis is: nr %i with a half-length of: %f\n", axisIndex, aLength);
283
284
285  /* get the closest vertex near the center */
286  float               dist = 999999.0f;                    //!< the smallest distance to each vertex
287  float               tmpDist;                             //!< temporary distance
288  int                 vertexIndex;
289  Plane               middlePlane(*box->axis[axisIndex], *box->center); //!< the middle plane
290
291  for(int i = 0; i < box->numOfVertices; ++i)
292  {
293    tmpDist = fabs(middlePlane.distancePoint(box->vertices[i]));
294    if( tmpDist < dist)
295    {
296      dist = tmpDist;
297      vertexIndex = i;
298    }
299  }
300
301  printf("\nthe clostest vertex is nr: %i, with a dist of: %f\n", vertexIndex ,dist);
302
303
304  /* now definin the separation plane through this specified nearest point and partition
305    the points depending on which side they are located
306  */
307  Plane              separationPlane(*box->axis[axisIndex], box->vertices[vertexIndex]);  //!< separation plane
308  tList<sVec3D>      partition1;                           //!< the vertex partition 1
309  tList<sVec3D>      partition2;                           //!< the vertex partition 2
310
311  for(int i = 0; i < box->numOfVertices; ++i)
312  {
313    if( separationPlane.distancePoint(box->vertices[i]) > 0.0f)
314      partition1.add(&box->vertices[i]);
315    else
316      partition2.add(&box->vertices[i]);
317  }
318  partition1.add(&box->vertices[vertexIndex]);
319
320  printf("\npartition1: got %i vertices/ partition 2: got %i vertices\n", partition1.getSize(), partition2.getSize());
321
322
323  /* now comes the separation into two different sVec3D arrays */
324  tIterator<sVec3D>* iterator;                             //!< the iterator to go through the lists
325  sVec3D*            element;                              //!< the elements
326  int                index;                                //!< index storage place
327  sVec3D*            vertList1;                            //!< the vertex list 1
328  sVec3D*            vertList2;                            //!< the vertex list 2
329
330  vertList1 = new sVec3D[partition1.getSize()];
331  vertList2 = new sVec3D[partition2.getSize()];
332
333  iterator = partition1.getIterator();
334  element = iterator->nextElement();
335  index = 0;
336  while( element != NULL)
337  {
338    vertList1[index][0] = element[0][0];
339    vertList1[index][1] = element[0][1];
340    vertList1[index][2] = element[0][2];
341    ++index;
342    element = iterator->nextElement();
343  }
344
345  printf("\npartition 1:\n");
346  for(int i = 0; i < partition1.getSize(); ++i)
347  {
348    printf("v[%i][0] = %f\n", i, vertList1[i][0]);
349    printf("v[%i][1] = %f\n", i, vertList1[i][1]);
350    printf("v[%i][2] = %f\n", i, vertList1[i][2]);
351  }
352
353  iterator = partition2.getIterator();
354  element = iterator->nextElement();
355  index = 0;
356  while( element != NULL)
357  {
358    vertList2[index][0] = element[0][0];
359    vertList2[index][1] = element[0][1];
360    vertList2[index][2] = element[0][2];
361    ++index;
362    element = iterator->nextElement();
363  }
364
365  printf("\npartition 2:\n");
366  for(int i = 0; i < partition2.getSize(); ++i)
367  {
368    printf("v[%i][0] = %f\n", i, vertList2[i][0]);
369    printf("v[%i][1] = %f\n", i, vertList2[i][1]);
370    printf("v[%i][2] = %f\n", i, vertList2[i][2]);
371  }
372
373  /* now spawn the obb tree: create the nodes and descent */
374  OBBTreeNode*       node1 = new OBBTreeNode();
375  OBBTreeNode*       node2 = new OBBTreeNode();
376
377  this->nodeLeft = node1;
378  this->nodeRight = node2;
379
380  this->nodeLeft->spawnBVTree(this->depth - 1, vertList1, partition1.getSize());
381  this->nodeRight->spawnBVTree(this->depth - 1, vertList2, partition2.getSize());
382}
383
384
385void OBBTreeNode::collideWith(const BVTree &tree)
386{}
387
388
389void OBBTreeNode::drawBV(int currentDepth, const int depth) const
390{
391//   glBegin(GL_LINE_LOOP);
392//   glColor3f(1.0, 1.0, 1.0);
393//   for(int i = 0; i < this->bvElement->numOfVertices; ++i)
394//     {
395//       glVertex3f(this->bvElement->vertices[i][0], this->bvElement->vertices[i][1], this->bvElement->vertices[i][2]);
396//       //printf("v(%f, %f, %f)\n", this->vertices[i][0], this->vertices[i][1], this->vertices[i][2]);
397//     }
398//   glEnd();
399}
400
401
402void OBBTreeNode::drawBVPolygon(int currentDepth, const int depth) const
403{
404
405  /* draw world axes */
406  glBegin(GL_LINES);
407  glColor3f(0.0, 0.4, 0.3);
408  glVertex3f(0.0, 0.0, 0.0);
409  glVertex3f(3.0, 0.0, 0.0);
410
411  glVertex3f(0.0, 0.0, 0.0);
412  glVertex3f(0.0, 3.0, 0.0);
413
414  glVertex3f(0.0, 0.0, 0.0);
415  glVertex3f(0.0, 0.0, 3.0);
416  glEnd();
417
418
419
420  /* draw the obb axes */
421  glBegin(GL_LINES);
422  glColor3f(0.0, 0.4, 0.3);
423  glVertex3f(this->bvElement->center->x, this->bvElement->center->y, this->bvElement->center->z);
424  glVertex3f(this->bvElement->center->x + this->bvElement->axis[0]->x * this->bvElement->halfLength[0],
425             this->bvElement->center->y + this->bvElement->axis[0]->y * this->bvElement->halfLength[0],
426             this->bvElement->center->z + this->bvElement->axis[0]->z * this->bvElement->halfLength[0]);
427
428  glVertex3f(this->bvElement->center->x, this->bvElement->center->y, this->bvElement->center->z);
429  glVertex3f(this->bvElement->center->x + this->bvElement->axis[1]->x * this->bvElement->halfLength[1],
430             this->bvElement->center->y + this->bvElement->axis[1]->y * this->bvElement->halfLength[1],
431             this->bvElement->center->z + this->bvElement->axis[1]->z * this->bvElement->halfLength[1]);
432
433  glVertex3f(this->bvElement->center->x, this->bvElement->center->y, this->bvElement->center->z);
434  glVertex3f(this->bvElement->center->x + this->bvElement->axis[2]->x * this->bvElement->halfLength[2],
435             this->bvElement->center->y + this->bvElement->axis[2]->y * this->bvElement->halfLength[2],
436             this->bvElement->center->z + this->bvElement->axis[2]->z * this->bvElement->halfLength[2]);
437  glEnd();
438
439
440  Vector cen = *this->bvElement->center;
441  Vector** axis = this->bvElement->axis;
442  float* len = this->bvElement->halfLength;
443
444  /* draw bounding box */
445  glBegin(GL_LINE_LOOP);
446  glColor3f(0.3, 0.4, 0.7);
447  glVertex3f(cen.x + axis[0]->x * len[0] + axis[1]->x * len[1] + axis[2]->x * len[2],
448             cen.y + axis[0]->y * len[0] + axis[1]->y * len[1] + axis[2]->y * len[2],
449             cen.z + axis[0]->z * len[0] + axis[1]->z * len[1] + axis[2]->z * len[2]);
450  glVertex3f(cen.x + axis[0]->x * len[0] + axis[1]->x * len[1] - axis[2]->x * len[2],
451             cen.y + axis[0]->y * len[0] + axis[1]->y * len[1] - axis[2]->y * len[2],
452             cen.z + axis[0]->z * len[0] + axis[1]->z * len[1] - axis[2]->z * len[2]);
453  glVertex3f(cen.x + axis[0]->x * len[0] - axis[1]->x * len[1] - axis[2]->x * len[2],
454             cen.y + axis[0]->y * len[0] - axis[1]->y * len[1] - axis[2]->y * len[2],
455             cen.z + axis[0]->z * len[0] - axis[1]->z * len[1] - axis[2]->z * len[2]);
456  glVertex3f(cen.x + axis[0]->x * len[0] - axis[1]->x * len[1] + axis[2]->x * len[2],
457             cen.y + axis[0]->y * len[0] - axis[1]->y * len[1] + axis[2]->y * len[2],
458             cen.z + axis[0]->z * len[0] - axis[1]->z * len[1] + axis[2]->z * len[2]);
459  glEnd();
460
461  glBegin(GL_LINE_LOOP);
462  glVertex3f(cen.x + axis[0]->x * len[0] - axis[1]->x * len[1] + axis[2]->x * len[2],
463             cen.y + axis[0]->y * len[0] - axis[1]->y * len[1] + axis[2]->y * len[2],
464             cen.z + axis[0]->z * len[0] - axis[1]->z * len[1] + axis[2]->z * len[2]);
465  glVertex3f(cen.x + axis[0]->x * len[0] - axis[1]->x * len[1] - axis[2]->x * len[2],
466             cen.y + axis[0]->y * len[0] - axis[1]->y * len[1] - axis[2]->y * len[2],
467             cen.z + axis[0]->z * len[0] - axis[1]->z * len[1] - axis[2]->z * len[2]);
468  glVertex3f(cen.x - axis[0]->x * len[0] - axis[1]->x * len[1] - axis[2]->x * len[2],
469             cen.y - axis[0]->y * len[0] - axis[1]->y * len[1] - axis[2]->y * len[2],
470             cen.z - axis[0]->z * len[0] - axis[1]->z * len[1] - axis[2]->z * len[2]);
471  glVertex3f(cen.x - axis[0]->x * len[0] - axis[1]->x * len[1] + axis[2]->x * len[2],
472             cen.y - axis[0]->y * len[0] - axis[1]->y * len[1] + axis[2]->y * len[2],
473             cen.z - axis[0]->z * len[0] - axis[1]->z * len[1] + axis[2]->z * len[2]);
474  glEnd();
475
476  glBegin(GL_LINE_LOOP);
477  glVertex3f(cen.x - axis[0]->x * len[0] - axis[1]->x * len[1] + axis[2]->x * len[2],
478             cen.y - axis[0]->y * len[0] - axis[1]->y * len[1] + axis[2]->y * len[2],
479             cen.z - axis[0]->z * len[0] - axis[1]->z * len[1] + axis[2]->z * len[2]);
480  glVertex3f(cen.x - axis[0]->x * len[0] - axis[1]->x * len[1] - axis[2]->x * len[2],
481             cen.y - axis[0]->y * len[0] - axis[1]->y * len[1] - axis[2]->y * len[2],
482             cen.z - axis[0]->z * len[0] - axis[1]->z * len[1] - axis[2]->z * len[2]);
483  glVertex3f(cen.x - axis[0]->x * len[0] + axis[1]->x * len[1] - axis[2]->x * len[2],
484             cen.y - axis[0]->y * len[0] + axis[1]->y * len[1] - axis[2]->y * len[2],
485             cen.z - axis[0]->z * len[0] + axis[1]->z * len[1] - axis[2]->z * len[2]);
486  glVertex3f(cen.x - axis[0]->x * len[0] + axis[1]->x * len[1] + axis[2]->x * len[2],
487             cen.y - axis[0]->y * len[0] + axis[1]->y * len[1] + axis[2]->y * len[2],
488             cen.z - axis[0]->z * len[0] + axis[1]->z * len[1] + axis[2]->z * len[2]);
489  glEnd();
490
491  glBegin(GL_LINE_LOOP);
492  glVertex3f(cen.x - axis[0]->x * len[0] + axis[1]->x * len[1] - axis[2]->x * len[2],
493             cen.y - axis[0]->y * len[0] + axis[1]->y * len[1] - axis[2]->y * len[2],
494             cen.z - axis[0]->z * len[0] + axis[1]->z * len[1] - axis[2]->z * len[2]);
495  glVertex3f(cen.x - axis[0]->x * len[0] + axis[1]->x * len[1] + axis[2]->x * len[2],
496             cen.y - axis[0]->y * len[0] + axis[1]->y * len[1] + axis[2]->y * len[2],
497             cen.z - axis[0]->z * len[0] + axis[1]->z * len[1] + axis[2]->z * len[2]);
498  glVertex3f(cen.x + axis[0]->x * len[0] + axis[1]->x * len[1] + axis[2]->x * len[2],
499             cen.y + axis[0]->y * len[0] + axis[1]->y * len[1] + axis[2]->y * len[2],
500             cen.z + axis[0]->z * len[0] + axis[1]->z * len[1] + axis[2]->z * len[2]);
501  glVertex3f(cen.x + axis[0]->x * len[0] + axis[1]->x * len[1] - axis[2]->x * len[2],
502             cen.y + axis[0]->y * len[0] + axis[1]->y * len[1] - axis[2]->y * len[2],
503             cen.z + axis[0]->z * len[0] + axis[1]->z * len[1] - axis[2]->z * len[2]);
504  glEnd();
505
506/*
507  glVertex3f(cen.x - axis[0]->x * len[0] + axis[1]->x * len[1] - axis[2]->x * len[2],
508             cen.y - axis[0]->y * len[0] + axis[1]->y * len[1] - axis[2]->y * len[2],
509             cen.z - axis[0]->z * len[0] + axis[1]->z * len[1] - axis[2]->z * len[2]);
510  glVertex3f(cen.x - axis[0]->x * len[0] + axis[1]->x * len[1] + axis[2]->x * len[2],
511             cen.y - axis[0]->y * len[0] + axis[1]->y * len[1] + axis[2]->y * len[2],
512             cen.z - axis[0]->z * len[0] + axis[1]->z * len[1] + axis[2]->z * len[2]);*/
513
514
515  glEnd();
516
517
518}
519
520
521void OBBTreeNode::drawBVBlended(int currentDepth, const int depth) const
522{}
523
524
525void OBBTreeNode::debug()
526{
527
528  /*
529  for(int i = 0; i < length; i++)
530    {
531      printf("vertex %i: %f, %f, %f\n", i, verticesList[i][0], verticesList[i][1], verticesList[i][2]);
532    }
533  */
534}
Note: See TracBrowser for help on using the repository browser.