Blender  V2.59
MT_Matrix4x4.h
Go to the documentation of this file.
00001 /*
00002  * $Id: MT_Matrix4x4.h 35158 2011-02-25 11:49:19Z jesterking $
00003  * ***** BEGIN GPL LICENSE BLOCK *****
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * as published by the Free Software Foundation; either version 2
00008  * of the License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software Foundation,
00017  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018  *
00019  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00020  * All rights reserved.
00021  *
00022  * The Original Code is: all of this file.
00023  *
00024  * Contributor(s): none yet.
00025  *
00026  * ***** END GPL LICENSE BLOCK *****
00027  */
00028 
00041 #ifndef MT_MATRIX4X4_H
00042 #define MT_MATRIX4X4_H
00043 
00044 #include <MT_assert.h>
00045 
00046 #include "MT_Vector4.h"
00047 #include "MT_Transform.h"
00048 
00049 // Row-major 4x4 matrix
00050 
00051 class MT_Matrix4x4 {
00052 public:
00056     MT_Matrix4x4() {}
00060     MT_Matrix4x4(const float *m) { setValue(m); }
00064     MT_Matrix4x4(const double *m) { setValue(m); }
00065     
00069     MT_Matrix4x4(MT_Scalar xx, MT_Scalar xy, MT_Scalar xz, MT_Scalar xw,
00070                  MT_Scalar yx, MT_Scalar yy, MT_Scalar yz, MT_Scalar yw,
00071                  MT_Scalar zx, MT_Scalar zy, MT_Scalar zz, MT_Scalar zw,
00072                  MT_Scalar wx, MT_Scalar wy, MT_Scalar wz, MT_Scalar ww) { 
00073         setValue(xx, xy, xz, xw, 
00074                  yx, yy, yz, yw,
00075                  zx, zy, zz, zw,
00076                                  wx, wy, wz, ww);
00077     }
00078         
00082         MT_Matrix4x4(const MT_Transform &t) {
00083 
00084                 const MT_Matrix3x3 &basis = t.getBasis();
00085                 const MT_Vector3 &origin = t.getOrigin();       
00086 
00087                 setValue(
00088                         basis[0][0],basis[0][1],basis[0][2],origin[0],
00089                         basis[1][0],basis[1][1],basis[1][2],origin[1],
00090                         basis[2][0],basis[2][1],basis[2][2],origin[2],
00091                         MT_Scalar(0),MT_Scalar(0),MT_Scalar(0),MT_Scalar(1)
00092                 );
00093         }
00094                 
00098     MT_Vector4&       operator[](int i)       { return m_el[i]; }
00102     const MT_Vector4& operator[](int i) const { return m_el[i]; }
00103 
00107     void setValue(const float *m) {
00108         m_el[0][0] = *m++; m_el[1][0] = *m++; m_el[2][0] = *m++; m_el[3][0] = *m++;
00109         m_el[0][1] = *m++; m_el[1][1] = *m++; m_el[2][1] = *m++; m_el[3][1] = *m++;
00110         m_el[0][2] = *m++; m_el[1][2] = *m++; m_el[2][2] = *m++; m_el[3][2] = *m++;
00111         m_el[0][3] = *m++; m_el[1][3] = *m++; m_el[2][3] = *m++; m_el[3][3] = *m;
00112     }
00113 
00118     void setValue(const double *m) {
00119         m_el[0][0] = *m++; m_el[1][0] = *m++; m_el[2][0] = *m++; m_el[3][0] = *m++;
00120         m_el[0][1] = *m++; m_el[1][1] = *m++; m_el[2][1] = *m++; m_el[3][1] = *m++;
00121         m_el[0][2] = *m++; m_el[1][2] = *m++; m_el[2][2] = *m++; m_el[3][2] = *m++;
00122         m_el[0][3] = *m++; m_el[1][3] = *m++; m_el[2][3] = *m++; m_el[3][3] = *m;
00123     }
00124 
00128     void setValue(MT_Scalar xx, MT_Scalar xy, MT_Scalar xz, MT_Scalar xw,
00129                   MT_Scalar yx, MT_Scalar yy, MT_Scalar yz, MT_Scalar yw,
00130                   MT_Scalar zx, MT_Scalar zy, MT_Scalar zz, MT_Scalar zw,
00131                   MT_Scalar wx, MT_Scalar wy, MT_Scalar wz, MT_Scalar ww) {
00132         m_el[0][0] = xx; m_el[0][1] = xy; m_el[0][2] = xz; m_el[0][3] = xw;
00133         m_el[1][0] = yx; m_el[1][1] = yy; m_el[1][2] = yz; m_el[1][3] = yw;
00134         m_el[2][0] = zx; m_el[2][1] = zy; m_el[2][2] = zz; m_el[2][3] = zw;
00135         m_el[3][0] = wx; m_el[3][1] = wy; m_el[3][2] = wz; m_el[3][3] = ww;
00136     }
00137         
00141     void scale(MT_Scalar x, MT_Scalar y, MT_Scalar z, MT_Scalar w) {
00142         m_el[0][0] *= x; m_el[0][1] *= y; m_el[0][2] *= z; m_el[0][3] *= w;
00143         m_el[1][0] *= x; m_el[1][1] *= y; m_el[1][2] *= z; m_el[1][3] *= w;
00144         m_el[2][0] *= x; m_el[2][1] *= y; m_el[2][2] *= z; m_el[2][3] *= w;
00145         m_el[3][0] *= x; m_el[3][1] *= y; m_el[3][2] *= z; m_el[3][3] *= w;
00146     }
00147 
00151     MT_Matrix4x4 scaled(MT_Scalar x, MT_Scalar y, MT_Scalar z, MT_Scalar w) const {
00152         return MT_Matrix4x4(m_el[0][0] * x, m_el[0][1] * y, m_el[0][2] * z, m_el[0][3] * w,
00153                             m_el[1][0] * x, m_el[1][1] * y, m_el[1][2] * z, m_el[1][3] * w,
00154                             m_el[2][0] * x, m_el[2][1] * y, m_el[2][2] * z, m_el[2][3] * w,
00155                             m_el[3][0] * x, m_el[3][1] * y, m_el[3][2] * z, m_el[3][3] * w);
00156     }
00157 
00161     void setIdentity() { 
00162         setValue(MT_Scalar(1.0), MT_Scalar(0.0), MT_Scalar(0.0), MT_Scalar(0.0),
00163                  MT_Scalar(0.0), MT_Scalar(1.0), MT_Scalar(0.0), MT_Scalar(0.0),
00164                  MT_Scalar(0.0), MT_Scalar(0.0), MT_Scalar(1.0), MT_Scalar(0.0),
00165                              MT_Scalar(0.0), MT_Scalar(0.0), MT_Scalar(0.0), MT_Scalar(1.0)); 
00166     }
00167 
00171         float getElement(int i, int j) {
00172                 return (float) m_el[i][j];
00173         }
00174         
00178     void getValue(float *m) const {
00179         *m++ = (float) m_el[0][0]; *m++ = (float) m_el[1][0]; *m++ = (float) m_el[2][0]; *m++ = (float) m_el[3][0];
00180         *m++ = (float) m_el[0][1]; *m++ = (float) m_el[1][1]; *m++ = (float) m_el[2][1]; *m++ = (float) m_el[3][1];
00181         *m++ = (float) m_el[0][2]; *m++ = (float) m_el[1][2]; *m++ = (float) m_el[2][2]; *m++ = (float) m_el[3][2];
00182         *m++ = (float) m_el[0][3]; *m++ = (float) m_el[1][3]; *m++ = (float) m_el[2][3]; *m = (float) m_el[3][3];
00183     }
00184 
00188     void getValue(double *m) const {
00189         *m++ = m_el[0][0]; *m++ = m_el[1][0]; *m++ = m_el[2][0]; *m++ = m_el[3][0];
00190         *m++ = m_el[0][1]; *m++ = m_el[1][1]; *m++ = m_el[2][1]; *m++ = m_el[3][1];
00191         *m++ = m_el[0][2]; *m++ = m_el[1][2]; *m++ = m_el[2][2]; *m++ = m_el[3][2];
00192         *m++ = m_el[0][3]; *m++ = m_el[1][3]; *m++ = m_el[2][3]; *m = m_el[3][3];
00193     }
00194 
00198     MT_Matrix4x4& operator*=(const MT_Matrix4x4& m); 
00199 
00203     MT_Scalar tdot(int c, const MT_Vector4& v) const {
00204         return m_el[0][c] * v[0]
00205                         + m_el[1][c] * v[1]
00206                         + m_el[2][c] * v[2]
00207                         + m_el[3][c] * v[3];
00208     }
00209 
00210         /* I'll postpone this for now... - nzc*/ 
00211 /*      MT_Scalar    determinant() const; */
00212 /*      MT_Matrix4x4 adjoint() const; */
00213 /*      MT_Matrix4x4 inverse() const;  */
00214 
00215         MT_Matrix4x4 absolute() const;
00216 
00217         MT_Matrix4x4 transposed() const; 
00218         void         transpose();
00219 
00220         MT_Matrix4x4 inverse() const;
00221         void         invert();
00222   
00223 protected:
00227     MT_Vector4 m_el[4];
00228 };
00229 
00230 /* These multiplicators do exactly what you ask from them: they
00231  * multiply in the indicated order. */
00232 MT_Vector4   operator*(const MT_Matrix4x4& m, const MT_Vector4& v);
00233 MT_Vector4   operator*(const MT_Vector4& v, const MT_Matrix4x4& m);
00234 MT_Matrix4x4 operator*(const MT_Matrix4x4& m1, const MT_Matrix4x4& m2);
00235 
00236 /*  MT_Matrix4x4 MT_multTransposeLeft(const MT_Matrix4x4& m1, const MT_Matrix4x4& m2); */
00237 /*  MT_Matrix4x4 MT_multTransposeRight(const MT_Matrix4x4& m1, const MT_Matrix4x4& m2); */
00238 
00239 inline MT_OStream& operator<<(MT_OStream& os, const MT_Matrix4x4& m) {
00240     return os << m[0] << GEN_endl
00241                           << m[1] << GEN_endl
00242                           << m[2] << GEN_endl
00243                           << m[3] << GEN_endl;
00244 
00245 
00246         
00247 }
00248 
00249 #ifdef GEN_INLINED
00250 #include "MT_Matrix4x4.inl"
00251 #endif
00252 
00253 #endif
00254