libMesh::Sphere Class Reference

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

Private Attributes

Point _cent
 
Real _rad
 

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:

  • $ 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 75 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(), and libMesh::libmesh_assert_greater().

45 {
46  libmesh_assert_greater (r, 0.);
47 
48  this->create_from_center_radius (c, r);
49 }
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_assert_greater(), libMesh::Real, and libMesh::TypeVector< T >::size_sq().

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.size_sq() - pd.size_sq());
79  Real f = 0.5*(pb.size_sq() - pd.size_sq());
80  Real g = 0.5*(pc.size_sq() - pd.size_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).size();
99 
100  this->create_from_center_radius(c,r);
101 }
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 }
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::libmesh_assert_greater(), radius(), and libMesh::TypeVector< T >::size().

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.size() > this->radius())
148  return true;
149 
150  return false;
151 }
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(), libMesh::libmesh_assert_greater(), and radius().

156 {
157  libmesh_assert_greater (this->radius(), 0.);
158 
159  return ( !this->above_surface (p) );
160 }
const Point& libMesh::Sphere::center ( ) const
inline
Returns
the center of the sphere.

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

165 { return _cent; }
Point& libMesh::Sphere::center ( )
inline
Returns
the center of the sphere.

Definition at line 170 of file sphere.h.

References _cent.

170 { return _cent; }
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(), libMesh::libmesh_assert_greater(), 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 }
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(), libMesh::libmesh_assert_greater(), and radius().

Referenced by Sphere().

112 {
113  this->center() = c;
114  this->radius() = r;
115 
116  libmesh_assert_greater (this->radius(), 0.);
117 }
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(), libMesh::libmesh_assert_greater(), 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()).size();
134 
135  return the_distance - (this->radius() + other_sphere.radius());
136 }
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 }
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::libmesh_assert_greater(), radius(), and libMesh::TypeVector< T >::size().

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.size() - this->radius()) < 1.e-10)
174  return true;
175 
176  return false;
177 }
Real libMesh::Sphere::radius ( ) const
inline

Returns the radius of the sphere.

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

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

Returns the radius of the sphere as a writeable reference.

Definition at line 160 of file sphere.h.

References _rad.

160 { 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 204 of file sphere.h.

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

205 {
206  // constant translation in the origin
207  const Point c (cart-this->center());
208 
209  // phi: special care, so that it gives 0..2pi results
210  const Real phi = std::atan2(c(1), c(0));
211 
212  return Point(/* radius */ c.size(),
213  /* theta */ std::atan2( std::sqrt( c(0)*c(0) + c(1)*c(1) ), c(2) ),
214  /* phi */ ( (phi < 0) ? 2.*libMesh::pi+phi : phi ) );
215 }
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(), libMesh::libmesh_assert_greater(), 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 }
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 220 of file sphere.h.

References center(), and libMesh::Real.

221 {
222  const Real r = sph(0);
223  const Real theta = sph(1);
224  const Real phi = sph(2);
225 
226  // constant translation out of the origin
227  return Point (/* x */ r*std::sin(theta)*std::cos(phi) + this->center()(0),
228  /* y */ r*std::sin(theta)*std::sin(phi) + this->center()(1),
229  /* z */ r*std::cos(theta) + this->center()(2));
230 }

Member Data Documentation

Point libMesh::Sphere::_cent
private

The center of the sphere.

Definition at line 191 of file sphere.h.

Referenced by center().

Real libMesh::Sphere::_rad
private

The radius of the sphere.

Definition at line 196 of file sphere.h.

Referenced by radius().


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

Site Created By: libMesh Developers
Last modified: February 07 2014 16:58:02 UTC

Hosted By:
SourceForge.net Logo