HG-MD  1
Vector.h
Go to the documentation of this file.
00001 
00003 
00004 
00005 
00006 #ifndef VECTOR_H
00007 #define VECTOR_H
00008 
00009 #include <cmath>
00010 #include <sstream>
00011 
00012 class Vec3D
00013 {
00014 public:
00015 
00016         Mdouble X, Y, Z;
00017 
00018         inline Vec3D(void){}
00019 
00020         inline Vec3D (const Mdouble x, const Mdouble y, const Mdouble z)
00021         {       
00022                 X = x; Y = y; Z = z;
00023         }
00024 
00025         inline void set_zero ()
00026         {       
00027                 X = 0.0; Y = 0.0; Z = 0.0;
00028         }
00029 
00030         inline Vec3D operator + (const Vec3D &A) const
00031         { 
00032                 return Vec3D(X + A.X, Y + A.Y, Z + A.Z); 
00033         }
00034 
00035         inline Vec3D operator - (const Vec3D &A) const
00036         {
00037                 return Vec3D(X - A.X, Y - A.Y, Z - A.Z);
00038         }
00039 
00040         inline Vec3D operator + (const Mdouble A) const
00041         { 
00042                 return Vec3D(X + A, Y + A, Z + A); 
00043         }
00044 
00045         inline Vec3D operator - (const Mdouble A) const
00046         { 
00047                 return Vec3D(X - A, Y - A, Z - A); 
00048         }
00049 
00050         inline Vec3D operator * (const Mdouble A) const
00051         { 
00052                 return Vec3D(X * A, Y * A, Z * A); 
00053         }
00054 
00055         inline Vec3D operator / (const Mdouble A) const
00056         { 
00057                 return Vec3D(X / A, Y / A, Z / A); 
00058         }
00059 
00060         inline Vec3D& operator+=(const Vec3D &A)
00061         {
00062                 X += A.X;
00063                 Y += A.Y;
00064                 Z += A.Z;
00065                 return *this;
00066         }
00067 
00068         inline Vec3D& operator-=(const Vec3D &A)
00069         {
00070                 X -= A.X;
00071                 Y -= A.Y;
00072                 Z -= A.Z;
00073                 return *this;
00074         }
00075 
00076         inline Vec3D& operator*=(const Mdouble a)
00077         {
00078                 X *= a;
00079                 Y *= a;
00080                 Z *= a;
00081                 return *this;
00082         }
00083 
00084         inline Vec3D& operator/=(const Mdouble a)
00085         {
00086                 X /= a;
00087                 Y /= a;
00088                 Z /= a;
00089                 return *this;
00090         }
00091 
00092         // Dot product
00093         friend inline Mdouble Dot(const Vec3D &A, const Vec3D &B)
00094         {
00095                 return A.X * B.X + A.Y * B.Y + A.Z * B.Z;
00096         }
00097 
00098         // Piecewise max
00099         friend inline Vec3D max(const Vec3D &A, const Vec3D &B)
00100         {
00101                 return Vec3D(std::max(A.X,B.X), std::max(A.Y,B.Y), std::max(A.Z,B.Z));
00102         }
00103 
00104         // Piecewise min
00105         friend inline Vec3D min(const Vec3D &A, const Vec3D &B)
00106         {
00107                 return Vec3D(std::min(A.X,B.X), std::min(A.Y,B.Y), std::min(A.Z,B.Z));
00108         }
00109 
00110         // Pointwise square 
00111         friend inline Vec3D square(const Vec3D &A)
00112         {
00113                 return Vec3D(A.X * A.X, A.Y * A.Y, A.Z * A.Z);
00114         }
00115 
00116         // make this vector unit length 
00117         inline void normalize() {
00118                 *this  /= this->GetLength();
00119         }
00120 
00121         // make this vector unit length 
00122         inline void SetLength(Mdouble length) {
00123                 *this  /= this->GetLength()*length;
00124         }
00125 
00126         // Pointwise square root
00127         friend inline Vec3D sqrt(const Vec3D &A)
00128         {
00129                 return Vec3D(sqrt(A.X), sqrt(A.Y), sqrt(A.Z));
00130         }
00131 
00132         // Cross product
00133         friend inline Vec3D Cross(const Vec3D &A, const Vec3D &B)
00134         {
00135                 return Vec3D(A.Y*B.Z-A.Z*B.Y, A.Z*B.X-A.X*B.Z, A.X*B.Y-A.Y*B.X);
00136         }
00137 
00138         friend inline Mdouble GetDistance(const Vec3D &A, const Vec3D &B)
00139         {
00140                 return sqrt(GetDistance2(A, B));
00141         }
00142 
00143         friend inline Mdouble GetDistance2(const Vec3D &A, const Vec3D &B)
00144         {
00145                 return ((A.X - B.X) * (A.X - B.X) + (A.Y - B.Y) * (A.Y - B.Y) + (A.Z - B.Z) * (A.Z - B.Z));
00146         }
00147         
00148         friend Mdouble GetLength2(const Vec3D &A)
00149         {
00150                 return (A.X * A.X + A.Y * A.Y + A.Z * A.Z);
00151         }
00152 
00153         Mdouble GetLength2() const
00154         {
00155                 return (X * X + Y * Y + Z * Z);
00156         }
00157 
00158         inline Mdouble GetLength() const
00159         {
00160                 return sqrt(GetLength2());
00161         }
00162 
00163         friend inline Mdouble GetLength(const Vec3D &A)
00164         {
00165                 return A.GetLength();
00166         }
00167         
00168         friend inline Vec3D GetUnitVector(const Vec3D &A)
00169         {
00170                 Mdouble Length2 = A.GetLength2();
00171                 if (Length2) return A/sqrt(Length2);
00172                 else return Vec3D(0,0,0);
00173         }
00174         
00175         friend inline std::ostream& operator<<(std::ostream& os, const Vec3D &A)
00176         {
00177                 os << A.X << ' ' << A.Y << ' ' << A.Z;
00178                 return os;
00179         }
00180         
00181         friend inline std::istream& operator>>(std::istream& is, Vec3D &A)
00182         {
00183                 is >> A.X >> A.Y >> A.Z;
00184                 return is;
00185         }
00186         
00187         friend inline Vec3D operator+ (const Mdouble& a, const Vec3D &A) {return Vec3D(A.X + a, A.Y + a, A.Z + a);} 
00188         friend inline Vec3D operator- (const Mdouble& a, const Vec3D &A) {return Vec3D(A.X - a, A.Y - a, A.Z - a);} 
00189         friend inline Vec3D operator- (const Vec3D &A) {return Vec3D(-A.X, -A.Y, -A.Z);} 
00190         friend inline Vec3D operator* (const Mdouble& a, const Vec3D &A) {return Vec3D(A.X * a, A.Y * a, A.Z * a);}     
00191         
00192 };
00193 
00194 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines