Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifndef SUNDANCE_POINT_H
00032 #define SUNDANCE_POINT_H
00033
00034 #include "SundanceDefs.hpp"
00035 #include "SundanceExceptions.hpp"
00036 #include "Teuchos_Utils.hpp"
00037
00038
00039
00040 namespace Sundance
00041 {
00042
00043
00044
00045 class Point
00046 {
00047 public:
00048
00049 inline Point();
00050 inline Point(const double& x);
00051 inline Point(const double& x, const double& y);
00052 inline Point(const double& x, const double& y, const double& z);
00053 inline Point(const Point& other);
00054 inline Point& operator=(const Point& other);
00055
00056 inline int dim() const {return dim_;}
00057 inline double& operator[](int i);
00058 inline const double& operator[](int i) const ;
00059 inline void resize(int i);
00060
00061
00062
00063 inline Point& operator+=(const Point& p) ;
00064 inline Point& operator-=(const Point& p) ;
00065 inline Point& operator*=(const double& a) ;
00066 inline Point& operator/=(const double& a) ;
00067
00068
00069
00070 inline Point operator+() const ;
00071 inline Point operator-() const ;
00072
00073
00074
00075 inline Point operator+(const Point& p) const ;
00076 inline Point operator-(const Point& p) const ;
00077 inline double operator*(const Point& p) const ;
00078
00079
00080
00081 inline Point operator*(const double& a) const ;
00082 inline Point operator/(const double& a) const ;
00083
00084 inline double distance(const Point& x) const ;
00085
00086 inline std::string toString() const ;
00087
00088 static bool unitTest() ;
00089
00090 protected:
00091 void boundsCheck(int i) const ;
00092 int dim_;
00093 double x_[3];
00094 };
00095 }
00096
00097
00098 namespace std
00099 {
00100 ostream& operator<<(std::ostream& os, const Sundance::Point& p);
00101
00102 }
00103
00104 namespace Sundance
00105 {
00106 inline Point operator*(const double& a, const Point& p);
00107
00108 inline Point cross(const Point& x, const Point& y);
00109
00110
00111
00112 inline Point::Point()
00113 : dim_(0)
00114 {;}
00115
00116 inline Point::Point(const double& x)
00117 : dim_(1)
00118 {
00119 x_[0] = x;
00120 }
00121
00122 inline Point::Point(const double& x, const double& y)
00123 : dim_(2)
00124 {
00125 x_[0] = x;
00126 x_[1] = y;
00127 }
00128
00129 inline Point::Point(const double& x, const double& y, const double& z)
00130 : dim_(3)
00131 {
00132 x_[0] = x;
00133 x_[1] = y;
00134 x_[2] = z;
00135 }
00136
00137 inline Point::Point(const Point& other)
00138 : dim_(other.dim_)
00139 {
00140 for (int i=0; i<dim_; i++) x_[i] = other.x_[i];
00141 }
00142
00143 Point& Point::operator=(const Point& other)
00144 {
00145 if (&other==this) return *this;
00146
00147 dim_ = other.dim_;
00148 for (int i=0; i<dim_; i++) x_[i] = other.x_[i];
00149 return *this;
00150 }
00151
00152 double& Point::operator[](int i)
00153 {
00154 #ifndef NOBOUNDSCHECK
00155 boundsCheck(i);
00156 #endif
00157 return x_[i];
00158 }
00159
00160 const double& Point::operator[](int i) const
00161 {
00162 #ifndef NOBOUNDSCHECK
00163 boundsCheck(i);
00164 #endif
00165 return x_[i];
00166 }
00167
00168 void Point::resize(int i)
00169 {
00170 #ifndef NOBOUNDSCHECK
00171 TEST_FOR_EXCEPTION(i < 0 || i>3, RuntimeError,
00172 "void Point::resize() invalid dimension");
00173 #endif
00174 dim_ = i;
00175 }
00176
00177 Point& Point::operator+=(const Point& p)
00178 {
00179 TEST_FOR_EXCEPTION(p.dim() != dim_, RuntimeError,
00180 "Point::operator+=() dimension mismatch "
00181 "operands are: " << *this << " and "
00182 << p );
00183
00184 for (int i=0; i<dim_; i++) x_[i] += p.x_[i];
00185 return *this;
00186 }
00187
00188 Point& Point::operator-=(const Point& p)
00189 {
00190
00191 TEST_FOR_EXCEPTION(p.dim() != dim_, RuntimeError,
00192 "Point::operator-=() dimension mismatch "
00193 "operands are: " << *this << " and "
00194 << p );
00195
00196 for (int i=0; i<dim_; i++) x_[i] -= p.x_[i];
00197 return *this;
00198 }
00199
00200 Point& Point::operator*=(const double& a)
00201 {
00202 for (int i=0; i<dim_; i++) x_[i] *= a;
00203 return *this;
00204 }
00205
00206 Point& Point::operator/=(const double& a)
00207 {
00208 for (int i=0; i<dim_; i++) x_[i] /= a;
00209 return *this;
00210 }
00211
00212 Point Point::operator-() const
00213 {
00214 Point rtn(*this);
00215 for (int i=0; i<dim_; i++) rtn.x_[i] = -rtn.x_[i];
00216 return rtn;
00217 }
00218
00219 Point Point::operator+() const
00220 {
00221 return *this;
00222 }
00223
00224 Point Point::operator+(const Point& p) const
00225 {
00226 Point rtn(*this);
00227 rtn += p;
00228 return rtn;
00229 }
00230
00231 Point Point::operator-(const Point& p) const
00232 {
00233 Point rtn(*this);
00234 rtn -= p;
00235 return rtn;
00236 }
00237
00238 double Point::operator*(const Point& p) const
00239 {
00240 double rtn = 0.0;
00241
00242 TEST_FOR_EXCEPTION(p.dim() != dim_, RuntimeError,
00243 "Point::operator*() dimension mismatch "
00244 "operands are: " << *this << " and "
00245 << p );
00246
00247 for (int i=0; i<dim_; i++) rtn += x_[i]*p.x_[i];
00248 return rtn;
00249 }
00250
00251 Point Point::operator*(const double& a) const
00252 {
00253 Point rtn(*this);
00254 rtn *= a;
00255 return rtn;
00256 }
00257
00258 Point Point::operator/(const double& a) const
00259 {
00260 Point rtn(*this);
00261 rtn /= a;
00262 return rtn;
00263 }
00264
00265 Point operator*(const double& a, const Point& p)
00266 {
00267 return p.operator*(a);
00268 }
00269
00270 inline Point cross(const Point& a, const Point& b)
00271 {
00272 return Point( a[1]*b[2] - b[1]*a[2],
00273 -a[0]*b[2] + a[2]*b[0],
00274 a[0]*b[1] - a[1]*b[0]);
00275 }
00276
00277 inline std::string Point::toString() const
00278 {
00279 std::string rtn = "{";
00280 for (int i=0; i<dim(); i++)
00281 {
00282 rtn += Teuchos::toString(x_[i]);
00283 if (i<dim()-1) rtn += ", ";
00284 }
00285 rtn += "}";
00286 return rtn;
00287 }
00288
00289
00290 inline double Point::distance(const Point& x) const
00291 {
00292 Point dx = *this-x;
00293 return ::sqrt(dx*dx);
00294 }
00295 }
00296
00297 namespace Teuchos
00298 {
00299 inline std::string toString(const Sundance::Point& x)
00300 {
00301 return x.toString();
00302 }
00303 }
00304
00305 namespace std
00306 {
00307 ostream& operator<<(std::ostream& os, const Sundance::Point& p);
00308 }
00309
00310
00311 #endif
00312
00313
00314
00315
00316