Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

matrix.cpp 5.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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://sam.zoy.org/projects/COPYING.WTFPL for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include <cppunit/extensions/HelperMacros.h>
  14. #include <cppunit/TestCaller.h>
  15. #include <cppunit/TestCase.h>
  16. #include <cppunit/TestSuite.h>
  17. #include "core.h"
  18. namespace lol
  19. {
  20. class MatrixTest : public CppUnit::TestCase
  21. {
  22. CPPUNIT_TEST_SUITE(MatrixTest);
  23. CPPUNIT_TEST(test_vec_eq);
  24. CPPUNIT_TEST(test_vec_lt);
  25. CPPUNIT_TEST(test_vec_unary);
  26. CPPUNIT_TEST(test_vec_cast);
  27. CPPUNIT_TEST(test_mat_det);
  28. CPPUNIT_TEST(test_mat_mul);
  29. CPPUNIT_TEST(test_mat_inv);
  30. CPPUNIT_TEST_SUITE_END();
  31. public:
  32. MatrixTest() : CppUnit::TestCase("Matrix Test") {}
  33. void setUp()
  34. {
  35. identity = mat4(1.0f);
  36. triangular = mat4(vec4(1.0f, 0.0f, 0.0f, 0.0f),
  37. vec4(7.0f, 2.0f, 0.0f, 0.0f),
  38. vec4(1.0f, 5.0f, 3.0f, 0.0f),
  39. vec4(8.0f, 9.0f, 2.0f, 4.0f));
  40. invertible = mat4(vec4( 1.0f, 1.0f, 2.0f, -1.0f),
  41. vec4(-2.0f, -1.0f, -2.0f, 2.0f),
  42. vec4( 4.0f, 2.0f, 5.0f, -4.0f),
  43. vec4( 5.0f, -3.0f, -7.0f, -6.0f));
  44. }
  45. void tearDown() {}
  46. void test_vec_eq()
  47. {
  48. vec2 a2(1.0f, 2.0f);
  49. vec2 b2(0.0f, 2.0f);
  50. vec2 c2(1.0f, 0.0f);
  51. CPPUNIT_ASSERT(a2 == a2);
  52. CPPUNIT_ASSERT(!(a2 != a2));
  53. CPPUNIT_ASSERT(a2 != b2);
  54. CPPUNIT_ASSERT(!(a2 == b2));
  55. CPPUNIT_ASSERT(a2 != c2);
  56. CPPUNIT_ASSERT(!(a2 == c2));
  57. vec3 a3(1.0f, 2.0f, 3.0f);
  58. vec3 b3(0.0f, 2.0f, 3.0f);
  59. vec3 c3(1.0f, 0.0f, 3.0f);
  60. vec3 d3(1.0f, 2.0f, 0.0f);
  61. CPPUNIT_ASSERT(a3 == a3);
  62. CPPUNIT_ASSERT(!(a3 != a3));
  63. CPPUNIT_ASSERT(a3 != b3);
  64. CPPUNIT_ASSERT(!(a3 == b3));
  65. CPPUNIT_ASSERT(a3 != c3);
  66. CPPUNIT_ASSERT(!(a3 == c3));
  67. CPPUNIT_ASSERT(a3 != d3);
  68. CPPUNIT_ASSERT(!(a3 == d3));
  69. vec4 a4(1.0f, 2.0f, 3.0f, 4.0f);
  70. vec4 b4(0.0f, 2.0f, 3.0f, 4.0f);
  71. vec4 c4(1.0f, 0.0f, 3.0f, 4.0f);
  72. vec4 d4(1.0f, 2.0f, 0.0f, 4.0f);
  73. vec4 e4(1.0f, 2.0f, 3.0f, 0.0f);
  74. CPPUNIT_ASSERT(a4 == a4);
  75. CPPUNIT_ASSERT(!(a4 != a4));
  76. CPPUNIT_ASSERT(a4 != b4);
  77. CPPUNIT_ASSERT(!(a4 == b4));
  78. CPPUNIT_ASSERT(a4 != c4);
  79. CPPUNIT_ASSERT(!(a4 == c4));
  80. CPPUNIT_ASSERT(a4 != d4);
  81. CPPUNIT_ASSERT(!(a4 == d4));
  82. CPPUNIT_ASSERT(a4 != e4);
  83. CPPUNIT_ASSERT(!(a4 == e4));
  84. }
  85. void test_vec_lt()
  86. {
  87. vec2 a2(1.0f, 3.0f);
  88. vec2 b2(0.0f, 0.0f);
  89. vec2 c2(1.0f, 1.0f);
  90. vec2 d2(2.0f, 2.0f);
  91. vec2 e2(3.0f, 3.0f);
  92. vec2 f2(4.0f, 4.0f);
  93. CPPUNIT_ASSERT(a2 <= a2);
  94. CPPUNIT_ASSERT(!(a2 < a2));
  95. CPPUNIT_ASSERT(!(a2 <= b2));
  96. CPPUNIT_ASSERT(!(a2 < b2));
  97. CPPUNIT_ASSERT(!(a2 <= c2));
  98. CPPUNIT_ASSERT(!(a2 < c2));
  99. CPPUNIT_ASSERT(!(a2 <= d2));
  100. CPPUNIT_ASSERT(!(a2 < d2));
  101. CPPUNIT_ASSERT(a2 <= e2);
  102. CPPUNIT_ASSERT(!(a2 < e2));
  103. CPPUNIT_ASSERT(a2 <= f2);
  104. CPPUNIT_ASSERT(a2 < f2);
  105. }
  106. void test_vec_unary()
  107. {
  108. vec2 a(1.0f, 3.0f);
  109. vec2 b(-1.0f, -3.0f);
  110. CPPUNIT_ASSERT(a == -b);
  111. CPPUNIT_ASSERT(-a == b);
  112. }
  113. void test_vec_cast()
  114. {
  115. vec2 a1(1.0f, 3.0f);
  116. vec3 b(a1);
  117. vec2 a2(b);
  118. CPPUNIT_ASSERT(b.x == a1.x);
  119. CPPUNIT_ASSERT(b.y == a1.y);
  120. CPPUNIT_ASSERT(b.z == 0.0f);
  121. CPPUNIT_ASSERT(a2 == a1);
  122. vec4 c(a1);
  123. vec2 a3(c);
  124. CPPUNIT_ASSERT(c.x == a1.x);
  125. CPPUNIT_ASSERT(c.y == a1.y);
  126. CPPUNIT_ASSERT(c.z == 0.0f);
  127. CPPUNIT_ASSERT(c.w == 0.0f);
  128. CPPUNIT_ASSERT(a3 == a1);
  129. }
  130. void test_mat_det()
  131. {
  132. float d1 = triangular.det();
  133. CPPUNIT_ASSERT(d1 == 24.0f);
  134. float d2 = invertible.det();
  135. CPPUNIT_ASSERT(d2 == -1.0f);
  136. }
  137. void test_mat_mul()
  138. {
  139. mat4 m0 = identity;
  140. mat4 m1 = identity;
  141. mat4 m2 = m0 * m1;
  142. CPPUNIT_ASSERT(m2[0][0] == 1.0f);
  143. CPPUNIT_ASSERT(m2[1][0] == 0.0f);
  144. CPPUNIT_ASSERT(m2[2][0] == 0.0f);
  145. CPPUNIT_ASSERT(m2[3][0] == 0.0f);
  146. CPPUNIT_ASSERT(m2[0][1] == 0.0f);
  147. CPPUNIT_ASSERT(m2[1][1] == 1.0f);
  148. CPPUNIT_ASSERT(m2[2][1] == 0.0f);
  149. CPPUNIT_ASSERT(m2[3][1] == 0.0f);
  150. CPPUNIT_ASSERT(m2[0][2] == 0.0f);
  151. CPPUNIT_ASSERT(m2[1][2] == 0.0f);
  152. CPPUNIT_ASSERT(m2[2][2] == 1.0f);
  153. CPPUNIT_ASSERT(m2[3][2] == 0.0f);
  154. CPPUNIT_ASSERT(m2[0][3] == 0.0f);
  155. CPPUNIT_ASSERT(m2[1][3] == 0.0f);
  156. CPPUNIT_ASSERT(m2[2][3] == 0.0f);
  157. CPPUNIT_ASSERT(m2[3][3] == 1.0f);
  158. }
  159. void test_mat_inv()
  160. {
  161. mat4 m0 = invertible;
  162. mat4 m1 = m0.invert();
  163. mat4 m2 = m0 * m1;
  164. CPPUNIT_ASSERT(m2[0][0] == 1.0f);
  165. CPPUNIT_ASSERT(m2[1][0] == 0.0f);
  166. CPPUNIT_ASSERT(m2[2][0] == 0.0f);
  167. CPPUNIT_ASSERT(m2[3][0] == 0.0f);
  168. CPPUNIT_ASSERT(m2[0][1] == 0.0f);
  169. CPPUNIT_ASSERT(m2[1][1] == 1.0f);
  170. CPPUNIT_ASSERT(m2[2][1] == 0.0f);
  171. CPPUNIT_ASSERT(m2[3][1] == 0.0f);
  172. CPPUNIT_ASSERT(m2[0][2] == 0.0f);
  173. CPPUNIT_ASSERT(m2[1][2] == 0.0f);
  174. CPPUNIT_ASSERT(m2[2][2] == 1.0f);
  175. CPPUNIT_ASSERT(m2[3][2] == 0.0f);
  176. CPPUNIT_ASSERT(m2[0][3] == 0.0f);
  177. CPPUNIT_ASSERT(m2[1][3] == 0.0f);
  178. CPPUNIT_ASSERT(m2[2][3] == 0.0f);
  179. CPPUNIT_ASSERT(m2[3][3] == 1.0f);
  180. }
  181. private:
  182. mat4 triangular, identity, invertible;
  183. };
  184. CPPUNIT_TEST_SUITE_REGISTRATION(MatrixTest);
  185. } /* namespace lol */