2
0

geo_decls.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*-------------------------------------------------------------------------
  2. *
  3. * geo_decls.h - Declarations for various 2D constructs.
  4. *
  5. *
  6. * Portions Copyright (c) 1996-2022, 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. #include <math.h>
  21. #include "fmgr.h"
  22. /*--------------------------------------------------------------------
  23. * Useful floating point utilities and constants.
  24. *--------------------------------------------------------------------
  25. *
  26. * "Fuzzy" floating-point comparisons: values within EPSILON of each other
  27. * are considered equal. Beware of normal reasoning about the behavior of
  28. * these comparisons, since for example FPeq does not behave transitively.
  29. *
  30. * Note that these functions are not NaN-aware and will give FALSE for
  31. * any case involving NaN inputs.
  32. *
  33. * Also note that these will give sane answers for infinite inputs,
  34. * where it's important to avoid computing Inf minus Inf; we do so
  35. * by eliminating equality cases before subtracting.
  36. */
  37. #define EPSILON 1.0E-06
  38. #ifdef EPSILON
  39. #define FPzero(A) (fabs(A) <= EPSILON)
  40. static inline bool
  41. FPeq(double A, double B)
  42. {
  43. return A == B || fabs(A - B) <= EPSILON;
  44. }
  45. static inline bool
  46. FPne(double A, double B)
  47. {
  48. return A != B && fabs(A - B) > EPSILON;
  49. }
  50. static inline bool
  51. FPlt(double A, double B)
  52. {
  53. return A + EPSILON < B;
  54. }
  55. static inline bool
  56. FPle(double A, double B)
  57. {
  58. return A <= B + EPSILON;
  59. }
  60. static inline bool
  61. FPgt(double A, double B)
  62. {
  63. return A > B + EPSILON;
  64. }
  65. static inline bool
  66. FPge(double A, double B)
  67. {
  68. return A + EPSILON >= B;
  69. }
  70. #else
  71. #define FPzero(A) ((A) == 0)
  72. #define FPeq(A,B) ((A) == (B))
  73. #define FPne(A,B) ((A) != (B))
  74. #define FPlt(A,B) ((A) < (B))
  75. #define FPle(A,B) ((A) <= (B))
  76. #define FPgt(A,B) ((A) > (B))
  77. #define FPge(A,B) ((A) >= (B))
  78. #endif
  79. #define HYPOT(A, B) pg_hypot(A, B)
  80. /*---------------------------------------------------------------------
  81. * Point - (x,y)
  82. *-------------------------------------------------------------------*/
  83. typedef struct
  84. {
  85. float8 x,
  86. y;
  87. } Point;
  88. /*---------------------------------------------------------------------
  89. * LSEG - A straight line, specified by endpoints.
  90. *-------------------------------------------------------------------*/
  91. typedef struct
  92. {
  93. Point p[2];
  94. } LSEG;
  95. /*---------------------------------------------------------------------
  96. * PATH - Specified by vertex points.
  97. *-------------------------------------------------------------------*/
  98. typedef struct
  99. {
  100. int32 vl_len_; /* varlena header (do not touch directly!) */
  101. int32 npts;
  102. int32 closed; /* is this a closed polygon? */
  103. int32 dummy; /* padding to make it double align */
  104. Point p[FLEXIBLE_ARRAY_MEMBER];
  105. } PATH;
  106. /*---------------------------------------------------------------------
  107. * LINE - Specified by its general equation (Ax+By+C=0).
  108. *-------------------------------------------------------------------*/
  109. typedef struct
  110. {
  111. float8 A,
  112. B,
  113. C;
  114. } LINE;
  115. /*---------------------------------------------------------------------
  116. * BOX - Specified by two corner points, which are
  117. * sorted to save calculation time later.
  118. *-------------------------------------------------------------------*/
  119. typedef struct
  120. {
  121. Point high,
  122. low; /* corner POINTs */
  123. } BOX;
  124. /*---------------------------------------------------------------------
  125. * POLYGON - Specified by an array of doubles defining the points,
  126. * keeping the number of points and the bounding box for
  127. * speed purposes.
  128. *-------------------------------------------------------------------*/
  129. typedef struct
  130. {
  131. int32 vl_len_; /* varlena header (do not touch directly!) */
  132. int32 npts;
  133. BOX boundbox;
  134. Point p[FLEXIBLE_ARRAY_MEMBER];
  135. } POLYGON;
  136. /*---------------------------------------------------------------------
  137. * CIRCLE - Specified by a center point and radius.
  138. *-------------------------------------------------------------------*/
  139. typedef struct
  140. {
  141. Point center;
  142. float8 radius;
  143. } CIRCLE;
  144. /*
  145. * fmgr interface macros
  146. *
  147. * Path and Polygon are toastable varlena types, the others are just
  148. * fixed-size pass-by-reference types.
  149. */
  150. #define DatumGetPointP(X) ((Point *) DatumGetPointer(X))
  151. #define PointPGetDatum(X) PointerGetDatum(X)
  152. #define PG_GETARG_POINT_P(n) DatumGetPointP(PG_GETARG_DATUM(n))
  153. #define PG_RETURN_POINT_P(x) return PointPGetDatum(x)
  154. #define DatumGetLsegP(X) ((LSEG *) DatumGetPointer(X))
  155. #define LsegPGetDatum(X) PointerGetDatum(X)
  156. #define PG_GETARG_LSEG_P(n) DatumGetLsegP(PG_GETARG_DATUM(n))
  157. #define PG_RETURN_LSEG_P(x) return LsegPGetDatum(x)
  158. #define DatumGetPathP(X) ((PATH *) PG_DETOAST_DATUM(X))
  159. #define DatumGetPathPCopy(X) ((PATH *) PG_DETOAST_DATUM_COPY(X))
  160. #define PathPGetDatum(X) PointerGetDatum(X)
  161. #define PG_GETARG_PATH_P(n) DatumGetPathP(PG_GETARG_DATUM(n))
  162. #define PG_GETARG_PATH_P_COPY(n) DatumGetPathPCopy(PG_GETARG_DATUM(n))
  163. #define PG_RETURN_PATH_P(x) return PathPGetDatum(x)
  164. #define DatumGetLineP(X) ((LINE *) DatumGetPointer(X))
  165. #define LinePGetDatum(X) PointerGetDatum(X)
  166. #define PG_GETARG_LINE_P(n) DatumGetLineP(PG_GETARG_DATUM(n))
  167. #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)
  172. #define DatumGetPolygonP(X) ((POLYGON *) PG_DETOAST_DATUM(X))
  173. #define DatumGetPolygonPCopy(X) ((POLYGON *) PG_DETOAST_DATUM_COPY(X))
  174. #define PolygonPGetDatum(X) PointerGetDatum(X)
  175. #define PG_GETARG_POLYGON_P(n) DatumGetPolygonP(PG_GETARG_DATUM(n))
  176. #define PG_GETARG_POLYGON_P_COPY(n) DatumGetPolygonPCopy(PG_GETARG_DATUM(n))
  177. #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)
  182. /*
  183. * in geo_ops.c
  184. */
  185. extern float8 pg_hypot(float8 x, float8 y);
  186. #endif /* GEO_DECLS_H */