00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef _LIB_NUMERICS_3D_VECTOR3_H_
00018 #define _LIB_NUMERICS_3D_VECTOR3_H_ 1
00019
00124 #include <lib/numerics/num_math.h>
00125
00126 namespace NUM
00127 {
00128
00129
00130 template<typename T>class OMatrix;
00131
00132
00144 template<typename T=dbl>class Vector3
00145 {
00146 public:
00149 enum { X=0, Y=1, Z=2 };
00150
00152 static const Vector3<T> UX,UY,UZ,Null;
00153
00155 enum _Mul { Mul };
00156 enum _TrafoDir { TrafoDir };
00157 enum _TrafoNorm { TrafoNorm };
00158
00159 private:
00161 T v[3];
00164
00165 public:
00167 inline Vector3() {}
00169 inline Vector3(T x,T y,T z)
00170 { v[0]=x; v[1]=y; v[2]=z; }
00172 inline Vector3(const Vector3 &a)
00173 { v[0]=a.v[0]; v[1]=a.v[1]; v[2]=a.v[2]; }
00175 inline Vector3(const T *a)
00176 { v[0]=a[0]; v[1]=a[1]; v[2]=a[2]; }
00178
00179 inline Vector3(_Mul,const OMatrix<T> &a,const Vector3<T> &b);
00181 inline Vector3(_TrafoDir,const OMatrix<T> &m,const Vector3<T> &b);
00183 inline Vector3(_TrafoNorm,const Vector3<T> &a,const OMatrix<T> &m);
00185 inline ~Vector3() {}
00186
00189 inline T &operator[](int i) { return(v[i]); }
00190 inline const T &operator[](int i) const { return(v[i]); }
00191
00193 inline operator T *() { return(v); }
00194 inline operator const T *() const { return(v); }
00195
00197 inline int dim() const
00198 { return(3); }
00199
00201 inline Vector3 &operator=(const Vector3 &a)
00202 { v[0]=a.v[0]; v[1]=a.v[1]; v[2]=a.v[2]; return(*this); }
00203
00205 inline Vector3 &promote(T a)
00206 { v[0]=a; v[1]=a; v[2]=a; return(*this); }
00207
00209 inline T len2() const
00210 { return(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]); }
00212 inline T len() const
00213 { return(sqrt(len2())); }
00214
00216 inline T norm1() const
00217 { return(fabs(v[0])+fabs(v[1])+fabs(v[2])); }
00218
00220 inline T max() const
00221 { return(v[0]>v[1] ? (v[0]>v[2] ? v[0] : v[2]) :
00222 (v[1]>v[2] ? v[1] : v[2])); }
00224 inline T min() const
00225 { return(v[0]<v[1] ? (v[0]<v[2] ? v[0] : v[2]) :
00226 (v[1]<v[2] ? v[1] : v[2])); }
00227
00229 inline Vector3 &operator+=(const Vector3 &a)
00230 { v[0]+=a.v[0]; v[1]+=a.v[1]; v[2]+=a.v[2]; return(*this); }
00232 inline Vector3 &operator-=(const Vector3 &a)
00233 { v[0]-=a.v[0]; v[1]-=a.v[1]; v[2]-=a.v[2]; return(*this); }
00234
00236 inline Vector3 &AddScaled(const Vector3 &a,T f)
00237 { v[0]+=a.v[0]*f; v[1]+=a.v[1]*f; v[2]+=a.v[2]*f; return(*this); }
00238
00240 inline Vector3 &operator*=(T a)
00241 { v[0]*=a; v[1]*=a; v[2]*=a; return(*this); }
00244 inline Vector3 &operator/=(T a)
00245 { v[0]/=a; v[1]/=a; v[2]/=a; return(*this); }
00246
00248 inline Vector3 &cross(const Vector3 &a,const Vector3 &b)
00249 { v[0]=a[1]*b[2] - a[2]*b[1];
00250 v[1]=a[2]*b[0] - a[0]*b[2];
00251 v[2]=a[0]*b[1] - a[1]*b[0]; return(*this); }
00252
00254 inline T normalize()
00255 { T l=len(),f=1.0/l; v[0]*=f; v[1]*=f; v[2]*=f; return(l); }
00257 inline T normalize(T wl)
00258 { T l=len(),f=wl/l; v[0]*=f; v[1]*=f; v[2]*=f; return(l); }
00259
00261 inline Vector3 &neg()
00262 { v[0]=-v[0]; v[1]=-v[1]; v[2]=-v[2]; return(*this); }
00263
00266 inline bool IsNull(T epsilon) const
00267 { return(fabs(v[0])<epsilon && fabs(v[1])<epsilon &&
00268 fabs(v[2])<epsilon); }
00269 };
00270
00272 template<typename T>inline Vector3<T> operator+(
00273 const Vector3<T> &a,const Vector3<T> &b)
00274 { return(Vector3<T>(a[0]+b[0],a[1]+b[1],a[2]+b[2])); }
00276 template<typename T>inline Vector3<T> operator-(
00277 const Vector3<T> &a,const Vector3<T> &b)
00278 { return(Vector3<T>(a[0]-b[0],a[1]-b[1],a[2]-b[2])); }
00279
00281 template<typename T>inline Vector3<T> operator*(const Vector3<T> &a,T b)
00282 { return(Vector3<T>(a[0]*b,a[1]*b,a[2]*b)); }
00283 template<typename T>inline Vector3<T> operator*(T b,const Vector3<T> &a)
00284 { return(Vector3<T>(b*a[0],b*a[1],b*a[2])); }
00285
00288 template<typename T>inline Vector3<T> operator/(const Vector3<T> &a,T b)
00289 { return(Vector3<T>(a[0]/b,a[1]/b,a[2]/b)); }
00290
00292 template<typename T>inline T len2(const Vector3<T> &v)
00293 { return(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]); }
00295 template<typename T>inline T len(const Vector3<T> &v)
00296 { return(sqrt(len2(v))); }
00297
00299 template<typename T>inline T dist2(const Vector3<T> &a,const Vector3<T> &b)
00300 { return(sqr(a[0]-b[0])+sqr(a[1]-b[1])+sqr(a[2]-b[2])); }
00302 template<typename T>inline T dist(const Vector3<T> &a,const Vector3<T> &b)
00303 { return(sqrt(dist2(a,b))); }
00304
00306 template<typename T>inline Vector3<T> normalize(const Vector3<T> &v)
00307 { T f=1.0/v.len(); return(Vector3<T>(v[0]*f,v[1]*f,v[2]*f)); }
00309 template<typename T>inline Vector3<T> normalize(const Vector3<T> &v,T l)
00310 { T f=l/v.len(); return(Vector3<T>(v[0]*f,v[1]*f,v[2]*f)); }
00311
00313 template<typename T>inline T dot(const Vector3<T> &a,const Vector3<T> &b)
00314 { return(a[0]*b[0] + a[1]*b[1] + a[2]*b[2]); }
00315
00317 template<typename T>inline Vector3<T> cross(
00318 const Vector3<T> &a,const Vector3<T> &b)
00319 { return(Vector3<T>(
00320 a[1]*b[2] - a[2]*b[1],
00321 a[2]*b[0] - a[0]*b[2],
00322 a[0]*b[1] - a[1]*b[0])); }
00323
00325 template<typename T>inline T angle(const Vector3<T> &a,const Vector3<T> &b)
00326 { return(acos(dot(a,b)/sqrt(a.len2()*b.len2()))); }
00327
00330 template<typename T>inline bool equal(const Vector3<T> &a,const Vector3<T> &b,
00331 T epsilon)
00332 { return(fabs(a[0]-b[0])<epsilon && fabs(a[1]-b[1])<epsilon &&
00333 fabs(a[2]-b[2])<epsilon); }
00334
00336 template<typename T>inline Vector3<T> LinComb(
00337 const Vector3<T> &va,T ca,const Vector3<T> &vb)
00338 { return(Vector3<T>(
00339 ca*va[0] + vb[0],
00340 ca*va[1] + vb[1],
00341 ca*va[2] + vb[2] )); }
00342
00344 template<typename T>inline Vector3<T> LinComb(
00345 const Vector3<T> &va,T ca,const Vector3<T> &vb,T cb)
00346 { return(Vector3<T>(
00347 ca*va[0] + cb*vb[0],
00348 ca*va[1] + cb*vb[1],
00349 ca*va[2] + cb*vb[2] )); }
00350
00352 template<typename T>inline Vector3<T> LinComb(
00353 const Vector3<T> &va,T ca,const Vector3<T> &vb,T cb,const Vector3<T> &vc)
00354 { return(Vector3<T>(
00355 ca*va[0] + cb*vb[0] + vc[0],
00356 ca*va[1] + cb*vb[1] + vc[1],
00357 ca*va[2] + cb*vb[2] + vc[2] )); }
00358
00360 template<typename T>inline Vector3<T> LinComb(
00361 const Vector3<T> &va,T ca,const Vector3<T> &vb,T cb,
00362 const Vector3<T> &vc,T cc)
00363 { return(Vector3<T>(
00364 ca*va[0] + cb*vb[0] + cc*vc[0],
00365 ca*va[1] + cb*vb[1] + cc*vc[1],
00366 ca*va[2] + cb*vb[2] + cc*vc[2] )); }
00367
00369 template<typename T>inline Vector3<T> average(
00370 const Vector3<T> &a,const Vector3<T> &b)
00371 { return(Vector3<T>(0.5*(a[0]+b[0]),0.5*(a[1]+b[1]),0.5*(a[2]+b[2]))); }
00372
00373 }
00374
00375 #endif