1 /*------------------------------------------------------------------------- 2 * 3 * geo_decls.h - Declarations for various 2D constructs. 4 * 5 * 6 * Portions Copyright (c) 1996-2017, 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.c 179 */ 180 181 /* private point routines */ 182 extern double point_dt(Point *pt1, Point *pt2); 183 extern double point_sl(Point *pt1, Point *pt2); 184 extern double pg_hypot(double x, double y); 185 186 #endif /* GEO_DECLS_H */ 187