Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: eigenVc Cleanup

File size: 2.6 KB
RevLine 
[5661]1
2#include <stdio.h>
3#include <math.h>
[5662]4#include "matrix.h"
[5661]5
[5662]6
[5668]7Vector Matrix::getEigenValues() const
[5662]8{
[5665]9  Vector eigVl;
[5662]10
[5661]11  float a = 0;
12  float b = 0;
13
14  float c[3];
15
16  // c[0] is the determinante of mat
[5662]17  c[0] = this->m11 * this->m22 * this->m33 +
18      2* this->m12 * this->m13 * this->m23 -
19      this->m11 * this->m23 * this->m23 -
20      this->m22 * this->m13 * this->m13 -
21      this->m33 * this->m12 * this->m12;
[5661]22
23  // c[1] is the trace of a
[5662]24  c[1] = this->m11 * this->m22 -
25      this->m12 * this->m12 +
26      this->m11 * this->m33 -
27      this->m13 * this->m13 +
28      this->m22 * this->m33 -
29      this->m23 * this->m23;
[5661]30
31  // c[2] is the sum of the diagonal elements
[5662]32  c[2] = this->m11 +
33      this->m22 +
34      this->m33;
[5661]35
36
37  // Computing the roots:
38  a = (3.0*c[1] - c[2]*c[2]) / 3.0;
39  b = (-2.0*c[2]*c[2]*c[2] + 9.0*c[1]*c[2] - 27.0*c[0]) / 27.0;
40
41  float Q = b*b/4.0 + a*a*a/27.0;
42
[5662]43  // 3 distinct Roots
[5661]44  if (Q < 0)
45  {
46    float psi = atan2(sqrt(-Q), -b/2.0);
47    float p = sqrt((b/2.0)*(b/2.0) - Q);
48
[5665]49    eigVl.x = c[2]/3.0 + 2 * pow(p, 1/3.0) * cos(psi/3.0);
50    eigVl.y = c[2]/3.0 - pow(p, 1/3.0) * (cos(psi/3.0)
[5661]51        + sqrt(3.0) * sin(psi/3.0));
[5665]52    eigVl.z = c[2]/3.0 - pow(p, 1/3.0) * (cos(psi/3.0)
[5661]53        - sqrt(3.0) * sin(psi/3.0));
54
55  }
[5662]56  // 2 Distinct Roots
[5661]57  else if (Q == 0)
58  {
[5665]59    eigVl.x = c[2]/3.0 + pow(b/2.0, 1.0/3.0);
60    eigVl.y = c[2]/3.0 + pow(b/2.0, 1.0/3.0);
61    eigVl.z = c[2]/3.0 + 2* pow(b/2.0, 1.0/3.0);
[5661]62  }
[5662]63  // 1 Root (not calculating anything.)
[5661]64  else if (Q > 0)
65  {
[5668]66    printf("This Matrix is a multiple of the Identity matrix (lambda * I3))\n");
[5665]67    eigVl.x = eigVl.y = eigVl.z = 1;
[5661]68  }
[5665]69  return eigVl;
70}
[5661]71
[5668]72void Matrix::getEigenVectors(Vector& a, Vector& b, Vector& c) const
[5665]73{
[5668]74  Vector eigVl = this->getEigenValues();
[5665]75
[5668]76  float eigVal[3] = { eigVl.x, eigVl.y, eigVl.z };
77
78  Vector eigVc[3];
79  /* eigenvec test */
[5669]80  for(int i = 0; i < 2; i++)
[5664]81  {
[5668]82    eigVc[i].x = -1/this->m13*(this->m33 - eigVal[i]) + (this->m32*(-this->m31*this->m32 + this->m12*this->m33 - this->m12*eigVal[i])) /
83        this->m13*(-this->m13*this->m22 - this->m12*this->m23 + this->m13*eigVal[i]);
[5662]84
[5668]85    eigVc[i].y = -( -this->m13*this->m23 + this->m12*this->m33 - this->m12*eigVal[i]) /
86        (-this->m31*this->m22 + this->m12*this->m23 + this->m13*eigVal[i]);
[5664]87
[5668]88    eigVc[i].z = 1.0f;
[5664]89
[5669]90    eigVc[i] /= eigVc[i].len();
[5668]91  }
[5669]92  eigVc[2] = eigVc[0].cross(eigVc[1]);
[5664]93
[5669]94  a = eigVc[0];
95  b = eigVc[1];
96  c = eigVc[2];
[5662]97}
[5661]98
[5662]99void Matrix::debug() const
100{
[5668]101  printf("| %f | %f | %f |\n", this->m11, this->m12, this->m13 );
102  printf("| %f | %f | %f |\n", this->m21, this->m22, this->m23 );
103  printf("| %f | %f | %f |\n", this->m31, this->m32, this->m33 );
[5661]104
105}
[5662]106
Note: See TracBrowser for help on using the repository browser.