QuaternionTests.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include "tests/QuaternionTests.h"
  2. #include "math/Quaternion.h"
  3. #include "test/Test.h"
  4. const float eps = 0.0001f;
  5. template<int N, typename T>
  6. static void compareVectors(const Core::Vector<N, T>& wanted,
  7. const Core::Vector<N, T>& actual) {
  8. for(int i = 0; i < N; i++) {
  9. CORE_TEST_FLOAT(wanted[i], actual[i], eps);
  10. }
  11. }
  12. static void testInit() {
  13. Core::Quaternion q;
  14. CORE_TEST_STRING("(0.00 i + 0.00 j + 0.00 k + 1.00)", q);
  15. }
  16. static void testAxisAndDegreesInit() {
  17. Core::Quaternion q(Core::Vector3(1.0f, 2.0f, 3.0f), 142.0f);
  18. CORE_TEST_STRING("(0.25 i + 0.51 j + 0.76 k + 0.33)", q);
  19. }
  20. static void testLerp() {
  21. Core::Quaternion q1(Core::Vector3(2.0f, 5.0f, 7.0f), 130.0f);
  22. Core::Quaternion q2(Core::Vector3(1.0f, 2.0f, 4.0f), 260.0f);
  23. Core::Quaternion q3 = q1.lerp(0.3f, q2);
  24. CORE_TEST_STRING("(0.22 i + 0.53 j + 0.81 k + 0.12)", q3);
  25. }
  26. static void testMulSet() {
  27. Core::Quaternion q1(Core::Vector3(2.0f, 5.0f, 7.0f), 50.0f);
  28. Core::Quaternion q2(Core::Vector3(2.0f, 5.0f, 7.0f), 60.0f);
  29. Core::Quaternion q3;
  30. q3 *= q1;
  31. CORE_TEST_STRING(q1, q3);
  32. q3 *= q2;
  33. CORE_TEST_STRING(Core::Quaternion(Core::Vector3(2.0f, 5.0f, 7.0f), 110.0f),
  34. q3);
  35. }
  36. static void testMul() {
  37. Core::Quaternion q1(Core::Vector3(2.0f, 5.0f, 7.0f), 50.0f);
  38. Core::Quaternion q2(Core::Vector3(2.0f, 5.0f, 7.0f), 60.0f);
  39. Core::Quaternion q3 = q1 * q2;
  40. CORE_TEST_STRING(Core::Quaternion(Core::Vector3(2.0f, 5.0f, 7.0f), 110.0f),
  41. q3);
  42. }
  43. static void testMulVector() {
  44. Core::Quaternion q1(Core::Vector3(1.0f, 0.0f, 0.0f), 90.0f);
  45. Core::Quaternion q2(Core::Vector3(0.0f, 1.0f, 0.0f), 90.0f);
  46. Core::Quaternion q3(Core::Vector3(0.0f, 0.0f, 1.0f), 90.0f);
  47. Core::Vector3 v1(1.0f, 0.0f, 0.0f);
  48. Core::Vector3 v2(0.0f, 1.0f, 0.0f);
  49. Core::Vector3 v3(0.0f, 0.0f, 1.0f);
  50. compareVectors(Core::Vector3(1.0f, 0.0f, 0.0f), q1 * v1);
  51. compareVectors(Core::Vector3(0.0f, 0.0f, 1.0f), q1 * v2);
  52. compareVectors(Core::Vector3(0.0f, -1.0f, 0.0f), q1 * v3);
  53. compareVectors(Core::Vector3(0.0f, 0.0f, -1.0f), q2 * v1);
  54. compareVectors(Core::Vector3(0.0f, 1.0f, 0.0f), q2 * v2);
  55. compareVectors(Core::Vector3(1.0f, 0.0f, 0.0f), q2 * v3);
  56. compareVectors(Core::Vector3(0.0f, 1.0f, 0.0f), q3 * v1);
  57. compareVectors(Core::Vector3(-1.0f, 0.0f, 0.0f), q3 * v2);
  58. compareVectors(Core::Vector3(0.0f, 0.0f, 1.0f), q3 * v3);
  59. }
  60. void Core::QuaternionTests::test() {
  61. testInit();
  62. testAxisAndDegreesInit();
  63. testLerp();
  64. testMulSet();
  65. testMul();
  66. testMulVector();
  67. }