1 #include "catch.hpp" 2 3 #include <osmium/area/detail/node_ref_segment.hpp> 4 5 using osmium::area::detail::NodeRefSegment; 6 using osmium::area::detail::role_type; 7 8 TEST_CASE("Default construction of NodeRefSegment") { 9 NodeRefSegment s; 10 REQUIRE(s.first().ref() == 0); 11 REQUIRE(s.first().location() == osmium::Location()); 12 REQUIRE(s.second().ref() == 0); 13 REQUIRE(s.second().location() == osmium::Location()); 14 } 15 16 TEST_CASE("Construction of NodeRefSegment with NodeRefs") { 17 osmium::NodeRef nr1{1, {1.2, 3.4}}; 18 osmium::NodeRef nr2{2, {1.4, 3.1}}; 19 osmium::NodeRef nr3{3, {1.2, 3.6}}; 20 osmium::NodeRef nr4{4, {1.2, 3.7}}; 21 22 NodeRefSegment s1{nr1, nr2, role_type::unknown, nullptr}; 23 REQUIRE(s1.first().ref() == 1); 24 REQUIRE(s1.second().ref() == 2); 25 26 NodeRefSegment s2{nr2, nr3, role_type::unknown, nullptr}; 27 REQUIRE(s2.first().ref() == 3); 28 REQUIRE(s2.second().ref() == 2); 29 30 NodeRefSegment s3{nr3, nr4, role_type::unknown, nullptr}; 31 REQUIRE(s3.first().ref() == 3); 32 REQUIRE(s3.second().ref() == 4); 33 } 34 35 TEST_CASE("Intersection of NodeRefSegments") { 36 NodeRefSegment s1{{ 1, {0.0, 0.0}}, { 2, {2.0, 2.0}}, role_type::unknown, nullptr}; 37 NodeRefSegment s2{{ 3, {0.0, 2.0}}, { 4, {2.0, 0.0}}, role_type::unknown, nullptr}; 38 NodeRefSegment s3{{ 5, {2.0, 0.0}}, { 6, {4.0, 2.0}}, role_type::unknown, nullptr}; 39 NodeRefSegment s4{{ 7, {1.0, 0.0}}, { 8, {3.0, 2.0}}, role_type::unknown, nullptr}; 40 NodeRefSegment s5{{ 9, {0.0, 4.0}}, {10, {4.0, 0.0}}, role_type::unknown, nullptr}; 41 NodeRefSegment s6{{11, {0.0, 0.0}}, {12, {1.0, 1.0}}, role_type::unknown, nullptr}; 42 NodeRefSegment s7{{13, {1.0, 1.0}}, {14, {3.0, 3.0}}, role_type::unknown, nullptr}; 43 44 REQUIRE(calculate_intersection(s1, s2) == osmium::Location(1.0, 1.0)); 45 REQUIRE(calculate_intersection(s2, s1) == osmium::Location(1.0, 1.0)); 46 47 REQUIRE(calculate_intersection(s1, s3) == osmium::Location()); 48 REQUIRE(calculate_intersection(s3, s1) == osmium::Location()); 49 50 REQUIRE(calculate_intersection(s2, s3) == osmium::Location()); 51 REQUIRE(calculate_intersection(s3, s2) == osmium::Location()); 52 53 REQUIRE(calculate_intersection(s1, s4) == osmium::Location()); 54 REQUIRE(calculate_intersection(s4, s1) == osmium::Location()); 55 56 REQUIRE(calculate_intersection(s1, s5) == osmium::Location(2.0, 2.0)); 57 REQUIRE(calculate_intersection(s5, s1) == osmium::Location(2.0, 2.0)); 58 59 REQUIRE(calculate_intersection(s1, s6) == osmium::Location(1.0, 1.0)); 60 REQUIRE(calculate_intersection(s6, s1) == osmium::Location(1.0, 1.0)); 61 62 REQUIRE(calculate_intersection(s1, s7) == osmium::Location(1.0, 1.0)); 63 REQUIRE(calculate_intersection(s7, s1) == osmium::Location(1.0, 1.0)); 64 65 REQUIRE(calculate_intersection(s6, s7) == osmium::Location()); 66 REQUIRE(calculate_intersection(s7, s6) == osmium::Location()); 67 } 68 69 TEST_CASE("Intersection of collinear NodeRefSegments") { 70 NodeRefSegment s1{{ 1, {0.0, 0.0}}, { 2, {2.0, 0.0}}, role_type::unknown, nullptr}; // *---* 71 NodeRefSegment s2{{ 3, {2.0, 0.0}}, { 4, {4.0, 0.0}}, role_type::unknown, nullptr}; // *---* 72 NodeRefSegment s3{{ 5, {0.0, 0.0}}, { 6, {1.0, 0.0}}, role_type::unknown, nullptr}; // *-* 73 NodeRefSegment s4{{ 7, {1.0, 0.0}}, { 8, {2.0, 0.0}}, role_type::unknown, nullptr}; // *-* 74 NodeRefSegment s5{{ 9, {1.0, 0.0}}, {10, {3.0, 0.0}}, role_type::unknown, nullptr}; // *---* 75 NodeRefSegment s6{{11, {0.0, 0.0}}, {12, {4.0, 0.0}}, role_type::unknown, nullptr}; // *-------* 76 NodeRefSegment s7{{13, {0.0, 0.0}}, {14, {5.0, 0.0}}, role_type::unknown, nullptr}; // *---------* 77 NodeRefSegment s8{{13, {1.0, 0.0}}, {14, {5.0, 0.0}}, role_type::unknown, nullptr}; // *-------* 78 NodeRefSegment s9{{13, {3.0, 0.0}}, {14, {4.0, 0.0}}, role_type::unknown, nullptr}; // *-* 79 80 REQUIRE(calculate_intersection(s1, s1) == osmium::Location()); 81 82 REQUIRE(calculate_intersection(s1, s2) == osmium::Location()); 83 REQUIRE(calculate_intersection(s2, s1) == osmium::Location()); 84 85 REQUIRE(calculate_intersection(s1, s3) == osmium::Location(1.0, 0.0)); 86 REQUIRE(calculate_intersection(s3, s1) == osmium::Location(1.0, 0.0)); 87 88 REQUIRE(calculate_intersection(s1, s4) == osmium::Location(1.0, 0.0)); 89 REQUIRE(calculate_intersection(s4, s1) == osmium::Location(1.0, 0.0)); 90 91 REQUIRE(calculate_intersection(s1, s5) == osmium::Location(1.0, 0.0)); 92 REQUIRE(calculate_intersection(s5, s1) == osmium::Location(1.0, 0.0)); 93 94 REQUIRE(calculate_intersection(s1, s6) == osmium::Location(2.0, 0.0)); 95 REQUIRE(calculate_intersection(s6, s1) == osmium::Location(2.0, 0.0)); 96 97 REQUIRE(calculate_intersection(s1, s7) == osmium::Location(2.0, 0.0)); 98 REQUIRE(calculate_intersection(s7, s1) == osmium::Location(2.0, 0.0)); 99 100 REQUIRE(calculate_intersection(s1, s8) == osmium::Location(1.0, 0.0)); 101 REQUIRE(calculate_intersection(s8, s1) == osmium::Location(1.0, 0.0)); 102 103 REQUIRE(calculate_intersection(s1, s9) == osmium::Location()); 104 REQUIRE(calculate_intersection(s9, s1) == osmium::Location()); 105 106 REQUIRE(calculate_intersection(s5, s6) == osmium::Location(1.0, 0.0)); 107 REQUIRE(calculate_intersection(s6, s5) == osmium::Location(1.0, 0.0)); 108 109 REQUIRE(calculate_intersection(s7, s8) == osmium::Location(1.0, 0.0)); 110 REQUIRE(calculate_intersection(s8, s7) == osmium::Location(1.0, 0.0)); 111 } 112 113 TEST_CASE("Intersection of very long NodeRefSegments") { 114 NodeRefSegment s1{{1, {90.0, 90.0}}, {2, {-90.0, -90.0}}, role_type::unknown, nullptr}; 115 NodeRefSegment s2{{1, {-90.0, 90.0}}, {2, {90.0, -90.0}}, role_type::unknown, nullptr}; 116 REQUIRE(calculate_intersection(s1, s2) == osmium::Location(0.0, 0.0)); 117 118 NodeRefSegment s3{{1, {-90.0, -90.0}}, {2, {90.0, 90.0}}, role_type::unknown, nullptr}; 119 NodeRefSegment s4{{1, {-90.0, 90.0}}, {2, {90.0, -90.0}}, role_type::unknown, nullptr}; 120 REQUIRE(calculate_intersection(s3, s4) == osmium::Location(0.0, 0.0)); 121 122 NodeRefSegment s5{{1, {-90.00000001, -90.0}}, {2, {90.0, 90.0}}, role_type::unknown, nullptr}; 123 NodeRefSegment s6{{1, {-90.0, 90.0}}, {2, {90.0, -90.0}}, role_type::unknown, nullptr}; 124 REQUIRE(calculate_intersection(s5, s6) == osmium::Location(0.0, 0.0)); 125 } 126 127 TEST_CASE("Ordering of NodeRefSegements") { 128 osmium::NodeRef node_ref1{1, {1.0, 3.0}}; 129 osmium::NodeRef node_ref2{2, {1.4, 2.9}}; 130 osmium::NodeRef node_ref3{3, {1.2, 3.0}}; 131 osmium::NodeRef node_ref4{4, {1.2, 3.3}}; 132 133 REQUIRE(node_ref1 < node_ref2); 134 REQUIRE(node_ref2 < node_ref3); 135 REQUIRE(node_ref1 < node_ref3); 136 REQUIRE(node_ref1 >= node_ref1); 137 138 REQUIRE( osmium::location_less()(node_ref1, node_ref2)); 139 REQUIRE_FALSE(osmium::location_less()(node_ref2, node_ref3)); 140 REQUIRE( osmium::location_less()(node_ref1, node_ref3)); 141 REQUIRE( osmium::location_less()(node_ref3, node_ref4)); 142 REQUIRE_FALSE(osmium::location_less()(node_ref1, node_ref1)); 143 } 144 145 TEST_CASE("More ordering of NodeRefSegments") { 146 osmium::NodeRef nr0{0, {0.0, 0.0}}; 147 osmium::NodeRef nr1{1, {1.0, 0.0}}; 148 osmium::NodeRef nr2{2, {0.0, 1.0}}; 149 osmium::NodeRef nr3{3, {2.0, 0.0}}; 150 osmium::NodeRef nr4{4, {0.0, 2.0}}; 151 osmium::NodeRef nr5{5, {1.0, 1.0}}; 152 osmium::NodeRef nr6{6, {2.0, 2.0}}; 153 osmium::NodeRef nr7{6, {1.0, 2.0}}; 154 155 NodeRefSegment s1{nr0, nr1, role_type::unknown, nullptr}; 156 NodeRefSegment s2{nr0, nr2, role_type::unknown, nullptr}; 157 NodeRefSegment s3{nr0, nr3, role_type::unknown, nullptr}; 158 NodeRefSegment s4{nr0, nr4, role_type::unknown, nullptr}; 159 NodeRefSegment s5{nr0, nr5, role_type::unknown, nullptr}; 160 NodeRefSegment s6{nr0, nr6, role_type::unknown, nullptr}; 161 NodeRefSegment s7{nr0, nr7, role_type::unknown, nullptr}; 162 163 // s1 164 REQUIRE_FALSE(s1 < s1); 165 REQUIRE(s2 < s1); 166 REQUIRE(s1 < s3); 167 REQUIRE(s4 < s1); 168 REQUIRE(s5 < s1); 169 REQUIRE(s6 < s1); 170 REQUIRE(s7 < s1); 171 172 // s2 173 REQUIRE_FALSE(s2 < s2); 174 REQUIRE(s2 < s3); 175 REQUIRE(s2 < s4); 176 REQUIRE(s2 < s5); 177 REQUIRE(s2 < s6); 178 REQUIRE(s2 < s7); 179 180 // s3 181 REQUIRE_FALSE(s3 < s3); 182 REQUIRE(s4 < s3); 183 REQUIRE(s5 < s3); 184 REQUIRE(s6 < s3); 185 REQUIRE(s7 < s3); 186 187 // s4 188 REQUIRE_FALSE(s4 < s4); 189 REQUIRE(s4 < s5); 190 REQUIRE(s4 < s6); 191 REQUIRE(s4 < s7); 192 193 // s5 194 REQUIRE_FALSE(s5 < s5); 195 REQUIRE(s5 < s6); 196 REQUIRE(s7 < s5); 197 198 // s6 199 REQUIRE_FALSE(s6 < s6); 200 REQUIRE(s7 < s6); 201 202 // s7 203 REQUIRE_FALSE(s7 < s7); 204 } 205 206