gtc_quaternion.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. #include <glm/gtc/constants.hpp>
  2. #include <glm/gtc/quaternion.hpp>
  3. #include <glm/gtc/matrix_transform.hpp>
  4. #include <glm/ext/matrix_relational.hpp>
  5. #include <glm/ext/vector_relational.hpp>
  6. #include <glm/ext/scalar_relational.hpp>
  7. #include <glm/glm.hpp>
  8. #include <vector>
  9. static int test_quat_angle()
  10. {
  11. int Error = 0;
  12. {
  13. glm::quat Q = glm::angleAxis(glm::pi<float>() * 0.25f, glm::vec3(0, 0, 1));
  14. glm::quat N = glm::normalize(Q);
  15. float L = glm::length(N);
  16. Error += glm::equal(L, 1.0f, 0.01f) ? 0 : 1;
  17. float A = glm::angle(N);
  18. Error += glm::equal(A, glm::pi<float>() * 0.25f, 0.01f) ? 0 : 1;
  19. }
  20. {
  21. glm::quat Q = glm::angleAxis(glm::pi<float>() * 0.25f, glm::normalize(glm::vec3(0, 1, 1)));
  22. glm::quat N = glm::normalize(Q);
  23. float L = glm::length(N);
  24. Error += glm::equal(L, 1.0f, 0.01f) ? 0 : 1;
  25. float A = glm::angle(N);
  26. Error += glm::equal(A, glm::pi<float>() * 0.25f, 0.01f) ? 0 : 1;
  27. }
  28. {
  29. glm::quat Q = glm::angleAxis(glm::pi<float>() * 0.25f, glm::normalize(glm::vec3(1, 2, 3)));
  30. glm::quat N = glm::normalize(Q);
  31. float L = glm::length(N);
  32. Error += glm::equal(L, 1.0f, 0.01f) ? 0 : 1;
  33. float A = glm::angle(N);
  34. Error += glm::equal(A, glm::pi<float>() * 0.25f, 0.01f) ? 0 : 1;
  35. }
  36. return Error;
  37. }
  38. static int test_quat_angleAxis()
  39. {
  40. int Error = 0;
  41. glm::quat A = glm::angleAxis(0.f, glm::vec3(0.f, 0.f, 1.f));
  42. glm::quat B = glm::angleAxis(glm::pi<float>() * 0.5f, glm::vec3(0, 0, 1));
  43. glm::quat C = glm::mix(A, B, 0.5f);
  44. glm::quat D = glm::angleAxis(glm::pi<float>() * 0.25f, glm::vec3(0, 0, 1));
  45. Error += glm::equal(C.x, D.x, 0.01f) ? 0 : 1;
  46. Error += glm::equal(C.y, D.y, 0.01f) ? 0 : 1;
  47. Error += glm::equal(C.z, D.z, 0.01f) ? 0 : 1;
  48. Error += glm::equal(C.w, D.w, 0.01f) ? 0 : 1;
  49. return Error;
  50. }
  51. static int test_quat_mix()
  52. {
  53. int Error = 0;
  54. glm::quat A = glm::angleAxis(0.f, glm::vec3(0.f, 0.f, 1.f));
  55. glm::quat B = glm::angleAxis(glm::pi<float>() * 0.5f, glm::vec3(0, 0, 1));
  56. glm::quat C = glm::mix(A, B, 0.5f);
  57. glm::quat D = glm::angleAxis(glm::pi<float>() * 0.25f, glm::vec3(0, 0, 1));
  58. Error += glm::equal(C.x, D.x, 0.01f) ? 0 : 1;
  59. Error += glm::equal(C.y, D.y, 0.01f) ? 0 : 1;
  60. Error += glm::equal(C.z, D.z, 0.01f) ? 0 : 1;
  61. Error += glm::equal(C.w, D.w, 0.01f) ? 0 : 1;
  62. return Error;
  63. }
  64. static int test_quat_normalize()
  65. {
  66. int Error(0);
  67. {
  68. glm::quat Q = glm::angleAxis(glm::pi<float>() * 0.25f, glm::vec3(0, 0, 1));
  69. glm::quat N = glm::normalize(Q);
  70. float L = glm::length(N);
  71. Error += glm::equal(L, 1.0f, 0.000001f) ? 0 : 1;
  72. }
  73. {
  74. glm::quat Q = glm::angleAxis(glm::pi<float>() * 0.25f, glm::vec3(0, 0, 2));
  75. glm::quat N = glm::normalize(Q);
  76. float L = glm::length(N);
  77. Error += glm::equal(L, 1.0f, 0.000001f) ? 0 : 1;
  78. }
  79. {
  80. glm::quat Q = glm::angleAxis(glm::pi<float>() * 0.25f, glm::vec3(1, 2, 3));
  81. glm::quat N = glm::normalize(Q);
  82. float L = glm::length(N);
  83. Error += glm::equal(L, 1.0f, 0.000001f) ? 0 : 1;
  84. }
  85. return Error;
  86. }
  87. static int test_quat_euler()
  88. {
  89. int Error = 0;
  90. {
  91. glm::quat q(1.0f, 0.0f, 0.0f, 1.0f);
  92. float Roll = glm::roll(q);
  93. float Pitch = glm::pitch(q);
  94. float Yaw = glm::yaw(q);
  95. glm::vec3 Angles = glm::eulerAngles(q);
  96. Error += glm::all(glm::equal(Angles, glm::vec3(Pitch, Yaw, Roll), 0.000001f)) ? 0 : 1;
  97. }
  98. {
  99. glm::dquat q(1.0, 0.0, 0.0, 1.0);
  100. double Roll = glm::roll(q);
  101. double Pitch = glm::pitch(q);
  102. double Yaw = glm::yaw(q);
  103. glm::dvec3 Angles = glm::eulerAngles(q);
  104. Error += glm::all(glm::equal(Angles, glm::dvec3(Pitch, Yaw, Roll), 0.000001)) ? 0 : 1;
  105. }
  106. return Error;
  107. }
  108. static int test_quat_slerp()
  109. {
  110. int Error = 0;
  111. float const Epsilon = 0.0001f;//glm::epsilon<float>();
  112. float sqrt2 = std::sqrt(2.0f)/2.0f;
  113. glm::quat id(static_cast<float>(1), static_cast<float>(0), static_cast<float>(0), static_cast<float>(0));
  114. glm::quat Y90rot(sqrt2, 0.0f, sqrt2, 0.0f);
  115. // Testing a == 0
  116. // Must be id
  117. glm::quat id2 = glm::slerp(id, Y90rot, 0.0f);
  118. Error += glm::all(glm::equal(id, id2, Epsilon)) ? 0 : 1;
  119. // Testing a == 1
  120. // Must be 90 degrees rotation on Y : 0 0.7 0 0.7
  121. glm::quat Y90rot2 = glm::slerp(id, Y90rot, 1.0f);
  122. Error += glm::all(glm::equal(Y90rot, Y90rot2, Epsilon)) ? 0 : 1;
  123. // Testing standard, easy case
  124. // Must be 45 degrees rotation on Y : 0 0.38 0 0.92
  125. glm::quat Y45rot1 = glm::slerp(id, Y90rot, 0.5f);
  126. Error += glm::all(glm::equal(Y45rot1, glm::quat(0.924f, 0.0f, 0.383f, 0.0f), 0.01f)) ? 0 : 1;
  127. // Testing reverse case
  128. // Must be 45 degrees rotation on Y : 0 0.38 0 0.92
  129. glm::quat Ym45rot2 = glm::slerp(Y90rot, id, 0.5f);
  130. // Testing against full circle around the sphere instead of shortest path
  131. // Must be 45 degrees rotation on Y
  132. // certainly not a 135 degrees rotation
  133. glm::quat Y45rot3 = glm::slerp(id , -Y90rot, 0.5f);
  134. float Y45angle3 = glm::angle(Y45rot3);
  135. Error += glm::equal(Y45angle3, glm::pi<float>() * 0.25f, Epsilon) ? 0 : 1;
  136. Error += glm::all(glm::equal(Ym45rot2, Y45rot3, Epsilon)) ? 0 : 1;
  137. // Same, but inverted
  138. // Must also be 45 degrees rotation on Y : 0 0.38 0 0.92
  139. // -0 -0.38 -0 -0.92 is ok too
  140. glm::quat Y45rot4 = glm::slerp(-Y90rot, id, 0.5f);
  141. Error += glm::all(glm::equal(Ym45rot2, -Y45rot4, Epsilon)) ? 0 : 1;
  142. // Testing q1 = q2
  143. // Must be 90 degrees rotation on Y : 0 0.7 0 0.7
  144. glm::quat Y90rot3 = glm::slerp(Y90rot, Y90rot, 0.5f);
  145. Error += glm::all(glm::equal(Y90rot, Y90rot3, Epsilon)) ? 0 : 1;
  146. // Testing 180 degrees rotation
  147. // Must be 90 degrees rotation on almost any axis that is on the XZ plane
  148. glm::quat XZ90rot = glm::slerp(id, -Y90rot, 0.5f);
  149. float XZ90angle = glm::angle(XZ90rot); // Must be PI/4 = 0.78;
  150. Error += glm::equal(XZ90angle, glm::pi<float>() * 0.25f, Epsilon) ? 0 : 1;
  151. // Testing almost equal quaternions (this test should pass through the linear interpolation)
  152. // Must be 0 0.00X 0 0.99999
  153. glm::quat almostid = glm::slerp(id, glm::angleAxis(0.1f, glm::vec3(0.0f, 1.0f, 0.0f)), 0.5f);
  154. Error += glm::all(glm::equal(almostid, glm::quat(1.0f, 0.0f, 0.0f, 0.0f), 0.1f)) ? 0 : 1;
  155. // Testing quaternions with opposite sign
  156. {
  157. glm::quat a(-1, 0, 0, 0);
  158. glm::quat result = glm::slerp(a, id, 0.5f);
  159. Error += glm::equal(glm::pow(glm::dot(id, result), 2.f), 1.f, 0.01f) ? 0 : 1;
  160. }
  161. return Error;
  162. }
  163. static int test_quat_slerp_spins()
  164. {
  165. int Error = 0;
  166. float const Epsilon = 0.0001f;//glm::epsilon<float>();
  167. float sqrt2 = std::sqrt(2.0f) / 2.0f;
  168. glm::quat id(static_cast<float>(1), static_cast<float>(0), static_cast<float>(0), static_cast<float>(0));
  169. glm::quat Y90rot(sqrt2, 0.0f, sqrt2, 0.0f);
  170. glm::quat Y180rot(0.0f, 0.0f, 1.0f, 0.0f);
  171. // Testing a == 0, k == 1
  172. // Must be id
  173. glm::quat id2 = glm::slerp(id, id, 1.0f, 1);
  174. Error += glm::all(glm::equal(id, id2, Epsilon)) ? 0 : 1;
  175. // Testing a == 1, k == 2
  176. // Must be id
  177. glm::quat id3 = glm::slerp(id, id, 1.0f, 2);
  178. Error += glm::all(glm::equal(id, id3, Epsilon)) ? 0 : 1;
  179. // Testing a == 1, k == 1
  180. // Must be 90 degrees rotation on Y : 0 0.7 0 0.7
  181. // Negative quaternion is representing same orientation
  182. glm::quat Y90rot2 = glm::slerp(id, Y90rot, 1.0f, 1);
  183. Error += glm::all(glm::equal(Y90rot, -Y90rot2, Epsilon)) ? 0 : 1;
  184. // Testing a == 1, k == 2
  185. // Must be id
  186. glm::quat Y90rot3 = glm::slerp(id, Y90rot, 8.0f / 9.0f, 2);
  187. Error += glm::all(glm::equal(id, Y90rot3, Epsilon)) ? 0 : 1;
  188. // Testing a == 1, k == 1
  189. // Must be 90 degrees rotation on Y : 0 0.7 0 0.7
  190. glm::quat Y90rot4 = glm::slerp(id, Y90rot, 0.2f, 1);
  191. Error += glm::all(glm::equal(Y90rot, Y90rot4, Epsilon)) ? 0 : 1;
  192. // Testing reverse case
  193. // Must be 45 degrees rotation on Y : 0 0.38 0 0.92
  194. // Negative quaternion is representing same orientation
  195. glm::quat Ym45rot2 = glm::slerp(Y90rot, id, 0.9f, 1);
  196. glm::quat Ym45rot3 = glm::slerp(Y90rot, id, 0.5f);
  197. Error += glm::all(glm::equal(-Ym45rot2, Ym45rot3, Epsilon)) ? 0 : 1;
  198. // Testing against full circle around the sphere instead of shortest path
  199. // Must be 45 degrees rotation on Y
  200. // certainly not a 135 degrees rotation
  201. glm::quat Y45rot3 = glm::slerp(id, -Y90rot, 0.5f, 0);
  202. float Y45angle3 = glm::angle(Y45rot3);
  203. Error += glm::equal(Y45angle3, glm::pi<float>() * 0.25f, Epsilon) ? 0 : 1;
  204. Error += glm::all(glm::equal(Ym45rot3, Y45rot3, Epsilon)) ? 0 : 1;
  205. // Same, but inverted
  206. // Must also be 45 degrees rotation on Y : 0 0.38 0 0.92
  207. // -0 -0.38 -0 -0.92 is ok too
  208. glm::quat Y45rot4 = glm::slerp(-Y90rot, id, 0.5f, 0);
  209. Error += glm::all(glm::equal(Ym45rot2, Y45rot4, Epsilon)) ? 0 : 1;
  210. // Testing q1 = q2 k == 2
  211. // Must be 90 degrees rotation on Y : 0 0.7 0 0.7
  212. glm::quat Y90rot5 = glm::slerp(Y90rot, Y90rot, 0.5f, 2);
  213. Error += glm::all(glm::equal(Y90rot, Y90rot5, Epsilon)) ? 0 : 1;
  214. // Testing 180 degrees rotation
  215. // Must be 90 degrees rotation on almost any axis that is on the XZ plane
  216. glm::quat XZ90rot = glm::slerp(id, -Y90rot, 0.5f, 1);
  217. float XZ90angle = glm::angle(XZ90rot); // Must be PI/4 = 0.78;
  218. Error += glm::equal(XZ90angle, glm::pi<float>() * 1.25f, Epsilon) ? 0 : 1;
  219. // Testing rotation over long arc
  220. // Distance from id to 90 degrees is 270 degrees, so 2/3 of it should be 180 degrees
  221. // Negative quaternion is representing same orientation
  222. glm::quat Neg90rot = glm::slerp(id, Y90rot, 2.0f / 3.0f, -1);
  223. Error += glm::all(glm::equal(Y180rot, -Neg90rot, Epsilon)) ? 0 : 1;
  224. return Error;
  225. }
  226. static int test_quat_mul_vec()
  227. {
  228. int Error(0);
  229. glm::quat q = glm::angleAxis(glm::pi<float>() * 0.5f, glm::vec3(0, 0, 1));
  230. glm::vec3 v(1, 0, 0);
  231. glm::vec3 u(q * v);
  232. glm::vec3 w(u * q);
  233. Error += glm::all(glm::equal(v, w, 0.01f)) ? 0 : 1;
  234. return Error;
  235. }
  236. static int test_mul()
  237. {
  238. int Error = 0;
  239. glm::quat temp1 = glm::normalize(glm::quat(1.0f, glm::vec3(0.0, 1.0, 0.0)));
  240. glm::quat temp2 = glm::normalize(glm::quat(0.5f, glm::vec3(1.0, 0.0, 0.0)));
  241. glm::vec3 transformed0 = (temp1 * glm::vec3(0.0, 1.0, 0.0) * glm::inverse(temp1));
  242. glm::vec3 temp4 = temp2 * transformed0 * glm::inverse(temp2);
  243. Error += glm::all(glm::equal(temp4, glm::vec3(0.0f, -0.28f, -0.96f), 0.01f)) ? 0 : 1;
  244. glm::quat temp5 = glm::normalize(temp1 * temp2);
  245. glm::vec3 temp6 = temp5 * glm::vec3(0.0, 1.0, 0.0) * glm::inverse(temp5);
  246. Error += glm::all(glm::equal(temp6, glm::vec3(-0.48f, 0.36f, -0.8f), 0.01f)) ? 0 : 1;
  247. glm::quat temp7(1.0f, glm::vec3(0.0, 1.0, 0.0));
  248. temp7 *= temp5;
  249. temp7 *= glm::inverse(temp5);
  250. Error += glm::any(glm::notEqual(temp7, glm::quat(1.0f, glm::vec3(0.0, 1.0, 0.0)), glm::epsilon<float>())) ? 1 : 0;
  251. return Error;
  252. }
  253. static int test_identity()
  254. {
  255. int Error = 0;
  256. glm::quat const Q = glm::identity<glm::quat>();
  257. Error += glm::all(glm::equal(Q, glm::quat(1, 0, 0, 0), 0.0001f)) ? 0 : 1;
  258. Error += glm::any(glm::notEqual(Q, glm::quat(1, 0, 0, 0), 0.0001f)) ? 1 : 0;
  259. glm::mat4 const M = glm::identity<glm::mat4x4>();
  260. glm::mat4 const N(1.0f);
  261. Error += glm::all(glm::equal(M, N, 0.0001f)) ? 0 : 1;
  262. return Error;
  263. }
  264. int main()
  265. {
  266. int Error = 0;
  267. Error += test_mul();
  268. Error += test_quat_mul_vec();
  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. Error += test_quat_slerp_spins();
  276. Error += test_identity();
  277. return Error;
  278. }