| 1 | /* |
| 2 | Copyright (c) 2017, 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 <initializer_list> |
| 28 | #include "mysqld_error.h" |
| 29 | #include "sql/dd/dd.h" |
| 30 | #include "sql/dd/impl/types/spatial_reference_system_impl.h" |
| 31 | #include "sql/dd/types/spatial_reference_system.h" |
| 32 | #include "sql/gis/geometries.h" |
| 33 | #include "sql/gis/geometries_cs.h" |
| 34 | #include "sql/gis/hausdorff_distance.h" |
| 35 | #include "sql/gis/st_units_of_measure.h" |
| 36 | #include "unittest/gunit/test_utils.h" |
| 37 | |
| 38 | namespace hausdorff_distance_unittest { |
| 39 | |
| 40 | auto GetCartesianSrs() { |
| 41 | // Cartesian SRS. |
| 42 | return std::unique_ptr<dd::Spatial_reference_system_impl>(); |
| 43 | } |
| 44 | |
| 45 | auto GetGeographicalSrs() { |
| 46 | // EPSG 4326, but with long-lat axes (E-N). |
| 47 | std::unique_ptr<dd::Spatial_reference_system_impl> srs( |
| 48 | dynamic_cast<dd::Spatial_reference_system_impl *>( |
| 49 | dd::create_object<dd::Spatial_reference_system>())); |
| 50 | srs->set_id(4326); |
| 51 | srs->set_name("WGS 84"); |
| 52 | srs->set_created(0UL); |
| 53 | srs->set_last_altered(0UL); |
| 54 | srs->set_organization("EPSG"); |
| 55 | srs->set_organization_coordsys_id(4326); |
| 56 | srs->set_definition( |
| 57 | "GEOGCS[\"WGS 84\",DATUM[\"World Geodetic System " |
| 58 | "1984\",SPHEROID[\"WGS " |
| 59 | "84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]]," |
| 60 | "AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[" |
| 61 | "\"EPSG\",\"8901\"]],UNIT[\"degree\",0.017453292519943278," |
| 62 | "AUTHORITY[\"EPSG\",\"9122\"]],AXIS[\"Lon\",EAST],AXIS[\"Lat\"," |
| 63 | "NORTH],AUTHORITY[\"EPSG\",\"4326\"]]"); |
| 64 | srs->set_description(""); |
| 65 | srs->parse_definition(); |
| 66 | return srs; |
| 67 | } |
| 68 | |
| 69 | auto GetGeographicalSrsDiffFlat() { |
| 70 | // EPSG 4326, but with long-lat axes (E-N) and different flattening. |
| 71 | std::unique_ptr<dd::Spatial_reference_system_impl> srs( |
| 72 | dynamic_cast<dd::Spatial_reference_system_impl *>( |
| 73 | dd::create_object<dd::Spatial_reference_system>())); |
| 74 | srs->set_id(4326); |
| 75 | srs->set_name("WGS 84"); |
| 76 | srs->set_created(0UL); |
| 77 | srs->set_last_altered(0UL); |
| 78 | srs->set_organization("EPSG"); |
| 79 | srs->set_organization_coordsys_id(4326); |
| 80 | srs->set_definition( |
| 81 | "GEOGCS[\"WGS 84\",DATUM[\"World Geodetic System " |
| 82 | "1984\",SPHEROID[\"WGS " |
| 83 | "84\",6378137,290,AUTHORITY[\"EPSG\",\"7030\"]]," |
| 84 | "AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[" |
| 85 | "\"EPSG\",\"8901\"]],UNIT[\"degree\",0.017453292519943278," |
| 86 | "AUTHORITY[\"EPSG\",\"9122\"]],AXIS[\"Lon\",EAST],AXIS[\"Lat\"," |
| 87 | "NORTH],AUTHORITY[\"EPSG\",\"4326\"]]"); |
| 88 | srs->set_description(""); |
| 89 | srs->parse_definition(); |
| 90 | return srs; |
| 91 | } |
| 92 | |
| 93 | template <typename Point, typename Linestring> |
| 94 | auto MakeLinestring(const std::initializer_list<double> &data) { |
| 95 | if (data.size() % 2 != 0) { |
| 96 | throw std::exception(); /* purecov: dead code */ |
| 97 | } |
| 98 | Linestring linestring; |
| 99 | for (auto it = data.begin(); it != data.end(); std::advance(it, 2)) { |
| 100 | linestring.push_back(Point(*it, *(it + 1))); |
| 101 | } |
| 102 | return linestring; |
| 103 | } |
| 104 | |
| 105 | template <typename Point, typename MultiPoint> |
| 106 | auto MakeMultipoint( |
| 107 | const std::initializer_list<std::initializer_list<double>> &data) { |
| 108 | MultiPoint multipoint; |
| 109 | for (auto point : data) { |
| 110 | EXPECT_EQ(point.size(), 2); |
| 111 | multipoint.push_back(Point(*point.begin(), *(point.begin() + 1))); |
| 112 | } |
| 113 | return multipoint; |
| 114 | } |
| 115 | |
| 116 | template <typename SRS> |
| 117 | double TestHausdorffDistance(const gis::Geometry &g1, const gis::Geometry &g2, |
| 118 | SRS srs, bool *null_value = nullptr) { |
| 119 | double hausdorff_distance = 0.0; |
| 120 | if (null_value != nullptr) { |
| 121 | bool res = gis::hausdorff_distance(srs, &g1, &g2, "testcase", |
| 122 | &hausdorff_distance, null_value); |
| 123 | EXPECT_FALSE(res); |
| 124 | } else { |
| 125 | bool is_null = false; |
| 126 | bool res = gis::hausdorff_distance(srs, &g1, &g2, "testcase", |
| 127 | &hausdorff_distance, &is_null); |
| 128 | EXPECT_FALSE(res); |
| 129 | EXPECT_FALSE(is_null); |
| 130 | } |
| 131 | return hausdorff_distance; |
| 132 | } |
| 133 | |
| 134 | TEST(HausdorffDistanceUnitTest, LinestringLinestring) { |
| 135 | auto gl1 = MakeLinestring<gis::Geographic_point, gis::Geographic_linestring>( |
| 136 | {-1., 0.5, -.1, 0.5}); |
| 137 | auto gl2 = MakeLinestring<gis::Geographic_point, gis::Geographic_linestring>( |
| 138 | {-1., 0.5, .1, 0.5}); |
| 139 | auto srs1 = GetGeographicalSrs(); |
| 140 | // 32bit may compute distance as 1119900.6074340444 |
| 141 | EXPECT_NEAR(TestHausdorffDistance(gl1, gl2, srs1.get()), 1119900.6074340483, |
| 142 | 1e-8); |
| 143 | auto srs2 = GetGeographicalSrsDiffFlat(); |
| 144 | // 32bit may compute distance as 1119925.1618088416 |
| 145 | EXPECT_NEAR(TestHausdorffDistance(gl1, gl2, srs2.get()), 1119925.1618088456, |
| 146 | 1e-8); |
| 147 | auto cl1 = MakeLinestring<gis::Cartesian_point, gis::Cartesian_linestring>( |
| 148 | {-1., 0.5, -.1, 0.5}); |
| 149 | auto cl2 = MakeLinestring<gis::Cartesian_point, gis::Cartesian_linestring>( |
| 150 | {-1., 0.5, .1, 0.5}); |
| 151 | auto srs3 = GetCartesianSrs(); |
| 152 | EXPECT_DOUBLE_EQ(TestHausdorffDistance(cl1, cl2, srs3.get()), 0.2); |
| 153 | } |
| 154 | |
| 155 | TEST(HausdorffDistanceUnitTest, PointMultipoint) { |
| 156 | gis::Geographic_point gp{0.0, 0.0}; |
| 157 | auto gmp = MakeMultipoint<gis::Geographic_point, gis::Geographic_multipoint>( |
| 158 | {{0, 1}, {1, 0}, {1, 1}, {1, 2}, {1, 3}}); |
| 159 | auto srs1 = GetGeographicalSrs(); |
| 160 | EXPECT_DOUBLE_EQ(TestHausdorffDistance(gp, gmp, srs1.get()), |
| 161 | 6352860.8773248382); |
| 162 | EXPECT_DOUBLE_EQ(TestHausdorffDistance(gmp, gp, srs1.get()), |
| 163 | 6352860.8773248382); |
| 164 | |
| 165 | gis::Cartesian_point cp{0.0, 0.0}; |
| 166 | auto cmp = MakeMultipoint<gis::Cartesian_point, gis::Cartesian_multipoint>( |
| 167 | {{0, 1}, {1, 0}, {1, 1}, {1, 2}, {1, 3}}); |
| 168 | auto srs2 = GetCartesianSrs(); |
| 169 | EXPECT_DOUBLE_EQ(TestHausdorffDistance(cp, cmp, srs2.get()), 1); |
| 170 | EXPECT_DOUBLE_EQ(TestHausdorffDistance(cmp, cp, srs2.get()), 1); |
| 171 | } |
| 172 | |
| 173 | TEST(HausdorffDistanceUnitTest, MultipointMultipoint) { |
| 174 | auto gmp1 = MakeMultipoint<gis::Geographic_point, gis::Geographic_multipoint>( |
| 175 | {{0, 0}, {1, 1}, {2, 1}, {1, 2}, {2, 2}}); |
| 176 | auto gmp2 = MakeMultipoint<gis::Geographic_point, gis::Geographic_multipoint>( |
| 177 | {{0, 1}, {1, 0}, {1, 1}, {1, 2}, {1, 3}}); |
| 178 | auto srs1 = GetGeographicalSrs(); |
| 179 | EXPECT_DOUBLE_EQ(TestHausdorffDistance(gmp1, gmp2, srs1.get()), |
| 180 | 6352860.8773248382); |
| 181 | |
| 182 | auto cmp1 = MakeMultipoint<gis::Cartesian_point, gis::Cartesian_multipoint>( |
| 183 | {{0, 0}, {1, 1}, {2, 1}, {1, 2}, {2, 2}}); |
| 184 | auto cmp2 = MakeMultipoint<gis::Cartesian_point, gis::Cartesian_multipoint>( |
| 185 | {{0, 1}, {1, 0}, {1, 1}, {1, 2}, {1, 3}}); |
| 186 | auto srs2 = GetCartesianSrs(); |
| 187 | EXPECT_DOUBLE_EQ(TestHausdorffDistance(cmp1, cmp2, srs2.get()), 1); |
| 188 | } |
| 189 | |
| 190 | TEST(HausdorffDistanceUnitTest, MultilinestringMultilinestring) { |
| 191 | gis::Geographic_multilinestring gml1; |
| 192 | gml1.push_back( |
| 193 | MakeLinestring<gis::Geographic_point, gis::Geographic_linestring>( |
| 194 | {0, 0, 1, 1, 2, 1, 1, 2, 2, 2})); |
| 195 | gis::Geographic_multilinestring gml2; |
| 196 | gml2.push_back( |
| 197 | MakeLinestring<gis::Geographic_point, gis::Geographic_linestring>( |
| 198 | {0, 1, 1, 0, 1, 1, 1, 2, 1, 3})); |
| 199 | |
| 200 | auto srs1 = GetGeographicalSrs(); |
| 201 | EXPECT_DOUBLE_EQ(TestHausdorffDistance(gml1, gml2, srs1.get()), |
| 202 | 6352860.8773248382); |
| 203 | |
| 204 | gis::Cartesian_multilinestring cml1; |
| 205 | cml1.push_back( |
| 206 | MakeLinestring<gis::Cartesian_point, gis::Cartesian_linestring>( |
| 207 | {0, 0, 1, 1, 2, 1, 1, 2, 2, 2})); |
| 208 | gis::Cartesian_multilinestring cml2; |
| 209 | cml2.push_back( |
| 210 | MakeLinestring<gis::Cartesian_point, gis::Cartesian_linestring>( |
| 211 | {0, 1, 1, 0, 1, 1, 1, 2, 1, 3})); |
| 212 | |
| 213 | auto srs2 = GetCartesianSrs(); |
| 214 | EXPECT_DOUBLE_EQ(TestHausdorffDistance(cml1, cml2, srs2.get()), 1); |
| 215 | } |
| 216 | |
| 217 | TEST(HausdorffDistanceUnitTest, LinestringMultilinestring) { |
| 218 | auto gl = MakeLinestring<gis::Geographic_point, gis::Geographic_linestring>( |
| 219 | {0, 0, 1, 1, 2, 1, 1, 2, 2, 2}); |
| 220 | gis::Geographic_multilinestring gml; |
| 221 | gml.push_back( |
| 222 | MakeLinestring<gis::Geographic_point, gis::Geographic_linestring>( |
| 223 | {0, 1, 1, 0, 1, 1, 1, 2, 1, 3})); |
| 224 | |
| 225 | auto srs1 = GetGeographicalSrs(); |
| 226 | EXPECT_DOUBLE_EQ(TestHausdorffDistance(gl, gml, srs1.get()), |
| 227 | 6352860.8773248382); |
| 228 | EXPECT_DOUBLE_EQ(TestHausdorffDistance(gml, gl, srs1.get()), |
| 229 | 6352860.8773248382); |
| 230 | |
| 231 | auto cl = MakeLinestring<gis::Cartesian_point, gis::Cartesian_linestring>( |
| 232 | {0, 0, 1, 1, 2, 1, 1, 2, 2, 2}); |
| 233 | gis::Cartesian_multilinestring cml; |
| 234 | cml.push_back(MakeLinestring<gis::Cartesian_point, gis::Cartesian_linestring>( |
| 235 | {0, 1, 1, 0, 1, 1, 1, 2, 1, 3})); |
| 236 | |
| 237 | auto srs2 = GetCartesianSrs(); |
| 238 | EXPECT_DOUBLE_EQ(TestHausdorffDistance(cl, cml, srs2.get()), 1); |
| 239 | EXPECT_DOUBLE_EQ(TestHausdorffDistance(cml, cl, srs2.get()), 1); |
| 240 | } |
| 241 | |
| 242 | } // namespace hausdorff_distance_unittest |