$extrastylesheet
libMesh::Sphere Class Reference

#include <sphere.h>

Inheritance diagram for libMesh::Sphere:

List of all members.

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 72 of file sphere.h.


Constructor & Destructor Documentation

Dummy Constructor.

Definition at line 36 of file sphere.C.

                :
  _rad(-1.)
{
}
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().

{
  libmesh_assert_greater (r, 0.);

  this->create_from_center_radius (c, r);
}
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::Real, and libMesh::TypeVector< T >::size_sq().

{
  Point pad = pa - pd;
  Point pbd = pb - pd;
  Point pcd = pc - pd;

  TensorValue<Real> T(pad,pbd,pcd);

  Real D = T.det();

  // The points had better not be coplanar
  libmesh_assert_greater (std::abs(D), 1e-12);

  Real e = 0.5*(pa.size_sq() - pd.size_sq());
  Real f = 0.5*(pb.size_sq() - pd.size_sq());
  Real g = 0.5*(pc.size_sq() - pd.size_sq());

  TensorValue<Real> T1(e,pad(1),pad(2),
                       f,pbd(1),pbd(2),
                       g,pcd(1),pcd(2));
  Real sx = T1.det()/D;

  TensorValue<Real> T2(pad(0),e,pad(2),
                       pbd(0),f,pbd(2),
                       pcd(0),g,pcd(2));
  Real sy = T2.det()/D;

  TensorValue<Real> T3(pad(0),pad(1),e,
                       pbd(0),pbd(1),f,
                       pcd(0),pcd(1),g);
  Real sz = T3.det()/D;

  Point c(sx,sy,sz);
  Real r = (c-pa).size();

  this->create_from_center_radius(c,r);
}
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().

                                          :
  Surface()
{
  this->create_from_center_radius (other_sphere.center(),
                                   other_sphere.radius());
}

Destructor. Does nothing at the moment.

Definition at line 105 of file sphere.C.

{
}

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(), radius(), and libMesh::TypeVector< T >::size().

Referenced by below_surface().

{
  libmesh_assert_greater (this->radius(), 0.);

  // create a vector from the center to the point.
  const Point w = p - this->center();

  if (w.size() > this->radius())
    return true;

  return false;
}
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().

{
  libmesh_assert_greater (this->radius(), 0.);

  return ( !this->above_surface (p) );
}
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().

{ return _cent; }
Returns:
the center of the sphere.

Definition at line 167 of file sphere.h.

References _cent.

{ 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(), radius(), and unit_normal().

{
  libmesh_assert_greater (this->radius(), 0.);

  // get the normal from the surface in the direction
  // of p
  Point normal = this->unit_normal (p);

  // The closest point on the sphere is in the direction
  // of the normal a distance r from the center.
  const Point cp = this->center() + normal*this->radius();

  return cp;
}
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().

{
  this->center() = c;
  this->radius() = r;

  libmesh_assert_greater (this->radius(), 0.);
}
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().

{
  libmesh_assert_greater ( this->radius(), 0. );
  libmesh_assert_greater ( other_sphere.radius(), 0. );

  const Real the_distance = (this->center() - other_sphere.center()).size();

  return the_distance - (this->radius() + other_sphere.radius());
}
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().

{
  return distance(other_sphere) < 0 ? true : false;
}
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(), radius(), and libMesh::TypeVector< T >::size().

{
  libmesh_assert_greater (this->radius(), 0.);

  // Create a vector from the center to the point.
  const Point w = p - this->center();

  // if the size of that vector is the same as the radius() then
  // the point is on the surface.
  if (std::abs(w.size() - this->radius()) < 1.e-10)
    return true;

  return false;
}
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().

{ return _rad; }

Returns the radius of the sphere as a writeable reference.

Definition at line 157 of file sphere.h.

References _rad.

{ return _rad; }
Point libMesh::Sphere::surface_coords ( const Point cart) const [inline, virtual]
Returns:
the spherical coordinates for the cartesian coordinates cart.

Reimplemented from libMesh::Surface.

Definition at line 201 of file sphere.h.

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

{
  // constant translation in the origin
  const Point c (cart-this->center());

  // phi: special care, so that it gives 0..2pi results
  const Real phi = std::atan2(c(1), c(0));

  return Point(/* radius */ c.size(),
               /* theta  */ std::atan2( std::sqrt( c(0)*c(0) + c(1)*c(1) ), c(2) ),
               /* phi    */ ( (phi < 0)  ?  2.*libMesh::pi+phi  :  phi ) );
}
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().

{
  libmesh_assert_greater (this->radius(), 0.);

  libmesh_assert_not_equal_to (p, this->center());

  // Create a vector from the center to the point
  Point n = p - this->center();

  return n.unit();
}
Point libMesh::Sphere::world_coords ( const Point sph) const [inline, virtual]
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.

{
  const Real r     = sph(0);
  const Real theta = sph(1);
  const Real phi   = sph(2);

  // constant translation out of the origin
  return Point (/* x */ r*std::sin(theta)*std::cos(phi) + this->center()(0),
                /* y */ r*std::sin(theta)*std::sin(phi) + this->center()(1),
                /* z */ r*std::cos(theta)               + this->center()(2));
}

Member Data Documentation

The center of the sphere.

Definition at line 188 of file sphere.h.

Referenced by center().

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: