Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: now drawing o-boundingbox as a polygon, seems to be one minor accuracy problem

File size: 14.6 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  ax[0].r = *box->center; ax[0].a = *box->axis[0];
220  ax[1].r = *box->center; ax[1].a = *box->axis[1];
221  ax[2].r = *box->center; ax[2].a = *box->axis[2];
222
223  Plane p0(*box->axis[0], *box->center);
224  Plane p1(*box->axis[1], *box->center);
225  Plane p2(*box->axis[2], *box->center);
226
227
228
229  halfLength[0] = 0.0f;
230  for(int j = 0; j < length; ++j)
231  {
232    tmpLength = p0.distancePoint(vertices[j]);
233    if( tmpLength > halfLength[0])
234      halfLength[0] = tmpLength;
235  }
236
237
238  halfLength[1] = 0.0f;
239  for(int j = 0; j < length; ++j)
240  {
241    tmpLength = p1.distancePoint(vertices[j]);
242    if( tmpLength > halfLength[1])
243      halfLength[1] = tmpLength;
244  }
245
246  halfLength[2] = 0.0f;
247  for(int j = 0; j < length; ++j)
248  {
249    tmpLength = p2.distancePoint(vertices[j]);
250    if( tmpLength > halfLength[2])
251      halfLength[2] = tmpLength;
252  }
253
254  box->halfLength = halfLength;
255
256
257
258  printf("we got length: \n");
259  for(int i = 0; i < 3; ++i)
260    printf("length[%i] = %f\n", i, box->halfLength[i]);
261}
262
263
264void OBBTreeNode::forkBox(OBB* box)
265{
266  /* get the longest axis of the box */
267  float aLength = -1.0f;
268  int axisNr = 0;
269  for(int i = 0; i < 3; ++i)
270    {
271      if( aLength < box->axis[i]->len())
272        {
273          aLength = box->axis[i]->len();
274          axisNr = i;
275        }
276    }
277
278  /* get the closest vertex near the center */
279
280}
281
282
283void OBBTreeNode::collideWith(const BVTree &tree)
284{}
285
286
287void OBBTreeNode::drawBV(int currentDepth, const int depth) const
288{
289  glBegin(GL_LINE_LOOP);
290  glColor3f(1.0, 1.0, 1.0);
291  for(int i = 0; i < this->bvElement->numOfVertices; ++i)
292    {
293      glVertex3f(this->bvElement->vertices[i][0], this->bvElement->vertices[i][1], this->bvElement->vertices[i][2]);
294      //printf("v(%f, %f, %f)\n", this->vertices[i][0], this->vertices[i][1], this->vertices[i][2]);
295    }
296  glEnd();
297}
298
299
300void OBBTreeNode::drawBVPolygon(int currentDepth, const int depth) const
301{
302
303  /* draw the obb axes */
304  glBegin(GL_LINES);
305  glColor3f(0.0, 0.4, 0.3);
306  glVertex3f(this->bvElement->center->x, this->bvElement->center->y, this->bvElement->center->z);
307  glVertex3f(this->bvElement->center->x + this->bvElement->axis[0]->x * this->bvElement->halfLength[0],
308             this->bvElement->center->y + this->bvElement->axis[0]->y * this->bvElement->halfLength[0],
309             this->bvElement->center->z + this->bvElement->axis[0]->z * this->bvElement->halfLength[0]);
310
311  glVertex3f(this->bvElement->center->x, this->bvElement->center->y, this->bvElement->center->z);
312  glVertex3f(this->bvElement->center->x + this->bvElement->axis[1]->x * this->bvElement->halfLength[1],
313             this->bvElement->center->y + this->bvElement->axis[1]->y * this->bvElement->halfLength[1],
314             this->bvElement->center->z + this->bvElement->axis[1]->z * this->bvElement->halfLength[1]);
315
316  glVertex3f(this->bvElement->center->x, this->bvElement->center->y, this->bvElement->center->z);
317  glVertex3f(this->bvElement->center->x + this->bvElement->axis[2]->x * this->bvElement->halfLength[2],
318             this->bvElement->center->y + this->bvElement->axis[2]->y * this->bvElement->halfLength[2],
319             this->bvElement->center->z + this->bvElement->axis[2]->z * this->bvElement->halfLength[2]);
320  glEnd();
321
322
323  Vector cen = *this->bvElement->center;
324  Vector** axis = this->bvElement->axis;
325  float* len = this->bvElement->halfLength;
326
327  /* draw bounding box */
328  glBegin(GL_LINE_LOOP);
329  glColor3f(0.3, 0.4, 0.7);
330  glVertex3f(cen.x + axis[0]->x * len[0] + axis[1]->x * len[1] + axis[2]->x * len[2],
331             cen.y + axis[0]->y * len[0] + axis[1]->y * len[1] + axis[2]->y * len[2],
332             cen.z + axis[0]->z * len[0] + axis[1]->z * len[1] + axis[2]->z * len[2]);
333  glVertex3f(cen.x + axis[0]->x * len[0] + axis[1]->x * len[1] - axis[2]->x * len[2],
334             cen.y + axis[0]->y * len[0] + axis[1]->y * len[1] - axis[2]->y * len[2],
335             cen.z + axis[0]->z * len[0] + axis[1]->z * len[1] - axis[2]->z * len[2]);
336  glVertex3f(cen.x + axis[0]->x * len[0] - axis[1]->x * len[1] - axis[2]->x * len[2],
337             cen.y + axis[0]->y * len[0] - axis[1]->y * len[1] - axis[2]->y * len[2],
338             cen.z + axis[0]->z * len[0] - axis[1]->z * len[1] - axis[2]->z * len[2]);
339  glVertex3f(cen.x + axis[0]->x * len[0] - axis[1]->x * len[1] + axis[2]->x * len[2],
340             cen.y + axis[0]->y * len[0] - axis[1]->y * len[1] + axis[2]->y * len[2],
341             cen.z + axis[0]->z * len[0] - axis[1]->z * len[1] + axis[2]->z * len[2]);
342  glEnd();
343
344  glBegin(GL_LINE_LOOP);
345  glVertex3f(cen.x + axis[0]->x * len[0] - axis[1]->x * len[1] + axis[2]->x * len[2],
346             cen.y + axis[0]->y * len[0] - axis[1]->y * len[1] + axis[2]->y * len[2],
347             cen.z + axis[0]->z * len[0] - axis[1]->z * len[1] + axis[2]->z * len[2]);
348  glVertex3f(cen.x + axis[0]->x * len[0] - axis[1]->x * len[1] - axis[2]->x * len[2],
349             cen.y + axis[0]->y * len[0] - axis[1]->y * len[1] - axis[2]->y * len[2],
350             cen.z + axis[0]->z * len[0] - axis[1]->z * len[1] - axis[2]->z * len[2]);
351  glVertex3f(cen.x - axis[0]->x * len[0] - axis[1]->x * len[1] - axis[2]->x * len[2],
352             cen.y - axis[0]->y * len[0] - axis[1]->y * len[1] - axis[2]->y * len[2],
353             cen.z - axis[0]->z * len[0] - axis[1]->z * len[1] - axis[2]->z * len[2]);
354  glVertex3f(cen.x - axis[0]->x * len[0] - axis[1]->x * len[1] + axis[2]->x * len[2],
355             cen.y - axis[0]->y * len[0] - axis[1]->y * len[1] + axis[2]->y * len[2],
356             cen.z - axis[0]->z * len[0] - axis[1]->z * len[1] + axis[2]->z * len[2]);
357  glEnd();
358
359  glBegin(GL_LINE_LOOP);
360  glVertex3f(cen.x - axis[0]->x * len[0] - axis[1]->x * len[1] + axis[2]->x * len[2],
361             cen.y - axis[0]->y * len[0] - axis[1]->y * len[1] + axis[2]->y * len[2],
362             cen.z - axis[0]->z * len[0] - axis[1]->z * len[1] + axis[2]->z * len[2]);
363  glVertex3f(cen.x - axis[0]->x * len[0] - axis[1]->x * len[1] - axis[2]->x * len[2],
364             cen.y - axis[0]->y * len[0] - axis[1]->y * len[1] - axis[2]->y * len[2],
365             cen.z - axis[0]->z * len[0] - axis[1]->z * len[1] - axis[2]->z * len[2]);
366  glVertex3f(cen.x - axis[0]->x * len[0] + axis[1]->x * len[1] - axis[2]->x * len[2],
367             cen.y - axis[0]->y * len[0] + axis[1]->y * len[1] - axis[2]->y * len[2],
368             cen.z - axis[0]->z * len[0] + axis[1]->z * len[1] - axis[2]->z * len[2]);
369  glVertex3f(cen.x - axis[0]->x * len[0] + axis[1]->x * len[1] + axis[2]->x * len[2],
370             cen.y - axis[0]->y * len[0] + axis[1]->y * len[1] + axis[2]->y * len[2],
371             cen.z - axis[0]->z * len[0] + axis[1]->z * len[1] + axis[2]->z * len[2]);
372  glEnd();
373
374  glBegin(GL_LINE_LOOP);
375  glVertex3f(cen.x - axis[0]->x * len[0] + axis[1]->x * len[1] - axis[2]->x * len[2],
376             cen.y - axis[0]->y * len[0] + axis[1]->y * len[1] - axis[2]->y * len[2],
377             cen.z - axis[0]->z * len[0] + axis[1]->z * len[1] - axis[2]->z * len[2]);
378  glVertex3f(cen.x - axis[0]->x * len[0] + axis[1]->x * len[1] + axis[2]->x * len[2],
379             cen.y - axis[0]->y * len[0] + axis[1]->y * len[1] + axis[2]->y * len[2],
380             cen.z - axis[0]->z * len[0] + axis[1]->z * len[1] + axis[2]->z * len[2]);
381  glVertex3f(cen.x + axis[0]->x * len[0] + axis[1]->x * len[1] + axis[2]->x * len[2],
382             cen.y + axis[0]->y * len[0] + axis[1]->y * len[1] + axis[2]->y * len[2],
383             cen.z + axis[0]->z * len[0] + axis[1]->z * len[1] + axis[2]->z * len[2]);
384  glVertex3f(cen.x + axis[0]->x * len[0] + axis[1]->x * len[1] - axis[2]->x * len[2],
385             cen.y + axis[0]->y * len[0] + axis[1]->y * len[1] - axis[2]->y * len[2],
386             cen.z + axis[0]->z * len[0] + axis[1]->z * len[1] - axis[2]->z * len[2]);
387  glEnd();
388
389/*
390  glVertex3f(cen.x - axis[0]->x * len[0] + axis[1]->x * len[1] - axis[2]->x * len[2],
391             cen.y - axis[0]->y * len[0] + axis[1]->y * len[1] - axis[2]->y * len[2],
392             cen.z - axis[0]->z * len[0] + axis[1]->z * len[1] - axis[2]->z * len[2]);
393  glVertex3f(cen.x - axis[0]->x * len[0] + axis[1]->x * len[1] + axis[2]->x * len[2],
394             cen.y - axis[0]->y * len[0] + axis[1]->y * len[1] + axis[2]->y * len[2],
395             cen.z - axis[0]->z * len[0] + axis[1]->z * len[1] + axis[2]->z * len[2]);*/
396
397
398  glEnd();
399
400
401}
402
403
404void OBBTreeNode::drawBVBlended(int currentDepth, const int depth) const
405{}
406
407
408void OBBTreeNode::debug()
409{
410
411  /*
412  for(int i = 0; i < length; i++)
413    {
414      printf("vertex %i: %f, %f, %f\n", i, verticesList[i][0], verticesList[i][1], verticesList[i][2]);
415    }
416  */
417}
Note: See TracBrowser for help on using the repository browser.