| 1 | // Copyright (C) 2016 Paul Lemire <[email protected]> |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include <QtTest/QTest> |
| 5 | #include <Qt3DCore/private/matrix4x4_sse_p.h> |
| 6 | #include <QVector4D> |
| 7 | |
| 8 | using namespace Qt3DCore; |
| 9 | |
| 10 | class tst_Matrix4x4_SSE: public QObject |
| 11 | { |
| 12 | Q_OBJECT |
| 13 | |
| 14 | #ifdef __SSE2__ |
| 15 | private Q_SLOTS: |
| 16 | |
| 17 | void defaultConstruction() |
| 18 | { |
| 19 | // GIVEN |
| 20 | Matrix4x4_SSE mat4; |
| 21 | |
| 22 | // THEN |
| 23 | QCOMPARE(mat4.m11(), 1.0f); |
| 24 | QCOMPARE(mat4.m12(), 0.0f); |
| 25 | QCOMPARE(mat4.m13(), 0.0f); |
| 26 | QCOMPARE(mat4.m14(), 0.0f); |
| 27 | |
| 28 | QCOMPARE(mat4.m21(), 0.0f); |
| 29 | QCOMPARE(mat4.m22(), 1.0f); |
| 30 | QCOMPARE(mat4.m23(), 0.0f); |
| 31 | QCOMPARE(mat4.m24(), 0.0f); |
| 32 | |
| 33 | QCOMPARE(mat4.m31(), 0.0f); |
| 34 | QCOMPARE(mat4.m32(), 0.0f); |
| 35 | QCOMPARE(mat4.m33(), 1.0f); |
| 36 | QCOMPARE(mat4.m34(), 0.0f); |
| 37 | |
| 38 | QCOMPARE(mat4.m41(), 0.0f); |
| 39 | QCOMPARE(mat4.m42(), 0.0f); |
| 40 | QCOMPARE(mat4.m43(), 0.0f); |
| 41 | QCOMPARE(mat4.m44(), 1.0f); |
| 42 | |
| 43 | } |
| 44 | |
| 45 | void checkExplicitConstruction() |
| 46 | { |
| 47 | // GIVEN |
| 48 | Matrix4x4_SSE mat4(11.0f, 12.0f, 13.0f, 14.0f, |
| 49 | 21.0f, 22.0f, 23.0f, 24.0f, |
| 50 | 31.0f, 32.0f, 33.0f, 34.0f, |
| 51 | 41.0f, 42.0f, 43.0f, 44.0f); |
| 52 | |
| 53 | // THEN |
| 54 | QCOMPARE(mat4.m11(), 11.0f); |
| 55 | QCOMPARE(mat4.m12(), 12.0f); |
| 56 | QCOMPARE(mat4.m13(), 13.0f); |
| 57 | QCOMPARE(mat4.m14(), 14.0f); |
| 58 | |
| 59 | QCOMPARE(mat4.m21(), 21.0f); |
| 60 | QCOMPARE(mat4.m22(), 22.0f); |
| 61 | QCOMPARE(mat4.m23(), 23.0f); |
| 62 | QCOMPARE(mat4.m24(), 24.0f); |
| 63 | |
| 64 | QCOMPARE(mat4.m31(), 31.0f); |
| 65 | QCOMPARE(mat4.m32(), 32.0f); |
| 66 | QCOMPARE(mat4.m33(), 33.0f); |
| 67 | QCOMPARE(mat4.m34(), 34.0f); |
| 68 | |
| 69 | QCOMPARE(mat4.m41(), 41.0f); |
| 70 | QCOMPARE(mat4.m42(), 42.0f); |
| 71 | QCOMPARE(mat4.m43(), 43.0f); |
| 72 | QCOMPARE(mat4.m44(), 44.0f); |
| 73 | } |
| 74 | |
| 75 | void checkTransposed() |
| 76 | { |
| 77 | // GIVEN |
| 78 | Matrix4x4_SSE mat4(11.0f, 12.0f, 13.0f, 14.0f, |
| 79 | 21.0f, 22.0f, 23.0f, 24.0f, |
| 80 | 31.0f, 32.0f, 33.0f, 34.0f, |
| 81 | 41.0f, 42.0f, 43.0f, 44.0f); |
| 82 | |
| 83 | // WHEN |
| 84 | mat4 = mat4.transposed(); |
| 85 | |
| 86 | // THEN |
| 87 | QCOMPARE(mat4.m11(), 11.0f); |
| 88 | QCOMPARE(mat4.m12(), 21.0f); |
| 89 | QCOMPARE(mat4.m13(), 31.0f); |
| 90 | QCOMPARE(mat4.m14(), 41.0f); |
| 91 | |
| 92 | QCOMPARE(mat4.m21(), 12.0f); |
| 93 | QCOMPARE(mat4.m22(), 22.0f); |
| 94 | QCOMPARE(mat4.m23(), 32.0f); |
| 95 | QCOMPARE(mat4.m24(), 42.0f); |
| 96 | |
| 97 | QCOMPARE(mat4.m31(), 13.0f); |
| 98 | QCOMPARE(mat4.m32(), 23.0f); |
| 99 | QCOMPARE(mat4.m33(), 33.0f); |
| 100 | QCOMPARE(mat4.m34(), 43.0f); |
| 101 | |
| 102 | QCOMPARE(mat4.m41(), 14.0f); |
| 103 | QCOMPARE(mat4.m42(), 24.0f); |
| 104 | QCOMPARE(mat4.m43(), 34.0f); |
| 105 | QCOMPARE(mat4.m44(), 44.0f); |
| 106 | } |
| 107 | |
| 108 | void checkMultiplication() |
| 109 | { |
| 110 | { |
| 111 | // GIVEN |
| 112 | QMatrix4x4 mat1; |
| 113 | QMatrix4x4 mat2; |
| 114 | |
| 115 | mat1.rotate(45.0f, QVector3D(1.0f, 0.0f, 0.0f)); |
| 116 | mat2.translate(5.0f, 12.0f, 11.0f); |
| 117 | |
| 118 | const Matrix4x4_SSE mat1fast(mat1); |
| 119 | const Matrix4x4_SSE mat2fast(mat2); |
| 120 | |
| 121 | // WHEN |
| 122 | const Matrix4x4_SSE ret = mat1fast * mat2fast; |
| 123 | |
| 124 | // THEN |
| 125 | QCOMPARE(ret.toQMatrix4x4(), mat1 * mat2); |
| 126 | } |
| 127 | { |
| 128 | // GIVEN |
| 129 | QMatrix4x4 mat1; |
| 130 | QMatrix4x4 mat2; |
| 131 | |
| 132 | mat1.rotate(45.0f, QVector3D(1.0f, 0.0f, 0.0f)); |
| 133 | mat2.translate(5.0f, 12.0f, 11.0f); |
| 134 | |
| 135 | const Matrix4x4_SSE mat1fast(mat1); |
| 136 | const Matrix4x4_SSE mat2fast(mat2); |
| 137 | |
| 138 | // WHEN |
| 139 | const Matrix4x4_SSE ret = mat2fast * mat1fast; |
| 140 | |
| 141 | // THEN |
| 142 | QCOMPARE(ret.toQMatrix4x4(), mat2 * mat1); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | void checkAddition() |
| 147 | { |
| 148 | { |
| 149 | // GIVEN |
| 150 | QMatrix4x4 mat1; |
| 151 | QMatrix4x4 mat2; |
| 152 | |
| 153 | mat1.rotate(45.0f, QVector3D(1.0f, 0.0f, 0.0f)); |
| 154 | mat2.translate(5.0f, 12.0f, 11.0f); |
| 155 | |
| 156 | const Matrix4x4_SSE mat1fast(mat1); |
| 157 | const Matrix4x4_SSE mat2fast(mat2); |
| 158 | |
| 159 | // WHEN |
| 160 | const Matrix4x4_SSE ret = mat1fast + mat2fast; |
| 161 | |
| 162 | // THEN |
| 163 | QCOMPARE(ret.toQMatrix4x4(), mat1 + mat2); |
| 164 | } |
| 165 | { |
| 166 | // GIVEN |
| 167 | QMatrix4x4 mat1; |
| 168 | QMatrix4x4 mat2; |
| 169 | |
| 170 | mat1.rotate(45.0f, QVector3D(1.0f, 0.0f, 0.0f)); |
| 171 | mat2.translate(5.0f, 12.0f, 11.0f); |
| 172 | |
| 173 | const Matrix4x4_SSE mat1fast(mat1); |
| 174 | const Matrix4x4_SSE mat2fast(mat2); |
| 175 | |
| 176 | // WHEN |
| 177 | const Matrix4x4_SSE ret = mat2fast + mat1fast; |
| 178 | |
| 179 | // THEN |
| 180 | QCOMPARE(ret.toQMatrix4x4(), mat2 + mat1); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | void checkSubstraction() |
| 185 | { |
| 186 | { |
| 187 | // GIVEN |
| 188 | QMatrix4x4 mat1; |
| 189 | QMatrix4x4 mat2; |
| 190 | |
| 191 | mat1.rotate(45.0f, QVector3D(1.0f, 0.0f, 0.0f)); |
| 192 | mat2.translate(5.0f, 12.0f, 11.0f); |
| 193 | |
| 194 | const Matrix4x4_SSE mat1fast(mat1); |
| 195 | const Matrix4x4_SSE mat2fast(mat2); |
| 196 | |
| 197 | // WHEN |
| 198 | const Matrix4x4_SSE ret = mat1fast - mat2fast; |
| 199 | |
| 200 | // THEN |
| 201 | QCOMPARE(ret.toQMatrix4x4(), mat1 - mat2); |
| 202 | } |
| 203 | { |
| 204 | // GIVEN |
| 205 | QMatrix4x4 mat1; |
| 206 | QMatrix4x4 mat2; |
| 207 | |
| 208 | mat1.rotate(45.0f, QVector3D(1.0f, 0.0f, 0.0f)); |
| 209 | mat2.translate(5.0f, 12.0f, 11.0f); |
| 210 | |
| 211 | const Matrix4x4_SSE mat1fast(mat1); |
| 212 | const Matrix4x4_SSE mat2fast(mat2); |
| 213 | |
| 214 | // WHEN |
| 215 | const Matrix4x4_SSE ret = mat2fast - mat1fast; |
| 216 | |
| 217 | // THEN |
| 218 | QCOMPARE(ret.toQMatrix4x4(), mat2 - mat1); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | void checkEquality() |
| 223 | { |
| 224 | { |
| 225 | // GIVEN |
| 226 | Matrix4x4_SSE c1; |
| 227 | Matrix4x4_SSE c2; |
| 228 | |
| 229 | // THEN |
| 230 | QVERIFY(c1 == c2); |
| 231 | } |
| 232 | { |
| 233 | QMatrix4x4 tmp; |
| 234 | tmp.translate(5.0f, 8.0f, 14.0f); |
| 235 | |
| 236 | // GIVEN |
| 237 | Matrix4x4_SSE c1(tmp); |
| 238 | Matrix4x4_SSE c2(tmp); |
| 239 | |
| 240 | // THEN |
| 241 | QVERIFY(c1 == c2); |
| 242 | } |
| 243 | { |
| 244 | QMatrix4x4 tmp; |
| 245 | tmp.translate(5.0f, 8.0f, 14.0f); |
| 246 | |
| 247 | // GIVEN |
| 248 | Matrix4x4_SSE c1; |
| 249 | Matrix4x4_SSE c2(tmp); |
| 250 | |
| 251 | // THEN |
| 252 | QVERIFY(!(c1 == c2)); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | void checkInequality() |
| 257 | { |
| 258 | { |
| 259 | // GIVEN |
| 260 | Matrix4x4_SSE c1; |
| 261 | Matrix4x4_SSE c2; |
| 262 | |
| 263 | // THEN |
| 264 | QVERIFY(!(c1 != c2)); |
| 265 | } |
| 266 | { |
| 267 | // GIVEN |
| 268 | QMatrix4x4 tmp; |
| 269 | tmp.translate(5.0f, 8.0f, 14.0f); |
| 270 | |
| 271 | Matrix4x4_SSE c1(tmp); |
| 272 | tmp.translate(3.0f, 9.0f, -4.0f); |
| 273 | Matrix4x4_SSE c2(tmp); |
| 274 | |
| 275 | // THEN |
| 276 | QVERIFY(c1 != c2); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | void checkMatrixVector4DMultiplication() |
| 281 | { |
| 282 | |
| 283 | // GIVEN |
| 284 | QMatrix4x4 tmpMat; |
| 285 | QVector4D tmpVec4(1.0f, 2.0f, 3.0f, 4.0f); |
| 286 | tmpMat.translate(5.0f, 8.0f, 14.0f); |
| 287 | |
| 288 | Matrix4x4_SSE mat(tmpMat); |
| 289 | Vector4D vec4(tmpVec4); |
| 290 | |
| 291 | // WHEN |
| 292 | const Vector4D resultingVec = mat * vec4; |
| 293 | |
| 294 | // THEN |
| 295 | QCOMPARE(resultingVec.toQVector4D(), tmpMat * tmpVec4); |
| 296 | } |
| 297 | |
| 298 | void checkVector4DMatrixMultiplication() |
| 299 | { |
| 300 | |
| 301 | // GIVEN |
| 302 | QMatrix4x4 tmpMat; |
| 303 | QVector4D tmpVec4(1.0f, 2.0f, 3.0f, 4.0f); |
| 304 | tmpMat.translate(5.0f, 8.0f, 14.0f); |
| 305 | |
| 306 | Matrix4x4_SSE mat(tmpMat); |
| 307 | Vector4D vec4(tmpVec4); |
| 308 | |
| 309 | // WHEN |
| 310 | const Vector4D resultingVec = vec4 * mat; |
| 311 | |
| 312 | // THEN |
| 313 | QCOMPARE(resultingVec.toQVector4D(), tmpVec4 * tmpMat); |
| 314 | } |
| 315 | |
| 316 | void checkMatrixVector3DMultiplication() |
| 317 | { |
| 318 | |
| 319 | // GIVEN |
| 320 | QMatrix4x4 tmpMat; |
| 321 | QVector3D tmpVec3(1.0f, 2.0f, 3.0f); |
| 322 | tmpMat.translate(5.0f, 8.0f, 14.0f); |
| 323 | |
| 324 | Matrix4x4_SSE mat(tmpMat); |
| 325 | Vector3D vec3(tmpVec3); |
| 326 | |
| 327 | // WHEN |
| 328 | const Vector3D resultingVec = mat * vec3; |
| 329 | |
| 330 | // THEN |
| 331 | QCOMPARE(resultingVec.toQVector3D(), tmpMat.map(tmpVec3)); |
| 332 | } |
| 333 | |
| 334 | void checkVector3DMatrixMultiplication() |
| 335 | { |
| 336 | |
| 337 | // GIVEN |
| 338 | QMatrix4x4 tmpMat; |
| 339 | QVector3D tmpVec3(1.0f, 2.0f, 3.0f); |
| 340 | tmpMat.translate(5.0f, 8.0f, 14.0f); |
| 341 | |
| 342 | Matrix4x4_SSE mat(tmpMat); |
| 343 | Vector3D vec3(tmpVec3); |
| 344 | |
| 345 | // WHEN |
| 346 | const Vector3D resultingVec = vec3 * mat; |
| 347 | |
| 348 | // THEN |
| 349 | QCOMPARE(resultingVec.toQVector3D(), (QVector4D(tmpVec3, 1.f) * tmpMat).toVector3DAffine()); |
| 350 | } |
| 351 | |
| 352 | void checkRows() |
| 353 | { |
| 354 | // GIVEN |
| 355 | const Matrix4x4_SSE mat4(11.0f, 12.0f, 13.0f, 14.0f, |
| 356 | 21.0f, 22.0f, 23.0f, 24.0f, |
| 357 | 31.0f, 32.0f, 33.0f, 34.0f, |
| 358 | 41.0f, 42.0f, 43.0f, 44.0f); |
| 359 | |
| 360 | { |
| 361 | // WHEN |
| 362 | const Vector4D row = mat4.row(0); |
| 363 | |
| 364 | // THEN |
| 365 | QCOMPARE(row.x(), 11.0f); |
| 366 | QCOMPARE(row.y(), 12.0f); |
| 367 | QCOMPARE(row.z(), 13.0f); |
| 368 | QCOMPARE(row.w(), 14.0f); |
| 369 | } |
| 370 | { |
| 371 | // WHEN |
| 372 | const Vector4D row = mat4.row(1); |
| 373 | |
| 374 | // THEN |
| 375 | QCOMPARE(row.x(), 21.0f); |
| 376 | QCOMPARE(row.y(), 22.0f); |
| 377 | QCOMPARE(row.z(), 23.0f); |
| 378 | QCOMPARE(row.w(), 24.0f); |
| 379 | } |
| 380 | { |
| 381 | // WHEN |
| 382 | const Vector4D row = mat4.row(2); |
| 383 | |
| 384 | // THEN |
| 385 | QCOMPARE(row.x(), 31.0f); |
| 386 | QCOMPARE(row.y(), 32.0f); |
| 387 | QCOMPARE(row.z(), 33.0f); |
| 388 | QCOMPARE(row.w(), 34.0f); |
| 389 | } |
| 390 | { |
| 391 | // WHEN |
| 392 | const Vector4D row = mat4.row(3); |
| 393 | |
| 394 | // THEN |
| 395 | QCOMPARE(row.x(), 41.0f); |
| 396 | QCOMPARE(row.y(), 42.0f); |
| 397 | QCOMPARE(row.z(), 43.0f); |
| 398 | QCOMPARE(row.w(), 44.0f); |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | void checkColumns() |
| 403 | { |
| 404 | // GIVEN |
| 405 | const Matrix4x4_SSE mat4(11.0f, 12.0f, 13.0f, 14.0f, |
| 406 | 21.0f, 22.0f, 23.0f, 24.0f, |
| 407 | 31.0f, 32.0f, 33.0f, 34.0f, |
| 408 | 41.0f, 42.0f, 43.0f, 44.0f); |
| 409 | |
| 410 | { |
| 411 | // WHEN |
| 412 | const Vector4D row = mat4.column(0); |
| 413 | |
| 414 | // THEN |
| 415 | QCOMPARE(row.x(), 11.0f); |
| 416 | QCOMPARE(row.y(), 21.0f); |
| 417 | QCOMPARE(row.z(), 31.0f); |
| 418 | QCOMPARE(row.w(), 41.0f); |
| 419 | } |
| 420 | { |
| 421 | // WHEN |
| 422 | const Vector4D row = mat4.column(1); |
| 423 | |
| 424 | // THEN |
| 425 | QCOMPARE(row.x(), 12.0f); |
| 426 | QCOMPARE(row.y(), 22.0f); |
| 427 | QCOMPARE(row.z(), 32.0f); |
| 428 | QCOMPARE(row.w(), 42.0f); |
| 429 | } |
| 430 | { |
| 431 | // WHEN |
| 432 | const Vector4D row = mat4.column(2); |
| 433 | |
| 434 | // THEN |
| 435 | QCOMPARE(row.x(), 13.0f); |
| 436 | QCOMPARE(row.y(), 23.0f); |
| 437 | QCOMPARE(row.z(), 33.0f); |
| 438 | QCOMPARE(row.w(), 43.0f); |
| 439 | } |
| 440 | { |
| 441 | // WHEN |
| 442 | const Vector4D row = mat4.column(3); |
| 443 | |
| 444 | // THEN |
| 445 | QCOMPARE(row.x(), 14.0f); |
| 446 | QCOMPARE(row.y(), 24.0f); |
| 447 | QCOMPARE(row.z(), 34.0f); |
| 448 | QCOMPARE(row.w(), 44.0f); |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | void checkVectorMapVector() |
| 453 | { |
| 454 | { |
| 455 | // GIVEN |
| 456 | QMatrix4x4 tmpMat; |
| 457 | QVector3D tmpVec3(1.0f, 0.0f, 0.0f); |
| 458 | tmpMat.rotate(90.f, 0.f, 1.f, 0.f); |
| 459 | |
| 460 | Matrix4x4_SSE mat(tmpMat); |
| 461 | Vector3D vec3(tmpVec3); |
| 462 | |
| 463 | // WHEN |
| 464 | const Vector3D resultingVec = mat.mapVector(vec3); |
| 465 | |
| 466 | // THEN |
| 467 | QCOMPARE(resultingVec.toQVector3D(), tmpMat.mapVector(tmpVec3)); |
| 468 | } |
| 469 | { |
| 470 | // GIVEN |
| 471 | QMatrix4x4 tmpMat; |
| 472 | QVector3D tmpVec3(0.0f, 0.0f, -1.0f); |
| 473 | tmpMat.rotate(90.f, 0.f, 1.f, 0.f); |
| 474 | |
| 475 | Matrix4x4_SSE mat(tmpMat); |
| 476 | Vector3D vec3(tmpVec3); |
| 477 | |
| 478 | // WHEN |
| 479 | const Vector3D resultingVec = mat.mapVector(vec3); |
| 480 | |
| 481 | // THEN |
| 482 | QCOMPARE(resultingVec.toQVector3D(), tmpMat.mapVector(tmpVec3)); |
| 483 | } |
| 484 | { |
| 485 | // GIVEN |
| 486 | QMatrix4x4 tmpMat; |
| 487 | QVector3D tmpVec3(3.0f, -3.0f, -1.0f); |
| 488 | tmpMat.rotate(90.f, 0.33f, 0.33f, 0.33f); |
| 489 | |
| 490 | Matrix4x4_SSE mat(tmpMat); |
| 491 | Vector3D vec3(tmpVec3); |
| 492 | |
| 493 | // WHEN |
| 494 | const Vector3D resultingVec = mat.mapVector(vec3); |
| 495 | |
| 496 | // THEN |
| 497 | QCOMPARE(resultingVec.toQVector3D(), tmpMat.mapVector(tmpVec3)); |
| 498 | } |
| 499 | } |
| 500 | #endif // __SSE2__ |
| 501 | }; |
| 502 | |
| 503 | QTEST_MAIN(tst_Matrix4x4_SSE) |
| 504 | |
| 505 | #include "tst_matrix4x4_sse.moc" |