Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

vector.cpp 5.7 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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(VectorTest)
  18. {
  19. void SetUp() {}
  20. void TearDown() {}
  21. LOLUNIT_TEST(VectorEquality)
  22. {
  23. vec2 a2(1.0f, 2.0f);
  24. vec2 b2(0.0f, 2.0f);
  25. vec2 c2(1.0f, 0.0f);
  26. LOLUNIT_ASSERT_EQUAL(a2, a2);
  27. LOLUNIT_ASSERT_NOT_DIFFERENT(a2, a2);
  28. LOLUNIT_ASSERT_DIFFERENT(a2, b2);
  29. LOLUNIT_ASSERT_NOT_EQUAL(a2, b2);
  30. LOLUNIT_ASSERT_DIFFERENT(a2, c2);
  31. LOLUNIT_ASSERT_NOT_EQUAL(a2, c2);
  32. vec3 a3(1.0f, 2.0f, 3.0f);
  33. vec3 b3(0.0f, 2.0f, 3.0f);
  34. vec3 c3(1.0f, 0.0f, 3.0f);
  35. vec3 d3(1.0f, 2.0f, 0.0f);
  36. LOLUNIT_ASSERT_EQUAL(a3, a3);
  37. LOLUNIT_ASSERT_NOT_DIFFERENT(a3, a3);
  38. LOLUNIT_ASSERT_DIFFERENT(a3, b3);
  39. LOLUNIT_ASSERT_NOT_EQUAL(a3, b3);
  40. LOLUNIT_ASSERT_DIFFERENT(a3, c3);
  41. LOLUNIT_ASSERT_NOT_EQUAL(a3, c3);
  42. LOLUNIT_ASSERT_DIFFERENT(a3, d3);
  43. LOLUNIT_ASSERT_NOT_EQUAL(a3, d3);
  44. vec4 a4(1.0f, 2.0f, 3.0f, 4.0f);
  45. vec4 b4(0.0f, 2.0f, 3.0f, 4.0f);
  46. vec4 c4(1.0f, 0.0f, 3.0f, 4.0f);
  47. vec4 d4(1.0f, 2.0f, 0.0f, 4.0f);
  48. vec4 e4(1.0f, 2.0f, 3.0f, 0.0f);
  49. LOLUNIT_ASSERT_EQUAL(a4, a4);
  50. LOLUNIT_ASSERT_NOT_DIFFERENT(a4, a4);
  51. LOLUNIT_ASSERT_DIFFERENT(a4, b4);
  52. LOLUNIT_ASSERT_NOT_EQUAL(a4, b4);
  53. LOLUNIT_ASSERT_DIFFERENT(a4, c4);
  54. LOLUNIT_ASSERT_NOT_EQUAL(a4, c4);
  55. LOLUNIT_ASSERT_DIFFERENT(a4, d4);
  56. LOLUNIT_ASSERT_NOT_EQUAL(a4, d4);
  57. LOLUNIT_ASSERT_DIFFERENT(a4, e4);
  58. LOLUNIT_ASSERT_NOT_EQUAL(a4, e4);
  59. }
  60. LOLUNIT_TEST(VectorInequality)
  61. {
  62. vec2 a2(1.0f, 3.0f);
  63. vec2 b2(0.0f, 0.0f);
  64. vec2 c2(1.0f, 1.0f);
  65. vec2 d2(2.0f, 2.0f);
  66. vec2 e2(3.0f, 3.0f);
  67. vec2 f2(4.0f, 4.0f);
  68. LOLUNIT_ASSERT_LEQUAL(a2, a2);
  69. LOLUNIT_ASSERT_NOT_LESS(a2, a2);
  70. LOLUNIT_ASSERT_NOT_LEQUAL(a2, b2);
  71. LOLUNIT_ASSERT_NOT_LESS(a2, b2);
  72. LOLUNIT_ASSERT_NOT_LEQUAL(a2, c2);
  73. LOLUNIT_ASSERT_NOT_LESS(a2, c2);
  74. LOLUNIT_ASSERT_NOT_LEQUAL(a2, d2);
  75. LOLUNIT_ASSERT_NOT_LESS(a2, d2);
  76. LOLUNIT_ASSERT_LEQUAL(a2, e2);
  77. LOLUNIT_ASSERT_NOT_LESS(a2, e2);
  78. LOLUNIT_ASSERT_LEQUAL(a2, f2);
  79. LOLUNIT_ASSERT_LESS(a2, f2);
  80. }
  81. LOLUNIT_TEST(VectorSwizzle)
  82. {
  83. vec3 a(1.0f, 2.0f, 3.0f);
  84. vec3 b(4.0f, 5.0f, 6.0f);
  85. vec3 c;
  86. c = a;
  87. c.x = b.y;
  88. LOLUNIT_ASSERT_EQUAL(c.x, 5.0f);
  89. LOLUNIT_ASSERT_EQUAL(c.y, 2.0f);
  90. LOLUNIT_ASSERT_EQUAL(c.z, 3.0f);
  91. #if 0 /* Visual Studio doesn't support these yet. */
  92. c = a;
  93. c.xy = b.yz;
  94. LOLUNIT_ASSERT_EQUAL(c.x, 5.0f);
  95. LOLUNIT_ASSERT_EQUAL(c.y, 6.0f);
  96. LOLUNIT_ASSERT_EQUAL(c.z, 3.0f);
  97. c = a;
  98. c.xy = b.zz;
  99. LOLUNIT_ASSERT_EQUAL(c.x, 6.0f);
  100. LOLUNIT_ASSERT_EQUAL(c.y, 6.0f);
  101. LOLUNIT_ASSERT_EQUAL(c.z, 3.0f);
  102. c = a;
  103. c.xz = b.xy;
  104. LOLUNIT_ASSERT_EQUAL(c.x, 4.0f);
  105. LOLUNIT_ASSERT_EQUAL(c.y, 2.0f);
  106. LOLUNIT_ASSERT_EQUAL(c.z, 5.0f);
  107. c = a;
  108. c.xz = b.xz;
  109. LOLUNIT_ASSERT_EQUAL(c.x, 4.0f);
  110. LOLUNIT_ASSERT_EQUAL(c.y, 2.0f);
  111. LOLUNIT_ASSERT_EQUAL(c.z, 6.0f);
  112. c = a;
  113. c.xz = c.zy = b.yx;
  114. LOLUNIT_ASSERT_EQUAL(c.x, 5.0f);
  115. LOLUNIT_ASSERT_EQUAL(c.y, 4.0f);
  116. LOLUNIT_ASSERT_EQUAL(c.z, 4.0f);
  117. #endif
  118. }
  119. LOLUNIT_TEST(VectorUnaryMinus)
  120. {
  121. vec2 a(1.0f, 3.0f);
  122. vec2 b(-1.0f, -3.0f);
  123. LOLUNIT_ASSERT_EQUAL(a, -b);
  124. LOLUNIT_ASSERT_EQUAL(-a, b);
  125. }
  126. LOLUNIT_TEST(CastVector)
  127. {
  128. vec2 a1(1.0f, 3.0f);
  129. vec3 b(a1, 0.0f);
  130. vec2 a2(b.xy);
  131. LOLUNIT_ASSERT_EQUAL(b.x, a1.x);
  132. LOLUNIT_ASSERT_EQUAL(b.y, a1.y);
  133. LOLUNIT_ASSERT_EQUAL(b.z, 0.0f);
  134. LOLUNIT_ASSERT_EQUAL(a2, a1);
  135. vec4 c(a1, 0.0f, 0.0f);
  136. vec2 a3(c.xy);
  137. LOLUNIT_ASSERT_EQUAL(c.x, a1.x);
  138. LOLUNIT_ASSERT_EQUAL(c.y, a1.y);
  139. LOLUNIT_ASSERT_EQUAL(c.z, 0.0f);
  140. LOLUNIT_ASSERT_EQUAL(c.w, 0.0f);
  141. LOLUNIT_ASSERT_EQUAL(a3, a1);
  142. }
  143. LOLUNIT_TEST(Orthogonal)
  144. {
  145. vec3 a(1.f, 0.f, 0.f);
  146. vec3 b(0.f, 1.f, 0.f);
  147. vec3 c(0.f, 0.f, 1.f);
  148. LOLUNIT_ASSERT_DOUBLES_EQUAL(dot(orthogonal(a), a), 0.f, 1e-6f);
  149. LOLUNIT_ASSERT_DOUBLES_EQUAL(dot(orthogonal(b), b), 0.f, 1e-6f);
  150. LOLUNIT_ASSERT_DOUBLES_EQUAL(dot(orthogonal(c), c), 0.f, 1e-6f);
  151. /* The length of the orthogonal vector should be at least
  152. * sqrt(2)/2 times the length of the original vector. */
  153. LOLUNIT_ASSERT_GREATER(length(orthogonal(a)), 0.7f);
  154. LOLUNIT_ASSERT_GREATER(length(orthogonal(b)), 0.7f);
  155. LOLUNIT_ASSERT_GREATER(length(orthogonal(c)), 0.7f);
  156. LOLUNIT_ASSERT_DOUBLES_EQUAL(dot(orthonormal(a), a), 0.f, 1e-6f);
  157. LOLUNIT_ASSERT_DOUBLES_EQUAL(dot(orthonormal(b), b), 0.f, 1e-6f);
  158. LOLUNIT_ASSERT_DOUBLES_EQUAL(dot(orthonormal(c), c), 0.f, 1e-6f);
  159. /* The length of the orthonormal vector should be 1. */
  160. LOLUNIT_ASSERT_DOUBLES_EQUAL(length(orthonormal(a)), 1.f, 1e-6f);
  161. LOLUNIT_ASSERT_DOUBLES_EQUAL(length(orthonormal(b)), 1.f, 1e-6f);
  162. LOLUNIT_ASSERT_DOUBLES_EQUAL(length(orthonormal(c)), 1.f, 1e-6f);
  163. }
  164. };
  165. } /* namespace lol */