gtx_quaternion.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #define GLM_ENABLE_EXPERIMENTAL
  2. #include <glm/gtc/epsilon.hpp>
  3. #include <glm/gtc/type_ptr.hpp>
  4. #include <glm/gtc/matrix_transform.hpp>
  5. #include <glm/gtx/transform.hpp>
  6. #include <glm/gtx/quaternion.hpp>
  7. #include <glm/gtx/compatibility.hpp>
  8. #include <glm/ext.hpp>
  9. int test_quat_fastMix()
  10. {
  11. int Error = 0;
  12. glm::quat A = glm::angleAxis(0.0f, glm::vec3(0, 0, 1));
  13. glm::quat B = glm::angleAxis(glm::pi<float>() * 0.5f, glm::vec3(0, 0, 1));
  14. glm::quat C = glm::fastMix(A, B, 0.5f);
  15. glm::quat D = glm::angleAxis(glm::pi<float>() * 0.25f, glm::vec3(0, 0, 1));
  16. Error += glm::epsilonEqual(C.x, D.x, 0.01f) ? 0 : 1;
  17. Error += glm::epsilonEqual(C.y, D.y, 0.01f) ? 0 : 1;
  18. Error += glm::epsilonEqual(C.z, D.z, 0.01f) ? 0 : 1;
  19. Error += glm::epsilonEqual(C.w, D.w, 0.01f) ? 0 : 1;
  20. return Error;
  21. }
  22. int test_quat_shortMix()
  23. {
  24. int Error(0);
  25. glm::quat A = glm::angleAxis(0.0f, glm::vec3(0, 0, 1));
  26. glm::quat B = glm::angleAxis(glm::pi<float>() * 0.5f, glm::vec3(0, 0, 1));
  27. glm::quat C = glm::shortMix(A, B, 0.5f);
  28. glm::quat D = glm::angleAxis(glm::pi<float>() * 0.25f, glm::vec3(0, 0, 1));
  29. Error += glm::epsilonEqual(C.x, D.x, 0.01f) ? 0 : 1;
  30. Error += glm::epsilonEqual(C.y, D.y, 0.01f) ? 0 : 1;
  31. Error += glm::epsilonEqual(C.z, D.z, 0.01f) ? 0 : 1;
  32. Error += glm::epsilonEqual(C.w, D.w, 0.01f) ? 0 : 1;
  33. return Error;
  34. }
  35. int test_orientation()
  36. {
  37. int Error = 0;
  38. {
  39. glm::quat q(1.0f, 0.0f, 0.0f, 1.0f);
  40. float p = glm::roll(q);
  41. Error += glm::epsilonEqual(p, glm::pi<float>() * 0.5f, 0.0001f) ? 0 : 1;
  42. }
  43. {
  44. glm::quat q(1.0f, 0.0f, 0.0f, 1.0f);
  45. float p = glm::pitch(q);
  46. Error += glm::epsilonEqual(p, 0.f, 0.0001f) ? 0 : 1;
  47. }
  48. {
  49. glm::quat q(1.0f, 0.0f, 0.0f, 1.0f);
  50. float p = glm::yaw(q);
  51. Error += glm::epsilonEqual(p, 0.f, 0.0001f) ? 0 : 1;
  52. }
  53. return Error;
  54. }
  55. int test_rotation()
  56. {
  57. int Error(0);
  58. glm::vec3 v(1, 0, 0);
  59. glm::vec3 u(0, 1, 0);
  60. glm::quat Rotation = glm::rotation(v, u);
  61. float Angle = glm::angle(Rotation);
  62. Error += glm::abs(Angle - glm::pi<float>() * 0.5f) < glm::epsilon<float>() ? 0 : 1;
  63. return Error;
  64. }
  65. int test_log()
  66. {
  67. int Error(0);
  68. glm::quat q;
  69. glm::quat p = glm::log(q);
  70. glm::quat r = glm::exp(p);
  71. return Error;
  72. }
  73. int test_quat_lookAt()
  74. {
  75. int Error(0);
  76. glm::vec3 eye(0.0f);
  77. glm::vec3 center(1.1f, -2.0f, 3.1416f);
  78. glm::vec3 up = glm::vec3(-0.17f, 7.23f, -1.744f);
  79. glm::quat test_quat = glm::quatLookAt(center - eye, up);
  80. glm::quat test_mat = glm::conjugate(glm::quat_cast(glm::lookAt(eye, center, up)));
  81. Error += static_cast<int>(glm::abs(glm::length(test_quat) - 1.0f) > glm::epsilon<float>());
  82. Error += static_cast<int>(glm::min(glm::length(test_quat + (-test_mat)), glm::length(test_quat + test_mat)) > glm::epsilon<float>());
  83. return Error;
  84. }
  85. int main()
  86. {
  87. int Error = 0;
  88. Error += test_log();
  89. Error += test_rotation();
  90. Error += test_orientation();
  91. Error += test_quat_fastMix();
  92. Error += test_quat_shortMix();
  93. Error += test_quat_lookAt();
  94. return Error;
  95. }