gtc_quaternion.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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/gtc/gtc_quaternion.cpp
  28. /// @date 2010-09-16 / 2014-11-25
  29. /// @author Christophe Riccio
  30. ///////////////////////////////////////////////////////////////////////////////////
  31. #define GLM_META_PROG_HELPERS
  32. #include <glm/gtc/quaternion.hpp>
  33. #include <glm/gtc/epsilon.hpp>
  34. #include <glm/vector_relational.hpp>
  35. #include <vector>
  36. int test_quat_angle()
  37. {
  38. int Error = 0;
  39. {
  40. glm::quat Q = glm::angleAxis(glm::pi<float>() * 0.25f, glm::vec3(0, 0, 1));
  41. glm::quat N = glm::normalize(Q);
  42. float L = glm::length(N);
  43. Error += glm::epsilonEqual(L, 1.0f, 0.01f) ? 0 : 1;
  44. float A = glm::angle(N);
  45. Error += glm::epsilonEqual(A, glm::pi<float>() * 0.25f, 0.01f) ? 0 : 1;
  46. }
  47. {
  48. glm::quat Q = glm::angleAxis(glm::pi<float>() * 0.25f, glm::normalize(glm::vec3(0, 1, 1)));
  49. glm::quat N = glm::normalize(Q);
  50. float L = glm::length(N);
  51. Error += glm::epsilonEqual(L, 1.0f, 0.01f) ? 0 : 1;
  52. float A = glm::angle(N);
  53. Error += glm::epsilonEqual(A, glm::pi<float>() * 0.25f, 0.01f) ? 0 : 1;
  54. }
  55. {
  56. glm::quat Q = glm::angleAxis(glm::pi<float>() * 0.25f, glm::normalize(glm::vec3(1, 2, 3)));
  57. glm::quat N = glm::normalize(Q);
  58. float L = glm::length(N);
  59. Error += glm::epsilonEqual(L, 1.0f, 0.01f) ? 0 : 1;
  60. float A = glm::angle(N);
  61. Error += glm::epsilonEqual(A, glm::pi<float>() * 0.25f, 0.01f) ? 0 : 1;
  62. }
  63. return Error;
  64. }
  65. int test_quat_angleAxis()
  66. {
  67. int Error = 0;
  68. glm::quat A = glm::angleAxis(0.0f, glm::vec3(0, 0, 1));
  69. glm::quat B = glm::angleAxis(glm::pi<float>() * 0.5f, glm::vec3(0, 0, 1));
  70. glm::quat C = glm::mix(A, B, 0.5f);
  71. glm::quat D = glm::angleAxis(glm::pi<float>() * 0.25f, glm::vec3(0, 0, 1));
  72. Error += glm::epsilonEqual(C.x, D.x, 0.01f) ? 0 : 1;
  73. Error += glm::epsilonEqual(C.y, D.y, 0.01f) ? 0 : 1;
  74. Error += glm::epsilonEqual(C.z, D.z, 0.01f) ? 0 : 1;
  75. Error += glm::epsilonEqual(C.w, D.w, 0.01f) ? 0 : 1;
  76. return Error;
  77. }
  78. int test_quat_mix()
  79. {
  80. int Error = 0;
  81. glm::quat A = glm::angleAxis(0.0f, glm::vec3(0, 0, 1));
  82. glm::quat B = glm::angleAxis(glm::pi<float>() * 0.5f, glm::vec3(0, 0, 1));
  83. glm::quat C = glm::mix(A, B, 0.5f);
  84. glm::quat D = glm::angleAxis(glm::pi<float>() * 0.25f, glm::vec3(0, 0, 1));
  85. Error += glm::epsilonEqual(C.x, D.x, 0.01f) ? 0 : 1;
  86. Error += glm::epsilonEqual(C.y, D.y, 0.01f) ? 0 : 1;
  87. Error += glm::epsilonEqual(C.z, D.z, 0.01f) ? 0 : 1;
  88. Error += glm::epsilonEqual(C.w, D.w, 0.01f) ? 0 : 1;
  89. return Error;
  90. }
  91. int test_quat_precision()
  92. {
  93. int Error = 0;
  94. Error += sizeof(glm::lowp_quat) <= sizeof(glm::mediump_quat) ? 0 : 1;
  95. Error += sizeof(glm::mediump_quat) <= sizeof(glm::highp_quat) ? 0 : 1;
  96. return Error;
  97. }
  98. int test_quat_normalize()
  99. {
  100. int Error(0);
  101. {
  102. glm::quat Q = glm::angleAxis(glm::pi<float>() * 0.25f, glm::vec3(0, 0, 1));
  103. glm::quat N = glm::normalize(Q);
  104. float L = glm::length(N);
  105. Error += glm::epsilonEqual(L, 1.0f, 0.000001f) ? 0 : 1;
  106. }
  107. {
  108. glm::quat Q = glm::angleAxis(glm::pi<float>() * 0.25f, glm::vec3(0, 0, 2));
  109. glm::quat N = glm::normalize(Q);
  110. float L = glm::length(N);
  111. Error += glm::epsilonEqual(L, 1.0f, 0.000001f) ? 0 : 1;
  112. }
  113. {
  114. glm::quat Q = glm::angleAxis(glm::pi<float>() * 0.25f, glm::vec3(1, 2, 3));
  115. glm::quat N = glm::normalize(Q);
  116. float L = glm::length(N);
  117. Error += glm::epsilonEqual(L, 1.0f, 0.000001f) ? 0 : 1;
  118. }
  119. return Error;
  120. }
  121. int test_quat_euler()
  122. {
  123. int Error(0);
  124. {
  125. glm::quat q(1.0f, 0.0f, 0.0f, 1.0f);
  126. float Roll = glm::roll(q);
  127. float Pitch = glm::pitch(q);
  128. float Yaw = glm::yaw(q);
  129. glm::vec3 Angles = glm::eulerAngles(q);
  130. }
  131. {
  132. glm::dquat q(1.0f, 0.0f, 0.0f, 1.0f);
  133. double Roll = glm::roll(q);
  134. double Pitch = glm::pitch(q);
  135. double Yaw = glm::yaw(q);
  136. glm::dvec3 Angles = glm::eulerAngles(q);
  137. }
  138. return Error;
  139. }
  140. int test_quat_slerp()
  141. {
  142. int Error(0);
  143. float const Epsilon = 0.0001f;//glm::epsilon<float>();
  144. float sqrt2 = sqrt(2.0f)/2.0f;
  145. glm::quat id;
  146. glm::quat Y90rot(sqrt2, 0.0f, sqrt2, 0.0f);
  147. glm::quat Y180rot(0.0f, 0.0f, 1.0f, 0.0f);
  148. // Testing a == 0
  149. // Must be id
  150. glm::quat id2 = glm::slerp(id, Y90rot, 0.0f);
  151. Error += glm::all(glm::epsilonEqual(id, id2, Epsilon)) ? 0 : 1;
  152. // Testing a == 1
  153. // Must be 90° rotation on Y : 0 0.7 0 0.7
  154. glm::quat Y90rot2 = glm::slerp(id, Y90rot, 1.0f);
  155. Error += glm::all(glm::epsilonEqual(Y90rot, Y90rot2, Epsilon)) ? 0 : 1;
  156. // Testing standard, easy case
  157. // Must be 45° rotation on Y : 0 0.38 0 0.92
  158. glm::quat Y45rot1 = glm::slerp(id, Y90rot, 0.5f);
  159. // Testing reverse case
  160. // Must be 45° rotation on Y : 0 0.38 0 0.92
  161. glm::quat Ym45rot2 = glm::slerp(Y90rot, id, 0.5f);
  162. // Testing against full circle around the sphere instead of shortest path
  163. // Must be 45° rotation on Y
  164. // certainly not a 135° rotation
  165. glm::quat Y45rot3 = glm::slerp(id , -Y90rot, 0.5f);
  166. float Y45angle3 = glm::angle(Y45rot3);
  167. Error += glm::epsilonEqual(Y45angle3, glm::pi<float>() * 0.25f, Epsilon) ? 0 : 1;
  168. Error += glm::all(glm::epsilonEqual(Ym45rot2, Y45rot3, Epsilon)) ? 0 : 1;
  169. // Same, but inverted
  170. // Must also be 45° rotation on Y : 0 0.38 0 0.92
  171. // -0 -0.38 -0 -0.92 is ok too
  172. glm::quat Y45rot4 = glm::slerp(-Y90rot, id, 0.5f);
  173. Error += glm::all(glm::epsilonEqual(Ym45rot2, -Y45rot4, Epsilon)) ? 0 : 1;
  174. // Testing q1 = q2
  175. // Must be 90° rotation on Y : 0 0.7 0 0.7
  176. glm::quat Y90rot3 = glm::slerp(Y90rot, Y90rot, 0.5f);
  177. Error += glm::all(glm::epsilonEqual(Y90rot, Y90rot3, Epsilon)) ? 0 : 1;
  178. // Testing 180° rotation
  179. // Must be 90° rotation on almost any axis that is on the XZ plane
  180. glm::quat XZ90rot = glm::slerp(id, -Y90rot, 0.5f);
  181. float XZ90angle = glm::angle(XZ90rot); // Must be PI/4 = 0.78;
  182. Error += glm::epsilonEqual(XZ90angle, glm::pi<float>() * 0.25f, Epsilon) ? 0 : 1;
  183. // Testing almost equal quaternions (this test should pass through the linear interpolation)
  184. // Must be 0 0.00X 0 0.99999
  185. glm::quat almostid = glm::slerp(id, glm::angleAxis(0.1f, glm::vec3(0.0f, 1.0f, 0.0f)), 0.5f);
  186. // Testing quaternions with opposite sign
  187. {
  188. glm::quat a(-1, 0, 0, 0);
  189. glm::quat result = glm::slerp(a, id, 0.5f);
  190. Error += glm::epsilonEqual(glm::pow(glm::dot(id, result), 2.f), 1.f, 0.01f) ? 0 : 1;
  191. }
  192. return Error;
  193. }
  194. int test_quat_mul()
  195. {
  196. int Error(0);
  197. glm::quat temp1 = glm::normalize(glm::quat(1.0f, glm::vec3(0.0, 1.0, 0.0)));
  198. glm::quat temp2 = glm::normalize(glm::quat(0.5f, glm::vec3(1.0, 0.0, 0.0)));
  199. glm::vec3 transformed0 = (temp1 * glm::vec3(0.0, 1.0, 0.0) * glm::inverse(temp1));
  200. glm::vec3 temp4 = temp2 * transformed0 * glm::inverse(temp2);
  201. glm::quat temp5 = glm::normalize(temp1 * temp2);
  202. glm::vec3 temp6 = temp5 * glm::vec3(0.0, 1.0, 0.0) * glm::inverse(temp5);
  203. # ifndef GLM_FORCE_NO_CTOR_INIT
  204. {
  205. glm::quat temp7;
  206. temp7 *= temp5;
  207. temp7 *= glm::inverse(temp5);
  208. Error += temp7 != glm::quat();
  209. }
  210. # endif
  211. return Error;
  212. }
  213. int test_quat_two_axis_ctr()
  214. {
  215. int Error(0);
  216. glm::quat q1(glm::vec3(1, 0, 0), glm::vec3(0, 1, 0));
  217. glm::vec3 v1 = q1 * glm::vec3(1, 0, 0);
  218. Error += glm::all(glm::epsilonEqual(v1, glm::vec3(0, 1, 0), 0.0001f)) ? 0 : 1;
  219. glm::quat q2 = q1 * q1;
  220. glm::vec3 v2 = q2 * glm::vec3(1, 0, 0);
  221. Error += glm::all(glm::epsilonEqual(v2, glm::vec3(-1, 0, 0), 0.0001f)) ? 0 : 1;
  222. return Error;
  223. }
  224. int test_quat_type()
  225. {
  226. glm::quat A;
  227. glm::dquat B;
  228. return 0;
  229. }
  230. int test_quat_mul_vec()
  231. {
  232. int Error(0);
  233. glm::quat q = glm::angleAxis(glm::pi<float>() * 0.5f, glm::vec3(0, 0, 1));
  234. glm::vec3 v(1, 0, 0);
  235. glm::vec3 u(q * v);
  236. glm::vec3 w(u * q);
  237. Error += glm::all(glm::epsilonEqual(v, w, 0.01f)) ? 0 : 1;
  238. return Error;
  239. }
  240. int test_quat_ctr()
  241. {
  242. int Error(0);
  243. # if GLM_HAS_TRIVIAL_QUERIES
  244. // Error += std::is_trivially_default_constructible<glm::quat>::value ? 0 : 1;
  245. // Error += std::is_trivially_default_constructible<glm::dquat>::value ? 0 : 1;
  246. // Error += std::is_trivially_copy_assignable<glm::quat>::value ? 0 : 1;
  247. // Error += std::is_trivially_copy_assignable<glm::dquat>::value ? 0 : 1;
  248. Error += std::is_trivially_copyable<glm::quat>::value ? 0 : 1;
  249. Error += std::is_trivially_copyable<glm::dquat>::value ? 0 : 1;
  250. Error += std::is_copy_constructible<glm::quat>::value ? 0 : 1;
  251. Error += std::is_copy_constructible<glm::dquat>::value ? 0 : 1;
  252. # endif
  253. # if GLM_HAS_INITIALIZER_LISTS
  254. {
  255. glm::quat A{0, 1, 2, 3};
  256. std::vector<glm::quat> B{
  257. {0, 1, 2, 3},
  258. {0, 1, 2, 3}};
  259. }
  260. # endif//GLM_HAS_INITIALIZER_LISTS
  261. return Error;
  262. }
  263. int main()
  264. {
  265. int Error(0);
  266. #ifdef GLM_META_PROG_HELPERS
  267. assert(glm::quat::components == 4);
  268. assert(glm::quat::components == glm::quat().length());
  269. #endif
  270. Error += test_quat_ctr();
  271. Error += test_quat_mul_vec();
  272. Error += test_quat_two_axis_ctr();
  273. Error += test_quat_mul();
  274. Error += test_quat_precision();
  275. Error += test_quat_type();
  276. Error += test_quat_angle();
  277. Error += test_quat_angleAxis();
  278. Error += test_quat_mix();
  279. Error += test_quat_normalize();
  280. Error += test_quat_euler();
  281. Error += test_quat_slerp();
  282. return Error;
  283. }