libMesh
sphere.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2017 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 
72 class Sphere : public Surface
73 {
74 public:
75 
79  Sphere ();
80 
84  Sphere (const Point & c, const Real r);
85 
89  Sphere (const Point &, const Point &, const Point &, const Point &);
90 
94  Sphere (const Sphere & other_sphere);
95 
99  ~Sphere ();
100 
104  void create_from_center_radius (const Point & c, const Real r);
105 
110  bool intersects (const Sphere & other_sphere) const;
111 
116  Real distance (const Sphere & other_sphere) const;
117 
122  virtual bool above_surface (const Point & p) const libmesh_override;
123 
128  virtual bool below_surface (const Point & p) const libmesh_override;
129 
137  virtual bool on_surface (const Point & p) const libmesh_override;
138 
142  virtual Point closest_point (const Point & p) const libmesh_override;
143 
148  virtual Point unit_normal (const Point & p) const libmesh_override;
149 
153  Real radius() const { return _rad; }
154 
158  Real & radius() { return _rad; }
159 
163  const Point & center() const { return _cent; }
164 
168  Point & center() { return _cent; }
169 
174  virtual Point surface_coords (const Point & cart) const libmesh_override;
175 
180  virtual Point world_coords (const Point & sph) const libmesh_override;
181 
182 
183 private:
184 
189 
194 };
195 
196 
197 
198 // ------------------------------------------------------------
199 // Sphere inline functions
200 inline
201 Point Sphere::surface_coords (const Point & cart) const
202 {
203  // constant translation in the origin
204  const Point c (cart-this->center());
205 
206  // phi: special care, so that it gives 0..2pi results
207  const Real phi = std::atan2(c(1), c(0));
208 
209  return Point(/* radius */ c.norm(),
210  /* theta */ std::atan2( std::sqrt( c(0)*c(0) + c(1)*c(1) ), c(2) ),
211  /* phi */ ( (phi < 0) ? 2.*libMesh::pi+phi : phi ) );
212 }
213 
214 
215 
216 inline
217 Point Sphere::world_coords (const Point & sph) const
218 {
219  const Real r = sph(0);
220  const Real theta = sph(1);
221  const Real phi = sph(2);
222 
223  // constant translation out of the origin
224  return Point (/* x */ r*std::sin(theta)*std::cos(phi) + this->center()(0),
225  /* y */ r*std::sin(theta)*std::sin(phi) + this->center()(1),
226  /* z */ r*std::cos(theta) + this->center()(2));
227 }
228 
229 
230 
231 } // namespace libMesh
232 
233 
234 #endif // LIBMESH_SPHERE_H
Point _cent
The center of the sphere.
Definition: sphere.h:188
This class defines a sphere.
Definition: sphere.h:72
virtual Point surface_coords(const Point &cart) const libmesh_override
Definition: sphere.h:201
Real norm() const
Definition: type_vector.h:909
virtual Point unit_normal(const Point &p) const libmesh_override
Definition: sphere.C:198
Real distance(const Sphere &other_sphere) const
Definition: sphere.C:128
The libMesh namespace provides an interface to certain functionality in the library.
virtual Point closest_point(const Point &p) const libmesh_override
Definition: sphere.C:181
Real radius() const
Definition: sphere.h:153
bool intersects(const Sphere &other_sphere) const
Definition: sphere.C:121
virtual bool below_surface(const Point &p) const libmesh_override
Definition: sphere.C:155
Real & radius()
Definition: sphere.h:158
Sphere()
Dummy Constructor.
Definition: sphere.C:36
Point & center()
Definition: sphere.h:168
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Real _rad
The radius of the sphere.
Definition: sphere.h:193
virtual bool on_surface(const Point &p) const libmesh_override
Definition: sphere.C:164
~Sphere()
Destructor.
Definition: sphere.C:105
void create_from_center_radius(const Point &c, const Real r)
Defines a sphere of radius r centered at c.
Definition: sphere.C:111
The base class for all "surface" related geometric objects.
Definition: surface.h:42
virtual bool above_surface(const Point &p) const libmesh_override
Definition: sphere.C:140
virtual Point world_coords(const Point &sph) const libmesh_override
Definition: sphere.h:217
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition: point.h:38
const Real pi
.
Definition: libmesh.h:172
const Point & center() const
Definition: sphere.h:163