00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef _MBVEC3_H
00024 #define _MBVEC3_H
00025
00026 #ifdef _WIN32
00027 # pragma warning( push )
00028 # pragma warning(disable:4250)
00029 #endif
00030
00031
00032 #include <Inventor/STL/iostream>
00033 #include <Inventor/STL/limits>
00034 #include <Inventor/SbMathHelper.h>
00035
00036 #include <cmath>
00037
00050 template <typename _T>
00051 class MbVec3
00052 {
00053 public:
00054 typedef _T ValueType;
00055
00057 static MbVec3 numeric_limit_max()
00058 {
00059 return MbVec3<_T>((std::numeric_limits<_T>::max)());
00060 }
00061
00063 MbVec3()
00064 {
00065 vec[0] = vec[1] = vec[2] = 0;
00066 }
00068 MbVec3(const _T v[3])
00069 {
00070 vec[0] = v[0]; vec[1] = v[1]; vec[2] = v[2];
00071 }
00072
00074 MbVec3(_T x, _T y, _T z)
00075 {
00076 vec[0] = x; vec[1] = y; vec[2] = z;
00077 }
00078
00079 template < typename _Type >
00080 MbVec3(_Type x, _Type y, _Type z)
00081 {
00082 vec[0] = (_T) x; vec[1] = (_T) y; vec[2] = (_T) z;
00083 }
00084
00086 explicit MbVec3(char v)
00087 {
00088 vec[0] = (_T)v; vec[1] = (_T)v; vec[2] = (_T)v;
00089 }
00090
00092 explicit MbVec3(unsigned char v)
00093 {
00094 vec[0] = (_T)v; vec[1] = (_T)v; vec[2] = (_T)v;
00095 }
00096
00098 explicit MbVec3(short v)
00099 {
00100 vec[0] = (_T)v; vec[1] = (_T)v; vec[2] = (_T)v;
00101 }
00102
00104 explicit MbVec3(unsigned short v)
00105 {
00106 vec[0] = (_T)v; vec[1] = (_T)v; vec[2] = (_T)v;
00107 }
00108
00110 explicit MbVec3(int v)
00111 {
00112 vec[0] = (_T)v; vec[1] = (_T)v; vec[2] = (_T)v;
00113 }
00114
00116 explicit MbVec3(size_t v)
00117 {
00118 vec[0] = (_T)v; vec[1] = (_T)v; vec[2] = (_T)v;
00119 }
00120
00122 explicit MbVec3(long v)
00123 {
00124 vec[0] = (_T)v; vec[1] = (_T)v; vec[2] = (_T)v;
00125 }
00126
00127 #if defined(_WIN32)
00128
00129 explicit MbVec3(unsigned long v)
00130 {
00131 vec[0] = (_T)v; vec[1] = (_T)v; vec[2] = (_T)v;
00132 }
00133 #endif
00134
00136 explicit MbVec3(float v)
00137 {
00138 vec[0] = (_T)v; vec[1] = (_T)v; vec[2] = (_T)v;
00139 }
00140
00142 explicit MbVec3(double v)
00143 {
00144 vec[0] = (_T)v; vec[1] = (_T)v; vec[2] = (_T)v;
00145 }
00146
00148 explicit MbVec3(long double v)
00149 {
00150 vec[0] = (_T)v; vec[1] = (_T)v; vec[2] = (_T)v;
00151 }
00152
00154 template<typename _MbVec3T>
00155 explicit MbVec3(const _MbVec3T& v)
00156 {
00157 vec[0] = (_T)v[0];
00158 vec[1] = (_T)v[1];
00159 vec[2] = (_T)v[2];
00160 }
00161
00165 MbVec3 cross(const MbVec3<_T> &v) const
00166 {
00167 return MbVec3<_T>(vec[1] * v.vec[2] - vec[2] * v.vec[1],
00168 vec[2] * v.vec[0] - vec[0] * v.vec[2],
00169 vec[0] * v.vec[1] - vec[1] * v.vec[0]);
00170 }
00171
00175 _T dot(const MbVec3<_T> &v) const
00176 {
00177 return (vec[0] * v.vec[0] + vec[1] * v.vec[1] + vec[2] * v.vec[2]);
00178 }
00179
00183 const _T *getValue() const { return vec; }
00184
00188 void getValue(_T &x, _T &y, _T &z) const
00189 {
00190 x = vec[0]; y = vec[1]; z = vec[2];
00191 }
00192
00196 _T length() const
00197 {
00198 return _T(sqrt(double(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2])));
00199 }
00200
00204 _T normalize();
00205
00209 void negate()
00210 {
00211 vec[0] = -vec[0]; vec[1] = -vec[1]; vec[2] = -vec[2];
00212 }
00213
00217 MbVec3 mult(const MbVec3<_T> &v) const
00218 {
00219 return MbVec3<_T>(vec[0] * v.vec[0], vec[1] * v.vec[1],vec[2] * v.vec[2]) ;
00220 }
00221
00225 MbVec3 div(const MbVec3<_T> &v) const
00226 {
00227 return MbVec3<_T>(vec[0] / v.vec[0], vec[1] / v.vec[1],vec[2] / v.vec[2]) ;
00228 }
00229
00233 MbVec3<_T>& setValue(const _T v[3])
00234 {
00235 vec[0] = v[0]; vec[1] = v[1]; vec[2] = v[2]; return *this;
00236 }
00237
00241 MbVec3<_T> &setValue(_T x, _T y, _T z)
00242 {
00243 vec[0] = x; vec[1] = y; vec[2] = z; return *this;
00244 }
00245
00247
00250 _T& operator [](int i) { return (vec[i]); }
00251 const _T & operator [](int i) const { return (vec[i]); }
00252 _T& operator [](size_t i) { return (vec[i]); }
00253 const _T & operator [](size_t i) const { return (vec[i]); }
00255
00259 MbVec3 & operator =(_T d)
00260 {
00261 vec[0] = vec[1] = vec[2] = d;
00262 return *this;
00263 }
00264
00268 MbVec3 & operator *=(_T d)
00269 {
00270 vec[0] *= d;
00271 vec[1] *= d;
00272 vec[2] *= d;
00273 return *this;
00274 }
00275
00279 MbVec3 & operator /=(_T d)
00280 {
00281 vec[0] /= d;
00282 vec[1] /= d;
00283 vec[2] /= d;
00284 return *this;
00285 }
00286
00290 MbVec3 & operator +=(const MbVec3 &v)
00291 {
00292 vec[0] += v.vec[0];
00293 vec[1] += v.vec[1];
00294 vec[2] += v.vec[2];
00295 return *this;
00296 }
00297
00301 MbVec3 & operator -=(const MbVec3 &v)
00302 {
00303 vec[0] -= v.vec[0];
00304 vec[1] -= v.vec[1];
00305 vec[2] -= v.vec[2];
00306 return *this;
00307 }
00308
00309
00313 MbVec3 operator -() const
00314 {
00315 return MbVec3<_T>(-vec[0],-vec[1],-vec[2]);
00316 }
00317
00321 friend MbVec3 operator *(const MbVec3 &v1, const MbVec3 &v2)
00322 {
00323 return MbVec3(v1.vec[0] * v2.vec[0], v1.vec[1] * v2.vec[1], v1.vec[2] * v2.vec[2]);
00324 }
00325
00329 friend MbVec3 operator *(const MbVec3 &v, _T d)
00330 {
00331 return MbVec3<_T>(v.vec[0] * d, v.vec[1] * d, v.vec[2] * d);
00332 }
00333
00337 friend MbVec3 operator *(_T d, const MbVec3 &v)
00338 { return v * d; }
00339
00343 friend MbVec3 operator /(const MbVec3 &v, _T d)
00344 {
00345 return MbVec3<_T>(v.vec[0] / d, v.vec[1] / d, v.vec[2] / d);
00346 }
00347
00351 friend MbVec3 operator /(const MbVec3 &v1, const MbVec3 &v2)
00352 {
00353 return MbVec3(v1.vec[0] / v2.vec[0], v1.vec[1] / v2.vec[1], v1.vec[2] / v2.vec[2]);
00354 }
00355
00359 friend MbVec3 operator +(const MbVec3 &v1, const MbVec3 &v2)
00360 {
00361 return MbVec3(v1.vec[0] + v2.vec[0], v1.vec[1] + v2.vec[1], v1.vec[2] + v2.vec[2]);
00362 }
00363
00367 friend MbVec3 operator +(const MbVec3 &v, _T d)
00368 {
00369 return MbVec3(v.vec[0] + d, v.vec[1] + d, v.vec[2] + d);
00370 }
00371
00375 friend MbVec3 operator +(_T d, const MbVec3 &v)
00376 {
00377 return MbVec3(v.vec[0] + d, v.vec[1] + d, v.vec[2] + d);
00378 }
00379
00383 friend MbVec3 operator -(const MbVec3 &v1, const MbVec3 &v2)
00384 {
00385 return MbVec3(v1.vec[0] - v2.vec[0], v1.vec[1] - v2.vec[1], v1.vec[2] - v2.vec[2]);
00386 }
00387
00391 friend MbVec3 operator -(const MbVec3 &v, _T d)
00392 {
00393 return MbVec3(v.vec[0] - d, v.vec[1] - d, v.vec[2] - d);
00394 }
00395
00399 friend MbVec3 sqrt(const MbVec3 &v)
00400 {
00401 return MbVec3(sqrt(v.vec[0]), sqrt(v.vec[1]), sqrt(v.vec[2]));
00402 }
00403
00407 friend bool operator ==(const MbVec3 &v1, const MbVec3 &v2)
00408 {
00409 return (v1.vec[0] == v2.vec[0] && v1.vec[1] == v2.vec[1] && v1.vec[2] == v2.vec[2]);
00410 }
00411
00415 friend bool operator !=(const MbVec3 &v1, const MbVec3 &v2)
00416 {
00417 return !(v1 == v2);
00418 }
00419
00423 friend bool operator <(const MbVec3 &v1, const MbVec3 &v2)
00424 {
00425 return v1.length() < v2.length();
00426 }
00427
00431 friend bool operator <=(const MbVec3 &v1, const MbVec3 &v2)
00432 {
00433 return v1.length() <= v2.length();
00434 }
00435
00439 friend bool operator >(const MbVec3 &v1, const MbVec3 &v2)
00440 {
00441 return v1.length() > v2.length();
00442 }
00443
00447 friend bool operator >=(const MbVec3 &v1, const MbVec3 &v2)
00448 {
00449 return v1.length() >= v2.length();
00450 }
00451
00456 bool equals(const MbVec3 &v, _T tolerance) const
00457 {
00458 MbVec3 diff = *this - v;
00459 return diff.dot(diff) <= tolerance;
00460 }
00461
00466 bool equals(const MbVec3 &v) const
00467 {
00468 return this->equals(v,std::numeric_limits<_T>::epsilon());
00469 }
00470
00474 friend bool areCollinear(const MbVec3<_T> &v1, const MbVec3<_T> &v2, _T tol)
00475 {
00476 return SbMathHelper::isCoinc(v1[0]*v2[1],v1[1]*v2[0],tol) && SbMathHelper::isCoinc(v1[0]*v2[2],v1[2]*v2[0],tol)
00477 && SbMathHelper::isCoinc(v1[1]*v2[2],v1[2]*v2[1],tol);
00478 }
00479
00484 friend bool areCollinear(const MbVec3<_T> &v1, const MbVec3<_T> &v2)
00485 {
00486 return areCollinear(v1,v2,std::numeric_limits<_T>::epsilon());
00487 }
00488
00492 friend inline std::ostream& operator << (std::ostream& os, const MbVec3& v)
00493 {
00494 return os << "(" << v.vec[0] << "," << v.vec[1] << "," << v.vec[2] << ")";
00495 }
00496
00497 private:
00498 _T vec[3];
00499 };
00500
00501 template <typename _T>
00502 _T
00503 MbVec3<_T>::normalize()
00504 {
00505 _T len = length();
00506
00507 if (len != 0)
00508 (*this) /= len;
00509 else
00510 setValue(0,0,0);
00511 return len;
00512 }
00513
00519 typedef MbVec3<size_t> MbVec3ui;
00520
00526 typedef MbVec3<float> MbVec3f;
00532 typedef MbVec3<double> MbVec3d;
00533
00534 #ifdef _WIN32
00535 # pragma warning( pop )
00536 #endif
00537
00538 #endif
00539
00540
00541
00542
00543
00544
00545
00546