1 /*-------------------------------------------------------------------------
3 * geo_decls.h - Declarations for various 2D constructs.
6 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
12 * These routines do *not* use the float types from adt/.
14 * XXX These routines were not written by a numerical analyst.
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
19 *-------------------------------------------------------------------------
28 /*--------------------------------------------------------------------
29 * Useful floating point utilities and constants.
30 *-------------------------------------------------------------------*/
33 #define EPSILON 1.0E-06
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)
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))
53 #define HYPOT(A, B) sqrt((A) * (A) + (B) * (B))
55 /*---------------------------------------------------------------------
57 *-------------------------------------------------------------------*/
65 /*---------------------------------------------------------------------
66 * LSEG - A straight line, specified by endpoints.
67 *-------------------------------------------------------------------*/
72 double m
; /* precomputed to save time, not in tuple */
76 /*---------------------------------------------------------------------
77 * PATH - Specified by vertex points.
78 *-------------------------------------------------------------------*/
81 int32 vl_len_
; /* varlena header (do not touch directly!) */
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 */
89 /*---------------------------------------------------------------------
90 * LINE - Specified by its general equation (Ax+By+C=0).
91 * If there is a y-intercept, it is C, which
92 * incidentally gives a freebie point on the line
93 * (if B=0, then C is the x-intercept).
94 * Slope m is precalculated to save time; if
95 * the line is not vertical, m == A.
96 *-------------------------------------------------------------------*/
107 /*---------------------------------------------------------------------
108 * BOX - Specified by two corner points, which are
109 * sorted to save calculation time later.
110 *-------------------------------------------------------------------*/
114 low
; /* corner POINTs */
117 /*---------------------------------------------------------------------
118 * POLYGON - Specified by an array of doubles defining the points,
119 * keeping the number of points and the bounding box for
121 *-------------------------------------------------------------------*/
124 int32 vl_len_
; /* varlena header (do not touch directly!) */
127 Point p
[1]; /* variable length array of POINTs */
130 /*---------------------------------------------------------------------
131 * CIRCLE - Specified by a center point and radius.
132 *-------------------------------------------------------------------*/
140 * fmgr interface macros
142 * Path and Polygon are toastable varlena types, the others are just
143 * fixed-size pass-by-reference types.
146 #define DatumGetPointP(X) ((Point *) DatumGetPointer(X))
147 #define PointPGetDatum(X) PointerGetDatum(X)
148 #define PG_GETARG_POINT_P(n) DatumGetPointP(PG_GETARG_DATUM(n))
149 #define PG_RETURN_POINT_P(x) return PointPGetDatum(x)
151 #define DatumGetLsegP(X) ((LSEG *) DatumGetPointer(X))
152 #define LsegPGetDatum(X) PointerGetDatum(X)
153 #define PG_GETARG_LSEG_P(n) DatumGetLsegP(PG_GETARG_DATUM(n))
154 #define PG_RETURN_LSEG_P(x) return LsegPGetDatum(x)
156 #define DatumGetPathP(X) ((PATH *) PG_DETOAST_DATUM(X))
157 #define DatumGetPathPCopy(X) ((PATH *) PG_DETOAST_DATUM_COPY(X))
158 #define PathPGetDatum(X) PointerGetDatum(X)
159 #define PG_GETARG_PATH_P(n) DatumGetPathP(PG_GETARG_DATUM(n))
160 #define PG_GETARG_PATH_P_COPY(n) DatumGetPathPCopy(PG_GETARG_DATUM(n))
161 #define PG_RETURN_PATH_P(x) return PathPGetDatum(x)
163 #define DatumGetLineP(X) ((LINE *) DatumGetPointer(X))
164 #define LinePGetDatum(X) PointerGetDatum(X)
165 #define PG_GETARG_LINE_P(n) DatumGetLineP(PG_GETARG_DATUM(n))
166 #define PG_RETURN_LINE_P(x) return LinePGetDatum(x)
168 #define DatumGetBoxP(X) ((BOX *) DatumGetPointer(X))
169 #define BoxPGetDatum(X) PointerGetDatum(X)
170 #define PG_GETARG_BOX_P(n) DatumGetBoxP(PG_GETARG_DATUM(n))
171 #define PG_RETURN_BOX_P(x) return BoxPGetDatum(x)
173 #define DatumGetPolygonP(X) ((POLYGON *) PG_DETOAST_DATUM(X))
174 #define DatumGetPolygonPCopy(X) ((POLYGON *) PG_DETOAST_DATUM_COPY(X))
175 #define PolygonPGetDatum(X) PointerGetDatum(X)
176 #define PG_GETARG_POLYGON_P(n) DatumGetPolygonP(PG_GETARG_DATUM(n))
177 #define PG_GETARG_POLYGON_P_COPY(n) DatumGetPolygonPCopy(PG_GETARG_DATUM(n))
178 #define PG_RETURN_POLYGON_P(x) return PolygonPGetDatum(x)
180 #define DatumGetCircleP(X) ((CIRCLE *) DatumGetPointer(X))
181 #define CirclePGetDatum(X) PointerGetDatum(X)
182 #define PG_GETARG_CIRCLE_P(n) DatumGetCircleP(PG_GETARG_DATUM(n))
183 #define PG_RETURN_CIRCLE_P(x) return CirclePGetDatum(x)
190 /* public point routines */
191 extern Datum
point_in(PG_FUNCTION_ARGS
);
192 extern Datum
point_out(PG_FUNCTION_ARGS
);
193 extern Datum
point_recv(PG_FUNCTION_ARGS
);
194 extern Datum
point_send(PG_FUNCTION_ARGS
);
195 extern Datum
construct_point(PG_FUNCTION_ARGS
);
196 extern Datum
point_left(PG_FUNCTION_ARGS
);
197 extern Datum
point_right(PG_FUNCTION_ARGS
);
198 extern Datum
point_above(PG_FUNCTION_ARGS
);
199 extern Datum
point_below(PG_FUNCTION_ARGS
);
200 extern Datum
point_vert(PG_FUNCTION_ARGS
);
201 extern Datum
point_horiz(PG_FUNCTION_ARGS
);
202 extern Datum
point_eq(PG_FUNCTION_ARGS
);
203 extern Datum
point_ne(PG_FUNCTION_ARGS
);
204 extern Datum
point_distance(PG_FUNCTION_ARGS
);
205 extern Datum
point_slope(PG_FUNCTION_ARGS
);
206 extern Datum
point_add(PG_FUNCTION_ARGS
);
207 extern Datum
point_sub(PG_FUNCTION_ARGS
);
208 extern Datum
point_mul(PG_FUNCTION_ARGS
);
209 extern Datum
point_div(PG_FUNCTION_ARGS
);
211 /* private routines */
212 extern double point_dt(Point
*pt1
, Point
*pt2
);
213 extern double point_sl(Point
*pt1
, Point
*pt2
);
215 /* public lseg routines */
216 extern Datum
lseg_in(PG_FUNCTION_ARGS
);
217 extern Datum
lseg_out(PG_FUNCTION_ARGS
);
218 extern Datum
lseg_recv(PG_FUNCTION_ARGS
);
219 extern Datum
lseg_send(PG_FUNCTION_ARGS
);
220 extern Datum
lseg_intersect(PG_FUNCTION_ARGS
);
221 extern Datum
lseg_parallel(PG_FUNCTION_ARGS
);
222 extern Datum
lseg_perp(PG_FUNCTION_ARGS
);
223 extern Datum
lseg_vertical(PG_FUNCTION_ARGS
);
224 extern Datum
lseg_horizontal(PG_FUNCTION_ARGS
);
225 extern Datum
lseg_eq(PG_FUNCTION_ARGS
);
226 extern Datum
lseg_ne(PG_FUNCTION_ARGS
);
227 extern Datum
lseg_lt(PG_FUNCTION_ARGS
);
228 extern Datum
lseg_le(PG_FUNCTION_ARGS
);
229 extern Datum
lseg_gt(PG_FUNCTION_ARGS
);
230 extern Datum
lseg_ge(PG_FUNCTION_ARGS
);
231 extern Datum
lseg_construct(PG_FUNCTION_ARGS
);
232 extern Datum
lseg_length(PG_FUNCTION_ARGS
);
233 extern Datum
lseg_distance(PG_FUNCTION_ARGS
);
234 extern Datum
lseg_center(PG_FUNCTION_ARGS
);
235 extern Datum
lseg_interpt(PG_FUNCTION_ARGS
);
236 extern Datum
dist_pl(PG_FUNCTION_ARGS
);
237 extern Datum
dist_ps(PG_FUNCTION_ARGS
);
238 extern Datum
dist_ppath(PG_FUNCTION_ARGS
);
239 extern Datum
dist_pb(PG_FUNCTION_ARGS
);
240 extern Datum
dist_sl(PG_FUNCTION_ARGS
);
241 extern Datum
dist_sb(PG_FUNCTION_ARGS
);
242 extern Datum
dist_lb(PG_FUNCTION_ARGS
);
243 extern Datum
close_lseg(PG_FUNCTION_ARGS
);
244 extern Datum
close_pl(PG_FUNCTION_ARGS
);
245 extern Datum
close_ps(PG_FUNCTION_ARGS
);
246 extern Datum
close_pb(PG_FUNCTION_ARGS
);
247 extern Datum
close_sl(PG_FUNCTION_ARGS
);
248 extern Datum
close_sb(PG_FUNCTION_ARGS
);
249 extern Datum
close_ls(PG_FUNCTION_ARGS
);
250 extern Datum
close_lb(PG_FUNCTION_ARGS
);
251 extern Datum
on_pl(PG_FUNCTION_ARGS
);
252 extern Datum
on_ps(PG_FUNCTION_ARGS
);
253 extern Datum
on_pb(PG_FUNCTION_ARGS
);
254 extern Datum
on_ppath(PG_FUNCTION_ARGS
);
255 extern Datum
on_sl(PG_FUNCTION_ARGS
);
256 extern Datum
on_sb(PG_FUNCTION_ARGS
);
257 extern Datum
inter_sl(PG_FUNCTION_ARGS
);
258 extern Datum
inter_sb(PG_FUNCTION_ARGS
);
259 extern Datum
inter_lb(PG_FUNCTION_ARGS
);
261 /* public line routines */
262 extern Datum
line_in(PG_FUNCTION_ARGS
);
263 extern Datum
line_out(PG_FUNCTION_ARGS
);
264 extern Datum
line_recv(PG_FUNCTION_ARGS
);
265 extern Datum
line_send(PG_FUNCTION_ARGS
);
266 extern Datum
line_interpt(PG_FUNCTION_ARGS
);
267 extern Datum
line_distance(PG_FUNCTION_ARGS
);
268 extern Datum
line_construct_pp(PG_FUNCTION_ARGS
);
269 extern Datum
line_intersect(PG_FUNCTION_ARGS
);
270 extern Datum
line_parallel(PG_FUNCTION_ARGS
);
271 extern Datum
line_perp(PG_FUNCTION_ARGS
);
272 extern Datum
line_vertical(PG_FUNCTION_ARGS
);
273 extern Datum
line_horizontal(PG_FUNCTION_ARGS
);
274 extern Datum
line_eq(PG_FUNCTION_ARGS
);
276 /* public box routines */
277 extern Datum
box_in(PG_FUNCTION_ARGS
);
278 extern Datum
box_out(PG_FUNCTION_ARGS
);
279 extern Datum
box_recv(PG_FUNCTION_ARGS
);
280 extern Datum
box_send(PG_FUNCTION_ARGS
);
281 extern Datum
box_same(PG_FUNCTION_ARGS
);
282 extern Datum
box_overlap(PG_FUNCTION_ARGS
);
283 extern Datum
box_left(PG_FUNCTION_ARGS
);
284 extern Datum
box_overleft(PG_FUNCTION_ARGS
);
285 extern Datum
box_right(PG_FUNCTION_ARGS
);
286 extern Datum
box_overright(PG_FUNCTION_ARGS
);
287 extern Datum
box_below(PG_FUNCTION_ARGS
);
288 extern Datum
box_overbelow(PG_FUNCTION_ARGS
);
289 extern Datum
box_above(PG_FUNCTION_ARGS
);
290 extern Datum
box_overabove(PG_FUNCTION_ARGS
);
291 extern Datum
box_contained(PG_FUNCTION_ARGS
);
292 extern Datum
box_contain(PG_FUNCTION_ARGS
);
293 extern Datum
box_below_eq(PG_FUNCTION_ARGS
);
294 extern Datum
box_above_eq(PG_FUNCTION_ARGS
);
295 extern Datum
box_lt(PG_FUNCTION_ARGS
);
296 extern Datum
box_gt(PG_FUNCTION_ARGS
);
297 extern Datum
box_eq(PG_FUNCTION_ARGS
);
298 extern Datum
box_le(PG_FUNCTION_ARGS
);
299 extern Datum
box_ge(PG_FUNCTION_ARGS
);
300 extern Datum
box_area(PG_FUNCTION_ARGS
);
301 extern Datum
box_width(PG_FUNCTION_ARGS
);
302 extern Datum
box_height(PG_FUNCTION_ARGS
);
303 extern Datum
box_distance(PG_FUNCTION_ARGS
);
304 extern Datum
box_center(PG_FUNCTION_ARGS
);
305 extern Datum
box_intersect(PG_FUNCTION_ARGS
);
306 extern Datum
box_diagonal(PG_FUNCTION_ARGS
);
307 extern Datum
points_box(PG_FUNCTION_ARGS
);
308 extern Datum
box_add(PG_FUNCTION_ARGS
);
309 extern Datum
box_sub(PG_FUNCTION_ARGS
);
310 extern Datum
box_mul(PG_FUNCTION_ARGS
);
311 extern Datum
box_div(PG_FUNCTION_ARGS
);
313 /* public path routines */
314 extern Datum
path_area(PG_FUNCTION_ARGS
);
315 extern Datum
path_in(PG_FUNCTION_ARGS
);
316 extern Datum
path_out(PG_FUNCTION_ARGS
);
317 extern Datum
path_recv(PG_FUNCTION_ARGS
);
318 extern Datum
path_send(PG_FUNCTION_ARGS
);
319 extern Datum
path_n_lt(PG_FUNCTION_ARGS
);
320 extern Datum
path_n_gt(PG_FUNCTION_ARGS
);
321 extern Datum
path_n_eq(PG_FUNCTION_ARGS
);
322 extern Datum
path_n_le(PG_FUNCTION_ARGS
);
323 extern Datum
path_n_ge(PG_FUNCTION_ARGS
);
324 extern Datum
path_inter(PG_FUNCTION_ARGS
);
325 extern Datum
path_distance(PG_FUNCTION_ARGS
);
326 extern Datum
path_length(PG_FUNCTION_ARGS
);
328 extern Datum
path_isclosed(PG_FUNCTION_ARGS
);
329 extern Datum
path_isopen(PG_FUNCTION_ARGS
);
330 extern Datum
path_npoints(PG_FUNCTION_ARGS
);
332 extern Datum
path_close(PG_FUNCTION_ARGS
);
333 extern Datum
path_open(PG_FUNCTION_ARGS
);
334 extern Datum
path_add(PG_FUNCTION_ARGS
);
335 extern Datum
path_add_pt(PG_FUNCTION_ARGS
);
336 extern Datum
path_sub_pt(PG_FUNCTION_ARGS
);
337 extern Datum
path_mul_pt(PG_FUNCTION_ARGS
);
338 extern Datum
path_div_pt(PG_FUNCTION_ARGS
);
340 extern Datum
path_center(PG_FUNCTION_ARGS
);
341 extern Datum
path_poly(PG_FUNCTION_ARGS
);
343 /* public polygon routines */
344 extern Datum
poly_in(PG_FUNCTION_ARGS
);
345 extern Datum
poly_out(PG_FUNCTION_ARGS
);
346 extern Datum
poly_recv(PG_FUNCTION_ARGS
);
347 extern Datum
poly_send(PG_FUNCTION_ARGS
);
348 extern Datum
poly_left(PG_FUNCTION_ARGS
);
349 extern Datum
poly_overleft(PG_FUNCTION_ARGS
);
350 extern Datum
poly_right(PG_FUNCTION_ARGS
);
351 extern Datum
poly_overright(PG_FUNCTION_ARGS
);
352 extern Datum
poly_below(PG_FUNCTION_ARGS
);
353 extern Datum
poly_overbelow(PG_FUNCTION_ARGS
);
354 extern Datum
poly_above(PG_FUNCTION_ARGS
);
355 extern Datum
poly_overabove(PG_FUNCTION_ARGS
);
356 extern Datum
poly_same(PG_FUNCTION_ARGS
);
357 extern Datum
poly_overlap(PG_FUNCTION_ARGS
);
358 extern Datum
poly_contain(PG_FUNCTION_ARGS
);
359 extern Datum
poly_contained(PG_FUNCTION_ARGS
);
360 extern Datum
poly_contain_pt(PG_FUNCTION_ARGS
);
361 extern Datum
pt_contained_poly(PG_FUNCTION_ARGS
);
362 extern Datum
poly_distance(PG_FUNCTION_ARGS
);
363 extern Datum
poly_npoints(PG_FUNCTION_ARGS
);
364 extern Datum
poly_center(PG_FUNCTION_ARGS
);
365 extern Datum
poly_box(PG_FUNCTION_ARGS
);
366 extern Datum
poly_path(PG_FUNCTION_ARGS
);
367 extern Datum
box_poly(PG_FUNCTION_ARGS
);
369 /* public circle routines */
370 extern Datum
circle_in(PG_FUNCTION_ARGS
);
371 extern Datum
circle_out(PG_FUNCTION_ARGS
);
372 extern Datum
circle_recv(PG_FUNCTION_ARGS
);
373 extern Datum
circle_send(PG_FUNCTION_ARGS
);
374 extern Datum
circle_same(PG_FUNCTION_ARGS
);
375 extern Datum
circle_overlap(PG_FUNCTION_ARGS
);
376 extern Datum
circle_overleft(PG_FUNCTION_ARGS
);
377 extern Datum
circle_left(PG_FUNCTION_ARGS
);
378 extern Datum
circle_right(PG_FUNCTION_ARGS
);
379 extern Datum
circle_overright(PG_FUNCTION_ARGS
);
380 extern Datum
circle_contained(PG_FUNCTION_ARGS
);
381 extern Datum
circle_contain(PG_FUNCTION_ARGS
);
382 extern Datum
circle_below(PG_FUNCTION_ARGS
);
383 extern Datum
circle_above(PG_FUNCTION_ARGS
);
384 extern Datum
circle_overbelow(PG_FUNCTION_ARGS
);
385 extern Datum
circle_overabove(PG_FUNCTION_ARGS
);
386 extern Datum
circle_eq(PG_FUNCTION_ARGS
);
387 extern Datum
circle_ne(PG_FUNCTION_ARGS
);
388 extern Datum
circle_lt(PG_FUNCTION_ARGS
);
389 extern Datum
circle_gt(PG_FUNCTION_ARGS
);
390 extern Datum
circle_le(PG_FUNCTION_ARGS
);
391 extern Datum
circle_ge(PG_FUNCTION_ARGS
);
392 extern Datum
circle_contain_pt(PG_FUNCTION_ARGS
);
393 extern Datum
pt_contained_circle(PG_FUNCTION_ARGS
);
394 extern Datum
circle_add_pt(PG_FUNCTION_ARGS
);
395 extern Datum
circle_sub_pt(PG_FUNCTION_ARGS
);
396 extern Datum
circle_mul_pt(PG_FUNCTION_ARGS
);
397 extern Datum
circle_div_pt(PG_FUNCTION_ARGS
);
398 extern Datum
circle_diameter(PG_FUNCTION_ARGS
);
399 extern Datum
circle_radius(PG_FUNCTION_ARGS
);
400 extern Datum
circle_distance(PG_FUNCTION_ARGS
);
401 extern Datum
dist_pc(PG_FUNCTION_ARGS
);
402 extern Datum
dist_cpoly(PG_FUNCTION_ARGS
);
403 extern Datum
circle_center(PG_FUNCTION_ARGS
);
404 extern Datum
cr_circle(PG_FUNCTION_ARGS
);
405 extern Datum
box_circle(PG_FUNCTION_ARGS
);
406 extern Datum
circle_box(PG_FUNCTION_ARGS
);
407 extern Datum
poly_circle(PG_FUNCTION_ARGS
);
408 extern Datum
circle_poly(PG_FUNCTION_ARGS
);
409 extern Datum
circle_area(PG_FUNCTION_ARGS
);
411 /* support routines for the GiST access method (access/gist/gistproc.c) */
412 extern Datum
gist_box_compress(PG_FUNCTION_ARGS
);
413 extern Datum
gist_box_decompress(PG_FUNCTION_ARGS
);
414 extern Datum
gist_box_union(PG_FUNCTION_ARGS
);
415 extern Datum
gist_box_picksplit(PG_FUNCTION_ARGS
);
416 extern Datum
gist_box_consistent(PG_FUNCTION_ARGS
);
417 extern Datum
gist_box_penalty(PG_FUNCTION_ARGS
);
418 extern Datum
gist_box_same(PG_FUNCTION_ARGS
);
419 extern Datum
gist_poly_compress(PG_FUNCTION_ARGS
);
420 extern Datum
gist_poly_consistent(PG_FUNCTION_ARGS
);
421 extern Datum
gist_circle_compress(PG_FUNCTION_ARGS
);
422 extern Datum
gist_circle_consistent(PG_FUNCTION_ARGS
);
425 extern Datum
areasel(PG_FUNCTION_ARGS
);
426 extern Datum
areajoinsel(PG_FUNCTION_ARGS
);
427 extern Datum
positionsel(PG_FUNCTION_ARGS
);
428 extern Datum
positionjoinsel(PG_FUNCTION_ARGS
);
429 extern Datum
contsel(PG_FUNCTION_ARGS
);
430 extern Datum
contjoinsel(PG_FUNCTION_ARGS
);
432 #endif /* GEO_DECLS_H */