Skip to content

Commit 27ad07f

Browse files
committed
some changes
1 parent 69b2c6c commit 27ad07f

File tree

2 files changed

+22
-25
lines changed

2 files changed

+22
-25
lines changed

ZEngine/ZEngine/Core/Maths/Matrix.h

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ namespace ZEngine::Core::Maths
6262
Matrix<T, R, C> operator+(Matrix<T, R, C>& other)
6363
{
6464
Matrix<T, R, C> result{};
65-
for (size_t i = 0; i < R; ++i)
65+
for (size_t j = 0; j < C; ++j)
6666
{
67-
for (size_t j = 0; j < C; j++)
67+
for (size_t i = 0; i < R; i++)
6868
{
6969
result[i][j] = other[i][j] + (*this)[i][j];
7070
}
@@ -75,9 +75,9 @@ namespace ZEngine::Core::Maths
7575
Matrix<T, R, C> operator-(Matrix<T, R, C>& other)
7676
{
7777
Matrix<T, R, C> result{};
78-
for (size_t i = 0; i < R; ++i)
78+
for (size_t j = 0; j < C; ++j)
7979
{
80-
for (size_t j = 0; j < C; j++)
80+
for (size_t i = 0; i < R; i++)
8181
{
8282
result[i][j] = (*this)[i][j] - other[i][j];
8383
}
@@ -92,8 +92,7 @@ namespace ZEngine::Core::Maths
9292

9393
Mat2()
9494
{
95-
for (size_t i = 0; i < 4; ++i)
96-
this->m_data[i] = 0;
95+
std::memset(this->m_data, 0, sizeof(this->m_data));
9796
}
9897

9998
Mat2(T m00, T m01, T m10, T m11)
@@ -181,7 +180,7 @@ namespace ZEngine::Core::Maths
181180
}
182181
return *this;
183182
}
184-
Mat2 inverse()
183+
Mat2 Inverse()
185184
{
186185
T det = this->determinant();
187186
ZENGINE_VALIDATE_ASSERT(det != 0, "Matrix is singular and cannot be inverted");
@@ -198,8 +197,7 @@ namespace ZEngine::Core::Maths
198197

199198
Mat3()
200199
{
201-
for (size_t i = 0; i < 9; ++i)
202-
this->m_data[i] = 0;
200+
std::memset(this->m_data, 0, sizeof(this->m_data));
203201
}
204202

205203
Mat3(T m00, T m01, T m02, T m10, T m11, T m12, T m20, T m21, T m22)
@@ -292,7 +290,7 @@ namespace ZEngine::Core::Maths
292290
return *this;
293291
}
294292

295-
Mat3 inverse()
293+
Mat3 Inverse()
296294
{
297295
T det = this->determinant();
298296
ZENGINE_VALIDATE_ASSERT(det != 0, "Matrix is singular and cannot be inverted");
@@ -318,8 +316,7 @@ namespace ZEngine::Core::Maths
318316

319317
Mat4()
320318
{
321-
for (size_t i = 0; i < 16; ++i)
322-
this->m_data[i] = 0;
319+
std::memset(this->m_data, 0, sizeof(this->m_data));
323320
}
324321

325322
Mat4(T m00, T m01, T m02, T m03, T m10, T m11, T m12, T m13, T m20, T m21, T m22, T m23, T m30, T m31, T m32, T m33)
@@ -423,7 +420,7 @@ namespace ZEngine::Core::Maths
423420
this->m_data[2] * (this->m_data[4] * (this->m_data[9] * this->m_data[15] - this->m_data[11] * this->m_data[13]) - this->m_data[5] * (this->m_data[8] * this->m_data[15] - this->m_data[11] * this->m_data[12]) + this->m_data[7] * (this->m_data[8] * this->m_data[13] - this->m_data[9] * this->m_data[12])) -
424421
this->m_data[3] * (this->m_data[4] * (this->m_data[9] * this->m_data[14] - this->m_data[10] * this->m_data[13]) - this->m_data[5] * (this->m_data[8] * this->m_data[14] - this->m_data[10] * this->m_data[12]) + this->m_data[6] * (this->m_data[8] * this->m_data[13] - this->m_data[9] * this->m_data[12])));
425422
}
426-
Mat4<T> inverse() const
423+
Mat4<T> Inverse() const
427424
{
428425
const T* m = this->m_data;
429426

@@ -483,22 +480,22 @@ namespace ZEngine::Core::Maths
483480
using Mat4f = Mat4<float>;
484481

485482
template <typename T>
486-
T identity();
483+
T Identity();
487484

488485
template <>
489-
inline Mat2f identity<Mat2f>()
486+
inline Mat2f Identity<Mat2f>()
490487
{
491488
return Mat2f(1, 0, 0, 1);
492489
}
493490

494491
template <>
495-
inline Mat3f identity<Mat3f>()
492+
inline Mat3f Identity<Mat3f>()
496493
{
497494
return Mat3f(1, 0, 0, 0, 1, 0, 0, 0, 1);
498495
}
499496

500497
template <>
501-
inline Mat4f identity<Mat4f>()
498+
inline Mat4f Identity<Mat4f>()
502499
{
503500
return Mat4f(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
504501
}
@@ -507,9 +504,9 @@ namespace ZEngine::Core::Maths
507504
Matrix<T, R, C> operator*(const Matrix<T, R, K>& a, const Matrix<T, K, C>& b)
508505
{
509506
Matrix<T, R, C> result{};
510-
for (size_t i = 0; i < R; ++i)
507+
for (size_t j = 0; j < C; ++j)
511508
{
512-
for (size_t j = 0; j < C; ++j)
509+
for (size_t i = 0; i < R; ++i)
513510
{
514511
T sum = T{};
515512
for (size_t k = 0; k < K; ++k)

ZEngine/tests/Maths/Matrix_test.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ TEST(MatrixTest, RowAndColumnAccessFloat)
7272

7373
TEST(MatrixTest, Identity)
7474
{
75-
Mat2f I2 = identity<Mat2f>();
76-
Mat3f I3 = identity<Mat3f>();
77-
Mat4f I4 = identity<Mat4f>();
75+
Mat2f I2 = Identity<Mat2f>();
76+
Mat3f I3 = Identity<Mat3f>();
77+
Mat4f I4 = Identity<Mat4f>();
7878

7979
for (int i = 0; i < 2; ++i)
8080
for (int j = 0; j < 2; ++j)
@@ -92,21 +92,21 @@ TEST(MatrixTest, Identity)
9292
TEST(MatrixTest, Inverse)
9393
{
9494
Mat2f m2(4.0f, 7.0f, 2.0f, 6.0f);
95-
Mat2f inv2 = m2.inverse();
95+
Mat2f inv2 = m2.Inverse();
9696
Mat2f shouldBeIdentity2 = m2 * inv2;
9797
for (int i = 0; i < 2; ++i)
9898
for (int j = 0; j < 2; ++j)
9999
EXPECT_NEAR(shouldBeIdentity2[i][j], (i == j ? 1.0f : 0.0f), EPSILON);
100100

101101
Mat3f m3(1, 2, 3, 0, 1, 4, 5, 6, 0);
102-
Mat3f inv3 = m3.inverse();
102+
Mat3f inv3 = m3.Inverse();
103103
Mat3f shouldBeIdentity3 = m3 * inv3;
104104
for (int i = 0; i < 3; ++i)
105105
for (int j = 0; j < 3; ++j)
106106
EXPECT_NEAR(shouldBeIdentity3[i][j], (i == j ? 1.0f : 0.0f), EPSILON);
107107

108108
Mat4f m4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
109-
Mat4f inv4 = m4.inverse();
109+
Mat4f inv4 = m4.Inverse();
110110
Mat4f shouldBeIdentity4 = m4 * inv4;
111111
for (int i = 0; i < 4; ++i)
112112
for (int j = 0; j < 4; ++j)

0 commit comments

Comments
 (0)