QuaternionTests.cpp 2.0 KB

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