TORCS  1.3.9
The Open Racing Car Simulator
Matrix.h
Go to the documentation of this file.
1 /*
2  3D - C++ Class Library for 3D Transformations
3  Copyright (C) 1996-1998 Gino van den Bergen
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public
16  License along with this library; if not, write to the Free
17  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 
19  Please send remarks, questions and bug reports to gino@win.tue.nl,
20  or write to:
21  Gino van den Bergen
22  Department of Mathematics and Computing Science
23  Eindhoven University of Technology
24  P.O. Box 513, 5600 MB Eindhoven, The Netherlands
25 */
26 
27 #ifndef _MATRIX_H_
28 #define _MATRIX_H_
29 
30 #include "Vector.h"
31 #include "Quaternion.h"
32 
33 typedef Scalar Mat3[3][3];
34 
35 // Row-major 3x3 matrix
36 
37 class Matrix {
38 public:
39  Matrix() {}
40  Matrix(const float *m) { setValue(m); }
41  Matrix(const double *m) { setValue(m); }
43  Matrix(Scalar x, Scalar y, Scalar z) { setScaling(x, y, z); }
44  Matrix(Scalar xx, Scalar xy, Scalar xz,
45  Scalar yx, Scalar yy, Scalar yz,
46  Scalar zx, Scalar zy, Scalar zz) {
47  setValue(xx, xy, xz, yx, yy, yz, zx, zy, zz);
48  }
49 
50  Vector& operator[](int i) { return *(Vector *)elem[i]; }
51  const Vector& operator[](int i) const { return *(Vector *)elem[i]; }
52 
53  Mat3& getValue() { return elem; }
54  const Mat3& getValue() const { return elem; }
55 
56  void setValue(const float *m) {
57  elem[X][X] = *m++; elem[Y][X] = *m++; elem[Z][X] = *m++; m++;
58  elem[X][Y] = *m++; elem[Y][Y] = *m++; elem[Z][Y] = *m++; m++;
59  elem[X][Z] = *m++; elem[Y][Z] = *m++; elem[Z][Z] = *m;
60  }
61 
62  void setValue(const double *m) {
63  elem[X][X] = *m++; elem[Y][X] = *m++; elem[Z][X] = *m++; m++;
64  elem[X][Y] = *m++; elem[Y][Y] = *m++; elem[Z][Y] = *m++; m++;
65  elem[X][Z] = *m++; elem[Y][Z] = *m++; elem[Z][Z] = *m;
66  }
67 
68  void setValue(Scalar xx, Scalar xy, Scalar xz,
69  Scalar yx, Scalar yy, Scalar yz,
70  Scalar zx, Scalar zy, Scalar zz) {
71  elem[X][X] = xx; elem[X][Y] = xy; elem[X][Z] = xz;
72  elem[Y][X] = yx; elem[Y][Y] = yy; elem[Y][Z] = yz;
73  elem[Z][X] = zx; elem[Z][Y] = zy; elem[Z][Z] = zz;
74  }
75 
76  void setRotation(const Quaternion& q) {
77  Scalar d = q.length2();
78  assert(!eqz(d));
79  Scalar s = 2 / d;
80  Scalar xs = q[X] * s, ys = q[Y] * s, zs = q[Z] * s;
81  Scalar wx = q[W] * xs, wy = q[W] * ys, wz = q[W] * zs;
82  Scalar xx = q[X] * xs, xy = q[X] * ys, xz = q[X] * zs;
83  Scalar yy = q[Y] * ys, yz = q[Y] * zs, zz = q[Z] * zs;
84  setValue(1 - (yy + zz), xy - wz, xz + wy,
85  xy + wz , 1 - (xx + zz), yz - wx,
86  xz - wy , yz + wx , 1 - (xx + yy));
87  }
88 
90  setValue(x, 0, 0, 0, y, 0, 0, 0, z);
91  }
92 
93  void setIdentity() { setValue(1, 0, 0, 0, 1, 0, 0, 0, 1); }
94 
95  Matrix& operator*=(const Matrix& m);
96 
97  Scalar tdot(int i, const Vector& v) const {
98  return elem[X][i] * v[X] + elem[Y][i] * v[Y] + elem[Z][i] * v[Z];
99  }
100 
101  Scalar determinant() const;
102  Matrix absolute() const;
103  Matrix transpose() const;
104  Matrix adjoint() const;
105  Matrix inverse() const;
106 
107 protected:
109 };
110 
111 Vector operator*(const Matrix& m, const Vector& v);
112 Vector operator*(const Vector& v, const Matrix& m);
113 Matrix operator*(const Matrix& m1, const Matrix& m2);
114 
115 Matrix multTransposeLeft(const Matrix& m1, const Matrix& m2);
116 
117 Matrix transpose(const Matrix& m);
118 Matrix adjoint(const Matrix& m);
119 Matrix inverse(const Matrix& m);
120 Matrix absolute(const Matrix& m);
121 
122 ostream& operator<<(ostream& os, const Matrix& m);
123 
124 
125 
126 inline Matrix& Matrix::operator*=(const Matrix& m) {
127  setValue(elem[X][X] * m[X][X] + elem[X][Y] * m[Y][X] + elem[X][Z] * m[Z][X],
128  elem[X][X] * m[X][Y] + elem[X][Y] * m[Y][Y] + elem[X][Z] * m[Z][Y],
129  elem[X][X] * m[X][Z] + elem[X][Y] * m[Y][Z] + elem[X][Z] * m[Z][Z],
130  elem[Y][X] * m[X][X] + elem[Y][Y] * m[Y][X] + elem[Y][Z] * m[Z][X],
131  elem[Y][X] * m[X][Y] + elem[Y][Y] * m[Y][Y] + elem[Y][Z] * m[Z][Y],
132  elem[Y][X] * m[X][Z] + elem[Y][Y] * m[Y][Z] + elem[Y][Z] * m[Z][Z],
133  elem[Z][X] * m[X][X] + elem[Z][Y] * m[Y][X] + elem[Z][Z] * m[Z][X],
134  elem[Z][X] * m[X][Y] + elem[Z][Y] * m[Y][Y] + elem[Z][Z] * m[Z][Y],
135  elem[Z][X] * m[X][Z] + elem[Z][Y] * m[Y][Z] + elem[Z][Z] * m[Z][Z]);
136  return *this;
137 }
138 
139 inline Scalar Matrix::determinant() const {
140  return triple((*this)[X], (*this)[Y], (*this)[Z]);
141 }
142 
143 inline Matrix Matrix::absolute() const {
144  return Matrix(fabs(elem[X][X]), fabs(elem[X][Y]), fabs(elem[X][Z]),
145  fabs(elem[Y][X]), fabs(elem[Y][Y]), fabs(elem[Y][Z]),
146  fabs(elem[Z][X]), fabs(elem[Z][Y]), fabs(elem[Z][Z]));
147 }
148 
149 inline Matrix Matrix::transpose() const {
150  return Matrix(elem[X][X], elem[Y][X], elem[Z][X],
151  elem[X][Y], elem[Y][Y], elem[Z][Y],
152  elem[X][Z], elem[Y][Z], elem[Z][Z]);
153 }
154 
155 inline Matrix Matrix::adjoint() const {
156  return Matrix(elem[Y][Y] * elem[Z][Z] - elem[Y][Z] * elem[Z][Y],
157  elem[X][Z] * elem[Z][Y] - elem[X][Y] * elem[Z][Z],
158  elem[X][Y] * elem[Y][Z] - elem[X][Z] * elem[Y][Y],
159  elem[Y][Z] * elem[Z][X] - elem[Y][X] * elem[Z][Z],
160  elem[X][X] * elem[Z][Z] - elem[X][Z] * elem[Z][X],
161  elem[X][Z] * elem[Y][X] - elem[X][X] * elem[Y][Z],
162  elem[Y][X] * elem[Z][Y] - elem[Y][Y] * elem[Z][X],
163  elem[X][Y] * elem[Z][X] - elem[X][X] * elem[Z][Y],
164  elem[X][X] * elem[Y][Y] - elem[X][Y] * elem[Y][X]);
165 }
166 
167 inline Matrix Matrix::inverse() const {
168  Vector co(elem[Y][Y] * elem[Z][Z] - elem[Y][Z] * elem[Z][Y],
169  elem[Y][Z] * elem[Z][X] - elem[Y][X] * elem[Z][Z],
170  elem[Y][X] * elem[Z][Y] - elem[Y][Y] * elem[Z][X]);
171  Scalar d = dot((*this)[X], co);
172  assert(!eqz(d));
173  Scalar s = 1 / d;
174  return Matrix(co[X] * s,
175  (elem[X][Z] * elem[Z][Y] - elem[X][Y] * elem[Z][Z]) * s,
176  (elem[X][Y] * elem[Y][Z] - elem[X][Z] * elem[Y][Y]) * s,
177  co[Y] * s,
178  (elem[X][X] * elem[Z][Z] - elem[X][Z] * elem[Z][X]) * s,
179  (elem[X][Z] * elem[Y][X] - elem[X][X] * elem[Y][Z]) * s,
180  co[Z] * s,
181  (elem[X][Y] * elem[Z][X] - elem[X][X] * elem[Z][Y]) * s,
182  (elem[X][X] * elem[Y][Y] - elem[X][Y] * elem[Y][X]) * s);
183 }
184 
185 inline Vector operator*(const Matrix& m, const Vector& v) {
186  return Vector(dot(m[X], v), dot(m[Y], v), dot(m[Z], v));
187 }
188 
189 inline Vector operator*(const Vector& v, const Matrix& m) {
190  return Vector(m.tdot(X, v), m.tdot(Y, v), m.tdot(Z, v));
191 }
192 
193 inline Matrix operator*(const Matrix& m1, const Matrix& m2) {
194  return Matrix(
195  m1[X][X] * m2[X][X] + m1[X][Y] * m2[Y][X] + m1[X][Z] * m2[Z][X],
196  m1[X][X] * m2[X][Y] + m1[X][Y] * m2[Y][Y] + m1[X][Z] * m2[Z][Y],
197  m1[X][X] * m2[X][Z] + m1[X][Y] * m2[Y][Z] + m1[X][Z] * m2[Z][Z],
198  m1[Y][X] * m2[X][X] + m1[Y][Y] * m2[Y][X] + m1[Y][Z] * m2[Z][X],
199  m1[Y][X] * m2[X][Y] + m1[Y][Y] * m2[Y][Y] + m1[Y][Z] * m2[Z][Y],
200  m1[Y][X] * m2[X][Z] + m1[Y][Y] * m2[Y][Z] + m1[Y][Z] * m2[Z][Z],
201  m1[Z][X] * m2[X][X] + m1[Z][Y] * m2[Y][X] + m1[Z][Z] * m2[Z][X],
202  m1[Z][X] * m2[X][Y] + m1[Z][Y] * m2[Y][Y] + m1[Z][Z] * m2[Z][Y],
203  m1[Z][X] * m2[X][Z] + m1[Z][Y] * m2[Y][Z] + m1[Z][Z] * m2[Z][Z]);
204 }
205 
206 inline Matrix multTransposeLeft(const Matrix& m1, const Matrix& m2) {
207  return Matrix(
208  m1[X][X] * m2[X][X] + m1[Y][X] * m2[Y][X] + m1[Z][X] * m2[Z][X],
209  m1[X][X] * m2[X][Y] + m1[Y][X] * m2[Y][Y] + m1[Z][X] * m2[Z][Y],
210  m1[X][X] * m2[X][Z] + m1[Y][X] * m2[Y][Z] + m1[Z][X] * m2[Z][Z],
211  m1[X][Y] * m2[X][X] + m1[Y][Y] * m2[Y][X] + m1[Z][Y] * m2[Z][X],
212  m1[X][Y] * m2[X][Y] + m1[Y][Y] * m2[Y][Y] + m1[Z][Y] * m2[Z][Y],
213  m1[X][Y] * m2[X][Z] + m1[Y][Y] * m2[Y][Z] + m1[Z][Y] * m2[Z][Z],
214  m1[X][Z] * m2[X][X] + m1[Y][Z] * m2[Y][X] + m1[Z][Z] * m2[Z][X],
215  m1[X][Z] * m2[X][Y] + m1[Y][Z] * m2[Y][Y] + m1[Z][Z] * m2[Z][Y],
216  m1[X][Z] * m2[X][Z] + m1[Y][Z] * m2[Y][Z] + m1[Z][Z] * m2[Z][Z]);
217 }
218 
219 inline Scalar determinant(const Matrix& m) { return m.determinant(); }
220 inline Matrix absolute(const Matrix& m) { return m.absolute(); }
221 inline Matrix transpose(const Matrix& m) { return m.transpose(); }
222 inline Matrix adjoint(const Matrix& m) { return m.adjoint(); }
223 inline Matrix inverse(const Matrix& m) { return m.inverse(); }
224 
225 inline ostream& operator<<(ostream& os, const Matrix& m) {
226  return os << m[X] << endl << m[Y] << endl << m[Z] << endl;
227 }
228 
229 #endif
Definition: Basic.h:58
Matrix adjoint(const Matrix &m)
Definition: Matrix.h:222
void setValue(const double *m)
Definition: Matrix.h:62
static Point q[4]
Definition: Convex.cpp:55
Matrix()
Definition: Matrix.h:39
Matrix(const Quaternion &q)
Definition: Matrix.h:42
Scalar determinant(const Matrix &m)
Definition: Matrix.h:219
Matrix adjoint() const
Definition: Matrix.h:155
Matrix(const double *m)
Definition: Matrix.h:41
Matrix absolute() const
Definition: Matrix.h:143
Scalar triple(const Vector &v1, const Vector &v2, const Vector &v3)
Definition: Vector.h:166
Matrix transpose(const Matrix &m)
Definition: Matrix.h:221
Matrix inverse(const Matrix &m)
Definition: Matrix.h:223
Matrix absolute(const Matrix &m)
Definition: Matrix.h:220
Scalar dot(const Quaternion &q1, const Quaternion &q2)
Definition: Quaternion.h:163
Definition: Basic.h:58
Scalar determinant() const
Definition: Matrix.h:139
const Vector & operator[](int i) const
Definition: Matrix.h:51
Mat3 & getValue()
Definition: Matrix.h:53
Definition: Basic.h:58
Scalar Mat3[3][3]
Definition: Matrix.h:33
void setValue(Scalar xx, Scalar xy, Scalar xz, Scalar yx, Scalar yy, Scalar yz, Scalar zx, Scalar zy, Scalar zz)
Definition: Matrix.h:68
void setIdentity()
Definition: Matrix.h:93
Scalar length2() const
Definition: Vector.h:125
void setScaling(Scalar x, Scalar y, Scalar z)
Definition: Matrix.h:89
Matrix & operator*=(const Matrix &m)
Definition: Matrix.h:126
const Mat3 & getValue() const
Definition: Matrix.h:54
Matrix(Scalar x, Scalar y, Scalar z)
Definition: Matrix.h:43
Definition: Basic.h:58
ostream & operator<<(ostream &os, const Matrix &m)
Definition: Matrix.h:225
Scalar tdot(int i, const Vector &v) const
Definition: Matrix.h:97
Matrix multTransposeLeft(const Matrix &m1, const Matrix &m2)
Definition: Matrix.h:206
Vector operator*(const Matrix &m, const Vector &v)
Definition: Matrix.h:185
Mat3 elem
Definition: Matrix.h:108
void setValue(const float *m)
Definition: Matrix.h:56
Matrix(const float *m)
Definition: Matrix.h:40
Definition: Matrix.h:37
Matrix transpose() const
Definition: Matrix.h:149
static Vector y[4]
Definition: Convex.cpp:56
#define Scalar
Definition: Basic.h:34
Matrix(Scalar xx, Scalar xy, Scalar xz, Scalar yx, Scalar yy, Scalar yz, Scalar zx, Scalar zy, Scalar zz)
Definition: Matrix.h:44
bool eqz(Scalar x)
Definition: Basic.h:47
Definition: Vector.h:32
void setRotation(const Quaternion &q)
Definition: Matrix.h:76
Matrix inverse() const
Definition: Matrix.h:167
Vector & operator[](int i)
Definition: Matrix.h:50