SundancePoint.hpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 // ************************************************************************
00003 // 
00004 //                              Sundance
00005 //                 Copyright (2005) Sandia Corporation
00006 // 
00007 // Copyright (year first published) Sandia Corporation.  Under the terms 
00008 // of Contract DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government 
00009 // retains certain rights in this software.
00010 // 
00011 // This library is free software; you can redistribute it and/or modify
00012 // it under the terms of the GNU Lesser General Public License as
00013 // published by the Free Software Foundation; either version 2.1 of the
00014 // License, or (at your option) any later version.
00015 //  
00016 // This library is distributed in the hope that it will be useful, but
00017 // WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019 // Lesser General Public License for more details.
00020 //                                                                                 
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License along with this library; if not, write to the Free Software
00023 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00024 // USA                                                                                
00025 // Questions? Contact Kevin Long (krlong@sandia.gov), 
00026 // Sandia National Laboratories, Livermore, California, USA
00027 // 
00028 // ************************************************************************
00029 /* @HEADER@ */
00030 
00031 #ifndef SUNDANCE_POINT_H
00032 #define SUNDANCE_POINT_H
00033 
00034 #include "SundanceDefs.hpp"
00035 #include "PlayaExceptions.hpp"
00036 #include "SundanceOut.hpp"
00037 #include "Teuchos_Utils.hpp"
00038 
00039 
00040 
00041 namespace Sundance
00042 {
00043   /**
00044    * Point represents a spatial point.
00045    */
00046   class Point
00047     {
00048     public:
00049       /* empty ctor */
00050       inline Point();
00051       inline Point(const double& x);
00052       inline Point(const double& x, const double& y);
00053       inline Point(const double& x, const double& y, const double& z);
00054       inline Point(const Point& other);
00055       inline Point& operator=(const Point& other);
00056 
00057       inline int dim() const {return dim_;}
00058       inline double& operator[](int i);
00059       inline const double& operator[](int i) const ;
00060       inline void resize(int i);
00061 
00062       /* reflexive arithmetic operators */
00063 
00064       inline Point& operator+=(const Point& p) ;
00065       inline Point& operator-=(const Point& p) ;
00066       inline Point& operator*=(const double& a) ;
00067       inline Point& operator/=(const double& a) ;
00068 
00069       /* unary plus and minus */
00070 
00071       inline Point operator+() const ;
00072       inline Point operator-() const ;
00073 
00074       /* binary operations with Points */
00075 
00076       inline Point operator+(const Point& p) const ;
00077       inline Point operator-(const Point& p) const ;
00078       inline double operator*(const Point& p) const ;
00079 
00080       /* binary operations with doubles */
00081 
00082       inline Point operator*(const double& a) const ;
00083       inline Point operator/(const double& a) const ;
00084 
00085       inline double distance(const Point& x) const ;
00086 
00087       inline std::string toString() const ;
00088 
00089       static bool unitTest() ;
00090 
00091     protected:
00092       void boundsCheck(int i) const ;
00093       int dim_;
00094       double x_[3];
00095     };
00096 }
00097 
00098 
00099 namespace std
00100 {
00101   ostream& operator<<(std::ostream& os, const Sundance::Point& p);
00102 
00103 }
00104 
00105 namespace Sundance
00106 {
00107   inline Point operator*(const double& a, const Point& p);
00108 
00109   inline Point cross(const Point& x, const Point& y);
00110 
00111 
00112 
00113   inline Point::Point()
00114     : dim_(0)
00115     {;}
00116 
00117   inline Point::Point(const double& x)
00118     : dim_(1)
00119     {
00120       x_[0] = x;
00121     }
00122 
00123   inline Point::Point(const double& x, const double& y)
00124     : dim_(2)
00125     {
00126       x_[0] = x;
00127       x_[1] = y;
00128     }
00129 
00130   inline Point::Point(const double& x, const double& y, const double& z)
00131     : dim_(3)
00132     {
00133       x_[0] = x;
00134       x_[1] = y;
00135       x_[2] = z;
00136     }
00137 
00138   inline Point::Point(const Point& other)
00139     : dim_(other.dim_)
00140     {
00141       for (int i=0; i<dim_; i++) x_[i] = other.x_[i];
00142     }
00143 
00144   Point& Point::operator=(const Point& other)
00145     {
00146       if (&other==this) return *this;
00147 
00148       dim_ = other.dim_;
00149       for (int i=0; i<dim_; i++) x_[i] = other.x_[i];
00150       return *this;
00151     }
00152 
00153   double& Point::operator[](int i)
00154     {
00155 #ifndef NOBOUNDSCHECK
00156       boundsCheck(i);
00157 #endif
00158       return x_[i];
00159     }
00160 
00161   const double& Point::operator[](int i) const
00162     {
00163 #ifndef NOBOUNDSCHECK
00164       boundsCheck(i);
00165 #endif
00166       return x_[i];
00167     }
00168 
00169   void Point::resize(int i)
00170     {
00171 #ifndef NOBOUNDSCHECK
00172       TEUCHOS_TEST_FOR_EXCEPTION(i < 0 || i>3, std::runtime_error,
00173                          "void Point::resize() invalid dimension");
00174 #endif
00175       dim_ = i;
00176     }
00177 
00178   Point& Point::operator+=(const Point& p)
00179     {
00180       TEUCHOS_TEST_FOR_EXCEPTION(p.dim() != dim_, std::runtime_error,
00181                          "Point::operator+=() dimension mismatch "
00182                          "operands are: " << *this << " and " 
00183                          << p );
00184 
00185       for (int i=0; i<dim_; i++) x_[i] += p.x_[i];
00186       return *this;
00187     }
00188 
00189   Point& Point::operator-=(const Point& p)
00190     {
00191 
00192       TEUCHOS_TEST_FOR_EXCEPTION(p.dim() != dim_, std::runtime_error,
00193                          "Point::operator-=() dimension mismatch "
00194                          "operands are: " << *this << " and " 
00195                          << p );
00196       
00197       for (int i=0; i<dim_; i++) x_[i] -= p.x_[i];
00198       return *this;
00199     }
00200 
00201   Point& Point::operator*=(const double& a)
00202     {
00203       for (int i=0; i<dim_; i++) x_[i] *= a;
00204       return *this;
00205     }
00206 
00207   Point& Point::operator/=(const double& a)
00208     {
00209       for (int i=0; i<dim_; i++) x_[i] /= a;
00210       return *this;
00211     }
00212 
00213   Point Point::operator-() const
00214     {
00215       Point rtn(*this);
00216       for (int i=0; i<dim_; i++) rtn.x_[i] = -rtn.x_[i];
00217       return rtn;
00218     }
00219 
00220   Point Point::operator+() const
00221     {
00222       return *this;
00223     }
00224 
00225   Point Point::operator+(const Point& p) const
00226     {
00227       Point rtn(*this);
00228       rtn += p;
00229       return rtn;
00230     }
00231 
00232   Point Point::operator-(const Point& p) const
00233     {
00234       Point rtn(*this);
00235       rtn -= p;
00236       return rtn;
00237     }
00238 
00239   double Point::operator*(const Point& p) const
00240     {
00241       double rtn = 0.0;
00242 
00243       TEUCHOS_TEST_FOR_EXCEPTION(p.dim() != dim_, std::runtime_error,
00244                          "Point::operator*() dimension mismatch "
00245                          "operands are: " << *this << " and " 
00246                          << p );
00247       
00248       for (int i=0; i<dim_; i++) rtn += x_[i]*p.x_[i];
00249       return rtn;
00250     }
00251 
00252   Point Point::operator*(const double& a) const
00253     {
00254       Point rtn(*this);
00255       rtn *= a;
00256       return rtn;
00257     }
00258 
00259   Point Point::operator/(const double& a) const
00260     {
00261       Point rtn(*this);
00262       rtn /= a;
00263       return rtn;
00264     }
00265 
00266   Point operator*(const double& a, const Point& p)
00267     {
00268       return p.operator*(a);
00269     }
00270 
00271   inline Point cross(const Point& a, const Point& b)
00272   {
00273     return Point( a[1]*b[2] - b[1]*a[2], 
00274                   -a[0]*b[2] + a[2]*b[0],
00275                   a[0]*b[1] - a[1]*b[0]);
00276   }
00277 
00278   inline std::string Point::toString() const
00279     {
00280       std::string rtn = "{";
00281       for (int i=0; i<dim(); i++)
00282         {
00283           rtn += Teuchos::toString(x_[i]);
00284           if (i<dim()-1) rtn += ", ";
00285         }
00286       rtn += "}";
00287       return rtn;
00288     }
00289 
00290 
00291   inline double Point::distance(const Point& x) const 
00292   {
00293     Point dx = *this-x;
00294     return ::sqrt(dx*dx); 
00295   }
00296 }
00297 
00298 namespace Teuchos
00299 {
00300   inline std::string toString(const Sundance::Point& x)
00301     {
00302       return x.toString();
00303     }
00304 }
00305 
00306 namespace std
00307 {
00308   ostream& operator<<(std::ostream& os, const Sundance::Point& p);
00309 }
00310 
00311 
00312 #endif
00313 
00314 
00315 
00316 
00317 

Site Contact