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
RevLine 
[5673]1/*
2   orxonox - the future of 3D-vertical-scrollers
[5661]3
[5673]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
[5661]17#include <stdio.h>
18#include <math.h>
19
[5662]20
[5668]21Vector Matrix::getEigenValues() const
[5662]22{
[5665]23  Vector eigVl;
[5662]24
[5661]25  float a = 0;
26  float b = 0;
27
28  float c[3];
29
30  // c[0] is the determinante of mat
[5662]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;
[5661]36
37  // c[1] is the trace of a
[5662]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;
[5661]44
45  // c[2] is the sum of the diagonal elements
[5662]46  c[2] = this->m11 +
47      this->m22 +
48      this->m33;
[5661]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
[5662]57  // 3 distinct Roots
[5661]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
[5665]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)
[5661]65        + sqrt(3.0) * sin(psi/3.0));
[5665]66    eigVl.z = c[2]/3.0 - pow(p, 1/3.0) * (cos(psi/3.0)
[5661]67        - sqrt(3.0) * sin(psi/3.0));
68
69  }
[5662]70  // 2 Distinct Roots
[5661]71  else if (Q == 0)
72  {
[5665]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);
[5661]76  }
[5662]77  // 1 Root (not calculating anything.)
[5661]78  else if (Q > 0)
79  {
[5668]80    printf("This Matrix is a multiple of the Identity matrix (lambda * I3))\n");
[5665]81    eigVl.x = eigVl.y = eigVl.z = 1;
[5661]82  }
[5665]83  return eigVl;
84}
[5661]85
[5668]86void Matrix::getEigenVectors(Vector& a, Vector& b, Vector& c) const
[5665]87{
[5668]88  Vector eigVl = this->getEigenValues();
[5665]89
[5668]90  float eigVal[3] = { eigVl.x, eigVl.y, eigVl.z };
91
92  Vector eigVc[3];
93  /* eigenvec test */
[5669]94  for(int i = 0; i < 2; i++)
[5664]95  {
[5668]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]);
[5662]98
[5668]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]);
[5664]101
[5668]102    eigVc[i].z = 1.0f;
[5664]103
[5669]104    eigVc[i] /= eigVc[i].len();
[5668]105  }
[5669]106  eigVc[2] = eigVc[0].cross(eigVc[1]);
[5664]107
[5669]108  a = eigVc[0];
109  b = eigVc[1];
110  c = eigVc[2];
[5662]111}
[5661]112
[5662]113void Matrix::debug() const
114{
[5668]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 );
[5661]118
119}
[5662]120
Note: See TracBrowser for help on using the repository browser.