unit-Transform.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #define CATCH_CONFIG_ENABLE_BENCHMARKING
  2. #include <catch2/catch.hpp>
  3. #include <iostream>
  4. #include <gul/math/Transform.h>
  5. #include <glm/gtx/io.hpp>
  6. #include <glm/gtc/random.hpp>
  7. SCENARIO("test")
  8. {
  9. gul::Transform T;
  10. auto M = T.getMatrix();
  11. REQUIRE( M[0][0] == Approx(1.0f));
  12. REQUIRE( M[1][1] == Approx(1.0f));
  13. REQUIRE( M[2][2] == Approx(1.0f));
  14. REQUIRE( M[3][3] == Approx(1.0f));
  15. }
  16. glm::mat4 matrix_multiply(gul::Transform const &T)
  17. {
  18. return glm::translate( glm::mat4(1.0f), T.position) * glm::mat4_cast(T.rotation) * glm::scale( glm::mat4(1.0), T.scale);
  19. }
  20. glm::mat4 sneaky(gul::Transform const &T)
  21. {
  22. auto M = glm::mat4_cast(T.rotation);
  23. M[0] *= T.scale[0];
  24. M[1] *= T.scale[1];
  25. M[2] *= T.scale[2];
  26. M[3] = glm::vec4(T.position,1.0f);
  27. return M;
  28. }
  29. SCENARIO("Testing Transform::getMatrix()")
  30. {
  31. gul::Transform T;
  32. T.lookat( {5,5,5}, {0,1,0});
  33. T.position = {3,3,3};
  34. T.scale = {2,3,4};
  35. BENCHMARK("Matrix Multiply")
  36. {
  37. return matrix_multiply(T);
  38. };
  39. BENCHMARK("Sneaky")
  40. {
  41. return sneaky(T);
  42. };
  43. BENCHMARK("T.getMatrix()")
  44. {
  45. return T.getMatrix();
  46. };
  47. auto V = glm::linearRand( glm::vec3(-10), glm::vec3(10));
  48. BENCHMARK("T * v")
  49. {
  50. return T*V;
  51. };
  52. auto N = T.getMatrix();
  53. BENCHMARK("M * v")
  54. {
  55. return glm::vec3(N*glm::vec4(V,1.0f));
  56. };
  57. BENCHMARK("T.getMatrix() * v")
  58. {
  59. return glm::vec3(T.getMatrix()*glm::vec4(V,1.0f));
  60. };
  61. // Test that the T.getMatrix() method produces the same
  62. // transformed vector
  63. for(int i=0;i<100;i++)
  64. {
  65. auto v = glm::linearRand( glm::vec3(-10), glm::vec3(10));
  66. auto M1 = matrix_multiply(T);
  67. auto M2 = sneaky(T);
  68. auto M3 = T.getMatrix();
  69. auto a = M1 * glm::vec4(v,1.0);
  70. auto b = M2 * glm::vec4(v,1.0);
  71. auto c = M3 * glm::vec4(v,1.0);
  72. auto d = T * v;
  73. REQUIRE( a.x == Approx(b.x));
  74. REQUIRE( a.y == Approx(b.y));
  75. REQUIRE( a.z == Approx(b.z));
  76. REQUIRE( c.x == Approx(b.x));
  77. REQUIRE( c.y == Approx(b.y));
  78. REQUIRE( c.z == Approx(b.z));
  79. REQUIRE( a.x == Approx(d.x).epsilon(0.1));
  80. REQUIRE( a.y == Approx(d.y).epsilon(0.1));
  81. REQUIRE( a.z == Approx(d.z).epsilon(0.1));
  82. }
  83. }