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