From c58f81d64029b163097221df2de64e221a04e96a Mon Sep 17 00:00:00 2001
From: Hartmut Seichter <hartmut@technotecture.com>
Date: Tue, 16 Jul 2024 15:45:03 +0200
Subject: [PATCH] added a more useful function to calculate the AABB

---
 src/core/include/pw/core/aabb.hpp   | 20 +++++++++++++++++---
 src/core/tests/pwcore_test_aabb.cpp | 21 +++++++++++++++++++--
 2 files changed, 36 insertions(+), 5 deletions(-)

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 <cstddef>
 #include <numeric>
 #include <pw/core/vector.hpp>
 
@@ -43,12 +44,25 @@ template <typename Scalar, std::size_t N> struct aabb final {
 
     static constexpr auto make(
         const std::vector<pw::vector<Scalar, N>>& vertices) -> aabb<Scalar, N> {
-        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<pw::vector<Scalar, N>>& vertices,
+        std::vector<size_t>& indices) -> aabb<Scalar, N> {
+        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 <cstddef>
+#include <iterator>
 #include <pw/core/aabb.hpp>
 
 #include <print>
@@ -5,8 +7,13 @@
 
 auto main() -> int {
 
-    auto vertices = std::vector<pw::vector<float, 3>>{
-        {-1.f, 1.f, 0}, {2.f, -1.f, 3.f}, {0, 0, -1.f}};
+    auto vertices =
+        std::vector<pw::vector<float, 3>>{{-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<std::size_t>{0, 1, 2};
 
     auto aabb_1 =
         pw::aabb<decltype(vertices)::value_type::value_type,
@@ -17,4 +24,14 @@ auto main() -> int {
 
     std::print("aabb::max -> {} {} {}\n", aabb_1.max.x(), aabb_1.max.y(),
                aabb_1.max.z());
+
+    auto aabb_2 = pw::aabb<decltype(vertices)::value_type::value_type,
+                           decltype(vertices)::value_type::coefficients>::
+        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());
 }