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_Vector3D_H 00018 #define WP_Vector3D_H 00019 00020 #include <cmath> 00021 #include <iostream> 00022 #include "WP_Def.h" 00023 #include "WP_Vector3D.h" 00024 #include "WP_Matrix3D.h" 00025 #include "WP_Math.h" 00026 00027 namespace WPCG 00028 { 00029 00048 class WP_Vector3D 00049 { 00050 public: 00051 WP_Vector3D() 00052 { 00053 data[0] = data[1] = data[2] = data[3] = 0.0; 00054 } 00055 00061 WP_Vector3D(scalar x, scalar y, scalar z) 00062 { 00063 data[0] = x; 00064 data[1] = y; 00065 data[2] = z; 00066 data[3] = 0.0; 00067 } 00068 00072 WP_Vector3D(scalar* s) 00073 { 00074 data[0] = s[0]; 00075 data[1] = s[1]; 00076 data[2] = s[2]; 00077 data[3] = 0.0; 00078 } 00079 00080 ~WP_Vector3D(){}; 00081 00082 // copy constructor 00083 00084 WP_Vector3D(const WP_Vector3D& v) 00085 { 00086 data[0] = v.data[0]; 00087 data[1] = v.data[1]; 00088 data[2] = v.data[2]; 00089 } 00090 00096 inline WP_Vector3D& operator=(const WP_Vector3D& v) 00097 { 00098 if (this == &v) 00099 return *this; 00100 data[0] = v.data[0]; 00101 data[1] = v.data[1]; 00102 data[2] = v.data[2]; 00103 return *this; 00104 } 00105 00111 inline scalar operator*(const WP_Vector3D& v) const 00112 { 00113 return data[0] * v.data[0] + data[1] * v.data[1] + data[2] * v.data[2]; 00114 } 00115 00121 inline WP_Vector3D operator-(const WP_Vector3D& v) const 00122 { 00123 WP_Vector3D temp; 00124 temp = *this; 00125 temp.data[0] -= v.data[0]; 00126 temp.data[1] -= v.data[1]; 00127 temp.data[2] -= v.data[2]; 00128 return temp; 00129 } 00130 00136 inline WP_Vector3D operator+(const WP_Vector3D& v) const 00137 { 00138 WP_Vector3D temp; 00139 temp = *this; 00140 temp.data[0] += v.data[0]; 00141 temp.data[1] += v.data[1]; 00142 temp.data[2] += v.data[2]; 00143 return temp; 00144 } 00145 00151 inline WP_Vector3D operator*(scalar s) const 00152 { 00153 WP_Vector3D temp; 00154 temp = *this; 00155 temp.data[0] *= s; 00156 temp.data[1] *= s; 00157 temp.data[2] *= s; 00158 return temp; 00159 } 00160 00166 inline WP_Vector3D operator/(scalar s) const 00167 { 00168 WP_Vector3D temp; 00169 temp = *this; 00170 temp.data[0] /= s; 00171 temp.data[1] /= s; 00172 temp.data[2] /= s; 00173 return temp; 00174 } 00175 00180 inline WP_Vector3D& operator-() 00181 { 00182 data[0] = -data[0]; 00183 data[1] = -data[1]; 00184 data[2] = -data[2]; 00185 return *this; 00186 } 00187 00193 inline WP_Vector3D& operator-=(const WP_Vector3D& v) 00194 { 00195 data[0] = data[0] - v.data[0]; 00196 data[1] = data[1] - v.data[1]; 00197 data[2] = data[2] - v.data[2]; 00198 return *this; 00199 } 00200 00206 inline WP_Vector3D& operator+=(const WP_Vector3D& v) 00207 { 00208 data[0] = data[0] + v.data[0]; 00209 data[1] = data[1] + v.data[1]; 00210 data[2] = data[2] + v.data[2]; 00211 return *this; 00212 } 00213 00219 inline WP_Vector3D& operator*=(scalar s) 00220 { 00221 data[0] *= s; 00222 data[1] *= s; 00223 data[2] *= s; 00224 return *this; 00225 } 00226 00232 inline WP_Vector3D& operator/=(scalar s) 00233 { 00234 data[0] /= s; 00235 data[1] /= s; 00236 data[2] /= s; 00237 return *this; 00238 } 00239 00245 inline WP_Vector3D& operator*=(const WP_Matrix3D& m) 00246 { 00247 WP_Vector3D v = *this; 00248 00249 data[0] = m.data[0] * v.data[0] + m.data[4] * v.data[1] + m.data[8] * v.data[2]; 00250 data[1] = m.data[1] * v.data[0] + m.data[5] * v.data[1] + m.data[9] * v.data[2]; 00251 data[2] = m.data[2] * v.data[0] + m.data[6] * v.data[1] + m.data[10] * v.data[2]; 00252 00253 return *this; 00254 } 00255 00261 inline WP_Vector3D operator*(const WP_Matrix3D& m) const 00262 { 00263 WP_Vector3D temp = *this; 00264 temp *= m; 00265 return temp; 00266 } 00267 00273 inline bool crossProduct(const WP_Vector3D& v) 00274 { 00275 scalar degrees = getDegreesBetween(v); 00276 if ((degrees > -0.00001 && degrees < 0.00001) || (degrees > 179.99999 && degrees < 180.00001)) 00277 { 00278 return false;//vectors have an angle of 0 degrees between them, so crossproduct is not possible 00279 } 00280 00281 *this = WP_Vector3D(data[1] * v.data[2] - data[2] * v.data[1], data[2] * v.data[0] - data[0] * v.data[2], 00282 data[0] * v.data[1] - data[1] * v.data[0]); 00283 return true; 00284 } 00285 00291 inline bool orthogonal(const WP_Vector3D& v) const 00292 { 00293 scalar dot = *this * v; 00294 return dot <= 0.00001 && dot >= -0.00001; 00295 } 00296 00301 inline scalar length() const 00302 { 00303 return sqrt(data[0] * data[0] + data[1] * data[1] + data[2] * data[2]); 00304 } 00305 00310 inline scalar normalize() 00311 { 00312 scalar l = length(); 00313 00314 data[0] /= l; 00315 data[1] /= l; 00316 data[2] /= l; 00317 00318 if (data[0] == 0.0 && data[1] == 0.0 && data[2] == 0.0) 00319 data[0] = data[1] = data[2] = 1.0; 00320 00321 return l; 00322 } 00323 00329 inline scalar getDegreesBetween(const WP_Vector3D& v) const 00330 { 00331 WP_Math* m = WP_Math::getInstance(); 00332 00333 scalar dot = *this * v; 00334 dot /= length() * v.length(); 00335 return m->fRadToDegree((scalar)acos(dot)); 00336 } 00337 00343 inline scalar getRadsBetween(const WP_Vector3D& v) const 00344 { 00345 scalar dot = *this * v; 00346 dot /= length() * v.length(); 00347 return (scalar)acos(dot); 00348 } 00349 00353 inline void print() const 00354 { 00355 cout << data[0] << endl << data[1] << endl << data[2] << endl << data[3] << endl; 00356 } 00357 00361 scalar data[4]; 00362 }; 00363 } 00364 #endif 00365