vector3.h

00001 #ifndef __VECTOR3D_H__
00002 #define __VECTOR3D_H__
00003 
00004 #include <math.h>
00005 #include <algobase.h>
00006 #include <fstream>
00007 #include <iostream>
00008 #include <vector>
00009 
00010 #define ABS(x) ((x)>0?(x):(-x))
00011 #define RAD2DEG(x) (180*(x)/M_PI)
00012 #define DEG2RAD(x) (M_PI*(x)/180)
00013 #define ALMOST_ZERO 0.0001
00014 
00015 
00016 double normalize(double min, double max, double value);
00017 
00018 namespace Geometry
00019 {
00020         // Useful for simplified, more generic code.
00021         static const unsigned DIM = 3;
00022 
00023         // Messy but needed predeclarations
00024         template <class T> class Vector3D;
00025 //      template <class T> Vector3D<T> operator- (const Vector3D<T>& u, const Vector3D<T>& v);
00026         template <class T> Vector3D<T> operator* (T factor, const Vector3D<T>& v);
00027         template <class T> std::ostream& operator<< (std::ostream& out, const Vector3D<T>& v);
00028         template <class T> std::istream& operator>> (std::istream& in, Vector3D<T>& v);
00029 
00030         // Vector3D class
00031         template <class T>
00032   class Vector3D
00033   {
00034   public:
00035     Vector3D(const Vector3D<T>& v);
00036     explicit Vector3D();
00037     explicit Vector3D(const T& x, const T& y, const T& z);
00038     explicit Vector3D(const T data[3]);
00039     explicit Vector3D(const Vector3D<T>* v);
00040     explicit Vector3D(const std::vector<T>& v);
00041 
00042                 static unsigned dim() { return DIM; }
00043 
00044     void set(const T&x, const T& y, const T& z);
00045     void set(const T data[3]);
00046 
00047     Vector3D<T>& operator=(const Vector3D<T>& src);
00048     bool operator==(const Vector3D<T>& src) const;
00049     bool operator<(const Vector3D<T>& src) const;
00050 
00051     void getSpheric(double& theta, double& phi) const;
00052     void getSpheric(double& distance, double& theta, double& phi) const;
00053     void setSpheric(double distance, double theta, double phi);
00054 
00055     const T& x() const { return _data[0]; };
00056     const T& y() const { return _data[1]; };
00057     const T& z() const { return _data[2]; };
00058     void x(const T& t) { _data[0] = t; };
00059     void y(const T& t) { _data[1] = t; };
00060     void z(const T& t) { _data[2] = t; };
00061 
00062     T& operator[](int index);
00063     const T& operator[](int index) const;
00064     const T* getData() const {return _data;};
00065     T* getDataRef() {return _data;};
00066 
00067     friend std::ostream& operator<< <>(std::ostream& out, const Vector3D<T>& v);
00068     friend std::istream& operator>> <>(std::istream& in, Vector3D<T>& v);
00069 
00070     void normalize();
00071     void setLength(T len);
00072     double module() const { return sqrt(squaredModule()); }
00073     double squaredModule() const;
00074     double angle(const Vector3D<T>& v) const;
00075     double distance(const Vector3D<T>& v) const { return sqrt(squaredDistance(v)); }
00076     double squaredDistance(const Vector3D<T>& v) const;
00077     double infDistance(const Vector3D<T>& v) const;
00078     double manhattanDistance(const Vector3D<T>& v) const;
00079          double distanceToPlane(const Vector3D<T>& P, const Vector3D<T>& N) const;
00080 
00081     void rotateX(double angle);
00082     void rotateY(double angle);
00083     void rotateZ(double angle);
00084                 Vector3D<T> rotation(const Vector3D<T>& axis, double angle) const;
00085 
00086     T operator*(const Vector3D<T>& v) const;
00087     T dotProduct(const Vector3D<T>& v) const { return (*this) * v; }
00088                 T operator*(const std::vector<T>& v) const;
00089 
00090     Vector3D<T> operator/(T factor) const;
00091     const Vector3D<T>& operator/=(T factor);
00092 
00093     Vector3D<T> operator*(T factor) const;
00094     const Vector3D<T>& operator*=(T factor);
00095 //    friend Vector3D<T> operator*(T factor, const Vector3D<T>& v);
00096 
00097                 Vector3D<T> operator^(const Vector3D<T>& v) const;
00098                 Vector3D<T> crossProduct(const Vector3D<T>& v) const{ return (*this)^v; }
00099 
00100     Vector3D<T> operator-(const Vector3D<T>& v) const;
00101                 Vector3D<T> difference(const Vector3D<T>& v) const{ return (*this) - v; }
00102                 const Vector3D<T>& operator-=(const Vector3D<T>& v);
00103                 const Vector3D<T>& substract(const Vector3D<T>& v) { return (*this) -= v; }
00104 
00105                 Vector3D<T> projectX() const;
00106                 Vector3D<T> projectY() const;
00107                 Vector3D<T> projectZ() const;
00108     Vector3D<T> project(const Vector3D<T>& n) const;
00109                 Vector3D<T> intersect(const Vector3D<T>& center, const Vector3D<T>& normal) const;
00110                 Vector3D<T> perpendicular() const;
00111 
00112     Vector3D<T> operator+(const Vector3D<T>& v) const;
00113                 Vector3D<T> sum(const Vector3D<T>& v) const { return (*this) + v; }
00114     const Vector3D<T>& operator+=(const Vector3D<T>& v);
00115                 const Vector3D<T>& add(const Vector3D<T>& v){ return (*this) += v; }
00116 
00117   protected:
00118     T _data[3];
00119   };
00120 
00121         // Needed operator* that I don't know how to move to the cpp
00122         template <class T> Vector3D<T> operator* (T factor, const Vector3D<T>& v)
00123   {
00124     return v * factor;
00125   }
00126 
00127         // Needed operator* that I don't know how to move to the cpp
00128         template <class T> Vector3D<T> operator- (const Vector3D<T>& v)
00129   {
00130     return Vector3D<T>(-v.x(), -v.y(), -v.z());
00131   }
00132 
00133         // Output operator
00134         // Format: "(a.aa, b.bbb, c.c)"
00135         template <class T>
00136   std::ostream& operator<< (std::ostream& out, const Vector3D<T>& v)
00137   {
00138     out  << v._data[0] << " " << v._data[1] << " " << v._data[2];
00139 
00140     return out;
00141   }
00142 
00143         // Input operator
00144         // Format: "a.aa b.bbb c.c"
00145         template <class T>
00146   std::istream& operator>> (std::istream& in, Vector3D<T>& v)
00147   {
00148                 in >> v._data[0] >> v._data[1] >> v._data[2];
00149 
00150     return in;
00151   }
00152 
00153         typedef Vector3D<int> Vector3Di;
00154         typedef Vector3D<float> Vector3Df;
00155         typedef Vector3D<double> Vector3Dd;
00156         typedef Vector3D<short> Vector3Ds;
00157         typedef Vector3D<char> Vector3Dc;
00158 };
00159 
00160 
00161 #endif

Generated on Wed Apr 9 19:22:38 2008 for HalfEdge library by  doxygen 1.5.3