Platformer in OpenGL
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.1 KiB

5 years ago
  1. #define GLM_ENABLE_EXPERIMENTAL
  2. #include <glm/gtx/matrix_factorisation.hpp>
  3. #include <glm/gtc/constants.hpp>
  4. #include <glm/gtc/epsilon.hpp>
  5. template <glm::length_t C, glm::length_t R, typename T, glm::qualifier Q>
  6. int test_qr(glm::mat<C, R, T, Q> m)
  7. {
  8. int Error = 0;
  9. T const epsilon = static_cast<T>(1e-10);
  10. glm::mat<(C < R ? C : R), R, T, Q> q(-999);
  11. glm::mat<C, (C < R ? C : R), T, Q> r(-999);
  12. glm::qr_decompose(m, q, r);
  13. //Test if q*r really equals the input matrix
  14. glm::mat<C, R, T, Q> tm = q*r;
  15. glm::mat<C, R, T, Q> err = tm - m;
  16. for (glm::length_t i = 0; i < C; i++)
  17. for (glm::length_t j = 0; j < R; j++)
  18. Error += glm::abs(err[i][j]) > epsilon ? 1 : 0;
  19. //Test if the columns of q are orthonormal
  20. for (glm::length_t i = 0; i < (C < R ? C : R); i++)
  21. {
  22. Error += (length(q[i]) - 1) > epsilon ? 1 : 0;
  23. for (glm::length_t j = 0; j<i; j++)
  24. Error += glm::abs(dot(q[i], q[j])) > epsilon ? 1 : 0;
  25. }
  26. //Test if the matrix r is upper triangular
  27. for (glm::length_t i = 0; i < C; i++)
  28. for (glm::length_t j = i + 1; j < (C < R ? C : R); j++)
  29. Error += glm::epsilonEqual(r[i][j], static_cast<T>(0), glm::epsilon<T>()) ? 0 : 1;
  30. return Error;
  31. }
  32. template <glm::length_t C, glm::length_t R, typename T, glm::qualifier Q>
  33. int test_rq(glm::mat<C, R, T, Q> m)
  34. {
  35. int Error = 0;
  36. T const epsilon = static_cast<T>(1e-10);
  37. glm::mat<C, (C < R ? C : R), T, Q> q(-999);
  38. glm::mat<(C < R ? C : R), R, T, Q> r(-999);
  39. glm::rq_decompose(m, r, q);
  40. //Test if q*r really equals the input matrix
  41. glm::mat<C, R, T, Q> tm = r*q;
  42. glm::mat<C, R, T, Q> err = tm - m;
  43. for (glm::length_t i = 0; i < C; i++)
  44. for (glm::length_t j = 0; j < R; j++)
  45. Error += glm::abs(err[i][j]) > epsilon ? 1 : 0;
  46. //Test if the rows of q are orthonormal
  47. glm::mat<(C < R ? C : R), C, T, Q> tq = transpose(q);
  48. for (glm::length_t i = 0; i < (C < R ? C : R); i++)
  49. {
  50. Error += (length(tq[i]) - 1) > epsilon ? 1 : 0;
  51. for (glm::length_t j = 0; j<i; j++)
  52. Error += glm::abs(dot(tq[i], tq[j])) > epsilon ? 1 : 0;
  53. }
  54. //Test if the matrix r is upper triangular
  55. for (glm::length_t i = 0; i < (C < R ? C : R); i++)
  56. for (glm::length_t j = R - (C < R ? C : R) + i + 1; j < R; j++)
  57. Error += glm::epsilonEqual(r[i][j], static_cast<T>(0), glm::epsilon<T>()) ? 0 : 1;
  58. return Error;
  59. }
  60. int main()
  61. {
  62. int Error = 0;
  63. //Test QR square
  64. Error += test_qr(glm::dmat3(12.0, 6.0, -4.0, -51.0, 167.0, 24.0, 4.0, -68.0, -41.0)) ? 1 : 0;
  65. //Test RQ square
  66. Error += test_rq(glm::dmat3(12.0, 6.0, -4.0, -51.0, 167.0, 24.0, 4.0, -68.0, -41.0)) ? 1 : 0;
  67. //Test QR triangular 1
  68. Error += test_qr(glm::dmat3x4(12.0, 6.0, -4.0, -51.0, 167.0, 24.0, 4.0, -68.0, -41.0, 7.0, 2.0, 15.0)) ? 1 : 0;
  69. //Test QR triangular 2
  70. Error += test_qr(glm::dmat4x3(12.0, 6.0, -4.0, -51.0, 167.0, 24.0, 4.0, -68.0, -41.0, 7.0, 2.0, 15.0)) ? 1 : 0;
  71. //Test RQ triangular 1 : Fails at the triangular test
  72. Error += test_rq(glm::dmat3x4(12.0, 6.0, -4.0, -51.0, 167.0, 24.0, 4.0, -68.0, -41.0, 7.0, 2.0, 15.0)) ? 1 : 0;
  73. //Test QR triangular 2
  74. Error += test_rq(glm::dmat4x3(12.0, 6.0, -4.0, -51.0, 167.0, 24.0, 4.0, -68.0, -41.0, 7.0, 2.0, 15.0)) ? 1 : 0;
  75. return Error;
  76. }