2020-07-30 10:06:47 -04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
struct AABB {
|
|
|
|
glm::vec3 min, max;
|
2022-08-16 07:41:12 -04:00
|
|
|
|
2020-07-30 10:06:47 -04:00
|
|
|
glm::vec3 center() const {
|
|
|
|
return 0.5f * (max + min);
|
|
|
|
}
|
2022-08-16 07:41:12 -04:00
|
|
|
|
2020-07-30 10:06:47 -04:00
|
|
|
glm::vec3 extent() const {
|
|
|
|
return max - center();
|
|
|
|
}
|
2022-08-16 07:41:12 -04:00
|
|
|
|
2020-07-30 10:06:47 -04:00
|
|
|
bool contains(const glm::vec3 point) const {
|
|
|
|
return glm::all(glm::lessThan(point, max)) && glm::all(glm::greaterThan(point, min));
|
|
|
|
}
|
2022-08-16 07:41:12 -04:00
|
|
|
|
2020-07-30 10:06:47 -04:00
|
|
|
bool inside(const AABB extent) const {
|
2022-08-16 07:41:12 -04:00
|
|
|
return (
|
|
|
|
max.x > extent.min.x && min.x < extent.max.x && max.y > extent.min.y && min.y < extent.max.y &&
|
|
|
|
max.z > extent.min.z && min.z < extent.max.z);
|
2020-07-30 10:06:47 -04:00
|
|
|
}
|
2022-08-16 07:41:12 -04:00
|
|
|
|
2020-07-30 10:06:47 -04:00
|
|
|
bool contains(const Ray& ray) const {
|
|
|
|
const float t1 = (min.x - ray.origin.x) / ray.direction.x;
|
|
|
|
const float t2 = (max.x - ray.origin.x) / ray.direction.x;
|
|
|
|
const float t3 = (min.y - ray.origin.y) / ray.direction.y;
|
|
|
|
const float t4 = (max.y - ray.origin.y) / ray.direction.y;
|
|
|
|
const float t5 = (min.z - ray.origin.z) / ray.direction.z;
|
|
|
|
const float t6 = (max.z - ray.origin.z) / ray.direction.z;
|
2022-08-16 07:41:12 -04:00
|
|
|
|
2020-07-30 10:06:47 -04:00
|
|
|
const float tmin = std::min(std::max(std::min(t1, t2), std::min(t3, t4)), std::min(t5, t6));
|
|
|
|
const float tmax = std::min(std::min(std::max(t1, t2), std::max(t3, t4)), std::max(t5, t6));
|
2022-08-16 07:41:12 -04:00
|
|
|
|
2020-07-30 10:06:47 -04:00
|
|
|
// if tmax < 0, ray (line) is intersecting AABB, but whole AABB is behing us
|
2022-08-16 07:41:12 -04:00
|
|
|
// if(tmax < 0)
|
2020-07-31 22:52:16 -04:00
|
|
|
// return false;
|
2022-08-16 07:41:12 -04:00
|
|
|
|
2020-07-30 10:06:47 -04:00
|
|
|
// if tmin > tmax, ray doesn't intersect AABB
|
2022-08-16 07:41:12 -04:00
|
|
|
if (tmin > tmax)
|
2020-07-30 10:06:47 -04:00
|
|
|
return false;
|
2022-08-16 07:41:12 -04:00
|
|
|
|
|
|
|
if (tmin < 0.0f)
|
2020-07-30 10:06:47 -04:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|