diagonal ext function

This commit is contained in:
BlackMATov
2021-02-27 08:44:28 +07:00
parent 05cc7f0bf2
commit 7fdd065136
5 changed files with 52 additions and 11 deletions

View File

@@ -1769,13 +1769,13 @@ template < typename T, size_t Size >
T component(const vec<T, Size>& v, size_t index);
template < typename T, size_t Size >
vec<T, Size> component(vec<T, Size> v, size_t index, T x);
vec<T, Size> component(const vec<T, Size>& v, size_t index, T x);
template < typename T, size_t Size >
vec<T, Size> row(const mat<T, Size>& m, size_t index);
template < typename T, size_t Size >
mat<T, Size> row(mat<T, Size> m, size_t index, const vec<T, Size>& v);
mat<T, Size> row(const mat<T, Size>& m, size_t index, const vec<T, Size>& v);
template < typename T, size_t Size >
vec<T, Size> column(const mat<T, Size>& m, size_t index);
@@ -1783,6 +1783,12 @@ vec<T, Size> column(const mat<T, Size>& m, size_t index);
template < typename T, size_t Size >
mat<T, Size> column(const mat<T, Size>& m, size_t index, const vec<T, Size>& v);
template < typename T, size_t Size >
vec<T, Size> diagonal(const mat<T, Size>& m);
template < typename T, size_t Size >
mat<T, Size> diagonal(const mat<T, Size>& m, const vec<T, Size>& diagonal);
template < typename T >
T real(const qua<T>& q);

View File

@@ -152,9 +152,10 @@ namespace vmath_hpp
}
template < typename T, std::size_t Size >
[[nodiscard]] constexpr vec<T, Size> component(vec<T, Size> v, std::size_t index, T component) {
v[index] = component;
return v;
[[nodiscard]] constexpr vec<T, Size> component(const vec<T, Size>& v, std::size_t index, T component) {
vec vv = v;
vv[index] = component;
return vv;
}
// row
@@ -165,9 +166,10 @@ namespace vmath_hpp
}
template < typename T, std::size_t Size >
[[nodiscard]] constexpr mat<T, Size> row(mat<T, Size> m, std::size_t index, const vec<T, Size>& row) {
m.rows[index] = row;
return m;
[[nodiscard]] constexpr mat<T, Size> row(const mat<T, Size>& m, std::size_t index, const vec<T, Size>& row) {
mat mm = m;
mm[index] = row;
return mm;
}
// column
@@ -197,6 +199,31 @@ namespace vmath_hpp
return impl::column_impl(m, index, column, std::make_index_sequence<Size>{});
}
// diagonal
namespace impl
{
template < typename T, std::size_t Size, std::size_t... Is >
[[nodiscard]] constexpr vec<T, Size> diagonal_impl(const mat<T, Size>& m, std::index_sequence<Is...>) {
return { m[Is][Is]... };
}
template < typename T, std::size_t Size, std::size_t... Is >
[[nodiscard]] constexpr mat<T, Size> diagonal_impl(const mat<T, Size>& m, const vec<T, Size>& diagonal, std::index_sequence<Is...>) {
return { component(m[Is], Is, diagonal[Is])... };
}
}
template < typename T, std::size_t Size >
[[nodiscard]] constexpr vec<T, Size> diagonal(const mat<T, Size>& m) {
return impl::diagonal_impl(m, std::make_index_sequence<Size>{});
}
template < typename T, std::size_t Size >
[[nodiscard]] constexpr mat<T, Size> diagonal(const mat<T, Size>& m, const vec<T, Size>& diagonal) {
return impl::diagonal_impl(m, diagonal, std::make_index_sequence<Size>{});
}
// real
template < typename T >

View File

@@ -18,7 +18,7 @@ endif()
file(GLOB_RECURSE UNTESTS_SOURCES "*.cpp" "*.hpp")
add_executable(${PROJECT_NAME} ${UNTESTS_SOURCES})
target_link_libraries(${PROJECT_NAME} vmath.hpp.singles)
target_link_libraries(${PROJECT_NAME} vmath.hpp)
target_compile_options(${PROJECT_NAME}
PRIVATE

View File

@@ -205,6 +205,14 @@ TEST_CASE("vmath/ext/access") {
STATIC_CHECK(column(imat2(), 1, {3,4}) == imat2(1,3,0,4));
}
SUBCASE("diagonal") {
STATIC_CHECK(diagonal(imat2(1,2,3,4)) == ivec2(1,4));
STATIC_CHECK(diagonal(imat2(1,2,3,4), ivec2(10,40)) == imat2(10,2,3,40));
STATIC_CHECK(diagonal(imat3(1,2,3,4,5,6,7,8,9)) == ivec3(1,5,9));
STATIC_CHECK(diagonal(imat3(1,2,3,4,5,6,7,8,9), ivec3(10,50,90)) == imat3(10,2,3,4,50,6,7,8,90));
}
SUBCASE("real") {
STATIC_CHECK(real(qua{1,2,3,4}) == 4);
STATIC_CHECK(real(qua{1,2,3,4}, 5) == qua{1,2,3,5});

View File

@@ -362,10 +362,10 @@ namespace vmath_hpp
namespace vmath_hpp
{
template fix<float> component(const fix2f&, std::size_t);
template fix2f component(fix2f, std::size_t, fix<float>);
template fix2f component(const fix2f&, std::size_t, fix<float>);
template fix2f row(const fix2x2f&, std::size_t);
template fix2x2f row(fix2x2f, std::size_t, const fix2f&);
template fix2x2f row(const fix2x2f&, std::size_t, const fix2f&);
template fix2f column(const fix2x2f&, std::size_t);
template fix2x2f column(const fix2x2f&, std::size_t, const fix2f&);