libMesh
Loading...
Searching...
No Matches
sphere.h
Go to the documentation of this file.
1// The libMesh Finite Element Library.
2// Copyright (C) 2002-2026 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
31namespace libMesh
32{
33
72class Sphere : public Surface
73{
74public:
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
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
183private:
184
189
194};
195
196
197
198// ------------------------------------------------------------
199// Sphere inline functions
200inline
202{
203#if LIBMESH_DIM > 2
204 // constant translation in the origin
205 const Point c (cart-this->center());
206
207 // phi: special care, so that it gives 0..2pi results
208 const Real phi = std::atan2(c(1), c(0));
209
210 return Point(/* radius */ c.norm(),
211 /* theta */ std::atan2( std::sqrt( c(0)*c(0) + c(1)*c(1) ), c(2) ),
212 /* phi */ ( (phi < 0) ? 2.*libMesh::pi+phi : phi ) );
213#else
214 libmesh_ignore(cart);
215 libmesh_not_implemented();
216#endif
217}
218
219
220
221inline
223{
224 const Real r = sph(0);
225 const Real theta = sph(1);
226 const Real phi = sph(2);
227
228 // constant translation out of the origin
229 return Point (/* x */ r*std::sin(theta)*std::cos(phi) + this->center()(0),
230 /* y */ r*std::sin(theta)*std::sin(phi) + this->center()(1),
231 /* z */ r*std::cos(theta) + this->center()(2));
232}
233
234
235
236} // namespace libMesh
237
238
239#endif // LIBMESH_SPHERE_H
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition point.h:40
This class defines a sphere.
Definition sphere.h:73
Point & center()
Definition sphere.h:168
virtual Point closest_point(const Point &p) const override
Definition sphere.C:184
Real _rad
The radius of the sphere.
Definition sphere.h:193
Real & radius()
Definition sphere.h:158
virtual Point world_coords(const Point &sph) const override
Definition sphere.h:222
const Point & center() const
Definition sphere.h:163
Real radius() const
Definition sphere.h:153
void create_from_center_radius(const Point &c, const Real r)
Defines a sphere of radius r centered at c.
Definition sphere.C:114
bool intersects(const Sphere &other_sphere) const
Definition sphere.C:124
Point _cent
The center of the sphere.
Definition sphere.h:188
virtual bool below_surface(const Point &p) const override
Definition sphere.C:158
virtual bool on_surface(const Point &p) const override
Definition sphere.C:167
virtual Point surface_coords(const Point &cart) const override
Definition sphere.h:201
Sphere()
Dummy Constructor.
Definition sphere.C:36
Real distance(const Sphere &other_sphere) const
Definition sphere.C:131
virtual bool above_surface(const Point &p) const override
Definition sphere.C:143
~Sphere()
Destructor.
virtual Point unit_normal(const Point &p) const override
Definition sphere.C:201
The base class for all "surface" related geometric objects.
Definition surface.h:43
auto norm() const
The libMesh namespace provides an interface to certain functionality in the library.
void libmesh_ignore(const Args &...)
const Real pi
.
Definition libmesh.h:292
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real