core_func_matrix.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 <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::mat2 m = glm::outerProduct(glm::vec2(1.0f), glm::vec2(1.0f)); }
  91. { glm::mat3 m = glm::outerProduct(glm::vec3(1.0f), glm::vec3(1.0f)); }
  92. { glm::mat4 m = glm::outerProduct(glm::vec4(1.0f), glm::vec4(1.0f)); }
  93. { glm::mat2x3 m = glm::outerProduct(glm::vec3(1.0f), glm::vec2(1.0f)); }
  94. { glm::mat2x4 m = glm::outerProduct(glm::vec4(1.0f), glm::vec2(1.0f)); }
  95. { glm::mat3x2 m = glm::outerProduct(glm::vec2(1.0f), glm::vec3(1.0f)); }
  96. { glm::mat3x4 m = glm::outerProduct(glm::vec4(1.0f), glm::vec3(1.0f)); }
  97. { glm::mat4x2 m = glm::outerProduct(glm::vec2(1.0f), glm::vec4(1.0f)); }
  98. { glm::mat4x3 m = glm::outerProduct(glm::vec3(1.0f), glm::vec4(1.0f)); }
  99. return 0;
  100. }
  101. int test_transpose()
  102. {
  103. int Error(0);
  104. {
  105. mat2 m(0, 1, 2, 3);
  106. mat2 t = transpose(m);
  107. Error += t == mat2(0, 2, 1, 3) ? 0 : 1;
  108. }
  109. {
  110. mat2x3 m(0, 1, 2, 3, 4, 5);
  111. mat3x2 t = transpose(m);
  112. Error += t == mat3x2(0, 3, 1, 4, 2, 5) ? 0 : 1;
  113. }
  114. {
  115. mat2x4 m(0, 1, 2, 3, 4, 5, 6, 7);
  116. mat4x2 t = transpose(m);
  117. Error += t == mat4x2(0, 4, 1, 5, 2, 6, 3, 7) ? 0 : 1;
  118. }
  119. {
  120. mat3 m(0, 1, 2, 3, 4, 5, 6, 7, 8);
  121. mat3 t = transpose(m);
  122. Error += t == mat3(0, 3, 6, 1, 4, 7, 2, 5, 8) ? 0 : 1;
  123. }
  124. {
  125. mat3x2 m(0, 1, 2, 3, 4, 5);
  126. mat2x3 t = transpose(m);
  127. Error += t == mat2x3(0, 2, 4, 1, 3, 5) ? 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. Error += t == mat4x3(0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11) ? 0 : 1;
  133. }
  134. {
  135. mat4 m(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
  136. mat4 t = transpose(m);
  137. Error += t == mat4(0, 4, 8, 12, 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15) ? 0 : 1;
  138. }
  139. {
  140. mat4x2 m(0, 1, 2, 3, 4, 5, 6, 7);
  141. mat2x4 t = transpose(m);
  142. Error += t == mat2x4(0, 2, 4, 6, 1, 3, 5, 7) ? 0 : 1;
  143. }
  144. {
  145. mat4x3 m(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
  146. mat3x4 t = transpose(m);
  147. Error += t == mat3x4(0, 3, 6, 9, 1, 4, 7, 10, 2, 5, 8, 11) ? 0 : 1;
  148. }
  149. return Error;
  150. }
  151. int test_determinant()
  152. {
  153. return 0;
  154. }
  155. int test_inverse()
  156. {
  157. int Failed(0);
  158. glm::mat4x4 A4x4(
  159. glm::vec4(1, 0, 1, 0),
  160. glm::vec4(0, 1, 0, 0),
  161. glm::vec4(0, 0, 1, 0),
  162. glm::vec4(0, 0, 0, 1));
  163. glm::mat4x4 B4x4 = inverse(A4x4);
  164. glm::mat4x4 I4x4 = A4x4 * B4x4;
  165. Failed += I4x4 == glm::mat4x4(1) ? 0 : 1;
  166. glm::mat3x3 A3x3(
  167. glm::vec3(1, 0, 1),
  168. glm::vec3(0, 1, 0),
  169. glm::vec3(0, 0, 1));
  170. glm::mat3x3 B3x3 = glm::inverse(A3x3);
  171. glm::mat3x3 I3x3 = A3x3 * B3x3;
  172. Failed += I3x3 == glm::mat3x3(1) ? 0 : 1;
  173. glm::mat2x2 A2x2(
  174. glm::vec2(1, 1),
  175. glm::vec2(0, 1));
  176. glm::mat2x2 B2x2 = glm::inverse(A2x2);
  177. glm::mat2x2 I2x2 = A2x2 * B2x2;
  178. Failed += I2x2 == glm::mat2x2(1) ? 0 : 1;
  179. return Failed;
  180. }
  181. template <typename VEC3, typename MAT4>
  182. int test_inverse_perf(std::size_t Count, std::size_t Instance, char const * Message)
  183. {
  184. std::vector<MAT4> TestInputs;
  185. TestInputs.resize(Count);
  186. std::vector<MAT4> TestOutputs;
  187. TestOutputs.resize(TestInputs.size());
  188. VEC3 Axis(glm::normalize(VEC3(1.0f, 2.0f, 3.0f)));
  189. for(std::size_t i = 0; i < TestInputs.size(); ++i)
  190. {
  191. 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);
  192. TestInputs[i] = glm::rotate(glm::translate(MAT4(1), Axis * f), f, Axis);
  193. //TestInputs[i] = glm::translate(MAT4(1), Axis * f);
  194. }
  195. std::clock_t StartTime = std::clock();
  196. for(std::size_t i = 0; i < TestInputs.size(); ++i)
  197. TestOutputs[i] = glm::inverse(TestInputs[i]);
  198. std::clock_t EndTime = std::clock();
  199. for(std::size_t i = 0; i < TestInputs.size(); ++i)
  200. TestOutputs[i] = TestOutputs[i] * TestInputs[i];
  201. typename MAT4::value_type Diff(0);
  202. for(std::size_t Entry = 0; Entry < TestOutputs.size(); ++Entry)
  203. {
  204. MAT4 i(1.0);
  205. MAT4 m(TestOutputs[Entry]);
  206. for(glm::length_t y = 0; y < m.length(); ++y)
  207. for(glm::length_t x = 0; x < m[y].length(); ++x)
  208. Diff = glm::max(m[y][x], i[y][x]);
  209. }
  210. //glm::uint Ulp = 0;
  211. //Ulp = glm::max(glm::float_distance(*Dst, *Src), Ulp);
  212. printf("inverse<%s>(%f): %lu\n", Message, Diff, EndTime - StartTime);
  213. return 0;
  214. }
  215. int main()
  216. {
  217. int Error(0);
  218. Error += test_matrixCompMult();
  219. Error += test_outerProduct();
  220. Error += test_transpose();
  221. Error += test_determinant();
  222. Error += test_inverse();
  223. # ifdef NDEBUG
  224. std::size_t const Samples(1000);
  225. for(std::size_t i = 0; i < 1; ++i)
  226. {
  227. Error += test_inverse_perf<glm::vec3, glm::mat4>(Samples, i, "mat4");
  228. Error += test_inverse_perf<glm::dvec3, glm::dmat4>(Samples, i, "dmat4");
  229. }
  230. # endif//NDEBUG
  231. return Error;
  232. }