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