Matrix4x4T.hh
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 #ifndef ACG_MATRIX4X4_HH
00053 #define ACG_MATRIX4X4_HH
00054
00055
00056
00057
00058 #include "VectorT.hh"
00059 #include <math.h>
00060 #include "../Config/ACGDefines.hh"
00061 #include <iostream>
00062
00063
00064
00065
00066
00067 namespace ACG {
00068
00069
00070
00071
00072
00073 #define matrixOperator(src, dst, op) { \
00074 Scalar *_a = dst;\
00075 const Scalar *_b = src;\
00076 *_a++ op *_b++; *_a++ op *_b++; *_a++ op *_b++; *_a++ op *_b++; \
00077 *_a++ op *_b++; *_a++ op *_b++; *_a++ op *_b++; *_a++ op *_b++; \
00078 *_a++ op *_b++; *_a++ op *_b++; *_a++ op *_b++; *_a++ op *_b++; \
00079 *_a++ op *_b++; *_a++ op *_b++; *_a++ op *_b++; *_a op *_b ; \
00080 }
00081
00082 #define MAT(m,r,c) ((m)[(r)+((c)<<2)])
00083
00084 template<typename Scalar> inline bool checkEpsilon(Scalar x) {
00085 return fabs(x) < (1e-6);
00086 }
00087
00088 template<> inline bool checkEpsilon(float x) {
00089 return fabs(x) < (1e-4);
00090 }
00091
00092
00093
00094
00095
00096
00100 template <class Scalar>
00101 class Matrix4x4T
00102 {
00103 public:
00104
00106 Matrix4x4T() {}
00107
00109 template <class OtherScalar>
00110 inline Matrix4x4T(const Matrix4x4T<OtherScalar> _rhs) {
00111 operator=(_rhs);
00112 }
00113
00116 inline Matrix4x4T(const Scalar _array[16]) {
00117 matrixOperator(_array, mat_, =);
00118 }
00119
00121 ~Matrix4x4T() {}
00122
00123
00125 template<typename otherScalar>
00126 inline Matrix4x4T<Scalar>& operator=(const Matrix4x4T<otherScalar>& _rhs) {
00127 for (int i=0; i<4; ++i)
00128 for (int j=0; j<4; ++j)
00129 operator()(i,j) = _rhs(i,j);
00130 return *this;
00131 }
00132
00133
00134
00136 inline Scalar& operator()(unsigned int row, unsigned int col) {
00137 return MAT(mat_,row,col);
00138 }
00139
00141 inline const Scalar& operator()(unsigned int row, unsigned int col) const {
00142 return MAT(mat_,row,col);
00143 }
00144
00145
00147 inline bool operator== (const Matrix4x4T<Scalar>& _rhs) const {
00148 int i;
00149 const Scalar *a = mat_;
00150 const Scalar *b = _rhs.mat_;
00151 for(i=0;i< 16;i++,a++,b++)
00152 if(! checkEpsilon( *a - *b ))
00153 return false;
00154 return true;
00155 }
00156
00158 inline bool operator!= (const Matrix4x4T<Scalar>& _rhs) const {
00159 return !( operator==(_rhs) );
00160 }
00161
00162
00164 inline Matrix4x4T operator+ (const Matrix4x4T<Scalar>& _rhs) const {
00165 Matrix4x4T<Scalar> m(_rhs);
00166 matrixOperator(mat_, m.mat_, += );
00167 return m;
00168 }
00169
00171 inline Matrix4x4T operator- (const Matrix4x4T<Scalar>& _rhs) const {
00172 Matrix4x4T<Scalar> m(*this);
00173 matrixOperator(_rhs.mat_, m.mat_, -= );
00174 return m;
00175 }
00176
00178 Matrix4x4T operator*(const Matrix4x4T<Scalar>& inst) const;
00179
00180
00182 inline Matrix4x4T& operator+= ( const Matrix4x4T<Scalar>& _rhs) {
00183 matrixOperator(_rhs.mat_, mat_, += );
00184 return *this;
00185 }
00186
00188 inline Matrix4x4T& operator-= ( const Matrix4x4T<Scalar>& _rhs) {
00189 matrixOperator(_rhs.mat_, mat_, -= );
00190 return *this;
00191 }
00192
00194 Matrix4x4T& operator*= (const Matrix4x4T<Scalar>& _rhs);
00195
00197 Matrix4x4T& leftMult(const Matrix4x4T<Scalar>& _rhs);
00198
00199
00201 template <typename T>
00202 inline VectorT<T,4> operator*(const VectorT<T,4>& _v) const;
00203
00205 template <typename T>
00206 inline VectorT<T,3> transform_point(const VectorT<T,3>& _v) const;
00207
00209 template <typename T>
00210 inline VectorT<T,3> transform_vector(const VectorT<T,3>& _v) const;
00211
00212
00214 inline void clear();
00215
00217 inline void identity();
00218
00219
00221 inline bool is_identity() const {
00222 int i;
00223 const Scalar *a = mat_;
00224 Scalar b = 0.0;
00225 for(i=0;i< 16;i++,a++,b++) {
00226 if ( ( i == 0) || ( i == 5 ) || ( i == 10 ) || ( i == 15 ) )
00227 b = 1.0;
00228 else
00229 b = 0.0;
00230 if(! checkEpsilon( *a - b ))
00231 return false;
00232 }
00233 return true;
00234 }
00235
00236
00238 inline void transpose();
00239
00240
00242 bool invert();
00243
00244
00248 inline const Scalar* get_raw_data() const { return mat_; }
00249 inline const Scalar* raw() const { return mat_; }
00250 inline const Scalar* data() const { return mat_; }
00251
00252 private:
00253
00254 Scalar mat_[16];
00255 };
00256
00257
00259 typedef Matrix4x4T<float> Matrix4x4f;
00261 typedef Matrix4x4T<double> Matrix4x4d;
00262
00263
00264
00265
00266
00267
00268
00270 template<typename Scalar>
00271 inline std::ostream&
00272 operator<<(std::ostream& os, const Matrix4x4T<Scalar>& m)
00273 {
00274 for(int i=0; i<4; i++)
00275 {
00276 for(int j=0; j<4; j++)
00277 os << m(i,j) << " ";
00278 os << "\n";
00279 }
00280 return os;
00281 }
00282
00283
00285 template<typename Scalar>
00286 inline std::istream&
00287 operator>>(std::istream& is, Matrix4x4T<Scalar>& m)
00288 {
00289 for(int i=0; i<4; i++)
00290 for(int j=0; j<4; j++)
00291 is >> m(i,j);
00292 return is;
00293 }
00294
00295
00296
00297
00298 #undef matrixOperator
00299 #undef MAT
00300
00301
00302 }
00303
00304 #if defined(INCLUDE_TEMPLATES) && !defined(ACG_MATRIX4X4_C)
00305 #define ACG_MATRIX4X4_TEMPLATES
00306 #include "Matrix4x4T.cc"
00307 #endif
00308
00309 #endif // ACG_MATRIX4X4_HH defined
00310
00311