Skip to content

Commit

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

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 @@ -1020,6 +1038,25 @@ template <class T> struct Matrix3x3
return *this;
}

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 @@ -1347,6 +1384,26 @@ template <class T> struct Matrix4x4
return *this;
}

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
81 changes: 81 additions & 0 deletions Tests/DiligentCoreTest/src/Common/MathLibTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1373,6 +1373,87 @@ TEST(Common_BasicMath, ScalarMatrixMultiply)
// clang-format on
}

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

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

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

0 comments on commit 145ae11

Please sign in to comment.