1 /* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
22 
23 #ifndef ITEM_GEOFUNC_BGWRAP_INCLUDED
24 #define ITEM_GEOFUNC_BGWRAP_INCLUDED
25 
26 /**
27   Wraps and dispatches type specific BG function calls according to operation
28   type and both operands' types.
29 
30   We want to isolate boost header file inclusion only inside this file, so we
31   put this class declaration in an internal header file. And we want to make the
32   methods static since no state is needed here.
33 
34   @tparam Geom_types Geometry types definitions.
35 */
36 
37 #include <set>
38 
39 template<typename Geom_types>
40 class BG_wrap {
41 public:
42 
43   typedef typename Geom_types::Point Point;
44   typedef typename Geom_types::Linestring Linestring;
45   typedef typename Geom_types::Polygon Polygon;
46   typedef typename Geom_types::Multipoint Multipoint;
47   typedef typename Geom_types::Multilinestring Multilinestring;
48   typedef typename Geom_types::Multipolygon Multipolygon;
49   typedef typename Geom_types::Coordinate_type Coord_type;
50   typedef typename Geom_types::Coordinate_system Coordsys;
51 
52   // For abbrievation.
53   typedef Item_func_spatial_rel Ifsr;
54   typedef std::set<Point, bgpt_lt> Point_set;
55   typedef std::vector<Point> Point_vector;
56 
57   static int point_within_geometry(Geometry *g1, Geometry *g2,
58                                    my_bool *pnull_value);
59 
60   static int multipoint_within_geometry(Geometry *g1, Geometry *g2,
61                                         my_bool *pnull_value);
62 
63   static int linestring_within_geometry(Geometry *g1, Geometry *g2,
64                                         my_bool *pnull_value);
65   static int multilinestring_within_geometry(Geometry *g1, Geometry *g2,
66                                              my_bool *pnull_value);
67   static int polygon_within_geometry(Geometry *g1, Geometry *g2,
68                                      my_bool *pnull_value);
69   static int multipolygon_within_geometry(Geometry *g1, Geometry *g2,
70                                           my_bool *pnull_value);
71 
72   static int multipoint_equals_geometry(Geometry *g1, Geometry *g2,
73                                         my_bool *pnull_value);
74 
75   static int point_disjoint_geometry(Geometry *g1, Geometry *g2,
76                                      my_bool *pnull_value);
77   static int multipoint_disjoint_geometry(Geometry *g1, Geometry *g2,
78                                           my_bool *pnull_value);
79 
80   static int linestring_disjoint_geometry(Geometry *g1, Geometry *g2,
81                                           my_bool *pnull_value);
82   static int multilinestring_disjoint_geometry(Geometry *g1, Geometry *g2,
83                                                my_bool *pnull_value);
84   static int polygon_disjoint_geometry(Geometry *g1, Geometry *g2,
85                                        my_bool *pnull_value);
86   static int multipolygon_disjoint_geometry(Geometry *g1, Geometry *g2,
87                                             my_bool *pnull_value);
88   static int point_intersects_geometry(Geometry *g1, Geometry *g2,
89                                        my_bool *pnull_value);
90   static int multipoint_intersects_geometry(Geometry *g1, Geometry *g2,
91                                             my_bool *pnull_value);
92   static int linestring_intersects_geometry(Geometry *g1, Geometry *g2,
93                                             my_bool *pnull_value);
94   static int multilinestring_intersects_geometry(Geometry *g1, Geometry *g2,
95                                                  my_bool *pnull_value);
96   static int polygon_intersects_geometry(Geometry *g1, Geometry *g2,
97                                          my_bool *pnull_value);
98   static int multipolygon_intersects_geometry(Geometry *g1, Geometry *g2,
99                                               my_bool *pnull_value);
100   static int linestring_crosses_geometry(Geometry *g1, Geometry *g2,
101                                          my_bool *pnull_value);
102   static int multipoint_crosses_geometry(Geometry *g1, Geometry *g2,
103                                          my_bool *pnull_value);
104   static int multilinestring_crosses_geometry(Geometry *g1, Geometry *g2,
105                                               my_bool *pnull_value);
106   static int multipoint_overlaps_multipoint(Geometry *g1, Geometry *g2,
107                                             my_bool *pnull_value);
108   static int point_touches_geometry(Geometry *g1, Geometry *g2,
109                                     my_bool *pnull_value);
110   static int multipoint_touches_geometry(Geometry *g1, Geometry *g2,
111                                          my_bool *pnull_value);
112   static int linestring_touches_geometry(Geometry *g1, Geometry *g2,
113                                          my_bool *pnull_value);
114   static int multilinestring_touches_polygon(Geometry *g1, Geometry *g2,
115                                              my_bool *pnull_value);
116   static int multilinestring_touches_geometry(Geometry *g1, Geometry *g2,
117                                               my_bool *pnull_value);
118   static int polygon_touches_geometry(Geometry *g1, Geometry *g2,
119                                       my_bool *pnull_value);
120   static int multipolygon_touches_geometry(Geometry *g1, Geometry *g2,
121                                            my_bool *pnull_value);
122 
123 private:
124   template<typename Geom_type>
125   static int multipoint_disjoint_geometry_internal(const Multipoint &mpts1,
126                                                    const Geom_type &geom);
127   template<typename Geom_type>
128   static int multipoint_disjoint_multi_geometry(const Multipoint &mpts,
129                                                 const Geom_type &geom);
130   template <typename GeomType>
131   static int multipoint_within_geometry_internal(const Multipoint &mpts,
132                                                  const GeomType &geom);
133   static int multipoint_within_multipolygon(const Multipoint &mpts,
134                                             const Multipolygon &mplgn);
135 };// BG_wrap
136 
137 
138 /*
139   Call a BG function with specified types of operands. We have to create
140   geo1 and geo2 because operands g1 and g2 are created without their WKB data
141   parsed, so not suitable for BG to use. geo1 will share the same copy of WKB
142   data with g1, also true for geo2.
143  */
144 #define BGCALL(res, bgfunc, GeoType1, g1, GeoType2, g2, pnullval) do {  \
145   const void *pg1= g1->normalize_ring_order();                          \
146   const void *pg2= g2->normalize_ring_order();                          \
147   if (pg1 != NULL && pg2 != NULL)                                       \
148   {                                                                     \
149     GeoType1 geo1(pg1, g1->get_data_size(), g1->get_flags(),            \
150                   g1->get_srid());                                      \
151     GeoType2 geo2(pg2, g2->get_data_size(), g2->get_flags(),            \
152                   g2->get_srid());                                      \
153     res= boost::geometry::bgfunc(geo1, geo2);                           \
154   }                                                                     \
155   else                                                                  \
156   {                                                                     \
157     my_error(ER_GIS_INVALID_DATA, MYF(0), "st_" #bgfunc);               \
158     (*(pnullval))= 1;                                                   \
159   }                                                                     \
160 } while (0)
161 
162 #endif  // ITEM_GEOFUNC_BGWRAP_INCLUDED
163