+std::ostream &operator <<(std::ostream &out, const AABB &box) {
+ return out << "AABB(" << box.min << ", " << box.max << ')';
+}
+
+std::ostream &operator <<(std::ostream &out, const Ray &ray) {
+ return out << "Ray(" << ray.orig << ", " << ray.dir << ')';
+}
+
+bool Intersection(
+ const Ray &ray,
+ const AABB &box,
+ float &dist
+) noexcept {
+ float t_min = 0.0f;
+ float t_max = std::numeric_limits<float>::infinity();
+ for (int i = 0; i < 3; ++i) {
+ float t1 = (box.min[i] - ray.orig[i]) * ray.inv_dir[i];
+ float t2 = (box.max[i] - ray.orig[i]) * ray.inv_dir[i];
+ t_min = std::max(t_min, std::min(t1, t2));
+ t_max = std::min(t_max, std::max(t1, t2));
+ }
+ dist = t_min;
+ return t_max >= t_min;
+}
+