選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

216 行
7.2 KiB

  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 <lol/main.h>
  14. #include "lol/unit.h"
  15. namespace lol
  16. {
  17. LOLUNIT_FIXTURE(RotationTest)
  18. {
  19. void SetUp() {}
  20. void TearDown() {}
  21. LOLUNIT_TEST(Rotate2D)
  22. {
  23. /* Rotations must be CCW */
  24. mat2 m90 = mat2::rotate(90.f);
  25. vec2 a(2.f, 3.f);
  26. vec2 b = m90 * a;
  27. LOLUNIT_ASSERT_DOUBLES_EQUAL(b.x, -a.y, 1e-5);
  28. LOLUNIT_ASSERT_DOUBLES_EQUAL(b.y, a.x, 1e-5);
  29. float d = determinant(m90);
  30. LOLUNIT_ASSERT_DOUBLES_EQUAL(d, 1.0, 1e-5);
  31. }
  32. LOLUNIT_TEST(Compose2D)
  33. {
  34. /* Rotating 20 degrees twice must equal rotating 40 degrees */
  35. mat2 m20 = mat2::rotate(20.f);
  36. mat2 m40 = mat2::rotate(40.f);
  37. mat2 m20x20 = m20 * m20;
  38. LOLUNIT_ASSERT_DOUBLES_EQUAL(m20x20[0][0], m40[0][0], 1e-5);
  39. LOLUNIT_ASSERT_DOUBLES_EQUAL(m20x20[1][0], m40[1][0], 1e-5);
  40. LOLUNIT_ASSERT_DOUBLES_EQUAL(m20x20[0][1], m40[0][1], 1e-5);
  41. LOLUNIT_ASSERT_DOUBLES_EQUAL(m20x20[1][1], m40[1][1], 1e-5);
  42. }
  43. LOLUNIT_TEST(Rotate3D)
  44. {
  45. /* Rotations must be CCW along each axis */
  46. mat3 m90x = mat3::rotate(90.f, 1.f, 0.f, 0.f);
  47. mat3 m90y = mat3::rotate(90.f, 0.f, 1.f, 0.f);
  48. mat3 m90z = mat3::rotate(90.f, 0.f, 0.f, 1.f);
  49. vec3 a(2.f, 3.f, 4.f);
  50. vec3 b = m90x * a;
  51. vec3 c = m90y * a;
  52. vec3 d = m90z * a;
  53. LOLUNIT_ASSERT_DOUBLES_EQUAL(b.x, a.x, 1e-5);
  54. LOLUNIT_ASSERT_DOUBLES_EQUAL(b.y, -a.z, 1e-5);
  55. LOLUNIT_ASSERT_DOUBLES_EQUAL(b.z, a.y, 1e-5);
  56. LOLUNIT_ASSERT_DOUBLES_EQUAL(c.x, a.z, 1e-5);
  57. LOLUNIT_ASSERT_DOUBLES_EQUAL(c.y, a.y, 1e-5);
  58. LOLUNIT_ASSERT_DOUBLES_EQUAL(c.z, -a.x, 1e-5);
  59. LOLUNIT_ASSERT_DOUBLES_EQUAL(d.x, -a.y, 1e-5);
  60. LOLUNIT_ASSERT_DOUBLES_EQUAL(d.y, a.x, 1e-5);
  61. LOLUNIT_ASSERT_DOUBLES_EQUAL(d.z, a.z, 1e-5);
  62. float dx = determinant(m90x);
  63. float dy = determinant(m90y);
  64. float dz = determinant(m90z);
  65. LOLUNIT_ASSERT_DOUBLES_EQUAL(dx, 1.0, 1e-5);
  66. LOLUNIT_ASSERT_DOUBLES_EQUAL(dy, 1.0, 1e-5);
  67. LOLUNIT_ASSERT_DOUBLES_EQUAL(dz, 1.0, 1e-5);
  68. }
  69. LOLUNIT_TEST(Compose3D)
  70. {
  71. /* Rotating 20 degrees twice must equal rotating 40 degrees */
  72. mat3 m20 = mat3::rotate(20.f, 1.f, 2.f, 3.f);
  73. mat3 m40 = mat3::rotate(40.f, 1.f, 2.f, 3.f);
  74. mat3 m20x20 = m20 * m20;
  75. LOLUNIT_ASSERT_DOUBLES_EQUAL(m20x20[0][0], m40[0][0], 1e-5);
  76. LOLUNIT_ASSERT_DOUBLES_EQUAL(m20x20[1][0], m40[1][0], 1e-5);
  77. LOLUNIT_ASSERT_DOUBLES_EQUAL(m20x20[2][0], m40[2][0], 1e-5);
  78. LOLUNIT_ASSERT_DOUBLES_EQUAL(m20x20[0][1], m40[0][1], 1e-5);
  79. LOLUNIT_ASSERT_DOUBLES_EQUAL(m20x20[1][1], m40[1][1], 1e-5);
  80. LOLUNIT_ASSERT_DOUBLES_EQUAL(m20x20[2][1], m40[2][1], 1e-5);
  81. LOLUNIT_ASSERT_DOUBLES_EQUAL(m20x20[0][2], m40[0][2], 1e-5);
  82. LOLUNIT_ASSERT_DOUBLES_EQUAL(m20x20[1][2], m40[1][2], 1e-5);
  83. LOLUNIT_ASSERT_DOUBLES_EQUAL(m20x20[2][2], m40[2][2], 1e-5);
  84. }
  85. LOLUNIT_TEST(QuaternionTransform)
  86. {
  87. /* Rotating using a quaternion must equal rotating using a matrix */
  88. mat3 m20 = mat3::rotate(20.f, 1.f, 2.f, 3.f);
  89. quat q20 = quat::rotate(20.f, 1.f, 2.f, 3.f);
  90. vec3 a(-2.f, 4.f, 3.f);
  91. vec3 b = m20 * a;
  92. vec3 c = q20.transform(a);
  93. LOLUNIT_ASSERT_DOUBLES_EQUAL(c.x, b.x, 1e-5);
  94. LOLUNIT_ASSERT_DOUBLES_EQUAL(c.y, b.y, 1e-5);
  95. LOLUNIT_ASSERT_DOUBLES_EQUAL(c.z, b.z, 1e-5);
  96. float n = norm(q20);
  97. LOLUNIT_ASSERT_DOUBLES_EQUAL(n, 1.0, 1e-5);
  98. }
  99. LOLUNIT_TEST(QuaternionFromMatrix)
  100. {
  101. /* A rotation matrix converted to a quaternion should match the
  102. * quaternion built with the same parameters */
  103. quat q1 = quat::rotate(20.f, 1.f, 2.f, 3.f);
  104. quat q2 = quat(mat3::rotate(20.f, 1.f, 2.f, 3.f));
  105. LOLUNIT_ASSERT_DOUBLES_EQUAL(q2.w, q1.w, 1e-5);
  106. LOLUNIT_ASSERT_DOUBLES_EQUAL(q2.x, q1.x, 1e-5);
  107. LOLUNIT_ASSERT_DOUBLES_EQUAL(q2.y, q1.y, 1e-5);
  108. LOLUNIT_ASSERT_DOUBLES_EQUAL(q2.z, q1.z, 1e-5);
  109. float n1 = norm(q1);
  110. float n2 = norm(q2);
  111. LOLUNIT_ASSERT_DOUBLES_EQUAL(n1, 1.0, 1e-5);
  112. LOLUNIT_ASSERT_DOUBLES_EQUAL(n2, 1.0, 1e-5);
  113. }
  114. LOLUNIT_TEST(MatrixFromQuaternion)
  115. {
  116. /* A quaternion converted to a rotation matrix should match the
  117. * rotation matrix built with the same parameters */
  118. mat3 m1 = mat3::rotate(60.f, 1.f, -2.f, 3.f);
  119. mat3 m2 = mat3(quat::rotate(60.f, 1.f, -2.f, 3.f));
  120. LOLUNIT_ASSERT_DOUBLES_EQUAL(m2[0][0], m1[0][0], 1e-5);
  121. LOLUNIT_ASSERT_DOUBLES_EQUAL(m2[1][0], m1[1][0], 1e-5);
  122. LOLUNIT_ASSERT_DOUBLES_EQUAL(m2[2][0], m1[2][0], 1e-5);
  123. LOLUNIT_ASSERT_DOUBLES_EQUAL(m2[0][1], m1[0][1], 1e-5);
  124. LOLUNIT_ASSERT_DOUBLES_EQUAL(m2[1][1], m1[1][1], 1e-5);
  125. LOLUNIT_ASSERT_DOUBLES_EQUAL(m2[2][1], m1[2][1], 1e-5);
  126. LOLUNIT_ASSERT_DOUBLES_EQUAL(m2[0][2], m1[0][2], 1e-5);
  127. LOLUNIT_ASSERT_DOUBLES_EQUAL(m2[1][2], m1[1][2], 1e-5);
  128. LOLUNIT_ASSERT_DOUBLES_EQUAL(m2[2][2], m1[2][2], 1e-5);
  129. float d1 = determinant(m1);
  130. float d2 = determinant(m2);
  131. LOLUNIT_ASSERT_DOUBLES_EQUAL(d1, 1.0, 1e-5);
  132. LOLUNIT_ASSERT_DOUBLES_EQUAL(d2, 1.0, 1e-5);
  133. }
  134. LOLUNIT_TEST(MatrixCompositionThroughQuaternions)
  135. {
  136. /* Combining two rotation matrices should match the matrix created
  137. * from the combination of the two equivalent quaternions */
  138. mat3 m1 = mat3::rotate(60.f, 1.f, -2.f, 3.f);
  139. mat3 m2 = mat3::rotate(20.f, -3.f, 1.f, -3.f);
  140. mat3 m3 = m2 * m1;
  141. mat3 m4(quat(m2) * quat(m1));
  142. LOLUNIT_ASSERT_DOUBLES_EQUAL(m4[0][0], m3[0][0], 1e-5);
  143. LOLUNIT_ASSERT_DOUBLES_EQUAL(m4[1][0], m3[1][0], 1e-5);
  144. LOLUNIT_ASSERT_DOUBLES_EQUAL(m4[2][0], m3[2][0], 1e-5);
  145. LOLUNIT_ASSERT_DOUBLES_EQUAL(m4[0][1], m3[0][1], 1e-5);
  146. LOLUNIT_ASSERT_DOUBLES_EQUAL(m4[1][1], m3[1][1], 1e-5);
  147. LOLUNIT_ASSERT_DOUBLES_EQUAL(m4[2][1], m3[2][1], 1e-5);
  148. LOLUNIT_ASSERT_DOUBLES_EQUAL(m4[0][2], m3[0][2], 1e-5);
  149. LOLUNIT_ASSERT_DOUBLES_EQUAL(m4[1][2], m3[1][2], 1e-5);
  150. LOLUNIT_ASSERT_DOUBLES_EQUAL(m4[2][2], m3[2][2], 1e-5);
  151. }
  152. LOLUNIT_TEST(QuaternionCompositionThroughMatrices)
  153. {
  154. /* Combining two quaternions should match the quaternion created
  155. * from the combination of the two equivalent rotation matrices */
  156. quat q1 = quat::rotate(60.f, 1.f, -2.f, 3.f);
  157. quat q2 = quat::rotate(20.f, -3.f, 1.f, -2.f);
  158. quat q3 = q2 * q1;
  159. quat q4(mat3(q2) * mat3(q1));
  160. LOLUNIT_ASSERT_DOUBLES_EQUAL(q4.w, q3.w, 1e-5);
  161. LOLUNIT_ASSERT_DOUBLES_EQUAL(q4.x, q3.x, 1e-5);
  162. LOLUNIT_ASSERT_DOUBLES_EQUAL(q4.y, q3.y, 1e-5);
  163. LOLUNIT_ASSERT_DOUBLES_EQUAL(q4.z, q3.z, 1e-5);
  164. }
  165. };
  166. } /* namespace lol */