Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: collision detection framework now works again

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  for(int i = 0; i < length; i++)
138    {
139      printf("vertex %i: %f, %f, %f\n", i, verticesList[i][0], verticesList[i][1], verticesList[i][2]);
140    }
141
142   
143  for(int i = 0; i < 3; ++i)
144    {
145   
146      box->covarianceMatrix[i][0] = covariance[i][0];
147      box->covarianceMatrix[i][1] = covariance[i][1];
148      box->covarianceMatrix[i][3] = covariance[i][2];
149    }
150  *box->center = center;
151
152
153  /* now getting spanning vectors of the sub-space:
154     the eigenvectors of a symmertric matrix, such as the
155     covarience matrix are mutually orthogonal.
156     after normalizing them, they can be used as a the basis
157     vectors
158  */
159 
160
161}
162
163
164void OBBTreeNode::forkBox(OBB* box)
165{
166  /* get the longest axis of the box */
167  float aLength = -1.0f;
168  int axisNr = 0;
169  for(int i = 0; i < 3; ++i)
170    {
171      if( aLength < box->axis[i].len())
172        {
173          aLength = box->axis[i].len();
174          axisNr = i;
175        }
176    }
177 
178  /* get the closest vertex near the center */
179 
180}
181
182
183void OBBTreeNode::collideWith(const BVTree &tree)
184{}
185
186
187void OBBTreeNode::drawBV(int currentDepth, const int depth) const
188{
189  glColor3f(1.0, 1.0, 1.0);
190  glBegin(GL_TRIANGLES);
191  for(int i = 0; i < this->numOfVertices; ++i)
192    {
193      glVertex3f(this->vertices[i][0], this->vertices[i][1], this->vertices[i][2]);
194      //printf("v(%f, %f, %f)\n", this->vertices[i][0], this->vertices[i][1], this->vertices[i][2]);
195    }
196  glEnd();
197}
198
199
200void OBBTreeNode::drawBVPolygon(int currentDepth, const int depth) const
201{
202  this->bvElement->axis;
203 
204  //glBegin(GL_TRIANGLE);
205  //glVertex3f(this->bvElement->center );
206  //glEnd();
207}
208
209
210void OBBTreeNode::drawBVBlended(int currentDepth, const int depth) const
211{}
Note: See TracBrowser for help on using the repository browser.