Developer Documentation
Loading...
Searching...
No Matches
Matrix4x4T.hh
1/*===========================================================================*\
2 * *
3 * OpenFlipper *
4 * Copyright (c) 2001-2015, RWTH-Aachen University *
5 * Department of Computer Graphics and Multimedia *
6 * All rights reserved. *
7 * www.openflipper.org *
8 * *
9 *---------------------------------------------------------------------------*
10 * This file is part of OpenFlipper. *
11 *---------------------------------------------------------------------------*
12 * *
13 * Redistribution and use in source and binary forms, with or without *
14 * modification, are permitted provided that the following conditions *
15 * are met: *
16 * *
17 * 1. Redistributions of source code must retain the above copyright notice, *
18 * this list of conditions and the following disclaimer. *
19 * *
20 * 2. Redistributions in binary form must reproduce the above copyright *
21 * notice, this list of conditions and the following disclaimer in the *
22 * documentation and/or other materials provided with the distribution. *
23 * *
24 * 3. Neither the name of the copyright holder nor the names of its *
25 * contributors may be used to endorse or promote products derived from *
26 * this software without specific prior written permission. *
27 * *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40\*===========================================================================*/
41
42
43
44
45
46//=============================================================================
47//
48// CLASS Matrix4x4T
49//
50//=============================================================================
51
52
53#ifndef ACG_MATRIX4X4_HH
54#define ACG_MATRIX4X4_HH
55
56
57//== INCLUDES =================================================================
58
59#include "VectorT.hh"
60#include <cmath>
61#include "../Config/ACGDefines.hh"
62#include <iostream>
63#include <algorithm>
64#include <functional>
65
66
67//== NAMESPACES ==============================================================
68
69
70namespace ACG {
71
72
73//== MACROS / HELPERS =========================================================
74
75template<typename Scalar> inline bool checkEpsilon(Scalar x) {
76 return fabs(x) < (1e-6);
77}
78
79template<> inline bool checkEpsilon(float x) {
80 return fabs(x) < (1e-4);
81}
82
83
84
85//== CLASS DEFINITION =========================================================
86
87
91template <class Scalar>
93{
94public:
95 using value_type = Scalar;
96
99
101 template <class OtherScalar>
103 operator=(_rhs);
104 }
105
108 inline Matrix4x4T(const Scalar _array[16]) {
109 std::copy(_array,_array+16, mat_);
110 }
111
114
115
117 template<typename otherScalar>
119 // produces warning C4244 on msvc (implicit cast double to float)
120// std::copy(_rhs.data(),_rhs.data()+16,mat_);
121 for (int i = 0; i < 16; ++i)
122 mat_[i] = Scalar(_rhs.data()[i]);
123
124 return *this;
125 }
126
127
128
130 inline Scalar& operator()(unsigned int row, unsigned int col) {
131 return mat_[(row)+((col)<<2)];
132 }
133
135 inline const Scalar& operator()(unsigned int row, unsigned int col) const {
136 return mat_[(row)+((col)<<2)];
137 }
138
139
141 inline bool operator== (const Matrix4x4T<Scalar>& _rhs) const {
142 int i;
143 const Scalar *a = mat_;
144 const Scalar *b = _rhs.mat_;
145 for(i=0;i< 16;i++,a++,b++)
146 if(! checkEpsilon( *a - *b ))
147 return false;
148 return true;
149 }
150
152 inline bool operator!= (const Matrix4x4T<Scalar>& _rhs) const {
153 return !( operator==(_rhs) );
154 }
155
156
159 std::transform(mat_,mat_+16, _rhs.mat_, _rhs.mat_, std::plus<Scalar>());
160 return _rhs;
161 }
162
165 std::transform(mat_,mat_+16, _rhs.mat_, _rhs.mat_, std::minus<Scalar>());
166 return _rhs;
167 }
168
170 Matrix4x4T operator*(const Matrix4x4T<Scalar>& inst) const;
171
173 Matrix4x4T operator*(const Scalar& scalar);
174
175
178 std::transform(mat_,mat_+16,_rhs.mat_, mat_, std::plus<Scalar>());
179 return *this;
180 }
181
184 std::transform(mat_,mat_+16, _rhs.mat_, mat_, std::minus<Scalar>());
185 return *this;
186 }
187
190
193
194
196 template <typename T>
197 inline VectorT<T,4> operator*(const VectorT<T,4>& _v) const;
198
200 template <typename T>
201 inline VectorT<T,3> transform_point(const VectorT<T,3>& _v) const;
202
204 template <typename T>
205 inline VectorT<T,3> transform_vector(const VectorT<T,3>& _v) const;
206
208 inline void clear();
209
211 inline void identity();
212
213
215 inline bool is_identity() const {
216 int i;
217 const Scalar *a = mat_;
218 Scalar b = 0.0;
219 for(i=0;i< 16;i++,a++,b++) {
220 if ( ( i == 0) || ( i == 5 ) || ( i == 10 ) || ( i == 15 ) )
221 b = 1.0;
222 else
223 b = 0.0;
224 if(! checkEpsilon( *a - b ))
225 return false;
226 }
227 return true;
228 }
229
230
232 inline void transpose();
233
234
236 bool invert();
237
238 Scalar determinant() const {
239 return mat_[12] * mat_[9] * mat_[6] * mat_[3] - mat_[8] * mat_[13] * mat_[6] * mat_[3] -
240 mat_[12] * mat_[5] * mat_[10] * mat_[3] + mat_[4] * mat_[13] * mat_[10] * mat_[3] +
241 mat_[8] * mat_[5] * mat_[14] * mat_[3] - mat_[4] * mat_[9] * mat_[14] * mat_[3] -
242 mat_[12] * mat_[9] * mat_[2] * mat_[7] + mat_[8] * mat_[13] * mat_[2] * mat_[7] +
243 mat_[12] * mat_[1] * mat_[10] * mat_[7] - mat_[0] * mat_[13] * mat_[10] * mat_[7] -
244 mat_[8] * mat_[1] * mat_[14] * mat_[7] + mat_[0] * mat_[9] * mat_[14] * mat_[7] +
245 mat_[12] * mat_[5] * mat_[2] * mat_[11] - mat_[4] * mat_[13] * mat_[2] * mat_[11] -
246 mat_[12] * mat_[1] * mat_[6] * mat_[11] + mat_[0] * mat_[13] * mat_[6] * mat_[11] +
247 mat_[4] * mat_[1] * mat_[14] * mat_[11] - mat_[0] * mat_[5] * mat_[14] * mat_[11] -
248 mat_[8] * mat_[5] * mat_[2] * mat_[15] + mat_[4] * mat_[9] * mat_[2] * mat_[15] +
249 mat_[8] * mat_[1] * mat_[6] * mat_[15] - mat_[0] * mat_[9] * mat_[6] * mat_[15] -
250 mat_[4] * mat_[1] * mat_[10] * mat_[15] + mat_[0] * mat_[5] * mat_[10] * mat_[15];
251 }
252
253
257 inline const Scalar* get_raw_data() const { return mat_; }
258 inline const Scalar* raw() const { return mat_; }
259 inline const Scalar* data() const { return mat_; }
260
261protected:
262
263 Scalar mat_[16];
264};
265
266
271
272
273
274
275//== IO to/from streams =======================================================
276
277
279template<typename Scalar>
280inline std::ostream&
281operator<<(std::ostream& os, const Matrix4x4T<Scalar>& m)
282{
283 for(int i=0; i<4; i++)
284 {
285 for(int j=0; j<4; j++)
286 os << m(i,j) << " ";
287 os << "\n";
288 }
289 return os;
290}
291
292
294template<typename Scalar>
295inline std::istream&
296operator>>(std::istream& is, Matrix4x4T<Scalar>& m)
297{
298 for(int i=0; i<4; i++)
299 for(int j=0; j<4; j++)
300 is >> m(i,j);
301 return is;
302}
303
304//=============================================================================
305} // namespace ACG
306//=============================================================================
307#if defined(INCLUDE_TEMPLATES) && !defined(ACG_MATRIX4X4_C)
308#define ACG_MATRIX4X4_TEMPLATES
309#include "Matrix4x4T_impl.hh"
310#endif
311//=============================================================================
312#endif // ACG_MATRIX4X4_HH defined
313//=============================================================================
314
Matrix4x4T operator+(Matrix4x4T< Scalar > _rhs) const
self + _rhs
Matrix4x4T & operator*=(const Matrix4x4T< Scalar > &_rhs)
self *= _rhs
Matrix4x4T()
constructor: uninitialized values
Definition Matrix4x4T.hh:98
Matrix4x4T(const Scalar _array[16])
const Scalar * get_raw_data() const
Matrix4x4T & leftMult(const Matrix4x4T< Scalar > &_rhs)
multiply from left: self = _rhs * self
const Scalar & operator()(unsigned int row, unsigned int col) const
access operator (read only)
VectorT< T, 3 > transform_vector(const VectorT< T, 3 > &_v) const
transform vector (x',y',z',0) = A * (x,y,z,0)
void identity()
setup an identity matrix
Matrix4x4T operator-(Matrix4x4T< Scalar > _rhs) const
self - _rhs
~Matrix4x4T()
destructor
bool invert()
matrix inversion (returns true on success)
Matrix4x4T(const Matrix4x4T< OtherScalar > &_rhs)
construct from other matrix type
bool operator==(const Matrix4x4T< Scalar > &_rhs) const
compare two matrices (up to some epsilon)
bool is_identity() const
check if the matrix is the identity ( up to an epsilon )
void clear()
sets all elements to zero
Matrix4x4T & operator+=(const Matrix4x4T< Scalar > &_rhs)
self += _rhs
Matrix4x4T operator*(const Matrix4x4T< Scalar > &inst) const
self * _rhs
bool operator!=(const Matrix4x4T< Scalar > &_rhs) const
compare two matrices
Matrix4x4T & operator-=(const Matrix4x4T< Scalar > &_rhs)
self -= _rhs
Matrix4x4T< Scalar > & operator=(const Matrix4x4T< otherScalar > &_rhs)
assignment from other matrix type
Scalar & operator()(unsigned int row, unsigned int col)
access operator (read and write)
void transpose()
transpose matrix
VectorT< T, 3 > transform_point(const VectorT< T, 3 > &_v) const
transform point (x',y',z',1) = M * (x,y,z,1)
Namespace providing different geometric functions concerning angles.
Matrix4x4T< double > Matrix4x4d
typedef
std::ostream & operator<<(std::ostream &os, const Matrix4x4T< Scalar > &m)
output matrix to ostream os
Matrix4x4T< float > Matrix4x4f
typedef
std::istream & operator>>(std::istream &is, Matrix4x4T< Scalar > &m)
read the space-separated components of a vector from a stream *‍/