Core 1.0
Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Attributes | Friends | List of all members
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. More...
 
 Sphere (const Vector &, const double &=0.0)
 Creates a sphere given center and radius. More...
 
 Sphere (const Vector &, const Vector &)
 Creates a sphere given two points. More...
 
 Sphere (const Vector &, const Vector &, const Vector &)
 Creates a sphere given three vertices. More...
 
 Sphere (const Vector &, const Vector &, const Vector &, const Vector &)
 Create a sphere circumsizing four vertices. More...
 
 Sphere (const QVector< Vector > &)
 Compute the minimal bounding sphere of a set of points. More...
 
 ~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. More...
 
int Intersect (const Ray &, double &, double &) const
 Compute the intersection between a sphere and a ray. More...
 
int Intersect (const Ray &, double &, double &, Vector &, Vector &) const
 This function computes the intersections between a sphere and a ray. More...
 
bool Intersect (const Box &) const
 Box-sphere intersection test. More...
 
bool Intersect (const Sphere &, Circle &) const
 Check if two spheres intersect. More...
 
bool Intersect (const Sphere &) const
 Check if two spheres intersect. More...
 
bool Intersect (const Ray &, double &) const
 This function computes the first intersection between a sphere and a ray. More...
 
bool Inside (const Vector &) const
 Check if a point is inside or outside the sphere. More...
 
double Volume () const
 Compute the volume of the sphere.
 
double Volume (const Sphere &) const
 Compute the volume of the intersection of two spheres. More...
 
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. More...
 
double R (const Vector &) const
 Compute the squared distance between a point and the sphere. More...
 
double Signed (const Vector &) const
 Compute the signed distance between a point and the sphere. More...
 
double R (const Sphere &) const
 Compute the signed distance between two spheres. More...
 
double R (const Vector &, const Vector &) const
 Compute the great-circle or orthodromic distance. More...
 
void Rotate (const Matrix &)
 Rotates a sphere. More...
 
void Translate (const Vector &)
 Translates a sphere. More...
 
void Scale (const double &)
 Uniformly scales a sphere. More...
 
Sphere Translated (const Vector &) const
 Translate a sphere. More...
 
Sphere Scaled (const Vector &) const
 Scales a sphere by a given vector. More...
 
Sphere Rotated (const Matrix &) const
 Rotates a sphere. More...
 
Sphere Transformed (const Frame &) const
 Transforms a sphere. More...
 
Sphere InverseTransformed (const Frame &) const
 Inverse transforms a sphere. More...
 
void Extend (const double &)
 Extend the sphere, i.e. increase the radius of the sphere. More...
 
Sphere Extended (const double &) const
 Extend the sphere, i.e. increase the radius of the sphere. More...
 
Vector RandomSurface (Random &=Random::R239) const
 Generate a random point on the sphere. More...
 
Vector RandomInside (Random &=Random::R239) const
 Generate a random vector inside the sphere. More...
 
Vector Fibonacci (int, int) const
 
QVector< VectorPoisson (const double &, int, Random &=Random::R239) const
 Create a Poisson Disc sampling of the sphere. More...
 
Vector2 Euler (const Vector &) const
 Compute the Euler coordinates of a point. More...
 

Static Public Member Functions

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

Static Public Attributes

static const double epsilon = 1.0e-4
  ε 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 &, const Sphere &)
 Computes the Minkowski sum of two spheres.
 
Sphere operator* (const double &, const Sphere &)
 Scales a sphere by a scalar factor. The homothetic sphere has a positive radius whatever the scaling factor.
 
Sphere operator* (const Sphere &, const double &)
 Scales a sphere by a scalar factor. The homothetic sphere has a positive radius whatever the scaling factor.
 
std::ostream & operator<< (std::ostream &, const Sphere &)
 Overloaded. More...
 

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.

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 Euler coordinates of a point.

Parameters
pPoint.
Returns
A vector containing the theta and phi angles.

◆ Extend()

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.

◆ 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.

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.

If the centers are identical, then the spheres intersect if and only if their radii are equal. Otherwise, there is a circle of intersection, the normal of the plane of intersection is derived from the centers of the spheres.

Parameters
sphereSphere.
circleCircle.

◆ Intesection()

bool Sphere::Intesection ( 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.

◆ 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 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.

◆ 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 complex computations are performed.

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

Friends And Related Function Documentation

◆ operator<<

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

Overloaded.

Parameters
sStream.
sphereThe sphere.