MySQL 8.4.4
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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 physically
82///
83/// There is another function mbr_equal_logically(), which checks for
84/// equality in logical sense.
85///
86/// For both MBRs, the coordinates of the MBR's minimum corners must be smaller
87/// than or equal to the corresponding coordinates of the maximum corner.
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 the minimum corner coordinates are larger than the corresponding
145/// coordinates of the maximum corner, and if not all a and b coordinates are
146/// the same, the function returns the inverse result, i.e., return true if a is
147/// not covered by b.
148///
149/// @param[in] srs Spatial reference system.
150/// @param[in] a The first MBR.
151/// @param[in] b The second MBR.
152///
153/// @retval true MBR a is within MBR b.
154/// @retval false MBR a isn't within MBR b.
156 rtr_mbr_t *b);
157
158/// Expands an MBR to also cover another MBR.
159///
160/// @note The function takes a dimension parameter, but currently only supports
161/// 2d MBRs.
162///
163/// MBR format: a[0] = xmin, a[1] = xmax, a[2] = ymin, a[3] = ymax. Same for b.
164///
165/// @param[in] srs Spatial reference system.
166/// @param[in,out] a The first MBR, where the joined result will be.
167/// @param[in] b The second MBR.
168/// @param[in] n_dim Number of dimensions. Must be 2.
169void mbr_join(const dd::Spatial_reference_system *srs, double *a,
170 const double *b, int n_dim);
171
172/// Computes the combined area of two MBRs.
173///
174/// The MBRs may overlap.
175///
176/// @note The function takes a dimension parameter, but currently only supports
177/// 2d MBRs.
178///
179/// @param[in] srs Spatial reference system.
180/// @param[in] a The first MBR.
181/// @param[in] b The second MBR.
182/// @param[in] n_dim Number of dimensions. Must be 2.
183///
184/// @return The area of MBR a expanded by MBR b.
185double mbr_join_area(const dd::Spatial_reference_system *srs, const double *a,
186 const double *b, int n_dim);
187
188/// Computes the area of an MBR.
189///
190/// @note The function takes a dimension parameter, but currently only supports
191/// 2d MBRs.
192///
193/// @param[in] srs Spatial reference system.
194/// @param[in] a The MBR.
195/// @param[in] n_dim Number of dimensions. Must be 2.
196///
197/// @return Are of the MBR.
198double compute_area(const dd::Spatial_reference_system *srs, const double *a,
199 int n_dim);
200
201/// Computes the MBR of a geometry.
202///
203/// If the geometry is empty, a box that covers the entire domain is returned.
204///
205/// The geometry is expected to be on the storage format (SRID + WKB). The
206/// caller is expected to provide an output buffer that is large enough.
207///
208/// @note The function takes a dimension parameter, but currently only supports
209/// 2d MBRs.
210///
211/// The SRID of the SRS parameter must match the SRID stored in the first four
212/// bytes of the geometry string.
213///
214/// @param[in] srs Spatial reference system.
215/// @param[in] store The geometry.
216/// @param[in] size Number of bytes in the geometry string.
217/// @param[in] n_dims Number of dimensions. Must be 2.
218/// @param[out] mbr The computed MBR.
219/// @param[out] srid SRID of the geometry
220///
221/// @retval 0 The geometry is valid.
222/// @retval -1 The geometry is invalid.
224 const uchar *store, uint size, uint n_dims, double *mbr,
225 gis::srid_t *srid);
226
227/// Computes the extra area covered if an MBR is expanded to cover another MBR.
228///
229/// The formula is area(a + b) - area(a). This is generally different from
230/// area(b). If MBR b overlaps MBR a, area(a+b) - area(a) < area(b). If they are
231/// far apart, area(a+b) - area(a) > area(b).
232///
233/// @note If MBRs a and b are far apart, the function may return Inf.
234///
235/// @param[in] srs Spatial reference system.
236/// @param[in] mbr_a First MBR.
237/// @param[in] mbr_b Second MBR.
238/// @param[in] mbr_len MBR length in bytes. Must be 4 * sizeof(double).
239/// @param[out] ab_area The total area of MBRs a and b combined into one MBR.
240///
241/// @return The increase in area when expanding from MBR a to also cover MBR b.
243 const uchar *mbr_a, const uchar *mbr_b, int mbr_len,
244 double *ab_area);
245
246/// Calculates the overlapping area between two MBRs.
247///
248/// @param[in] srs Spatial reference system.
249/// @param[in] mbr_a First MBR.
250/// @param[in] mbr_b Second MBR.
251/// @param[in] mbr_len MBR length in bytes. Must be 4 * sizeof(double).
252///
253/// @return The area of the overlapping region.
255 const uchar *mbr_a, const uchar *mbr_b,
256 int mbr_len);
257
258#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:431
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:119
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:188
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:304
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:364
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:485
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:219
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:157
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:338
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:270
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