vector2D.h

Go to the documentation of this file.
00001 
00015 #ifndef _DLR_VECTOR2D_H_
00016 #define _DLR_VECTOR2D_H_
00017 
00018 #include <iostream>
00019 #include <dlrCommon/exception.h>
00020 
00021 namespace dlr {
00022 
00023   namespace numeric {
00024     
00028     class Vector2D {
00029     public:
00030 
00034       Vector2D()
00035         : m_x(0), m_y(0) {}
00036     
00037 
00045       Vector2D(double xCoord, double yCoord)
00046         : m_x(xCoord), m_y(yCoord) {}
00047 
00048 
00059       Vector2D(double xCoord, double yCoord, double alpha)
00060         : m_x(xCoord), m_y(yCoord) {this->normalize(alpha);}
00061 
00062 
00069       Vector2D(const Vector2D& vec)
00070         : m_x(vec.m_x), m_y(vec.m_y) {}
00071 
00072 
00076       ~Vector2D() {}
00077 
00078 
00084       inline Vector2D& clear() {
00085         m_x = 0.0; m_y = 0.0;
00086         return *this;
00087       }
00088 
00089       
00099       inline void setValue(double xCoord, double yCoord) {
00100         m_x = xCoord; m_y = yCoord;
00101       }
00102 
00103 
00114       inline void setValue(double xCoord, double yCoord, double alpha) {
00115         m_x = xCoord; m_y = yCoord; normalize(alpha);
00116       }
00117 
00118 
00125       inline double& x() {return m_x;}
00126 
00127 
00134       inline double x() const {return m_x;}
00135 
00136     
00143       inline double& y() {return m_y;}
00144 
00145     
00152       inline double y() const {return m_y;}
00153     
00154 
00164       Vector2D& operator=(const Vector2D& vec) {
00165         setValue(vec.m_x, vec.m_y); return *this; // Self-assignment OK.
00166       }
00167 
00168 
00178       inline double& operator[](size_t index) {
00179         if(index == 0) {return this->x();}
00180         return this->y();
00181       }
00182 
00183           
00193       inline double operator[](size_t index) const {
00194         if(index == 0) {return this->x();}
00195         return this->y();
00196       }
00197           
00207       Vector2D& operator*=(double scalar) {
00208         m_x *= scalar; m_y *= scalar; return *this;
00209       }
00210 
00211 
00221       Vector2D& operator/=(double scalar) {
00222         if (scalar == 0.0) {
00223           DLR_THROW(ValueException, "Vector2D::operator/=()",
00224                     "Bad scalar: Divide by Zero\n");
00225         }
00226         m_x /= scalar; m_y /= scalar; return *this;
00227       }
00228 
00229 
00239       Vector2D& operator+=(const Vector2D& vec) {
00240         m_x += vec.m_x; m_y += vec.m_y; return *this;
00241       }
00242 
00243 
00253       Vector2D& operator-=(const Vector2D& vec) {
00254         m_x -= vec.m_x; m_y -= vec.m_y; return *this;
00255       }
00256 
00257 
00264       Vector2D operator-() {
00265         return Vector2D(-m_x, -m_y);
00266       }
00267     
00268     private:
00269       // Private member functions.
00270       inline void normalize(double alpha) {
00271         if(alpha == 1.0) {return;}
00272         if(alpha == 0.0) {
00273           DLR_THROW(ValueException, "Vector2D::normalize()",
00274                     "Bad alpha (0.0).");
00275         }
00276         m_x /= alpha; m_y /= alpha;
00277         return;
00278       }
00279 
00280       // Private data members.
00281       double m_x;
00282       double m_y;
00283     }; // class Vector2D
00284 
00285 
00286     /* ============== Non-member function declarations ============== */
00287   
00298     Vector2D
00299     operator+(const Vector2D& vector0, const Vector2D& vector1);
00300 
00313     Vector2D
00314     operator-(const Vector2D& vector0, const Vector2D& vector1);
00315   
00326     Vector2D
00327     operator*(const Vector2D& vector0, const Vector2D& vector1);
00328 
00340     Vector2D
00341     operator/(const Vector2D& vector0, const Vector2D& vector1);
00342 
00354     Vector2D operator+(const Vector2D& vector0, double scalar0);
00355 
00367     Vector2D operator-(const Vector2D& vector0, double scalar0);
00368 
00380     Vector2D operator*(const Vector2D& vector0, double scalar0);
00381 
00393     Vector2D operator/(const Vector2D& vector0, double scalar0);
00394 
00402     bool operator==(const Vector2D& vector0, const Vector2D& vector1);
00403 
00412     bool operator!=(const Vector2D& vector0, const Vector2D& vector1);
00413 
00414 
00427     Vector2D operator+(double scalar0, const Vector2D& vector0);
00428 
00429 
00442     Vector2D operator*(double scalar0, const Vector2D& vector0);
00443 
00444   
00460     std::ostream& operator<<(std::ostream& stream, const Vector2D& vector0);
00461 
00476     std::istream& operator>>(std::istream& stream, Vector2D& vector0);
00477 
00478   } // namespace numeric
00479 
00480 } // namespace dlr
00481 
00482 
00483 /* ======= Declarations to maintain compatibility with legacy code. ======= */
00484 
00485 namespace dlr {
00486 
00487   using numeric::Vector2D;
00488 
00489 } // namespace dlr
00490 
00491 
00492 /* ============ Definitions of inline & template functions ============ */
00493 
00494 namespace dlr {
00495 
00496   namespace numeric {
00497     
00498     inline Vector2D operator+(double scalar0, const Vector2D& vector0)
00499     {
00500       return vector0 + scalar0;
00501     }
00502   
00503     inline Vector2D operator*(double scalar0, const Vector2D& vector0)
00504     {
00505       return vector0 * scalar0;
00506     }
00507 
00508   } // namespace numeric
00509 
00510 } // namespace dlr
00511 
00512 #endif // #ifndef _DLR_VECTOR2D_H_

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