Blender V2.61 - r43446

MT_Matrix4x4.h

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