core_func_matrix.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. #include <glm/ext/matrix_relational.hpp>
  2. #include <glm/ext/matrix_transform.hpp>
  3. #include <glm/ext/scalar_constants.hpp>
  4. #include <glm/mat2x2.hpp>
  5. #include <glm/mat2x3.hpp>
  6. #include <glm/mat2x4.hpp>
  7. #include <glm/mat3x2.hpp>
  8. #include <glm/mat3x3.hpp>
  9. #include <glm/mat3x4.hpp>
  10. #include <glm/mat4x2.hpp>
  11. #include <glm/mat4x3.hpp>
  12. #include <glm/mat4x4.hpp>
  13. #include <vector>
  14. #include <ctime>
  15. #include <cstdio>
  16. using namespace glm;
  17. int test_matrixCompMult()
  18. {
  19. int Error(0);
  20. {
  21. mat2 m(0, 1, 2, 3);
  22. mat2 n = matrixCompMult(m, m);
  23. mat2 expected = mat2(0, 1, 4, 9);
  24. Error += all(equal(n, expected, epsilon<float>())) ? 0 : 1;
  25. }
  26. {
  27. mat2x3 m(0, 1, 2, 3, 4, 5);
  28. mat2x3 n = matrixCompMult(m, m);
  29. mat2x3 expected = mat2x3(0, 1, 4, 9, 16, 25);
  30. Error += all(equal(n, expected, epsilon<float>())) ? 0 : 1;
  31. }
  32. {
  33. mat2x4 m(0, 1, 2, 3, 4, 5, 6, 7);
  34. mat2x4 n = matrixCompMult(m, m);
  35. mat2x4 expected = mat2x4(0, 1, 4, 9, 16, 25, 36, 49);
  36. Error += all(equal(n, expected, epsilon<float>())) ? 0 : 1;
  37. }
  38. {
  39. mat3 m(0, 1, 2, 3, 4, 5, 6, 7, 8);
  40. mat3 n = matrixCompMult(m, m);
  41. mat3 expected = mat3(0, 1, 4, 9, 16, 25, 36, 49, 64);
  42. Error += all(equal(n, expected, epsilon<float>())) ? 0 : 1;
  43. }
  44. {
  45. mat3x2 m(0, 1, 2, 3, 4, 5);
  46. mat3x2 n = matrixCompMult(m, m);
  47. mat3x2 expected = mat3x2(0, 1, 4, 9, 16, 25);
  48. Error += all(equal(n, expected, epsilon<float>())) ? 0 : 1;
  49. }
  50. {
  51. mat3x4 m(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
  52. mat3x4 n = matrixCompMult(m, m);
  53. mat3x4 expected = mat3x4(0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121);
  54. Error += all(equal(n, expected, epsilon<float>())) ? 0 : 1;
  55. }
  56. {
  57. mat4 m(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
  58. mat4 n = matrixCompMult(m, m);
  59. mat4 expected = mat4(0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225);
  60. Error += all(equal(n, expected, epsilon<float>())) ? 0 : 1;
  61. }
  62. {
  63. mat4x2 m(0, 1, 2, 3, 4, 5, 6, 7);
  64. mat4x2 n = matrixCompMult(m, m);
  65. mat4x2 expected = mat4x2(0, 1, 4, 9, 16, 25, 36, 49);
  66. Error += all(equal(n, expected, epsilon<float>())) ? 0 : 1;
  67. }
  68. {
  69. mat4x3 m(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
  70. mat4x3 n = matrixCompMult(m, m);
  71. mat4x3 expected = mat4x3(0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121);
  72. Error += all(equal(n, expected, epsilon<float>())) ? 0 : 1;
  73. }
  74. return Error;
  75. }
  76. int test_outerProduct()
  77. {
  78. { glm::mat2 m = glm::outerProduct(glm::vec2(1.0f), glm::vec2(1.0f)); }
  79. { glm::mat3 m = glm::outerProduct(glm::vec3(1.0f), glm::vec3(1.0f)); }
  80. { glm::mat4 m = glm::outerProduct(glm::vec4(1.0f), glm::vec4(1.0f)); }
  81. { glm::mat2x3 m = glm::outerProduct(glm::vec3(1.0f), glm::vec2(1.0f)); }
  82. { glm::mat2x4 m = glm::outerProduct(glm::vec4(1.0f), glm::vec2(1.0f)); }
  83. { glm::mat3x2 m = glm::outerProduct(glm::vec2(1.0f), glm::vec3(1.0f)); }
  84. { glm::mat3x4 m = glm::outerProduct(glm::vec4(1.0f), glm::vec3(1.0f)); }
  85. { glm::mat4x2 m = glm::outerProduct(glm::vec2(1.0f), glm::vec4(1.0f)); }
  86. { glm::mat4x3 m = glm::outerProduct(glm::vec3(1.0f), glm::vec4(1.0f)); }
  87. return 0;
  88. }
  89. int test_transpose()
  90. {
  91. int Error(0);
  92. {
  93. mat2 const m(0, 1, 2, 3);
  94. mat2 const t = transpose(m);
  95. mat2 const expected = mat2(0, 2, 1, 3);
  96. Error += all(equal(t, expected, epsilon<float>())) ? 0 : 1;
  97. }
  98. {
  99. mat2x3 m(0, 1, 2, 3, 4, 5);
  100. mat3x2 t = transpose(m);
  101. mat3x2 const expected = mat3x2(0, 3, 1, 4, 2, 5);
  102. Error += all(equal(t, expected, epsilon<float>())) ? 0 : 1;
  103. }
  104. {
  105. mat2x4 m(0, 1, 2, 3, 4, 5, 6, 7);
  106. mat4x2 t = transpose(m);
  107. mat4x2 const expected = mat4x2(0, 4, 1, 5, 2, 6, 3, 7);
  108. Error += all(equal(t, expected, epsilon<float>())) ? 0 : 1;
  109. }
  110. {
  111. mat3 m(0, 1, 2, 3, 4, 5, 6, 7, 8);
  112. mat3 t = transpose(m);
  113. mat3 const expected = mat3(0, 3, 6, 1, 4, 7, 2, 5, 8);
  114. Error += all(equal(t, expected, epsilon<float>())) ? 0 : 1;
  115. }
  116. {
  117. mat3x2 m(0, 1, 2, 3, 4, 5);
  118. mat2x3 t = transpose(m);
  119. mat2x3 const expected = mat2x3(0, 2, 4, 1, 3, 5);
  120. Error += all(equal(t, expected, epsilon<float>())) ? 0 : 1;
  121. }
  122. {
  123. mat3x4 m(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
  124. mat4x3 t = transpose(m);
  125. mat4x3 const expected = mat4x3(0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11);
  126. Error += all(equal(t, expected, epsilon<float>())) ? 0 : 1;
  127. }
  128. {
  129. mat4 m(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
  130. mat4 t = transpose(m);
  131. mat4 const expected = mat4(0, 4, 8, 12, 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15);
  132. Error += all(equal(t, expected, epsilon<float>())) ? 0 : 1;
  133. }
  134. {
  135. mat4x2 m(0, 1, 2, 3, 4, 5, 6, 7);
  136. mat2x4 t = transpose(m);
  137. mat2x4 const expected = mat2x4(0, 2, 4, 6, 1, 3, 5, 7);
  138. Error += all(equal(t, expected, epsilon<float>())) ? 0 : 1;
  139. }
  140. {
  141. mat4x3 m(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
  142. mat3x4 t = transpose(m);
  143. mat3x4 const expected = mat3x4(0, 3, 6, 9, 1, 4, 7, 10, 2, 5, 8, 11);
  144. Error += all(equal(t, expected, epsilon<float>())) ? 0 : 1;
  145. }
  146. return Error;
  147. }
  148. int test_determinant()
  149. {
  150. return 0;
  151. }
  152. int test_inverse()
  153. {
  154. int Error = 0;
  155. {
  156. glm::mat4x4 A4x4(
  157. glm::vec4(1, 0, 1, 0),
  158. glm::vec4(0, 1, 0, 0),
  159. glm::vec4(0, 0, 1, 0),
  160. glm::vec4(0, 0, 0, 1));
  161. glm::mat4x4 B4x4 = inverse(A4x4);
  162. glm::mat4x4 I4x4 = A4x4 * B4x4;
  163. glm::mat4x4 Identity(1);
  164. Error += all(equal(I4x4, Identity, epsilon<float>())) ? 0 : 1;
  165. }
  166. {
  167. glm::mat3x3 A3x3(
  168. glm::vec3(1, 0, 1),
  169. glm::vec3(0, 1, 0),
  170. glm::vec3(0, 0, 1));
  171. glm::mat3x3 B3x3 = glm::inverse(A3x3);
  172. glm::mat3x3 I3x3 = A3x3 * B3x3;
  173. glm::mat3x3 Identity(1);
  174. Error += all(equal(I3x3, Identity, epsilon<float>())) ? 0 : 1;
  175. }
  176. {
  177. glm::mat2x2 A2x2(
  178. glm::vec2(1, 1),
  179. glm::vec2(0, 1));
  180. glm::mat2x2 B2x2 = glm::inverse(A2x2);
  181. glm::mat2x2 I2x2 = A2x2 * B2x2;
  182. glm::mat2x2 Identity(1);
  183. Error += all(equal(I2x2, Identity, epsilon<float>())) ? 0 : 1;
  184. }
  185. return Error;
  186. }
  187. int test_inverse_simd()
  188. {
  189. int Error = 0;
  190. glm::mat4x4 const Identity(1);
  191. glm::mat4x4 const A4x4(
  192. glm::vec4(1, 0, 1, 0),
  193. glm::vec4(0, 1, 0, 0),
  194. glm::vec4(0, 0, 1, 0),
  195. glm::vec4(0, 0, 0, 1));
  196. glm::mat4x4 const B4x4 = glm::inverse(A4x4);
  197. glm::mat4x4 const I4x4 = A4x4 * B4x4;
  198. Error += glm::all(glm::equal(I4x4, Identity, 0.001f)) ? 0 : 1;
  199. return Error;
  200. }
  201. template<typename VEC3, typename MAT4>
  202. int test_inverse_perf(std::size_t Count, std::size_t Instance, char const * Message)
  203. {
  204. std::vector<MAT4> TestInputs;
  205. TestInputs.resize(Count);
  206. std::vector<MAT4> TestOutputs;
  207. TestOutputs.resize(TestInputs.size());
  208. VEC3 Axis(glm::normalize(VEC3(1.0f, 2.0f, 3.0f)));
  209. for(std::size_t i = 0; i < TestInputs.size(); ++i)
  210. {
  211. typename MAT4::value_type f = static_cast<typename MAT4::value_type>(i + Instance) * typename MAT4::value_type(0.1) + typename MAT4::value_type(0.1);
  212. TestInputs[i] = glm::rotate(glm::translate(MAT4(1), Axis * f), f, Axis);
  213. //TestInputs[i] = glm::translate(MAT4(1), Axis * f);
  214. }
  215. std::clock_t StartTime = std::clock();
  216. for(std::size_t i = 0; i < TestInputs.size(); ++i)
  217. TestOutputs[i] = glm::inverse(TestInputs[i]);
  218. std::clock_t EndTime = std::clock();
  219. for(std::size_t i = 0; i < TestInputs.size(); ++i)
  220. TestOutputs[i] = TestOutputs[i] * TestInputs[i];
  221. typename MAT4::value_type Diff(0);
  222. for(std::size_t Entry = 0; Entry < TestOutputs.size(); ++Entry)
  223. {
  224. MAT4 i(1.0);
  225. MAT4 m(TestOutputs[Entry]);
  226. for(glm::length_t y = 0; y < m.length(); ++y)
  227. for(glm::length_t x = 0; x < m[y].length(); ++x)
  228. Diff = glm::max(m[y][x], i[y][x]);
  229. }
  230. //glm::uint Ulp = 0;
  231. //Ulp = glm::max(glm::float_distance(*Dst, *Src), Ulp);
  232. std::printf("inverse<%s>(%f): %lu\n", Message, static_cast<double>(Diff), EndTime - StartTime);
  233. return 0;
  234. }
  235. int main()
  236. {
  237. int Error = 0;
  238. Error += test_matrixCompMult();
  239. Error += test_outerProduct();
  240. Error += test_transpose();
  241. Error += test_determinant();
  242. Error += test_inverse();
  243. Error += test_inverse_simd();
  244. # ifdef NDEBUG
  245. std::size_t const Samples = 1000;
  246. # else
  247. std::size_t const Samples = 1;
  248. # endif//NDEBUG
  249. for(std::size_t i = 0; i < 1; ++i)
  250. {
  251. Error += test_inverse_perf<glm::vec3, glm::mat4>(Samples, i, "mat4");
  252. Error += test_inverse_perf<glm::dvec3, glm::dmat4>(Samples, i, "dmat4");
  253. }
  254. return Error;
  255. }