1/*****************************************************************************
2Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
3
4This program is free software; you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation; version 2 of the License.
7
8This program is distributed in the hope that it will be useful,
9but WITHOUT ANY WARRANTY; without even the implied warranty of
10MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11GNU General Public License for more details.
12
13You should have received a copy of the GNU General Public License
14along with this program; if not, write to the Free Software Foundation,
1551 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
16*****************************************************************************/
17
18/**************************************************//**
19@file gis0geo.h
20The 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
33mbr is 0. So, we define this macro for calculating the area increasing
34when we need to enlarge the mbr. */
35#define LINE_MBR_WEIGHTS 0.001
36
37/* Types of "well-known binary representation" (wkb) format. */
38enum 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. */
50enum 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)
57if 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,
620 if the buffer is too small */
63uint
64get_wkb_of_default_point(
65 uint n_dims,
66 uchar* wkb,
67 uint len);
68
69/*************************************************************//**
70Calculate minimal bounding rectangle (mbr) of the spatial object
71stored in "well-known binary representation" (wkb) format.
72@return 0 if ok */
73int
74rtree_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. */
82struct 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/*************************************************************//**
91Inline function for reserving coords */
92inline
93static
94double*
95reserve_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/*************************************************************//**
105Split rtree nodes.
106Return which group the first rec is in. */
107int
108split_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/*************************************************************//**
122Compares two keys a and b depending on nextflag
123nextflag 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
130Returns 0 on success. */
131int
132rtree_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/*************************************************************//**
141Calculates MBR_AREA(a+b) - MBR_AREA(a)
142Note: when 'a' and 'b' objects are far from each other,
143the area increase can be really big, so this function
144can return 'inf' as a result. */
145double
146rtree_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 */
157double
158rtree_area_overlapping(
159 const uchar* a,
160 const uchar* b,
161 int mbr_len);
162#endif
163