$extrastylesheet
sphere.h
Go to the documentation of this file.
00001 // The libMesh Finite Element Library.
00002 // Copyright (C) 2002-2014 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
00003 
00004 // This library is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser General Public
00006 // License as published by the Free Software Foundation; either
00007 // version 2.1 of the License, or (at your option) any later version.
00008 
00009 // This library is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // Lesser General Public License for more details.
00013 
00014 // You should have received a copy of the GNU Lesser General Public
00015 // License along with this library; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 
00018 
00019 
00020 #ifndef LIBMESH_SPHERE_H
00021 #define LIBMESH_SPHERE_H
00022 
00023 // Local includes
00024 #include "libmesh/surface.h"
00025 #include "libmesh/libmesh.h"
00026 
00027 // C++ includes
00028 #include <cstdlib> // *must* precede <cmath> for proper std:abs() on PGI, Sun Studio CC
00029 #include <cmath>
00030 
00031 namespace libMesh
00032 {
00033 
00034 
00072 class Sphere : public Surface
00073 {
00074 public:
00075 
00079   Sphere ();
00080 
00084   Sphere (const Point& c, const Real r);
00085 
00089   Sphere (const Point&, const Point&, const Point&, const Point&);
00090 
00094   Sphere (const Sphere& other_sphere);
00095 
00099   ~Sphere ();
00100 
00104   void create_from_center_radius (const Point& c, const Real r);
00105 
00110   bool intersects (const Sphere& other_sphere) const;
00111 
00116   Real distance (const Sphere& other_sphere) const;
00117 
00122   bool above_surface (const Point& p) const;
00123 
00128   bool below_surface (const Point& p) const;
00129 
00136   bool on_surface (const Point& p) const;
00137 
00141   Point closest_point (const Point& p) const;
00142 
00147   Point unit_normal (const Point& p) const;
00148 
00152   Real radius() const { return _rad; }
00153 
00157   Real& radius() { return _rad; }
00158 
00162   const Point& center() const { return _cent; }
00163 
00167   Point& center() { return _cent; }
00168 
00173   Point surface_coords (const Point& cart) const;
00174 
00179   Point world_coords (const Point& sph) const;
00180 
00181 
00182 private:
00183 
00184 
00188   Point _cent;
00189 
00193   Real  _rad;
00194 };
00195 
00196 
00197 
00198 // ------------------------------------------------------------
00199 // Sphere inline functions
00200 inline
00201 Point Sphere::surface_coords (const Point& cart) const
00202 {
00203   // constant translation in the origin
00204   const Point c (cart-this->center());
00205 
00206   // phi: special care, so that it gives 0..2pi results
00207   const Real phi = std::atan2(c(1), c(0));
00208 
00209   return Point(/* radius */ c.size(),
00210                /* theta  */ std::atan2( std::sqrt( c(0)*c(0) + c(1)*c(1) ), c(2) ),
00211                /* phi    */ ( (phi < 0)  ?  2.*libMesh::pi+phi  :  phi ) );
00212 }
00213 
00214 
00215 
00216 inline
00217 Point Sphere::world_coords (const Point& sph) const
00218 {
00219   const Real r     = sph(0);
00220   const Real theta = sph(1);
00221   const Real phi   = sph(2);
00222 
00223   // constant translation out of the origin
00224   return Point (/* x */ r*std::sin(theta)*std::cos(phi) + this->center()(0),
00225                 /* y */ r*std::sin(theta)*std::sin(phi) + this->center()(1),
00226                 /* z */ r*std::cos(theta)               + this->center()(2));
00227 }
00228 
00229 
00230 
00231 } // namespace libMesh
00232 
00233 
00234 #endif // LIBMESH_SPHERE_H