vector3D.h

Go to the documentation of this file.
00001 
00015 #ifndef _DLR_VECTOR3D_H_
00016 #define _DLR_VECTOR3D_H_
00017 
00018 #include <iostream>
00019 #include <dlrCommon/exception.h>
00020 
00021 namespace dlr {
00022 
00023   namespace numeric {
00024     
00028     class Vector3D {
00029     public:
00033       Vector3D()
00034         : m_x(0), m_y(0), m_z(0) {}
00035 
00046       Vector3D(double xCoord, double yCoord, double zCoord)
00047         : m_x(xCoord), m_y(yCoord), m_z(zCoord) {}
00048 
00059       Vector3D(double xCoord, double yCoord, double zCoord, double alpha)
00060         : m_x(xCoord), m_y(yCoord), m_z(zCoord) {this->normalize(alpha);}
00061     
00067       Vector3D(const Vector3D& source)
00068         : m_x(source.m_x), m_y(source.m_y), m_z(source.m_z) {}
00069 
00070 
00074       ~Vector3D() {}
00075 
00076       
00082       inline Vector3D& clear() {
00083         m_x = 0.0; m_y = 0.0; m_z = 0.0;
00084         return *this;
00085       }
00086 
00087       
00095       inline void setValue(double xCoord, double yCoord, double zCoord) {
00096         m_x = xCoord; m_y = yCoord; m_z = zCoord;
00097       }
00098 
00107       inline void setValue(double xCoord, double yCoord, double zCoord,
00108                            double alpha) {
00109         m_x = xCoord; m_y = yCoord; m_z = zCoord; normalize(alpha);
00110       }
00111 
00117       inline double& x() {return m_x;}
00118 
00124       inline double x() const {return m_x;}
00125 
00131       inline double& y() {return m_y;}
00132 
00138       inline double y() const {return m_y;}
00139     
00145       inline double& z() {return m_z;}
00146 
00152       inline double z() const {return m_z;}
00153 
00160       Vector3D& operator=(const Vector3D& source) {
00161         setValue(source.m_x, source.m_y, source.m_z); return *this;
00162       }
00163 
00173       inline double& operator[](size_t index) {
00174         switch(index) {
00175         case 0: return this->x(); break;
00176         case 1: return this->y(); break;
00177         }
00178         return this->z();
00179       }
00180           
00190       inline double operator[](size_t index) const {
00191         switch(index) {
00192         case 0: return this->x(); break;
00193         case 1: return this->y(); break;
00194         }
00195         return this->z();
00196       }
00197           
00204       Vector3D& operator*=(double scalar) {
00205         m_x *= scalar; m_y *= scalar; m_z *= scalar; return *this;
00206       }
00207 
00214       Vector3D& operator/=(double scalar) {
00215         if (scalar == 0) {
00216           DLR_THROW(ValueException, "Vector3D::operator/=(double)",
00217                     "Can't divide by zero.");
00218         }
00219         m_x /= scalar; m_y /= scalar; m_z /= scalar; return *this;
00220       }
00221 
00228       Vector3D& operator+=(const Vector3D& vec) {
00229         m_x += vec.m_x; m_y += vec.m_y; m_z += vec.m_z; return *this;
00230       }
00231 
00238       Vector3D& operator-=(const Vector3D& vec) {
00239         m_x -= vec.m_x; m_y -= vec.m_y; m_z -= vec.m_z; return *this;
00240       }
00241 
00247       Vector3D operator-() {
00248         return Vector3D(-m_x, -m_y, -m_z);
00249       }
00250     
00251     private:
00252       // Private member functions.
00253       inline void normalize(double alpha) {
00254         if(alpha == 1.0) {return;}
00255         if(alpha == 0.0) {
00256           DLR_THROW(ValueException, "Vector3D::normalize()",
00257                     "Bad alpha (0.0).");
00258         }
00259         m_x /= alpha; m_y /= alpha; m_z /= alpha;
00260         return;
00261       }
00262 
00263       // Private data members.
00264       double m_x;
00265       double m_y;
00266       double m_z;
00267     }; // class Vector3D
00268 
00269 
00270     /* ============== Non-member function declarations ============== */
00271   
00282     Vector3D
00283     operator+(const Vector3D& vector0, const Vector3D& vector1);
00284 
00297     Vector3D
00298     operator-(const Vector3D& vector0, const Vector3D& vector1);
00299   
00310     Vector3D
00311     operator*(const Vector3D& vector0, const Vector3D& vector1);
00312 
00324     Vector3D
00325     operator/(const Vector3D& vector0, const Vector3D& vector1);
00326 
00338     Vector3D operator+(const Vector3D& vector0, double scalar0);
00339 
00351     Vector3D operator-(const Vector3D& vector0, double scalar0);
00352 
00364     Vector3D operator*(const Vector3D& vector0, double scalar0);
00365 
00377     Vector3D operator/(const Vector3D& vector0, double scalar0);
00378 
00386     bool operator==(const Vector3D& vector0, const Vector3D& vector1);
00387 
00396     bool operator!=(const Vector3D& vector0, const Vector3D& vector1);
00397 
00398 
00411     Vector3D operator+(double scalar0, const Vector3D& vector0);
00412 
00413 
00426     Vector3D operator*(double scalar0, const Vector3D& vector0);
00427 
00428 
00444     std::ostream& operator<<(std::ostream& stream, const Vector3D& vector0);
00445 
00446 
00461     std::istream& operator>>(std::istream& stream, Vector3D& vector0);
00462 
00463   } // namespace numeric
00464 
00465 } // namespace dlr
00466 
00467 
00468 /* ======= Declarations to maintain compatibility with legacy code. ======= */
00469 
00470 namespace dlr {
00471 
00472   using numeric::Vector3D;
00473 
00474 } // namespace dlr
00475 
00476 /* ============ Definitions of inline & template functions ============ */
00477 
00478 namespace dlr {
00479 
00480   namespace numeric {
00481     
00482     inline Vector3D operator+(double scalar0, const Vector3D& vector0)
00483     {
00484       return vector0 + scalar0;
00485     }
00486   
00487     inline Vector3D operator*(double scalar0, const Vector3D& vector0)
00488     {
00489       return vector0 * scalar0;
00490     }
00491 
00492   } // namespace numeric
00493 
00494 } // namespace dlr
00495 
00496 #endif // #ifndef _DLR_VECTOR3D_H_

Generated on Wed Nov 25 00:42:43 2009 for dlrUtilities Utility Library by  doxygen 1.5.8