1 /*-------------------------------------------------------------------------
3 * geo_decls.h - Declarations for various 2D constructs.
6 * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
9 * $Id: geo_decls.h,v 1.41 2003/08/04 02:40:15 momjian Exp $
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 *-------------------------------------------------------------------------
26 /*--------------------------------------------------------------------
27 * Useful floating point utilities and constants.
28 *-------------------------------------------------------------------*/
31 #define EPSILON 1.0E-06
34 #define FPzero(A) (fabs(A) <= EPSILON)
35 #define FPeq(A,B) (fabs((A) - (B)) <= EPSILON)
36 #define FPne(A,B) (fabs((A) - (B)) > EPSILON)
37 #define FPlt(A,B) ((B) - (A) > EPSILON)
38 #define FPle(A,B) ((A) - (B) <= EPSILON)
39 #define FPgt(A,B) ((A) - (B) > EPSILON)
40 #define FPge(A,B) ((B) - (A) <= EPSILON)
42 #define FPzero(A) ((A) == 0)
43 #define FPeq(A,B) ((A) == (B))
44 #define FPne(A,B) ((A) != (B))
45 #define FPlt(A,B) ((A) < (B))
46 #define FPle(A,B) ((A) <= (B))
47 #define FPgt(A,B) ((A) > (B))
48 #define FPge(A,B) ((A) >= (B))
51 #define HYPOT(A, B) sqrt((A) * (A) + (B) * (B))
53 /*---------------------------------------------------------------------
55 *-------------------------------------------------------------------*/
63 /*---------------------------------------------------------------------
64 * LSEG - A straight line, specified by endpoints.
65 *-------------------------------------------------------------------*/
70 double m; /* precomputed to save time, not in tuple */
74 /*---------------------------------------------------------------------
75 * PATH - Specified by vertex points.
76 *-------------------------------------------------------------------*/
79 int32 size; /* XXX varlena */
81 int32 closed; /* is this a closed polygon? */
82 int32 dummy; /* padding to make it double align */
83 Point p[1]; /* variable length array of POINTs */
87 /*---------------------------------------------------------------------
88 * LINE - Specified by its general equation (Ax+By+C=0).
89 * If there is a y-intercept, it is C, which
90 * incidentally gives a freebie point on the line
91 * (if B=0, then C is the x-intercept).
92 * Slope m is precalculated to save time; if
93 * the line is not vertical, m == A.
94 *-------------------------------------------------------------------*/
105 /*---------------------------------------------------------------------
106 * BOX - Specified by two corner points, which are
107 * sorted to save calculation time later.
108 *-------------------------------------------------------------------*/
112 low; /* corner POINTs */
115 /*---------------------------------------------------------------------
116 * POLYGON - Specified by an array of doubles defining the points,
117 * keeping the number of points and the bounding box for
119 *-------------------------------------------------------------------*/
122 int32 size; /* XXX varlena */
125 Point p[1]; /* variable length array of POINTs */
128 /*---------------------------------------------------------------------
129 * CIRCLE - Specified by a center point and radius.
130 *-------------------------------------------------------------------*/
138 * fmgr interface macros
140 * Path and Polygon are toastable varlena types, the others are just
141 * fixed-size pass-by-reference types.
144 #define DatumGetPointP(X) ((Point *) DatumGetPointer(X))
145 #define PointPGetDatum(X) PointerGetDatum(X)
146 #define PG_GETARG_POINT_P(n) DatumGetPointP(PG_GETARG_DATUM(n))
147 #define PG_RETURN_POINT_P(x) return PointPGetDatum(x)
149 #define DatumGetLsegP(X) ((LSEG *) DatumGetPointer(X))
150 #define LsegPGetDatum(X) PointerGetDatum(X)
151 #define PG_GETARG_LSEG_P(n) DatumGetLsegP(PG_GETARG_DATUM(n))
152 #define PG_RETURN_LSEG_P(x) return LsegPGetDatum(x)
154 #define DatumGetPathP(X) ((PATH *) PG_DETOAST_DATUM(X))
155 #define DatumGetPathPCopy(X) ((PATH *) PG_DETOAST_DATUM_COPY(X))
156 #define PathPGetDatum(X) PointerGetDatum(X)
157 #define PG_GETARG_PATH_P(n) DatumGetPathP(PG_GETARG_DATUM(n))
158 #define PG_GETARG_PATH_P_COPY(n) DatumGetPathPCopy(PG_GETARG_DATUM(n))
159 #define PG_RETURN_PATH_P(x) return PathPGetDatum(x)
161 #define DatumGetLineP(X) ((LINE *) DatumGetPointer(X))
162 #define LinePGetDatum(X) PointerGetDatum(X)
163 #define PG_GETARG_LINE_P(n) DatumGetLineP(PG_GETARG_DATUM(n))
164 #define PG_RETURN_LINE_P(x) return LinePGetDatum(x)
166 #define DatumGetBoxP(X) ((BOX *) DatumGetPointer(X))
167 #define BoxPGetDatum(X) PointerGetDatum(X)
168 #define PG_GETARG_BOX_P(n) DatumGetBoxP(PG_GETARG_DATUM(n))
169 #define PG_RETURN_BOX_P(x) return BoxPGetDatum(x)
171 #define DatumGetPolygonP(X) ((POLYGON *) PG_DETOAST_DATUM(X))
172 #define DatumGetPolygonPCopy(X) ((POLYGON *) PG_DETOAST_DATUM_COPY(X))
173 #define PolygonPGetDatum(X) PointerGetDatum(X)
174 #define PG_GETARG_POLYGON_P(n) DatumGetPolygonP(PG_GETARG_DATUM(n))
175 #define PG_GETARG_POLYGON_P_COPY(n) DatumGetPolygonPCopy(PG_GETARG_DATUM(n))
176 #define PG_RETURN_POLYGON_P(x) return PolygonPGetDatum(x)
178 #define DatumGetCircleP(X) ((CIRCLE *) DatumGetPointer(X))
179 #define CirclePGetDatum(X) PointerGetDatum(X)
180 #define PG_GETARG_CIRCLE_P(n) DatumGetCircleP(PG_GETARG_DATUM(n))
181 #define PG_RETURN_CIRCLE_P(x) return CirclePGetDatum(x)
188 /* public point routines */
189 extern Datum point_in(PG_FUNCTION_ARGS);
190 extern Datum point_out(PG_FUNCTION_ARGS);
191 extern Datum point_recv(PG_FUNCTION_ARGS);
192 extern Datum point_send(PG_FUNCTION_ARGS);
193 extern Datum construct_point(PG_FUNCTION_ARGS);
194 extern Datum point_left(PG_FUNCTION_ARGS);
195 extern Datum point_right(PG_FUNCTION_ARGS);
196 extern Datum point_above(PG_FUNCTION_ARGS);
197 extern Datum point_below(PG_FUNCTION_ARGS);
198 extern Datum point_vert(PG_FUNCTION_ARGS);
199 extern Datum point_horiz(PG_FUNCTION_ARGS);
200 extern Datum point_eq(PG_FUNCTION_ARGS);
201 extern Datum point_ne(PG_FUNCTION_ARGS);
202 extern Datum point_distance(PG_FUNCTION_ARGS);
203 extern Datum point_slope(PG_FUNCTION_ARGS);
204 extern Datum point_add(PG_FUNCTION_ARGS);
205 extern Datum point_sub(PG_FUNCTION_ARGS);
206 extern Datum point_mul(PG_FUNCTION_ARGS);
207 extern Datum point_div(PG_FUNCTION_ARGS);
209 /* private routines */
210 extern double point_dt(Point *pt1, Point *pt2);
211 extern double point_sl(Point *pt1, Point *pt2);
213 /* public lseg routines */
214 extern Datum lseg_in(PG_FUNCTION_ARGS);
215 extern Datum lseg_out(PG_FUNCTION_ARGS);
216 extern Datum lseg_recv(PG_FUNCTION_ARGS);
217 extern Datum lseg_send(PG_FUNCTION_ARGS);
218 extern Datum lseg_intersect(PG_FUNCTION_ARGS);
219 extern Datum lseg_parallel(PG_FUNCTION_ARGS);
220 extern Datum lseg_perp(PG_FUNCTION_ARGS);
221 extern Datum lseg_vertical(PG_FUNCTION_ARGS);
222 extern Datum lseg_horizontal(PG_FUNCTION_ARGS);
223 extern Datum lseg_eq(PG_FUNCTION_ARGS);
224 extern Datum lseg_ne(PG_FUNCTION_ARGS);
225 extern Datum lseg_lt(PG_FUNCTION_ARGS);
226 extern Datum lseg_le(PG_FUNCTION_ARGS);
227 extern Datum lseg_gt(PG_FUNCTION_ARGS);
228 extern Datum lseg_ge(PG_FUNCTION_ARGS);
229 extern Datum lseg_construct(PG_FUNCTION_ARGS);
230 extern Datum lseg_length(PG_FUNCTION_ARGS);
231 extern Datum lseg_distance(PG_FUNCTION_ARGS);
232 extern Datum lseg_center(PG_FUNCTION_ARGS);
233 extern Datum lseg_interpt(PG_FUNCTION_ARGS);
234 extern Datum dist_pl(PG_FUNCTION_ARGS);
235 extern Datum dist_ps(PG_FUNCTION_ARGS);
236 extern Datum dist_ppath(PG_FUNCTION_ARGS);
237 extern Datum dist_pb(PG_FUNCTION_ARGS);
238 extern Datum dist_sl(PG_FUNCTION_ARGS);
239 extern Datum dist_sb(PG_FUNCTION_ARGS);
240 extern Datum dist_lb(PG_FUNCTION_ARGS);
241 extern Datum close_lseg(PG_FUNCTION_ARGS);
242 extern Datum close_pl(PG_FUNCTION_ARGS);
243 extern Datum close_ps(PG_FUNCTION_ARGS);
244 extern Datum close_pb(PG_FUNCTION_ARGS);
245 extern Datum close_sl(PG_FUNCTION_ARGS);
246 extern Datum close_sb(PG_FUNCTION_ARGS);
247 extern Datum close_ls(PG_FUNCTION_ARGS);
248 extern Datum close_lb(PG_FUNCTION_ARGS);
249 extern Datum on_pl(PG_FUNCTION_ARGS);
250 extern Datum on_ps(PG_FUNCTION_ARGS);
251 extern Datum on_pb(PG_FUNCTION_ARGS);
252 extern Datum on_ppath(PG_FUNCTION_ARGS);
253 extern Datum on_sl(PG_FUNCTION_ARGS);
254 extern Datum on_sb(PG_FUNCTION_ARGS);
255 extern Datum inter_sl(PG_FUNCTION_ARGS);
256 extern Datum inter_sb(PG_FUNCTION_ARGS);
257 extern Datum inter_lb(PG_FUNCTION_ARGS);
259 /* public line routines */
260 extern Datum line_in(PG_FUNCTION_ARGS);
261 extern Datum line_out(PG_FUNCTION_ARGS);
262 extern Datum line_recv(PG_FUNCTION_ARGS);
263 extern Datum line_send(PG_FUNCTION_ARGS);
264 extern Datum line_interpt(PG_FUNCTION_ARGS);
265 extern Datum line_distance(PG_FUNCTION_ARGS);
266 extern Datum line_construct_pp(PG_FUNCTION_ARGS);
267 extern Datum line_intersect(PG_FUNCTION_ARGS);
268 extern Datum line_parallel(PG_FUNCTION_ARGS);
269 extern Datum line_perp(PG_FUNCTION_ARGS);
270 extern Datum line_vertical(PG_FUNCTION_ARGS);
271 extern Datum line_horizontal(PG_FUNCTION_ARGS);
272 extern Datum line_eq(PG_FUNCTION_ARGS);
274 /* public box routines */
275 extern Datum box_in(PG_FUNCTION_ARGS);
276 extern Datum box_out(PG_FUNCTION_ARGS);
277 extern Datum box_recv(PG_FUNCTION_ARGS);
278 extern Datum box_send(PG_FUNCTION_ARGS);
279 extern Datum box_same(PG_FUNCTION_ARGS);
280 extern Datum box_overlap(PG_FUNCTION_ARGS);
281 extern Datum box_overleft(PG_FUNCTION_ARGS);
282 extern Datum box_left(PG_FUNCTION_ARGS);
283 extern Datum box_right(PG_FUNCTION_ARGS);
284 extern Datum box_overright(PG_FUNCTION_ARGS);
285 extern Datum box_contained(PG_FUNCTION_ARGS);
286 extern Datum box_contain(PG_FUNCTION_ARGS);
287 extern Datum box_below(PG_FUNCTION_ARGS);
288 extern Datum box_above(PG_FUNCTION_ARGS);
289 extern Datum box_lt(PG_FUNCTION_ARGS);
290 extern Datum box_gt(PG_FUNCTION_ARGS);
291 extern Datum box_eq(PG_FUNCTION_ARGS);
292 extern Datum box_le(PG_FUNCTION_ARGS);
293 extern Datum box_ge(PG_FUNCTION_ARGS);
294 extern Datum box_area(PG_FUNCTION_ARGS);
295 extern Datum box_width(PG_FUNCTION_ARGS);
296 extern Datum box_height(PG_FUNCTION_ARGS);
297 extern Datum box_distance(PG_FUNCTION_ARGS);
298 extern Datum box_center(PG_FUNCTION_ARGS);
299 extern Datum box_intersect(PG_FUNCTION_ARGS);
300 extern Datum box_diagonal(PG_FUNCTION_ARGS);
301 extern Datum points_box(PG_FUNCTION_ARGS);
302 extern Datum box_add(PG_FUNCTION_ARGS);
303 extern Datum box_sub(PG_FUNCTION_ARGS);
304 extern Datum box_mul(PG_FUNCTION_ARGS);
305 extern Datum box_div(PG_FUNCTION_ARGS);
307 /* public path routines */
308 extern Datum path_in(PG_FUNCTION_ARGS);
309 extern Datum path_out(PG_FUNCTION_ARGS);
310 extern Datum path_recv(PG_FUNCTION_ARGS);
311 extern Datum path_send(PG_FUNCTION_ARGS);
312 extern Datum path_n_lt(PG_FUNCTION_ARGS);
313 extern Datum path_n_gt(PG_FUNCTION_ARGS);
314 extern Datum path_n_eq(PG_FUNCTION_ARGS);
315 extern Datum path_n_le(PG_FUNCTION_ARGS);
316 extern Datum path_n_ge(PG_FUNCTION_ARGS);
317 extern Datum path_inter(PG_FUNCTION_ARGS);
318 extern Datum path_distance(PG_FUNCTION_ARGS);
319 extern Datum path_length(PG_FUNCTION_ARGS);
321 extern Datum path_isclosed(PG_FUNCTION_ARGS);
322 extern Datum path_isopen(PG_FUNCTION_ARGS);
323 extern Datum path_npoints(PG_FUNCTION_ARGS);
325 extern Datum path_close(PG_FUNCTION_ARGS);
326 extern Datum path_open(PG_FUNCTION_ARGS);
327 extern Datum path_add(PG_FUNCTION_ARGS);
328 extern Datum path_add_pt(PG_FUNCTION_ARGS);
329 extern Datum path_sub_pt(PG_FUNCTION_ARGS);
330 extern Datum path_mul_pt(PG_FUNCTION_ARGS);
331 extern Datum path_div_pt(PG_FUNCTION_ARGS);
333 extern Datum path_center(PG_FUNCTION_ARGS);
334 extern Datum path_poly(PG_FUNCTION_ARGS);
336 /* public polygon routines */
337 extern Datum poly_in(PG_FUNCTION_ARGS);
338 extern Datum poly_out(PG_FUNCTION_ARGS);
339 extern Datum poly_recv(PG_FUNCTION_ARGS);
340 extern Datum poly_send(PG_FUNCTION_ARGS);
341 extern Datum poly_left(PG_FUNCTION_ARGS);
342 extern Datum poly_overleft(PG_FUNCTION_ARGS);
343 extern Datum poly_right(PG_FUNCTION_ARGS);
344 extern Datum poly_overright(PG_FUNCTION_ARGS);
345 extern Datum poly_same(PG_FUNCTION_ARGS);
346 extern Datum poly_overlap(PG_FUNCTION_ARGS);
347 extern Datum poly_contain(PG_FUNCTION_ARGS);
348 extern Datum poly_contained(PG_FUNCTION_ARGS);
349 extern Datum poly_contain_pt(PG_FUNCTION_ARGS);
350 extern Datum pt_contained_poly(PG_FUNCTION_ARGS);
351 extern Datum poly_distance(PG_FUNCTION_ARGS);
352 extern Datum poly_npoints(PG_FUNCTION_ARGS);
353 extern Datum poly_center(PG_FUNCTION_ARGS);
354 extern Datum poly_box(PG_FUNCTION_ARGS);
355 extern Datum poly_path(PG_FUNCTION_ARGS);
356 extern Datum box_poly(PG_FUNCTION_ARGS);
358 /* public circle routines */
359 extern Datum circle_in(PG_FUNCTION_ARGS);
360 extern Datum circle_out(PG_FUNCTION_ARGS);
361 extern Datum circle_recv(PG_FUNCTION_ARGS);
362 extern Datum circle_send(PG_FUNCTION_ARGS);
363 extern Datum circle_same(PG_FUNCTION_ARGS);
364 extern Datum circle_overlap(PG_FUNCTION_ARGS);
365 extern Datum circle_overleft(PG_FUNCTION_ARGS);
366 extern Datum circle_left(PG_FUNCTION_ARGS);
367 extern Datum circle_right(PG_FUNCTION_ARGS);
368 extern Datum circle_overright(PG_FUNCTION_ARGS);
369 extern Datum circle_contained(PG_FUNCTION_ARGS);
370 extern Datum circle_contain(PG_FUNCTION_ARGS);
371 extern Datum circle_below(PG_FUNCTION_ARGS);
372 extern Datum circle_above(PG_FUNCTION_ARGS);
373 extern Datum circle_eq(PG_FUNCTION_ARGS);
374 extern Datum circle_ne(PG_FUNCTION_ARGS);
375 extern Datum circle_lt(PG_FUNCTION_ARGS);
376 extern Datum circle_gt(PG_FUNCTION_ARGS);
377 extern Datum circle_le(PG_FUNCTION_ARGS);
378 extern Datum circle_ge(PG_FUNCTION_ARGS);
379 extern Datum circle_contain_pt(PG_FUNCTION_ARGS);
380 extern Datum pt_contained_circle(PG_FUNCTION_ARGS);
381 extern Datum circle_add_pt(PG_FUNCTION_ARGS);
382 extern Datum circle_sub_pt(PG_FUNCTION_ARGS);
383 extern Datum circle_mul_pt(PG_FUNCTION_ARGS);
384 extern Datum circle_div_pt(PG_FUNCTION_ARGS);
385 extern Datum circle_diameter(PG_FUNCTION_ARGS);
386 extern Datum circle_radius(PG_FUNCTION_ARGS);
387 extern Datum circle_distance(PG_FUNCTION_ARGS);
388 extern Datum dist_pc(PG_FUNCTION_ARGS);
389 extern Datum dist_cpoly(PG_FUNCTION_ARGS);
390 extern Datum circle_center(PG_FUNCTION_ARGS);
391 extern Datum cr_circle(PG_FUNCTION_ARGS);
392 extern Datum box_circle(PG_FUNCTION_ARGS);
393 extern Datum circle_box(PG_FUNCTION_ARGS);
394 extern Datum poly_circle(PG_FUNCTION_ARGS);
395 extern Datum circle_poly(PG_FUNCTION_ARGS);
396 extern Datum circle_area(PG_FUNCTION_ARGS);
398 /* support routines for the rtree access method (rtproc.c) */
399 extern Datum rt_box_union(PG_FUNCTION_ARGS);
400 extern Datum rt_box_inter(PG_FUNCTION_ARGS);
401 extern Datum rt_box_size(PG_FUNCTION_ARGS);
402 extern Datum rt_bigbox_size(PG_FUNCTION_ARGS);
403 extern Datum rt_poly_size(PG_FUNCTION_ARGS);
404 extern Datum rt_poly_union(PG_FUNCTION_ARGS);
405 extern Datum rt_poly_inter(PG_FUNCTION_ARGS);
408 extern Datum areasel(PG_FUNCTION_ARGS);
409 extern Datum areajoinsel(PG_FUNCTION_ARGS);
410 extern Datum positionsel(PG_FUNCTION_ARGS);
411 extern Datum positionjoinsel(PG_FUNCTION_ARGS);
412 extern Datum contsel(PG_FUNCTION_ARGS);
413 extern Datum contjoinsel(PG_FUNCTION_ARGS);
415 #endif /* GEO_DECLS_H */