Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

quat.cpp 16 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://www.wtfpl.net/ for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include "core.h"
  14. #include "lol/unit.h"
  15. namespace lol
  16. {
  17. LOLUNIT_FIXTURE(QuaternionTest)
  18. {
  19. void SetUp() {}
  20. void TearDown() {}
  21. LOLUNIT_TEST(Equality)
  22. {
  23. quat a4(1.f, 2.f, 3.f, 4.f);
  24. quat b4(0.f, 2.f, 3.f, 4.f);
  25. quat c4(1.f, 0.f, 3.f, 4.f);
  26. quat d4(1.f, 2.f, 0.f, 4.f);
  27. quat e4(1.f, 2.f, 3.f, 0.f);
  28. LOLUNIT_ASSERT_EQUAL(a4, a4);
  29. LOLUNIT_ASSERT_NOT_DIFFERENT(a4, a4);
  30. LOLUNIT_ASSERT_DIFFERENT(a4, b4);
  31. LOLUNIT_ASSERT_NOT_EQUAL(a4, b4);
  32. LOLUNIT_ASSERT_DIFFERENT(a4, c4);
  33. LOLUNIT_ASSERT_NOT_EQUAL(a4, c4);
  34. LOLUNIT_ASSERT_DIFFERENT(a4, d4);
  35. LOLUNIT_ASSERT_NOT_EQUAL(a4, d4);
  36. LOLUNIT_ASSERT_DIFFERENT(a4, e4);
  37. LOLUNIT_ASSERT_NOT_EQUAL(a4, e4);
  38. }
  39. LOLUNIT_TEST(UnaryMinus)
  40. {
  41. quat a(1.f, 3.f, 2.f, 4.f);
  42. quat b(-1.f, -3.f, -2.f, -4.f);
  43. LOLUNIT_ASSERT_EQUAL(a, -b);
  44. LOLUNIT_ASSERT_EQUAL(-a, b);
  45. }
  46. LOLUNIT_TEST(Conjugate)
  47. {
  48. quat a(1.f, 3.f, 2.f, 4.f);
  49. quat b(1.f, -3.f, -2.f, -4.f);
  50. LOLUNIT_ASSERT_EQUAL(a, ~b);
  51. LOLUNIT_ASSERT_EQUAL(~a, b);
  52. }
  53. LOLUNIT_TEST(Norm)
  54. {
  55. quat a(2.f, -2.f, -8.f, 3.f);
  56. LOLUNIT_ASSERT_EQUAL(norm(a), 9.f);
  57. quat b = a * ~a;
  58. quat c(norm(a) * norm(a), 0.f, 0.f, 0.f);
  59. LOLUNIT_ASSERT_EQUAL(b, c);
  60. quat d(2.f, 5.f, -4.f, -2.f);
  61. LOLUNIT_ASSERT_EQUAL(norm(a * d), norm(a) * norm(d));
  62. }
  63. LOLUNIT_TEST(Dot)
  64. {
  65. quat a(-1.f, 2.f, -3.f, 4.f);
  66. quat b(8.f, 7.f, 6.f, 5.f);
  67. LOLUNIT_ASSERT_EQUAL(dot(a, b), 8.f);
  68. }
  69. LOLUNIT_TEST(Base)
  70. {
  71. quat one(1.f, 0.f, 0.f, 0.f);
  72. quat i(0.f, 1.f, 0.f, 0.f);
  73. quat j(0.f, 0.f, 1.f, 0.f);
  74. quat k(0.f, 0.f, 0.f, 1.f);
  75. LOLUNIT_ASSERT_EQUAL(norm(one), 1.f);
  76. LOLUNIT_ASSERT_EQUAL(norm(i), 1.f);
  77. LOLUNIT_ASSERT_EQUAL(norm(j), 1.f);
  78. LOLUNIT_ASSERT_EQUAL(norm(k), 1.f);
  79. LOLUNIT_ASSERT_EQUAL(i * i, -one);
  80. LOLUNIT_ASSERT_EQUAL(j * j, -one);
  81. LOLUNIT_ASSERT_EQUAL(k * k, -one);
  82. LOLUNIT_ASSERT_EQUAL(i * j * k, -one);
  83. LOLUNIT_ASSERT_EQUAL(i * j, k);
  84. LOLUNIT_ASSERT_EQUAL(j * i, -k);
  85. LOLUNIT_ASSERT_EQUAL(j * k, i);
  86. LOLUNIT_ASSERT_EQUAL(k * j, -i);
  87. LOLUNIT_ASSERT_EQUAL(k * i, j);
  88. LOLUNIT_ASSERT_EQUAL(i * k, -j);
  89. }
  90. LOLUNIT_TEST(Normalize)
  91. {
  92. quat a(2.f, -2.f, -8.f, 3.f);
  93. quat b = normalize(a);
  94. LOLUNIT_ASSERT_DOUBLES_EQUAL(norm(b), 1.0, 1e-5);
  95. }
  96. LOLUNIT_TEST(Reciprocal)
  97. {
  98. quat a(2.f, -2.f, -8.f, 3.f);
  99. quat b = re(a);
  100. quat c = 1.f / a;
  101. LOLUNIT_ASSERT_DOUBLES_EQUAL(b.w, c.w, 1e-5);
  102. LOLUNIT_ASSERT_DOUBLES_EQUAL(b.x, c.x, 1e-5);
  103. LOLUNIT_ASSERT_DOUBLES_EQUAL(b.y, c.y, 1e-5);
  104. LOLUNIT_ASSERT_DOUBLES_EQUAL(b.z, c.z, 1e-5);
  105. quat m1 = a * b;
  106. quat m2 = b * a;
  107. LOLUNIT_ASSERT_DOUBLES_EQUAL(m1.w, m2.w, 1e-5);
  108. LOLUNIT_ASSERT_DOUBLES_EQUAL(m1.x, m2.x, 1e-5);
  109. LOLUNIT_ASSERT_DOUBLES_EQUAL(m1.y, m2.y, 1e-5);
  110. LOLUNIT_ASSERT_DOUBLES_EQUAL(m1.z, m2.z, 1e-5);
  111. LOLUNIT_ASSERT_DOUBLES_EQUAL(m1.w, 1.0, 1e-5);
  112. LOLUNIT_ASSERT_DOUBLES_EQUAL(m1.x, 0.0, 1e-5);
  113. LOLUNIT_ASSERT_DOUBLES_EQUAL(m1.y, 0.0, 1e-5);
  114. LOLUNIT_ASSERT_DOUBLES_EQUAL(m1.z, 0.0, 1e-5);
  115. }
  116. LOLUNIT_TEST(Rotation)
  117. {
  118. /* Check that rotating 10 degrees twice means rotating 20 degrees */
  119. quat a = quat::rotate(10.f, vec3(1, 0, 0));
  120. quat b = quat::rotate(20.f, vec3(1, 0, 0));
  121. quat c = a * a;
  122. LOLUNIT_ASSERT_DOUBLES_EQUAL(c.w, b.w, 1e-5);
  123. LOLUNIT_ASSERT_DOUBLES_EQUAL(c.x, b.x, 1e-5);
  124. LOLUNIT_ASSERT_DOUBLES_EQUAL(c.y, b.y, 1e-5);
  125. LOLUNIT_ASSERT_DOUBLES_EQUAL(c.z, b.z, 1e-5);
  126. /* Check that rotating 10 degrees then 20 is the same as 20 then 10 */
  127. quat d = a * b;
  128. quat e = b * a;
  129. LOLUNIT_ASSERT_DOUBLES_EQUAL(e.w, d.w, 1e-5);
  130. LOLUNIT_ASSERT_DOUBLES_EQUAL(e.x, d.x, 1e-5);
  131. LOLUNIT_ASSERT_DOUBLES_EQUAL(e.y, d.y, 1e-5);
  132. LOLUNIT_ASSERT_DOUBLES_EQUAL(e.z, d.z, 1e-5);
  133. }
  134. LOLUNIT_TEST(FromTwoVectors)
  135. {
  136. vec3 a(1.f, 2.f, 3.f);
  137. vec3 b(4.f, 5.f, 6.f);
  138. float ratio = length(a) / length(b);
  139. quat q = quat::rotate(a, b);
  140. /* Check that q transforms a into b */
  141. vec3 c = q.transform(a);
  142. LOLUNIT_ASSERT_DOUBLES_EQUAL(c.x, b.x * ratio, 1e-5);
  143. LOLUNIT_ASSERT_DOUBLES_EQUAL(c.y, b.y * ratio, 1e-5);
  144. LOLUNIT_ASSERT_DOUBLES_EQUAL(c.z, b.z * ratio, 1e-5);
  145. /* Check that ~q transforms b into a */
  146. vec3 d = (~q).transform(b);
  147. LOLUNIT_ASSERT_DOUBLES_EQUAL(d.x, a.x / ratio, 1e-5);
  148. LOLUNIT_ASSERT_DOUBLES_EQUAL(d.y, a.y / ratio, 1e-5);
  149. LOLUNIT_ASSERT_DOUBLES_EQUAL(d.z, a.z / ratio, 1e-5);
  150. }
  151. LOLUNIT_TEST(FromEulerNorm)
  152. {
  153. for (int i = 0; i < 100; ++i)
  154. {
  155. vec3 angles(rand(360.f), rand(360.f), rand(360.f));
  156. /* Tait-Bryan */
  157. quat q1 = quat::fromeuler_xyz(angles);
  158. LOLUNIT_ASSERT_DOUBLES_EQUAL(norm(q1), 1.f, 1e-5);
  159. quat q2 = quat::fromeuler_yzx(angles);
  160. LOLUNIT_ASSERT_DOUBLES_EQUAL(norm(q2), 1.f, 1e-5);
  161. quat q3 = quat::fromeuler_zxy(angles);
  162. LOLUNIT_ASSERT_DOUBLES_EQUAL(norm(q3), 1.f, 1e-5);
  163. quat q4 = quat::fromeuler_xzy(angles);
  164. LOLUNIT_ASSERT_DOUBLES_EQUAL(norm(q4), 1.f, 1e-5);
  165. quat q5 = quat::fromeuler_zyx(angles);
  166. LOLUNIT_ASSERT_DOUBLES_EQUAL(norm(q5), 1.f, 1e-5);
  167. quat q6 = quat::fromeuler_yxz(angles);
  168. LOLUNIT_ASSERT_DOUBLES_EQUAL(norm(q6), 1.f, 1e-5);
  169. /* Euler */
  170. quat q7 = quat::fromeuler_xyx(angles);
  171. LOLUNIT_ASSERT_DOUBLES_EQUAL(norm(q7), 1.f, 1e-5);
  172. quat q8 = quat::fromeuler_yzy(angles);
  173. LOLUNIT_ASSERT_DOUBLES_EQUAL(norm(q8), 1.f, 1e-5);
  174. quat q9 = quat::fromeuler_zxz(angles);
  175. LOLUNIT_ASSERT_DOUBLES_EQUAL(norm(q9), 1.f, 1e-5);
  176. quat q10 = quat::fromeuler_xzx(angles);
  177. LOLUNIT_ASSERT_DOUBLES_EQUAL(norm(q10), 1.f, 1e-5);
  178. quat q11 = quat::fromeuler_zyz(angles);
  179. LOLUNIT_ASSERT_DOUBLES_EQUAL(norm(q11), 1.f, 1e-5);
  180. quat q12 = quat::fromeuler_yxy(angles);
  181. LOLUNIT_ASSERT_DOUBLES_EQUAL(norm(q12), 1.f, 1e-5);
  182. }
  183. }
  184. LOLUNIT_TEST(FirstTwoEulerAngles)
  185. {
  186. for (int i = 0; i < 100; ++i)
  187. {
  188. /* We check that fromeuler_xyx and fromeuler_xyz give the
  189. * same result if the 3rd angle is zero. */
  190. vec3 angles(rand(360.f), rand(360.f), 0.f);
  191. quat q1, q2;
  192. q1 = quat::fromeuler_xyz(angles);
  193. q2 = quat::fromeuler_xyx(angles);
  194. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.w, q2.w, 1e-5);
  195. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.x, q2.x, 1e-5);
  196. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.y, q2.y, 1e-5);
  197. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.z, q2.z, 1e-5);
  198. q1 = quat::fromeuler_yzx(angles);
  199. q2 = quat::fromeuler_yzy(angles);
  200. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.w, q2.w, 1e-5);
  201. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.x, q2.x, 1e-5);
  202. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.y, q2.y, 1e-5);
  203. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.z, q2.z, 1e-5);
  204. q1 = quat::fromeuler_zxy(angles);
  205. q2 = quat::fromeuler_zxz(angles);
  206. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.w, q2.w, 1e-5);
  207. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.x, q2.x, 1e-5);
  208. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.y, q2.y, 1e-5);
  209. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.z, q2.z, 1e-5);
  210. q1 = quat::fromeuler_xzy(angles);
  211. q2 = quat::fromeuler_xzx(angles);
  212. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.w, q2.w, 1e-5);
  213. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.x, q2.x, 1e-5);
  214. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.y, q2.y, 1e-5);
  215. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.z, q2.z, 1e-5);
  216. q1 = quat::fromeuler_zyx(angles);
  217. q2 = quat::fromeuler_zyz(angles);
  218. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.w, q2.w, 1e-5);
  219. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.x, q2.x, 1e-5);
  220. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.y, q2.y, 1e-5);
  221. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.z, q2.z, 1e-5);
  222. q1 = quat::fromeuler_yxz(angles);
  223. q2 = quat::fromeuler_yxy(angles);
  224. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.w, q2.w, 1e-5);
  225. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.x, q2.x, 1e-5);
  226. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.y, q2.y, 1e-5);
  227. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.z, q2.z, 1e-5);
  228. }
  229. }
  230. LOLUNIT_TEST(LastTwoEulerAngles)
  231. {
  232. for (int i = 0; i < 100; ++i)
  233. {
  234. /* We check that fromeuler_zyz and fromeuler_xyz give the
  235. * same result if the 1st angle is zero. */
  236. vec3 angles(0.f, rand(360.f), rand(360.f));
  237. quat q1, q2;
  238. q1 = quat::fromeuler_xyz(angles);
  239. q2 = quat::fromeuler_zyz(angles);
  240. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.w, q2.w, 1e-5);
  241. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.x, q2.x, 1e-5);
  242. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.y, q2.y, 1e-5);
  243. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.z, q2.z, 1e-5);
  244. q1 = quat::fromeuler_yzx(angles);
  245. q2 = quat::fromeuler_xzx(angles);
  246. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.w, q2.w, 1e-5);
  247. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.x, q2.x, 1e-5);
  248. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.y, q2.y, 1e-5);
  249. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.z, q2.z, 1e-5);
  250. q1 = quat::fromeuler_zxy(angles);
  251. q2 = quat::fromeuler_yxy(angles);
  252. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.w, q2.w, 1e-5);
  253. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.x, q2.x, 1e-5);
  254. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.y, q2.y, 1e-5);
  255. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.z, q2.z, 1e-5);
  256. q1 = quat::fromeuler_xzy(angles);
  257. q2 = quat::fromeuler_yzy(angles);
  258. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.w, q2.w, 1e-5);
  259. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.x, q2.x, 1e-5);
  260. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.y, q2.y, 1e-5);
  261. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.z, q2.z, 1e-5);
  262. q1 = quat::fromeuler_zyx(angles);
  263. q2 = quat::fromeuler_xyx(angles);
  264. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.w, q2.w, 1e-5);
  265. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.x, q2.x, 1e-5);
  266. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.y, q2.y, 1e-5);
  267. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.z, q2.z, 1e-5);
  268. q1 = quat::fromeuler_yxz(angles);
  269. q2 = quat::fromeuler_zxz(angles);
  270. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.w, q2.w, 1e-5);
  271. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.x, q2.x, 1e-5);
  272. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.y, q2.y, 1e-5);
  273. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.z, q2.z, 1e-5);
  274. }
  275. }
  276. LOLUNIT_TEST(TaitBryanAngles)
  277. {
  278. for (int i = 0; i < 100; ++i)
  279. {
  280. /* Pick a random point and a random quaternion. We want
  281. * to check whether going to Tait-Bryan angles and back to
  282. * quaternion creates the same transform. */
  283. vec3 p(rand(1.f, 2.f), rand(1.f, 2.f), rand(1.f, 2.f));
  284. quat q0 = normalize(quat(rand(-1.f, 1.f), rand(-1.f, 1.f),
  285. rand(-1.f, 1.f), rand(-1.f, 1.f)));
  286. vec3 p0 = q0.transform(p);
  287. /* x-y-z */
  288. quat q1 = quat::fromeuler_xyz(vec3::toeuler_xyz(q0));
  289. vec3 p1 = q1.transform(p);
  290. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.x, p0.x, 1e-4);
  291. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.y, p0.y, 1e-4);
  292. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.z, p0.z, 1e-4);
  293. /* y-z-x */
  294. q1 = quat::fromeuler_yzx(vec3::toeuler_yzx(q0));
  295. p1 = q1.transform(p);
  296. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.x, p0.x, 1e-4);
  297. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.y, p0.y, 1e-4);
  298. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.z, p0.z, 1e-4);
  299. /* z-x-y */
  300. q1 = quat::fromeuler_zxy(vec3::toeuler_zxy(q0));
  301. p1 = q1.transform(p);
  302. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.x, p0.x, 1e-4);
  303. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.y, p0.y, 1e-4);
  304. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.z, p0.z, 1e-4);
  305. /* x-z-y */
  306. q1 = quat::fromeuler_xzy(vec3::toeuler_xzy(q0));
  307. p1 = q1.transform(p);
  308. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.x, p0.x, 1e-4);
  309. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.y, p0.y, 1e-4);
  310. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.z, p0.z, 1e-4);
  311. /* z-y-x */
  312. q1 = quat::fromeuler_zyx(vec3::toeuler_zyx(q0));
  313. p1 = q1.transform(p);
  314. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.x, p0.x, 1e-4);
  315. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.y, p0.y, 1e-4);
  316. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.z, p0.z, 1e-4);
  317. /* y-x-z */
  318. q1 = quat::fromeuler_yxz(vec3::toeuler_yxz(q0));
  319. p1 = q1.transform(p);
  320. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.x, p0.x, 1e-4);
  321. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.y, p0.y, 1e-4);
  322. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.z, p0.z, 1e-4);
  323. }
  324. }
  325. LOLUNIT_TEST(EulerAngles)
  326. {
  327. for (int i = 0; i < 100; ++i)
  328. {
  329. /* Pick a random point and a random quaternion. We want
  330. * to check whether going to Euler angles and back to
  331. * quaternion creates the same transform. */
  332. vec3 p(rand(1.f, 2.f), rand(1.f, 2.f), rand(1.f, 2.f));
  333. quat q0 = normalize(quat(rand(-1.f, 1.f), rand(-1.f, 1.f),
  334. rand(-1.f, 1.f), rand(-1.f, 1.f)));
  335. vec3 p0 = q0.transform(p);
  336. /* x-y-x */
  337. quat q1 = quat::fromeuler_xyx(vec3::toeuler_xyx(q0));
  338. vec3 p1 = q1.transform(p);
  339. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.x, p0.x, 1e-4);
  340. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.y, p0.y, 1e-4);
  341. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.z, p0.z, 1e-4);
  342. /* y-z-y */
  343. q1 = quat::fromeuler_yzy(vec3::toeuler_yzy(q0));
  344. p1 = q1.transform(p);
  345. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.x, p0.x, 1e-4);
  346. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.y, p0.y, 1e-4);
  347. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.z, p0.z, 1e-4);
  348. /* z-x-z */
  349. q1 = quat::fromeuler_zxz(vec3::toeuler_zxz(q0));
  350. p1 = q1.transform(p);
  351. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.x, p0.x, 1e-4);
  352. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.y, p0.y, 1e-4);
  353. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.z, p0.z, 1e-4);
  354. /* x-z-x */
  355. q1 = quat::fromeuler_xzx(vec3::toeuler_xzx(q0));
  356. p1 = q1.transform(p);
  357. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.x, p0.x, 1e-4);
  358. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.y, p0.y, 1e-4);
  359. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.z, p0.z, 1e-4);
  360. /* z-y-z */
  361. q1 = quat::fromeuler_zyz(vec3::toeuler_zyz(q0));
  362. p1 = q1.transform(p);
  363. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.x, p0.x, 1e-4);
  364. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.y, p0.y, 1e-4);
  365. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.z, p0.z, 1e-4);
  366. /* y-x-y */
  367. q1 = quat::fromeuler_yxy(vec3::toeuler_yxy(q0));
  368. p1 = q1.transform(p);
  369. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.x, p0.x, 1e-4);
  370. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.y, p0.y, 1e-4);
  371. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.z, p0.z, 1e-4);
  372. }
  373. }
  374. };
  375. } /* namespace lol */