Skip to content

Commit

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

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 <typename T>
inline constexpr Matrix2x2<T> operator*(T s, const Matrix2x2<T>& Mat)
{
return Mat * s;
}

template <class T> struct Matrix3x3
{
Expand Down Expand Up @@ -1169,6 +1186,26 @@ template <class T> struct Matrix3x3
}
};

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 <typename T>
inline constexpr Matrix3x3<T> operator*(T s, const Matrix3x3<T>& Mat)
{
return Mat * s;
}


template <class T> struct Matrix4x4
{
union
Expand Down Expand Up @@ -1730,6 +1767,26 @@ template <class T> struct Matrix4x4
}
};

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 <typename T>
inline constexpr Matrix4x4<T> operator*(T s, const Matrix4x4<T>& Mat)
{
return Mat * s;
}

// Template Vector Operations


Expand Down Expand Up @@ -2003,6 +2060,10 @@ using double4x4 = Matrix4x4<double>;
using double3x3 = Matrix3x3<double>;
using double2x2 = Matrix2x2<double>;

using int4x4 = Matrix4x4<Int32>;
using int3x3 = Matrix3x3<Int32>;
using int2x2 = Matrix2x2<Int32>;

template <typename T = float>
struct Quaternion
{
Expand Down
55 changes: 55 additions & 0 deletions Tests/DiligentCoreTest/src/Common/MathLibTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1318,6 +1318,61 @@ TEST(Common_BasicMath, HighPrecisionCross)
}
}


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

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

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

0 comments on commit acdb2a6

Please sign in to comment.