diff --git a/src/core/include/pw/core/aabb.hpp b/src/core/include/pw/core/aabb.hpp index 712a15a..e447a48 100644 --- a/src/core/include/pw/core/aabb.hpp +++ b/src/core/include/pw/core/aabb.hpp @@ -24,6 +24,7 @@ #define PW_CORE_AABB_HPP #include "vector.hpp" +#include #include #include @@ -43,12 +44,25 @@ template struct aabb final { static constexpr auto make( const std::vector>& vertices) -> aabb { - return std::accumulate(std::begin(vertices), std::end(vertices), - aabb{.min{vertices.front()}, .max{vertices.front()}}, - [](const auto& prev, const auto& e) { + return std::accumulate( + std::begin(vertices), std::end(vertices), + aabb{.min{vertices.front()}, .max{vertices.front()}}, + [](const auto& prev, const auto& e) { return aabb{.min{e.min(prev.min)}, .max{e.max(prev.max)}}; }); } + + static constexpr auto make_from_indexed_vertices( + const std::vector>& vertices, + std::vector& indices) -> aabb { + return std::accumulate(std::begin(indices), std::end(indices), + aabb{.min{vertices[indices.front()]}, + .max{vertices[indices.front()]}}, + [&vertices](const auto& prev, const auto& e) { + return aabb{.min{vertices[e].min(prev.min)}, + .max{vertices[e].max(prev.max)}}; + }); + } }; } // namespace pw diff --git a/src/core/tests/pwcore_test_aabb.cpp b/src/core/tests/pwcore_test_aabb.cpp index cd1d078..aeb2017 100644 --- a/src/core/tests/pwcore_test_aabb.cpp +++ b/src/core/tests/pwcore_test_aabb.cpp @@ -1,3 +1,5 @@ +#include +#include #include #include @@ -5,8 +7,13 @@ auto main() -> int { - auto vertices = std::vector>{ - {-1.f, 1.f, 0}, {2.f, -1.f, 3.f}, {0, 0, -1.f}}; + auto vertices = + std::vector>{{-1.f, 1.f, 0}, + {2.f, -1.f, 3.f}, + {0, 0, -1.f}, + {-1'000.f, 2'000.f, 3'000.f}}; + + auto indices = std::vector{0, 1, 2}; auto aabb_1 = pw::aabb int { std::print("aabb::max -> {} {} {}\n", aabb_1.max.x(), aabb_1.max.y(), aabb_1.max.z()); + + auto aabb_2 = pw::aabb:: + make_from_indexed_vertices(vertices, indices); + + std::print("aabb::min -> {} {} {}\n", aabb_2.min.x(), aabb_2.min.y(), + aabb_2.min.z()); + + std::print("aabb::max -> {} {} {}\n", aabb_2.max.x(), aabb_2.max.y(), + aabb_2.max.z()); }