Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: drawing in diffrent hirarchies works now, with diffrent colors

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