mitsuba/src/libcore/aabb.cpp

71 lines
1.5 KiB
C++
Raw Normal View History

#include <mitsuba/core/aabb.h>
MTS_NAMESPACE_BEGIN
Point AABB::getCorner(uint8_t corner) const {
return Point(corner & 1 ? max.x : min.x,
corner & 2 ? max.y : min.y,
corner & 4 ? max.z : min.z);
}
Point AABB::getMidPoint() const {
return Point(
min.x + max.x,
min.y + max.y,
min.z + max.z
) * 0.5f;
}
bool AABB::contains(const Point &vec) const {
return isValid() &&
(vec.x >= min.x && vec.x <= max.x) &&
(vec.y >= min.y && vec.y <= max.y) &&
(vec.z >= min.z && vec.z <= max.z);
}
void AABB::expandBy(const Point &vec) {
min.x = std::min(min.x, vec.x);
min.y = std::min(min.y, vec.y);
min.z = std::min(min.z, vec.z);
max.x = std::max(max.x, vec.x);
max.y = std::max(max.y, vec.y);
max.z = std::max(max.z, vec.z);
}
void AABB::expandBy(const AABB &aabb) {
min.x = std::min(min.x, aabb.min.x);
min.y = std::min(min.y, aabb.min.y);
min.z = std::min(min.z, aabb.min.z);
max.x = std::max(max.x, aabb.max.x);
max.y = std::max(max.y, aabb.max.y);
max.z = std::max(max.z, aabb.max.z);
}
Float AABB::distanceTo(const Point &p) const {
Float result = 0;
for (int i=0; i<3; ++i) {
Float value = 0;
if (p[i] < min[i])
value = min[i] - p[i];
if (p[i] > max[i])
value = p[i] - max[i];
result += value*value;
}
return std::sqrt(result);
}
std::string AABB::toString() const {
std::ostringstream oss;
oss << "AABB[";
if (!isValid()) {
oss << "invalid";
} else {
oss << "min=" << min.toString()
<< ", max=" << max.toString();
}
oss << "]";
return oss.str();
}
MTS_NAMESPACE_END