Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: 3 axis get checked:) 12 to go…

File size: 30.6 KB
RevLine 
[4588]1/*
[4541]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
[4617]11### File Specific:
[4541]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"
[4542]19#include "list.h"
20#include "obb.h"
[4616]21#include "obb_tree.h"
[4544]22#include "vector.h"
[4550]23#include "abstract_model.h"
[4700]24#include "p_node.h"
[4541]25
[4543]26#include <math.h>
27
[4638]28#include "stdincl.h"
[4572]29
[4627]30#include "lin_alg.h"
[4572]31
32
33
[4627]34
[4541]35using namespace std;
36
[4622]37OBBTree*  OBBTreeNode::obbTree = NULL;
[4541]38
[4630]39float**  OBBTreeNode::coMat = NULL;
40float**  OBBTreeNode::eigvMat = NULL;
41float*   OBBTreeNode::eigvlMat = NULL;
42int*     OBBTreeNode::rotCount = NULL;
43
[4541]44/**
45   \brief standard constructor
[4617]46 */
[4588]47OBBTreeNode::OBBTreeNode ()
[4541]48{
[4617]49  this->setClassID(CL_OBB_TREE_NODE, "OBBTreeNode");
[4618]50  this->nodeLeft = NULL;
51  this->nodeRight = NULL;
[4630]52
53  if(coMat == NULL)
54  {
55    coMat = new float*[4];
56    for(int i = 0; i < 4; i++)
57      coMat[i] = new float[4];
58  }
59  if(eigvMat == NULL)
60  {
61    eigvMat = new float*[4];
62    for(int i = 0; i < 4; i++)
63      eigvMat[i] = new float[4];
64  }
65  if( eigvlMat == NULL)
66  {
67    eigvlMat = new float[4];
68  }
69  if( rotCount == NULL)
70    rotCount = new int;
[4638]71
72  this->sphereObj = gluNewQuadric();
[4541]73}
74
75
76/**
77   \brief standard deconstructor
[4617]78 */
[4588]79OBBTreeNode::~OBBTreeNode ()
[4541]80{
81  // delete what has to be deleted here
82}
83
84
[4542]85
86/**
87   \brief creates a new BVTree or BVTree partition
[4614]88   \param depth: how much more depth-steps to go: if == 1 don't go any deeper!
[4542]89   \param verticesList: the list of vertices of the object - each vertices triple is interpreted as a triangle
[4617]90 */
[4544]91void OBBTreeNode::spawnBVTree(const int depth, sVec3D *verticesList, const int length)
[4542]92{
[4638]93  PRINT(0)("\n");
94  this->treeIndex = this->obbTree->getID();
95  PRINTF(0)("OBB Depth: %i, tree index: %i, numVertices: %i\n", depth, treeIndex, length);
[4614]96  this->depth = depth;
97
[4638]98
[4630]99  this->bvElement = new OBB();
[4638]100  this->bvElement->vertices = verticesList;
101  this->bvElement->numOfVertices = length;
102  PRINTF(3)("Created OBBox\n");
[4632]103  this->calculateBoxCovariance(this->bvElement, verticesList, length);
[4638]104  PRINTF(3)("Calculated attributes1\n");
[4632]105  this->calculateBoxEigenvectors(this->bvElement, verticesList, length);
[4638]106  PRINTF(3)("Calculated attributes2\n");
[4632]107  this->calculateBoxAxis(this->bvElement, verticesList, length);
[4638]108  PRINTF(3)("Calculated attributes3\n");
[4617]109
[4632]110
[4614]111  if( likely( this->depth > 0))
112  {
113    this->forkBox(this->bvElement);
[4626]114
[4630]115
[4648]116    if(this->tmpLen1 > 0)
[4638]117    {
118      OBBTreeNode* node1 = new OBBTreeNode();
119      this->nodeLeft = node1;
120      this->nodeLeft->spawnBVTree(depth - 1, this->tmpVert1, this->tmpLen1);
121    }
122    else
123    {
124      PRINTF(0)("Aboarding tree walk: less than 3 vertices left\n");
125    }
[4630]126
[4648]127    if( this->tmpLen2 > 0)
[4638]128    {
129      OBBTreeNode* node2 = new OBBTreeNode();
130      this->nodeRight = node2;
131      this->nodeRight->spawnBVTree(depth - 1, this->tmpVert2, this->tmpLen2);
132    }
133    else
134    {
135      PRINTF(0)("Aboarding tree walk: less than 3 vertices left\n");
136    }
[4630]137
[4614]138  }
[4557]139}
140
141
142
[4632]143void OBBTreeNode::calculateBoxCovariance(OBB* box, sVec3D* verticesList, int length)
[4557]144{
[4543]145  float     facelet[length];                         //!< surface area of the i'th triangle of the convex hull
146  float     face;                                    //!< surface area of the entire convex hull
[4588]147  Vector    centroid[length];                        //!< centroid of the i'th convex hull
[4557]148  Vector    center;                                  //!< the center of the entire hull
[4544]149  Vector    p, q, r;                                 //!< holder of the polygon data, much more conveniant to work with Vector than sVec3d
[4545]150  Vector    t1, t2;                                  //!< temporary values
[4628]151  float     covariance[3][3];                        //!< the covariance matrix
[4674]152  int       mode = 0;                                //!< mode = 0: vertex soup, no connections, mode = 1: 3 following verteces build a triangle
[4588]153
[4553]154  this->numOfVertices = length;
155  this->vertices = verticesList;
156
[4562]157
[4648]158  if( likely(mode == 0))
159  {
160    /* fist compute all the convex hull face/facelets and centroids */
161    for(int i = 0; i < length; i+=3)          /* FIX-ME-QUICK: hops of 3, array indiscontinuity*/
162    {
163      p = verticesList[i];
164      q = verticesList[i + 1];
165      r = verticesList[i + 2];
[4638]166
[4648]167      t1 = p - q; t2 = p - r;
[4638]168
[4648]169      /* finding the facelet surface via cross-product */
170      facelet[i] = 0.5f * fabs( t1.cross(t2).len() );
171      /* update the entire convex hull surface */
172      face += facelet[i];
173
174      /* calculate the cetroid of the hull triangles */
175      centroid[i] = (p + q + r) * 1/3;
176      /* now calculate the centroid of the entire convex hull, weighted average of triangle centroids */
177      center += centroid[i] * facelet[i];
178    }
179    /* take the average of the centroid sum */
180    center /= face;
181    PRINTF(3)("-- Calculated Center\n");
182
183
184    /* now calculate the covariance matrix - if not written in three for-loops, it would compute faster: minor */
185    for(int j = 0; j < 3; ++j)
186    {
187      for(int k = 0; k < 3; ++k)
188      {
189        for(int i = 0; i < length; i+=3)
190        {
191          p = verticesList[i];
192          q = verticesList[i + 1];
193          r = verticesList[i + 2];
194
195          covariance[j][k] = facelet[i] / (12.0f * face) * (9.0f * centroid[i][j] * centroid[i][k] + p[j] * p[k] +
196              q[j] * q[k] + r[j] * r[k]) - center[j] * center[k];
197        }
198      }
199    }
200    PRINTF(3)("-- Calculated Covariance\n");
201  }
202  else if( mode == 1)
[4617]203  {
[4648]204    for( int i = 0; i < length; i+=3)          /* FIX-ME-QUICK: hops of 3, array indiscontinuity*/
205    {
206      p = verticesList[i];
207      q = verticesList[i + 1];
208      r = verticesList[i + 2];
[4588]209
[4648]210      centroid[i] = (p + q + r) / 3.0f;
211      center += centroid[i];
212    }
213    center /= length;
[4588]214
[4648]215    for( int j = 0; j < 3; ++j)
216    {
217      for( int k = 0; k < 3; ++k)
218      {
219        for( int i = 0; i < length; i+=3)
220        {
221          p = verticesList[i];
222          q = verticesList[i +1];
223          r = verticesList[i + 2];
[4545]224
[4648]225          covariance[j][k] = p[j] * p[k] + q[j] * q[k] + r[j] + r[k];
226        }
227        covariance[j][k] /= (3.0f * length);
228      }
229    }
230    PRINTF(3)("-- Calculated Covariance\n");
[4617]231  }
[4648]232  else if( mode == 2)
233  {
234    /* fist compute all the convex hull face/facelets and centroids */
235    for(int i = 0; i < length; i+=3)          /* FIX-ME-QUICK: hops of 3, array indiscontinuity*/
236    {
237      p = verticesList[i];
238      q = verticesList[i + 1];
239      r = verticesList[i + 2];
[4562]240
[4648]241      t1 = p - q; t2 = p - r;
[4562]242
[4648]243      /* finding the facelet surface via cross-product */
244      facelet[i] = 0.5f * fabs( t1.cross(t2).len() );
245      /* update the entire convex hull surface */
246      face += facelet[i];
247
248      /* calculate the cetroid of the hull triangles */
249      centroid[i] = (p + q + r) * 1/3;
250      /* now calculate the centroid of the entire convex hull, weighted average of triangle centroids */
251      center += centroid[i] * facelet[i];
252    }
253    /* take the average of the centroid sum */
254    center /= face;
255    PRINTF(3)("-- Calculated Center\n");
256
257    for( int j = 0; j < 3; ++j)
258    {
259      for( int k = 0; k < 3; ++k)
260      {
261        for( int i = 0; i < length; i+=3)
262        {
263          p = verticesList[i];
264          q = verticesList[i +1];
265          r = verticesList[i + 2];
266
267          covariance[j][k] = p[j] * p[k] + q[j] * q[k] + r[j] + r[k];
268        }
269        covariance[j][k] /= (3.0f * length);
270      }
271    }
272    PRINTF(3)("-- Calculated Covariance\n");
273  }
274  else
[4617]275  {
[4648]276    for( int i = 0; i < length; ++i)          /* FIX-ME-QUICK: hops of 3, array indiscontinuity*/
[4545]277    {
[4648]278      center += verticesList[i];
279    }
280    center /= length;
281
282    for( int j = 0; j < 3; ++j)
283    {
284      for( int k = 0; k < 3; ++k)
[4617]285      {
[4648]286        for( int i = 0; i < length; i+=3)
287        {
288          p = verticesList[i];
289          q = verticesList[i +1];
290          r = verticesList[i + 2];
[4544]291
[4648]292          covariance[j][k] = p[j] * p[k] + q[j] * q[k] + r[j] + r[k];
293        }
294        covariance[j][k] /= (3.0f * length);
[4617]295      }
[4545]296    }
[4648]297    PRINTF(3)("-- Calculated Covariance\n");
[4617]298  }
[4562]299
[4648]300  PRINTF(3)("\nVertex Data:\n");
[4638]301  for(int i = 0; i < length; i++)
302  {
[4648]303    PRINTF(3)("vertex %i: %f, %f, %f\n", i, box->vertices[i][0], box->vertices[i][1], box->vertices[i][2]);
[4638]304  }
[4588]305
[4648]306
[4675]307  PRINTF(3)("\nCovariance Matrix:\n");
[4674]308  for(int j = 0; j < 3; ++j)
309  {
[4675]310    PRINT(3)(" |");
[4674]311    for(int k = 0; k < 3; ++k)
312    {
[4675]313      PRINT(3)(" \b%f ", covariance[j][k]);
[4674]314    }
[4675]315    PRINT(3)(" |\n");
[4674]316  }
317
[4638]318  PRINTF(3)("center: %f, %f, %f\n", center.x, center.y, center.z);
[4553]319
[4588]320
[4674]321  for(int i = 0; i < 3; ++i)
322  {
323    box->covarianceMatrix[i][0] = covariance[i][0];
324    box->covarianceMatrix[i][1] = covariance[i][1];
325    box->covarianceMatrix[i][2] = covariance[i][2];
326  }
[4560]327  *box->center = center;
[4638]328  PRINTF(3)("-- Written Result to obb\n");
[4631]329}
[4557]330
[4631]331
332
[4632]333void OBBTreeNode::calculateBoxEigenvectors(OBB* box, sVec3D* verticesList, int length)
[4631]334{
335
[4557]336  /* now getting spanning vectors of the sub-space:
[4617]337  the eigenvectors of a symmertric matrix, such as the
338  covarience matrix are mutually orthogonal.
339  after normalizing them, they can be used as a the basis
340  vectors
[4557]341  */
[4660]342  Vector*              axis = new Vector[3];                //!< the references to the obb axis
[4588]343
[4631]344  coMat[1][1] = box->covarianceMatrix[0][0]; coMat[1][2] = box->covarianceMatrix[0][1]; coMat[1][3] = box->covarianceMatrix[0][2];
345  coMat[2][1] = box->covarianceMatrix[1][0]; coMat[2][2] = box->covarianceMatrix[1][1]; coMat[2][3] = box->covarianceMatrix[1][2];
346  coMat[3][1] = box->covarianceMatrix[2][0]; coMat[3][2] = box->covarianceMatrix[2][1]; coMat[3][3] = box->covarianceMatrix[2][2];
[4627]347
[4630]348  /* new jacobi tests */
349  JacobI(coMat, 3, eigvlMat, eigvMat, rotCount);
[4638]350  PRINTF(3)("-- Done Jacobi Decomposition\n");
[4628]351
[4627]352
[4638]353//   PRINTF(3)("Jacobi\n");
[4629]354//   for(int j = 1; j < 4; ++j)
355//   {
[4638]356//     PRINTF(3)(" |");
[4629]357//     for(int k = 1; k < 4; ++k)
358//     {
[4638]359//       PRINTF(3)(" \b%f ", eigvMat[j][k]);
[4629]360//     }
[4638]361//     PRINTF(3)(" |\n");
[4629]362//   }
363
[4660]364  axis[0].x = eigvMat[1][1]; axis[0].y = eigvMat[2][1]; axis[0].z = eigvMat[3][1];
365  axis[1].x = eigvMat[1][2]; axis[1].y = eigvMat[2][2]; axis[1].z = eigvMat[3][2];
366  axis[2].x = eigvMat[1][3]; axis[2].y = eigvMat[2][3]; axis[2].z = eigvMat[3][3];
[4576]367  box->axis = axis;
[4660]368
[4638]369  PRINTF(3)("-- Got Axis\n");
[4588]370
[4675]371  PRINTF(3)("eigenvector: %f, %f, %f\n", box->axis[0].x, box->axis[0].y, box->axis[0].z);
372  PRINTF(3)("eigenvector: %f, %f, %f\n", box->axis[1].x, box->axis[1].y, box->axis[1].z);
373  PRINTF(3)("eigenvector: %f, %f, %f\n", box->axis[2].x, box->axis[2].y, box->axis[2].z);
[4632]374}
[4588]375
[4626]376
[4632]377void OBBTreeNode::calculateBoxAxis(OBB* box, sVec3D* verticesList, int length)
[4631]378{
[4630]379
[4576]380  /* now get the axis length */
[4578]381  Line                ax[3];                                 //!< the axis
382  float*              halfLength = new float[3];             //!< half length of the axis
383  float               tmpLength;                             //!< tmp save point for the length
[4660]384  Plane               p0(box->axis[0], *box->center);       //!< the axis planes
385  Plane               p1(box->axis[1], *box->center);
386  Plane               p2(box->axis[2], *box->center);
[4658]387  float               maxLength[3];
388  float               minLength[3];
[4588]389
[4658]390
391  /* get a bad bounding box */
[4589]392  halfLength[0] = -1.0f;
[4585]393  for(int j = 0; j < length; ++j)
[4658]394    {
[4661]395      tmpLength = fabs(p0.distancePoint(vertices[j]));
[4658]396      if( tmpLength > halfLength[0])
[4659]397        halfLength[0] = tmpLength;
[4658]398    }
399
400  halfLength[1] = -1.0f;
401  for(int j = 0; j < length; ++j)
402    {
403      tmpLength = fabs(p1.distancePoint(vertices[j]));
404      if( tmpLength > halfLength[1])
[4659]405        halfLength[1] = tmpLength;
[4658]406    }
407
408  halfLength[2] = -1.0f;
409  for(int j = 0; j < length; ++j)
410    {
[4661]411      tmpLength = fabs(p2.distancePoint(vertices[j]));
[4658]412      if( tmpLength > halfLength[2])
[4659]413        halfLength[2] = tmpLength;
[4658]414    }
415
416
417
418  /* get the maximal dimensions of the body in all directions */
[4660]419   maxLength[0] = 0.0f;
420   minLength[0] = 0.0f;
421   for(int j = 0; j < length; ++j)
422   {
423     tmpLength = p0.distancePoint(vertices[j]);
424     if( tmpLength > maxLength[0])
425       maxLength[0] = tmpLength;
426     else if( tmpLength < minLength[0])
427       minLength[0] = tmpLength;
428   }
[4578]429
[4660]430   maxLength[1] = 0.0f;
431   minLength[1] = 0.0f;
432   for(int j = 0; j < length; ++j)
433   {
[4661]434     tmpLength = p1.distancePoint(vertices[j]);
[4660]435     if( tmpLength > maxLength[1])
436       maxLength[1] = tmpLength;
437     else if( tmpLength < minLength[1])
438       minLength[1] = tmpLength;
439   }
[4585]440
[4660]441   maxLength[2] = 0.0f;
442   minLength[2] = 0.0f;
443   for(int j = 0; j < length; ++j)
444   {
[4661]445     tmpLength = p2.distancePoint(vertices[j]);
[4660]446     if( tmpLength > maxLength[2])
447       maxLength[2] = tmpLength;
448     else if( tmpLength < minLength[2])
449       minLength[2] = tmpLength;
450   }
[4585]451
[4660]452
453   /* calculate the real centre of the body by using the axis length */
[4668]454   float centerOffset[3];
455   float newHalfLength[3];
[4660]456   for(int i = 0; i < 3; ++i)
457     {
[4674]458       PRINTF(3)("max: %f, min: %f \n", maxLength[i], minLength[i]);
[4668]459       centerOffset[i] = (maxLength[i] + minLength[i]) / 2.0f; // min length is negatie
460       newHalfLength[i] = (maxLength[i] - minLength[i]) / 2.0f; // min length is negative
461       *box->center +=  (box->axis[i] * centerOffset[i]);            // update the new center vector
462       halfLength[i] = newHalfLength[i];
[4660]463     }
464
465
466
[4586]467  box->halfLength = halfLength;
[4638]468  PRINTF(3)("-- Written Axis to obb\n");
469  PRINTF(3)("-- Finished Calculating Attributes\n");
[4585]470
[4638]471
472
473//   PRINTF(3)("\nwe got length: \n");
474  for(int i = 0; i < 3; ++i)
475  {
476    //if( box->halfLength[i] == 0.0)
[4660]477    PRINTF(3)("length[%i] = %f\n", i, box->halfLength[i]);
[4638]478  }
[4542]479}
480
481
[4609]482
483/**
484  \brief this separates an ob-box in the middle
485  \param box: the box to separate
486
487  this will separate the box into to smaller boxes. the separation is done along the middle of the longest axis
488 */
[4557]489void OBBTreeNode::forkBox(OBB* box)
490{
491  /* get the longest axis of the box */
[4609]492  float               aLength = -1.0f;                     //!< the length of the longest axis
493  int                 axisIndex = 0;                       //!< this is the nr of the longest axis
494
[4557]495  for(int i = 0; i < 3; ++i)
[4609]496  {
497    if( aLength < box->halfLength[i])
[4557]498    {
[4609]499      aLength = box->halfLength[i];
500      axisIndex = i;
[4557]501    }
[4609]502  }
[4588]503
[4688]504   PRINTF(3)("longest axis is: nr %i with a half-length of: %f\n", axisIndex, aLength);
[4609]505
506
[4557]507  /* get the closest vertex near the center */
[4611]508  float               dist = 999999.0f;                    //!< the smallest distance to each vertex
[4609]509  float               tmpDist;                             //!< temporary distance
510  int                 vertexIndex;
[4660]511  Plane               middlePlane(box->axis[axisIndex], *box->center); //!< the middle plane
[4588]512
[4660]513  vertexIndex = 0;
[4609]514  for(int i = 0; i < box->numOfVertices; ++i)
515  {
[4611]516    tmpDist = fabs(middlePlane.distancePoint(box->vertices[i]));
517    if( tmpDist < dist)
518    {
[4609]519      dist = tmpDist;
[4611]520      vertexIndex = i;
521    }
[4609]522  }
523
[4638]524//   PRINTF(3)("\nthe clostest vertex is nr: %i, with a dist of: %f\n", vertexIndex ,dist);
[4609]525
526
[4611]527  /* now definin the separation plane through this specified nearest point and partition
[4617]528  the points depending on which side they are located
[4611]529  */
530  tList<sVec3D>      partition1;                           //!< the vertex partition 1
531  tList<sVec3D>      partition2;                           //!< the vertex partition 2
532
[4695]533  PRINTF(3)("vertex index: %i, of %i\n", vertexIndex, box->numOfVertices);
[4660]534  this->separationPlane = new Plane(box->axis[axisIndex], box->vertices[vertexIndex]);  //!< separation plane
[4632]535  this->sepPlaneCenter = &box->vertices[vertexIndex];
536  this->longestAxisIndex = axisIndex;
537
[4612]538  for(int i = 0; i < box->numOfVertices; ++i)
539  {
[4632]540    if( this->separationPlane->distancePoint(box->vertices[i]) > 0.0f)
[4612]541      partition1.add(&box->vertices[i]);
542    else
543      partition2.add(&box->vertices[i]);
544  }
[4613]545  partition1.add(&box->vertices[vertexIndex]);
[4611]546
[4638]547//   PRINTF(3)("\npartition1: got %i vertices/ partition 2: got %i vertices\n", partition1.getSize(), partition2.getSize());
[4612]548
[4613]549
550  /* now comes the separation into two different sVec3D arrays */
551  tIterator<sVec3D>* iterator;                             //!< the iterator to go through the lists
552  sVec3D*            element;                              //!< the elements
553  int                index;                                //!< index storage place
554  sVec3D*            vertList1;                            //!< the vertex list 1
555  sVec3D*            vertList2;                            //!< the vertex list 2
556
557  vertList1 = new sVec3D[partition1.getSize()];
558  vertList2 = new sVec3D[partition2.getSize()];
559
560  iterator = partition1.getIterator();
561  element = iterator->nextElement();
562  index = 0;
563  while( element != NULL)
564  {
565    vertList1[index][0] = element[0][0];
566    vertList1[index][1] = element[0][1];
567    vertList1[index][2] = element[0][2];
568    ++index;
569    element = iterator->nextElement();
570  }
571
[4638]572//   PRINTF(0)("\npartition 1:\n");
[4626]573//   for(int i = 0; i < partition1.getSize(); ++i)
574//   {
[4638]575//     PRINTF(0)("v[%i][0] = %f,\tv[%i][1] = %f,\tv[%i][1] = %f\n", i, vertList1[i][0], i, vertList1[i][1], i, vertList1[i][2]);
[4626]576//   }
[4613]577
578  iterator = partition2.getIterator();
579  element = iterator->nextElement();
580  index = 0;
581  while( element != NULL)
582  {
583    vertList2[index][0] = element[0][0];
584    vertList2[index][1] = element[0][1];
585    vertList2[index][2] = element[0][2];
586    ++index;
587    element = iterator->nextElement();
588  }
589
[4630]590  this->tmpVert1 = vertList1;
591  this->tmpVert2 = vertList2;
592  this->tmpLen1 = partition1.getSize();
593  this->tmpLen2 = partition2.getSize();
594
[4638]595  delete iterator;
596
597//   PRINTF(0)("\npartition 2:\n");
[4626]598//   for(int i = 0; i < partition2.getSize(); ++i)
599//   {
[4638]600//     PRINTF(0)("v[%i][0] = %f,\tv[%i][1] = %f,\tv[%i][1] = %f\n", i, vertList2[i][0], i,  vertList2[i][1], i, vertList2[i][2]);
[4626]601//   }
[4557]602}
603
604
[4626]605
606
[4700]607void OBBTreeNode::collideWith(BVTreeNode* treeNode, PNode* nodeA, PNode* nodeB)
[4695]608{
[4700]609  PRINTF(0)("collideWith\n");
[4695]610  /* if the obb overlap, make subtests: check which node is realy overlaping  */
[4700]611  if( this->overlapTest(this->bvElement, ((OBBTreeNode*)treeNode)->bvElement, nodeA, nodeB))
[4695]612  {
613    /* check if left node overlaps */
614    if( unlikely( this->nodeLeft != NULL))
[4700]615      if( this->overlapTest(this->nodeLeft->bvElement, ((OBBTreeNode*)treeNode)->bvElement, nodeA, nodeB))
616        this->nodeLeft->collideWith(((OBBTreeNode*)treeNode)->nodeLeft, nodeA, nodeB);
[4695]617    /* check if right node overlaps */
618    if( unlikely( this->nodeRight != NULL))
[4700]619      if(this->overlapTest(this->nodeRight->bvElement, ((OBBTreeNode*)treeNode)->bvElement, nodeA, nodeB))
620        this->nodeLeft->collideWith(((OBBTreeNode*)treeNode)->nodeRight, nodeA, nodeB);
[4695]621  }
622}
[4542]623
624
[4626]625
[4700]626bool OBBTreeNode::overlapTest(OBB* boxA, OBB* boxB, PNode* nodeA, PNode* nodeB)
[4695]627{
[4700]628 
629 
[4696]630  /* first check all axis */
[4700]631  Vector t = nodeA->getAbsCoor() + *boxA->center - ( nodeB->getAbsCoor() + *boxB->center);
632  float rA = 0.0f;
633  float rB = 0.0f;
634  Vector l;
[4626]635
[4700]636  for(int j = 0; j < 3; ++j)
637    {
638      rA = 0.0f;
639      rB = 0.0f;
640      l = boxA->axis[j];
641
642      for(int i = 0; i < 3; ++i)
643        {
644          rA += fabs(boxA->halfLength[i] * boxA->axis[i].dot(l));
645        }
646 
647      for(int i = 0; i < 3; ++i)
648        {
649          rB += fabs(boxB->halfLength[i] * boxB->axis[i].dot(l));
650        }
651 
652      PRINTF(0)("s = %f, rA+rB = %f\n", fabs(t.dot(l)), rA+rB);
653
654      if( (rA + rB) < fabs(t.dot(l)))
655        PRINTF(0)(" - The Boxes are disjoint!\n");
656      else
657        PRINTF(0)(" - The Boxes are not disjoint\n");
658 
659      PRINTF(0)("rA = %f, rB = %f\n", rA, rB);
660
661
662      /* now check all orthogonals from the axis */
663
664    }
665  return false;
[4695]666}
667
668
[4696]669
[4635]670void OBBTreeNode::drawBV(int depth, int drawMode) const
[4553]671{
[4638]672  this->obbTree->getMaterial(treeIndex)->select();
[4635]673
674  /* draw the model itself, there is some problem concerning this: the vertices are drawn multiple times */
675  if( drawMode & DRAW_MODEL || drawMode & DRAW_ALL)
676  {
[4638]677    if( !(drawMode & DRAW_SINGLE && depth != 0))
[4622]678    {
[4638]679      for(int i = 0; i < this->bvElement->numOfVertices; ++i)
680      {
681        glPushMatrix();
[4648]682        //glMatrixMode(GL_MODELVIEW);
683        //glVertex3f(this->bvElement->vertices[i][0], this->bvElement->vertices[i][1], this->bvElement->vertices[i][2]);
[4638]684        glTranslatef(this->bvElement->vertices[i][0], this->bvElement->vertices[i][1], this->bvElement->vertices[i][2]);
[4676]685        gluSphere(this->sphereObj, 0.1, 10, 10);
[4648]686        //PRINTF(0)("v(%f, %f, %f)\n", this->bvElement->vertices[i][0], this->bvElement->vertices[i][1], this->bvElement->vertices[i][2]);
[4638]687        glPopMatrix();
688      }
[4622]689    }
[4635]690  }
[4542]691
692
[4589]693  /* draw world axes */
[4676]694  if( drawMode & DRAW_BV_AXIS)
695  {
696    glBegin(GL_LINES);
697    glColor3f(0.0, 0.4, 0.3);
698    glVertex3f(0.0, 0.0, 0.0);
699    glVertex3f(3.0, 0.0, 0.0);
[4589]700
[4676]701    glVertex3f(0.0, 0.0, 0.0);
702    glVertex3f(0.0, 3.0, 0.0);
[4589]703
[4676]704    glVertex3f(0.0, 0.0, 0.0);
705    glVertex3f(0.0, 0.0, 3.0);
706    glEnd();
707  }
[4674]708
[4688]709
[4635]710  if( drawMode & DRAW_BV_AXIS || drawMode & DRAW_ALL)
711  {
[4636]712    if( !(drawMode & DRAW_SINGLE && depth != 0))
[4635]713    {
714      /* draw the obb axes */
715      glBegin(GL_LINES);
716      glColor3f(0.0, 0.4, 0.3);
717      glVertex3f(this->bvElement->center->x, this->bvElement->center->y, this->bvElement->center->z);
[4660]718      glVertex3f(this->bvElement->center->x + this->bvElement->axis[0].x * this->bvElement->halfLength[0],
719                 this->bvElement->center->y + this->bvElement->axis[0].y * this->bvElement->halfLength[0],
720                 this->bvElement->center->z + this->bvElement->axis[0].z * this->bvElement->halfLength[0]);
[4589]721
[4635]722      glVertex3f(this->bvElement->center->x, this->bvElement->center->y, this->bvElement->center->z);
[4660]723      glVertex3f(this->bvElement->center->x + this->bvElement->axis[1].x * this->bvElement->halfLength[1],
724                 this->bvElement->center->y + this->bvElement->axis[1].y * this->bvElement->halfLength[1],
725                 this->bvElement->center->z + this->bvElement->axis[1].z * this->bvElement->halfLength[1]);
[4588]726
[4635]727      glVertex3f(this->bvElement->center->x, this->bvElement->center->y, this->bvElement->center->z);
[4660]728      glVertex3f(this->bvElement->center->x + this->bvElement->axis[2].x * this->bvElement->halfLength[2],
729                 this->bvElement->center->y + this->bvElement->axis[2].y * this->bvElement->halfLength[2],
730                 this->bvElement->center->z + this->bvElement->axis[2].z * this->bvElement->halfLength[2]);
[4635]731      glEnd();
732    }
733  }
[4581]734
[4588]735
[4674]736  /* DRAW POLYGONS */
[4673]737  if( drawMode & DRAW_BV_POLYGON || drawMode & DRAW_ALL || drawMode & DRAW_BV_BLENDED)
[4635]738  {
[4636]739    if( !(drawMode & DRAW_SINGLE && depth != 0))
[4635]740    {
[4636]741    Vector cen = *this->bvElement->center;
[4660]742    Vector* axis = this->bvElement->axis;
[4636]743    float* len = this->bvElement->halfLength;
[4588]744
[4670]745    if( drawMode & DRAW_BV_BLENDED)
746      this->obbTree->getTransparentMaterial(treeIndex)->select();
747
[4636]748    /* draw bounding box */
[4670]749    if( drawMode & DRAW_BV_BLENDED)
750      glBegin(GL_QUADS);
751    else
752      glBegin(GL_LINE_LOOP);
[4660]753    glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2],
754               cen.y + axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2],
755               cen.z + axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]);
756    glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2],
757               cen.y + axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2],
758               cen.z + axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]);
759    glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
760               cen.y + axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
761               cen.z + axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
762    glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2],
763               cen.y + axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2],
764               cen.z + axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]);
[4636]765    glEnd();
[4588]766
[4670]767    if( drawMode & DRAW_BV_BLENDED)
768      glBegin(GL_QUADS);
769    else
770      glBegin(GL_LINE_LOOP);
[4660]771    glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2],
772               cen.y + axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2],
773               cen.z + axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]);
774    glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
775               cen.y + axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
776               cen.z + axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
777    glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
778               cen.y - axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
779               cen.z - axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
780    glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2],
781               cen.y - axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2],
782               cen.z - axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]);
[4636]783    glEnd();
[4588]784
[4670]785    if( drawMode & DRAW_BV_BLENDED)
786      glBegin(GL_QUADS);
787    else
788      glBegin(GL_LINE_LOOP);
[4660]789    glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2],
790               cen.y - axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2],
791               cen.z - axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]);
792    glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
793               cen.y - axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
794               cen.z - axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
795    glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2],
796               cen.y - axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2],
797               cen.z - axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]);
798    glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2],
799               cen.y - axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2],
800               cen.z - axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]);
[4636]801    glEnd();
[4588]802
[4670]803    if( drawMode & DRAW_BV_BLENDED)
804      glBegin(GL_QUADS);
805    else
806      glBegin(GL_LINE_LOOP);
[4660]807    glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2],
808               cen.y - axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2],
809               cen.z - axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]);
810    glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2],
811               cen.y - axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2],
812               cen.z - axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]);
813    glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2],
814               cen.y + axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2],
815               cen.z + axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]);
816    glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2],
817               cen.y + axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2],
818               cen.z + axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]);
[4636]819    glEnd();
[4670]820
[4671]821
[4670]822    if( drawMode & DRAW_BV_BLENDED)
[4671]823    {
824      glBegin(GL_QUADS);
825      glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2],
826                 cen.y - axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2],
827                 cen.z - axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]);
828      glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2],
829                 cen.y + axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2],
830                 cen.z + axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]);
831      glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
832                 cen.y + axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
833                 cen.z + axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
834      glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
835                 cen.y - axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
836                 cen.z - axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
837      glEnd();
838
839      glBegin(GL_QUADS);
840      glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2],
841                 cen.y - axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2],
842                 cen.z - axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]);
843      glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2],
844                 cen.y + axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2],
845                 cen.z + axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]);
846      glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2],
847                 cen.y + axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2],
848                 cen.z + axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]);
849      glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2],
850                 cen.y - axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2],
851                 cen.z - axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]);
852      glEnd();
853    }
854
855
856    if( drawMode & DRAW_BV_BLENDED)
[4670]857      this->obbTree->getMaterial(treeIndex)->select();
[4635]858    }
[4636]859
[4635]860  }
[4588]861
[4674]862  /* DRAW SEPARATING PLANE */
[4635]863  if( drawMode & DRAW_SEPARATING_PLANE || drawMode & DRAW_ALL)
[4632]864  {
[4636]865    if( !(drawMode & DRAW_SINGLE && depth != 0))
[4635]866    {
[4671]867      if( drawMode & DRAW_BV_BLENDED)
868        this->obbTree->getTransparentMaterial(treeIndex)->select();
869
[4636]870    /* now draw the separation plane */
[4660]871    Vector a1 = this->bvElement->axis[(this->longestAxisIndex + 1)%3];
872    Vector a2 = this->bvElement->axis[(this->longestAxisIndex + 2)%3];
[4636]873    Vector c = *this->bvElement->center;
874    float l1 = this->bvElement->halfLength[(this->longestAxisIndex + 1)%3];
875    float l2 = this->bvElement->halfLength[(this->longestAxisIndex + 2)%3];
876    glBegin(GL_QUADS);
877    glVertex3f(c.x + a1.x * l1 + a2.x * l2, c.y + a1.y * l1+ a2.y * l2, c.z + a1.z * l1 + a2.z * l2);
878    glVertex3f(c.x - a1.x * l1 + a2.x * l2, c.y - a1.y * l1+ a2.y * l2, c.z - a1.z * l1 + a2.z * l2);
879    glVertex3f(c.x - a1.x * l1 - a2.x * l2, c.y - a1.y * l1- a2.y * l2, c.z - a1.z * l1 - a2.z * l2);
880    glVertex3f(c.x + a1.x * l1 - a2.x * l2, c.y + a1.y * l1- a2.y * l2, c.z + a1.z * l1 - a2.z * l2);
881    glEnd();
[4671]882
883    if( drawMode & DRAW_BV_BLENDED)
884      this->obbTree->getMaterial(treeIndex)->select();
885
[4635]886    }
[4632]887  }
[4588]888
[4622]889  if( this->nodeLeft != NULL && depth != 0 )
[4635]890    this->nodeLeft->drawBV(depth - 1, drawMode);
[4618]891  if( this->nodeRight != NULL && depth != 0)
[4635]892    this->nodeRight->drawBV(depth - 1, drawMode);
[4588]893
[4557]894}
[4542]895
896
[4568]897
[4685]898void OBBTreeNode::debug(void) const
[4568]899{
900
901  /*
902  for(int i = 0; i < length; i++)
[4617]903  {
[4638]904  PRINTF(3)("vertex %i: %f, %f, %f\n", i, verticesList[i][0], verticesList[i][1], verticesList[i][2]);
[4617]905}
[4568]906  */
907}
Note: See TracBrowser for help on using the repository browser.