gtx_intersect.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #define GLM_ENABLE_EXPERIMENTAL
  2. #include <glm/glm.hpp>
  3. #include <glm/gtc/epsilon.hpp>
  4. #include <glm/gtx/intersect.hpp>
  5. static int test_intersectRayPlane()
  6. {
  7. int Error = 0;
  8. glm::vec3 const PlaneOrigin(0, 0, 1);
  9. glm::vec3 const PlaneNormal(0, 0, -1);
  10. glm::vec3 const RayOrigin(0, 0, 0);
  11. glm::vec3 const RayDir(0, 0, 1);
  12. // check that inversion of the plane normal has no effect
  13. {
  14. float Distance = 0;
  15. bool const Result = glm::intersectRayPlane(RayOrigin, RayDir, PlaneOrigin, PlaneNormal, Distance);
  16. Error += glm::abs(Distance - 1.f) <= std::numeric_limits<float>::epsilon() ? 0 : 1;
  17. Error += Result ? 0 : 1;
  18. }
  19. {
  20. float Distance = 0;
  21. bool const Result = glm::intersectRayPlane(RayOrigin, RayDir, PlaneOrigin, -1.f * PlaneNormal, Distance);
  22. Error += glm::abs(Distance - 1.f) <= std::numeric_limits<float>::epsilon() ? 0 : 1;
  23. Error += Result ? 0 : 1;
  24. }
  25. // check if plane is before of behind the ray origin
  26. {
  27. float Distance = 9.9999f; // value should not be changed
  28. bool const Result = glm::intersectRayPlane(RayOrigin, RayDir, -1.f * PlaneOrigin, PlaneNormal, Distance);
  29. Error += glm::abs(Distance - 9.9999f) <= std::numeric_limits<float>::epsilon() ? 0 : 1;
  30. Error += Result ? 1 : 0; // there is no intersection in front of the ray origin, only behind
  31. }
  32. return Error;
  33. }
  34. static int test_intersectRayTriangle()
  35. {
  36. int Error = 0;
  37. glm::vec3 const Orig(0, 0, 2);
  38. glm::vec3 const Dir(0, 0, -1);
  39. glm::vec3 const Vert0(0, 0, 0);
  40. glm::vec3 const Vert1(-1, -1, 0);
  41. glm::vec3 const Vert2(1, -1, 0);
  42. glm::vec2 BaryPosition(0);
  43. float Distance = 0;
  44. bool const Result = glm::intersectRayTriangle(Orig, Dir, Vert0, Vert1, Vert2, BaryPosition, Distance);
  45. Error += glm::all(glm::epsilonEqual(BaryPosition, glm::vec2(0), std::numeric_limits<float>::epsilon())) ? 0 : 1;
  46. Error += glm::abs(Distance - 2.f) <= std::numeric_limits<float>::epsilon() ? 0 : 1;
  47. Error += Result ? 0 : 1;
  48. return Error;
  49. }
  50. #if GLM_PLATFORM != GLM_PLATFORM_LINUX
  51. static int test_intersectLineTriangle()
  52. {
  53. int Error = 0;
  54. glm::vec3 const Orig(0, 0, 2);
  55. glm::vec3 const Dir(0, 0, -1);
  56. glm::vec3 const Vert0(0, 0, 0);
  57. glm::vec3 const Vert1(-1, -1, 0);
  58. glm::vec3 const Vert2(1, -1, 0);
  59. glm::vec3 Position(2.0f, 0.0f, 0.0f);
  60. bool const Result = glm::intersectLineTriangle(Orig, Dir, Vert0, Vert1, Vert2, Position);
  61. Error += glm::all(glm::epsilonEqual(Position, glm::vec3(2.0f, 0.0f, 0.0f), std::numeric_limits<float>::epsilon())) ? 0 : 1;
  62. Error += Result ? 0 : 1;
  63. return Error;
  64. }
  65. #endif//GLM_PLATFORM != GLM_PLATFORM_LINUX
  66. int main()
  67. {
  68. int Error = 0;
  69. #if GLM_PLATFORM != GLM_PLATFORM_LINUX
  70. Error += test_intersectRayPlane();
  71. Error += test_intersectRayTriangle();
  72. Error += test_intersectLineTriangle(); // Disabled on 2025/01/16, C.I. failing on Ubuntu latest, GCC 13.3.0
  73. #endif//GLM_PLATFORM != GLM_PLATFORM_LINUX
  74. return Error;
  75. }