diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2025-03-13 20:42:33 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2025-03-13 20:42:33 +0000 |
commit | b9771cbeb80e5b540587a01be145154612bbc83d (patch) | |
tree | 6dff7eb8ff1deaa4b11697d7b5a27fac8bff3432 /gfx/aabb.h | |
parent | Split core view definition out of Camera into Frustum (diff) | |
parent | Split Terrain::generateMeshes into smaller functions (diff) | |
download | ilt-main.tar.bz2 ilt-main.tar.xz ilt-main.zip |
Diffstat (limited to 'gfx/aabb.h')
-rw-r--r-- | gfx/aabb.h | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/gfx/aabb.h b/gfx/aabb.h new file mode 100644 index 0000000..ce15a0f --- /dev/null +++ b/gfx/aabb.h @@ -0,0 +1,41 @@ +#pragma once + +#include "maths.h" +#include <algorithm> +#include <tuple> + +template<Arithmetic T, glm::qualifier Q = glm::defaultp> class AxisAlignedBoundingBox { +public: + using V = glm::vec<3, T, Q>; + AxisAlignedBoundingBox() = default; + + AxisAlignedBoundingBox(const V & min, const V & max) : min {min}, max {max} { } + + AxisAlignedBoundingBox & + operator+=(const V & point) + { + min = glm::min(min, point); + max = glm::max(max, point); + return *this; + } + + AxisAlignedBoundingBox + operator-(const V & viewPoint) const + { + return {min - viewPoint, max - viewPoint}; + } + + [[nodiscard]] static AxisAlignedBoundingBox + fromPoints(auto && points) + { + using Limits = std::numeric_limits<T>; + static constexpr const auto INITIAL = std::make_pair(V {Limits::max()}, V {Limits::min()}); + return std::make_from_tuple<AxisAlignedBoundingBox<T, Q>>( + std::ranges::fold_left(points, INITIAL, [](const auto & prev, const auto & point) { + auto & [min, max] = prev; + return std::make_pair(glm::min(min, point), glm::max(max, point)); + })); + } + + V min {}, max {}; +}; |