MySQL 9.0.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, 2024, 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
82///
83/// For both MBRs, the coordinates of the MBR's minimum corners must be smaller
84/// than or equal to the corresponding coordinates of the maximum corner.
85///
86/// @param[in] srs Spatial reference system.
87/// @param[in] a The first MBR.
88/// @param[in] b The second MBR.
89///
90/// @retval true The two MBRs are equal.
91/// @retval false The two MBRs aren't equal.
93 rtr_mbr_t *b);
94
95/// Checks if two MBRs intersect each other
96///
97/// @param[in] srs Spatial reference system.
98/// @param[in] a The first MBR.
99/// @param[in] b The second MBR.
100///
101/// For both MBRs, the coordinates of the MBR's minimum corners must be smaller
102/// than or equal to the corresponding coordinates of the maximum corner.
103///
104/// @retval true The MBRs intersect each other.
105/// @retval false The MBRs are disjoint.
107 rtr_mbr_t *b);
108
109/// Checks if two MBRs are disjoint
110///
111/// For both MBRs, the coordinates of the MBR's minimum corners must be smaller
112/// than or equal to the corresponding coordinates of the maximum corner.
113///
114/// @retval true The MBRs are disjoint.
115/// @retval false The MBRs intersect each other.
117 rtr_mbr_t *b);
118
119/// Checks if one MBR is covered by another MBR.
120///
121/// @warning Despite the name, this function computes the covered_by relation,
122/// not within.
123///
124/// @note If the minimum corner coordinates are larger than the corresponding
125/// coordinates of the maximum corner, and if not all a and b coordinates are
126/// the same, the function returns the inverse result, i.e., return true if a is
127/// not covered by b.
128///
129/// @param[in] srs Spatial reference system.
130/// @param[in] a The first MBR.
131/// @param[in] b The second MBR.
132///
133/// @retval true MBR a is within MBR b.
134/// @retval false MBR a isn't within MBR b.
136 rtr_mbr_t *b);
137
138/// Expands an MBR to also cover another MBR.
139///
140/// @note The function takes a dimension parameter, but currently only supports
141/// 2d MBRs.
142///
143/// MBR format: a[0] = xmin, a[1] = xmax, a[2] = ymin, a[3] = ymax. Same for b.
144///
145/// @param[in] srs Spatial reference system.
146/// @param[in,out] a The first MBR, where the joined result will be.
147/// @param[in] b The second MBR.
148/// @param[in] n_dim Number of dimensions. Must be 2.
149void mbr_join(const dd::Spatial_reference_system *srs, double *a,
150 const double *b, int n_dim);
151
152/// Computes the combined area of two MBRs.
153///
154/// The MBRs may overlap.
155///
156/// @note The function takes a dimension parameter, but currently only supports
157/// 2d MBRs.
158///
159/// @param[in] srs Spatial reference system.
160/// @param[in] a The first MBR.
161/// @param[in] b The second MBR.
162/// @param[in] n_dim Number of dimensions. Must be 2.
163///
164/// @return The area of MBR a expanded by MBR b.
165double mbr_join_area(const dd::Spatial_reference_system *srs, const double *a,
166 const double *b, int n_dim);
167
168/// Computes the area of an MBR.
169///
170/// @note The function takes a dimension parameter, but currently only supports
171/// 2d MBRs.
172///
173/// @param[in] srs Spatial reference system.
174/// @param[in] a The MBR.
175/// @param[in] n_dim Number of dimensions. Must be 2.
176///
177/// @return Are of the MBR.
178double compute_area(const dd::Spatial_reference_system *srs, const double *a,
179 int n_dim);
180
181/// Computes the MBR of a geometry.
182///
183/// If the geometry is empty, a box that covers the entire domain is returned.
184///
185/// The geometry is expected to be on the storage format (SRID + WKB). The
186/// caller is expected to provide an output buffer that is large enough.
187///
188/// @note The function takes a dimension parameter, but currently only supports
189/// 2d MBRs.
190///
191/// The SRID of the SRS parameter must match the SRID stored in the first four
192/// bytes of the geometry string.
193///
194/// @param[in] srs Spatial reference system.
195/// @param[in] store The geometry.
196/// @param[in] size Number of bytes in the geometry string.
197/// @param[in] n_dims Number of dimensions. Must be 2.
198/// @param[out] mbr The computed MBR.
199/// @param[out] srid SRID of the geometry
200///
201/// @retval 0 The geometry is valid.
202/// @retval -1 The geometry is invalid.
204 const uchar *store, uint size, uint n_dims, double *mbr,
205 gis::srid_t *srid);
206
207/// Computes the extra area covered if an MBR is expanded to cover another MBR.
208///
209/// The formula is area(a + b) - area(a). This is generally different from
210/// area(b). If MBR b overlaps MBR a, area(a+b) - area(a) < area(b). If they are
211/// far apart, area(a+b) - area(a) > area(b).
212///
213/// @note If MBRs a and b are far apart, the function may return Inf.
214///
215/// @param[in] srs Spatial reference system.
216/// @param[in] mbr_a First MBR.
217/// @param[in] mbr_b Second MBR.
218/// @param[in] mbr_len MBR length in bytes. Must be 4 * sizeof(double).
219/// @param[out] ab_area The total area of MBRs a and b combined into one MBR.
220///
221/// @return The increase in area when expanding from MBR a to also cover MBR b.
223 const uchar *mbr_a, const uchar *mbr_b, int mbr_len,
224 double *ab_area);
225
226/// Calculates the overlapping area between two MBRs.
227///
228/// @param[in] srs Spatial reference system.
229/// @param[in] mbr_a First MBR.
230/// @param[in] mbr_b Second MBR.
231/// @param[in] mbr_len MBR length in bytes. Must be 4 * sizeof(double).
232///
233/// @return The area of the overlapping region.
235 const uchar *mbr_a, const uchar *mbr_b,
236 int mbr_len);
237
238#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:421
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:178
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:294
bool mbr_equal_cmp(const dd::Spatial_reference_system *srs, rtr_mbr_t *a, rtr_mbr_t *b)
Checks if two MBRs are equal.
Definition: rtree_support.cc:109
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:354
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:475
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:209
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:147
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:328
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:260
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