From e25be445854e7b0bd907d33f6b2b0140f41624d1 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Fri, 26 Feb 2021 09:54:20 +0700 Subject: [PATCH] matrix ctor conversions --- README.md | 18 +++--- headers/vmath.hpp/vmath_mat.hpp | 104 ++++++++++++++++++-------------- untests/vmath_mat_fun_tests.cpp | 34 +++++++++++ 3 files changed, 102 insertions(+), 54 deletions(-) diff --git a/README.md b/README.md index ee561db..fc3a683 100644 --- a/README.md +++ b/README.md @@ -252,6 +252,10 @@ public: mat_base( const row_type& row0, const row_type& row1); + + template < typename A > mat_base(const mat_base& other); + template < typename A > explicit mat_base(const mat_base& other); + template < typename A > explicit mat_base(const mat_base& other); }; template < typename T > @@ -284,9 +288,9 @@ public: const mat_base& m, const vec_base& v); - explicit mat_base(const mat_base& other); - - explicit operator mat() const; + template < typename A > mat_base(const mat_base& other); + template < typename A > explicit mat_base(const mat_base& other); + template < typename A > explicit mat_base(const mat_base& other); }; template < typename T > @@ -321,11 +325,9 @@ public: const mat_base& m, const vec_base& v); - explicit mat_base(const mat_base& other); - explicit mat_base(const mat_base& other); - - explicit operator mat() const; - explicit operator mat() const; + template < typename A > mat_base(const mat_base& other); + template < typename A > explicit mat_base(const mat_base& other); + template < typename A > explicit mat_base(const mat_base& other); }; template < typename T, size_t Size > diff --git a/headers/vmath.hpp/vmath_mat.hpp b/headers/vmath.hpp/vmath_mat.hpp index c1a008f..a322373 100644 --- a/headers/vmath.hpp/vmath_mat.hpp +++ b/headers/vmath.hpp/vmath_mat.hpp @@ -26,9 +26,9 @@ namespace vmath_hpp::detail : mat_base(identity_init) {} constexpr mat_base(uninit_t) {} - constexpr mat_base(zero_init_t) : mat_base{zero_init, zero_init} {} - constexpr mat_base(unit_init_t) : mat_base{unit_init, unit_init} {} - constexpr mat_base(identity_init_t) : mat_base{T{1}} {} + constexpr mat_base(zero_init_t): mat_base{zero_init, zero_init} {} + constexpr mat_base(unit_init_t): mat_base{unit_init, unit_init} {} + constexpr mat_base(identity_init_t): mat_base{T{1}} {} constexpr explicit mat_base(T d) : rows{ @@ -51,6 +51,21 @@ namespace vmath_hpp::detail const row_type& row0, const row_type& row1) : rows{row0, row1} {} + + template < typename A, std::enable_if_t, int> = 0 > + constexpr mat_base(const mat_base& other): mat_base( + row_type{other.rows[0]}, + row_type{other.rows[1]}) {} + + template < typename A, std::enable_if_t, int> = 0 > + constexpr explicit mat_base(const mat_base& other): mat_base( + row_type{other.rows[0]}, + row_type{other.rows[1]}) {} + + template < typename A, std::enable_if_t, int> = 0 > + constexpr explicit mat_base(const mat_base& other): mat_base( + row_type{other.rows[0]}, + row_type{other.rows[1]}) {} }; template < typename T > @@ -63,9 +78,9 @@ namespace vmath_hpp::detail : mat_base(identity_init) {} constexpr mat_base(uninit_t) {} - constexpr mat_base(zero_init_t) : mat_base{zero_init, zero_init, zero_init} {} - constexpr mat_base(unit_init_t) : mat_base{unit_init, unit_init, unit_init} {} - constexpr mat_base(identity_init_t) : mat_base{T{1}} {} + constexpr mat_base(zero_init_t): mat_base{zero_init, zero_init, zero_init} {} + constexpr mat_base(unit_init_t): mat_base{unit_init, unit_init, unit_init} {} + constexpr mat_base(identity_init_t): mat_base{T{1}} {} constexpr explicit mat_base(T d) : rows{ @@ -102,18 +117,23 @@ namespace vmath_hpp::detail {m.rows[1], T{0}}, {v, T{1}}} {} - constexpr explicit mat_base( - const mat_base& other) - : rows{ - {other.rows[0], T{0}}, - {other.rows[1], T{0}}, - {T{0}, T{0}, T{1}}} {} + template < typename A, std::enable_if_t, int> = 0 > + constexpr mat_base(const mat_base& other): mat_base( + row_type{other.rows[0]}, + row_type{other.rows[1]}, + row_type{other.rows[2]}) {} - constexpr explicit operator mat() const { - return { - vec{rows[0]}, - vec{rows[1]}}; - } + template < typename A, std::enable_if_t, int> = 0 > + constexpr explicit mat_base(const mat_base& other): mat_base( + row_type{other.rows[0], T{0}}, + row_type{other.rows[1], T{0}}, + row_type{T{0}, T{0}, T{1}}) {} + + template < typename A, std::enable_if_t, int> = 0 > + constexpr explicit mat_base(const mat_base& other): mat_base( + row_type{other.rows[0]}, + row_type{other.rows[1]}, + row_type{other.rows[2]}) {} }; template < typename T > @@ -126,9 +146,9 @@ namespace vmath_hpp::detail : mat_base(identity_init) {} constexpr mat_base(uninit_t) {} - constexpr mat_base(zero_init_t) : mat_base{zero_init, zero_init, zero_init, zero_init} {} - constexpr mat_base(unit_init_t) : mat_base{unit_init, unit_init, unit_init, unit_init} {} - constexpr mat_base(identity_init_t) : mat_base{T{1}} {} + constexpr mat_base(zero_init_t): mat_base{zero_init, zero_init, zero_init, zero_init} {} + constexpr mat_base(unit_init_t): mat_base{unit_init, unit_init, unit_init, unit_init} {} + constexpr mat_base(identity_init_t): mat_base{T{1}} {} constexpr explicit mat_base(T d) : rows{ @@ -171,34 +191,26 @@ namespace vmath_hpp::detail {m.rows[2], T{0}}, {v, T{1}}} {} - constexpr explicit mat_base( - const mat_base& other) - : rows{ - {other.rows[0], T{0}, T{0}}, - {other.rows[1], T{0}, T{0}}, - {T{0}, T{0}, T{1}, T{0}}, - {T{0}, T{0}, T{0}, T{1}}} {} + template < typename A, std::enable_if_t, int> = 0 > + constexpr mat_base(const mat_base& other): mat_base( + row_type{other.rows[0]}, + row_type{other.rows[1]}, + row_type{other.rows[2]}, + row_type{other.rows[3]}) {} - constexpr explicit mat_base( - const mat_base& other) - : rows{ - {other.rows[0], T{0}}, - {other.rows[1], T{0}}, - {other.rows[2], T{0}}, - {T{0}, T{0}, T{0}, T{1}}} {} + template < typename A, std::enable_if_t, int> = 0 > + constexpr explicit mat_base(const mat_base& other): mat_base( + row_type{other.rows[0], T{0}, T{0}}, + row_type{other.rows[1], T{0}, T{0}}, + row_type{T{0}, T{0}, T{1}, T{0}}, + row_type{T{0}, T{0}, T{0}, T{1}}) {} - constexpr explicit operator mat() const { - return { - vec{rows[0]}, - vec{rows[1]}}; - } - - constexpr explicit operator mat() const { - return { - vec{rows[0]}, - vec{rows[1]}, - vec{rows[2]}}; - } + template < typename A, std::enable_if_t, int> = 0 > + constexpr explicit mat_base(const mat_base& other): mat_base( + row_type{other.rows[0], T{0}}, + row_type{other.rows[1], T{0}}, + row_type{other.rows[2], T{0}}, + row_type{T{0}, T{0}, T{0}, T{1}}) {} }; } diff --git a/untests/vmath_mat_fun_tests.cpp b/untests/vmath_mat_fun_tests.cpp index a91252e..60a99cd 100644 --- a/untests/vmath_mat_fun_tests.cpp +++ b/untests/vmath_mat_fun_tests.cpp @@ -158,6 +158,40 @@ TEST_CASE("vmath/mat_fun") { STATIC_CHECK(0.f / int2x2{1,1,1,1} == float2x2{0.f}); } + SUBCASE("Conversions2") { + { + STATIC_CHECK(float2x2(1.0) == float2x2(1)); + STATIC_CHECK(float2x2(int2(1,2)) == float2x2(float2(1,2))); + STATIC_CHECK(float2x2(int2(1,2),double2(3,4)) == float2x2(1,2,3,4)); + + STATIC_CHECK(float2x2(int2x2(1)) == float2x2(1)); + STATIC_CHECK(float2x2(int3x3(1)) == float2x2(1)); + STATIC_CHECK(float2x2(int4x4(1)) == float2x2(1)); + } + { + STATIC_CHECK(float3x3(1.0) == float3x3(1)); + STATIC_CHECK(float3x3(int3(1,2,3)) == float3x3(float3(1,2,3))); + STATIC_CHECK(float3x3(int3(1,2,3),double3(2,3,4),uint3(3,4,5)) == float3x3(1,2,3,2,3,4,3,4,5)); + + STATIC_CHECK(float3x3(int2x2(1),uint2(2)) == float3x3(float2x2(1),float2(2))); + + STATIC_CHECK(float3x3(int2x2(1)) == float3x3(1)); + STATIC_CHECK(float3x3(int3x3(1)) == float3x3(1)); + STATIC_CHECK(float3x3(int4x4(1)) == float3x3(1)); + } + { + STATIC_CHECK(float4x4(1.0) == float4x4(1)); + STATIC_CHECK(float4x4(int4(1,2,3,4)) == float4x4(float4(1,2,3,4))); + STATIC_CHECK(float4x4(int4(1,2,3,4),double4(2,3,4,5),uint4(3,4,5,6),int4(4,5,6,7)) == float4x4(1,2,3,4,2,3,4,5,3,4,5,6,4,5,6,7)); + + STATIC_CHECK(float4x4(int3x3(1),uint3(2)) == float4x4(float3x3(1),float3(2))); + + STATIC_CHECK(float4x4(int2x2(1)) == float4x4(1)); + STATIC_CHECK(float4x4(int3x3(1)) == float4x4(1)); + STATIC_CHECK(float4x4(int4x4(1)) == float4x4(1)); + } + } + SUBCASE("relational functions") { STATIC_CHECK_FALSE(any(bool2x2(false, false, false, false))); STATIC_CHECK(any(bool2x2(true, false, true, false)));