You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

544 line
18 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2014 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. {
  21. /* Generate identity quaternions */
  22. m_vectorpairs.Push(vec3::axis_x, vec3::axis_x);
  23. m_vectorpairs.Push(2.f * vec3::axis_x, 3.f * vec3::axis_x);
  24. /* Generate 90-degree rotations */
  25. m_vectorpairs.Push(vec3::axis_x, vec3::axis_y);
  26. m_vectorpairs.Push(2.f * vec3::axis_x, 3.f * vec3::axis_y);
  27. /* Generate 180-degree rotations */
  28. m_vectorpairs.Push(vec3::axis_x, -vec3::axis_x);
  29. m_vectorpairs.Push(2.f * vec3::axis_x, -3.f * vec3::axis_x);
  30. /* Fill array with random test values */
  31. for (int i = 0; i < 10000; ++i)
  32. {
  33. vec3 v1 = lol::pow(10.f, rand(-5.f, 5.f))
  34. * vec3(rand(-1.f, 1.f), rand(-1.f, 1.f), rand(-1.f, 1.f));
  35. vec3 v2 = lol::pow(10.f, rand(-5.f, 5.f))
  36. * vec3(rand(-1.f, 1.f), rand(-1.f, 1.f), rand(-1.f, 1.f));
  37. m_vectorpairs.Push(v1, v2);
  38. }
  39. }
  40. void TearDown() {}
  41. LOLUNIT_TEST(Equality)
  42. {
  43. quat a4(1.f, 2.f, 3.f, 4.f);
  44. quat b4(0.f, 2.f, 3.f, 4.f);
  45. quat c4(1.f, 0.f, 3.f, 4.f);
  46. quat d4(1.f, 2.f, 0.f, 4.f);
  47. quat e4(1.f, 2.f, 3.f, 0.f);
  48. LOLUNIT_ASSERT_EQUAL(a4, a4);
  49. LOLUNIT_ASSERT_NOT_DIFFERENT(a4, a4);
  50. LOLUNIT_ASSERT_DIFFERENT(a4, b4);
  51. LOLUNIT_ASSERT_NOT_EQUAL(a4, b4);
  52. LOLUNIT_ASSERT_DIFFERENT(a4, c4);
  53. LOLUNIT_ASSERT_NOT_EQUAL(a4, c4);
  54. LOLUNIT_ASSERT_DIFFERENT(a4, d4);
  55. LOLUNIT_ASSERT_NOT_EQUAL(a4, d4);
  56. LOLUNIT_ASSERT_DIFFERENT(a4, e4);
  57. LOLUNIT_ASSERT_NOT_EQUAL(a4, e4);
  58. }
  59. LOLUNIT_TEST(UnaryMinus)
  60. {
  61. quat a(1.f, 3.f, 2.f, 4.f);
  62. quat b(-1.f, -3.f, -2.f, -4.f);
  63. LOLUNIT_ASSERT_EQUAL(a, -b);
  64. LOLUNIT_ASSERT_EQUAL(-a, b);
  65. }
  66. LOLUNIT_TEST(Conjugate)
  67. {
  68. quat a(1.f, 3.f, 2.f, 4.f);
  69. quat b(1.f, -3.f, -2.f, -4.f);
  70. LOLUNIT_ASSERT_EQUAL(a, ~b);
  71. LOLUNIT_ASSERT_EQUAL(~a, b);
  72. }
  73. LOLUNIT_TEST(Norm)
  74. {
  75. quat a(2.f, -2.f, -8.f, 3.f);
  76. LOLUNIT_ASSERT_EQUAL(norm(a), 9.f);
  77. quat b = a * ~a;
  78. quat c(norm(a) * norm(a), 0.f, 0.f, 0.f);
  79. LOLUNIT_ASSERT_EQUAL(b, c);
  80. quat d(2.f, 5.f, -4.f, -2.f);
  81. LOLUNIT_ASSERT_EQUAL(norm(a * d), norm(a) * norm(d));
  82. }
  83. LOLUNIT_TEST(Dot)
  84. {
  85. quat a(-1.f, 2.f, -3.f, 4.f);
  86. quat b(8.f, 7.f, 6.f, 5.f);
  87. LOLUNIT_ASSERT_EQUAL(dot(a, b), 8.f);
  88. }
  89. LOLUNIT_TEST(Base)
  90. {
  91. quat one(1.f, 0.f, 0.f, 0.f);
  92. quat i(0.f, 1.f, 0.f, 0.f);
  93. quat j(0.f, 0.f, 1.f, 0.f);
  94. quat k(0.f, 0.f, 0.f, 1.f);
  95. LOLUNIT_ASSERT_EQUAL(norm(one), 1.f);
  96. LOLUNIT_ASSERT_EQUAL(norm(i), 1.f);
  97. LOLUNIT_ASSERT_EQUAL(norm(j), 1.f);
  98. LOLUNIT_ASSERT_EQUAL(norm(k), 1.f);
  99. LOLUNIT_ASSERT_EQUAL(i * i, -one);
  100. LOLUNIT_ASSERT_EQUAL(j * j, -one);
  101. LOLUNIT_ASSERT_EQUAL(k * k, -one);
  102. LOLUNIT_ASSERT_EQUAL(i * j * k, -one);
  103. LOLUNIT_ASSERT_EQUAL(i * j, k);
  104. LOLUNIT_ASSERT_EQUAL(j * i, -k);
  105. LOLUNIT_ASSERT_EQUAL(j * k, i);
  106. LOLUNIT_ASSERT_EQUAL(k * j, -i);
  107. LOLUNIT_ASSERT_EQUAL(k * i, j);
  108. LOLUNIT_ASSERT_EQUAL(i * k, -j);
  109. }
  110. LOLUNIT_TEST(Normalize)
  111. {
  112. quat a(2.f, -2.f, -8.f, 3.f);
  113. quat b = normalize(a);
  114. LOLUNIT_ASSERT_DOUBLES_EQUAL(norm(b), 1.0, 1e-5);
  115. }
  116. LOLUNIT_TEST(Reciprocal)
  117. {
  118. quat a(2.f, -2.f, -8.f, 3.f);
  119. quat b = re(a);
  120. quat c = 1.f / a;
  121. LOLUNIT_ASSERT_DOUBLES_EQUAL(b.w, c.w, 1e-5);
  122. LOLUNIT_ASSERT_DOUBLES_EQUAL(b.x, c.x, 1e-5);
  123. LOLUNIT_ASSERT_DOUBLES_EQUAL(b.y, c.y, 1e-5);
  124. LOLUNIT_ASSERT_DOUBLES_EQUAL(b.z, c.z, 1e-5);
  125. quat m1 = a * b;
  126. quat m2 = b * a;
  127. LOLUNIT_ASSERT_DOUBLES_EQUAL(m1.w, m2.w, 1e-5);
  128. LOLUNIT_ASSERT_DOUBLES_EQUAL(m1.x, m2.x, 1e-5);
  129. LOLUNIT_ASSERT_DOUBLES_EQUAL(m1.y, m2.y, 1e-5);
  130. LOLUNIT_ASSERT_DOUBLES_EQUAL(m1.z, m2.z, 1e-5);
  131. LOLUNIT_ASSERT_DOUBLES_EQUAL(m1.w, 1.0, 1e-5);
  132. LOLUNIT_ASSERT_DOUBLES_EQUAL(m1.x, 0.0, 1e-5);
  133. LOLUNIT_ASSERT_DOUBLES_EQUAL(m1.y, 0.0, 1e-5);
  134. LOLUNIT_ASSERT_DOUBLES_EQUAL(m1.z, 0.0, 1e-5);
  135. }
  136. LOLUNIT_TEST(Rotation)
  137. {
  138. /* Check that rotating 10 degrees twice means rotating 20 degrees */
  139. quat a = quat::rotate(10.f, vec3::axis_x);
  140. quat b = quat::rotate(20.f, vec3::axis_x);
  141. quat c = a * a;
  142. LOLUNIT_ASSERT_DOUBLES_EQUAL(c.w, b.w, 1e-5);
  143. LOLUNIT_ASSERT_DOUBLES_EQUAL(c.x, b.x, 1e-5);
  144. LOLUNIT_ASSERT_DOUBLES_EQUAL(c.y, b.y, 1e-5);
  145. LOLUNIT_ASSERT_DOUBLES_EQUAL(c.z, b.z, 1e-5);
  146. /* Check that rotating 10 degrees then 20 is the same as 20 then 10 */
  147. quat d = a * b;
  148. quat e = b * a;
  149. LOLUNIT_ASSERT_DOUBLES_EQUAL(e.w, d.w, 1e-5);
  150. LOLUNIT_ASSERT_DOUBLES_EQUAL(e.x, d.x, 1e-5);
  151. LOLUNIT_ASSERT_DOUBLES_EQUAL(e.y, d.y, 1e-5);
  152. LOLUNIT_ASSERT_DOUBLES_EQUAL(e.z, d.z, 1e-5);
  153. }
  154. LOLUNIT_TEST(ToAxisAngle)
  155. {
  156. quat q = quat::rotate(10.f, vec3::axis_x);
  157. vec3 axis = q.axis();
  158. float angle = q.angle();
  159. LOLUNIT_ASSERT_DOUBLES_EQUAL(1.0, axis.x, 1e-6);
  160. LOLUNIT_ASSERT_DOUBLES_EQUAL(0.0, axis.y, 1e-6);
  161. LOLUNIT_ASSERT_DOUBLES_EQUAL(0.0, axis.z, 1e-6);
  162. LOLUNIT_ASSERT_DOUBLES_EQUAL(10.0, (double)degrees(angle), 1e-6);
  163. }
  164. LOLUNIT_TEST(FromTwoVectors)
  165. {
  166. for (int i = 0; i < m_vectorpairs.Count(); ++i)
  167. {
  168. vec3 a = m_vectorpairs[i].m1;
  169. vec3 b = m_vectorpairs[i].m2;
  170. vec3 da = normalize(a);
  171. vec3 db = normalize(b);
  172. quat q = quat::rotate(a, b);
  173. /* Check that q is a unit quaternion */
  174. LOLUNIT_ASSERT_DOUBLES_EQUAL(1.0, (double)norm(q), 1e-5);
  175. /* Check that q transforms da into db */
  176. vec3 c = q.transform(da);
  177. LOLUNIT_ASSERT_DOUBLES_EQUAL(c.x, db.x, 1e-5);
  178. LOLUNIT_ASSERT_DOUBLES_EQUAL(c.y, db.y, 1e-5);
  179. LOLUNIT_ASSERT_DOUBLES_EQUAL(c.z, db.z, 1e-5);
  180. /* Check that ~q transforms db into da */
  181. vec3 d = (~q).transform(db);
  182. LOLUNIT_ASSERT_DOUBLES_EQUAL(d.x, da.x, 1e-5);
  183. LOLUNIT_ASSERT_DOUBLES_EQUAL(d.y, da.y, 1e-5);
  184. LOLUNIT_ASSERT_DOUBLES_EQUAL(d.z, da.z, 1e-5);
  185. if (distance(da, db) > 1e-6f)
  186. {
  187. /* If da and db differ, check that the rotation axis is normal to both
  188. * vectors, which is only true if the rotation uses the shortest path. */
  189. vec3 axis = q.axis();
  190. LOLUNIT_ASSERT_DOUBLES_EQUAL(0.0, (double)dot(axis, da), 1e-5);
  191. LOLUNIT_ASSERT_DOUBLES_EQUAL(0.0, (double)dot(axis, db), 1e-5);
  192. }
  193. else
  194. {
  195. /* If da and db are roughly the same, check that the rotation angle
  196. * is zero. */
  197. LOLUNIT_ASSERT_DOUBLES_EQUAL(0.0, (double)q.angle(), 1e-5);
  198. }
  199. }
  200. }
  201. LOLUNIT_TEST(FromEulerNorm)
  202. {
  203. for (int i = 0; i < 100; ++i)
  204. {
  205. vec3 angles(rand(360.f), rand(360.f), rand(360.f));
  206. /* Tait-Bryan */
  207. quat q1 = quat::fromeuler_xyz(angles);
  208. LOLUNIT_ASSERT_DOUBLES_EQUAL(norm(q1), 1.f, 1e-5);
  209. quat q2 = quat::fromeuler_yzx(angles);
  210. LOLUNIT_ASSERT_DOUBLES_EQUAL(norm(q2), 1.f, 1e-5);
  211. quat q3 = quat::fromeuler_zxy(angles);
  212. LOLUNIT_ASSERT_DOUBLES_EQUAL(norm(q3), 1.f, 1e-5);
  213. quat q4 = quat::fromeuler_xzy(angles);
  214. LOLUNIT_ASSERT_DOUBLES_EQUAL(norm(q4), 1.f, 1e-5);
  215. quat q5 = quat::fromeuler_zyx(angles);
  216. LOLUNIT_ASSERT_DOUBLES_EQUAL(norm(q5), 1.f, 1e-5);
  217. quat q6 = quat::fromeuler_yxz(angles);
  218. LOLUNIT_ASSERT_DOUBLES_EQUAL(norm(q6), 1.f, 1e-5);
  219. /* Euler */
  220. quat q7 = quat::fromeuler_xyx(angles);
  221. LOLUNIT_ASSERT_DOUBLES_EQUAL(norm(q7), 1.f, 1e-5);
  222. quat q8 = quat::fromeuler_yzy(angles);
  223. LOLUNIT_ASSERT_DOUBLES_EQUAL(norm(q8), 1.f, 1e-5);
  224. quat q9 = quat::fromeuler_zxz(angles);
  225. LOLUNIT_ASSERT_DOUBLES_EQUAL(norm(q9), 1.f, 1e-5);
  226. quat q10 = quat::fromeuler_xzx(angles);
  227. LOLUNIT_ASSERT_DOUBLES_EQUAL(norm(q10), 1.f, 1e-5);
  228. quat q11 = quat::fromeuler_zyz(angles);
  229. LOLUNIT_ASSERT_DOUBLES_EQUAL(norm(q11), 1.f, 1e-5);
  230. quat q12 = quat::fromeuler_yxy(angles);
  231. LOLUNIT_ASSERT_DOUBLES_EQUAL(norm(q12), 1.f, 1e-5);
  232. }
  233. }
  234. LOLUNIT_TEST(FirstTwoEulerAngles)
  235. {
  236. for (int i = 0; i < 100; ++i)
  237. {
  238. /* We check that fromeuler_xyx and fromeuler_xyz give the
  239. * same result if the 3rd angle is zero. */
  240. vec3 angles(rand(360.f), rand(360.f), 0.f);
  241. quat q1, q2;
  242. q1 = quat::fromeuler_xyz(angles);
  243. q2 = quat::fromeuler_xyx(angles);
  244. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.w, q2.w, 1e-5);
  245. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.x, q2.x, 1e-5);
  246. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.y, q2.y, 1e-5);
  247. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.z, q2.z, 1e-5);
  248. q1 = quat::fromeuler_yzx(angles);
  249. q2 = quat::fromeuler_yzy(angles);
  250. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.w, q2.w, 1e-5);
  251. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.x, q2.x, 1e-5);
  252. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.y, q2.y, 1e-5);
  253. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.z, q2.z, 1e-5);
  254. q1 = quat::fromeuler_zxy(angles);
  255. q2 = quat::fromeuler_zxz(angles);
  256. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.w, q2.w, 1e-5);
  257. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.x, q2.x, 1e-5);
  258. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.y, q2.y, 1e-5);
  259. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.z, q2.z, 1e-5);
  260. q1 = quat::fromeuler_xzy(angles);
  261. q2 = quat::fromeuler_xzx(angles);
  262. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.w, q2.w, 1e-5);
  263. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.x, q2.x, 1e-5);
  264. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.y, q2.y, 1e-5);
  265. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.z, q2.z, 1e-5);
  266. q1 = quat::fromeuler_zyx(angles);
  267. q2 = quat::fromeuler_zyz(angles);
  268. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.w, q2.w, 1e-5);
  269. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.x, q2.x, 1e-5);
  270. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.y, q2.y, 1e-5);
  271. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.z, q2.z, 1e-5);
  272. q1 = quat::fromeuler_yxz(angles);
  273. q2 = quat::fromeuler_yxy(angles);
  274. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.w, q2.w, 1e-5);
  275. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.x, q2.x, 1e-5);
  276. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.y, q2.y, 1e-5);
  277. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.z, q2.z, 1e-5);
  278. }
  279. }
  280. LOLUNIT_TEST(LastTwoEulerAngles)
  281. {
  282. for (int i = 0; i < 100; ++i)
  283. {
  284. /* We check that fromeuler_zyz and fromeuler_xyz give the
  285. * same result if the 1st angle is zero. */
  286. vec3 angles(0.f, rand(360.f), rand(360.f));
  287. quat q1, q2;
  288. q1 = quat::fromeuler_xyz(angles);
  289. q2 = quat::fromeuler_zyz(angles);
  290. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.w, q2.w, 1e-5);
  291. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.x, q2.x, 1e-5);
  292. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.y, q2.y, 1e-5);
  293. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.z, q2.z, 1e-5);
  294. q1 = quat::fromeuler_yzx(angles);
  295. q2 = quat::fromeuler_xzx(angles);
  296. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.w, q2.w, 1e-5);
  297. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.x, q2.x, 1e-5);
  298. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.y, q2.y, 1e-5);
  299. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.z, q2.z, 1e-5);
  300. q1 = quat::fromeuler_zxy(angles);
  301. q2 = quat::fromeuler_yxy(angles);
  302. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.w, q2.w, 1e-5);
  303. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.x, q2.x, 1e-5);
  304. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.y, q2.y, 1e-5);
  305. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.z, q2.z, 1e-5);
  306. q1 = quat::fromeuler_xzy(angles);
  307. q2 = quat::fromeuler_yzy(angles);
  308. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.w, q2.w, 1e-5);
  309. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.x, q2.x, 1e-5);
  310. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.y, q2.y, 1e-5);
  311. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.z, q2.z, 1e-5);
  312. q1 = quat::fromeuler_zyx(angles);
  313. q2 = quat::fromeuler_xyx(angles);
  314. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.w, q2.w, 1e-5);
  315. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.x, q2.x, 1e-5);
  316. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.y, q2.y, 1e-5);
  317. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.z, q2.z, 1e-5);
  318. q1 = quat::fromeuler_yxz(angles);
  319. q2 = quat::fromeuler_zxz(angles);
  320. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.w, q2.w, 1e-5);
  321. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.x, q2.x, 1e-5);
  322. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.y, q2.y, 1e-5);
  323. LOLUNIT_ASSERT_DOUBLES_EQUAL(q1.z, q2.z, 1e-5);
  324. }
  325. }
  326. LOLUNIT_TEST(TaitBryanAngles)
  327. {
  328. for (int i = 0; i < 100; ++i)
  329. {
  330. /* Pick a random point and a random quaternion. We want
  331. * to check whether going to Tait-Bryan angles and back to
  332. * quaternion creates the same transform. */
  333. vec3 p(rand(1.f, 2.f), rand(1.f, 2.f), rand(1.f, 2.f));
  334. quat q0 = normalize(quat(rand(-1.f, 1.f), rand(-1.f, 1.f),
  335. rand(-1.f, 1.f), rand(-1.f, 1.f)));
  336. vec3 p0 = q0.transform(p);
  337. /* x-y-z */
  338. quat q1 = quat::fromeuler_xyz(vec3::toeuler_xyz(q0));
  339. vec3 p1 = q1.transform(p);
  340. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.x, p0.x, 1e-4);
  341. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.y, p0.y, 1e-4);
  342. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.z, p0.z, 1e-4);
  343. /* y-z-x */
  344. q1 = quat::fromeuler_yzx(vec3::toeuler_yzx(q0));
  345. p1 = q1.transform(p);
  346. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.x, p0.x, 1e-4);
  347. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.y, p0.y, 1e-4);
  348. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.z, p0.z, 1e-4);
  349. /* z-x-y */
  350. q1 = quat::fromeuler_zxy(vec3::toeuler_zxy(q0));
  351. p1 = q1.transform(p);
  352. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.x, p0.x, 1e-4);
  353. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.y, p0.y, 1e-4);
  354. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.z, p0.z, 1e-4);
  355. /* x-z-y */
  356. q1 = quat::fromeuler_xzy(vec3::toeuler_xzy(q0));
  357. p1 = q1.transform(p);
  358. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.x, p0.x, 1e-4);
  359. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.y, p0.y, 1e-4);
  360. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.z, p0.z, 1e-4);
  361. /* z-y-x */
  362. q1 = quat::fromeuler_zyx(vec3::toeuler_zyx(q0));
  363. p1 = q1.transform(p);
  364. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.x, p0.x, 1e-4);
  365. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.y, p0.y, 1e-4);
  366. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.z, p0.z, 1e-4);
  367. /* y-x-z */
  368. q1 = quat::fromeuler_yxz(vec3::toeuler_yxz(q0));
  369. p1 = q1.transform(p);
  370. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.x, p0.x, 1e-4);
  371. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.y, p0.y, 1e-4);
  372. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.z, p0.z, 1e-4);
  373. }
  374. }
  375. LOLUNIT_TEST(EulerAngles)
  376. {
  377. for (int i = 0; i < 100; ++i)
  378. {
  379. /* Pick a random point and a random quaternion. We want
  380. * to check whether going to Euler angles and back to
  381. * quaternion creates the same transform. */
  382. vec3 p(rand(1.f, 2.f), rand(1.f, 2.f), rand(1.f, 2.f));
  383. quat q0 = normalize(quat(rand(-1.f, 1.f), rand(-1.f, 1.f),
  384. rand(-1.f, 1.f), rand(-1.f, 1.f)));
  385. vec3 p0 = q0.transform(p);
  386. /* x-y-x */
  387. quat q1 = quat::fromeuler_xyx(vec3::toeuler_xyx(q0));
  388. vec3 p1 = q1.transform(p);
  389. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.x, p0.x, 1e-4);
  390. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.y, p0.y, 1e-4);
  391. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.z, p0.z, 1e-4);
  392. /* y-z-y */
  393. q1 = quat::fromeuler_yzy(vec3::toeuler_yzy(q0));
  394. p1 = q1.transform(p);
  395. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.x, p0.x, 1e-4);
  396. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.y, p0.y, 1e-4);
  397. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.z, p0.z, 1e-4);
  398. /* z-x-z */
  399. q1 = quat::fromeuler_zxz(vec3::toeuler_zxz(q0));
  400. p1 = q1.transform(p);
  401. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.x, p0.x, 1e-4);
  402. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.y, p0.y, 1e-4);
  403. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.z, p0.z, 1e-4);
  404. /* x-z-x */
  405. q1 = quat::fromeuler_xzx(vec3::toeuler_xzx(q0));
  406. p1 = q1.transform(p);
  407. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.x, p0.x, 1e-4);
  408. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.y, p0.y, 1e-4);
  409. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.z, p0.z, 1e-4);
  410. /* z-y-z */
  411. q1 = quat::fromeuler_zyz(vec3::toeuler_zyz(q0));
  412. p1 = q1.transform(p);
  413. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.x, p0.x, 1e-4);
  414. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.y, p0.y, 1e-4);
  415. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.z, p0.z, 1e-4);
  416. /* y-x-y */
  417. q1 = quat::fromeuler_yxy(vec3::toeuler_yxy(q0));
  418. p1 = q1.transform(p);
  419. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.x, p0.x, 1e-4);
  420. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.y, p0.y, 1e-4);
  421. LOLUNIT_ASSERT_DOUBLES_EQUAL(p1.z, p0.z, 1e-4);
  422. }
  423. }
  424. private:
  425. Array<vec3, vec3> m_vectorpairs;
  426. };
  427. } /* namespace lol */