#pragma once #include #include "Vector.hpp" struct AABB { AABB() = default; AABB(Vector<3> min, Vector<3> max) : min(min), max(max) {} explicit AABB(Vector<3> max) : max(max) {} Vector<3> size() const { return max - min; } Vector<3> center() const { return (min + max) / 2; } Bool contains(Vector<3> point) const { return point.x() >= min.x() && point.x() <= max.x() && point.y() >= min.y() && point.y() <= max.y() && point.z() >= min.z() && point.z() <= max.z(); } Bool intersects_on_x(AABB other) const { return min.x() <= other.max.x() && max.x() >= other.min.x(); } Bool intersects_on_y(AABB other) const { return min.y() <= other.max.y() && max.y() >= other.min.y(); } Bool intersects_on_z(AABB other) const { return min.z() <= other.max.z() && max.z() >= other.min.z(); } Bool collides(AABB other) const { return intersects_on_x(other) && intersects_on_y(other) && intersects_on_z(other); } std::array, 8> corners() const { return {{ {min.x(), min.y(), min.z()}, {min.x(), min.y(), max.z()}, {min.x(), max.y(), min.z()}, {min.x(), max.y(), max.z()}, {max.x(), min.y(), min.z()}, {max.x(), min.y(), max.z()}, {max.x(), max.y(), min.z()}, {max.x(), max.y(), max.z()}, }}; } AABB offset(Vector<3> by) const { return {min + by, max + by}; } AABB sum(AABB with) const { auto new_size = size() + with.size(); return {center() - new_size / 2, center() + new_size / 2}; } AABB cast_box(Vector<3> v, AABB against) const; Vector<3> min, max; };