QuaternionTests.cpp 2.9 KB

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