25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

matrix.cpp 4.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2012 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 "core.h"
  14. #include "lol/unit.h"
  15. namespace lol
  16. {
  17. LOLUNIT_FIXTURE(MatrixTest)
  18. {
  19. void SetUp()
  20. {
  21. id2 = mat2(1.0f);
  22. tri2 = mat2(vec2(1.0f, 0.0f),
  23. vec2(7.0f, 2.0f));
  24. inv2 = mat2(vec2(4.0f, 3.0f),
  25. vec2(3.0f, 2.0f));
  26. id3 = mat3(1.0f);
  27. tri3 = mat3(vec3(1.0f, 0.0f, 0.0f),
  28. vec3(7.0f, 2.0f, 0.0f),
  29. vec3(1.0f, 5.0f, 3.0f));
  30. inv3 = mat3(vec3(2.0f, 3.0f, 5.0f),
  31. vec3(3.0f, 2.0f, 3.0f),
  32. vec3(9.0f, 5.0f, 7.0f));
  33. id4 = mat4(1.0f);
  34. tri4 = mat4(vec4(1.0f, 0.0f, 0.0f, 0.0f),
  35. vec4(7.0f, 2.0f, 0.0f, 0.0f),
  36. vec4(1.0f, 5.0f, 3.0f, 0.0f),
  37. vec4(8.0f, 9.0f, 2.0f, 4.0f));
  38. inv4 = mat4(vec4( 1.0f, 1.0f, 2.0f, -1.0f),
  39. vec4(-2.0f, -1.0f, -2.0f, 2.0f),
  40. vec4( 4.0f, 2.0f, 5.0f, -4.0f),
  41. vec4( 5.0f, -3.0f, -7.0f, -6.0f));
  42. }
  43. void TearDown() {}
  44. LOLUNIT_TEST(Determinant)
  45. {
  46. float d1, d2;
  47. d1 = determinant(tri2);
  48. LOLUNIT_ASSERT_EQUAL(d1, 2.0f);
  49. d2 = determinant(inv2);
  50. LOLUNIT_ASSERT_EQUAL(d2, -1.0f);
  51. d1 = determinant(tri3);
  52. LOLUNIT_ASSERT_EQUAL(d1, 6.0f);
  53. d2 = determinant(inv3);
  54. LOLUNIT_ASSERT_EQUAL(d2, 1.0f);
  55. d1 = determinant(tri4);
  56. LOLUNIT_ASSERT_EQUAL(d1, 24.0f);
  57. d2 = determinant(inv4);
  58. LOLUNIT_ASSERT_EQUAL(d2, -1.0f);
  59. }
  60. LOLUNIT_TEST(Multiplication)
  61. {
  62. mat4 m0 = id4;
  63. mat4 m1 = id4;
  64. mat4 m2 = m0 * m1;
  65. LOLUNIT_ASSERT_EQUAL(m2[0][0], 1.0f);
  66. LOLUNIT_ASSERT_EQUAL(m2[1][0], 0.0f);
  67. LOLUNIT_ASSERT_EQUAL(m2[2][0], 0.0f);
  68. LOLUNIT_ASSERT_EQUAL(m2[3][0], 0.0f);
  69. LOLUNIT_ASSERT_EQUAL(m2[0][1], 0.0f);
  70. LOLUNIT_ASSERT_EQUAL(m2[1][1], 1.0f);
  71. LOLUNIT_ASSERT_EQUAL(m2[2][1], 0.0f);
  72. LOLUNIT_ASSERT_EQUAL(m2[3][1], 0.0f);
  73. LOLUNIT_ASSERT_EQUAL(m2[0][2], 0.0f);
  74. LOLUNIT_ASSERT_EQUAL(m2[1][2], 0.0f);
  75. LOLUNIT_ASSERT_EQUAL(m2[2][2], 1.0f);
  76. LOLUNIT_ASSERT_EQUAL(m2[3][2], 0.0f);
  77. LOLUNIT_ASSERT_EQUAL(m2[0][3], 0.0f);
  78. LOLUNIT_ASSERT_EQUAL(m2[1][3], 0.0f);
  79. LOLUNIT_ASSERT_EQUAL(m2[2][3], 0.0f);
  80. LOLUNIT_ASSERT_EQUAL(m2[3][3], 1.0f);
  81. }
  82. LOLUNIT_TEST(Inverse2x2)
  83. {
  84. mat2 m0 = inv2;
  85. mat2 m1 = inverse(m0);
  86. mat2 m2 = m0 * m1;
  87. LOLUNIT_ASSERT_EQUAL(m2[0][0], 1.0f);
  88. LOLUNIT_ASSERT_EQUAL(m2[1][0], 0.0f);
  89. LOLUNIT_ASSERT_EQUAL(m2[0][1], 0.0f);
  90. LOLUNIT_ASSERT_EQUAL(m2[1][1], 1.0f);
  91. }
  92. LOLUNIT_TEST(Inverse3x3)
  93. {
  94. mat3 m0 = inv3;
  95. mat3 m1 = inverse(m0);
  96. mat3 m2 = m0 * m1;
  97. LOLUNIT_ASSERT_EQUAL(m2[0][0], 1.0f);
  98. LOLUNIT_ASSERT_EQUAL(m2[1][0], 0.0f);
  99. LOLUNIT_ASSERT_EQUAL(m2[2][0], 0.0f);
  100. LOLUNIT_ASSERT_EQUAL(m2[0][1], 0.0f);
  101. LOLUNIT_ASSERT_EQUAL(m2[1][1], 1.0f);
  102. LOLUNIT_ASSERT_EQUAL(m2[2][1], 0.0f);
  103. LOLUNIT_ASSERT_EQUAL(m2[0][2], 0.0f);
  104. LOLUNIT_ASSERT_EQUAL(m2[1][2], 0.0f);
  105. LOLUNIT_ASSERT_EQUAL(m2[2][2], 1.0f);
  106. }
  107. LOLUNIT_TEST(Inverse4x4)
  108. {
  109. mat4 m0 = inv4;
  110. mat4 m1 = inverse(m0);
  111. mat4 m2 = m0 * m1;
  112. LOLUNIT_ASSERT_EQUAL(m2[0][0], 1.0f);
  113. LOLUNIT_ASSERT_EQUAL(m2[1][0], 0.0f);
  114. LOLUNIT_ASSERT_EQUAL(m2[2][0], 0.0f);
  115. LOLUNIT_ASSERT_EQUAL(m2[3][0], 0.0f);
  116. LOLUNIT_ASSERT_EQUAL(m2[0][1], 0.0f);
  117. LOLUNIT_ASSERT_EQUAL(m2[1][1], 1.0f);
  118. LOLUNIT_ASSERT_EQUAL(m2[2][1], 0.0f);
  119. LOLUNIT_ASSERT_EQUAL(m2[3][1], 0.0f);
  120. LOLUNIT_ASSERT_EQUAL(m2[0][2], 0.0f);
  121. LOLUNIT_ASSERT_EQUAL(m2[1][2], 0.0f);
  122. LOLUNIT_ASSERT_EQUAL(m2[2][2], 1.0f);
  123. LOLUNIT_ASSERT_EQUAL(m2[3][2], 0.0f);
  124. LOLUNIT_ASSERT_EQUAL(m2[0][3], 0.0f);
  125. LOLUNIT_ASSERT_EQUAL(m2[1][3], 0.0f);
  126. LOLUNIT_ASSERT_EQUAL(m2[2][3], 0.0f);
  127. LOLUNIT_ASSERT_EQUAL(m2[3][3], 1.0f);
  128. }
  129. mat2 tri2, id2, inv2;
  130. mat3 tri3, id3, inv3;
  131. mat4 tri4, id4, inv4;
  132. };
  133. } /* namespace lol */