vector ctor conversions

This commit is contained in:
BlackMATov
2021-02-25 23:50:34 +07:00
parent 28bc36c0cd
commit 2557b0af0f
3 changed files with 71 additions and 52 deletions

View File

@@ -95,6 +95,10 @@ public:
explicit vec_base(T v);
vec_base(T x, T y);
template < typename A > vec_base(const vec_base<A, 2>& xy);
template < typename A > explicit vec_base(const vec_base<A, 3>& xy);
template < typename A > explicit vec_base(const vec_base<A, 4>& xy);
};
template < typename T >
@@ -114,7 +118,8 @@ public:
vec_base(const vec_base<T, 2>& xy, T z);
vec_base(T x, const vec_base<T, 2>& yz);
explicit operator vec<T, 2>() const;
template < typename A > vec_base(const vec_base<A, 3>& xyz);
template < typename A > explicit vec_base(const vec_base<A, 4>& xyz);
};
template < typename T >
@@ -133,15 +138,13 @@ public:
vec_base(const vec_base<T, 2>& xy, T z, T w);
vec_base(T x, const vec_base<T, 2>& yz, T w);
vec_base(T x, T y, const vec_base<T, 2>& zw);
vec_base(const vec_base<T, 2>& xy, const vec_base<T, 2>& zw);
vec_base(const vec_base<T, 3>& xyz, T w);
vec_base(T x, const vec_base<T, 3>& yzw);
explicit operator vec<T, 2>() const;
explicit operator vec<T, 3>() const;
template < typename A > vec_base(const vec_base<A, 4>& xyzw);
};
template < typename T, size_t Size >

View File

@@ -22,14 +22,20 @@ namespace vmath_hpp::detail
: vec_base{zero_init} {}
constexpr vec_base(uninit_t) {}
constexpr vec_base(zero_init_t) : vec_base{T{0}} {}
constexpr vec_base(unit_init_t) : vec_base{T{1}} {}
constexpr vec_base(zero_init_t): vec_base{T{0}} {}
constexpr vec_base(unit_init_t): vec_base{T{1}} {}
constexpr explicit vec_base(T v)
: x{v}, y{v} {}
constexpr explicit vec_base(T v): x{v}, y{v} {}
constexpr vec_base(T x, T y): x{x}, y{y} {}
constexpr vec_base(T x, T y)
: x{x}, y{y} {}
template < typename A, std::enable_if_t<std::is_convertible_v<A, T>, int> = 0 >
constexpr vec_base(const vec_base<A, 2>& xy): vec_base(xy[0], xy[1]) {}
template < typename A, std::enable_if_t<std::is_convertible_v<A, T>, int> = 0 >
constexpr explicit vec_base(const vec_base<A, 3>& xy): vec_base(xy[0], xy[1]) {}
template < typename A, std::enable_if_t<std::is_convertible_v<A, T>, int> = 0 >
constexpr explicit vec_base(const vec_base<A, 4>& xy): vec_base(xy[0], xy[1]) {}
[[nodiscard]] constexpr T& operator[](std::size_t index) noexcept {
switch ( index ) {
@@ -57,24 +63,20 @@ namespace vmath_hpp::detail
: vec_base{zero_init} {}
constexpr vec_base(uninit_t) {}
constexpr vec_base(zero_init_t) : vec_base{T{0}} {}
constexpr vec_base(unit_init_t) : vec_base{T{1}} {}
constexpr vec_base(zero_init_t): vec_base{T{0}} {}
constexpr vec_base(unit_init_t): vec_base{T{1}} {}
constexpr explicit vec_base(T v)
: x{v}, y{v}, z{v} {}
constexpr explicit vec_base(T v): x{v}, y{v}, z{v} {}
constexpr vec_base(T x, T y, T z): x{x}, y{y}, z{z} {}
constexpr vec_base(T x, T y, T z)
: x{x}, y{y}, z{z} {}
constexpr vec_base(const vec_base<T, 2>& xy, T z): vec_base(xy[0], xy[1], z) {}
constexpr vec_base(T x, const vec_base<T, 2>& yz): vec_base(x, yz[0], yz[1]) {}
constexpr vec_base(const vec_base<T, 2>& xy, T z)
: x{xy[0]}, y{xy[1]}, z{z} {}
template < typename A, std::enable_if_t<std::is_convertible_v<A, T>, int> = 0 >
constexpr vec_base(const vec_base<A, 3>& xyz): vec_base(xyz[0], xyz[1], xyz[2]) {}
constexpr vec_base(T x, const vec_base<T, 2>& yz)
: x{x}, y{yz[0]}, z{yz[1]} {}
constexpr explicit operator vec<T, 2>() const {
return {x, y};
}
template < typename A, std::enable_if_t<std::is_convertible_v<A, T>, int> = 0 >
constexpr explicit vec_base(const vec_base<A, 4>& xyz): vec_base(xyz[0], xyz[1], xyz[2]) {}
[[nodiscard]] constexpr T& operator[](std::size_t index) noexcept {
switch ( index ) {
@@ -107,37 +109,19 @@ namespace vmath_hpp::detail
constexpr vec_base(zero_init_t) : vec_base{T{0}} {}
constexpr vec_base(unit_init_t) : vec_base{T{1}} {}
constexpr explicit vec_base(T v)
: x{v}, y{v}, z{v}, w{v} {}
constexpr explicit vec_base(T v): x{v}, y{v}, z{v}, w{v} {}
constexpr vec_base(T x, T y, T z, T w): x{x}, y{y}, z{z}, w{w} {}
constexpr vec_base(T x, T y, T z, T w)
: x{x}, y{y}, z{z}, w{w} {}
constexpr vec_base(const vec_base<T, 2>& xy, T z, T w): vec_base(xy[0], xy[1], z, w) {}
constexpr vec_base(T x, const vec_base<T, 2>& yz, T w): vec_base(x, yz[0], yz[1], w) {}
constexpr vec_base(T x, T y, const vec_base<T, 2>& zw): vec_base(x, y, zw[0], zw[1]) {}
constexpr vec_base(const vec_base<T, 2>& xy, const vec_base<T, 2>& zw): vec_base(xy[0], xy[1], zw[0], zw[1]) {}
constexpr vec_base(const vec_base<T, 2>& xy, T z, T w)
: x{xy[0]}, y{xy[1]}, z{z}, w{w} {}
constexpr vec_base(const vec_base<T, 3>& xyz, T w): vec_base(xyz[0], xyz[1], xyz[2], w) {}
constexpr vec_base(T x, const vec_base<T, 3>& yzw): vec_base(x, yzw[0], yzw[1], yzw[2]) {}
constexpr vec_base(T x, const vec_base<T, 2>& yz, T w)
: x{x}, y{yz[0]}, z{yz[1]}, w{w} {}
constexpr vec_base(T x, T y, const vec_base<T, 2>& zw)
: x{x}, y{y}, z{zw[0]}, w{zw[1]} {}
constexpr vec_base(const vec_base<T, 2>& xy, const vec_base<T, 2>& zw)
: x{xy[0]}, y{xy[1]}, z{zw[0]}, w{zw[1]} {}
constexpr vec_base(const vec_base<T, 3>& xyz, T w)
: x{xyz[0]}, y{xyz[1]}, z{xyz[2]}, w{w} {}
constexpr vec_base(T x, const vec_base<T, 3>& yzw)
: x{x}, y{yzw[0]}, z{yzw[1]}, w{yzw[2]} {}
constexpr explicit operator vec<T, 2>() const {
return {x, y};
}
constexpr explicit operator vec<T, 3>() const {
return {x, y, z};
}
template < typename A, std::enable_if_t<std::is_convertible_v<A, T>, int> = 0 >
constexpr vec_base(const vec_base<A, 4>& xyzw): vec_base(xyzw[0], xyzw[1], xyzw[2], xyzw[3]) {}
[[nodiscard]] constexpr T& operator[](std::size_t index) noexcept {
switch ( index ) {

View File

@@ -125,6 +125,38 @@ TEST_CASE("vmath/vec_fun") {
STATIC_CHECK(float2{} / int2{1} == float2{});
}
SUBCASE("Conversions2") {
{
STATIC_CHECK(float2(1) == float2(1,1));
STATIC_CHECK(float2(1,2.0) == float2(1,2));
STATIC_CHECK(float2(int2(1,2)) == float2(1,2));
STATIC_CHECK(float2(int3(1,2,3)) == float2(1,2));
STATIC_CHECK(float2(int4(1,2,3,4)) == float2(1,2));
}
{
STATIC_CHECK(float3(1) == float3(1,1,1));
STATIC_CHECK(float3(1,2.0,3u) == float3(1,2,3));
STATIC_CHECK(float3(int3(1,2,3)) == float3(1,2,3));
STATIC_CHECK(float3(int4(1,2,3,4)) == float3(1,2,3));
STATIC_CHECK(float3(int2(1,2),3.0) == float3(1,2,3));
STATIC_CHECK(float3(1.0,int2(2,3)) == float3(1,2,3));
}
{
STATIC_CHECK(float4(1) == float4(1,1,1,1));
STATIC_CHECK(float4(1,2.0,3u,4) == float4(1,2,3,4));
STATIC_CHECK(float4(int4(1,2,3,4)) == float4(1,2,3,4));
STATIC_CHECK(float4(int2{1,2},3u,4.0) == float4(1,2,3,4));
STATIC_CHECK(float4(1,int2{2,3},4.0) == float4(1,2,3,4));
STATIC_CHECK(float4(1,2.f,int2{3,4}) == float4(1,2,3,4));
STATIC_CHECK(float4(int2{1,2},float2{3,4}) == float4(1,2,3,4));
STATIC_CHECK(float4(int3{1,2,3},4.0) == float4(1,2,3,4));
STATIC_CHECK(float4(1.0,int3{2,3,4}) == float4(1,2,3,4));
}
}
SUBCASE("Angle and Trigonometric Functions") {
STATIC_CHECK(radians(degrees(float2(12.13f))) == uapprox2(12.13f));
STATIC_CHECK(degrees(radians(float2(12.13f))) == uapprox2(12.13f));