libMesh
Loading...
Searching...
No Matches
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.
 
 Sphere (const Point &c, const Real r)
 Constructs a sphere of radius r centered at c.
 
 Sphere (const Point &, const Point &, const Point &, const Point &)
 Constructs a sphere connecting four points.
 
 Sphere (const Sphere &other_sphere)
 Copy-constructor.
 
 ~Sphere ()
 Destructor.
 
void create_from_center_radius (const Point &c, const Real r)
 Defines a sphere of radius r centered at c.
 
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.
 
Real _rad
 The radius of the sphere.
 

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}
Real _rad
The radius of the sphere.
Definition sphere.h:193

◆ 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}
void create_from_center_radius(const Point &c, const Real r)
Defines a sphere of radius r centered at c.
Definition sphere.C:114

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}
auto norm(const T &a)
void libmesh_ignore(const Args &...)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real

References create_from_center_radius(), libMesh::TypeTensor< T >::det(), libMesh::libmesh_ignore(), 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}
Surface()=default
Constructor.

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

◆ ~Sphere()

libMesh::Sphere::~Sphere ( )
default

Destructor.

Does nothing at the moment.

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

144{
145 libmesh_assert_greater (this->radius(), 0.);
146
147 // create a vector from the center to the point.
148 const Point w = p - this->center();
149
150 if (w.norm() > this->radius())
151 return true;
152
153 return false;
154}
const Point & center() const
Definition sphere.h:163
Real radius() const
Definition sphere.h:153

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

159{
160 libmesh_assert_greater (this->radius(), 0.);
161
162 return ( !this->above_surface (p) );
163}
virtual bool above_surface(const Point &p) const override
Definition sphere.C:143

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; }
Point _cent
The center of the sphere.
Definition sphere.h:188

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

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

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

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

References center(), and radius().

Referenced by Sphere(), Sphere(), and 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 131 of file sphere.C.

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

References center(), 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 124 of file sphere.C.

125{
126 return distance(other_sphere) < 0 ? true : false;
127}
Real distance(const Sphere &other_sphere) const
Definition sphere.C:131

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

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

References 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}
const Real pi
.
Definition libmesh.h:292

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

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

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

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(), and center().

◆ _rad

Real libMesh::Sphere::_rad
private

The radius of the sphere.

Definition at line 193 of file sphere.h.

Referenced by radius(), and radius().


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