LCOV - code coverage report
Current view: top level - src/backend/utils/adt - geo_ops.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 92.5 % 2540 2350
Test Date: 2026-01-26 10:56:24 Functions: 95.1 % 266 253
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 82.9 % 956 793

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * geo_ops.c
       4                 :             :  *        2D geometric operations
       5                 :             :  *
       6                 :             :  * This module implements the geometric functions and operators.  The
       7                 :             :  * geometric types are (from simple to more complicated):
       8                 :             :  *
       9                 :             :  * - point
      10                 :             :  * - line
      11                 :             :  * - line segment
      12                 :             :  * - box
      13                 :             :  * - circle
      14                 :             :  * - polygon
      15                 :             :  *
      16                 :             :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
      17                 :             :  * Portions Copyright (c) 1994, Regents of the University of California
      18                 :             :  *
      19                 :             :  *
      20                 :             :  * IDENTIFICATION
      21                 :             :  *        src/backend/utils/adt/geo_ops.c
      22                 :             :  *
      23                 :             :  *-------------------------------------------------------------------------
      24                 :             :  */
      25                 :             : #include "postgres.h"
      26                 :             : 
      27                 :             : #include <math.h>
      28                 :             : #include <limits.h>
      29                 :             : #include <float.h>
      30                 :             : #include <ctype.h>
      31                 :             : 
      32                 :             : #include "libpq/pqformat.h"
      33                 :             : #include "miscadmin.h"
      34                 :             : #include "nodes/miscnodes.h"
      35                 :             : #include "utils/float.h"
      36                 :             : #include "utils/fmgrprotos.h"
      37                 :             : #include "utils/geo_decls.h"
      38                 :             : #include "varatt.h"
      39                 :             : 
      40                 :             : /*
      41                 :             :  * * Type constructors have this form:
      42                 :             :  *   void type_construct(Type *result, ...);
      43                 :             :  *
      44                 :             :  * * Operators commonly have signatures such as
      45                 :             :  *   void type1_operator_type2(Type *result, Type1 *obj1, Type2 *obj2);
      46                 :             :  *
      47                 :             :  * Common operators are:
      48                 :             :  * * Intersection point:
      49                 :             :  *   bool type1_interpt_type2(Point *result, Type1 *obj1, Type2 *obj2);
      50                 :             :  *              Return whether the two objects intersect. If *result is not NULL,
      51                 :             :  *              it is set to the intersection point.
      52                 :             :  *
      53                 :             :  * * Containment:
      54                 :             :  *   bool type1_contain_type2(Type1 *obj1, Type2 *obj2);
      55                 :             :  *              Return whether obj1 contains obj2.
      56                 :             :  *   bool type1_contain_type2(Type1 *contains_obj, Type1 *contained_obj);
      57                 :             :  *              Return whether obj1 contains obj2 (used when types are the same)
      58                 :             :  *
      59                 :             :  * * Distance of closest point in or on obj1 to obj2:
      60                 :             :  *   float8 type1_closept_type2(Point *result, Type1 *obj1, Type2 *obj2);
      61                 :             :  *              Returns the shortest distance between two objects.  If *result is not
      62                 :             :  *              NULL, it is set to the closest point in or on obj1 to obj2.
      63                 :             :  *
      64                 :             :  * These functions may be used to implement multiple SQL-level operators.  For
      65                 :             :  * example, determining whether two lines are parallel is done by checking
      66                 :             :  * whether they don't intersect.
      67                 :             :  */
      68                 :             : 
      69                 :             : /*
      70                 :             :  * Internal routines
      71                 :             :  */
      72                 :             : 
      73                 :             : enum path_delim
      74                 :             : {
      75                 :             :         PATH_NONE, PATH_OPEN, PATH_CLOSED
      76                 :             : };
      77                 :             : 
      78                 :             : /* Routines for points */
      79                 :             : static inline void point_construct(Point *result, float8 x, float8 y);
      80                 :             : static inline void point_add_point(Point *result, Point *pt1, Point *pt2);
      81                 :             : static inline void point_sub_point(Point *result, Point *pt1, Point *pt2);
      82                 :             : static inline void point_mul_point(Point *result, Point *pt1, Point *pt2);
      83                 :             : static inline void point_div_point(Point *result, Point *pt1, Point *pt2);
      84                 :             : static inline bool point_eq_point(Point *pt1, Point *pt2);
      85                 :             : static inline float8 point_dt(Point *pt1, Point *pt2);
      86                 :             : static inline float8 point_sl(Point *pt1, Point *pt2);
      87                 :             : static int      point_inside(Point *p, int npts, Point *plist);
      88                 :             : 
      89                 :             : /* Routines for lines */
      90                 :             : static inline void line_construct(LINE *result, Point *pt, float8 m);
      91                 :             : static inline float8 line_sl(LINE *line);
      92                 :             : static inline float8 line_invsl(LINE *line);
      93                 :             : static bool line_interpt_line(Point *result, LINE *l1, LINE *l2);
      94                 :             : static bool line_contain_point(LINE *line, Point *point);
      95                 :             : static float8 line_closept_point(Point *result, LINE *line, Point *point);
      96                 :             : 
      97                 :             : /* Routines for line segments */
      98                 :             : static inline void statlseg_construct(LSEG *lseg, Point *pt1, Point *pt2);
      99                 :             : static inline float8 lseg_sl(LSEG *lseg);
     100                 :             : static inline float8 lseg_invsl(LSEG *lseg);
     101                 :             : static bool lseg_interpt_line(Point *result, LSEG *lseg, LINE *line);
     102                 :             : static bool lseg_interpt_lseg(Point *result, LSEG *l1, LSEG *l2);
     103                 :             : static int      lseg_crossing(float8 x, float8 y, float8 prev_x, float8 prev_y);
     104                 :             : static bool lseg_contain_point(LSEG *lseg, Point *pt);
     105                 :             : static float8 lseg_closept_point(Point *result, LSEG *lseg, Point *pt);
     106                 :             : static float8 lseg_closept_line(Point *result, LSEG *lseg, LINE *line);
     107                 :             : static float8 lseg_closept_lseg(Point *result, LSEG *on_lseg, LSEG *to_lseg);
     108                 :             : 
     109                 :             : /* Routines for boxes */
     110                 :             : static inline void box_construct(BOX *result, Point *pt1, Point *pt2);
     111                 :             : static void box_cn(Point *center, BOX *box);
     112                 :             : static bool box_ov(BOX *box1, BOX *box2);
     113                 :             : static float8 box_ar(BOX *box);
     114                 :             : static float8 box_ht(BOX *box);
     115                 :             : static float8 box_wd(BOX *box);
     116                 :             : static bool box_contain_point(BOX *box, Point *point);
     117                 :             : static bool box_contain_box(BOX *contains_box, BOX *contained_box);
     118                 :             : static bool box_contain_lseg(BOX *box, LSEG *lseg);
     119                 :             : static bool box_interpt_lseg(Point *result, BOX *box, LSEG *lseg);
     120                 :             : static float8 box_closept_point(Point *result, BOX *box, Point *pt);
     121                 :             : static float8 box_closept_lseg(Point *result, BOX *box, LSEG *lseg);
     122                 :             : 
     123                 :             : /* Routines for circles */
     124                 :             : static float8 circle_ar(CIRCLE *circle);
     125                 :             : 
     126                 :             : /* Routines for polygons */
     127                 :             : static void make_bound_box(POLYGON *poly);
     128                 :             : static void poly_to_circle(CIRCLE *result, POLYGON *poly);
     129                 :             : static bool lseg_inside_poly(Point *a, Point *b, POLYGON *poly, int start);
     130                 :             : static bool poly_contain_poly(POLYGON *contains_poly, POLYGON *contained_poly);
     131                 :             : static bool plist_same(int npts, Point *p1, Point *p2);
     132                 :             : static float8 dist_ppoly_internal(Point *pt, POLYGON *poly);
     133                 :             : 
     134                 :             : /* Routines for encoding and decoding */
     135                 :             : static bool single_decode(char *num, float8 *x, char **endptr_p,
     136                 :             :                                                   const char *type_name, const char *orig_string,
     137                 :             :                                                   Node *escontext);
     138                 :             : static void single_encode(float8 x, StringInfo str);
     139                 :             : static bool pair_decode(char *str, float8 *x, float8 *y, char **endptr_p,
     140                 :             :                                                 const char *type_name, const char *orig_string,
     141                 :             :                                                 Node *escontext);
     142                 :             : static void pair_encode(float8 x, float8 y, StringInfo str);
     143                 :             : static int      pair_count(char *s, char delim);
     144                 :             : static bool path_decode(char *str, bool opentype, int npts, Point *p,
     145                 :             :                                                 bool *isopen, char **endptr_p,
     146                 :             :                                                 const char *type_name, const char *orig_string,
     147                 :             :                                                 Node *escontext);
     148                 :             : static char *path_encode(enum path_delim path_delim, int npts, Point *pt);
     149                 :             : 
     150                 :             : 
     151                 :             : /*
     152                 :             :  * Delimiters for input and output strings.
     153                 :             :  * LDELIM, RDELIM, and DELIM are left, right, and separator delimiters, respectively.
     154                 :             :  * LDELIM_EP, RDELIM_EP are left and right delimiters for paths with endpoints.
     155                 :             :  */
     156                 :             : 
     157                 :             : #define LDELIM                  '('
     158                 :             : #define RDELIM                  ')'
     159                 :             : #define DELIM                   ','
     160                 :             : #define LDELIM_EP               '['
     161                 :             : #define RDELIM_EP               ']'
     162                 :             : #define LDELIM_C                '<'
     163                 :             : #define RDELIM_C                '>'
     164                 :             : #define LDELIM_L                '{'
     165                 :             : #define RDELIM_L                '}'
     166                 :             : 
     167                 :             : 
     168                 :             : /*
     169                 :             :  * Geometric data types are composed of points.
     170                 :             :  * This code tries to support a common format throughout the data types,
     171                 :             :  *      to allow for more predictable usage and data type conversion.
     172                 :             :  * The fundamental unit is the point. Other units are line segments,
     173                 :             :  *      open paths, boxes, closed paths, and polygons (which should be considered
     174                 :             :  *      non-intersecting closed paths).
     175                 :             :  *
     176                 :             :  * Data representation is as follows:
     177                 :             :  *      point:                          (x,y)
     178                 :             :  *      line segment:           [(x1,y1),(x2,y2)]
     179                 :             :  *      box:                            (x1,y1),(x2,y2)
     180                 :             :  *      open path:                      [(x1,y1),...,(xn,yn)]
     181                 :             :  *      closed path:            ((x1,y1),...,(xn,yn))
     182                 :             :  *      polygon:                        ((x1,y1),...,(xn,yn))
     183                 :             :  *
     184                 :             :  * For boxes, the points are opposite corners with the first point at the top right.
     185                 :             :  * For closed paths and polygons, the points should be reordered to allow
     186                 :             :  *      fast and correct equality comparisons.
     187                 :             :  *
     188                 :             :  * XXX perhaps points in complex shapes should be reordered internally
     189                 :             :  *      to allow faster internal operations, but should keep track of input order
     190                 :             :  *      and restore that order for text output - tgl 97/01/16
     191                 :             :  */
     192                 :             : 
     193                 :             : static bool
     194                 :       37394 : single_decode(char *num, float8 *x, char **endptr_p,
     195                 :             :                           const char *type_name, const char *orig_string,
     196                 :             :                           Node *escontext)
     197                 :             : {
     198                 :       37394 :         *x = float8in_internal(num, endptr_p, type_name, orig_string, escontext);
     199   [ +  +  -  + ]:       37394 :         return (!SOFT_ERROR_OCCURRED(escontext));
     200                 :             : }                                                               /* single_decode() */
     201                 :             : 
     202                 :             : static void
     203                 :        1516 : single_encode(float8 x, StringInfo str)
     204                 :             : {
     205                 :        1516 :         char       *xstr = float8out_internal(x);
     206                 :             : 
     207                 :        1516 :         appendStringInfoString(str, xstr);
     208                 :        1516 :         pfree(xstr);
     209                 :        1516 : }                                                               /* single_encode() */
     210                 :             : 
     211                 :             : static bool
     212                 :       18635 : pair_decode(char *str, float8 *x, float8 *y, char **endptr_p,
     213                 :             :                         const char *type_name, const char *orig_string,
     214                 :             :                         Node *escontext)
     215                 :             : {
     216                 :       18635 :         bool            has_delim;
     217                 :             : 
     218         [ +  + ]:       18650 :         while (isspace((unsigned char) *str))
     219                 :          15 :                 str++;
     220         [ +  + ]:       18635 :         if ((has_delim = (*str == LDELIM)))
     221                 :       12161 :                 str++;
     222                 :             : 
     223         [ +  - ]:       18635 :         if (!single_decode(str, x, &str, type_name, orig_string, escontext))
     224                 :           0 :                 return false;
     225                 :             : 
     226         [ +  + ]:       18635 :         if (*str++ != DELIM)
     227                 :           9 :                 goto fail;
     228                 :             : 
     229         [ +  + ]:       18626 :         if (!single_decode(str, y, &str, type_name, orig_string, escontext))
     230                 :           9 :                 return false;
     231                 :             : 
     232         [ +  + ]:       18617 :         if (has_delim)
     233                 :             :         {
     234         [ +  + ]:       12148 :                 if (*str++ != RDELIM)
     235                 :           3 :                         goto fail;
     236         [ +  + ]:       12154 :                 while (isspace((unsigned char) *str))
     237                 :           9 :                         str++;
     238                 :       12145 :         }
     239                 :             : 
     240                 :             :         /* report stopping point if wanted, else complain if not end of string */
     241         [ +  + ]:       18614 :         if (endptr_p)
     242                 :       18344 :                 *endptr_p = str;
     243         [ +  + ]:         270 :         else if (*str != '\0')
     244                 :           1 :                 goto fail;
     245                 :       18613 :         return true;
     246                 :             : 
     247                 :             : fail:
     248         [ +  + ]:          13 :         ereturn(escontext, false,
     249                 :             :                         (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
     250                 :             :                          errmsg("invalid input syntax for type %s: \"%s\"",
     251                 :             :                                         type_name, orig_string)));
     252         [ -  + ]:       18635 : }
     253                 :             : 
     254                 :             : static void
     255                 :       16943 : pair_encode(float8 x, float8 y, StringInfo str)
     256                 :             : {
     257                 :       16943 :         char       *xstr = float8out_internal(x);
     258                 :       16943 :         char       *ystr = float8out_internal(y);
     259                 :             : 
     260                 :       16943 :         appendStringInfo(str, "%s,%s", xstr, ystr);
     261                 :       16943 :         pfree(xstr);
     262                 :       16943 :         pfree(ystr);
     263                 :       16943 : }
     264                 :             : 
     265                 :             : static bool
     266                 :        8535 : path_decode(char *str, bool opentype, int npts, Point *p,
     267                 :             :                         bool *isopen, char **endptr_p,
     268                 :             :                         const char *type_name, const char *orig_string,
     269                 :             :                         Node *escontext)
     270                 :             : {
     271                 :        8535 :         int                     depth = 0;
     272                 :        8535 :         char       *cp;
     273                 :        8535 :         int                     i;
     274                 :             : 
     275         [ +  + ]:        8536 :         while (isspace((unsigned char) *str))
     276                 :           1 :                 str++;
     277         [ +  + ]:        8535 :         if ((*isopen = (*str == LDELIM_EP)))
     278                 :             :         {
     279                 :             :                 /* no open delimiter allowed? */
     280         [ +  + ]:        5068 :                 if (!opentype)
     281                 :           1 :                         goto fail;
     282                 :        5067 :                 depth++;
     283                 :        5067 :                 str++;
     284                 :        5067 :         }
     285         [ +  + ]:        3467 :         else if (*str == LDELIM)
     286                 :             :         {
     287                 :        3448 :                 cp = (str + 1);
     288         [ +  + ]:        3449 :                 while (isspace((unsigned char) *cp))
     289                 :           1 :                         cp++;
     290         [ +  + ]:        3448 :                 if (*cp == LDELIM)
     291                 :             :                 {
     292                 :         202 :                         depth++;
     293                 :         202 :                         str = cp;
     294                 :         202 :                 }
     295         [ +  + ]:        3246 :                 else if (strrchr(str, LDELIM) == str)
     296                 :             :                 {
     297                 :        3176 :                         depth++;
     298                 :        3176 :                         str = cp;
     299                 :        3176 :                 }
     300                 :        3448 :         }
     301                 :             : 
     302         [ +  + ]:       26817 :         for (i = 0; i < npts; i++)
     303                 :             :         {
     304   [ +  +  +  + ]:       36590 :                 if (!pair_decode(str, &(p->x), &(p->y), &str, type_name, orig_string,
     305                 :       18295 :                                                  escontext))
     306                 :          12 :                         return false;
     307         [ +  + ]:       18283 :                 if (*str == DELIM)
     308                 :        9755 :                         str++;
     309                 :       18283 :                 p++;
     310                 :       18283 :         }
     311                 :             : 
     312         [ +  + ]:       16946 :         while (depth > 0)
     313                 :             :         {
     314   [ +  +  +  +  :        8431 :                 if (*str == RDELIM || (*str == RDELIM_EP && *isopen && depth == 1))
             +  +  +  - ]
     315                 :             :                 {
     316                 :        8424 :                         depth--;
     317                 :        8424 :                         str++;
     318         [ +  + ]:        8427 :                         while (isspace((unsigned char) *str))
     319                 :           3 :                                 str++;
     320                 :        8424 :                 }
     321                 :             :                 else
     322                 :           7 :                         goto fail;
     323                 :             :         }
     324                 :             : 
     325                 :             :         /* report stopping point if wanted, else complain if not end of string */
     326         [ +  + ]:        8515 :         if (endptr_p)
     327                 :        5145 :                 *endptr_p = str;
     328         [ +  + ]:        3370 :         else if (*str != '\0')
     329                 :           1 :                 goto fail;
     330                 :        8514 :         return true;
     331                 :             : 
     332                 :             : fail:
     333         [ -  + ]:           9 :         ereturn(escontext, false,
     334                 :             :                         (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
     335                 :             :                          errmsg("invalid input syntax for type %s: \"%s\"",
     336                 :             :                                         type_name, orig_string)));
     337         [ -  + ]:        8535 : }                                                               /* path_decode() */
     338                 :             : 
     339                 :             : static char *
     340                 :        8763 : path_encode(enum path_delim path_delim, int npts, Point *pt)
     341                 :             : {
     342                 :        8763 :         StringInfoData str;
     343                 :        8763 :         int                     i;
     344                 :             : 
     345                 :        8763 :         initStringInfo(&str);
     346                 :             : 
     347      [ +  +  + ]:        8763 :         switch (path_delim)
     348                 :             :         {
     349                 :             :                 case PATH_CLOSED:
     350                 :        1767 :                         appendStringInfoChar(&str, LDELIM);
     351                 :        1767 :                         break;
     352                 :             :                 case PATH_OPEN:
     353                 :        2207 :                         appendStringInfoChar(&str, LDELIM_EP);
     354                 :        2207 :                         break;
     355                 :             :                 case PATH_NONE:
     356                 :             :                         break;
     357                 :             :         }
     358                 :             : 
     359         [ +  + ]:       24190 :         for (i = 0; i < npts; i++)
     360                 :             :         {
     361         [ +  + ]:       15427 :                 if (i > 0)
     362                 :        6664 :                         appendStringInfoChar(&str, DELIM);
     363                 :       15427 :                 appendStringInfoChar(&str, LDELIM);
     364                 :       15427 :                 pair_encode(pt->x, pt->y, &str);
     365                 :       15427 :                 appendStringInfoChar(&str, RDELIM);
     366                 :       15427 :                 pt++;
     367                 :       15427 :         }
     368                 :             : 
     369      [ +  +  + ]:        8763 :         switch (path_delim)
     370                 :             :         {
     371                 :             :                 case PATH_CLOSED:
     372                 :        1767 :                         appendStringInfoChar(&str, RDELIM);
     373                 :        1767 :                         break;
     374                 :             :                 case PATH_OPEN:
     375                 :        2207 :                         appendStringInfoChar(&str, RDELIM_EP);
     376                 :        2207 :                         break;
     377                 :             :                 case PATH_NONE:
     378                 :             :                         break;
     379                 :             :         }
     380                 :             : 
     381                 :       17526 :         return str.data;
     382                 :        8763 : }                                                               /* path_encode() */
     383                 :             : 
     384                 :             : /*-------------------------------------------------------------
     385                 :             :  * pair_count - count the number of points
     386                 :             :  * allow the following notation:
     387                 :             :  * '((1,2),(3,4))'
     388                 :             :  * '(1,3,2,4)'
     389                 :             :  * require an odd number of delim characters in the string
     390                 :             :  *-------------------------------------------------------------*/
     391                 :             : static int
     392                 :        5218 : pair_count(char *s, char delim)
     393                 :             : {
     394                 :        5218 :         int                     ndelim = 0;
     395                 :             : 
     396         [ +  + ]:       23319 :         while ((s = strchr(s, delim)) != NULL)
     397                 :             :         {
     398                 :       18101 :                 ndelim++;
     399                 :       18101 :                 s++;
     400                 :             :         }
     401         [ +  + ]:        5218 :         return (ndelim % 2) ? ((ndelim + 1) / 2) : -1;
     402                 :        5218 : }
     403                 :             : 
     404                 :             : 
     405                 :             : /***********************************************************************
     406                 :             :  **
     407                 :             :  **             Routines for two-dimensional boxes.
     408                 :             :  **
     409                 :             :  ***********************************************************************/
     410                 :             : 
     411                 :             : /*----------------------------------------------------------
     412                 :             :  * Formatting and conversion routines.
     413                 :             :  *---------------------------------------------------------*/
     414                 :             : 
     415                 :             : /*              box_in  -               convert a string to internal form.
     416                 :             :  *
     417                 :             :  *              External format: (two corners of box)
     418                 :             :  *                              "(f8, f8), (f8, f8)"
     419                 :             :  *                              also supports the older style "(f8, f8, f8, f8)"
     420                 :             :  */
     421                 :             : Datum
     422                 :        3300 : box_in(PG_FUNCTION_ARGS)
     423                 :             : {
     424                 :        3300 :         char       *str = PG_GETARG_CSTRING(0);
     425                 :        3300 :         Node       *escontext = fcinfo->context;
     426                 :        3300 :         BOX                *box = palloc_object(BOX);
     427                 :        3300 :         bool            isopen;
     428                 :        3300 :         float8          x,
     429                 :             :                                 y;
     430                 :             : 
     431   [ +  +  +  + ]:        6600 :         if (!path_decode(str, false, 2, &(box->high), &isopen, NULL, "box", str,
     432                 :        3300 :                                          escontext))
     433                 :           4 :                 PG_RETURN_NULL();
     434                 :             : 
     435                 :             :         /* reorder corners if necessary... */
     436         [ +  + ]:        3296 :         if (float8_lt(box->high.x, box->low.x))
     437                 :             :         {
     438                 :         147 :                 x = box->high.x;
     439                 :         147 :                 box->high.x = box->low.x;
     440                 :         147 :                 box->low.x = x;
     441                 :         147 :         }
     442         [ +  + ]:        3296 :         if (float8_lt(box->high.y, box->low.y))
     443                 :             :         {
     444                 :         149 :                 y = box->high.y;
     445                 :         149 :                 box->high.y = box->low.y;
     446                 :         149 :                 box->low.y = y;
     447                 :         149 :         }
     448                 :             : 
     449                 :        3296 :         PG_RETURN_BOX_P(box);
     450                 :        3300 : }
     451                 :             : 
     452                 :             : /*              box_out -               convert a box to external form.
     453                 :             :  */
     454                 :             : Datum
     455                 :         951 : box_out(PG_FUNCTION_ARGS)
     456                 :             : {
     457                 :         951 :         BOX                *box = PG_GETARG_BOX_P(0);
     458                 :             : 
     459                 :        1902 :         PG_RETURN_CSTRING(path_encode(PATH_NONE, 2, &(box->high)));
     460                 :         951 : }
     461                 :             : 
     462                 :             : /*
     463                 :             :  *              box_recv                        - converts external binary format to box
     464                 :             :  */
     465                 :             : Datum
     466                 :           0 : box_recv(PG_FUNCTION_ARGS)
     467                 :             : {
     468                 :           0 :         StringInfo      buf = (StringInfo) PG_GETARG_POINTER(0);
     469                 :           0 :         BOX                *box;
     470                 :           0 :         float8          x,
     471                 :             :                                 y;
     472                 :             : 
     473                 :           0 :         box = palloc_object(BOX);
     474                 :             : 
     475                 :           0 :         box->high.x = pq_getmsgfloat8(buf);
     476                 :           0 :         box->high.y = pq_getmsgfloat8(buf);
     477                 :           0 :         box->low.x = pq_getmsgfloat8(buf);
     478                 :           0 :         box->low.y = pq_getmsgfloat8(buf);
     479                 :             : 
     480                 :             :         /* reorder corners if necessary... */
     481         [ #  # ]:           0 :         if (float8_lt(box->high.x, box->low.x))
     482                 :             :         {
     483                 :           0 :                 x = box->high.x;
     484                 :           0 :                 box->high.x = box->low.x;
     485                 :           0 :                 box->low.x = x;
     486                 :           0 :         }
     487         [ #  # ]:           0 :         if (float8_lt(box->high.y, box->low.y))
     488                 :             :         {
     489                 :           0 :                 y = box->high.y;
     490                 :           0 :                 box->high.y = box->low.y;
     491                 :           0 :                 box->low.y = y;
     492                 :           0 :         }
     493                 :             : 
     494                 :           0 :         PG_RETURN_BOX_P(box);
     495                 :           0 : }
     496                 :             : 
     497                 :             : /*
     498                 :             :  *              box_send                        - converts box to binary format
     499                 :             :  */
     500                 :             : Datum
     501                 :           0 : box_send(PG_FUNCTION_ARGS)
     502                 :             : {
     503                 :           0 :         BOX                *box = PG_GETARG_BOX_P(0);
     504                 :           0 :         StringInfoData buf;
     505                 :             : 
     506                 :           0 :         pq_begintypsend(&buf);
     507                 :           0 :         pq_sendfloat8(&buf, box->high.x);
     508                 :           0 :         pq_sendfloat8(&buf, box->high.y);
     509                 :           0 :         pq_sendfloat8(&buf, box->low.x);
     510                 :           0 :         pq_sendfloat8(&buf, box->low.y);
     511                 :           0 :         PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
     512                 :           0 : }
     513                 :             : 
     514                 :             : 
     515                 :             : /*              box_construct   -               fill in a new box.
     516                 :             :  */
     517                 :             : static inline void
     518                 :       43384 : box_construct(BOX *result, Point *pt1, Point *pt2)
     519                 :             : {
     520         [ +  + ]:       43384 :         if (float8_gt(pt1->x, pt2->x))
     521                 :             :         {
     522                 :        3093 :                 result->high.x = pt1->x;
     523                 :        3093 :                 result->low.x = pt2->x;
     524                 :        3093 :         }
     525                 :             :         else
     526                 :             :         {
     527                 :       40291 :                 result->high.x = pt2->x;
     528                 :       40291 :                 result->low.x = pt1->x;
     529                 :             :         }
     530         [ +  + ]:       43384 :         if (float8_gt(pt1->y, pt2->y))
     531                 :             :         {
     532                 :        3089 :                 result->high.y = pt1->y;
     533                 :        3089 :                 result->low.y = pt2->y;
     534                 :        3089 :         }
     535                 :             :         else
     536                 :             :         {
     537                 :       40295 :                 result->high.y = pt2->y;
     538                 :       40295 :                 result->low.y = pt1->y;
     539                 :             :         }
     540                 :       43384 : }
     541                 :             : 
     542                 :             : 
     543                 :             : /*----------------------------------------------------------
     544                 :             :  *      Relational operators for BOXes.
     545                 :             :  *              <, >, <=, >=, and == are based on box area.
     546                 :             :  *---------------------------------------------------------*/
     547                 :             : 
     548                 :             : /*              box_same                -               are two boxes identical?
     549                 :             :  */
     550                 :             : Datum
     551                 :        2293 : box_same(PG_FUNCTION_ARGS)
     552                 :             : {
     553                 :        2293 :         BOX                *box1 = PG_GETARG_BOX_P(0);
     554                 :        2293 :         BOX                *box2 = PG_GETARG_BOX_P(1);
     555                 :             : 
     556         [ +  + ]:        2293 :         PG_RETURN_BOOL(point_eq_point(&box1->high, &box2->high) &&
     557                 :             :                                    point_eq_point(&box1->low, &box2->low));
     558                 :        2293 : }
     559                 :             : 
     560                 :             : /*              box_overlap             -               does box1 overlap box2?
     561                 :             :  */
     562                 :             : Datum
     563                 :       10789 : box_overlap(PG_FUNCTION_ARGS)
     564                 :             : {
     565                 :       10789 :         BOX                *box1 = PG_GETARG_BOX_P(0);
     566                 :       10789 :         BOX                *box2 = PG_GETARG_BOX_P(1);
     567                 :             : 
     568                 :       21578 :         PG_RETURN_BOOL(box_ov(box1, box2));
     569                 :       10789 : }
     570                 :             : 
     571                 :             : static bool
     572                 :      246030 : box_ov(BOX *box1, BOX *box2)
     573                 :             : {
     574         [ +  + ]:      371674 :         return (FPle(box1->low.x, box2->high.x) &&
     575         [ +  + ]:      125644 :                         FPle(box2->low.x, box1->high.x) &&
     576         [ +  + ]:       20487 :                         FPle(box1->low.y, box2->high.y) &&
     577                 :       16412 :                         FPle(box2->low.y, box1->high.y));
     578                 :             : }
     579                 :             : 
     580                 :             : /*              box_left                -               is box1 strictly left of box2?
     581                 :             :  */
     582                 :             : Datum
     583                 :        8337 : box_left(PG_FUNCTION_ARGS)
     584                 :             : {
     585                 :        8337 :         BOX                *box1 = PG_GETARG_BOX_P(0);
     586                 :        8337 :         BOX                *box2 = PG_GETARG_BOX_P(1);
     587                 :             : 
     588                 :       16674 :         PG_RETURN_BOOL(FPlt(box1->high.x, box2->low.x));
     589                 :        8337 : }
     590                 :             : 
     591                 :             : /*              box_overleft    -               is the right edge of box1 at or left of
     592                 :             :  *                                                              the right edge of box2?
     593                 :             :  *
     594                 :             :  *              This is "less than or equal" for the end of a time range,
     595                 :             :  *              when time ranges are stored as rectangles.
     596                 :             :  */
     597                 :             : Datum
     598                 :       16618 : box_overleft(PG_FUNCTION_ARGS)
     599                 :             : {
     600                 :       16618 :         BOX                *box1 = PG_GETARG_BOX_P(0);
     601                 :       16618 :         BOX                *box2 = PG_GETARG_BOX_P(1);
     602                 :             : 
     603                 :       33236 :         PG_RETURN_BOOL(FPle(box1->high.x, box2->high.x));
     604                 :       16618 : }
     605                 :             : 
     606                 :             : /*              box_right               -               is box1 strictly right of box2?
     607                 :             :  */
     608                 :             : Datum
     609                 :       21957 : box_right(PG_FUNCTION_ARGS)
     610                 :             : {
     611                 :       21957 :         BOX                *box1 = PG_GETARG_BOX_P(0);
     612                 :       21957 :         BOX                *box2 = PG_GETARG_BOX_P(1);
     613                 :             : 
     614                 :       43914 :         PG_RETURN_BOOL(FPgt(box1->low.x, box2->high.x));
     615                 :       21957 : }
     616                 :             : 
     617                 :             : /*              box_overright   -               is the left edge of box1 at or right of
     618                 :             :  *                                                              the left edge of box2?
     619                 :             :  *
     620                 :             :  *              This is "greater than or equal" for time ranges, when time ranges
     621                 :             :  *              are stored as rectangles.
     622                 :             :  */
     623                 :             : Datum
     624                 :       18733 : box_overright(PG_FUNCTION_ARGS)
     625                 :             : {
     626                 :       18733 :         BOX                *box1 = PG_GETARG_BOX_P(0);
     627                 :       18733 :         BOX                *box2 = PG_GETARG_BOX_P(1);
     628                 :             : 
     629                 :       37466 :         PG_RETURN_BOOL(FPge(box1->low.x, box2->low.x));
     630                 :       18733 : }
     631                 :             : 
     632                 :             : /*              box_below               -               is box1 strictly below box2?
     633                 :             :  */
     634                 :             : Datum
     635                 :        4613 : box_below(PG_FUNCTION_ARGS)
     636                 :             : {
     637                 :        4613 :         BOX                *box1 = PG_GETARG_BOX_P(0);
     638                 :        4613 :         BOX                *box2 = PG_GETARG_BOX_P(1);
     639                 :             : 
     640                 :        9226 :         PG_RETURN_BOOL(FPlt(box1->high.y, box2->low.y));
     641                 :        4613 : }
     642                 :             : 
     643                 :             : /*              box_overbelow   -               is the upper edge of box1 at or below
     644                 :             :  *                                                              the upper edge of box2?
     645                 :             :  */
     646                 :             : Datum
     647                 :       13406 : box_overbelow(PG_FUNCTION_ARGS)
     648                 :             : {
     649                 :       13406 :         BOX                *box1 = PG_GETARG_BOX_P(0);
     650                 :       13406 :         BOX                *box2 = PG_GETARG_BOX_P(1);
     651                 :             : 
     652                 :       26812 :         PG_RETURN_BOOL(FPle(box1->high.y, box2->high.y));
     653                 :       13406 : }
     654                 :             : 
     655                 :             : /*              box_above               -               is box1 strictly above box2?
     656                 :             :  */
     657                 :             : Datum
     658                 :        9620 : box_above(PG_FUNCTION_ARGS)
     659                 :             : {
     660                 :        9620 :         BOX                *box1 = PG_GETARG_BOX_P(0);
     661                 :        9620 :         BOX                *box2 = PG_GETARG_BOX_P(1);
     662                 :             : 
     663                 :       19240 :         PG_RETURN_BOOL(FPgt(box1->low.y, box2->high.y));
     664                 :        9620 : }
     665                 :             : 
     666                 :             : /*              box_overabove   -               is the lower edge of box1 at or above
     667                 :             :  *                                                              the lower edge of box2?
     668                 :             :  */
     669                 :             : Datum
     670                 :       18525 : box_overabove(PG_FUNCTION_ARGS)
     671                 :             : {
     672                 :       18525 :         BOX                *box1 = PG_GETARG_BOX_P(0);
     673                 :       18525 :         BOX                *box2 = PG_GETARG_BOX_P(1);
     674                 :             : 
     675                 :       37050 :         PG_RETURN_BOOL(FPge(box1->low.y, box2->low.y));
     676                 :       18525 : }
     677                 :             : 
     678                 :             : /*              box_contained   -               is box1 contained by box2?
     679                 :             :  */
     680                 :             : Datum
     681                 :       26382 : box_contained(PG_FUNCTION_ARGS)
     682                 :             : {
     683                 :       26382 :         BOX                *box1 = PG_GETARG_BOX_P(0);
     684                 :       26382 :         BOX                *box2 = PG_GETARG_BOX_P(1);
     685                 :             : 
     686                 :       52764 :         PG_RETURN_BOOL(box_contain_box(box2, box1));
     687                 :       26382 : }
     688                 :             : 
     689                 :             : /*              box_contain             -               does box1 contain box2?
     690                 :             :  */
     691                 :             : Datum
     692                 :        2042 : box_contain(PG_FUNCTION_ARGS)
     693                 :             : {
     694                 :        2042 :         BOX                *box1 = PG_GETARG_BOX_P(0);
     695                 :        2042 :         BOX                *box2 = PG_GETARG_BOX_P(1);
     696                 :             : 
     697                 :        4084 :         PG_RETURN_BOOL(box_contain_box(box1, box2));
     698                 :        2042 : }
     699                 :             : 
     700                 :             : /*
     701                 :             :  * Check whether the second box is in the first box or on its border
     702                 :             :  */
     703                 :             : static bool
     704                 :       42579 : box_contain_box(BOX *contains_box, BOX *contained_box)
     705                 :             : {
     706         [ +  + ]:       69592 :         return FPge(contains_box->high.x, contained_box->high.x) &&
     707         [ +  + ]:       27013 :                 FPle(contains_box->low.x, contained_box->low.x) &&
     708         [ +  + ]:       19296 :                 FPge(contains_box->high.y, contained_box->high.y) &&
     709                 :       16249 :                 FPle(contains_box->low.y, contained_box->low.y);
     710                 :             : }
     711                 :             : 
     712                 :             : 
     713                 :             : /*              box_positionop  -
     714                 :             :  *                              is box1 entirely {above,below} box2?
     715                 :             :  *
     716                 :             :  * box_below_eq and box_above_eq are obsolete versions that (probably
     717                 :             :  * erroneously) accept the equal-boundaries case.  Since these are not
     718                 :             :  * in sync with the box_left and box_right code, they are deprecated and
     719                 :             :  * not supported in the PG 8.1 rtree operator class extension.
     720                 :             :  */
     721                 :             : Datum
     722                 :          25 : box_below_eq(PG_FUNCTION_ARGS)
     723                 :             : {
     724                 :          25 :         BOX                *box1 = PG_GETARG_BOX_P(0);
     725                 :          25 :         BOX                *box2 = PG_GETARG_BOX_P(1);
     726                 :             : 
     727                 :          50 :         PG_RETURN_BOOL(FPle(box1->high.y, box2->low.y));
     728                 :          25 : }
     729                 :             : 
     730                 :             : Datum
     731                 :          25 : box_above_eq(PG_FUNCTION_ARGS)
     732                 :             : {
     733                 :          25 :         BOX                *box1 = PG_GETARG_BOX_P(0);
     734                 :          25 :         BOX                *box2 = PG_GETARG_BOX_P(1);
     735                 :             : 
     736                 :          50 :         PG_RETURN_BOOL(FPge(box1->low.y, box2->high.y));
     737                 :          25 : }
     738                 :             : 
     739                 :             : 
     740                 :             : /*              box_relop               -               is area(box1) relop area(box2), within
     741                 :             :  *                                                              our accuracy constraint?
     742                 :             :  */
     743                 :             : Datum
     744                 :           5 : box_lt(PG_FUNCTION_ARGS)
     745                 :             : {
     746                 :           5 :         BOX                *box1 = PG_GETARG_BOX_P(0);
     747                 :           5 :         BOX                *box2 = PG_GETARG_BOX_P(1);
     748                 :             : 
     749                 :          10 :         PG_RETURN_BOOL(FPlt(box_ar(box1), box_ar(box2)));
     750                 :           5 : }
     751                 :             : 
     752                 :             : Datum
     753                 :           5 : box_gt(PG_FUNCTION_ARGS)
     754                 :             : {
     755                 :           5 :         BOX                *box1 = PG_GETARG_BOX_P(0);
     756                 :           5 :         BOX                *box2 = PG_GETARG_BOX_P(1);
     757                 :             : 
     758                 :          10 :         PG_RETURN_BOOL(FPgt(box_ar(box1), box_ar(box2)));
     759                 :           5 : }
     760                 :             : 
     761                 :             : Datum
     762                 :           5 : box_eq(PG_FUNCTION_ARGS)
     763                 :             : {
     764                 :           5 :         BOX                *box1 = PG_GETARG_BOX_P(0);
     765                 :           5 :         BOX                *box2 = PG_GETARG_BOX_P(1);
     766                 :             : 
     767                 :          10 :         PG_RETURN_BOOL(FPeq(box_ar(box1), box_ar(box2)));
     768                 :           5 : }
     769                 :             : 
     770                 :             : Datum
     771                 :           5 : box_le(PG_FUNCTION_ARGS)
     772                 :             : {
     773                 :           5 :         BOX                *box1 = PG_GETARG_BOX_P(0);
     774                 :           5 :         BOX                *box2 = PG_GETARG_BOX_P(1);
     775                 :             : 
     776                 :          10 :         PG_RETURN_BOOL(FPle(box_ar(box1), box_ar(box2)));
     777                 :           5 : }
     778                 :             : 
     779                 :             : Datum
     780                 :           5 : box_ge(PG_FUNCTION_ARGS)
     781                 :             : {
     782                 :           5 :         BOX                *box1 = PG_GETARG_BOX_P(0);
     783                 :           5 :         BOX                *box2 = PG_GETARG_BOX_P(1);
     784                 :             : 
     785                 :          10 :         PG_RETURN_BOOL(FPge(box_ar(box1), box_ar(box2)));
     786                 :           5 : }
     787                 :             : 
     788                 :             : 
     789                 :             : /*----------------------------------------------------------
     790                 :             :  *      "Arithmetic" operators on boxes.
     791                 :             :  *---------------------------------------------------------*/
     792                 :             : 
     793                 :             : /*              box_area                -               returns the area of the box.
     794                 :             :  */
     795                 :             : Datum
     796                 :           5 : box_area(PG_FUNCTION_ARGS)
     797                 :             : {
     798                 :           5 :         BOX                *box = PG_GETARG_BOX_P(0);
     799                 :             : 
     800                 :          10 :         PG_RETURN_FLOAT8(box_ar(box));
     801                 :           5 : }
     802                 :             : 
     803                 :             : 
     804                 :             : /*              box_width               -               returns the width of the box
     805                 :             :  *                                                                (horizontal magnitude).
     806                 :             :  */
     807                 :             : Datum
     808                 :           5 : box_width(PG_FUNCTION_ARGS)
     809                 :             : {
     810                 :           5 :         BOX                *box = PG_GETARG_BOX_P(0);
     811                 :             : 
     812                 :          10 :         PG_RETURN_FLOAT8(box_wd(box));
     813                 :           5 : }
     814                 :             : 
     815                 :             : 
     816                 :             : /*              box_height              -               returns the height of the box
     817                 :             :  *                                                                (vertical magnitude).
     818                 :             :  */
     819                 :             : Datum
     820                 :           5 : box_height(PG_FUNCTION_ARGS)
     821                 :             : {
     822                 :           5 :         BOX                *box = PG_GETARG_BOX_P(0);
     823                 :             : 
     824                 :          10 :         PG_RETURN_FLOAT8(box_ht(box));
     825                 :           5 : }
     826                 :             : 
     827                 :             : 
     828                 :             : /*              box_distance    -               returns the distance between the
     829                 :             :  *                                                                center points of two boxes.
     830                 :             :  */
     831                 :             : Datum
     832                 :          25 : box_distance(PG_FUNCTION_ARGS)
     833                 :             : {
     834                 :          25 :         BOX                *box1 = PG_GETARG_BOX_P(0);
     835                 :          25 :         BOX                *box2 = PG_GETARG_BOX_P(1);
     836                 :          25 :         Point           a,
     837                 :             :                                 b;
     838                 :             : 
     839                 :          25 :         box_cn(&a, box1);
     840                 :          25 :         box_cn(&b, box2);
     841                 :             : 
     842                 :          50 :         PG_RETURN_FLOAT8(point_dt(&a, &b));
     843                 :          25 : }
     844                 :             : 
     845                 :             : 
     846                 :             : /*              box_center              -               returns the center point of the box.
     847                 :             :  */
     848                 :             : Datum
     849                 :          15 : box_center(PG_FUNCTION_ARGS)
     850                 :             : {
     851                 :          15 :         BOX                *box = PG_GETARG_BOX_P(0);
     852                 :          15 :         Point      *result = palloc_object(Point);
     853                 :             : 
     854                 :          15 :         box_cn(result, box);
     855                 :             : 
     856                 :          30 :         PG_RETURN_POINT_P(result);
     857                 :          15 : }
     858                 :             : 
     859                 :             : 
     860                 :             : /*              box_ar  -               returns the area of the box.
     861                 :             :  */
     862                 :             : static float8
     863                 :          55 : box_ar(BOX *box)
     864                 :             : {
     865                 :          55 :         return float8_mul(box_wd(box), box_ht(box));
     866                 :             : }
     867                 :             : 
     868                 :             : 
     869                 :             : /*              box_cn  -               stores the centerpoint of the box into *center.
     870                 :             :  */
     871                 :             : static void
     872                 :          79 : box_cn(Point *center, BOX *box)
     873                 :             : {
     874                 :          79 :         center->x = float8_div(float8_pl(box->high.x, box->low.x), 2.0);
     875                 :          79 :         center->y = float8_div(float8_pl(box->high.y, box->low.y), 2.0);
     876                 :          79 : }
     877                 :             : 
     878                 :             : 
     879                 :             : /*              box_wd  -               returns the width (length) of the box
     880                 :             :  *                                                                (horizontal magnitude).
     881                 :             :  */
     882                 :             : static float8
     883                 :          60 : box_wd(BOX *box)
     884                 :             : {
     885                 :          60 :         return float8_mi(box->high.x, box->low.x);
     886                 :             : }
     887                 :             : 
     888                 :             : 
     889                 :             : /*              box_ht  -               returns the height of the box
     890                 :             :  *                                                                (vertical magnitude).
     891                 :             :  */
     892                 :             : static float8
     893                 :          60 : box_ht(BOX *box)
     894                 :             : {
     895                 :          60 :         return float8_mi(box->high.y, box->low.y);
     896                 :             : }
     897                 :             : 
     898                 :             : 
     899                 :             : /*----------------------------------------------------------
     900                 :             :  *      Funky operations.
     901                 :             :  *---------------------------------------------------------*/
     902                 :             : 
     903                 :             : /*              box_intersect   -
     904                 :             :  *                              returns the overlapping portion of two boxes,
     905                 :             :  *                                or NULL if they do not intersect.
     906                 :             :  */
     907                 :             : Datum
     908                 :          25 : box_intersect(PG_FUNCTION_ARGS)
     909                 :             : {
     910                 :          25 :         BOX                *box1 = PG_GETARG_BOX_P(0);
     911                 :          25 :         BOX                *box2 = PG_GETARG_BOX_P(1);
     912                 :          25 :         BOX                *result;
     913                 :             : 
     914         [ +  + ]:          25 :         if (!box_ov(box1, box2))
     915                 :          14 :                 PG_RETURN_NULL();
     916                 :             : 
     917                 :          11 :         result = palloc_object(BOX);
     918                 :             : 
     919                 :          11 :         result->high.x = float8_min(box1->high.x, box2->high.x);
     920                 :          11 :         result->low.x = float8_max(box1->low.x, box2->low.x);
     921                 :          11 :         result->high.y = float8_min(box1->high.y, box2->high.y);
     922                 :          11 :         result->low.y = float8_max(box1->low.y, box2->low.y);
     923                 :             : 
     924                 :          11 :         PG_RETURN_BOX_P(result);
     925                 :          25 : }
     926                 :             : 
     927                 :             : 
     928                 :             : /*              box_diagonal    -
     929                 :             :  *                              returns a line segment which happens to be the
     930                 :             :  *                                positive-slope diagonal of "box".
     931                 :             :  */
     932                 :             : Datum
     933                 :           5 : box_diagonal(PG_FUNCTION_ARGS)
     934                 :             : {
     935                 :           5 :         BOX                *box = PG_GETARG_BOX_P(0);
     936                 :           5 :         LSEG       *result = palloc_object(LSEG);
     937                 :             : 
     938                 :           5 :         statlseg_construct(result, &box->high, &box->low);
     939                 :             : 
     940                 :          10 :         PG_RETURN_LSEG_P(result);
     941                 :           5 : }
     942                 :             : 
     943                 :             : /***********************************************************************
     944                 :             :  **
     945                 :             :  **             Routines for 2D lines.
     946                 :             :  **
     947                 :             :  ***********************************************************************/
     948                 :             : 
     949                 :             : static bool
     950                 :          22 : line_decode(char *s, const char *str, LINE *line, Node *escontext)
     951                 :             : {
     952                 :             :         /* s was already advanced over leading '{' */
     953         [ -  + ]:          22 :         if (!single_decode(s, &line->A, &s, "line", str, escontext))
     954                 :           0 :                 return false;
     955         [ +  + ]:          22 :         if (*s++ != DELIM)
     956                 :           1 :                 goto fail;
     957         [ +  - ]:          21 :         if (!single_decode(s, &line->B, &s, "line", str, escontext))
     958                 :           0 :                 return false;
     959         [ +  + ]:          21 :         if (*s++ != DELIM)
     960                 :           3 :                 goto fail;
     961         [ +  + ]:          18 :         if (!single_decode(s, &line->C, &s, "line", str, escontext))
     962                 :           4 :                 return false;
     963         [ +  + ]:          14 :         if (*s++ != RDELIM_L)
     964                 :           1 :                 goto fail;
     965         [ +  + ]:          14 :         while (isspace((unsigned char) *s))
     966                 :           1 :                 s++;
     967         [ +  + ]:          13 :         if (*s != '\0')
     968                 :           1 :                 goto fail;
     969                 :          12 :         return true;
     970                 :             : 
     971                 :             : fail:
     972         [ +  + ]:           6 :         ereturn(escontext, false,
     973                 :             :                         (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
     974                 :             :                          errmsg("invalid input syntax for type %s: \"%s\"",
     975                 :             :                                         "line", str)));
     976                 :          22 : }
     977                 :             : 
     978                 :             : Datum
     979                 :          26 : line_in(PG_FUNCTION_ARGS)
     980                 :             : {
     981                 :          26 :         char       *str = PG_GETARG_CSTRING(0);
     982                 :          26 :         Node       *escontext = fcinfo->context;
     983                 :          26 :         LINE       *line = palloc_object(LINE);
     984                 :          26 :         LSEG            lseg;
     985                 :          26 :         bool            isopen;
     986                 :          26 :         char       *s;
     987                 :             : 
     988                 :          26 :         s = str;
     989         [ +  + ]:          27 :         while (isspace((unsigned char) *s))
     990                 :           1 :                 s++;
     991         [ +  + ]:          26 :         if (*s == LDELIM_L)
     992                 :             :         {
     993         [ +  + ]:          17 :                 if (!line_decode(s + 1, str, line, escontext))
     994                 :           6 :                         PG_RETURN_NULL();
     995   [ +  +  +  + ]:          11 :                 if (FPzero(line->A) && FPzero(line->B))
     996         [ -  + ]:           2 :                         ereturn(escontext, (Datum) 0,
     997                 :             :                                         (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
     998                 :             :                                          errmsg("invalid line specification: A and B cannot both be zero")));
     999                 :           9 :         }
    1000                 :             :         else
    1001                 :             :         {
    1002   [ +  +  +  + ]:          18 :                 if (!path_decode(s, true, 2, &lseg.p[0], &isopen, NULL, "line", str,
    1003                 :           9 :                                                  escontext))
    1004                 :           2 :                         PG_RETURN_NULL();
    1005         [ +  + ]:           7 :                 if (point_eq_point(&lseg.p[0], &lseg.p[1]))
    1006         [ +  + ]:           2 :                         ereturn(escontext, (Datum) 0,
    1007                 :             :                                         (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
    1008                 :             :                                          errmsg("invalid line specification: must be two distinct points")));
    1009                 :             : 
    1010                 :             :                 /*
    1011                 :             :                  * XXX lseg_sl() and line_construct() can throw overflow/underflow
    1012                 :             :                  * errors.  Eventually we should allow those to be soft, but the
    1013                 :             :                  * notational pain seems to outweigh the value for now.
    1014                 :             :                  */
    1015                 :           5 :                 line_construct(line, &lseg.p[0], lseg_sl(&lseg));
    1016                 :             :         }
    1017                 :             : 
    1018                 :          14 :         PG_RETURN_LINE_P(line);
    1019                 :          24 : }
    1020                 :             : 
    1021                 :             : 
    1022                 :             : Datum
    1023                 :        1146 : line_out(PG_FUNCTION_ARGS)
    1024                 :             : {
    1025                 :        1146 :         LINE       *line = PG_GETARG_LINE_P(0);
    1026                 :        1146 :         char       *astr = float8out_internal(line->A);
    1027                 :        1146 :         char       *bstr = float8out_internal(line->B);
    1028                 :        1146 :         char       *cstr = float8out_internal(line->C);
    1029                 :             : 
    1030                 :        2292 :         PG_RETURN_CSTRING(psprintf("%c%s%c%s%c%s%c", LDELIM_L, astr, DELIM, bstr,
    1031                 :             :                                                            DELIM, cstr, RDELIM_L));
    1032                 :        1146 : }
    1033                 :             : 
    1034                 :             : /*
    1035                 :             :  *              line_recv                       - converts external binary format to line
    1036                 :             :  */
    1037                 :             : Datum
    1038                 :           0 : line_recv(PG_FUNCTION_ARGS)
    1039                 :             : {
    1040                 :           0 :         StringInfo      buf = (StringInfo) PG_GETARG_POINTER(0);
    1041                 :           0 :         LINE       *line;
    1042                 :             : 
    1043                 :           0 :         line = palloc_object(LINE);
    1044                 :             : 
    1045                 :           0 :         line->A = pq_getmsgfloat8(buf);
    1046                 :           0 :         line->B = pq_getmsgfloat8(buf);
    1047                 :           0 :         line->C = pq_getmsgfloat8(buf);
    1048                 :             : 
    1049   [ #  #  #  # ]:           0 :         if (FPzero(line->A) && FPzero(line->B))
    1050   [ #  #  #  # ]:           0 :                 ereport(ERROR,
    1051                 :             :                                 (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
    1052                 :             :                                  errmsg("invalid line specification: A and B cannot both be zero")));
    1053                 :             : 
    1054                 :           0 :         PG_RETURN_LINE_P(line);
    1055                 :           0 : }
    1056                 :             : 
    1057                 :             : /*
    1058                 :             :  *              line_send                       - converts line to binary format
    1059                 :             :  */
    1060                 :             : Datum
    1061                 :           0 : line_send(PG_FUNCTION_ARGS)
    1062                 :             : {
    1063                 :           0 :         LINE       *line = PG_GETARG_LINE_P(0);
    1064                 :           0 :         StringInfoData buf;
    1065                 :             : 
    1066                 :           0 :         pq_begintypsend(&buf);
    1067                 :           0 :         pq_sendfloat8(&buf, line->A);
    1068                 :           0 :         pq_sendfloat8(&buf, line->B);
    1069                 :           0 :         pq_sendfloat8(&buf, line->C);
    1070                 :           0 :         PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
    1071                 :           0 : }
    1072                 :             : 
    1073                 :             : 
    1074                 :             : /*----------------------------------------------------------
    1075                 :             :  *      Conversion routines from one line formula to internal.
    1076                 :             :  *              Internal form:  Ax+By+C=0
    1077                 :             :  *---------------------------------------------------------*/
    1078                 :             : 
    1079                 :             : /*
    1080                 :             :  * Fill already-allocated LINE struct from the point and the slope
    1081                 :             :  */
    1082                 :             : static inline void
    1083                 :     1090910 : line_construct(LINE *result, Point *pt, float8 m)
    1084                 :             : {
    1085         [ +  + ]:     1090910 :         if (isinf(m))
    1086                 :             :         {
    1087                 :             :                 /* vertical - use "x = C" */
    1088                 :      249116 :                 result->A = -1.0;
    1089                 :      249116 :                 result->B = 0.0;
    1090                 :      249116 :                 result->C = pt->x;
    1091                 :      249116 :         }
    1092         [ +  + ]:      841794 :         else if (m == 0)
    1093                 :             :         {
    1094                 :             :                 /* horizontal - use "y = C" */
    1095                 :      246403 :                 result->A = 0.0;
    1096                 :      246403 :                 result->B = -1.0;
    1097                 :      246403 :                 result->C = pt->y;
    1098                 :      246403 :         }
    1099                 :             :         else
    1100                 :             :         {
    1101                 :             :                 /* use "mx - y + yinter = 0" */
    1102                 :      595391 :                 result->A = m;
    1103                 :      595391 :                 result->B = -1.0;
    1104                 :      595391 :                 result->C = float8_mi(pt->y, float8_mul(m, pt->x));
    1105                 :             :                 /* on some platforms, the preceding expression tends to produce -0 */
    1106         [ +  + ]:      595391 :                 if (result->C == 0.0)
    1107                 :         810 :                         result->C = 0.0;
    1108                 :             :         }
    1109                 :     1090910 : }
    1110                 :             : 
    1111                 :             : /* line_construct_pp()
    1112                 :             :  * two points
    1113                 :             :  */
    1114                 :             : Datum
    1115                 :          90 : line_construct_pp(PG_FUNCTION_ARGS)
    1116                 :             : {
    1117                 :          90 :         Point      *pt1 = PG_GETARG_POINT_P(0);
    1118                 :          90 :         Point      *pt2 = PG_GETARG_POINT_P(1);
    1119                 :          90 :         LINE       *result = palloc_object(LINE);
    1120                 :             : 
    1121         [ +  + ]:          90 :         if (point_eq_point(pt1, pt2))
    1122   [ +  -  +  - ]:           1 :                 ereport(ERROR,
    1123                 :             :                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    1124                 :             :                                  errmsg("invalid line specification: must be two distinct points")));
    1125                 :             : 
    1126                 :          89 :         line_construct(result, pt1, point_sl(pt1, pt2));
    1127                 :             : 
    1128                 :         178 :         PG_RETURN_LINE_P(result);
    1129                 :          89 : }
    1130                 :             : 
    1131                 :             : 
    1132                 :             : /*----------------------------------------------------------
    1133                 :             :  *      Relative position routines.
    1134                 :             :  *---------------------------------------------------------*/
    1135                 :             : 
    1136                 :             : Datum
    1137                 :         100 : line_intersect(PG_FUNCTION_ARGS)
    1138                 :             : {
    1139                 :         100 :         LINE       *l1 = PG_GETARG_LINE_P(0);
    1140                 :         100 :         LINE       *l2 = PG_GETARG_LINE_P(1);
    1141                 :             : 
    1142                 :         200 :         PG_RETURN_BOOL(line_interpt_line(NULL, l1, l2));
    1143                 :         100 : }
    1144                 :             : 
    1145                 :             : Datum
    1146                 :         100 : line_parallel(PG_FUNCTION_ARGS)
    1147                 :             : {
    1148                 :         100 :         LINE       *l1 = PG_GETARG_LINE_P(0);
    1149                 :         100 :         LINE       *l2 = PG_GETARG_LINE_P(1);
    1150                 :             : 
    1151                 :         200 :         PG_RETURN_BOOL(!line_interpt_line(NULL, l1, l2));
    1152                 :         100 : }
    1153                 :             : 
    1154                 :             : Datum
    1155                 :         100 : line_perp(PG_FUNCTION_ARGS)
    1156                 :             : {
    1157                 :         100 :         LINE       *l1 = PG_GETARG_LINE_P(0);
    1158                 :         100 :         LINE       *l2 = PG_GETARG_LINE_P(1);
    1159                 :             : 
    1160         [ +  + ]:         100 :         if (FPzero(l1->A))
    1161                 :          30 :                 PG_RETURN_BOOL(FPzero(l2->B));
    1162         [ +  + ]:          70 :         if (FPzero(l2->A))
    1163                 :          21 :                 PG_RETURN_BOOL(FPzero(l1->B));
    1164         [ +  + ]:          49 :         if (FPzero(l1->B))
    1165                 :          14 :                 PG_RETURN_BOOL(FPzero(l2->A));
    1166         [ +  + ]:          35 :         if (FPzero(l2->B))
    1167                 :          10 :                 PG_RETURN_BOOL(FPzero(l1->A));
    1168                 :             : 
    1169                 :          25 :         PG_RETURN_BOOL(FPeq(float8_div(float8_mul(l1->A, l2->A),
    1170                 :             :                                                                    float8_mul(l1->B, l2->B)), -1.0));
    1171                 :         100 : }
    1172                 :             : 
    1173                 :             : Datum
    1174                 :          10 : line_vertical(PG_FUNCTION_ARGS)
    1175                 :             : {
    1176                 :          10 :         LINE       *line = PG_GETARG_LINE_P(0);
    1177                 :             : 
    1178                 :          20 :         PG_RETURN_BOOL(FPzero(line->B));
    1179                 :          10 : }
    1180                 :             : 
    1181                 :             : Datum
    1182                 :          10 : line_horizontal(PG_FUNCTION_ARGS)
    1183                 :             : {
    1184                 :          10 :         LINE       *line = PG_GETARG_LINE_P(0);
    1185                 :             : 
    1186                 :          20 :         PG_RETURN_BOOL(FPzero(line->A));
    1187                 :          10 : }
    1188                 :             : 
    1189                 :             : 
    1190                 :             : /*
    1191                 :             :  * Check whether the two lines are the same
    1192                 :             :  */
    1193                 :             : Datum
    1194                 :         102 : line_eq(PG_FUNCTION_ARGS)
    1195                 :             : {
    1196                 :         102 :         LINE       *l1 = PG_GETARG_LINE_P(0);
    1197                 :         102 :         LINE       *l2 = PG_GETARG_LINE_P(1);
    1198                 :         102 :         float8          ratio;
    1199                 :             : 
    1200                 :             :         /* If any NaNs are involved, insist on exact equality */
    1201   [ -  +  +  +  :         102 :         if (unlikely(isnan(l1->A) || isnan(l1->B) || isnan(l1->C) ||
          +  -  -  +  +  
          +  +  -  -  +  
          +  +  +  -  -  
          +  +  +  +  -  
          -  +  +  +  +  
                -  +  + ]
    1202                 :             :                                  isnan(l2->A) || isnan(l2->B) || isnan(l2->C)))
    1203                 :             :         {
    1204   [ +  +  +  + ]:         810 :                 PG_RETURN_BOOL(float8_eq(l1->A, l2->A) &&
    1205                 :             :                                            float8_eq(l1->B, l2->B) &&
    1206                 :             :                                            float8_eq(l1->C, l2->C));
    1207                 :             :         }
    1208                 :             : 
    1209                 :             :         /* Otherwise, lines whose parameters are proportional are the same */
    1210         [ +  + ]:          64 :         if (!FPzero(l2->A))
    1211                 :          40 :                 ratio = float8_div(l1->A, l2->A);
    1212         [ -  + ]:          24 :         else if (!FPzero(l2->B))
    1213                 :          24 :                 ratio = float8_div(l1->B, l2->B);
    1214         [ #  # ]:           0 :         else if (!FPzero(l2->C))
    1215                 :           0 :                 ratio = float8_div(l1->C, l2->C);
    1216                 :             :         else
    1217                 :           0 :                 ratio = 1.0;
    1218                 :             : 
    1219   [ +  +  +  + ]:          64 :         PG_RETURN_BOOL(FPeq(l1->A, float8_mul(ratio, l2->A)) &&
    1220                 :             :                                    FPeq(l1->B, float8_mul(ratio, l2->B)) &&
    1221                 :             :                                    FPeq(l1->C, float8_mul(ratio, l2->C)));
    1222                 :         874 : }
    1223                 :             : 
    1224                 :             : 
    1225                 :             : /*----------------------------------------------------------
    1226                 :             :  *      Line arithmetic routines.
    1227                 :             :  *---------------------------------------------------------*/
    1228                 :             : 
    1229                 :             : /*
    1230                 :             :  * Return slope of the line
    1231                 :             :  */
    1232                 :             : static inline float8
    1233                 :          80 : line_sl(LINE *line)
    1234                 :             : {
    1235         [ +  + ]:          80 :         if (FPzero(line->A))
    1236                 :          24 :                 return 0.0;
    1237         [ +  + ]:          56 :         if (FPzero(line->B))
    1238                 :          16 :                 return get_float8_infinity();
    1239                 :          40 :         return float8_div(line->A, -line->B);
    1240                 :          80 : }
    1241                 :             : 
    1242                 :             : 
    1243                 :             : /*
    1244                 :             :  * Return inverse slope of the line
    1245                 :             :  */
    1246                 :             : static inline float8
    1247                 :      298860 : line_invsl(LINE *line)
    1248                 :             : {
    1249         [ +  + ]:      298860 :         if (FPzero(line->A))
    1250                 :      119220 :                 return get_float8_infinity();
    1251         [ +  + ]:      179640 :         if (FPzero(line->B))
    1252                 :      115934 :                 return 0.0;
    1253                 :       63706 :         return float8_div(line->B, line->A);
    1254                 :      298860 : }
    1255                 :             : 
    1256                 :             : 
    1257                 :             : /* line_distance()
    1258                 :             :  * Distance between two lines.
    1259                 :             :  */
    1260                 :             : Datum
    1261                 :         108 : line_distance(PG_FUNCTION_ARGS)
    1262                 :             : {
    1263                 :         108 :         LINE       *l1 = PG_GETARG_LINE_P(0);
    1264                 :         108 :         LINE       *l2 = PG_GETARG_LINE_P(1);
    1265                 :         108 :         float8          ratio;
    1266                 :             : 
    1267         [ +  + ]:         108 :         if (line_interpt_line(NULL, l1, l2))    /* intersecting? */
    1268                 :          84 :                 PG_RETURN_FLOAT8(0.0);
    1269                 :             : 
    1270   [ +  +  -  +  :          24 :         if (!FPzero(l1->A) && !isnan(l1->A) && !FPzero(l2->A) && !isnan(l2->A))
          +  +  +  -  -  
             +  +  +  +  
                      - ]
    1271                 :           7 :                 ratio = float8_div(l1->A, l2->A);
    1272   [ +  +  -  +  :          45 :         else if (!FPzero(l1->B) && !isnan(l1->B) && !FPzero(l2->B) && !isnan(l2->B))
          +  +  +  -  -  
             +  +  +  +  
                      - ]
    1273                 :           9 :                 ratio = float8_div(l1->B, l2->B);
    1274                 :             :         else
    1275                 :          72 :                 ratio = 1.0;
    1276                 :             : 
    1277                 :          16 :         PG_RETURN_FLOAT8(float8_div(fabs(float8_mi(l1->C,
    1278                 :             :                                                                                            float8_mul(ratio, l2->C))),
    1279                 :             :                                                                 hypot(l1->A, l1->B)));
    1280                 :         100 : }
    1281                 :             : 
    1282                 :             : /* line_interpt()
    1283                 :             :  * Point where two lines l1, l2 intersect (if any)
    1284                 :             :  */
    1285                 :             : Datum
    1286                 :         100 : line_interpt(PG_FUNCTION_ARGS)
    1287                 :             : {
    1288                 :         100 :         LINE       *l1 = PG_GETARG_LINE_P(0);
    1289                 :         100 :         LINE       *l2 = PG_GETARG_LINE_P(1);
    1290                 :         100 :         Point      *result;
    1291                 :             : 
    1292                 :         100 :         result = palloc_object(Point);
    1293                 :             : 
    1294         [ +  + ]:         100 :         if (!line_interpt_line(result, l1, l2))
    1295                 :          16 :                 PG_RETURN_NULL();
    1296                 :          84 :         PG_RETURN_POINT_P(result);
    1297                 :         100 : }
    1298                 :             : 
    1299                 :             : /*
    1300                 :             :  * Internal version of line_interpt
    1301                 :             :  *
    1302                 :             :  * Return whether two lines intersect. If *result is not NULL, it is set to
    1303                 :             :  * the intersection point.
    1304                 :             :  *
    1305                 :             :  * NOTE: If the lines are identical then we will find they are parallel
    1306                 :             :  * and report "no intersection".  This is a little weird, but since
    1307                 :             :  * there's no *unique* intersection, maybe it's appropriate behavior.
    1308                 :             :  *
    1309                 :             :  * If the lines have NaN constants, we will return true, and the intersection
    1310                 :             :  * point would have NaN coordinates.  We shouldn't return false in this case
    1311                 :             :  * because that would mean the lines are parallel.
    1312                 :             :  */
    1313                 :             : static bool
    1314                 :      695475 : line_interpt_line(Point *result, LINE *l1, LINE *l2)
    1315                 :             : {
    1316                 :      695475 :         float8          x,
    1317                 :             :                                 y;
    1318                 :             : 
    1319         [ +  + ]:      695475 :         if (!FPzero(l1->B))
    1320                 :             :         {
    1321         [ +  + ]:      506075 :                 if (FPeq(l2->A, float8_mul(l1->A, float8_div(l2->B, l1->B))))
    1322                 :        1598 :                         return false;
    1323                 :             : 
    1324                 :     1513431 :                 x = float8_div(float8_mi(float8_mul(l1->B, l2->C),
    1325                 :      504477 :                                                                  float8_mul(l2->B, l1->C)),
    1326                 :     1008954 :                                            float8_mi(float8_mul(l1->A, l2->B),
    1327                 :      504477 :                                                                  float8_mul(l2->A, l1->B)));
    1328                 :      504477 :                 y = float8_div(-float8_pl(float8_mul(l1->A, x), l1->C), l1->B);
    1329                 :      504477 :         }
    1330         [ +  + ]:      189400 :         else if (!FPzero(l2->B))
    1331                 :             :         {
    1332         [ -  + ]:      188907 :                 if (FPeq(l1->A, float8_mul(l2->A, float8_div(l1->B, l2->B))))
    1333                 :           0 :                         return false;
    1334                 :             : 
    1335                 :      566721 :                 x = float8_div(float8_mi(float8_mul(l2->B, l1->C),
    1336                 :      188907 :                                                                  float8_mul(l1->B, l2->C)),
    1337                 :      377814 :                                            float8_mi(float8_mul(l2->A, l1->B),
    1338                 :      188907 :                                                                  float8_mul(l1->A, l2->B)));
    1339                 :      188907 :                 y = float8_div(-float8_pl(float8_mul(l2->A, x), l2->C), l2->B);
    1340                 :      188907 :         }
    1341                 :             :         else
    1342                 :         493 :                 return false;
    1343                 :             : 
    1344                 :             :         /* On some platforms, the preceding expressions tend to produce -0. */
    1345         [ +  + ]:      693384 :         if (x == 0.0)
    1346                 :       20285 :                 x = 0.0;
    1347         [ +  + ]:      693384 :         if (y == 0.0)
    1348                 :       19988 :                 y = 0.0;
    1349                 :             : 
    1350         [ +  + ]:      693384 :         if (result != NULL)
    1351                 :      693132 :                 point_construct(result, x, y);
    1352                 :             : 
    1353                 :      693384 :         return true;
    1354                 :      695475 : }
    1355                 :             : 
    1356                 :             : 
    1357                 :             : /***********************************************************************
    1358                 :             :  **
    1359                 :             :  **             Routines for 2D paths (sequences of line segments, also
    1360                 :             :  **                             called `polylines').
    1361                 :             :  **
    1362                 :             :  **                             This is not a general package for geometric paths,
    1363                 :             :  **                             which of course include polygons; the emphasis here
    1364                 :             :  **                             is on (for example) usefulness in wire layout.
    1365                 :             :  **
    1366                 :             :  ***********************************************************************/
    1367                 :             : 
    1368                 :             : /*----------------------------------------------------------
    1369                 :             :  *      String to path / path to string conversion.
    1370                 :             :  *              External format:
    1371                 :             :  *                              "((xcoord, ycoord),... )"
    1372                 :             :  *                              "[(xcoord, ycoord),... ]"
    1373                 :             :  *                              "(xcoord, ycoord),... "
    1374                 :             :  *                              "[xcoord, ycoord,... ]"
    1375                 :             :  *              Also support older format:
    1376                 :             :  *                              "(closed, npts, xcoord, ycoord,... )"
    1377                 :             :  *---------------------------------------------------------*/
    1378                 :             : 
    1379                 :             : Datum
    1380                 :           9 : path_area(PG_FUNCTION_ARGS)
    1381                 :             : {
    1382                 :           9 :         PATH       *path = PG_GETARG_PATH_P(0);
    1383                 :           9 :         float8          area = 0.0;
    1384                 :           9 :         int                     i,
    1385                 :             :                                 j;
    1386                 :             : 
    1387         [ +  + ]:           9 :         if (!path->closed)
    1388                 :           4 :                 PG_RETURN_NULL();
    1389                 :             : 
    1390         [ +  + ]:          14 :         for (i = 0; i < path->npts; i++)
    1391                 :             :         {
    1392                 :           9 :                 j = (i + 1) % path->npts;
    1393                 :           9 :                 area = float8_pl(area, float8_mul(path->p[i].x, path->p[j].y));
    1394                 :           9 :                 area = float8_mi(area, float8_mul(path->p[i].y, path->p[j].x));
    1395                 :           9 :         }
    1396                 :             : 
    1397                 :           5 :         PG_RETURN_FLOAT8(float8_div(fabs(area), 2.0));
    1398                 :           9 : }
    1399                 :             : 
    1400                 :             : 
    1401                 :             : Datum
    1402                 :        5151 : path_in(PG_FUNCTION_ARGS)
    1403                 :             : {
    1404                 :        5151 :         char       *str = PG_GETARG_CSTRING(0);
    1405                 :        5151 :         Node       *escontext = fcinfo->context;
    1406                 :        5151 :         PATH       *path;
    1407                 :        5151 :         bool            isopen;
    1408                 :        5151 :         char       *s;
    1409                 :        5151 :         int                     npts;
    1410                 :        5151 :         int                     size;
    1411                 :        5151 :         int                     base_size;
    1412                 :        5151 :         int                     depth = 0;
    1413                 :             : 
    1414         [ +  + ]:        5151 :         if ((npts = pair_count(str, ',')) <= 0)
    1415         [ -  + ]:           2 :                 ereturn(escontext, (Datum) 0,
    1416                 :             :                                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
    1417                 :             :                                  errmsg("invalid input syntax for type %s: \"%s\"",
    1418                 :             :                                                 "path", str)));
    1419                 :             : 
    1420                 :        5149 :         s = str;
    1421         [ +  + ]:        5151 :         while (isspace((unsigned char) *s))
    1422                 :           2 :                 s++;
    1423                 :             : 
    1424                 :             :         /* skip single leading paren */
    1425   [ +  +  +  + ]:        5149 :         if ((*s == LDELIM) && (strrchr(s, LDELIM) == s))
    1426                 :             :         {
    1427                 :           4 :                 s++;
    1428                 :           4 :                 depth++;
    1429                 :           4 :         }
    1430                 :             : 
    1431                 :        5149 :         base_size = sizeof(path->p[0]) * npts;
    1432                 :        5149 :         size = offsetof(PATH, p) + base_size;
    1433                 :             : 
    1434                 :             :         /* Check for integer overflow */
    1435   [ +  -  -  + ]:        5149 :         if (base_size / npts != sizeof(path->p[0]) || size <= base_size)
    1436         [ #  # ]:           0 :                 ereturn(escontext, (Datum) 0,
    1437                 :             :                                 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
    1438                 :             :                                  errmsg("too many points requested")));
    1439                 :             : 
    1440                 :        5149 :         path = (PATH *) palloc(size);
    1441                 :             : 
    1442                 :        5149 :         SET_VARSIZE(path, size);
    1443                 :        5149 :         path->npts = npts;
    1444                 :             : 
    1445   [ +  +  +  + ]:       10298 :         if (!path_decode(s, true, npts, &(path->p[0]), &isopen, &s, "path", str,
    1446                 :        5149 :                                          escontext))
    1447                 :           2 :                 PG_RETURN_NULL();
    1448                 :             : 
    1449         [ +  + ]:        5147 :         if (depth >= 1)
    1450                 :             :         {
    1451         [ +  + ]:           5 :                 if (*s++ != RDELIM)
    1452         [ +  + ]:           2 :                         ereturn(escontext, (Datum) 0,
    1453                 :             :                                         (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
    1454                 :             :                                          errmsg("invalid input syntax for type %s: \"%s\"",
    1455                 :             :                                                         "path", str)));
    1456         [ +  + ]:           4 :                 while (isspace((unsigned char) *s))
    1457                 :           1 :                         s++;
    1458                 :           3 :         }
    1459         [ +  + ]:        5145 :         if (*s != '\0')
    1460         [ +  + ]:           2 :                 ereturn(escontext, (Datum) 0,
    1461                 :             :                                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
    1462                 :             :                                  errmsg("invalid input syntax for type %s: \"%s\"",
    1463                 :             :                                                 "path", str)));
    1464                 :             : 
    1465                 :        5143 :         path->closed = (!isopen);
    1466                 :             :         /* prevent instability in unused pad bytes */
    1467                 :        5143 :         path->dummy = 0;
    1468                 :             : 
    1469                 :        5143 :         PG_RETURN_PATH_P(path);
    1470                 :        5147 : }
    1471                 :             : 
    1472                 :             : 
    1473                 :             : Datum
    1474                 :        1844 : path_out(PG_FUNCTION_ARGS)
    1475                 :             : {
    1476                 :        1844 :         PATH       *path = PG_GETARG_PATH_P(0);
    1477                 :             : 
    1478                 :        3688 :         PG_RETURN_CSTRING(path_encode(path->closed ? PATH_CLOSED : PATH_OPEN, path->npts, path->p));
    1479                 :        1844 : }
    1480                 :             : 
    1481                 :             : /*
    1482                 :             :  *              path_recv                       - converts external binary format to path
    1483                 :             :  *
    1484                 :             :  * External representation is closed flag (a boolean byte), int32 number
    1485                 :             :  * of points, and the points.
    1486                 :             :  */
    1487                 :             : Datum
    1488                 :           0 : path_recv(PG_FUNCTION_ARGS)
    1489                 :             : {
    1490                 :           0 :         StringInfo      buf = (StringInfo) PG_GETARG_POINTER(0);
    1491                 :           0 :         PATH       *path;
    1492                 :           0 :         int                     closed;
    1493                 :           0 :         int32           npts;
    1494                 :           0 :         int32           i;
    1495                 :           0 :         int                     size;
    1496                 :             : 
    1497                 :           0 :         closed = pq_getmsgbyte(buf);
    1498                 :           0 :         npts = pq_getmsgint(buf, sizeof(int32));
    1499         [ #  # ]:           0 :         if (npts <= 0 || npts >= (int32) ((INT_MAX - offsetof(PATH, p)) / sizeof(Point)))
    1500   [ #  #  #  # ]:           0 :                 ereport(ERROR,
    1501                 :             :                                 (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
    1502                 :             :                                  errmsg("invalid number of points in external \"path\" value")));
    1503                 :             : 
    1504                 :           0 :         size = offsetof(PATH, p) + sizeof(path->p[0]) * npts;
    1505                 :           0 :         path = (PATH *) palloc(size);
    1506                 :             : 
    1507                 :           0 :         SET_VARSIZE(path, size);
    1508                 :           0 :         path->npts = npts;
    1509                 :           0 :         path->closed = (closed ? 1 : 0);
    1510                 :             :         /* prevent instability in unused pad bytes */
    1511                 :           0 :         path->dummy = 0;
    1512                 :             : 
    1513         [ #  # ]:           0 :         for (i = 0; i < npts; i++)
    1514                 :             :         {
    1515                 :           0 :                 path->p[i].x = pq_getmsgfloat8(buf);
    1516                 :           0 :                 path->p[i].y = pq_getmsgfloat8(buf);
    1517                 :           0 :         }
    1518                 :             : 
    1519                 :           0 :         PG_RETURN_PATH_P(path);
    1520                 :           0 : }
    1521                 :             : 
    1522                 :             : /*
    1523                 :             :  *              path_send                       - converts path to binary format
    1524                 :             :  */
    1525                 :             : Datum
    1526                 :           0 : path_send(PG_FUNCTION_ARGS)
    1527                 :             : {
    1528                 :           0 :         PATH       *path = PG_GETARG_PATH_P(0);
    1529                 :           0 :         StringInfoData buf;
    1530                 :           0 :         int32           i;
    1531                 :             : 
    1532                 :           0 :         pq_begintypsend(&buf);
    1533                 :           0 :         pq_sendbyte(&buf, path->closed ? 1 : 0);
    1534                 :           0 :         pq_sendint32(&buf, path->npts);
    1535         [ #  # ]:           0 :         for (i = 0; i < path->npts; i++)
    1536                 :             :         {
    1537                 :           0 :                 pq_sendfloat8(&buf, path->p[i].x);
    1538                 :           0 :                 pq_sendfloat8(&buf, path->p[i].y);
    1539                 :           0 :         }
    1540                 :           0 :         PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
    1541                 :           0 : }
    1542                 :             : 
    1543                 :             : 
    1544                 :             : /*----------------------------------------------------------
    1545                 :             :  *      Relational operators.
    1546                 :             :  *              These are based on the path cardinality,
    1547                 :             :  *              as stupid as that sounds.
    1548                 :             :  *
    1549                 :             :  *              Better relops and access methods coming soon.
    1550                 :             :  *---------------------------------------------------------*/
    1551                 :             : 
    1552                 :             : Datum
    1553                 :          81 : path_n_lt(PG_FUNCTION_ARGS)
    1554                 :             : {
    1555                 :          81 :         PATH       *p1 = PG_GETARG_PATH_P(0);
    1556                 :          81 :         PATH       *p2 = PG_GETARG_PATH_P(1);
    1557                 :             : 
    1558                 :         162 :         PG_RETURN_BOOL(p1->npts < p2->npts);
    1559                 :          81 : }
    1560                 :             : 
    1561                 :             : Datum
    1562                 :          81 : path_n_gt(PG_FUNCTION_ARGS)
    1563                 :             : {
    1564                 :          81 :         PATH       *p1 = PG_GETARG_PATH_P(0);
    1565                 :          81 :         PATH       *p2 = PG_GETARG_PATH_P(1);
    1566                 :             : 
    1567                 :         162 :         PG_RETURN_BOOL(p1->npts > p2->npts);
    1568                 :          81 : }
    1569                 :             : 
    1570                 :             : Datum
    1571                 :          81 : path_n_eq(PG_FUNCTION_ARGS)
    1572                 :             : {
    1573                 :          81 :         PATH       *p1 = PG_GETARG_PATH_P(0);
    1574                 :          81 :         PATH       *p2 = PG_GETARG_PATH_P(1);
    1575                 :             : 
    1576                 :         162 :         PG_RETURN_BOOL(p1->npts == p2->npts);
    1577                 :          81 : }
    1578                 :             : 
    1579                 :             : Datum
    1580                 :          81 : path_n_le(PG_FUNCTION_ARGS)
    1581                 :             : {
    1582                 :          81 :         PATH       *p1 = PG_GETARG_PATH_P(0);
    1583                 :          81 :         PATH       *p2 = PG_GETARG_PATH_P(1);
    1584                 :             : 
    1585                 :         162 :         PG_RETURN_BOOL(p1->npts <= p2->npts);
    1586                 :          81 : }
    1587                 :             : 
    1588                 :             : Datum
    1589                 :          81 : path_n_ge(PG_FUNCTION_ARGS)
    1590                 :             : {
    1591                 :          81 :         PATH       *p1 = PG_GETARG_PATH_P(0);
    1592                 :          81 :         PATH       *p2 = PG_GETARG_PATH_P(1);
    1593                 :             : 
    1594                 :         162 :         PG_RETURN_BOOL(p1->npts >= p2->npts);
    1595                 :          81 : }
    1596                 :             : 
    1597                 :             : /*----------------------------------------------------------
    1598                 :             :  * Conversion operators.
    1599                 :             :  *---------------------------------------------------------*/
    1600                 :             : 
    1601                 :             : Datum
    1602                 :          27 : path_isclosed(PG_FUNCTION_ARGS)
    1603                 :             : {
    1604                 :          27 :         PATH       *path = PG_GETARG_PATH_P(0);
    1605                 :             : 
    1606                 :          54 :         PG_RETURN_BOOL(path->closed);
    1607                 :          27 : }
    1608                 :             : 
    1609                 :             : Datum
    1610                 :          19 : path_isopen(PG_FUNCTION_ARGS)
    1611                 :             : {
    1612                 :          19 :         PATH       *path = PG_GETARG_PATH_P(0);
    1613                 :             : 
    1614                 :          38 :         PG_RETURN_BOOL(!path->closed);
    1615                 :          19 : }
    1616                 :             : 
    1617                 :             : Datum
    1618                 :         905 : path_npoints(PG_FUNCTION_ARGS)
    1619                 :             : {
    1620                 :         905 :         PATH       *path = PG_GETARG_PATH_P(0);
    1621                 :             : 
    1622                 :        1810 :         PG_RETURN_INT32(path->npts);
    1623                 :         905 : }
    1624                 :             : 
    1625                 :             : 
    1626                 :             : Datum
    1627                 :          13 : path_close(PG_FUNCTION_ARGS)
    1628                 :             : {
    1629                 :          13 :         PATH       *path = PG_GETARG_PATH_P_COPY(0);
    1630                 :             : 
    1631                 :          13 :         path->closed = true;
    1632                 :             : 
    1633                 :          26 :         PG_RETURN_PATH_P(path);
    1634                 :          13 : }
    1635                 :             : 
    1636                 :             : Datum
    1637                 :           9 : path_open(PG_FUNCTION_ARGS)
    1638                 :             : {
    1639                 :           9 :         PATH       *path = PG_GETARG_PATH_P_COPY(0);
    1640                 :             : 
    1641                 :           9 :         path->closed = false;
    1642                 :             : 
    1643                 :          18 :         PG_RETURN_PATH_P(path);
    1644                 :           9 : }
    1645                 :             : 
    1646                 :             : 
    1647                 :             : /* path_inter -
    1648                 :             :  *              Does p1 intersect p2 at any point?
    1649                 :             :  *              Use bounding boxes for a quick (O(n)) check, then do a
    1650                 :             :  *              O(n^2) iterative edge check.
    1651                 :             :  */
    1652                 :             : Datum
    1653                 :      230153 : path_inter(PG_FUNCTION_ARGS)
    1654                 :             : {
    1655                 :      230153 :         PATH       *p1 = PG_GETARG_PATH_P(0);
    1656                 :      230153 :         PATH       *p2 = PG_GETARG_PATH_P(1);
    1657                 :      230153 :         BOX                     b1,
    1658                 :             :                                 b2;
    1659                 :      230153 :         int                     i,
    1660                 :             :                                 j;
    1661                 :      230153 :         LSEG            seg1,
    1662                 :             :                                 seg2;
    1663                 :             : 
    1664         [ +  - ]:      230153 :         Assert(p1->npts > 0 && p2->npts > 0);
    1665                 :             : 
    1666                 :      230153 :         b1.high.x = b1.low.x = p1->p[0].x;
    1667                 :      230153 :         b1.high.y = b1.low.y = p1->p[0].y;
    1668         [ +  + ]:      875350 :         for (i = 1; i < p1->npts; i++)
    1669                 :             :         {
    1670                 :      645197 :                 b1.high.x = float8_max(p1->p[i].x, b1.high.x);
    1671                 :      645197 :                 b1.high.y = float8_max(p1->p[i].y, b1.high.y);
    1672                 :      645197 :                 b1.low.x = float8_min(p1->p[i].x, b1.low.x);
    1673                 :      645197 :                 b1.low.y = float8_min(p1->p[i].y, b1.low.y);
    1674                 :      645197 :         }
    1675                 :      230153 :         b2.high.x = b2.low.x = p2->p[0].x;
    1676                 :      230153 :         b2.high.y = b2.low.y = p2->p[0].y;
    1677         [ +  + ]:      658554 :         for (i = 1; i < p2->npts; i++)
    1678                 :             :         {
    1679                 :      428401 :                 b2.high.x = float8_max(p2->p[i].x, b2.high.x);
    1680                 :      428401 :                 b2.high.y = float8_max(p2->p[i].y, b2.high.y);
    1681                 :      428401 :                 b2.low.x = float8_min(p2->p[i].x, b2.low.x);
    1682                 :      428401 :                 b2.low.y = float8_min(p2->p[i].y, b2.low.y);
    1683                 :      428401 :         }
    1684         [ +  + ]:      230153 :         if (!box_ov(&b1, &b2))
    1685                 :      224570 :                 PG_RETURN_BOOL(false);
    1686                 :             : 
    1687                 :             :         /* pairwise check lseg intersections */
    1688         [ +  + ]:       27502 :         for (i = 0; i < p1->npts; i++)
    1689                 :             :         {
    1690                 :       23148 :                 int                     iprev;
    1691                 :             : 
    1692         [ +  + ]:       23148 :                 if (i > 0)
    1693                 :       17565 :                         iprev = i - 1;
    1694                 :             :                 else
    1695                 :             :                 {
    1696         [ +  + ]:        5583 :                         if (!p1->closed)
    1697                 :        1331 :                                 continue;
    1698                 :        4252 :                         iprev = p1->npts - 1;        /* include the closure segment */
    1699                 :             :                 }
    1700                 :             : 
    1701         [ +  + ]:       72611 :                 for (j = 0; j < p2->npts; j++)
    1702                 :             :                 {
    1703                 :       52023 :                         int                     jprev;
    1704                 :             : 
    1705         [ +  + ]:       52023 :                         if (j > 0)
    1706                 :       30206 :                                 jprev = j - 1;
    1707                 :             :                         else
    1708                 :             :                         {
    1709         [ +  + ]:       21817 :                                 if (!p2->closed)
    1710                 :       20623 :                                         continue;
    1711                 :        1194 :                                 jprev = p2->npts - 1;        /* include the closure segment */
    1712                 :             :                         }
    1713                 :             : 
    1714                 :       31400 :                         statlseg_construct(&seg1, &p1->p[iprev], &p1->p[i]);
    1715                 :       31400 :                         statlseg_construct(&seg2, &p2->p[jprev], &p2->p[j]);
    1716         [ +  + ]:       31400 :                         if (lseg_interpt_lseg(NULL, &seg1, &seg2))
    1717                 :        1229 :                                 PG_RETURN_BOOL(true);
    1718      [ +  +  + ]:       52023 :                 }
    1719      [ +  +  + ]:       23148 :         }
    1720                 :             : 
    1721                 :             :         /* if we dropped through, no two segs intersected */
    1722                 :        4354 :         PG_RETURN_BOOL(false);
    1723                 :      230153 : }
    1724                 :             : 
    1725                 :             : /* path_distance()
    1726                 :             :  * This essentially does a cartesian product of the lsegs in the
    1727                 :             :  *      two paths, and finds the min distance between any two lsegs
    1728                 :             :  */
    1729                 :             : Datum
    1730                 :          81 : path_distance(PG_FUNCTION_ARGS)
    1731                 :             : {
    1732                 :          81 :         PATH       *p1 = PG_GETARG_PATH_P(0);
    1733                 :          81 :         PATH       *p2 = PG_GETARG_PATH_P(1);
    1734                 :          81 :         float8          min = 0.0;              /* initialize to keep compiler quiet */
    1735                 :          81 :         bool            have_min = false;
    1736                 :          81 :         float8          tmp;
    1737                 :          81 :         int                     i,
    1738                 :             :                                 j;
    1739                 :          81 :         LSEG            seg1,
    1740                 :             :                                 seg2;
    1741                 :             : 
    1742         [ +  + ]:         252 :         for (i = 0; i < p1->npts; i++)
    1743                 :             :         {
    1744                 :         171 :                 int                     iprev;
    1745                 :             : 
    1746         [ +  + ]:         171 :                 if (i > 0)
    1747                 :          90 :                         iprev = i - 1;
    1748                 :             :                 else
    1749                 :             :                 {
    1750         [ +  + ]:          81 :                         if (!p1->closed)
    1751                 :          36 :                                 continue;
    1752                 :          45 :                         iprev = p1->npts - 1;        /* include the closure segment */
    1753                 :             :                 }
    1754                 :             : 
    1755         [ +  + ]:         420 :                 for (j = 0; j < p2->npts; j++)
    1756                 :             :                 {
    1757                 :         285 :                         int                     jprev;
    1758                 :             : 
    1759         [ +  + ]:         285 :                         if (j > 0)
    1760                 :         150 :                                 jprev = j - 1;
    1761                 :             :                         else
    1762                 :             :                         {
    1763         [ +  + ]:         135 :                                 if (!p2->closed)
    1764                 :          60 :                                         continue;
    1765                 :          75 :                                 jprev = p2->npts - 1;        /* include the closure segment */
    1766                 :             :                         }
    1767                 :             : 
    1768                 :         225 :                         statlseg_construct(&seg1, &p1->p[iprev], &p1->p[i]);
    1769                 :         225 :                         statlseg_construct(&seg2, &p2->p[jprev], &p2->p[j]);
    1770                 :             : 
    1771                 :         225 :                         tmp = lseg_closept_lseg(NULL, &seg1, &seg2);
    1772   [ +  +  +  + ]:         225 :                         if (!have_min || float8_lt(tmp, min))
    1773                 :             :                         {
    1774                 :          97 :                                 min = tmp;
    1775                 :          97 :                                 have_min = true;
    1776                 :          97 :                         }
    1777         [ +  + ]:         285 :                 }
    1778         [ +  + ]:         171 :         }
    1779                 :             : 
    1780         [ +  - ]:          81 :         if (!have_min)
    1781                 :           0 :                 PG_RETURN_NULL();
    1782                 :             : 
    1783                 :          81 :         PG_RETURN_FLOAT8(min);
    1784                 :          81 : }
    1785                 :             : 
    1786                 :             : 
    1787                 :             : /*----------------------------------------------------------
    1788                 :             :  *      "Arithmetic" operations.
    1789                 :             :  *---------------------------------------------------------*/
    1790                 :             : 
    1791                 :             : Datum
    1792                 :           9 : path_length(PG_FUNCTION_ARGS)
    1793                 :             : {
    1794                 :           9 :         PATH       *path = PG_GETARG_PATH_P(0);
    1795                 :           9 :         float8          result = 0.0;
    1796                 :           9 :         int                     i;
    1797                 :             : 
    1798         [ +  + ]:          28 :         for (i = 0; i < path->npts; i++)
    1799                 :             :         {
    1800                 :          19 :                 int                     iprev;
    1801                 :             : 
    1802         [ +  + ]:          19 :                 if (i > 0)
    1803                 :          10 :                         iprev = i - 1;
    1804                 :             :                 else
    1805                 :             :                 {
    1806         [ +  + ]:           9 :                         if (!path->closed)
    1807                 :           4 :                                 continue;
    1808                 :           5 :                         iprev = path->npts - 1; /* include the closure segment */
    1809                 :             :                 }
    1810                 :             : 
    1811                 :          15 :                 result = float8_pl(result, point_dt(&path->p[iprev], &path->p[i]));
    1812      [ -  +  + ]:          19 :         }
    1813                 :             : 
    1814                 :          18 :         PG_RETURN_FLOAT8(result);
    1815                 :           9 : }
    1816                 :             : 
    1817                 :             : /***********************************************************************
    1818                 :             :  **
    1819                 :             :  **             Routines for 2D points.
    1820                 :             :  **
    1821                 :             :  ***********************************************************************/
    1822                 :             : 
    1823                 :             : /*----------------------------------------------------------
    1824                 :             :  *      String to point, point to string conversion.
    1825                 :             :  *              External format:
    1826                 :             :  *                              "(x,y)"
    1827                 :             :  *                              "x,y"
    1828                 :             :  *---------------------------------------------------------*/
    1829                 :             : 
    1830                 :             : Datum
    1831                 :         276 : point_in(PG_FUNCTION_ARGS)
    1832                 :             : {
    1833                 :         276 :         char       *str = PG_GETARG_CSTRING(0);
    1834                 :         276 :         Point      *point = palloc_object(Point);
    1835                 :             : 
    1836                 :             :         /* Ignore failure from pair_decode, since our return value won't matter */
    1837                 :         276 :         pair_decode(str, &point->x, &point->y, NULL, "point", str, fcinfo->context);
    1838                 :         552 :         PG_RETURN_POINT_P(point);
    1839                 :         276 : }
    1840                 :             : 
    1841                 :             : Datum
    1842                 :        3838 : point_out(PG_FUNCTION_ARGS)
    1843                 :             : {
    1844                 :        3838 :         Point      *pt = PG_GETARG_POINT_P(0);
    1845                 :             : 
    1846                 :        7676 :         PG_RETURN_CSTRING(path_encode(PATH_NONE, 1, pt));
    1847                 :        3838 : }
    1848                 :             : 
    1849                 :             : /*
    1850                 :             :  *              point_recv                      - converts external binary format to point
    1851                 :             :  */
    1852                 :             : Datum
    1853                 :           3 : point_recv(PG_FUNCTION_ARGS)
    1854                 :             : {
    1855                 :           3 :         StringInfo      buf = (StringInfo) PG_GETARG_POINTER(0);
    1856                 :           3 :         Point      *point;
    1857                 :             : 
    1858                 :           3 :         point = palloc_object(Point);
    1859                 :           3 :         point->x = pq_getmsgfloat8(buf);
    1860                 :           3 :         point->y = pq_getmsgfloat8(buf);
    1861                 :           6 :         PG_RETURN_POINT_P(point);
    1862                 :           3 : }
    1863                 :             : 
    1864                 :             : /*
    1865                 :             :  *              point_send                      - converts point to binary format
    1866                 :             :  */
    1867                 :             : Datum
    1868                 :           3 : point_send(PG_FUNCTION_ARGS)
    1869                 :             : {
    1870                 :           3 :         Point      *pt = PG_GETARG_POINT_P(0);
    1871                 :           3 :         StringInfoData buf;
    1872                 :             : 
    1873                 :           3 :         pq_begintypsend(&buf);
    1874                 :           3 :         pq_sendfloat8(&buf, pt->x);
    1875                 :           3 :         pq_sendfloat8(&buf, pt->y);
    1876                 :           6 :         PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
    1877                 :           3 : }
    1878                 :             : 
    1879                 :             : 
    1880                 :             : /*
    1881                 :             :  * Initialize a point
    1882                 :             :  */
    1883                 :             : static inline void
    1884                 :      856210 : point_construct(Point *result, float8 x, float8 y)
    1885                 :             : {
    1886                 :      856210 :         result->x = x;
    1887                 :      856210 :         result->y = y;
    1888                 :      856210 : }
    1889                 :             : 
    1890                 :             : 
    1891                 :             : /*----------------------------------------------------------
    1892                 :             :  *      Relational operators for Points.
    1893                 :             :  *              Since we do have a sense of coordinates being
    1894                 :             :  *              "equal" to a given accuracy (point_vert, point_horiz),
    1895                 :             :  *              the other ops must preserve that sense.  This means
    1896                 :             :  *              that results may, strictly speaking, be a lie (unless
    1897                 :             :  *              EPSILON = 0.0).
    1898                 :             :  *---------------------------------------------------------*/
    1899                 :             : 
    1900                 :             : Datum
    1901                 :      117243 : point_left(PG_FUNCTION_ARGS)
    1902                 :             : {
    1903                 :      117243 :         Point      *pt1 = PG_GETARG_POINT_P(0);
    1904                 :      117243 :         Point      *pt2 = PG_GETARG_POINT_P(1);
    1905                 :             : 
    1906                 :      234486 :         PG_RETURN_BOOL(FPlt(pt1->x, pt2->x));
    1907                 :      117243 : }
    1908                 :             : 
    1909                 :             : Datum
    1910                 :     2804899 : point_right(PG_FUNCTION_ARGS)
    1911                 :             : {
    1912                 :     2804899 :         Point      *pt1 = PG_GETARG_POINT_P(0);
    1913                 :     2804899 :         Point      *pt2 = PG_GETARG_POINT_P(1);
    1914                 :             : 
    1915                 :     5609798 :         PG_RETURN_BOOL(FPgt(pt1->x, pt2->x));
    1916                 :     2804899 : }
    1917                 :             : 
    1918                 :             : Datum
    1919                 :     2844645 : point_above(PG_FUNCTION_ARGS)
    1920                 :             : {
    1921                 :     2844645 :         Point      *pt1 = PG_GETARG_POINT_P(0);
    1922                 :     2844645 :         Point      *pt2 = PG_GETARG_POINT_P(1);
    1923                 :             : 
    1924                 :     5689290 :         PG_RETURN_BOOL(FPgt(pt1->y, pt2->y));
    1925                 :     2844645 : }
    1926                 :             : 
    1927                 :             : Datum
    1928                 :      207417 : point_below(PG_FUNCTION_ARGS)
    1929                 :             : {
    1930                 :      207417 :         Point      *pt1 = PG_GETARG_POINT_P(0);
    1931                 :      207417 :         Point      *pt2 = PG_GETARG_POINT_P(1);
    1932                 :             : 
    1933                 :      414834 :         PG_RETURN_BOOL(FPlt(pt1->y, pt2->y));
    1934                 :      207417 : }
    1935                 :             : 
    1936                 :             : Datum
    1937                 :       82410 : point_vert(PG_FUNCTION_ARGS)
    1938                 :             : {
    1939                 :       82410 :         Point      *pt1 = PG_GETARG_POINT_P(0);
    1940                 :       82410 :         Point      *pt2 = PG_GETARG_POINT_P(1);
    1941                 :             : 
    1942                 :      164820 :         PG_RETURN_BOOL(FPeq(pt1->x, pt2->x));
    1943                 :       82410 : }
    1944                 :             : 
    1945                 :             : Datum
    1946                 :       88346 : point_horiz(PG_FUNCTION_ARGS)
    1947                 :             : {
    1948                 :       88346 :         Point      *pt1 = PG_GETARG_POINT_P(0);
    1949                 :       88346 :         Point      *pt2 = PG_GETARG_POINT_P(1);
    1950                 :             : 
    1951                 :      176692 :         PG_RETURN_BOOL(FPeq(pt1->y, pt2->y));
    1952                 :       88346 : }
    1953                 :             : 
    1954                 :             : Datum
    1955                 :       13367 : point_eq(PG_FUNCTION_ARGS)
    1956                 :             : {
    1957                 :       13367 :         Point      *pt1 = PG_GETARG_POINT_P(0);
    1958                 :       13367 :         Point      *pt2 = PG_GETARG_POINT_P(1);
    1959                 :             : 
    1960                 :       26734 :         PG_RETURN_BOOL(point_eq_point(pt1, pt2));
    1961                 :       13367 : }
    1962                 :             : 
    1963                 :             : Datum
    1964                 :         117 : point_ne(PG_FUNCTION_ARGS)
    1965                 :             : {
    1966                 :         117 :         Point      *pt1 = PG_GETARG_POINT_P(0);
    1967                 :         117 :         Point      *pt2 = PG_GETARG_POINT_P(1);
    1968                 :             : 
    1969                 :         234 :         PG_RETURN_BOOL(!point_eq_point(pt1, pt2));
    1970                 :         117 : }
    1971                 :             : 
    1972                 :             : 
    1973                 :             : /*
    1974                 :             :  * Check whether the two points are the same
    1975                 :             :  */
    1976                 :             : static inline bool
    1977                 :       50563 : point_eq_point(Point *pt1, Point *pt2)
    1978                 :             : {
    1979                 :             :         /* If any NaNs are involved, insist on exact equality */
    1980   [ -  +  +  +  :       50563 :         if (unlikely(isnan(pt1->x) || isnan(pt1->y) ||
          +  -  -  +  +  
          +  +  -  -  +  
          +  +  +  -  +  
                      + ]
    1981                 :             :                                  isnan(pt2->x) || isnan(pt2->y)))
    1982         [ +  + ]:      303151 :                 return (float8_eq(pt1->x, pt2->x) && float8_eq(pt1->y, pt2->y));
    1983                 :             : 
    1984         [ +  + ]:       50492 :         return (FPeq(pt1->x, pt2->x) && FPeq(pt1->y, pt2->y));
    1985                 :      353643 : }
    1986                 :             : 
    1987                 :             : 
    1988                 :             : /*----------------------------------------------------------
    1989                 :             :  *      "Arithmetic" operators on points.
    1990                 :             :  *---------------------------------------------------------*/
    1991                 :             : 
    1992                 :             : Datum
    1993                 :      123275 : point_distance(PG_FUNCTION_ARGS)
    1994                 :             : {
    1995                 :      123275 :         Point      *pt1 = PG_GETARG_POINT_P(0);
    1996                 :      123275 :         Point      *pt2 = PG_GETARG_POINT_P(1);
    1997                 :             : 
    1998                 :      246550 :         PG_RETURN_FLOAT8(point_dt(pt1, pt2));
    1999                 :      123275 : }
    2000                 :             : 
    2001                 :             : static inline float8
    2002                 :     2591120 : point_dt(Point *pt1, Point *pt2)
    2003                 :             : {
    2004                 :     2591120 :         return hypot(float8_mi(pt1->x, pt2->x), float8_mi(pt1->y, pt2->y));
    2005                 :             : }
    2006                 :             : 
    2007                 :             : Datum
    2008                 :         100 : point_slope(PG_FUNCTION_ARGS)
    2009                 :             : {
    2010                 :         100 :         Point      *pt1 = PG_GETARG_POINT_P(0);
    2011                 :         100 :         Point      *pt2 = PG_GETARG_POINT_P(1);
    2012                 :             : 
    2013                 :         200 :         PG_RETURN_FLOAT8(point_sl(pt1, pt2));
    2014                 :         100 : }
    2015                 :             : 
    2016                 :             : 
    2017                 :             : /*
    2018                 :             :  * Return slope of two points
    2019                 :             :  *
    2020                 :             :  * Note that this function returns Inf when the points are the same.
    2021                 :             :  */
    2022                 :             : static inline float8
    2023                 :      641534 : point_sl(Point *pt1, Point *pt2)
    2024                 :             : {
    2025         [ +  + ]:      641534 :         if (FPeq(pt1->x, pt2->x))
    2026                 :       71417 :                 return get_float8_infinity();
    2027         [ +  + ]:      570117 :         if (FPeq(pt1->y, pt2->y))
    2028                 :       70749 :                 return 0.0;
    2029                 :      499368 :         return float8_div(float8_mi(pt1->y, pt2->y), float8_mi(pt1->x, pt2->x));
    2030                 :      641534 : }
    2031                 :             : 
    2032                 :             : 
    2033                 :             : /*
    2034                 :             :  * Return inverse slope of two points
    2035                 :             :  *
    2036                 :             :  * Note that this function returns 0.0 when the points are the same.
    2037                 :             :  */
    2038                 :             : static inline float8
    2039                 :      151080 : point_invsl(Point *pt1, Point *pt2)
    2040                 :             : {
    2041         [ +  + ]:      151080 :         if (FPeq(pt1->x, pt2->x))
    2042                 :       59768 :                 return 0.0;
    2043         [ +  + ]:       91312 :         if (FPeq(pt1->y, pt2->y))
    2044                 :       58534 :                 return get_float8_infinity();
    2045                 :       32778 :         return float8_div(float8_mi(pt1->x, pt2->x), float8_mi(pt2->y, pt1->y));
    2046                 :      151080 : }
    2047                 :             : 
    2048                 :             : 
    2049                 :             : /***********************************************************************
    2050                 :             :  **
    2051                 :             :  **             Routines for 2D line segments.
    2052                 :             :  **
    2053                 :             :  ***********************************************************************/
    2054                 :             : 
    2055                 :             : /*----------------------------------------------------------
    2056                 :             :  *      String to lseg, lseg to string conversion.
    2057                 :             :  *              External forms: "[(x1, y1), (x2, y2)]"
    2058                 :             :  *                                              "(x1, y1), (x2, y2)"
    2059                 :             :  *                                              "x1, y1, x2, y2"
    2060                 :             :  *              closed form ok  "((x1, y1), (x2, y2))"
    2061                 :             :  *              (old form)              "(x1, y1, x2, y2)"
    2062                 :             :  *---------------------------------------------------------*/
    2063                 :             : 
    2064                 :             : Datum
    2065                 :          12 : lseg_in(PG_FUNCTION_ARGS)
    2066                 :             : {
    2067                 :          12 :         char       *str = PG_GETARG_CSTRING(0);
    2068                 :          12 :         Node       *escontext = fcinfo->context;
    2069                 :          12 :         LSEG       *lseg = palloc_object(LSEG);
    2070                 :          12 :         bool            isopen;
    2071                 :             : 
    2072   [ +  +  +  + ]:          24 :         if (!path_decode(str, true, 2, &lseg->p[0], &isopen, NULL, "lseg", str,
    2073                 :          12 :                                          escontext))
    2074                 :           2 :                 PG_RETURN_NULL();
    2075                 :             : 
    2076                 :          10 :         PG_RETURN_LSEG_P(lseg);
    2077                 :          12 : }
    2078                 :             : 
    2079                 :             : 
    2080                 :             : Datum
    2081                 :        1208 : lseg_out(PG_FUNCTION_ARGS)
    2082                 :             : {
    2083                 :        1208 :         LSEG       *ls = PG_GETARG_LSEG_P(0);
    2084                 :             : 
    2085                 :        2416 :         PG_RETURN_CSTRING(path_encode(PATH_OPEN, 2, &ls->p[0]));
    2086                 :        1208 : }
    2087                 :             : 
    2088                 :             : /*
    2089                 :             :  *              lseg_recv                       - converts external binary format to lseg
    2090                 :             :  */
    2091                 :             : Datum
    2092                 :           0 : lseg_recv(PG_FUNCTION_ARGS)
    2093                 :             : {
    2094                 :           0 :         StringInfo      buf = (StringInfo) PG_GETARG_POINTER(0);
    2095                 :           0 :         LSEG       *lseg;
    2096                 :             : 
    2097                 :           0 :         lseg = palloc_object(LSEG);
    2098                 :             : 
    2099                 :           0 :         lseg->p[0].x = pq_getmsgfloat8(buf);
    2100                 :           0 :         lseg->p[0].y = pq_getmsgfloat8(buf);
    2101                 :           0 :         lseg->p[1].x = pq_getmsgfloat8(buf);
    2102                 :           0 :         lseg->p[1].y = pq_getmsgfloat8(buf);
    2103                 :             : 
    2104                 :           0 :         PG_RETURN_LSEG_P(lseg);
    2105                 :           0 : }
    2106                 :             : 
    2107                 :             : /*
    2108                 :             :  *              lseg_send                       - converts lseg to binary format
    2109                 :             :  */
    2110                 :             : Datum
    2111                 :           0 : lseg_send(PG_FUNCTION_ARGS)
    2112                 :             : {
    2113                 :           0 :         LSEG       *ls = PG_GETARG_LSEG_P(0);
    2114                 :           0 :         StringInfoData buf;
    2115                 :             : 
    2116                 :           0 :         pq_begintypsend(&buf);
    2117                 :           0 :         pq_sendfloat8(&buf, ls->p[0].x);
    2118                 :           0 :         pq_sendfloat8(&buf, ls->p[0].y);
    2119                 :           0 :         pq_sendfloat8(&buf, ls->p[1].x);
    2120                 :           0 :         pq_sendfloat8(&buf, ls->p[1].y);
    2121                 :           0 :         PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
    2122                 :           0 : }
    2123                 :             : 
    2124                 :             : 
    2125                 :             : /* lseg_construct -
    2126                 :             :  *              form a LSEG from two Points.
    2127                 :             :  */
    2128                 :             : Datum
    2129                 :           1 : lseg_construct(PG_FUNCTION_ARGS)
    2130                 :             : {
    2131                 :           1 :         Point      *pt1 = PG_GETARG_POINT_P(0);
    2132                 :           1 :         Point      *pt2 = PG_GETARG_POINT_P(1);
    2133                 :           1 :         LSEG       *result = palloc_object(LSEG);
    2134                 :             : 
    2135                 :           1 :         statlseg_construct(result, pt1, pt2);
    2136                 :             : 
    2137                 :           2 :         PG_RETURN_LSEG_P(result);
    2138                 :           1 : }
    2139                 :             : 
    2140                 :             : /* like lseg_construct, but assume space already allocated */
    2141                 :             : static inline void
    2142                 :      168487 : statlseg_construct(LSEG *lseg, Point *pt1, Point *pt2)
    2143                 :             : {
    2144                 :      168487 :         lseg->p[0].x = pt1->x;
    2145                 :      168487 :         lseg->p[0].y = pt1->y;
    2146                 :      168487 :         lseg->p[1].x = pt2->x;
    2147                 :      168487 :         lseg->p[1].y = pt2->y;
    2148                 :      168487 : }
    2149                 :             : 
    2150                 :             : 
    2151                 :             : /*
    2152                 :             :  * Return slope of the line segment
    2153                 :             :  */
    2154                 :             : static inline float8
    2155                 :      641345 : lseg_sl(LSEG *lseg)
    2156                 :             : {
    2157                 :      641345 :         return point_sl(&lseg->p[0], &lseg->p[1]);
    2158                 :             : }
    2159                 :             : 
    2160                 :             : 
    2161                 :             : /*
    2162                 :             :  * Return inverse slope of the line segment
    2163                 :             :  */
    2164                 :             : static inline float8
    2165                 :          64 : lseg_invsl(LSEG *lseg)
    2166                 :             : {
    2167                 :          64 :         return point_invsl(&lseg->p[0], &lseg->p[1]);
    2168                 :             : }
    2169                 :             : 
    2170                 :             : 
    2171                 :             : Datum
    2172                 :           8 : lseg_length(PG_FUNCTION_ARGS)
    2173                 :             : {
    2174                 :           8 :         LSEG       *lseg = PG_GETARG_LSEG_P(0);
    2175                 :             : 
    2176                 :          16 :         PG_RETURN_FLOAT8(point_dt(&lseg->p[0], &lseg->p[1]));
    2177                 :           8 : }
    2178                 :             : 
    2179                 :             : /*----------------------------------------------------------
    2180                 :             :  *      Relative position routines.
    2181                 :             :  *---------------------------------------------------------*/
    2182                 :             : 
    2183                 :             : /*
    2184                 :             :  **  find intersection of the two lines, and see if it falls on
    2185                 :             :  **  both segments.
    2186                 :             :  */
    2187                 :             : Datum
    2188                 :        4228 : lseg_intersect(PG_FUNCTION_ARGS)
    2189                 :             : {
    2190                 :        4228 :         LSEG       *l1 = PG_GETARG_LSEG_P(0);
    2191                 :        4228 :         LSEG       *l2 = PG_GETARG_LSEG_P(1);
    2192                 :             : 
    2193                 :        8456 :         PG_RETURN_BOOL(lseg_interpt_lseg(NULL, l1, l2));
    2194                 :        4228 : }
    2195                 :             : 
    2196                 :             : 
    2197                 :             : Datum
    2198                 :          64 : lseg_parallel(PG_FUNCTION_ARGS)
    2199                 :             : {
    2200                 :          64 :         LSEG       *l1 = PG_GETARG_LSEG_P(0);
    2201                 :          64 :         LSEG       *l2 = PG_GETARG_LSEG_P(1);
    2202                 :             : 
    2203                 :         128 :         PG_RETURN_BOOL(FPeq(lseg_sl(l1), lseg_sl(l2)));
    2204                 :          64 : }
    2205                 :             : 
    2206                 :             : /*
    2207                 :             :  * Determine if two line segments are perpendicular.
    2208                 :             :  */
    2209                 :             : Datum
    2210                 :          64 : lseg_perp(PG_FUNCTION_ARGS)
    2211                 :             : {
    2212                 :          64 :         LSEG       *l1 = PG_GETARG_LSEG_P(0);
    2213                 :          64 :         LSEG       *l2 = PG_GETARG_LSEG_P(1);
    2214                 :             : 
    2215                 :         128 :         PG_RETURN_BOOL(FPeq(lseg_sl(l1), lseg_invsl(l2)));
    2216                 :          64 : }
    2217                 :             : 
    2218                 :             : Datum
    2219                 :           8 : lseg_vertical(PG_FUNCTION_ARGS)
    2220                 :             : {
    2221                 :           8 :         LSEG       *lseg = PG_GETARG_LSEG_P(0);
    2222                 :             : 
    2223                 :          16 :         PG_RETURN_BOOL(FPeq(lseg->p[0].x, lseg->p[1].x));
    2224                 :           8 : }
    2225                 :             : 
    2226                 :             : Datum
    2227                 :           8 : lseg_horizontal(PG_FUNCTION_ARGS)
    2228                 :             : {
    2229                 :           8 :         LSEG       *lseg = PG_GETARG_LSEG_P(0);
    2230                 :             : 
    2231                 :          16 :         PG_RETURN_BOOL(FPeq(lseg->p[0].y, lseg->p[1].y));
    2232                 :           8 : }
    2233                 :             : 
    2234                 :             : 
    2235                 :             : Datum
    2236                 :          64 : lseg_eq(PG_FUNCTION_ARGS)
    2237                 :             : {
    2238                 :          64 :         LSEG       *l1 = PG_GETARG_LSEG_P(0);
    2239                 :          64 :         LSEG       *l2 = PG_GETARG_LSEG_P(1);
    2240                 :             : 
    2241         [ +  + ]:          64 :         PG_RETURN_BOOL(point_eq_point(&l1->p[0], &l2->p[0]) &&
    2242                 :             :                                    point_eq_point(&l1->p[1], &l2->p[1]));
    2243                 :          64 : }
    2244                 :             : 
    2245                 :             : Datum
    2246                 :          64 : lseg_ne(PG_FUNCTION_ARGS)
    2247                 :             : {
    2248                 :          64 :         LSEG       *l1 = PG_GETARG_LSEG_P(0);
    2249                 :          64 :         LSEG       *l2 = PG_GETARG_LSEG_P(1);
    2250                 :             : 
    2251         [ +  + ]:          64 :         PG_RETURN_BOOL(!point_eq_point(&l1->p[0], &l2->p[0]) ||
    2252                 :             :                                    !point_eq_point(&l1->p[1], &l2->p[1]));
    2253                 :          64 : }
    2254                 :             : 
    2255                 :             : Datum
    2256                 :          64 : lseg_lt(PG_FUNCTION_ARGS)
    2257                 :             : {
    2258                 :          64 :         LSEG       *l1 = PG_GETARG_LSEG_P(0);
    2259                 :          64 :         LSEG       *l2 = PG_GETARG_LSEG_P(1);
    2260                 :             : 
    2261                 :         128 :         PG_RETURN_BOOL(FPlt(point_dt(&l1->p[0], &l1->p[1]),
    2262                 :             :                                                 point_dt(&l2->p[0], &l2->p[1])));
    2263                 :          64 : }
    2264                 :             : 
    2265                 :             : Datum
    2266                 :          64 : lseg_le(PG_FUNCTION_ARGS)
    2267                 :             : {
    2268                 :          64 :         LSEG       *l1 = PG_GETARG_LSEG_P(0);
    2269                 :          64 :         LSEG       *l2 = PG_GETARG_LSEG_P(1);
    2270                 :             : 
    2271                 :         128 :         PG_RETURN_BOOL(FPle(point_dt(&l1->p[0], &l1->p[1]),
    2272                 :             :                                                 point_dt(&l2->p[0], &l2->p[1])));
    2273                 :          64 : }
    2274                 :             : 
    2275                 :             : Datum
    2276                 :          64 : lseg_gt(PG_FUNCTION_ARGS)
    2277                 :             : {
    2278                 :          64 :         LSEG       *l1 = PG_GETARG_LSEG_P(0);
    2279                 :          64 :         LSEG       *l2 = PG_GETARG_LSEG_P(1);
    2280                 :             : 
    2281                 :         128 :         PG_RETURN_BOOL(FPgt(point_dt(&l1->p[0], &l1->p[1]),
    2282                 :             :                                                 point_dt(&l2->p[0], &l2->p[1])));
    2283                 :          64 : }
    2284                 :             : 
    2285                 :             : Datum
    2286                 :          64 : lseg_ge(PG_FUNCTION_ARGS)
    2287                 :             : {
    2288                 :          64 :         LSEG       *l1 = PG_GETARG_LSEG_P(0);
    2289                 :          64 :         LSEG       *l2 = PG_GETARG_LSEG_P(1);
    2290                 :             : 
    2291                 :         128 :         PG_RETURN_BOOL(FPge(point_dt(&l1->p[0], &l1->p[1]),
    2292                 :             :                                                 point_dt(&l2->p[0], &l2->p[1])));
    2293                 :          64 : }
    2294                 :             : 
    2295                 :             : 
    2296                 :             : /*----------------------------------------------------------
    2297                 :             :  *      Line arithmetic routines.
    2298                 :             :  *---------------------------------------------------------*/
    2299                 :             : 
    2300                 :             : /* lseg_distance -
    2301                 :             :  *              If two segments don't intersect, then the closest
    2302                 :             :  *              point will be from one of the endpoints to the other
    2303                 :             :  *              segment.
    2304                 :             :  */
    2305                 :             : Datum
    2306                 :          64 : lseg_distance(PG_FUNCTION_ARGS)
    2307                 :             : {
    2308                 :          64 :         LSEG       *l1 = PG_GETARG_LSEG_P(0);
    2309                 :          64 :         LSEG       *l2 = PG_GETARG_LSEG_P(1);
    2310                 :             : 
    2311                 :         128 :         PG_RETURN_FLOAT8(lseg_closept_lseg(NULL, l1, l2));
    2312                 :          64 : }
    2313                 :             : 
    2314                 :             : 
    2315                 :             : Datum
    2316                 :          16 : lseg_center(PG_FUNCTION_ARGS)
    2317                 :             : {
    2318                 :          16 :         LSEG       *lseg = PG_GETARG_LSEG_P(0);
    2319                 :          16 :         Point      *result;
    2320                 :             : 
    2321                 :          16 :         result = palloc_object(Point);
    2322                 :             : 
    2323                 :          16 :         result->x = float8_div(float8_pl(lseg->p[0].x, lseg->p[1].x), 2.0);
    2324                 :          16 :         result->y = float8_div(float8_pl(lseg->p[0].y, lseg->p[1].y), 2.0);
    2325                 :             : 
    2326                 :          32 :         PG_RETURN_POINT_P(result);
    2327                 :          16 : }
    2328                 :             : 
    2329                 :             : 
    2330                 :             : /*
    2331                 :             :  * Return whether the two segments intersect. If *result is not NULL,
    2332                 :             :  * it is set to the intersection point.
    2333                 :             :  *
    2334                 :             :  * This function is almost perfectly symmetric, even though it doesn't look
    2335                 :             :  * like it.  See lseg_interpt_line() for the other half of it.
    2336                 :             :  */
    2337                 :             : static bool
    2338                 :      244725 : lseg_interpt_lseg(Point *result, LSEG *l1, LSEG *l2)
    2339                 :             : {
    2340                 :      244725 :         Point           interpt;
    2341                 :      244725 :         LINE            tmp;
    2342                 :             : 
    2343                 :      244725 :         line_construct(&tmp, &l2->p[0], lseg_sl(l2));
    2344         [ +  + ]:      244725 :         if (!lseg_interpt_line(&interpt, l1, &tmp))
    2345                 :      230976 :                 return false;
    2346                 :             : 
    2347                 :             :         /*
    2348                 :             :          * If the line intersection point isn't within l2, there is no valid
    2349                 :             :          * segment intersection point at all.
    2350                 :             :          */
    2351         [ +  + ]:       13749 :         if (!lseg_contain_point(l2, &interpt))
    2352                 :       10297 :                 return false;
    2353                 :             : 
    2354         [ +  + ]:        3452 :         if (result != NULL)
    2355                 :        1119 :                 *result = interpt;
    2356                 :             : 
    2357                 :        3452 :         return true;
    2358                 :      244725 : }
    2359                 :             : 
    2360                 :             : Datum
    2361                 :         958 : lseg_interpt(PG_FUNCTION_ARGS)
    2362                 :             : {
    2363                 :         958 :         LSEG       *l1 = PG_GETARG_LSEG_P(0);
    2364                 :         958 :         LSEG       *l2 = PG_GETARG_LSEG_P(1);
    2365                 :         958 :         Point      *result;
    2366                 :             : 
    2367                 :         958 :         result = palloc_object(Point);
    2368                 :             : 
    2369         [ +  + ]:         958 :         if (!lseg_interpt_lseg(result, l1, l2))
    2370                 :          64 :                 PG_RETURN_NULL();
    2371                 :         894 :         PG_RETURN_POINT_P(result);
    2372                 :         958 : }
    2373                 :             : 
    2374                 :             : /***********************************************************************
    2375                 :             :  **
    2376                 :             :  **             Routines for position comparisons of differently-typed
    2377                 :             :  **                             2D objects.
    2378                 :             :  **
    2379                 :             :  ***********************************************************************/
    2380                 :             : 
    2381                 :             : /*---------------------------------------------------------------------
    2382                 :             :  *              dist_
    2383                 :             :  *                              Minimum distance from one object to another.
    2384                 :             :  *-------------------------------------------------------------------*/
    2385                 :             : 
    2386                 :             : /*
    2387                 :             :  * Distance from a point to a line
    2388                 :             :  */
    2389                 :             : Datum
    2390                 :         100 : dist_pl(PG_FUNCTION_ARGS)
    2391                 :             : {
    2392                 :         100 :         Point      *pt = PG_GETARG_POINT_P(0);
    2393                 :         100 :         LINE       *line = PG_GETARG_LINE_P(1);
    2394                 :             : 
    2395                 :         200 :         PG_RETURN_FLOAT8(line_closept_point(NULL, line, pt));
    2396                 :         100 : }
    2397                 :             : 
    2398                 :             : /*
    2399                 :             :  * Distance from a line to a point
    2400                 :             :  */
    2401                 :             : Datum
    2402                 :         100 : dist_lp(PG_FUNCTION_ARGS)
    2403                 :             : {
    2404                 :         100 :         LINE       *line = PG_GETARG_LINE_P(0);
    2405                 :         100 :         Point      *pt = PG_GETARG_POINT_P(1);
    2406                 :             : 
    2407                 :         200 :         PG_RETURN_FLOAT8(line_closept_point(NULL, line, pt));
    2408                 :         100 : }
    2409                 :             : 
    2410                 :             : /*
    2411                 :             :  * Distance from a point to a lseg
    2412                 :             :  */
    2413                 :             : Datum
    2414                 :          80 : dist_ps(PG_FUNCTION_ARGS)
    2415                 :             : {
    2416                 :          80 :         Point      *pt = PG_GETARG_POINT_P(0);
    2417                 :          80 :         LSEG       *lseg = PG_GETARG_LSEG_P(1);
    2418                 :             : 
    2419                 :         160 :         PG_RETURN_FLOAT8(lseg_closept_point(NULL, lseg, pt));
    2420                 :          80 : }
    2421                 :             : 
    2422                 :             : /*
    2423                 :             :  * Distance from a lseg to a point
    2424                 :             :  */
    2425                 :             : Datum
    2426                 :          80 : dist_sp(PG_FUNCTION_ARGS)
    2427                 :             : {
    2428                 :          80 :         LSEG       *lseg = PG_GETARG_LSEG_P(0);
    2429                 :          80 :         Point      *pt = PG_GETARG_POINT_P(1);
    2430                 :             : 
    2431                 :         160 :         PG_RETURN_FLOAT8(lseg_closept_point(NULL, lseg, pt));
    2432                 :          80 : }
    2433                 :             : 
    2434                 :             : static float8
    2435                 :         180 : dist_ppath_internal(Point *pt, PATH *path)
    2436                 :             : {
    2437                 :         180 :         float8          result = 0.0;   /* keep compiler quiet */
    2438                 :         180 :         bool            have_min = false;
    2439                 :         180 :         float8          tmp;
    2440                 :         180 :         int                     i;
    2441                 :         180 :         LSEG            lseg;
    2442                 :             : 
    2443         [ +  - ]:         180 :         Assert(path->npts > 0);
    2444                 :             : 
    2445                 :             :         /*
    2446                 :             :          * The distance from a point to a path is the smallest distance from the
    2447                 :             :          * point to any of its constituent segments.
    2448                 :             :          */
    2449         [ +  + ]:         560 :         for (i = 0; i < path->npts; i++)
    2450                 :             :         {
    2451                 :         380 :                 int                     iprev;
    2452                 :             : 
    2453         [ +  + ]:         380 :                 if (i > 0)
    2454                 :         200 :                         iprev = i - 1;
    2455                 :             :                 else
    2456                 :             :                 {
    2457         [ +  + ]:         180 :                         if (!path->closed)
    2458                 :          80 :                                 continue;
    2459                 :         100 :                         iprev = path->npts - 1; /* Include the closure segment */
    2460                 :             :                 }
    2461                 :             : 
    2462                 :         300 :                 statlseg_construct(&lseg, &path->p[iprev], &path->p[i]);
    2463                 :         300 :                 tmp = lseg_closept_point(NULL, &lseg, pt);
    2464   [ +  +  +  + ]:         300 :                 if (!have_min || float8_lt(tmp, result))
    2465                 :             :                 {
    2466                 :         188 :                         result = tmp;
    2467                 :         188 :                         have_min = true;
    2468                 :         188 :                 }
    2469      [ -  +  + ]:         380 :         }
    2470                 :             : 
    2471                 :         360 :         return result;
    2472                 :         180 : }
    2473                 :             : 
    2474                 :             : /*
    2475                 :             :  * Distance from a point to a path
    2476                 :             :  */
    2477                 :             : Datum
    2478                 :          90 : dist_ppath(PG_FUNCTION_ARGS)
    2479                 :             : {
    2480                 :          90 :         Point      *pt = PG_GETARG_POINT_P(0);
    2481                 :          90 :         PATH       *path = PG_GETARG_PATH_P(1);
    2482                 :             : 
    2483                 :         180 :         PG_RETURN_FLOAT8(dist_ppath_internal(pt, path));
    2484                 :          90 : }
    2485                 :             : 
    2486                 :             : /*
    2487                 :             :  * Distance from a path to a point
    2488                 :             :  */
    2489                 :             : Datum
    2490                 :          90 : dist_pathp(PG_FUNCTION_ARGS)
    2491                 :             : {
    2492                 :          90 :         PATH       *path = PG_GETARG_PATH_P(0);
    2493                 :          90 :         Point      *pt = PG_GETARG_POINT_P(1);
    2494                 :             : 
    2495                 :         180 :         PG_RETURN_FLOAT8(dist_ppath_internal(pt, path));
    2496                 :          90 : }
    2497                 :             : 
    2498                 :             : /*
    2499                 :             :  * Distance from a point to a box
    2500                 :             :  */
    2501                 :             : Datum
    2502                 :          71 : dist_pb(PG_FUNCTION_ARGS)
    2503                 :             : {
    2504                 :          71 :         Point      *pt = PG_GETARG_POINT_P(0);
    2505                 :          71 :         BOX                *box = PG_GETARG_BOX_P(1);
    2506                 :             : 
    2507                 :         142 :         PG_RETURN_FLOAT8(box_closept_point(NULL, box, pt));
    2508                 :          71 : }
    2509                 :             : 
    2510                 :             : /*
    2511                 :             :  * Distance from a box to a point
    2512                 :             :  */
    2513                 :             : Datum
    2514                 :       25877 : dist_bp(PG_FUNCTION_ARGS)
    2515                 :             : {
    2516                 :       25877 :         BOX                *box = PG_GETARG_BOX_P(0);
    2517                 :       25877 :         Point      *pt = PG_GETARG_POINT_P(1);
    2518                 :             : 
    2519                 :       51754 :         PG_RETURN_FLOAT8(box_closept_point(NULL, box, pt));
    2520                 :       25877 : }
    2521                 :             : 
    2522                 :             : /*
    2523                 :             :  * Distance from a lseg to a line
    2524                 :             :  */
    2525                 :             : Datum
    2526                 :          80 : dist_sl(PG_FUNCTION_ARGS)
    2527                 :             : {
    2528                 :          80 :         LSEG       *lseg = PG_GETARG_LSEG_P(0);
    2529                 :          80 :         LINE       *line = PG_GETARG_LINE_P(1);
    2530                 :             : 
    2531                 :         160 :         PG_RETURN_FLOAT8(lseg_closept_line(NULL, lseg, line));
    2532                 :          80 : }
    2533                 :             : 
    2534                 :             : /*
    2535                 :             :  * Distance from a line to a lseg
    2536                 :             :  */
    2537                 :             : Datum
    2538                 :          80 : dist_ls(PG_FUNCTION_ARGS)
    2539                 :             : {
    2540                 :          80 :         LINE       *line = PG_GETARG_LINE_P(0);
    2541                 :          80 :         LSEG       *lseg = PG_GETARG_LSEG_P(1);
    2542                 :             : 
    2543                 :         160 :         PG_RETURN_FLOAT8(lseg_closept_line(NULL, lseg, line));
    2544                 :          80 : }
    2545                 :             : 
    2546                 :             : /*
    2547                 :             :  * Distance from a lseg to a box
    2548                 :             :  */
    2549                 :             : Datum
    2550                 :          40 : dist_sb(PG_FUNCTION_ARGS)
    2551                 :             : {
    2552                 :          40 :         LSEG       *lseg = PG_GETARG_LSEG_P(0);
    2553                 :          40 :         BOX                *box = PG_GETARG_BOX_P(1);
    2554                 :             : 
    2555                 :          80 :         PG_RETURN_FLOAT8(box_closept_lseg(NULL, box, lseg));
    2556                 :          40 : }
    2557                 :             : 
    2558                 :             : /*
    2559                 :             :  * Distance from a box to a lseg
    2560                 :             :  */
    2561                 :             : Datum
    2562                 :          40 : dist_bs(PG_FUNCTION_ARGS)
    2563                 :             : {
    2564                 :          40 :         BOX                *box = PG_GETARG_BOX_P(0);
    2565                 :          40 :         LSEG       *lseg = PG_GETARG_LSEG_P(1);
    2566                 :             : 
    2567                 :          80 :         PG_RETURN_FLOAT8(box_closept_lseg(NULL, box, lseg));
    2568                 :          40 : }
    2569                 :             : 
    2570                 :             : static float8
    2571                 :          56 : dist_cpoly_internal(CIRCLE *circle, POLYGON *poly)
    2572                 :             : {
    2573                 :          56 :         float8          result;
    2574                 :             : 
    2575                 :             :         /* calculate distance to center, and subtract radius */
    2576                 :         112 :         result = float8_mi(dist_ppoly_internal(&circle->center, poly),
    2577                 :          56 :                                            circle->radius);
    2578         [ +  + ]:          56 :         if (result < 0.0)
    2579                 :          30 :                 result = 0.0;
    2580                 :             : 
    2581                 :         112 :         return result;
    2582                 :          56 : }
    2583                 :             : 
    2584                 :             : /*
    2585                 :             :  * Distance from a circle to a polygon
    2586                 :             :  */
    2587                 :             : Datum
    2588                 :          56 : dist_cpoly(PG_FUNCTION_ARGS)
    2589                 :             : {
    2590                 :          56 :         CIRCLE     *circle = PG_GETARG_CIRCLE_P(0);
    2591                 :          56 :         POLYGON    *poly = PG_GETARG_POLYGON_P(1);
    2592                 :             : 
    2593                 :         112 :         PG_RETURN_FLOAT8(dist_cpoly_internal(circle, poly));
    2594                 :          56 : }
    2595                 :             : 
    2596                 :             : /*
    2597                 :             :  * Distance from a polygon to a circle
    2598                 :             :  */
    2599                 :             : Datum
    2600                 :           0 : dist_polyc(PG_FUNCTION_ARGS)
    2601                 :             : {
    2602                 :           0 :         POLYGON    *poly = PG_GETARG_POLYGON_P(0);
    2603                 :           0 :         CIRCLE     *circle = PG_GETARG_CIRCLE_P(1);
    2604                 :             : 
    2605                 :           0 :         PG_RETURN_FLOAT8(dist_cpoly_internal(circle, poly));
    2606                 :           0 : }
    2607                 :             : 
    2608                 :             : /*
    2609                 :             :  * Distance from a point to a polygon
    2610                 :             :  */
    2611                 :             : Datum
    2612                 :          70 : dist_ppoly(PG_FUNCTION_ARGS)
    2613                 :             : {
    2614                 :          70 :         Point      *point = PG_GETARG_POINT_P(0);
    2615                 :          70 :         POLYGON    *poly = PG_GETARG_POLYGON_P(1);
    2616                 :             : 
    2617                 :         140 :         PG_RETURN_FLOAT8(dist_ppoly_internal(point, poly));
    2618                 :          70 : }
    2619                 :             : 
    2620                 :             : Datum
    2621                 :        5718 : dist_polyp(PG_FUNCTION_ARGS)
    2622                 :             : {
    2623                 :        5718 :         POLYGON    *poly = PG_GETARG_POLYGON_P(0);
    2624                 :        5718 :         Point      *point = PG_GETARG_POINT_P(1);
    2625                 :             : 
    2626                 :       11436 :         PG_RETURN_FLOAT8(dist_ppoly_internal(point, poly));
    2627                 :        5718 : }
    2628                 :             : 
    2629                 :             : static float8
    2630                 :        5844 : dist_ppoly_internal(Point *pt, POLYGON *poly)
    2631                 :             : {
    2632                 :        5844 :         float8          result;
    2633                 :        5844 :         float8          d;
    2634                 :        5844 :         int                     i;
    2635                 :        5844 :         LSEG            seg;
    2636                 :             : 
    2637         [ +  + ]:        5844 :         if (point_inside(pt, poly->npts, poly->p) != 0)
    2638                 :          30 :                 return 0.0;
    2639                 :             : 
    2640                 :             :         /* initialize distance with segment between first and last points */
    2641                 :        5814 :         seg.p[0].x = poly->p[0].x;
    2642                 :        5814 :         seg.p[0].y = poly->p[0].y;
    2643                 :        5814 :         seg.p[1].x = poly->p[poly->npts - 1].x;
    2644                 :        5814 :         seg.p[1].y = poly->p[poly->npts - 1].y;
    2645                 :        5814 :         result = lseg_closept_point(NULL, &seg, pt);
    2646                 :             : 
    2647                 :             :         /* check distances for other segments */
    2648         [ +  + ]:       43002 :         for (i = 0; i < poly->npts - 1; i++)
    2649                 :             :         {
    2650                 :       37188 :                 seg.p[0].x = poly->p[i].x;
    2651                 :       37188 :                 seg.p[0].y = poly->p[i].y;
    2652                 :       37188 :                 seg.p[1].x = poly->p[i + 1].x;
    2653                 :       37188 :                 seg.p[1].y = poly->p[i + 1].y;
    2654                 :       37188 :                 d = lseg_closept_point(NULL, &seg, pt);
    2655         [ +  + ]:       37188 :                 if (float8_lt(d, result))
    2656                 :        1098 :                         result = d;
    2657                 :       37188 :         }
    2658                 :             : 
    2659                 :        5814 :         return result;
    2660                 :        5844 : }
    2661                 :             : 
    2662                 :             : 
    2663                 :             : /*---------------------------------------------------------------------
    2664                 :             :  *              interpt_
    2665                 :             :  *                              Intersection point of objects.
    2666                 :             :  *                              We choose to ignore the "point" of intersection between
    2667                 :             :  *                                lines and boxes, since there are typically two.
    2668                 :             :  *-------------------------------------------------------------------*/
    2669                 :             : 
    2670                 :             : /*
    2671                 :             :  * Return whether the line segment intersect with the line. If *result is not
    2672                 :             :  * NULL, it is set to the intersection point.
    2673                 :             :  */
    2674                 :             : static bool
    2675                 :      396215 : lseg_interpt_line(Point *result, LSEG *lseg, LINE *line)
    2676                 :             : {
    2677                 :      396215 :         Point           interpt;
    2678                 :      396215 :         LINE            tmp;
    2679                 :             : 
    2680                 :             :         /*
    2681                 :             :          * First, we promote the line segment to a line, because we know how to
    2682                 :             :          * find the intersection point of two lines.  If they don't have an
    2683                 :             :          * intersection point, we are done.
    2684                 :             :          */
    2685                 :      396215 :         line_construct(&tmp, &lseg->p[0], lseg_sl(lseg));
    2686         [ +  + ]:      396215 :         if (!line_interpt_line(&interpt, &tmp, line))
    2687                 :        2027 :                 return false;
    2688                 :             : 
    2689                 :             :         /*
    2690                 :             :          * Then, we check whether the intersection point is actually on the line
    2691                 :             :          * segment.
    2692                 :             :          */
    2693         [ +  + ]:      394188 :         if (!lseg_contain_point(lseg, &interpt))
    2694                 :      378442 :                 return false;
    2695         [ +  + ]:       15746 :         if (result != NULL)
    2696                 :             :         {
    2697                 :             :                 /*
    2698                 :             :                  * If there is an intersection, then check explicitly for matching
    2699                 :             :                  * endpoints since there may be rounding effects with annoying LSB
    2700                 :             :                  * residue.
    2701                 :             :                  */
    2702         [ +  + ]:       15682 :                 if (point_eq_point(&lseg->p[0], &interpt))
    2703                 :        1102 :                         *result = lseg->p[0];
    2704         [ +  + ]:       14580 :                 else if (point_eq_point(&lseg->p[1], &interpt))
    2705                 :        2209 :                         *result = lseg->p[1];
    2706                 :             :                 else
    2707                 :       12371 :                         *result = interpt;
    2708                 :       15682 :         }
    2709                 :             : 
    2710                 :       15746 :         return true;
    2711                 :      396215 : }
    2712                 :             : 
    2713                 :             : /*---------------------------------------------------------------------
    2714                 :             :  *              close_
    2715                 :             :  *                              Point of closest proximity between objects.
    2716                 :             :  *-------------------------------------------------------------------*/
    2717                 :             : 
    2718                 :             : /*
    2719                 :             :  * If *result is not NULL, it is set to the intersection point of a
    2720                 :             :  * perpendicular of the line through the point.  Returns the distance
    2721                 :             :  * of those two points.
    2722                 :             :  */
    2723                 :             : static float8
    2724                 :      298860 : line_closept_point(Point *result, LINE *line, Point *point)
    2725                 :             : {
    2726                 :      298860 :         Point           closept;
    2727                 :      298860 :         LINE            tmp;
    2728                 :             : 
    2729                 :             :         /*
    2730                 :             :          * We drop a perpendicular to find the intersection point.  Ordinarily we
    2731                 :             :          * should always find it, but that can fail in the presence of NaN
    2732                 :             :          * coordinates, and perhaps even from simple roundoff issues.
    2733                 :             :          */
    2734                 :      298860 :         line_construct(&tmp, point, line_invsl(line));
    2735         [ -  + ]:      298860 :         if (!line_interpt_line(&closept, &tmp, line))
    2736                 :             :         {
    2737         [ #  # ]:           0 :                 if (result != NULL)
    2738                 :           0 :                         *result = *point;
    2739                 :             : 
    2740                 :           0 :                 return get_float8_nan();
    2741                 :             :         }
    2742                 :             : 
    2743         [ +  + ]:      298860 :         if (result != NULL)
    2744                 :         100 :                 *result = closept;
    2745                 :             : 
    2746                 :      298860 :         return point_dt(&closept, point);
    2747                 :      298860 : }
    2748                 :             : 
    2749                 :             : Datum
    2750                 :         100 : close_pl(PG_FUNCTION_ARGS)
    2751                 :             : {
    2752                 :         100 :         Point      *pt = PG_GETARG_POINT_P(0);
    2753                 :         100 :         LINE       *line = PG_GETARG_LINE_P(1);
    2754                 :         100 :         Point      *result;
    2755                 :             : 
    2756                 :         100 :         result = palloc_object(Point);
    2757                 :             : 
    2758   [ -  +  +  +  :         100 :         if (isnan(line_closept_point(result, line, pt)))
                   +  - ]
    2759                 :         164 :                 PG_RETURN_NULL();
    2760                 :             : 
    2761                 :          64 :         PG_RETURN_POINT_P(result);
    2762                 :         228 : }
    2763                 :             : 
    2764                 :             : 
    2765                 :             : /*
    2766                 :             :  * Closest point on line segment to specified point.
    2767                 :             :  *
    2768                 :             :  * If *result is not NULL, set it to the closest point on the line segment
    2769                 :             :  * to the point.  Returns the distance of the two points.
    2770                 :             :  */
    2771                 :             : static float8
    2772                 :      151016 : lseg_closept_point(Point *result, LSEG *lseg, Point *pt)
    2773                 :             : {
    2774                 :      151016 :         Point           closept;
    2775                 :      151016 :         LINE            tmp;
    2776                 :             : 
    2777                 :             :         /*
    2778                 :             :          * To find the closest point, we draw a perpendicular line from the point
    2779                 :             :          * to the line segment.
    2780                 :             :          */
    2781                 :      151016 :         line_construct(&tmp, pt, point_invsl(&lseg->p[0], &lseg->p[1]));
    2782                 :      151016 :         lseg_closept_line(&closept, lseg, &tmp);
    2783                 :             : 
    2784         [ +  + ]:      151016 :         if (result != NULL)
    2785                 :       79361 :                 *result = closept;
    2786                 :             : 
    2787                 :      302032 :         return point_dt(&closept, pt);
    2788                 :      151016 : }
    2789                 :             : 
    2790                 :             : Datum
    2791                 :          80 : close_ps(PG_FUNCTION_ARGS)
    2792                 :             : {
    2793                 :          80 :         Point      *pt = PG_GETARG_POINT_P(0);
    2794                 :          80 :         LSEG       *lseg = PG_GETARG_LSEG_P(1);
    2795                 :          80 :         Point      *result;
    2796                 :             : 
    2797                 :          80 :         result = palloc_object(Point);
    2798                 :             : 
    2799   [ -  +  +  +  :          80 :         if (isnan(lseg_closept_point(result, lseg, pt)))
                   +  - ]
    2800                 :         144 :                 PG_RETURN_NULL();
    2801                 :             : 
    2802                 :          64 :         PG_RETURN_POINT_P(result);
    2803                 :         208 : }
    2804                 :             : 
    2805                 :             : 
    2806                 :             : /*
    2807                 :             :  * Closest point on line segment to line segment
    2808                 :             :  */
    2809                 :             : static float8
    2810                 :         878 : lseg_closept_lseg(Point *result, LSEG *on_lseg, LSEG *to_lseg)
    2811                 :             : {
    2812                 :         878 :         Point           point;
    2813                 :         878 :         float8          dist,
    2814                 :             :                                 d;
    2815                 :             : 
    2816                 :             :         /* First, we handle the case when the line segments are intersecting. */
    2817         [ +  + ]:         878 :         if (lseg_interpt_lseg(result, on_lseg, to_lseg))
    2818                 :           4 :                 return 0.0;
    2819                 :             : 
    2820                 :             :         /*
    2821                 :             :          * Then, we find the closest points from the endpoints of the second line
    2822                 :             :          * segment, and keep the closest one.
    2823                 :             :          */
    2824                 :         874 :         dist = lseg_closept_point(result, on_lseg, &to_lseg->p[0]);
    2825                 :         874 :         d = lseg_closept_point(&point, on_lseg, &to_lseg->p[1]);
    2826         [ +  + ]:         874 :         if (float8_lt(d, dist))
    2827                 :             :         {
    2828                 :         291 :                 dist = d;
    2829         [ +  + ]:         291 :                 if (result != NULL)
    2830                 :         136 :                         *result = point;
    2831                 :         291 :         }
    2832                 :             : 
    2833                 :             :         /* The closest point can still be one of the endpoints, so we test them. */
    2834                 :         874 :         d = lseg_closept_point(NULL, to_lseg, &on_lseg->p[0]);
    2835         [ +  + ]:         874 :         if (float8_lt(d, dist))
    2836                 :             :         {
    2837                 :         192 :                 dist = d;
    2838         [ +  + ]:         192 :                 if (result != NULL)
    2839                 :         124 :                         *result = on_lseg->p[0];
    2840                 :         192 :         }
    2841                 :         874 :         d = lseg_closept_point(NULL, to_lseg, &on_lseg->p[1]);
    2842         [ +  + ]:         874 :         if (float8_lt(d, dist))
    2843                 :             :         {
    2844                 :          74 :                 dist = d;
    2845         [ +  + ]:          74 :                 if (result != NULL)
    2846                 :          42 :                         *result = on_lseg->p[1];
    2847                 :          74 :         }
    2848                 :             : 
    2849                 :         874 :         return dist;
    2850                 :         878 : }
    2851                 :             : 
    2852                 :             : Datum
    2853                 :          64 : close_lseg(PG_FUNCTION_ARGS)
    2854                 :             : {
    2855                 :          64 :         LSEG       *l1 = PG_GETARG_LSEG_P(0);
    2856                 :          64 :         LSEG       *l2 = PG_GETARG_LSEG_P(1);
    2857                 :          64 :         Point      *result;
    2858                 :             : 
    2859         [ +  + ]:          64 :         if (lseg_sl(l1) == lseg_sl(l2))
    2860                 :          13 :                 PG_RETURN_NULL();
    2861                 :             : 
    2862                 :          51 :         result = palloc_object(Point);
    2863                 :             : 
    2864   [ -  +  +  +  :          51 :         if (isnan(lseg_closept_lseg(result, l2, l1)))
                   +  - ]
    2865                 :          87 :                 PG_RETURN_NULL();
    2866                 :             : 
    2867                 :          36 :         PG_RETURN_POINT_P(result);
    2868                 :         136 : }
    2869                 :             : 
    2870                 :             : 
    2871                 :             : /*
    2872                 :             :  * Closest point on or in box to specified point.
    2873                 :             :  *
    2874                 :             :  * If *result is not NULL, set it to the closest point on the box to the
    2875                 :             :  * given point, and return the distance of the two points.
    2876                 :             :  */
    2877                 :             : static float8
    2878                 :       25998 : box_closept_point(Point *result, BOX *box, Point *pt)
    2879                 :             : {
    2880                 :       25998 :         float8          dist,
    2881                 :             :                                 d;
    2882                 :       25998 :         Point           point,
    2883                 :             :                                 closept;
    2884                 :       25998 :         LSEG            lseg;
    2885                 :             : 
    2886         [ +  + ]:       25998 :         if (box_contain_point(box, pt))
    2887                 :             :         {
    2888         [ +  + ]:           7 :                 if (result != NULL)
    2889                 :           1 :                         *result = *pt;
    2890                 :             : 
    2891                 :           7 :                 return 0.0;
    2892                 :             :         }
    2893                 :             : 
    2894                 :             :         /* pairwise check lseg distances */
    2895                 :       25991 :         point.x = box->low.x;
    2896                 :       25991 :         point.y = box->high.y;
    2897                 :       25991 :         statlseg_construct(&lseg, &box->low, &point);
    2898                 :       25991 :         dist = lseg_closept_point(result, &lseg, pt);
    2899                 :             : 
    2900                 :       25991 :         statlseg_construct(&lseg, &box->high, &point);
    2901                 :       25991 :         d = lseg_closept_point(&closept, &lseg, pt);
    2902         [ +  + ]:       25991 :         if (float8_lt(d, dist))
    2903                 :             :         {
    2904                 :        1239 :                 dist = d;
    2905         [ +  + ]:        1239 :                 if (result != NULL)
    2906                 :           9 :                         *result = closept;
    2907                 :        1239 :         }
    2908                 :             : 
    2909                 :       25991 :         point.x = box->high.x;
    2910                 :       25991 :         point.y = box->low.y;
    2911                 :       25991 :         statlseg_construct(&lseg, &box->low, &point);
    2912                 :       25991 :         d = lseg_closept_point(&closept, &lseg, pt);
    2913         [ +  + ]:       25991 :         if (float8_lt(d, dist))
    2914                 :             :         {
    2915                 :        1323 :                 dist = d;
    2916         [ +  + ]:        1323 :                 if (result != NULL)
    2917                 :           1 :                         *result = closept;
    2918                 :        1323 :         }
    2919                 :             : 
    2920                 :       25991 :         statlseg_construct(&lseg, &box->high, &point);
    2921                 :       25991 :         d = lseg_closept_point(&closept, &lseg, pt);
    2922         [ +  + ]:       25991 :         if (float8_lt(d, dist))
    2923                 :             :         {
    2924                 :           6 :                 dist = d;
    2925         [ +  + ]:           6 :                 if (result != NULL)
    2926                 :           2 :                         *result = closept;
    2927                 :           6 :         }
    2928                 :             : 
    2929                 :       25991 :         return dist;
    2930                 :       25998 : }
    2931                 :             : 
    2932                 :             : Datum
    2933                 :          50 : close_pb(PG_FUNCTION_ARGS)
    2934                 :             : {
    2935                 :          50 :         Point      *pt = PG_GETARG_POINT_P(0);
    2936                 :          50 :         BOX                *box = PG_GETARG_BOX_P(1);
    2937                 :          50 :         Point      *result;
    2938                 :             : 
    2939                 :          50 :         result = palloc_object(Point);
    2940                 :             : 
    2941   [ -  +  +  +  :          50 :         if (isnan(box_closept_point(result, box, pt)))
                   +  - ]
    2942                 :          95 :                 PG_RETURN_NULL();
    2943                 :             : 
    2944                 :          45 :         PG_RETURN_POINT_P(result);
    2945                 :         140 : }
    2946                 :             : 
    2947                 :             : /*
    2948                 :             :  * Closest point on line segment to line.
    2949                 :             :  *
    2950                 :             :  * Return the distance between the line and the closest point of the line
    2951                 :             :  * segment to the line.  If *result is not NULL, set it to that point.
    2952                 :             :  *
    2953                 :             :  * NOTE: When the lines are parallel, endpoints of one of the line segment
    2954                 :             :  * are FPeq(), in presence of NaN or Infinite coordinates, or perhaps =
    2955                 :             :  * even because of simple roundoff issues, there may not be a single closest
    2956                 :             :  * point.  We are likely to set the result to the second endpoint in these
    2957                 :             :  * cases.
    2958                 :             :  */
    2959                 :             : static float8
    2960                 :      151247 : lseg_closept_line(Point *result, LSEG *lseg, LINE *line)
    2961                 :             : {
    2962                 :      151247 :         float8          dist1,
    2963                 :             :                                 dist2;
    2964                 :             : 
    2965         [ +  + ]:      151247 :         if (lseg_interpt_line(result, lseg, line))
    2966                 :        1967 :                 return 0.0;
    2967                 :             : 
    2968                 :      149280 :         dist1 = line_closept_point(NULL, line, &lseg->p[0]);
    2969                 :      149280 :         dist2 = line_closept_point(NULL, line, &lseg->p[1]);
    2970                 :             : 
    2971         [ +  + ]:      149280 :         if (dist1 < dist2)
    2972                 :             :         {
    2973         [ +  + ]:       79285 :                 if (result != NULL)
    2974                 :       79251 :                         *result = lseg->p[0];
    2975                 :             : 
    2976                 :       79285 :                 return dist1;
    2977                 :             :         }
    2978                 :             :         else
    2979                 :             :         {
    2980         [ +  + ]:       69995 :                 if (result != NULL)
    2981                 :       69903 :                         *result = lseg->p[1];
    2982                 :             : 
    2983                 :       69995 :                 return dist2;
    2984                 :             :         }
    2985                 :      151247 : }
    2986                 :             : 
    2987                 :             : Datum
    2988                 :          80 : close_ls(PG_FUNCTION_ARGS)
    2989                 :             : {
    2990                 :          80 :         LINE       *line = PG_GETARG_LINE_P(0);
    2991                 :          80 :         LSEG       *lseg = PG_GETARG_LSEG_P(1);
    2992                 :          80 :         Point      *result;
    2993                 :             : 
    2994         [ +  + ]:          80 :         if (lseg_sl(lseg) == line_sl(line))
    2995                 :           9 :                 PG_RETURN_NULL();
    2996                 :             : 
    2997                 :          71 :         result = palloc_object(Point);
    2998                 :             : 
    2999   [ -  +  +  +  :          71 :         if (isnan(lseg_closept_line(result, lseg, line)))
                   +  - ]
    3000                 :         118 :                 PG_RETURN_NULL();
    3001                 :             : 
    3002                 :          47 :         PG_RETURN_POINT_P(result);
    3003                 :         174 : }
    3004                 :             : 
    3005                 :             : 
    3006                 :             : /*
    3007                 :             :  * Closest point on or in box to line segment.
    3008                 :             :  *
    3009                 :             :  * Returns the distance between the closest point on or in the box to
    3010                 :             :  * the line segment.  If *result is not NULL, it is set to that point.
    3011                 :             :  */
    3012                 :             : static float8
    3013                 :         120 : box_closept_lseg(Point *result, BOX *box, LSEG *lseg)
    3014                 :             : {
    3015                 :         120 :         float8          dist,
    3016                 :             :                                 d;
    3017                 :         120 :         Point           point,
    3018                 :             :                                 closept;
    3019                 :         120 :         LSEG            bseg;
    3020                 :             : 
    3021         [ +  + ]:         120 :         if (box_interpt_lseg(result, box, lseg))
    3022                 :          24 :                 return 0.0;
    3023                 :             : 
    3024                 :             :         /* pairwise check lseg distances */
    3025                 :          96 :         point.x = box->low.x;
    3026                 :          96 :         point.y = box->high.y;
    3027                 :          96 :         statlseg_construct(&bseg, &box->low, &point);
    3028                 :          96 :         dist = lseg_closept_lseg(result, &bseg, lseg);
    3029                 :             : 
    3030                 :          96 :         statlseg_construct(&bseg, &box->high, &point);
    3031                 :          96 :         d = lseg_closept_lseg(&closept, &bseg, lseg);
    3032         [ +  + ]:          96 :         if (float8_lt(d, dist))
    3033                 :             :         {
    3034                 :          24 :                 dist = d;
    3035         [ +  + ]:          24 :                 if (result != NULL)
    3036                 :           8 :                         *result = closept;
    3037                 :          24 :         }
    3038                 :             : 
    3039                 :          96 :         point.x = box->high.x;
    3040                 :          96 :         point.y = box->low.y;
    3041                 :          96 :         statlseg_construct(&bseg, &box->low, &point);
    3042                 :          96 :         d = lseg_closept_lseg(&closept, &bseg, lseg);
    3043         [ +  + ]:          96 :         if (float8_lt(d, dist))
    3044                 :             :         {
    3045                 :           3 :                 dist = d;
    3046         [ +  + ]:           3 :                 if (result != NULL)
    3047                 :           1 :                         *result = closept;
    3048                 :           3 :         }
    3049                 :             : 
    3050                 :          96 :         statlseg_construct(&bseg, &box->high, &point);
    3051                 :          96 :         d = lseg_closept_lseg(&closept, &bseg, lseg);
    3052         [ +  + ]:          96 :         if (float8_lt(d, dist))
    3053                 :             :         {
    3054                 :           3 :                 dist = d;
    3055         [ +  + ]:           3 :                 if (result != NULL)
    3056                 :           1 :                         *result = closept;
    3057                 :           3 :         }
    3058                 :             : 
    3059                 :          96 :         return dist;
    3060                 :         120 : }
    3061                 :             : 
    3062                 :             : Datum
    3063                 :          40 : close_sb(PG_FUNCTION_ARGS)
    3064                 :             : {
    3065                 :          40 :         LSEG       *lseg = PG_GETARG_LSEG_P(0);
    3066                 :          40 :         BOX                *box = PG_GETARG_BOX_P(1);
    3067                 :          40 :         Point      *result;
    3068                 :             : 
    3069                 :          40 :         result = palloc_object(Point);
    3070                 :             : 
    3071   [ -  +  +  +  :          40 :         if (isnan(box_closept_lseg(result, box, lseg)))
                   +  - ]
    3072                 :          75 :                 PG_RETURN_NULL();
    3073                 :             : 
    3074                 :          35 :         PG_RETURN_POINT_P(result);
    3075                 :         110 : }
    3076                 :             : 
    3077                 :             : 
    3078                 :             : /*---------------------------------------------------------------------
    3079                 :             :  *              on_
    3080                 :             :  *                              Whether one object lies completely within another.
    3081                 :             :  *-------------------------------------------------------------------*/
    3082                 :             : 
    3083                 :             : /*
    3084                 :             :  *              Does the point satisfy the equation?
    3085                 :             :  */
    3086                 :             : static bool
    3087                 :         184 : line_contain_point(LINE *line, Point *point)
    3088                 :             : {
    3089                 :         184 :         return FPzero(float8_pl(float8_pl(float8_mul(line->A, point->x),
    3090                 :             :                                                                           float8_mul(line->B, point->y)),
    3091                 :             :                                                         line->C));
    3092                 :             : }
    3093                 :             : 
    3094                 :             : Datum
    3095                 :         100 : on_pl(PG_FUNCTION_ARGS)
    3096                 :             : {
    3097                 :         100 :         Point      *pt = PG_GETARG_POINT_P(0);
    3098                 :         100 :         LINE       *line = PG_GETARG_LINE_P(1);
    3099                 :             : 
    3100                 :         200 :         PG_RETURN_BOOL(line_contain_point(line, pt));
    3101                 :         100 : }
    3102                 :             : 
    3103                 :             : 
    3104                 :             : /*
    3105                 :             :  *              Determine colinearity by detecting a triangle inequality.
    3106                 :             :  * This algorithm seems to behave nicely even with lsb residues - tgl 1997-07-09
    3107                 :             :  */
    3108                 :             : static bool
    3109                 :      669143 : lseg_contain_point(LSEG *lseg, Point *pt)
    3110                 :             : {
    3111                 :     2007429 :         return FPeq(point_dt(pt, &lseg->p[0]) +
    3112                 :      669143 :                                 point_dt(pt, &lseg->p[1]),
    3113                 :      669143 :                                 point_dt(&lseg->p[0], &lseg->p[1]));
    3114                 :             : }
    3115                 :             : 
    3116                 :             : Datum
    3117                 :          80 : on_ps(PG_FUNCTION_ARGS)
    3118                 :             : {
    3119                 :          80 :         Point      *pt = PG_GETARG_POINT_P(0);
    3120                 :          80 :         LSEG       *lseg = PG_GETARG_LSEG_P(1);
    3121                 :             : 
    3122                 :         160 :         PG_RETURN_BOOL(lseg_contain_point(lseg, pt));
    3123                 :          80 : }
    3124                 :             : 
    3125                 :             : 
    3126                 :             : /*
    3127                 :             :  * Check whether the point is in the box or on its border
    3128                 :             :  */
    3129                 :             : static bool
    3130                 :       72964 : box_contain_point(BOX *box, Point *point)
    3131                 :             : {
    3132   [ +  +  +  + ]:       89865 :         return box->high.x >= point->x && box->low.x <= point->x &&
    3133         [ +  + ]:       16901 :                 box->high.y >= point->y && box->low.y <= point->y;
    3134                 :             : }
    3135                 :             : 
    3136                 :             : Datum
    3137                 :       23042 : on_pb(PG_FUNCTION_ARGS)
    3138                 :             : {
    3139                 :       23042 :         Point      *pt = PG_GETARG_POINT_P(0);
    3140                 :       23042 :         BOX                *box = PG_GETARG_BOX_P(1);
    3141                 :             : 
    3142                 :       46084 :         PG_RETURN_BOOL(box_contain_point(box, pt));
    3143                 :       23042 : }
    3144                 :             : 
    3145                 :             : Datum
    3146                 :       23781 : box_contain_pt(PG_FUNCTION_ARGS)
    3147                 :             : {
    3148                 :       23781 :         BOX                *box = PG_GETARG_BOX_P(0);
    3149                 :       23781 :         Point      *pt = PG_GETARG_POINT_P(1);
    3150                 :             : 
    3151                 :       47562 :         PG_RETURN_BOOL(box_contain_point(box, pt));
    3152                 :       23781 : }
    3153                 :             : 
    3154                 :             : /* on_ppath -
    3155                 :             :  *              Whether a point lies within (on) a polyline.
    3156                 :             :  *              If open, we have to (groan) check each segment.
    3157                 :             :  * (uses same algorithm as for point intersecting segment - tgl 1997-07-09)
    3158                 :             :  *              If closed, we use the old O(n) ray method for point-in-polygon.
    3159                 :             :  *                              The ray is horizontal, from pt out to the right.
    3160                 :             :  *                              Each segment that crosses the ray counts as an
    3161                 :             :  *                              intersection; note that an endpoint or edge may touch
    3162                 :             :  *                              but not cross.
    3163                 :             :  *                              (we can do p-in-p in lg(n), but it takes preprocessing)
    3164                 :             :  */
    3165                 :             : Datum
    3166                 :         100 : on_ppath(PG_FUNCTION_ARGS)
    3167                 :             : {
    3168                 :         100 :         Point      *pt = PG_GETARG_POINT_P(0);
    3169                 :         100 :         PATH       *path = PG_GETARG_PATH_P(1);
    3170                 :         100 :         int                     i,
    3171                 :             :                                 n;
    3172                 :         100 :         float8          a,
    3173                 :             :                                 b;
    3174                 :             : 
    3175                 :             :         /*-- OPEN --*/
    3176         [ +  + ]:         100 :         if (!path->closed)
    3177                 :             :         {
    3178                 :          50 :                 n = path->npts - 1;
    3179                 :          50 :                 a = point_dt(pt, &path->p[0]);
    3180         [ +  + ]:         118 :                 for (i = 0; i < n; i++)
    3181                 :             :                 {
    3182                 :          73 :                         b = point_dt(pt, &path->p[i + 1]);
    3183         [ +  + ]:          73 :                         if (FPeq(float8_pl(a, b), point_dt(&path->p[i], &path->p[i + 1])))
    3184                 :           5 :                                 PG_RETURN_BOOL(true);
    3185                 :          68 :                         a = b;
    3186                 :          68 :                 }
    3187                 :          45 :                 PG_RETURN_BOOL(false);
    3188                 :             :         }
    3189                 :             : 
    3190                 :             :         /*-- CLOSED --*/
    3191                 :          50 :         PG_RETURN_BOOL(point_inside(pt, path->npts, path->p) != 0);
    3192                 :         100 : }
    3193                 :             : 
    3194                 :             : 
    3195                 :             : /*
    3196                 :             :  * Check whether the line segment is on the line or close enough
    3197                 :             :  *
    3198                 :             :  * It is, if both of its points are on the line or close enough.
    3199                 :             :  */
    3200                 :             : Datum
    3201                 :          80 : on_sl(PG_FUNCTION_ARGS)
    3202                 :             : {
    3203                 :          80 :         LSEG       *lseg = PG_GETARG_LSEG_P(0);
    3204                 :          80 :         LINE       *line = PG_GETARG_LINE_P(1);
    3205                 :             : 
    3206         [ +  + ]:          80 :         PG_RETURN_BOOL(line_contain_point(line, &lseg->p[0]) &&
    3207                 :             :                                    line_contain_point(line, &lseg->p[1]));
    3208                 :          80 : }
    3209                 :             : 
    3210                 :             : 
    3211                 :             : /*
    3212                 :             :  * Check whether the line segment is in the box or on its border
    3213                 :             :  *
    3214                 :             :  * It is, if both of its points are in the box or on its border.
    3215                 :             :  */
    3216                 :             : static bool
    3217                 :          40 : box_contain_lseg(BOX *box, LSEG *lseg)
    3218                 :             : {
    3219         [ +  + ]:          43 :         return box_contain_point(box, &lseg->p[0]) &&
    3220                 :           3 :                 box_contain_point(box, &lseg->p[1]);
    3221                 :             : }
    3222                 :             : 
    3223                 :             : Datum
    3224                 :          40 : on_sb(PG_FUNCTION_ARGS)
    3225                 :             : {
    3226                 :          40 :         LSEG       *lseg = PG_GETARG_LSEG_P(0);
    3227                 :          40 :         BOX                *box = PG_GETARG_BOX_P(1);
    3228                 :             : 
    3229                 :          80 :         PG_RETURN_BOOL(box_contain_lseg(box, lseg));
    3230                 :          40 : }
    3231                 :             : 
    3232                 :             : /*---------------------------------------------------------------------
    3233                 :             :  *              inter_
    3234                 :             :  *                              Whether one object intersects another.
    3235                 :             :  *-------------------------------------------------------------------*/
    3236                 :             : 
    3237                 :             : Datum
    3238                 :          80 : inter_sl(PG_FUNCTION_ARGS)
    3239                 :             : {
    3240                 :          80 :         LSEG       *lseg = PG_GETARG_LSEG_P(0);
    3241                 :          80 :         LINE       *line = PG_GETARG_LINE_P(1);
    3242                 :             : 
    3243                 :         160 :         PG_RETURN_BOOL(lseg_interpt_line(NULL, lseg, line));
    3244                 :          80 : }
    3245                 :             : 
    3246                 :             : 
    3247                 :             : /*
    3248                 :             :  * Do line segment and box intersect?
    3249                 :             :  *
    3250                 :             :  * Segment completely inside box counts as intersection.
    3251                 :             :  * If you want only segments crossing box boundaries,
    3252                 :             :  *      try converting box to path first.
    3253                 :             :  *
    3254                 :             :  * This function also sets the *result to the closest point on the line
    3255                 :             :  * segment to the center of the box when they overlap and the result is
    3256                 :             :  * not NULL.  It is somewhat arbitrary, but maybe the best we can do as
    3257                 :             :  * there are typically two points they intersect.
    3258                 :             :  *
    3259                 :             :  * Optimize for non-intersection by checking for box intersection first.
    3260                 :             :  * - thomas 1998-01-30
    3261                 :             :  */
    3262                 :             : static bool
    3263                 :         160 : box_interpt_lseg(Point *result, BOX *box, LSEG *lseg)
    3264                 :             : {
    3265                 :         160 :         BOX                     lbox;
    3266                 :         160 :         LSEG            bseg;
    3267                 :         160 :         Point           point;
    3268                 :             : 
    3269                 :         160 :         lbox.low.x = float8_min(lseg->p[0].x, lseg->p[1].x);
    3270                 :         160 :         lbox.low.y = float8_min(lseg->p[0].y, lseg->p[1].y);
    3271                 :         160 :         lbox.high.x = float8_max(lseg->p[0].x, lseg->p[1].x);
    3272                 :         160 :         lbox.high.y = float8_max(lseg->p[0].y, lseg->p[1].y);
    3273                 :             : 
    3274                 :             :         /* nothing close to overlap? then not going to intersect */
    3275         [ +  + ]:         160 :         if (!box_ov(&lbox, box))
    3276                 :         104 :                 return false;
    3277                 :             : 
    3278         [ +  + ]:          56 :         if (result != NULL)
    3279                 :             :         {
    3280                 :          14 :                 box_cn(&point, box);
    3281                 :          14 :                 lseg_closept_point(result, lseg, &point);
    3282                 :          14 :         }
    3283                 :             : 
    3284                 :             :         /* an endpoint of segment is inside box? then clearly intersects */
    3285   [ +  +  +  + ]:          56 :         if (box_contain_point(box, &lseg->p[0]) ||
    3286                 :          44 :                 box_contain_point(box, &lseg->p[1]))
    3287                 :          16 :                 return true;
    3288                 :             : 
    3289                 :             :         /* pairwise check lseg intersections */
    3290                 :          40 :         point.x = box->low.x;
    3291                 :          40 :         point.y = box->high.y;
    3292                 :          40 :         statlseg_construct(&bseg, &box->low, &point);
    3293         [ +  + ]:          40 :         if (lseg_interpt_lseg(NULL, &bseg, lseg))
    3294                 :          16 :                 return true;
    3295                 :             : 
    3296                 :          24 :         statlseg_construct(&bseg, &box->high, &point);
    3297         [ -  + ]:          24 :         if (lseg_interpt_lseg(NULL, &bseg, lseg))
    3298                 :           0 :                 return true;
    3299                 :             : 
    3300                 :          24 :         point.x = box->high.x;
    3301                 :          24 :         point.y = box->low.y;
    3302                 :          24 :         statlseg_construct(&bseg, &box->low, &point);
    3303         [ -  + ]:          24 :         if (lseg_interpt_lseg(NULL, &bseg, lseg))
    3304                 :           0 :                 return true;
    3305                 :             : 
    3306                 :          24 :         statlseg_construct(&bseg, &box->high, &point);
    3307         [ -  + ]:          24 :         if (lseg_interpt_lseg(NULL, &bseg, lseg))
    3308                 :           0 :                 return true;
    3309                 :             : 
    3310                 :             :         /* if we dropped through, no two segs intersected */
    3311                 :          24 :         return false;
    3312                 :         160 : }
    3313                 :             : 
    3314                 :             : Datum
    3315                 :          40 : inter_sb(PG_FUNCTION_ARGS)
    3316                 :             : {
    3317                 :          40 :         LSEG       *lseg = PG_GETARG_LSEG_P(0);
    3318                 :          40 :         BOX                *box = PG_GETARG_BOX_P(1);
    3319                 :             : 
    3320                 :          80 :         PG_RETURN_BOOL(box_interpt_lseg(NULL, box, lseg));
    3321                 :          40 : }
    3322                 :             : 
    3323                 :             : 
    3324                 :             : /* inter_lb()
    3325                 :             :  * Do line and box intersect?
    3326                 :             :  */
    3327                 :             : Datum
    3328                 :          50 : inter_lb(PG_FUNCTION_ARGS)
    3329                 :             : {
    3330                 :          50 :         LINE       *line = PG_GETARG_LINE_P(0);
    3331                 :          50 :         BOX                *box = PG_GETARG_BOX_P(1);
    3332                 :          50 :         LSEG            bseg;
    3333                 :          50 :         Point           p1,
    3334                 :             :                                 p2;
    3335                 :             : 
    3336                 :             :         /* pairwise check lseg intersections */
    3337                 :          50 :         p1.x = box->low.x;
    3338                 :          50 :         p1.y = box->low.y;
    3339                 :          50 :         p2.x = box->low.x;
    3340                 :          50 :         p2.y = box->high.y;
    3341                 :          50 :         statlseg_construct(&bseg, &p1, &p2);
    3342         [ +  + ]:          50 :         if (lseg_interpt_line(NULL, &bseg, line))
    3343                 :          11 :                 PG_RETURN_BOOL(true);
    3344                 :          39 :         p1.x = box->high.x;
    3345                 :          39 :         p1.y = box->high.y;
    3346                 :          39 :         statlseg_construct(&bseg, &p1, &p2);
    3347         [ +  + ]:          39 :         if (lseg_interpt_line(NULL, &bseg, line))
    3348                 :           2 :                 PG_RETURN_BOOL(true);
    3349                 :          37 :         p2.x = box->high.x;
    3350                 :          37 :         p2.y = box->low.y;
    3351                 :          37 :         statlseg_construct(&bseg, &p1, &p2);
    3352         [ -  + ]:          37 :         if (lseg_interpt_line(NULL, &bseg, line))
    3353                 :           0 :                 PG_RETURN_BOOL(true);
    3354                 :          37 :         p1.x = box->low.x;
    3355                 :          37 :         p1.y = box->low.y;
    3356                 :          37 :         statlseg_construct(&bseg, &p1, &p2);
    3357         [ -  + ]:          37 :         if (lseg_interpt_line(NULL, &bseg, line))
    3358                 :           0 :                 PG_RETURN_BOOL(true);
    3359                 :             : 
    3360                 :             :         /* if we dropped through, no intersection */
    3361                 :          37 :         PG_RETURN_BOOL(false);
    3362                 :          50 : }
    3363                 :             : 
    3364                 :             : /*------------------------------------------------------------------
    3365                 :             :  * The following routines define a data type and operator class for
    3366                 :             :  * POLYGONS .... Part of which (the polygon's bounding box) is built on
    3367                 :             :  * top of the BOX data type.
    3368                 :             :  *
    3369                 :             :  * make_bound_box - create the bounding box for the input polygon
    3370                 :             :  *------------------------------------------------------------------*/
    3371                 :             : 
    3372                 :             : /*---------------------------------------------------------------------
    3373                 :             :  * Make the smallest bounding box for the given polygon.
    3374                 :             :  *---------------------------------------------------------------------*/
    3375                 :             : static void
    3376                 :       10083 : make_bound_box(POLYGON *poly)
    3377                 :             : {
    3378                 :       10083 :         int                     i;
    3379                 :       10083 :         float8          x1,
    3380                 :             :                                 y1,
    3381                 :             :                                 x2,
    3382                 :             :                                 y2;
    3383                 :             : 
    3384         [ +  - ]:       10083 :         Assert(poly->npts > 0);
    3385                 :             : 
    3386                 :       10083 :         x1 = x2 = poly->p[0].x;
    3387                 :       10083 :         y2 = y1 = poly->p[0].y;
    3388         [ +  + ]:      120339 :         for (i = 1; i < poly->npts; i++)
    3389                 :             :         {
    3390         [ +  + ]:      110256 :                 if (float8_lt(poly->p[i].x, x1))
    3391                 :          11 :                         x1 = poly->p[i].x;
    3392         [ +  + ]:      110256 :                 if (float8_gt(poly->p[i].x, x2))
    3393                 :       60174 :                         x2 = poly->p[i].x;
    3394         [ +  + ]:      110256 :                 if (float8_lt(poly->p[i].y, y1))
    3395                 :       30066 :                         y1 = poly->p[i].y;
    3396         [ +  + ]:      110256 :                 if (float8_gt(poly->p[i].y, y2))
    3397                 :       30099 :                         y2 = poly->p[i].y;
    3398                 :      110256 :         }
    3399                 :             : 
    3400                 :       10083 :         poly->boundbox.low.x = x1;
    3401                 :       10083 :         poly->boundbox.high.x = x2;
    3402                 :       10083 :         poly->boundbox.low.y = y1;
    3403                 :       10083 :         poly->boundbox.high.y = y2;
    3404                 :       10083 : }
    3405                 :             : 
    3406                 :             : /*------------------------------------------------------------------
    3407                 :             :  * poly_in - read in the polygon from a string specification
    3408                 :             :  *
    3409                 :             :  *              External format:
    3410                 :             :  *                              "((x0,y0),...,(xn,yn))"
    3411                 :             :  *                              "x0,y0,...,xn,yn"
    3412                 :             :  *                              also supports the older style "(x1,...,xn,y1,...yn)"
    3413                 :             :  *------------------------------------------------------------------*/
    3414                 :             : Datum
    3415                 :          69 : poly_in(PG_FUNCTION_ARGS)
    3416                 :             : {
    3417                 :          69 :         char       *str = PG_GETARG_CSTRING(0);
    3418                 :          69 :         Node       *escontext = fcinfo->context;
    3419                 :          69 :         POLYGON    *poly;
    3420                 :          69 :         int                     npts;
    3421                 :          69 :         int                     size;
    3422                 :          69 :         int                     base_size;
    3423                 :          69 :         bool            isopen;
    3424                 :             : 
    3425         [ +  + ]:          69 :         if ((npts = pair_count(str, ',')) <= 0)
    3426         [ +  + ]:           8 :                 ereturn(escontext, (Datum) 0,
    3427                 :             :                                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
    3428                 :             :                                  errmsg("invalid input syntax for type %s: \"%s\"",
    3429                 :             :                                                 "polygon", str)));
    3430                 :             : 
    3431                 :          61 :         base_size = sizeof(poly->p[0]) * npts;
    3432                 :          61 :         size = offsetof(POLYGON, p) + base_size;
    3433                 :             : 
    3434                 :             :         /* Check for integer overflow */
    3435   [ +  +  +  + ]:          61 :         if (base_size / npts != sizeof(poly->p[0]) || size <= base_size)
    3436         [ #  # ]:           2 :                 ereturn(escontext, (Datum) 0,
    3437                 :             :                                 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
    3438                 :             :                                  errmsg("too many points requested")));
    3439                 :             : 
    3440                 :          59 :         poly = (POLYGON *) palloc0(size);       /* zero any holes */
    3441                 :             : 
    3442                 :          59 :         SET_VARSIZE(poly, size);
    3443                 :          59 :         poly->npts = npts;
    3444                 :             : 
    3445   [ +  +  +  + ]:         118 :         if (!path_decode(str, false, npts, &(poly->p[0]), &isopen, NULL, "polygon",
    3446                 :          59 :                                          str, escontext))
    3447                 :           2 :                 PG_RETURN_NULL();
    3448                 :             : 
    3449                 :          57 :         make_bound_box(poly);
    3450                 :             : 
    3451                 :          57 :         PG_RETURN_POLYGON_P(poly);
    3452                 :          61 : }
    3453                 :             : 
    3454                 :             : /*---------------------------------------------------------------
    3455                 :             :  * poly_out - convert internal POLYGON representation to the
    3456                 :             :  *                        character string format "((f8,f8),...,(f8,f8))"
    3457                 :             :  *---------------------------------------------------------------*/
    3458                 :             : Datum
    3459                 :         922 : poly_out(PG_FUNCTION_ARGS)
    3460                 :             : {
    3461                 :         922 :         POLYGON    *poly = PG_GETARG_POLYGON_P(0);
    3462                 :             : 
    3463                 :        1844 :         PG_RETURN_CSTRING(path_encode(PATH_CLOSED, poly->npts, poly->p));
    3464                 :         922 : }
    3465                 :             : 
    3466                 :             : /*
    3467                 :             :  *              poly_recv                       - converts external binary format to polygon
    3468                 :             :  *
    3469                 :             :  * External representation is int32 number of points, and the points.
    3470                 :             :  * We recompute the bounding box on read, instead of trusting it to
    3471                 :             :  * be valid.  (Checking it would take just as long, so may as well
    3472                 :             :  * omit it from external representation.)
    3473                 :             :  */
    3474                 :             : Datum
    3475                 :           0 : poly_recv(PG_FUNCTION_ARGS)
    3476                 :             : {
    3477                 :           0 :         StringInfo      buf = (StringInfo) PG_GETARG_POINTER(0);
    3478                 :           0 :         POLYGON    *poly;
    3479                 :           0 :         int32           npts;
    3480                 :           0 :         int32           i;
    3481                 :           0 :         int                     size;
    3482                 :             : 
    3483                 :           0 :         npts = pq_getmsgint(buf, sizeof(int32));
    3484         [ #  # ]:           0 :         if (npts <= 0 || npts >= (int32) ((INT_MAX - offsetof(POLYGON, p)) / sizeof(Point)))
    3485   [ #  #  #  # ]:           0 :                 ereport(ERROR,
    3486                 :             :                                 (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
    3487                 :             :                                  errmsg("invalid number of points in external \"polygon\" value")));
    3488                 :             : 
    3489                 :           0 :         size = offsetof(POLYGON, p) + sizeof(poly->p[0]) * npts;
    3490                 :           0 :         poly = (POLYGON *) palloc0(size);       /* zero any holes */
    3491                 :             : 
    3492                 :           0 :         SET_VARSIZE(poly, size);
    3493                 :           0 :         poly->npts = npts;
    3494                 :             : 
    3495         [ #  # ]:           0 :         for (i = 0; i < npts; i++)
    3496                 :             :         {
    3497                 :           0 :                 poly->p[i].x = pq_getmsgfloat8(buf);
    3498                 :           0 :                 poly->p[i].y = pq_getmsgfloat8(buf);
    3499                 :           0 :         }
    3500                 :             : 
    3501                 :           0 :         make_bound_box(poly);
    3502                 :             : 
    3503                 :           0 :         PG_RETURN_POLYGON_P(poly);
    3504                 :           0 : }
    3505                 :             : 
    3506                 :             : /*
    3507                 :             :  *              poly_send                       - converts polygon to binary format
    3508                 :             :  */
    3509                 :             : Datum
    3510                 :           0 : poly_send(PG_FUNCTION_ARGS)
    3511                 :             : {
    3512                 :           0 :         POLYGON    *poly = PG_GETARG_POLYGON_P(0);
    3513                 :           0 :         StringInfoData buf;
    3514                 :           0 :         int32           i;
    3515                 :             : 
    3516                 :           0 :         pq_begintypsend(&buf);
    3517                 :           0 :         pq_sendint32(&buf, poly->npts);
    3518         [ #  # ]:           0 :         for (i = 0; i < poly->npts; i++)
    3519                 :             :         {
    3520                 :           0 :                 pq_sendfloat8(&buf, poly->p[i].x);
    3521                 :           0 :                 pq_sendfloat8(&buf, poly->p[i].y);
    3522                 :           0 :         }
    3523                 :           0 :         PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
    3524                 :           0 : }
    3525                 :             : 
    3526                 :             : 
    3527                 :             : /*-------------------------------------------------------
    3528                 :             :  * Is polygon A strictly left of polygon B? i.e. is
    3529                 :             :  * the right most point of A left of the left most point
    3530                 :             :  * of B?
    3531                 :             :  *-------------------------------------------------------*/
    3532                 :             : Datum
    3533                 :          49 : poly_left(PG_FUNCTION_ARGS)
    3534                 :             : {
    3535                 :          49 :         POLYGON    *polya = PG_GETARG_POLYGON_P(0);
    3536                 :          49 :         POLYGON    *polyb = PG_GETARG_POLYGON_P(1);
    3537                 :          49 :         bool            result;
    3538                 :             : 
    3539                 :          49 :         result = polya->boundbox.high.x < polyb->boundbox.low.x;
    3540                 :             : 
    3541                 :             :         /*
    3542                 :             :          * Avoid leaking memory for toasted inputs ... needed for rtree indexes
    3543                 :             :          */
    3544         [ -  + ]:          49 :         PG_FREE_IF_COPY(polya, 0);
    3545         [ -  + ]:          49 :         PG_FREE_IF_COPY(polyb, 1);
    3546                 :             : 
    3547                 :          98 :         PG_RETURN_BOOL(result);
    3548                 :          49 : }
    3549                 :             : 
    3550                 :             : /*-------------------------------------------------------
    3551                 :             :  * Is polygon A overlapping or left of polygon B? i.e. is
    3552                 :             :  * the right most point of A at or left of the right most point
    3553                 :             :  * of B?
    3554                 :             :  *-------------------------------------------------------*/
    3555                 :             : Datum
    3556                 :          49 : poly_overleft(PG_FUNCTION_ARGS)
    3557                 :             : {
    3558                 :          49 :         POLYGON    *polya = PG_GETARG_POLYGON_P(0);
    3559                 :          49 :         POLYGON    *polyb = PG_GETARG_POLYGON_P(1);
    3560                 :          49 :         bool            result;
    3561                 :             : 
    3562                 :          49 :         result = polya->boundbox.high.x <= polyb->boundbox.high.x;
    3563                 :             : 
    3564                 :             :         /*
    3565                 :             :          * Avoid leaking memory for toasted inputs ... needed for rtree indexes
    3566                 :             :          */
    3567         [ -  + ]:          49 :         PG_FREE_IF_COPY(polya, 0);
    3568         [ -  + ]:          49 :         PG_FREE_IF_COPY(polyb, 1);
    3569                 :             : 
    3570                 :          98 :         PG_RETURN_BOOL(result);
    3571                 :          49 : }
    3572                 :             : 
    3573                 :             : /*-------------------------------------------------------
    3574                 :             :  * Is polygon A strictly right of polygon B? i.e. is
    3575                 :             :  * the left most point of A right of the right most point
    3576                 :             :  * of B?
    3577                 :             :  *-------------------------------------------------------*/
    3578                 :             : Datum
    3579                 :          49 : poly_right(PG_FUNCTION_ARGS)
    3580                 :             : {
    3581                 :          49 :         POLYGON    *polya = PG_GETARG_POLYGON_P(0);
    3582                 :          49 :         POLYGON    *polyb = PG_GETARG_POLYGON_P(1);
    3583                 :          49 :         bool            result;
    3584                 :             : 
    3585                 :          49 :         result = polya->boundbox.low.x > polyb->boundbox.high.x;
    3586                 :             : 
    3587                 :             :         /*
    3588                 :             :          * Avoid leaking memory for toasted inputs ... needed for rtree indexes
    3589                 :             :          */
    3590         [ -  + ]:          49 :         PG_FREE_IF_COPY(polya, 0);
    3591         [ -  + ]:          49 :         PG_FREE_IF_COPY(polyb, 1);
    3592                 :             : 
    3593                 :          98 :         PG_RETURN_BOOL(result);
    3594                 :          49 : }
    3595                 :             : 
    3596                 :             : /*-------------------------------------------------------
    3597                 :             :  * Is polygon A overlapping or right of polygon B? i.e. is
    3598                 :             :  * the left most point of A at or right of the left most point
    3599                 :             :  * of B?
    3600                 :             :  *-------------------------------------------------------*/
    3601                 :             : Datum
    3602                 :          49 : poly_overright(PG_FUNCTION_ARGS)
    3603                 :             : {
    3604                 :          49 :         POLYGON    *polya = PG_GETARG_POLYGON_P(0);
    3605                 :          49 :         POLYGON    *polyb = PG_GETARG_POLYGON_P(1);
    3606                 :          49 :         bool            result;
    3607                 :             : 
    3608                 :          49 :         result = polya->boundbox.low.x >= polyb->boundbox.low.x;
    3609                 :             : 
    3610                 :             :         /*
    3611                 :             :          * Avoid leaking memory for toasted inputs ... needed for rtree indexes
    3612                 :             :          */
    3613         [ -  + ]:          49 :         PG_FREE_IF_COPY(polya, 0);
    3614         [ -  + ]:          49 :         PG_FREE_IF_COPY(polyb, 1);
    3615                 :             : 
    3616                 :          98 :         PG_RETURN_BOOL(result);
    3617                 :          49 : }
    3618                 :             : 
    3619                 :             : /*-------------------------------------------------------
    3620                 :             :  * Is polygon A strictly below polygon B? i.e. is
    3621                 :             :  * the upper most point of A below the lower most point
    3622                 :             :  * of B?
    3623                 :             :  *-------------------------------------------------------*/
    3624                 :             : Datum
    3625                 :          49 : poly_below(PG_FUNCTION_ARGS)
    3626                 :             : {
    3627                 :          49 :         POLYGON    *polya = PG_GETARG_POLYGON_P(0);
    3628                 :          49 :         POLYGON    *polyb = PG_GETARG_POLYGON_P(1);
    3629                 :          49 :         bool            result;
    3630                 :             : 
    3631                 :          49 :         result = polya->boundbox.high.y < polyb->boundbox.low.y;
    3632                 :             : 
    3633                 :             :         /*
    3634                 :             :          * Avoid leaking memory for toasted inputs ... needed for rtree indexes
    3635                 :             :          */
    3636         [ -  + ]:          49 :         PG_FREE_IF_COPY(polya, 0);
    3637         [ -  + ]:          49 :         PG_FREE_IF_COPY(polyb, 1);
    3638                 :             : 
    3639                 :          98 :         PG_RETURN_BOOL(result);
    3640                 :          49 : }
    3641                 :             : 
    3642                 :             : /*-------------------------------------------------------
    3643                 :             :  * Is polygon A overlapping or below polygon B? i.e. is
    3644                 :             :  * the upper most point of A at or below the upper most point
    3645                 :             :  * of B?
    3646                 :             :  *-------------------------------------------------------*/
    3647                 :             : Datum
    3648                 :          49 : poly_overbelow(PG_FUNCTION_ARGS)
    3649                 :             : {
    3650                 :          49 :         POLYGON    *polya = PG_GETARG_POLYGON_P(0);
    3651                 :          49 :         POLYGON    *polyb = PG_GETARG_POLYGON_P(1);
    3652                 :          49 :         bool            result;
    3653                 :             : 
    3654                 :          49 :         result = polya->boundbox.high.y <= polyb->boundbox.high.y;
    3655                 :             : 
    3656                 :             :         /*
    3657                 :             :          * Avoid leaking memory for toasted inputs ... needed for rtree indexes
    3658                 :             :          */
    3659         [ -  + ]:          49 :         PG_FREE_IF_COPY(polya, 0);
    3660         [ -  + ]:          49 :         PG_FREE_IF_COPY(polyb, 1);
    3661                 :             : 
    3662                 :          98 :         PG_RETURN_BOOL(result);
    3663                 :          49 : }
    3664                 :             : 
    3665                 :             : /*-------------------------------------------------------
    3666                 :             :  * Is polygon A strictly above polygon B? i.e. is
    3667                 :             :  * the lower most point of A above the upper most point
    3668                 :             :  * of B?
    3669                 :             :  *-------------------------------------------------------*/
    3670                 :             : Datum
    3671                 :          49 : poly_above(PG_FUNCTION_ARGS)
    3672                 :             : {
    3673                 :          49 :         POLYGON    *polya = PG_GETARG_POLYGON_P(0);
    3674                 :          49 :         POLYGON    *polyb = PG_GETARG_POLYGON_P(1);
    3675                 :          49 :         bool            result;
    3676                 :             : 
    3677                 :          49 :         result = polya->boundbox.low.y > polyb->boundbox.high.y;
    3678                 :             : 
    3679                 :             :         /*
    3680                 :             :          * Avoid leaking memory for toasted inputs ... needed for rtree indexes
    3681                 :             :          */
    3682         [ -  + ]:          49 :         PG_FREE_IF_COPY(polya, 0);
    3683         [ -  + ]:          49 :         PG_FREE_IF_COPY(polyb, 1);
    3684                 :             : 
    3685                 :          98 :         PG_RETURN_BOOL(result);
    3686                 :          49 : }
    3687                 :             : 
    3688                 :             : /*-------------------------------------------------------
    3689                 :             :  * Is polygon A overlapping or above polygon B? i.e. is
    3690                 :             :  * the lower most point of A at or above the lower most point
    3691                 :             :  * of B?
    3692                 :             :  *-------------------------------------------------------*/
    3693                 :             : Datum
    3694                 :          49 : poly_overabove(PG_FUNCTION_ARGS)
    3695                 :             : {
    3696                 :          49 :         POLYGON    *polya = PG_GETARG_POLYGON_P(0);
    3697                 :          49 :         POLYGON    *polyb = PG_GETARG_POLYGON_P(1);
    3698                 :          49 :         bool            result;
    3699                 :             : 
    3700                 :          49 :         result = polya->boundbox.low.y >= polyb->boundbox.low.y;
    3701                 :             : 
    3702                 :             :         /*
    3703                 :             :          * Avoid leaking memory for toasted inputs ... needed for rtree indexes
    3704                 :             :          */
    3705         [ -  + ]:          49 :         PG_FREE_IF_COPY(polya, 0);
    3706         [ -  + ]:          49 :         PG_FREE_IF_COPY(polyb, 1);
    3707                 :             : 
    3708                 :          98 :         PG_RETURN_BOOL(result);
    3709                 :          49 : }
    3710                 :             : 
    3711                 :             : 
    3712                 :             : /*-------------------------------------------------------
    3713                 :             :  * Is polygon A the same as polygon B? i.e. are all the
    3714                 :             :  * points the same?
    3715                 :             :  * Check all points for matches in both forward and reverse
    3716                 :             :  *      direction since polygons are non-directional and are
    3717                 :             :  *      closed shapes.
    3718                 :             :  *-------------------------------------------------------*/
    3719                 :             : Datum
    3720                 :        1049 : poly_same(PG_FUNCTION_ARGS)
    3721                 :             : {
    3722                 :        1049 :         POLYGON    *polya = PG_GETARG_POLYGON_P(0);
    3723                 :        1049 :         POLYGON    *polyb = PG_GETARG_POLYGON_P(1);
    3724                 :        1049 :         bool            result;
    3725                 :             : 
    3726         [ +  + ]:        1049 :         if (polya->npts != polyb->npts)
    3727                 :          34 :                 result = false;
    3728                 :             :         else
    3729                 :        1015 :                 result = plist_same(polya->npts, polya->p, polyb->p);
    3730                 :             : 
    3731                 :             :         /*
    3732                 :             :          * Avoid leaking memory for toasted inputs ... needed for rtree indexes
    3733                 :             :          */
    3734         [ -  + ]:        1049 :         PG_FREE_IF_COPY(polya, 0);
    3735         [ +  + ]:        1049 :         PG_FREE_IF_COPY(polyb, 1);
    3736                 :             : 
    3737                 :        2098 :         PG_RETURN_BOOL(result);
    3738                 :        1049 : }
    3739                 :             : 
    3740                 :             : /*-----------------------------------------------------------------
    3741                 :             :  * Determine if polygon A overlaps polygon B
    3742                 :             :  *-----------------------------------------------------------------*/
    3743                 :             : static bool
    3744                 :        4903 : poly_overlap_internal(POLYGON *polya, POLYGON *polyb)
    3745                 :             : {
    3746                 :        4903 :         bool            result;
    3747                 :             : 
    3748         [ +  - ]:        4903 :         Assert(polya->npts > 0 && polyb->npts > 0);
    3749                 :             : 
    3750                 :             :         /* Quick check by bounding box */
    3751                 :        4903 :         result = box_ov(&polya->boundbox, &polyb->boundbox);
    3752                 :             : 
    3753                 :             :         /*
    3754                 :             :          * Brute-force algorithm - try to find intersected edges, if so then
    3755                 :             :          * polygons are overlapped else check is one polygon inside other or not
    3756                 :             :          * by testing single point of them.
    3757                 :             :          */
    3758         [ +  + ]:        4903 :         if (result)
    3759                 :             :         {
    3760                 :        1769 :                 int                     ia,
    3761                 :             :                                         ib;
    3762                 :        1769 :                 LSEG            sa,
    3763                 :             :                                         sb;
    3764                 :             : 
    3765                 :             :                 /* Init first of polya's edge with last point */
    3766                 :        1769 :                 sa.p[0] = polya->p[polya->npts - 1];
    3767                 :        1769 :                 result = false;
    3768                 :             : 
    3769   [ +  +  +  + ]:       21091 :                 for (ia = 0; ia < polya->npts && !result; ia++)
    3770                 :             :                 {
    3771                 :             :                         /* Second point of polya's edge is a current one */
    3772                 :       19322 :                         sa.p[1] = polya->p[ia];
    3773                 :             : 
    3774                 :             :                         /* Init first of polyb's edge with last point */
    3775                 :       19322 :                         sb.p[0] = polyb->p[polyb->npts - 1];
    3776                 :             : 
    3777   [ +  +  +  + ]:       96169 :                         for (ib = 0; ib < polyb->npts && !result; ib++)
    3778                 :             :                         {
    3779                 :       76847 :                                 sb.p[1] = polyb->p[ib];
    3780                 :       76847 :                                 result = lseg_interpt_lseg(NULL, &sa, &sb);
    3781                 :       76847 :                                 sb.p[0] = sb.p[1];
    3782                 :       76847 :                         }
    3783                 :             : 
    3784                 :             :                         /*
    3785                 :             :                          * move current endpoint to the first point of next edge
    3786                 :             :                          */
    3787                 :       19322 :                         sa.p[0] = sa.p[1];
    3788                 :       19322 :                 }
    3789                 :             : 
    3790         [ +  + ]:        1769 :                 if (!result)
    3791                 :             :                 {
    3792         [ +  + ]:        1579 :                         result = (point_inside(polya->p, polyb->npts, polyb->p) ||
    3793                 :         738 :                                           point_inside(polyb->p, polya->npts, polya->p));
    3794                 :        1579 :                 }
    3795                 :        1769 :         }
    3796                 :             : 
    3797                 :        9806 :         return result;
    3798                 :        4903 : }
    3799                 :             : 
    3800                 :             : Datum
    3801                 :        4854 : poly_overlap(PG_FUNCTION_ARGS)
    3802                 :             : {
    3803                 :        4854 :         POLYGON    *polya = PG_GETARG_POLYGON_P(0);
    3804                 :        4854 :         POLYGON    *polyb = PG_GETARG_POLYGON_P(1);
    3805                 :        4854 :         bool            result;
    3806                 :             : 
    3807                 :        4854 :         result = poly_overlap_internal(polya, polyb);
    3808                 :             : 
    3809                 :             :         /*
    3810                 :             :          * Avoid leaking memory for toasted inputs ... needed for rtree indexes
    3811                 :             :          */
    3812         [ +  + ]:        4854 :         PG_FREE_IF_COPY(polya, 0);
    3813         [ +  + ]:        4854 :         PG_FREE_IF_COPY(polyb, 1);
    3814                 :             : 
    3815                 :        9708 :         PG_RETURN_BOOL(result);
    3816                 :        4854 : }
    3817                 :             : 
    3818                 :             : /*
    3819                 :             :  * Tests special kind of segment for in/out of polygon.
    3820                 :             :  * Special kind means:
    3821                 :             :  *      - point a should be on segment s
    3822                 :             :  *      - segment (a,b) should not be contained by s
    3823                 :             :  * Returns true if:
    3824                 :             :  *      - segment (a,b) is collinear to s and (a,b) is in polygon
    3825                 :             :  *      - segment (a,b) s not collinear to s. Note: that doesn't
    3826                 :             :  *        mean that segment is in polygon!
    3827                 :             :  */
    3828                 :             : 
    3829                 :             : static bool
    3830                 :         101 : touched_lseg_inside_poly(Point *a, Point *b, LSEG *s, POLYGON *poly, int start)
    3831                 :             : {
    3832                 :             :         /* point a is on s, b is not */
    3833                 :         101 :         LSEG            t;
    3834                 :             : 
    3835                 :         101 :         t.p[0] = *a;
    3836                 :         101 :         t.p[1] = *b;
    3837                 :             : 
    3838         [ +  + ]:         101 :         if (point_eq_point(a, s->p))
    3839                 :             :         {
    3840         [ -  + ]:          12 :                 if (lseg_contain_point(&t, s->p + 1))
    3841                 :           0 :                         return lseg_inside_poly(b, s->p + 1, poly, start);
    3842                 :          12 :         }
    3843         [ +  + ]:          89 :         else if (point_eq_point(a, s->p + 1))
    3844                 :             :         {
    3845         [ -  + ]:          26 :                 if (lseg_contain_point(&t, s->p))
    3846                 :           0 :                         return lseg_inside_poly(b, s->p, poly, start);
    3847                 :          26 :         }
    3848         [ -  + ]:          63 :         else if (lseg_contain_point(&t, s->p))
    3849                 :             :         {
    3850                 :           0 :                 return lseg_inside_poly(b, s->p, poly, start);
    3851                 :             :         }
    3852         [ -  + ]:          63 :         else if (lseg_contain_point(&t, s->p + 1))
    3853                 :             :         {
    3854                 :           0 :                 return lseg_inside_poly(b, s->p + 1, poly, start);
    3855                 :             :         }
    3856                 :             : 
    3857                 :         101 :         return true;                            /* may be not true, but that will check later */
    3858                 :         101 : }
    3859                 :             : 
    3860                 :             : /*
    3861                 :             :  * Returns true if segment (a,b) is in polygon, option
    3862                 :             :  * start is used for optimization - function checks
    3863                 :             :  * polygon's edges starting from start
    3864                 :             :  */
    3865                 :             : static bool
    3866                 :       33046 : lseg_inside_poly(Point *a, Point *b, POLYGON *poly, int start)
    3867                 :             : {
    3868                 :       33046 :         LSEG            s,
    3869                 :             :                                 t;
    3870                 :       33046 :         int                     i;
    3871                 :       33046 :         bool            res = true,
    3872                 :       33046 :                                 intersection = false;
    3873                 :             : 
    3874                 :             :         /* since this function recurses, it could be driven to stack overflow */
    3875                 :       33046 :         check_stack_depth();
    3876                 :             : 
    3877                 :       33046 :         t.p[0] = *a;
    3878                 :       33046 :         t.p[1] = *b;
    3879         [ +  + ]:       33046 :         s.p[0] = poly->p[(start == 0) ? (poly->npts - 1) : (start - 1)];
    3880                 :             : 
    3881   [ +  +  +  + ]:      163449 :         for (i = start; i < poly->npts && res; i++)
    3882                 :             :         {
    3883                 :      130481 :                 Point           interpt;
    3884                 :             : 
    3885         [ +  - ]:      130481 :                 CHECK_FOR_INTERRUPTS();
    3886                 :             : 
    3887                 :      130481 :                 s.p[1] = poly->p[i];
    3888                 :             : 
    3889         [ +  + ]:      130481 :                 if (lseg_contain_point(&s, t.p))
    3890                 :             :                 {
    3891         [ +  + ]:         131 :                         if (lseg_contain_point(&s, t.p + 1))
    3892                 :          78 :                                 return true;    /* t is contained by s */
    3893                 :             : 
    3894                 :             :                         /* Y-cross */
    3895                 :          53 :                         res = touched_lseg_inside_poly(t.p, t.p + 1, &s, poly, i + 1);
    3896                 :          53 :                 }
    3897         [ +  + ]:      130350 :                 else if (lseg_contain_point(&s, t.p + 1))
    3898                 :             :                 {
    3899                 :             :                         /* Y-cross */
    3900                 :          48 :                         res = touched_lseg_inside_poly(t.p + 1, t.p, &s, poly, i + 1);
    3901                 :          48 :                 }
    3902         [ +  + ]:      130302 :                 else if (lseg_interpt_lseg(&interpt, &t, &s))
    3903                 :             :                 {
    3904                 :             :                         /*
    3905                 :             :                          * segments are X-crossing, go to check each subsegment
    3906                 :             :                          */
    3907                 :             : 
    3908                 :         225 :                         intersection = true;
    3909                 :         225 :                         res = lseg_inside_poly(t.p, &interpt, poly, i + 1);
    3910         [ +  + ]:         225 :                         if (res)
    3911                 :         193 :                                 res = lseg_inside_poly(t.p + 1, &interpt, poly, i + 1);
    3912                 :         225 :                 }
    3913                 :             : 
    3914                 :      130403 :                 s.p[0] = s.p[1];
    3915         [ +  + ]:      130481 :         }
    3916                 :             : 
    3917   [ +  +  -  + ]:       32968 :         if (res && !intersection)
    3918                 :             :         {
    3919                 :       32743 :                 Point           p;
    3920                 :             : 
    3921                 :             :                 /*
    3922                 :             :                  * if X-intersection wasn't found, then check central point of tested
    3923                 :             :                  * segment. In opposite case we already check all subsegments
    3924                 :             :                  */
    3925                 :       32743 :                 p.x = float8_div(float8_pl(t.p[0].x, t.p[1].x), 2.0);
    3926                 :       32743 :                 p.y = float8_div(float8_pl(t.p[0].y, t.p[1].y), 2.0);
    3927                 :             : 
    3928                 :       32743 :                 res = point_inside(&p, poly->npts, poly->p);
    3929                 :       32743 :         }
    3930                 :             : 
    3931                 :       32968 :         return res;
    3932                 :       33046 : }
    3933                 :             : 
    3934                 :             : /*
    3935                 :             :  * Check whether the first polygon contains the second
    3936                 :             :  */
    3937                 :             : static bool
    3938                 :       14155 : poly_contain_poly(POLYGON *contains_poly, POLYGON *contained_poly)
    3939                 :             : {
    3940                 :       14155 :         int                     i;
    3941                 :       14155 :         LSEG            s;
    3942                 :             : 
    3943         [ +  - ]:       14155 :         Assert(contains_poly->npts > 0 && contained_poly->npts > 0);
    3944                 :             : 
    3945                 :             :         /*
    3946                 :             :          * Quick check to see if contained's bounding box is contained in
    3947                 :             :          * contains' bb.
    3948                 :             :          */
    3949         [ +  + ]:       14155 :         if (!box_contain_box(&contains_poly->boundbox, &contained_poly->boundbox))
    3950                 :        9557 :                 return false;
    3951                 :             : 
    3952                 :        4598 :         s.p[0] = contained_poly->p[contained_poly->npts - 1];
    3953                 :             : 
    3954         [ +  + ]:       35148 :         for (i = 0; i < contained_poly->npts; i++)
    3955                 :             :         {
    3956                 :       32628 :                 s.p[1] = contained_poly->p[i];
    3957         [ +  + ]:       32628 :                 if (!lseg_inside_poly(s.p, s.p + 1, contains_poly, 0))
    3958                 :        2078 :                         return false;
    3959                 :       30550 :                 s.p[0] = s.p[1];
    3960                 :       30550 :         }
    3961                 :             : 
    3962                 :        2520 :         return true;
    3963                 :       14155 : }
    3964                 :             : 
    3965                 :             : Datum
    3966                 :          64 : poly_contain(PG_FUNCTION_ARGS)
    3967                 :             : {
    3968                 :          64 :         POLYGON    *polya = PG_GETARG_POLYGON_P(0);
    3969                 :          64 :         POLYGON    *polyb = PG_GETARG_POLYGON_P(1);
    3970                 :          64 :         bool            result;
    3971                 :             : 
    3972                 :          64 :         result = poly_contain_poly(polya, polyb);
    3973                 :             : 
    3974                 :             :         /*
    3975                 :             :          * Avoid leaking memory for toasted inputs ... needed for rtree indexes
    3976                 :             :          */
    3977         [ +  + ]:          64 :         PG_FREE_IF_COPY(polya, 0);
    3978         [ +  + ]:          64 :         PG_FREE_IF_COPY(polyb, 1);
    3979                 :             : 
    3980                 :         128 :         PG_RETURN_BOOL(result);
    3981                 :          64 : }
    3982                 :             : 
    3983                 :             : 
    3984                 :             : /*-----------------------------------------------------------------
    3985                 :             :  * Determine if polygon A is contained by polygon B
    3986                 :             :  *-----------------------------------------------------------------*/
    3987                 :             : Datum
    3988                 :       14091 : poly_contained(PG_FUNCTION_ARGS)
    3989                 :             : {
    3990                 :       14091 :         POLYGON    *polya = PG_GETARG_POLYGON_P(0);
    3991                 :       14091 :         POLYGON    *polyb = PG_GETARG_POLYGON_P(1);
    3992                 :       14091 :         bool            result;
    3993                 :             : 
    3994                 :             :         /* Just switch the arguments and pass it off to poly_contain */
    3995                 :       14091 :         result = poly_contain_poly(polyb, polya);
    3996                 :             : 
    3997                 :             :         /*
    3998                 :             :          * Avoid leaking memory for toasted inputs ... needed for rtree indexes
    3999                 :             :          */
    4000         [ +  + ]:       14091 :         PG_FREE_IF_COPY(polya, 0);
    4001         [ +  + ]:       14091 :         PG_FREE_IF_COPY(polyb, 1);
    4002                 :             : 
    4003                 :       28182 :         PG_RETURN_BOOL(result);
    4004                 :       14091 : }
    4005                 :             : 
    4006                 :             : 
    4007                 :             : Datum
    4008                 :          74 : poly_contain_pt(PG_FUNCTION_ARGS)
    4009                 :             : {
    4010                 :          74 :         POLYGON    *poly = PG_GETARG_POLYGON_P(0);
    4011                 :          74 :         Point      *p = PG_GETARG_POINT_P(1);
    4012                 :             : 
    4013                 :         148 :         PG_RETURN_BOOL(point_inside(p, poly->npts, poly->p) != 0);
    4014                 :          74 : }
    4015                 :             : 
    4016                 :             : Datum
    4017                 :          80 : pt_contained_poly(PG_FUNCTION_ARGS)
    4018                 :             : {
    4019                 :          80 :         Point      *p = PG_GETARG_POINT_P(0);
    4020                 :          80 :         POLYGON    *poly = PG_GETARG_POLYGON_P(1);
    4021                 :             : 
    4022                 :         160 :         PG_RETURN_BOOL(point_inside(p, poly->npts, poly->p) != 0);
    4023                 :          80 : }
    4024                 :             : 
    4025                 :             : 
    4026                 :             : Datum
    4027                 :          49 : poly_distance(PG_FUNCTION_ARGS)
    4028                 :             : {
    4029                 :          49 :         POLYGON    *polya = PG_GETARG_POLYGON_P(0);
    4030                 :          49 :         POLYGON    *polyb = PG_GETARG_POLYGON_P(1);
    4031                 :          49 :         float8          min = 0.0;              /* initialize to keep compiler quiet */
    4032                 :          49 :         bool            have_min = false;
    4033                 :          49 :         float8          tmp;
    4034                 :          49 :         int                     i,
    4035                 :             :                                 j;
    4036                 :          49 :         LSEG            seg1,
    4037                 :             :                                 seg2;
    4038                 :             : 
    4039                 :             :         /*
    4040                 :             :          * Distance is zero if polygons overlap.  We must check this because the
    4041                 :             :          * path distance will not give the right answer if one poly is entirely
    4042                 :             :          * within the other.
    4043                 :             :          */
    4044         [ +  + ]:          49 :         if (poly_overlap_internal(polya, polyb))
    4045                 :          25 :                 PG_RETURN_FLOAT8(0.0);
    4046                 :             : 
    4047                 :             :         /*
    4048                 :             :          * When they don't overlap, the distance calculation is identical to that
    4049                 :             :          * for closed paths (i.e., we needn't care about the fact that polygons
    4050                 :             :          * include their contained areas).  See path_distance().
    4051                 :             :          */
    4052         [ +  + ]:          88 :         for (i = 0; i < polya->npts; i++)
    4053                 :             :         {
    4054                 :          64 :                 int                     iprev;
    4055                 :             : 
    4056         [ +  + ]:          64 :                 if (i > 0)
    4057                 :          40 :                         iprev = i - 1;
    4058                 :             :                 else
    4059                 :          24 :                         iprev = polya->npts - 1;
    4060                 :             : 
    4061         [ +  + ]:         218 :                 for (j = 0; j < polyb->npts; j++)
    4062                 :             :                 {
    4063                 :         154 :                         int                     jprev;
    4064                 :             : 
    4065         [ +  + ]:         154 :                         if (j > 0)
    4066                 :          90 :                                 jprev = j - 1;
    4067                 :             :                         else
    4068                 :          64 :                                 jprev = polyb->npts - 1;
    4069                 :             : 
    4070                 :         154 :                         statlseg_construct(&seg1, &polya->p[iprev], &polya->p[i]);
    4071                 :         154 :                         statlseg_construct(&seg2, &polyb->p[jprev], &polyb->p[j]);
    4072                 :             : 
    4073                 :         154 :                         tmp = lseg_closept_lseg(NULL, &seg1, &seg2);
    4074   [ +  +  +  + ]:         154 :                         if (!have_min || float8_lt(tmp, min))
    4075                 :             :                         {
    4076                 :          32 :                                 min = tmp;
    4077                 :          32 :                                 have_min = true;
    4078                 :          32 :                         }
    4079                 :         154 :                 }
    4080                 :          64 :         }
    4081                 :             : 
    4082         [ +  - ]:          24 :         if (!have_min)
    4083                 :           0 :                 PG_RETURN_NULL();
    4084                 :             : 
    4085                 :          24 :         PG_RETURN_FLOAT8(min);
    4086                 :          49 : }
    4087                 :             : 
    4088                 :             : 
    4089                 :             : /***********************************************************************
    4090                 :             :  **
    4091                 :             :  **             Routines for 2D points.
    4092                 :             :  **
    4093                 :             :  ***********************************************************************/
    4094                 :             : 
    4095                 :             : Datum
    4096                 :      161633 : construct_point(PG_FUNCTION_ARGS)
    4097                 :             : {
    4098                 :      161633 :         float8          x = PG_GETARG_FLOAT8(0);
    4099                 :      161633 :         float8          y = PG_GETARG_FLOAT8(1);
    4100                 :      161633 :         Point      *result;
    4101                 :             : 
    4102                 :      161633 :         result = palloc_object(Point);
    4103                 :             : 
    4104                 :      161633 :         point_construct(result, x, y);
    4105                 :             : 
    4106                 :      323266 :         PG_RETURN_POINT_P(result);
    4107                 :      161633 : }
    4108                 :             : 
    4109                 :             : 
    4110                 :             : static inline void
    4111                 :         512 : point_add_point(Point *result, Point *pt1, Point *pt2)
    4112                 :             : {
    4113                 :        1024 :         point_construct(result,
    4114                 :         512 :                                         float8_pl(pt1->x, pt2->x),
    4115                 :         512 :                                         float8_pl(pt1->y, pt2->y));
    4116                 :         512 : }
    4117                 :             : 
    4118                 :             : Datum
    4119                 :         100 : point_add(PG_FUNCTION_ARGS)
    4120                 :             : {
    4121                 :         100 :         Point      *p1 = PG_GETARG_POINT_P(0);
    4122                 :         100 :         Point      *p2 = PG_GETARG_POINT_P(1);
    4123                 :         100 :         Point      *result;
    4124                 :             : 
    4125                 :         100 :         result = palloc_object(Point);
    4126                 :             : 
    4127                 :         100 :         point_add_point(result, p1, p2);
    4128                 :             : 
    4129                 :         200 :         PG_RETURN_POINT_P(result);
    4130                 :         100 : }
    4131                 :             : 
    4132                 :             : 
    4133                 :             : static inline void
    4134                 :         470 : point_sub_point(Point *result, Point *pt1, Point *pt2)
    4135                 :             : {
    4136                 :         940 :         point_construct(result,
    4137                 :         470 :                                         float8_mi(pt1->x, pt2->x),
    4138                 :         470 :                                         float8_mi(pt1->y, pt2->y));
    4139                 :         470 : }
    4140                 :             : 
    4141                 :             : Datum
    4142                 :         100 : point_sub(PG_FUNCTION_ARGS)
    4143                 :             : {
    4144                 :         100 :         Point      *p1 = PG_GETARG_POINT_P(0);
    4145                 :         100 :         Point      *p2 = PG_GETARG_POINT_P(1);
    4146                 :         100 :         Point      *result;
    4147                 :             : 
    4148                 :         100 :         result = palloc_object(Point);
    4149                 :             : 
    4150                 :         100 :         point_sub_point(result, p1, p2);
    4151                 :             : 
    4152                 :         200 :         PG_RETURN_POINT_P(result);
    4153                 :         100 : }
    4154                 :             : 
    4155                 :             : 
    4156                 :             : static inline void
    4157                 :         370 : point_mul_point(Point *result, Point *pt1, Point *pt2)
    4158                 :             : {
    4159                 :         740 :         point_construct(result,
    4160                 :         740 :                                         float8_mi(float8_mul(pt1->x, pt2->x),
    4161                 :         370 :                                                           float8_mul(pt1->y, pt2->y)),
    4162                 :         740 :                                         float8_pl(float8_mul(pt1->x, pt2->y),
    4163                 :         370 :                                                           float8_mul(pt1->y, pt2->x)));
    4164                 :         370 : }
    4165                 :             : 
    4166                 :             : Datum
    4167                 :          50 : point_mul(PG_FUNCTION_ARGS)
    4168                 :             : {
    4169                 :          50 :         Point      *p1 = PG_GETARG_POINT_P(0);
    4170                 :          50 :         Point      *p2 = PG_GETARG_POINT_P(1);
    4171                 :          50 :         Point      *result;
    4172                 :             : 
    4173                 :          50 :         result = palloc_object(Point);
    4174                 :             : 
    4175                 :          50 :         point_mul_point(result, p1, p2);
    4176                 :             : 
    4177                 :         100 :         PG_RETURN_POINT_P(result);
    4178                 :          50 : }
    4179                 :             : 
    4180                 :             : 
    4181                 :             : static inline void
    4182                 :          99 : point_div_point(Point *result, Point *pt1, Point *pt2)
    4183                 :             : {
    4184                 :          99 :         float8          div;
    4185                 :             : 
    4186                 :          99 :         div = float8_pl(float8_mul(pt2->x, pt2->x), float8_mul(pt2->y, pt2->y));
    4187                 :             : 
    4188                 :         198 :         point_construct(result,
    4189                 :         297 :                                         float8_div(float8_pl(float8_mul(pt1->x, pt2->x),
    4190                 :         198 :                                                                                  float8_mul(pt1->y, pt2->y)), div),
    4191                 :         297 :                                         float8_div(float8_mi(float8_mul(pt1->y, pt2->x),
    4192                 :         198 :                                                                                  float8_mul(pt1->x, pt2->y)), div));
    4193                 :          99 : }
    4194                 :             : 
    4195                 :             : Datum
    4196                 :          22 : point_div(PG_FUNCTION_ARGS)
    4197                 :             : {
    4198                 :          22 :         Point      *p1 = PG_GETARG_POINT_P(0);
    4199                 :          22 :         Point      *p2 = PG_GETARG_POINT_P(1);
    4200                 :          22 :         Point      *result;
    4201                 :             : 
    4202                 :          22 :         result = palloc_object(Point);
    4203                 :             : 
    4204                 :          22 :         point_div_point(result, p1, p2);
    4205                 :             : 
    4206                 :          44 :         PG_RETURN_POINT_P(result);
    4207                 :          22 : }
    4208                 :             : 
    4209                 :             : 
    4210                 :             : /***********************************************************************
    4211                 :             :  **
    4212                 :             :  **             Routines for 2D boxes.
    4213                 :             :  **
    4214                 :             :  ***********************************************************************/
    4215                 :             : 
    4216                 :             : Datum
    4217                 :       40244 : points_box(PG_FUNCTION_ARGS)
    4218                 :             : {
    4219                 :       40244 :         Point      *p1 = PG_GETARG_POINT_P(0);
    4220                 :       40244 :         Point      *p2 = PG_GETARG_POINT_P(1);
    4221                 :       40244 :         BOX                *result;
    4222                 :             : 
    4223                 :       40244 :         result = palloc_object(BOX);
    4224                 :             : 
    4225                 :       40244 :         box_construct(result, p1, p2);
    4226                 :             : 
    4227                 :       80488 :         PG_RETURN_BOX_P(result);
    4228                 :       40244 : }
    4229                 :             : 
    4230                 :             : Datum
    4231                 :          50 : box_add(PG_FUNCTION_ARGS)
    4232                 :             : {
    4233                 :          50 :         BOX                *box = PG_GETARG_BOX_P(0);
    4234                 :          50 :         Point      *p = PG_GETARG_POINT_P(1);
    4235                 :          50 :         BOX                *result;
    4236                 :             : 
    4237                 :          50 :         result = palloc_object(BOX);
    4238                 :             : 
    4239                 :          50 :         point_add_point(&result->high, &box->high, p);
    4240                 :          50 :         point_add_point(&result->low, &box->low, p);
    4241                 :             : 
    4242                 :         100 :         PG_RETURN_BOX_P(result);
    4243                 :          50 : }
    4244                 :             : 
    4245                 :             : Datum
    4246                 :          50 : box_sub(PG_FUNCTION_ARGS)
    4247                 :             : {
    4248                 :          50 :         BOX                *box = PG_GETARG_BOX_P(0);
    4249                 :          50 :         Point      *p = PG_GETARG_POINT_P(1);
    4250                 :          50 :         BOX                *result;
    4251                 :             : 
    4252                 :          50 :         result = palloc_object(BOX);
    4253                 :             : 
    4254                 :          50 :         point_sub_point(&result->high, &box->high, p);
    4255                 :          50 :         point_sub_point(&result->low, &box->low, p);
    4256                 :             : 
    4257                 :         100 :         PG_RETURN_BOX_P(result);
    4258                 :          50 : }
    4259                 :             : 
    4260                 :             : Datum
    4261                 :          25 : box_mul(PG_FUNCTION_ARGS)
    4262                 :             : {
    4263                 :          25 :         BOX                *box = PG_GETARG_BOX_P(0);
    4264                 :          25 :         Point      *p = PG_GETARG_POINT_P(1);
    4265                 :          25 :         BOX                *result;
    4266                 :          25 :         Point           high,
    4267                 :             :                                 low;
    4268                 :             : 
    4269                 :          25 :         result = palloc_object(BOX);
    4270                 :             : 
    4271                 :          25 :         point_mul_point(&high, &box->high, p);
    4272                 :          25 :         point_mul_point(&low, &box->low, p);
    4273                 :             : 
    4274                 :          25 :         box_construct(result, &high, &low);
    4275                 :             : 
    4276                 :          50 :         PG_RETURN_BOX_P(result);
    4277                 :          25 : }
    4278                 :             : 
    4279                 :             : Datum
    4280                 :          10 : box_div(PG_FUNCTION_ARGS)
    4281                 :             : {
    4282                 :          10 :         BOX                *box = PG_GETARG_BOX_P(0);
    4283                 :          10 :         Point      *p = PG_GETARG_POINT_P(1);
    4284                 :          10 :         BOX                *result;
    4285                 :          10 :         Point           high,
    4286                 :             :                                 low;
    4287                 :             : 
    4288                 :          10 :         result = palloc_object(BOX);
    4289                 :             : 
    4290                 :          10 :         point_div_point(&high, &box->high, p);
    4291                 :          10 :         point_div_point(&low, &box->low, p);
    4292                 :             : 
    4293                 :          10 :         box_construct(result, &high, &low);
    4294                 :             : 
    4295                 :          20 :         PG_RETURN_BOX_P(result);
    4296                 :          10 : }
    4297                 :             : 
    4298                 :             : /*
    4299                 :             :  * Convert point to empty box
    4300                 :             :  */
    4301                 :             : Datum
    4302                 :          61 : point_box(PG_FUNCTION_ARGS)
    4303                 :             : {
    4304                 :          61 :         Point      *pt = PG_GETARG_POINT_P(0);
    4305                 :          61 :         BOX                *box;
    4306                 :             : 
    4307                 :          61 :         box = palloc_object(BOX);
    4308                 :             : 
    4309                 :          61 :         box->high.x = pt->x;
    4310                 :          61 :         box->low.x = pt->x;
    4311                 :          61 :         box->high.y = pt->y;
    4312                 :          61 :         box->low.y = pt->y;
    4313                 :             : 
    4314                 :         122 :         PG_RETURN_BOX_P(box);
    4315                 :          61 : }
    4316                 :             : 
    4317                 :             : /*
    4318                 :             :  * Smallest bounding box that includes both of the given boxes
    4319                 :             :  */
    4320                 :             : Datum
    4321                 :          25 : boxes_bound_box(PG_FUNCTION_ARGS)
    4322                 :             : {
    4323                 :          50 :         BOX                *box1 = PG_GETARG_BOX_P(0),
    4324                 :          25 :                            *box2 = PG_GETARG_BOX_P(1),
    4325                 :             :                            *container;
    4326                 :             : 
    4327                 :          25 :         container = palloc_object(BOX);
    4328                 :             : 
    4329                 :          25 :         container->high.x = float8_max(box1->high.x, box2->high.x);
    4330                 :          25 :         container->low.x = float8_min(box1->low.x, box2->low.x);
    4331                 :          25 :         container->high.y = float8_max(box1->high.y, box2->high.y);
    4332                 :          25 :         container->low.y = float8_min(box1->low.y, box2->low.y);
    4333                 :             : 
    4334                 :          50 :         PG_RETURN_BOX_P(container);
    4335                 :          25 : }
    4336                 :             : 
    4337                 :             : 
    4338                 :             : /***********************************************************************
    4339                 :             :  **
    4340                 :             :  **             Routines for 2D paths.
    4341                 :             :  **
    4342                 :             :  ***********************************************************************/
    4343                 :             : 
    4344                 :             : /* path_add()
    4345                 :             :  * Concatenate two paths (only if they are both open).
    4346                 :             :  */
    4347                 :             : Datum
    4348                 :          81 : path_add(PG_FUNCTION_ARGS)
    4349                 :             : {
    4350                 :          81 :         PATH       *p1 = PG_GETARG_PATH_P(0);
    4351                 :          81 :         PATH       *p2 = PG_GETARG_PATH_P(1);
    4352                 :          81 :         PATH       *result;
    4353                 :          81 :         int                     size,
    4354                 :             :                                 base_size;
    4355                 :          81 :         int                     i;
    4356                 :             : 
    4357   [ +  +  +  + ]:          81 :         if (p1->closed || p2->closed)
    4358                 :          65 :                 PG_RETURN_NULL();
    4359                 :             : 
    4360                 :          16 :         base_size = sizeof(p1->p[0]) * (p1->npts + p2->npts);
    4361                 :          16 :         size = offsetof(PATH, p) + base_size;
    4362                 :             : 
    4363                 :             :         /* Check for integer overflow */
    4364         [ +  - ]:          16 :         if (base_size / sizeof(p1->p[0]) != (p1->npts + p2->npts) ||
    4365                 :          16 :                 size <= base_size)
    4366   [ #  #  #  # ]:           0 :                 ereport(ERROR,
    4367                 :             :                                 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
    4368                 :             :                                  errmsg("too many points requested")));
    4369                 :             : 
    4370                 :          16 :         result = (PATH *) palloc(size);
    4371                 :             : 
    4372                 :          16 :         SET_VARSIZE(result, size);
    4373                 :          16 :         result->npts = (p1->npts + p2->npts);
    4374                 :          16 :         result->closed = p1->closed;
    4375                 :             :         /* prevent instability in unused pad bytes */
    4376                 :          16 :         result->dummy = 0;
    4377                 :             : 
    4378         [ +  + ]:          56 :         for (i = 0; i < p1->npts; i++)
    4379                 :             :         {
    4380                 :          40 :                 result->p[i].x = p1->p[i].x;
    4381                 :          40 :                 result->p[i].y = p1->p[i].y;
    4382                 :          40 :         }
    4383         [ +  + ]:          56 :         for (i = 0; i < p2->npts; i++)
    4384                 :             :         {
    4385                 :          40 :                 result->p[i + p1->npts].x = p2->p[i].x;
    4386                 :          40 :                 result->p[i + p1->npts].y = p2->p[i].y;
    4387                 :          40 :         }
    4388                 :             : 
    4389                 :          16 :         PG_RETURN_PATH_P(result);
    4390                 :          81 : }
    4391                 :             : 
    4392                 :             : /* path_add_pt()
    4393                 :             :  * Translation operators.
    4394                 :             :  */
    4395                 :             : Datum
    4396                 :          90 : path_add_pt(PG_FUNCTION_ARGS)
    4397                 :             : {
    4398                 :          90 :         PATH       *path = PG_GETARG_PATH_P_COPY(0);
    4399                 :          90 :         Point      *point = PG_GETARG_POINT_P(1);
    4400                 :          90 :         int                     i;
    4401                 :             : 
    4402         [ +  + ]:         280 :         for (i = 0; i < path->npts; i++)
    4403                 :         190 :                 point_add_point(&path->p[i], &path->p[i], point);
    4404                 :             : 
    4405                 :         180 :         PG_RETURN_PATH_P(path);
    4406                 :          90 : }
    4407                 :             : 
    4408                 :             : Datum
    4409                 :          90 : path_sub_pt(PG_FUNCTION_ARGS)
    4410                 :             : {
    4411                 :          90 :         PATH       *path = PG_GETARG_PATH_P_COPY(0);
    4412                 :          90 :         Point      *point = PG_GETARG_POINT_P(1);
    4413                 :          90 :         int                     i;
    4414                 :             : 
    4415         [ +  + ]:         280 :         for (i = 0; i < path->npts; i++)
    4416                 :         190 :                 point_sub_point(&path->p[i], &path->p[i], point);
    4417                 :             : 
    4418                 :         180 :         PG_RETURN_PATH_P(path);
    4419                 :          90 : }
    4420                 :             : 
    4421                 :             : /* path_mul_pt()
    4422                 :             :  * Rotation and scaling operators.
    4423                 :             :  */
    4424                 :             : Datum
    4425                 :          90 : path_mul_pt(PG_FUNCTION_ARGS)
    4426                 :             : {
    4427                 :          90 :         PATH       *path = PG_GETARG_PATH_P_COPY(0);
    4428                 :          90 :         Point      *point = PG_GETARG_POINT_P(1);
    4429                 :          90 :         int                     i;
    4430                 :             : 
    4431         [ +  + ]:         280 :         for (i = 0; i < path->npts; i++)
    4432                 :         190 :                 point_mul_point(&path->p[i], &path->p[i], point);
    4433                 :             : 
    4434                 :         180 :         PG_RETURN_PATH_P(path);
    4435                 :          90 : }
    4436                 :             : 
    4437                 :             : Datum
    4438                 :          19 : path_div_pt(PG_FUNCTION_ARGS)
    4439                 :             : {
    4440                 :          19 :         PATH       *path = PG_GETARG_PATH_P_COPY(0);
    4441                 :          19 :         Point      *point = PG_GETARG_POINT_P(1);
    4442                 :          19 :         int                     i;
    4443                 :             : 
    4444         [ +  + ]:          57 :         for (i = 0; i < path->npts; i++)
    4445                 :          38 :                 point_div_point(&path->p[i], &path->p[i], point);
    4446                 :             : 
    4447                 :          38 :         PG_RETURN_PATH_P(path);
    4448                 :          19 : }
    4449                 :             : 
    4450                 :             : 
    4451                 :             : Datum
    4452                 :          15 : path_poly(PG_FUNCTION_ARGS)
    4453                 :             : {
    4454                 :          15 :         PATH       *path = PG_GETARG_PATH_P(0);
    4455                 :          15 :         POLYGON    *poly;
    4456                 :          15 :         int                     size;
    4457                 :          15 :         int                     i;
    4458                 :             : 
    4459                 :             :         /* This is not very consistent --- other similar cases return NULL ... */
    4460         [ +  + ]:          15 :         if (!path->closed)
    4461   [ +  -  +  - ]:           1 :                 ereport(ERROR,
    4462                 :             :                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    4463                 :             :                                  errmsg("open path cannot be converted to polygon")));
    4464                 :             : 
    4465                 :             :         /*
    4466                 :             :          * Never overflows: the old size fit in MaxAllocSize, and the new size is
    4467                 :             :          * just a small constant larger.
    4468                 :             :          */
    4469                 :          14 :         size = offsetof(POLYGON, p) + sizeof(poly->p[0]) * path->npts;
    4470                 :          14 :         poly = (POLYGON *) palloc(size);
    4471                 :             : 
    4472                 :          14 :         SET_VARSIZE(poly, size);
    4473                 :          14 :         poly->npts = path->npts;
    4474                 :             : 
    4475         [ +  + ]:          42 :         for (i = 0; i < path->npts; i++)
    4476                 :             :         {
    4477                 :          28 :                 poly->p[i].x = path->p[i].x;
    4478                 :          28 :                 poly->p[i].y = path->p[i].y;
    4479                 :          28 :         }
    4480                 :             : 
    4481                 :          14 :         make_bound_box(poly);
    4482                 :             : 
    4483                 :          28 :         PG_RETURN_POLYGON_P(poly);
    4484                 :          14 : }
    4485                 :             : 
    4486                 :             : 
    4487                 :             : /***********************************************************************
    4488                 :             :  **
    4489                 :             :  **             Routines for 2D polygons.
    4490                 :             :  **
    4491                 :             :  ***********************************************************************/
    4492                 :             : 
    4493                 :             : Datum
    4494                 :          21 : poly_npoints(PG_FUNCTION_ARGS)
    4495                 :             : {
    4496                 :          21 :         POLYGON    *poly = PG_GETARG_POLYGON_P(0);
    4497                 :             : 
    4498                 :          42 :         PG_RETURN_INT32(poly->npts);
    4499                 :          21 : }
    4500                 :             : 
    4501                 :             : 
    4502                 :             : Datum
    4503                 :           7 : poly_center(PG_FUNCTION_ARGS)
    4504                 :             : {
    4505                 :           7 :         POLYGON    *poly = PG_GETARG_POLYGON_P(0);
    4506                 :           7 :         Point      *result;
    4507                 :           7 :         CIRCLE          circle;
    4508                 :             : 
    4509                 :           7 :         result = palloc_object(Point);
    4510                 :             : 
    4511                 :           7 :         poly_to_circle(&circle, poly);
    4512                 :           7 :         *result = circle.center;
    4513                 :             : 
    4514                 :          14 :         PG_RETURN_POINT_P(result);
    4515                 :           7 : }
    4516                 :             : 
    4517                 :             : 
    4518                 :             : Datum
    4519                 :           7 : poly_box(PG_FUNCTION_ARGS)
    4520                 :             : {
    4521                 :           7 :         POLYGON    *poly = PG_GETARG_POLYGON_P(0);
    4522                 :           7 :         BOX                *box;
    4523                 :             : 
    4524                 :           7 :         box = palloc_object(BOX);
    4525                 :           7 :         *box = poly->boundbox;
    4526                 :             : 
    4527                 :          14 :         PG_RETURN_BOX_P(box);
    4528                 :           7 : }
    4529                 :             : 
    4530                 :             : 
    4531                 :             : /* box_poly()
    4532                 :             :  * Convert a box to a polygon.
    4533                 :             :  */
    4534                 :             : Datum
    4535                 :        3105 : box_poly(PG_FUNCTION_ARGS)
    4536                 :             : {
    4537                 :        3105 :         BOX                *box = PG_GETARG_BOX_P(0);
    4538                 :        3105 :         POLYGON    *poly;
    4539                 :        3105 :         int                     size;
    4540                 :             : 
    4541                 :             :         /* map four corners of the box to a polygon */
    4542                 :        3105 :         size = offsetof(POLYGON, p) + sizeof(poly->p[0]) * 4;
    4543                 :        3105 :         poly = (POLYGON *) palloc(size);
    4544                 :             : 
    4545                 :        3105 :         SET_VARSIZE(poly, size);
    4546                 :        3105 :         poly->npts = 4;
    4547                 :             : 
    4548                 :        3105 :         poly->p[0].x = box->low.x;
    4549                 :        3105 :         poly->p[0].y = box->low.y;
    4550                 :        3105 :         poly->p[1].x = box->low.x;
    4551                 :        3105 :         poly->p[1].y = box->high.y;
    4552                 :        3105 :         poly->p[2].x = box->high.x;
    4553                 :        3105 :         poly->p[2].y = box->high.y;
    4554                 :        3105 :         poly->p[3].x = box->high.x;
    4555                 :        3105 :         poly->p[3].y = box->low.y;
    4556                 :             : 
    4557                 :        3105 :         box_construct(&poly->boundbox, &box->high, &box->low);
    4558                 :             : 
    4559                 :        6210 :         PG_RETURN_POLYGON_P(poly);
    4560                 :        3105 : }
    4561                 :             : 
    4562                 :             : 
    4563                 :             : Datum
    4564                 :           7 : poly_path(PG_FUNCTION_ARGS)
    4565                 :             : {
    4566                 :           7 :         POLYGON    *poly = PG_GETARG_POLYGON_P(0);
    4567                 :           7 :         PATH       *path;
    4568                 :           7 :         int                     size;
    4569                 :           7 :         int                     i;
    4570                 :             : 
    4571                 :             :         /*
    4572                 :             :          * Never overflows: the old size fit in MaxAllocSize, and the new size is
    4573                 :             :          * smaller by a small constant.
    4574                 :             :          */
    4575                 :           7 :         size = offsetof(PATH, p) + sizeof(path->p[0]) * poly->npts;
    4576                 :           7 :         path = (PATH *) palloc(size);
    4577                 :             : 
    4578                 :           7 :         SET_VARSIZE(path, size);
    4579                 :           7 :         path->npts = poly->npts;
    4580                 :           7 :         path->closed = true;
    4581                 :             :         /* prevent instability in unused pad bytes */
    4582                 :           7 :         path->dummy = 0;
    4583                 :             : 
    4584         [ +  + ]:          28 :         for (i = 0; i < poly->npts; i++)
    4585                 :             :         {
    4586                 :          21 :                 path->p[i].x = poly->p[i].x;
    4587                 :          21 :                 path->p[i].y = poly->p[i].y;
    4588                 :          21 :         }
    4589                 :             : 
    4590                 :          14 :         PG_RETURN_PATH_P(path);
    4591                 :           7 : }
    4592                 :             : 
    4593                 :             : 
    4594                 :             : /***********************************************************************
    4595                 :             :  **
    4596                 :             :  **             Routines for circles.
    4597                 :             :  **
    4598                 :             :  ***********************************************************************/
    4599                 :             : 
    4600                 :             : /*----------------------------------------------------------
    4601                 :             :  * Formatting and conversion routines.
    4602                 :             :  *---------------------------------------------------------*/
    4603                 :             : 
    4604                 :             : /*              circle_in               -               convert a string to internal form.
    4605                 :             :  *
    4606                 :             :  *              External format: (center and radius of circle)
    4607                 :             :  *                              "<(f8,f8),f8>"
    4608                 :             :  *                              also supports quick entry style "f8,f8,f8"
    4609                 :             :  */
    4610                 :             : Datum
    4611                 :          64 : circle_in(PG_FUNCTION_ARGS)
    4612                 :             : {
    4613                 :          64 :         char       *str = PG_GETARG_CSTRING(0);
    4614                 :          64 :         Node       *escontext = fcinfo->context;
    4615                 :          64 :         CIRCLE     *circle = palloc_object(CIRCLE);
    4616                 :          64 :         char       *s,
    4617                 :             :                            *cp;
    4618                 :          64 :         int                     depth = 0;
    4619                 :             : 
    4620                 :          64 :         s = str;
    4621         [ +  + ]:          68 :         while (isspace((unsigned char) *s))
    4622                 :           4 :                 s++;
    4623         [ +  + ]:          64 :         if (*s == LDELIM_C)
    4624                 :          53 :                 depth++, s++;
    4625         [ +  + ]:          11 :         else if (*s == LDELIM)
    4626                 :             :         {
    4627                 :             :                 /* If there are two left parens, consume the first one */
    4628                 :           7 :                 cp = (s + 1);
    4629         [ +  + ]:           9 :                 while (isspace((unsigned char) *cp))
    4630                 :           2 :                         cp++;
    4631         [ +  + ]:           7 :                 if (*cp == LDELIM)
    4632                 :           2 :                         depth++, s = cp;
    4633                 :           7 :         }
    4634                 :             : 
    4635                 :             :         /* pair_decode will consume parens around the pair, if any */
    4636   [ +  +  +  + ]:         128 :         if (!pair_decode(s, &circle->center.x, &circle->center.y, &s, "circle", str,
    4637                 :          64 :                                          escontext))
    4638                 :           2 :                 PG_RETURN_NULL();
    4639                 :             : 
    4640         [ +  + ]:          62 :         if (*s == DELIM)
    4641                 :          61 :                 s++;
    4642                 :             : 
    4643         [ +  - ]:          62 :         if (!single_decode(s, &circle->radius, &s, "circle", str, escontext))
    4644                 :           0 :                 PG_RETURN_NULL();
    4645                 :             : 
    4646                 :             :         /* We have to accept NaN. */
    4647         [ +  + ]:          62 :         if (circle->radius < 0.0)
    4648         [ -  + ]:           2 :                 ereturn(escontext, (Datum) 0,
    4649                 :             :                                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
    4650                 :             :                                  errmsg("invalid input syntax for type %s: \"%s\"",
    4651                 :             :                                                 "circle", str)));
    4652                 :             : 
    4653         [ +  + ]:         114 :         while (depth > 0)
    4654                 :             :         {
    4655   [ +  +  +  +  :          56 :                 if ((*s == RDELIM) || ((*s == RDELIM_C) && (depth == 1)))
                   +  + ]
    4656                 :             :                 {
    4657                 :          54 :                         depth--;
    4658                 :          54 :                         s++;
    4659         [ +  + ]:          57 :                         while (isspace((unsigned char) *s))
    4660                 :           3 :                                 s++;
    4661                 :          54 :                 }
    4662                 :             :                 else
    4663         [ +  + ]:           2 :                         ereturn(escontext, (Datum) 0,
    4664                 :             :                                         (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
    4665                 :             :                                          errmsg("invalid input syntax for type %s: \"%s\"",
    4666                 :             :                                                         "circle", str)));
    4667                 :             :         }
    4668                 :             : 
    4669         [ +  + ]:          58 :         if (*s != '\0')
    4670         [ +  + ]:           2 :                 ereturn(escontext, (Datum) 0,
    4671                 :             :                                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
    4672                 :             :                                  errmsg("invalid input syntax for type %s: \"%s\"",
    4673                 :             :                                                 "circle", str)));
    4674                 :             : 
    4675                 :          56 :         PG_RETURN_CIRCLE_P(circle);
    4676                 :          60 : }
    4677                 :             : 
    4678                 :             : /*              circle_out              -               convert a circle to external form.
    4679                 :             :  */
    4680                 :             : Datum
    4681                 :        1516 : circle_out(PG_FUNCTION_ARGS)
    4682                 :             : {
    4683                 :        1516 :         CIRCLE     *circle = PG_GETARG_CIRCLE_P(0);
    4684                 :        1516 :         StringInfoData str;
    4685                 :             : 
    4686                 :        1516 :         initStringInfo(&str);
    4687                 :             : 
    4688                 :        1516 :         appendStringInfoChar(&str, LDELIM_C);
    4689                 :        1516 :         appendStringInfoChar(&str, LDELIM);
    4690                 :        1516 :         pair_encode(circle->center.x, circle->center.y, &str);
    4691                 :        1516 :         appendStringInfoChar(&str, RDELIM);
    4692                 :        1516 :         appendStringInfoChar(&str, DELIM);
    4693                 :        1516 :         single_encode(circle->radius, &str);
    4694                 :        1516 :         appendStringInfoChar(&str, RDELIM_C);
    4695                 :             : 
    4696                 :        3032 :         PG_RETURN_CSTRING(str.data);
    4697                 :        1516 : }
    4698                 :             : 
    4699                 :             : /*
    4700                 :             :  *              circle_recv                     - converts external binary format to circle
    4701                 :             :  */
    4702                 :             : Datum
    4703                 :           0 : circle_recv(PG_FUNCTION_ARGS)
    4704                 :             : {
    4705                 :           0 :         StringInfo      buf = (StringInfo) PG_GETARG_POINTER(0);
    4706                 :           0 :         CIRCLE     *circle;
    4707                 :             : 
    4708                 :           0 :         circle = palloc_object(CIRCLE);
    4709                 :             : 
    4710                 :           0 :         circle->center.x = pq_getmsgfloat8(buf);
    4711                 :           0 :         circle->center.y = pq_getmsgfloat8(buf);
    4712                 :           0 :         circle->radius = pq_getmsgfloat8(buf);
    4713                 :             : 
    4714                 :             :         /* We have to accept NaN. */
    4715         [ #  # ]:           0 :         if (circle->radius < 0.0)
    4716   [ #  #  #  # ]:           0 :                 ereport(ERROR,
    4717                 :             :                                 (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
    4718                 :             :                                  errmsg("invalid radius in external \"circle\" value")));
    4719                 :             : 
    4720                 :           0 :         PG_RETURN_CIRCLE_P(circle);
    4721                 :           0 : }
    4722                 :             : 
    4723                 :             : /*
    4724                 :             :  *              circle_send                     - converts circle to binary format
    4725                 :             :  */
    4726                 :             : Datum
    4727                 :           0 : circle_send(PG_FUNCTION_ARGS)
    4728                 :             : {
    4729                 :           0 :         CIRCLE     *circle = PG_GETARG_CIRCLE_P(0);
    4730                 :           0 :         StringInfoData buf;
    4731                 :             : 
    4732                 :           0 :         pq_begintypsend(&buf);
    4733                 :           0 :         pq_sendfloat8(&buf, circle->center.x);
    4734                 :           0 :         pq_sendfloat8(&buf, circle->center.y);
    4735                 :           0 :         pq_sendfloat8(&buf, circle->radius);
    4736                 :           0 :         PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
    4737                 :           0 : }
    4738                 :             : 
    4739                 :             : 
    4740                 :             : /*----------------------------------------------------------
    4741                 :             :  *      Relational operators for CIRCLEs.
    4742                 :             :  *              <, >, <=, >=, and == are based on circle area.
    4743                 :             :  *---------------------------------------------------------*/
    4744                 :             : 
    4745                 :             : /*              circles identical?
    4746                 :             :  *
    4747                 :             :  * We consider NaNs values to be equal to each other to let those circles
    4748                 :             :  * to be found.
    4749                 :             :  */
    4750                 :             : Datum
    4751                 :          64 : circle_same(PG_FUNCTION_ARGS)
    4752                 :             : {
    4753                 :          64 :         CIRCLE     *circle1 = PG_GETARG_CIRCLE_P(0);
    4754                 :          64 :         CIRCLE     *circle2 = PG_GETARG_CIRCLE_P(1);
    4755                 :             : 
    4756   [ -  +  +  +  :          64 :         PG_RETURN_BOOL(((isnan(circle1->radius) && isnan(circle2->radius)) ||
          +  -  -  +  +  
                -  +  + ]
    4757                 :             :                                         FPeq(circle1->radius, circle2->radius)) &&
    4758                 :             :                                    point_eq_point(&circle1->center, &circle2->center));
    4759                 :          64 : }
    4760                 :             : 
    4761                 :             : /*              circle_overlap  -               does circle1 overlap circle2?
    4762                 :             :  */
    4763                 :             : Datum
    4764                 :        3203 : circle_overlap(PG_FUNCTION_ARGS)
    4765                 :             : {
    4766                 :        3203 :         CIRCLE     *circle1 = PG_GETARG_CIRCLE_P(0);
    4767                 :        3203 :         CIRCLE     *circle2 = PG_GETARG_CIRCLE_P(1);
    4768                 :             : 
    4769                 :        6406 :         PG_RETURN_BOOL(FPle(point_dt(&circle1->center, &circle2->center),
    4770                 :             :                                                 float8_pl(circle1->radius, circle2->radius)));
    4771                 :        3203 : }
    4772                 :             : 
    4773                 :             : /*              circle_overleft -               is the right edge of circle1 at or left of
    4774                 :             :  *                                                              the right edge of circle2?
    4775                 :             :  */
    4776                 :             : Datum
    4777                 :          64 : circle_overleft(PG_FUNCTION_ARGS)
    4778                 :             : {
    4779                 :          64 :         CIRCLE     *circle1 = PG_GETARG_CIRCLE_P(0);
    4780                 :          64 :         CIRCLE     *circle2 = PG_GETARG_CIRCLE_P(1);
    4781                 :             : 
    4782                 :         128 :         PG_RETURN_BOOL(FPle(float8_pl(circle1->center.x, circle1->radius),
    4783                 :             :                                                 float8_pl(circle2->center.x, circle2->radius)));
    4784                 :          64 : }
    4785                 :             : 
    4786                 :             : /*              circle_left             -               is circle1 strictly left of circle2?
    4787                 :             :  */
    4788                 :             : Datum
    4789                 :          64 : circle_left(PG_FUNCTION_ARGS)
    4790                 :             : {
    4791                 :          64 :         CIRCLE     *circle1 = PG_GETARG_CIRCLE_P(0);
    4792                 :          64 :         CIRCLE     *circle2 = PG_GETARG_CIRCLE_P(1);
    4793                 :             : 
    4794                 :         128 :         PG_RETURN_BOOL(FPlt(float8_pl(circle1->center.x, circle1->radius),
    4795                 :             :                                                 float8_mi(circle2->center.x, circle2->radius)));
    4796                 :          64 : }
    4797                 :             : 
    4798                 :             : /*              circle_right    -               is circle1 strictly right of circle2?
    4799                 :             :  */
    4800                 :             : Datum
    4801                 :          64 : circle_right(PG_FUNCTION_ARGS)
    4802                 :             : {
    4803                 :          64 :         CIRCLE     *circle1 = PG_GETARG_CIRCLE_P(0);
    4804                 :          64 :         CIRCLE     *circle2 = PG_GETARG_CIRCLE_P(1);
    4805                 :             : 
    4806                 :         128 :         PG_RETURN_BOOL(FPgt(float8_mi(circle1->center.x, circle1->radius),
    4807                 :             :                                                 float8_pl(circle2->center.x, circle2->radius)));
    4808                 :          64 : }
    4809                 :             : 
    4810                 :             : /*              circle_overright        -       is the left edge of circle1 at or right of
    4811                 :             :  *                                                              the left edge of circle2?
    4812                 :             :  */
    4813                 :             : Datum
    4814                 :          64 : circle_overright(PG_FUNCTION_ARGS)
    4815                 :             : {
    4816                 :          64 :         CIRCLE     *circle1 = PG_GETARG_CIRCLE_P(0);
    4817                 :          64 :         CIRCLE     *circle2 = PG_GETARG_CIRCLE_P(1);
    4818                 :             : 
    4819                 :         128 :         PG_RETURN_BOOL(FPge(float8_mi(circle1->center.x, circle1->radius),
    4820                 :             :                                                 float8_mi(circle2->center.x, circle2->radius)));
    4821                 :          64 : }
    4822                 :             : 
    4823                 :             : /*              circle_contained                -               is circle1 contained by circle2?
    4824                 :             :  */
    4825                 :             : Datum
    4826                 :          64 : circle_contained(PG_FUNCTION_ARGS)
    4827                 :             : {
    4828                 :          64 :         CIRCLE     *circle1 = PG_GETARG_CIRCLE_P(0);
    4829                 :          64 :         CIRCLE     *circle2 = PG_GETARG_CIRCLE_P(1);
    4830                 :             : 
    4831                 :         128 :         PG_RETURN_BOOL(FPle(point_dt(&circle1->center, &circle2->center),
    4832                 :             :                                                 float8_mi(circle2->radius, circle1->radius)));
    4833                 :          64 : }
    4834                 :             : 
    4835                 :             : /*              circle_contain  -               does circle1 contain circle2?
    4836                 :             :  */
    4837                 :             : Datum
    4838                 :          66 : circle_contain(PG_FUNCTION_ARGS)
    4839                 :             : {
    4840                 :          66 :         CIRCLE     *circle1 = PG_GETARG_CIRCLE_P(0);
    4841                 :          66 :         CIRCLE     *circle2 = PG_GETARG_CIRCLE_P(1);
    4842                 :             : 
    4843                 :         132 :         PG_RETURN_BOOL(FPle(point_dt(&circle1->center, &circle2->center),
    4844                 :             :                                                 float8_mi(circle1->radius, circle2->radius)));
    4845                 :          66 : }
    4846                 :             : 
    4847                 :             : 
    4848                 :             : /*              circle_below            -               is circle1 strictly below circle2?
    4849                 :             :  */
    4850                 :             : Datum
    4851                 :          64 : circle_below(PG_FUNCTION_ARGS)
    4852                 :             : {
    4853                 :          64 :         CIRCLE     *circle1 = PG_GETARG_CIRCLE_P(0);
    4854                 :          64 :         CIRCLE     *circle2 = PG_GETARG_CIRCLE_P(1);
    4855                 :             : 
    4856                 :         128 :         PG_RETURN_BOOL(FPlt(float8_pl(circle1->center.y, circle1->radius),
    4857                 :             :                                                 float8_mi(circle2->center.y, circle2->radius)));
    4858                 :          64 : }
    4859                 :             : 
    4860                 :             : /*              circle_above    -               is circle1 strictly above circle2?
    4861                 :             :  */
    4862                 :             : Datum
    4863                 :          64 : circle_above(PG_FUNCTION_ARGS)
    4864                 :             : {
    4865                 :          64 :         CIRCLE     *circle1 = PG_GETARG_CIRCLE_P(0);
    4866                 :          64 :         CIRCLE     *circle2 = PG_GETARG_CIRCLE_P(1);
    4867                 :             : 
    4868                 :         128 :         PG_RETURN_BOOL(FPgt(float8_mi(circle1->center.y, circle1->radius),
    4869                 :             :                                                 float8_pl(circle2->center.y, circle2->radius)));
    4870                 :          64 : }
    4871                 :             : 
    4872                 :             : /*              circle_overbelow -              is the upper edge of circle1 at or below
    4873                 :             :  *                                                              the upper edge of circle2?
    4874                 :             :  */
    4875                 :             : Datum
    4876                 :          64 : circle_overbelow(PG_FUNCTION_ARGS)
    4877                 :             : {
    4878                 :          64 :         CIRCLE     *circle1 = PG_GETARG_CIRCLE_P(0);
    4879                 :          64 :         CIRCLE     *circle2 = PG_GETARG_CIRCLE_P(1);
    4880                 :             : 
    4881                 :         128 :         PG_RETURN_BOOL(FPle(float8_pl(circle1->center.y, circle1->radius),
    4882                 :             :                                                 float8_pl(circle2->center.y, circle2->radius)));
    4883                 :          64 : }
    4884                 :             : 
    4885                 :             : /*              circle_overabove        -       is the lower edge of circle1 at or above
    4886                 :             :  *                                                              the lower edge of circle2?
    4887                 :             :  */
    4888                 :             : Datum
    4889                 :          64 : circle_overabove(PG_FUNCTION_ARGS)
    4890                 :             : {
    4891                 :          64 :         CIRCLE     *circle1 = PG_GETARG_CIRCLE_P(0);
    4892                 :          64 :         CIRCLE     *circle2 = PG_GETARG_CIRCLE_P(1);
    4893                 :             : 
    4894                 :         128 :         PG_RETURN_BOOL(FPge(float8_mi(circle1->center.y, circle1->radius),
    4895                 :             :                                                 float8_mi(circle2->center.y, circle2->radius)));
    4896                 :          64 : }
    4897                 :             : 
    4898                 :             : 
    4899                 :             : /*              circle_relop    -               is area(circle1) relop area(circle2), within
    4900                 :             :  *                                                              our accuracy constraint?
    4901                 :             :  */
    4902                 :             : Datum
    4903                 :          64 : circle_eq(PG_FUNCTION_ARGS)
    4904                 :             : {
    4905                 :          64 :         CIRCLE     *circle1 = PG_GETARG_CIRCLE_P(0);
    4906                 :          64 :         CIRCLE     *circle2 = PG_GETARG_CIRCLE_P(1);
    4907                 :             : 
    4908                 :         128 :         PG_RETURN_BOOL(FPeq(circle_ar(circle1), circle_ar(circle2)));
    4909                 :          64 : }
    4910                 :             : 
    4911                 :             : Datum
    4912                 :          64 : circle_ne(PG_FUNCTION_ARGS)
    4913                 :             : {
    4914                 :          64 :         CIRCLE     *circle1 = PG_GETARG_CIRCLE_P(0);
    4915                 :          64 :         CIRCLE     *circle2 = PG_GETARG_CIRCLE_P(1);
    4916                 :             : 
    4917                 :         128 :         PG_RETURN_BOOL(FPne(circle_ar(circle1), circle_ar(circle2)));
    4918                 :          64 : }
    4919                 :             : 
    4920                 :             : Datum
    4921                 :         263 : circle_lt(PG_FUNCTION_ARGS)
    4922                 :             : {
    4923                 :         263 :         CIRCLE     *circle1 = PG_GETARG_CIRCLE_P(0);
    4924                 :         263 :         CIRCLE     *circle2 = PG_GETARG_CIRCLE_P(1);
    4925                 :             : 
    4926                 :         526 :         PG_RETURN_BOOL(FPlt(circle_ar(circle1), circle_ar(circle2)));
    4927                 :         263 : }
    4928                 :             : 
    4929                 :             : Datum
    4930                 :          64 : circle_gt(PG_FUNCTION_ARGS)
    4931                 :             : {
    4932                 :          64 :         CIRCLE     *circle1 = PG_GETARG_CIRCLE_P(0);
    4933                 :          64 :         CIRCLE     *circle2 = PG_GETARG_CIRCLE_P(1);
    4934                 :             : 
    4935                 :         128 :         PG_RETURN_BOOL(FPgt(circle_ar(circle1), circle_ar(circle2)));
    4936                 :          64 : }
    4937                 :             : 
    4938                 :             : Datum
    4939                 :          64 : circle_le(PG_FUNCTION_ARGS)
    4940                 :             : {
    4941                 :          64 :         CIRCLE     *circle1 = PG_GETARG_CIRCLE_P(0);
    4942                 :          64 :         CIRCLE     *circle2 = PG_GETARG_CIRCLE_P(1);
    4943                 :             : 
    4944                 :         128 :         PG_RETURN_BOOL(FPle(circle_ar(circle1), circle_ar(circle2)));
    4945                 :          64 : }
    4946                 :             : 
    4947                 :             : Datum
    4948                 :          81 : circle_ge(PG_FUNCTION_ARGS)
    4949                 :             : {
    4950                 :          81 :         CIRCLE     *circle1 = PG_GETARG_CIRCLE_P(0);
    4951                 :          81 :         CIRCLE     *circle2 = PG_GETARG_CIRCLE_P(1);
    4952                 :             : 
    4953                 :         162 :         PG_RETURN_BOOL(FPge(circle_ar(circle1), circle_ar(circle2)));
    4954                 :          81 : }
    4955                 :             : 
    4956                 :             : 
    4957                 :             : /*----------------------------------------------------------
    4958                 :             :  *      "Arithmetic" operators on circles.
    4959                 :             :  *---------------------------------------------------------*/
    4960                 :             : 
    4961                 :             : /* circle_add_pt()
    4962                 :             :  * Translation operator.
    4963                 :             :  */
    4964                 :             : Datum
    4965                 :          80 : circle_add_pt(PG_FUNCTION_ARGS)
    4966                 :             : {
    4967                 :          80 :         CIRCLE     *circle = PG_GETARG_CIRCLE_P(0);
    4968                 :          80 :         Point      *point = PG_GETARG_POINT_P(1);
    4969                 :          80 :         CIRCLE     *result;
    4970                 :             : 
    4971                 :          80 :         result = palloc_object(CIRCLE);
    4972                 :             : 
    4973                 :          80 :         point_add_point(&result->center, &circle->center, point);
    4974                 :          80 :         result->radius = circle->radius;
    4975                 :             : 
    4976                 :         160 :         PG_RETURN_CIRCLE_P(result);
    4977                 :          80 : }
    4978                 :             : 
    4979                 :             : Datum
    4980                 :          80 : circle_sub_pt(PG_FUNCTION_ARGS)
    4981                 :             : {
    4982                 :          80 :         CIRCLE     *circle = PG_GETARG_CIRCLE_P(0);
    4983                 :          80 :         Point      *point = PG_GETARG_POINT_P(1);
    4984                 :          80 :         CIRCLE     *result;
    4985                 :             : 
    4986                 :          80 :         result = palloc_object(CIRCLE);
    4987                 :             : 
    4988                 :          80 :         point_sub_point(&result->center, &circle->center, point);
    4989                 :          80 :         result->radius = circle->radius;
    4990                 :             : 
    4991                 :         160 :         PG_RETURN_CIRCLE_P(result);
    4992                 :          80 : }
    4993                 :             : 
    4994                 :             : 
    4995                 :             : /* circle_mul_pt()
    4996                 :             :  * Rotation and scaling operators.
    4997                 :             :  */
    4998                 :             : Datum
    4999                 :          80 : circle_mul_pt(PG_FUNCTION_ARGS)
    5000                 :             : {
    5001                 :          80 :         CIRCLE     *circle = PG_GETARG_CIRCLE_P(0);
    5002                 :          80 :         Point      *point = PG_GETARG_POINT_P(1);
    5003                 :          80 :         CIRCLE     *result;
    5004                 :             : 
    5005                 :          80 :         result = palloc_object(CIRCLE);
    5006                 :             : 
    5007                 :          80 :         point_mul_point(&result->center, &circle->center, point);
    5008                 :          80 :         result->radius = float8_mul(circle->radius, hypot(point->x, point->y));
    5009                 :             : 
    5010                 :         160 :         PG_RETURN_CIRCLE_P(result);
    5011                 :          80 : }
    5012                 :             : 
    5013                 :             : Datum
    5014                 :          18 : circle_div_pt(PG_FUNCTION_ARGS)
    5015                 :             : {
    5016                 :          18 :         CIRCLE     *circle = PG_GETARG_CIRCLE_P(0);
    5017                 :          18 :         Point      *point = PG_GETARG_POINT_P(1);
    5018                 :          18 :         CIRCLE     *result;
    5019                 :             : 
    5020                 :          18 :         result = palloc_object(CIRCLE);
    5021                 :             : 
    5022                 :          18 :         point_div_point(&result->center, &circle->center, point);
    5023                 :          18 :         result->radius = float8_div(circle->radius, hypot(point->x, point->y));
    5024                 :             : 
    5025                 :          36 :         PG_RETURN_CIRCLE_P(result);
    5026                 :          18 : }
    5027                 :             : 
    5028                 :             : 
    5029                 :             : /*              circle_area             -               returns the area of the circle.
    5030                 :             :  */
    5031                 :             : Datum
    5032                 :          85 : circle_area(PG_FUNCTION_ARGS)
    5033                 :             : {
    5034                 :          85 :         CIRCLE     *circle = PG_GETARG_CIRCLE_P(0);
    5035                 :             : 
    5036                 :         170 :         PG_RETURN_FLOAT8(circle_ar(circle));
    5037                 :          85 : }
    5038                 :             : 
    5039                 :             : 
    5040                 :             : /*              circle_diameter -               returns the diameter of the circle.
    5041                 :             :  */
    5042                 :             : Datum
    5043                 :          16 : circle_diameter(PG_FUNCTION_ARGS)
    5044                 :             : {
    5045                 :          16 :         CIRCLE     *circle = PG_GETARG_CIRCLE_P(0);
    5046                 :             : 
    5047                 :          32 :         PG_RETURN_FLOAT8(float8_mul(circle->radius, 2.0));
    5048                 :          16 : }
    5049                 :             : 
    5050                 :             : 
    5051                 :             : /*              circle_radius   -               returns the radius of the circle.
    5052                 :             :  */
    5053                 :             : Datum
    5054                 :        3126 : circle_radius(PG_FUNCTION_ARGS)
    5055                 :             : {
    5056                 :        3126 :         CIRCLE     *circle = PG_GETARG_CIRCLE_P(0);
    5057                 :             : 
    5058                 :        6252 :         PG_RETURN_FLOAT8(circle->radius);
    5059                 :        3126 : }
    5060                 :             : 
    5061                 :             : 
    5062                 :             : /*              circle_distance -               returns the distance between
    5063                 :             :  *                                                                two circles.
    5064                 :             :  */
    5065                 :             : Datum
    5066                 :          28 : circle_distance(PG_FUNCTION_ARGS)
    5067                 :             : {
    5068                 :          28 :         CIRCLE     *circle1 = PG_GETARG_CIRCLE_P(0);
    5069                 :          28 :         CIRCLE     *circle2 = PG_GETARG_CIRCLE_P(1);
    5070                 :          28 :         float8          result;
    5071                 :             : 
    5072                 :          56 :         result = float8_mi(point_dt(&circle1->center, &circle2->center),
    5073                 :          28 :                                            float8_pl(circle1->radius, circle2->radius));
    5074         [ +  + ]:          28 :         if (result < 0.0)
    5075                 :          12 :                 result = 0.0;
    5076                 :             : 
    5077                 :          56 :         PG_RETURN_FLOAT8(result);
    5078                 :          28 : }
    5079                 :             : 
    5080                 :             : 
    5081                 :             : Datum
    5082                 :           4 : circle_contain_pt(PG_FUNCTION_ARGS)
    5083                 :             : {
    5084                 :           4 :         CIRCLE     *circle = PG_GETARG_CIRCLE_P(0);
    5085                 :           4 :         Point      *point = PG_GETARG_POINT_P(1);
    5086                 :           4 :         float8          d;
    5087                 :             : 
    5088                 :           4 :         d = point_dt(&circle->center, point);
    5089                 :           8 :         PG_RETURN_BOOL(d <= circle->radius);
    5090                 :           4 : }
    5091                 :             : 
    5092                 :             : 
    5093                 :             : Datum
    5094                 :          10 : pt_contained_circle(PG_FUNCTION_ARGS)
    5095                 :             : {
    5096                 :          10 :         Point      *point = PG_GETARG_POINT_P(0);
    5097                 :          10 :         CIRCLE     *circle = PG_GETARG_CIRCLE_P(1);
    5098                 :          10 :         float8          d;
    5099                 :             : 
    5100                 :          10 :         d = point_dt(&circle->center, point);
    5101                 :          20 :         PG_RETURN_BOOL(d <= circle->radius);
    5102                 :          10 : }
    5103                 :             : 
    5104                 :             : 
    5105                 :             : /*              dist_pc -               returns the distance between
    5106                 :             :  *                                                a point and a circle.
    5107                 :             :  */
    5108                 :             : Datum
    5109                 :         141 : dist_pc(PG_FUNCTION_ARGS)
    5110                 :             : {
    5111                 :         141 :         Point      *point = PG_GETARG_POINT_P(0);
    5112                 :         141 :         CIRCLE     *circle = PG_GETARG_CIRCLE_P(1);
    5113                 :         141 :         float8          result;
    5114                 :             : 
    5115                 :         282 :         result = float8_mi(point_dt(point, &circle->center),
    5116                 :         141 :                                            circle->radius);
    5117         [ +  + ]:         141 :         if (result < 0.0)
    5118                 :          19 :                 result = 0.0;
    5119                 :             : 
    5120                 :         282 :         PG_RETURN_FLOAT8(result);
    5121                 :         141 : }
    5122                 :             : 
    5123                 :             : /*
    5124                 :             :  * Distance from a circle to a point
    5125                 :             :  */
    5126                 :             : Datum
    5127                 :        3121 : dist_cpoint(PG_FUNCTION_ARGS)
    5128                 :             : {
    5129                 :        3121 :         CIRCLE     *circle = PG_GETARG_CIRCLE_P(0);
    5130                 :        3121 :         Point      *point = PG_GETARG_POINT_P(1);
    5131                 :        3121 :         float8          result;
    5132                 :             : 
    5133                 :        3121 :         result = float8_mi(point_dt(point, &circle->center), circle->radius);
    5134         [ +  - ]:        3121 :         if (result < 0.0)
    5135                 :           0 :                 result = 0.0;
    5136                 :             : 
    5137                 :        6242 :         PG_RETURN_FLOAT8(result);
    5138                 :        3121 : }
    5139                 :             : 
    5140                 :             : /*              circle_center   -               returns the center point of the circle.
    5141                 :             :  */
    5142                 :             : Datum
    5143                 :        3151 : circle_center(PG_FUNCTION_ARGS)
    5144                 :             : {
    5145                 :        3151 :         CIRCLE     *circle = PG_GETARG_CIRCLE_P(0);
    5146                 :        3151 :         Point      *result;
    5147                 :             : 
    5148                 :        3151 :         result = palloc_object(Point);
    5149                 :        3151 :         result->x = circle->center.x;
    5150                 :        3151 :         result->y = circle->center.y;
    5151                 :             : 
    5152                 :        6302 :         PG_RETURN_POINT_P(result);
    5153                 :        3151 : }
    5154                 :             : 
    5155                 :             : 
    5156                 :             : /*              circle_ar               -               returns the area of the circle.
    5157                 :             :  */
    5158                 :             : static float8
    5159                 :        1285 : circle_ar(CIRCLE *circle)
    5160                 :             : {
    5161                 :        1285 :         return float8_mul(float8_mul(circle->radius, circle->radius), M_PI);
    5162                 :             : }
    5163                 :             : 
    5164                 :             : 
    5165                 :             : /*----------------------------------------------------------
    5166                 :             :  *      Conversion operators.
    5167                 :             :  *---------------------------------------------------------*/
    5168                 :             : 
    5169                 :             : Datum
    5170                 :       30026 : cr_circle(PG_FUNCTION_ARGS)
    5171                 :             : {
    5172                 :       30026 :         Point      *center = PG_GETARG_POINT_P(0);
    5173                 :       30026 :         float8          radius = PG_GETARG_FLOAT8(1);
    5174                 :       30026 :         CIRCLE     *result;
    5175                 :             : 
    5176                 :       30026 :         result = palloc_object(CIRCLE);
    5177                 :             : 
    5178                 :       30026 :         result->center.x = center->x;
    5179                 :       30026 :         result->center.y = center->y;
    5180                 :       30026 :         result->radius = radius;
    5181                 :             : 
    5182                 :       60052 :         PG_RETURN_CIRCLE_P(result);
    5183                 :       30026 : }
    5184                 :             : 
    5185                 :             : Datum
    5186                 :           8 : circle_box(PG_FUNCTION_ARGS)
    5187                 :             : {
    5188                 :           8 :         CIRCLE     *circle = PG_GETARG_CIRCLE_P(0);
    5189                 :           8 :         BOX                *box;
    5190                 :           8 :         float8          delta;
    5191                 :             : 
    5192                 :           8 :         box = palloc_object(BOX);
    5193                 :             : 
    5194                 :           8 :         delta = float8_div(circle->radius, sqrt(2.0));
    5195                 :             : 
    5196                 :           8 :         box->high.x = float8_pl(circle->center.x, delta);
    5197                 :           8 :         box->low.x = float8_mi(circle->center.x, delta);
    5198                 :           8 :         box->high.y = float8_pl(circle->center.y, delta);
    5199                 :           8 :         box->low.y = float8_mi(circle->center.y, delta);
    5200                 :             : 
    5201                 :          16 :         PG_RETURN_BOX_P(box);
    5202                 :           8 : }
    5203                 :             : 
    5204                 :             : /* box_circle()
    5205                 :             :  * Convert a box to a circle.
    5206                 :             :  */
    5207                 :             : Datum
    5208                 :        3105 : box_circle(PG_FUNCTION_ARGS)
    5209                 :             : {
    5210                 :        3105 :         BOX                *box = PG_GETARG_BOX_P(0);
    5211                 :        3105 :         CIRCLE     *circle;
    5212                 :             : 
    5213                 :        3105 :         circle = palloc_object(CIRCLE);
    5214                 :             : 
    5215                 :        3105 :         circle->center.x = float8_div(float8_pl(box->high.x, box->low.x), 2.0);
    5216                 :        3105 :         circle->center.y = float8_div(float8_pl(box->high.y, box->low.y), 2.0);
    5217                 :             : 
    5218                 :        3105 :         circle->radius = point_dt(&circle->center, &box->high);
    5219                 :             : 
    5220                 :        6210 :         PG_RETURN_CIRCLE_P(circle);
    5221                 :        3105 : }
    5222                 :             : 
    5223                 :             : 
    5224                 :             : Datum
    5225                 :       10014 : circle_poly(PG_FUNCTION_ARGS)
    5226                 :             : {
    5227                 :       10014 :         int32           npts = PG_GETARG_INT32(0);
    5228                 :       10014 :         CIRCLE     *circle = PG_GETARG_CIRCLE_P(1);
    5229                 :       10014 :         POLYGON    *poly;
    5230                 :       10014 :         int                     base_size,
    5231                 :             :                                 size;
    5232                 :       10014 :         int                     i;
    5233                 :       10014 :         float8          angle;
    5234                 :       10014 :         float8          anglestep;
    5235                 :             : 
    5236         [ +  + ]:       10014 :         if (FPzero(circle->radius))
    5237   [ +  -  +  - ]:           1 :                 ereport(ERROR,
    5238                 :             :                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    5239                 :             :                                  errmsg("cannot convert circle with radius zero to polygon")));
    5240                 :             : 
    5241         [ +  + ]:       10013 :         if (npts < 2)
    5242   [ +  -  +  - ]:           1 :                 ereport(ERROR,
    5243                 :             :                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    5244                 :             :                                  errmsg("must request at least 2 points")));
    5245                 :             : 
    5246                 :       10012 :         base_size = sizeof(poly->p[0]) * npts;
    5247                 :       10012 :         size = offsetof(POLYGON, p) + base_size;
    5248                 :             : 
    5249                 :             :         /* Check for integer overflow */
    5250         [ +  - ]:       10012 :         if (base_size / npts != sizeof(poly->p[0]) || size <= base_size)
    5251   [ #  #  #  # ]:           0 :                 ereport(ERROR,
    5252                 :             :                                 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
    5253                 :             :                                  errmsg("too many points requested")));
    5254                 :             : 
    5255                 :       10012 :         poly = (POLYGON *) palloc0(size);       /* zero any holes */
    5256                 :       10012 :         SET_VARSIZE(poly, size);
    5257                 :       10012 :         poly->npts = npts;
    5258                 :             : 
    5259                 :       10012 :         anglestep = float8_div(2.0 * M_PI, npts);
    5260                 :             : 
    5261         [ +  + ]:      130132 :         for (i = 0; i < npts; i++)
    5262                 :             :         {
    5263                 :      120120 :                 angle = float8_mul(anglestep, i);
    5264                 :             : 
    5265                 :      240240 :                 poly->p[i].x = float8_mi(circle->center.x,
    5266                 :      120120 :                                                                  float8_mul(circle->radius, cos(angle)));
    5267                 :      240240 :                 poly->p[i].y = float8_pl(circle->center.y,
    5268                 :      120120 :                                                                  float8_mul(circle->radius, sin(angle)));
    5269                 :      120120 :         }
    5270                 :             : 
    5271                 :       10012 :         make_bound_box(poly);
    5272                 :             : 
    5273                 :       20024 :         PG_RETURN_POLYGON_P(poly);
    5274                 :       10012 : }
    5275                 :             : 
    5276                 :             : /*
    5277                 :             :  * Convert polygon to circle
    5278                 :             :  *
    5279                 :             :  * The result must be preallocated.
    5280                 :             :  *
    5281                 :             :  * XXX This algorithm should use weighted means of line segments
    5282                 :             :  *      rather than straight average values of points - tgl 97/01/21.
    5283                 :             :  */
    5284                 :             : static void
    5285                 :          12 : poly_to_circle(CIRCLE *result, POLYGON *poly)
    5286                 :             : {
    5287                 :          12 :         int                     i;
    5288                 :             : 
    5289         [ +  - ]:          12 :         Assert(poly->npts > 0);
    5290                 :             : 
    5291                 :          12 :         result->center.x = 0;
    5292                 :          12 :         result->center.y = 0;
    5293                 :          12 :         result->radius = 0;
    5294                 :             : 
    5295         [ +  + ]:          54 :         for (i = 0; i < poly->npts; i++)
    5296                 :          42 :                 point_add_point(&result->center, &result->center, &poly->p[i]);
    5297                 :          12 :         result->center.x = float8_div(result->center.x, poly->npts);
    5298                 :          12 :         result->center.y = float8_div(result->center.y, poly->npts);
    5299                 :             : 
    5300         [ +  + ]:          54 :         for (i = 0; i < poly->npts; i++)
    5301                 :          84 :                 result->radius = float8_pl(result->radius,
    5302                 :          42 :                                                                    point_dt(&poly->p[i], &result->center));
    5303                 :          12 :         result->radius = float8_div(result->radius, poly->npts);
    5304                 :          12 : }
    5305                 :             : 
    5306                 :             : Datum
    5307                 :           5 : poly_circle(PG_FUNCTION_ARGS)
    5308                 :             : {
    5309                 :           5 :         POLYGON    *poly = PG_GETARG_POLYGON_P(0);
    5310                 :           5 :         CIRCLE     *result;
    5311                 :             : 
    5312                 :           5 :         result = palloc_object(CIRCLE);
    5313                 :             : 
    5314                 :           5 :         poly_to_circle(result, poly);
    5315                 :             : 
    5316                 :          10 :         PG_RETURN_CIRCLE_P(result);
    5317                 :           5 : }
    5318                 :             : 
    5319                 :             : 
    5320                 :             : /***********************************************************************
    5321                 :             :  **
    5322                 :             :  **             Private routines for multiple types.
    5323                 :             :  **
    5324                 :             :  ***********************************************************************/
    5325                 :             : 
    5326                 :             : /*
    5327                 :             :  *      Test to see if the point is inside the polygon, returns 1/0, or 2 if
    5328                 :             :  *      the point is on the polygon.
    5329                 :             :  *      Code adapted but not copied from integer-based routines in WN: A
    5330                 :             :  *      Server for the HTTP
    5331                 :             :  *      version 1.15.1, file wn/image.c
    5332                 :             :  *      http://hopf.math.northwestern.edu/index.html
    5333                 :             :  *      Description of algorithm:  http://www.linuxjournal.com/article/2197
    5334                 :             :  *                                                         http://www.linuxjournal.com/article/2029
    5335                 :             :  */
    5336                 :             : 
    5337                 :             : #define POINT_ON_POLYGON INT_MAX
    5338                 :             : 
    5339                 :             : static int
    5340                 :       41108 : point_inside(Point *p, int npts, Point *plist)
    5341                 :             : {
    5342                 :       41108 :         float8          x0,
    5343                 :             :                                 y0;
    5344                 :       41108 :         float8          prev_x,
    5345                 :             :                                 prev_y;
    5346                 :       41108 :         int                     i = 0;
    5347                 :       41108 :         float8          x,
    5348                 :             :                                 y;
    5349                 :       41108 :         int                     cross,
    5350                 :       41108 :                                 total_cross = 0;
    5351                 :             : 
    5352         [ +  - ]:       41108 :         Assert(npts > 0);
    5353                 :             : 
    5354                 :             :         /* compute first polygon point relative to single point */
    5355                 :       41108 :         x0 = float8_mi(plist[0].x, p->x);
    5356                 :       41108 :         y0 = float8_mi(plist[0].y, p->y);
    5357                 :             : 
    5358                 :       41108 :         prev_x = x0;
    5359                 :       41108 :         prev_y = y0;
    5360                 :             :         /* loop over polygon points and aggregate total_cross */
    5361         [ +  + ]:      189654 :         for (i = 1; i < npts; i++)
    5362                 :             :         {
    5363                 :             :                 /* compute next polygon point relative to single point */
    5364                 :      148568 :                 x = float8_mi(plist[i].x, p->x);
    5365                 :      148568 :                 y = float8_mi(plist[i].y, p->y);
    5366                 :             : 
    5367                 :             :                 /* compute previous to current point crossing */
    5368         [ +  + ]:      148568 :                 if ((cross = lseg_crossing(x, y, prev_x, prev_y)) == POINT_ON_POLYGON)
    5369                 :          22 :                         return 2;
    5370                 :      148546 :                 total_cross += cross;
    5371                 :             : 
    5372                 :      148546 :                 prev_x = x;
    5373                 :      148546 :                 prev_y = y;
    5374                 :      148546 :         }
    5375                 :             : 
    5376                 :             :         /* now do the first point */
    5377         [ +  + ]:       41086 :         if ((cross = lseg_crossing(x0, y0, prev_x, prev_y)) == POINT_ON_POLYGON)
    5378                 :          18 :                 return 2;
    5379                 :       41068 :         total_cross += cross;
    5380                 :             : 
    5381         [ +  + ]:       41068 :         if (total_cross != 0)
    5382                 :       31532 :                 return 1;
    5383                 :        9536 :         return 0;
    5384                 :       41108 : }
    5385                 :             : 
    5386                 :             : 
    5387                 :             : /* lseg_crossing()
    5388                 :             :  * Returns +/-2 if line segment crosses the positive X-axis in a +/- direction.
    5389                 :             :  * Returns +/-1 if one point is on the positive X-axis.
    5390                 :             :  * Returns 0 if both points are on the positive X-axis, or there is no crossing.
    5391                 :             :  * Returns POINT_ON_POLYGON if the segment contains (0,0).
    5392                 :             :  * Wow, that is one confusing API, but it is used above, and when summed,
    5393                 :             :  * can tell is if a point is in a polygon.
    5394                 :             :  */
    5395                 :             : 
    5396                 :             : static int
    5397                 :      189654 : lseg_crossing(float8 x, float8 y, float8 prev_x, float8 prev_y)
    5398                 :             : {
    5399                 :      189654 :         float8          z;
    5400                 :      189654 :         int                     y_sign;
    5401                 :             : 
    5402         [ +  + ]:      189654 :         if (FPzero(y))
    5403                 :             :         {                                                       /* y == 0, on X axis */
    5404         [ +  + ]:         290 :                 if (FPzero(x))                  /* (x,y) is (0,0)? */
    5405                 :          38 :                         return POINT_ON_POLYGON;
    5406         [ +  + ]:         252 :                 else if (FPgt(x, 0))
    5407                 :             :                 {                                               /* x > 0 */
    5408         [ +  + ]:         168 :                         if (FPzero(prev_y)) /* y and prev_y are zero */
    5409                 :             :                                 /* prev_x > 0? */
    5410                 :          10 :                                 return FPgt(prev_x, 0.0) ? 0 : POINT_ON_POLYGON;
    5411                 :         158 :                         return FPlt(prev_y, 0.0) ? 1 : -1;
    5412                 :             :                 }
    5413                 :             :                 else
    5414                 :             :                 {                                               /* x < 0, x not on positive X axis */
    5415         [ +  + ]:          84 :                         if (FPzero(prev_y))
    5416                 :             :                                 /* prev_x < 0? */
    5417                 :           4 :                                 return FPlt(prev_x, 0.0) ? 0 : POINT_ON_POLYGON;
    5418                 :          80 :                         return 0;
    5419                 :             :                 }
    5420                 :             :         }
    5421                 :             :         else
    5422                 :             :         {                                                       /* y != 0 */
    5423                 :             :                 /* compute y crossing direction from previous point */
    5424                 :      189364 :                 y_sign = FPgt(y, 0.0) ? 1 : -1;
    5425                 :             : 
    5426         [ +  + ]:      189364 :                 if (FPzero(prev_y))
    5427                 :             :                         /* previous point was on X axis, so new point is either off or on */
    5428         [ +  + ]:         254 :                         return FPlt(prev_x, 0.0) ? 0 : y_sign;
    5429   [ +  +  +  + ]:      285785 :                 else if ((y_sign < 0 && FPlt(prev_y, 0.0)) ||
    5430         [ +  - ]:       96675 :                                  (y_sign > 0 && FPgt(prev_y, 0.0)))
    5431                 :             :                         /* both above or below X axis */
    5432                 :      120414 :                         return 0;                       /* same sign */
    5433                 :             :                 else
    5434                 :             :                 {                                               /* y and prev_y cross X-axis */
    5435   [ +  +  +  + ]:       68696 :                         if (FPge(x, 0.0) && FPgt(prev_x, 0.0))
    5436                 :             :                                 /* both non-negative so cross positive X-axis */
    5437                 :       24981 :                                 return 2 * y_sign;
    5438   [ +  +  +  + ]:       43715 :                         if (FPlt(x, 0.0) && FPle(prev_x, 0.0))
    5439                 :             :                                 /* both non-positive so do not cross positive X-axis */
    5440                 :       22074 :                                 return 0;
    5441                 :             : 
    5442                 :             :                         /* x and y cross axes, see URL above point_inside() */
    5443                 :       43282 :                         z = float8_mi(float8_mul(float8_mi(x, prev_x), y),
    5444                 :       21641 :                                                   float8_mul(float8_mi(y, prev_y), x));
    5445         [ +  + ]:       21641 :                         if (FPzero(z))
    5446                 :           2 :                                 return POINT_ON_POLYGON;
    5447   [ +  +  +  + ]:       34055 :                         if ((y_sign < 0 && FPlt(z, 0.0)) ||
    5448         [ +  - ]:       12416 :                                 (y_sign > 0 && FPgt(z, 0.0)))
    5449                 :       12114 :                                 return 0;
    5450                 :        9525 :                         return 2 * y_sign;
    5451                 :             :                 }
    5452                 :             :         }
    5453                 :      189654 : }
    5454                 :             : 
    5455                 :             : 
    5456                 :             : static bool
    5457                 :        1015 : plist_same(int npts, Point *p1, Point *p2)
    5458                 :             : {
    5459                 :        1015 :         int                     i,
    5460                 :             :                                 ii,
    5461                 :             :                                 j;
    5462                 :             : 
    5463                 :             :         /* find match for first point */
    5464         [ +  + ]:        1043 :         for (i = 0; i < npts; i++)
    5465                 :             :         {
    5466         [ +  + ]:        1037 :                 if (point_eq_point(&p2[i], &p1[0]))
    5467                 :             :                 {
    5468                 :             : 
    5469                 :             :                         /* match found? then look forward through remaining points */
    5470         [ +  + ]:        3030 :                         for (ii = 1, j = i + 1; ii < npts; ii++, j++)
    5471                 :             :                         {
    5472         [ +  + ]:        2023 :                                 if (j >= npts)
    5473                 :           3 :                                         j = 0;
    5474         [ +  + ]:        2023 :                                 if (!point_eq_point(&p2[j], &p1[ii]))
    5475                 :           6 :                                         break;
    5476                 :        2017 :                         }
    5477         [ +  + ]:        1013 :                         if (ii == npts)
    5478                 :        1007 :                                 return true;
    5479                 :             : 
    5480                 :             :                         /* match not found forwards? then look backwards */
    5481         [ +  + ]:          14 :                         for (ii = 1, j = i - 1; ii < npts; ii++, j--)
    5482                 :             :                         {
    5483         [ +  + ]:          12 :                                 if (j < 0)
    5484                 :           2 :                                         j = (npts - 1);
    5485         [ +  + ]:          12 :                                 if (!point_eq_point(&p2[j], &p1[ii]))
    5486                 :           4 :                                         break;
    5487                 :           8 :                         }
    5488         [ +  + ]:           6 :                         if (ii == npts)
    5489                 :           2 :                                 return true;
    5490                 :           4 :                 }
    5491                 :          28 :         }
    5492                 :             : 
    5493                 :           6 :         return false;
    5494                 :        1015 : }
        

Generated by: LCOV version 2.3.2-1