matrix ctor conversions

This commit is contained in:
BlackMATov
2021-02-26 09:54:20 +07:00
parent 2557b0af0f
commit e25be44585
3 changed files with 102 additions and 54 deletions

View File

@@ -252,6 +252,10 @@ public:
mat_base(
const row_type& row0,
const row_type& row1);
template < typename A > mat_base(const mat_base<A, 2>& other);
template < typename A > explicit mat_base(const mat_base<A, 3>& other);
template < typename A > explicit mat_base(const mat_base<A, 4>& other);
};
template < typename T >
@@ -284,9 +288,9 @@ public:
const mat_base<T, 2>& m,
const vec_base<T, 2>& v);
explicit mat_base(const mat_base<T, 2>& other);
explicit operator mat<T, 2>() const;
template < typename A > mat_base(const mat_base<A, 3>& other);
template < typename A > explicit mat_base(const mat_base<A, 2>& other);
template < typename A > explicit mat_base(const mat_base<A, 4>& other);
};
template < typename T >
@@ -321,11 +325,9 @@ public:
const mat_base<T, 3>& m,
const vec_base<T, 3>& v);
explicit mat_base(const mat_base<T, 2>& other);
explicit mat_base(const mat_base<T, 3>& other);
explicit operator mat<T, 2>() const;
explicit operator mat<T, 3>() const;
template < typename A > mat_base(const mat_base<A, 4>& other);
template < typename A > explicit mat_base(const mat_base<A, 2>& other);
template < typename A > explicit mat_base(const mat_base<A, 3>& other);
};
template < typename T, size_t Size >

View File

@@ -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<std::is_convertible_v<A, T>, int> = 0 >
constexpr mat_base(const mat_base<A, 2>& other): mat_base(
row_type{other.rows[0]},
row_type{other.rows[1]}) {}
template < typename A, std::enable_if_t<std::is_convertible_v<A, T>, int> = 0 >
constexpr explicit mat_base(const mat_base<A, 3>& other): mat_base(
row_type{other.rows[0]},
row_type{other.rows[1]}) {}
template < typename A, std::enable_if_t<std::is_convertible_v<A, T>, int> = 0 >
constexpr explicit mat_base(const mat_base<A, 4>& 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<T, 2>& 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<std::is_convertible_v<A, T>, int> = 0 >
constexpr mat_base(const mat_base<A, 3>& other): mat_base(
row_type{other.rows[0]},
row_type{other.rows[1]},
row_type{other.rows[2]}) {}
constexpr explicit operator mat<T, 2>() const {
return {
vec<T, 2>{rows[0]},
vec<T, 2>{rows[1]}};
}
template < typename A, std::enable_if_t<std::is_convertible_v<A, T>, int> = 0 >
constexpr explicit mat_base(const mat_base<A, 2>& 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<std::is_convertible_v<A, T>, int> = 0 >
constexpr explicit mat_base(const mat_base<A, 4>& 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<T, 2>& 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<std::is_convertible_v<A, T>, int> = 0 >
constexpr mat_base(const mat_base<A, 4>& 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<T, 3>& 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<std::is_convertible_v<A, T>, int> = 0 >
constexpr explicit mat_base(const mat_base<A, 2>& 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<T, 2>() const {
return {
vec<T, 2>{rows[0]},
vec<T, 2>{rows[1]}};
}
constexpr explicit operator mat<T, 3>() const {
return {
vec<T, 3>{rows[0]},
vec<T, 3>{rows[1]},
vec<T, 3>{rows[2]}};
}
template < typename A, std::enable_if_t<std::is_convertible_v<A, T>, int> = 0 >
constexpr explicit mat_base(const mat_base<A, 3>& 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}}) {}
};
}

View File

@@ -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)));