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 }

◆ 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.

45 {
46  libmesh_assert_greater (r, 0.);
47 
48  this->create_from_center_radius (c, r);
49 }

References create_from_center_radius().

◆ 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.

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 }

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

◆ Sphere() [4/4]

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

Copy-constructor.

Definition at line 53 of file sphere.C.

53  :
54  Surface()
55 {
56  this->create_from_center_radius (other_sphere.center(),
57  other_sphere.radius());
58 }

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

◆ ~Sphere()

libMesh::Sphere::~Sphere ( )

Destructor.

Does nothing at the moment.

Definition at line 110 of file sphere.C.

111 {
112 }

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 145 of file sphere.C.

146 {
147  libmesh_assert_greater (this->radius(), 0.);
148 
149  // create a vector from the center to the point.
150  const Point w = p - this->center();
151 
152  if (w.norm() > this->radius())
153  return true;
154 
155  return false;
156 }

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

Referenced by below_surface().

◆ 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 160 of file sphere.C.

161 {
162  libmesh_assert_greater (this->radius(), 0.);
163 
164  return ( !this->above_surface (p) );
165 }

References above_surface(), and radius().

◆ center() [1/2]

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

Definition at line 168 of file sphere.h.

168 { return _cent; }

References _cent.

◆ center() [2/2]

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

Definition at line 163 of file sphere.h.

163 { return _cent; }

References _cent.

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

◆ 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 186 of file sphere.C.

187 {
188  libmesh_assert_greater (this->radius(), 0.);
189 
190  // get the normal from the surface in the direction
191  // of p
192  Point normal = this->unit_normal (p);
193 
194  // The closest point on the sphere is in the direction
195  // of the normal a distance r from the center.
196  const Point cp = this->center() + normal*this->radius();
197 
198  return cp;
199 }

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

◆ 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 116 of file sphere.C.

117 {
118  this->center() = c;
119  this->radius() = r;
120 
121  libmesh_assert_greater (this->radius(), 0.);
122 }

References center(), and radius().

Referenced by Sphere().

◆ 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 133 of file sphere.C.

134 {
135  libmesh_assert_greater ( this->radius(), 0. );
136  libmesh_assert_greater ( other_sphere.radius(), 0. );
137 
138  const Real the_distance = (this->center() - other_sphere.center()).norm();
139 
140  return the_distance - (this->radius() + other_sphere.radius());
141 }

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

Referenced by intersects().

◆ intersects()

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

Definition at line 126 of file sphere.C.

127 {
128  return distance(other_sphere) < 0 ? true : false;
129 }

References distance().

◆ 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 169 of file sphere.C.

170 {
171  libmesh_assert_greater (this->radius(), 0.);
172 
173  // Create a vector from the center to the point.
174  const Point w = p - this->center();
175 
176  // if the size of that vector is the same as the radius() then
177  // the point is on the surface.
178  if (std::abs(w.norm() - this->radius()) < 1.e-10)
179  return true;
180 
181  return false;
182 }

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

◆ radius() [1/2]

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

Definition at line 158 of file sphere.h.

158 { return _rad; }

References _rad.

◆ radius() [2/2]

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

Definition at line 153 of file sphere.h.

153 { return _rad; }

References _rad.

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

◆ 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.

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 }

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

◆ 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 203 of file sphere.C.

204 {
205  libmesh_assert_greater (this->radius(), 0.);
206 
207  libmesh_assert_not_equal_to (p, this->center());
208 
209  // Create a vector from the center to the point
210  Point n = p - this->center();
211 
212  return n.unit();
213 }

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

Referenced by closest_point().

◆ 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.

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 }

References center(), and libMesh::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:
libMesh::pi
const Real pi
.
Definition: libmesh.h:237
libMesh::Surface::Surface
Surface()
Constructor.
Definition: surface.h:49
std::sqrt
MetaPhysicL::DualNumber< T, D > sqrt(const MetaPhysicL::DualNumber< T, D > &in)
std::abs
MetaPhysicL::DualNumber< T, D > abs(const MetaPhysicL::DualNumber< T, D > &in)
libMesh::Sphere::_cent
Point _cent
The center of the sphere.
Definition: sphere.h:188
libMesh::Sphere::above_surface
virtual bool above_surface(const Point &p) const override
Definition: sphere.C:145
libMesh::libmesh_ignore
void libmesh_ignore(const Args &...)
Definition: libmesh_common.h:526
libMesh::Sphere::radius
Real radius() const
Definition: sphere.h:153
libMesh::Sphere::unit_normal
virtual Point unit_normal(const Point &p) const override
Definition: sphere.C:203
libMesh::Sphere::distance
Real distance(const Sphere &other_sphere) const
Definition: sphere.C:133
libMesh::Sphere::create_from_center_radius
void create_from_center_radius(const Point &c, const Real r)
Defines a sphere of radius r centered at c.
Definition: sphere.C:116
std::norm
MetaPhysicL::DualNumber< T, D > norm(const MetaPhysicL::DualNumber< T, D > &in)
libMesh::Real
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Definition: libmesh_common.h:121
libMesh::Sphere::center
const Point & center() const
Definition: sphere.h:163
libMesh::Sphere::_rad
Real _rad
The radius of the sphere.
Definition: sphere.h:193