Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Matrix included into Project

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