qua: real, imag access funcs

This commit is contained in:
BlackMATov
2021-02-07 15:18:08 +07:00
parent 337a1a6672
commit 3d521fd745
2 changed files with 44 additions and 8 deletions

View File

@@ -149,8 +149,8 @@ namespace vmath_hpp
} }
template < typename T, std::size_t Size > template < typename T, std::size_t Size >
[[nodiscard]] constexpr vec<T, Size> component(vec<T, Size> v, std::size_t index, T x) { [[nodiscard]] constexpr vec<T, Size> component(vec<T, Size> v, std::size_t index, T component) {
v[index] = x; v[index] = component;
return v; return v;
} }
@@ -162,8 +162,8 @@ namespace vmath_hpp
} }
template < typename T, std::size_t Size > 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>& v) { [[nodiscard]] constexpr mat<T, Size> row(mat<T, Size> m, std::size_t index, const vec<T, Size>& row) {
m.rows[index] = v; m.rows[index] = row;
return m; return m;
} }
@@ -179,8 +179,8 @@ namespace vmath_hpp
template < typename T, std::size_t Size, std::size_t... Is > template < typename T, std::size_t Size, std::size_t... Is >
[[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE
mat<T, Size> column_impl(const mat<T, Size>& m, std::size_t index, const vec<T, Size>& v, std::index_sequence<Is...>) { mat<T, Size> column_impl(const mat<T, Size>& m, std::size_t index, const vec<T, Size>& column, std::index_sequence<Is...>) {
return { component(m[Is], index, v[Is])... }; return { component(m[Is], index, column[Is])... };
} }
} }
@@ -190,8 +190,34 @@ namespace vmath_hpp
} }
template < typename T, std::size_t Size > template < typename T, std::size_t Size >
[[nodiscard]] constexpr mat<T, Size> column(const mat<T, Size>& m, std::size_t index, const vec<T, Size>& v) { [[nodiscard]] constexpr mat<T, Size> column(const mat<T, Size>& m, std::size_t index, const vec<T, Size>& column) {
return impl::column_impl(m, index, v, std::make_index_sequence<Size>{}); return impl::column_impl(m, index, column, std::make_index_sequence<Size>{});
}
// real
template < typename T >
[[nodiscard]] constexpr T real(const qua<T>& q) {
return q.s;
}
template < typename T >
[[nodiscard]] constexpr qua<T> real(qua<T> q, T real) {
q.s = real;
return q;
}
// imag
template < typename T >
[[nodiscard]] constexpr vec<T, 3> imag(const qua<T>& q) {
return q.v;
}
template < typename T >
[[nodiscard]] constexpr qua<T> imag(qua<T> q, const vec<T, 3>& imag) {
q.v = imag;
return q;
} }
} }

View File

@@ -201,6 +201,16 @@ TEST_CASE("vmath/ext") {
STATIC_REQUIRE(column(int2x2(), 1, {3,4}) == int2x2(1,3,0,4)); STATIC_REQUIRE(column(int2x2(), 1, {3,4}) == int2x2(1,3,0,4));
} }
SECTION("real") {
STATIC_REQUIRE(real(qua{1,2,3,4}) == 4);
STATIC_REQUIRE(real(qua{1,2,3,4}, 5) == qua{1,2,3,5});
}
SECTION("imag") {
STATIC_REQUIRE(imag(qua{1,2,3,4}) == vec{1,2,3});
STATIC_REQUIRE(imag(qua{1,2,3,4}, {4,3,2}) == qua{4,3,2,4});
}
SECTION("matrix translate") { SECTION("matrix translate") {
STATIC_REQUIRE(float3(2.f,3.f,1.f) * translate(float2{1.f,2.f}) == uapprox3(3.f,5.f,1.f)); STATIC_REQUIRE(float3(2.f,3.f,1.f) * translate(float2{1.f,2.f}) == uapprox3(3.f,5.f,1.f));
STATIC_REQUIRE(float3(2.f,3.f,1.f) * translate(translate(float2{1.f,2.f}), float2{1.f,2.f}) == uapprox3(4.f,7.f,1.f)); STATIC_REQUIRE(float3(2.f,3.f,1.f) * translate(translate(float2{1.f,2.f}), float2{1.f,2.f}) == uapprox3(4.f,7.f,1.f));