1
0
forked from 0ad/0ad

Make CMatrix3D::operator*= behave as expected.

This commit is contained in:
Stan 2024-09-13 13:27:42 +02:00
parent e56ebb3f46
commit 496e92fa81
Signed by: Stan
GPG Key ID: 244943DFF8370D60
3 changed files with 51 additions and 5 deletions

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2019 Wildfire Games. /* Copyright (C) 2024 Wildfire Games.
* *
* Permission is hereby granted, free of charge, to any person obtaining * Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the * a copy of this software and associated documentation files (the
@ -24,8 +24,9 @@
#define INCLUDED_SELF_TEST #define INCLUDED_SELF_TEST
// for convenience, to avoid having to include all of these manually // for convenience, to avoid having to include all of these manually
#include "lib/status.h" #include "lib/lib.h"
#include "lib/os_path.h" #include "lib/os_path.h"
#include "lib/status.h"
#include "lib/posix/posix.h" #include "lib/posix/posix.h"
#define CXXTEST_HAVE_EH #define CXXTEST_HAVE_EH
@ -147,6 +148,14 @@ std::vector<T> ts_make_vector(T* start, size_t size_bytes)
#define TS_ASSERT_VECTOR_EQUALS_ARRAY(vec1, array) TS_ASSERT_EQUALS(vec1, ts_make_vector((array), sizeof(array))) #define TS_ASSERT_VECTOR_EQUALS_ARRAY(vec1, array) TS_ASSERT_EQUALS(vec1, ts_make_vector((array), sizeof(array)))
#define TS_ASSERT_VECTOR_CONTAINS(vec1, element) TS_ASSERT(std::find((vec1).begin(), (vec1).end(), element) != (vec1).end()); #define TS_ASSERT_VECTOR_CONTAINS(vec1, element) TS_ASSERT(std::find((vec1).begin(), (vec1).end(), element) != (vec1).end());
#define TS_ASSERT_MATRIX_EQUALS_DELTA(m1, m2, size, epsilon) \
for (int j = 0; j < size; ++j) \
TS_ASSERT_DELTA(m1._data[j], m2._data[j], epsilon);
#define TS_ASSERT_MATRIX_DIFFERS_DELTA(m1, m2, size, epsilon) \
for (int j = 0; j < size; ++j) \
TS_ASSERT(!feq(m1._data[j], m2._data[j], epsilon));
class ScriptInterface; class ScriptInterface;
// Script-based testing setup (defined in test_setup.cpp). Defines TS_* functions. // Script-based testing setup (defined in test_setup.cpp). Defines TS_* functions.
void ScriptTestSetup(const ScriptInterface&); void ScriptTestSetup(const ScriptInterface&);

View File

@ -128,8 +128,7 @@ public:
// matrix multiplication/assignment // matrix multiplication/assignment
CMatrix3D& operator*=(const CMatrix3D &matrix) CMatrix3D& operator*=(const CMatrix3D &matrix)
{ {
Concatenate(matrix); return *this = *this * matrix;
return *this;
} }
// matrix scaling // matrix scaling

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2021 Wildfire Games. /* Copyright (C) 2024 Wildfire Games.
* This file is part of 0 A.D. * This file is part of 0 A.D.
* *
* 0 A.D. is free software: you can redistribute it and/or modify * 0 A.D. is free software: you can redistribute it and/or modify
@ -59,6 +59,44 @@ public:
} }
} }
void test_compoundMultiplication()
{
const float EPS = 2e-4f;
float invertibleData[16] = {
2.f, -3.f, 0.f, 1.f,
-2.f, 3.f, 0.f, 0.f,
1.f, -2.f, 1.f, 0.f,
1.f, -1.f, 0.f, 0.f
};
// Invertible matrix.
CMatrix3D a(invertibleData);
CMatrix3D n;
a.GetInverse(n);
a *= n;
CMatrix3D a2(invertibleData);
n *= a2;
TS_ASSERT_MATRIX_EQUALS_DELTA(a, n, 16, EPS);
// Non invertible matrix.
float nonInvertibleData[16] = {
2.f, -3.f, 0.f, 1.f,
-2.f, 3.f, 0.f, 0.f,
1.f, -2.f, 1.f, 0.f,
0.f, 0.f, 0.f, 0.f
};
CMatrix3D b(nonInvertibleData);
b.GetInverse(n);
b *= n;
CMatrix3D b2(nonInvertibleData);
n *= b2;
TS_ASSERT_MATRIX_DIFFERS_DELTA(b, n, 16, EPS);
}
void test_quats() void test_quats()
{ {
std::uniform_real_distribution<float> distribution01(0.0f, std::nextafter(1.0f, 2.0f)); std::uniform_real_distribution<float> distribution01(0.0f, std::nextafter(1.0f, 2.0f));