QuaternionTests.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include "tests/QuaternionTests.h"
  2. #include "math/Quaternion.h"
  3. #include "test/Test.h"
  4. using V3 = Core::Vector3;
  5. static void testInit() {
  6. Core::Quaternion q;
  7. CORE_TEST_STRING("(0.00 i + 0.00 j + 0.00 k + 1.00)", q);
  8. }
  9. static void testAxisAndDegreesInit() {
  10. Core::Quaternion q(V3(1.0f, 2.0f, 3.0f), 142.0f);
  11. CORE_TEST_STRING("(0.25 i + 0.51 j + 0.76 k + 0.33)", q);
  12. }
  13. static void testLerp() {
  14. Core::Quaternion q1(V3(2.0f, 5.0f, 7.0f), 130.0f);
  15. Core::Quaternion q2(V3(1.0f, 2.0f, 4.0f), 260.0f);
  16. Core::Quaternion q3 = q1.lerp(0.3f, q2);
  17. CORE_TEST_STRING("(0.22 i + 0.53 j + 0.81 k + 0.12)", q3);
  18. }
  19. static void testMulSet() {
  20. Core::Quaternion q1(V3(2.0f, 5.0f, 7.0f), 50.0f);
  21. Core::Quaternion q2(V3(2.0f, 5.0f, 7.0f), 60.0f);
  22. Core::Quaternion q3;
  23. q3 *= q1;
  24. CORE_TEST_STRING(q1, q3);
  25. q3 *= q2;
  26. CORE_TEST_STRING(Core::Quaternion(V3(2.0f, 5.0f, 7.0f), 110.0f), q3);
  27. }
  28. static void testMul() {
  29. Core::Quaternion q1(V3(2.0f, 5.0f, 7.0f), 50.0f);
  30. Core::Quaternion q2(V3(2.0f, 5.0f, 7.0f), 60.0f);
  31. Core::Quaternion q3 = q1 * q2;
  32. CORE_TEST_STRING(Core::Quaternion(V3(2.0f, 5.0f, 7.0f), 110.0f), q3);
  33. }
  34. static void testMulVector() {
  35. Core::Quaternion q1(V3(1.0f, 0.0f, 0.0f), 90.0f);
  36. Core::Quaternion q2(V3(0.0f, 1.0f, 0.0f), 90.0f);
  37. Core::Quaternion q3(V3(0.0f, 0.0f, 1.0f), 90.0f);
  38. V3 v1(1.0f, 0.0f, 0.0f);
  39. V3 v2(0.0f, 1.0f, 0.0f);
  40. V3 v3(0.0f, 0.0f, 1.0f);
  41. CORE_TEST_VECTOR(V3(1.0f, 0.0f, 0.0f), q1 * v1);
  42. CORE_TEST_VECTOR(V3(0.0f, 0.0f, 1.0f), q1 * v2);
  43. CORE_TEST_VECTOR(V3(0.0f, -1.0f, 0.0f), q1 * v3);
  44. CORE_TEST_VECTOR(V3(0.0f, 0.0f, -1.0f), q2 * v1);
  45. CORE_TEST_VECTOR(V3(0.0f, 1.0f, 0.0f), q2 * v2);
  46. CORE_TEST_VECTOR(V3(1.0f, 0.0f, 0.0f), q2 * v3);
  47. CORE_TEST_VECTOR(V3(0.0f, 1.0f, 0.0f), q3 * v1);
  48. CORE_TEST_VECTOR(V3(-1.0f, 0.0f, 0.0f), q3 * v2);
  49. CORE_TEST_VECTOR(V3(0.0f, 0.0f, 1.0f), q3 * v3);
  50. }
  51. void Core::QuaternionTests::test() {
  52. testInit();
  53. testAxisAndDegreesInit();
  54. testLerp();
  55. testMulSet();
  56. testMul();
  57. testMulVector();
  58. }