00001 /* Copyright (C) 2001 W.P. van Paassen - peter@paassen.tmfweb.nl 00002 00003 This program is free software; you can redistribute it and/or modify it under 00004 the terms of the GNU General Public License as published by the Free 00005 Software Foundation; either version 2 of the License, or (at your 00006 option) any later version. 00007 00008 This program is distributed in the hope that it will be useful, but WITHOUT 00009 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00010 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 00011 for more details. 00012 00013 You should have received a copy of the GNU General Public License 00014 along with this program; see the file COPYING. If not, write to the Free 00015 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ 00016 00017 #ifndef WP_Matrix3D_H 00018 #define WP_Matrix3D_H 00019 00020 #include "WP_Def.h" 00021 00022 namespace WPCG 00023 { 00042 class WP_Matrix3D 00043 { 00044 public: 00045 friend class WP_Vector3D; 00046 friend class WP_Point3D; 00047 00048 WP_Matrix3D(); 00049 ~WP_Matrix3D(){}; 00050 00057 WP_Matrix3D(const byte type, scalar x, scalar y, scalar z); 00058 00063 WP_Matrix3D(const byte type, WP_Vector3D* v); 00064 00069 WP_Matrix3D(const byte type, scalar angle); 00070 00078 WP_Matrix3D(WP_Vector3D eye, const WP_Vector3D& u, const WP_Vector3D& v, const WP_Vector3D& n); 00079 00083 WP_Matrix3D(scalar f[4][4]); 00084 00088 WP_Matrix3D(scalar* f); 00089 00095 inline WP_Matrix3D& operator=(const WP_Matrix3D& m) 00096 { 00097 if (this == &m) 00098 return *this; 00099 00100 for (int i = 0; i < 16; i++) 00101 { 00102 data[i] = m.data[i]; 00103 } 00104 return *this; 00105 } 00106 00112 inline WP_Matrix3D operator*(const WP_Matrix3D & m) const 00113 { 00114 WP_Matrix3D temp = *this; 00115 temp *= m; 00116 return temp; 00117 } 00118 00124 inline WP_Matrix3D operator+(const WP_Matrix3D & m) const 00125 { 00126 WP_Matrix3D temp = *this; 00127 for (int i = 0; i < 16; i++) 00128 temp.data[i] += m.data[i]; 00129 return temp; 00130 } 00131 00137 inline WP_Matrix3D operator-(const WP_Matrix3D & m) const 00138 { 00139 WP_Matrix3D temp = *this; 00140 for (int i = 0; i < 16; i++) 00141 temp.data[i] -= m.data[i]; 00142 return temp; 00143 } 00144 00150 inline WP_Matrix3D operator*(scalar s) const 00151 { 00152 WP_Matrix3D temp = *this; 00153 for (int i = 0; i < 16; i++) 00154 temp.data[i] *= s; 00155 return temp; 00156 } 00157 00163 inline WP_Matrix3D operator/(scalar s) const 00164 { 00165 WP_Matrix3D temp = *this; 00166 for (int i = 0; i < 16; i++) 00167 temp.data[i] /= s; 00168 return temp; 00169 } 00170 00174 inline WP_Matrix3D& operator-() 00175 { 00176 for (int i = 0; i < 16; i++) 00177 data[i] = -data[i]; 00178 00179 return *this; 00180 } 00181 00182 00188 WP_Matrix3D& operator*=(const WP_Matrix3D & m); 00189 00195 inline WP_Matrix3D& operator+=(const WP_Matrix3D & m) 00196 { 00197 for (int i = 0; i < 16; i++) 00198 data[i] += m.data[i]; 00199 00200 return *this; 00201 } 00202 00208 inline WP_Matrix3D& operator-=(const WP_Matrix3D & m) 00209 { 00210 for (int i = 0; i < 16; i++) 00211 data[i] -= m.data[i]; 00212 00213 return *this; 00214 } 00215 00221 inline WP_Matrix3D& operator*=(scalar s) 00222 { 00223 for (int i = 0; i < 16; i++) 00224 data[i] *= s; 00225 00226 return *this; 00227 } 00228 00234 WP_Matrix3D& operator/=(scalar s) 00235 { 00236 for (int i = 0; i < 16; i++) 00237 data[i] /= s; 00238 00239 return *this; 00240 } 00241 00245 void createIdentity(); 00246 00251 void preMultiply(const WP_Matrix3D& m); 00252 00256 void transpose(); 00257 00261 void print() const; 00262 00267 inline scalar determinant() 00268 { 00269 if (isHomogenous()) 00270 { 00271 return (data[0] * (data[5] * data[10] + (-data[6] * data[9]))) + 00272 (-data[1] * (data[4] * data[10] + (-data[6] * data[8]))) + 00273 (data[2] * (data[4] * data[9] + (-data[5] * data[8]))); 00274 } 00275 else 00276 { 00277 return 0.0; //FIXME implement determinant for non-homogenous matrices 00278 } 00279 } 00280 00281 00286 bool inverse(); 00287 00292 bool isNonSingular() 00293 { 00294 scalar d = determinant(); 00295 return d <= -0.00001 || d >= 0.00001; 00296 } 00297 00302 bool isIdentity(); 00303 00308 inline bool isHomogenous() 00309 { 00310 if (data[3] != 0.0) 00311 return false; 00312 00313 if (data[7] != 0.0) 00314 return false; 00315 00316 if (data[11] != 0.0) 00317 return false; 00318 00319 return data[15] == 1.0; 00320 } 00321 00325 scalar data[16]; 00326 00327 private: 00328 00333 void createXRotation(scalar angle); 00334 00339 void createYRotation(scalar angle); 00340 00345 void createZRotation(scalar angle); 00346 }; 00347 } 00348 #endif 00349