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

Axis aligned box in the plane. More...

#include <box.h>

Inheritance diagram for Box2:
Array2 Diamond2 SmoothBox2 ArrayIndex2 ArrayInteger2 ColorField ScalarField2 VectorField2

Public Member Functions

 Box2 ()
 Empty.
 
 Box2 (const double &)
 Create a square box centered at the origin and of given half side length. More...
 
 Box2 (const double &, const double &)
 Create a box centered at the origin and of given dimensions. More...
 
 Box2 (const Vector2 &)
 Create a box from a single vertex. More...
 
 Box2 (const Vector2 &, const Vector2 &)
 Create a box. More...
 
 Box2 (const Vector2 &, const double &)
 Creates a box. More...
 
 Box2 (const Box &)
 Create a box in the plane given a box. More...
 
 Box2 (const Box2 &, const Box2 &)
 Create a box embedding two boxes. More...
 
 Box2 (const Box2 &, const Matrix2 &)
 Creates an axis aligned bounding box from a box and a transformation matrix. More...
 
 Box2 (const Box2 &, const Frame2 &)
 Creates an axis aligned bounding box from a box and a frame. More...
 
 Box2 (const QVector< Vector2 > &)
 Compute the bounding box of a set of points. More...
 
 Box2 (const QSize &)
 Create a box from a Qt size. More...
 
Vector2operator[] (int)
 Returns either end vertex of the box.
 
Vector2 operator[] (int) const
 Overloaded.
 
Vector2 Size () const
 Compute the size (width and height) of a box.
 
double Width () const
 Compute the width of a box. More...
 
double Height () const
 Compute the height of a box. More...
 
Vector2 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 Area () const
 Compute the surface area of a box.
 
double Perimeter () const
 Compute the perimeter of the box.
 
Vector2 Center () const
 Returns the center of the box.
 
Vector2 Vertex (int) const
 Returns the k-th vertex of the box. More...
 
Vector2 Vertex (int, int, int, int) const
 Compute the coordinates of a grid aligned point. More...
 
Box2 Sub (int) const
 Computes the sub-box in the n-th quadrant. More...
 
double R (const Vector2 &) const
 Compute the squared distance between the box and a point. More...
 
double Signed (const Vector2 &) const
 Computes the signed distance between the box and a point. More...
 
double R (const Box2 &) const
 Compute the squared Euclidean distance between two boxes. More...
 
void Translate (const Vector2 &)
 Translates a box. More...
 
void Scale (const Vector2 &)
 Scales a box. More...
 
void Scale (const double &)
 Scales a box. More...
 
Box2 Translated (const Vector2 &) const
 Translated box. More...
 
Box2 Scaled (const double &) const
 Scales a box and return the scaled box. More...
 
Box2 Scaled (const Vector2 &) const
 Scales a box and return the scaled box. More...
 
Box2 Scaled (const QSize &) const
 Scales a box according to a Qt size. More...
 
Box2 ScaledTo (const double &) const
 Scale the box so that the largest side should equal the argument value. More...
 
Box2 Rotated (const double &) const
 Compute the box embedding the rotated box. More...
 
Box2 Rotated (const Matrix2 &) const
 Compute the box embedding the rotated box. More...
 
Box2 Centered () const
 Compute the box translated to origin. More...
 
void SetCubic ()
 Creates the tightest embedding cube from an arbitrarilly shaped box. More...
 
void SetInscribedCubic ()
 Creates the biggest cube inscribed in the box. More...
 
Box2 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...
 
void Extend (const Vector2 &)
 Extend the limits of the box given a point. More...
 
Box2 Extended (const double &) const
 Extend the limits of the box by a given distance. More...
 
void SetParallelepipedic (int, int &, int &)
 Inflates a box so that its dimensions should be a fraction of its maximum side length. More...
 
void SetParallelepipedic (const double &, int &, int &)
 Creates a parallelepipedic box whose dimensions are integer multiples of a given input reference size. More...
 
bool Inside (const Vector2 &) const
 Test if a point is inside the box. More...
 
bool Inside (const Vector2 &, const double &) const
 Test if a point lies withing a given range of the box. More...
 
bool Intersect (const Box2 &) const
 Check if the box intersects another box. More...
 
double OverlapArea (const Box2 &) const
 Computes the overlapping area of two boxes. More...
 
Box2 Intersection (const Box2 &) const
 Computes the intersection between two boxes. More...
 
Box ToBox (const double &, const double &) const
 Convert a planar Box2 to a Box. More...
 
int Quadrant (const Vector2 &) const
 Computes quadrant index of a vertex with respect to the box center. More...
 
bool Intersect (const Ray2 &, double &, double &) const
 Computes the intersection between a box and a ray. More...
 
bool Intersect (const Ray2 &) const
 Computes the intersection between a box and a ray. More...
 
bool Intersect (const Line2 &, double &, double &) const
 Computes the intersection between a box and a line. More...
 
bool Intersect (const Segment2 &, double &, double &) const
 Compute the intersection between a box and a segment. More...
 
bool Intersect (const Line2 &) const
 Check the intersection between a box and a line. More...
 
bool Intersect (const Segment2 &) const
 Check the intersection between a box and a segment. More...
 
void Draw (QGraphicsScene &, const QPen &=QPen(), const QBrush &=QBrush()) const
 Draw a rectangle. More...
 
Vector2 RandomInside (Random &=Random::R239) const
 Generate a random vector inside the box. More...
 
Vector2 RandomOn (Random &=Random::R239) const
 Generate a random vector on the perimeter of the box. More...
 
QRectF GetQtRect () const
 Create the Qt rectangle.
 
QRect TileRange (const Box2 &) const
 Compute the range of index for tiling the argument box so that it covers the box. More...
 
Box2 Tile (int, int) const
 Return the tiled the box using integer coordinates. More...
 
Box2 Tile (const QRect &) const
 Return the tiled the box using integer coordinates. More...
 
QVector< Vector2Poisson (const double &, int, Random &=Random::R239) const
 Compute a Poisson disc distribution inside a box. More...
 

Static Public Member Functions

static Box2 MinMax (const Vector2 &, const Vector2 &)
 Create a box from any two points. More...
 

Static Public Attributes

static const double epsilon = 1.0e-5
 Epsilon value used to check intersections and some round off errors.
 
static const Box2 Infinity
 Huge bounding box, which should enclose any other.
 
static const Box2 Null
 Null box, equivalent to. More...
 
static const Box2 Unit
 Unit box, defined as. More...
 

Protected Attributes

Vector2 b
 Lower and upper vertices of the box.
 

Friends

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

Detailed Description

Axis aligned box in the plane.

See also
Box

Constructor & Destructor Documentation

◆ Box2() [1/11]

Box2::Box2 ( const double &  r)
inlineexplicit

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

This is equivalent to:

Box2 box(Vector2(0.0),2.0); // Simplified constructor Box2(2.0);
Axis aligned box in the plane.
Definition: box.h:371
Vectors in two dimensions.
Definition: evector.h:617
Parameters
rHalf side length.

◆ Box2() [2/11]

Box2::Box2 ( const double &  x,
const double &  y 
)
inlineexplicit

Create a box centered at the origin and of given dimensions.

This is equivalent to:

double width, height;
Box2 box(Vector2(-width/2.0,-height/2.0),Vector2(width/2.0,height/2.0); // Simplified constructor Box2(width,height);
Parameters
x,ySize.

◆ Box2() [3/11]

Box2::Box2 ( const Vector2 p)
inlineexplicit

Create a box from a single vertex.

Parameters
pPoint.

◆ Box2() [4/11]

Box2::Box2 ( const Vector2 a,
const Vector2 b 
)
inlineexplicit

Create a box.

Note that is possible to create a box using Vector as parameters as the compiler will call the constructor Vector2::Vector2(const Vector&).

Parameters
a,bPoints.

◆ Box2() [5/11]

Box2::Box2 ( const Vector2 c,
const double &  r 
)
inlineexplicit

Creates a box.

Parameters
cCenter.
rRadius.

◆ Box2() [6/11]

Box2::Box2 ( const Box box)
inlineexplicit

Create a box in the plane given a box.

Parameters
boxThe box.

◆ Box2() [7/11]

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

Create a box embedding two boxes.

Parameters
x,yArgument boxes.

◆ Box2() [8/11]

Box2::Box2 ( const Box2 box,
const Matrix2 t 
)
explicit

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

Parameters
boxThe box.
tTransformation matrix.

◆ Box2() [9/11]

Box2::Box2 ( const Box2 box,
const Frame2 frame 
)
explicit

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

Parameters
boxThe box.
frameTransformation.

◆ Box2() [10/11]

Box2::Box2 ( const QVector< Vector2 > &  p)
explicit

Compute the bounding box of a set of points.

Parameters
pSet of points (should not be empty).

◆ Box2() [11/11]

Box2::Box2 ( const QSize &  size)
explicit

Create a box from a Qt size.

Parameters
sizeSize.

Member Function Documentation

◆ Centered()

Box2 Box2::Centered ( ) const

Compute the box translated to origin.

This is the same as:

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

◆ Cube()

Box2 Box2::Cube ( ) const

Return the tightest embedding cube from an arbitrarilly shaped box.

See also
SetSubic()

◆ Draw()

void Box2::Draw ( QGraphicsScene &  scene,
const QPen &  pen = QPen(),
const QBrush &  brush = QBrush() 
) const

Draw a rectangle.

Parameters
sceneGraphics scene.
penThe pen.
brushThe brush, should the box be filled.

◆ Extend() [1/2]

void Box2::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 Box2::Extend ( const Vector2 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()

Box2 Box2::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.

◆ Height()

double Box2::Height ( ) const
inline

Compute the height of a box.

See also
Box2::Size()

◆ Inside() [1/2]

bool Box2::Inside ( const Vector2 p) const

Test if a point is inside the box.

Parameters
pPoint.

◆ Inside() [2/2]

bool Box2::Inside ( const Vector2 p,
const double &  r 
) const

Test if a point lies withing a given range of the box.

The following two pieces of code are almost equivalent:

bool a=Box2(1.0).Inside(Vector(1.5),1.0); // Do not modify the box and test range directly
Box2()
Empty.
Definition: box.h:376
Vectors in three dimensions.
Definition: evector.h:21

In this case we modify the geometry:

Box2 box(1.0);
box.Extend(1.0); // We change the box geometry
// Note that we could have used a Minkowski sum: box=box+Box(1.0);
bool a=box.Inside(Vector(1.5));
Parameters
pPoint.
rRange.

◆ Intersect() [1/7]

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

Check if the box intersects another box.

Parameters
boxArgument box.

◆ Intersect() [2/7]

bool Box2::Intersect ( const Line2 l) const

Check the intersection between a box and a line.

This function is a convenience fuction for:

double a,b;
// Intersect with segment
return Intersect(l,a,b);
Vector2 b
Lower and upper vertices of the box.
Definition: box.h:373
bool Intersect(const Box2 &) const
Check if the box intersects another box.
Definition: box.h:711
Parameters
lThe line.

◆ Intersect() [3/7]

bool Box2::Intersect ( const Line2 s,
double &  tmin,
double &  tmax 
) const

Computes the intersection between a box and a line.

Sorted intersection depths are returned if intersection occurs.

Parameters
sThe line.
tmin,tmaxIntersection depths

◆ Intersect() [4/7]

bool Box2::Intersect ( const Ray2 ray) const

Computes the intersection between a box and a ray.

Parameters
rayThe ray.
tmin,tmaxIntersection depths

◆ Intersect() [5/7]

bool Box2::Intersect ( const Ray2 ray,
double &  tmin,
double &  tmax 
) const

Computes the intersection between a box and a ray.

Parameters
rayThe ray.
tmin,tmaxIntersection depths

◆ Intersect() [6/7]

bool Box2::Intersect ( const Segment2 s) const

Check the intersection between a box and a segment.

Parameters
sThe segment.

◆ Intersect() [7/7]

bool Box2::Intersect ( const Segment2 s,
double &  tmin,
double &  tmax 
) const

Compute the intersection between a box and a segment.

Parameters
sThe line.
tmin,tmaxIntersection depths
See also
Box2::Intersect(const Line2&,double&,double&) const

◆ Intersection()

Box2 Box2::Intersection ( const Box2 x) const

Computes the intersection between two boxes.

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

Parameters
xArgument box.

◆ MinMax()

Box2 Box2::MinMax ( const Vector2 a,
const Vector2 b 
)
inlinestatic

Create a box from any two points.

Parameters
a,bPoints.

◆ OverlapArea()

double Box2::OverlapArea ( const Box2 box) const
inline

Computes the overlapping area of two boxes.

This is a convenience function for the following code:

Box2 a,b;
double area=(a.Intersection(b)).Area();
Box2 Intersection(const Box2 &) const
Computes the intersection between two boxes.
Definition: box2.cpp:196
double Area() const
Compute the surface area of a box.
Definition: box.h:688
See also
Box2::Intersection(const Box2&) const
Parameters
box

◆ Poisson()

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

Compute a Poisson disc distribution inside a box.

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

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

◆ Quadrant()

int Box2::Quadrant ( const Vector2 p) const

Computes quadrant index of a vertex with respect to the box center.

See also
Vector2::Quadrant()
Parameters
pPoint.

◆ R() [1/2]

double Box2::R ( const Box2 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 Box2::R ( const Vector2 p) const

Compute the squared distance between the box and a point.

Parameters
pPoint.

◆ RandomInside()

Vector2 Box2::RandomInside ( Random random = Random::R239) const

Generate a random vector inside the box.

Parameters
randomRandom number generator.

◆ RandomOn()

Vector2 Box2::RandomOn ( Random random = Random::R239) const

Generate a random vector on the perimeter of the box.

Parameters
randomRandom number generator.

◆ Rotated() [1/2]

Box2 Box2::Rotated ( const double &  a) const

Compute the box embedding the rotated box.

Parameters
aRotation angle.

◆ Rotated() [2/2]

Box2 Box2::Rotated ( const Matrix2 r) const

Compute the box embedding the rotated box.

Parameters
rRotation matrix.

◆ Scale() [1/2]

void Box2::Scale ( const double &  s)

Scales a box.

Note that this function handles negative coefficients.

Parameters
sScaling factor.

◆ Scale() [2/2]

void Box2::Scale ( const Vector2 s)

Scales a box.

Note that this function handles negative coefficients in the scaling vector.

Parameters
sScaling vector.

◆ Scaled() [1/3]

Box2 Box2::Scaled ( const double &  s) const

Scales a box and return the scaled box.

Parameters
sScaling factor.
See also
Box2::Scaled(const Vector2&)

◆ Scaled() [2/3]

Box2 Box2::Scaled ( const QSize &  s) const

Scales a box according to a Qt size.

This function preserves the width of the box, and scales its height according to the ratio.

Parameters
sSize.
See also
Box2::Scaled

◆ Scaled() [3/3]

Box2 Box2::Scaled ( const Vector2 s) const

Scales a box and return the scaled box.

Parameters
sScaling vector.
See also
Box2::Scale

◆ ScaledTo()

Box2 Box2::ScaledTo ( const double &  s) const

Scale the box so that the largest side should equal the argument value.

Parameters
sTarget largest size.

◆ SetCubic()

void Box2::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.

◆ SetInscribedCubic()

void Box2::SetInscribedCubic ( )

Creates the biggest cube inscribed 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 Box2::SetParallelepipedic ( const double &  size,
int &  x,
int &  y 
)

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,yTwo integers.

◆ SetParallelepipedic() [2/2]

void Box2::SetParallelepipedic ( int  n,
int &  x,
int &  y 
)

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

Parameters
nFraction.
x,yTwo integers.

◆ Signed()

double Box2::Signed ( const Vector2 p) const

Computes the signed distance between the box and a point.

Parameters
pPoint.

◆ Sub()

Box2 Box2::Sub ( int  n) const

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

Parameters
nQuadrant index.

◆ Tile() [1/2]

Box2 Box2::Tile ( const QRect &  r) const

Return the tiled the box using integer coordinates.

Parameters
rRectangle defining tiling coordinates.
See also
TileRange(const Box2&), Tile(int,int)

◆ Tile() [2/2]

Box2 Box2::Tile ( int  x,
int  y 
) const

Return the tiled the box using integer coordinates.

Parameters
x,yInteger coordinates.
See also
TileRange(const Box2&), Tile(const QRect&)

◆ TileRange()

QRect Box2::TileRange ( const Box2 t) const

Compute the range of index for tiling the argument box so that it covers the box.

Parameters
tTiling box.
See also
Tile(const Box2&), Tile(const QRect&)

◆ ToBox()

Box Box2::ToBox ( const double &  a,
const double &  b 
) const

Convert a planar Box2 to a Box.

Parameters
a,bLower and upper coordinates (note that a should be smaller than b).

◆ Translate()

void Box2::Translate ( const Vector2 t)

Translates a box.

Parameters
tTranslation vector.

◆ Translated()

Box2 Box2::Translated ( const Vector2 t) const

Translated box.

Parameters
tTranslation vector.

◆ Vertex() [1/2]

Vector2 Box2::Vertex ( int  k) const
inline

Returns the k-th vertex of the box.

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

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

◆ Vertex() [2/2]

Vector2 Box2::Vertex ( int  i,
int  j,
int  x,
int  y 
) 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,jInteger coordinates.
x,yVirtual grid size.

◆ Width()

double Box2::Width ( ) const
inline

Compute the width of a box.

See also
Box2::Size()

Friends And Related Function Documentation

◆ operator!=

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

Check if two boxes are (strictly) different.

Parameters
a,bBoxes.

◆ operator*

Box2 operator* ( const double &  t,
const Box2 box 
)
friend

Scales a box by a scalar factor.

Parameters
tScaling factor.
boxThe box.

◆ operator+

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

Computes the Minkowski sum of two boxes.

Parameters
a,bArgument boxes.

◆ operator<<

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

Overloaded.

Parameters
sStream.
boxThe box.

◆ operator==

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

Check if two boxes are (strictly) equal.

Parameters
a,bBoxes.

Member Data Documentation

◆ Null

const Box2 Box2::Null
static

Null box, equivalent to.

Box2(Vector2(0.0));

◆ Unit

const Box2 Box2::Unit
static

Unit box, defined as.

Box2(Vector2(0.0),Vector2(1.0));