Core 1.0
Sphere Class Reference

Spheres. More...

#include <sphere.h>

Inheritance diagram for Sphere:
SmoothSphere

Public Member Functions

 Sphere ()
 Empty.
 Sphere (const double &)
 Creates a sphere centered at origin with specified radius.
 Sphere (const Vector &, const double &=0.0)
 Creates a sphere given center and radius.
 Sphere (const Vector &, const Vector &)
 Creates a sphere given two points.
 Sphere (const Vector &, const Vector &, const Vector &)
 Creates a sphere given three vertices.
 Sphere (const Vector &, const Vector &, const Vector &, const Vector &)
 Create a sphere circumsizing four vertices.
 Sphere (const QVector< Vector > &)
 Compute the minimal bounding sphere of a set of points.
 ~Sphere ()
 Empty.
Vector Center () const
 Gets the center of a sphere.
double Radius () const
 Gets the radius of a sphere.
Box GetBox () const
 Compute the bounding box of a sphere.
bool Intersect (const Ray &) const
 Check the intersection between a sphere and a ray.
int Intersect (const Ray &, double &, double &) const
 Compute the intersection between a sphere and a ray.
int Intersect (const Ray &, double &, double &, Vector &, Vector &) const
 This function computes the intersections between a sphere and a ray.
bool Intersect (const Box &) const
 Box-sphere intersection test.
bool Intersect (const Sphere &, Circle &) const
 Check if two spheres intersect and compute the circle of intersection.
bool Intersect (const Sphere &) const
 Check if two spheres intersect.
bool SurfaceIntersect (const Sphere &) const
 Check if two spheres intersect, only considering their surface.
bool Intersect (const Ray &, double &) const
 This function computes the first intersection between a sphere and a ray.
bool Inside (const Vector &) const
 Check if a point is inside or outside the sphere.
double Volume () const
 Compute the volume of the sphere.
double Volume (const Sphere &) const
 Compute the volume of the intersection of two spheres.
double Area () const
 Compute the surface area of a sphere.
Vector Normal (const Vector &) const
 Computes the normal vector between a point and the sphere.
double R (const Vector &) const
 Compute the squared distance between a point and the sphere.
double Signed (const Vector &) const
 Compute the signed distance between a point and the sphere.
double R (const Sphere &) const
 Compute the signed distance between two spheres.
double R (const Vector &, const Vector &) const
 Compute the great-circle or orthodromic distance.
void Rotate (const Matrix &)
 Rotates a sphere.
void Translate (const Vector &)
 Translates a sphere.
void Scale (const double &)
 Uniformly scales a sphere.
Sphere Translated (const Vector &) const
 Translate a sphere.
Sphere Scaled (const Vector &) const
 Scales a sphere by a given vector.
Sphere Rotated (const Matrix &) const
 Rotates a sphere.
Sphere Transformed (const Frame &) const
 Transforms a sphere.
Sphere InverseTransformed (const Frame &) const
 Inverse transforms a sphere.
void Extend (const Vector &)
 Extend the sphere so that the argument point should be embedded in the new sphere.
void Extend (const double &)
 Extend the sphere, i.e. increase the radius of the sphere.
Sphere Extended (const double &) const
 Extend the sphere, i.e. increase the radius of the sphere.
Vector RandomSurface (Random &=Random::R239) const
 Generate a random point on the sphere.
Vector RandomInside (Random &=Random::R239) const
 Generate a random vector inside the sphere.
Vector Fibonacci (int, int) const
QVector< Vector > Poisson (const double &, int, Random &=Random::R239) const
 Create a Poisson Disc sampling of the surface of the sphere.
Vector2 Euler (const Vector &) const
 Compute the spherical (Euler) coordinates of a point relative to the sphere.

Static Public Member Functions

static Vector RandomNormal (Random &=Random::R239)
 Generate a random unit vector orthonormal to the sphere.
static double Area (const double &)
 Compute the surface area of a sphere.
static double Volume (const double &)
 Compute the volume of a sphere.
static Vector2 EquiRectangular (int, int, int, int)
 Compute the Euler coordinates of a point defined in a rectangle map.
static bool Intersection (const Sphere &, const Sphere &, const Sphere &, Vector &, Vector &)
 Compute the intersection of three spheres.
static bool Intersection (const Sphere &, const Sphere &, const Vector &, Vector &, Vector &)
 Compute the intersection of two spheres and a plane.

Static Public Attributes

static const double epsilon = 1.0e-6
 \( \varepsilon \) for intersection tests.
static const Sphere Null
 Empty sphere.
static const Sphere Infinity
 Infinite sphere.
static const Sphere Unit
 Unit sphere.

Protected Attributes

Vector c = Vector::Null
 Center.
double r = 0.0
 Radius.

Friends

Sphere operator+ (const Sphere &a, const Sphere &b)
 Computes the Minkowski sum of two spheres.
Sphere operator* (const double &t, const Sphere &sphere)
 Scales a sphere by a scalar factor. The homothetic sphere has a positive radius whatever the scaling factor.
Sphere operator* (const Sphere &sphere, const double &t)
 Scales a sphere by a scalar factor. The homothetic sphere has a positive radius whatever the scaling factor.
std::ostream & operator<< (std::ostream &s, const Sphere &sphere)
 Overloaded.

Detailed Description

Spheres.

Spheres are defined by their center and radius. The squared radius is not stored in the data-structure, and is computed on the fly whenever needed.

This class implements several high level functions to compute the minimal bounding sphere of a set of points. It also implements the Minkowski sum of two spheres.

The sphere-ray intersection functions assume that the ray is normalized, i.e. has a unit direction vector.

Constructor & Destructor Documentation

◆ Sphere() [1/6]

Sphere::Sphere ( const double & r)
explicit

Creates a sphere centered at origin with specified radius.

Parameters
rRadius.

◆ Sphere() [2/6]

Sphere::Sphere ( const Vector & c,
const double & r = 0.0 )
explicit

Creates a sphere given center and radius.

Parameters
cCenter.
rRadius.

◆ Sphere() [3/6]

Sphere::Sphere ( const Vector & a,
const Vector & b )
explicit

Creates a sphere given two points.

Basically set the center to the middle of the segment and set the radius to the half length of the segment.

Parameters
a,bThe two vertices.

◆ Sphere() [4/6]

Sphere::Sphere ( const Vector & x,
const Vector & y,
const Vector & z )
explicit

Creates a sphere given three vertices.

Set the center to the center of the triangle, and compute the corresponding radius. Note that the argument vertices should not be aligned.

Parameters
x,y,zThe three vertices.

◆ Sphere() [5/6]

Sphere::Sphere ( const Vector & p0,
const Vector & p1,
const Vector & p2,
const Vector & p3 )
explicit

Create a sphere circumsizing four vertices.

This is the same as a sphere bounding a tetrahedron.

See also
Tetrahedra::GetSphere()
Parameters
p0,p1,p2,p3Argument vertices

◆ Sphere() [6/6]

Sphere::Sphere ( const QVector< Vector > & p)
explicit

Compute the minimal bounding sphere of a set of points.

Error around 1%~2%. Based on Bo Tian, Bouncing bubble: a fast algorithm for minimal enclosing pall problem, 2012.

The sphere of an empty set of points is the null sphere, centered at the origin, with a null radius.

Parameters
pSet of points.

Member Function Documentation

◆ Area()

double Sphere::Area ( const double & r)
inlinestatic

Compute the surface area of a sphere.

Parameters
rRadius.

◆ EquiRectangular()

Vector2 Sphere::EquiRectangular ( int i,
int j,
int x,
int y )
static

Compute the Euler coordinates of a point defined in a rectangle map.

Parameters
i,jPoint coordinates.
x,ySize of the map.
Returns
A vector containing the theta and phi angles.

◆ Euler()

Vector2 Sphere::Euler ( const Vector & p) const

Compute the spherical (Euler) coordinates of a point relative to the sphere.

The point p is first projected onto the unit sphere centered at c by computing the unit direction vector \(\mathbf{u} = \mathrm{Normalized}(p - c)\). The two angles are then:

  • \(\theta \in [0,\pi]\) : polar (colatitude) angle from the Z axis, \(\theta = \arccos(u_z)\).
  • \(\phi \in (-\pi,\pi]\) : azimuthal angle in the XY plane, \(\phi = \mathrm{atan2}(u_x, u_y)\).
Parameters
pPoint in world space (need not lie on the sphere surface).
Returns
Vector2(theta, phi) with angles in radians.

◆ Extend() [1/2]

void Sphere::Extend ( const double & e)

Extend the sphere, i.e. increase the radius of the sphere.

Contrary to Scale, this function does not change the center.

Parameters
eRadius change.

◆ Extend() [2/2]

void Sphere::Extend ( const Vector & p)

Extend the sphere so that the argument point should be embedded in the new sphere.

This is the same as computing the smallest enclosing sphere of a sphere and a point.

Parameters
pPoint.
See also
Circle2::Extend(const Vector2&)

◆ Extended()

Sphere Sphere::Extended ( const double & e) const

Extend the sphere, i.e. increase the radius of the sphere.

See also
Extend
Parameters
eRadius change.

◆ Fibonacci()

Vector Sphere::Fibonacci ( int i,
int n ) const

Compute the i-th point of the Fibonacci distribution of points on the sphere.

In other words, phyllotaxic samples for nearly optimal distribution over a sphere.

Parameters
iPoint index.
nNumber of samples on the sphere.

◆ Inside()

bool Sphere::Inside ( const Vector & p) const

Check if a point is inside or outside the sphere.

Parameters
pThe point.

◆ Intersect() [1/7]

bool Sphere::Intersect ( const Box & box) const

Box-sphere intersection test.

The algorithm calculates the square of the distance from the box to the sphere by analyzing the orientation of the sphere relative to the box in a single loop. If the box is not axis aligned, transform the center of the sphere to the box's local coordinate frame.

After J. Arvo, A simple method for box-sphere intersection testing, In A. Glassner, Graphics Gems: 335-339, Academic Press 1990.

See also
Box::R()

◆ Intersect() [2/7]

bool Sphere::Intersect ( const Ray & ray) const

Check the intersection between a sphere and a ray.

Note that intersections are sorted.

This function assumes that the ray is normalized, i.e. has a unit direction vector.

Parameters
rayThe (normalized) ray.
See also
Sphere::Intersect(const Ray&,double&,double&) const

◆ Intersect() [3/7]

bool Sphere::Intersect ( const Ray & ray,
double & t ) const

This function computes the first intersection between a sphere and a ray.

This function assumes that the ray is normalized, i.e., has a unit direction vector.

Parameters
rayThe (normalized) ray.
tReturned intersection depth.

◆ Intersect() [4/7]

int Sphere::Intersect ( const Ray & ray,
double & ta,
double & tb ) const

Compute the intersection between a sphere and a ray.

Note that intersections are sorted.

This function assumes that the ray is normalized, i.e. has a unit direction vector.

Parameters
rayThe (normalized) ray.
ta,tbIntersection depths.

◆ Intersect() [5/7]

int Sphere::Intersect ( const Ray & ray,
double & ta,
double & tb,
Vector & na,
Vector & nb ) const

This function computes the intersections between a sphere and a ray.

The intersections are sorted.

This function assumes that the ray is normalized, i.e. has a unit direction vector.

Parameters
rayThe (normalized) ray.
ta,tbIntersection depths.
na,nbNormals at intersection points.

◆ Intersect() [6/7]

bool Sphere::Intersect ( const Sphere & sphere) const

Check if two spheres intersect.

Parameters
sphereSphere.

◆ Intersect() [7/7]

bool Sphere::Intersect ( const Sphere & sphere,
Circle & circle ) const

Check if two spheres intersect and compute the circle of intersection.

The spheres intersect if the distance between the centers is between the absolute value of the difference of the radii and the sum of the radii. There is then a circle of intersection, in the plane orthogonal to the line of the centers. The plane may be outside of the segment joining the centers, if the radii are very different.

The function returns false if the spheres are concentric, as there is no circle of intersection (there are infinitely many common points if the radii are equal).

Parameters
sphereSphere.
circleReturned Circle, whose axis is the normalized vector from the center of the other sphere to the center of this sphere.

◆ Intersection() [1/2]

bool Sphere::Intersection ( const Sphere & sa,
const Sphere & sb,
const Sphere & sc,
Vector & u,
Vector & v )
static

Compute the intersection of three spheres.

Parameters
sa,sb,scSpheres.
u,vReturned points.

◆ Intersection() [2/2]

bool Sphere::Intersection ( const Sphere & sa,
const Sphere & sb,
const Vector & n,
Vector & u,
Vector & v )
static

Compute the intersection of two spheres and a plane.

Parameters
sa,sbSpheres.
nUnit normal of the plane containing both centers, should be orthogonal to the direction between the centers.
u,vReturned points.

◆ InverseTransformed()

Sphere Sphere::InverseTransformed ( const Frame & t) const

Inverse transforms a sphere.

Parameters
tTransformation.

◆ Normal()

Vector Sphere::Normal ( const Vector & p) const

Computes the normal vector between a point and the sphere.

Simply project point onto the sphere, and return the corresponding Euclidean distance vector.

Parameters
pPoint.

◆ Poisson()

QVector< Vector > Sphere::Poisson ( const double & r,
int n,
Random & random = Random::R239 ) const

Create a Poisson Disc sampling of the surface of the sphere.

Simple dart throwing algorithm.

Parameters
rRadius of the discs.
nMaximum number of darts thrown on the sphere.
randomRandom number generator.

◆ R() [1/3]

double Sphere::R ( const Sphere & s) const

Compute the signed distance between two spheres.

If the spheres intersect, the result is negative.

Parameters
sThe other sphere.

◆ R() [2/3]

double Sphere::R ( const Vector & p) const

Compute the squared distance between a point and the sphere.

Parameters
pThe point.

◆ R() [3/3]

double Sphere::R ( const Vector & a,
const Vector & b ) const

Compute the great-circle or orthodromic distance.

It is the geodesic distance between two points on the surface of a sphere.

This function is computationnally intensive, requiring several square root and inverse trigonometric function evaluations.

Parameters
a,bPoints on the sphere.

◆ RandomInside()

Vector Sphere::RandomInside ( Random & random = Random::R239) const

Generate a random vector inside the sphere.

Parameters
randomRandom number generator.

◆ RandomNormal()

Vector Sphere::RandomNormal ( Random & random = Random::R239)
static

Generate a random unit vector orthonormal to the sphere.

Parameters
randomRandom number generator.

◆ RandomSurface()

Vector Sphere::RandomSurface ( Random & random = Random::R239) const

Generate a random point on the sphere.

Parameters
randomRandom number generator.

◆ Rotate()

void Sphere::Rotate ( const Matrix & r)

Rotates a sphere.

Parameters
rRotation matrix.

◆ Rotated()

Sphere Sphere::Rotated ( const Matrix & r) const

Rotates a sphere.

Parameters
rRotation matrix.

◆ Scale()

void Sphere::Scale ( const double & s)

Uniformly scales a sphere.

Parameters
sScaling factor.

◆ Scaled()

Sphere Sphere::Scaled ( const Vector & s) const

Scales a sphere by a given vector.

Since the sphere may become an ellipsoid, compute the enclosing sphere.

Parameters
sScaling vector.

◆ Signed()

double Sphere::Signed ( const Vector & p) const

Compute the signed distance between a point and the sphere.

Parameters
pThe point.

◆ SurfaceIntersect()

bool Sphere::SurfaceIntersect ( const Sphere & sphere) const

Check if two spheres intersect, only considering their surface.

Parameters
sphereSphere.

◆ Transformed()

Sphere Sphere::Transformed ( const Frame & t) const

Transforms a sphere.

Parameters
tTransformation.

◆ Translate()

void Sphere::Translate ( const Vector & t)

Translates a sphere.

Parameters
tTranslation vector.

◆ Translated()

Sphere Sphere::Translated ( const Vector & t) const

Translate a sphere.

Parameters
tTranslation vector.

◆ Volume() [1/2]

double Sphere::Volume ( const double & r)
inlinestatic

Compute the volume of a sphere.

Parameters
rRadius.

◆ Volume() [2/2]

double Sphere::Volume ( const Sphere & sphere) const

Compute the volume of the intersection of two spheres.

If the spheres do not intersect, the function returns 0; if the smallest is included in the largest, the function returns the volume of the smallest, otherwise the volume of the lens is computed.

For two spheres of radii \( R \) and \( r \) whose centers are at distance \( d \), with \( |R-r|<d<R+r \), the volume of the intersection is:

\[V=\frac{\pi(R+r-d)^2\left(d^2+2dr-3r^2+2dR+6rR-3R^2\right)}{12d}. \]

See also
Circle::Area(const Circle2&)
Parameters
sphereThe other sphere.

◆ operator<<

std::ostream & operator<< ( std::ostream & s,
const Sphere & sphere )
friend

Overloaded.

Parameters
sStream.
sphereThe sphere.