Open PaperOpt
OpenPaperOpt/V3.h
Go to the documentation of this file.
00001 #ifndef V3_H
00002 #define V3_H
00003 
00004 #include "V2.h"
00005 #include <math.h>
00006 #include <iostream>
00007 
00008 #ifndef PI
00009 #define PI 3.1415926535897932384626433832795
00010 #endif
00011 
00017 template <class T>
00018 class V3 {
00019 
00020 private:
00021     T x, y, z;
00022 
00023 public:
00024         V3() : x(0), y(0), z(0) {}
00025         V3(const T _x, const T _y, const T _z) : x(_x), y(_y), z(_z) {}
00026         V3(const V3 <T>& v) : x(v.x), y(v.y), z(v.z) {}
00027         V3(const V2 <T> &v,T _z)      {x=v.X();y=v.Y();z=_z;}
00028 
00029         T X()                   const { return x; }
00030         T Y()                   const { return y; }
00031         T Z()                   const { return z; }
00032         T P(int i) const {
00033                 switch (i) {
00034                         case 0: return x;
00035                         case 1: return y;
00036                         case 2: return z;
00037                         default: return 0;
00038                 }
00039         }
00040         V2 <T> Drop(int i) const {
00041                 switch (i){
00042                         case 0: return V2 <T>(y,z);
00043                         case 1: return V2 <T>(x,z);
00044                         default: return V2 <T>(x,y);
00045                 }
00046         }
00047 
00048         void X(T _x) { x = _x; }
00049         void Y(T _y) { y = _y; }
00050         void Z(T _z) { z = _z; }
00051 
00052         T LengthVector() { return sqrt(x*x + y*y + z*z); }
00053         T LengthVector2() { return (x*x + y*y + z*z); }
00054 
00055         void NormalizeVector() {
00056                 T temp = sqrt(x*x + y*y + z*z);
00057                 if (temp!=0) {
00058                         x /= temp;
00059                         y /= temp;
00060                         z /= temp;
00061                 }               
00062         }
00063         V3 <T>& Normalize() {
00064                 operator*=((T)(1.0/sqrt(LengthVector2())));
00065                 return *this;
00066         }
00067     
00068         T Sum() const {return x+y+z;}
00069     
00070         V3 <T> AbsVec() const {return V3 <T>((T)fabs(x),(T)fabs(y),(T)fabs(z));}
00071         T AbsMax()   const {T dd,d=fabs(x);dd=fabs(y);if (dd>d) d=dd;dd=fabs(z);if (dd>d) d=dd;return d;}
00072         int GetCDrop() const {if (fabs(x)>fabs(y) && fabs(x)>fabs(z)) return 0;if (fabs(y)>fabs(z)) return 1;return 2;}
00073     V3 <T>&   ClipOne()      {if (x>1) x=1;if (y>1) y=1;if (z>1) z=1;return *this;}
00074     V3 <T>&   Rotate90XY()   {T t=x;x=-y;y=t;return *this;}
00075 
00076     void  SetMin(const V3 <T>& v)    {if (v.x<x) x=v.x;if (v.y<y) y=v.y;if (v.z<z) z=v.z;}
00077     void  SetMax(const V3 <T>& v)    {if (v.x>x) x=v.x;if (v.y>y) y=v.y;if (v.z>z) z=v.z;}
00078 
00079         //operator overloaded for different return type---void
00080         void operator+=(const V3 <T>& v) { x += v.x; y +=v.y; z += v.z; }
00081         void operator-=(const V3 <T>& v) { x -= v.x; y -=v.y; z -= v.z; }
00082         void operator*=(const V3 <T>& v) { x *= v.x; y *=v.y; z *= v.z; }
00083         void operator*=(const double k) { x *= k; y *= k; z *= k; }
00084         void operator/=(const V3 <T>& v) { x /= v.x; y /= v.y; z /= v.z; }
00085         void operator/=(T f)     {operator*=((T)(1.0/f));}
00086         
00087         //Negate Operation
00088         void negate() {x = -x;y = -y;z = -z;}
00089         V3 <T> operator-(void) const   {return V3 <T> (-x,-y,-z);}
00090         
00091         //operator overloaded for different return type---V3
00092         V3 <T>operator+(const V3 <T> & b) const { return V3 <T>(x+b.x, y+b.y, z+b.z); }
00093         V3 <T>operator-(const V3 <T> & b) const { return V3 <T>(x-b.x, y-b.y, z-b.z); }
00094         
00095         T operator*(const V3 <T> & b) const { return x*b.x + y*b.y + z*b.z; }
00096         V3 <T> operator*(T f) const { return V3 <T>(x*f, y*f, z*f); }
00097         V3 <T> operator/(T f) const { return (*this)*(1.0/f); }
00098         
00099 
00100         //friend ostream& operator<<(ostream&, const V3 <T>&);
00101 
00102         void CrossProduct(const V3<T>& u, const V3<T>& v) {
00103                 x = u.y * v.z - u.z * v.y;
00104                 y = u.z * v.x - u.x * v.z;
00105         z = u.x * v.y - u.y * v.x;
00106   }
00107 
00108 
00109         T vectMult(const V3 <T> & u) const {
00110                 return x * u.x + y * u.y + z * u.z;
00111         }
00112 
00113         void VectDivConst(const T& v) { x /= v; y /= v; z /= v; }
00114         void VectMulConst(const T& v) { x *= v; y *= v; z *= v; }
00115 };
00116 template <class T>
00117 inline V3 <T> cross(const V3 <T> & a,const V3 <T> & b){
00118         return V3 <T>(a.Y()*b.Z()-a.Z()*b.Y(),a.Z()*b.X()-a.X()*b.Z(),a.X()*b.Y()-a.Y()*b.X());
00119 }
00120 
00121 template <class T>
00122 inline V3 <T> reflect(const V3 <T> & i,const V3 <T> & n){
00123         T s=-(i*n)*2;
00124         return i+(n*s);
00125 }
00126 #endif