add matrix transform overloads

This commit is contained in:
BlackMATov
2020-11-29 06:26:21 +07:00
parent 987071fbb1
commit 0841b7a1f0
4 changed files with 130 additions and 101 deletions

View File

@@ -1066,9 +1066,15 @@ constexpr mat<T, Size> column(const mat<T, Size>& m, size_t index, const vec<T,
### Matrix Transform 3D
```cpp
template < typename T >
constexpr mat<T, 4> translate(T x, T y, T z);
template < typename T >
constexpr mat<T, 4> translate(const vec<T, 3>& v);
template < typename T >
constexpr mat<T, 4> translate(const mat<T, 4>& m, T x, T y, T z);
template < typename T >
constexpr mat<T, 4> translate(const mat<T, 4>& m, const vec<T, 3>& v);
@@ -1078,12 +1084,20 @@ mat<T, 4> rotate(T angle, const vec<T, 3>& axis);
template < typename T >
mat<T, 4> rotate(const mat<T, 4>& m, T angle, const vec<T, 3>& axis);
template < typename T >
constexpr mat<T, 4> scale(T x, T y, T z);
template < typename T >
constexpr mat<T, 4> scale(const vec<T, 3>& v);
template < typename T >
constexpr mat<T, 4> scale(const mat<T, 4>& m, T x, T y, T z);
template < typename T >
constexpr mat<T, 4> scale(const mat<T, 4>& m, const vec<T, 3>& v);
// look_at
template < typename T >
mat<T, 4> look_at_lh(const vec<T, 3>& eye, const vec<T, 3>& at, const vec<T, 3>& up);
@@ -1094,9 +1108,15 @@ mat<T, 4> look_at_rh(const vec<T, 3>& eye, const vec<T, 3>& at, const vec<T, 3>&
### Matrix Transform 2D
```cpp
template < typename T >
constexpr mat<T, 3> translate(T x, T y);
template < typename T >
constexpr mat<T, 3> translate(const vec<T, 2>& v);
template < typename T >
constexpr mat<T, 3> translate(const mat<T, 3>& m, T x, T y);
template < typename T >
constexpr mat<T, 3> translate(const mat<T, 3>& m, const vec<T, 2>& v);
@@ -1106,15 +1126,27 @@ mat<T, 3> rotate(T angle);
template < typename T >
mat<T, 3> rotate(const mat<T, 3>& m, T angle);
template < typename T >
constexpr mat<T, 3> scale(T x, T y);
template < typename T >
constexpr mat<T, 3> scale(const vec<T, 2>& v);
template < typename T >
constexpr mat<T, 3> scale(const mat<T, 3>& m, T x, T y);
template < typename T >
constexpr mat<T, 3> scale(const mat<T, 3>& m, const vec<T, 2>& v);
template < typename T >
constexpr mat<T, 3> shear(T x, T y);
template < typename T >
constexpr mat<T, 3> shear(const vec<T, 2>& v);
template < typename T >
constexpr mat<T, 3> shear(const mat<T, 3>& m, T x, T y);
template < typename T >
constexpr mat<T, 3> shear(const mat<T, 3>& m, const vec<T, 2>& v);

View File

@@ -186,12 +186,22 @@ namespace vmath_hpp
// translate
template < typename T >
[[nodiscard]] constexpr mat<T, 4> translate(const vec<T, 3>& v) {
[[nodiscard]] constexpr mat<T, 4> 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<T, 4> translate(const vec<T, 3>& v) {
return translate(v.x, v.y, v.z);
}
template < typename T >
[[nodiscard]] constexpr mat<T, 4> translate(const mat<T, 4>& 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<T, 4> scale(const vec<T, 3>& v) {
[[nodiscard]] constexpr mat<T, 4> 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<T, 4> scale(const vec<T, 3>& v) {
return scale(v.x, v.y, v.z);
}
template < typename T >
[[nodiscard]] constexpr mat<T, 4> scale(const mat<T, 4>& 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<T, 3> translate(const vec<T, 2>& v) {
[[nodiscard]] constexpr mat<T, 3> 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<T, 3> translate(const vec<T, 2>& v) {
return translate(v.x, v.y);
}
template < typename T >
[[nodiscard]] constexpr mat<T, 3> translate(const mat<T, 3>& 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<T, 3> scale(const vec<T, 2>& v) {
[[nodiscard]] constexpr mat<T, 3> 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<T, 3> scale(const vec<T, 2>& v) {
return scale(v.x, v.y);
}
template < typename T >
[[nodiscard]] constexpr mat<T, 3> scale(const mat<T, 3>& 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<T, 3> shear(const vec<T, 2>& v) {
[[nodiscard]] constexpr mat<T, 3> 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<T, 3> shear(const vec<T, 2>& v) {
return shear(v.x, v.y);
}
template < typename T >
[[nodiscard]] constexpr mat<T, 3> shear(const mat<T, 3>& m, T x, T y) {
return m * shear(x, y);
}
template < typename T >

View File

@@ -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<float> * m1 * rm1,
unit4_z<float>,
approx_epsilon_v<float>)));
}
{
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<float> * m2 * rm2,
unit4_z<float>,
approx_epsilon_v<float>)));
}
{
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<float> * m3 * rm3,
unit3_z<float>,
approx_epsilon_v<float>)));
}
{
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<float> * m4 * rm4,
unit2_y<float>,
approx_epsilon_v<float>)));
}
}
}

View File

@@ -79,46 +79,6 @@ namespace vmath_tests
constexpr explicit approx4(const vec<T, 4>& v) : value(v) {}
};
template < typename T >
struct approx2x2 {
mat<T, 2> value;
constexpr explicit approx2x2(const mat<T, 2>& v) : value(v) {}
};
template < typename T >
struct approx3x3 {
mat<T, 3> value;
constexpr explicit approx3x3(const mat<T, 3>& v) : value(v) {}
};
template < typename T >
struct approx4x4 {
mat<T, 4> value;
constexpr explicit approx4x4(const mat<T, 4>& v) : value(v) {}
};
//
//
//
template < typename T >
approx2(const vec<T, 2>&) -> approx2<T>;
template < typename T >
approx3(const vec<T, 3>&) -> approx3<T>;
template < typename T >
approx4(const vec<T, 4>&) -> approx4<T>;
template < typename T >
approx2x2(const mat<T, 2>&) -> approx2x2<T>;
template < typename T >
approx3x3(const mat<T, 3>&) -> approx3x3<T>;
template < typename T >
approx4x4(const mat<T, 4>&) -> approx4x4<T>;
//
//
//
@@ -142,25 +102,4 @@ namespace vmath_tests
constexpr bool operator==(const vec<T, 4>& l, const approx4<T>& r) {
return all(equal_to(l, r.value, approx_epsilon_v<T>));
}
template < typename T >
constexpr bool operator==(const mat<T, 2>& l, const approx2x2<T>& r) {
return l[0] == approx2(r.value[0])
&& l[1] == approx2(r.value[1]);
}
template < typename T >
constexpr bool operator==(const mat<T, 3>& l, const approx3x3<T>& 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<T, 4>& l, const approx4x4<T>& 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]);
}
}