geomc 1.0
A c++ linear algebra template library
Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | Protected Types | List of all members
Rect< T, N > Class Template Reference

An N-dimensional axis-aligned interval. More...

#include <geomc/shape/Rect.h>

Inheritance diagram for Rect< T, N >:
Convex< T, N, Rect< T, N > > Projectable< T, N, Rect< T, N > > RayIntersectable< T, N, Rect< T, N > > Bounded< T, _N, Derived > SdfEvaluable< T, N, Derived >

Public Types

typedef PointType< T, N >::point_t point_t
 Type of object confined by this Rect.
 
typedef T elem_t
 The coordinate type of this shape.
 

Public Member Functions

constexpr Rect ()
 Construct an empty interval. More...
 
constexpr Rect (point_t lo, point_t hi)
 Construct a Rect with extremes lo and hi. If for any axis lo > hi, the Rect is empty. More...
 
constexpr Rect (point_t p)
 Construct a Rect containing only the point p.
 
Rect< T, Noperator| (const Rect< T, N > &b) const
 Interval union. More...
 
Rect< T, N > & operator|= (const Rect< T, N > &b)
 Interval union. More...
 
Rect< T, Noperator| (const point_t &p)
 Point union. More...
 
Rect< T, N > & operator|= (const point_t &p)
 Point union. More...
 
Rect< T, Noperator& (const Rect< T, N > &b) const
 Interval intersection. More...
 
Rect< T, N > & operator&= (const Rect< T, N > &b)
 Interval intersection. More...
 
Rect< T, Noperator+ (point_t dx) const
 Translation. More...
 
Rect< T, Noperator- (point_t dx) const
 Translation. More...
 
Rect< T, N > & operator+= (point_t dx)
 Translation. More...
 
Rect< T, N > & operator-= (point_t dx)
 Translation. More...
 
Rect< T, Noperator- (const Rect< T, N > &other)
 Interval exclusion. More...
 
Rect< T, N > & operator-= (const Rect< T, N > &other)
 Interval exclusion. More...
 
bool operator== (Rect< T, N > b) const
 Equality test. More...
 
bool operator!= (Rect< T, N > b) const
 Inequality test. More...
 
Rect< T, Noperator* (point_t a) const
 Scale transformation. More...
 
Rect< T, N > & operator*= (point_t a)
 Scale transformation. More...
 
Rect< T, Noperator/ (point_t a) const
 Scale transformation. More...
 
Rect< T, N > & operator/= (point_t a)
 Scale transformation. More...
 
template<index_t M>
Rect< T, M+Noperator* (const Rect< T, M > &r) const
 Interval cartesian product. More...
 
template<typename U , index_t M>
 operator Rect< U, M > () const
 Element-wise typecast. More...
 
bool contains (point_t pt) const
 Point containment test. More...
 
bool contains (const Rect< T, N > &box) const
 Box containment. More...
 
Rect< T, 1 > axis (size_t k) const
 Return the one dimensional range spanned along axis k.
 
bool intersects (const Rect< T, N > &box) const
 Range intersection test. More...
 
point_t center () const
 Center point. More...
 
point_t dimensions () const
 Axial size. More...
 
void set_dimensions (point_t dim)
 Change the size of the region, adjusting about its center. More...
 
void set_corners (point_t corner1, point_t corner2)
 Reconstruct from corner points. More...
 
Rect< T, Ncentered_on (point_t c) const
 Place a copy at a new location. More...
 
Rect< T, Ndilated (point_t c) const
 Morphological dilation. More...
 
Rect< T, Nminkowski_sum (const Rect< T, N > &other) const
 Minkowski sum. More...
 
volume () const
 N-dimensional volume. More...
 
bool is_empty () const
 Empty region test. More...
 
point_t clip (point_t p) const
 Clamp the coordinates of p to lie within this Rect. More...
 
point_t project (point_t p) const
 Return the point on the surface of the shape which is nearest to p. More...
 
sdf (point_t p) const
 Return the signed distance to the surface of the shape.
 
dist2 (point_t p) const
 Squared distance to the interior of the Rect. More...
 
point_t remap (point_t s) const
 Map the unit interval to the region. More...
 
point_t unmap (point_t p) const
 Find p's fractional position within this Rect. More...
 
std::pair< point_t, bool > contact_vector (const Rect< T, N > &other) const
 Return a vector which moves this into precise disjoint contact with other, and a boolean indicating whether the two shapes currently overlap.
 
point_t convex_support (point_t d) const
 
Rect< T, 1 > intersect (const Ray< T, N > &r) const
 Ray-shape intersection test.
 
Rect< T, Nbounds () const
 
bool intersects (const Convex< T, N, Shape > &other) const
 Convex shape overlap test. More...
 
Vec< T, Nnormal (Vec< T, N > p) const
 Unit-length outward-facing direction. More...
 

Static Public Member Functions

static Rect< T, Nfrom_center (typename Rect< T, N >::point_t c, typename Rect< T, N >::point_t dims)
 Construct a Rect from a center point and extent. More...
 
static Rect< T, Nfrom_corner (typename Rect< T, N >::point_t corner, typename Rect< T, N >::point_t dims)
 Construct a Rect from a corner point and an extent. More...
 
static Rect< T, Nspanning_corners (point_t c1, point_t c2)
 Construct a Rect containing the two corners c1 and c2. More...
 
template<typename PointIterator >
static Rect< T, Nfrom_point_sequence (PointIterator begin, PointIterator end)
 Construct a Rect containing all the points in the sequence between begin and end. More...
 
static bool contains (typename Rect< T, N >::point_t lo, typename Rect< T, N >::point_t hi, typename Rect< T, N >::point_t pt)
 Test whether a point is in the N-dimensional range [lo, hi]. More...
 

Public Attributes

point_t lo
 Lower extremes.
 
point_t hi
 Upper extremes.
 

Static Public Attributes

static const point_t endpoint_measure
 
static const Rect< T, Nunit_interval = Rect<T,N>((T)0, (T)1)
 A Rect covering the range [0, 1] along all axes.
 
static const Rect< T, Nsigned_unit_interval = Rect<T,N>((T)-1, (T)1)
 A Rect covering the range [-1, 1] along all axes.
 
static const Rect< T, Nfull
 A Rect that contains all points. More...
 
static const Rect< T, Nempty
 A Rect that contains no points. More...
 
static constexpr size_t N = _N
 The dimension of this shape.
 

Protected Types

typedef PointType< T, Nptype
 

Detailed Description

template<typename T, index_t N>
class geom::Rect< T, N >

An N-dimensional axis-aligned interval.

This class works naturally with both 1D and multi-dimensional ranges. 1D ranges have point type T, while multi-dimensional ranges have point type Vec<T,N>. A typedef of this may be accessed via:

Rect<T,N>::point_t

Example:

If any coordinate of lo is greater than the same coordinate of hi, then the Rect is empty.

Rect boundaries are inclusive. The points lo and hi are considered to be inside the Rect. Therefore, Rects that contain only a single point, edge, or face are considered non-empty.

Note that this convention differs slightly from conventional "interval" logic on integers, wherein the upper boundary is excluded. This convention was chosen so that adding any point to a Rect as a vertex ensures that the Rect thereafter includes that point. To iterate over the range within an integer Rect, consider using a GridIterator object, which abstracts away the boundary logic for you.

Constructor & Destructor Documentation

◆ Rect() [1/2]

constexpr Rect ( )
inlineconstexpr

Construct an empty interval.

Sets lower and upper bounds to the maximum and minimum values of T, respectively.

A union between this Rect and any finite shape is an identity operation.

◆ Rect() [2/2]

constexpr Rect ( point_t  lo,
point_t  hi 
)
inlineconstexpr

Construct a Rect with extremes lo and hi. If for any axis lo > hi, the Rect is empty.

Parameters
loLower extreme
hiUpper extreme

Member Function Documentation

◆ center()

point_t center ( ) const
inline

Center point.

Returns
The center point of this region.

◆ centered_on()

Rect< T, N > centered_on ( point_t  c) const
inline

Place a copy at a new location.

Make a new Rect having the same size as this one, but with its center at c.

Parameters
cNew center point.

◆ clip()

point_t clip ( point_t  p) const
inline

Clamp the coordinates of p to lie within this Rect.

Result can be considered the point nearest to p contained in this Rect.

◆ contains() [1/3]

bool contains ( const Rect< T, N > &  box) const
inline

Box containment.

Returns
true if and only if this Rect fully contains box, in other words that there is no point contained by box which is not contained by this.

◆ contains() [2/3]

bool contains ( point_t  pt) const
inline

Point containment test.

Returns
true if and only if pt is inside this rectangle. Points on the surface of the Rect are considered to be contained by it.

◆ contains() [3/3]

static bool contains ( typename Rect< T, N >::point_t  lo,
typename Rect< T, N >::point_t  hi,
typename Rect< T, N >::point_t  pt 
)
inlinestatic

Test whether a point is in the N-dimensional range [lo, hi].

If lo > hi along any axis, then the range is empty and the function returns false.

Parameters
loLower extreme
hiUpper extreme
ptTest point
Returns
true if pt is inside the exremes

◆ dilated()

Rect< T, N > dilated ( point_t  c) const
inline

Morphological dilation.

Return a Rect with the boundary extended coordinate-wise by the amount c in all directions.

◆ dimensions()

point_t dimensions ( ) const
inline

Axial size.

Returns
The size of this region along each axis.

Note that for integer type Rects, since both the high and low boundaries are included, the length along each axis is hi - lo + 1.

◆ dist2()

T dist2 ( point_t  p) const
inline

Squared distance to the interior of the Rect.

Returns
The square of the distance to the nearest point contained by this Rect; zero if p is inside.

◆ from_center()

static Rect< T, N > from_center ( typename Rect< T, N >::point_t  c,
typename Rect< T, N >::point_t  dims 
)
inlinestatic

Construct a Rect from a center point and extent.

Parameters
cCenter of the new Rect
dimsLengths of each axis; may be negative.
Returns
A new Rect with its center at c.

◆ from_corner()

static Rect< T, N > from_corner ( typename Rect< T, N >::point_t  corner,
typename Rect< T, N >::point_t  dims 
)
inlinestatic

Construct a Rect from a corner point and an extent.

Parameters
cornerArbitrary corner point.
dimsLengths of each axis relative to given corner. Lengths may be negative.
Returns
A new Rect with one corner at corner.

◆ from_point_sequence()

static Rect< T, N > from_point_sequence ( PointIterator  begin,
PointIterator  end 
)
inlinestatic

Construct a Rect containing all the points in the sequence between begin and end.

Template Parameters
PointIteratorAn iterator to a point_t.
Parameters
beginIterator to the first point.
endIterator to the off-end point.
Returns
A new Rect containing all the points in the given sequence.

◆ intersects() [1/2]

bool intersects ( const Convex< T, N, Shape > &  other) const
inlineinherited

Convex shape overlap test.

Returns
True if and only if this convex shape overlaps other; false otherwise.

◆ intersects() [2/2]

bool intersects ( const Rect< T, N > &  box) const
inline

Range intersection test.

Returns
true if and only if there is a point overlapped by both Rects.

◆ is_empty()

bool is_empty ( ) const
inline

Empty region test.

Returns
true if and only if this region contains no points; which will be the case if lo[i] > hi[i] for any axis i.

◆ minkowski_sum()

Rect< T, N > minkowski_sum ( const Rect< T, N > &  other) const
inline

Minkowski sum.

Combine this Rect with another using the Minkowski sum.

◆ normal()

Vec< T, N > normal ( Vec< T, N >  p) const
inlineinherited

Unit-length outward-facing direction.

For any point, return the direction which points directly away from the nearest point on the shape, away from its interior.

This should be the same as the gradient of the sdf().

◆ operator Rect< U, M >()

operator Rect< U, M > ( ) const
inline

Element-wise typecast.

Returns
A new Rect, with elements all of type U.

◆ operator!=()

bool operator!= ( Rect< T, N b) const
inline

Inequality test.

Returns
true if and only if any extreme of b is different from the corresponding extreme in this.

◆ operator&()

Rect< T, N > operator& ( const Rect< T, N > &  b) const
inline

Interval intersection.

Compute a Rect representing the area overlapped by both this and b. This is the unsigned intersection, which means that an intersection with an empty interval yields another empty interval. If empty intervals should instead subtract from nonempty intervals, use signed_intersection().

The unsigned intersection is the most common form of interval intersection, and is the more efficient of the two.

◆ operator&=()

Rect< T, N > & operator&= ( const Rect< T, N > &  b)
inline

Interval intersection.

Confine this Rect to the area overlapping b. This is the unsigned intersection, which means that an intersection with an empty interval yields another empty interval. If empty intervals should instead subtract from nonempty intervals, use signed_intersection().

The unsigned intersection is the most common form of interval intersection, and is the more efficient of the two.

Returns
A reference to this for convenience.

◆ operator*() [1/2]

Rect< T, M+N > operator* ( const Rect< T, M > &  r) const
inline

Interval cartesian product.

Extrude this Rect into a higher dimension, by treating the extents of r as the extents along the new dimensions. In other words, concatenate the coordinates of this and r into a new Rect.

For example, the product of a 2D rectangle with a 1D range is a 3D box. If multiplied in that order, the product will have the extents of the rectangle along its first two axes and the extents of the 1D range along its third axis.

Template Parameters
MDimensionality of r.

◆ operator*() [2/2]

Rect< T, N > operator* ( point_t  a) const
inline

Scale transformation.

Parameters
aScale factor.
Returns
A new Rect, scaled about the origin by factor a.

◆ operator*=()

Rect< T, N > & operator*= ( point_t  a)
inline

Scale transformation.

Scale this Rect about the origin by factor a.

Parameters
aScale factor
Returns
A reference to this, for convenience.

◆ operator+()

Rect< T, N > operator+ ( point_t  dx) const
inline

Translation.

Parameters
dxAmount by which to translate this region. Add dx to the coordinates of all the bounds.
Returns
A translated Rect.

◆ operator+=()

Rect< T, N > & operator+= ( point_t  dx)
inline

Translation.

Parameters
dxAmount by which to translate this region. Add dx to the coordinates of all the bounds.
Returns
A reference to this, for convenience.

◆ operator-() [1/2]

Rect< T, N > operator- ( const Rect< T, N > &  other)
inline

Interval exclusion.

For any dimension where this fully contains other, the result is the empty interval. Any dimension of other that is empty has no effect.

Parameters
otherRange to be excluded from the range of this Rect.
Returns
A new Rect whose range does not overlap other.

◆ operator-() [2/2]

Rect< T, N > operator- ( point_t  dx) const
inline

Translation.

Parameters
dxAmount by which to translate this region. Subtract dx from the coordinates of all the bounds.
Returns
A translated Rect.

◆ operator-=() [1/2]

Rect< T, N > & operator-= ( const Rect< T, N > &  other)
inline

Interval exclusion.

For any dimension where this fully contains other, the result is the empty interval. Any dimension of other that is empty has no effect.

Parameters
otherRange to remove from this Rect.
Returns
A reference to this, for convenience.

◆ operator-=() [2/2]

Rect< T, N > & operator-= ( point_t  dx)
inline

Translation.

Parameters
dxAmount by which to translate this region. Subtract dx from the coordinates of all the bounds.
Returns
A reference to this, for convenience.

◆ operator/()

Rect< T, N > operator/ ( point_t  a) const
inline

Scale transformation.

Parameters
aScale factor.
Returns
A new Rect, scaled about the origin by multiple 1 / a.

◆ operator/=()

Rect< T, N > & operator/= ( point_t  a)
inline

Scale transformation.

Scale this Rect about the origin by factor 1 / a.

Parameters
aScale factor
Returns
A reference to this, for convenience.

◆ operator==()

bool operator== ( Rect< T, N b) const
inline

Equality test.

Returns
true if and only if all the corresponding extremes of b are the same.

◆ operator|() [1/2]

Rect< T, N > operator| ( const point_t p)
inline

Point union.

Returns
A Rect fully containing this and the point p.

◆ operator|() [2/2]

Rect< T, N > operator| ( const Rect< T, N > &  b) const
inline

Interval union.

Returns
A Rect fully containing this and box b.

◆ operator|=() [1/2]

Rect< T, N > & operator|= ( const point_t p)
inline

Point union.

Extend this to fully contain p.

Returns
a reference to this, for convenience.

◆ operator|=() [2/2]

Rect< T, N > & operator|= ( const Rect< T, N > &  b)
inline

Interval union.

Extend this to fully contain b.

Returns
A reference to this, for convenience.

◆ project()

point_t project ( point_t  p) const
inline

Return the point on the surface of the shape which is nearest to p.

Distinct from clip() in that p is projected to the boundary of the Rect, regardless of whether it's inside or outside. By contrast, clip() leaves p unchanged if it lies in the Rect's interior.

◆ remap()

point_t remap ( point_t  s) const
inline

Map the unit interval to the region.

Remap s on the [0,1] interval to the extents of this Rect. In other words, interpolate the corners of this Rect using s as an interpolation parameter.

Inverse operation of unmap().

Values of s between 0 and 1 correspond to points inside this Rect.

◆ set_corners()

void set_corners ( point_t  corner1,
point_t  corner2 
)
inline

Reconstruct from corner points.

Re-configure this region to exactly contain the two given points.

◆ set_dimensions()

void set_dimensions ( point_t  dim)
inline

Change the size of the region, adjusting about its center.

Parameters
dimNew lengths along each axis.

◆ spanning_corners()

static Rect< T, N > spanning_corners ( point_t  c1,
point_t  c2 
)
inlinestatic

Construct a Rect containing the two corners c1 and c2.

Parameters
c1A corner of the Rect
c2The corner opposite c1
Returns
A new Rect with corners at c1 and c2.

◆ unmap()

point_t unmap ( point_t  p) const
inline

Find p's fractional position within this Rect.

Inverse operation of remap().

◆ volume()

T volume ( ) const
inline

N-dimensional volume.

Compute the measure of the region contained by this Rect as the product of its dimensions. Result measures a length if N is 1; an area if N is 2; a volume if 3; etc.

Returns
The volume of this region.

Member Data Documentation

◆ empty

constexpr Rect< T, N > empty
staticconstexpr
Initial value:
= Rect<T,N>(
std::numeric_limits<T>::infinity(),
-std::numeric_limits<T>::infinity())

A Rect that contains no points.

◆ endpoint_measure

constexpr Rect< T, N >::point_t endpoint_measure
staticconstexpr
Initial value:
=
std::is_integral<T>::value ? 1 : 0

◆ full

constexpr Rect< T, N > full
staticconstexpr
Initial value:
= Rect<T,N>(
-std::numeric_limits<T>::infinity(),
std::numeric_limits<T>::infinity())

A Rect that contains all points.


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