geomc 1.0
A c++ linear algebra template library
Loading...
Searching...
No Matches
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, Rect< T, N > > SdfEvaluable< T, N, Rect< T, N > > Dimensional< T, N >

Public Types

using elem_t
 The coordinate type of this object.
 
using point_t
 The type of a point in this object's space.
 

Public Member Functions

constexpr Rect ()
 Construct an empty interval.
 
template<index_t... J>
requires ((J + ...) == N)
constexpr Rect (const Rect< T, J > &... r)
 Construct a Rect from lower-dimensional Rects.
 
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.
 
constexpr Rect (point_t p)
 Construct a Rect containing only the point p.
 
Rect< T, Nabs () const
 Normalize the sign of the region.
 
Rect< T, 1 > axis (size_t k) const
 Return the one dimensional range spanned along axis k.
 
Rect< T, Nbounds () const
 
point_t center () const
 Center point.
 
Rect< T, Ncentered_on (point_t c) const
 Place a copy at a new location.
 
point_t clip (point_t p) const
 Clamp the coordinates of p to lie within this Rect.
 
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.
 
bool contains (const Rect< T, N > &box) const
 Box containment.
 
bool contains (point_t pt) const
 Point containment test.
 
point_t convex_support (point_t d) const
 
Rect< T, Ndilated (point_t c) const
 Morphological dilation.
 
point_t dimensions () const
 Axial size.
 
dist2 (point_t p) const
 Squared distance to the interior of the Rect.
 
point_t face_areas () const
 Measure of the axial faces of this Rect.
 
Rect< T, Nfitted_to (Rect< T, N > other) const
 Return a rect having the same aspect (ratios of axial lengths) as this one, sized and positioned to minimally contain other.
 
Rect< T, 1 > intersect (const Ray< T, N > &r) const
 Ray-shape intersection test.
 
bool intersects (const Convex< T, N, Shape > &other) const
 Convex shape overlap test.
 
bool intersects (const Rect< T, N > &box) const
 Range intersection test.
 
bool intersects (const Sphere< T, N > &sph) const
 
bool intersects (const Transformed< Rect< T, N > > &box) const
 Transformed range intersection test.
 
bool is_empty () const
 Empty region test.
 
auto length () const
 Return a "normalized" length measure encoding the size of this box.
 
measure_boundary () const
 Measure of the boundary of this Rect.
 
measure_interior () const
 N-dimensional volume.
 
Rect< T, Nminkowski_sum (const Rect< T, N > &other) const
 Minkowski sum.
 
point_t normal (point_t p) const
 Outward-facing direction.
 
template<typename U, index_t M>
 operator Rect< U, M > () const
 Element-wise typecast.
 
bool operator!= (const Rect< T, N > &b) const
 Inequality test.
 
Rect< T, Noperator& (const Rect< T, N > &b) const
 Interval intersection.
 
Rect< T, N > & operator&= (const Rect< T, N > &b)
 Interval intersection.
 
template<typename... I>
requires (... and std::convertible_to<I, size_t>)
Rect< T, sizeof...(I)> operator() (I... indices) const
 Dimensionally slice a Rect.
 
template<index_t M>
Rect< T, M+Noperator* (const Rect< T, M > &r) const
 Interval cartesian product.
 
Rect< T, Noperator* (point_t a) const
 Scale transformation.
 
Rect< T, N > & operator*= (point_t a)
 Scale transformation.
 
Rect< T, Noperator+ (point_t dx) const
 Translation.
 
Rect< T, N > & operator+= (point_t dx)
 Translation.
 
Rect< T, Noperator- (const Rect< T, N > &other)
 Interval exclusion.
 
Rect< T, Noperator- (point_t dx) const
 Translation.
 
Rect< T, N > & operator-= (const Rect< T, N > &other)
 Interval exclusion.
 
Rect< T, N > & operator-= (point_t dx)
 Translation.
 
Rect< T, Noperator/ (point_t a) const
 Scale transformation.
 
Rect< T, N > & operator/= (point_t a)
 Scale transformation.
 
bool operator== (const Rect< T, N > &b) const
 Equality test.
 
Rect< T, Noperator| (const point_t &p)
 Point union.
 
Rect< T, Noperator| (const Rect< T, N > &b) const
 Interval union.
 
Rect< T, N > & operator|= (const point_t &p)
 Point union.
 
Rect< T, N > & operator|= (const Rect< T, N > &b)
 Interval union.
 
point_t project (point_t p) const
 Return the point on the surface of the shape which is nearest to p.
 
point_t remap (point_t s) const
 Map the unit interval to the region.
 
template<index_t M>
requires (N == 1 and M > 1)
Vec< T, M > remap (Vec< T, M > s) const
 Map the unit interval to the region, broadcasting to a different dimension.
 
sdf (point_t p) const
 Return the signed distance to the surface of the shape.
 
void set_corners (point_t corner1, point_t corner2)
 Reconstruct from corner points.
 
void set_dimensions (point_t dim)
 Change the size of the region, adjusting about its center.
 
point_t unmap (point_t p) const
 Find p's fractional position within this Rect.
 
template<index_t M>
requires (N == 1 and M > 1)
Vec< T, M > unmap (Vec< T, M > p) const
 Find p's fractional position within this Rect, broadcasting to a different dimension.
 

Static Public Member Functions

static constexpr bool admits_cusps ()
 
static bool contains (VecType< T, N > lo, VecType< T, N > hi, VecType< T, N > pt)
 Test whether a point is in the N-dimensional range [lo, hi].
 
static Rect< T, Nfrom_center (VecType< T, N > c, VecType< T, N > dims)
 Construct a Rect from a center point and extent.
 
static Rect< T, Nfrom_corners (point_t c1, point_t c2)
 Construct a Rect containing the two corners c1 and c2.
 
static Rect< T, Nfrom_edge (VecType< T, N > edge, VecType< T, N > dims)
 Construct a Rect from a corner point and an extent.
 
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.
 
static Rect< T, Nfrom_size (VecType< T, N > dims)
 Construct a Rect at the origin having dimensions dims.
 

Public Attributes

point_t hi
 Upper extremes.
 
point_t lo
 Lower extremes.
 

Static Public Attributes

static const Rect< T, Nempty
 A Rect that contains no points.
 
static const point_t endpoint_measure
 
static const Rect< T, Nfull
 A Rect that contains all points.
 
static constexpr index_t N
 The dimension of this object.
 
static const Rect< T, Nsigned_unit_interval
 A Rect covering the range [-1, 1] along all axes.
 
static const Rect< T, Nunit_interval
 A Rect covering the range [0, 1] along all axes.
 

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.

Member Typedef Documentation

◆ point_t

using point_t
inherited

The type of a point in this object's space.

An N-vector of T if N > 1, otherwise a T.

Constructor & Destructor Documentation

◆ Rect() [1/2]

template<typename T, index_t N>
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]

template<typename T, index_t N>
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

◆ abs()

template<typename T, index_t N>
Rect< T, N > abs ( ) const
inline

Normalize the sign of the region.

If any extent has negative sign (lo > hi), the lo and hi coordinates for that axis are swapped. The result is a non-empty Rect.

◆ center()

template<typename T, index_t N>
point_t center ( ) const
inline

Center point.

Returns
The center point of this region.

◆ centered_on()

template<typename T, index_t N>
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()

template<typename T, index_t N>
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]

template<typename T, index_t N>
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]

template<typename T, index_t N>
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]

template<typename T, index_t N>
static bool contains ( VecType< T, N > lo,
VecType< T, N > hi,
VecType< T, N > 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()

template<typename T, index_t N>
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()

template<typename T, index_t N>
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()

template<typename T, index_t N>
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.

◆ face_areas()

template<typename T, index_t N>
point_t face_areas ( ) const
inline

Measure of the axial faces of this Rect.

The ith component of the result is the area of the face perpendicular to the ith axis.

◆ fitted_to()

template<typename T, index_t N>
Rect< T, N > fitted_to ( Rect< T, N > other) const
inline

Return a rect having the same aspect (ratios of axial lengths) as this one, sized and positioned to minimally contain other.

The fitted box will be a rigid rescaling and translation of this box. No reflections or rotations will be performed. The signs of the dimensions will be the same as this Rect.

Not available for integral ranges, since in general the aspect can't be preserved.

Zero dimensions will be ignored and not adjusted. To expand the zero dimensions, take the union of other with the result.

Parameters
otherRect to contain.

◆ from_center()

template<typename T, index_t N>
static Rect< T, N > from_center ( VecType< T, N > c,
VecType< T, N > 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_corners()

template<typename T, index_t N>
static Rect< T, N > from_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.

◆ from_edge()

template<typename T, index_t N>
static Rect< T, N > from_edge ( VecType< T, N > edge,
VecType< T, N > dims )
inlinestatic

Construct a Rect from a corner point and an extent.

Parameters
edgeArbitrary 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()

template<typename T, index_t N>
template<typename PointIterator>
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.

◆ from_size()

template<typename T, index_t N>
static Rect< T, N > from_size ( VecType< T, N > dims)
inlinestatic

Construct a Rect at the origin having dimensions dims.

Parameters
dimsLengths of each axis.
Returns
Rect<T,N> A new Rect with its center at the origin.

◆ intersects() [1/3]

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/3]

template<typename T, index_t N>
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.

◆ intersects() [3/3]

template<typename T, index_t N>
bool intersects ( const Transformed< Rect< T, N > > & box) const
inline

Transformed range intersection test.

Alias for box.intersects(*this).

◆ is_empty()

template<typename T, index_t N>
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.

◆ length()

template<typename T, index_t N>
auto length ( ) const
inline

Return a "normalized" length measure encoding the size of this box.

Computed as the Nth root of the volume.

◆ measure_boundary()

template<typename T, index_t N>
T measure_boundary ( ) const
inline

Measure of the boundary of this Rect.

Measures the length/perimter/surface area of this Rect.

◆ measure_interior()

template<typename T, index_t N>
T measure_interior ( ) 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.

The volume of an empty or negative Rect is 0.

Returns
The volume of this region.

◆ minkowski_sum()

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

Minkowski sum.

Combine this Rect with another using the Minkowski sum.

◆ operator Rect< U, M >()

template<typename T, index_t N>
template<typename U, index_t M>
operator Rect< U, M > ( ) const
inline

Element-wise typecast.

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

◆ operator!=()

template<typename T, index_t N>
bool operator!= ( const 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&()

template<typename T, index_t N>
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&=()

template<typename T, index_t N>
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()()

template<typename T, index_t N>
template<typename... I>
requires (... and std::convertible_to<I, size_t>)
Rect< T, sizeof...(I)> operator() ( I... indices) const
inline

Dimensionally slice a Rect.

Return a Rect of dimension M by taking the range for each axis given by the arugments. For example, the result of r(0,2) will be a 2D Rect with the x and z ranges of this Rect.

◆ operator*() [1/2]

template<typename T, index_t N>
template<index_t M>
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]

template<typename T, index_t N>
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*=()

template<typename T, index_t N>
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+()

template<typename T, index_t N>
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+=()

template<typename T, index_t N>
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]

template<typename T, index_t N>
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]

template<typename T, index_t N>
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]

template<typename T, index_t N>
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]

template<typename T, index_t N>
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/()

template<typename T, index_t N>
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/=()

template<typename T, index_t N>
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==()

template<typename T, index_t N>
bool operator== ( const 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]

template<typename T, index_t N>
Rect< T, N > operator| ( const point_t & p)
inline

Point union.

Returns
A Rect fully containing this and the point p.

◆ operator|() [2/2]

template<typename T, index_t N>
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]

template<typename T, index_t N>
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]

template<typename T, index_t N>
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()

template<typename T, index_t N>
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() [1/2]

template<typename T, index_t N>
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.

See also
remap_transform()

◆ remap() [2/2]

template<typename T, index_t N>
template<index_t M>
requires (N == 1 and M > 1)
Vec< T, M > remap ( Vec< T, M > s) const
inline

Map the unit interval to the region, broadcasting to a different dimension.

Requires that N == 1.

Each coordinate of s is treated as a fraction within this 1-D Rect.

◆ set_corners()

template<typename T, index_t N>
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()

template<typename T, index_t N>
void set_dimensions ( point_t dim)
inline

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

Parameters
dimNew lengths along each axis.

◆ unmap() [1/2]

template<typename T, index_t N>
point_t unmap ( point_t p) const
inline

Find p's fractional position within this Rect.

Inverse operation of remap().

See also
unmap_transform()

◆ unmap() [2/2]

template<typename T, index_t N>
template<index_t M>
requires (N == 1 and M > 1)
Vec< T, M > unmap ( Vec< T, M > p) const
inline

Find p's fractional position within this Rect, broadcasting to a different dimension.

Requires that N == 1.

Each coordinate of the result is the fraction of the corresponding coordinate of p within this 1-D Rect.


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