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 libmesh_override
 
virtual bool below_surface (const Point &p) const libmesh_override
 
virtual bool on_surface (const Point &p) const libmesh_override
 
virtual Point closest_point (const Point &p) const libmesh_override
 
virtual Point unit_normal (const Point &p) const libmesh_override
 
Real radius () const
 
Realradius ()
 
const Pointcenter () const
 
Pointcenter ()
 
virtual Point surface_coords (const Point &cart) const libmesh_override
 
virtual Point world_coords (const Point &sph) const libmesh_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

libMesh::Sphere::Sphere ( )

Dummy Constructor.

Definition at line 36 of file sphere.C.

36  :
37  _rad(-1.)
38 {
39 }
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
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)
PetscErrorCode Vec Mat Mat pc
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
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
libMesh::Sphere::~Sphere ( )

Destructor. Does nothing at the moment.

Definition at line 105 of file sphere.C.

106 {
107 }

Member Function Documentation

bool libMesh::Sphere::above_surface ( const Point p) const
virtual
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 }
Real radius() const
Definition: sphere.h:152
const Point & center() const
Definition: sphere.h:162
bool libMesh::Sphere::below_surface ( const Point p) const
virtual
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 }
Real radius() const
Definition: sphere.h:152
virtual bool above_surface(const Point &p) const libmesh_override
Definition: sphere.C:140
const Point& libMesh::Sphere::center ( ) const
inline
Returns
the center of the sphere.

Definition at line 162 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().

162 { return _cent; }
Point _cent
Definition: sphere.h:187
Point& libMesh::Sphere::center ( )
inline
Returns
the center of the sphere.

Definition at line 167 of file sphere.h.

References _cent, surface_coords(), and world_coords().

167 { return _cent; }
Point _cent
Definition: sphere.h:187
Point libMesh::Sphere::closest_point ( const Point p) const
virtual
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().

Referenced by libMesh::MeshTools::Generation::build_sphere().

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 }
virtual Point unit_normal(const Point &p) const libmesh_override
Definition: sphere.C:198
Real radius() const
Definition: sphere.h:152
const Point & center() const
Definition: sphere.h:162
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 }
Real radius() const
Definition: sphere.h:152
const Point & center() const
Definition: sphere.h:162
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 }
Real radius() const
Definition: sphere.h:152
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
const Point & center() const
Definition: sphere.h:162
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
bool libMesh::Sphere::on_surface ( const Point p) const
virtual
Returns
true if the point p is on the surface, false otherwise. Note that 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)
Real radius() const
Definition: sphere.h:152
const Point & center() const
Definition: sphere.h:162
Real libMesh::Sphere::radius ( ) const
inline

Returns the radius of the sphere.

Definition at line 152 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().

152 { return _rad; }
Real& libMesh::Sphere::radius ( )
inline

Returns the radius of the sphere as a writeable reference.

Definition at line 157 of file sphere.h.

References _rad.

157 { return _rad; }
Point libMesh::Sphere::surface_coords ( const Point cart) const
inlinevirtual
Returns
the spherical coordinates for the cartesian coordinates cart.

Reimplemented from libMesh::Surface.

Definition at line 200 of file sphere.h.

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

Referenced by center().

201 {
202  // constant translation in the origin
203  const Point c (cart-this->center());
204 
205  // phi: special care, so that it gives 0..2pi results
206  const Real phi = std::atan2(c(1), c(0));
207 
208  return Point(/* radius */ c.norm(),
209  /* theta */ std::atan2( std::sqrt( c(0)*c(0) + c(1)*c(1) ), c(2) ),
210  /* phi */ ( (phi < 0) ? 2.*libMesh::pi+phi : phi ) );
211 }
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
const Real pi
Definition: libmesh.h:172
const Point & center() const
Definition: sphere.h:162
Point libMesh::Sphere::unit_normal ( const Point p) const
virtual
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 }
Real radius() const
Definition: sphere.h:152
const Point & center() const
Definition: sphere.h:162
Point libMesh::Sphere::world_coords ( const Point sph) const
inlinevirtual
Returns
the cartesian coordinates for the spherical coordinates sph.

Reimplemented from libMesh::Surface.

Definition at line 216 of file sphere.h.

References center(), and libMesh::Real.

Referenced by center().

217 {
218  const Real r = sph(0);
219  const Real theta = sph(1);
220  const Real phi = sph(2);
221 
222  // constant translation out of the origin
223  return Point (/* x */ r*std::sin(theta)*std::cos(phi) + this->center()(0),
224  /* y */ r*std::sin(theta)*std::sin(phi) + this->center()(1),
225  /* z */ r*std::cos(theta) + this->center()(2));
226 }
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
const Point & center() const
Definition: sphere.h:162

Member Data Documentation

Point libMesh::Sphere::_cent
private

The center of the sphere.

Definition at line 187 of file sphere.h.

Referenced by center().

Real libMesh::Sphere::_rad
private

The radius of the sphere.

Definition at line 192 of file sphere.h.

Referenced by radius().


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