51 lines
1.3 KiB
C++
51 lines
1.3 KiB
C++
#ifndef AABB_H
|
|
#define AABB_H
|
|
|
|
#include <cmath>
|
|
|
|
#include "vec3.h"
|
|
|
|
class Aabb {
|
|
public:
|
|
Aabb() {}
|
|
Aabb(const Point3& a, const Point3& b) {
|
|
_min = a;
|
|
_max = b;
|
|
}
|
|
|
|
Point3 min() const { return _min; }
|
|
Point3 max() const { return _max; }
|
|
|
|
bool hit(const Ray& r, double tmin, double tmax) const {
|
|
for (int a = 0; a < 3; a++) {
|
|
auto invD = 1.0f / r.direction()[a];
|
|
auto t0 = (min()[a] - r.origin()[a]) * invD;
|
|
auto t1 = (max()[a] - r.origin()[a]) * invD;
|
|
if (invD < 0.0f)
|
|
std::swap(t0, t1);
|
|
tmin = t0 > tmin ? t0 : tmin;
|
|
tmax = t1 < tmax ? t1 : tmax;
|
|
if (tmax <= tmin)
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private:
|
|
Point3 _min;
|
|
Point3 _max;
|
|
};
|
|
|
|
inline Aabb surrounding_box(Aabb box0, Aabb box1) {
|
|
Point3 small(std::fmin(box0.min().x(), box1.min().x()),
|
|
std::fmin(box0.min().y(), box1.min().y()),
|
|
std::fmin(box0.min().z(), box1.min().z()));
|
|
|
|
Point3 big(std::fmax(box0.max().x(), box1.max().x()),
|
|
std::fmax(box0.max().y(), box1.max().y()),
|
|
std::fmax(box0.max().z(), box1.max().z()));
|
|
return Aabb(small, big);
|
|
}
|
|
|
|
#endif // AABB_H
|