|
| 1 | +#include <gtest/gtest.h> |
| 2 | +#include <vector> |
| 3 | + |
| 4 | +#include "PolyUtil.hpp" |
| 5 | + |
| 6 | + |
| 7 | +TEST(PolyUtil, isLocationOnEdge) { |
| 8 | + // Empty |
| 9 | + std::vector<LatLng> empty; |
| 10 | + EXPECT_FALSE(PolyUtil::isLocationOnEdge(LatLng(0, 0), empty, true)); |
| 11 | + EXPECT_FALSE(PolyUtil::isLocationOnEdge(LatLng(0, 0), empty, false)); |
| 12 | + |
| 13 | + // One point. |
| 14 | + std::vector<LatLng> one = { {1, 2} }; |
| 15 | + EXPECT_TRUE(PolyUtil::isLocationOnEdge(LatLng(1, 2), one, true)); |
| 16 | + EXPECT_TRUE(PolyUtil::isLocationOnEdge(LatLng(1, 2), one, false)); |
| 17 | + |
| 18 | + EXPECT_FALSE(PolyUtil::isLocationOnEdge(LatLng(3, 5), one, true)); |
| 19 | + EXPECT_FALSE(PolyUtil::isLocationOnEdge(LatLng(3, 5), one, false)); |
| 20 | + |
| 21 | + // Endpoints |
| 22 | + std::vector<LatLng> endpoints = { {1, 2}, {3, 5} }; |
| 23 | + for (const auto & point : { LatLng(1, 2), LatLng(3, 5) }) { |
| 24 | + EXPECT_TRUE(PolyUtil::isLocationOnEdge(point, endpoints, true)); |
| 25 | + EXPECT_TRUE(PolyUtil::isLocationOnEdge(point, endpoints, false)); |
| 26 | + } |
| 27 | + EXPECT_FALSE(PolyUtil::isLocationOnEdge(LatLng(0, 0), endpoints, true)); |
| 28 | + EXPECT_FALSE(PolyUtil::isLocationOnEdge(LatLng(0, 0), endpoints, false)); |
| 29 | + |
| 30 | + double small = 5e-7; // About 5cm on equator, half the default tolerance. |
| 31 | + double big = 2e-6; // About 10cm on equator, double the default tolerance. |
| 32 | + |
| 33 | + // On equator. |
| 34 | + std::vector<LatLng> equator = { {0, 90}, {0, 180} }; |
| 35 | + for (const auto & point : { LatLng(0, 90-small), LatLng(0, 90+small), LatLng(0-small, 90), LatLng(0, 135), LatLng(small, 135) }) { |
| 36 | + EXPECT_TRUE(PolyUtil::isLocationOnEdge(point, equator, true)); |
| 37 | + } |
| 38 | + for (const auto & point : { LatLng(0, 90 - big), LatLng(0, 0), LatLng(0, -90), LatLng(big, 135) }) { |
| 39 | + EXPECT_FALSE(PolyUtil::isLocationOnEdge(point, equator, false)); |
| 40 | + } |
| 41 | + |
| 42 | + // Ends on same latitude. |
| 43 | + std::vector<LatLng> sameLatitude = { {-45, -180}, {-45, -small} }; |
| 44 | + for (const auto & point : { LatLng(-45, 180+small), LatLng(-45, 180-small), LatLng(-45-small, 180-small), LatLng(-45, 0) }) { |
| 45 | + EXPECT_TRUE(PolyUtil::isLocationOnEdge(point, sameLatitude, true)); |
| 46 | + } |
| 47 | + for (const auto & point : { LatLng(-45, big), LatLng(-45, 180-big), LatLng(-45+big, -90), LatLng(-45, 90) }) { |
| 48 | + EXPECT_FALSE(PolyUtil::isLocationOnEdge(point, sameLatitude, false)); |
| 49 | + } |
| 50 | + |
| 51 | + // Meridian. |
| 52 | + std::vector<LatLng> meridian = { {-10, 30}, {45, 30} }; |
| 53 | + for (const auto & point : { LatLng(10, 30 - small), LatLng(20, 30 + small), LatLng(-10 - small, 30 + small) }) { |
| 54 | + EXPECT_TRUE(PolyUtil::isLocationOnEdge(point, meridian, true)); |
| 55 | + } |
| 56 | + for (const auto & point : { LatLng(-10 - big, 30), LatLng(10, -150), LatLng(0, 30 - big) }) { |
| 57 | + EXPECT_FALSE(PolyUtil::isLocationOnEdge(point, meridian, false)); |
| 58 | + } |
| 59 | + |
| 60 | + // Slanted close to meridian, close to North pole. |
| 61 | + std::vector<LatLng> northPole = { {0, 0}, {90 - small, 0 + big} }; |
| 62 | + for (const auto & point : { LatLng(1, 0 + small), LatLng(2, 0 - small), LatLng(90 - small, -90), LatLng(90 - small, 10) }) { |
| 63 | + EXPECT_TRUE(PolyUtil::isLocationOnEdge(point, northPole, true)); |
| 64 | + } |
| 65 | + for (const auto & point : { LatLng(-big, 0), LatLng(90 - big, 180), LatLng(10, big) }) { |
| 66 | + EXPECT_FALSE(PolyUtil::isLocationOnEdge(point, northPole, false)); |
| 67 | + } |
| 68 | + |
| 69 | + // Arc > 120 deg. |
| 70 | + std::vector<LatLng> poly = { {0, 0}, {0, 179.999} }; |
| 71 | + for (const auto & point : { LatLng(0, 90), LatLng(0, small), LatLng(0, 179), LatLng(small, 90) }) { |
| 72 | + EXPECT_TRUE(PolyUtil::isLocationOnEdge(point, poly, true)); |
| 73 | + } |
| 74 | + for (const auto & point : { LatLng(0, -90), LatLng(small, -100), LatLng(0, 180), LatLng(0, -big), LatLng(90, 0), LatLng(-90, 180) }) { |
| 75 | + EXPECT_FALSE(PolyUtil::isLocationOnEdge(point, poly, false)); |
| 76 | + } |
| 77 | + |
| 78 | + std::vector<LatLng> poly2 = { {10, 5}, {30, 15} }; |
| 79 | + for (const auto & point : { LatLng(10+2*big, 5+big), LatLng(10+big, 5+big/2), LatLng(30-2*big, 15-big) }) { |
| 80 | + EXPECT_TRUE(PolyUtil::isLocationOnEdge(point, poly2, true)); |
| 81 | + } |
| 82 | + for (const auto & point : { LatLng(20, 10), LatLng(10-big, 5-big/2), LatLng(30+2*big, 15+big), LatLng(10+2*big, 5), LatLng(10, 5+big) }) { |
| 83 | + EXPECT_FALSE(PolyUtil::isLocationOnEdge(point, poly2, false)); |
| 84 | + } |
| 85 | + |
| 86 | + std::vector<LatLng> poly3 = { {90 - small, 0}, {0, 180 - small / 2} }; |
| 87 | + for (const auto & point : { LatLng(big, -180 + small / 2), LatLng(big, 180 - small / 4), LatLng(big, 180 - small) }) { |
| 88 | + EXPECT_TRUE(PolyUtil::isLocationOnEdge(point, poly3, true)); |
| 89 | + } |
| 90 | + for (const auto & point : { LatLng(-big, -180 + small / 2), LatLng(-big, 180), LatLng(-big, 180 - small) }) { |
| 91 | + EXPECT_FALSE(PolyUtil::isLocationOnEdge(point, poly3, false)); |
| 92 | + } |
| 93 | + |
| 94 | + // Reaching close to North pole. |
| 95 | + std::vector<LatLng> closeToNorthPole = { {80, 0}, {80, 180 - small} }; |
| 96 | + |
| 97 | + for (const auto & point : { LatLng(90 - small, -90), LatLng(90, -135), LatLng(80 - small, 0), LatLng(80 + small, 0) }) { |
| 98 | + EXPECT_TRUE(PolyUtil::isLocationOnEdge(point, closeToNorthPole, true)); |
| 99 | + } |
| 100 | + for (const auto & point : { LatLng(80, 90), LatLng(79, big) }) { |
| 101 | + EXPECT_FALSE(PolyUtil::isLocationOnEdge(point, closeToNorthPole, true)); |
| 102 | + } |
| 103 | + |
| 104 | + for (const auto & point : { LatLng(80 - small, 0), LatLng(80 + small, 0), LatLng(80, 90) }) { |
| 105 | + // EXPECT_TRUE(PolyUtil::isLocationOnEdge(point, closeToNorthPole, false)); |
| 106 | + } |
| 107 | + for (const auto & point : { LatLng(79, big), LatLng(90 - small, -90), LatLng(90, -135) }) { |
| 108 | + EXPECT_FALSE(PolyUtil::isLocationOnEdge(point, closeToNorthPole, false)); |
| 109 | + } |
| 110 | +} |
0 commit comments