1 | /***************************************************************************** |
2 | Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved. |
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 as published by |
6 | the Free Software Foundation; version 2 of the License. |
7 | |
8 | This program is distributed in the hope that it will be useful, |
9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | GNU General Public License for more details. |
12 | |
13 | You should have received a copy of the GNU General Public License |
14 | along with this program; if not, write to the Free Software Foundation, |
15 | 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA |
16 | *****************************************************************************/ |
17 | |
18 | /**************************************************//** |
19 | @file gis0geo.h |
20 | The r-tree define from MyISAM |
21 | *******************************************************/ |
22 | |
23 | #ifndef _gis0geo_h |
24 | #define _gis0geo_h |
25 | |
26 | #include "my_global.h" |
27 | #include "string.h" |
28 | |
29 | #define SPTYPE HA_KEYTYPE_DOUBLE |
30 | #define SPLEN 8 |
31 | |
32 | /* Since the mbr could be a point or a linestring, in this case, area of |
33 | mbr is 0. So, we define this macro for calculating the area increasing |
34 | when we need to enlarge the mbr. */ |
35 | #define LINE_MBR_WEIGHTS 0.001 |
36 | |
37 | /* Types of "well-known binary representation" (wkb) format. */ |
38 | enum wkbType |
39 | { |
40 | wkbPoint = 1, |
41 | wkbLineString = 2, |
42 | wkbPolygon = 3, |
43 | wkbMultiPoint = 4, |
44 | wkbMultiLineString = 5, |
45 | wkbMultiPolygon = 6, |
46 | wkbGeometryCollection = 7 |
47 | }; |
48 | |
49 | /* Byte order of "well-known binary representation" (wkb) format. */ |
50 | enum wkbByteOrder |
51 | { |
52 | wkbXDR = 0, /* Big Endian */ |
53 | wkbNDR = 1 /* Little Endian */ |
54 | }; |
55 | |
56 | /** Get the wkb of default POINT value, which represents POINT(0 0) |
57 | if it's of dimension 2, etc. |
58 | @param[in] n_dims dimensions |
59 | @param[out] wkb wkb buffer for default POINT |
60 | @param[in] len length of wkb buffer |
61 | @return non-0 indicate the length of wkb of the default POINT, |
62 | 0 if the buffer is too small */ |
63 | uint |
64 | get_wkb_of_default_point( |
65 | uint n_dims, |
66 | uchar* wkb, |
67 | uint len); |
68 | |
69 | /*************************************************************//** |
70 | Calculate minimal bounding rectangle (mbr) of the spatial object |
71 | stored in "well-known binary representation" (wkb) format. |
72 | @return 0 if ok */ |
73 | int |
74 | rtree_mbr_from_wkb( |
75 | /*===============*/ |
76 | uchar* wkb, /*!< in: pointer to wkb. */ |
77 | uint size, /*!< in: size of wkb. */ |
78 | uint n_dims, /*!< in: dimensions. */ |
79 | double* mbr); /*!< in/out: mbr. */ |
80 | |
81 | /* Rtree split node structure. */ |
82 | struct rtr_split_node_t |
83 | { |
84 | double square; /* square of the mbr.*/ |
85 | int n_node; /* which group in.*/ |
86 | uchar* key; /* key. */ |
87 | double* coords; /* mbr. */ |
88 | }; |
89 | |
90 | /*************************************************************//** |
91 | Inline function for reserving coords */ |
92 | inline |
93 | static |
94 | double* |
95 | reserve_coords(double **d_buffer, /*!< in/out: buffer. */ |
96 | int n_dim) /*!< in: dimensions. */ |
97 | /*===========*/ |
98 | { |
99 | double *coords = *d_buffer; |
100 | (*d_buffer) += n_dim * 2; |
101 | return coords; |
102 | } |
103 | |
104 | /*************************************************************//** |
105 | Split rtree nodes. |
106 | Return which group the first rec is in. */ |
107 | int |
108 | split_rtree_node( |
109 | /*=============*/ |
110 | rtr_split_node_t* node, /*!< in: split nodes.*/ |
111 | int n_entries, /*!< in: entries number.*/ |
112 | int all_size, /*!< in: total key's size.*/ |
113 | int key_size, /*!< in: key's size.*/ |
114 | int min_size, /*!< in: minimal group size.*/ |
115 | int size1, /*!< in: size of group.*/ |
116 | int size2, /*!< in: initial group sizes */ |
117 | double** d_buffer, /*!< in/out: buffer.*/ |
118 | int n_dim, /*!< in: dimensions. */ |
119 | uchar* first_rec); /*!< in: the first rec. */ |
120 | |
121 | /*************************************************************//** |
122 | Compares two keys a and b depending on nextflag |
123 | nextflag can contain these flags: |
124 | MBR_INTERSECT(a,b) a overlaps b |
125 | MBR_CONTAIN(a,b) a contains b |
126 | MBR_DISJOINT(a,b) a disjoint b |
127 | MBR_WITHIN(a,b) a within b |
128 | MBR_EQUAL(a,b) All coordinates of MBRs are equal |
129 | MBR_DATA(a,b) Data reference is the same |
130 | Returns 0 on success. */ |
131 | int |
132 | rtree_key_cmp( |
133 | /*==========*/ |
134 | page_cur_mode_t mode, /*!< in: compare method. */ |
135 | const uchar* b, /*!< in: first key. */ |
136 | int b_len, /*!< in: first key len. */ |
137 | const uchar* a, /*!< in: second key. */ |
138 | int a_len); /*!< in: second key len. */ |
139 | |
140 | /*************************************************************//** |
141 | Calculates MBR_AREA(a+b) - MBR_AREA(a) |
142 | Note: when 'a' and 'b' objects are far from each other, |
143 | the area increase can be really big, so this function |
144 | can return 'inf' as a result. */ |
145 | double |
146 | rtree_area_increase( |
147 | const uchar* a, /*!< in: first mbr. */ |
148 | const uchar* b, /*!< in: second mbr. */ |
149 | int a_len, /*!< in: mbr length. */ |
150 | double* ab_area); /*!< out: increased area. */ |
151 | |
152 | /** Calculates overlapping area |
153 | @param[in] a mbr a |
154 | @param[in] b mbr b |
155 | @param[in] mbr_len mbr length |
156 | @return overlapping area */ |
157 | double |
158 | rtree_area_overlapping( |
159 | const uchar* a, |
160 | const uchar* b, |
161 | int mbr_len); |
162 | #endif |
163 | |