core_func_matrix.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////
  2. // OpenGL Mathematics Copyright (c) 2005 - 2013 G-Truc Creation (www.g-truc.net)
  3. ///////////////////////////////////////////////////////////////////////////////////////////////////
  4. // Created : 2011-01-15
  5. // Updated : 2012-05-02
  6. // Licence : This source is under MIT licence
  7. // File : test/core/func_matrix.cpp
  8. ///////////////////////////////////////////////////////////////////////////////////////////////////
  9. #include <glm/matrix.hpp>
  10. using namespace glm;
  11. int test_matrixCompMult()
  12. {
  13. int Error(0);
  14. {
  15. mat2 m(0, 1, 2, 3);
  16. mat2 n = matrixCompMult(m, m);
  17. Error += n == mat2(0, 1, 4, 9) ? 0 : 1;
  18. }
  19. {
  20. mat2x3 m(0, 1, 2, 3, 4, 5);
  21. mat2x3 n = matrixCompMult(m, m);
  22. Error += n == mat2x3(0, 1, 4, 9, 16, 25) ? 0 : 1;
  23. }
  24. {
  25. mat2x4 m(0, 1, 2, 3, 4, 5, 6, 7);
  26. mat2x4 n = matrixCompMult(m, m);
  27. Error += n == mat2x4(0, 1, 4, 9, 16, 25, 36, 49) ? 0 : 1;
  28. }
  29. {
  30. mat3 m(0, 1, 2, 3, 4, 5, 6, 7, 8);
  31. mat3 n = matrixCompMult(m, m);
  32. Error += n == mat3(0, 1, 4, 9, 16, 25, 36, 49, 64) ? 0 : 1;
  33. }
  34. {
  35. mat3x2 m(0, 1, 2, 3, 4, 5);
  36. mat3x2 n = matrixCompMult(m, m);
  37. Error += n == mat3x2(0, 1, 4, 9, 16, 25) ? 0 : 1;
  38. }
  39. {
  40. mat3x4 m(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
  41. mat3x4 n = matrixCompMult(m, m);
  42. Error += n == mat3x4(0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121) ? 0 : 1;
  43. }
  44. {
  45. mat4 m(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
  46. mat4 n = matrixCompMult(m, m);
  47. Error += n == mat4(0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225) ? 0 : 1;
  48. }
  49. {
  50. mat4x2 m(0, 1, 2, 3, 4, 5, 6, 7);
  51. mat4x2 n = matrixCompMult(m, m);
  52. Error += n == mat4x2(0, 1, 4, 9, 16, 25, 36, 49) ? 0 : 1;
  53. }
  54. {
  55. mat4x3 m(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
  56. mat4x3 n = matrixCompMult(m, m);
  57. Error += n == mat4x3(0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121) ? 0 : 1;
  58. }
  59. return Error;
  60. }
  61. int test_outerProduct()
  62. {
  63. return 0;
  64. }
  65. int test_transpose()
  66. {
  67. int Error(0);
  68. {
  69. mat2 m(0, 1, 2, 3);
  70. mat2 t = transpose(m);
  71. Error += t == mat2(0, 2, 1, 3) ? 0 : 1;
  72. }
  73. {
  74. mat2x3 m(0, 1, 2, 3, 4, 5);
  75. mat3x2 t = transpose(m);
  76. Error += t == mat3x2(0, 3, 1, 4, 2, 5) ? 0 : 1;
  77. }
  78. {
  79. mat2x4 m(0, 1, 2, 3, 4, 5, 6, 7);
  80. mat4x2 t = transpose(m);
  81. Error += t == mat4x2(0, 4, 1, 5, 2, 6, 3, 7) ? 0 : 1;
  82. }
  83. {
  84. mat3 m(0, 1, 2, 3, 4, 5, 6, 7, 8);
  85. mat3 t = transpose(m);
  86. Error += t == mat3(0, 3, 6, 1, 4, 7, 2, 5, 8) ? 0 : 1;
  87. }
  88. {
  89. mat3x2 m(0, 1, 2, 3, 4, 5);
  90. mat2x3 t = transpose(m);
  91. Error += t == mat2x3(0, 2, 4, 1, 3, 5) ? 0 : 1;
  92. }
  93. {
  94. mat3x4 m(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
  95. mat4x3 t = transpose(m);
  96. Error += t == mat4x3(0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11) ? 0 : 1;
  97. }
  98. {
  99. mat4 m(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
  100. mat4 t = transpose(m);
  101. Error += t == mat4(0, 4, 8, 12, 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15) ? 0 : 1;
  102. }
  103. {
  104. mat4x2 m(0, 1, 2, 3, 4, 5, 6, 7);
  105. mat2x4 t = transpose(m);
  106. Error += t == mat2x4(0, 2, 4, 6, 1, 3, 5, 7) ? 0 : 1;
  107. }
  108. {
  109. mat4x3 m(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
  110. mat3x4 t = transpose(m);
  111. Error += t == mat3x4(0, 3, 6, 9, 1, 4, 7, 10, 2, 5, 8, 11) ? 0 : 1;
  112. }
  113. return Error;
  114. }
  115. int test_determinant()
  116. {
  117. return 0;
  118. }
  119. int test_inverse()
  120. {
  121. int Failed(0);
  122. glm::mat4x4 A4x4(
  123. glm::vec4(1, 0, 1, 0),
  124. glm::vec4(0, 1, 0, 0),
  125. glm::vec4(0, 0, 1, 0),
  126. glm::vec4(0, 0, 0, 1));
  127. glm::mat4x4 B4x4 = inverse(A4x4);
  128. glm::mat4x4 I4x4 = A4x4 * B4x4;
  129. Failed += I4x4 == glm::mat4x4(1) ? 0 : 1;
  130. glm::mat3x3 A3x3(
  131. glm::vec3(1, 0, 1),
  132. glm::vec3(0, 1, 0),
  133. glm::vec3(0, 0, 1));
  134. glm::mat3x3 B3x3 = glm::inverse(A3x3);
  135. glm::mat3x3 I3x3 = A3x3 * B3x3;
  136. Failed += I3x3 == glm::mat3x3(1) ? 0 : 1;
  137. glm::mat2x2 A2x2(
  138. glm::vec2(1, 1),
  139. glm::vec2(0, 1));
  140. glm::mat2x2 B2x2 = glm::inverse(A2x2);
  141. glm::mat2x2 I2x2 = A2x2 * B2x2;
  142. Failed += I2x2 == glm::mat2x2(1) ? 0 : 1;
  143. return Failed;
  144. }
  145. int main()
  146. {
  147. int Failed = 0;
  148. Failed += test_matrixCompMult();
  149. Failed += test_outerProduct();
  150. Failed += test_transpose();
  151. Failed += test_determinant();
  152. Failed += test_inverse();
  153. return Failed;
  154. }