gtx_quaternion.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. ///////////////////////////////////////////////////////////////////////////////////
  2. /// OpenGL Mathematics (glm.g-truc.net)
  3. ///
  4. /// Copyright (c) 2005 - 2015 G-Truc Creation (www.g-truc.net)
  5. /// Permission is hereby granted, free of charge, to any person obtaining a copy
  6. /// of this software and associated documentation files (the "Software"), to deal
  7. /// in the Software without restriction, including without limitation the rights
  8. /// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. /// copies of the Software, and to permit persons to whom the Software is
  10. /// furnished to do so, subject to the following conditions:
  11. ///
  12. /// The above copyright notice and this permission notice shall be included in
  13. /// all copies or substantial portions of the Software.
  14. ///
  15. /// Restrictions:
  16. /// By making use of the Software for military purposes, you choose to make
  17. /// a Bunny unhappy.
  18. ///
  19. /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. /// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. /// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. /// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. /// THE SOFTWARE.
  26. ///
  27. /// @file test/gtx/gtx_quaternion.cpp
  28. /// @date 2011-05-25 / 2014-11-25
  29. /// @author Christophe Riccio
  30. ///////////////////////////////////////////////////////////////////////////////////
  31. #include <glm/gtc/epsilon.hpp>
  32. #include <glm/gtc/type_ptr.hpp>
  33. #include <glm/gtc/matrix_transform.hpp>
  34. #include <glm/gtx/transform.hpp>
  35. #include <glm/gtx/quaternion.hpp>
  36. #include <glm/gtx/compatibility.hpp>
  37. #include <glm/ext.hpp>
  38. int test_quat_fastMix()
  39. {
  40. int Error = 0;
  41. glm::quat A = glm::angleAxis(0.0f, glm::vec3(0, 0, 1));
  42. glm::quat B = glm::angleAxis(glm::pi<float>() * 0.5f, glm::vec3(0, 0, 1));
  43. glm::quat C = glm::fastMix(A, B, 0.5f);
  44. glm::quat D = glm::angleAxis(glm::pi<float>() * 0.25f, glm::vec3(0, 0, 1));
  45. Error += glm::epsilonEqual(C.x, D.x, 0.01f) ? 0 : 1;
  46. Error += glm::epsilonEqual(C.y, D.y, 0.01f) ? 0 : 1;
  47. Error += glm::epsilonEqual(C.z, D.z, 0.01f) ? 0 : 1;
  48. Error += glm::epsilonEqual(C.w, D.w, 0.01f) ? 0 : 1;
  49. return Error;
  50. }
  51. int test_quat_shortMix()
  52. {
  53. int Error(0);
  54. glm::quat A = glm::angleAxis(0.0f, glm::vec3(0, 0, 1));
  55. glm::quat B = glm::angleAxis(glm::pi<float>() * 0.5f, glm::vec3(0, 0, 1));
  56. glm::quat C = glm::shortMix(A, B, 0.5f);
  57. glm::quat D = glm::angleAxis(glm::pi<float>() * 0.25f, glm::vec3(0, 0, 1));
  58. Error += glm::epsilonEqual(C.x, D.x, 0.01f) ? 0 : 1;
  59. Error += glm::epsilonEqual(C.y, D.y, 0.01f) ? 0 : 1;
  60. Error += glm::epsilonEqual(C.z, D.z, 0.01f) ? 0 : 1;
  61. Error += glm::epsilonEqual(C.w, D.w, 0.01f) ? 0 : 1;
  62. return Error;
  63. }
  64. int test_orientation()
  65. {
  66. int Error(0);
  67. {
  68. glm::quat q(1.0f, 0.0f, 0.0f, 1.0f);
  69. float p = glm::roll(q);
  70. }
  71. {
  72. glm::quat q(1.0f, 0.0f, 0.0f, 1.0f);
  73. float p = glm::pitch(q);
  74. }
  75. {
  76. glm::quat q(1.0f, 0.0f, 0.0f, 1.0f);
  77. float p = glm::yaw(q);
  78. }
  79. return Error;
  80. }
  81. int test_rotation()
  82. {
  83. int Error(0);
  84. glm::vec3 v(1, 0, 0);
  85. glm::vec3 u(0, 1, 0);
  86. glm::quat Rotation = glm::rotation(v, u);
  87. float Angle = glm::angle(Rotation);
  88. Error += glm::abs(Angle - glm::pi<float>() * 0.5f) < glm::epsilon<float>() ? 0 : 1;
  89. return Error;
  90. }
  91. int test_log()
  92. {
  93. int Error(0);
  94. glm::quat q;
  95. glm::quat p = glm::log(q);
  96. glm::quat r = glm::exp(p);
  97. return Error;
  98. }
  99. int main()
  100. {
  101. int Error(0);
  102. Error += test_log();
  103. Error += test_rotation();
  104. Error += test_quat_fastMix();
  105. Error += test_quat_shortMix();
  106. return Error;
  107. }