23#ifndef VCL_SPACE_CORE_BOX_H
24#define VCL_SPACE_CORE_BOX_H
28#include <vclib/concepts/space/box.h>
29#include <vclib/math/min_max.h>
47template<Po
intConcept Po
intT>
62 static const uint
DIM = PointT::DIM;
76 Box(
const PointT&
p) : mMin(
p), mMax(
p) {}
93 PointT&
min() {
return mMin; }
100 const PointT&
min()
const {
return mMin; }
107 PointT&
max() {
return mMax; }
114 const PointT&
max()
const {
return mMax; }
116 template<
typename Scalar>
119 if constexpr (std::is_same_v<typename PointT::ScalarType, Scalar>) {
123 return Box<Point<Scalar, PointT::DIM>>(
124 mMin.template cast<Scalar>(), mMax.template cast<Scalar>());
138 for (uint
i = 0;
i < PointT::DIM; ++
i) {
139 if (mMin[
i] > mMax[
i])
168 for (uint
i = 0;
i < PointT::DIM; ++
i) {
169 if (
p[
i] < mMin[
i] ||
p[
i] > mMax[
i])
188 for (uint
i = 0;
i < PointT::DIM; ++
i) {
189 if (
p[
i] <= mMin[
i] ||
p[
i] >= mMax[
i])
204 for (uint
i = 0;
i < PointT::DIM; ++
i) {
205 if (
p[
i] < mMin[
i] ||
p[
i] >= mMax[
i])
220 for (uint
i = 0;
i < PointT::DIM; ++
i) {
221 if (
b.mMin[
i] >= mMax[
i] ||
b.mMax[
i] <= mMin[
i])
262 PointT
center()
const {
return (mMax + mMin) / 2; }
270 PointT
size()
const {
return mMax - mMin; }
280 auto vol = mMax[0] - mMin[0];
282 for (uint
i = 1;
i < PointT::DIM; ++
i) {
283 vol *= mMax[
i] - mMin[
i];
298 return mMax[
i] - mMin[
i];
310 auto m = mMax[0] - mMin[0];
311 for (uint
i = 1;
i < PointT::DIM; ++
i) {
312 if (mMax[
i] - mMin[
i] <
m)
313 m = mMax[
i] - mMin[
i];
327 auto m = mMax[0] - mMin[0];
328 for (uint
i = 1;
i < PointT::DIM; ++
i) {
329 if (mMax[
i] - mMin[
i] >
m)
330 m = mMax[
i] - mMin[
i];
345 for (uint
i = 0;
i < PointT::DIM; ++
i) {
346 if (mMin[
i] <
b.mMin[
i]) {
350 if (mMax[
i] >
b.mMax[
i]) {
374 std::numeric_limits<typename PointT::ScalarType>::max());
376 std::numeric_limits<typename PointT::ScalarType>::lowest());
414 template<
typename Scalar>
415 void add(
const PointT&
p, Scalar radius)
421 *
this =
Box(
p - radius,
p + radius);
481 mMin.deserialize(is);
482 mMax.deserialize(is);
493 return mMin ==
b.mMin && mMax ==
b.mMax;
508using Box2 = Box<Point2<S>>;
510using Box2i = Box<Point2i>;
511using Box2f = Box<Point2f>;
512using Box2d = Box<Point2d>;
515using Box3 = Box<Point3<S>>;
517using Box3i = Box<Point3i>;
518using Box3f = Box<Point3f>;
519using Box3d = Box<Point3d>;
A class representing a box in N-dimensional space.
Definition box.h:49
auto diagonal() const
Calculates the diagonal length of the box.
Definition box.h:249
const PointT & max() const
Returns a const reference to the maximum point of the box.
Definition box.h:114
PointT PointType
The type of point used to represent the corners of the box.
Definition box.h:57
Box(const PointT &p)
Initializes the box with the given point. The box is a valid empty box (meaning that the minimum and ...
Definition box.h:76
const PointT & min() const
Returns a const reference to the minimum point of the box.
Definition box.h:100
auto squaredDiagonal() const
Calculates the squared length of the diagonal of the box.
Definition box.h:255
bool intersects(const Box< PointT > &b) const
Same as Box::overlap.
Definition box.h:242
Box(const PointT &min, const PointT &max)
Initializes the box with the given minimum and maximum points.
Definition box.h:86
bool isInsideStrict(const PointT &p) const
Checks whether a given point is inside the box or not, bounds excluded.
Definition box.h:186
void deserialize(std::istream &is)
Deserializes the box from the given input stream.
Definition box.h:479
void translate(const PointT &p)
Translates the box by summing the values of p.
Definition box.h:459
PointT & max()
Returns a reference to the maximum point of the box.
Definition box.h:107
bool isInsideOpenBox(const PointT &p) const
Checks if a point is inside the open box (max bound excluded); e.g. p in [min, max).
Definition box.h:202
PointT & min()
Returns a reference to the minimum point of the box.
Definition box.h:93
void add(const PointT &p, Scalar radius)
Adds the given point to the current box, expanding this box by a radius around the point,...
Definition box.h:415
void add(const Box< PointT > &b)
Adds the given box to the current box, expanding this box in order that will contain also the b value...
Definition box.h:439
bool overlap(const Box< PointT > &b) const
Check if two boxes overlap.
Definition box.h:217
void add(const PointT &p)
Adds the given point to the current box, expanding this box in order to contain also the values of th...
Definition box.h:388
bool operator!=(const Box< PointT > &b) const
Compares the current box to another box for inequality.
Definition box.h:502
Box()
The Empty constructor of a box, initializes a null box.
Definition box.h:68
static const uint DIM
The dimensionality of the box.
Definition box.h:62
auto minDim() const
Calculates the minimum dimension of the box, which is defined as the smallest difference between the ...
Definition box.h:308
bool isInside(const PointT &p) const
Checks whether a given point is inside the box or not, bounds included.
Definition box.h:166
bool collide(const Box< PointT > &b) const
Same as Box::overlap.
Definition box.h:234
PointT center() const
Calculates the center point of the box.
Definition box.h:262
Box< PointT > intersection(const Box< PointT > &b) const
Computes and returns the intersection between the current box and the given box b.
Definition box.h:342
auto maxDim() const
Computes the maximum dimension of the box, which is defined as the greater difference between the max...
Definition box.h:325
bool isEmpty() const
Checks whether the box is empty or not.
Definition box.h:153
void serialize(std::ostream &os) const
Serializes the box to the given output stream.
Definition box.h:469
auto dim(uint i) const
Get the length of the box along a given dimension.
Definition box.h:295
auto volume() const
Computes the volume of the box.
Definition box.h:277
PointT size() const
Computes the size of the box.
Definition box.h:270
bool operator==(const Box< PointT > &b) const
Compares the current box to another box for equality.
Definition box.h:491
bool isNull() const
Checks whether the box is null or not.
Definition box.h:136
void setNull()
Sets the Box to null. A box is considered null if at least one min component is greater than the corr...
Definition box.h:371
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43
constexpr auto max(const T &p1, const T &p2)
Returns the maximum between the two parameters.
Definition min_max.h:83
constexpr auto min(const T &p1, const T &p2)
Returns the minimum between the two parameters.
Definition min_max.h:42