From 20257bd41297a6b86b209ae3f4ddca5542ebbf55 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Fri, 27 Nov 2020 03:46:16 +0700 Subject: [PATCH] vector reciprocal --- headers/vmath.hpp/vmath_ext.hpp | 20 ++++++++--------- headers/vmath.hpp/vmath_mat.hpp | 33 +++++++++++++---------------- headers/vmath.hpp/vmath_vec_fun.hpp | 5 +++++ untests/vmath_fun_tests.cpp | 23 +++++++++++--------- untests/vmath_vec_fun_tests.cpp | 1 + 5 files changed, 44 insertions(+), 38 deletions(-) diff --git a/headers/vmath.hpp/vmath_ext.hpp b/headers/vmath.hpp/vmath_ext.hpp index fca2e5b..4963e6b 100644 --- a/headers/vmath.hpp/vmath_ext.hpp +++ b/headers/vmath.hpp/vmath_ext.hpp @@ -337,6 +337,11 @@ namespace vmath_hpp { 0, 0, 1}}; } + template < typename T > + [[nodiscard]] constexpr mat shear(const mat& m, const vec& v) { + return m * shear(v); + } + template < typename T > [[nodiscard]] constexpr mat shear_x(T y) { return { @@ -345,6 +350,11 @@ namespace vmath_hpp {0, 0, 1}}; } + template < typename T > + [[nodiscard]] constexpr mat shear_x(const mat& m, T y) { + return m * shear_x(y); + } + template < typename T > [[nodiscard]] constexpr mat shear_y(T x) { return { @@ -353,16 +363,6 @@ namespace vmath_hpp {0, 0, 1}}; } - template < typename T > - [[nodiscard]] constexpr mat shear(const mat& m, const vec& v) { - return m * shear(v); - } - - template < typename T > - [[nodiscard]] constexpr mat shear_x(const mat& m, T y) { - return m * shear_x(y); - } - template < typename T > [[nodiscard]] constexpr mat shear_y(const mat& m, T x) { return m * shear_y(x); diff --git a/headers/vmath.hpp/vmath_mat.hpp b/headers/vmath.hpp/vmath_mat.hpp index ee45b27..ecb7a32 100644 --- a/headers/vmath.hpp/vmath_mat.hpp +++ b/headers/vmath.hpp/vmath_mat.hpp @@ -20,12 +20,11 @@ namespace vmath_hpp::detail class mat_base { public: using row_type = vec; - row_type rows[2]; + row_type rows[2] = { + {1, 0}, + {0, 1}}; public: - constexpr mat_base() : rows{ - row_type{1, 0}, - row_type{0, 1}, - } {} + mat_base() = default; constexpr explicit mat_base(T v) : rows{ @@ -61,13 +60,12 @@ namespace vmath_hpp::detail class mat_base { public: using row_type = vec; - row_type rows[3]; + row_type rows[3] = { + {1, 0, 0}, + {0, 1, 0}, + {0, 0, 1}}; public: - constexpr mat_base() : rows{ - row_type{1, 0, 0}, - row_type{0, 1, 0}, - row_type{0, 0, 1}, - } {} + mat_base() = default; constexpr explicit mat_base(T v) : rows{ @@ -109,14 +107,13 @@ namespace vmath_hpp::detail class mat_base { public: using row_type = vec; - row_type rows[4]; + row_type rows[4] = { + {1, 0, 0, 0}, + {0, 1, 0, 0}, + {0, 0, 1, 0}, + {0, 0, 0, 1}}; public: - constexpr mat_base() : rows{ - row_type{1, 0, 0, 0}, - row_type{0, 1, 0, 0}, - row_type{0, 0, 1, 0}, - row_type{0, 0, 0, 1}, - } {} + mat_base() = default; constexpr explicit mat_base(T v) : rows{ diff --git a/headers/vmath.hpp/vmath_vec_fun.hpp b/headers/vmath.hpp/vmath_vec_fun.hpp index 8efb19e..4227849 100644 --- a/headers/vmath.hpp/vmath_vec_fun.hpp +++ b/headers/vmath.hpp/vmath_vec_fun.hpp @@ -391,6 +391,11 @@ namespace vmath_hpp return map([](T x) { return sign(x); }, xs); } + template < typename T, std::size_t Size > + [[nodiscard]] constexpr vec reciprocal(const vec& xs) { + return map([](T x) { return reciprocal(x); }, xs); + } + template < typename T, std::size_t Size > [[nodiscard]] vec floor(const vec& xs) { return map([](T x) { return floor(x); }, xs); diff --git a/untests/vmath_fun_tests.cpp b/untests/vmath_fun_tests.cpp index 40d34bc..f2ef755 100644 --- a/untests/vmath_fun_tests.cpp +++ b/untests/vmath_fun_tests.cpp @@ -51,17 +51,20 @@ TEST_CASE("vmath/fun") { } SECTION("Common Functions") { - REQUIRE(abs(1) == 1); - REQUIRE(abs(-1) == 1); - REQUIRE(abs(1.f) == approx(1.f)); - REQUIRE(abs(-1.f) == approx(1.f)); + STATIC_REQUIRE(vmath_hpp::abs(1) == 1); + STATIC_REQUIRE(vmath_hpp::abs(-1) == 1); + STATIC_REQUIRE(vmath_hpp::abs(1.f) == approx(1.f)); + STATIC_REQUIRE(vmath_hpp::abs(-1.f) == approx(1.f)); - REQUIRE(sign(2) == 1); - REQUIRE(sign(-2) == -1); - REQUIRE(sign(0) == 0); - REQUIRE(sign(2.f) == approx(1.f)); - REQUIRE(sign(-2.f) == approx(-1.f)); - REQUIRE(sign(0.f) == approx(0.f)); + STATIC_REQUIRE(sign(2) == 1); + STATIC_REQUIRE(sign(-2) == -1); + STATIC_REQUIRE(sign(0) == 0); + STATIC_REQUIRE(sign(2.f) == approx(1.f)); + STATIC_REQUIRE(sign(-2.f) == approx(-1.f)); + STATIC_REQUIRE(sign(0.f) == approx(0.f)); + + STATIC_REQUIRE(reciprocal(2.f) == approx(0.5f)); + STATIC_REQUIRE(reciprocal(4.f) == approx(0.25f)); REQUIRE(floor(1.7f) == approx(1.f)); REQUIRE(trunc(1.7f) == approx(1.f)); diff --git a/untests/vmath_vec_fun_tests.cpp b/untests/vmath_vec_fun_tests.cpp index 3dbfb53..c1ce6dd 100644 --- a/untests/vmath_vec_fun_tests.cpp +++ b/untests/vmath_vec_fun_tests.cpp @@ -127,6 +127,7 @@ TEST_CASE("vmath/vec_fun") { SECTION("Common Functions") { STATIC_REQUIRE(abs(float2(1.f, -1.f)) == approx2(1.f,1.f)); STATIC_REQUIRE(sign(float3(1.f, -1.f, 0.f)) == approx3(1.f,-1.f,0.f)); + STATIC_REQUIRE(reciprocal(float2(2.f, 4.f)) == approx2(0.5f,0.25f)); (void)floor(float2(1.f, -1.f)); (void)trunc(float2(1.f, -1.f));