1 /*
2  topology_private.h -- Topology opaque definitions
3 
4  version 5.0, 2020 August 1
5 
6  Author: Sandro Furieri a.furieri@lqt.it
7 
8  ------------------------------------------------------------------------------
9 
10  Version: MPL 1.1/GPL 2.0/LGPL 2.1
11 
12  The contents of this file are subject to the Mozilla Public License Version
13  1.1 (the "License"); you may not use this file except in compliance with
14  the License. You may obtain a copy of the License at
15  http://www.mozilla.org/MPL/
16 
17 Software distributed under the License is distributed on an "AS IS" basis,
18 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
19 for the specific language governing rights and limitations under the
20 License.
21 
22 The Original Code is the SpatiaLite library
23 
24 The Initial Developer of the Original Code is Alessandro Furieri
25 
26 Portions created by the Initial Developer are Copyright (C) 2015-2021
27 the Initial Developer. All Rights Reserved.
28 
29 Contributor(s):
30 
31 Alternatively, the contents of this file may be used under the terms of
32 either the GNU General Public License Version 2 or later (the "GPL"), or
33 the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
34 in which case the provisions of the GPL or the LGPL are applicable instead
35 of those above. If you wish to allow use of your version of this file only
36 under the terms of either the GPL or the LGPL, and not to allow others to
37 use your version of this file under the terms of the MPL, indicate your
38 decision by deleting the provisions above and replace them with the notice
39 and other provisions required by the GPL or the LGPL. If you do not delete
40 the provisions above, a recipient may use your version of this file under
41 the terms of any one of the MPL, the GPL or the LGPL.
42 
43 */
44 
45 /**
46  \file topology_private.h
47 
48  SpatiaLite Topology private header file
49  */
50 #ifndef DOXYGEN_SHOULD_SKIP_THIS
51 #ifdef _WIN32
52 #ifdef DLL_EXPORT
53 #define TOPOLOGY_PRIVATE
54 #else
55 #define TOPOLOGY_PRIVATE
56 #endif
57 #else
58 #define TOPOLOGY_PRIVATE __attribute__ ((visibility("hidden")))
59 #endif
60 #endif
61 
62 #define GAIA_MODE_TOPO_FACE		0x00
63 #define GAIA_MODE_TOPO_NO_FACE	0xbb
64 
65 struct gaia_topology
66 {
67 /* a struct wrapping a Topology Accessor Object */
68     const void *cache;
69     sqlite3 *db_handle;
70     char *topology_name;
71     int srid;
72     double tolerance;
73     int has_z;
74     char *last_error_message;
75     sqlite3_stmt *stmt_getNodeWithinDistance2D;
76     sqlite3_stmt *stmt_insertNodes;
77     sqlite3_stmt *stmt_getEdgeWithinDistance2D;
78     sqlite3_stmt *stmt_getNextEdgeId;
79     sqlite3_stmt *stmt_setNextEdgeId;
80     sqlite3_stmt *stmt_insertEdges;
81     sqlite3_stmt *stmt_getFaceContainingPoint_1;
82     sqlite3_stmt *stmt_getFaceContainingPoint_2;
83     sqlite3_stmt *stmt_deleteEdges;
84     sqlite3_stmt *stmt_getNodeWithinBox2D;
85     sqlite3_stmt *stmt_getEdgeWithinBox2D;
86     sqlite3_stmt *stmt_getFaceWithinBox2D;
87     sqlite3_stmt *stmt_getAllEdges;
88     sqlite3_stmt *stmt_updateNodes;
89     sqlite3_stmt *stmt_insertFaces;
90     sqlite3_stmt *stmt_updateFacesById;
91     sqlite3_stmt *stmt_getRingEdges;
92     sqlite3_stmt *stmt_deleteFacesById;
93     sqlite3_stmt *stmt_deleteNodesById;
94     void *callbacks;
95     void *rtt_iface;
96     void *rtt_topology;
97     struct gaia_topology *prev;
98     struct gaia_topology *next;
99 };
100 
101 struct face_edge_item
102 {
103 /* a struct wrapping a Face-Edge item */
104     sqlite3_int64 edge_id;
105     sqlite3_int64 left_face;
106     sqlite3_int64 right_face;
107     gaiaGeomCollPtr geom;
108     int count;
109     struct face_edge_item *next;
110 };
111 
112 struct face_item
113 {
114 /* a struct wrapping a Face item */
115     sqlite3_int64 face_id;
116     struct face_item *next;
117 };
118 
119 struct face_edges
120 {
121 /* a struct containing Face-Edge items */
122     int has_z;
123     int srid;
124     struct face_edge_item *first_edge;
125     struct face_edge_item *last_edge;
126     struct face_item *first_face;
127     struct face_item *last_face;
128 };
129 
130 /* common utilities */
131 TOPOLOGY_PRIVATE RTLINE *gaia_convert_linestring_to_rtline (const RTCTX * ctx,
132 							    gaiaLinestringPtr
133 							    ln, int srid,
134 							    int has_z);
135 TOPOLOGY_PRIVATE RTPOLY *gaia_convert_polygon_to_rtpoly (const RTCTX * ctx,
136 							 gaiaPolygonPtr pg,
137 							 int srid, int has_z);
138 
139 /* prototypes for functions handling Topology errors */
140 TOPOLOGY_PRIVATE void gaiatopo_reset_last_error_msg (GaiaTopologyAccessorPtr
141 						     accessor);
142 
143 TOPOLOGY_PRIVATE void gaiatopo_set_last_error_msg (GaiaTopologyAccessorPtr
144 						   accessor, const char *msg);
145 
146 TOPOLOGY_PRIVATE const char
147     *gaiatopo_get_last_exception (GaiaTopologyAccessorPtr accessor);
148 
149 TOPOLOGY_PRIVATE struct face_edges *auxtopo_create_face_edges (int has_z,
150 							       int srid);
151 
152 TOPOLOGY_PRIVATE void auxtopo_free_face_edges (struct face_edges *list);
153 
154 TOPOLOGY_PRIVATE void auxtopo_add_face_edge (struct face_edges *list,
155 					     sqlite3_int64 face_id,
156 					     sqlite3_int64 edge_id,
157 					     sqlite3_int64 left_face,
158 					     sqlite3_int64 right_face,
159 					     gaiaGeomCollPtr geom);
160 
161 TOPOLOGY_PRIVATE void auxtopo_select_valid_face_edges (struct face_edges *list);
162 
163 TOPOLOGY_PRIVATE gaiaGeomCollPtr auxtopo_polygonize_face_edges (struct
164 								face_edges
165 								*list,
166 								const void
167 								*cache);
168 
169 TOPOLOGY_PRIVATE gaiaGeomCollPtr
170 auxtopo_polygonize_face_edges_generalize (struct face_edges *list,
171 					  const void *cache);
172 
173 TOPOLOGY_PRIVATE int auxtopo_create_togeotable_sql (sqlite3 * db_handle,
174 						    const char *db_prefix,
175 						    const char *ref_table,
176 						    const char *ref_column,
177 						    const char *out_table,
178 						    char **xcreate,
179 						    char **xselect,
180 						    char **xinsert,
181 						    int *ref_geom_col);
182 
183 TOPOLOGY_PRIVATE int auxtopo_retrieve_geometry_type (sqlite3 * handle,
184 						     const char *db_prefix,
185 						     const char *table,
186 						     const char *column,
187 						     int *ref_type);
188 
189 TOPOLOGY_PRIVATE gaiaGeomCollPtr auxtopo_make_geom_from_point (int srid,
190 							       int has_z,
191 							       gaiaPointPtr pt);
192 
193 TOPOLOGY_PRIVATE gaiaGeomCollPtr auxtopo_make_geom_from_line (int srid,
194 							      gaiaLinestringPtr
195 							      line);
196 
197 TOPOLOGY_PRIVATE void auxtopo_destroy_geom_from (gaiaGeomCollPtr reference);
198 
199 TOPOLOGY_PRIVATE void auxtopo_copy_linestring (gaiaLinestringPtr line,
200 					       gaiaGeomCollPtr geom);
201 
202 TOPOLOGY_PRIVATE void auxtopo_copy_linestring3d (gaiaLinestringPtr line,
203 						 gaiaGeomCollPtr geom);
204 
205 TOPOLOGY_PRIVATE int auxtopo_insert_into_topology (GaiaTopologyAccessorPtr
206 						   accessor,
207 						   gaiaGeomCollPtr geom,
208 						   double tolerance,
209 						   int line_max_points,
210 						   double max_length, int mode,
211 						   gaiaGeomCollPtr *
212 						   failing_geometry);
213 
214 
215 /* prototypes for functions creating some SQL prepared statement */
216 TOPOLOGY_PRIVATE sqlite3_stmt
217     * do_create_stmt_getNodeWithinDistance2D (GaiaTopologyAccessorPtr accessor);
218 
219 TOPOLOGY_PRIVATE sqlite3_stmt
220     * do_create_stmt_getNodeWithinBox2D (GaiaTopologyAccessorPtr accessor);
221 
222 TOPOLOGY_PRIVATE sqlite3_stmt
223     * do_create_stmt_getEdgeWithinDistance2D (GaiaTopologyAccessorPtr accessor);
224 
225 TOPOLOGY_PRIVATE sqlite3_stmt
226     * do_create_stmt_getEdgeWithinBox2D (GaiaTopologyAccessorPtr accessor);
227 
228 TOPOLOGY_PRIVATE sqlite3_stmt
229     * do_create_stmt_getFaceWithinBox2D (GaiaTopologyAccessorPtr accessor);
230 
231 TOPOLOGY_PRIVATE sqlite3_stmt
232     * do_create_stmt_getAllEdges (GaiaTopologyAccessorPtr accessor);
233 
234 TOPOLOGY_PRIVATE sqlite3_stmt
235     *
236 do_create_stmt_getFaceContainingPoint_1 (GaiaTopologyAccessorPtr accessor);
237 
238 TOPOLOGY_PRIVATE sqlite3_stmt
239     *
240 do_create_stmt_getFaceContainingPoint_2 (GaiaTopologyAccessorPtr accessor);
241 
242 TOPOLOGY_PRIVATE sqlite3_stmt
243     * do_create_stmt_insertNodes (GaiaTopologyAccessorPtr accessor);
244 
245 TOPOLOGY_PRIVATE sqlite3_stmt
246     * do_create_stmt_insertEdges (GaiaTopologyAccessorPtr accessor);
247 
248 TOPOLOGY_PRIVATE sqlite3_stmt
249     * do_create_stmt_updateEdges (GaiaTopologyAccessorPtr accessor);
250 
251 TOPOLOGY_PRIVATE sqlite3_stmt
252     * do_create_stmt_getNextEdgeId (GaiaTopologyAccessorPtr accessor);
253 
254 TOPOLOGY_PRIVATE sqlite3_stmt
255     * do_create_stmt_setNextEdgeId (GaiaTopologyAccessorPtr accessor);
256 
257 TOPOLOGY_PRIVATE sqlite3_stmt
258     * do_create_stmt_getRingEdges (GaiaTopologyAccessorPtr accessor);
259 
260 TOPOLOGY_PRIVATE sqlite3_stmt
261     * do_create_stmt_insertFaces (GaiaTopologyAccessorPtr accessor);
262 
263 TOPOLOGY_PRIVATE sqlite3_stmt
264     * do_create_stmt_updateFacesById (GaiaTopologyAccessorPtr accessor);
265 
266 TOPOLOGY_PRIVATE sqlite3_stmt
267     * do_create_stmt_deleteFacesById (GaiaTopologyAccessorPtr accessor);
268 
269 TOPOLOGY_PRIVATE sqlite3_stmt
270     * do_create_stmt_deleteNodesById (GaiaTopologyAccessorPtr accessor);
271 
272 TOPOLOGY_PRIVATE void
273 finalize_topogeo_prepared_stmts (GaiaTopologyAccessorPtr accessor);
274 
275 TOPOLOGY_PRIVATE void
276 create_topogeo_prepared_stmts (GaiaTopologyAccessorPtr accessor);
277 
278 TOPOLOGY_PRIVATE void finalize_all_topo_prepared_stmts (const void *cache);
279 
280 TOPOLOGY_PRIVATE void create_all_topo_prepared_stmts (const void *cache);
281 
282 
283 /* callback function prototypes */
284 const char *callback_lastErrorMessage (const RTT_BE_DATA * be);
285 
286 int callback_freeTopology (RTT_BE_TOPOLOGY * topo);
287 
288 RTT_BE_TOPOLOGY *callback_loadTopologyByName (const RTT_BE_DATA * be,
289 					      const char *name);
290 
291 RTT_ISO_NODE *callback_getNodeById (const RTT_BE_TOPOLOGY * topo,
292 				    const RTT_ELEMID * ids, int *numelems,
293 				    int fields);
294 
295 RTT_ISO_NODE *callback_getNodeWithinDistance2D (const RTT_BE_TOPOLOGY *
296 						topo, const RTPOINT * pt,
297 						double dist, int *numelems,
298 						int fields, int limit);
299 
300 int callback_insertNodes (const RTT_BE_TOPOLOGY * topo,
301 			  RTT_ISO_NODE * nodes, int numelems);
302 
303 RTT_ISO_EDGE *callback_getEdgeById (const RTT_BE_TOPOLOGY * topo,
304 				    const RTT_ELEMID * ids, int *numelems,
305 				    int fields);
306 
307 RTT_ISO_EDGE *callback_getEdgeWithinDistance2D (const RTT_BE_TOPOLOGY *
308 						topo, const RTPOINT * pt,
309 						double dist, int *numelems,
310 						int fields, int limit);
311 
312 RTT_ELEMID callback_getNextEdgeId (const RTT_BE_TOPOLOGY * topo);
313 
314 int callback_insertEdges (const RTT_BE_TOPOLOGY * topo,
315 			  RTT_ISO_EDGE * edges, int numelems);
316 
317 int callback_updateEdges (const RTT_BE_TOPOLOGY * topo,
318 			  const RTT_ISO_EDGE * sel_edge, int sel_fields,
319 			  const RTT_ISO_EDGE * upd_edge, int upd_fields,
320 			  const RTT_ISO_EDGE * exc_edge, int exc_fields);
321 
322 RTT_ISO_FACE *callback_getFaceById (const RTT_BE_TOPOLOGY * topo,
323 				    const RTT_ELEMID * ids, int *numelems,
324 				    int fields);
325 
326 RTT_ELEMID callback_getFaceContainingPoint (const RTT_BE_TOPOLOGY * topo,
327 					    const RTPOINT * pt);
328 
329 int callback_deleteEdges (const RTT_BE_TOPOLOGY * topo,
330 			  const RTT_ISO_EDGE * sel_edge, int sel_fields);
331 
332 RTT_ISO_NODE *callback_getNodeWithinBox2D (const RTT_BE_TOPOLOGY * topo,
333 					   const RTGBOX * box, int *numelems,
334 					   int fields, int limit);
335 
336 RTT_ISO_EDGE *callback_getEdgeWithinBox2D (const RTT_BE_TOPOLOGY * topo,
337 					   const RTGBOX * box, int *numelems,
338 					   int fields, int limit);
339 
340 RTT_ISO_EDGE *callback_getAllEdges (const RTT_BE_TOPOLOGY * topo, int *numelems,
341 				    int fields, int limit);
342 
343 RTT_ISO_EDGE *callback_getEdgeByNode (const RTT_BE_TOPOLOGY * topo,
344 				      const RTT_ELEMID * ids,
345 				      int *numelems, int fields);
346 
347 int callback_updateNodes (const RTT_BE_TOPOLOGY * topo,
348 			  const RTT_ISO_NODE * sel_node, int sel_fields,
349 			  const RTT_ISO_NODE * upd_node, int upd_fields,
350 			  const RTT_ISO_NODE * exc_node, int exc_fields);
351 
352 int callback_updateTopoGeomFaceSplit (const RTT_BE_TOPOLOGY * topo,
353 				      RTT_ELEMID split_face,
354 				      RTT_ELEMID new_face1,
355 				      RTT_ELEMID new_face2);
356 
357 int callback_insertFaces (const RTT_BE_TOPOLOGY * topo,
358 			  RTT_ISO_FACE * faces, int numelems);
359 
360 int callback_updateFacesById (const RTT_BE_TOPOLOGY * topo,
361 			      const RTT_ISO_FACE * faces, int numfaces);
362 
363 int callback_deleteFacesById (const RTT_BE_TOPOLOGY * topo,
364 			      const RTT_ELEMID * ids, int numelems);
365 
366 RTT_ELEMID *callback_getRingEdges (const RTT_BE_TOPOLOGY * topo,
367 				   RTT_ELEMID edge, int *numedges, int limit);
368 
369 int callback_updateEdgesById (const RTT_BE_TOPOLOGY * topo,
370 			      const RTT_ISO_EDGE * edges, int numedges,
371 			      int upd_fields);
372 
373 RTT_ISO_EDGE *callback_getEdgeByFace (const RTT_BE_TOPOLOGY * topo,
374 				      const RTT_ELEMID * ids,
375 				      int *numelems, int fields,
376 				      const RTGBOX * box);
377 
378 RTT_ISO_NODE *callback_getNodeByFace (const RTT_BE_TOPOLOGY * topo,
379 				      const RTT_ELEMID * faces,
380 				      int *numelems, int fields,
381 				      const RTGBOX * box);
382 
383 int callback_updateNodesById (const RTT_BE_TOPOLOGY * topo,
384 			      const RTT_ISO_NODE * nodes, int numnodes,
385 			      int upd_fields);
386 
387 int callback_deleteNodesById (const RTT_BE_TOPOLOGY * topo,
388 			      const RTT_ELEMID * ids, int numelems);
389 
390 int callback_updateTopoGeomEdgeSplit (const RTT_BE_TOPOLOGY * topo,
391 				      RTT_ELEMID split_edge,
392 				      RTT_ELEMID new_edge1,
393 				      RTT_ELEMID new_edge2);
394 
395 int callback_checkTopoGeomRemEdge (const RTT_BE_TOPOLOGY * topo,
396 				   RTT_ELEMID rem_edge, RTT_ELEMID face_left,
397 				   RTT_ELEMID face_right);
398 
399 int callback_updateTopoGeomFaceHeal (const RTT_BE_TOPOLOGY * topo,
400 				     RTT_ELEMID face1, RTT_ELEMID face2,
401 				     RTT_ELEMID newface);
402 
403 int callback_checkTopoGeomRemNode (const RTT_BE_TOPOLOGY * topo,
404 				   RTT_ELEMID rem_node, RTT_ELEMID e1,
405 				   RTT_ELEMID e2);
406 
407 int callback_updateTopoGeomEdgeHeal (const RTT_BE_TOPOLOGY * topo,
408 				     RTT_ELEMID edge1, RTT_ELEMID edge2,
409 				     RTT_ELEMID newedge);
410 
411 RTT_ISO_FACE *callback_getFaceWithinBox2D (const RTT_BE_TOPOLOGY * topo,
412 					   const RTGBOX * box, int *numelems,
413 					   int fields, int limit);
414 
415 int callback_topoGetSRID (const RTT_BE_TOPOLOGY * topo);
416 
417 double callback_topoGetPrecision (const RTT_BE_TOPOLOGY * topo);
418 
419 int callback_topoHasZ (const RTT_BE_TOPOLOGY * topo);
420