core_func_matrix.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. ///////////////////////////////////////////////////////////////////////////////////
  2. /// OpenGL Mathematics (glm.g-truc.net)
  3. ///
  4. /// Copyright (c) 2005 - 2015 G-Truc Creation (www.g-truc.net)
  5. /// Permission is hereby granted, free of charge, to any person obtaining a copy
  6. /// of this software and associated documentation files (the "Software"), to deal
  7. /// in the Software without restriction, including without limitation the rights
  8. /// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. /// copies of the Software, and to permit persons to whom the Software is
  10. /// furnished to do so, subject to the following conditions:
  11. ///
  12. /// The above copyright notice and this permission notice shall be included in
  13. /// all copies or substantial portions of the Software.
  14. ///
  15. /// Restrictions:
  16. /// By making use of the Software for military purposes, you choose to make
  17. /// a Bunny unhappy.
  18. ///
  19. /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. /// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. /// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. /// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. /// THE SOFTWARE.
  26. ///
  27. /// @file test/core/func_matrix.cpp
  28. /// @date 2007-01-25 / 2011-06-07
  29. /// @author Christophe Riccio
  30. ///////////////////////////////////////////////////////////////////////////////////
  31. #include <glm/matrix.hpp>
  32. #include <glm/gtc/matrix_transform.hpp>
  33. #include <glm/gtc/ulp.hpp>
  34. #include <glm/gtc/epsilon.hpp>
  35. #include <vector>
  36. #include <ctime>
  37. #include <cstdio>
  38. using namespace glm;
  39. int test_matrixCompMult()
  40. {
  41. int Error(0);
  42. {
  43. mat2 m(0, 1, 2, 3);
  44. mat2 n = matrixCompMult(m, m);
  45. Error += n == mat2(0, 1, 4, 9) ? 0 : 1;
  46. }
  47. {
  48. mat2x3 m(0, 1, 2, 3, 4, 5);
  49. mat2x3 n = matrixCompMult(m, m);
  50. Error += n == mat2x3(0, 1, 4, 9, 16, 25) ? 0 : 1;
  51. }
  52. {
  53. mat2x4 m(0, 1, 2, 3, 4, 5, 6, 7);
  54. mat2x4 n = matrixCompMult(m, m);
  55. Error += n == mat2x4(0, 1, 4, 9, 16, 25, 36, 49) ? 0 : 1;
  56. }
  57. {
  58. mat3 m(0, 1, 2, 3, 4, 5, 6, 7, 8);
  59. mat3 n = matrixCompMult(m, m);
  60. Error += n == mat3(0, 1, 4, 9, 16, 25, 36, 49, 64) ? 0 : 1;
  61. }
  62. {
  63. mat3x2 m(0, 1, 2, 3, 4, 5);
  64. mat3x2 n = matrixCompMult(m, m);
  65. Error += n == mat3x2(0, 1, 4, 9, 16, 25) ? 0 : 1;
  66. }
  67. {
  68. mat3x4 m(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
  69. mat3x4 n = matrixCompMult(m, m);
  70. Error += n == mat3x4(0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121) ? 0 : 1;
  71. }
  72. {
  73. mat4 m(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
  74. mat4 n = matrixCompMult(m, m);
  75. Error += n == mat4(0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225) ? 0 : 1;
  76. }
  77. {
  78. mat4x2 m(0, 1, 2, 3, 4, 5, 6, 7);
  79. mat4x2 n = matrixCompMult(m, m);
  80. Error += n == mat4x2(0, 1, 4, 9, 16, 25, 36, 49) ? 0 : 1;
  81. }
  82. {
  83. mat4x3 m(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
  84. mat4x3 n = matrixCompMult(m, m);
  85. Error += n == mat4x3(0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121) ? 0 : 1;
  86. }
  87. return Error;
  88. }
  89. int test_outerProduct()
  90. {
  91. { glm::mat2 m = glm::outerProduct(glm::vec2(1.0f), glm::vec2(1.0f)); }
  92. { glm::mat3 m = glm::outerProduct(glm::vec3(1.0f), glm::vec3(1.0f)); }
  93. { glm::mat4 m = glm::outerProduct(glm::vec4(1.0f), glm::vec4(1.0f)); }
  94. { glm::mat2x3 m = glm::outerProduct(glm::vec3(1.0f), glm::vec2(1.0f)); }
  95. { glm::mat2x4 m = glm::outerProduct(glm::vec4(1.0f), glm::vec2(1.0f)); }
  96. { glm::mat3x2 m = glm::outerProduct(glm::vec2(1.0f), glm::vec3(1.0f)); }
  97. { glm::mat3x4 m = glm::outerProduct(glm::vec4(1.0f), glm::vec3(1.0f)); }
  98. { glm::mat4x2 m = glm::outerProduct(glm::vec2(1.0f), glm::vec4(1.0f)); }
  99. { glm::mat4x3 m = glm::outerProduct(glm::vec3(1.0f), glm::vec4(1.0f)); }
  100. return 0;
  101. }
  102. int test_transpose()
  103. {
  104. int Error(0);
  105. {
  106. mat2 m(0, 1, 2, 3);
  107. mat2 t = transpose(m);
  108. Error += t == mat2(0, 2, 1, 3) ? 0 : 1;
  109. }
  110. {
  111. mat2x3 m(0, 1, 2, 3, 4, 5);
  112. mat3x2 t = transpose(m);
  113. Error += t == mat3x2(0, 3, 1, 4, 2, 5) ? 0 : 1;
  114. }
  115. {
  116. mat2x4 m(0, 1, 2, 3, 4, 5, 6, 7);
  117. mat4x2 t = transpose(m);
  118. Error += t == mat4x2(0, 4, 1, 5, 2, 6, 3, 7) ? 0 : 1;
  119. }
  120. {
  121. mat3 m(0, 1, 2, 3, 4, 5, 6, 7, 8);
  122. mat3 t = transpose(m);
  123. Error += t == mat3(0, 3, 6, 1, 4, 7, 2, 5, 8) ? 0 : 1;
  124. }
  125. {
  126. mat3x2 m(0, 1, 2, 3, 4, 5);
  127. mat2x3 t = transpose(m);
  128. Error += t == mat2x3(0, 2, 4, 1, 3, 5) ? 0 : 1;
  129. }
  130. {
  131. mat3x4 m(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
  132. mat4x3 t = transpose(m);
  133. Error += t == mat4x3(0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11) ? 0 : 1;
  134. }
  135. {
  136. mat4 m(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
  137. mat4 t = transpose(m);
  138. Error += t == mat4(0, 4, 8, 12, 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15) ? 0 : 1;
  139. }
  140. {
  141. mat4x2 m(0, 1, 2, 3, 4, 5, 6, 7);
  142. mat2x4 t = transpose(m);
  143. Error += t == mat2x4(0, 2, 4, 6, 1, 3, 5, 7) ? 0 : 1;
  144. }
  145. {
  146. mat4x3 m(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
  147. mat3x4 t = transpose(m);
  148. Error += t == mat3x4(0, 3, 6, 9, 1, 4, 7, 10, 2, 5, 8, 11) ? 0 : 1;
  149. }
  150. return Error;
  151. }
  152. int test_determinant()
  153. {
  154. return 0;
  155. }
  156. int test_inverse()
  157. {
  158. int Failed(0);
  159. glm::mat4x4 A4x4(
  160. glm::vec4(1, 0, 1, 0),
  161. glm::vec4(0, 1, 0, 0),
  162. glm::vec4(0, 0, 1, 0),
  163. glm::vec4(0, 0, 0, 1));
  164. glm::mat4x4 B4x4 = inverse(A4x4);
  165. glm::mat4x4 I4x4 = A4x4 * B4x4;
  166. Failed += I4x4 == glm::mat4x4(1) ? 0 : 1;
  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. Failed += I3x3 == glm::mat3x3(1) ? 0 : 1;
  174. glm::mat2x2 A2x2(
  175. glm::vec2(1, 1),
  176. glm::vec2(0, 1));
  177. glm::mat2x2 B2x2 = glm::inverse(A2x2);
  178. glm::mat2x2 I2x2 = A2x2 * B2x2;
  179. Failed += I2x2 == glm::mat2x2(1) ? 0 : 1;
  180. return Failed;
  181. }
  182. int test_inverse_simd()
  183. {
  184. int Error = 0;
  185. glm::mat4x4 const Identity(1);
  186. glm::mat4x4 const A4x4(
  187. glm::vec4(1, 0, 1, 0),
  188. glm::vec4(0, 1, 0, 0),
  189. glm::vec4(0, 0, 1, 0),
  190. glm::vec4(0, 0, 0, 1));
  191. glm::mat4x4 const B4x4 = glm::inverse(A4x4);
  192. glm::mat4x4 const I4x4 = A4x4 * B4x4;
  193. Error += glm::all(glm::epsilonEqual(I4x4[0], Identity[0], 0.001f)) ? 0 : 1;
  194. Error += glm::all(glm::epsilonEqual(I4x4[1], Identity[1], 0.001f)) ? 0 : 1;
  195. Error += glm::all(glm::epsilonEqual(I4x4[2], Identity[2], 0.001f)) ? 0 : 1;
  196. Error += glm::all(glm::epsilonEqual(I4x4[3], Identity[3], 0.001f)) ? 0 : 1;
  197. return Error;
  198. }
  199. template <typename VEC3, typename MAT4>
  200. int test_inverse_perf(std::size_t Count, std::size_t Instance, char const * Message)
  201. {
  202. std::vector<MAT4> TestInputs;
  203. TestInputs.resize(Count);
  204. std::vector<MAT4> TestOutputs;
  205. TestOutputs.resize(TestInputs.size());
  206. VEC3 Axis(glm::normalize(VEC3(1.0f, 2.0f, 3.0f)));
  207. for(std::size_t i = 0; i < TestInputs.size(); ++i)
  208. {
  209. 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);
  210. TestInputs[i] = glm::rotate(glm::translate(MAT4(1), Axis * f), f, Axis);
  211. //TestInputs[i] = glm::translate(MAT4(1), Axis * f);
  212. }
  213. std::clock_t StartTime = std::clock();
  214. for(std::size_t i = 0; i < TestInputs.size(); ++i)
  215. TestOutputs[i] = glm::inverse(TestInputs[i]);
  216. std::clock_t EndTime = std::clock();
  217. for(std::size_t i = 0; i < TestInputs.size(); ++i)
  218. TestOutputs[i] = TestOutputs[i] * TestInputs[i];
  219. typename MAT4::value_type Diff(0);
  220. for(std::size_t Entry = 0; Entry < TestOutputs.size(); ++Entry)
  221. {
  222. MAT4 i(1.0);
  223. MAT4 m(TestOutputs[Entry]);
  224. for(glm::length_t y = 0; y < m.length(); ++y)
  225. for(glm::length_t x = 0; x < m[y].length(); ++x)
  226. Diff = glm::max(m[y][x], i[y][x]);
  227. }
  228. //glm::uint Ulp = 0;
  229. //Ulp = glm::max(glm::float_distance(*Dst, *Src), Ulp);
  230. printf("inverse<%s>(%f): %lu\n", Message, Diff, EndTime - StartTime);
  231. return 0;
  232. }
  233. int main()
  234. {
  235. int Error(0);
  236. Error += test_matrixCompMult();
  237. Error += test_outerProduct();
  238. Error += test_transpose();
  239. Error += test_determinant();
  240. Error += test_inverse();
  241. Error += test_inverse_simd();
  242. # ifdef NDEBUG
  243. std::size_t const Samples(1000);
  244. for(std::size_t i = 0; i < 1; ++i)
  245. {
  246. Error += test_inverse_perf<glm::vec3, glm::mat4>(Samples, i, "mat4");
  247. Error += test_inverse_perf<glm::dvec3, glm::dmat4>(Samples, i, "dmat4");
  248. }
  249. # endif//NDEBUG
  250. return Error;
  251. }