From cfa27fd8eed42599195fdff6d7d0e25586e8f18c Mon Sep 17 00:00:00 2001 From: untodesu Date: Thu, 11 Sep 2025 16:00:57 +0500 Subject: Graft header-only math::AABB from qfengine --- core/math/CMakeLists.txt | 1 - core/math/aabb.cc | 59 ------------------ core/math/aabb.hh | 126 ++++++++++++++++++++++++++++++++++----- game/shared/entity/collision.cc | 6 +- game/shared/entity/collision.hh | 2 +- game/shared/world/CMakeLists.txt | 3 +- game/shared/world/chunk_aabb.cc | 54 ----------------- game/shared/world/chunk_aabb.hh | 25 +------- 8 files changed, 120 insertions(+), 156 deletions(-) delete mode 100644 core/math/aabb.cc delete mode 100644 game/shared/world/chunk_aabb.cc diff --git a/core/math/CMakeLists.txt b/core/math/CMakeLists.txt index 692492e..b994969 100644 --- a/core/math/CMakeLists.txt +++ b/core/math/CMakeLists.txt @@ -1,5 +1,4 @@ target_sources(core PRIVATE - "${CMAKE_CURRENT_LIST_DIR}/aabb.cc" "${CMAKE_CURRENT_LIST_DIR}/aabb.hh" "${CMAKE_CURRENT_LIST_DIR}/angles.hh" "${CMAKE_CURRENT_LIST_DIR}/concepts.hh" diff --git a/core/math/aabb.cc b/core/math/aabb.cc deleted file mode 100644 index 1111149..0000000 --- a/core/math/aabb.cc +++ /dev/null @@ -1,59 +0,0 @@ -#include "core/pch.hh" - -#include "core/math/aabb.hh" - -math::AABB::AABB(const glm::fvec3& min, const glm::fvec3& max) -{ - set_bounds(min, max); -} - -void math::AABB::set_bounds(const glm::fvec3& min, const glm::fvec3& max) -{ - this->min = min; - this->max = max; -} - -void math::AABB::set_offset(const glm::fvec3& base, const glm::fvec3& size) -{ - this->min = base; - this->max = base + size; -} - -bool math::AABB::contains(const glm::fvec3& point) const -{ - auto result = true; - result = result && (point.x >= min.x) && (point.x <= max.x); - result = result && (point.y >= min.y) && (point.y <= max.y); - result = result && (point.z >= min.z) && (point.z <= max.z); - return result; -} - -bool math::AABB::intersect(const AABB& other_box) const -{ - auto result = true; - result = result && (min.x < other_box.max.x) && (max.x > other_box.min.x); - result = result && (min.y < other_box.max.y) && (max.y > other_box.min.y); - result = result && (min.z < other_box.max.z) && (max.z > other_box.min.z); - return result; -} - -math::AABB math::AABB::combine_with(const math::AABB& other_box) const -{ - AABB result; - result.set_bounds(min, other_box.max); - return result; -} - -math::AABB math::AABB::multiply_with(const math::AABB& other_box) const -{ - AABB result; - result.set_bounds(other_box.min, max); - return result; -} - -math::AABB math::AABB::push(const glm::fvec3& vector) const -{ - AABB result; - result.set_bounds(min + vector, max + vector); - return result; -} diff --git a/core/math/aabb.hh b/core/math/aabb.hh index bff8e04..59a345a 100644 --- a/core/math/aabb.hh +++ b/core/math/aabb.hh @@ -1,28 +1,126 @@ #pragma once +#include "core/math/concepts.hh" + namespace math { -class AABB final { +template +class AABB { public: - AABB(void) = default; - explicit AABB(const glm::fvec3& min, const glm::fvec3& max); + using value_type = T; + using vector_type = glm::vec<3, T>; + + constexpr AABB(void) = default; + constexpr explicit AABB(const vector_type& start, const vector_type& end); virtual ~AABB(void) = default; - void set_bounds(const glm::fvec3& min, const glm::fvec3& max); - void set_offset(const glm::fvec3& base, const glm::fvec3& size); + constexpr void set_bounds(const vector_type& start, const vector_type& end); + constexpr void set_offset(const vector_type& base, const vector_type& size); - const glm::fvec3& get_min(void) const; - const glm::fvec3& get_max(void) const; + constexpr const vector_type& get_min(void) const; + constexpr const vector_type& get_max(void) const; - bool contains(const glm::fvec3& point) const; - bool intersect(const AABB& other_box) const; + constexpr bool contains(const vector_type& point) const; + constexpr bool intersect(const AABB& other_box) const; - AABB combine_with(const AABB& other_box) const; - AABB multiply_with(const AABB& other_box) const; - AABB push(const glm::fvec3& vector) const; + constexpr AABB combine(const AABB& other_box) const; + constexpr AABB multiply(const AABB& other_box) const; + constexpr AABB push(const vector_type& vector) const; public: - glm::fvec3 min; - glm::fvec3 max; + vector_type min {}; + vector_type max {}; }; } // namespace math + +namespace math +{ +using AABBf = AABB; +} // namespace math + +template +constexpr math::AABB::AABB(const vector_type& start, const vector_type& end) +{ + set_bounds(start, end); +} + +template +constexpr void math::AABB::set_bounds(const vector_type& start, const vector_type& end) +{ + min = start; + max = end; +} + +template +constexpr void math::AABB::set_offset(const vector_type& base, const vector_type& size) +{ + min = base; + max = base + size; +} + +template +constexpr const typename math::AABB::vector_type& math::AABB::get_min(void) const +{ + return min; +} + +template +constexpr const typename math::AABB::vector_type& math::AABB::get_max(void) const +{ + return max; +} + +template +constexpr bool math::AABB::contains(const vector_type& point) const +{ + auto result = true; + result = result && (point.x >= min.x) && (point.x <= max.x); + result = result && (point.y >= min.y) && (point.y <= max.y); + result = result && (point.z >= min.z) && (point.z <= max.z); + return result; +} + +template +constexpr bool math::AABB::intersect(const AABB& other_box) const +{ + auto result = true; + result = result && (min.x <= other_box.max.x) && (max.x >= other_box.min.x); + result = result && (min.y <= other_box.max.y) && (max.y >= other_box.min.y); + result = result && (min.z <= other_box.max.z) && (max.z >= other_box.min.z); + return result; +} + +template +constexpr math::AABB math::AABB::combine(const AABB& other_box) const +{ + AABB result; + result.min.x = glm::min(min.x, other_box.min.x); + result.min.y = glm::min(min.y, other_box.min.y); + result.min.z = glm::min(min.z, other_box.min.z); + result.max.x = glm::max(max.x, other_box.max.x); + result.max.y = glm::max(max.y, other_box.max.y); + result.max.z = glm::max(max.z, other_box.max.z); + return result; +} + +template +constexpr math::AABB math::AABB::multiply(const AABB& other_box) const +{ + AABB result; + result.min.x = glm::max(min.x, other_box.min.x); + result.min.y = glm::max(min.y, other_box.min.y); + result.min.z = glm::max(min.z, other_box.min.z); + result.max.x = glm::min(max.x, other_box.max.x); + result.max.y = glm::min(max.y, other_box.max.y); + result.max.z = glm::min(max.z, other_box.max.z); + return result; +} + +template +constexpr math::AABB math::AABB::push(const vector_type& vector) const +{ + AABB result; + result.min = min + vector; + result.max = max + vector; + return result; +} diff --git a/game/shared/entity/collision.cc b/game/shared/entity/collision.cc index dbe7d30..24f5d49 100644 --- a/game/shared/entity/collision.cc +++ b/game/shared/entity/collision.cc @@ -24,7 +24,7 @@ static int vgrid_collide(const world::Dimension* dimension, int d, entity::Colli const auto& ref_aabb = collision.aabb; const auto current_aabb = ref_aabb.push(transform.local); - auto next_aabb = math::AABB(current_aabb); + auto next_aabb = math::AABBf(current_aabb); next_aabb.min[d] += move; next_aabb.max[d] += move; @@ -60,7 +60,7 @@ static int vgrid_collide(const world::Dimension* dimension, int d, entity::Colli world::voxel_touch latch_touch = world::voxel_touch::NOTHING; glm::fvec3 latch_values = glm::fvec3(0.0f, 0.0f, 0.0f); world::voxel_surface latch_surface = world::voxel_surface::UNKNOWN; - math::AABB latch_vbox; + math::AABBf latch_vbox; for(auto i = dmin; i != dmax; i += ddir) { for(auto j = lpos_min[u]; j < lpos_max[u]; ++j) @@ -79,7 +79,7 @@ static int vgrid_collide(const world::Dimension* dimension, int d, entity::Colli continue; } - math::AABB vbox; + math::AABBf vbox; vbox.min = glm::fvec3(lpos); vbox.max = glm::fvec3(lpos) + 1.0f; diff --git a/game/shared/entity/collision.hh b/game/shared/entity/collision.hh index ca94665..95a9b86 100644 --- a/game/shared/entity/collision.hh +++ b/game/shared/entity/collision.hh @@ -10,7 +10,7 @@ class Dimension; namespace entity { struct Collision final { - math::AABB aabb; + math::AABBf aabb; public: // NOTE: entity::Collision::fixed_update must be called diff --git a/game/shared/world/CMakeLists.txt b/game/shared/world/CMakeLists.txt index 6b9c281..15d5b59 100644 --- a/game/shared/world/CMakeLists.txt +++ b/game/shared/world/CMakeLists.txt @@ -1,8 +1,7 @@ target_sources(shared PRIVATE + "${CMAKE_CURRENT_LIST_DIR}/chunk_aabb.hh" "${CMAKE_CURRENT_LIST_DIR}/chunk.cc" "${CMAKE_CURRENT_LIST_DIR}/chunk.hh" - "${CMAKE_CURRENT_LIST_DIR}/chunk_aabb.cc" - "${CMAKE_CURRENT_LIST_DIR}/chunk_aabb.hh" "${CMAKE_CURRENT_LIST_DIR}/dimension.cc" "${CMAKE_CURRENT_LIST_DIR}/dimension.hh" "${CMAKE_CURRENT_LIST_DIR}/feature.cc" diff --git a/game/shared/world/chunk_aabb.cc b/game/shared/world/chunk_aabb.cc deleted file mode 100644 index 634b230..0000000 --- a/game/shared/world/chunk_aabb.cc +++ /dev/null @@ -1,54 +0,0 @@ -#include "shared/pch.hh" - -#include "shared/world/chunk_aabb.hh" - -void world::ChunkAABB::set_bounds(const chunk_pos& min, const chunk_pos& max) -{ - this->min = min; - this->max = max; -} - -void world::ChunkAABB::set_offset(const chunk_pos& base, const chunk_pos& size) -{ - this->min = base; - this->max = base + size; -} - -bool world::ChunkAABB::contains(const chunk_pos& point) const -{ - auto result = true; - result = result && (point.x >= min.x) && (point.x <= max.x); - result = result && (point.y >= min.y) && (point.y <= max.y); - result = result && (point.z >= min.z) && (point.z <= max.z); - return result; -} - -bool world::ChunkAABB::intersect(const ChunkAABB& other_box) const -{ - auto result = true; - result = result && (min.x < other_box.max.x) && (max.x > other_box.min.x); - result = result && (min.y < other_box.max.y) && (max.y > other_box.min.y); - result = result && (min.z < other_box.max.z) && (max.z > other_box.min.z); - return result; -} - -world::ChunkAABB world::ChunkAABB::combine_with(const ChunkAABB& other_box) const -{ - ChunkAABB result; - result.set_bounds(min, other_box.max); - return result; -} - -world::ChunkAABB world::ChunkAABB::multiply_with(const ChunkAABB& other_box) const -{ - ChunkAABB result; - result.set_bounds(other_box.min, max); - return result; -} - -world::ChunkAABB world::ChunkAABB::push(const chunk_pos& vector) const -{ - ChunkAABB result; - result.set_bounds(min + vector, max + vector); - return result; -} diff --git a/game/shared/world/chunk_aabb.hh b/game/shared/world/chunk_aabb.hh index a9e0205..3a2d26f 100644 --- a/game/shared/world/chunk_aabb.hh +++ b/game/shared/world/chunk_aabb.hh @@ -2,32 +2,13 @@ #define SHARED_CHUNK_AABB 1 #pragma once +#include "core/math/aabb.hh" + #include "shared/types.hh" namespace world { -class ChunkAABB final { -public: - ChunkAABB(void) = default; - virtual ~ChunkAABB(void) = default; - - void set_bounds(const chunk_pos& min, const chunk_pos& max); - void set_offset(const chunk_pos& base, const chunk_pos& size); - - const chunk_pos& get_min(void) const; - const chunk_pos& get_max(void) const; - - bool contains(const chunk_pos& point) const; - bool intersect(const ChunkAABB& other_box) const; - - ChunkAABB combine_with(const ChunkAABB& other_box) const; - ChunkAABB multiply_with(const ChunkAABB& other_box) const; - ChunkAABB push(const chunk_pos& vector) const; - -public: - chunk_pos min; - chunk_pos max; -}; +using ChunkAABB = math::AABB; } // namespace world #endif // SHARED_CHUNK_AABB -- cgit