52 #ifndef ACG_MATRIX4X4_HH
53 #define ACG_MATRIX4X4_HH
72 #define matrixOperator(src, dst, op) { \
74 const Scalar *_b = src;\
75 *_a++ op *_b++; *_a++ op *_b++; *_a++ op *_b++; *_a++ op *_b++; \
76 *_a++ op *_b++; *_a++ op *_b++; *_a++ op *_b++; *_a++ op *_b++; \
77 *_a++ op *_b++; *_a++ op *_b++; *_a++ op *_b++; *_a++ op *_b++; \
78 *_a++ op *_b++; *_a++ op *_b++; *_a++ op *_b++; *_a op *_b ; \
81 #define MAT(m,r,c) ((m)[(r)+((c)<<2)])
83 template<
typename Scalar>
inline bool checkEpsilon(Scalar x) {
84 return fabs(x) < (1e-6);
87 template<>
inline bool checkEpsilon(
float x) {
88 return fabs(x) < (1e-4);
99 template <
class Scalar>
108 template <
class OtherScalar>
119 matrixOperator(_array, mat_, =);
127 template<
typename otherScalar>
129 for (
int i=0; i<4; ++i)
130 for (
int j=0; j<4; ++j)
131 operator()(i,j) = _rhs(i,j);
138 inline Scalar&
operator()(
unsigned int row,
unsigned int col) {
139 return MAT(mat_,row,col);
143 inline const Scalar&
operator()(
unsigned int row,
unsigned int col)
const {
144 return MAT(mat_,row,col);
151 const Scalar *a = mat_;
152 const Scalar *b = _rhs.mat_;
153 for(i=0;i< 16;i++,a++,b++)
154 if(! checkEpsilon( *a - *b ))
168 matrixOperator(mat_, m.mat_, += );
175 matrixOperator(_rhs.mat_, m.mat_, -= );
188 matrixOperator(_rhs.mat_, mat_, += );
194 matrixOperator(_rhs.mat_, mat_, -= );
206 template <
typename T>
207 inline VectorT<T,4>
operator*(
const VectorT<T,4>& _v)
const;
210 template <
typename T>
214 template <
typename T>
227 const Scalar *a = mat_;
229 for(i=0;i< 16;i++,a++,b++) {
230 if ( ( i == 0) || ( i == 5 ) || ( i == 10 ) || ( i == 15 ) )
234 if(! checkEpsilon( *a - b ))
248 Scalar determinant()
const {
249 return mat_[12] * mat_[9] * mat_[6] * mat_[3] - mat_[8] * mat_[13] * mat_[6] * mat_[3] -
250 mat_[12] * mat_[5] * mat_[10] * mat_[3] + mat_[4] * mat_[13] * mat_[10] * mat_[3] +
251 mat_[8] * mat_[5] * mat_[14] * mat_[3] - mat_[4] * mat_[9] * mat_[14] * mat_[3] -
252 mat_[12] * mat_[9] * mat_[2] * mat_[7] + mat_[8] * mat_[13] * mat_[2] * mat_[7] +
253 mat_[12] * mat_[1] * mat_[10] * mat_[7] - mat_[0] * mat_[13] * mat_[10] * mat_[7] -
254 mat_[8] * mat_[1] * mat_[14] * mat_[7] + mat_[0] * mat_[9] * mat_[14] * mat_[7] +
255 mat_[12] * mat_[5] * mat_[2] * mat_[11] - mat_[4] * mat_[13] * mat_[2] * mat_[11] -
256 mat_[12] * mat_[1] * mat_[6] * mat_[11] + mat_[0] * mat_[13] * mat_[6] * mat_[11] +
257 mat_[4] * mat_[1] * mat_[14] * mat_[11] - mat_[0] * mat_[5] * mat_[14] * mat_[11] -
258 mat_[8] * mat_[5] * mat_[2] * mat_[15] + mat_[4] * mat_[9] * mat_[2] * mat_[15] +
259 mat_[8] * mat_[1] * mat_[6] * mat_[15] - mat_[0] * mat_[9] * mat_[6] * mat_[15] -
260 mat_[4] * mat_[1] * mat_[10] * mat_[15] + mat_[0] * mat_[5] * mat_[10] * mat_[15];
268 inline const Scalar* raw()
const {
return mat_; }
269 inline const Scalar* data()
const {
return mat_; }
278 typedef Matrix4x4T<float> Matrix4x4f;
280 typedef Matrix4x4T<double> Matrix4x4d;
289 template<
typename Scalar>
291 operator<<(std::ostream& os, const Matrix4x4T<Scalar>& m)
293 for(
int i=0; i<4; i++)
295 for(
int j=0; j<4; j++)
304 template<
typename Scalar>
306 operator>>(std::istream& is, Matrix4x4T<Scalar>& m)
308 for(
int i=0; i<4; i++)
309 for(
int j=0; j<4; j++)
317 #undef matrixOperator
323 #if defined(INCLUDE_TEMPLATES) && !defined(ACG_MATRIX4X4_C)
324 #define ACG_MATRIX4X4_TEMPLATES
325 #include "Matrix4x4T.cc"
328 #endif // ACG_MATRIX4X4_HH defined
bool operator!=(const Matrix4x4T< Scalar > &_rhs) const
compare two matrices
Definition: Matrix4x4T.hh:160
void clear()
sets all elements to zero
Definition: Matrix4x4T.cc:233
~Matrix4x4T()
destructor
Definition: Matrix4x4T.hh:123
Matrix4x4T(const Matrix4x4T< OtherScalar > _rhs)
construct from other matrix type
Definition: Matrix4x4T.hh:109
Matrix4x4T & operator+=(const Matrix4x4T< Scalar > &_rhs)
self += _rhs
Definition: Matrix4x4T.hh:187
Definition: Matrix4x4T.cc:65
Matrix4x4T operator*(const Matrix4x4T< Scalar > &inst) const
self * _rhs
Definition: Matrix4x4T.cc:78
void transpose()
transpose matrix
Definition: Matrix4x4T.cc:265
Matrix4x4T(const Scalar _array[16])
Definition: Matrix4x4T.hh:115
VectorT< T, 3 > transform_vector(const VectorT< T, 3 > &_v) const
transform vector (x',y',z',0) = A * (x,y,z,0)
Definition: Matrix4x4T.cc:218
void identity()
setup an identity matrix
Definition: Matrix4x4T.cc:249
Matrix4x4T()
constructor: uninitialized values
Definition: Matrix4x4T.hh:105
bool invert()
matrix inversion (returns true on success)
Definition: Matrix4x4T.cc:290
Matrix4x4T operator+(const Matrix4x4T< Scalar > &_rhs) const
self + _rhs
Definition: Matrix4x4T.hh:166
bool operator==(const Matrix4x4T< Scalar > &_rhs) const
compare two matrices (up to some epsilon)
Definition: Matrix4x4T.hh:149
Definition: Matrix4x4T.hh:100
Matrix4x4T & operator-=(const Matrix4x4T< Scalar > &_rhs)
self -= _rhs
Definition: Matrix4x4T.hh:193
Matrix4x4T & operator*=(const Matrix4x4T< Scalar > &_rhs)
self *= _rhs
Definition: Matrix4x4T.cc:108
VectorT< T, 3 > transform_point(const VectorT< T, 3 > &_v) const
transform point (x',y',z',1) = M * (x,y,z,1)
Definition: Matrix4x4T.cc:195
bool is_identity() const
check if the matrix is the identity ( up to an epsilon )
Definition: Matrix4x4T.hh:225
Scalar & operator()(unsigned int row, unsigned int col)
access operator (read and write)
Definition: Matrix4x4T.hh:138
Matrix4x4T< Scalar > & operator=(const Matrix4x4T< otherScalar > &_rhs)
assignment from other matrix type
Definition: Matrix4x4T.hh:128
const Scalar & operator()(unsigned int row, unsigned int col) const
access operator (read only)
Definition: Matrix4x4T.hh:143
Matrix4x4T & leftMult(const Matrix4x4T< Scalar > &_rhs)
multiply from left: self = _rhs * self
Definition: Matrix4x4T.cc:136
Matrix4x4T operator-(const Matrix4x4T< Scalar > &_rhs) const
self - _rhs
Definition: Matrix4x4T.hh:173
const Scalar * get_raw_data() const
Definition: Matrix4x4T.hh:267