Core 1.0
Public Member Functions | Static Public Attributes | Protected Attributes | Friends | List of all members
Box Class Reference

An axis aligned box. More...

#include <box.h>

Inheritance diagram for Box:
Array BoxEmpty BoxSphere Cage ArrayIndex DynamicArrayIndex ScalarField VectorField Voxel

Public Member Functions

 Box ()
 Empty.
 
 Box (const double &)
 Create a cube centered at the origin and of given half side length. More...
 
 Box (const Vector &)
 Create an empty box given one vertex. More...
 
 Box (const Vector &, const Vector &)
 Create a box given two opposite corners. More...
 
 Box (const Vector &, const double &)
 Create a box given a center point and the half side length. More...
 
 Box (const Vector &, const double &, const double &, const double &)
 Create a box given a center point and its width, length, and height. More...
 
 Box (const double &, const double &, const double &)
 Create a box given its sizes. More...
 
 Box (const Vector *, int)
 Creates the bounding box of a set of points. More...
 
 Box (const QVector< Vector > &)
 Creates the bounding box of a set of points. More...
 
 Box (const Box &, const Box &)
 Create a box embedding two boxes. More...
 
 Box (const Box &, const Frame &)
 Creates an axis aligned bounding box from a box and a frame. More...
 
 Box (const Box &, const FrameScaled &)
 Creates an axis aligned bounding box from a box and a frame. More...
 
 Box (const Box &, const Matrix &)
 Creates an axis aligned bounding box from a box and a transformation matrix. More...
 
 ~Box ()
 Empty.
 
Vectoroperator[] (int)
 Returns either end vertex of the box.
 
Vector operator[] (int) const
 Overloaded.
 
Vector Center () const
 Returns the center of the box.
 
Vector Vertex (int) const
 Returns the k-th vertex of the box. More...
 
Vector Vertex (int, int, int, int, int, int) const
 Compute the coordinates of a grid aligned point. More...
 
Segment Edge (int) const
 Compute the k-th edge segment of the box. More...
 
Plane Face (int) const
 Compute the k-th plane of the box. More...
 
Vector Size () const
 Compute the size (width, length and height) of a box. More...
 
Vector Diagonal () const
 Returns the diagonal of the box.
 
double Radius () const
 Returns the radius of the box, i.e. the length of the half diagonal of the box.
 
double R (const Vector &) const
 Computes the squared Euclidean distance between the box and a point. More...
 
double Signed (const Vector &) const
 Computes the signed distance between the box and a point. More...
 
Vector Normal (const Vector &) const
 Computes the normal vector between a point and a box. More...
 
double R (const Box &) const
 Compute the squared Euclidean distance between two boxes. More...
 
double RInfinity (const Box &) const
 Compute the maximum distance between two boxes. More...
 
int Intersect (const Ray &) const
 Check the intersection between a box and a ray. More...
 
int Intersect (const Ray &, double &, double &) const
 Computes the intersection between a box and a ray. More...
 
int Intersect (const Ray &, double &) const
 Compute the first positive intersection between the box and a ray. More...
 
int Intersect (const Ray &, double &, double &, Vector &, Vector &) const
 Compute the intersection between an axis aligned box and a ray. More...
 
int Intersect (const Ray &, double &, Vector &) const
 Compute the first positive intersection between the box and a ray. More...
 
int Intersect (const Vector &, const Vector &) const
 Check if a segment intersects an axis aligned box. More...
 
bool Intersect (const Box &) const
 Check if the box intersects another box. More...
 
bool Inside (const Box &) const
 Check if an argument box is inside the box. More...
 
bool Inside (const Vector &) const
 Check if a point is inside the box. More...
 
Box Intersection (const Box &) const
 Computes the intersection between two boxes. More...
 
int Difference (const Box &, Box *) const
 Computes the difference between two boxes. More...
 
bool Empty () const
 Check if a box is empty. More...
 
double Volume () const
 Compute the volume of a box.
 
double Area () const
 Compute the surface area of a box.
 
void SetCubic ()
 Creates the tightest embedding cube from an arbitrarilly shaped box. More...
 
void SetInscribedCubic ()
 Creates the biggest cube iscribed in the box. More...
 
Box Cube () const
 Return the tightest embedding cube from an arbitrarilly shaped box. More...
 
void Extend (const double &)
 Extend the limits of the box by a given distance. More...
 
Box Extended (const double &) const
 Extend the limits of the box by a given distance. More...
 
void Extend (const Vector &)
 Extend the limits of the box given a point. More...
 
void SetParallelepipedic (const double &, int &, int &, int &)
 Creates a parallelepipedic box whose dimensions are integer multiples of a given input reference size. More...
 
void SetParallelepipedic (int, int &, int &, int &)
 Inflates a box so that its dimensions should be a fraction of its maximum side length. More...
 
Box Sub (int) const
 Computes the sub-box in the n-th octant. More...
 
int Octant (const Vector &) const
 Compute the octant index of a vertex with respect to the box center. More...
 
void Translate (const Vector &)
 Translates a box. More...
 
Box Translated (const Vector &) const
 Translated box. More...
 
void Scale (const double &)
 Scales a box. More...
 
void Scale (const Vector &)
 Scales a box. More...
 
Box Scaled (const Vector &) const
 Scales a box and return the scaled box. More...
 
Box Offsetted (const Vector &) const
 Offets a box. More...
 
Box Centered () const
 Compute the box translated to origin. More...
 
Box Cut (const Vector &, const Vector &) const
 Compute the minimal box embedding the box cut by half space. More...
 
int IntegerAxis () const
 Compute the axis which has the greater length.
 
Vector RandomInside (Random &=Random::R239) const
 Generate a random vector inside the box. More...
 
Vector RandomSurface (Random &=Random::R239) const
 Generate a random vector on the surface of the box. More...
 
QVector< VectorPoisson (const double &, int, Random &=Random::R239) const
 Compute a Poisson sphere distribution inside a box. More...
 

Static Public Attributes

static constexpr const double epsilon = 1.0e-5
 Epsilon value used to check intersections and some round off errors and for ray intersection tests.
 
static const Box Infinity
 Largest box. More...
 
static const Box Null
 Null box, equivalent to: More...
 
static const Box Unit
 Unit box.
 
static const int edge [24]
 Edge vertex indexes.
 
static const Vector normal [6]
 Face normals.
 

Protected Attributes

Vector a
 Lower vertex.
 
Vector b
 Upper vertex.
 

Friends

bool operator== (const Box &, const Box &)
 Check if two boxes are (strictly) equal. More...
 
bool operator!= (const Box &, const Box &)
 Check if two boxes are (strictly) different. More...
 
Box operator+ (const Box &, const Box &)
 Computes the Minkowski sum of two boxes. More...
 
Box operator* (const double &, const Box &)
 Scales a box by a scalar factor.
 
Box operator* (const Box &, const double &)
 Overloaded.
 
std::ostream & operator<< (std::ostream &, const Box &)
 Overloaded. More...
 

Detailed Description

An axis aligned box.

The class stores the opposite two corners as vectors. The center and the radius (diagonal vector) are computed on the fly by inline functions.

The vertices of a box can be obtained by the Box::Vertex() member function which returns one of the eight vertices of the box. The two opposite corners can be obtained faster as follows:

Box box(Vector(0.0,0.0,0.0),Vector(1.0,1.0,1.0)); // Unit box
Vector a=box[0]; // Lower vertex
Vector b=box[1]; // Opposite vertex
An axis aligned box.
Definition: box.h:23
Vector b
Upper vertex.
Definition: box.h:26
Vector a
Lower vertex.
Definition: box.h:25
Vectors in three dimensions.
Definition: evector.h:21

This class provides a set of useful functions, such as the intersection between a box and a ray. This class also implements the Minkowski sum of boxes by overloading some operators.

Note that some intersection methods involving boxes may be implemented in other classes, for instance Segment::Intersect(const Box&) const.

Constructor & Destructor Documentation

◆ Box() [1/12]

Box::Box ( const double &  r)
explicit

Create a cube centered at the origin and of given half side length.

This is equivalent to:

Box box(Vector(0.0),2.0); // Simplified constructor Box(2.0);
Parameters
rHalf side length.

◆ Box() [2/12]

Box::Box ( const Vector a)
explicit

Create an empty box given one vertex.

Parameters
aVertex.

◆ Box() [3/12]

Box::Box ( const Vector a,
const Vector b 
)
explicit

Create a box given two opposite corners.

Note that this constructor does not check the coordinates of the two vectors. Therefore, the coordinates of a should be lower than those of b.

To create the axis aligned bounding box of two vectors a and b in the general case, one should use:

static Vector Min(const Vector &, const Vector &)
Return a vector with coordinates set to the minimum coordinates of the two argument vectors.
Definition: evector.h:521
double Max() const
Compute the maximum component of a vector.
Definition: evector.h:423
Parameters
a,bEnd vertices.

◆ Box() [4/12]

Box::Box ( const Vector c,
const double &  r 
)
explicit

Create a box given a center point and the half side length.

Parameters
cCenter.
rHalf side length.

◆ Box() [5/12]

Box::Box ( const Vector c,
const double &  x,
const double &  y,
const double &  z 
)
explicit

Create a box given a center point and its width, length, and height.

Parameters
ccenter.
x,y,zWidth,length, and height.

◆ Box() [6/12]

Box::Box ( const double &  x,
const double &  y,
const double &  z 
)
explicit

Create a box given its sizes.

Parameters
x,y,zWidth,length, and height.

◆ Box() [7/12]

Box::Box ( const Vector v,
int  n 
)
explicit

Creates the bounding box of a set of points.

Parameters
vArray of vertices.
nNumber of vertices.

◆ Box() [8/12]

Box::Box ( const QVector< Vector > &  v)
explicit

Creates the bounding box of a set of points.

Parameters
vArray of vertices.

◆ Box() [9/12]

Box::Box ( const Box x,
const Box y 
)
explicit

Create a box embedding two boxes.

Parameters
x,yArgument boxes.

◆ Box() [10/12]

Box::Box ( const Box box,
const Frame frame 
)
explicit

Creates an axis aligned bounding box from a box and a frame.

Parameters
boxThe box.
frameTransformation.

◆ Box() [11/12]

Box::Box ( const Box box,
const FrameScaled frame 
)
explicit

Creates an axis aligned bounding box from a box and a frame.

Parameters
boxThe box.
frameTransformation.

◆ Box() [12/12]

Box::Box ( const Box box,
const Matrix t 
)
explicit

Creates an axis aligned bounding box from a box and a transformation matrix.

Parameters
boxThe box.
tTransformation matrix.

Member Function Documentation

◆ Centered()

Box Box::Centered ( ) const

Compute the box translated to origin.

This is the same as:

Box centered=box;
centered.Translate(-box.Center());
void Translate(const Vector &)
Translates a box.
Definition: box.cpp:1257

◆ Cube()

Box Box::Cube ( ) const

Return the tightest embedding cube from an arbitrarilly shaped box.

See also
SetSubic()

◆ Cut()

Box Box::Cut ( const Vector p,
const Vector n 
) const

Compute the minimal box embedding the box cut by half space.

The part of the box that will be removed lies in the positive side of the half space.

Parameters
pPlane point.
nPlane normal.

◆ Difference()

int Box::Difference ( const Box y,
Box boxes 
) const

Computes the difference between two boxes.

Parameters
yArgument box.
boxesAn array of up to 8 boxes.

◆ Edge()

Segment Box::Edge ( int  k) const

Compute the k-th edge segment of the box.

See also
Box::Vertex(int) const
Parameters
kInteger.

◆ Empty()

bool Box::Empty ( ) const
inline

Check if a box is empty.

The box is empty if one of the coordinates of its lower point is greater than the coordinates of the opposite point.

◆ Extend() [1/2]

void Box::Extend ( const double &  r)

Extend the limits of the box by a given distance.

Note that this is the same as performing the Minkowski sum with a cubic box of size r.

Parameters
rRange.

◆ Extend() [2/2]

void Box::Extend ( const Vector p)

Extend the limits of the box given a point.

If the point lies inside the box, the vertices of the box are unchanged.

Parameters
pPoint.

◆ Extended()

Box Box::Extended ( const double &  r) const

Extend the limits of the box by a given distance.

Note that this is the same as performing the Minkowski sum with a cubic box of size r.

Parameters
rRange.

◆ Face()

Plane Box::Face ( int  k) const

Compute the k-th plane of the box.

Parameters
kInteger.

◆ Inside() [1/2]

bool Box::Inside ( const Box box) const
inline

Check if an argument box is inside the box.

Parameters
boxThe box.

◆ Inside() [2/2]

bool Box::Inside ( const Vector p) const
inline

Check if a point is inside the box.

Parameters
pPoint.

◆ Intersect() [1/7]

bool Box::Intersect ( const Box box) const
inline

Check if the box intersects another box.

Parameters
boxArgument box.

◆ Intersect() [2/7]

int Box::Intersect ( const Ray ray) const

Check the intersection between a box and a ray.

Although intersection depths are computed internally, none are returned. This function calls the intersection function and hides arguments in local variables.

Parameters
rayThe ray.

◆ Intersect() [3/7]

int Box::Intersect ( const Ray ray,
double &  t 
) const

Compute the first positive intersection between the box and a ray.

Parameters
rayThe ray.
tIntersection depth.

◆ Intersect() [4/7]

int Box::Intersect ( const Ray ray,
double &  tmin,
double &  tmax 
) const

Computes the intersection between a box and a ray.

Sorted intersection depths are returned if intersection occurs.

Parameters
rayThe ray
tmin,tmaxIntersection depths

◆ Intersect() [5/7]

int Box::Intersect ( const Ray ray,
double &  tmin,
double &  tmax,
Vector an,
Vector bn 
) const

Compute the intersection between an axis aligned box and a ray.

Parameters return the sorted intersection depths and the corresponding normal vectors.

Parameters
rayThe ray.
tmin,tmaxMinimum and maximum intersection depths.
an,bnNormals at intersection points.

◆ Intersect() [6/7]

int Box::Intersect ( const Ray ray,
double &  t,
Vector n 
) const

Compute the first positive intersection between the box and a ray.

Parameters
rayThe ray.
tIntersection depth.
nNormal at intersection point.

◆ Intersect() [7/7]

int Box::Intersect ( const Vector a,
const Vector b 
) const

Check if a segment intersects an axis aligned box.

Testing a box and a segment for intersection requires checking only six separating axes: the box's three principal axes, and the vector cross products of these axes with the line direction. Again, the vectors used for these tests do not have to be normalized, and these tests can be simplified by transforming the line segment into the box's coordinate frame.

Parameters
a,bLine segment.

◆ Intersection()

Box Box::Intersection ( const Box x) const

Computes the intersection between two boxes.

Note that if the intersection is empty, the resulting box is invalid.

Parameters
xArgument box.

◆ Normal()

Vector Box::Normal ( const Vector p) const
inline

Computes the normal vector between a point and a box.

Let q the projection of p onto the box, the normal vector is defined as n=p-q.

Parameters
pPoint.

◆ Octant()

int Box::Octant ( const Vector p) const

Compute the octant index of a vertex with respect to the box center.

See also
Vector::Octant()
Parameters
pPoint.

◆ Offsetted()

Box Box::Offsetted ( const Vector v) const

Offets a box.

Parameters
vOffset vector.
See also
Box::Extend

◆ Poisson()

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

Compute a Poisson sphere distribution inside a box.

This function uses a simple O(n3) dart throwing algorithm.

See also
SphereTile
Parameters
rRadius of the sphere.
nNumber of candidate points.
randomRandom number generator.

◆ R() [1/2]

double Box::R ( const Box y) const

Compute the squared Euclidean distance between two boxes.

This function computes the squared distance to avoid the computation of a square root.

Parameters
yThe box.

◆ R() [2/2]

double Box::R ( const Vector p) const
inline

Computes the squared Euclidean distance between the box and a point.

Parameters
pPoint.

◆ RandomInside()

Vector Box::RandomInside ( Random random = Random::R239) const

Generate a random vector inside the box.

Parameters
randomRandom number generator.

◆ RandomSurface()

Vector Box::RandomSurface ( Random random = Random::R239) const

Generate a random vector on the surface of the box.

Parameters
randomRandom number generator.

◆ RInfinity()

double Box::RInfinity ( const Box y) const

Compute the maximum distance between two boxes.

Parameters
yThe box.

◆ Scale() [1/2]

void Box::Scale ( const double &  s)

Scales a box.

Note that this function handles negative coefficients in the scaling vector (by swapping coordinates if need be).

Parameters
sScaling.

◆ Scale() [2/2]

void Box::Scale ( const Vector s)

Scales a box.

Note that this function handles negative coefficients in the scaling vector (by swapping coordinates if need be).

Parameters
sScaling vector.

◆ Scaled()

Box Box::Scaled ( const Vector s) const

Scales a box and return the scaled box.

Parameters
sScaling vector.
See also
Box::Scale

◆ SetCubic()

void Box::SetCubic ( )

Creates the tightest embedding cube from an arbitrarilly shaped box.

This function creates a cube located at the same center point, and its side length equal to the maximum side of the argument box.

See also
SetInscribedCubic()

◆ SetInscribedCubic()

void Box::SetInscribedCubic ( )

Creates the biggest cube iscribed in the box.

This function creates a cube located at the same center point, and its side length equal to the minimum side of the argument box.

See also
SetCubic()

◆ SetParallelepipedic() [1/2]

void Box::SetParallelepipedic ( const double &  size,
int &  x,
int &  y,
int &  z 
)

Creates a parallelepipedic box whose dimensions are integer multiples of a given input reference size.

Parameters
sizeReference size, the dimension of the box will be a multiple of this size.
x,y,zThree integers.

◆ SetParallelepipedic() [2/2]

void Box::SetParallelepipedic ( int  n,
int &  x,
int &  y,
int &  z 
)

Inflates a box so that its dimensions should be a fraction of its maximum side length.

Parameters
nFraction.
x,y,zThree integers.

◆ Signed()

double Box::Signed ( const Vector p) const

Computes the signed distance between the box and a point.

Parameters
pPoint.

◆ Size()

Vector Box::Size ( ) const
inline

Compute the size (width, length and height) of a box.

See also
Box::Diagonal()

◆ Sub()

Box Box::Sub ( int  n) const

Computes the sub-box in the n-th octant.

Parameters
nOctant index.

◆ Translate()

void Box::Translate ( const Vector t)

Translates a box.

Parameters
tTranslation vector.

◆ Translated()

Box Box::Translated ( const Vector t) const

Translated box.

Parameters
tTranslation vector.

◆ Vertex() [1/2]

Vector Box::Vertex ( int  k) const
inline

Returns the k-th vertex of the box.

The returned vector is computed by analysing the first three bits of k as follows:

Vector vertex=Vector((k&1)?b[0]:a[0],(k&2)?b[1]:a[1],(k&4)?b[2]:a[2]);

◆ Vertex() [2/2]

Vector Box::Vertex ( int  i,
int  j,
int  k,
int  x,
int  y,
int  z 
) const

Compute the coordinates of a grid aligned point.

This function computes the coordinates of a point inside the box as if the box was decomposed into a regular grid.

Parameters
i,j,kInteger coordinates.
x,y,zVirtual grid size.

Friends And Related Function Documentation

◆ operator!=

bool operator!= ( const Box a,
const Box b 
)
friend

Check if two boxes are (strictly) different.

Parameters
a,bBoxes.

◆ operator+

Box operator+ ( const Box a,
const Box b 
)
friend

Computes the Minkowski sum of two boxes.

Parameters
a,bArgument boxes.

◆ operator<<

std::ostream & operator<< ( std::ostream &  s,
const Box box 
)
friend

Overloaded.

Parameters
sStream.
boxThe box.

◆ operator==

bool operator== ( const Box a,
const Box b 
)
friend

Check if two boxes are (strictly) equal.

Parameters
a,bBoxes.

Member Data Documentation

◆ Infinity

const Box Box::Infinity
static

Largest box.

Huge bounding box, which should enclose any other.

◆ Null

const Box Box::Null
static

Null box, equivalent to:

Box(Vector(0.0));
Box()
Empty.
Definition: box.h:29