added a more useful function to calculate the AABB

This commit is contained in:
Hartmut Seichter 2024-07-16 15:45:03 +02:00
parent 4547b9c6f8
commit c58f81d640
2 changed files with 36 additions and 5 deletions

View file

@ -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

View file

@ -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());
}