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 4.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2011 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), 81.f);
  57. quat b = a * ~a;
  58. quat c(norm(a), 0.f, 0.f, 0.f);
  59. LOLUNIT_ASSERT_EQUAL(b, c);
  60. quat d(2.f, 3.f, -4.f, -1.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. };
  135. } /* namespace lol */