added a more useful function to calculate the AABB
This commit is contained in:
parent
4547b9c6f8
commit
c58f81d640
2 changed files with 36 additions and 5 deletions
|
@ -24,6 +24,7 @@
|
||||||
#define PW_CORE_AABB_HPP
|
#define PW_CORE_AABB_HPP
|
||||||
|
|
||||||
#include "vector.hpp"
|
#include "vector.hpp"
|
||||||
|
#include <cstddef>
|
||||||
#include <numeric>
|
#include <numeric>
|
||||||
#include <pw/core/vector.hpp>
|
#include <pw/core/vector.hpp>
|
||||||
|
|
||||||
|
@ -43,12 +44,25 @@ template <typename Scalar, std::size_t N> struct aabb final {
|
||||||
|
|
||||||
static constexpr auto make(
|
static constexpr auto make(
|
||||||
const std::vector<pw::vector<Scalar, N>>& vertices) -> aabb<Scalar, N> {
|
const std::vector<pw::vector<Scalar, N>>& vertices) -> aabb<Scalar, N> {
|
||||||
return std::accumulate(std::begin(vertices), std::end(vertices),
|
return std::accumulate(
|
||||||
aabb{.min{vertices.front()}, .max{vertices.front()}},
|
std::begin(vertices), std::end(vertices),
|
||||||
[](const auto& prev, const auto& e) {
|
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)}};
|
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
|
} // namespace pw
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
#include <cstddef>
|
||||||
|
#include <iterator>
|
||||||
#include <pw/core/aabb.hpp>
|
#include <pw/core/aabb.hpp>
|
||||||
|
|
||||||
#include <print>
|
#include <print>
|
||||||
|
@ -5,8 +7,13 @@
|
||||||
|
|
||||||
auto main() -> int {
|
auto main() -> int {
|
||||||
|
|
||||||
auto vertices = std::vector<pw::vector<float, 3>>{
|
auto vertices =
|
||||||
{-1.f, 1.f, 0}, {2.f, -1.f, 3.f}, {0, 0, -1.f}};
|
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 =
|
auto aabb_1 =
|
||||||
pw::aabb<decltype(vertices)::value_type::value_type,
|
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(),
|
std::print("aabb::max -> {} {} {}\n", aabb_1.max.x(), aabb_1.max.y(),
|
||||||
aabb_1.max.z());
|
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());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue