Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/math/matrix.cc @ 5675

Last change on this file since 5675 was 5675, checked in by bensch, 18 years ago

orxonox/trunk: optimisations

File size: 3.4 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  /* eigenvec creation */
91  eigVc1.x = -1/this->m13*(this->m33 - eigenValues.x) +
92      (this->m32*(-this->m31*this->m32 + this->m12*this->m33 - this->m12*eigenValues.x)) /
93      this->m13*(-this->m13*this->m22 - this->m12*this->m23 + this->m13*eigenValues.x);
94
95  eigVc1.y = -( -this->m13*this->m23 + this->m12*this->m33 - this->m12*eigenValues.x) /
96      (-this->m31*this->m22 + this->m12*this->m23 + this->m13*eigenValues.x);
97
98  eigVc1.z = 1.0f;
99
100  eigVc2.x = -1/this->m13*(this->m33 - eigenValues.y) +
101      (this->m32*(-this->m31*this->m32 + this->m12*this->m33 - this->m12*eigenValues.y)) /
102      this->m13*(-this->m13*this->m22 - this->m12*this->m23 + this->m13*eigenValues.y);
103
104  eigVc2.y = -( -this->m13*this->m23 + this->m12*this->m33 - this->m12*eigenValues.y) /
105      (-this->m31*this->m22 + this->m12*this->m23 + this->m13*eigenValues.y);
106
107  eigVc2.z = 1.0f;
108
109  eigVc3 = eigVc1.cross(eigVc2);
110
111  eigVc1.normalize();
112  eigVc2.normalize();
113  eigVc3.normalize();
114}
115
116void Matrix::debug() const
117{
118  printf("| %f | %f | %f |\n", this->m11, this->m12, this->m13 );
119  printf("| %f | %f | %f |\n", this->m21, this->m22, this->m23 );
120  printf("| %f | %f | %f |\n", this->m31, this->m32, this->m33 );
121
122}
123
Note: See TracBrowser for help on using the repository browser.