libMesh::Sphere Class Reference

A geometric object representing a sphere. More...

#include <sphere.h>

Inheritance diagram for libMesh::Sphere:

Public Member Functions

 Sphere ()
 
 Sphere (const Point &c, const Real r)
 
 Sphere (const Point &, const Point &, const Point &, const Point &)
 
 Sphere (const Sphere &other_sphere)
 
 ~Sphere ()
 
void create_from_center_radius (const Point &c, const Real r)
 
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
 
Real _rad
 

Detailed Description

A geometric object representing a sphere.

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:

  • $ 0 \le r < \infty $
  • $ 0 \le \theta < \pi $
  • $ 0 \le \phi < 2\pi $

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

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.

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)
Definition: sphere.C:111

◆ 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::TypeVector< T >::norm_sq(), and libMesh::Real.

66 {
67  Point pad = pa - pd;
68  Point pbd = pb - pd;
69  Point pcd = pc - pd;
70 
71  TensorValue<Real> T(pad,pbd,pcd);
72 
73  Real D = T.det();
74 
75  // The points had better not be coplanar
76  libmesh_assert_greater (std::abs(D), 1e-12);
77 
78  Real e = 0.5*(pa.norm_sq() - pd.norm_sq());
79  Real f = 0.5*(pb.norm_sq() - pd.norm_sq());
80  Real g = 0.5*(pc.norm_sq() - pd.norm_sq());
81 
82  TensorValue<Real> T1(e,pad(1),pad(2),
83  f,pbd(1),pbd(2),
84  g,pcd(1),pcd(2));
85  Real sx = T1.det()/D;
86 
87  TensorValue<Real> T2(pad(0),e,pad(2),
88  pbd(0),f,pbd(2),
89  pcd(0),g,pcd(2));
90  Real sy = T2.det()/D;
91 
92  TensorValue<Real> T3(pad(0),pad(1),e,
93  pbd(0),pbd(1),f,
94  pcd(0),pcd(1),g);
95  Real sz = T3.det()/D;
96 
97  Point c(sx,sy,sz);
98  Real r = (c-pa).norm();
99 
100  this->create_from_center_radius(c,r);
101 }
double abs(double a)
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

◆ 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 }
void create_from_center_radius(const Point &c, const Real r)
Definition: sphere.C:111

◆ ~Sphere()

libMesh::Sphere::~Sphere ( )

Destructor. Does nothing at the moment.

Definition at line 105 of file sphere.C.

106 {
107 }

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

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

Referenced by below_surface().

141 {
142  libmesh_assert_greater (this->radius(), 0.);
143 
144  // create a vector from the center to the point.
145  const Point w = p - this->center();
146 
147  if (w.norm() > this->radius())
148  return true;
149 
150  return false;
151 }
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 155 of file sphere.C.

References above_surface(), and radius().

156 {
157  libmesh_assert_greater (this->radius(), 0.);
158 
159  return ( !this->above_surface (p) );
160 }
virtual bool above_surface(const Point &p) const override
Definition: sphere.C:140
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
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
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 181 of file sphere.C.

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

182 {
183  libmesh_assert_greater (this->radius(), 0.);
184 
185  // get the normal from the surface in the direction
186  // of p
187  Point normal = this->unit_normal (p);
188 
189  // The closest point on the sphere is in the direction
190  // of the normal a distance r from the center.
191  const Point cp = this->center() + normal*this->radius();
192 
193  return cp;
194 }
const Point & center() const
Definition: sphere.h:163
virtual Point unit_normal(const Point &p) const override
Definition: sphere.C:198
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 111 of file sphere.C.

References center(), and radius().

Referenced by Sphere().

112 {
113  this->center() = c;
114  this->radius() = r;
115 
116  libmesh_assert_greater (this->radius(), 0.);
117 }
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 128 of file sphere.C.

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

Referenced by intersects().

129 {
130  libmesh_assert_greater ( this->radius(), 0. );
131  libmesh_assert_greater ( other_sphere.radius(), 0. );
132 
133  const Real the_distance = (this->center() - other_sphere.center()).norm();
134 
135  return the_distance - (this->radius() + other_sphere.radius());
136 }
const Point & center() const
Definition: sphere.h:163
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 121 of file sphere.C.

References distance().

122 {
123  return distance(other_sphere) < 0 ? true : false;
124 }
Real distance(const Sphere &other_sphere) const
Definition: sphere.C:128

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

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

165 {
166  libmesh_assert_greater (this->radius(), 0.);
167 
168  // Create a vector from the center to the point.
169  const Point w = p - this->center();
170 
171  // if the size of that vector is the same as the radius() then
172  // the point is on the surface.
173  if (std::abs(w.norm() - this->radius()) < 1.e-10)
174  return true;
175 
176  return false;
177 }
double abs(double a)
const Point & center() const
Definition: sphere.h:163
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; }

◆ 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; }

◆ 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::TypeVector< T >::norm(), libMesh::pi, and libMesh::Real.

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

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

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

Referenced by closest_point().

199 {
200  libmesh_assert_greater (this->radius(), 0.);
201 
202  libmesh_assert_not_equal_to (p, this->center());
203 
204  // Create a vector from the center to the point
205  Point n = p - this->center();
206 
207  return n.unit();
208 }
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 217 of file sphere.h.

References center(), and libMesh::Real.

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 }
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: