1 /*-------------------------------------------------------------------------
2  *
3  * geo_decls.h - Declarations for various 2D constructs.
4  *
5  *
6  * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  * src/include/utils/geo_decls.h
10  *
11  * NOTE
12  *	  These routines do *not* use the float types from adt/.
13  *
14  *	  XXX These routines were not written by a numerical analyst.
15  *
16  *	  XXX I have made some attempt to flesh out the operators
17  *		and data types. There are still some more to do. - tgl 97/04/19
18  *
19  *-------------------------------------------------------------------------
20  */
21 #ifndef GEO_DECLS_H
22 #define GEO_DECLS_H
23 
24 #include <math.h>
25 
26 #include "fmgr.h"
27 
28 /*--------------------------------------------------------------------
29  * Useful floating point utilities and constants.
30  *-------------------------------------------------------------------*/
31 
32 
33 #define EPSILON					1.0E-06
34 
35 #ifdef EPSILON
36 #define FPzero(A)				(fabs(A) <= EPSILON)
37 #define FPeq(A,B)				(fabs((A) - (B)) <= EPSILON)
38 #define FPne(A,B)				(fabs((A) - (B)) > EPSILON)
39 #define FPlt(A,B)				((B) - (A) > EPSILON)
40 #define FPle(A,B)				((A) - (B) <= EPSILON)
41 #define FPgt(A,B)				((A) - (B) > EPSILON)
42 #define FPge(A,B)				((B) - (A) <= EPSILON)
43 #else
44 #define FPzero(A)				((A) == 0)
45 #define FPeq(A,B)				((A) == (B))
46 #define FPne(A,B)				((A) != (B))
47 #define FPlt(A,B)				((A) < (B))
48 #define FPle(A,B)				((A) <= (B))
49 #define FPgt(A,B)				((A) > (B))
50 #define FPge(A,B)				((A) >= (B))
51 #endif
52 
53 #define HYPOT(A, B)				pg_hypot(A, B)
54 
55 /*---------------------------------------------------------------------
56  * Point - (x,y)
57  *-------------------------------------------------------------------*/
58 typedef struct
59 {
60 	double		x,
61 				y;
62 } Point;
63 
64 
65 /*---------------------------------------------------------------------
66  * LSEG - A straight line, specified by endpoints.
67  *-------------------------------------------------------------------*/
68 typedef struct
69 {
70 	Point		p[2];
71 } LSEG;
72 
73 
74 /*---------------------------------------------------------------------
75  * PATH - Specified by vertex points.
76  *-------------------------------------------------------------------*/
77 typedef struct
78 {
79 	int32		vl_len_;		/* varlena header (do not touch directly!) */
80 	int32		npts;
81 	int32		closed;			/* is this a closed polygon? */
82 	int32		dummy;			/* padding to make it double align */
83 	Point		p[FLEXIBLE_ARRAY_MEMBER];
84 } PATH;
85 
86 
87 /*---------------------------------------------------------------------
88  * LINE - Specified by its general equation (Ax+By+C=0).
89  *-------------------------------------------------------------------*/
90 typedef struct
91 {
92 	double		A,
93 				B,
94 				C;
95 } LINE;
96 
97 
98 /*---------------------------------------------------------------------
99  * BOX	- Specified by two corner points, which are
100  *		 sorted to save calculation time later.
101  *-------------------------------------------------------------------*/
102 typedef struct
103 {
104 	Point		high,
105 				low;			/* corner POINTs */
106 } BOX;
107 
108 /*---------------------------------------------------------------------
109  * POLYGON - Specified by an array of doubles defining the points,
110  *		keeping the number of points and the bounding box for
111  *		speed purposes.
112  *-------------------------------------------------------------------*/
113 typedef struct
114 {
115 	int32		vl_len_;		/* varlena header (do not touch directly!) */
116 	int32		npts;
117 	BOX			boundbox;
118 	Point		p[FLEXIBLE_ARRAY_MEMBER];
119 } POLYGON;
120 
121 /*---------------------------------------------------------------------
122  * CIRCLE - Specified by a center point and radius.
123  *-------------------------------------------------------------------*/
124 typedef struct
125 {
126 	Point		center;
127 	double		radius;
128 } CIRCLE;
129 
130 /*
131  * fmgr interface macros
132  *
133  * Path and Polygon are toastable varlena types, the others are just
134  * fixed-size pass-by-reference types.
135  */
136 
137 #define DatumGetPointP(X)	 ((Point *) DatumGetPointer(X))
138 #define PointPGetDatum(X)	 PointerGetDatum(X)
139 #define PG_GETARG_POINT_P(n) DatumGetPointP(PG_GETARG_DATUM(n))
140 #define PG_RETURN_POINT_P(x) return PointPGetDatum(x)
141 
142 #define DatumGetLsegP(X)	((LSEG *) DatumGetPointer(X))
143 #define LsegPGetDatum(X)	PointerGetDatum(X)
144 #define PG_GETARG_LSEG_P(n) DatumGetLsegP(PG_GETARG_DATUM(n))
145 #define PG_RETURN_LSEG_P(x) return LsegPGetDatum(x)
146 
147 #define DatumGetPathP(X)		 ((PATH *) PG_DETOAST_DATUM(X))
148 #define DatumGetPathPCopy(X)	 ((PATH *) PG_DETOAST_DATUM_COPY(X))
149 #define PathPGetDatum(X)		 PointerGetDatum(X)
150 #define PG_GETARG_PATH_P(n)		 DatumGetPathP(PG_GETARG_DATUM(n))
151 #define PG_GETARG_PATH_P_COPY(n) DatumGetPathPCopy(PG_GETARG_DATUM(n))
152 #define PG_RETURN_PATH_P(x)		 return PathPGetDatum(x)
153 
154 #define DatumGetLineP(X)	((LINE *) DatumGetPointer(X))
155 #define LinePGetDatum(X)	PointerGetDatum(X)
156 #define PG_GETARG_LINE_P(n) DatumGetLineP(PG_GETARG_DATUM(n))
157 #define PG_RETURN_LINE_P(x) return LinePGetDatum(x)
158 
159 #define DatumGetBoxP(X)    ((BOX *) DatumGetPointer(X))
160 #define BoxPGetDatum(X)    PointerGetDatum(X)
161 #define PG_GETARG_BOX_P(n) DatumGetBoxP(PG_GETARG_DATUM(n))
162 #define PG_RETURN_BOX_P(x) return BoxPGetDatum(x)
163 
164 #define DatumGetPolygonP(X)			((POLYGON *) PG_DETOAST_DATUM(X))
165 #define DatumGetPolygonPCopy(X)		((POLYGON *) PG_DETOAST_DATUM_COPY(X))
166 #define PolygonPGetDatum(X)			PointerGetDatum(X)
167 #define PG_GETARG_POLYGON_P(n)		DatumGetPolygonP(PG_GETARG_DATUM(n))
168 #define PG_GETARG_POLYGON_P_COPY(n) DatumGetPolygonPCopy(PG_GETARG_DATUM(n))
169 #define PG_RETURN_POLYGON_P(x)		return PolygonPGetDatum(x)
170 
171 #define DatumGetCircleP(X)	  ((CIRCLE *) DatumGetPointer(X))
172 #define CirclePGetDatum(X)	  PointerGetDatum(X)
173 #define PG_GETARG_CIRCLE_P(n) DatumGetCircleP(PG_GETARG_DATUM(n))
174 #define PG_RETURN_CIRCLE_P(x) return CirclePGetDatum(x)
175 
176 
177 /*
178  * in geo_ops.h
179  */
180 
181 /* public point routines */
182 extern Datum point_in(PG_FUNCTION_ARGS);
183 extern Datum point_out(PG_FUNCTION_ARGS);
184 extern Datum point_recv(PG_FUNCTION_ARGS);
185 extern Datum point_send(PG_FUNCTION_ARGS);
186 extern Datum construct_point(PG_FUNCTION_ARGS);
187 extern Datum point_left(PG_FUNCTION_ARGS);
188 extern Datum point_right(PG_FUNCTION_ARGS);
189 extern Datum point_above(PG_FUNCTION_ARGS);
190 extern Datum point_below(PG_FUNCTION_ARGS);
191 extern Datum point_vert(PG_FUNCTION_ARGS);
192 extern Datum point_horiz(PG_FUNCTION_ARGS);
193 extern Datum point_eq(PG_FUNCTION_ARGS);
194 extern Datum point_ne(PG_FUNCTION_ARGS);
195 extern Datum point_distance(PG_FUNCTION_ARGS);
196 extern Datum point_slope(PG_FUNCTION_ARGS);
197 extern Datum point_add(PG_FUNCTION_ARGS);
198 extern Datum point_sub(PG_FUNCTION_ARGS);
199 extern Datum point_mul(PG_FUNCTION_ARGS);
200 extern Datum point_div(PG_FUNCTION_ARGS);
201 
202 /* private routines */
203 extern double point_dt(Point *pt1, Point *pt2);
204 extern double point_sl(Point *pt1, Point *pt2);
205 extern double pg_hypot(double x, double y);
206 
207 /* public lseg routines */
208 extern Datum lseg_in(PG_FUNCTION_ARGS);
209 extern Datum lseg_out(PG_FUNCTION_ARGS);
210 extern Datum lseg_recv(PG_FUNCTION_ARGS);
211 extern Datum lseg_send(PG_FUNCTION_ARGS);
212 extern Datum lseg_intersect(PG_FUNCTION_ARGS);
213 extern Datum lseg_parallel(PG_FUNCTION_ARGS);
214 extern Datum lseg_perp(PG_FUNCTION_ARGS);
215 extern Datum lseg_vertical(PG_FUNCTION_ARGS);
216 extern Datum lseg_horizontal(PG_FUNCTION_ARGS);
217 extern Datum lseg_eq(PG_FUNCTION_ARGS);
218 extern Datum lseg_ne(PG_FUNCTION_ARGS);
219 extern Datum lseg_lt(PG_FUNCTION_ARGS);
220 extern Datum lseg_le(PG_FUNCTION_ARGS);
221 extern Datum lseg_gt(PG_FUNCTION_ARGS);
222 extern Datum lseg_ge(PG_FUNCTION_ARGS);
223 extern Datum lseg_construct(PG_FUNCTION_ARGS);
224 extern Datum lseg_length(PG_FUNCTION_ARGS);
225 extern Datum lseg_distance(PG_FUNCTION_ARGS);
226 extern Datum lseg_center(PG_FUNCTION_ARGS);
227 extern Datum lseg_interpt(PG_FUNCTION_ARGS);
228 extern Datum dist_pl(PG_FUNCTION_ARGS);
229 extern Datum dist_ps(PG_FUNCTION_ARGS);
230 extern Datum dist_ppath(PG_FUNCTION_ARGS);
231 extern Datum dist_pb(PG_FUNCTION_ARGS);
232 extern Datum dist_sl(PG_FUNCTION_ARGS);
233 extern Datum dist_sb(PG_FUNCTION_ARGS);
234 extern Datum dist_lb(PG_FUNCTION_ARGS);
235 extern Datum close_lseg(PG_FUNCTION_ARGS);
236 extern Datum close_pl(PG_FUNCTION_ARGS);
237 extern Datum close_ps(PG_FUNCTION_ARGS);
238 extern Datum close_pb(PG_FUNCTION_ARGS);
239 extern Datum close_sl(PG_FUNCTION_ARGS);
240 extern Datum close_sb(PG_FUNCTION_ARGS);
241 extern Datum close_ls(PG_FUNCTION_ARGS);
242 extern Datum close_lb(PG_FUNCTION_ARGS);
243 extern Datum on_pl(PG_FUNCTION_ARGS);
244 extern Datum on_ps(PG_FUNCTION_ARGS);
245 extern Datum on_pb(PG_FUNCTION_ARGS);
246 extern Datum on_ppath(PG_FUNCTION_ARGS);
247 extern Datum on_sl(PG_FUNCTION_ARGS);
248 extern Datum on_sb(PG_FUNCTION_ARGS);
249 extern Datum inter_sl(PG_FUNCTION_ARGS);
250 extern Datum inter_sb(PG_FUNCTION_ARGS);
251 extern Datum inter_lb(PG_FUNCTION_ARGS);
252 
253 /* public line routines */
254 extern Datum line_in(PG_FUNCTION_ARGS);
255 extern Datum line_out(PG_FUNCTION_ARGS);
256 extern Datum line_recv(PG_FUNCTION_ARGS);
257 extern Datum line_send(PG_FUNCTION_ARGS);
258 extern Datum line_interpt(PG_FUNCTION_ARGS);
259 extern Datum line_distance(PG_FUNCTION_ARGS);
260 extern Datum line_construct_pp(PG_FUNCTION_ARGS);
261 extern Datum line_intersect(PG_FUNCTION_ARGS);
262 extern Datum line_parallel(PG_FUNCTION_ARGS);
263 extern Datum line_perp(PG_FUNCTION_ARGS);
264 extern Datum line_vertical(PG_FUNCTION_ARGS);
265 extern Datum line_horizontal(PG_FUNCTION_ARGS);
266 extern Datum line_eq(PG_FUNCTION_ARGS);
267 
268 /* public box routines */
269 extern Datum box_in(PG_FUNCTION_ARGS);
270 extern Datum box_out(PG_FUNCTION_ARGS);
271 extern Datum box_recv(PG_FUNCTION_ARGS);
272 extern Datum box_send(PG_FUNCTION_ARGS);
273 extern Datum box_same(PG_FUNCTION_ARGS);
274 extern Datum box_overlap(PG_FUNCTION_ARGS);
275 extern Datum box_left(PG_FUNCTION_ARGS);
276 extern Datum box_overleft(PG_FUNCTION_ARGS);
277 extern Datum box_right(PG_FUNCTION_ARGS);
278 extern Datum box_overright(PG_FUNCTION_ARGS);
279 extern Datum box_below(PG_FUNCTION_ARGS);
280 extern Datum box_overbelow(PG_FUNCTION_ARGS);
281 extern Datum box_above(PG_FUNCTION_ARGS);
282 extern Datum box_overabove(PG_FUNCTION_ARGS);
283 extern Datum box_contained(PG_FUNCTION_ARGS);
284 extern Datum box_contain(PG_FUNCTION_ARGS);
285 extern Datum box_contain_pt(PG_FUNCTION_ARGS);
286 extern Datum box_below_eq(PG_FUNCTION_ARGS);
287 extern Datum box_above_eq(PG_FUNCTION_ARGS);
288 extern Datum box_lt(PG_FUNCTION_ARGS);
289 extern Datum box_gt(PG_FUNCTION_ARGS);
290 extern Datum box_eq(PG_FUNCTION_ARGS);
291 extern Datum box_le(PG_FUNCTION_ARGS);
292 extern Datum box_ge(PG_FUNCTION_ARGS);
293 extern Datum box_area(PG_FUNCTION_ARGS);
294 extern Datum box_width(PG_FUNCTION_ARGS);
295 extern Datum box_height(PG_FUNCTION_ARGS);
296 extern Datum box_distance(PG_FUNCTION_ARGS);
297 extern Datum box_center(PG_FUNCTION_ARGS);
298 extern Datum box_intersect(PG_FUNCTION_ARGS);
299 extern Datum box_diagonal(PG_FUNCTION_ARGS);
300 extern Datum points_box(PG_FUNCTION_ARGS);
301 extern Datum box_add(PG_FUNCTION_ARGS);
302 extern Datum box_sub(PG_FUNCTION_ARGS);
303 extern Datum box_mul(PG_FUNCTION_ARGS);
304 extern Datum box_div(PG_FUNCTION_ARGS);
305 extern Datum point_box(PG_FUNCTION_ARGS);
306 extern Datum boxes_bound_box(PG_FUNCTION_ARGS);
307 
308 /* public path routines */
309 extern Datum path_area(PG_FUNCTION_ARGS);
310 extern Datum path_in(PG_FUNCTION_ARGS);
311 extern Datum path_out(PG_FUNCTION_ARGS);
312 extern Datum path_recv(PG_FUNCTION_ARGS);
313 extern Datum path_send(PG_FUNCTION_ARGS);
314 extern Datum path_n_lt(PG_FUNCTION_ARGS);
315 extern Datum path_n_gt(PG_FUNCTION_ARGS);
316 extern Datum path_n_eq(PG_FUNCTION_ARGS);
317 extern Datum path_n_le(PG_FUNCTION_ARGS);
318 extern Datum path_n_ge(PG_FUNCTION_ARGS);
319 extern Datum path_inter(PG_FUNCTION_ARGS);
320 extern Datum path_distance(PG_FUNCTION_ARGS);
321 extern Datum path_length(PG_FUNCTION_ARGS);
322 
323 extern Datum path_isclosed(PG_FUNCTION_ARGS);
324 extern Datum path_isopen(PG_FUNCTION_ARGS);
325 extern Datum path_npoints(PG_FUNCTION_ARGS);
326 
327 extern Datum path_close(PG_FUNCTION_ARGS);
328 extern Datum path_open(PG_FUNCTION_ARGS);
329 extern Datum path_add(PG_FUNCTION_ARGS);
330 extern Datum path_add_pt(PG_FUNCTION_ARGS);
331 extern Datum path_sub_pt(PG_FUNCTION_ARGS);
332 extern Datum path_mul_pt(PG_FUNCTION_ARGS);
333 extern Datum path_div_pt(PG_FUNCTION_ARGS);
334 
335 extern Datum path_center(PG_FUNCTION_ARGS);
336 extern Datum path_poly(PG_FUNCTION_ARGS);
337 
338 /* public polygon routines */
339 extern Datum poly_in(PG_FUNCTION_ARGS);
340 extern Datum poly_out(PG_FUNCTION_ARGS);
341 extern Datum poly_recv(PG_FUNCTION_ARGS);
342 extern Datum poly_send(PG_FUNCTION_ARGS);
343 extern Datum poly_left(PG_FUNCTION_ARGS);
344 extern Datum poly_overleft(PG_FUNCTION_ARGS);
345 extern Datum poly_right(PG_FUNCTION_ARGS);
346 extern Datum poly_overright(PG_FUNCTION_ARGS);
347 extern Datum poly_below(PG_FUNCTION_ARGS);
348 extern Datum poly_overbelow(PG_FUNCTION_ARGS);
349 extern Datum poly_above(PG_FUNCTION_ARGS);
350 extern Datum poly_overabove(PG_FUNCTION_ARGS);
351 extern Datum poly_same(PG_FUNCTION_ARGS);
352 extern Datum poly_overlap(PG_FUNCTION_ARGS);
353 extern Datum poly_contain(PG_FUNCTION_ARGS);
354 extern Datum poly_contained(PG_FUNCTION_ARGS);
355 extern Datum poly_contain_pt(PG_FUNCTION_ARGS);
356 extern Datum pt_contained_poly(PG_FUNCTION_ARGS);
357 extern Datum poly_distance(PG_FUNCTION_ARGS);
358 extern Datum poly_npoints(PG_FUNCTION_ARGS);
359 extern Datum poly_center(PG_FUNCTION_ARGS);
360 extern Datum poly_box(PG_FUNCTION_ARGS);
361 extern Datum poly_path(PG_FUNCTION_ARGS);
362 extern Datum box_poly(PG_FUNCTION_ARGS);
363 
364 /* public circle routines */
365 extern Datum circle_in(PG_FUNCTION_ARGS);
366 extern Datum circle_out(PG_FUNCTION_ARGS);
367 extern Datum circle_recv(PG_FUNCTION_ARGS);
368 extern Datum circle_send(PG_FUNCTION_ARGS);
369 extern Datum circle_same(PG_FUNCTION_ARGS);
370 extern Datum circle_overlap(PG_FUNCTION_ARGS);
371 extern Datum circle_overleft(PG_FUNCTION_ARGS);
372 extern Datum circle_left(PG_FUNCTION_ARGS);
373 extern Datum circle_right(PG_FUNCTION_ARGS);
374 extern Datum circle_overright(PG_FUNCTION_ARGS);
375 extern Datum circle_contained(PG_FUNCTION_ARGS);
376 extern Datum circle_contain(PG_FUNCTION_ARGS);
377 extern Datum circle_below(PG_FUNCTION_ARGS);
378 extern Datum circle_above(PG_FUNCTION_ARGS);
379 extern Datum circle_overbelow(PG_FUNCTION_ARGS);
380 extern Datum circle_overabove(PG_FUNCTION_ARGS);
381 extern Datum circle_eq(PG_FUNCTION_ARGS);
382 extern Datum circle_ne(PG_FUNCTION_ARGS);
383 extern Datum circle_lt(PG_FUNCTION_ARGS);
384 extern Datum circle_gt(PG_FUNCTION_ARGS);
385 extern Datum circle_le(PG_FUNCTION_ARGS);
386 extern Datum circle_ge(PG_FUNCTION_ARGS);
387 extern Datum circle_contain_pt(PG_FUNCTION_ARGS);
388 extern Datum pt_contained_circle(PG_FUNCTION_ARGS);
389 extern Datum circle_add_pt(PG_FUNCTION_ARGS);
390 extern Datum circle_sub_pt(PG_FUNCTION_ARGS);
391 extern Datum circle_mul_pt(PG_FUNCTION_ARGS);
392 extern Datum circle_div_pt(PG_FUNCTION_ARGS);
393 extern Datum circle_diameter(PG_FUNCTION_ARGS);
394 extern Datum circle_radius(PG_FUNCTION_ARGS);
395 extern Datum circle_distance(PG_FUNCTION_ARGS);
396 extern Datum dist_pc(PG_FUNCTION_ARGS);
397 extern Datum dist_cpoint(PG_FUNCTION_ARGS);
398 extern Datum dist_cpoly(PG_FUNCTION_ARGS);
399 extern Datum dist_ppoly(PG_FUNCTION_ARGS);
400 extern Datum dist_polyp(PG_FUNCTION_ARGS);
401 extern Datum circle_center(PG_FUNCTION_ARGS);
402 extern Datum cr_circle(PG_FUNCTION_ARGS);
403 extern Datum box_circle(PG_FUNCTION_ARGS);
404 extern Datum circle_box(PG_FUNCTION_ARGS);
405 extern Datum poly_circle(PG_FUNCTION_ARGS);
406 extern Datum circle_poly(PG_FUNCTION_ARGS);
407 extern Datum circle_area(PG_FUNCTION_ARGS);
408 
409 /* support routines for the GiST access method (access/gist/gistproc.c) */
410 extern Datum gist_box_compress(PG_FUNCTION_ARGS);
411 extern Datum gist_box_decompress(PG_FUNCTION_ARGS);
412 extern Datum gist_box_union(PG_FUNCTION_ARGS);
413 extern Datum gist_box_picksplit(PG_FUNCTION_ARGS);
414 extern Datum gist_box_consistent(PG_FUNCTION_ARGS);
415 extern Datum gist_box_penalty(PG_FUNCTION_ARGS);
416 extern Datum gist_box_same(PG_FUNCTION_ARGS);
417 extern Datum gist_box_fetch(PG_FUNCTION_ARGS);
418 extern Datum gist_poly_compress(PG_FUNCTION_ARGS);
419 extern Datum gist_poly_consistent(PG_FUNCTION_ARGS);
420 extern Datum gist_poly_distance(PG_FUNCTION_ARGS);
421 extern Datum gist_circle_compress(PG_FUNCTION_ARGS);
422 extern Datum gist_circle_consistent(PG_FUNCTION_ARGS);
423 extern Datum gist_circle_distance(PG_FUNCTION_ARGS);
424 extern Datum gist_point_compress(PG_FUNCTION_ARGS);
425 extern Datum gist_point_consistent(PG_FUNCTION_ARGS);
426 extern Datum gist_point_distance(PG_FUNCTION_ARGS);
427 extern Datum gist_point_fetch(PG_FUNCTION_ARGS);
428 
429 /* utils/adt/geo_spgist.c */
430 Datum		spg_box_quad_config(PG_FUNCTION_ARGS);
431 Datum		spg_box_quad_choose(PG_FUNCTION_ARGS);
432 Datum		spg_box_quad_picksplit(PG_FUNCTION_ARGS);
433 Datum		spg_box_quad_inner_consistent(PG_FUNCTION_ARGS);
434 Datum		spg_box_quad_leaf_consistent(PG_FUNCTION_ARGS);
435 
436 /* geo_selfuncs.c */
437 extern Datum areasel(PG_FUNCTION_ARGS);
438 extern Datum areajoinsel(PG_FUNCTION_ARGS);
439 extern Datum positionsel(PG_FUNCTION_ARGS);
440 extern Datum positionjoinsel(PG_FUNCTION_ARGS);
441 extern Datum contsel(PG_FUNCTION_ARGS);
442 extern Datum contjoinsel(PG_FUNCTION_ARGS);
443 
444 #endif   /* GEO_DECLS_H */
445