SundancePoint.hpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 // ************************************************************************
00003 // 
00004 //                             Sundance
00005 //                 Copyright 2011 Sandia Corporation
00006 // 
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Kevin Long (kevin.long@ttu.edu)
00038 // 
00039 
00040 /* @HEADER@ */
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    * Point represents a spatial point.
00056    */
00057   class Point
00058     {
00059     public:
00060       /* empty ctor */
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       /* reflexive arithmetic operators */
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       /* unary plus and minus */
00081 
00082       inline Point operator+() const ;
00083       inline Point operator-() const ;
00084 
00085       /* binary operations with Points */
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       /* binary operations with doubles */
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 

Site Contact