| 1 |
#include "gtest/gtest.h" |
| 2 |
|
| 3 |
#include "math/Matrix3.h" |
| 4 |
|
| 5 |
namespace test |
| 6 |
{ |
| 7 |
|
| 8 |
TEST(Matrix3Test, CreateIdentityMatrix) |
| 9 |
{ |
| 10 |
const auto identity = Matrix3::getIdentity(); |
| 11 |
EXPECT_EQ(identity, Matrix3::byRows(1, 0, 0, |
| 12 |
0, 1, 0, |
| 13 |
0, 0, 1)); |
| 14 |
} |
| 15 |
|
| 16 |
TEST(Matrix3Test, AssignMatrixComponents) |
| 17 |
{ |
| 18 |
Matrix3 identity; |
| 19 |
|
| 20 |
identity.xx() = 1; |
| 21 |
identity.xy() = 0; |
| 22 |
identity.xz() = 0; |
| 23 |
|
| 24 |
identity.yx() = 0; |
| 25 |
identity.yy() = 1; |
| 26 |
identity.yz() = 0; |
| 27 |
|
| 28 |
identity.zx() = 0; |
| 29 |
identity.zy() = 0; |
| 30 |
identity.zz() = 1; |
| 31 |
|
| 32 |
EXPECT_EQ(identity, Matrix3::getIdentity()); |
| 33 |
} |
| 34 |
|
| 35 |
TEST(Matrix3Test, ConstructMatrixByRows) |
| 36 |
{ |
| 37 |
auto m = Matrix3::byRows(1, 2.5, 3, |
| 38 |
0.34, 51, -6, |
| 39 |
7, 9, 17); |
| 40 |
|
| 41 |
// Check individual values |
| 42 |
EXPECT_EQ(m.xx(), 1); |
| 43 |
EXPECT_EQ(m.xy(), 0.34); |
| 44 |
EXPECT_EQ(m.xz(), 7); |
| 45 |
|
| 46 |
EXPECT_EQ(m.yx(), 2.5); |
| 47 |
EXPECT_EQ(m.yy(), 51); |
| 48 |
EXPECT_EQ(m.yz(), 9); |
| 49 |
|
| 50 |
EXPECT_EQ(m.zx(), 3); |
| 51 |
EXPECT_EQ(m.zy(), -6); |
| 52 |
EXPECT_EQ(m.zz(), 17); |
| 53 |
} |
| 54 |
|
| 55 |
TEST(Matrix3Test, MatrixEquality) |
| 56 |
{ |
| 57 |
Matrix3 m1 = Matrix3::byRows(1, 2, 3.5, |
| 58 |
5, -6, 17, |
| 59 |
9.01, 10, 11); |
| 60 |
Matrix3 m2 = m1; |
| 61 |
|
| 62 |
EXPECT_TRUE(m1 == m2); |
| 63 |
EXPECT_EQ(m1, m2); |
| 64 |
EXPECT_NE(m1, Matrix3::getIdentity()); |
| 65 |
EXPECT_NE(m1, Matrix3::getIdentity()); |
| 66 |
EXPECT_TRUE(m1 != Matrix3::getIdentity()); |
| 67 |
EXPECT_TRUE(m2 != Matrix3::getIdentity()); |
| 68 |
} |
| 69 |
|
| 70 |
TEST(Matrix3Test, MatrixMultiplication) |
| 71 |
{ |
| 72 |
auto a = Matrix3::byColumns(3, 5, 7, 11, 13, 17, 19, 23, 29); |
| 73 |
auto b = Matrix3::byColumns(61, 67, 71, 73, 79, 83, 89, 97, 101); |
| 74 |
|
| 75 |
// Check multiplied result |
| 76 |
auto c = a.getMultipliedBy(b); |
| 77 |
EXPECT_EQ(c, Matrix3::byColumns(2269, 2809, 3625, |
| 78 |
2665, 3301, 4261, |
| 79 |
3253, 4029, 5201)); |
| 80 |
|
| 81 |
// Multiplication has not changed original |
| 82 |
EXPECT_NE(a, c); |
| 83 |
|
| 84 |
// Check operator multiplication as well |
| 85 |
EXPECT_EQ(a * b, c); |
| 86 |
|
| 87 |
// Test Pre-Multiplication |
| 88 |
EXPECT_EQ(b.getMultipliedBy(a), a.getPremultipliedBy(b)) << "Matrix pre-multiplication mismatch"; |
| 89 |
} |
| 90 |
|
| 91 |
TEST(Matrix3Test, MatrixFullInverse) |
| 92 |
{ |
| 93 |
auto a = Matrix3::byColumns(3, 5, 7, 11, 13, 17, 19, 23, 29); |
| 94 |
|
| 95 |
auto inv = a.getFullInverse(); |
| 96 |
|
| 97 |
EXPECT_DOUBLE_EQ(inv.xx(), -7.0 / 10) << "Matrix inversion failed on xx"; |
| 98 |
EXPECT_DOUBLE_EQ(inv.xy(), 4.0 / 5) << "Matrix inversion failed on xy"; |
| 99 |
EXPECT_DOUBLE_EQ(inv.xz(), -3.0 / 10) << "Matrix inversion failed on xz"; |
| 100 |
|
| 101 |
EXPECT_DOUBLE_EQ(inv.yx(), 1.0 / 5) << "Matrix inversion failed on yx"; |
| 102 |
EXPECT_DOUBLE_EQ(inv.yy(), -23.0 / 10) << "Matrix inversion failed on yy"; |
| 103 |
EXPECT_DOUBLE_EQ(inv.yz(), 13.0 / 10) << "Matrix inversion failed on yz"; |
| 104 |
|
| 105 |
EXPECT_DOUBLE_EQ(inv.zx(), 3.0 / 10) << "Matrix inversion failed on zx"; |
| 106 |
EXPECT_DOUBLE_EQ(inv.zy(), 13.0 / 10) << "Matrix inversion failed on zy"; |
| 107 |
EXPECT_DOUBLE_EQ(inv.zz(), -4.0 / 5) << "Matrix inversion failed on zz"; |
| 108 |
} |
| 109 |
|
| 110 |
TEST(Matrix3Test, MatrixTransformation) |
| 111 |
{ |
| 112 |
auto a = Matrix3::byColumns(3, 5, 7, 11, 13, 17, 19, 23, 29); |
| 113 |
|
| 114 |
{ |
| 115 |
Vector2 v(61, 67); |
| 116 |
|
| 117 |
Vector2 transformed = a.transformPoint(v); |
| 118 |
|
| 119 |
EXPECT_EQ(transformed.x(), 939) << "Vector2 transformation failed"; |
| 120 |
EXPECT_EQ(transformed.y(), 1199) << "Vector2 transformation failed"; |
| 121 |
|
| 122 |
EXPECT_EQ(a * v, a.transformPoint(v)); |
| 123 |
} |
| 124 |
|
| 125 |
{ |
| 126 |
Vector3 vector(83, 89, 97); |
| 127 |
Vector3 transformed = a.transform(vector); |
| 128 |
|
| 129 |
EXPECT_EQ(transformed.x(), 3071) << "Vector3 transformation failed"; |
| 130 |
EXPECT_EQ(transformed.y(), 3803) << "Vector3 transformation failed"; |
| 131 |
EXPECT_EQ(transformed.z(), 4907) << "Vector3 transformation failed"; |
| 132 |
|
| 133 |
EXPECT_EQ(a * vector, a.transform(vector)); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
TEST(Matrix3Test, ConstructTranslationMatrix) |
| 138 |
{ |
| 139 |
const Vector2 translation(1.5, -2939); |
| 140 |
auto tm = Matrix3::getTranslation(translation); |
| 141 |
|
| 142 |
EXPECT_EQ(tm, Matrix3::byRows(1, 0, translation.x(), 0, 1, translation.y(), 0, 0, 1)); |
| 143 |
} |
| 144 |
|
| 145 |
TEST(Matrix3Test, ConstructRotationMatrix) |
| 146 |
{ |
| 147 |
const double angle = degrees_to_radians(15); |
| 148 |
auto tm = Matrix3::getRotation(angle); |
| 149 |
|
| 150 |
EXPECT_EQ(tm, Matrix3::byRows(cos(angle), -sin(angle), 0, |
| 151 |
sin(angle), cos(angle), 0, |
| 152 |
0, 0, 1)); |
| 153 |
} |
| 154 |
|
| 155 |
TEST(Matrix3Test, ConstructScaleMatrix) |
| 156 |
{ |
| 157 |
const Vector2 scale(1.5, -3.2); |
| 158 |
auto tm = Matrix3::getScale(scale); |
| 159 |
|
| 160 |
EXPECT_EQ(tm, Matrix3::byRows(scale.x(), 0, 0, |
| 161 |
0, scale.y(), 0, |
| 162 |
0, 0, 1)); |
| 163 |
} |
| 164 |
|
| 165 |
} |