Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/collision_detection/src/lib/math/matrix.cc @ 5889

Last change on this file since 5889 was 5677, checked in by bensch, 19 years ago

orxonox/trunk: more optimisations

File size: 3.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: Benjamin Grauer
13   co-programmer: Patrick Boenzli
14*/
15#include "matrix.h"
16
17#include <stdio.h>
18#include <math.h>
19
20
21int Matrix::getEigenValues(Vector& eigenValues) const
22{
23  int retVal = -1;
24  float a = 0;
25  float b = 0;
26
27  float c[3];
28
29  // c[0] is the determinante of mat
30  c[0] = this->m11 * this->m22 * this->m33 +
31      2* this->m12 * this->m13 * this->m23 -
32      this->m11 * this->m23 * this->m23 -
33      this->m22 * this->m13 * this->m13 -
34      this->m33 * this->m12 * this->m12;
35
36  // c[1] is the trace of a
37  c[1] = this->m11 * this->m22 -
38      this->m12 * this->m12 +
39      this->m11 * this->m33 -
40      this->m13 * this->m13 +
41      this->m22 * this->m33 -
42      this->m23 * this->m23;
43
44  // c[2] is the sum of the diagonal elements
45  c[2] = this->m11 +
46      this->m22 +
47      this->m33;
48
49
50  // Computing the roots:
51  a = (3.0*c[1] - c[2]*c[2]) / 3.0;
52  b = (-2.0*c[2]*c[2]*c[2] + 9.0*c[1]*c[2] - 27.0*c[0]) / 27.0;
53
54  float Q = b*b/4.0 + a*a*a/27.0;
55
56  // 3 distinct Roots
57  if (Q < 0)
58  {
59    float psi = atan2(sqrt(-Q), -b/2.0);
60    float p = sqrt((b/2.0)*(b/2.0) - Q);
61
62    eigenValues.x = c[2]/3.0 + 2 * pow(p, 1/3.0) * cos(psi/3.0);
63    eigenValues.y = c[2]/3.0 - pow(p, 1/3.0) * (cos(psi/3.0)
64        + sqrt(3.0) * sin(psi/3.0));
65    eigenValues.z = c[2]/3.0 - pow(p, 1/3.0) * (cos(psi/3.0)
66        - sqrt(3.0) * sin(psi/3.0));
67    retVal = 3;
68  }
69  // 2 Distinct Roots
70  else if (Q == 0)
71  {
72    eigenValues.x = eigenValues.y = c[2]/3.0 + pow(b/2.0, 1.0/3.0);
73    eigenValues.z = c[2]/3.0 + 2* pow(b/2.0, 1.0/3.0);
74    retVal = 2;
75  }
76  // 1 Root (not calculating anything.)
77  else if (Q > 0)
78  {
79    eigenValues.x = eigenValues.y = eigenValues.z = 1;
80    retVal = 1;
81  }
82  return retVal;
83}
84
85void Matrix::getEigenVectors(Vector& eigVc1, Vector& eigVc2, Vector& eigVc3) const
86{
87  Vector eigenValues;
88  int eigenValuesCount = this->getEigenValues(eigenValues);
89
90  if (eigenValuesCount == 2 || eigenValuesCount == 3)
91  {
92    /* eigenvec creation */
93    eigVc1.x = -1/this->m13*(this->m33 - eigenValues.x) +
94        (this->m32*(-this->m31*this->m32 + this->m12*this->m33 - this->m12*eigenValues.x)) /
95        this->m13*(-this->m13*this->m22 - this->m12*this->m23 + this->m13*eigenValues.x);
96
97    eigVc1.y = -( -this->m13*this->m23 + this->m12*this->m33 - this->m12*eigenValues.x) /
98        (-this->m31*this->m22 + this->m12*this->m23 + this->m13*eigenValues.x);
99
100    eigVc1.z = 1.0f;
101
102    eigVc2.x = -1/this->m13*(this->m33 - eigenValues.y) +
103        (this->m32*(-this->m31*this->m32 + this->m12*this->m33 - this->m12*eigenValues.y)) /
104        this->m13*(-this->m13*this->m22 - this->m12*this->m23 + this->m13*eigenValues.y);
105
106    eigVc2.y = -( -this->m13*this->m23 + this->m12*this->m33 - this->m12*eigenValues.y) /
107        (-this->m31*this->m22 + this->m12*this->m23 + this->m13*eigenValues.y);
108
109    eigVc2.z = 1.0f;
110
111    eigVc3 = eigVc1.cross(eigVc2);
112  }
113  else if (eigenValuesCount == 1)
114  {
115    eigVc1 = Vector(1,0,0);
116    eigVc2 = Vector(0,1,0);
117    eigVc3 = Vector(0,0,1);
118  }
119  eigVc1.normalize();
120  eigVc2.normalize();
121  eigVc3.normalize();
122}
123
124void Matrix::debug() const
125{
126  printf("| %f | %f | %f |\n", this->m11, this->m12, this->m13 );
127  printf("| %f | %f | %f |\n", this->m21, this->m22, this->m23 );
128  printf("| %f | %f | %f |\n", this->m31, this->m32, this->m33 );
129
130}
131
Note: See TracBrowser for help on using the repository browser.