2
0

gtc_quaternion.cpp 9.8 KB

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