Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/terrain/src/lib/math/matrix.cc @ 8484

Last change on this file since 8484 was 7711, checked in by patrick, 19 years ago

trunk: merged the cd branche back to trunk

File size: 8.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#include <math.h>
17
18#ifdef DEBUG
19#include "debug.h"
20#else
21#include <stdio.h>
22#define PRINT(x) printf
23#endif
24
25/**
26 * constructs a Matrix from all Parameters in a Row
27 * @param m11 [0][0]
28 * @param m12 [0][1]
29 * @param m13 [0][2]
30 * @param m21 [1][0]
31 * @param m22 [1][1]
32 * @param m23 [1][2]
33 * @param m31 [2][0]
34 * @param m32 [2][1]
35 * @param m33 [2][2]
36 */
37Matrix::Matrix ( float m11, float m12, float m13,
38             float m21, float m22, float m23,
39             float m31, float m32, float m33 )
40{
41  this->m11 = m11; this->m12 = m12; this->m13 = m13;
42  this->m21 = m21; this->m22 = m22; this->m23 = m23;
43  this->m31 = m31; this->m32 = m32; this->m33 = m33;
44};
45
46/**
47 * creates a Matrix out of an Array of floats with size [3][3]
48 * @param m the Matrix stored in an Array
49 */
50Matrix::Matrix(const float m[3][3])
51{
52  this->m11 = m[0][0]; this->m12 = m[0][1]; this->m13 = m[0][2];
53  this->m21 = m[1][0]; this->m22 = m[1][1]; this->m23 = m[1][2];
54  this->m31 = m[2][0]; this->m32 = m[2][1]; this->m33 = m[2][2];
55};
56
57
58/**
59 * adds a Matrix to this one returning the result
60 * @param m the Matrix to add to this one
61 * @returns a copy of this Matrix added m
62 */
63Matrix Matrix::operator+ (const Matrix& m) const
64{
65  return Matrix (this->m11 + m.m11, this->m12 + m.m12, this->m13 + m.m13,
66                 this->m21 + m.m21, this->m22 + m.m22, this->m23 + m.m23,
67                 this->m31 + m.m31, this->m32 + m.m32, this->m33 + m.m33);
68}
69
70/**
71 * sustracts a Matrix from this one returning the result
72 * @param m the Matrix to substract from this one
73 * @returns a copy of this Matrix substracted m
74 */
75Matrix Matrix::operator- (const Matrix& m) const
76{
77  return Matrix (this->m11 - m.m11, this->m12 - m.m12, this->m13 - m.m13,
78                 this->m21 - m.m21, this->m22 - m.m22, this->m23 - m.m23,
79                 this->m31 - m.m31, this->m32 - m.m32, this->m33 - m.m33);
80}
81
82/**
83 * multiplies each value of a copu of this Matrix by k
84 * @param k the multiplication factor
85 * @returns a copy of this Matrix multiplied by k
86 */
87Matrix Matrix::operator* (float k) const
88{
89  return Matrix(this->m11 * k, this->m12 * k, this->m13 * k,
90                this->m21 * k, this->m22 * k, this->m23 * k,
91                this->m31 * k, this->m32 * k, this->m33 * k);
92}
93
94/**
95 * multiplies the Matrix by a Vector returning a Vector of the result
96 * @param v the Vector the matrix will be multiplied with
97 * @returns the result of the Multiplication
98 */
99Vector Matrix::operator* (const Vector& v) const
100{
101  return Vector (this->m11*v.x + this->m12*v.y + this->m13*v.z,
102                 this->m21*v.x + this->m22*v.y + this->m23*v.z,
103                 this->m31*v.x + this->m32*v.y + this->m33*v.z );
104}
105
106/**
107 * @returns a Transposed copy of this Matrix
108 */
109Matrix Matrix::getTransposed() const
110{
111  return Matrix( this->m11, this->m21, this->m31,
112                 this->m12, this->m22, this->m32,
113                 this->m13, this->m23, this->m33);
114}
115
116/**
117 * converts the Matrix into 3 Vector, and returns them in m1, m2 and m3
118 * @param m1 the first Column of the Matrix as a Vector
119 * @param m2 the second Column of the Matrix as a Vector
120 * @param m3 the third Column of the Matrix as a Vector
121 */
122void Matrix::toVectors(Vector& m1, Vector& m2, Vector& m3) const
123{
124  m1 = Vector(this->m11, this->m21, this->m31);
125  m2 = Vector(this->m12, this->m22, this->m32);
126  m3 = Vector(this->m13, this->m23, this->m33);
127}
128
129/**
130 * @returns the Determinant of this Matrix
131 */
132float Matrix::getDeterminant() const
133{
134  return this->m11*(this->m22*this->m33 - this->m23*this->m32) -
135      this->m12*(this->m21*this->m33 - this->m23*this->m31) +
136      this->m13*(this->m21*this->m32 - this->m22*this->m31);
137}
138
139/**
140 * calculates an returns the EingenValues of this Matrix.
141 * @param eigneValues the Values calculated in a Vector
142 * @returns the Count of found eigenValues
143 *
144 * This Function calculates the EigenValues of a 3x3-Matrix explicitly.
145 * the Returned value eigenValues has the Values stored in Vector form
146 * The Vector will be filled upside down, meaning if the count of found
147 * eingenValues is 1 the only value will be located in eigneValues.x
148 */
149int Matrix::getEigenValues(Vector& eigenValues) const
150{
151  int retVal = -1;
152  float a = 0;
153  float b = 0;
154
155  float c[3];
156
157  // c[0] is the determinante of mat
158  c[0] = this->m11 * this->m22 * this->m33 +
159      2* this->m12 * this->m13 * this->m23 -
160      this->m11 * this->m23 * this->m23 -
161      this->m22 * this->m13 * this->m13 -
162      this->m33 * this->m12 * this->m12;
163
164  // c[1] is the trace of a
165  c[1] = this->m11 * this->m22 -
166      this->m12 * this->m12 +
167      this->m11 * this->m33 -
168      this->m13 * this->m13 +
169      this->m22 * this->m33 -
170      this->m23 * this->m23;
171
172  // c[2] is the sum of the diagonal elements
173  c[2] = this->m11 +
174      this->m22 +
175      this->m33;
176
177
178  // Computing the roots:
179  a = (3.0*c[1] - c[2]*c[2]) / 3.0;
180  b = (-2.0*c[2]*c[2]*c[2] + 9.0*c[1]*c[2] - 27.0*c[0]) / 27.0;
181
182  float Q = b*b/4.0 + a*a*a/27.0;
183
184  // 3 distinct Roots
185  if (Q < 0)
186  {
187    float psi = atan2(sqrt(-Q), -b/2.0);
188    float p = sqrt((b/2.0)*(b/2.0) - Q);
189
190    eigenValues.x = c[2]/3.0 + 2 * pow(p, 1/3.0) * cos(psi/3.0);
191    eigenValues.y = c[2]/3.0 - pow(p, 1/3.0) * (cos(psi/3.0)
192        + sqrt(3.0) * sin(psi/3.0));
193    eigenValues.z = c[2]/3.0 - pow(p, 1/3.0) * (cos(psi/3.0)
194        - sqrt(3.0) * sin(psi/3.0));
195    retVal = 3;
196  }
197  // 2 Distinct Roots
198  else if (Q == 0)
199  {
200    eigenValues.x = eigenValues.y = c[2]/3.0 + pow(b/2.0, 1.0/3.0);
201    eigenValues.z = c[2]/3.0 + 2* pow(b/2.0, 1.0/3.0);
202    retVal = 2;
203  }
204  // 1 Root (not calculating anything.)
205  else if (Q > 0)
206  {
207    eigenValues.x = eigenValues.y = eigenValues.z = 1;
208    retVal = 1;
209  }
210  return retVal;
211}
212
213/**
214 * calculates and returns the EigenVectors of this function as Vectors.
215 * @param eigVc1 the first eigenVector will be stored here.
216 * @param eigVc2 the second eigenVector will be stored here.
217 * @param eigVc3 the third eigenVector will be stored here.
218 */
219void Matrix::getEigenVectors(Vector& eigVc1, Vector& eigVc2, Vector& eigVc3) const
220{
221  Vector eigenValues;
222  int eigenValuesCount = this->getEigenValues(eigenValues);
223
224  if (eigenValuesCount == 2 || eigenValuesCount == 3)
225  {
226    /* eigenvec creation */
227    eigVc1.x = -1/this->m13*(this->m33 - eigenValues.x) +
228        (this->m32*(-this->m31*this->m32 + this->m12*this->m33 - this->m12*eigenValues.x)) /
229        this->m13*(-this->m13*this->m22 - this->m12*this->m23 + this->m13*eigenValues.x);
230
231    eigVc1.y = -( -this->m13*this->m23 + this->m12*this->m33 - this->m12*eigenValues.x) /
232        (-this->m31*this->m22 + this->m12*this->m23 + this->m13*eigenValues.x);
233
234    eigVc1.z = 1.0f;
235
236    eigVc2.x = -1/this->m13*(this->m33 - eigenValues.y) +
237        (this->m32*(-this->m31*this->m32 + this->m12*this->m33 - this->m12*eigenValues.y)) /
238        this->m13*(-this->m13*this->m22 - this->m12*this->m23 + this->m13*eigenValues.y);
239
240    eigVc2.y = -( -this->m13*this->m23 + this->m12*this->m33 - this->m12*eigenValues.y) /
241        (-this->m31*this->m22 + this->m12*this->m23 + this->m13*eigenValues.y);
242
243    eigVc2.z = 1.0f;
244
245    eigVc3 = eigVc1.cross(eigVc2);
246
247    eigVc2 = eigVc3.cross(eigVc1);
248  }
249  else if (eigenValuesCount == 1)
250  {
251    eigVc1 = Vector(1,0,0);
252    eigVc2 = Vector(0,1,0);
253    eigVc3 = Vector(0,0,1);
254  }
255  eigVc1.normalize();
256  eigVc2.normalize();
257  eigVc3.normalize();
258
259  if (!(eigVc1.cross(eigVc3) == eigVc2))
260  {
261    eigVc3.cross(eigVc1);
262//     eigVc2.debug();
263  }
264/*  printf("ok\n")*/;
265}
266
267/**
268 * prints out some nice debug information
269 */
270void Matrix::debug() const
271{
272  printf("| %f | %f | %f |\n", this->m11, this->m12, this->m13 );
273  printf("| %f | %f | %f |\n", this->m21, this->m22, this->m23 );
274  printf("| %f | %f | %f |\n", this->m31, this->m32, this->m33 );
275
276}
277
Note: See TracBrowser for help on using the repository browser.