sphere.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2014 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 
20 #ifndef LIBMESH_SPHERE_H
21 #define LIBMESH_SPHERE_H
22 
23 // Local includes
24 #include "libmesh/surface.h"
25 #include "libmesh/libmesh.h"
26 
27 // C++ includes
28 #include <cstdlib> // *must* precede <cmath> for proper std:abs() on PGI, Sun Studio CC
29 #include <cmath>
30 
31 namespace libMesh
32 {
33 
34 
73 // ------------------------------------------------------------
74 // Sphere class definition
75 class Sphere : public Surface
76 {
77 public:
78 
82  Sphere ();
83 
87  Sphere (const Point& c, const Real r);
88 
92  Sphere (const Point&, const Point&, const Point&, const Point&);
93 
97  Sphere (const Sphere& other_sphere);
98 
102  ~Sphere ();
103 
107  void create_from_center_radius (const Point& c, const Real r);
108 
113  bool intersects (const Sphere& other_sphere) const;
114 
119  Real distance (const Sphere& other_sphere) const;
120 
125  bool above_surface (const Point& p) const;
126 
131  bool below_surface (const Point& p) const;
132 
139  bool on_surface (const Point& p) const;
140 
144  Point closest_point (const Point& p) const;
145 
150  Point unit_normal (const Point& p) const;
151 
155  Real radius() const { return _rad; }
156 
160  Real& radius() { return _rad; }
161 
165  const Point& center() const { return _cent; }
166 
170  Point& center() { return _cent; }
171 
176  Point surface_coords (const Point& cart) const;
177 
182  Point world_coords (const Point& sph) const;
183 
184 
185 private:
186 
187 
192 
197 };
198 
199 
200 
201 // ------------------------------------------------------------
202 // Sphere inline functions
203 inline
204 Point Sphere::surface_coords (const Point& cart) const
205 {
206  // constant translation in the origin
207  const Point c (cart-this->center());
208 
209  // phi: special care, so that it gives 0..2pi results
210  const Real phi = std::atan2(c(1), c(0));
211 
212  return Point(/* radius */ c.size(),
213  /* theta */ std::atan2( std::sqrt( c(0)*c(0) + c(1)*c(1) ), c(2) ),
214  /* phi */ ( (phi < 0) ? 2.*libMesh::pi+phi : phi ) );
215 }
216 
217 
218 
219 inline
220 Point Sphere::world_coords (const Point& sph) const
221 {
222  const Real r = sph(0);
223  const Real theta = sph(1);
224  const Real phi = sph(2);
225 
226  // constant translation out of the origin
227  return Point (/* x */ r*std::sin(theta)*std::cos(phi) + this->center()(0),
228  /* y */ r*std::sin(theta)*std::sin(phi) + this->center()(1),
229  /* z */ r*std::cos(theta) + this->center()(2));
230 }
231 
232 
233 
234 } // namespace libMesh
235 
236 
237 #endif // LIBMESH_SPHERE_H

Site Created By: libMesh Developers
Last modified: February 07 2014 16:57:07 UTC

Hosted By:
SourceForge.net Logo