| 1 |
#include "gtest/gtest.h" |
| 2 |
|
| 3 |
#include "math/Matrix4.h" |
| 4 |
#include "MatrixUtils.h" |
| 5 |
#include "pivot.h" |
| 6 |
|
| 7 |
namespace test |
| 8 |
{ |
| 9 |
|
| 10 |
namespace |
| 11 |
{ |
| 12 |
double angle = 30.0; |
| 13 |
|
| 14 |
double cosAngle = cos(degrees_to_radians(angle)); |
| 15 |
double sinAngle = sin(degrees_to_radians(angle)); |
| 16 |
double EPSILON = 0.00001f; |
| 17 |
|
| 18 |
// EXPECT that two Vector3s are close to each other |
| 19 |
void expectNear(const Vector3& v1, const Vector3& v2, double eps = EPSILON) |
| 20 |
{ |
| 21 |
EXPECT_NEAR(v1.x(), v2.x(), eps); |
| 22 |
EXPECT_NEAR(v1.y(), v2.y(), eps); |
| 23 |
EXPECT_NEAR(v1.z(), v2.z(), eps); |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
TEST(Matrix4Test, CreateIdentityMatrix) |
| 28 |
{ |
| 29 |
const Matrix4 identity = Matrix4::getIdentity(); |
| 30 |
EXPECT_EQ(identity, Matrix4::byRows(1, 0, 0, 0, |
| 31 |
0, 1, 0, 0, |
| 32 |
0, 0, 1, 0, |
| 33 |
0, 0, 0, 1)); |
| 34 |
} |
| 35 |
|
| 36 |
TEST(Matrix4Test, AssignMatrixComponents) |
| 37 |
{ |
| 38 |
Matrix4 identity; |
| 39 |
|
| 40 |
identity.xx() = 1; |
| 41 |
identity.xy() = 0; |
| 42 |
identity.xz() = 0; |
| 43 |
identity.xw() = 0; |
| 44 |
|
| 45 |
identity.yx() = 0; |
| 46 |
identity.yy() = 1; |
| 47 |
identity.yz() = 0; |
| 48 |
identity.yw() = 0; |
| 49 |
|
| 50 |
identity.zx() = 0; |
| 51 |
identity.zy() = 0; |
| 52 |
identity.zz() = 1; |
| 53 |
identity.zw() = 0; |
| 54 |
|
| 55 |
identity.tx() = 0; |
| 56 |
identity.ty() = 0; |
| 57 |
identity.tz() = 0; |
| 58 |
identity.tw() = 1; |
| 59 |
|
| 60 |
EXPECT_EQ(identity, Matrix4::getIdentity()); |
| 61 |
} |
| 62 |
|
| 63 |
TEST(Matrix4Test, ConstructMatrixByRows) |
| 64 |
{ |
| 65 |
auto m = Matrix4::byRows(1, 2.5, 3, 0.34, |
| 66 |
51, -6, 7, 9, |
| 67 |
9, 100, 11, 20, |
| 68 |
13.11, 24, 15, 32); |
| 69 |
|
| 70 |
// Check individual values |
| 71 |
EXPECT_EQ(m.xx(), 1); |
| 72 |
EXPECT_EQ(m.xy(), 51); |
| 73 |
EXPECT_EQ(m.xz(), 9); |
| 74 |
EXPECT_EQ(m.xw(), 13.11); |
| 75 |
|
| 76 |
EXPECT_EQ(m.yx(), 2.5); |
| 77 |
EXPECT_EQ(m.yy(), -6); |
| 78 |
EXPECT_EQ(m.yz(), 100); |
| 79 |
EXPECT_EQ(m.yw(), 24); |
| 80 |
|
| 81 |
EXPECT_EQ(m.zx(), 3); |
| 82 |
EXPECT_EQ(m.zy(), 7); |
| 83 |
EXPECT_EQ(m.zz(), 11); |
| 84 |
EXPECT_EQ(m.zw(), 15); |
| 85 |
|
| 86 |
EXPECT_EQ(m.tx(), 0.34); |
| 87 |
EXPECT_EQ(m.ty(), 9); |
| 88 |
EXPECT_EQ(m.tz(), 20); |
| 89 |
EXPECT_EQ(m.tw(), 32); |
| 90 |
} |
| 91 |
|
| 92 |
TEST(Matrix4Test, ConstructTranslationMatrix) |
| 93 |
{ |
| 94 |
const Vector3 TRANS(1.5, -2939, 357); |
| 95 |
Matrix4 tm = Matrix4::getTranslation(TRANS); |
| 96 |
|
| 97 |
EXPECT_EQ(tm, Matrix4::byRows(1, 0, 0, TRANS.x(), |
| 98 |
0, 1, 0, TRANS.y(), |
| 99 |
0, 0, 1, TRANS.z(), |
| 100 |
0, 0, 0, 1)); |
| 101 |
EXPECT_EQ(tm.translation(), TRANS); |
| 102 |
} |
| 103 |
|
| 104 |
TEST(Matrix4Test, ConstructScaleMatrix) |
| 105 |
{ |
| 106 |
const Vector3 SCALE(0.75, 1.25, 960); |
| 107 |
Matrix4 sm = Matrix4::getScale(SCALE); |
| 108 |
|
| 109 |
EXPECT_EQ(sm, Matrix4::byRows(SCALE.x(), 0, 0, 0, |
| 110 |
0, SCALE.y(), 0, 0, |
| 111 |
0, 0, SCALE.z(), 0, |
| 112 |
0, 0, 0, 1)); |
| 113 |
EXPECT_EQ(sm.getScale(), SCALE); |
| 114 |
} |
| 115 |
|
| 116 |
TEST(Matrix4Test, AccessMatrixColumnVectors) |
| 117 |
{ |
| 118 |
Matrix4 m = Matrix4::byRows(1, 4, 8, -5, |
| 119 |
2, 9, 7, 13, |
| 120 |
11, -2, 10, 14, |
| 121 |
26, -100, 0.5, 3); |
| 122 |
|
| 123 |
// Read column values |
| 124 |
EXPECT_EQ(m.xCol3(), Vector3(1, 2, 11)); |
| 125 |
EXPECT_EQ(m.yCol3(), Vector3(4, 9, -2)); |
| 126 |
EXPECT_EQ(m.zCol3(), Vector3(8, 7, 10)); |
| 127 |
EXPECT_EQ(m.tCol(), Vector4(-5, 13, 14, 3)); |
| 128 |
EXPECT_EQ(m.translation(), Vector3(-5, 13, 14)); |
| 129 |
} |
| 130 |
|
| 131 |
TEST(MatrixTest, SetMatrixColumnVectors) |
| 132 |
{ |
| 133 |
Matrix4 m = Matrix4::byRows(1, 1.5, 2, 8, |
| 134 |
-1, 3, 4.5, 678, |
| 135 |
0.1, -0.8, 16, 2, |
| 136 |
75, 0, 1, -9); |
| 137 |
|
| 138 |
// Overwrite X column |
| 139 |
m.setXCol({5, -5, 9}); |
| 140 |
EXPECT_EQ(m, Matrix4::byRows(5, 1.5, 2, 8, |
| 141 |
-5, 3, 4.5, 678, |
| 142 |
9, -0.8, 16, 2, |
| 143 |
75, 0, 1, -9)); |
| 144 |
|
| 145 |
// Overwrite Y column |
| 146 |
m.setYCol({27, 14, 600}); |
| 147 |
EXPECT_EQ(m, Matrix4::byRows(5, 27, 2, 8, |
| 148 |
-5, 14, 4.5, 678, |
| 149 |
9, 600, 16, 2, |
| 150 |
75, 0, 1, -9)); |
| 151 |
|
| 152 |
// Overwrite Z column |
| 153 |
m.setZCol({-100, 150, 0.5}); |
| 154 |
EXPECT_EQ(m, Matrix4::byRows(5, 27, -100, 8, |
| 155 |
-5, 14, 150, 678, |
| 156 |
9, 600, 0.5, 2, |
| 157 |
75, 0, 1, -9)); |
| 158 |
|
| 159 |
// Set translation column |
| 160 |
m.setTranslation({-0.8, -6, 27}); |
| 161 |
EXPECT_EQ(m, Matrix4::byRows(5, 27, -100, -0.8, |
| 162 |
-5, 14, 150, -6, |
| 163 |
9, 600, 0.5, 27, |
| 164 |
75, 0, 1, -9)); |
| 165 |
} |
| 166 |
|
| 167 |
TEST(Matrix4Test, MatrixRawArrayData) |
| 168 |
{ |
| 169 |
Matrix4 m = Matrix4::byRows(1, 0.2, 35, 4, |
| 170 |
5, -6, 17, 300, |
| 171 |
3.02, 10, -11, 12.4, |
| 172 |
20, 17, 3001, -4.5); |
| 173 |
|
| 174 |
// Confirm data can be accessed as a packed C array of doubles (e.g. for |
| 175 |
// OpenGL methods) |
| 176 |
double* data = static_cast<double*>(m); |
| 177 |
EXPECT_EQ(data[0], 1); |
| 178 |
EXPECT_EQ(data[1], 5); |
| 179 |
EXPECT_EQ(data[2], 3.02); |
| 180 |
EXPECT_EQ(data[3], 20); |
| 181 |
EXPECT_EQ(data[4], 0.2); |
| 182 |
EXPECT_EQ(data[5], -6); |
| 183 |
EXPECT_EQ(data[6], 10); |
| 184 |
EXPECT_EQ(data[7], 17); |
| 185 |
EXPECT_EQ(data[8], 35); |
| 186 |
EXPECT_EQ(data[9], 17); |
| 187 |
EXPECT_EQ(data[10], -11); |
| 188 |
EXPECT_EQ(data[11], 3001); |
| 189 |
EXPECT_EQ(data[12], 4); |
| 190 |
EXPECT_EQ(data[13], 300); |
| 191 |
EXPECT_EQ(data[14], 12.4); |
| 192 |
EXPECT_EQ(data[15], -4.5); |
| 193 |
} |
| 194 |
|
| 195 |
TEST(Matrix4Test, MatrixEquality) |
| 196 |
{ |
| 197 |
Matrix4 m1 = Matrix4::byRows(1, 2, 3.5, 4, |
| 198 |
5, -6, 17, 800, |
| 199 |
9.01, 10, 11, 12.4, |
| 200 |
200, -10, 300, 400); |
| 201 |
Matrix4 m2 = m1; |
| 202 |
EXPECT_TRUE(m1 == m2); |
| 203 |
EXPECT_EQ(m1, m2); |
| 204 |
EXPECT_TRUE(m1 != Matrix4::getIdentity()); |
| 205 |
EXPECT_TRUE(m2 != Matrix4::getIdentity()); |
| 206 |
} |
| 207 |
|
| 208 |
TEST(Matrix4Test, MatrixTranspose) |
| 209 |
{ |
| 210 |
Matrix4 m = Matrix4::byRows(1, 2, 3, 4, |
| 211 |
5, 6, 7, 8, |
| 212 |
9, 10, 11, 12, |
| 213 |
13, 14, 15, 16); |
| 214 |
Matrix4 mT = Matrix4::byRows(1, 5, 9, 13, |
| 215 |
2, 6, 10, 14, |
| 216 |
3, 7, 11, 15, |
| 217 |
4, 8, 12, 16); |
| 218 |
|
| 219 |
// Return transposed copy |
| 220 |
EXPECT_EQ(m.getTransposed(), mT); |
| 221 |
|
| 222 |
// Transpose in place |
| 223 |
EXPECT_NE(m, mT); |
| 224 |
m.transpose(); |
| 225 |
EXPECT_EQ(m, mT); |
| 226 |
} |
| 227 |
|
| 228 |
TEST(Matrix4Test, ConvertDegreesAndRadians) |
| 229 |
{ |
| 230 |
math::Degrees thirtyD(30); |
| 231 |
EXPECT_DOUBLE_EQ(thirtyD.asDegrees(), 30); |
| 232 |
EXPECT_DOUBLE_EQ(thirtyD.asRadians(), math::PI / 6.0); |
| 233 |
|
| 234 |
math::Radians twoPiBy3R(2 * math::PI / 3.0); |
| 235 |
EXPECT_DOUBLE_EQ(twoPiBy3R.asDegrees(), 120); |
| 236 |
EXPECT_DOUBLE_EQ(twoPiBy3R.asRadians(), 2 * math::PI / 3.0); |
| 237 |
} |
| 238 |
|
| 239 |
TEST(Matrix4Test, MatrixRotationAboutZDegrees) |
| 240 |
{ |
| 241 |
math::Degrees angle(60.0); |
| 242 |
double cosAngle = cos(angle.asRadians()); |
| 243 |
double sinAngle = sin(angle.asRadians()); |
| 244 |
|
| 245 |
// Test Z rotation |
| 246 |
auto zRot = Matrix4::getRotationAboutZ(angle); |
| 247 |
expectNear(zRot, Matrix4::byRows(cosAngle, -sinAngle, 0, 0, |
| 248 |
sinAngle, cosAngle, 0, 0, |
| 249 |
0, 0, 1, 0, |
| 250 |
0, 0, 0, 1)); |
| 251 |
} |
| 252 |
|
| 253 |
TEST(Matrix4Test, MatrixRotationAboutZRadians) |
| 254 |
{ |
| 255 |
double angle = math::PI / 3.0; // 60 degrees in radians |
| 256 |
double cosAngle = cos(angle); |
| 257 |
double sinAngle = sin(angle); |
| 258 |
|
| 259 |
// Test Z rotation |
| 260 |
auto zRot = Matrix4::getRotationAboutZ(math::Radians(angle)); |
| 261 |
expectNear(zRot, Matrix4::byRows(cosAngle, -sinAngle, 0, 0, |
| 262 |
sinAngle, cosAngle, 0, 0, |
| 263 |
0, 0, 1, 0, |
| 264 |
0, 0, 0, 1)); |
| 265 |
} |
| 266 |
|
| 267 |
TEST(Matrix4Test, MatrixRotationForAxisAngle) |
| 268 |
{ |
| 269 |
// Rotation with a zero angle should do nothing |
| 270 |
Matrix4 noRot = Matrix4::getRotation(Vector3(0, 0, 1), 0); |
| 271 |
EXPECT_EQ(noRot, Matrix4::getIdentity()); |
| 272 |
|
| 273 |
// Simple 90 degree rotation about Z axis (note this Matrix4::getRotation |
| 274 |
// method always expects radians) |
| 275 |
Matrix4 z90 = Matrix4::getRotation(Vector3(0, 0, 1), math::PI / 2); |
| 276 |
expectNear(z90, Matrix4::getRotationAboutZ(math::Degrees(-90))); |
| 277 |
expectNear(z90 * Vector3(1, 0, 0), Vector3(0, -1, 0)); |
| 278 |
|
| 279 |
// Rotate unit X vector 180 degrees around a 45 degree axis, which should |
| 280 |
// map it on to the unit Y vector. |
| 281 |
Vector3 axis = Vector3(1, 1, 0).getNormalised(); |
| 282 |
Matrix4 rot45 = Matrix4::getRotation(axis, math::PI); |
| 283 |
expectNear(rot45 * Vector3(1, 0, 0), Vector3(0, 1, 0)); |
| 284 |
} |
| 285 |
|
| 286 |
TEST(Matrix4Test, MatrixRotationForEulerXYZDegrees) |
| 287 |
{ |
| 288 |
// Test euler angle constructors |
| 289 |
Vector3 euler(30, -55, 75); |
| 290 |
|
| 291 |
// Convert degrees to radians |
| 292 |
double pi = 3.141592653589793238462643383f; |
| 293 |
double cx = cos(euler[0] * math::PI / 180.0f); |
| 294 |
double sx = sin(euler[0] * math::PI / 180.0f); |
| 295 |
double cy = cos(euler[1] * math::PI / 180.0f); |
| 296 |
double sy = sin(euler[1] * math::PI / 180.0f); |
| 297 |
double cz = cos(euler[2] * math::PI / 180.0f); |
| 298 |
double sz = sin(euler[2] * math::PI / 180.0f); |
| 299 |
|
| 300 |
Matrix4 eulerXYZ = Matrix4::getRotationForEulerXYZDegrees(euler); |
| 301 |
|
| 302 |
EXPECT_DOUBLE_EQ(eulerXYZ.xx(), cy * cz) <<"Matrix getRotationForEulerXYZDegrees failed"; |
| 303 |
EXPECT_DOUBLE_EQ(eulerXYZ.xy(), cy * sz) <<"Matrix getRotationForEulerXYZDegrees failed"; |
| 304 |
EXPECT_DOUBLE_EQ(eulerXYZ.xz(), -sy) <<"Matrix getRotationForEulerXYZDegrees failed"; |
| 305 |
EXPECT_DOUBLE_EQ(eulerXYZ.xw(), 0) <<"Matrix getRotationForEulerXYZDegrees failed"; |
| 306 |
|
| 307 |
EXPECT_DOUBLE_EQ(eulerXYZ.yx(), sx * sy * cz - cx * sz) <<"Matrix getRotationForEulerXYZDegrees failed"; |
| 308 |
EXPECT_DOUBLE_EQ(eulerXYZ.yy(), sx * sy * sz + cx * cz) <<"Matrix getRotationForEulerXYZDegrees failed"; |
| 309 |
EXPECT_DOUBLE_EQ(eulerXYZ.yz(), sx * cy) <<"Matrix getRotationForEulerXYZDegrees failed"; |
| 310 |
EXPECT_DOUBLE_EQ(eulerXYZ.yw(), 0) <<"Matrix getRotationForEulerXYZDegrees failed"; |
| 311 |
|
| 312 |
EXPECT_DOUBLE_EQ(eulerXYZ.zx(), cx * sy * cz + sx * sz) <<"Matrix getRotationForEulerXYZDegrees failed"; |
| 313 |
EXPECT_DOUBLE_EQ(eulerXYZ.zy(), cx * sy * sz - sx * cz) <<"Matrix getRotationForEulerXYZDegrees failed"; |
| 314 |
EXPECT_DOUBLE_EQ(eulerXYZ.zz(), cx * cy) <<"Matrix getRotationForEulerXYZDegrees failed"; |
| 315 |
EXPECT_DOUBLE_EQ(eulerXYZ.zw(), 0) <<"Matrix getRotationForEulerXYZDegrees failed"; |
| 316 |
|
| 317 |
EXPECT_DOUBLE_EQ(eulerXYZ.tx(), 0) <<"Matrix getRotationForEulerXYZDegrees failed"; |
| 318 |
EXPECT_DOUBLE_EQ(eulerXYZ.ty(), 0) <<"Matrix getRotationForEulerXYZDegrees failed"; |
| 319 |
EXPECT_DOUBLE_EQ(eulerXYZ.tz(), 0) <<"Matrix getRotationForEulerXYZDegrees failed"; |
| 320 |
EXPECT_DOUBLE_EQ(eulerXYZ.tw(), 1) <<"Matrix getRotationForEulerXYZDegrees failed"; |
| 321 |
|
| 322 |
// Test Euler Angle retrieval (XYZ) |
| 323 |
Vector3 testEuler = eulerXYZ.getEulerAnglesXYZDegrees(); |
| 324 |
|
| 325 |
EXPECT_DOUBLE_EQ(testEuler.x(), euler.x()) << "getEulerAnglesXYZDegrees fault at x()"; |
| 326 |
EXPECT_DOUBLE_EQ(testEuler.y(), euler.y()) << "getEulerAnglesXYZDegrees fault at y()"; |
| 327 |
EXPECT_DOUBLE_EQ(testEuler.z(), euler.z()) << "getEulerAnglesXYZDegrees fault at z()"; |
| 328 |
} |
| 329 |
|
| 330 |
TEST(Matrix4Test, MatrixMultiplication) |
| 331 |
{ |
| 332 |
auto a = Matrix4::byColumns(3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59); |
| 333 |
auto b = Matrix4::byColumns(61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137); |
| 334 |
|
| 335 |
// Check multiplied result |
| 336 |
auto c = a.getMultipliedBy(b); |
| 337 |
EXPECT_EQ(c, Matrix4::byColumns(6252, 7076, 8196, 9430, |
| 338 |
8068, 9124, 10564, 12150, |
| 339 |
9432, 10696, 12400, 14298, |
| 340 |
11680, 13224, 15312, 17618)); |
| 341 |
|
| 342 |
// Multiplication has not changed original |
| 343 |
EXPECT_NE(a, c); |
| 344 |
|
| 345 |
// Check operator multiplication as well |
| 346 |
EXPECT_EQ(a * b, c); |
| 347 |
|
| 348 |
// Test Pre-Multiplication |
| 349 |
EXPECT_EQ(b.getMultipliedBy(a), a.getPremultipliedBy(b)) << "Matrix pre-multiplication mismatch"; |
| 350 |
} |
| 351 |
|
| 352 |
TEST(Matrix4Test, MatrixTransformation) |
| 353 |
{ |
| 354 |
auto a = Matrix4::byColumns(3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59); |
| 355 |
|
| 356 |
{ |
| 357 |
Vector3 v(61, 67, 71); |
| 358 |
|
| 359 |
Vector3 transformed = a.transformPoint(v); |
| 360 |
|
| 361 |
EXPECT_EQ(transformed.x(), 3156) << "Vector3 transformation failed"; |
| 362 |
EXPECT_EQ(transformed.y(), 3692) << "Vector3 transformation failed"; |
| 363 |
EXPECT_EQ(transformed.z(), 4380) << "Vector3 transformation failed"; |
| 364 |
|
| 365 |
Vector3 transformedDir = a.transformDirection(v); |
| 366 |
|
| 367 |
EXPECT_EQ(transformedDir.x(), 3113) << "Vector3 direction transformation failed"; |
| 368 |
EXPECT_EQ(transformedDir.y(), 3645) << "Vector3 direction transformation failed"; |
| 369 |
EXPECT_EQ(transformedDir.z(), 4327) << "Vector3 direction transformation failed"; |
| 370 |
} |
| 371 |
|
| 372 |
{ |
| 373 |
Vector4 vector(83, 89, 97, 101); |
| 374 |
Vector4 transformed = a.transform(vector); |
| 375 |
|
| 376 |
EXPECT_EQ(transformed.x(), 8562) << "Vector4 transformation failed"; |
| 377 |
EXPECT_EQ(transformed.y(), 9682) << "Vector4 transformation failed"; |
| 378 |
EXPECT_EQ(transformed.z(), 11214) << "Vector4 transformation failed"; |
| 379 |
EXPECT_EQ(transformed.w(), 12896) << "Vector4 transformation failed"; |
| 380 |
} |
| 381 |
|
| 382 |
EXPECT_EQ(a.tCol().x(), 43) << "Matrix4::t failed"; |
| 383 |
EXPECT_EQ(a.tCol().y(), 47) << "Matrix4::t failed"; |
| 384 |
EXPECT_EQ(a.tCol().z(), 53) << "Matrix4::t failed"; |
| 385 |
} |
| 386 |
|
| 387 |
TEST(Matrix4Test, MatrixTransformVectorEqualsMultiplication) |
| 388 |
{ |
| 389 |
const Vector3 TRANSLATION(2, 5, -7); |
| 390 |
auto t = Matrix4::getTranslation(TRANSLATION) |
| 391 |
* Matrix4::getRotation(Vector3(0, 1, 0), 2.1) |
| 392 |
* Matrix4::getScale(Vector3(2, 2.5, 0.75)); |
| 393 |
|
| 394 |
// transformPoint must be equivalent to a multiplication |
| 395 |
Vector3 vec3(16, -32, 0.05); |
| 396 |
EXPECT_EQ(t * vec3, t.transformPoint(vec3)); |
| 397 |
|
| 398 |
// transformDirection does NOT give the same result, because it ignores |
| 399 |
// translation (vector's W coordinate is assumed 0). |
| 400 |
EXPECT_NE(t * vec3, t.transformDirection(vec3)); |
| 401 |
|
| 402 |
// If we transform the direction then add the translation manually, we |
| 403 |
// should get the same result as transformPoint. |
| 404 |
EXPECT_EQ(t * vec3, t.transformDirection(vec3) + TRANSLATION); |
| 405 |
|
| 406 |
// transform() must be equivalent to a multiplication with a full Vector4 |
| 407 |
Vector4 vec4(128, 0.025, -8198, 0.5); |
| 408 |
EXPECT_EQ(t * vec4, t.transform(vec4)); |
| 409 |
|
| 410 |
// transformPoint() and transformDirection() must be equivalent to a |
| 411 |
// multiplication with the appropriate Vector4 (W == 0 or W == 1). |
| 412 |
EXPECT_EQ((t * Vector4(vec3, 1)).getVector3(), t.transformPoint(vec3)); |
| 413 |
EXPECT_EQ((t * Vector4(vec3, 0)).getVector3(), t.transformDirection(vec3)); |
| 414 |
} |
| 415 |
|
| 416 |
TEST(Matrix4Test, MatrixScaleAffineInverse) |
| 417 |
{ |
| 418 |
// Construct a scale matrix |
| 419 |
Vector3 SCALE(2, 4, 8); |
| 420 |
Matrix4 scaleMat = Matrix4::getScale(SCALE); |
| 421 |
EXPECT_EQ(scaleMat.getScale(), SCALE); |
| 422 |
|
| 423 |
// Get the affine inverse |
| 424 |
Matrix4 inverse = scaleMat.getInverse(); |
| 425 |
EXPECT_NE(inverse, scaleMat); |
| 426 |
scaleMat.invert(); |
| 427 |
EXPECT_EQ(scaleMat, inverse); |
| 428 |
|
| 429 |
// Inverse must have inverted scale factors |
| 430 |
EXPECT_EQ(inverse.getScale(), |
| 431 |
Vector3(1.0 / SCALE.x(), 1.0 / SCALE.y(), 1.0 / SCALE.z())); |
| 432 |
} |
| 433 |
|
| 434 |
TEST(Matrix4Test, MatrixTranslationAffineInverse) |
| 435 |
{ |
| 436 |
// Construct a translation matrix |
| 437 |
Vector3 TRANS(4, 32, -8); |
| 438 |
Matrix4 transMat = Matrix4::getTranslation(TRANS); |
| 439 |
EXPECT_EQ(transMat.getScale(), Vector3(1, 1, 1)); |
| 440 |
|
| 441 |
// Get the affine inverse |
| 442 |
Matrix4 inverse = transMat.getInverse(); |
| 443 |
EXPECT_NE(inverse, transMat); |
| 444 |
transMat.invert(); |
| 445 |
EXPECT_EQ(transMat, inverse); |
| 446 |
|
| 447 |
// Check resulting matrix |
| 448 |
EXPECT_EQ(inverse, Matrix4::byRows(1, 0, 0, -TRANS.x(), |
| 449 |
0, 1, 0, -TRANS.y(), |
| 450 |
0, 0, 1, -TRANS.z(), |
| 451 |
0, 0, 0, 1)); |
| 452 |
} |
| 453 |
|
| 454 |
TEST(Matrix4Test, MatrixRotationAffineInverse) |
| 455 |
{ |
| 456 |
// Construct a translation matrix |
| 457 |
const math::Degrees ANGLE(60); |
| 458 |
Matrix4 rotMat = Matrix4::getRotationAboutZ(ANGLE); |
| 459 |
|
| 460 |
// Get the affine inverse |
| 461 |
Matrix4 inverse = rotMat.getInverse(); |
| 462 |
|
| 463 |
// Inverse should be a rotation in the other direction |
| 464 |
const math::Degrees REV_ANGLE(-60); |
| 465 |
Matrix4 backRotMat = Matrix4::getRotationAboutZ(REV_ANGLE); |
| 466 |
expectNear(inverse, backRotMat); |
| 467 |
} |
| 468 |
|
| 469 |
TEST(Matrix4Test, MatrixAffineInverseMatchesFullInverse) |
| 470 |
{ |
| 471 |
// Create an affine transformation |
| 472 |
Matrix4 affTrans = Matrix4::getRotationAboutZ(math::Degrees(78)) |
| 473 |
* Matrix4::getScale(Vector3(2, 0.5, 1.2)) |
| 474 |
* Matrix4::getTranslation(Vector3(50, -8, 0)); |
| 475 |
|
| 476 |
// Since this is an affine transformation, the inverse should be the same as |
| 477 |
// the affine inverse |
| 478 |
expectNear(affTrans.getInverse(), affTrans.getFullInverse()); |
| 479 |
|
| 480 |
// Make a non-affine transformation |
| 481 |
Matrix4 nonAffTrans = affTrans; |
| 482 |
nonAffTrans.xw() = 0.5; |
| 483 |
nonAffTrans.tw() = 2; |
| 484 |
|
| 485 |
// This time the affine inverse and full inverse should be different. |
| 486 |
// Inspect a couple of values to make sure (there is no convenient assertion |
| 487 |
// for "two matrices are NOT nearly equal", but we can subtract them and |
| 488 |
// check that the differences exceed some threshold) |
| 489 |
Matrix4 aInv = nonAffTrans.getInverse(); |
| 490 |
Matrix4 fInv = nonAffTrans.getFullInverse(); |
| 491 |
Matrix4 diffInv = aInv - fInv; |
| 492 |
EXPECT_GT(diffInv.xx(), 0.1); |
| 493 |
EXPECT_GT(diffInv.yx(), 0.5); |
| 494 |
EXPECT_GT(diffInv.ty(), 8); |
| 495 |
} |
| 496 |
|
| 497 |
TEST(Matrix4Test, MatrixFullInverse) |
| 498 |
{ |
| 499 |
auto a = Matrix4::byColumns(3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59); |
| 500 |
|
| 501 |
auto inv = a.getFullInverse(); |
| 502 |
|
| 503 |
EXPECT_DOUBLE_EQ(inv.xx(), 0.39285714285714285) << "Matrix inversion failed on xx"; |
| 504 |
EXPECT_DOUBLE_EQ(inv.xy(), -0.71428571428571419) << "Matrix inversion failed on xy"; |
| 505 |
EXPECT_DOUBLE_EQ(inv.xz(), -0.3214285714285714) << "Matrix inversion failed on xz"; |
| 506 |
EXPECT_DOUBLE_EQ(inv.xw(), 0.42857142857142855) << "Matrix inversion failed on xw"; |
| 507 |
|
| 508 |
EXPECT_DOUBLE_EQ(inv.yx(), -0.27678571428571425) << "Matrix inversion failed on yx"; |
| 509 |
EXPECT_DOUBLE_EQ(inv.yy(), 0.4464285714285714) << "Matrix inversion failed on yy"; |
| 510 |
EXPECT_DOUBLE_EQ(inv.yz(), -0.33035714285714285) << "Matrix inversion failed on yz"; |
| 511 |
EXPECT_DOUBLE_EQ(inv.yw(), 0.10714285714285714) << "Matrix inversion failed on yw"; |
| 512 |
|
| 513 |
EXPECT_DOUBLE_EQ(inv.zx(), -0.6696428571428571) << "Matrix inversion failed on zx"; |
| 514 |
EXPECT_DOUBLE_EQ(inv.zy(), 0.6607142857142857) << "Matrix inversion failed on zy"; |
| 515 |
EXPECT_DOUBLE_EQ(inv.zz(), 0.99107142857142849) << "Matrix inversion failed on zz"; |
| 516 |
EXPECT_DOUBLE_EQ(inv.zw(), -0.8214285714285714) << "Matrix inversion failed on zw"; |
| 517 |
|
| 518 |
EXPECT_DOUBLE_EQ(inv.tx(), 0.5357142857142857) << "Matrix inversion failed on tx"; |
| 519 |
EXPECT_DOUBLE_EQ(inv.ty(), -0.42857142857142855) << "Matrix inversion failed on ty"; |
| 520 |
EXPECT_DOUBLE_EQ(inv.tz(), -0.39285714285714285) << "Matrix inversion failed on tz"; |
| 521 |
EXPECT_DOUBLE_EQ(inv.tw(), 0.3571428571428571) << "Matrix inversion failed on tw"; |
| 522 |
} |
| 523 |
|
| 524 |
TEST(Matrix4Test, MatrixTranslateBy) |
| 525 |
{ |
| 526 |
const Vector3 TRANS(27, -16, 0.84); |
| 527 |
|
| 528 |
// Add a translation to a matrix which already includes a transformation |
| 529 |
const Vector3 SCALE(2, 3, 2); |
| 530 |
Matrix4 m = Matrix4::getScale(SCALE); |
| 531 |
m.translateBy(TRANS); |
| 532 |
|
| 533 |
// Because translateBy() does a post-multiplication, the scale will affect |
| 534 |
// the translation |
| 535 |
EXPECT_EQ(m.translation(), SCALE*TRANS /* Componentwise product */); |
| 536 |
EXPECT_EQ(m, Matrix4::byRows(2, 0, 0, SCALE.x() * TRANS.x(), |
| 537 |
0, 3, 0, SCALE.y() * TRANS.y(), |
| 538 |
0, 0, 2, SCALE.z() * TRANS.z(), |
| 539 |
0, 0, 0, 1)); |
| 540 |
|
| 541 |
// Check the non-mutating getTranslatedBy as well |
| 542 |
const Matrix4 orig = Matrix4::getScale(SCALE); |
| 543 |
EXPECT_EQ(orig.getTranslatedBy(TRANS), m); |
| 544 |
EXPECT_NE(orig, m); |
| 545 |
|
| 546 |
// Add further translations and ensure the result is cumulative |
| 547 |
const Vector3 ONE(1, 1, 1); |
| 548 |
m.translateBy(ONE); |
| 549 |
expectNear(m.translation(), SCALE * (TRANS+ONE)); |
| 550 |
m.translateBy(-TRANS); |
| 551 |
expectNear(m.translation(), SCALE * ONE); |
| 552 |
} |
| 553 |
|
| 554 |
TEST(Matrix4Test, GetInverseScale) |
| 555 |
{ |
| 556 |
// Create an arbitrary transformation |
| 557 |
Matrix4 m = Matrix4::getRotationAboutZ(math::Degrees(15)) |
| 558 |
* Matrix4::getScale(Vector3(3, 2.5, 8.2)) |
| 559 |
* Matrix4::getTranslation(Vector3(10, 30, -61)); |
| 560 |
|
| 561 |
// Get the inverse scale |
| 562 |
Matrix4 invSc = getInverseScale(m); |
| 563 |
|
| 564 |
// Result should be a diagonal matrix containing only a scale |
| 565 |
EXPECT_TRUE(math::isNear(invSc.xCol3(), Vector3(1.0 / 3, 0, 0), 1E-6)); |
| 566 |
EXPECT_TRUE(math::isNear(invSc.yCol3(), Vector3(0, 1.0 / 2.5, 0), 1E-6)); |
| 567 |
EXPECT_TRUE(math::isNear(invSc.zCol3(), Vector3(0, 0, 1.0 / 8.2), 1E-6)); |
| 568 |
EXPECT_TRUE(math::isNear(invSc.tCol(), Vector4(0, 0, 0, 1), 1E-6)); |
| 569 |
} |
| 570 |
|
| 571 |
} |