PostgreSQL Source Code git master
geo_decls.h
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * geo_decls.h - Declarations for various 2D constructs.
4 *
5 *
6 * Portions Copyright (c) 1996-2025, 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 * XXX These routines were not written by a numerical analyst.
12 *
13 * XXX I have made some attempt to flesh out the operators
14 * and data types. There are still some more to do. - tgl 97/04/19
15 *
16 *-------------------------------------------------------------------------
17 */
18#ifndef GEO_DECLS_H
19#define GEO_DECLS_H
20
21#include <math.h>
22
23#include "fmgr.h"
24
25/*--------------------------------------------------------------------
26 * Useful floating point utilities and constants.
27 *--------------------------------------------------------------------
28 *
29 * "Fuzzy" floating-point comparisons: values within EPSILON of each other
30 * are considered equal. Beware of normal reasoning about the behavior of
31 * these comparisons, since for example FPeq does not behave transitively.
32 *
33 * Note that these functions are not NaN-aware and will give FALSE for
34 * any case involving NaN inputs.
35 *
36 * Also note that these will give sane answers for infinite inputs,
37 * where it's important to avoid computing Inf minus Inf; we do so
38 * by eliminating equality cases before subtracting.
39 */
40
41#define EPSILON 1.0E-06
42
43#ifdef EPSILON
44#define FPzero(A) (fabs(A) <= EPSILON)
45
46static inline bool
47FPeq(double A, double B)
48{
49 return A == B || fabs(A - B) <= EPSILON;
50}
51
52static inline bool
53FPne(double A, double B)
54{
55 return A != B && fabs(A - B) > EPSILON;
56}
57
58static inline bool
59FPlt(double A, double B)
60{
61 return A + EPSILON < B;
62}
63
64static inline bool
65FPle(double A, double B)
66{
67 return A <= B + EPSILON;
68}
69
70static inline bool
71FPgt(double A, double B)
72{
73 return A > B + EPSILON;
74}
75
76static inline bool
77FPge(double A, double B)
78{
79 return A + EPSILON >= B;
80}
81#else
82#define FPzero(A) ((A) == 0)
83#define FPeq(A,B) ((A) == (B))
84#define FPne(A,B) ((A) != (B))
85#define FPlt(A,B) ((A) < (B))
86#define FPle(A,B) ((A) <= (B))
87#define FPgt(A,B) ((A) > (B))
88#define FPge(A,B) ((A) >= (B))
89#endif
90
91
92/*---------------------------------------------------------------------
93 * Point - (x,y)
94 *-------------------------------------------------------------------*/
95typedef struct
96{
99} Point;
100
101
102/*---------------------------------------------------------------------
103 * LSEG - A straight line, specified by endpoints.
104 *-------------------------------------------------------------------*/
105typedef struct
106{
107 Point p[2];
108} LSEG;
109
110
111/*---------------------------------------------------------------------
112 * PATH - Specified by vertex points.
113 *-------------------------------------------------------------------*/
114typedef struct
115{
116 int32 vl_len_; /* varlena header (do not touch directly!) */
118 int32 closed; /* is this a closed polygon? */
119 int32 dummy; /* padding to make it double align */
121} PATH;
122
123
124/*---------------------------------------------------------------------
125 * LINE - Specified by its general equation (Ax+By+C=0).
126 *-------------------------------------------------------------------*/
127typedef struct
128{
132} LINE;
133
134
135/*---------------------------------------------------------------------
136 * BOX - Specified by two corner points, which are
137 * sorted to save calculation time later.
138 *-------------------------------------------------------------------*/
139typedef struct
140{
142 low; /* corner POINTs */
143} BOX;
144
145/*---------------------------------------------------------------------
146 * POLYGON - Specified by an array of doubles defining the points,
147 * keeping the number of points and the bounding box for
148 * speed purposes.
149 *-------------------------------------------------------------------*/
150typedef struct
151{
152 int32 vl_len_; /* varlena header (do not touch directly!) */
156} POLYGON;
157
158/*---------------------------------------------------------------------
159 * CIRCLE - Specified by a center point and radius.
160 *-------------------------------------------------------------------*/
161typedef struct
162{
165} CIRCLE;
166
167/*
168 * fmgr interface functions
169 *
170 * Path and Polygon are toastable varlena types, the others are just
171 * fixed-size pass-by-reference types.
172 */
173
174static inline Point *
176{
177 return (Point *) DatumGetPointer(X);
178}
179static inline Datum
181{
182 return PointerGetDatum(X);
183}
184#define PG_GETARG_POINT_P(n) DatumGetPointP(PG_GETARG_DATUM(n))
185#define PG_RETURN_POINT_P(x) return PointPGetDatum(x)
186
187static inline LSEG *
189{
190 return (LSEG *) DatumGetPointer(X);
191}
192static inline Datum
194{
195 return PointerGetDatum(X);
196}
197#define PG_GETARG_LSEG_P(n) DatumGetLsegP(PG_GETARG_DATUM(n))
198#define PG_RETURN_LSEG_P(x) return LsegPGetDatum(x)
199
200static inline PATH *
202{
203 return (PATH *) PG_DETOAST_DATUM(X);
204}
205static inline PATH *
207{
208 return (PATH *) PG_DETOAST_DATUM_COPY(X);
209}
210static inline Datum
212{
213 return PointerGetDatum(X);
214}
215#define PG_GETARG_PATH_P(n) DatumGetPathP(PG_GETARG_DATUM(n))
216#define PG_GETARG_PATH_P_COPY(n) DatumGetPathPCopy(PG_GETARG_DATUM(n))
217#define PG_RETURN_PATH_P(x) return PathPGetDatum(x)
218
219static inline LINE *
221{
222 return (LINE *) DatumGetPointer(X);
223}
224static inline Datum
226{
227 return PointerGetDatum(X);
228}
229#define PG_GETARG_LINE_P(n) DatumGetLineP(PG_GETARG_DATUM(n))
230#define PG_RETURN_LINE_P(x) return LinePGetDatum(x)
231
232static inline BOX *
234{
235 return (BOX *) DatumGetPointer(X);
236}
237static inline Datum
239{
240 return PointerGetDatum(X);
241}
242#define PG_GETARG_BOX_P(n) DatumGetBoxP(PG_GETARG_DATUM(n))
243#define PG_RETURN_BOX_P(x) return BoxPGetDatum(x)
244
245static inline POLYGON *
247{
248 return (POLYGON *) PG_DETOAST_DATUM(X);
249}
250static inline POLYGON *
252{
253 return (POLYGON *) PG_DETOAST_DATUM_COPY(X);
254}
255static inline Datum
257{
258 return PointerGetDatum(X);
259}
260#define PG_GETARG_POLYGON_P(n) DatumGetPolygonP(PG_GETARG_DATUM(n))
261#define PG_GETARG_POLYGON_P_COPY(n) DatumGetPolygonPCopy(PG_GETARG_DATUM(n))
262#define PG_RETURN_POLYGON_P(x) return PolygonPGetDatum(x)
263
264static inline CIRCLE *
266{
267 return (CIRCLE *) DatumGetPointer(X);
268}
269static inline Datum
271{
272 return PointerGetDatum(X);
273}
274#define PG_GETARG_CIRCLE_P(n) DatumGetCircleP(PG_GETARG_DATUM(n))
275#define PG_RETURN_CIRCLE_P(x) return CirclePGetDatum(x)
276
277#endif /* GEO_DECLS_H */
double float8
Definition: c.h:640
#define FLEXIBLE_ARRAY_MEMBER
Definition: c.h:475
int32_t int32
Definition: c.h:539
#define PG_DETOAST_DATUM_COPY(datum)
Definition: fmgr.h:242
#define PG_DETOAST_DATUM(datum)
Definition: fmgr.h:240
#define EPSILON
Definition: geo_decls.h:41
static Datum PathPGetDatum(const PATH *X)
Definition: geo_decls.h:211
static bool FPlt(double A, double B)
Definition: geo_decls.h:59
static Datum LsegPGetDatum(const LSEG *X)
Definition: geo_decls.h:193
static bool FPge(double A, double B)
Definition: geo_decls.h:77
static Datum PolygonPGetDatum(const POLYGON *X)
Definition: geo_decls.h:256
static PATH * DatumGetPathPCopy(Datum X)
Definition: geo_decls.h:206
static POLYGON * DatumGetPolygonPCopy(Datum X)
Definition: geo_decls.h:251
static LINE * DatumGetLineP(Datum X)
Definition: geo_decls.h:220
static Point * DatumGetPointP(Datum X)
Definition: geo_decls.h:175
static Datum LinePGetDatum(const LINE *X)
Definition: geo_decls.h:225
static PATH * DatumGetPathP(Datum X)
Definition: geo_decls.h:201
static Datum PointPGetDatum(const Point *X)
Definition: geo_decls.h:180
static POLYGON * DatumGetPolygonP(Datum X)
Definition: geo_decls.h:246
static bool FPne(double A, double B)
Definition: geo_decls.h:53
static BOX * DatumGetBoxP(Datum X)
Definition: geo_decls.h:233
static CIRCLE * DatumGetCircleP(Datum X)
Definition: geo_decls.h:265
static Datum CirclePGetDatum(const CIRCLE *X)
Definition: geo_decls.h:270
static LSEG * DatumGetLsegP(Datum X)
Definition: geo_decls.h:188
static bool FPgt(double A, double B)
Definition: geo_decls.h:71
static bool FPle(double A, double B)
Definition: geo_decls.h:65
static Datum BoxPGetDatum(const BOX *X)
Definition: geo_decls.h:238
static bool FPeq(double A, double B)
Definition: geo_decls.h:47
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:332
uint64_t Datum
Definition: postgres.h:70
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:322
Definition: geo_decls.h:140
Point low
Definition: geo_decls.h:142
Point high
Definition: geo_decls.h:141
float8 radius
Definition: geo_decls.h:164
Point center
Definition: geo_decls.h:163
float8 A
Definition: geo_decls.h:129
float8 B
Definition: geo_decls.h:130
float8 C
Definition: geo_decls.h:131
int32 vl_len_
Definition: geo_decls.h:116
int32 npts
Definition: geo_decls.h:117
int32 closed
Definition: geo_decls.h:118
int32 dummy
Definition: geo_decls.h:119
int32 vl_len_
Definition: geo_decls.h:152
int32 npts
Definition: geo_decls.h:153
BOX boundbox
Definition: geo_decls.h:154
float8 y
Definition: geo_decls.h:98
float8 x
Definition: geo_decls.h:97