Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: solved last accuracy problem. all is well now. the box could be a little more tight

File size: 14.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: the depth of the tree
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->bvElement = this->createBox();
72  this->calculateBoxAttributes(this->bvElement, verticesList, length);
73  this->forkBox(this->bvElement);
74}
75
76
77OBB* OBBTreeNode::createBox()
78{
79  return new OBB();
80}
81
82
83void OBBTreeNode::calculateBoxAttributes(OBB* box, sVec3D* verticesList, int length)
84{
85  float     facelet[length];                         //!< surface area of the i'th triangle of the convex hull
86  float     face;                                    //!< surface area of the entire convex hull
87  Vector    centroid[length];                        //!< centroid of the i'th convex hull
88  Vector    center;                                  //!< the center of the entire hull
89  Vector    p, q, r;                                 //!< holder of the polygon data, much more conveniant to work with Vector than sVec3d
90  Vector    t1, t2;                                  //!< temporary values
91  float     covariance[3][3];                        //!< the covariance matrix
92
93  this->numOfVertices = length;
94  this->vertices = verticesList;
95  box->vertices = verticesList;
96  box->numOfVertices = length;
97
98
99  /* fist compute all the convex hull face/facelets and centroids */
100  for(int i = 0; i < length; i+=3)          /* FIX-ME-QUICK: hops of 3, array indiscontinuity*/
101    {
102      p = verticesList[i];
103      q = verticesList[i +1];
104      r = verticesList[i + 2];
105
106      t1 = p - q; t2 = p - r;
107
108      /* finding the facelet surface via cross-product */
109      facelet[i] = 0.5f * fabs( t1.cross(t2).len() );
110      /* update the entire convex hull surface */
111      face += facelet[i];
112
113      /* calculate the cetroid of the hull triangles */
114      centroid[i] = (p + q + r) * 1/3;
115      /* now calculate the centroid of the entire convex hull, weighted average of triangle centroids */
116      center += centroid[i] * facelet[i];
117    }
118  /* take the average of the centroid sum */
119  center /= face;
120
121
122
123  /* now calculate the covariance matrix - if not written in three for-loops, it would compute faster: minor */
124  for(int j = 0; j < 3; ++j)
125    {
126      for(int k = 0; k < 3; ++k)
127        {
128          for(int i = 0; i < length; i+=3)
129            {
130              p = verticesList[i];
131              q = verticesList[i +1];
132              r = verticesList[i + 2];
133
134              covariance[j][k] = facelet[i] / (12.0f * face) * (9.0f * centroid[i][j] * centroid[i][k] + p[j]* p[k] +
135                                                                q[j] * q[k] + r[j]*r[k]) - center[j] * center[k];
136            }
137        }
138    }
139
140    printf("\nVertex Data:\n");
141    for(int i = 0; i < length; i++)
142    {
143      printf("vertex %i: %f, %f, %f\n", i, verticesList[i][0], verticesList[i][1], verticesList[i][2]);
144    }
145
146  printf("\nCovariance Matrix:\n");
147  for(int j = 0; j < 3; ++j)
148    {
149      printf(" |");
150      for(int k = 0; k < 3; ++k)
151        {
152          printf(" \b%f ", covariance[j][k]);
153        }
154      printf(" |\n");
155    }
156  printf("center: %f, %f, %f\n\n", center.x, center.y, center.z);
157
158
159  for(int i = 0; i < 3; ++i)
160    {
161
162      box->covarianceMatrix[i][0] = covariance[i][0];
163      box->covarianceMatrix[i][1] = covariance[i][1];
164      box->covarianceMatrix[i][3] = covariance[i][2];
165    }
166  *box->center = center;
167
168
169  /* now getting spanning vectors of the sub-space:
170     the eigenvectors of a symmertric matrix, such as the
171     covarience matrix are mutually orthogonal.
172     after normalizing them, they can be used as a the basis
173     vectors
174  */
175  Matrix                V(3,3);                               //!< for eigenvectors
176  DiagonalMatrix        D(3);                                 //!< for eigenvalues
177  SymmetricMatrix       C(3);                                 //!< for the covariance symmetrical matrix
178  Vector**              axis = new Vector*[3];                //!< the references to the obb axis
179
180  C(1,1) = covariance[0][0];
181  C(1,2) = covariance[0][1];
182  C(1,3) = covariance[0][2];
183  C(2,1) = covariance[1][0];
184  C(2,2) = covariance[1][1];
185  C(2,3) = covariance[1][2];
186  C(3,1) = covariance[2][0];
187  C(3,2) = covariance[2][1];
188  C(3,3) = covariance[2][2];
189
190  Jacobi(C, D, V);                                            /* do the jacobi decomposition */
191
192  printf("we got a result! YES: \n");
193
194  for(int j = 1; j < 4; ++j)
195  {
196    printf(" |");
197    for(int k = 1; k < 4; ++k)
198    {
199      printf(" \b%f ", V(j, k));
200    }
201    printf(" |\n");
202  }
203
204  axis[0] = new Vector(V(1, 1), V(2, 1), V(3, 1));
205  axis[1] = new Vector(V(1, 2), V(2, 2), V(3, 2));
206  axis[2] = new Vector(V(1, 3), V(2, 3), V(3, 3));
207  box->axis = axis;
208
209  printf("eigenvector: %f, %f, %f\n", box->axis[0]->x, box->axis[0]->y, box->axis[0]->z);
210  printf("eigenvector: %f, %f, %f\n", box->axis[1]->x, box->axis[1]->y, box->axis[1]->z);
211  printf("eigenvector: %f, %f, %f\n", box->axis[2]->x, box->axis[2]->y, box->axis[2]->z);
212
213
214  /* now get the axis length */
215  Line                ax[3];                                 //!< the axis
216  float*              halfLength = new float[3];             //!< half length of the axis
217  float               tmpLength;                             //!< tmp save point for the length
218
219
220  Plane p0(*box->axis[0], *box->center);
221  Plane p1(*box->axis[1], *box->center);
222  Plane p2(*box->axis[2], *box->center);
223
224  halfLength[0] = -1.0f;
225  for(int j = 0; j < length; ++j)
226  {
227    tmpLength = fabs(p0.distancePoint(vertices[j]));
228    if( tmpLength > halfLength[0])
229      halfLength[0] = tmpLength;
230  }
231
232  halfLength[1] = -1.0f;
233  for(int j = 0; j < length; ++j)
234  {
235    tmpLength = fabs(p1.distancePoint(vertices[j]));
236    if( tmpLength > halfLength[1])
237      halfLength[1] = tmpLength;
238  }
239
240  halfLength[2] = -1.0f;
241  for(int j = 0; j < length; ++j)
242  {
243    tmpLength = fabs(p2.distancePoint(vertices[j]));
244    if( tmpLength > halfLength[2])
245      halfLength[2] = tmpLength;
246  }
247
248  box->halfLength = halfLength;
249
250
251  printf("we got length: \n");
252  for(int i = 0; i < 3; ++i)
253    printf("length[%i] = %f\n", i, box->halfLength[i]);
254}
255
256
257void OBBTreeNode::forkBox(OBB* box)
258{
259  /* get the longest axis of the box */
260  float aLength = -1.0f;
261  int axisNr = 0;
262  for(int i = 0; i < 3; ++i)
263    {
264      if( aLength < box->axis[i]->len())
265        {
266          aLength = box->axis[i]->len();
267          axisNr = i;
268        }
269    }
270
271  /* get the closest vertex near the center */
272
273}
274
275
276void OBBTreeNode::collideWith(const BVTree &tree)
277{}
278
279
280void OBBTreeNode::drawBV(int currentDepth, const int depth) const
281{
282  glBegin(GL_LINE_LOOP);
283  glColor3f(1.0, 1.0, 1.0);
284  for(int i = 0; i < this->bvElement->numOfVertices; ++i)
285    {
286      glVertex3f(this->bvElement->vertices[i][0], this->bvElement->vertices[i][1], this->bvElement->vertices[i][2]);
287      //printf("v(%f, %f, %f)\n", this->vertices[i][0], this->vertices[i][1], this->vertices[i][2]);
288    }
289  glEnd();
290}
291
292
293void OBBTreeNode::drawBVPolygon(int currentDepth, const int depth) const
294{
295
296  /* draw world axes */
297  glBegin(GL_LINES);
298  glColor3f(0.0, 0.4, 0.3);
299  glVertex3f(0.0, 0.0, 0.0);
300  glVertex3f(3.0, 0.0, 0.0);
301
302  glVertex3f(0.0, 0.0, 0.0);
303  glVertex3f(0.0, 3.0, 0.0);
304
305  glVertex3f(0.0, 0.0, 0.0);
306  glVertex3f(0.0, 0.0, 3.0);
307  glEnd();
308
309
310
311  /* draw the obb axes */
312  glBegin(GL_LINES);
313  glColor3f(0.0, 0.4, 0.3);
314  glVertex3f(this->bvElement->center->x, this->bvElement->center->y, this->bvElement->center->z);
315  glVertex3f(this->bvElement->center->x + this->bvElement->axis[0]->x * this->bvElement->halfLength[0],
316             this->bvElement->center->y + this->bvElement->axis[0]->y * this->bvElement->halfLength[0],
317             this->bvElement->center->z + this->bvElement->axis[0]->z * this->bvElement->halfLength[0]);
318
319  glVertex3f(this->bvElement->center->x, this->bvElement->center->y, this->bvElement->center->z);
320  glVertex3f(this->bvElement->center->x + this->bvElement->axis[1]->x * this->bvElement->halfLength[1],
321             this->bvElement->center->y + this->bvElement->axis[1]->y * this->bvElement->halfLength[1],
322             this->bvElement->center->z + this->bvElement->axis[1]->z * this->bvElement->halfLength[1]);
323
324  glVertex3f(this->bvElement->center->x, this->bvElement->center->y, this->bvElement->center->z);
325  glVertex3f(this->bvElement->center->x + this->bvElement->axis[2]->x * this->bvElement->halfLength[2],
326             this->bvElement->center->y + this->bvElement->axis[2]->y * this->bvElement->halfLength[2],
327             this->bvElement->center->z + this->bvElement->axis[2]->z * this->bvElement->halfLength[2]);
328  glEnd();
329
330
331  Vector cen = *this->bvElement->center;
332  Vector** axis = this->bvElement->axis;
333  float* len = this->bvElement->halfLength;
334
335  /* draw bounding box */
336  glBegin(GL_LINE_LOOP);
337  glColor3f(0.3, 0.4, 0.7);
338  glVertex3f(cen.x + axis[0]->x * len[0] + axis[1]->x * len[1] + axis[2]->x * len[2],
339             cen.y + axis[0]->y * len[0] + axis[1]->y * len[1] + axis[2]->y * len[2],
340             cen.z + axis[0]->z * len[0] + axis[1]->z * len[1] + axis[2]->z * len[2]);
341  glVertex3f(cen.x + axis[0]->x * len[0] + axis[1]->x * len[1] - axis[2]->x * len[2],
342             cen.y + axis[0]->y * len[0] + axis[1]->y * len[1] - axis[2]->y * len[2],
343             cen.z + axis[0]->z * len[0] + axis[1]->z * len[1] - axis[2]->z * len[2]);
344  glVertex3f(cen.x + axis[0]->x * len[0] - axis[1]->x * len[1] - axis[2]->x * len[2],
345             cen.y + axis[0]->y * len[0] - axis[1]->y * len[1] - axis[2]->y * len[2],
346             cen.z + axis[0]->z * len[0] - axis[1]->z * len[1] - axis[2]->z * len[2]);
347  glVertex3f(cen.x + axis[0]->x * len[0] - axis[1]->x * len[1] + axis[2]->x * len[2],
348             cen.y + axis[0]->y * len[0] - axis[1]->y * len[1] + axis[2]->y * len[2],
349             cen.z + axis[0]->z * len[0] - axis[1]->z * len[1] + axis[2]->z * len[2]);
350  glEnd();
351
352  glBegin(GL_LINE_LOOP);
353  glVertex3f(cen.x + axis[0]->x * len[0] - axis[1]->x * len[1] + axis[2]->x * len[2],
354             cen.y + axis[0]->y * len[0] - axis[1]->y * len[1] + axis[2]->y * len[2],
355             cen.z + axis[0]->z * len[0] - axis[1]->z * len[1] + axis[2]->z * len[2]);
356  glVertex3f(cen.x + axis[0]->x * len[0] - axis[1]->x * len[1] - axis[2]->x * len[2],
357             cen.y + axis[0]->y * len[0] - axis[1]->y * len[1] - axis[2]->y * len[2],
358             cen.z + axis[0]->z * len[0] - axis[1]->z * len[1] - axis[2]->z * len[2]);
359  glVertex3f(cen.x - axis[0]->x * len[0] - axis[1]->x * len[1] - axis[2]->x * len[2],
360             cen.y - axis[0]->y * len[0] - axis[1]->y * len[1] - axis[2]->y * len[2],
361             cen.z - axis[0]->z * len[0] - axis[1]->z * len[1] - axis[2]->z * len[2]);
362  glVertex3f(cen.x - axis[0]->x * len[0] - axis[1]->x * len[1] + axis[2]->x * len[2],
363             cen.y - axis[0]->y * len[0] - axis[1]->y * len[1] + axis[2]->y * len[2],
364             cen.z - axis[0]->z * len[0] - axis[1]->z * len[1] + axis[2]->z * len[2]);
365  glEnd();
366
367  glBegin(GL_LINE_LOOP);
368  glVertex3f(cen.x - axis[0]->x * len[0] - axis[1]->x * len[1] + axis[2]->x * len[2],
369             cen.y - axis[0]->y * len[0] - axis[1]->y * len[1] + axis[2]->y * len[2],
370             cen.z - axis[0]->z * len[0] - axis[1]->z * len[1] + axis[2]->z * len[2]);
371  glVertex3f(cen.x - axis[0]->x * len[0] - axis[1]->x * len[1] - axis[2]->x * len[2],
372             cen.y - axis[0]->y * len[0] - axis[1]->y * len[1] - axis[2]->y * len[2],
373             cen.z - axis[0]->z * len[0] - axis[1]->z * len[1] - axis[2]->z * len[2]);
374  glVertex3f(cen.x - axis[0]->x * len[0] + axis[1]->x * len[1] - axis[2]->x * len[2],
375             cen.y - axis[0]->y * len[0] + axis[1]->y * len[1] - axis[2]->y * len[2],
376             cen.z - axis[0]->z * len[0] + axis[1]->z * len[1] - axis[2]->z * len[2]);
377  glVertex3f(cen.x - axis[0]->x * len[0] + axis[1]->x * len[1] + axis[2]->x * len[2],
378             cen.y - axis[0]->y * len[0] + axis[1]->y * len[1] + axis[2]->y * len[2],
379             cen.z - axis[0]->z * len[0] + axis[1]->z * len[1] + axis[2]->z * len[2]);
380  glEnd();
381
382  glBegin(GL_LINE_LOOP);
383  glVertex3f(cen.x - axis[0]->x * len[0] + axis[1]->x * len[1] - axis[2]->x * len[2],
384             cen.y - axis[0]->y * len[0] + axis[1]->y * len[1] - axis[2]->y * len[2],
385             cen.z - axis[0]->z * len[0] + axis[1]->z * len[1] - axis[2]->z * len[2]);
386  glVertex3f(cen.x - axis[0]->x * len[0] + axis[1]->x * len[1] + axis[2]->x * len[2],
387             cen.y - axis[0]->y * len[0] + axis[1]->y * len[1] + axis[2]->y * len[2],
388             cen.z - axis[0]->z * len[0] + axis[1]->z * len[1] + axis[2]->z * len[2]);
389  glVertex3f(cen.x + axis[0]->x * len[0] + axis[1]->x * len[1] + axis[2]->x * len[2],
390             cen.y + axis[0]->y * len[0] + axis[1]->y * len[1] + axis[2]->y * len[2],
391             cen.z + axis[0]->z * len[0] + axis[1]->z * len[1] + axis[2]->z * len[2]);
392  glVertex3f(cen.x + axis[0]->x * len[0] + axis[1]->x * len[1] - axis[2]->x * len[2],
393             cen.y + axis[0]->y * len[0] + axis[1]->y * len[1] - axis[2]->y * len[2],
394             cen.z + axis[0]->z * len[0] + axis[1]->z * len[1] - axis[2]->z * len[2]);
395  glEnd();
396
397/*
398  glVertex3f(cen.x - axis[0]->x * len[0] + axis[1]->x * len[1] - axis[2]->x * len[2],
399             cen.y - axis[0]->y * len[0] + axis[1]->y * len[1] - axis[2]->y * len[2],
400             cen.z - axis[0]->z * len[0] + axis[1]->z * len[1] - axis[2]->z * len[2]);
401  glVertex3f(cen.x - axis[0]->x * len[0] + axis[1]->x * len[1] + axis[2]->x * len[2],
402             cen.y - axis[0]->y * len[0] + axis[1]->y * len[1] + axis[2]->y * len[2],
403             cen.z - axis[0]->z * len[0] + axis[1]->z * len[1] + axis[2]->z * len[2]);*/
404
405
406  glEnd();
407
408
409}
410
411
412void OBBTreeNode::drawBVBlended(int currentDepth, const int depth) const
413{}
414
415
416void OBBTreeNode::debug()
417{
418
419  /*
420  for(int i = 0; i < length; i++)
421    {
422      printf("vertex %i: %f, %f, %f\n", i, verticesList[i][0], verticesList[i][1], verticesList[i][2]);
423    }
424  */
425}
Note: See TracBrowser for help on using the repository browser.