MySQL 9.3.0
Source Code Documentation
rtree_support.h
Go to the documentation of this file.
1#ifndef SQL_GIS_RTREE_SUPPORT_H_INCLUDED
2#define SQL_GIS_RTREE_SUPPORT_H_INCLUDED
3
4// Copyright (c) 2017, 2025, Oracle and/or its affiliates.
5//
6// This program is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License, version 2.0,
8// as published by the Free Software Foundation.
9//
10// This program is designed to work with certain software (including
11// but not limited to OpenSSL) that is licensed under separate terms,
12// as designated in a particular file or component or in included license
13// documentation. The authors of MySQL hereby grant you an additional
14// permission to link the program and your derivative works with the
15// separately licensed software that they have either included with
16// the program or referenced in the documentation.
17//
18// This program is distributed in the hope that it will be useful,
19// but WITHOUT ANY WARRANTY; without even the implied warranty of
20// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21// GNU General Public License, version 2.0, for more details.
22//
23// You should have received a copy of the GNU General Public License
24// along with this program; if not, write to the Free Software
25// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26
27/// @file
28///
29/// This file declares a set of functions that storage engines can call to do
30/// geometrical operations.
31
32#include "my_inttypes.h" // uchar, uint
33#include "sql/gis/srid.h"
34
35namespace dd {
36class Spatial_reference_system;
37}
38
39/// In memory representation of a minimum bounding rectangle
40typedef struct rtr_mbr {
41 /// minimum on x
42 double xmin;
43 /// maximum on x
44 double xmax;
45 /// minimum on y
46 double ymin;
47 /// maximum on y
48 double ymax;
50
51/// Fetches a copy of the dictionary entry for a spatial reference system.
52///
53/// Spatial reference dictionary cache objects have a limited lifetime,
54/// typically until the end of a transaction. This function returns a clone of
55/// the dictionary object so that it is valid also after the transaction has
56/// ended. This is necessary since InnoDB may do index operations after the
57/// transaction has ended.
58///
59/// @param[in] srid The spatial reference system ID to look up.
60///
61/// @return The spatial reference system dictionary entry, or nullptr.
63
64/// Checks if one MBR covers another MBR.
65///
66/// @warning Despite the name, this function computes the covers relation, not
67/// contains.
68///
69/// For both MBRs, the coordinates of the MBR's minimum corners must be smaller
70/// than or equal to the corresponding coordinates of the maximum corner.
71///
72/// @param[in] srs Spatial reference system.
73/// @param[in] a The first MBR.
74/// @param[in] b The second MBR.
75///
76/// @retval true MBR a contains MBR b.
77/// @retval false MBR a doesn't contain MBR b.
79 rtr_mbr_t *b);
80
81/// Checks if two MBRs are equal physically
82///
83/// There is another function mbr_equal_logically(), which checks for
84/// equality in logical sense.
85///
86/// If the spatial index was created in version before 8.0, there is possibility
87/// that MBR can have format of {x,y}min = DBL_MAX, {x,y}max= -DBL_MAX
88///
89/// @param[in] a The first MBR.
90/// @param[in] b The second MBR.
91///
92/// @retval true The two MBRs are equal physically.
93/// @retval false The two MBRs aren't equal physically.
95
96/// Checks if two MBRs are equal logically
97///
98/// Comparison is epsilon based using boost geometry.
99///
100/// There is another function mbr_equal_physically(), which checks for
101/// equality in physical sense.
102///
103/// For both MBRs, the coordinates of the MBR's minimum corners must be smaller
104/// than or equal to the corresponding coordinates of the maximum corner.
105///
106/// @param[in] srs Spatial reference system.
107/// @param[in] a The first MBR.
108/// @param[in] b The second MBR.
109///
110/// @retval true The two MBRs are equal logically.
111/// @retval false The two MBRs aren't equal logically.
113 rtr_mbr_t *b);
114
115/// Checks if two MBRs intersect each other
116///
117/// @param[in] srs Spatial reference system.
118/// @param[in] a The first MBR.
119/// @param[in] b The second MBR.
120///
121/// For both MBRs, the coordinates of the MBR's minimum corners must be smaller
122/// than or equal to the corresponding coordinates of the maximum corner.
123///
124/// @retval true The MBRs intersect each other.
125/// @retval false The MBRs are disjoint.
127 rtr_mbr_t *b);
128
129/// Checks if two MBRs are disjoint
130///
131/// For both MBRs, the coordinates of the MBR's minimum corners must be smaller
132/// than or equal to the corresponding coordinates of the maximum corner.
133///
134/// @retval true The MBRs are disjoint.
135/// @retval false The MBRs intersect each other.
137 rtr_mbr_t *b);
138
139/// Checks if one MBR is covered by another MBR.
140///
141/// @warning Despite the name, this function computes the covered_by relation,
142/// not within.
143///
144/// @note If for `a` the minimum corner coordinates are larger than the
145/// corresponding coordinates of the maximum corner, we return true.
146///
147/// @param[in] srs Spatial reference system.
148/// @param[in] a The first MBR.
149/// @param[in] b The second MBR.
150///
151/// @retval true MBR a is within MBR b.
152/// @retval false MBR a isn't within MBR b.
154 rtr_mbr_t *b);
155
156/// Expands an MBR to also cover another MBR.
157///
158/// @note The function takes a dimension parameter, but currently only supports
159/// 2d MBRs.
160///
161/// MBR format: a[0] = xmin, a[1] = xmax, a[2] = ymin, a[3] = ymax. Same for b.
162///
163/// @param[in] srs Spatial reference system.
164/// @param[in,out] a The first MBR, where the joined result will be.
165/// @param[in] b The second MBR.
166/// @param[in] n_dim Number of dimensions. Must be 2.
167void mbr_join(const dd::Spatial_reference_system *srs, double *a,
168 const double *b, int n_dim);
169
170/// Computes the combined area of two MBRs.
171///
172/// The MBRs may overlap.
173///
174/// @note The function takes a dimension parameter, but currently only supports
175/// 2d MBRs.
176///
177/// @param[in] srs Spatial reference system.
178/// @param[in] a The first MBR.
179/// @param[in] b The second MBR.
180/// @param[in] n_dim Number of dimensions. Must be 2.
181///
182/// @return The area of MBR a expanded by MBR b.
183double mbr_join_area(const dd::Spatial_reference_system *srs, const double *a,
184 const double *b, int n_dim);
185
186/// Computes the area of an MBR.
187///
188/// @note The function takes a dimension parameter, but currently only supports
189/// 2d MBRs.
190///
191/// @param[in] srs Spatial reference system.
192/// @param[in] a The MBR.
193/// @param[in] n_dim Number of dimensions. Must be 2.
194///
195/// @return Are of the MBR.
196double compute_area(const dd::Spatial_reference_system *srs, const double *a,
197 int n_dim);
198
199/// Computes the MBR of a geometry.
200///
201/// If the geometry is empty, a box that covers the entire domain is returned.
202///
203/// The geometry is expected to be on the storage format (SRID + WKB). The
204/// caller is expected to provide an output buffer that is large enough.
205///
206/// @note The function takes a dimension parameter, but currently only supports
207/// 2d MBRs.
208///
209/// The SRID of the SRS parameter must match the SRID stored in the first four
210/// bytes of the geometry string.
211///
212/// @param[in] srs Spatial reference system.
213/// @param[in] store The geometry.
214/// @param[in] size Number of bytes in the geometry string.
215/// @param[in] n_dims Number of dimensions. Must be 2.
216/// @param[out] mbr The computed MBR.
217/// @param[out] srid SRID of the geometry
218///
219/// @retval 0 The geometry is valid.
220/// @retval -1 The geometry is invalid.
222 const uchar *store, uint size, uint n_dims, double *mbr,
223 gis::srid_t *srid);
224
225/// Computes the extra area covered if an MBR is expanded to cover another MBR.
226///
227/// The formula is area(a + b) - area(a). This is generally different from
228/// area(b). If MBR b overlaps MBR a, area(a+b) - area(a) < area(b). If they are
229/// far apart, area(a+b) - area(a) > area(b).
230///
231/// @note If MBRs a and b are far apart, the function may return Inf.
232///
233/// @param[in] srs Spatial reference system.
234/// @param[in] mbr_a First MBR.
235/// @param[in] mbr_b Second MBR.
236/// @param[in] mbr_len MBR length in bytes. Must be 4 * sizeof(double).
237/// @param[out] ab_area The total area of MBRs a and b combined into one MBR.
238///
239/// @return The increase in area when expanding from MBR a to also cover MBR b.
241 const uchar *mbr_a, const uchar *mbr_b, int mbr_len,
242 double *ab_area);
243
244/// Calculates the overlapping area between two MBRs.
245///
246/// @param[in] srs Spatial reference system.
247/// @param[in] mbr_a First MBR.
248/// @param[in] mbr_b Second MBR.
249/// @param[in] mbr_len MBR length in bytes. Must be 4 * sizeof(double).
250///
251/// @return The area of the overlapping region.
253 const uchar *mbr_a, const uchar *mbr_b,
254 int mbr_len);
255
256#endif // SQL_GIS_RTREE_SUPPORT_H_INCLUDED
Definition: spatial_reference_system.h:53
bool store(THD *thd, const Table *tp)
Stores the SDI for a table.
Definition: sdi.cc:607
Some integer typedefs for easier portability.
unsigned char uchar
Definition: my_inttypes.h:52
The version of the current data dictionary table definitions.
Definition: dictionary_client.h:43
std::uint32_t srid_t
A spatial reference system ID (SRID).
Definition: srid.h:33
size_t size(const char *const c)
Definition: base64.h:46
double rtree_area_increase(const dd::Spatial_reference_system *srs, const uchar *mbr_a, const uchar *mbr_b, int mbr_len, double *ab_area)
Computes the extra area covered if an MBR is expanded to cover another MBR.
Definition: rtree_support.cc:496
bool mbr_equal_logically(const dd::Spatial_reference_system *srs, rtr_mbr_t *a, rtr_mbr_t *b)
Checks if two MBRs are equal logically.
Definition: rtree_support.cc:114
struct rtr_mbr rtr_mbr_t
In memory representation of a minimum bounding rectangle.
bool mbr_disjoint_cmp(const dd::Spatial_reference_system *srs, rtr_mbr_t *a, rtr_mbr_t *b)
Checks if two MBRs are disjoint.
Definition: rtree_support.cc:183
double mbr_join_area(const dd::Spatial_reference_system *srs, const double *a, const double *b, int n_dim)
Computes the combined area of two MBRs.
Definition: rtree_support.cc:369
int get_mbr_from_store(const dd::Spatial_reference_system *srs, const uchar *store, uint size, uint n_dims, double *mbr, gis::srid_t *srid)
Computes the MBR of a geometry.
Definition: rtree_support.cc:429
double rtree_area_overlapping(const dd::Spatial_reference_system *srs, const uchar *mbr_a, const uchar *mbr_b, int mbr_len)
Calculates the overlapping area between two MBRs.
Definition: rtree_support.cc:550
bool mbr_contain_cmp(const dd::Spatial_reference_system *srs, rtr_mbr_t *a, rtr_mbr_t *b)
Checks if one MBR covers another MBR.
Definition: rtree_support.cc:73
bool mbr_within_cmp(const dd::Spatial_reference_system *srs, rtr_mbr_t *a, rtr_mbr_t *b)
Checks if one MBR is covered by another MBR.
Definition: rtree_support.cc:214
bool mbr_intersect_cmp(const dd::Spatial_reference_system *srs, rtr_mbr_t *a, rtr_mbr_t *b)
Checks if two MBRs intersect each other.
Definition: rtree_support.cc:152
bool mbr_equal_physically(rtr_mbr_t *a, rtr_mbr_t *b)
Checks if two MBRs are equal physically.
Definition: rtree_support.cc:109
double compute_area(const dd::Spatial_reference_system *srs, const double *a, int n_dim)
Computes the area of an MBR.
Definition: rtree_support.cc:403
void mbr_join(const dd::Spatial_reference_system *srs, double *a, const double *b, int n_dim)
Expands an MBR to also cover another MBR.
Definition: rtree_support.cc:335
dd::Spatial_reference_system * fetch_srs(gis::srid_t srid)
Fetches a copy of the dictionary entry for a spatial reference system.
Definition: rtree_support.cc:60
In memory representation of a minimum bounding rectangle.
Definition: rtree_support.h:40
double xmin
minimum on x
Definition: rtree_support.h:42
double xmax
maximum on x
Definition: rtree_support.h:44
double ymin
minimum on y
Definition: rtree_support.h:46
double ymax
maximum on y
Definition: rtree_support.h:48