Blender  V2.59
MT_Matrix3x3.h
Go to the documentation of this file.
00001 /*
00002  * $Id: MT_Matrix3x3.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 
00034 /*
00035 
00036  * Copyright (c) 2000 Gino van den Bergen <gino@acm.org>
00037  *
00038  * Permission to use, copy, modify, distribute and sell this software
00039  * and its documentation for any purpose is hereby granted without fee,
00040  * provided that the above copyright notice appear in all copies and
00041  * that both that copyright notice and this permission notice appear
00042  * in supporting documentation.  Gino van den Bergen makes no
00043  * representations about the suitability of this software for any
00044  * purpose.  It is provided "as is" without express or implied warranty.
00045  *
00046  */
00047 
00048 #ifndef MT_MATRIX3X3_H
00049 #define MT_MATRIX3X3_H
00050 
00051 #include <MT_assert.h>
00052 
00053 #include "MT_Vector3.h"
00054 #include "MT_Quaternion.h"
00055 
00056 class MT_Matrix3x3 {
00057 public:
00058     MT_Matrix3x3() {}
00059     MT_Matrix3x3(const float *m) { setValue(m); }
00060     MT_Matrix3x3(const double *m) { setValue(m); }
00061     MT_Matrix3x3(const MT_Quaternion& q) { setRotation(q); }
00062     
00063         MT_Matrix3x3(const MT_Quaternion& q, const MT_Vector3& s) { 
00064                 setRotation(q); 
00065                 scale(s[0], s[1], s[2]);
00066         }
00067         
00068         MT_Matrix3x3(const MT_Vector3& euler) { setEuler(euler); }
00069         MT_Matrix3x3(const MT_Vector3& euler, const MT_Vector3& s) { 
00070                 setEuler(euler); 
00071                 scale(s[0], s[1], s[2]);
00072         }
00073         
00074     MT_Matrix3x3(MT_Scalar xx, MT_Scalar xy, MT_Scalar xz,
00075                  MT_Scalar yx, MT_Scalar yy, MT_Scalar yz,
00076                  MT_Scalar zx, MT_Scalar zy, MT_Scalar zz) { 
00077         setValue(xx, xy, xz, 
00078                  yx, yy, yz, 
00079                  zx, zy, zz);
00080     }
00081     
00082     MT_Vector3&       operator[](int i)       { return m_el[i]; }
00083     const MT_Vector3& operator[](int i) const { return m_el[i]; }
00084 
00085         MT_Vector3 getColumn(int i) const {
00086                 return MT_Vector3(m_el[0][i], m_el[1][i], m_el[2][i]);
00087         }
00088         void setColumn(int i, const MT_Vector3& v) {
00089                 m_el[0][i] = v[0];
00090                 m_el[1][i] = v[1];
00091                 m_el[2][i] = v[2];
00092         }
00093     
00094     void setValue(const float *m) {
00095         m_el[0][0] = *m++; m_el[1][0] = *m++; m_el[2][0] = *m++; m++;
00096         m_el[0][1] = *m++; m_el[1][1] = *m++; m_el[2][1] = *m++; m++;
00097         m_el[0][2] = *m++; m_el[1][2] = *m++; m_el[2][2] = *m;
00098     }
00099 
00100     void setValue(const double *m) {
00101         m_el[0][0] = *m++; m_el[1][0] = *m++; m_el[2][0] = *m++; m++;
00102         m_el[0][1] = *m++; m_el[1][1] = *m++; m_el[2][1] = *m++; m++;
00103         m_el[0][2] = *m++; m_el[1][2] = *m++; m_el[2][2] = *m;
00104     }
00105 
00106     void setValue3x3(const float *m) {
00107         m_el[0][0] = *m++; m_el[1][0] = *m++; m_el[2][0] = *m++;
00108         m_el[0][1] = *m++; m_el[1][1] = *m++; m_el[2][1] = *m++;
00109         m_el[0][2] = *m++; m_el[1][2] = *m++; m_el[2][2] = *m;
00110     }
00111 
00112     void setValue3x3(const double *m) {
00113         m_el[0][0] = *m++; m_el[1][0] = *m++; m_el[2][0] = *m++;
00114         m_el[0][1] = *m++; m_el[1][1] = *m++; m_el[2][1] = *m++;
00115         m_el[0][2] = *m++; m_el[1][2] = *m++; m_el[2][2] = *m;
00116     }
00117 
00118     void setValue(MT_Scalar xx, MT_Scalar xy, MT_Scalar xz, 
00119                   MT_Scalar yx, MT_Scalar yy, MT_Scalar yz, 
00120                   MT_Scalar zx, MT_Scalar zy, MT_Scalar zz) {
00121         m_el[0][0] = xx; m_el[0][1] = xy; m_el[0][2] = xz;
00122         m_el[1][0] = yx; m_el[1][1] = yy; m_el[1][2] = yz;
00123         m_el[2][0] = zx; m_el[2][1] = zy; m_el[2][2] = zz;
00124     }
00125   
00126     void setRotation(const MT_Quaternion& q) {
00127         MT_Scalar d = q.length2();
00128         MT_assert(!MT_fuzzyZero2(d));
00129         MT_Scalar s = MT_Scalar(2.0) / d;
00130         MT_Scalar xs = q[0] * s,   ys = q[1] * s,   zs = q[2] * s;
00131         MT_Scalar wx = q[3] * xs,  wy = q[3] * ys,  wz = q[3] * zs;
00132         MT_Scalar xx = q[0] * xs,  xy = q[0] * ys,  xz = q[0] * zs;
00133         MT_Scalar yy = q[1] * ys,  yz = q[1] * zs,  zz = q[2] * zs;
00134         setValue(MT_Scalar(1.0) - (yy + zz), xy - wz        ,         xz + wy,
00135                  xy + wz        , MT_Scalar(1.0) - (xx + zz),         yz - wx,
00136                  xz - wy        , yz + wx,         MT_Scalar(1.0) - (xx + yy));
00137     }
00138     
00147         void setEuler(const MT_Vector3& euler) {
00148                 MT_Scalar ci = cos(euler[0]); 
00149                 MT_Scalar cj = cos(euler[1]); 
00150                 MT_Scalar ch = cos(euler[2]);
00151                 MT_Scalar si = sin(euler[0]); 
00152                 MT_Scalar sj = sin(euler[1]); 
00153                 MT_Scalar sh = sin(euler[2]);
00154                 MT_Scalar cc = ci * ch; 
00155                 MT_Scalar cs = ci * sh; 
00156                 MT_Scalar sc = si * ch; 
00157                 MT_Scalar ss = si * sh;
00158                 
00159                 setValue(cj * ch, sj * sc - cs, sj * cc + ss,
00160                                  cj * sh, sj * ss + cc, sj * cs - sc, 
00161                                  -sj,      cj * si,      cj * ci);
00162         }
00163 
00164         void getEuler(MT_Scalar& yaw, MT_Scalar& pitch, MT_Scalar& roll) const
00165                 {                       
00166                         if (m_el[2][0] != -1.0 && m_el[2][0] != 1.0) {
00167                                 pitch = MT_Scalar(-asin(m_el[2][0]));
00168                                 yaw = MT_Scalar(atan2(m_el[2][1] / cos(pitch), m_el[2][2] / cos(pitch)));
00169                                 roll = MT_Scalar(atan2(m_el[1][0] / cos(pitch), m_el[0][0] / cos(pitch)));                              
00170                         }
00171                         else {
00172                                 roll = MT_Scalar(0);
00173                                 if (m_el[2][0] == -1.0) {
00174                                         pitch = MT_PI / 2.0;
00175                                         yaw = MT_Scalar(atan2(m_el[0][1], m_el[0][2]));
00176                                 }
00177                                 else {
00178                                         pitch = - MT_PI / 2.0;
00179                                         yaw = MT_Scalar(atan2(m_el[0][1], m_el[0][2]));
00180                                 }
00181                         }
00182                 }
00183 
00184     void scale(MT_Scalar x, MT_Scalar y, MT_Scalar z) {
00185         m_el[0][0] *= x; m_el[0][1] *= y; m_el[0][2] *= z;
00186         m_el[1][0] *= x; m_el[1][1] *= y; m_el[1][2] *= z;
00187         m_el[2][0] *= x; m_el[2][1] *= y; m_el[2][2] *= z;
00188     }
00189 
00190     MT_Matrix3x3 scaled(MT_Scalar x, MT_Scalar y, MT_Scalar z) const {
00191         return MT_Matrix3x3(m_el[0][0] * x, m_el[0][1] * y, m_el[0][2] * z,
00192                             m_el[1][0] * x, m_el[1][1] * y, m_el[1][2] * z,
00193                             m_el[2][0] * x, m_el[2][1] * y, m_el[2][2] * z);
00194     }
00195     
00196     void setIdentity() { 
00197         setValue(MT_Scalar(1.0), MT_Scalar(0.0), MT_Scalar(0.0), 
00198                  MT_Scalar(0.0), MT_Scalar(1.0), MT_Scalar(0.0), 
00199                  MT_Scalar(0.0), MT_Scalar(0.0), MT_Scalar(1.0)); 
00200     }
00201     
00202     void getValue(float *m) const {
00203         *m++ = (float) m_el[0][0]; *m++ = (float) m_el[1][0]; *m++ = (float) m_el[2][0]; *m++ = (float) 0.0;
00204         *m++ = (float) m_el[0][1]; *m++ = (float) m_el[1][1]; *m++ = (float) m_el[2][1]; *m++ = (float) 0.0;
00205         *m++ = (float) m_el[0][2]; *m++ = (float) m_el[1][2]; *m++ = (float) m_el[2][2]; *m   = (float) 0.0;
00206     }
00207 
00208     void getValue(double *m) const {
00209         *m++ = m_el[0][0]; *m++ = m_el[1][0]; *m++ = m_el[2][0]; *m++ = 0.0;
00210         *m++ = m_el[0][1]; *m++ = m_el[1][1]; *m++ = m_el[2][1]; *m++ = 0.0;
00211         *m++ = m_el[0][2]; *m++ = m_el[1][2]; *m++ = m_el[2][2]; *m   = 0.0;
00212     }
00213 
00214     void getValue3x3(float *m) const {
00215         *m++ = (float) m_el[0][0]; *m++ = (float) m_el[1][0]; *m++ = (float) m_el[2][0];
00216         *m++ = (float) m_el[0][1]; *m++ = (float) m_el[1][1]; *m++ = (float) m_el[2][1];
00217         *m++ = (float) m_el[0][2]; *m++ = (float) m_el[1][2]; *m++ = (float) m_el[2][2];
00218     }
00219 
00220     void getValue3x3(double *m) const {
00221         *m++ = m_el[0][0]; *m++ = m_el[1][0]; *m++ = m_el[2][0];
00222         *m++ = m_el[0][1]; *m++ = m_el[1][1]; *m++ = m_el[2][1];
00223         *m++ = m_el[0][2]; *m++ = m_el[1][2]; *m++ = m_el[2][2];
00224     }
00225 
00226     MT_Quaternion getRotation() const;
00227 
00228     MT_Matrix3x3& operator*=(const MT_Matrix3x3& m); 
00229 
00230     MT_Scalar tdot(int c, const MT_Vector3& v) const {
00231         return m_el[0][c] * v[0] + m_el[1][c] * v[1] + m_el[2][c] * v[2];
00232     }
00233   
00234     MT_Scalar cofac(int r1, int c1, int r2, int c2) const {
00235         return m_el[r1][c1] * m_el[r2][c2] - m_el[r1][c2] * m_el[r2][c1];
00236     }
00237 
00238     MT_Scalar    determinant() const;
00239         MT_Matrix3x3 adjoint() const;
00240 
00241     MT_Matrix3x3 absolute() const;
00242 
00243     MT_Matrix3x3 transposed() const;
00244     void         transpose();
00245 
00246     MT_Matrix3x3 inverse() const; 
00247         void         invert();
00248   
00249 protected:
00250 
00251     MT_Vector3 m_el[3];
00252 };
00253 
00254 MT_Vector3   operator*(const MT_Matrix3x3& m, const MT_Vector3& v);
00255 MT_Vector3   operator*(const MT_Vector3& v, const MT_Matrix3x3& m);
00256 MT_Matrix3x3 operator*(const MT_Matrix3x3& m1, const MT_Matrix3x3& m2);
00257 
00258 MT_Matrix3x3 MT_multTransposeLeft(const MT_Matrix3x3& m1, const MT_Matrix3x3& m2);
00259 MT_Matrix3x3 MT_multTransposeRight(const MT_Matrix3x3& m1, const MT_Matrix3x3& m2);
00260 
00261 inline MT_OStream& operator<<(MT_OStream& os, const MT_Matrix3x3& m) {
00262     return os << m[0] << GEN_endl << m[1] << GEN_endl << m[2] << GEN_endl;
00263 }
00264 
00265 #ifdef GEN_INLINED
00266 #include "MT_Matrix3x3.inl"
00267 #endif
00268 
00269 #endif
00270