]> granicus.if.org Git - postgresql/blob - src/include/utils/geo_decls.h
Improve performance of numeric sum(), avg(), stddev(), variance(), etc.
[postgresql] / src / include / utils / geo_decls.h
1 /*-------------------------------------------------------------------------
2  *
3  * geo_decls.h - Declarations for various 2D constructs.
4  *
5  *
6  * Portions Copyright (c) 1996-2013, 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
72         double          m;                              /* precomputed to save time, not in tuple */
73 } LSEG;
74
75
76 /*---------------------------------------------------------------------
77  * PATH - Specified by vertex points.
78  *-------------------------------------------------------------------*/
79 typedef struct
80 {
81         int32           vl_len_;                /* varlena header (do not touch directly!) */
82         int32           npts;
83         int32           closed;                 /* is this a closed polygon? */
84         int32           dummy;                  /* padding to make it double align */
85         Point           p[1];                   /* variable length array of POINTs */
86 } PATH;
87
88
89 /*---------------------------------------------------------------------
90  * LINE - Specified by its general equation (Ax+By+C=0).
91  *-------------------------------------------------------------------*/
92 typedef struct
93 {
94         double          A,
95                                 B,
96                                 C;
97 } LINE;
98
99
100 /*---------------------------------------------------------------------
101  * BOX  - Specified by two corner points, which are
102  *               sorted to save calculation time later.
103  *-------------------------------------------------------------------*/
104 typedef struct
105 {
106         Point           high,
107                                 low;                    /* corner POINTs */
108 } BOX;
109
110 /*---------------------------------------------------------------------
111  * POLYGON - Specified by an array of doubles defining the points,
112  *              keeping the number of points and the bounding box for
113  *              speed purposes.
114  *-------------------------------------------------------------------*/
115 typedef struct
116 {
117         int32           vl_len_;                /* varlena header (do not touch directly!) */
118         int32           npts;
119         BOX                     boundbox;
120         Point           p[1];                   /* variable length array of POINTs */
121 } POLYGON;
122
123 /*---------------------------------------------------------------------
124  * CIRCLE - Specified by a center point and radius.
125  *-------------------------------------------------------------------*/
126 typedef struct
127 {
128         Point           center;
129         double          radius;
130 } CIRCLE;
131
132 /*
133  * fmgr interface macros
134  *
135  * Path and Polygon are toastable varlena types, the others are just
136  * fixed-size pass-by-reference types.
137  */
138
139 #define DatumGetPointP(X)        ((Point *) DatumGetPointer(X))
140 #define PointPGetDatum(X)        PointerGetDatum(X)
141 #define PG_GETARG_POINT_P(n) DatumGetPointP(PG_GETARG_DATUM(n))
142 #define PG_RETURN_POINT_P(x) return PointPGetDatum(x)
143
144 #define DatumGetLsegP(X)        ((LSEG *) DatumGetPointer(X))
145 #define LsegPGetDatum(X)        PointerGetDatum(X)
146 #define PG_GETARG_LSEG_P(n) DatumGetLsegP(PG_GETARG_DATUM(n))
147 #define PG_RETURN_LSEG_P(x) return LsegPGetDatum(x)
148
149 #define DatumGetPathP(X)                 ((PATH *) PG_DETOAST_DATUM(X))
150 #define DatumGetPathPCopy(X)     ((PATH *) PG_DETOAST_DATUM_COPY(X))
151 #define PathPGetDatum(X)                 PointerGetDatum(X)
152 #define PG_GETARG_PATH_P(n)              DatumGetPathP(PG_GETARG_DATUM(n))
153 #define PG_GETARG_PATH_P_COPY(n) DatumGetPathPCopy(PG_GETARG_DATUM(n))
154 #define PG_RETURN_PATH_P(x)              return PathPGetDatum(x)
155
156 #define DatumGetLineP(X)        ((LINE *) DatumGetPointer(X))
157 #define LinePGetDatum(X)        PointerGetDatum(X)
158 #define PG_GETARG_LINE_P(n) DatumGetLineP(PG_GETARG_DATUM(n))
159 #define PG_RETURN_LINE_P(x) return LinePGetDatum(x)
160
161 #define DatumGetBoxP(X)    ((BOX *) DatumGetPointer(X))
162 #define BoxPGetDatum(X)    PointerGetDatum(X)
163 #define PG_GETARG_BOX_P(n) DatumGetBoxP(PG_GETARG_DATUM(n))
164 #define PG_RETURN_BOX_P(x) return BoxPGetDatum(x)
165
166 #define DatumGetPolygonP(X)                     ((POLYGON *) PG_DETOAST_DATUM(X))
167 #define DatumGetPolygonPCopy(X)         ((POLYGON *) PG_DETOAST_DATUM_COPY(X))
168 #define PolygonPGetDatum(X)                     PointerGetDatum(X)
169 #define PG_GETARG_POLYGON_P(n)          DatumGetPolygonP(PG_GETARG_DATUM(n))
170 #define PG_GETARG_POLYGON_P_COPY(n) DatumGetPolygonPCopy(PG_GETARG_DATUM(n))
171 #define PG_RETURN_POLYGON_P(x)          return PolygonPGetDatum(x)
172
173 #define DatumGetCircleP(X)        ((CIRCLE *) DatumGetPointer(X))
174 #define CirclePGetDatum(X)        PointerGetDatum(X)
175 #define PG_GETARG_CIRCLE_P(n) DatumGetCircleP(PG_GETARG_DATUM(n))
176 #define PG_RETURN_CIRCLE_P(x) return CirclePGetDatum(x)
177
178
179 /*
180  * in geo_ops.h
181  */
182
183 /* public point routines */
184 extern Datum point_in(PG_FUNCTION_ARGS);
185 extern Datum point_out(PG_FUNCTION_ARGS);
186 extern Datum point_recv(PG_FUNCTION_ARGS);
187 extern Datum point_send(PG_FUNCTION_ARGS);
188 extern Datum construct_point(PG_FUNCTION_ARGS);
189 extern Datum point_left(PG_FUNCTION_ARGS);
190 extern Datum point_right(PG_FUNCTION_ARGS);
191 extern Datum point_above(PG_FUNCTION_ARGS);
192 extern Datum point_below(PG_FUNCTION_ARGS);
193 extern Datum point_vert(PG_FUNCTION_ARGS);
194 extern Datum point_horiz(PG_FUNCTION_ARGS);
195 extern Datum point_eq(PG_FUNCTION_ARGS);
196 extern Datum point_ne(PG_FUNCTION_ARGS);
197 extern Datum point_distance(PG_FUNCTION_ARGS);
198 extern Datum point_slope(PG_FUNCTION_ARGS);
199 extern Datum point_add(PG_FUNCTION_ARGS);
200 extern Datum point_sub(PG_FUNCTION_ARGS);
201 extern Datum point_mul(PG_FUNCTION_ARGS);
202 extern Datum point_div(PG_FUNCTION_ARGS);
203
204 /* private routines */
205 extern double point_dt(Point *pt1, Point *pt2);
206 extern double point_sl(Point *pt1, Point *pt2);
207 extern double pg_hypot(double x, double y);
208
209 /* public lseg routines */
210 extern Datum lseg_in(PG_FUNCTION_ARGS);
211 extern Datum lseg_out(PG_FUNCTION_ARGS);
212 extern Datum lseg_recv(PG_FUNCTION_ARGS);
213 extern Datum lseg_send(PG_FUNCTION_ARGS);
214 extern Datum lseg_intersect(PG_FUNCTION_ARGS);
215 extern Datum lseg_parallel(PG_FUNCTION_ARGS);
216 extern Datum lseg_perp(PG_FUNCTION_ARGS);
217 extern Datum lseg_vertical(PG_FUNCTION_ARGS);
218 extern Datum lseg_horizontal(PG_FUNCTION_ARGS);
219 extern Datum lseg_eq(PG_FUNCTION_ARGS);
220 extern Datum lseg_ne(PG_FUNCTION_ARGS);
221 extern Datum lseg_lt(PG_FUNCTION_ARGS);
222 extern Datum lseg_le(PG_FUNCTION_ARGS);
223 extern Datum lseg_gt(PG_FUNCTION_ARGS);
224 extern Datum lseg_ge(PG_FUNCTION_ARGS);
225 extern Datum lseg_construct(PG_FUNCTION_ARGS);
226 extern Datum lseg_length(PG_FUNCTION_ARGS);
227 extern Datum lseg_distance(PG_FUNCTION_ARGS);
228 extern Datum lseg_center(PG_FUNCTION_ARGS);
229 extern Datum lseg_interpt(PG_FUNCTION_ARGS);
230 extern Datum dist_pl(PG_FUNCTION_ARGS);
231 extern Datum dist_ps(PG_FUNCTION_ARGS);
232 extern Datum dist_ppath(PG_FUNCTION_ARGS);
233 extern Datum dist_pb(PG_FUNCTION_ARGS);
234 extern Datum dist_sl(PG_FUNCTION_ARGS);
235 extern Datum dist_sb(PG_FUNCTION_ARGS);
236 extern Datum dist_lb(PG_FUNCTION_ARGS);
237 extern Datum close_lseg(PG_FUNCTION_ARGS);
238 extern Datum close_pl(PG_FUNCTION_ARGS);
239 extern Datum close_ps(PG_FUNCTION_ARGS);
240 extern Datum close_pb(PG_FUNCTION_ARGS);
241 extern Datum close_sl(PG_FUNCTION_ARGS);
242 extern Datum close_sb(PG_FUNCTION_ARGS);
243 extern Datum close_ls(PG_FUNCTION_ARGS);
244 extern Datum close_lb(PG_FUNCTION_ARGS);
245 extern Datum on_pl(PG_FUNCTION_ARGS);
246 extern Datum on_ps(PG_FUNCTION_ARGS);
247 extern Datum on_pb(PG_FUNCTION_ARGS);
248 extern Datum on_ppath(PG_FUNCTION_ARGS);
249 extern Datum on_sl(PG_FUNCTION_ARGS);
250 extern Datum on_sb(PG_FUNCTION_ARGS);
251 extern Datum inter_sl(PG_FUNCTION_ARGS);
252 extern Datum inter_sb(PG_FUNCTION_ARGS);
253 extern Datum inter_lb(PG_FUNCTION_ARGS);
254
255 /* public line routines */
256 extern Datum line_in(PG_FUNCTION_ARGS);
257 extern Datum line_out(PG_FUNCTION_ARGS);
258 extern Datum line_recv(PG_FUNCTION_ARGS);
259 extern Datum line_send(PG_FUNCTION_ARGS);
260 extern Datum line_interpt(PG_FUNCTION_ARGS);
261 extern Datum line_distance(PG_FUNCTION_ARGS);
262 extern Datum line_construct_pp(PG_FUNCTION_ARGS);
263 extern Datum line_intersect(PG_FUNCTION_ARGS);
264 extern Datum line_parallel(PG_FUNCTION_ARGS);
265 extern Datum line_perp(PG_FUNCTION_ARGS);
266 extern Datum line_vertical(PG_FUNCTION_ARGS);
267 extern Datum line_horizontal(PG_FUNCTION_ARGS);
268 extern Datum line_eq(PG_FUNCTION_ARGS);
269
270 /* public box routines */
271 extern Datum box_in(PG_FUNCTION_ARGS);
272 extern Datum box_out(PG_FUNCTION_ARGS);
273 extern Datum box_recv(PG_FUNCTION_ARGS);
274 extern Datum box_send(PG_FUNCTION_ARGS);
275 extern Datum box_same(PG_FUNCTION_ARGS);
276 extern Datum box_overlap(PG_FUNCTION_ARGS);
277 extern Datum box_left(PG_FUNCTION_ARGS);
278 extern Datum box_overleft(PG_FUNCTION_ARGS);
279 extern Datum box_right(PG_FUNCTION_ARGS);
280 extern Datum box_overright(PG_FUNCTION_ARGS);
281 extern Datum box_below(PG_FUNCTION_ARGS);
282 extern Datum box_overbelow(PG_FUNCTION_ARGS);
283 extern Datum box_above(PG_FUNCTION_ARGS);
284 extern Datum box_overabove(PG_FUNCTION_ARGS);
285 extern Datum box_contained(PG_FUNCTION_ARGS);
286 extern Datum box_contain(PG_FUNCTION_ARGS);
287 extern Datum box_contain_pt(PG_FUNCTION_ARGS);
288 extern Datum box_below_eq(PG_FUNCTION_ARGS);
289 extern Datum box_above_eq(PG_FUNCTION_ARGS);
290 extern Datum box_lt(PG_FUNCTION_ARGS);
291 extern Datum box_gt(PG_FUNCTION_ARGS);
292 extern Datum box_eq(PG_FUNCTION_ARGS);
293 extern Datum box_le(PG_FUNCTION_ARGS);
294 extern Datum box_ge(PG_FUNCTION_ARGS);
295 extern Datum box_area(PG_FUNCTION_ARGS);
296 extern Datum box_width(PG_FUNCTION_ARGS);
297 extern Datum box_height(PG_FUNCTION_ARGS);
298 extern Datum box_distance(PG_FUNCTION_ARGS);
299 extern Datum box_center(PG_FUNCTION_ARGS);
300 extern Datum box_intersect(PG_FUNCTION_ARGS);
301 extern Datum box_diagonal(PG_FUNCTION_ARGS);
302 extern Datum points_box(PG_FUNCTION_ARGS);
303 extern Datum box_add(PG_FUNCTION_ARGS);
304 extern Datum box_sub(PG_FUNCTION_ARGS);
305 extern Datum box_mul(PG_FUNCTION_ARGS);
306 extern Datum box_div(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_cpoly(PG_FUNCTION_ARGS);
398 extern Datum circle_center(PG_FUNCTION_ARGS);
399 extern Datum cr_circle(PG_FUNCTION_ARGS);
400 extern Datum box_circle(PG_FUNCTION_ARGS);
401 extern Datum circle_box(PG_FUNCTION_ARGS);
402 extern Datum poly_circle(PG_FUNCTION_ARGS);
403 extern Datum circle_poly(PG_FUNCTION_ARGS);
404 extern Datum circle_area(PG_FUNCTION_ARGS);
405
406 /* support routines for the GiST access method (access/gist/gistproc.c) */
407 extern Datum gist_box_compress(PG_FUNCTION_ARGS);
408 extern Datum gist_box_decompress(PG_FUNCTION_ARGS);
409 extern Datum gist_box_union(PG_FUNCTION_ARGS);
410 extern Datum gist_box_picksplit(PG_FUNCTION_ARGS);
411 extern Datum gist_box_consistent(PG_FUNCTION_ARGS);
412 extern Datum gist_box_penalty(PG_FUNCTION_ARGS);
413 extern Datum gist_box_same(PG_FUNCTION_ARGS);
414 extern Datum gist_poly_compress(PG_FUNCTION_ARGS);
415 extern Datum gist_poly_consistent(PG_FUNCTION_ARGS);
416 extern Datum gist_circle_compress(PG_FUNCTION_ARGS);
417 extern Datum gist_circle_consistent(PG_FUNCTION_ARGS);
418 extern Datum gist_point_compress(PG_FUNCTION_ARGS);
419 extern Datum gist_point_consistent(PG_FUNCTION_ARGS);
420 extern Datum gist_point_distance(PG_FUNCTION_ARGS);
421
422 /* geo_selfuncs.c */
423 extern Datum areasel(PG_FUNCTION_ARGS);
424 extern Datum areajoinsel(PG_FUNCTION_ARGS);
425 extern Datum positionsel(PG_FUNCTION_ARGS);
426 extern Datum positionjoinsel(PG_FUNCTION_ARGS);
427 extern Datum contsel(PG_FUNCTION_ARGS);
428 extern Datum contjoinsel(PG_FUNCTION_ARGS);
429
430 #endif   /* GEO_DECLS_H */