| 1 | /* |
| 2 | Copyright (c) 2020, 2026, Oracle and/or its affiliates. |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU General Public License, version 2.0, |
| 6 | as published by the Free Software Foundation. |
| 7 | |
| 8 | This program is designed to work with certain software (including |
| 9 | but not limited to OpenSSL) that is licensed under separate terms, |
| 10 | as designated in a particular file or component or in included license |
| 11 | documentation. The authors of MySQL hereby grant you an additional |
| 12 | permission to link the program and your derivative works with the |
| 13 | separately licensed software that they have either included with |
| 14 | the program or referenced in the documentation. |
| 15 | |
| 16 | This program is distributed in the hope that it will be useful, |
| 17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | GNU General Public License, version 2.0, for more details. |
| 20 | |
| 21 | You should have received a copy of the GNU General Public License |
| 22 | along with this program; if not, write to the Free Software |
| 23 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 24 | */ |
| 25 | |
| 26 | #include <gtest/gtest.h> |
| 27 | #include <memory> // unique_ptr |
| 28 | #include <ostream> // << |
| 29 | |
| 30 | #include "sql/dd/dd.h" |
| 31 | #include "sql/dd/impl/types/spatial_reference_system_impl.h" |
| 32 | #include "sql/dd/properties.h" |
| 33 | #include "sql/dd/types/spatial_reference_system.h" |
| 34 | #include "sql/gis/geometries.h" |
| 35 | #include "sql/gis/geometries_cs.h" |
| 36 | #include "sql/gis/relops.h" |
| 37 | #include "sql/gis/setops.h" |
| 38 | #include "template_utils.h" // down_cast |
| 39 | #include "unittest/gunit/gis_setops_testshapes.h" |
| 40 | #include "unittest/gunit/gis_test.h" |
| 41 | #include "unittest/gunit/gis_typeset.h" |
| 42 | |
| 43 | namespace gis_intersectionunittest { |
| 44 | |
| 45 | bool areEquals(const dd::Spatial_reference_system_impl *srs, |
| 46 | const gis::Geometry &expected_result, |
| 47 | const gis::Geometry &actual_result) { |
| 48 | bool is_equals = false; |
| 49 | bool is_null = false; |
| 50 | bool equals_error = gis::equals(srs, &expected_result, &actual_result, |
| 51 | "unittest", &is_equals, &is_null); |
| 52 | EXPECT_FALSE(equals_error); |
| 53 | EXPECT_FALSE(is_null); |
| 54 | return is_equals; |
| 55 | } |
| 56 | |
| 57 | template <typename Types> |
| 58 | struct IntersectionTest : Gis_test<Types> { |
| 59 | void test_valid_input(const gis::Geometry &g1, const gis::Geometry &g2, |
| 60 | gis::Geometry &expected_result) { |
| 61 | std::unique_ptr<gis::Geometry> result; |
| 62 | bool is_null = false; |
| 63 | bool error = |
| 64 | gis::intersection(this->m_srs.get(), &g1, &g2, "unittest", &result); |
| 65 | EXPECT_FALSE(error); |
| 66 | EXPECT_FALSE(is_null); |
| 67 | |
| 68 | // Verify geometry return type. |
| 69 | const std::string type_res(gis::type_to_name(result->type())); |
| 70 | const std::string type_exp(gis::type_to_name(expected_result.type())); |
| 71 | EXPECT_EQ(type_exp, type_res); |
| 72 | |
| 73 | // Verify result is correct. |
| 74 | EXPECT_PRED3(areEquals, this->m_srs.get(), expected_result, *result.get()); |
| 75 | } |
| 76 | }; |
| 77 | |
| 78 | // The purpose of these tests is to check that the result returned from |
| 79 | // gis::intersection is correct. The tests test all combinations of geometries. |
| 80 | |
| 81 | TYPED_TEST_SUITE(IntersectionTest, gis_typeset::Test_both); |
| 82 | |
| 83 | // intersection(... ,point, *, ...) |
| 84 | |
| 85 | TYPED_TEST(IntersectionTest, PointPoint) { |
| 86 | typename TypeParam::Point pt1{0, 0}; |
| 87 | typename TypeParam::Point pt2{0, 0.1}; |
| 88 | typename TypeParam::Geometrycollection empty_gc{}; |
| 89 | |
| 90 | this->test_valid_input(pt1, pt2, empty_gc); |
| 91 | this->test_valid_input(pt1, pt1, pt1); |
| 92 | } |
| 93 | |
| 94 | TYPED_TEST(IntersectionTest, PointMultipoint) { |
| 95 | typename TypeParam::Point pt1{0, 0}; |
| 96 | typename TypeParam::Point pt2{0, 0.1}; |
| 97 | typename TypeParam::Multipoint mpt = simple_mpt<TypeParam>(); |
| 98 | typename TypeParam::Geometrycollection empty_gc{}; |
| 99 | |
| 100 | this->test_valid_input(pt2, mpt, empty_gc); |
| 101 | this->test_valid_input(mpt, pt1, pt1); |
| 102 | } |
| 103 | |
| 104 | TYPED_TEST(IntersectionTest, PointLinestring) { |
| 105 | typename TypeParam::Point pt1{0, 0}; |
| 106 | typename TypeParam::Point pt2{0.1, 0.1}; |
| 107 | typename TypeParam::Linestring ls = simple_ls<TypeParam>(); |
| 108 | typename TypeParam::Geometrycollection empty_gc{}; |
| 109 | |
| 110 | this->test_valid_input(pt2, ls, empty_gc); |
| 111 | this->test_valid_input(pt1, ls, pt1); |
| 112 | this->test_valid_input(ls, pt1, pt1); |
| 113 | } |
| 114 | |
| 115 | TYPED_TEST(IntersectionTest, PointMultiLinestring) { |
| 116 | typename TypeParam::Point pt1{0, 0}; |
| 117 | typename TypeParam::Point pt2{0.1, 0.1}; |
| 118 | typename TypeParam::Multilinestring mls = simple_mls<TypeParam>(); |
| 119 | typename TypeParam::Geometrycollection empty_gc{}; |
| 120 | |
| 121 | this->test_valid_input(pt2, mls, empty_gc); |
| 122 | this->test_valid_input(pt1, mls, pt1); |
| 123 | this->test_valid_input(mls, pt1, pt1); |
| 124 | } |
| 125 | |
| 126 | TYPED_TEST(IntersectionTest, PointPolygon) { |
| 127 | typename TypeParam::Point pt1{0, 0}; |
| 128 | typename TypeParam::Point pt2{0, 0.2}; |
| 129 | typename TypeParam::Polygon py = base_py<TypeParam>(); |
| 130 | typename TypeParam::Geometrycollection empty_gc{}; |
| 131 | |
| 132 | this->test_valid_input(pt2, py, empty_gc); |
| 133 | this->test_valid_input(pt1, py, pt1); |
| 134 | this->test_valid_input(py, pt1, pt1); |
| 135 | } |
| 136 | |
| 137 | TYPED_TEST(IntersectionTest, PointMultipolygon) { |
| 138 | typename TypeParam::Point pt1{0, 0}; |
| 139 | typename TypeParam::Point pt2{0, 0.2}; |
| 140 | typename TypeParam::Multipolygon mpy = simple_mpy<TypeParam>(); |
| 141 | typename TypeParam::Geometrycollection empty_gc{}; |
| 142 | |
| 143 | this->test_valid_input(pt2, mpy, empty_gc); |
| 144 | this->test_valid_input(pt1, mpy, pt1); |
| 145 | this->test_valid_input(mpy, pt1, pt1); |
| 146 | } |
| 147 | |
| 148 | // intersection(..., multipoint, *, ...) |
| 149 | |
| 150 | TYPED_TEST(IntersectionTest, MultipointMultipoint) { |
| 151 | typename TypeParam::Multipoint mpt1 = simple_mpt<TypeParam>(); |
| 152 | typename TypeParam::Multipoint mpt2 = simple_mpt<TypeParam>(); |
| 153 | typename TypeParam::Geometrycollection empty_gc{}; |
| 154 | typename TypeParam::Point pt2{0.0, 0.1}; |
| 155 | |
| 156 | mpt1.push_back(pt2); |
| 157 | mpt2.push_back(typename TypeParam::Point{0.1, 0.1}); |
| 158 | |
| 159 | this->test_valid_input(mpt1, mpt1, mpt1); |
| 160 | this->test_valid_input(mpt1, mpt2, mpt1[0]); |
| 161 | } |
| 162 | |
| 163 | TYPED_TEST(IntersectionTest, MultipointLinestring) { |
| 164 | typename TypeParam::Multipoint mpt = simple_mpt<TypeParam>(); |
| 165 | typename TypeParam::Linestring ls = simple_ls<TypeParam>(); |
| 166 | |
| 167 | mpt.push_back(typename TypeParam::Point(0.1, 0.1)); |
| 168 | |
| 169 | this->test_valid_input(mpt, ls, mpt[0]); |
| 170 | this->test_valid_input(ls, mpt, mpt[0]); |
| 171 | } |
| 172 | |
| 173 | TYPED_TEST(IntersectionTest, MultipointMultilinestring) { |
| 174 | typename TypeParam::Multipoint mpt = simple_mpt<TypeParam>(); |
| 175 | typename TypeParam::Multilinestring mls = simple_mls<TypeParam>(); |
| 176 | |
| 177 | mpt.push_back(typename TypeParam::Point(0.1, 0.1)); |
| 178 | |
| 179 | this->test_valid_input(mpt, mls, mpt[0]); |
| 180 | this->test_valid_input(mls, mpt, mpt[0]); |
| 181 | } |
| 182 | |
| 183 | TYPED_TEST(IntersectionTest, MultipointPolygon) { |
| 184 | typename TypeParam::Multipoint mpt = simple_mpt<TypeParam>(); |
| 185 | typename TypeParam::Polygon py = base_py<TypeParam>(); |
| 186 | |
| 187 | mpt.push_back(typename TypeParam::Point(0.0, 0.2)); |
| 188 | |
| 189 | this->test_valid_input(mpt, py, mpt[0]); |
| 190 | this->test_valid_input(py, mpt, mpt[0]); |
| 191 | } |
| 192 | |
| 193 | TYPED_TEST(IntersectionTest, MultipointMultipolygon) { |
| 194 | typename TypeParam::Multipoint mpt = simple_mpt<TypeParam>(); |
| 195 | typename TypeParam::Multipolygon mpy = simple_mpy<TypeParam>(); |
| 196 | |
| 197 | mpt.push_back(typename TypeParam::Point(0.1, 0.2)); |
| 198 | |
| 199 | this->test_valid_input(mpt, mpy, mpt[0]); |
| 200 | this->test_valid_input(mpy, mpt, mpt[0]); |
| 201 | } |
| 202 | |
| 203 | // intersection(..., linestring, *, ...) |
| 204 | |
| 205 | TYPED_TEST(IntersectionTest, LinestringLinestring) { |
| 206 | typename TypeParam::Linestring ls1 = simple_ls<TypeParam>(); |
| 207 | typename TypeParam::Linestring ls2 = offset_simple_ls<TypeParam>(); |
| 208 | typename TypeParam::Linestring expected_result{}; |
| 209 | |
| 210 | expected_result.push_back(typename TypeParam::Point(0.05, 0.0)); |
| 211 | expected_result.push_back(typename TypeParam::Point(0.1, 0.0)); |
| 212 | |
| 213 | this->test_valid_input(ls1, ls1, ls1); |
| 214 | this->test_valid_input(ls1, ls2, expected_result); |
| 215 | } |
| 216 | |
| 217 | TYPED_TEST(IntersectionTest, LinestringMultilinestring) { |
| 218 | typename TypeParam::Multilinestring mls = simple_mls<TypeParam>(); |
| 219 | typename TypeParam::Linestring ls2 = offset_simple_ls<TypeParam>(); |
| 220 | typename TypeParam::Linestring expected_result{}; |
| 221 | |
| 222 | expected_result.push_back(typename TypeParam::Point(0.05, 0.0)); |
| 223 | expected_result.push_back(typename TypeParam::Point(0.1, 0.0)); |
| 224 | |
| 225 | this->test_valid_input(mls[0], mls, mls[0]); |
| 226 | this->test_valid_input(ls2, mls, expected_result); |
| 227 | } |
| 228 | |
| 229 | TYPED_TEST(IntersectionTest, LinestringPolygon) { |
| 230 | typename TypeParam::Linestring ls1 = diagonal_ls<TypeParam>(); |
| 231 | typename TypeParam::Polygon py = base_py<TypeParam>(); |
| 232 | |
| 233 | this->test_valid_input(ls1, py, ls1); |
| 234 | this->test_valid_input(py, ls1, ls1); |
| 235 | } |
| 236 | |
| 237 | TYPED_TEST(IntersectionTest, LinestringMultiPolygon) { |
| 238 | typename TypeParam::Linestring ls1 = diagonal_ls<TypeParam>(); |
| 239 | typename TypeParam::Multipolygon mpy = simple_mpy<TypeParam>(); |
| 240 | |
| 241 | this->test_valid_input(ls1, mpy, ls1); |
| 242 | this->test_valid_input(mpy, ls1, ls1); |
| 243 | } |
| 244 | |
| 245 | // intersection(..., multilinestring, *, ...) |
| 246 | |
| 247 | TYPED_TEST(IntersectionTest, MultilinestringMultilinestring) { |
| 248 | typename TypeParam::Multilinestring mls1 = simple_mls<TypeParam>(); |
| 249 | typename TypeParam::Multilinestring mls2{}; |
| 250 | typename TypeParam::Linestring expected_result{}; |
| 251 | |
| 252 | mls2.push_back(offset_simple_ls<TypeParam>()); |
| 253 | expected_result.push_back(typename TypeParam::Point{0.05, 0.0}); |
| 254 | expected_result.push_back(typename TypeParam::Point{0.1, 0.0}); |
| 255 | |
| 256 | this->test_valid_input(mls1, mls1, mls1[0]); |
| 257 | this->test_valid_input(mls1, mls2, expected_result); |
| 258 | } |
| 259 | |
| 260 | TYPED_TEST(IntersectionTest, MultilinestringPolygon) { |
| 261 | typename TypeParam::Multilinestring mls = simple_mls<TypeParam>(); |
| 262 | typename TypeParam::Polygon py = base_py<TypeParam>(); |
| 263 | |
| 264 | // mls.push_back(ls_overlapping_base_py<TypeParam>()); |
| 265 | |
| 266 | this->test_valid_input(mls, py, mls[0]); |
| 267 | this->test_valid_input(py, mls, mls[0]); |
| 268 | } |
| 269 | |
| 270 | TYPED_TEST(IntersectionTest, MultilinestringMultipolygon) { |
| 271 | typename TypeParam::Multilinestring mls = simple_mls<TypeParam>(); |
| 272 | typename TypeParam::Multipolygon mpy = simple_mpy<TypeParam>(); |
| 273 | |
| 274 | // mls.push_back(ls_overlapping_base_py<TypeParam>()); |
| 275 | |
| 276 | this->test_valid_input(mls, mpy, mls[0]); |
| 277 | this->test_valid_input(mpy, mls, mls[0]); |
| 278 | } |
| 279 | |
| 280 | // intersection(..., polygon, *, ...) |
| 281 | |
| 282 | TYPED_TEST(IntersectionTest, PolygonPolygon) { |
| 283 | typename TypeParam::Polygon py1 = base_py<TypeParam>(); |
| 284 | typename TypeParam::Polygon py2 = overlapping_py<TypeParam>(); |
| 285 | typename TypeParam::Linestring ls = vertical_ls<TypeParam>(); |
| 286 | |
| 287 | this->test_valid_input(py1, py1, py1); |
| 288 | this->test_valid_input(py1, py2, ls); |
| 289 | } |
| 290 | |
| 291 | TYPED_TEST(IntersectionTest, PolygonMultipolygon) { |
| 292 | typename TypeParam::Polygon py1 = base_py<TypeParam>(); |
| 293 | typename TypeParam::Polygon py2 = overlapping_py<TypeParam>(); |
| 294 | typename TypeParam::Multipolygon mpy{}; |
| 295 | typename TypeParam::Linestring ls = vertical_ls<TypeParam>(); |
| 296 | |
| 297 | mpy.push_back(py2); |
| 298 | |
| 299 | this->test_valid_input(py2, mpy, py2); |
| 300 | this->test_valid_input(mpy, py1, ls); |
| 301 | } |
| 302 | |
| 303 | // intersection(..., multipolygon, *, ...) |
| 304 | |
| 305 | TYPED_TEST(IntersectionTest, MultipolygonMultipolygon) { |
| 306 | typename TypeParam::Polygon py1 = base_py<TypeParam>(); |
| 307 | typename TypeParam::Polygon py2 = overlapping_py<TypeParam>(); |
| 308 | typename TypeParam::Polygon py3 = disjoint_py<TypeParam>(); |
| 309 | typename TypeParam::Multipolygon mpy1 = simple_mpy<TypeParam>(); |
| 310 | typename TypeParam::Multipolygon mpy2{}; |
| 311 | typename TypeParam::Linestring ls = vertical_ls<TypeParam>(); |
| 312 | |
| 313 | mpy2.push_back(py2); |
| 314 | mpy2.push_back(py3); |
| 315 | |
| 316 | this->test_valid_input(mpy1, mpy1, mpy1[0]); |
| 317 | this->test_valid_input(mpy1, mpy2, ls); |
| 318 | } |
| 319 | |
| 320 | // intersection(..., geometrycollection, *, ...) |
| 321 | |
| 322 | TYPED_TEST(IntersectionTest, GeometrycollectionPoint) { |
| 323 | typename TypeParam::Geometrycollection gc{}; |
| 324 | typename TypeParam::Geometrycollection empty_gc{}; |
| 325 | typename TypeParam::Point pt1{0, 0}; |
| 326 | typename TypeParam::Point pt2{0, 0.1}; |
| 327 | |
| 328 | gc.push_back(pt1); |
| 329 | |
| 330 | this->test_valid_input(gc, pt1, pt1); |
| 331 | this->test_valid_input(pt2, gc, empty_gc); |
| 332 | } |
| 333 | |
| 334 | TYPED_TEST(IntersectionTest, GeometrycollectionMultipoint) { |
| 335 | typename TypeParam::Geometrycollection gc{}; |
| 336 | typename TypeParam::Multipoint mpt = simple_mpt<TypeParam>(); |
| 337 | |
| 338 | gc.push_back(mpt[0]); |
| 339 | gc.push_back(typename TypeParam::Point(0.1, 0.1)); |
| 340 | |
| 341 | this->test_valid_input(mpt, gc, mpt[0]); |
| 342 | this->test_valid_input(gc, mpt, mpt[0]); |
| 343 | } |
| 344 | |
| 345 | TYPED_TEST(IntersectionTest, GeometrycollectionLinestring) { |
| 346 | typename TypeParam::Geometrycollection gc{}; |
| 347 | typename TypeParam::Linestring ls1 = simple_ls<TypeParam>(); |
| 348 | typename TypeParam::Linestring ls2 = diagonal_ls<TypeParam>(); |
| 349 | auto pt = typename TypeParam::Point(0.0, 0.0); |
| 350 | |
| 351 | gc.push_back(ls1); |
| 352 | |
| 353 | this->test_valid_input(ls1, gc, ls1); |
| 354 | this->test_valid_input(gc, ls2, pt); |
| 355 | } |
| 356 | |
| 357 | TYPED_TEST(IntersectionTest, GeometrycollectionMultilinestring) { |
| 358 | typename TypeParam::Geometrycollection gc{}; |
| 359 | typename TypeParam::Multilinestring mls = simple_mls<TypeParam>(); |
| 360 | typename TypeParam::Linestring ls2 = diagonal_ls<TypeParam>(); |
| 361 | auto pt = typename TypeParam::Point(0.0, 0.0); |
| 362 | |
| 363 | gc.push_back(ls2); |
| 364 | |
| 365 | this->test_valid_input(mls, gc, pt); |
| 366 | this->test_valid_input(gc, mls, pt); |
| 367 | } |
| 368 | |
| 369 | TYPED_TEST(IntersectionTest, GeometrycollectionPolygon) { |
| 370 | typename TypeParam::Geometrycollection gc{}; |
| 371 | typename TypeParam::Polygon py1 = base_py<TypeParam>(); |
| 372 | typename TypeParam::Polygon py2 = disjoint_py<TypeParam>(); |
| 373 | typename TypeParam::Geometrycollection empty_gc{}; |
| 374 | |
| 375 | gc.push_back(py1); |
| 376 | |
| 377 | this->test_valid_input(py1, gc, py1); |
| 378 | this->test_valid_input(gc, py2, empty_gc); |
| 379 | } |
| 380 | |
| 381 | TYPED_TEST(IntersectionTest, GeometrycollectionMultipolygon) { |
| 382 | typename TypeParam::Geometrycollection gc{}; |
| 383 | typename TypeParam::Multipolygon mpy = simple_mpy<TypeParam>(); |
| 384 | typename TypeParam::Polygon py2 = disjoint_py<TypeParam>(); |
| 385 | typename TypeParam::Geometrycollection empty_gc{}; |
| 386 | |
| 387 | gc.push_back(py2); |
| 388 | |
| 389 | this->test_valid_input(mpy, gc, empty_gc); |
| 390 | this->test_valid_input(gc, mpy, empty_gc); |
| 391 | } |
| 392 | |
| 393 | TYPED_TEST(IntersectionTest, GeometrycollectionGeometrycollection) { |
| 394 | typename TypeParam::Geometrycollection gc1{}; |
| 395 | typename TypeParam::Geometrycollection gc2{}; |
| 396 | typename TypeParam::Geometrycollection gc_result{}; |
| 397 | this->test_valid_input(gc1, gc1, gc1); |
| 398 | |
| 399 | typename TypeParam::Point pt{0, 0}; |
| 400 | typename TypeParam::Linestring ls = ls_overlapping_base_py<TypeParam>(); |
| 401 | typename TypeParam::Polygon py = overlapping_py<TypeParam>(); |
| 402 | typename TypeParam::Multipoint mpt = simple_mpt<TypeParam>(); |
| 403 | typename TypeParam::Multilinestring mls = simple_mls<TypeParam>(); |
| 404 | typename TypeParam::Multipolygon mpy = simple_mpy<TypeParam>(); |
| 405 | gc1.push_back(pt); |
| 406 | gc1.push_back(ls); |
| 407 | gc1.push_back(py); |
| 408 | gc1.push_back(mpt); |
| 409 | gc1.push_back(mls); |
| 410 | gc1.push_back(mpy); |
| 411 | gc1.push_back(gc1); |
| 412 | |
| 413 | gc2.push_back(mpy); |
| 414 | gc2.push_back(pt); |
| 415 | |
| 416 | gc_result.push_back(pt); |
| 417 | gc_result.push_back(mpy); |
| 418 | |
| 419 | this->test_valid_input(gc1, gc2, gc_result); |
| 420 | } |
| 421 | |
| 422 | } // namespace gis_intersectionunittest |