Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: moved the newmat source to lib

File size: 5.3 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
26using namespace std;
27
28
29/**
30   \brief standard constructor
31*/
32OBBTreeNode::OBBTreeNode () 
33{
34   this->setClassID(CL_OBB_TREE_NODE, "OBBTreeNode"); 
35
36}
37
38
39/**
40   \brief standard deconstructor
41
42*/
43OBBTreeNode::~OBBTreeNode () 
44{
45  // delete what has to be deleted here
46}
47
48
49
50/**
51   \brief creates a new BVTree or BVTree partition
52   \param depth: the depth of the tree
53   \param verticesList: the list of vertices of the object - each vertices triple is interpreted as a triangle
54*/
55void OBBTreeNode::spawnBVTree(const int depth, sVec3D *verticesList, const int length)
56{
57  this->bvElement = this->createBox();
58  this->calculateBoxAttributes(this->bvElement, verticesList, length);
59  this->forkBox(this->bvElement);
60}
61
62
63OBB* OBBTreeNode::createBox()
64{
65  return new OBB();
66}
67
68
69void OBBTreeNode::calculateBoxAttributes(OBB* box, sVec3D* verticesList, int length)
70{
71  float     facelet[length];                         //!< surface area of the i'th triangle of the convex hull
72  float     face;                                    //!< surface area of the entire convex hull
73  Vector    centroid[length];                        //!< centroid of the i'th convex hull 
74  Vector    center;                                  //!< the center of the entire hull
75  Vector    p, q, r;                                 //!< holder of the polygon data, much more conveniant to work with Vector than sVec3d
76  Vector    t1, t2;                                  //!< temporary values
77  float     covariance[3][3];                        //!< the covariance matrix
78   
79  this->numOfVertices = length;
80  this->vertices = verticesList;
81
82
83  /* fist compute all the convex hull face/facelets and centroids */
84  for(int i = 0; i < length; i+=3)          /* FIX-ME-QUICK: hops of 3, array indiscontinuity*/
85    {
86      p = verticesList[i];
87      q = verticesList[i +1];
88      r = verticesList[i + 2];
89     
90      t1 = p - q; t2 = p - r;
91     
92      /* finding the facelet surface via cross-product */
93      facelet[i] = 0.5f * fabs( t1.cross(t2).len() );
94      /* update the entire convex hull surface */
95      face += facelet[i];
96
97      /* calculate the cetroid of the hull triangles */
98      centroid[i] = (p + q + r) * 1/3;
99      /* now calculate the centroid of the entire convex hull, weighted average of triangle centroids */
100      center += centroid[i] * facelet[i];
101    }
102  /* take the average of the centroid sum */
103  center /= face;
104
105
106 
107  /* now calculate the covariance matrix - if not written in three for-loops, it would compute faster: minor */
108  for(int j = 0; j < 3; ++j)
109    {
110      for(int k = 0; k < 3; ++k)
111        {
112          for(int i = 0; i < length; i+=3)
113            {
114              p = verticesList[i];
115              q = verticesList[i +1];
116              r = verticesList[i + 2];
117
118              covariance[j][k] = facelet[i] / (12.0f * face) * (9.0f * centroid[i][j] * centroid[i][k] + p[j]* p[k] +
119                                                                q[j] * q[k] + r[j]*r[k]) - center[j] * center[k];
120            }
121        }
122    }
123
124 
125  printf("Covariance Matrix:\n");
126  for(int j = 0; j < 3; ++j)
127    {
128      printf(" |");
129      for(int k = 0; k < 3; ++k)
130        {
131          printf(" \b%f ", covariance[j][k]);
132        }
133      printf(" |\n");
134    }
135  printf("center: %f, %f, %f\n\n", center.x, center.y, center.z);
136
137   
138  for(int i = 0; i < 3; ++i)
139    {
140   
141      box->covarianceMatrix[i][0] = covariance[i][0];
142      box->covarianceMatrix[i][1] = covariance[i][1];
143      box->covarianceMatrix[i][3] = covariance[i][2];
144    }
145  *box->center = center;
146
147
148  /* now getting spanning vectors of the sub-space:
149     the eigenvectors of a symmertric matrix, such as the
150     covarience matrix are mutually orthogonal.
151     after normalizing them, they can be used as a the basis
152     vectors
153  */
154 
155
156}
157
158
159void OBBTreeNode::forkBox(OBB* box)
160{
161  /* get the longest axis of the box */
162  float aLength = -1.0f;
163  int axisNr = 0;
164  for(int i = 0; i < 3; ++i)
165    {
166      if( aLength < box->axis[i].len())
167        {
168          aLength = box->axis[i].len();
169          axisNr = i;
170        }
171    }
172 
173  /* get the closest vertex near the center */
174 
175}
176
177
178void OBBTreeNode::collideWith(const BVTree &tree)
179{}
180
181
182void OBBTreeNode::drawBV(int currentDepth, const int depth) const
183{
184  glColor3f(1.0, 1.0, 1.0);
185  glBegin(GL_TRIANGLES);
186  for(int i = 0; i < this->numOfVertices; ++i)
187    {
188      glVertex3f(this->vertices[i][0], this->vertices[i][1], this->vertices[i][2]);
189      //printf("v(%f, %f, %f)\n", this->vertices[i][0], this->vertices[i][1], this->vertices[i][2]);
190    }
191  glEnd();
192}
193
194
195void OBBTreeNode::drawBVPolygon(int currentDepth, const int depth) const
196{
197  this->bvElement->axis;
198 
199  //glBegin(GL_TRIANGLE);
200  //glVertex3f(this->bvElement->center );
201  //glEnd();
202}
203
204
205void OBBTreeNode::drawBVBlended(int currentDepth, const int depth) const
206{}
207
208
209void OBBTreeNode::debug()
210{
211
212  /*
213  for(int i = 0; i < length; i++)
214    {
215      printf("vertex %i: %f, %f, %f\n", i, verticesList[i][0], verticesList[i][1], verticesList[i][2]);
216    }
217  */
218}
Note: See TracBrowser for help on using the repository browser.