From 0841b7a1f0b12b16e0058dea43880b35252c1e7c Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Sun, 29 Nov 2020 06:26:21 +0700 Subject: [PATCH] add matrix transform overloads --- README.md | 32 +++++++++++ headers/vmath.hpp/vmath_ext.hpp | 94 +++++++++++++++++++++++++-------- untests/vmath_mat_fun_tests.cpp | 44 ++++++++------- untests/vmath_tests.hpp | 61 --------------------- 4 files changed, 130 insertions(+), 101 deletions(-) diff --git a/README.md b/README.md index 1ada1f6..3cadd60 100644 --- a/README.md +++ b/README.md @@ -1066,9 +1066,15 @@ constexpr mat column(const mat& m, size_t index, const vec +constexpr mat translate(T x, T y, T z); + template < typename T > constexpr mat translate(const vec& v); +template < typename T > +constexpr mat translate(const mat& m, T x, T y, T z); + template < typename T > constexpr mat translate(const mat& m, const vec& v); @@ -1078,12 +1084,20 @@ mat rotate(T angle, const vec& axis); template < typename T > mat rotate(const mat& m, T angle, const vec& axis); +template < typename T > +constexpr mat scale(T x, T y, T z); + template < typename T > constexpr mat scale(const vec& v); +template < typename T > +constexpr mat scale(const mat& m, T x, T y, T z); + template < typename T > constexpr mat scale(const mat& m, const vec& v); +// look_at + template < typename T > mat look_at_lh(const vec& eye, const vec& at, const vec& up); @@ -1094,9 +1108,15 @@ mat look_at_rh(const vec& eye, const vec& at, const vec& ### Matrix Transform 2D ```cpp +template < typename T > +constexpr mat translate(T x, T y); + template < typename T > constexpr mat translate(const vec& v); +template < typename T > +constexpr mat translate(const mat& m, T x, T y); + template < typename T > constexpr mat translate(const mat& m, const vec& v); @@ -1106,15 +1126,27 @@ mat rotate(T angle); template < typename T > mat rotate(const mat& m, T angle); +template < typename T > +constexpr mat scale(T x, T y); + template < typename T > constexpr mat scale(const vec& v); +template < typename T > +constexpr mat scale(const mat& m, T x, T y); + template < typename T > constexpr mat scale(const mat& m, const vec& v); +template < typename T > +constexpr mat shear(T x, T y); + template < typename T > constexpr mat shear(const vec& v); +template < typename T > +constexpr mat shear(const mat& m, T x, T y); + template < typename T > constexpr mat shear(const mat& m, const vec& v); diff --git a/headers/vmath.hpp/vmath_ext.hpp b/headers/vmath.hpp/vmath_ext.hpp index 7da0830..f9ccfa3 100644 --- a/headers/vmath.hpp/vmath_ext.hpp +++ b/headers/vmath.hpp/vmath_ext.hpp @@ -186,12 +186,22 @@ namespace vmath_hpp // translate template < typename T > - [[nodiscard]] constexpr mat translate(const vec& v) { + [[nodiscard]] constexpr mat translate(T x, T y, T z) { return { - { 1, 0, 0, 0}, - { 0, 1, 0, 0}, - { 0, 0, 1, 0}, - {v.x, v.y, v.z, 1}}; + {1, 0, 0, 0}, + {0, 1, 0, 0}, + {0, 0, 1, 0}, + {x, y, z, 1}}; + } + + template < typename T > + [[nodiscard]] constexpr mat translate(const vec& v) { + return translate(v.x, v.y, v.z); + } + + template < typename T > + [[nodiscard]] constexpr mat translate(const mat& m, T x, T y, T z) { + return m * translate(x, y, z); } template < typename T > @@ -233,12 +243,22 @@ namespace vmath_hpp // scale template < typename T > - [[nodiscard]] constexpr mat scale(const vec& v) { + [[nodiscard]] constexpr mat scale(T x, T y, T z) { return { - {v.x, 0, 0, 0}, - { 0, v.y, 0, 0}, - { 0, 0, v.z, 0}, - { 0, 0, 0, 1}}; + {x, 0, 0, 0}, + {0, y, 0, 0}, + {0, 0, z, 0}, + {0, 0, 0, 1}}; + } + + template < typename T > + [[nodiscard]] constexpr mat scale(const vec& v) { + return scale(v.x, v.y, v.z); + } + + template < typename T > + [[nodiscard]] constexpr mat scale(const mat& m, T x, T y, T z) { + return m * scale(x, y, z); } template < typename T > @@ -288,11 +308,21 @@ namespace vmath_hpp // translate template < typename T > - [[nodiscard]] constexpr mat translate(const vec& v) { + [[nodiscard]] constexpr mat translate(T x, T y) { return { - { 1, 0, 0}, - { 0, 1, 0}, - {v.x, v.y, 1}}; + {1, 0, 0}, + {0, 1, 0}, + {x, y, 1}}; + } + + template < typename T > + [[nodiscard]] constexpr mat translate(const vec& v) { + return translate(v.x, v.y); + } + + template < typename T > + [[nodiscard]] constexpr mat translate(const mat& m, T x, T y) { + return m * translate(x, y); } template < typename T > @@ -320,11 +350,21 @@ namespace vmath_hpp // scale template < typename T > - [[nodiscard]] constexpr mat scale(const vec& v) { + [[nodiscard]] constexpr mat scale(T x, T y) { return { - {v.x, 0, 0}, - { 0, v.y, 0}, - { 0, 0, 1}}; + {x, 0, 0}, + {0, y, 0}, + {0, 0, 1}}; + } + + template < typename T > + [[nodiscard]] constexpr mat scale(const vec& v) { + return scale(v.x, v.y); + } + + template < typename T > + [[nodiscard]] constexpr mat scale(const mat& m, T x, T y) { + return m * scale(x, y); } template < typename T > @@ -335,11 +375,21 @@ namespace vmath_hpp // shear template < typename T > - [[nodiscard]] constexpr mat shear(const vec& v) { + [[nodiscard]] constexpr mat shear(T x, T y) { return { - { 1, v.y, 0}, - {v.x, 1, 0}, - { 0, 0, 1}}; + {1, y, 0}, + {x, 1, 0}, + {0, 0, 1}}; + } + + template < typename T > + [[nodiscard]] constexpr mat shear(const vec& v) { + return shear(v.x, v.y); + } + + template < typename T > + [[nodiscard]] constexpr mat shear(const mat& m, T x, T y) { + return m * shear(x, y); } template < typename T > diff --git a/untests/vmath_mat_fun_tests.cpp b/untests/vmath_mat_fun_tests.cpp index d67d99b..1ad7927 100644 --- a/untests/vmath_mat_fun_tests.cpp +++ b/untests/vmath_mat_fun_tests.cpp @@ -192,33 +192,41 @@ TEST_CASE("vmath/mat_fun") { { constexpr float4x4 m1 = translate(float3(1.f, 2.f, 3.f)); - constexpr float4x4 inv_m1 = inverse(m1); - constexpr float4x4 ref_m1 = translate(float3(-1.f, -2.f, -3.f)); - STATIC_REQUIRE(inv_m1 == approx4x4(ref_m1)); + constexpr float4x4 rm1 = inverse(m1); + STATIC_REQUIRE(all(equal_to( + unit4_z * m1 * rm1, + unit4_z, + approx_epsilon_v))); } { - const float3 axis = normalize(float3(1.f, 2.f, 3.f)); - const float4x4 m1 = rotate(0.5f,axis); - const float4x4 inv_m1 = inverse(m1); - const float4x4 ref_m1 = rotate(-0.5f,axis); - REQUIRE(inv_m1 == approx4x4(ref_m1)); + const float3 axis2 = normalize(float3(1.f, 2.f, 3.f)); + const float4x4 m2 = rotate(0.5f,axis2); + const float4x4 rm2 = inverse(m2); + REQUIRE(all(equal_to( + unit4_z * m2 * rm2, + unit4_z, + approx_epsilon_v))); } { - const float3 axis = normalize(float3(1.f, 2.f, 3.f)); - const float3x3 m1 = float3x3(rotate(0.5f,axis)); - const float3x3 inv_m1 = inverse(m1); - const float3x3 ref_m1 = float3x3(rotate(-0.5f,axis)); - REQUIRE(inv_m1 == approx3x3(ref_m1)); + const float3 axis3 = normalize(float3(1.f, 2.f, 3.f)); + const float3x3 m3 = float3x3(rotate(0.5f,axis3)); + const float3x3 rm3 = inverse(m3); + REQUIRE(all(equal_to( + unit3_z * m3 * rm3, + unit3_z, + approx_epsilon_v))); } { - const float3 axis = normalize(float3(0.f, 0.f, 3.f)); - const float2x2 m1 = float2x2(rotate(0.5f,axis)); - const float2x2 inv_m1 = inverse(m1); - const float2x2 ref_m1 = float2x2(rotate(-0.5f,axis)); - REQUIRE(inv_m1 == approx2x2(ref_m1)); + const float3 axis4 = normalize(float3(0.f, 0.f, 3.f)); + const float2x2 m4 = float2x2(rotate(0.5f,axis4)); + const float2x2 rm4 = inverse(m4); + REQUIRE(all(equal_to( + unit2_y * m4 * rm4, + unit2_y, + approx_epsilon_v))); } } } diff --git a/untests/vmath_tests.hpp b/untests/vmath_tests.hpp index d76de7c..d9dcf67 100644 --- a/untests/vmath_tests.hpp +++ b/untests/vmath_tests.hpp @@ -79,46 +79,6 @@ namespace vmath_tests constexpr explicit approx4(const vec& v) : value(v) {} }; - template < typename T > - struct approx2x2 { - mat value; - constexpr explicit approx2x2(const mat& v) : value(v) {} - }; - - template < typename T > - struct approx3x3 { - mat value; - constexpr explicit approx3x3(const mat& v) : value(v) {} - }; - - template < typename T > - struct approx4x4 { - mat value; - constexpr explicit approx4x4(const mat& v) : value(v) {} - }; - - // - // - // - - template < typename T > - approx2(const vec&) -> approx2; - - template < typename T > - approx3(const vec&) -> approx3; - - template < typename T > - approx4(const vec&) -> approx4; - - template < typename T > - approx2x2(const mat&) -> approx2x2; - - template < typename T > - approx3x3(const mat&) -> approx3x3; - - template < typename T > - approx4x4(const mat&) -> approx4x4; - // // // @@ -142,25 +102,4 @@ namespace vmath_tests constexpr bool operator==(const vec& l, const approx4& r) { return all(equal_to(l, r.value, approx_epsilon_v)); } - - template < typename T > - constexpr bool operator==(const mat& l, const approx2x2& r) { - return l[0] == approx2(r.value[0]) - && l[1] == approx2(r.value[1]); - } - - template < typename T > - constexpr bool operator==(const mat& l, const approx3x3& r) { - return l[0] == approx3(r.value[0]) - && l[1] == approx3(r.value[1]) - && l[2] == approx3(r.value[2]); - } - - template < typename T > - constexpr bool operator==(const mat& l, const approx4x4& r) { - return l[0] == approx4(r.value[0]) - && l[1] == approx4(r.value[1]) - && l[2] == approx4(r.value[2]) - && l[3] == approx4(r.value[3]); - } }