gtx_intersect.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #define GLM_ENABLE_EXPERIMENTAL
  2. #include <glm/glm.hpp>
  3. #include <glm/gtc/epsilon.hpp>
  4. #include <glm/gtx/intersect.hpp>
  5. 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. 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. int test_intersectLineTriangle()
  51. {
  52. int Error = 0;
  53. glm::vec3 const Orig(0, 0, 2);
  54. glm::vec3 const Dir(0, 0, -1);
  55. glm::vec3 const Vert0(0, 0, 0);
  56. glm::vec3 const Vert1(-1, -1, 0);
  57. glm::vec3 const Vert2(1, -1, 0);
  58. glm::vec3 Position(2.0f, 0.0f, 0.0f);
  59. bool const Result = glm::intersectLineTriangle(Orig, Dir, Vert0, Vert1, Vert2, Position);
  60. Error += glm::all(glm::epsilonEqual(Position, glm::vec3(2.0f, 0.0f, 0.0f), std::numeric_limits<float>::epsilon())) ? 0 : 1;
  61. Error += Result ? 0 : 1;
  62. return Error;
  63. }
  64. int main()
  65. {
  66. int Error = 0;
  67. Error += test_intersectRayPlane();
  68. Error += test_intersectRayTriangle();
  69. Error += test_intersectLineTriangle();
  70. return Error;
  71. }