QuaternionTests.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include "tests/QuaternionTests.h"
  2. #include "math/Quaternion.h"
  3. #include "tests/Test.h"
  4. #include "utils/StringBuffer.h"
  5. typedef StringBuffer<100> String;
  6. const float eps = 0.0001f;
  7. template<int N, typename T>
  8. static void compareVectors(Test& test, const Vector<N, T>& wanted,
  9. const Vector<N, T>& actual, const char* text) {
  10. for(int i = 0; i < N; i++) {
  11. test.checkFloat(
  12. wanted[i], actual[i], eps,
  13. StringBuffer<50>(text).append(" (").append(i).append(")"));
  14. }
  15. }
  16. static void testInit(Test& test) {
  17. Quaternion q;
  18. test.checkEqual(String("(0.00 i + 0.00 j + 0.00 k + 1.00)"), String(q),
  19. "init");
  20. }
  21. static void testAxisAndDegreesInit(Test& test) {
  22. Quaternion q(Vector3(1.0f, 2.0f, 3.0f), 142.0f);
  23. test.checkEqual(String("(0.25 i + 0.51 j + 0.76 k + 0.33)"), String(q),
  24. "init with axis and degrees");
  25. }
  26. static void testLerp(Test& test) {
  27. Quaternion q1(Vector3(2.0f, 5.0f, 7.0f), 130.0f);
  28. Quaternion q2(Vector3(1.0f, 2.0f, 4.0f), 260.0f);
  29. Quaternion q3 = q1.lerp(0.3f, q2);
  30. test.checkEqual(String("(0.22 i + 0.53 j + 0.81 k + 0.12)"), String(q3),
  31. "lerp");
  32. }
  33. static void testMulSet(Test& test) {
  34. Quaternion q1(Vector3(2.0f, 5.0f, 7.0f), 50.0f);
  35. Quaternion q2(Vector3(2.0f, 5.0f, 7.0f), 60.0f);
  36. Quaternion q3;
  37. q3 *= q1;
  38. test.checkEqual(String(q1), String(q3), "mul set 1");
  39. q3 *= q2;
  40. test.checkEqual(String(Quaternion(Vector3(2.0f, 5.0f, 7.0f), 110.0f)),
  41. String(q3), "mul set 2");
  42. }
  43. static void testMul(Test& test) {
  44. Quaternion q1(Vector3(2.0f, 5.0f, 7.0f), 50.0f);
  45. Quaternion q2(Vector3(2.0f, 5.0f, 7.0f), 60.0f);
  46. Quaternion q3 = q1 * q2;
  47. test.checkEqual(String(Quaternion(Vector3(2.0f, 5.0f, 7.0f), 110.0f)),
  48. String(q3), "mul");
  49. }
  50. static void testMulVector(Test& test) {
  51. Quaternion q1(Vector3(1.0f, 0.0f, 0.0f), 90.0f);
  52. Quaternion q2(Vector3(0.0f, 1.0f, 0.0f), 90.0f);
  53. Quaternion q3(Vector3(0.0f, 0.0f, 1.0f), 90.0f);
  54. Vector3 v1(1.0f, 0.0f, 0.0f);
  55. Vector3 v2(0.0f, 1.0f, 0.0f);
  56. Vector3 v3(0.0f, 0.0f, 1.0f);
  57. compareVectors(test, Vector3(1.0f, 0.0f, 0.0f), q1 * v1,
  58. "mul with vector 1");
  59. compareVectors(test, Vector3(0.0f, 0.0f, 1.0f), q1 * v2,
  60. "mul with vector 2");
  61. compareVectors(test, Vector3(0.0f, -1.0f, 0.0f), q1 * v3,
  62. "mul with vector 3");
  63. compareVectors(test, Vector3(0.0f, 0.0f, -1.0f), q2 * v1,
  64. "mul with vector 4");
  65. compareVectors(test, Vector3(0.0f, 1.0f, 0.0f), q2 * v2,
  66. "mul with vector 5");
  67. compareVectors(test, Vector3(1.0f, 0.0f, 0.0f), q2 * v3,
  68. "mul with vector 6");
  69. compareVectors(test, Vector3(0.0f, 1.0f, 0.0f), q3 * v1,
  70. "mul with vector 7");
  71. compareVectors(test, Vector3(-1.0f, 0.0f, 0.0f), q3 * v2,
  72. "mul with vector 8");
  73. compareVectors(test, Vector3(0.0f, 0.0f, 1.0f), q3 * v3,
  74. "mul with vector 9");
  75. }
  76. void QuaternionTests::test() {
  77. Test test("Quaternion");
  78. testInit(test);
  79. testAxisAndDegreesInit(test);
  80. testLerp(test);
  81. testMulSet(test);
  82. testMul(test);
  83. testMulVector(test);
  84. test.finalize();
  85. }