|
| 1 | +/** |
| 2 | + * @file AABB.cpp |
| 3 | + * @author Edward Palmer |
| 4 | + * @date 2025-03-03 |
| 5 | + * |
| 6 | + * @copyright Copyright (c) 2025 |
| 7 | + * |
| 8 | + */ |
| 9 | + |
| 10 | +#include "AABB.hpp" |
| 11 | + |
| 12 | +#define swap(val1, val2) \ |
| 13 | + ({ \ |
| 14 | + double temp = (val1); \ |
| 15 | + (val1) = (val2); \ |
| 16 | + (val2) = temp; \ |
| 17 | + }) |
| 18 | + |
| 19 | + |
| 20 | +AABB::AABB() |
| 21 | +{ |
| 22 | + reset(); |
| 23 | +} |
| 24 | + |
| 25 | + |
| 26 | +AABB::AABB(Point3 min_, Point3 max_) : min(min_), max(max_) |
| 27 | +{ |
| 28 | +} |
| 29 | + |
| 30 | + |
| 31 | +void AABB::reset() |
| 32 | +{ |
| 33 | + min = point3(INFINITY, INFINITY, INFINITY); |
| 34 | + max = point3(-INFINITY, -INFINITY, -INFINITY); |
| 35 | +} |
| 36 | + |
| 37 | + |
| 38 | +void AABB::addPoint(Point3 pt) |
| 39 | +{ |
| 40 | + if (pt.x < min.x) min.x = pt.x; |
| 41 | + if (pt.x > max.x) max.x = pt.x; |
| 42 | + |
| 43 | + if (pt.y < min.y) min.y = pt.y; |
| 44 | + if (pt.y > max.y) max.y = pt.y; |
| 45 | + |
| 46 | + if (pt.z < min.z) min.z = pt.z; |
| 47 | + if (pt.z > max.z) max.z = pt.z; |
| 48 | +} |
| 49 | + |
| 50 | + |
| 51 | +AABB AABB::addBoundingBoxes(const AABB &box0, const AABB &box1) |
| 52 | +{ |
| 53 | + Point3 newMin = |
| 54 | + point3(std::min(box0.min.x, box1.min.x), std::min(box0.min.y, box1.min.y), std::min(box0.min.z, box1.min.z)); |
| 55 | + Point3 newMax = |
| 56 | + point3(std::max(box0.max.x, box1.max.x), std::max(box0.max.y, box1.max.y), std::max(box0.max.z, box1.max.z)); |
| 57 | + |
| 58 | + return AABB(newMin, newMax); |
| 59 | +} |
| 60 | + |
| 61 | + |
| 62 | +AABB AABB::operator+(const AABB &other) |
| 63 | +{ |
| 64 | + return addBoundingBoxes(*this, other); |
| 65 | +} |
| 66 | + |
| 67 | + |
| 68 | +bool AABB::hit(Ray *ray, double tmin, double tmax) |
| 69 | +{ |
| 70 | + Point3 origin = ray->origin; |
| 71 | + |
| 72 | + // Now test against x-direction. |
| 73 | + double invD = 1.0 / ray->direction.x; |
| 74 | + double t0 = (min.x - origin.x) * invD; |
| 75 | + double t1 = (max.x - origin.x) * invD; |
| 76 | + |
| 77 | + if (invD < 0.0) swap(t0, t1); |
| 78 | + |
| 79 | + tmin = std::max(t0, tmin); |
| 80 | + tmax = std::min(t1, tmax); |
| 81 | + |
| 82 | + if (tmax <= tmin) return false; |
| 83 | + |
| 84 | + // Now test against y-direction. |
| 85 | + invD = 1.0 / ray->direction.y; |
| 86 | + t0 = (min.y - origin.y) * invD; |
| 87 | + t1 = (max.y - origin.y) * invD; |
| 88 | + |
| 89 | + if (invD < 0.0) swap(t0, t1); |
| 90 | + |
| 91 | + tmin = std::max(t0, tmin); |
| 92 | + tmax = std::min(t1, tmax); |
| 93 | + |
| 94 | + if (tmax <= tmin) return false; |
| 95 | + |
| 96 | + // Now test against z-direction: |
| 97 | + invD = 1.0 / ray->direction.z; |
| 98 | + t0 = (min.z - origin.z) * invD; |
| 99 | + t1 = (max.z - origin.z) * invD; |
| 100 | + |
| 101 | + if (invD < 0.0) swap(t0, t1); |
| 102 | + |
| 103 | + tmin = std::max(t0, tmin); |
| 104 | + tmax = std::min(t1, tmax); |
| 105 | + |
| 106 | + if (tmax <= tmin) return false; |
| 107 | + |
| 108 | + return true; |
| 109 | +} |
0 commit comments