[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 | |
---|
[5675] | 21 | int Matrix::getEigenValues(Vector& eigenValues) const |
---|
[5662] | 22 | { |
---|
[5675] | 23 | int retVal = -1; |
---|
[5661] | 24 | float a = 0; |
---|
| 25 | float b = 0; |
---|
| 26 | |
---|
| 27 | float c[3]; |
---|
| 28 | |
---|
| 29 | // c[0] is the determinante of mat |
---|
[5662] | 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; |
---|
[5661] | 35 | |
---|
| 36 | // c[1] is the trace of a |
---|
[5662] | 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; |
---|
[5661] | 43 | |
---|
| 44 | // c[2] is the sum of the diagonal elements |
---|
[5662] | 45 | c[2] = this->m11 + |
---|
| 46 | this->m22 + |
---|
| 47 | this->m33; |
---|
[5661] | 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 | |
---|
[5662] | 56 | // 3 distinct Roots |
---|
[5661] | 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 | |
---|
[5675] | 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) |
---|
[5661] | 64 | + sqrt(3.0) * sin(psi/3.0)); |
---|
[5675] | 65 | eigenValues.z = c[2]/3.0 - pow(p, 1/3.0) * (cos(psi/3.0) |
---|
[5661] | 66 | - sqrt(3.0) * sin(psi/3.0)); |
---|
[5675] | 67 | retVal = 3; |
---|
[5661] | 68 | } |
---|
[5662] | 69 | // 2 Distinct Roots |
---|
[5661] | 70 | else if (Q == 0) |
---|
| 71 | { |
---|
[5675] | 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; |
---|
[5661] | 75 | } |
---|
[5662] | 76 | // 1 Root (not calculating anything.) |
---|
[5661] | 77 | else if (Q > 0) |
---|
| 78 | { |
---|
[5675] | 79 | eigenValues.x = eigenValues.y = eigenValues.z = 1; |
---|
| 80 | retVal = 1; |
---|
[5661] | 81 | } |
---|
[5675] | 82 | return retVal; |
---|
[5665] | 83 | } |
---|
[5661] | 84 | |
---|
[5675] | 85 | void Matrix::getEigenVectors(Vector& eigVc1, Vector& eigVc2, Vector& eigVc3) const |
---|
[5665] | 86 | { |
---|
[5675] | 87 | Vector eigenValues; |
---|
| 88 | int eigenValuesCount = this->getEigenValues(eigenValues); |
---|
[5665] | 89 | |
---|
[5677] | 90 | if (eigenValuesCount == 2 || eigenValuesCount == 3) |
---|
| 91 | { |
---|
| 92 | /* eigenvec creation */ |
---|
| 93 | eigVc1.x = -1/this->m13*(this->m33 - eigenValues.x) + |
---|
| 94 | (this->m32*(-this->m31*this->m32 + this->m12*this->m33 - this->m12*eigenValues.x)) / |
---|
| 95 | this->m13*(-this->m13*this->m22 - this->m12*this->m23 + this->m13*eigenValues.x); |
---|
[5668] | 96 | |
---|
[5677] | 97 | eigVc1.y = -( -this->m13*this->m23 + this->m12*this->m33 - this->m12*eigenValues.x) / |
---|
| 98 | (-this->m31*this->m22 + this->m12*this->m23 + this->m13*eigenValues.x); |
---|
[5662] | 99 | |
---|
[5677] | 100 | eigVc1.z = 1.0f; |
---|
[5664] | 101 | |
---|
[5677] | 102 | eigVc2.x = -1/this->m13*(this->m33 - eigenValues.y) + |
---|
| 103 | (this->m32*(-this->m31*this->m32 + this->m12*this->m33 - this->m12*eigenValues.y)) / |
---|
| 104 | this->m13*(-this->m13*this->m22 - this->m12*this->m23 + this->m13*eigenValues.y); |
---|
[5664] | 105 | |
---|
[5677] | 106 | eigVc2.y = -( -this->m13*this->m23 + this->m12*this->m33 - this->m12*eigenValues.y) / |
---|
| 107 | (-this->m31*this->m22 + this->m12*this->m23 + this->m13*eigenValues.y); |
---|
[5664] | 108 | |
---|
[5677] | 109 | eigVc2.z = 1.0f; |
---|
[5675] | 110 | |
---|
[5677] | 111 | eigVc3 = eigVc1.cross(eigVc2); |
---|
| 112 | } |
---|
| 113 | else if (eigenValuesCount == 1) |
---|
| 114 | { |
---|
| 115 | eigVc1 = Vector(1,0,0); |
---|
| 116 | eigVc2 = Vector(0,1,0); |
---|
| 117 | eigVc3 = Vector(0,0,1); |
---|
| 118 | } |
---|
[5675] | 119 | eigVc1.normalize(); |
---|
| 120 | eigVc2.normalize(); |
---|
| 121 | eigVc3.normalize(); |
---|
[5662] | 122 | } |
---|
[5661] | 123 | |
---|
[5662] | 124 | void Matrix::debug() const |
---|
| 125 | { |
---|
[5668] | 126 | printf("| %f | %f | %f |\n", this->m11, this->m12, this->m13 ); |
---|
| 127 | printf("| %f | %f | %f |\n", this->m21, this->m22, this->m23 ); |
---|
| 128 | printf("| %f | %f | %f |\n", this->m31, this->m32, this->m33 ); |
---|
[5661] | 129 | |
---|
| 130 | } |
---|
[5662] | 131 | |
---|