[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 | |
---|
[6450] | 85 | |
---|
| 86 | |
---|
| 87 | |
---|
[5675] | 88 | void Matrix::getEigenVectors(Vector& eigVc1, Vector& eigVc2, Vector& eigVc3) const |
---|
[5665] | 89 | { |
---|
[5675] | 90 | Vector eigenValues; |
---|
| 91 | int eigenValuesCount = this->getEigenValues(eigenValues); |
---|
[5665] | 92 | |
---|
[5677] | 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); |
---|
[5668] | 99 | |
---|
[5677] | 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); |
---|
[5662] | 102 | |
---|
[5677] | 103 | eigVc1.z = 1.0f; |
---|
[5664] | 104 | |
---|
[5677] | 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); |
---|
[5664] | 108 | |
---|
[5677] | 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); |
---|
[5664] | 111 | |
---|
[5677] | 112 | eigVc2.z = 1.0f; |
---|
[5675] | 113 | |
---|
[5677] | 114 | eigVc3 = eigVc1.cross(eigVc2); |
---|
[6450] | 115 | |
---|
| 116 | eigVc2 = eigVc3.cross(eigVc1); |
---|
[5677] | 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 | } |
---|
[5675] | 124 | eigVc1.normalize(); |
---|
| 125 | eigVc2.normalize(); |
---|
| 126 | eigVc3.normalize(); |
---|
[6450] | 127 | |
---|
| 128 | if (!(eigVc1.cross(eigVc3) == eigVc2)) |
---|
| 129 | { |
---|
| 130 | eigVc3.cross(eigVc1).debug(); |
---|
| 131 | eigVc2.debug(); |
---|
| 132 | } |
---|
| 133 | printf("ok\n"); |
---|
[5662] | 134 | } |
---|
[5661] | 135 | |
---|
[5662] | 136 | void Matrix::debug() const |
---|
| 137 | { |
---|
[5668] | 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 ); |
---|
[5661] | 141 | |
---|
| 142 | } |
---|
[5662] | 143 | |
---|