20 #if !defined(__MITSUBA_CORE_AABB_H_)
21 #define __MITSUBA_CORE_AABB_H_
36 template <
typename T>
struct TAABB {
65 : min(min), max(max) {
66 #if defined(MTS_DEBUG)
67 for (
int i=0; i<PointType::dim; ++i)
74 : min(aabb.min), max(aabb.max) { }
78 return min == aabb.
min && max == aabb.
max;
83 return min != aabb.
min || max != aabb.
max;
88 for (
int i=0; i<PointType::dim; ++i) {
89 min[i] = std::max(min[i], aabb.
min[i]);
90 max[i] = std::min(max[i], aabb.
max[i]);
102 min =
PointType( std::numeric_limits<Scalar>::infinity());
103 max =
PointType(-std::numeric_limits<Scalar>::infinity());
110 for (
int i=1; i<PointType::dim; ++i)
119 for (
int i=0; i<PointType::dim; ++i) {
121 for (
int j=0; j<PointType::dim; ++j) {
128 return 2.0f * result;
133 return (max + min) * (
Scalar) 0.5;
139 for (
int d=0; d<PointType::dim; ++d) {
140 if (index & (1 << d))
150 TAABB result(getCenter());
152 for (
int d=0; d<PointType::dim; ++d) {
153 if (index & (1 << d))
154 result.
max[d] = max[d];
156 result.
min[d] = min[d];
164 for (
int i=0; i<PointType::dim; ++i)
165 if (p[i] < min[i] || p[i] > max[i])
174 for (
int i=0; i<PointType::dim; ++i)
175 if (aabb.
min[i] < min[i] || aabb.
max[i] > max[i])
182 for (
int i=0; i<PointType::dim; ++i)
183 if (max[i] < aabb.
min[i] || min[i] > aabb.
max[i])
190 for (
int i=0; i<PointType::dim; ++i) {
191 min[i] = std::min(min[i], p[i]);
192 max[i] = std::max(max[i], p[i]);
198 for (
int i=0; i<PointType::dim; ++i) {
199 min[i] = std::min(min[i], aabb.
min[i]);
200 max[i] = std::max(max[i], aabb.
max[i]);
207 for (
int i=0; i<PointType::dim; ++i) {
210 value = min[i] - p[i];
211 else if (p[i] > max[i])
212 value = p[i] - max[i];
213 result += value*value;
220 return std::sqrt(squaredDistanceTo(p));
227 for (
int i=0; i<PointType::dim; ++i) {
229 if (aabb.
max[i] < min[i])
230 value = min[i] - aabb.
max[i];
231 else if (aabb.
min[i] > max[i])
232 value = aabb.
min[i] - max[i];
233 result += value*value;
240 return std::sqrt(squaredDistanceTo(aabb));
245 for (
int i=0; i<PointType::dim; ++i)
259 for (
int i=0; i<PointType::dim; ++i) {
271 for (
int i=1; i<PointType::dim; ++i)
272 if (d[i] > d[largest])
282 for (
int i=1; i<PointType::dim; ++i)
283 if (d[i] < d[shortest])
309 nearT = -std::numeric_limits<Float>::infinity();
310 farT = std::numeric_limits<Float>::infinity();
313 for (
int i=0; i<PointType::dim; i++) {
314 const Float origin = ray.
o[i];
315 const Float minVal = min[i], maxVal = max[i];
319 if (origin < minVal || origin > maxVal)
323 Float t1 = (minVal - origin) * ray.
dRcp[i];
324 Float t2 = (maxVal - origin) * ray.
dRcp[i];
329 nearT = std::max(t1, nearT);
330 farT = std::min(t2, farT);
332 if (!(nearT <= farT))
357 int nearAxis = -1, farAxis = -1;
360 for (
int i=0; i<PointType::dim; i++) {
361 const Float origin = ray.
o[i];
362 const Float minVal = min[i], maxVal = max[i];
366 if (origin < minVal || origin > maxVal)
370 Float t1 = (minVal - origin) * ray.
dRcp[i];
371 Float t2 = (maxVal - origin) * ray.
dRcp[i];
379 nearAxis = flip ? (i + PointType::dim) : i;
384 farAxis = flip ? i : (i + PointType::dim);
389 if (!(nearT <= farT))
392 near = ray(nearT); far = ray(farT);
396 near[nearAxis % PointType::dim] = ((
Float *)
this)[nearAxis];
399 far[farAxis % PointType::dim] = ((
Float *)
this)[farAxis];
406 min.serialize(stream);
407 max.serialize(stream);
412 std::ostringstream oss;
413 oss <<
"AABB" << PointType::dim <<
"[";
417 oss <<
"min=" << min.toString()
418 <<
", max=" << max.toString();
466 return (
Float) 2.0 * (d.x*d.y + d.x*d.z + d.y*d.z);
TAABB(Stream *stream)
Unserialize a bounding box from a binary data stream.
Definition: aabb.h:54
TAABB(const TAABB &aabb)
Copy constructor.
Definition: aabb.h:73
Float getSurfaceArea() const
Calculate the n-1 dimensional volume of the boundary.
Definition: aabb.h:116
bool operator==(const TAABB &aabb) const
Equality test.
Definition: aabb.h:77
FINLINE bool rayIntersect(const RayType &ray, Float &nearT, Float &farT, PointType &near, PointType &far) const
Calculate the overlap between an axis-aligned bounding box and a ray segment.
Definition: aabb.h:356
bool isEmpty() const
Return whether or not this bounding box covers anything at all.
Definition: aabb.h:258
FINLINE __m128 rayIntersectPacket(const TriAccel &tri, const RayPacket4 &packet, __m128 mint, __m128 maxt, __m128 inactive, Intersection4 &its)
Definition: triaccel_sse.h:27
PointType max
Component-wise maximum.
Definition: aabb.h:425
T PointType
Definition: aabb.h:37
Generic multi-dimensional bounding box data structure.
Definition: aabb.h:36
void expandBy(const PointType &p)
Expand the bounding box to contain another point.
Definition: aabb.h:189
AABB(const PointType &min, const PointType &max)
Create a bounding box from two positions.
Definition: aabb.h:455
void serialize(Stream *stream) const
Serialize this bounding box to a binary data stream.
Definition: aabb.h:405
void reset()
Mark the bounding box as invalid.
Definition: aabb.h:101
void clip(const TAABB &aabb)
Clip to another bounding box.
Definition: aabb.h:87
std::string toString() const
Return a string representation of the bounding box.
Definition: aabb.h:411
Float getSurfaceArea() const
Calculate the surface area of the bounding box.
Definition: aabb.h:464
Bounding sphere data structure in three dimensions.
Definition: bsphere.h:32
#define MTS_EXPORT_CORE
Definition: getopt.h:29
Scalar distanceTo(const TAABB &aabb) const
Calculate the minimum AABB-AABB distance.
Definition: aabb.h:239
TAABB()
Create a new invalid bounding box.
Definition: aabb.h:49
T::Scalar Scalar
Definition: aabb.h:38
VectorType d
Ray direction.
Definition: ray.h:46
PointType o
Ray origin.
Definition: ray.h:44
TAABB getChild(int index) const
Return a child bounding box in a interval-, quad-, octree, etc.
Definition: aabb.h:149
bool contains(const TAABB &aabb) const
Check whether a given bounding box is contained within this one.
Definition: aabb.h:171
AABB(const Point &p)
Create a collapsed AABB from a single point.
Definition: aabb.h:452
AABB(const TAABB< Point > &aabb)
Construct from a TAABB<Point>
Definition: aabb.h:460
Axis-aligned bounding box data structure in three dimensions.
Definition: aabb.h:437
Simple n-dimensional ray data structure with minimum / maximum extent information.
Definition: ray.h:36
void expandBy(const TAABB &aabb)
Expand the bounding box to contain another bounding box.
Definition: aabb.h:197
SIMD quad-packed ray for coherent ray tracing.
Definition: ray_sse.h:34
#define SAssert(cond)
``Static'' assertion (to be used outside of classes that derive from Object)
Definition: logger.h:79
TRay< PointType, VectorType > RayType
Definition: aabb.h:40
Abstract seekable stream class.
Definition: stream.h:58
bool overlaps(const TAABB &aabb) const
Axis-aligned bounding box overlap test.
Definition: aabb.h:181
Scalar squaredDistanceTo(const PointType &p) const
Calculate the squared point-AABB distance.
Definition: aabb.h:205
AABB()
Create a new invalid bounding box.
Definition: aabb.h:446
PointType getCenter() const
Return the center point.
Definition: aabb.h:132
AABB(Stream *stream)
Unserialize a bounding box from a binary data stream.
Definition: aabb.h:449
VectorType getExtents() const
Calculate the bounding box extents.
Definition: aabb.h:292
bool operator!=(const TAABB &aabb) const
Inequality test.
Definition: aabb.h:82
Scalar distanceTo(const PointType &p) const
Calculate the point-AABB distance.
Definition: aabb.h:219
PointType min
Component-wise minimum.
Definition: aabb.h:424
int getLargestAxis() const
Return the axis index with the largest associated side length.
Definition: aabb.h:267
PointType getCorner(int index) const
Return the position of one of the corners (in 0..2^dim-1)
Definition: aabb.h:137
bool contains(const PointType &p) const
Check whether a point lies on or inside the bounding box.
Definition: aabb.h:163
int getShortestAxis() const
Return the axis index with the shortest associated side length.
Definition: aabb.h:278
bool isValid() const
Return whether this bounding box is valid.
Definition: aabb.h:244
Scalar getVolume() const
Calculate the n-dimensional volume of the bounding box.
Definition: aabb.h:107
TAABB(const PointType &min, const PointType &max)
Create a bounding box from two positions.
Definition: aabb.h:64
TAABB(const PointType &p)
Create a collapsed AABB from a single point.
Definition: aabb.h:60
T::VectorType VectorType
Definition: aabb.h:39
VectorType dRcp
Componentwise reciprocals of the ray direction.
Definition: ray.h:48
Scalar squaredDistanceTo(const TAABB &aabb) const
Calculate the minimum squared AABB-AABB distance.
Definition: aabb.h:224
FINLINE bool rayIntersect(const RayType &ray, Float &nearT, Float &farT) const
Calculate the near and far ray-AABB intersection points (if they exist).
Definition: aabb.h:308