Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Examples  

Point.h

Go to the documentation of this file.
00001 #ifndef POINT_H
00002 #define POINT_H
00003 
00004 #include "SundanceDefs.h"
00005 
00006 #include "TSFUtils.h"
00007 #include "TSFError.h"
00008 #include <string>
00009 
00010 
00011 namespace Sundance
00012 {
00013 
00014   using namespace TSF;
00015   using std::string;
00016 
00017   using std::ostream;
00018 
00019   /**
00020    * \ingroup UserLevelGeometry
00021    * Point represents a spatial point.
00022    */
00023   class Point
00024     {
00025     public:
00026       /* empty ctor */
00027       inline Point();
00028       inline Point(const double& x);
00029       inline Point(const double& x, const double& y);
00030       inline Point(const double& x, const double& y, const double& z);
00031       inline Point(const Point& other);
00032       inline Point& operator=(const Point& other);
00033 
00034       inline int dim() const {return dim_;}
00035       inline double& operator[](int i);
00036       inline const double& operator[](int i) const ;
00037       inline void resize(int i);
00038 
00039       /* reflexive arithmetic operators */
00040 
00041       inline Point& operator+=(const Point& p) ;
00042       inline Point& operator-=(const Point& p) ;
00043       inline Point& operator*=(const double& a) ;
00044       inline Point& operator/=(const double& a) ;
00045 
00046       /* unary plus and minus */
00047 
00048       inline Point operator+() const ;
00049       inline Point operator-() const ;
00050 
00051       /* binary operations with Points */
00052 
00053       inline Point operator+(const Point& p) const ;
00054       inline Point operator-(const Point& p) const ;
00055       inline double operator*(const Point& p) const ;
00056 
00057       /* binary operations with doubles */
00058 
00059       inline Point operator*(const double& a) const ;
00060       inline Point operator/(const double& a) const ;
00061 
00062       inline string toString() const ;
00063 
00064       static bool unitTest() ;
00065 
00066     protected:
00067       void boundsCheck(int i) const ;
00068       int dim_;
00069       double x_[3];
00070     };
00071 
00072   ostream& operator<<(ostream& os, const Point& p);
00073   inline Point operator*(const double& a, const Point& p);
00074 
00075 
00076   inline Point::Point()
00077     : dim_(0)
00078     {;}
00079 
00080   inline Point::Point(const double& x)
00081     : dim_(1)
00082     {
00083       x_[0] = x;
00084     }
00085 
00086   inline Point::Point(const double& x, const double& y)
00087     : dim_(2)
00088     {
00089       x_[0] = x;
00090       x_[1] = y;
00091     }
00092 
00093   inline Point::Point(const double& x, const double& y, const double& z)
00094     : dim_(3)
00095     {
00096       x_[0] = x;
00097       x_[1] = y;
00098       x_[2] = z;
00099     }
00100 
00101   inline Point::Point(const Point& other)
00102     : dim_(other.dim_)
00103     {
00104       for (int i=0; i<dim_; i++) x_[i] = other.x_[i];
00105     }
00106 
00107   Point& Point::operator=(const Point& other)
00108     {
00109       if (&other==this) return *this;
00110 
00111       dim_ = other.dim_;
00112       for (int i=0; i<dim_; i++) x_[i] = other.x_[i];
00113       return *this;
00114     }
00115 
00116   double& Point::operator[](int i)
00117     {
00118 #ifndef NOBOUNDSCHECK
00119       boundsCheck(i);
00120 #endif
00121       return x_[i];
00122     }
00123 
00124   const double& Point::operator[](int i) const
00125     {
00126 #ifndef NOBOUNDSCHECK
00127       boundsCheck(i);
00128 #endif
00129       return x_[i];
00130     }
00131 
00132   void Point::resize(int i)
00133     {
00134 #ifndef NOBOUNDSCHECK
00135       if (i < 0 || i>3) TSFError::raise("Point::resize");
00136 #endif
00137       dim_ = i;
00138     }
00139 
00140   Point& Point::operator+=(const Point& p)
00141     {
00142       if (p.dim() != dim_) TSFError::raise("dim mismatch in Point::operator+=");
00143       for (int i=0; i<dim_; i++) x_[i] += p.x_[i];
00144       return *this;
00145     }
00146 
00147   Point& Point::operator-=(const Point& p)
00148     {
00149       if (p.dim() != dim_) TSFError::raise("dim mismatch in Point::operator-=");
00150       for (int i=0; i<dim_; i++) x_[i] -= p.x_[i];
00151       return *this;
00152     }
00153 
00154   Point& Point::operator*=(const double& a)
00155     {
00156       for (int i=0; i<dim_; i++) x_[i] *= a;
00157       return *this;
00158     }
00159 
00160   Point& Point::operator/=(const double& a)
00161     {
00162       for (int i=0; i<dim_; i++) x_[i] /= a;
00163       return *this;
00164     }
00165 
00166   Point Point::operator-() const
00167     {
00168       Point rtn(*this);
00169       for (int i=0; i<dim_; i++) rtn.x_[i] = -rtn.x_[i];
00170       return rtn;
00171     }
00172 
00173   Point Point::operator+() const
00174     {
00175       return *this;
00176     }
00177 
00178   Point Point::operator+(const Point& p) const
00179     {
00180       Point rtn(*this);
00181       rtn += p;
00182       return rtn;
00183     }
00184 
00185   Point Point::operator-(const Point& p) const
00186     {
00187       Point rtn(*this);
00188       rtn -= p;
00189       return rtn;
00190     }
00191 
00192   double Point::operator*(const Point& p) const
00193     {
00194       double rtn = 0.0;
00195       if (p.dim() != dim())
00196         TSFError::raise("mismatched dimensions in Point::operator*");
00197       for (int i=0; i<dim_; i++) rtn += x_[i]*p.x_[i];
00198       return rtn;
00199     }
00200 
00201   Point Point::operator*(const double& a) const
00202     {
00203       Point rtn(*this);
00204       rtn *= a;
00205       return rtn;
00206     }
00207 
00208   Point Point::operator/(const double& a) const
00209     {
00210       Point rtn(*this);
00211       rtn /= a;
00212       return rtn;
00213     }
00214 
00215   Point operator*(const double& a, const Point& p)
00216     {
00217       return p.operator*(a);
00218     }
00219 
00220   inline string Point::toString() const
00221     {
00222       string rtn = "{";
00223       for (int i=0; i<dim(); i++)
00224         {
00225           rtn += TSF::toString(x_[i]);
00226           if (i<dim()-1) rtn += ", ";
00227         }
00228       rtn += "}";
00229       return rtn;
00230     }
00231 
00232 }
00233 
00234 namespace TSF
00235 {
00236   inline string toString(const Sundance::Point& p)
00237     {
00238       return p.toString();
00239     }
00240 
00241 }
00242 
00243 #endif
00244 
00245 
00246 
00247 
00248 

Contact:
Kevin Long (krlong@ca.sandia.gov)


Documentation generated by