Skip to content

Commit

Permalink
Math lib: added matrix subtract and scalar divide operators
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Sep 29, 2023
1 parent 145ae11 commit d2595cb
Show file tree
Hide file tree
Showing 2 changed files with 310 additions and 2 deletions.
121 changes: 121 additions & 0 deletions Common/interface/BasicMath.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,14 @@ template <class T> struct Matrix2x2
return *this;
}

Matrix2x2& operator/=(T s)
{
for (int i = 0; i < 4; ++i)
(reinterpret_cast<T*>(this))[i] /= s;

return *this;
}

Matrix2x2& operator*=(const Matrix2x2& right)
{
*this = Mul(*this, right);
Expand All @@ -841,6 +849,24 @@ template <class T> struct Matrix2x2
// clang-format on
}

Matrix2x2& operator-=(const Matrix2x2& right)
{
for (int i = 0; i < 4; ++i)
Data()[i] -= right.Data()[i];
return *this;
}

Matrix2x2 operator-(const Matrix2x2& right) const
{
// clang-format off
return Matrix2x2
{
_11 - right._11, _12 - right._12,
_21 - right._21, _22 - right._22
};
// clang-format on
}

constexpr Matrix2x2 Transpose() const
{
return Matrix2x2{
Expand Down Expand Up @@ -925,6 +951,19 @@ inline constexpr Matrix2x2<T> operator*(T s, const Matrix2x2<T>& Mat)
return Mat * s;
}

template <typename T>
inline constexpr Matrix2x2<T> operator/(const Matrix2x2<T>& Mat, T s)
{
// clang-format off
return
{
Mat._11 / s, Mat._12 / s,
Mat._21 / s, Mat._22 / s
};
// clang-format on
}


template <class T> struct Matrix3x3
{
union
Expand Down Expand Up @@ -1038,6 +1077,14 @@ template <class T> struct Matrix3x3
return *this;
}

Matrix3x3& operator/=(T s)
{
for (int i = 0; i < 9; ++i)
(reinterpret_cast<T*>(this))[i] /= s;

return *this;
}

Matrix3x3& operator+=(const Matrix3x3& right)
{
for (int i = 0; i < 9; ++i)
Expand All @@ -1057,6 +1104,25 @@ template <class T> struct Matrix3x3
// clang-format on
}

Matrix3x3& operator-=(const Matrix3x3& right)
{
for (int i = 0; i < 9; ++i)
Data()[i] -= right.Data()[i];
return *this;
}

Matrix3x3 operator-(const Matrix3x3& right) const
{
// clang-format off
return Matrix3x3
{
_11 - right._11, _12 - right._12, _13 - right._13,
_21 - right._21, _22 - right._22, _23 - right._23,
_31 - right._31, _32 - right._32, _33 - right._33
};
// clang-format on
}

Matrix3x3& operator*=(const Matrix3x3& right)
{
*this = Mul(*this, right);
Expand Down Expand Up @@ -1242,6 +1308,19 @@ inline constexpr Matrix3x3<T> operator*(T s, const Matrix3x3<T>& Mat)
return Mat * s;
}

template <typename T>
inline constexpr Matrix3x3<T> operator/(const Matrix3x3<T>& Mat, T s)
{
// clang-format off
return
{
Mat._11 / s, Mat._12 / s, Mat._13 / s,
Mat._21 / s, Mat._22 / s, Mat._23 / s,
Mat._31 / s, Mat._32 / s, Mat._33 / s
};
// clang-format on
}


template <class T> struct Matrix4x4
{
Expand Down Expand Up @@ -1378,6 +1457,14 @@ template <class T> struct Matrix4x4
return *this;
}

Matrix4x4& operator/=(T s)
{
for (int i = 0; i < 16; ++i)
(reinterpret_cast<T*>(this))[i] /= s;

return *this;
}

Matrix4x4& operator*=(const Matrix4x4& right)
{
*this = Mul(*this, right);
Expand All @@ -1404,6 +1491,26 @@ template <class T> struct Matrix4x4
// clang-format on
}

Matrix4x4& operator-=(const Matrix4x4& right)
{
for (int i = 0; i < 16; ++i)
Data()[i] -= right.Data()[i];
return *this;
}

Matrix4x4 operator-(const Matrix4x4& right) const
{
// clang-format off
return Matrix4x4
{
_11 - right._11, _12 - right._12, _13 - right._13, _14 - right._14,
_21 - right._21, _22 - right._22, _23 - right._23, _24 - right._24,
_31 - right._31, _32 - right._32, _33 - right._33, _34 - right._34,
_41 - right._41, _42 - right._42, _43 - right._43, _44 - right._44
};
// clang-format on
}

constexpr Matrix4x4 Transpose() const
{
return Matrix4x4 //
Expand Down Expand Up @@ -1844,6 +1951,20 @@ inline constexpr Matrix4x4<T> operator*(T s, const Matrix4x4<T>& Mat)
return Mat * s;
}

template <typename T>
inline constexpr Matrix4x4<T> operator/(const Matrix4x4<T>& Mat, T s)
{
// clang-format off
return
{
Mat._11 / s, Mat._12 / s, Mat._13 / s, Mat._14 / s,
Mat._21 / s, Mat._22 / s, Mat._23 / s, Mat._24 / s,
Mat._31 / s, Mat._32 / s, Mat._33 / s, Mat._34 / s,
Mat._41 / s, Mat._42 / s, Mat._43 / s, Mat._44 / s
};
// clang-format on
}

// Template Vector Operations


Expand Down
191 changes: 189 additions & 2 deletions Tests/DiligentCoreTest/src/Common/MathLibTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1373,7 +1373,112 @@ TEST(Common_BasicMath, ScalarMatrixMultiply)
// clang-format on
}

TEST(Common_BasicMath, ScalarMatrixOpeartorPlus)
TEST(Common_BasicMath, ScalarMatrixMultiplyEqual)
{
// clang-format off
{
auto Mat = int2x2{1, 2,
3, 4};
Mat *= 2;
EXPECT_EQ(Mat,
int2x2(2, 4,
6, 8)
);
}
{
auto Mat = int3x3{1, 2, 3,
4, 5, 6,
7, 8, 9};
Mat *= 2;
EXPECT_EQ(Mat,
int3x3( 2, 4, 6,
8, 10, 12,
14, 16, 18)
);
}
{
auto Mat = int4x4{ 1, 2, 3, 4,
5, 6, 7, 8,
9, 10 ,11, 12,
13, 14, 15, 16};
Mat *= 2;
EXPECT_EQ(Mat,
int4x4( 2, 4, 6, 8,
10, 12, 14, 16,
18, 20 ,22, 24,
26, 28, 30, 32)
);
}
// clang-format on
}

TEST(Common_BasicMath, MatrixScalarDivide)
{
// clang-format off
EXPECT_EQ(float2x2(2, 4,
6, 8) / 2.f,
float2x2(1, 2,
3, 4)
);
EXPECT_EQ(float3x3( 2, 4, 6,
8, 10, 12,
14, 16, 18) / 2.f,
float3x3(1, 2, 3,
4, 5, 6,
7, 8, 9)
);
EXPECT_EQ(float4x4( 2, 4, 6, 8,
10, 12, 14, 16,
18, 20 ,22, 24,
26, 28, 30, 32) / 2.f,
float4x4( 1, 2, 3, 4,
5, 6, 7, 8,
9, 10 ,11, 12,
13, 14, 15, 16)
);
// clang-format on
}

TEST(Common_BasicMath, MatrixScalarDivideEqual)
{
// clang-format off
{
auto Mat = float2x2{2, 4,
6, 8};
Mat /= 2.f;
EXPECT_EQ(Mat,
float2x2(1, 2,
3, 4)
);
}
{
auto Mat = float3x3{ 2, 4, 6,
8, 10, 12,
14, 16, 18};
Mat /= 2.f;
EXPECT_EQ(Mat,
float3x3(1, 2, 3,
4, 5, 6,
7, 8, 9)
);
}
{
auto Mat = float4x4{ 2, 4, 6, 8,
10, 12, 14, 16,
18, 20 ,22, 24,
26, 28, 30, 32};
Mat /= 2.f;
EXPECT_EQ(Mat,
float4x4( 1, 2, 3, 4,
5, 6, 7, 8,
9, 10 ,11, 12,
13, 14, 15, 16)
);
}
// clang-format on
}

TEST(Common_BasicMath, MatrixOpeartorPlus)
{
// clang-format off
EXPECT_EQ(float2x2(1, 2,
Expand Down Expand Up @@ -1409,7 +1514,7 @@ TEST(Common_BasicMath, ScalarMatrixOpeartorPlus)
// clang-format on
}

TEST(Common_BasicMath, ScalarMatrixOpeartorPlusEqual)
TEST(Common_BasicMath, MatrixOpeartorPlusEqual)
{
// clang-format off
{
Expand Down Expand Up @@ -1454,6 +1559,88 @@ TEST(Common_BasicMath, ScalarMatrixOpeartorPlusEqual)
// clang-format on
}


TEST(Common_BasicMath, MatrixOpeartorMinus)
{
// clang-format off
EXPECT_EQ(float2x2( 6, 8,
10, 12) -
float2x2(5, 6,
7, 8),
float2x2(1, 2,
3, 4));
EXPECT_EQ(float3x3(11, 13, 15,
17, 19, 21,
23, 25, 27) -
float3x3(10, 11, 12,
13, 14, 15,
16, 17, 18),
float3x3(1, 2, 3,
4, 5, 6,
7, 8, 9));
EXPECT_EQ(float4x4(18, 20, 22, 24,
26, 28, 30, 32,
34, 36, 38, 40,
42, 44, 46, 48) -
float4x4(17, 18, 19, 20,
21, 22, 23, 24,
25, 26, 27, 28,
29, 30, 31, 32),
float4x4( 1, 2, 3, 4,
5, 6, 7, 8,
9, 10 ,11, 12,
13, 14, 15, 16)
);
// clang-format on
}


TEST(Common_BasicMath, MatrixOpeartorMinusEqual)
{
// clang-format off
{
auto Mat = float2x2{ 6, 8,
10, 12};
Mat -= float2x2{5, 6,
7, 8};
EXPECT_EQ(Mat,
float2x2(1, 2,
3, 4)
);
}
{
auto Mat = float3x3{11, 13, 15,
17, 19, 21,
23, 25, 27};
Mat -= float3x3{10, 11, 12,
13, 14, 15,
16, 17, 18};
EXPECT_EQ(Mat,
float3x3(1, 2, 3,
4, 5, 6,
7, 8, 9)
);
}
{
auto Mat = float4x4(18, 20, 22, 24,
26, 28, 30, 32,
34, 36, 38, 40,
42, 44, 46, 48);
Mat -= float4x4{17, 18, 19, 20,
21, 22, 23, 24,
25, 26, 27, 28,
29, 30, 31, 32};
EXPECT_EQ(Mat,
float4x4( 1, 2, 3, 4,
5, 6, 7, 8,
9, 10 ,11, 12,
13, 14, 15, 16)
);
}
// clang-format on
}


TEST(Common_AdvancedMath, Planes)
{
Plane3D plane = {};
Expand Down

0 comments on commit d2595cb

Please sign in to comment.