core_func_matrix.cpp 9.2 KB

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