Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

cd: correctness

File size: 3.7 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
85
86
87
88void Matrix::getEigenVectors(Vector& eigVc1, Vector& eigVc2, Vector& eigVc3) const
89{
90  Vector eigenValues;
91  int eigenValuesCount = this->getEigenValues(eigenValues);
92
93  if (eigenValuesCount == 2 || eigenValuesCount == 3)
94  {
95    /* eigenvec creation */
96    eigVc1.x = -1/this->m13*(this->m33 - eigenValues.x) +
97        (this->m32*(-this->m31*this->m32 + this->m12*this->m33 - this->m12*eigenValues.x)) /
98        this->m13*(-this->m13*this->m22 - this->m12*this->m23 + this->m13*eigenValues.x);
99
100    eigVc1.y = -( -this->m13*this->m23 + this->m12*this->m33 - this->m12*eigenValues.x) /
101        (-this->m31*this->m22 + this->m12*this->m23 + this->m13*eigenValues.x);
102
103    eigVc1.z = 1.0f;
104
105    eigVc2.x = -1/this->m13*(this->m33 - eigenValues.y) +
106        (this->m32*(-this->m31*this->m32 + this->m12*this->m33 - this->m12*eigenValues.y)) /
107        this->m13*(-this->m13*this->m22 - this->m12*this->m23 + this->m13*eigenValues.y);
108
109    eigVc2.y = -( -this->m13*this->m23 + this->m12*this->m33 - this->m12*eigenValues.y) /
110        (-this->m31*this->m22 + this->m12*this->m23 + this->m13*eigenValues.y);
111
112    eigVc2.z = 1.0f;
113
114    eigVc3 = eigVc1.cross(eigVc2);
115
116    eigVc2 = eigVc3.cross(eigVc1);
117  }
118  else if (eigenValuesCount == 1)
119  {
120    eigVc1 = Vector(1,0,0);
121    eigVc2 = Vector(0,1,0);
122    eigVc3 = Vector(0,0,1);
123  }
124  eigVc1.normalize();
125  eigVc2.normalize();
126  eigVc3.normalize();
127
128  if (!(eigVc1.cross(eigVc3) == eigVc2))
129  {
130    eigVc3.cross(eigVc1).debug();
131    eigVc2.debug();
132  }
133  printf("ok\n");
134}
135
136void Matrix::debug() const
137{
138  printf("| %f | %f | %f |\n", this->m11, this->m12, this->m13 );
139  printf("| %f | %f | %f |\n", this->m21, this->m22, this->m23 );
140  printf("| %f | %f | %f |\n", this->m31, this->m32, this->m33 );
141
142}
143
Note: See TracBrowser for help on using the repository browser.