added slicing to vectors and matrices
This commit is contained in:
parent
d2163ddd09
commit
70d002f57c
3 changed files with 19 additions and 27 deletions
|
@ -195,33 +195,10 @@ struct matrix final {
|
|||
template <std::size_t RowsSlice, std::size_t ColsSlice>
|
||||
constexpr auto slice(std::size_t r = 0, std::size_t c = 0) const noexcept
|
||||
-> matrix<Scalar, RowsSlice, ColsSlice> {
|
||||
// clang-format off
|
||||
return {
|
||||
// rows in target
|
||||
[&r, &c, this]<std::size_t... Rs>(std::index_sequence<Rs...>) {
|
||||
// cols in target
|
||||
return ([&r,&c,this]<std::size_t... Cs>(auto&& Ri,std::index_sequence<Cs...>) {
|
||||
return (*this)[Ri].swizzle(std::forward<std::size_t>(Cs)...); // expand row and cols
|
||||
}(std::forward<std::size_t>(Rs), std::make_index_sequence<ColsSlice>{}), ...); // fold over cols with current row
|
||||
}(std::make_index_sequence<RowsSlice>{}); // rowwise expansion
|
||||
};
|
||||
// clang-format on
|
||||
}
|
||||
|
||||
template <std::size_t RowsSlice, std::size_t ColsSlice>
|
||||
constexpr void slice_test(std::size_t r = 0,
|
||||
std::size_t c = 0) const noexcept {
|
||||
// clang-format off
|
||||
[&r, &c, this]<std::size_t... Rs>(std::index_sequence<Rs...>) { // rows in target
|
||||
([&r,&c,this]<std::size_t... Cs>(auto&& Ri, std::index_sequence<Cs...>) { // cols in target
|
||||
|
||||
// (std::print("[{} {}]",Ri,Cs),...);
|
||||
std::print("row:{} cols:[{} {}]",Ri,Cs...);
|
||||
std::print("\n");
|
||||
|
||||
}(std::forward<std::size_t>(Rs), std::make_index_sequence<ColsSlice>{}), ...); // fold over cols with current row
|
||||
return [&r, &c, this]<std::size_t... Rs>(std::index_sequence<Rs...>) {
|
||||
return matrix<Scalar, RowsSlice, ColsSlice>{(*this)[r + Rs].slice(
|
||||
std::make_index_sequence<ColsSlice>{}, c)...};
|
||||
}(std::make_index_sequence<RowsSlice>{}); // rowwise expansion
|
||||
// clang-format on
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -28,6 +28,7 @@
|
|||
#include <cmath>
|
||||
#include <concepts>
|
||||
#include <functional>
|
||||
#include <utility>
|
||||
|
||||
namespace pw {
|
||||
|
||||
|
@ -83,12 +84,18 @@ template <typename Scalar, std::size_t N> struct vector final {
|
|||
}(std::make_index_sequence<N>{});
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
template <std::integral... Args>
|
||||
constexpr auto swizzle(Args&&... indices) const noexcept
|
||||
-> vector<Scalar, sizeof...(Args)> {
|
||||
return {{Scalar{v_[indices]}...}};
|
||||
}
|
||||
|
||||
template <typename T, T... indices>
|
||||
constexpr auto slice(std::integer_sequence<T, indices...>,T offset = T{0}) const noexcept
|
||||
-> vector<Scalar, sizeof...(indices)> {
|
||||
return {{Scalar{v_[indices + offset]}...}};
|
||||
}
|
||||
|
||||
constexpr auto minor(std::unsigned_integral auto d0) const noexcept {
|
||||
return [this, &d0]<std::size_t... Ss>(std::index_sequence<Ss...>) {
|
||||
return vector<Scalar, N - 1>{
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
#include <pw/core/vector.hpp>
|
||||
|
||||
#include <print>
|
||||
#include <utility>
|
||||
|
||||
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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue