19 #if !defined(__NORI_BBOX_H)
44 Dimension = _PointType::Dimension
47 typedef _PointType PointType;
48 typedef typename PointType::Scalar Scalar;
49 typedef typename PointType::VectorType VectorType;
90 for (
int i=0; i<Dimension; ++i) {
92 for (
int j=0; j<Dimension; ++j) {
104 return (
max +
min) * (Scalar) 0.5f;
115 bool contains(
const PointType &p,
bool strict =
false)
const {
117 return (p.array() >
min.array()).all()
118 && (p.array() <
max.array()).all();
120 return (p.array() >=
min.array()).all()
121 && (p.array() <=
max.array()).all();
138 return (bbox.
min.array() >
min.array()).all()
139 && (bbox.
max.array() <
max.array()).all();
141 return (bbox.
min.array() >=
min.array()).all()
142 && (bbox.
max.array() <=
max.array()).all();
156 return (bbox.
min.array() <
max.array()).all()
157 && (bbox.
max.array() >
min.array()).all();
159 return (bbox.
min.array() <=
max.array()).all()
160 && (bbox.
max.array() >=
min.array()).all();
171 for (
int i=0; i<Dimension; ++i) {
174 value =
min[i] - p[i];
175 else if (p[i] >
max[i])
176 value = p[i] -
max[i];
177 result += value*value;
198 for (
int i=0; i<Dimension; ++i) {
201 value =
min[i] - bbox.
max[i];
202 else if (bbox.
min[i] >
max[i])
203 value = bbox.
min[i] -
max[i];
204 result += value*value;
228 return (
max.array() >=
min.array()).all();
233 return (
max.array() ==
min.array()).all();
238 return (
max.array() >
min.array()).all();
245 for (
int i=1; i<Dimension; ++i)
246 if (d[i] > d[largest])
255 for (
int i=1; i<Dimension; ++i)
256 if (d[i] < d[shortest])
283 min.setConstant( std::numeric_limits<Scalar>::infinity());
284 max.setConstant(-std::numeric_limits<Scalar>::infinity());
302 bbox1.
min.cwiseMin(bbox2.
min),
303 bbox1.
max.cwiseMax(bbox2.
max)
309 VectorType extents =
max-
min;
311 if (extents[0] >= extents[1] && extents[0] >= extents[2])
313 else if (extents[1] >= extents[0] && extents[1] >= extents[2])
322 for (
int i=0; i<Dimension; ++i)
323 result[i] = (index & (1 << i)) ?
max[i] :
min[i];
330 return "BoundingBox[invalid]";
332 return tfm::format(
"BoundingBox[min=%s, max=%s]",
min.toString(),
max.toString());
337 float nearT = -std::numeric_limits<float>::infinity();
338 float farT = std::numeric_limits<float>::infinity();
340 for (
int i=0; i<3; i++) {
341 float origin = ray.
o[i];
342 float minVal =
min[i], maxVal =
max[i];
345 if (origin < minVal || origin > maxVal)
348 float t1 = (minVal - origin) * ray.
dRcp[i];
349 float t2 = (maxVal - origin) * ray.
dRcp[i];
354 nearT = std::max(t1, nearT);
355 farT = std::min(t2, farT);
357 if (!(nearT <= farT))
362 return ray.
mint <= farT && nearT <= ray.
maxt;
367 nearT = -std::numeric_limits<float>::infinity();
368 farT = std::numeric_limits<float>::infinity();
370 for (
int i=0; i<3; i++) {
371 float origin = ray.
o[i];
372 float minVal =
min[i], maxVal =
max[i];
375 if (origin < minVal || origin > maxVal)
378 float t1 = (minVal - origin) * ray.
dRcp[i];
379 float t2 = (maxVal - origin) * ray.
dRcp[i];
384 nearT = std::max(t1, nearT);
385 farT = std::min(t2, farT);
387 if (!(nearT <= farT))
Generic n-dimensional bounding box data structure.
static TBoundingBox merge(const TBoundingBox &bbox1, const TBoundingBox &bbox2)
Merge two bounding boxes.
TBoundingBox()
Create a new invalid bounding box.
bool hasVolume() const
Check whether this bounding box has any associated volume.
bool contains(const TBoundingBox &bbox, bool strict=false) const
Check whether a specified bounding box lies on or within the current bounding box.
TBoundingBox(const PointType &p)
Create a collapsed bounding box from a single point.
bool isValid() const
Check whether this is a valid bounding box.
int getMajorAxis() const
Return the dimension index with the largest associated side length.
int getLargestAxis() const
Return the index of the largest axis.
void expandBy(const PointType &p)
Expand the bounding box to contain another point.
PointType getCenter() const
Return the center point.
Scalar distanceTo(const PointType &p) const
Calculate the smallest distance between the axis-aligned bounding box and the point p.
Scalar getVolume() const
Calculate the n-dimensional volume of the bounding box.
void reset()
Mark the bounding box as invalid.
int getMinorAxis() const
Return the dimension index with the shortest associated side length.
VectorType getExtents() const
Calculate the bounding box extents.
bool overlaps(const TBoundingBox &bbox, bool strict=false) const
Check two axis-aligned bounding boxes for possible overlap.
Scalar squaredDistanceTo(const TBoundingBox &bbox) const
Calculate the smallest square distance between the axis-aligned bounding box and bbox.
PointType getCorner(int index) const
Return the position of a bounding box corner.
void clip(const TBoundingBox &bbox)
Clip to another bounding box.
Scalar distanceTo(const TBoundingBox &bbox) const
Calculate the smallest distance between the axis-aligned bounding box and bbox.
std::string toString() const
Return a string representation of the bounding box.
bool contains(const PointType &p, bool strict=false) const
Check whether a point lies on or inside the bounding box.
bool operator==(const TBoundingBox &bbox) const
Test for equality against another bounding box.
PointType max
Component-wise maximum.
bool rayIntersect(const Ray3f &ray) const
Check if a ray intersects a bounding box.
PointType min
Component-wise minimum.
TBoundingBox(const PointType &min, const PointType &max)
Create a bounding box from two positions.
Scalar squaredDistanceTo(const PointType &p) const
Calculate the smallest squared distance between the axis-aligned bounding box and the point p.
bool isPoint() const
Check whether this bounding box has collapsed to a single point.
float getSurfaceArea() const
Calculate the n-1 dimensional volume of the boundary.
bool operator!=(const TBoundingBox &bbox) const
Test for inequality against another bounding box.
void expandBy(const TBoundingBox &bbox)
Expand the bounding box to contain another bounding box.
bool rayIntersect(const Ray3f &ray, float &nearT, float &farT) const
Return the overlapping region of the bounding box and an unbounded ray.
VectorType d
Ray direction.
Scalar mint
Minimum position on the ray segment.
Scalar maxt
Maximum position on the ray segment.
VectorType dRcp
Componentwise reciprocals of the ray direction.