core_func_matrix.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. ///////////////////////////////////////////////////////////////////////////////////
  2. /// OpenGL Mathematics (glm.g-truc.net)
  3. ///
  4. /// Copyright (c) 2005 - 2014 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 <vector>
  35. #include <ctime>
  36. #include <cstdio>
  37. using namespace glm;
  38. int test_matrixCompMult()
  39. {
  40. int Error(0);
  41. {
  42. mat2 m(0, 1, 2, 3);
  43. mat2 n = matrixCompMult(m, m);
  44. Error += n == mat2(0, 1, 4, 9) ? 0 : 1;
  45. }
  46. {
  47. mat2x3 m(0, 1, 2, 3, 4, 5);
  48. mat2x3 n = matrixCompMult(m, m);
  49. Error += n == mat2x3(0, 1, 4, 9, 16, 25) ? 0 : 1;
  50. }
  51. {
  52. mat2x4 m(0, 1, 2, 3, 4, 5, 6, 7);
  53. mat2x4 n = matrixCompMult(m, m);
  54. Error += n == mat2x4(0, 1, 4, 9, 16, 25, 36, 49) ? 0 : 1;
  55. }
  56. {
  57. mat3 m(0, 1, 2, 3, 4, 5, 6, 7, 8);
  58. mat3 n = matrixCompMult(m, m);
  59. Error += n == mat3(0, 1, 4, 9, 16, 25, 36, 49, 64) ? 0 : 1;
  60. }
  61. {
  62. mat3x2 m(0, 1, 2, 3, 4, 5);
  63. mat3x2 n = matrixCompMult(m, m);
  64. Error += n == mat3x2(0, 1, 4, 9, 16, 25) ? 0 : 1;
  65. }
  66. {
  67. mat3x4 m(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
  68. mat3x4 n = matrixCompMult(m, m);
  69. Error += n == mat3x4(0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121) ? 0 : 1;
  70. }
  71. {
  72. mat4 m(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
  73. mat4 n = matrixCompMult(m, m);
  74. Error += n == mat4(0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225) ? 0 : 1;
  75. }
  76. {
  77. mat4x2 m(0, 1, 2, 3, 4, 5, 6, 7);
  78. mat4x2 n = matrixCompMult(m, m);
  79. Error += n == mat4x2(0, 1, 4, 9, 16, 25, 36, 49) ? 0 : 1;
  80. }
  81. {
  82. mat4x3 m(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
  83. mat4x3 n = matrixCompMult(m, m);
  84. Error += n == mat4x3(0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121) ? 0 : 1;
  85. }
  86. return Error;
  87. }
  88. int test_outerProduct()
  89. {
  90. glm::mat4 m = glm::outerProduct(glm::vec4(1.0f), glm::vec4(1.0f));
  91. return 0;
  92. }
  93. int test_transpose()
  94. {
  95. int Error(0);
  96. {
  97. mat2 m(0, 1, 2, 3);
  98. mat2 t = transpose(m);
  99. Error += t == mat2(0, 2, 1, 3) ? 0 : 1;
  100. }
  101. {
  102. mat2x3 m(0, 1, 2, 3, 4, 5);
  103. mat3x2 t = transpose(m);
  104. Error += t == mat3x2(0, 3, 1, 4, 2, 5) ? 0 : 1;
  105. }
  106. {
  107. mat2x4 m(0, 1, 2, 3, 4, 5, 6, 7);
  108. mat4x2 t = transpose(m);
  109. Error += t == mat4x2(0, 4, 1, 5, 2, 6, 3, 7) ? 0 : 1;
  110. }
  111. {
  112. mat3 m(0, 1, 2, 3, 4, 5, 6, 7, 8);
  113. mat3 t = transpose(m);
  114. Error += t == mat3(0, 3, 6, 1, 4, 7, 2, 5, 8) ? 0 : 1;
  115. }
  116. {
  117. mat3x2 m(0, 1, 2, 3, 4, 5);
  118. mat2x3 t = transpose(m);
  119. Error += t == mat2x3(0, 2, 4, 1, 3, 5) ? 0 : 1;
  120. }
  121. {
  122. mat3x4 m(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
  123. mat4x3 t = transpose(m);
  124. Error += t == mat4x3(0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11) ? 0 : 1;
  125. }
  126. {
  127. mat4 m(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
  128. mat4 t = transpose(m);
  129. Error += t == mat4(0, 4, 8, 12, 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15) ? 0 : 1;
  130. }
  131. {
  132. mat4x2 m(0, 1, 2, 3, 4, 5, 6, 7);
  133. mat2x4 t = transpose(m);
  134. Error += t == mat2x4(0, 2, 4, 6, 1, 3, 5, 7) ? 0 : 1;
  135. }
  136. {
  137. mat4x3 m(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
  138. mat3x4 t = transpose(m);
  139. Error += t == mat3x4(0, 3, 6, 9, 1, 4, 7, 10, 2, 5, 8, 11) ? 0 : 1;
  140. }
  141. return Error;
  142. }
  143. int test_determinant()
  144. {
  145. return 0;
  146. }
  147. int test_inverse()
  148. {
  149. int Failed(0);
  150. glm::mat4x4 A4x4(
  151. glm::vec4(1, 0, 1, 0),
  152. glm::vec4(0, 1, 0, 0),
  153. glm::vec4(0, 0, 1, 0),
  154. glm::vec4(0, 0, 0, 1));
  155. glm::mat4x4 B4x4 = inverse(A4x4);
  156. glm::mat4x4 I4x4 = A4x4 * B4x4;
  157. Failed += I4x4 == glm::mat4x4(1) ? 0 : 1;
  158. glm::mat3x3 A3x3(
  159. glm::vec3(1, 0, 1),
  160. glm::vec3(0, 1, 0),
  161. glm::vec3(0, 0, 1));
  162. glm::mat3x3 B3x3 = glm::inverse(A3x3);
  163. glm::mat3x3 I3x3 = A3x3 * B3x3;
  164. Failed += I3x3 == glm::mat3x3(1) ? 0 : 1;
  165. glm::mat2x2 A2x2(
  166. glm::vec2(1, 1),
  167. glm::vec2(0, 1));
  168. glm::mat2x2 B2x2 = glm::inverse(A2x2);
  169. glm::mat2x2 I2x2 = A2x2 * B2x2;
  170. Failed += I2x2 == glm::mat2x2(1) ? 0 : 1;
  171. return Failed;
  172. }
  173. std::size_t const Count(10000000);
  174. template <typename VEC3, typename MAT4>
  175. int test_inverse_perf(std::size_t Instance, char const * Message)
  176. {
  177. std::vector<MAT4> TestInputs;
  178. TestInputs.resize(Count);
  179. std::vector<MAT4> TestOutputs;
  180. TestOutputs.resize(TestInputs.size());
  181. VEC3 Axis(glm::normalize(VEC3(1.0f, 2.0f, 3.0f)));
  182. for(std::size_t i = 0; i < TestInputs.size(); ++i)
  183. {
  184. 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);
  185. TestInputs[i] = glm::rotate(glm::translate(MAT4(1), Axis * f), f, Axis);
  186. //TestInputs[i] = glm::translate(MAT4(1), Axis * f);
  187. }
  188. std::clock_t StartTime = std::clock();
  189. for(std::size_t i = 0; i < TestInputs.size(); ++i)
  190. TestOutputs[i] = glm::inverse(TestInputs[i]);
  191. std::clock_t EndTime = std::clock();
  192. for(std::size_t i = 0; i < TestInputs.size(); ++i)
  193. TestOutputs[i] = TestOutputs[i] * TestInputs[i];
  194. typename MAT4::value_type Diff(0);
  195. for(std::size_t Entry = 0; Entry < TestOutputs.size(); ++Entry)
  196. {
  197. MAT4 i(1.0);
  198. MAT4 m(TestOutputs[Entry]);
  199. for(glm::length_t y = 0; y < m.length(); ++y)
  200. for(glm::length_t x = 0; x < m[y].length(); ++x)
  201. Diff = glm::max(m[y][x], i[y][x]);
  202. }
  203. //glm::uint Ulp = 0;
  204. //Ulp = glm::max(glm::float_distance(*Dst, *Src), Ulp);
  205. printf("inverse<%s>(%f): %lu\n", Message, Diff, EndTime - StartTime);
  206. return 0;
  207. }
  208. int main()
  209. {
  210. int Error(0);
  211. Error += test_matrixCompMult();
  212. Error += test_outerProduct();
  213. Error += test_transpose();
  214. Error += test_determinant();
  215. Error += test_inverse();
  216. # ifdef NDEBUG
  217. for(std::size_t i = 0; i < 1; ++i)
  218. {
  219. Error += test_inverse_perf<glm::vec3, glm::mat4>(i, "mat4");
  220. Error += test_inverse_perf<glm::dvec3, glm::dmat4>(i, "dmat4");
  221. }
  222. # endif//NDEBUG
  223. return Error;
  224. }