test_segment_intersection.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include <AP_gtest.h>
  2. #include <AP_Math/vector2.h>
  3. #define EXPECT_VECTOR2F_EQ(v1, v2) \
  4. do { \
  5. EXPECT_FLOAT_EQ(v1[0], v2[0]); \
  6. EXPECT_FLOAT_EQ(v1[1], v2[1]); \
  7. } while (false);
  8. #define TEST_INTERSECT(s1p1x,s1p1y,s1p2x,s1p2y, s2p1x,s2p1y,s2p2x,s2p2y, ix,iy, expected) \
  9. do { \
  10. const Vector2f s1p1(s1p1x,s1p1y); \
  11. const Vector2f s1p2(s1p2x,s1p2y); \
  12. const Vector2f s2p1(s2p1x,s2p1y); \
  13. const Vector2f s2p2(s2p2x,s2p2y); \
  14. Vector2f calculated_intersection; \
  15. bool result = Vector2f::segment_intersection(s1p1, s1p2, s2p1, s2p2, calculated_intersection); \
  16. EXPECT_EQ(expected, result); \
  17. if (result == expected && expected) { \
  18. Vector2f expected_intersection{ix, iy}; \
  19. EXPECT_VECTOR2F_EQ(calculated_intersection, expected_intersection); \
  20. } \
  21. result = Vector2f::segment_intersection(s2p1, s2p2, s1p1, s1p2, calculated_intersection); \
  22. EXPECT_EQ(expected, result); \
  23. if (expected) { \
  24. Vector2f expected_intersection{ix, iy}; \
  25. EXPECT_VECTOR2F_EQ(calculated_intersection, expected_intersection); \
  26. EXPECT_EQ(Vector2f::point_on_segment(calculated_intersection,s1p1, s1p2), true); \
  27. EXPECT_EQ(Vector2f::point_on_segment(calculated_intersection,s2p1, s2p2), true); \
  28. } \
  29. } while (false);
  30. #define SHOULD_INTERSECT(s1p1x,s1p1y,s1p2x,s1p2y, s2p1x,s2p1y,s2p2x,s2p2y, ix, iy) \
  31. TEST_INTERSECT(s1p1x,s1p1y,s1p2x,s1p2y, s2p1x,s2p1y,s2p2x,s2p2y, ix, iy, true)
  32. #define SHOULD_NOT_INTERSECT(s1p1x,s1p1y,s1p2x,s1p2y, s2p1x,s2p1y,s2p2x,s2p2y) \
  33. TEST_INTERSECT(s1p1x,s1p1y,s1p2x,s1p2y, s2p1x,s2p1y,s2p2x,s2p2y, 0, 0, false)
  34. TEST(SegmentIntersectionTests, Simple)
  35. {
  36. SHOULD_INTERSECT(0,0,2,2, 2,0,0,2, 1,1);
  37. SHOULD_INTERSECT(0,0,5,5, 2,0,0,2, 1,1);
  38. }
  39. TEST(SegmentIntersectionTests, Parallel)
  40. {
  41. SHOULD_NOT_INTERSECT(0,0,0,1, 2,0,2,1);
  42. }
  43. // the following should probably intersect but don't:
  44. // TEST(SegmentIntersectionTests, Subsegment)
  45. // {
  46. // SHOULD_INTERSECT(0,0,0,2, 0,0,0,1, 1,1);
  47. // }
  48. // TEST(SegmentIntersectionTests, Identical)
  49. // {
  50. // SHOULD_INTERSECT(0,0,0,2, 0,0,0,2, 1,1);
  51. // }
  52. TEST(SegmentIntersectionTests, NonIntersecting)
  53. {
  54. SHOULD_NOT_INTERSECT(0,0,0,2, 1,1,1,2);
  55. SHOULD_NOT_INTERSECT(0,0,0,1, -2,2,2,2)
  56. }
  57. AP_GTEST_MAIN()