1 // Copyright (C) 2006 Mathias Froehlich - Mathias.Froehlich@web.de
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Library General Public
5 // License as published by the Free Software Foundation; either
6 // version 2 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Library General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 /// Expression templates for poor programmers ... :)
32 enum { nCols = 4, nRows = 4, nEnts = 16 };
35 /// Default constructor. Does not initialize at all.
36 /// If you need them zero initialized, use SGMatrix::zeros()
39 /// Initialize with nans in the debug build, that will guarantee to have
40 /// a fast uninitialized default constructor in the release but shows up
41 /// uninitialized values in the debug build very fast ...
43 for (unsigned i = 0; i < nEnts; ++i)
44 _data.flat[i] = SGLimits<T>::quiet_NaN();
47 /// Constructor. Initialize by the content of a plain array,
48 /// make sure it has at least 16 elements
49 explicit SGMatrix(const T* data)
50 { for (unsigned i = 0; i < nEnts; ++i) _data.flat[i] = data[i]; }
52 /// Constructor, build up a SGMatrix from given elements
53 SGMatrix(T m00, T m01, T m02, T m03,
54 T m10, T m11, T m12, T m13,
55 T m20, T m21, T m22, T m23,
56 T m30, T m31, T m32, T m33)
58 _data.flat[0] = m00; _data.flat[1] = m10;
59 _data.flat[2] = m20; _data.flat[3] = m30;
60 _data.flat[4] = m01; _data.flat[5] = m11;
61 _data.flat[6] = m21; _data.flat[7] = m31;
62 _data.flat[8] = m02; _data.flat[9] = m12;
63 _data.flat[10] = m22; _data.flat[11] = m32;
64 _data.flat[12] = m03; _data.flat[13] = m13;
65 _data.flat[14] = m23; _data.flat[15] = m33;
68 /// Constructor, build up a SGMatrix from a translation
69 SGMatrix(const SGVec3<T>& trans)
72 /// Constructor, build up a SGMatrix from a rotation and a translation
73 SGMatrix(const SGQuat<T>& quat, const SGVec3<T>& trans)
75 /// Constructor, build up a SGMatrix from a rotation and a translation
76 SGMatrix(const SGQuat<T>& quat)
79 /// Copy constructor for a transposed negated matrix
80 SGMatrix(const TransNegRef<T>& tm)
83 /// Set from a tranlation
84 void set(const SGVec3<T>& trans)
86 _data.flat[0] = 1; _data.flat[4] = 0;
87 _data.flat[8] = 0; _data.flat[12] = -trans(0);
88 _data.flat[1] = 0; _data.flat[5] = 1;
89 _data.flat[9] = 0; _data.flat[13] = -trans(1);
90 _data.flat[2] = 0; _data.flat[6] = 0;
91 _data.flat[10] = 1; _data.flat[14] = -trans(2);
92 _data.flat[3] = 0; _data.flat[7] = 0;
93 _data.flat[11] = 0; _data.flat[15] = 1;
96 /// Set from a scale/rotation and tranlation
97 void set(const SGQuat<T>& quat, const SGVec3<T>& trans)
99 T w = quat.w(); T x = quat.x(); T y = quat.y(); T z = quat.z();
100 T xx = x*x; T yy = y*y; T zz = z*z;
101 T wx = w*x; T wy = w*y; T wz = w*z;
102 T xy = x*y; T xz = x*z; T yz = y*z;
103 _data.flat[0] = 1-2*(yy+zz); _data.flat[1] = 2*(xy-wz);
104 _data.flat[2] = 2*(xz+wy); _data.flat[3] = 0;
105 _data.flat[4] = 2*(xy+wz); _data.flat[5] = 1-2*(xx+zz);
106 _data.flat[6] = 2*(yz-wx); _data.flat[7] = 0;
107 _data.flat[8] = 2*(xz-wy); _data.flat[9] = 2*(yz+wx);
108 _data.flat[10] = 1-2*(xx+yy); _data.flat[11] = 0;
109 // Well, this one is ugly here, as that xform method on the current
110 // object needs the above data to be already set ...
111 SGVec3<T> t = xformVec(trans);
112 _data.flat[12] = -t(0); _data.flat[13] = -t(1);
113 _data.flat[14] = -t(2); _data.flat[15] = 1;
115 /// Set from a scale/rotation and tranlation
116 void set(const SGQuat<T>& quat)
118 T w = quat.w(); T x = quat.x(); T y = quat.y(); T z = quat.z();
119 T xx = x*x; T yy = y*y; T zz = z*z;
120 T wx = w*x; T wy = w*y; T wz = w*z;
121 T xy = x*y; T xz = x*z; T yz = y*z;
122 _data.flat[0] = 1-2*(yy+zz); _data.flat[1] = 2*(xy-wz);
123 _data.flat[2] = 2*(xz+wy); _data.flat[3] = 0;
124 _data.flat[4] = 2*(xy+wz); _data.flat[5] = 1-2*(xx+zz);
125 _data.flat[6] = 2*(yz-wx); _data.flat[7] = 0;
126 _data.flat[8] = 2*(xz-wy); _data.flat[9] = 2*(yz+wx);
127 _data.flat[10] = 1-2*(xx+yy); _data.flat[11] = 0;
128 _data.flat[12] = 0; _data.flat[13] = 0;
129 _data.flat[14] = 0; _data.flat[15] = 1;
132 /// set from a transposed negated matrix
133 void set(const TransNegRef<T>& tm)
135 const SGMatrix& m = tm.m;
136 _data.flat[0] = m(0,0);
137 _data.flat[1] = m(0,1);
138 _data.flat[2] = m(0,2);
139 _data.flat[3] = m(3,0);
141 _data.flat[4] = m(1,0);
142 _data.flat[5] = m(1,1);
143 _data.flat[6] = m(1,2);
144 _data.flat[7] = m(3,1);
146 _data.flat[8] = m(2,0);
147 _data.flat[9] = m(2,1);
148 _data.flat[10] = m(2,2);
149 _data.flat[11] = m(3,2);
151 // Well, this one is ugly here, as that xform method on the current
152 // object needs the above data to be already set ...
153 SGVec3<T> t = xformVec(SGVec3<T>(m(0,3), m(1,3), m(2,3)));
154 _data.flat[12] = -t(0);
155 _data.flat[13] = -t(1);
156 _data.flat[14] = -t(2);
157 _data.flat[15] = m(3,3);
160 /// Access by index, the index is unchecked
161 const T& operator()(unsigned i, unsigned j) const
162 { return _data.flat[i + 4*j]; }
163 /// Access by index, the index is unchecked
164 T& operator()(unsigned i, unsigned j)
165 { return _data.flat[i + 4*j]; }
167 /// Access raw data by index, the index is unchecked
168 const T& operator[](unsigned i) const
169 { return _data.flat[i]; }
170 /// Access by index, the index is unchecked
171 T& operator[](unsigned i)
172 { return _data.flat[i]; }
174 /// Get the data pointer
175 const T* data(void) const
176 { return _data.flat; }
177 /// Get the data pointer
179 { return _data.flat; }
181 /// Readonly interface function to ssg's sgMat4/sgdMat4
182 const T (&sg(void) const)[4][4]
183 { return _data.carray; }
184 /// Interface function to ssg's sgMat4/sgdMat4
186 { return _data.carray; }
189 SGMatrix& operator+=(const SGMatrix& m)
190 { for (unsigned i = 0; i < nEnts; ++i) _data.flat[i] += m._data.flat[i]; return *this; }
191 /// Inplace subtraction
192 SGMatrix& operator-=(const SGMatrix& m)
193 { for (unsigned i = 0; i < nEnts; ++i) _data.flat[i] -= m._data.flat[i]; return *this; }
194 /// Inplace scalar multiplication
196 SGMatrix& operator*=(S s)
197 { for (unsigned i = 0; i < nEnts; ++i) _data.flat[i] *= s; return *this; }
198 /// Inplace scalar multiplication by 1/s
200 SGMatrix& operator/=(S s)
201 { return operator*=(1/T(s)); }
202 /// Inplace matrix multiplication, post multiply
203 SGMatrix& operator*=(const SGMatrix<T>& m2);
205 SGVec3<T> xformPt(const SGVec3<T>& pt) const
208 tpt(0) = (*this)(0,3);
209 tpt(1) = (*this)(1,3);
210 tpt(2) = (*this)(2,3);
211 for (unsigned i = 0; i < SGMatrix<T>::nCols-1; ++i) {
213 tpt(0) += tmp*(*this)(0,i);
214 tpt(1) += tmp*(*this)(1,i);
215 tpt(2) += tmp*(*this)(2,i);
219 SGVec3<T> xformVec(const SGVec3<T>& v) const
223 tv(0) = tmp*(*this)(0,0);
224 tv(1) = tmp*(*this)(1,0);
225 tv(2) = tmp*(*this)(2,0);
226 for (unsigned i = 1; i < SGMatrix<T>::nCols-1; ++i) {
228 tv(0) += tmp*(*this)(0,i);
229 tv(1) += tmp*(*this)(1,i);
230 tv(2) += tmp*(*this)(2,i);
235 /// Return an all zero matrix
236 static SGMatrix zeros(void)
237 { return SGMatrix(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); }
239 /// Return a unit matrix
240 static SGMatrix unit(void)
241 { return SGMatrix(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1); }
244 /// Required to make that alias safe.
250 /// The actual data, the matrix is stored in column major order,
251 /// that matches the storage format of OpenGL
255 /// Class to distinguish between a matrix and the matrix with a transposed
256 /// rotational part and a negated translational part
259 TransNegRef(const SGMatrix<T>& _m) : m(_m) {}
260 const SGMatrix<T>& m;
263 /// Unary +, do nothing ...
267 operator+(const SGMatrix<T>& m)
270 /// Unary -, do nearly nothing
274 operator-(const SGMatrix<T>& m)
277 for (unsigned i = 0; i < SGMatrix<T>::nEnts; ++i)
286 operator+(const SGMatrix<T>& m1, const SGMatrix<T>& m2)
289 for (unsigned i = 0; i < SGMatrix<T>::nEnts; ++i)
290 ret[i] = m1[i] + m2[i];
298 operator-(const SGMatrix<T>& m1, const SGMatrix<T>& m2)
301 for (unsigned i = 0; i < SGMatrix<T>::nEnts; ++i)
302 ret[i] = m1[i] - m2[i];
306 /// Scalar multiplication
307 template<typename S, typename T>
310 operator*(S s, const SGMatrix<T>& m)
313 for (unsigned i = 0; i < SGMatrix<T>::nEnts; ++i)
318 /// Scalar multiplication
319 template<typename S, typename T>
322 operator*(const SGMatrix<T>& m, S s)
325 for (unsigned i = 0; i < SGMatrix<T>::nEnts; ++i)
330 /// Vector multiplication
334 operator*(const SGMatrix<T>& m, const SGVec4<T>& v)
342 for (unsigned i = 1; i < SGMatrix<T>::nCols; ++i) {
352 /// Vector multiplication
356 operator*(const TransNegRef<T>& tm, const SGVec4<T>& v)
358 const SGMatrix<T>& m = tm.m;
362 mv(0) = v2(0) = -tmp*m(0,3);
363 mv(1) = v2(1) = -tmp*m(1,3);
364 mv(2) = v2(2) = -tmp*m(2,3);
366 for (unsigned i = 0; i < SGMatrix<T>::nCols - 1; ++i) {
367 T tmp = v(i) + v2(i);
376 /// Matrix multiplication
380 operator*(const SGMatrix<T>& m1, const SGMatrix<T>& m2)
383 for (unsigned j = 0; j < SGMatrix<T>::nCols; ++j) {
385 m(0,j) = tmp*m1(0,0);
386 m(1,j) = tmp*m1(1,0);
387 m(2,j) = tmp*m1(2,0);
388 m(3,j) = tmp*m1(3,0);
389 for (unsigned i = 1; i < SGMatrix<T>::nCols; ++i) {
391 m(0,j) += tmp*m1(0,i);
392 m(1,j) += tmp*m1(1,i);
393 m(2,j) += tmp*m1(2,i);
394 m(3,j) += tmp*m1(3,i);
400 /// Inplace matrix multiplication, post multiply
404 SGMatrix<T>::operator*=(const SGMatrix<T>& m2)
405 { (*this) = operator*(*this, m2); return *this; }
407 /// Return a reference to the matrix typed to make sure we use the transposed
412 transNeg(const SGMatrix<T>& m)
413 { return TransNegRef<T>(m); }
415 /// Compute the inverse if the matrix src. Store the result in dst.
416 /// Return if the matrix is nonsingular. If it is singular dst contains
421 invert(SGMatrix<T>& dst, const SGMatrix<T>& src)
423 // Do a LU decomposition with row pivoting and solve into dst
424 SGMatrix<T> tmp = src;
425 dst = SGMatrix<T>::unit();
427 for (unsigned i = 0; i < 4; ++i) {
431 // Find the row with the maximum value in the i-th colum
432 for (unsigned j = i + 1; j < 4; ++j) {
433 if (fabs(tmp(j, i)) > fabs(val)) {
441 for (unsigned j = 0; j < 4; ++j) {
443 t = dst(i,j); dst(i,j) = dst(ind,j); dst(ind,j) = t;
444 t = tmp(i,j); tmp(i,j) = tmp(ind,j); tmp(ind,j) = t;
448 // Check for singularity
449 if (fabs(val) <= SGLimits<T>::min())
453 for (unsigned j = 0; j < 4; ++j) {
458 for (unsigned j = 0; j < 4; ++j) {
463 for (unsigned k = 0; k < 4; ++k) {
464 tmp(j,k) -= tmp(i,k) * val;
465 dst(j,k) -= dst(i,k) * val;
472 /// The 1-norm of the matrix, this is the largest column sum of
473 /// the absolute values of A.
477 norm1(const SGMatrix<T>& m)
480 for (unsigned i = 0; i < SGMatrix<T>::nRows; ++i) {
481 T sum = fabs(m(i, 0)) + fabs(m(i, 1)) + fabs(m(i, 2)) + fabs(m(i, 3));
488 /// The inf-norm of the matrix, this is the largest row sum of
489 /// the absolute values of A.
493 normInf(const SGMatrix<T>& m)
496 for (unsigned i = 0; i < SGMatrix<T>::nCols; ++i) {
497 T sum = fabs(m(0, i)) + fabs(m(1, i)) + fabs(m(2, i)) + fabs(m(3, i));
504 /// Return true if exactly the same
508 operator==(const SGMatrix<T>& m1, const SGMatrix<T>& m2)
510 for (unsigned i = 0; i < SGMatrix<T>::nEnts; ++i)
516 /// Return true if not exactly the same
520 operator!=(const SGMatrix<T>& m1, const SGMatrix<T>& m2)
521 { return ! (m1 == m2); }
523 /// Return true if equal to the relative tolerance tol
527 equivalent(const SGMatrix<T>& m1, const SGMatrix<T>& m2, T rtol, T atol)
528 { return norm1(m1 - m2) < rtol*(norm1(m1) + norm1(m2)) + atol; }
530 /// Return true if equal to the relative tolerance tol
534 equivalent(const SGMatrix<T>& m1, const SGMatrix<T>& m2, T rtol)
535 { return norm1(m1 - m2) < rtol*(norm1(m1) + norm1(m2)); }
537 /// Return true if about equal to roundoff of the underlying type
541 equivalent(const SGMatrix<T>& m1, const SGMatrix<T>& m2)
543 T tol = 100*SGLimits<T>::epsilon();
544 return equivalent(m1, m2, tol, tol);
551 isNaN(const SGMatrix<T>& m)
553 for (unsigned i = 0; i < SGMatrix<T>::nEnts; ++i) {
554 if (SGMisc<T>::isNaN(m[i]))
561 /// Output to an ostream
562 template<typename char_type, typename traits_type, typename T>
564 std::basic_ostream<char_type, traits_type>&
565 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGMatrix<T>& m)
567 s << "[ " << m(0,0) << ", " << m(0,1) << ", " << m(0,2) << ", " << m(0,3) << "\n";
568 s << " " << m(1,0) << ", " << m(1,1) << ", " << m(1,2) << ", " << m(1,3) << "\n";
569 s << " " << m(2,0) << ", " << m(2,1) << ", " << m(2,2) << ", " << m(2,3) << "\n";
570 s << " " << m(3,0) << ", " << m(3,1) << ", " << m(3,2) << ", " << m(3,3) << " ]";
574 /// Two classes doing actually the same on different types
575 typedef SGMatrix<float> SGMatrixf;
576 typedef SGMatrix<double> SGMatrixd;
580 toMatrixf(const SGMatrixd& m)
582 return SGMatrixf((float)m(0,0), (float)m(0,1), (float)m(0,2), (float)m(0,3),
583 (float)m(1,0), (float)m(1,1), (float)m(1,2), (float)m(1,3),
584 (float)m(3,0), (float)m(2,1), (float)m(2,2), (float)m(2,3),
585 (float)m(4,0), (float)m(4,1), (float)m(4,2), (float)m(4,3));
590 toMatrixd(const SGMatrixf& m)
592 return SGMatrixd(m(0,0), m(0,1), m(0,2), m(0,3),
593 m(1,0), m(1,1), m(1,2), m(1,3),
594 m(3,0), m(2,1), m(2,2), m(2,3),
595 m(4,0), m(4,1), m(4,2), m(4,3));