sphere.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2018 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 override;
123 
128  virtual bool below_surface (const Point & p) const override;
129 
137  virtual bool on_surface (const Point & p) const override;
138 
142  virtual Point closest_point (const Point & p) const override;
143 
148  virtual Point unit_normal (const Point & p) const 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 override;
175 
180  virtual Point world_coords (const Point & sph) const 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
Definition: sphere.h:188
virtual bool above_surface(const Point &p) const override
Definition: sphere.C:140
A geometric object representing a sphere.
Definition: sphere.h:72
Real norm() const
Definition: type_vector.h:912
virtual Point surface_coords(const Point &cart) const override
Definition: sphere.h:201
virtual Point closest_point(const Point &p) const override
Definition: sphere.C:181
const Point & center() const
Definition: sphere.h:163
virtual bool on_surface(const Point &p) const override
Definition: sphere.C:164
Real distance(const Sphere &other_sphere) const
Definition: sphere.C:128
Real & radius()
Definition: sphere.h:158
virtual Point unit_normal(const Point &p) const override
Definition: sphere.C:198
Point & center()
Definition: sphere.h:168
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
void create_from_center_radius(const Point &c, const Real r)
Definition: sphere.C:111
virtual Point world_coords(const Point &sph) const override
Definition: sphere.h:217
Base class for Plane and Sphere classes.
Definition: surface.h:42
Real radius() const
Definition: sphere.h:153
A geometric point in (x,y,z) space.
Definition: point.h:38
virtual bool below_surface(const Point &p) const override
Definition: sphere.C:155
const Real pi
Definition: libmesh.h:233
bool intersects(const Sphere &other_sphere) const
Definition: sphere.C:121