From 70d002f57c3c798a2abb0cd769d6c1c39b15cad9 Mon Sep 17 00:00:00 2001 From: Hartmut Seichter Date: Wed, 17 Jul 2024 23:36:16 +0200 Subject: [PATCH] added slicing to vectors and matrices --- src/core/include/pw/core/matrix.hpp | 29 +++------------------------ src/core/include/pw/core/vector.hpp | 9 ++++++++- src/core/tests/pwcore_test_matrix.cpp | 8 ++++++++ 3 files changed, 19 insertions(+), 27 deletions(-) diff --git a/src/core/include/pw/core/matrix.hpp b/src/core/include/pw/core/matrix.hpp index 92448a6..cdc9b0c 100644 --- a/src/core/include/pw/core/matrix.hpp +++ b/src/core/include/pw/core/matrix.hpp @@ -195,33 +195,10 @@ struct matrix final { template constexpr auto slice(std::size_t r = 0, std::size_t c = 0) const noexcept -> matrix { - // clang-format off - return { - // rows in target - [&r, &c, this](std::index_sequence) { - // cols in target - return ([&r,&c,this](auto&& Ri,std::index_sequence) { - return (*this)[Ri].swizzle(std::forward(Cs)...); // expand row and cols - }(std::forward(Rs), std::make_index_sequence{}), ...); // fold over cols with current row - }(std::make_index_sequence{}); // rowwise expansion - }; - // clang-format on - } - - template - constexpr void slice_test(std::size_t r = 0, - std::size_t c = 0) const noexcept { - // clang-format off - [&r, &c, this](std::index_sequence) { // rows in target - ([&r,&c,this](auto&& Ri, std::index_sequence) { // cols in target - - // (std::print("[{} {}]",Ri,Cs),...); - std::print("row:{} cols:[{} {}]",Ri,Cs...); - std::print("\n"); - - }(std::forward(Rs), std::make_index_sequence{}), ...); // fold over cols with current row + return [&r, &c, this](std::index_sequence) { + return matrix{(*this)[r + Rs].slice( + std::make_index_sequence{}, c)...}; }(std::make_index_sequence{}); // rowwise expansion - // clang-format on } }; diff --git a/src/core/include/pw/core/vector.hpp b/src/core/include/pw/core/vector.hpp index f409eba..2949e06 100644 --- a/src/core/include/pw/core/vector.hpp +++ b/src/core/include/pw/core/vector.hpp @@ -28,6 +28,7 @@ #include #include #include +#include namespace pw { @@ -83,12 +84,18 @@ template struct vector final { }(std::make_index_sequence{}); } - template + template constexpr auto swizzle(Args&&... indices) const noexcept -> vector { return {{Scalar{v_[indices]}...}}; } + template + constexpr auto slice(std::integer_sequence,T offset = T{0}) const noexcept + -> vector { + return {{Scalar{v_[indices + offset]}...}}; + } + constexpr auto minor(std::unsigned_integral auto d0) const noexcept { return [this, &d0](std::index_sequence) { return vector{ diff --git a/src/core/tests/pwcore_test_matrix.cpp b/src/core/tests/pwcore_test_matrix.cpp index c234d2a..884837e 100644 --- a/src/core/tests/pwcore_test_matrix.cpp +++ b/src/core/tests/pwcore_test_matrix.cpp @@ -4,6 +4,7 @@ #include #include +#include auto main() -> int { @@ -53,6 +54,13 @@ auto main() -> int { auto m22_slice = m33.slice<2, 2>(0, 0); + std::print("{}\n", typeid(m22_slice).name()); + + auto vvv = pw::vector { 1.f, 2, 3, 4 }; + + auto vvv_swizzle = vvv.slice(std::make_index_sequence<2>{}); + + pw::debug::d() << "matrix<3,3>.slice() -> \n" << pw::serialize::to_string(m33) << "\n to \n" << pw::serialize::to_string(m22_slice);