libMesh
Public Member Functions | Private Attributes | List of all members
libMesh::Sphere Class Reference


This class defines a sphere. More...

#include <sphere.h>

Inheritance diagram for libMesh::Sphere:
[legend]

Public Member Functions

 Sphere ()
 Dummy Constructor. More...
 
 Sphere (const Point &c, const Real r)
 Constructs a sphere of radius r centered at c. More...
 
 Sphere (const Point &, const Point &, const Point &, const Point &)
 Constructs a sphere connecting four points. More...
 
 Sphere (const Sphere &other_sphere)
 Copy-constructor. More...
 
 ~Sphere ()
 Destructor. More...
 
void create_from_center_radius (const Point &c, const Real r)
 Defines a sphere of radius r centered at c. More...
 
bool intersects (const Sphere &other_sphere) const
 
Real distance (const Sphere &other_sphere) const
 
virtual bool above_surface (const Point &p) const override
 
virtual bool below_surface (const Point &p) const override
 
virtual bool on_surface (const Point &p) const override
 
virtual Point closest_point (const Point &p) const override
 
virtual Point unit_normal (const Point &p) const override
 
Real radius () const
 
Realradius ()
 
const Pointcenter () const
 
Pointcenter ()
 
virtual Point surface_coords (const Point &cart) const override
 
virtual Point world_coords (const Point &sph) const override
 

Private Attributes

Point _cent
 The center of the sphere. More...
 
Real _rad
 The radius of the sphere. More...
 

Detailed Description


This class defines a sphere.

It also computes coordinate transformations between cartesian \( (x, y, z) \) and spherical \( (r, \theta, \phi) \) coordinates. The spherical coordinates are valid in the ranges:

The coordinates are related as follows: \( \phi \) is the angle in the xy plane starting with 0. from the positive x axis, \( \theta \) is measured against the positive z axis.

*
*        \      | Z
*         \theta|
*          \    |    .
*           \   |   .
*            \  |  .
*             \ | .
*              \|.
* --------------+---------.---------
*              /|\       .          Y
*             /phi\     .
*            /  |  \   .
*           /   |   \ .
*          /.........\
*         /     |
*      X /
* 
Author
Benjamin S. Kirk, Daniel Dreyer
Date
2002-2007 A geometric object representing a sphere.

Definition at line 72 of file sphere.h.

Constructor & Destructor Documentation

◆ Sphere() [1/4]

libMesh::Sphere::Sphere ( )

Dummy Constructor.

Definition at line 36 of file sphere.C.

36  :
37  _rad(-1.)
38 {
39 }
Real _rad
The radius of the sphere.
Definition: sphere.h:193

◆ Sphere() [2/4]

libMesh::Sphere::Sphere ( const Point c,
const Real  r 
)

Constructs a sphere of radius r centered at c.

Definition at line 43 of file sphere.C.

References create_from_center_radius().

45 {
46  libmesh_assert_greater (r, 0.);
47 
48  this->create_from_center_radius (c, r);
49 }
void create_from_center_radius(const Point &c, const Real r)
Defines a sphere of radius r centered at c.
Definition: sphere.C:114

◆ Sphere() [3/4]

libMesh::Sphere::Sphere ( const Point pa,
const Point pb,
const Point pc,
const Point pd 
)

Constructs a sphere connecting four points.

Definition at line 62 of file sphere.C.

References std::abs(), create_from_center_radius(), libMesh::TypeTensor< T >::det(), libMesh::libmesh_ignore(), libMesh::TensorTools::norm(), libMesh::TypeVector< T >::norm_sq(), and libMesh::Real.

66 {
67 #if LIBMESH_DIM > 2
68  Point pad = pa - pd;
69  Point pbd = pb - pd;
70  Point pcd = pc - pd;
71 
72  TensorValue<Real> T(pad,pbd,pcd);
73 
74  Real D = T.det();
75 
76  // The points had better not be coplanar
77  libmesh_assert_greater (std::abs(D), 1e-12);
78 
79  Real e = 0.5*(pa.norm_sq() - pd.norm_sq());
80  Real f = 0.5*(pb.norm_sq() - pd.norm_sq());
81  Real g = 0.5*(pc.norm_sq() - pd.norm_sq());
82 
83  TensorValue<Real> T1(e,pad(1),pad(2),
84  f,pbd(1),pbd(2),
85  g,pcd(1),pcd(2));
86  Real sx = T1.det()/D;
87 
88  TensorValue<Real> T2(pad(0),e,pad(2),
89  pbd(0),f,pbd(2),
90  pcd(0),g,pcd(2));
91  Real sy = T2.det()/D;
92 
93  TensorValue<Real> T3(pad(0),pad(1),e,
94  pbd(0),pbd(1),f,
95  pcd(0),pcd(1),g);
96  Real sz = T3.det()/D;
97 
98  Point c(sx,sy,sz);
99  Real r = (c-pa).norm();
100 
101  this->create_from_center_radius(c,r);
102 #else // LIBMESH_DIM > 2
103  libmesh_ignore(pa, pb, pc, pd);
104  libmesh_not_implemented();
105 #endif
106 }
ADRealEigenVector< T, D, asd > abs(const ADRealEigenVector< T, D, asd > &)
Definition: type_vector.h:57
void libmesh_ignore(const Args &...)
auto norm(const T &a) -> decltype(std::abs(a))
Definition: tensor_tools.h:74
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
void create_from_center_radius(const Point &c, const Real r)
Defines a sphere of radius r centered at c.
Definition: sphere.C:114

◆ Sphere() [4/4]

libMesh::Sphere::Sphere ( const Sphere other_sphere)

Copy-constructor.

Definition at line 53 of file sphere.C.

References center(), create_from_center_radius(), and radius().

53  :
54  Surface()
55 {
56  this->create_from_center_radius (other_sphere.center(),
57  other_sphere.radius());
58 }
Surface()=default
Constructor.
void create_from_center_radius(const Point &c, const Real r)
Defines a sphere of radius r centered at c.
Definition: sphere.C:114

◆ ~Sphere()

libMesh::Sphere::~Sphere ( )
default

Destructor.

Does nothing at the moment.

Member Function Documentation

◆ above_surface()

bool libMesh::Sphere::above_surface ( const Point p) const
overridevirtual
Returns
true if the point p is above the surface, false otherwise.

Implements libMesh::Surface.

Definition at line 143 of file sphere.C.

References center(), libMesh::TypeVector< T >::norm(), and radius().

Referenced by below_surface().

144 {
145  libmesh_assert_greater (this->radius(), 0.);
146 
147  // create a vector from the center to the point.
148  const Point w = p - this->center();
149 
150  if (w.norm() > this->radius())
151  return true;
152 
153  return false;
154 }
const Point & center() const
Definition: sphere.h:163
Real radius() const
Definition: sphere.h:153

◆ below_surface()

bool libMesh::Sphere::below_surface ( const Point p) const
overridevirtual
Returns
true if the point p is below the surface, false otherwise.

Implements libMesh::Surface.

Definition at line 158 of file sphere.C.

References above_surface(), and radius().

159 {
160  libmesh_assert_greater (this->radius(), 0.);
161 
162  return ( !this->above_surface (p) );
163 }
virtual bool above_surface(const Point &p) const override
Definition: sphere.C:143
Real radius() const
Definition: sphere.h:153

◆ center() [1/2]

const Point& libMesh::Sphere::center ( ) const
inline
Returns
The center of the sphere.

Definition at line 163 of file sphere.h.

References _cent.

Referenced by above_surface(), closest_point(), create_from_center_radius(), distance(), on_surface(), Sphere(), surface_coords(), unit_normal(), and world_coords().

163 { return _cent; }
Point _cent
The center of the sphere.
Definition: sphere.h:188

◆ center() [2/2]

Point& libMesh::Sphere::center ( )
inline
Returns
The center of the sphere.

Definition at line 168 of file sphere.h.

References _cent.

168 { return _cent; }
Point _cent
The center of the sphere.
Definition: sphere.h:188

◆ closest_point()

Point libMesh::Sphere::closest_point ( const Point p) const
overridevirtual
Returns
The closest point on the surface to point p.

Implements libMesh::Surface.

Definition at line 184 of file sphere.C.

References center(), radius(), and unit_normal().

185 {
186  libmesh_assert_greater (this->radius(), 0.);
187 
188  // get the normal from the surface in the direction
189  // of p
190  Point normal = this->unit_normal (p);
191 
192  // The closest point on the sphere is in the direction
193  // of the normal a distance r from the center.
194  const Point cp = this->center() + normal*this->radius();
195 
196  return cp;
197 }
const Point & center() const
Definition: sphere.h:163
virtual Point unit_normal(const Point &p) const override
Definition: sphere.C:201
Real radius() const
Definition: sphere.h:153

◆ create_from_center_radius()

void libMesh::Sphere::create_from_center_radius ( const Point c,
const Real  r 
)

Defines a sphere of radius r centered at c.

Definition at line 114 of file sphere.C.

References center(), and radius().

Referenced by Sphere().

115 {
116  this->center() = c;
117  this->radius() = r;
118 
119  libmesh_assert_greater (this->radius(), 0.);
120 }
const Point & center() const
Definition: sphere.h:163
Real radius() const
Definition: sphere.h:153

◆ distance()

Real libMesh::Sphere::distance ( const Sphere other_sphere) const
Returns
The distance between the surface of this sphere and another sphere.

Definition at line 131 of file sphere.C.

References center(), libMesh::TensorTools::norm(), radius(), and libMesh::Real.

Referenced by intersects().

132 {
133  libmesh_assert_greater ( this->radius(), 0. );
134  libmesh_assert_greater ( other_sphere.radius(), 0. );
135 
136  const Real the_distance = (this->center() - other_sphere.center()).norm();
137 
138  return the_distance - (this->radius() + other_sphere.radius());
139 }
const Point & center() const
Definition: sphere.h:163
auto norm(const T &a) -> decltype(std::abs(a))
Definition: tensor_tools.h:74
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Real radius() const
Definition: sphere.h:153

◆ intersects()

bool libMesh::Sphere::intersects ( const Sphere other_sphere) const
Returns
true if other_sphere intersects this sphere, false otherwise.

Definition at line 124 of file sphere.C.

References distance().

125 {
126  return distance(other_sphere) < 0 ? true : false;
127 }
Real distance(const Sphere &other_sphere) const
Definition: sphere.C:131

◆ on_surface()

bool libMesh::Sphere::on_surface ( const Point p) const
overridevirtual
Returns
true if the point p is on the surface, false otherwise.
Note
The definition of "on the surface" really means "very close" to account for roundoff error.

Implements libMesh::Surface.

Definition at line 167 of file sphere.C.

References std::abs(), center(), libMesh::TypeVector< T >::norm(), and radius().

168 {
169  libmesh_assert_greater (this->radius(), 0.);
170 
171  // Create a vector from the center to the point.
172  const Point w = p - this->center();
173 
174  // if the size of that vector is the same as the radius() then
175  // the point is on the surface.
176  if (std::abs(w.norm() - this->radius()) < 1.e-10)
177  return true;
178 
179  return false;
180 }
const Point & center() const
Definition: sphere.h:163
ADRealEigenVector< T, D, asd > abs(const ADRealEigenVector< T, D, asd > &)
Definition: type_vector.h:57
Real radius() const
Definition: sphere.h:153

◆ radius() [1/2]

Real libMesh::Sphere::radius ( ) const
inline
Returns
The radius of the sphere.

Definition at line 153 of file sphere.h.

References _rad.

Referenced by above_surface(), below_surface(), closest_point(), create_from_center_radius(), distance(), on_surface(), Sphere(), and unit_normal().

153 { return _rad; }
Real _rad
The radius of the sphere.
Definition: sphere.h:193

◆ radius() [2/2]

Real& libMesh::Sphere::radius ( )
inline
Returns
The radius of the sphere as a writable reference.

Definition at line 158 of file sphere.h.

References _rad.

158 { return _rad; }
Real _rad
The radius of the sphere.
Definition: sphere.h:193

◆ surface_coords()

Point libMesh::Sphere::surface_coords ( const Point cart) const
inlineoverridevirtual
Returns
The spherical coordinates for the cartesian coordinates cart.

Reimplemented from libMesh::Surface.

Definition at line 201 of file sphere.h.

References center(), libMesh::libmesh_ignore(), libMesh::TypeVector< T >::norm(), libMesh::pi, libMesh::Real, and std::sqrt().

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 }
ADRealEigenVector< T, D, asd > sqrt(const ADRealEigenVector< T, D, asd > &)
Definition: type_vector.h:53
const Point & center() const
Definition: sphere.h:163
void libmesh_ignore(const Args &...)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
const Real pi
.
Definition: libmesh.h:274

◆ unit_normal()

Point libMesh::Sphere::unit_normal ( const Point p) const
overridevirtual
Returns
A unit vector normal to the surface at point p.

Implements libMesh::Surface.

Definition at line 201 of file sphere.C.

References center(), radius(), and libMesh::TypeVector< T >::unit().

Referenced by closest_point().

202 {
203  libmesh_assert_greater (this->radius(), 0.);
204 
205  libmesh_assert_not_equal_to (p, this->center());
206 
207  // Create a vector from the center to the point
208  Point n = p - this->center();
209 
210  return n.unit();
211 }
const Point & center() const
Definition: sphere.h:163
Real radius() const
Definition: sphere.h:153

◆ world_coords()

Point libMesh::Sphere::world_coords ( const Point sph) const
inlineoverridevirtual
Returns
The cartesian coordinates for the spherical coordinates sph.

Reimplemented from libMesh::Surface.

Definition at line 222 of file sphere.h.

References center(), and libMesh::Real.

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 }
const Point & center() const
Definition: sphere.h:163
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real

Member Data Documentation

◆ _cent

Point libMesh::Sphere::_cent
private

The center of the sphere.

Definition at line 188 of file sphere.h.

Referenced by center().

◆ _rad

Real libMesh::Sphere::_rad
private

The radius of the sphere.

Definition at line 193 of file sphere.h.

Referenced by radius().


The documentation for this class was generated from the following files: