File: mbr_utils.cc

package info (click to toggle)
mysql-8.0 8.0.43-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,924 kB
  • sloc: cpp: 4,684,605; ansic: 412,450; pascal: 108,398; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; sh: 24,181; python: 21,816; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,076; makefile: 2,194; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (282 lines) | stat: -rw-r--r-- 10,726 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Copyright (c) 2017, 2025, Oracle and/or its affiliates.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License, version 2.0,
// as published by the Free Software Foundation.
//
// This program is designed to work with certain software (including
// but not limited to OpenSSL) that is licensed under separate terms,
// as designated in a particular file or component or in included license
// documentation.  The authors of MySQL hereby grant you an additional
// permission to link the program and your derivative works with the
// separately licensed software that they have either included with
// the program or referenced in the documentation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License, version 2.0, for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.

/// @file
///
/// This file implements the mbr_disjoint function.

#include "sql/gis/mbr_utils.h"

#include <cmath>  // std::isnan
#include <exception>

#include <boost/geometry.hpp>

#include "sql/dd/types/spatial_reference_system.h"  // dd::Spatial_reference_system
#include "sql/gis/box.h"
#include "sql/gis/box_traits.h"
#include "sql/gis/geometries.h"
#include "sql/gis/geometries_cs.h"
#include "sql/gis/geometries_traits.h"
#include "template_utils.h"  // down_cast

namespace bg = boost::geometry;

namespace gis {

bool mbrs_are_equal(Box const &mbr1, Box const &mbr2) {
  assert(mbr1.coordinate_system() == mbr2.coordinate_system());
  switch (mbr1.coordinate_system()) {
    case Coordinate_system::kCartesian:
      return bg::equals(*down_cast<const Cartesian_box *>(&mbr1),
                        *down_cast<const Cartesian_box *>(&mbr2));
    case Coordinate_system::kGeographic:
      return bg::equals(*down_cast<const Geographic_box *>(&mbr1),
                        *down_cast<const Geographic_box *>(&mbr2));
  }
  assert(false); /* purecov: inspected */
  return false;  /* purecov: inspected */
}

bool mbr_is_empty(Box const &mbr) {
  return std::isnan(mbr.min_corner().x()) && std::isnan(mbr.min_corner().y()) &&
         std::isnan(mbr.max_corner().x()) && std::isnan(mbr.max_corner().y());
}

bool mbr_is_point(Box const &mbr) {
  return mbr.min_corner().x() == mbr.max_corner().x() &&
         mbr.min_corner().y() == mbr.max_corner().y();
}

bool mbr_is_line(Box const &mbr) {
  return (mbr.min_corner().x() == mbr.max_corner().x()) !=
         (mbr.min_corner().y() == mbr.max_corner().y());
}

/// Merges a vector of Cartesian MBRs into one common MBR.
///
/// Since the coordinate system doesn't wrap, the order in which MBRs are
/// expanded doesn't matter.
///
/// @param[in] boxes Vector of MBRs to merge.
/// @param[out] mbr The resulting MBR.
static void merge_mbrs(const std::vector<Cartesian_box> &boxes,
                       Cartesian_box *mbr) {
  if (!boxes.empty()) *mbr = boxes[0];
  for (auto &box : boxes) bg::expand(*mbr, box);
}

/// Merges a vector of geographic MBRs into one common MBR.
///
/// The coordinate system wraps, so the MBRs must be expanded in the correct
/// order to avoid creating an MBR that is larger than necessary.
///
/// If the vector of boxes is empty, the result MBR is unchanged.
///
/// @param[in] boxes Vector of MBRs to merge.
/// @param[out] mbr The resulting MBR.
static void merge_mbrs(const std::vector<Geographic_box> &boxes,
                       Geographic_box *mbr) {
  if (!boxes.empty())
    bg::detail::envelope::envelope_range_of_boxes::apply(boxes, *mbr);
}

/// Computes the envelope of a Cartesian geometry.
///
/// The MBR returned may be a collapsed box.
///
/// @param[in] g The geometry.
/// @param[out] mbr The envelope of g.
static void cartesian_envelope(const Geometry *g, Cartesian_box *mbr) {
  switch (g->type()) {
    case Geometry_type::kPoint:
      bg::envelope(*down_cast<const Cartesian_point *>(g), *mbr);
      break;
    case Geometry_type::kLinestring:
      bg::envelope(*down_cast<const Cartesian_linestring *>(g), *mbr);
      break;
    case Geometry_type::kPolygon:
      bg::envelope(*down_cast<const Cartesian_polygon *>(g), *mbr);
      break;
    case Geometry_type::kGeometrycollection: {
      std::vector<Cartesian_box> boxes;
      Cartesian_box geom_mbr;
      for (auto geom : *down_cast<const Cartesian_geometrycollection *>(g)) {
        switch (geom->type()) {
          case Geometry_type::kPoint:
            bg::envelope(*down_cast<const Cartesian_point *>(geom), geom_mbr);
            break;
          case Geometry_type::kLinestring:
            bg::envelope(*down_cast<const Cartesian_linestring *>(geom),
                         geom_mbr);
            break;
          case Geometry_type::kPolygon:
            bg::envelope(*down_cast<const Cartesian_polygon *>(geom), geom_mbr);
            break;
          case Geometry_type::kGeometrycollection:
            cartesian_envelope(geom, &geom_mbr);
            break;
          case Geometry_type::kMultipoint:
            bg::envelope(*down_cast<const Cartesian_multipoint *>(geom),
                         geom_mbr);
            break;
          case Geometry_type::kMultilinestring:
            bg::envelope(*down_cast<const Cartesian_multilinestring *>(geom),
                         geom_mbr);
            break;
          case Geometry_type::kMultipolygon:
            bg::envelope(*down_cast<const Cartesian_multipolygon *>(geom),
                         geom_mbr);
            break;
          case Geometry_type::kGeometry:
            assert(false);
            throw std::exception();
        }

        // Cartesian_boxxes around empty geometries contain NaN in all
        // coordinates. If
        // passed to bg::expand, the result will be a box with all NaN
        // coordinates. Therefore, we skip empty boxes.
        if (!mbr_is_empty(geom_mbr)) boxes.push_back(geom_mbr);
      }
      merge_mbrs(boxes, mbr);
      break;
    }
    case Geometry_type::kMultipoint:
      bg::envelope(*down_cast<const Cartesian_multipoint *>(g), *mbr);
      break;
    case Geometry_type::kMultilinestring:
      bg::envelope(*down_cast<const Cartesian_multilinestring *>(g), *mbr);
      break;
    case Geometry_type::kMultipolygon:
      bg::envelope(*down_cast<const Cartesian_multipolygon *>(g), *mbr);
      break;
    case Geometry_type::kGeometry:
      assert(false);
      throw std::exception();
      break;
  }
}

/// Computes the envelope of a geographic geometry.
///
/// The MBR returned may be a collapsed box.
///
/// @param[in] g The geometry.
/// @param[in] semi_major Semi-major axis of ellipsoid.
/// @param[in] semi_minor Semi-minor axis of ellipsoid.
/// @param[out] mbr The envelope of g.
static void geographic_envelope(const Geometry *g, double semi_major,
                                double semi_minor, Geographic_box *mbr) {
  bg::strategy::envelope::geographic<bg::strategy::andoyer,
                                     bg::srs::spheroid<double>>
      strategy(bg::srs::spheroid<double>(semi_major, semi_minor));
  switch (g->type()) {
    case Geometry_type::kPoint:
      bg::envelope(*down_cast<const Geographic_point *>(g), *mbr);
      break;
    case Geometry_type::kLinestring:
      bg::envelope(*down_cast<const Geographic_linestring *>(g), *mbr,
                   strategy);
      break;
    case Geometry_type::kPolygon:
      bg::envelope(*down_cast<const Geographic_polygon *>(g), *mbr, strategy);
      break;
    case Geometry_type::kGeometrycollection: {
      std::vector<Geographic_box> boxes;
      Geographic_box geom_mbr;
      for (auto geom : *down_cast<const Geographic_geometrycollection *>(g)) {
        switch (geom->type()) {
          case Geometry_type::kPoint:
            bg::envelope(*down_cast<const Geographic_point *>(geom), geom_mbr);
            break;
          case Geometry_type::kLinestring:
            bg::envelope(*down_cast<const Geographic_linestring *>(geom),
                         geom_mbr, strategy);
            break;
          case Geometry_type::kPolygon:
            bg::envelope(*down_cast<const Geographic_polygon *>(geom), geom_mbr,
                         strategy);
            break;
          case Geometry_type::kGeometrycollection:
            geographic_envelope(geom, semi_major, semi_minor, &geom_mbr);
            break;
          case Geometry_type::kMultipoint:
            bg::envelope(*down_cast<const Geographic_multipoint *>(geom),
                         geom_mbr);
            break;
          case Geometry_type::kMultilinestring:
            bg::envelope(*down_cast<const Geographic_multilinestring *>(geom),
                         geom_mbr, strategy);
            break;
          case Geometry_type::kMultipolygon:
            bg::envelope(*down_cast<const Geographic_multipolygon *>(geom),
                         geom_mbr, strategy);
            break;
          case Geometry_type::kGeometry:
            assert(false);
            throw std::exception();
        }

        // Geographic_boxxes around empty geometries contain NaN in all
        // coordinates. If
        // passed to bg::expand, the result will be a box with all NaN
        // coordinates. Therefore, we skip empty boxes.
        if (!mbr_is_empty(geom_mbr)) boxes.push_back(geom_mbr);
      }
      merge_mbrs(boxes, mbr);
      break;
    }
    case Geometry_type::kMultipoint:
      bg::envelope(*down_cast<const Geographic_multipoint *>(g), *mbr);
      break;
    case Geometry_type::kMultilinestring:
      bg::envelope(*down_cast<const Geographic_multilinestring *>(g), *mbr,
                   strategy);
      break;
    case Geometry_type::kMultipolygon:
      bg::envelope(*down_cast<const Geographic_multipolygon *>(g), *mbr,
                   strategy);
      break;
    case Geometry_type::kGeometry:
      assert(false);
      throw std::exception();
      break;
  }
}

void box_envelope(const Geometry *g, const dd::Spatial_reference_system *srs,
                  Box *mbr) {
  switch (g->coordinate_system()) {
    case Coordinate_system::kCartesian:
      cartesian_envelope(g, down_cast<Cartesian_box *>(mbr));
      break;
    case Coordinate_system::kGeographic:
      geographic_envelope(g, srs ? srs->semi_major_axis() : 0.0,
                          srs ? srs->semi_minor_axis() : 0.0,
                          down_cast<Geographic_box *>(mbr));
      break;
  }
}

}  // namespace gis