Header And Logo

PostgreSQL
| The world's most advanced open source database.

Defines | Functions

geo_ops.c File Reference

#include "postgres.h"
#include <math.h>
#include <limits.h>
#include <float.h>
#include <ctype.h>
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/geo_decls.h"
Include dependency graph for geo_ops.c:

Go to the source code of this file.

Defines

#define M_PI   3.14159265358979323846
#define LDELIM   '('
#define RDELIM   ')'
#define DELIM   ','
#define LDELIM_EP   '['
#define RDELIM_EP   ']'
#define LDELIM_C   '<'
#define RDELIM_C   '>'
#define P_MAXLEN   (2*(DBL_DIG+3+7)+1)
#define POINTEQ(pt1, pt2)   (FPeq((pt1)->x, (pt2)->x) && FPeq((pt1)->y, (pt2)->y))
#define POINT_ON_POLYGON   INT_MAX

Functions

static int point_inside (Point *p, int npts, Point *plist)
static int lseg_crossing (double x, double y, double px, double py)
static BOXbox_construct (double x1, double x2, double y1, double y2)
static BOXbox_copy (BOX *box)
static BOXbox_fill (BOX *result, double x1, double x2, double y1, double y2)
static bool box_ov (BOX *box1, BOX *box2)
static double box_ht (BOX *box)
static double box_wd (BOX *box)
static double circle_ar (CIRCLE *circle)
static CIRCLEcircle_copy (CIRCLE *circle)
static LINEline_construct_pm (Point *pt, double m)
static void line_construct_pts (LINE *line, Point *pt1, Point *pt2)
static bool lseg_intersect_internal (LSEG *l1, LSEG *l2)
static double lseg_dt (LSEG *l1, LSEG *l2)
static bool on_ps_internal (Point *pt, LSEG *lseg)
static void make_bound_box (POLYGON *poly)
static bool plist_same (int npts, Point *p1, Point *p2)
static Pointpoint_construct (double x, double y)
static Pointpoint_copy (Point *pt)
static int single_decode (char *str, float8 *x, char **ss)
static int single_encode (float8 x, char *str)
static int pair_decode (char *str, float8 *x, float8 *y, char **s)
static int pair_encode (float8 x, float8 y, char *str)
static int pair_count (char *s, char delim)
static int path_decode (int opentype, int npts, char *str, int *isopen, char **ss, Point *p)
static char * path_encode (bool closed, int npts, Point *pt)
static void statlseg_construct (LSEG *lseg, Point *pt1, Point *pt2)
static double box_ar (BOX *box)
static void box_cn (Point *center, BOX *box)
static Pointinterpt_sl (LSEG *lseg, LINE *line)
static bool has_interpt_sl (LSEG *lseg, LINE *line)
static double dist_pl_internal (Point *pt, LINE *line)
static double dist_ps_internal (Point *pt, LSEG *lseg)
static Pointline_interpt_internal (LINE *l1, LINE *l2)
static bool lseg_inside_poly (Point *a, Point *b, POLYGON *poly, int start)
static Pointlseg_interpt_internal (LSEG *l1, LSEG *l2)
Datum box_in (PG_FUNCTION_ARGS)
Datum box_out (PG_FUNCTION_ARGS)
Datum box_recv (PG_FUNCTION_ARGS)
Datum box_send (PG_FUNCTION_ARGS)
Datum box_same (PG_FUNCTION_ARGS)
Datum box_overlap (PG_FUNCTION_ARGS)
Datum box_left (PG_FUNCTION_ARGS)
Datum box_overleft (PG_FUNCTION_ARGS)
Datum box_right (PG_FUNCTION_ARGS)
Datum box_overright (PG_FUNCTION_ARGS)
Datum box_below (PG_FUNCTION_ARGS)
Datum box_overbelow (PG_FUNCTION_ARGS)
Datum box_above (PG_FUNCTION_ARGS)
Datum box_overabove (PG_FUNCTION_ARGS)
Datum box_contained (PG_FUNCTION_ARGS)
Datum box_contain (PG_FUNCTION_ARGS)
Datum box_below_eq (PG_FUNCTION_ARGS)
Datum box_above_eq (PG_FUNCTION_ARGS)
Datum box_lt (PG_FUNCTION_ARGS)
Datum box_gt (PG_FUNCTION_ARGS)
Datum box_eq (PG_FUNCTION_ARGS)
Datum box_le (PG_FUNCTION_ARGS)
Datum box_ge (PG_FUNCTION_ARGS)
Datum box_area (PG_FUNCTION_ARGS)
Datum box_width (PG_FUNCTION_ARGS)
Datum box_height (PG_FUNCTION_ARGS)
Datum box_distance (PG_FUNCTION_ARGS)
Datum box_center (PG_FUNCTION_ARGS)
Datum box_intersect (PG_FUNCTION_ARGS)
Datum box_diagonal (PG_FUNCTION_ARGS)
Datum line_in (PG_FUNCTION_ARGS)
Datum line_out (PG_FUNCTION_ARGS)
Datum line_recv (PG_FUNCTION_ARGS)
Datum line_send (PG_FUNCTION_ARGS)
Datum line_construct_pp (PG_FUNCTION_ARGS)
Datum line_intersect (PG_FUNCTION_ARGS)
Datum line_parallel (PG_FUNCTION_ARGS)
Datum line_perp (PG_FUNCTION_ARGS)
Datum line_vertical (PG_FUNCTION_ARGS)
Datum line_horizontal (PG_FUNCTION_ARGS)
Datum line_eq (PG_FUNCTION_ARGS)
Datum line_distance (PG_FUNCTION_ARGS)
Datum line_interpt (PG_FUNCTION_ARGS)
Datum path_area (PG_FUNCTION_ARGS)
Datum path_in (PG_FUNCTION_ARGS)
Datum path_out (PG_FUNCTION_ARGS)
Datum path_recv (PG_FUNCTION_ARGS)
Datum path_send (PG_FUNCTION_ARGS)
Datum path_n_lt (PG_FUNCTION_ARGS)
Datum path_n_gt (PG_FUNCTION_ARGS)
Datum path_n_eq (PG_FUNCTION_ARGS)
Datum path_n_le (PG_FUNCTION_ARGS)
Datum path_n_ge (PG_FUNCTION_ARGS)
Datum path_isclosed (PG_FUNCTION_ARGS)
Datum path_isopen (PG_FUNCTION_ARGS)
Datum path_npoints (PG_FUNCTION_ARGS)
Datum path_close (PG_FUNCTION_ARGS)
Datum path_open (PG_FUNCTION_ARGS)
Datum path_inter (PG_FUNCTION_ARGS)
Datum path_distance (PG_FUNCTION_ARGS)
Datum path_length (PG_FUNCTION_ARGS)
Datum point_in (PG_FUNCTION_ARGS)
Datum point_out (PG_FUNCTION_ARGS)
Datum point_recv (PG_FUNCTION_ARGS)
Datum point_send (PG_FUNCTION_ARGS)
Datum point_left (PG_FUNCTION_ARGS)
Datum point_right (PG_FUNCTION_ARGS)
Datum point_above (PG_FUNCTION_ARGS)
Datum point_below (PG_FUNCTION_ARGS)
Datum point_vert (PG_FUNCTION_ARGS)
Datum point_horiz (PG_FUNCTION_ARGS)
Datum point_eq (PG_FUNCTION_ARGS)
Datum point_ne (PG_FUNCTION_ARGS)
Datum point_distance (PG_FUNCTION_ARGS)
double point_dt (Point *pt1, Point *pt2)
Datum point_slope (PG_FUNCTION_ARGS)
double point_sl (Point *pt1, Point *pt2)
Datum lseg_in (PG_FUNCTION_ARGS)
Datum lseg_out (PG_FUNCTION_ARGS)
Datum lseg_recv (PG_FUNCTION_ARGS)
Datum lseg_send (PG_FUNCTION_ARGS)
Datum lseg_construct (PG_FUNCTION_ARGS)
Datum lseg_length (PG_FUNCTION_ARGS)
Datum lseg_intersect (PG_FUNCTION_ARGS)
Datum lseg_parallel (PG_FUNCTION_ARGS)
Datum lseg_perp (PG_FUNCTION_ARGS)
Datum lseg_vertical (PG_FUNCTION_ARGS)
Datum lseg_horizontal (PG_FUNCTION_ARGS)
Datum lseg_eq (PG_FUNCTION_ARGS)
Datum lseg_ne (PG_FUNCTION_ARGS)
Datum lseg_lt (PG_FUNCTION_ARGS)
Datum lseg_le (PG_FUNCTION_ARGS)
Datum lseg_gt (PG_FUNCTION_ARGS)
Datum lseg_ge (PG_FUNCTION_ARGS)
Datum lseg_distance (PG_FUNCTION_ARGS)
Datum lseg_center (PG_FUNCTION_ARGS)
Datum lseg_interpt (PG_FUNCTION_ARGS)
Datum dist_pl (PG_FUNCTION_ARGS)
Datum dist_ps (PG_FUNCTION_ARGS)
Datum dist_ppath (PG_FUNCTION_ARGS)
Datum dist_pb (PG_FUNCTION_ARGS)
Datum dist_sl (PG_FUNCTION_ARGS)
Datum dist_sb (PG_FUNCTION_ARGS)
Datum dist_lb (PG_FUNCTION_ARGS)
Datum dist_cpoly (PG_FUNCTION_ARGS)
Datum close_pl (PG_FUNCTION_ARGS)
Datum close_ps (PG_FUNCTION_ARGS)
Datum close_lseg (PG_FUNCTION_ARGS)
Datum close_pb (PG_FUNCTION_ARGS)
Datum close_sl (PG_FUNCTION_ARGS)
Datum close_ls (PG_FUNCTION_ARGS)
Datum close_sb (PG_FUNCTION_ARGS)
Datum close_lb (PG_FUNCTION_ARGS)
Datum on_pl (PG_FUNCTION_ARGS)
Datum on_ps (PG_FUNCTION_ARGS)
Datum on_pb (PG_FUNCTION_ARGS)
Datum box_contain_pt (PG_FUNCTION_ARGS)
Datum on_ppath (PG_FUNCTION_ARGS)
Datum on_sl (PG_FUNCTION_ARGS)
Datum on_sb (PG_FUNCTION_ARGS)
Datum inter_sl (PG_FUNCTION_ARGS)
Datum inter_sb (PG_FUNCTION_ARGS)
Datum inter_lb (PG_FUNCTION_ARGS)
Datum poly_in (PG_FUNCTION_ARGS)
Datum poly_out (PG_FUNCTION_ARGS)
Datum poly_recv (PG_FUNCTION_ARGS)
Datum poly_send (PG_FUNCTION_ARGS)
Datum poly_left (PG_FUNCTION_ARGS)
Datum poly_overleft (PG_FUNCTION_ARGS)
Datum poly_right (PG_FUNCTION_ARGS)
Datum poly_overright (PG_FUNCTION_ARGS)
Datum poly_below (PG_FUNCTION_ARGS)
Datum poly_overbelow (PG_FUNCTION_ARGS)
Datum poly_above (PG_FUNCTION_ARGS)
Datum poly_overabove (PG_FUNCTION_ARGS)
Datum poly_same (PG_FUNCTION_ARGS)
Datum poly_overlap (PG_FUNCTION_ARGS)
static bool touched_lseg_inside_poly (Point *a, Point *b, LSEG *s, POLYGON *poly, int start)
Datum poly_contain (PG_FUNCTION_ARGS)
Datum poly_contained (PG_FUNCTION_ARGS)
Datum poly_contain_pt (PG_FUNCTION_ARGS)
Datum pt_contained_poly (PG_FUNCTION_ARGS)
Datum poly_distance (PG_FUNCTION_ARGS)
Datum construct_point (PG_FUNCTION_ARGS)
Datum point_add (PG_FUNCTION_ARGS)
Datum point_sub (PG_FUNCTION_ARGS)
Datum point_mul (PG_FUNCTION_ARGS)
Datum point_div (PG_FUNCTION_ARGS)
Datum points_box (PG_FUNCTION_ARGS)
Datum box_add (PG_FUNCTION_ARGS)
Datum box_sub (PG_FUNCTION_ARGS)
Datum box_mul (PG_FUNCTION_ARGS)
Datum box_div (PG_FUNCTION_ARGS)
Datum path_add (PG_FUNCTION_ARGS)
Datum path_add_pt (PG_FUNCTION_ARGS)
Datum path_sub_pt (PG_FUNCTION_ARGS)
Datum path_mul_pt (PG_FUNCTION_ARGS)
Datum path_div_pt (PG_FUNCTION_ARGS)
Datum path_center (PG_FUNCTION_ARGS)
Datum path_poly (PG_FUNCTION_ARGS)
Datum poly_npoints (PG_FUNCTION_ARGS)
Datum poly_center (PG_FUNCTION_ARGS)
Datum poly_box (PG_FUNCTION_ARGS)
Datum box_poly (PG_FUNCTION_ARGS)
Datum poly_path (PG_FUNCTION_ARGS)
Datum circle_in (PG_FUNCTION_ARGS)
Datum circle_out (PG_FUNCTION_ARGS)
Datum circle_recv (PG_FUNCTION_ARGS)
Datum circle_send (PG_FUNCTION_ARGS)
Datum circle_same (PG_FUNCTION_ARGS)
Datum circle_overlap (PG_FUNCTION_ARGS)
Datum circle_overleft (PG_FUNCTION_ARGS)
Datum circle_left (PG_FUNCTION_ARGS)
Datum circle_right (PG_FUNCTION_ARGS)
Datum circle_overright (PG_FUNCTION_ARGS)
Datum circle_contained (PG_FUNCTION_ARGS)
Datum circle_contain (PG_FUNCTION_ARGS)
Datum circle_below (PG_FUNCTION_ARGS)
Datum circle_above (PG_FUNCTION_ARGS)
Datum circle_overbelow (PG_FUNCTION_ARGS)
Datum circle_overabove (PG_FUNCTION_ARGS)
Datum circle_eq (PG_FUNCTION_ARGS)
Datum circle_ne (PG_FUNCTION_ARGS)
Datum circle_lt (PG_FUNCTION_ARGS)
Datum circle_gt (PG_FUNCTION_ARGS)
Datum circle_le (PG_FUNCTION_ARGS)
Datum circle_ge (PG_FUNCTION_ARGS)
Datum circle_add_pt (PG_FUNCTION_ARGS)
Datum circle_sub_pt (PG_FUNCTION_ARGS)
Datum circle_mul_pt (PG_FUNCTION_ARGS)
Datum circle_div_pt (PG_FUNCTION_ARGS)
Datum circle_area (PG_FUNCTION_ARGS)
Datum circle_diameter (PG_FUNCTION_ARGS)
Datum circle_radius (PG_FUNCTION_ARGS)
Datum circle_distance (PG_FUNCTION_ARGS)
Datum circle_contain_pt (PG_FUNCTION_ARGS)
Datum pt_contained_circle (PG_FUNCTION_ARGS)
Datum dist_pc (PG_FUNCTION_ARGS)
Datum circle_center (PG_FUNCTION_ARGS)
Datum cr_circle (PG_FUNCTION_ARGS)
Datum circle_box (PG_FUNCTION_ARGS)
Datum box_circle (PG_FUNCTION_ARGS)
Datum circle_poly (PG_FUNCTION_ARGS)
Datum poly_circle (PG_FUNCTION_ARGS)
double pg_hypot (double x, double y)

Define Documentation

#define DELIM   ','

Definition at line 81 of file geo_ops.c.

Referenced by circle_in(), pair_decode(), and path_decode().

#define LDELIM   '('

Definition at line 79 of file geo_ops.c.

Referenced by circle_in(), pair_decode(), path_decode(), and path_in().

#define LDELIM_C   '<'

Definition at line 84 of file geo_ops.c.

Referenced by circle_in().

#define LDELIM_EP   '['

Definition at line 82 of file geo_ops.c.

Referenced by path_decode().

#define M_PI   3.14159265358979323846

Definition at line 27 of file geo_ops.c.

Referenced by circle_ar().

#define P_MAXLEN   (2*(DBL_DIG+3+7)+1)

Definition at line 89 of file geo_ops.c.

Referenced by circle_out(), and path_encode().

#define POINT_ON_POLYGON   INT_MAX

Definition at line 5248 of file geo_ops.c.

Referenced by lseg_crossing(), and point_inside().

#define POINTEQ (   pt1,
  pt2 
)    (FPeq((pt1)->x, (pt2)->x) && FPeq((pt1)->y, (pt2)->y))
#define RDELIM   ')'

Definition at line 80 of file geo_ops.c.

Referenced by circle_in(), pair_decode(), path_decode(), and path_in().

#define RDELIM_C   '>'

Definition at line 85 of file geo_ops.c.

#define RDELIM_EP   ']'

Definition at line 83 of file geo_ops.c.


Function Documentation

Datum box_above ( PG_FUNCTION_ARGS   ) 

Definition at line 645 of file geo_ops.c.

References FPgt, BOX::high, BOX::low, PG_GETARG_BOX_P, PG_RETURN_BOOL, and Point::y.

Referenced by gist_box_leaf_consistent(), and rtree_internal_consistent().

{
    BOX        *box1 = PG_GETARG_BOX_P(0);
    BOX        *box2 = PG_GETARG_BOX_P(1);

    PG_RETURN_BOOL(FPgt(box1->low.y, box2->high.y));
}

Datum box_above_eq ( PG_FUNCTION_ARGS   ) 

Definition at line 712 of file geo_ops.c.

References FPge, BOX::high, BOX::low, PG_GETARG_BOX_P, PG_RETURN_BOOL, and Point::y.

{
    BOX        *box1 = PG_GETARG_BOX_P(0);
    BOX        *box2 = PG_GETARG_BOX_P(1);

    PG_RETURN_BOOL(FPge(box1->low.y, box2->high.y));
}

Datum box_add ( PG_FUNCTION_ARGS   ) 

Definition at line 4150 of file geo_ops.c.

References box_construct(), BOX::high, BOX::low, PG_GETARG_BOX_P, PG_GETARG_POINT_P, PG_RETURN_BOX_P, Point::x, and Point::y.

{
    BOX        *box = PG_GETARG_BOX_P(0);
    Point      *p = PG_GETARG_POINT_P(1);

    PG_RETURN_BOX_P(box_construct((box->high.x + p->x),
                                  (box->low.x + p->x),
                                  (box->high.y + p->y),
                                  (box->low.y + p->y)));
}

static double box_ar ( BOX box  )  [static]

Definition at line 844 of file geo_ops.c.

References box_ht(), and box_wd().

Referenced by box_area(), box_eq(), box_ge(), box_gt(), box_le(), and box_lt().

{
    return box_wd(box) * box_ht(box);
}

Datum box_area ( PG_FUNCTION_ARGS   ) 

Definition at line 777 of file geo_ops.c.

References box_ar(), PG_GETARG_BOX_P, and PG_RETURN_FLOAT8.

Datum box_below ( PG_FUNCTION_ARGS   ) 

Definition at line 622 of file geo_ops.c.

References FPlt, BOX::high, BOX::low, PG_GETARG_BOX_P, PG_RETURN_BOOL, and Point::y.

Referenced by gist_box_leaf_consistent(), and rtree_internal_consistent().

{
    BOX        *box1 = PG_GETARG_BOX_P(0);
    BOX        *box2 = PG_GETARG_BOX_P(1);

    PG_RETURN_BOOL(FPlt(box1->high.y, box2->low.y));
}

Datum box_below_eq ( PG_FUNCTION_ARGS   ) 

Definition at line 703 of file geo_ops.c.

References FPle, BOX::high, BOX::low, PG_GETARG_BOX_P, PG_RETURN_BOOL, and Point::y.

{
    BOX        *box1 = PG_GETARG_BOX_P(0);
    BOX        *box2 = PG_GETARG_BOX_P(1);

    PG_RETURN_BOOL(FPle(box1->high.y, box2->low.y));
}

Datum box_center ( PG_FUNCTION_ARGS   ) 

Definition at line 830 of file geo_ops.c.

References box_cn(), palloc(), PG_GETARG_BOX_P, and PG_RETURN_POINT_P.

{
    BOX        *box = PG_GETARG_BOX_P(0);
    Point      *result = (Point *) palloc(sizeof(Point));

    box_cn(result, box);

    PG_RETURN_POINT_P(result);
}

Datum box_circle ( PG_FUNCTION_ARGS   ) 

Definition at line 5122 of file geo_ops.c.

References CIRCLE::center, BOX::high, BOX::low, palloc(), PG_GETARG_BOX_P, PG_RETURN_CIRCLE_P, point_dt(), CIRCLE::radius, Point::x, and Point::y.

{
    BOX        *box = PG_GETARG_BOX_P(0);
    CIRCLE     *circle;

    circle = (CIRCLE *) palloc(sizeof(CIRCLE));

    circle->center.x = (box->high.x + box->low.x) / 2;
    circle->center.y = (box->high.y + box->low.y) / 2;

    circle->radius = point_dt(&circle->center, &box->high);

    PG_RETURN_CIRCLE_P(circle);
}

static void box_cn ( Point center,
BOX box 
) [static]

Definition at line 853 of file geo_ops.c.

References BOX::high, BOX::low, Point::x, and Point::y.

Referenced by box_center(), box_distance(), and close_sb().

{
    center->x = (box->high.x + box->low.x) / 2.0;
    center->y = (box->high.y + box->low.y) / 2.0;
}

static BOX * box_construct ( double  x1,
double  x2,
double  y1,
double  y2 
) [static]

Definition at line 477 of file geo_ops.c.

References box_fill(), and palloc().

Referenced by box_add(), box_div(), box_mul(), box_sub(), and points_box().

{
    BOX        *result = (BOX *) palloc(sizeof(BOX));

    return box_fill(result, x1, x2, y1, y2);
}

Datum box_contain ( PG_FUNCTION_ARGS   ) 

Definition at line 682 of file geo_ops.c.

References FPge, FPle, BOX::high, BOX::low, PG_GETARG_BOX_P, PG_RETURN_BOOL, Point::x, and Point::y.

Referenced by gist_box_leaf_consistent(), poly_contain(), and rtree_internal_consistent().

{
    BOX        *box1 = PG_GETARG_BOX_P(0);
    BOX        *box2 = PG_GETARG_BOX_P(1);

    PG_RETURN_BOOL(FPge(box1->high.x, box2->high.x) &&
                   FPle(box1->low.x, box2->low.x) &&
                   FPge(box1->high.y, box2->high.y) &&
                   FPle(box1->low.y, box2->low.y));
}

Datum box_contain_pt ( PG_FUNCTION_ARGS   ) 

Definition at line 3210 of file geo_ops.c.

References BOX::high, BOX::low, PG_GETARG_BOX_P, PG_GETARG_POINT_P, PG_RETURN_BOOL, Point::x, and Point::y.

Referenced by spg_quad_inner_consistent(), and spg_quad_leaf_consistent().

{
    BOX        *box = PG_GETARG_BOX_P(0);
    Point      *pt = PG_GETARG_POINT_P(1);

    PG_RETURN_BOOL(pt->x <= box->high.x && pt->x >= box->low.x &&
                   pt->y <= box->high.y && pt->y >= box->low.y);
}

Datum box_contained ( PG_FUNCTION_ARGS   ) 

Definition at line 668 of file geo_ops.c.

References FPge, FPle, BOX::high, BOX::low, PG_GETARG_BOX_P, PG_RETURN_BOOL, Point::x, and Point::y.

Referenced by gist_box_leaf_consistent().

{
    BOX        *box1 = PG_GETARG_BOX_P(0);
    BOX        *box2 = PG_GETARG_BOX_P(1);

    PG_RETURN_BOOL(FPle(box1->high.x, box2->high.x) &&
                   FPge(box1->low.x, box2->low.x) &&
                   FPle(box1->high.y, box2->high.y) &&
                   FPge(box1->low.y, box2->low.y));
}

static BOX * box_copy ( BOX box  )  [static]

Definition at line 518 of file geo_ops.c.

References palloc().

Referenced by poly_box().

{
    BOX        *result = (BOX *) palloc(sizeof(BOX));

    memcpy((char *) result, (char *) box, sizeof(BOX));

    return result;
}

Datum box_diagonal ( PG_FUNCTION_ARGS   ) 

Definition at line 914 of file geo_ops.c.

References BOX::high, BOX::low, palloc(), PG_GETARG_BOX_P, PG_RETURN_LSEG_P, and statlseg_construct().

{
    BOX        *box = PG_GETARG_BOX_P(0);
    LSEG       *result = (LSEG *) palloc(sizeof(LSEG));

    statlseg_construct(result, &box->high, &box->low);

    PG_RETURN_LSEG_P(result);
}

Datum box_distance ( PG_FUNCTION_ARGS   ) 

Definition at line 813 of file geo_ops.c.

References box_cn(), HYPOT, PG_GETARG_BOX_P, PG_RETURN_FLOAT8, Point::x, and Point::y.

{
    BOX        *box1 = PG_GETARG_BOX_P(0);
    BOX        *box2 = PG_GETARG_BOX_P(1);
    Point       a,
                b;

    box_cn(&a, box1);
    box_cn(&b, box2);

    PG_RETURN_FLOAT8(HYPOT(a.x - b.x, a.y - b.y));
}

Datum box_div ( PG_FUNCTION_ARGS   ) 
Datum box_eq ( PG_FUNCTION_ARGS   ) 

Definition at line 743 of file geo_ops.c.

References box_ar(), FPeq, PG_GETARG_BOX_P, and PG_RETURN_BOOL.

{
    BOX        *box1 = PG_GETARG_BOX_P(0);
    BOX        *box2 = PG_GETARG_BOX_P(1);

    PG_RETURN_BOOL(FPeq(box_ar(box1), box_ar(box2)));
}

static BOX * box_fill ( BOX result,
double  x1,
double  x2,
double  y1,
double  y2 
) [static]

Definition at line 488 of file geo_ops.c.

References BOX::high, BOX::low, Point::x, and Point::y.

Referenced by box_construct(), box_poly(), and make_bound_box().

{
    if (x1 > x2)
    {
        result->high.x = x1;
        result->low.x = x2;
    }
    else
    {
        result->high.x = x2;
        result->low.x = x1;
    }
    if (y1 > y2)
    {
        result->high.y = y1;
        result->low.y = y2;
    }
    else
    {
        result->high.y = y2;
        result->low.y = y1;
    }

    return result;
}

Datum box_ge ( PG_FUNCTION_ARGS   ) 

Definition at line 761 of file geo_ops.c.

References box_ar(), FPge, PG_GETARG_BOX_P, and PG_RETURN_BOOL.

{
    BOX        *box1 = PG_GETARG_BOX_P(0);
    BOX        *box2 = PG_GETARG_BOX_P(1);

    PG_RETURN_BOOL(FPge(box_ar(box1), box_ar(box2)));
}

Datum box_gt ( PG_FUNCTION_ARGS   ) 

Definition at line 734 of file geo_ops.c.

References box_ar(), FPgt, PG_GETARG_BOX_P, and PG_RETURN_BOOL.

{
    BOX        *box1 = PG_GETARG_BOX_P(0);
    BOX        *box2 = PG_GETARG_BOX_P(1);

    PG_RETURN_BOOL(FPgt(box_ar(box1), box_ar(box2)));
}

Datum box_height ( PG_FUNCTION_ARGS   ) 

Definition at line 801 of file geo_ops.c.

References BOX::high, BOX::low, PG_GETARG_BOX_P, PG_RETURN_FLOAT8, and Point::y.

{
    BOX        *box = PG_GETARG_BOX_P(0);

    PG_RETURN_FLOAT8(box->high.y - box->low.y);
}

static double box_ht ( BOX box  )  [static]

Definition at line 874 of file geo_ops.c.

References BOX::high, BOX::low, and Point::y.

Referenced by box_ar().

{
    return box->high.y - box->low.y;
}

Datum box_in ( PG_FUNCTION_ARGS   ) 

Definition at line 379 of file geo_ops.c.

References ereport, errcode(), errmsg(), ERROR, FALSE, BOX::high, BOX::low, palloc(), path_decode(), PG_GETARG_CSTRING, PG_RETURN_BOX_P, Point::x, and Point::y.

{
    char       *str = PG_GETARG_CSTRING(0);
    BOX        *box = (BOX *) palloc(sizeof(BOX));
    int         isopen;
    char       *s;
    double      x,
                y;

    if ((!path_decode(FALSE, 2, str, &isopen, &s, &(box->high)))
        || (*s != '\0'))
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                 errmsg("invalid input syntax for type box: \"%s\"", str)));

    /* reorder corners if necessary... */
    if (box->high.x < box->low.x)
    {
        x = box->high.x;
        box->high.x = box->low.x;
        box->low.x = x;
    }
    if (box->high.y < box->low.y)
    {
        y = box->high.y;
        box->high.y = box->low.y;
        box->low.y = y;
    }

    PG_RETURN_BOX_P(box);
}

Datum box_intersect ( PG_FUNCTION_ARGS   ) 

Definition at line 889 of file geo_ops.c.

References box_ov(), BOX::high, BOX::low, Max, Min, palloc(), PG_GETARG_BOX_P, PG_RETURN_BOX_P, PG_RETURN_NULL, Point::x, and Point::y.

{
    BOX        *box1 = PG_GETARG_BOX_P(0);
    BOX        *box2 = PG_GETARG_BOX_P(1);
    BOX        *result;

    if (!box_ov(box1, box2))
        PG_RETURN_NULL();

    result = (BOX *) palloc(sizeof(BOX));

    result->high.x = Min(box1->high.x, box2->high.x);
    result->low.x = Max(box1->low.x, box2->low.x);
    result->high.y = Min(box1->high.y, box2->high.y);
    result->low.y = Max(box1->low.y, box2->low.y);

    PG_RETURN_BOX_P(result);
}

Datum box_le ( PG_FUNCTION_ARGS   ) 

Definition at line 752 of file geo_ops.c.

References box_ar(), FPle, PG_GETARG_BOX_P, and PG_RETURN_BOOL.

{
    BOX        *box1 = PG_GETARG_BOX_P(0);
    BOX        *box2 = PG_GETARG_BOX_P(1);

    PG_RETURN_BOOL(FPle(box_ar(box1), box_ar(box2)));
}

Datum box_left ( PG_FUNCTION_ARGS   ) 

Definition at line 570 of file geo_ops.c.

References FPlt, BOX::high, BOX::low, PG_GETARG_BOX_P, PG_RETURN_BOOL, and Point::x.

Referenced by gist_box_leaf_consistent(), and rtree_internal_consistent().

{
    BOX        *box1 = PG_GETARG_BOX_P(0);
    BOX        *box2 = PG_GETARG_BOX_P(1);

    PG_RETURN_BOOL(FPlt(box1->high.x, box2->low.x));
}

Datum box_lt ( PG_FUNCTION_ARGS   ) 

Definition at line 725 of file geo_ops.c.

References box_ar(), FPlt, PG_GETARG_BOX_P, and PG_RETURN_BOOL.

{
    BOX        *box1 = PG_GETARG_BOX_P(0);
    BOX        *box2 = PG_GETARG_BOX_P(1);

    PG_RETURN_BOOL(FPlt(box_ar(box1), box_ar(box2)));
}

Datum box_mul ( PG_FUNCTION_ARGS   ) 
Datum box_out ( PG_FUNCTION_ARGS   ) 

Definition at line 414 of file geo_ops.c.

References BOX::high, path_encode(), PG_GETARG_BOX_P, and PG_RETURN_CSTRING.

{
    BOX        *box = PG_GETARG_BOX_P(0);

    PG_RETURN_CSTRING(path_encode(-1, 2, &(box->high)));
}

static bool box_ov ( BOX box1,
BOX box2 
) [static]

Definition at line 559 of file geo_ops.c.

References FPle, BOX::high, BOX::low, Point::x, and Point::y.

Referenced by box_intersect(), box_overlap(), inter_sb(), path_inter(), and poly_overlap().

{
    return (FPle(box1->low.x, box2->high.x) &&
            FPle(box2->low.x, box1->high.x) &&
            FPle(box1->low.y, box2->high.y) &&
            FPle(box2->low.y, box1->high.y));
}

Datum box_overabove ( PG_FUNCTION_ARGS   ) 

Definition at line 657 of file geo_ops.c.

References FPge, BOX::low, PG_GETARG_BOX_P, PG_RETURN_BOOL, and Point::y.

Referenced by gist_box_leaf_consistent(), and rtree_internal_consistent().

{
    BOX        *box1 = PG_GETARG_BOX_P(0);
    BOX        *box2 = PG_GETARG_BOX_P(1);

    PG_RETURN_BOOL(FPge(box1->low.y, box2->low.y));
}

Datum box_overbelow ( PG_FUNCTION_ARGS   ) 

Definition at line 634 of file geo_ops.c.

References FPle, BOX::high, PG_GETARG_BOX_P, PG_RETURN_BOOL, and Point::y.

Referenced by gist_box_leaf_consistent(), and rtree_internal_consistent().

{
    BOX        *box1 = PG_GETARG_BOX_P(0);
    BOX        *box2 = PG_GETARG_BOX_P(1);

    PG_RETURN_BOOL(FPle(box1->high.y, box2->high.y));
}

Datum box_overlap ( PG_FUNCTION_ARGS   ) 

Definition at line 550 of file geo_ops.c.

References box_ov(), PG_GETARG_BOX_P, and PG_RETURN_BOOL.

Referenced by gist_box_leaf_consistent(), and rtree_internal_consistent().

{
    BOX        *box1 = PG_GETARG_BOX_P(0);
    BOX        *box2 = PG_GETARG_BOX_P(1);

    PG_RETURN_BOOL(box_ov(box1, box2));
}

Datum box_overleft ( PG_FUNCTION_ARGS   ) 

Definition at line 585 of file geo_ops.c.

References FPle, BOX::high, PG_GETARG_BOX_P, PG_RETURN_BOOL, and Point::x.

Referenced by gist_box_leaf_consistent(), and rtree_internal_consistent().

{
    BOX        *box1 = PG_GETARG_BOX_P(0);
    BOX        *box2 = PG_GETARG_BOX_P(1);

    PG_RETURN_BOOL(FPle(box1->high.x, box2->high.x));
}

Datum box_overright ( PG_FUNCTION_ARGS   ) 

Definition at line 611 of file geo_ops.c.

References FPge, BOX::low, PG_GETARG_BOX_P, PG_RETURN_BOOL, and Point::x.

Referenced by gist_box_leaf_consistent(), and rtree_internal_consistent().

{
    BOX        *box1 = PG_GETARG_BOX_P(0);
    BOX        *box2 = PG_GETARG_BOX_P(1);

    PG_RETURN_BOOL(FPge(box1->low.x, box2->low.x));
}

Datum box_poly ( PG_FUNCTION_ARGS   ) 

Definition at line 4445 of file geo_ops.c.

References POLYGON::boundbox, box_fill(), BOX::high, BOX::low, POLYGON::npts, offsetof, POLYGON::p, palloc(), PG_GETARG_BOX_P, PG_RETURN_POLYGON_P, SET_VARSIZE, Point::x, and Point::y.

{
    BOX        *box = PG_GETARG_BOX_P(0);
    POLYGON    *poly;
    int         size;

    /* map four corners of the box to a polygon */
    size = offsetof(POLYGON, p[0]) +sizeof(poly->p[0]) * 4;
    poly = (POLYGON *) palloc(size);

    SET_VARSIZE(poly, size);
    poly->npts = 4;

    poly->p[0].x = box->low.x;
    poly->p[0].y = box->low.y;
    poly->p[1].x = box->low.x;
    poly->p[1].y = box->high.y;
    poly->p[2].x = box->high.x;
    poly->p[2].y = box->high.y;
    poly->p[3].x = box->high.x;
    poly->p[3].y = box->low.y;

    box_fill(&poly->boundbox, box->high.x, box->low.x,
             box->high.y, box->low.y);

    PG_RETURN_POLYGON_P(poly);
}

Datum box_recv ( PG_FUNCTION_ARGS   ) 

Definition at line 425 of file geo_ops.c.

References buf, BOX::high, BOX::low, palloc(), PG_GETARG_POINTER, PG_RETURN_BOX_P, pq_getmsgfloat8(), Point::x, and Point::y.

{
    StringInfo  buf = (StringInfo) PG_GETARG_POINTER(0);
    BOX        *box;
    double      x,
                y;

    box = (BOX *) palloc(sizeof(BOX));

    box->high.x = pq_getmsgfloat8(buf);
    box->high.y = pq_getmsgfloat8(buf);
    box->low.x = pq_getmsgfloat8(buf);
    box->low.y = pq_getmsgfloat8(buf);

    /* reorder corners if necessary... */
    if (box->high.x < box->low.x)
    {
        x = box->high.x;
        box->high.x = box->low.x;
        box->low.x = x;
    }
    if (box->high.y < box->low.y)
    {
        y = box->high.y;
        box->high.y = box->low.y;
        box->low.y = y;
    }

    PG_RETURN_BOX_P(box);
}

Datum box_right ( PG_FUNCTION_ARGS   ) 

Definition at line 596 of file geo_ops.c.

References FPgt, BOX::high, BOX::low, PG_GETARG_BOX_P, PG_RETURN_BOOL, and Point::x.

Referenced by gist_box_leaf_consistent(), and rtree_internal_consistent().

{
    BOX        *box1 = PG_GETARG_BOX_P(0);
    BOX        *box2 = PG_GETARG_BOX_P(1);

    PG_RETURN_BOOL(FPgt(box1->low.x, box2->high.x));
}

Datum box_same ( PG_FUNCTION_ARGS   ) 

Definition at line 536 of file geo_ops.c.

References FPeq, BOX::high, BOX::low, PG_GETARG_BOX_P, PG_RETURN_BOOL, Point::x, and Point::y.

Referenced by gist_box_leaf_consistent().

{
    BOX        *box1 = PG_GETARG_BOX_P(0);
    BOX        *box2 = PG_GETARG_BOX_P(1);

    PG_RETURN_BOOL(FPeq(box1->high.x, box2->high.x) &&
                   FPeq(box1->low.x, box2->low.x) &&
                   FPeq(box1->high.y, box2->high.y) &&
                   FPeq(box1->low.y, box2->low.y));
}

Datum box_send ( PG_FUNCTION_ARGS   ) 
Datum box_sub ( PG_FUNCTION_ARGS   ) 

Definition at line 4162 of file geo_ops.c.

References box_construct(), BOX::high, BOX::low, PG_GETARG_BOX_P, PG_GETARG_POINT_P, PG_RETURN_BOX_P, Point::x, and Point::y.

{
    BOX        *box = PG_GETARG_BOX_P(0);
    Point      *p = PG_GETARG_POINT_P(1);

    PG_RETURN_BOX_P(box_construct((box->high.x - p->x),
                                  (box->low.x - p->x),
                                  (box->high.y - p->y),
                                  (box->low.y - p->y)));
}

static double box_wd ( BOX box  )  [static]

Definition at line 864 of file geo_ops.c.

References BOX::high, BOX::low, and Point::x.

Referenced by box_ar().

{
    return box->high.x - box->low.x;
}

Datum box_width ( PG_FUNCTION_ARGS   ) 

Definition at line 789 of file geo_ops.c.

References BOX::high, BOX::low, PG_GETARG_BOX_P, PG_RETURN_FLOAT8, and Point::x.

{
    BOX        *box = PG_GETARG_BOX_P(0);

    PG_RETURN_FLOAT8(box->high.x - box->low.x);
}

Datum circle_above ( PG_FUNCTION_ARGS   ) 

Definition at line 4772 of file geo_ops.c.

References CIRCLE::center, FPgt, PG_GETARG_CIRCLE_P, PG_RETURN_BOOL, CIRCLE::radius, and Point::y.

{
    CIRCLE     *circle1 = PG_GETARG_CIRCLE_P(0);
    CIRCLE     *circle2 = PG_GETARG_CIRCLE_P(1);

    PG_RETURN_BOOL(FPgt((circle1->center.y - circle1->radius),
                        (circle2->center.y + circle2->radius)));
}

Datum circle_add_pt ( PG_FUNCTION_ARGS   ) 

Definition at line 4888 of file geo_ops.c.

References CIRCLE::center, circle_copy(), PG_GETARG_CIRCLE_P, PG_GETARG_POINT_P, PG_RETURN_CIRCLE_P, Point::x, and Point::y.

{
    CIRCLE     *circle = PG_GETARG_CIRCLE_P(0);
    Point      *point = PG_GETARG_POINT_P(1);
    CIRCLE     *result;

    result = circle_copy(circle);

    result->center.x += point->x;
    result->center.y += point->y;

    PG_RETURN_CIRCLE_P(result);
}

static double circle_ar ( CIRCLE circle  )  [static]

Definition at line 5073 of file geo_ops.c.

References M_PI, and CIRCLE::radius.

Referenced by circle_area(), circle_eq(), circle_ge(), circle_gt(), circle_le(), circle_lt(), and circle_ne().

{
    return M_PI * (circle->radius * circle->radius);
}

Datum circle_area ( PG_FUNCTION_ARGS   ) 

Definition at line 4965 of file geo_ops.c.

References circle_ar(), PG_GETARG_CIRCLE_P, and PG_RETURN_FLOAT8.

Datum circle_below ( PG_FUNCTION_ARGS   ) 

Definition at line 4760 of file geo_ops.c.

References CIRCLE::center, FPlt, PG_GETARG_CIRCLE_P, PG_RETURN_BOOL, CIRCLE::radius, and Point::y.

{
    CIRCLE     *circle1 = PG_GETARG_CIRCLE_P(0);
    CIRCLE     *circle2 = PG_GETARG_CIRCLE_P(1);

    PG_RETURN_BOOL(FPlt((circle1->center.y + circle1->radius),
                        (circle2->center.y - circle2->radius)));
}

Datum circle_box ( PG_FUNCTION_ARGS   ) 

Definition at line 5100 of file geo_ops.c.

References CIRCLE::center, BOX::high, BOX::low, palloc(), PG_GETARG_CIRCLE_P, PG_RETURN_BOX_P, CIRCLE::radius, Point::x, and Point::y.

{
    CIRCLE     *circle = PG_GETARG_CIRCLE_P(0);
    BOX        *box;
    double      delta;

    box = (BOX *) palloc(sizeof(BOX));

    delta = circle->radius / sqrt(2.0);

    box->high.x = circle->center.x + delta;
    box->low.x = circle->center.x - delta;
    box->high.y = circle->center.y + delta;
    box->low.y = circle->center.y - delta;

    PG_RETURN_BOX_P(box);
}

Datum circle_center ( PG_FUNCTION_ARGS   ) 

Definition at line 5057 of file geo_ops.c.

References CIRCLE::center, palloc(), PG_GETARG_CIRCLE_P, PG_RETURN_POINT_P, Point::x, and Point::y.

Referenced by poly_center().

{
    CIRCLE     *circle = PG_GETARG_CIRCLE_P(0);
    Point      *result;

    result = (Point *) palloc(sizeof(Point));
    result->x = circle->center.x;
    result->y = circle->center.y;

    PG_RETURN_POINT_P(result);
}

Datum circle_contain ( PG_FUNCTION_ARGS   ) 

Definition at line 4748 of file geo_ops.c.

References CIRCLE::center, FPle, PG_GETARG_CIRCLE_P, PG_RETURN_BOOL, point_dt(), and CIRCLE::radius.

{
    CIRCLE     *circle1 = PG_GETARG_CIRCLE_P(0);
    CIRCLE     *circle2 = PG_GETARG_CIRCLE_P(1);

    PG_RETURN_BOOL(FPle((point_dt(&circle1->center, &circle2->center) + circle2->radius), circle1->radius));
}

Datum circle_contain_pt ( PG_FUNCTION_ARGS   ) 

Definition at line 5014 of file geo_ops.c.

References CIRCLE::center, PG_GETARG_CIRCLE_P, PG_GETARG_POINT_P, PG_RETURN_BOOL, and point_dt().

Referenced by gist_point_consistent().

{
    CIRCLE     *circle = PG_GETARG_CIRCLE_P(0);
    Point      *point = PG_GETARG_POINT_P(1);
    double      d;

    d = point_dt(&circle->center, point);
    PG_RETURN_BOOL(d <= circle->radius);
}

Datum circle_contained ( PG_FUNCTION_ARGS   ) 

Definition at line 4737 of file geo_ops.c.

References CIRCLE::center, FPle, PG_GETARG_CIRCLE_P, PG_RETURN_BOOL, point_dt(), and CIRCLE::radius.

{
    CIRCLE     *circle1 = PG_GETARG_CIRCLE_P(0);
    CIRCLE     *circle2 = PG_GETARG_CIRCLE_P(1);

    PG_RETURN_BOOL(FPle((point_dt(&circle1->center, &circle2->center) + circle1->radius), circle2->radius));
}

static CIRCLE * circle_copy ( CIRCLE circle  )  [static]

Definition at line 4871 of file geo_ops.c.

References palloc(), and PointerIsValid.

Referenced by circle_add_pt(), circle_div_pt(), circle_mul_pt(), and circle_sub_pt().

{
    CIRCLE     *result;

    if (!PointerIsValid(circle))
        return NULL;

    result = (CIRCLE *) palloc(sizeof(CIRCLE));
    memcpy((char *) result, (char *) circle, sizeof(CIRCLE));
    return result;
}

Datum circle_diameter ( PG_FUNCTION_ARGS   ) 

Definition at line 4976 of file geo_ops.c.

References PG_GETARG_CIRCLE_P, PG_RETURN_FLOAT8, and CIRCLE::radius.

{
    CIRCLE     *circle = PG_GETARG_CIRCLE_P(0);

    PG_RETURN_FLOAT8(2 * circle->radius);
}

Datum circle_distance ( PG_FUNCTION_ARGS   ) 

Definition at line 4999 of file geo_ops.c.

References CIRCLE::center, PG_GETARG_CIRCLE_P, PG_RETURN_FLOAT8, point_dt(), and CIRCLE::radius.

{
    CIRCLE     *circle1 = PG_GETARG_CIRCLE_P(0);
    CIRCLE     *circle2 = PG_GETARG_CIRCLE_P(1);
    float8      result;

    result = point_dt(&circle1->center, &circle2->center)
        - (circle1->radius + circle2->radius);
    if (result < 0)
        result = 0;
    PG_RETURN_FLOAT8(result);
}

Datum circle_div_pt ( PG_FUNCTION_ARGS   ) 
Datum circle_eq ( PG_FUNCTION_ARGS   ) 

Definition at line 4812 of file geo_ops.c.

References circle_ar(), FPeq, PG_GETARG_CIRCLE_P, and PG_RETURN_BOOL.

{
    CIRCLE     *circle1 = PG_GETARG_CIRCLE_P(0);
    CIRCLE     *circle2 = PG_GETARG_CIRCLE_P(1);

    PG_RETURN_BOOL(FPeq(circle_ar(circle1), circle_ar(circle2)));
}

Datum circle_ge ( PG_FUNCTION_ARGS   ) 

Definition at line 4857 of file geo_ops.c.

References circle_ar(), FPge, PG_GETARG_CIRCLE_P, and PG_RETURN_BOOL.

{
    CIRCLE     *circle1 = PG_GETARG_CIRCLE_P(0);
    CIRCLE     *circle2 = PG_GETARG_CIRCLE_P(1);

    PG_RETURN_BOOL(FPge(circle_ar(circle1), circle_ar(circle2)));
}

Datum circle_gt ( PG_FUNCTION_ARGS   ) 

Definition at line 4839 of file geo_ops.c.

References circle_ar(), FPgt, PG_GETARG_CIRCLE_P, and PG_RETURN_BOOL.

{
    CIRCLE     *circle1 = PG_GETARG_CIRCLE_P(0);
    CIRCLE     *circle2 = PG_GETARG_CIRCLE_P(1);

    PG_RETURN_BOOL(FPgt(circle_ar(circle1), circle_ar(circle2)));
}

Datum circle_in ( PG_FUNCTION_ARGS   ) 

Definition at line 4518 of file geo_ops.c.

References CIRCLE::center, DELIM, ereport, errcode(), errmsg(), ERROR, LDELIM, LDELIM_C, pair_decode(), palloc(), PG_GETARG_CSTRING, PG_RETURN_CIRCLE_P, CIRCLE::radius, RDELIM, single_decode(), Point::x, and Point::y.

{
    char       *str = PG_GETARG_CSTRING(0);
    CIRCLE     *circle;
    char       *s,
               *cp;
    int         depth = 0;

    circle = (CIRCLE *) palloc(sizeof(CIRCLE));

    s = str;
    while (isspace((unsigned char) *s))
        s++;
    if ((*s == LDELIM_C) || (*s == LDELIM))
    {
        depth++;
        cp = (s + 1);
        while (isspace((unsigned char) *cp))
            cp++;
        if (*cp == LDELIM)
            s = cp;
    }

    if (!pair_decode(s, &circle->center.x, &circle->center.y, &s))
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
               errmsg("invalid input syntax for type circle: \"%s\"", str)));

    if (*s == DELIM)
        s++;
    while (isspace((unsigned char) *s))
        s++;

    if ((!single_decode(s, &circle->radius, &s)) || (circle->radius < 0))
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
               errmsg("invalid input syntax for type circle: \"%s\"", str)));

    while (depth > 0)
    {
        if ((*s == RDELIM)
            || ((*s == RDELIM_C) && (depth == 1)))
        {
            depth--;
            s++;
            while (isspace((unsigned char) *s))
                s++;
        }
        else
            ereport(ERROR,
                    (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
               errmsg("invalid input syntax for type circle: \"%s\"", str)));
    }

    if (*s != '\0')
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
               errmsg("invalid input syntax for type circle: \"%s\"", str)));

    PG_RETURN_CIRCLE_P(circle);
}

Datum circle_le ( PG_FUNCTION_ARGS   ) 

Definition at line 4848 of file geo_ops.c.

References circle_ar(), FPle, PG_GETARG_CIRCLE_P, and PG_RETURN_BOOL.

{
    CIRCLE     *circle1 = PG_GETARG_CIRCLE_P(0);
    CIRCLE     *circle2 = PG_GETARG_CIRCLE_P(1);

    PG_RETURN_BOOL(FPle(circle_ar(circle1), circle_ar(circle2)));
}

Datum circle_left ( PG_FUNCTION_ARGS   ) 

Definition at line 4700 of file geo_ops.c.

References CIRCLE::center, FPlt, PG_GETARG_CIRCLE_P, PG_RETURN_BOOL, CIRCLE::radius, and Point::x.

{
    CIRCLE     *circle1 = PG_GETARG_CIRCLE_P(0);
    CIRCLE     *circle2 = PG_GETARG_CIRCLE_P(1);

    PG_RETURN_BOOL(FPlt((circle1->center.x + circle1->radius),
                        (circle2->center.x - circle2->radius)));
}

Datum circle_lt ( PG_FUNCTION_ARGS   ) 

Definition at line 4830 of file geo_ops.c.

References circle_ar(), FPlt, PG_GETARG_CIRCLE_P, and PG_RETURN_BOOL.

{
    CIRCLE     *circle1 = PG_GETARG_CIRCLE_P(0);
    CIRCLE     *circle2 = PG_GETARG_CIRCLE_P(1);

    PG_RETURN_BOOL(FPlt(circle_ar(circle1), circle_ar(circle2)));
}

Datum circle_mul_pt ( PG_FUNCTION_ARGS   ) 
Datum circle_ne ( PG_FUNCTION_ARGS   ) 

Definition at line 4821 of file geo_ops.c.

References circle_ar(), FPne, PG_GETARG_CIRCLE_P, and PG_RETURN_BOOL.

{
    CIRCLE     *circle1 = PG_GETARG_CIRCLE_P(0);
    CIRCLE     *circle2 = PG_GETARG_CIRCLE_P(1);

    PG_RETURN_BOOL(FPne(circle_ar(circle1), circle_ar(circle2)));
}

Datum circle_out ( PG_FUNCTION_ARGS   ) 

Definition at line 4583 of file geo_ops.c.

References CIRCLE::center, ereport, errcode(), errmsg(), ERROR, P_MAXLEN, pair_encode(), palloc(), PG_GETARG_CIRCLE_P, PG_RETURN_CSTRING, CIRCLE::radius, single_encode(), Point::x, and Point::y.

{
    CIRCLE     *circle = PG_GETARG_CIRCLE_P(0);
    char       *result;
    char       *cp;

    result = palloc(2 * P_MAXLEN + 6);

    cp = result;
    *cp++ = LDELIM_C;
    *cp++ = LDELIM;
    if (!pair_encode(circle->center.x, circle->center.y, cp))
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("could not format \"circle\" value")));

    cp += strlen(cp);
    *cp++ = RDELIM;
    *cp++ = DELIM;
    if (!single_encode(circle->radius, cp))
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("could not format \"circle\" value")));

    cp += strlen(cp);
    *cp++ = RDELIM_C;
    *cp = '\0';

    PG_RETURN_CSTRING(result);
}

Datum circle_overabove ( PG_FUNCTION_ARGS   ) 

Definition at line 4798 of file geo_ops.c.

References CIRCLE::center, FPge, PG_GETARG_CIRCLE_P, PG_RETURN_BOOL, CIRCLE::radius, and Point::y.

{
    CIRCLE     *circle1 = PG_GETARG_CIRCLE_P(0);
    CIRCLE     *circle2 = PG_GETARG_CIRCLE_P(1);

    PG_RETURN_BOOL(FPge((circle1->center.y - circle1->radius),
                        (circle2->center.y - circle2->radius)));
}

Datum circle_overbelow ( PG_FUNCTION_ARGS   ) 

Definition at line 4785 of file geo_ops.c.

References CIRCLE::center, FPle, PG_GETARG_CIRCLE_P, PG_RETURN_BOOL, CIRCLE::radius, and Point::y.

{
    CIRCLE     *circle1 = PG_GETARG_CIRCLE_P(0);
    CIRCLE     *circle2 = PG_GETARG_CIRCLE_P(1);

    PG_RETURN_BOOL(FPle((circle1->center.y + circle1->radius),
                        (circle2->center.y + circle2->radius)));
}

Datum circle_overlap ( PG_FUNCTION_ARGS   ) 

Definition at line 4675 of file geo_ops.c.

References CIRCLE::center, FPle, PG_GETARG_CIRCLE_P, PG_RETURN_BOOL, point_dt(), and CIRCLE::radius.

{
    CIRCLE     *circle1 = PG_GETARG_CIRCLE_P(0);
    CIRCLE     *circle2 = PG_GETARG_CIRCLE_P(1);

    PG_RETURN_BOOL(FPle(point_dt(&circle1->center, &circle2->center),
                        circle1->radius + circle2->radius));
}

Datum circle_overleft ( PG_FUNCTION_ARGS   ) 

Definition at line 4688 of file geo_ops.c.

References CIRCLE::center, FPle, PG_GETARG_CIRCLE_P, PG_RETURN_BOOL, CIRCLE::radius, and Point::x.

{
    CIRCLE     *circle1 = PG_GETARG_CIRCLE_P(0);
    CIRCLE     *circle2 = PG_GETARG_CIRCLE_P(1);

    PG_RETURN_BOOL(FPle((circle1->center.x + circle1->radius),
                        (circle2->center.x + circle2->radius)));
}

Datum circle_overright ( PG_FUNCTION_ARGS   ) 

Definition at line 4725 of file geo_ops.c.

References CIRCLE::center, FPge, PG_GETARG_CIRCLE_P, PG_RETURN_BOOL, CIRCLE::radius, and Point::x.

{
    CIRCLE     *circle1 = PG_GETARG_CIRCLE_P(0);
    CIRCLE     *circle2 = PG_GETARG_CIRCLE_P(1);

    PG_RETURN_BOOL(FPge((circle1->center.x - circle1->radius),
                        (circle2->center.x - circle2->radius)));
}

Datum circle_poly ( PG_FUNCTION_ARGS   ) 

Definition at line 5139 of file geo_ops.c.

References CIRCLE::center, ereport, errcode(), errmsg(), ERROR, FPzero, i, make_bound_box(), POLYGON::npts, offsetof, POLYGON::p, palloc0(), PG_GETARG_CIRCLE_P, PG_GETARG_INT32, PG_RETURN_POLYGON_P, CIRCLE::radius, SET_VARSIZE, Point::x, and Point::y.

{
    int32       npts = PG_GETARG_INT32(0);
    CIRCLE     *circle = PG_GETARG_CIRCLE_P(1);
    POLYGON    *poly;
    int         base_size,
                size;
    int         i;
    double      angle;
    double      anglestep;

    if (FPzero(circle->radius))
        ereport(ERROR,
                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
               errmsg("cannot convert circle with radius zero to polygon")));

    if (npts < 2)
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("must request at least 2 points")));

    base_size = sizeof(poly->p[0]) * npts;
    size = offsetof(POLYGON, p[0]) +base_size;

    /* Check for integer overflow */
    if (base_size / npts != sizeof(poly->p[0]) || size <= base_size)
        ereport(ERROR,
                (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
                 errmsg("too many points requested")));

    poly = (POLYGON *) palloc0(size);   /* zero any holes */
    SET_VARSIZE(poly, size);
    poly->npts = npts;

    anglestep = (2.0 * M_PI) / npts;

    for (i = 0; i < npts; i++)
    {
        angle = i * anglestep;
        poly->p[i].x = circle->center.x - (circle->radius * cos(angle));
        poly->p[i].y = circle->center.y + (circle->radius * sin(angle));
    }

    make_bound_box(poly);

    PG_RETURN_POLYGON_P(poly);
}

Datum circle_radius ( PG_FUNCTION_ARGS   ) 

Definition at line 4987 of file geo_ops.c.

References PG_GETARG_CIRCLE_P, PG_RETURN_FLOAT8, and CIRCLE::radius.

{
    CIRCLE     *circle = PG_GETARG_CIRCLE_P(0);

    PG_RETURN_FLOAT8(circle->radius);
}

Datum circle_recv ( PG_FUNCTION_ARGS   ) 

Definition at line 4618 of file geo_ops.c.

References buf, CIRCLE::center, ereport, errcode(), errmsg(), ERROR, palloc(), PG_GETARG_POINTER, PG_RETURN_CIRCLE_P, pq_getmsgfloat8(), CIRCLE::radius, Point::x, and Point::y.

{
    StringInfo  buf = (StringInfo) PG_GETARG_POINTER(0);
    CIRCLE     *circle;

    circle = (CIRCLE *) palloc(sizeof(CIRCLE));

    circle->center.x = pq_getmsgfloat8(buf);
    circle->center.y = pq_getmsgfloat8(buf);
    circle->radius = pq_getmsgfloat8(buf);

    if (circle->radius < 0)
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
                 errmsg("invalid radius in external \"circle\" value")));

    PG_RETURN_CIRCLE_P(circle);
}

Datum circle_right ( PG_FUNCTION_ARGS   ) 

Definition at line 4712 of file geo_ops.c.

References CIRCLE::center, FPgt, PG_GETARG_CIRCLE_P, PG_RETURN_BOOL, CIRCLE::radius, and Point::x.

{
    CIRCLE     *circle1 = PG_GETARG_CIRCLE_P(0);
    CIRCLE     *circle2 = PG_GETARG_CIRCLE_P(1);

    PG_RETURN_BOOL(FPgt((circle1->center.x - circle1->radius),
                        (circle2->center.x + circle2->radius)));
}

Datum circle_same ( PG_FUNCTION_ARGS   ) 

Definition at line 4662 of file geo_ops.c.

References CIRCLE::center, FPeq, PG_GETARG_CIRCLE_P, PG_RETURN_BOOL, CIRCLE::radius, Point::x, and Point::y.

{
    CIRCLE     *circle1 = PG_GETARG_CIRCLE_P(0);
    CIRCLE     *circle2 = PG_GETARG_CIRCLE_P(1);

    PG_RETURN_BOOL(FPeq(circle1->radius, circle2->radius) &&
                   FPeq(circle1->center.x, circle2->center.x) &&
                   FPeq(circle1->center.y, circle2->center.y));
}

Datum circle_send ( PG_FUNCTION_ARGS   ) 
Datum circle_sub_pt ( PG_FUNCTION_ARGS   ) 

Definition at line 4903 of file geo_ops.c.

References CIRCLE::center, circle_copy(), PG_GETARG_CIRCLE_P, PG_GETARG_POINT_P, PG_RETURN_CIRCLE_P, Point::x, and Point::y.

{
    CIRCLE     *circle = PG_GETARG_CIRCLE_P(0);
    Point      *point = PG_GETARG_POINT_P(1);
    CIRCLE     *result;

    result = circle_copy(circle);

    result->center.x -= point->x;
    result->center.y -= point->y;

    PG_RETURN_CIRCLE_P(result);
}

Datum close_lb ( PG_FUNCTION_ARGS   ) 

Definition at line 3146 of file geo_ops.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_BOX_P, PG_GETARG_LINE_P, and PG_RETURN_NULL.

{
#ifdef NOT_USED
    LINE       *line = PG_GETARG_LINE_P(0);
    BOX        *box = PG_GETARG_BOX_P(1);
#endif

    /* think about this one for a while */
    ereport(ERROR,
            (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
             errmsg("function \"close_lb\" not implemented")));

    PG_RETURN_NULL();
}

Datum close_ls ( PG_FUNCTION_ARGS   ) 

Definition at line 3063 of file geo_ops.c.

References dist_pl_internal(), interpt_sl(), LSEG::p, PG_GETARG_LINE_P, PG_GETARG_LSEG_P, PG_RETURN_POINT_P, and point_copy().

{
    LINE       *line = PG_GETARG_LINE_P(0);
    LSEG       *lseg = PG_GETARG_LSEG_P(1);
    Point      *result;
    float8      d1,
                d2;

    result = interpt_sl(lseg, line);
    if (result)
        PG_RETURN_POINT_P(result);

    d1 = dist_pl_internal(&lseg->p[0], line);
    d2 = dist_pl_internal(&lseg->p[1], line);
    if (d1 < d2)
        result = point_copy(&lseg->p[0]);
    else
        result = point_copy(&lseg->p[1]);

    PG_RETURN_POINT_P(result);
}

Datum close_lseg ( PG_FUNCTION_ARGS   ) 

Definition at line 2927 of file geo_ops.c.

References close_ps(), DatumGetPointP, DirectFunctionCall2, dist_ps_internal(), LsegPGetDatum, NULL, LSEG::p, PG_GETARG_LSEG_P, PG_RETURN_POINT_P, point_copy(), and PointPGetDatum.

Referenced by close_sb().

{
    LSEG       *l1 = PG_GETARG_LSEG_P(0);
    LSEG       *l2 = PG_GETARG_LSEG_P(1);
    Point      *result = NULL;
    Point       point;
    double      dist;
    double      d;

    d = dist_ps_internal(&l1->p[0], l2);
    dist = d;
    memcpy(&point, &l1->p[0], sizeof(Point));

    if ((d = dist_ps_internal(&l1->p[1], l2)) < dist)
    {
        dist = d;
        memcpy(&point, &l1->p[1], sizeof(Point));
    }

    if (dist_ps_internal(&l2->p[0], l1) < dist)
    {
        result = DatumGetPointP(DirectFunctionCall2(close_ps,
                                                    PointPGetDatum(&l2->p[0]),
                                                    LsegPGetDatum(l1)));
        memcpy(&point, result, sizeof(Point));
        result = DatumGetPointP(DirectFunctionCall2(close_ps,
                                                    PointPGetDatum(&point),
                                                    LsegPGetDatum(l2)));
    }

    if (dist_ps_internal(&l2->p[1], l1) < dist)
    {
        result = DatumGetPointP(DirectFunctionCall2(close_ps,
                                                    PointPGetDatum(&l2->p[1]),
                                                    LsegPGetDatum(l1)));
        memcpy(&point, result, sizeof(Point));
        result = DatumGetPointP(DirectFunctionCall2(close_ps,
                                                    PointPGetDatum(&point),
                                                    LsegPGetDatum(l2)));
    }

    if (result == NULL)
        result = point_copy(&point);

    PG_RETURN_POINT_P(result);
}

Datum close_pb ( PG_FUNCTION_ARGS   ) 

Definition at line 2978 of file geo_ops.c.

References BoxPGetDatum, close_ps(), DatumGetBool, DirectFunctionCall2, dist_ps_internal(), BOX::high, BOX::low, LsegPGetDatum, on_pb(), PG_GETARG_BOX_P, PG_GETARG_POINT_P, PG_RETURN_DATUM, PG_RETURN_POINT_P, PointPGetDatum, statlseg_construct(), Point::x, and Point::y.

Referenced by dist_pb().

{
    Point      *pt = PG_GETARG_POINT_P(0);
    BOX        *box = PG_GETARG_BOX_P(1);
    LSEG        lseg,
                seg;
    Point       point;
    double      dist,
                d;

    if (DatumGetBool(DirectFunctionCall2(on_pb,
                                         PointPGetDatum(pt),
                                         BoxPGetDatum(box))))
        PG_RETURN_POINT_P(pt);

    /* pairwise check lseg distances */
    point.x = box->low.x;
    point.y = box->high.y;
    statlseg_construct(&lseg, &box->low, &point);
    dist = dist_ps_internal(pt, &lseg);

    statlseg_construct(&seg, &box->high, &point);
    if ((d = dist_ps_internal(pt, &seg)) < dist)
    {
        dist = d;
        memcpy(&lseg, &seg, sizeof(lseg));
    }

    point.x = box->high.x;
    point.y = box->low.y;
    statlseg_construct(&seg, &box->low, &point);
    if ((d = dist_ps_internal(pt, &seg)) < dist)
    {
        dist = d;
        memcpy(&lseg, &seg, sizeof(lseg));
    }

    statlseg_construct(&seg, &box->high, &point);
    if ((d = dist_ps_internal(pt, &seg)) < dist)
    {
        dist = d;
        memcpy(&lseg, &seg, sizeof(lseg));
    }

    PG_RETURN_DATUM(DirectFunctionCall2(close_ps,
                                        PointPGetDatum(pt),
                                        LsegPGetDatum(&lseg)));
}

Datum close_pl ( PG_FUNCTION_ARGS   ) 

Definition at line 2762 of file geo_ops.c.

References LINE::A, Assert, LINE::B, LINE::C, FPeq, FPzero, line_construct_pm(), line_interpt_internal(), LINE::m, NULL, palloc(), PG_GETARG_LINE_P, PG_GETARG_POINT_P, PG_RETURN_POINT_P, Point::x, and Point::y.

{
    Point      *pt = PG_GETARG_POINT_P(0);
    LINE       *line = PG_GETARG_LINE_P(1);
    Point      *result;
    LINE       *tmp;
    double      invm;

    result = (Point *) palloc(sizeof(Point));

#ifdef NOT_USED
    if (FPeq(line->A, -1.0) && FPzero(line->B))
    {                           /* vertical */
    }
#endif
    if (FPzero(line->B))        /* vertical? */
    {
        result->x = line->C;
        result->y = pt->y;
        PG_RETURN_POINT_P(result);
    }
    if (FPzero(line->A))        /* horizontal? */
    {
        result->x = pt->x;
        result->y = line->C;
        PG_RETURN_POINT_P(result);
    }
    /* drop a perpendicular and find the intersection point */
#ifdef NOT_USED
    invm = -1.0 / line->m;
#endif
    /* invert and flip the sign on the slope to get a perpendicular */
    invm = line->B / line->A;
    tmp = line_construct_pm(pt, invm);
    result = line_interpt_internal(tmp, line);
    Assert(result != NULL);
    PG_RETURN_POINT_P(result);
}

Datum close_ps ( PG_FUNCTION_ARGS   ) 

Definition at line 2813 of file geo_ops.c.

References LINE::A, Assert, LINE::B, LINE::C, FPeq, interpt_sl(), line_construct_pm(), LINE::m, NULL, LSEG::p, palloc(), PG_GETARG_LSEG_P, PG_GETARG_POINT_P, PG_RETURN_POINT_P, point_copy(), point_sl(), Point::x, and Point::y.

Referenced by close_lseg(), close_pb(), and close_sb().

{
    Point      *pt = PG_GETARG_POINT_P(0);
    LSEG       *lseg = PG_GETARG_LSEG_P(1);
    Point      *result = NULL;
    LINE       *tmp;
    double      invm;
    int         xh,
                yh;

#ifdef GEODEBUG
    printf("close_sp:pt->x %f pt->y %f\nlseg(0).x %f lseg(0).y %f  lseg(1).x %f lseg(1).y %f\n",
           pt->x, pt->y, lseg->p[0].x, lseg->p[0].y,
           lseg->p[1].x, lseg->p[1].y);
#endif

    /* xh (or yh) is the index of upper x( or y) end point of lseg */
    /* !xh (or !yh) is the index of lower x( or y) end point of lseg */
    xh = lseg->p[0].x < lseg->p[1].x;
    yh = lseg->p[0].y < lseg->p[1].y;

    if (FPeq(lseg->p[0].x, lseg->p[1].x))       /* vertical? */
    {
#ifdef GEODEBUG
        printf("close_ps- segment is vertical\n");
#endif
        /* first check if point is below or above the entire lseg. */
        if (pt->y < lseg->p[!yh].y)
            result = point_copy(&lseg->p[!yh]); /* below the lseg */
        else if (pt->y > lseg->p[yh].y)
            result = point_copy(&lseg->p[yh]);  /* above the lseg */
        if (result != NULL)
            PG_RETURN_POINT_P(result);

        /* point lines along (to left or right) of the vertical lseg. */

        result = (Point *) palloc(sizeof(Point));
        result->x = lseg->p[0].x;
        result->y = pt->y;
        PG_RETURN_POINT_P(result);
    }
    else if (FPeq(lseg->p[0].y, lseg->p[1].y))  /* horizontal? */
    {
#ifdef GEODEBUG
        printf("close_ps- segment is horizontal\n");
#endif
        /* first check if point is left or right of the entire lseg. */
        if (pt->x < lseg->p[!xh].x)
            result = point_copy(&lseg->p[!xh]); /* left of the lseg */
        else if (pt->x > lseg->p[xh].x)
            result = point_copy(&lseg->p[xh]);  /* right of the lseg */
        if (result != NULL)
            PG_RETURN_POINT_P(result);

        /* point lines along (at top or below) the horiz. lseg. */
        result = (Point *) palloc(sizeof(Point));
        result->x = pt->x;
        result->y = lseg->p[0].y;
        PG_RETURN_POINT_P(result);
    }

    /*
     * vert. and horiz. cases are down, now check if the closest point is one
     * of the end points or someplace on the lseg.
     */

    invm = -1.0 / point_sl(&(lseg->p[0]), &(lseg->p[1]));
    tmp = line_construct_pm(&lseg->p[!yh], invm);       /* lower edge of the
                                                         * "band" */
    if (pt->y < (tmp->A * pt->x + tmp->C))
    {                           /* we are below the lower edge */
        result = point_copy(&lseg->p[!yh]);     /* below the lseg, take lower
                                                 * end pt */
#ifdef GEODEBUG
        printf("close_ps below: tmp A %f  B %f   C %f    m %f\n",
               tmp->A, tmp->B, tmp->C, tmp->m);
#endif
        PG_RETURN_POINT_P(result);
    }
    tmp = line_construct_pm(&lseg->p[yh], invm);        /* upper edge of the
                                                         * "band" */
    if (pt->y > (tmp->A * pt->x + tmp->C))
    {                           /* we are below the lower edge */
        result = point_copy(&lseg->p[yh]);      /* above the lseg, take higher
                                                 * end pt */
#ifdef GEODEBUG
        printf("close_ps above: tmp A %f  B %f   C %f    m %f\n",
               tmp->A, tmp->B, tmp->C, tmp->m);
#endif
        PG_RETURN_POINT_P(result);
    }

    /*
     * at this point the "normal" from point will hit lseg. The closet point
     * will be somewhere on the lseg
     */
    tmp = line_construct_pm(pt, invm);
#ifdef GEODEBUG
    printf("close_ps- tmp A %f  B %f   C %f    m %f\n",
           tmp->A, tmp->B, tmp->C, tmp->m);
#endif
    result = interpt_sl(lseg, tmp);
    Assert(result != NULL);
#ifdef GEODEBUG
    printf("close_ps- result.x %f  result.y %f\n", result->x, result->y);
#endif
    PG_RETURN_POINT_P(result);
}

Datum close_sb ( PG_FUNCTION_ARGS   ) 

Definition at line 3089 of file geo_ops.c.

References box_cn(), BoxPGetDatum, close_lseg(), close_ps(), DatumGetBool, DirectFunctionCall2, BOX::high, inter_sb(), BOX::low, lseg_dt(), LsegPGetDatum, PG_GETARG_BOX_P, PG_GETARG_LSEG_P, PG_RETURN_DATUM, PointPGetDatum, statlseg_construct(), Point::x, and Point::y.

Referenced by dist_sb().

{
    LSEG       *lseg = PG_GETARG_LSEG_P(0);
    BOX        *box = PG_GETARG_BOX_P(1);
    Point       point;
    LSEG        bseg,
                seg;
    double      dist,
                d;

    /* segment intersects box? then just return closest point to center */
    if (DatumGetBool(DirectFunctionCall2(inter_sb,
                                         LsegPGetDatum(lseg),
                                         BoxPGetDatum(box))))
    {
        box_cn(&point, box);
        PG_RETURN_DATUM(DirectFunctionCall2(close_ps,
                                            PointPGetDatum(&point),
                                            LsegPGetDatum(lseg)));
    }

    /* pairwise check lseg distances */
    point.x = box->low.x;
    point.y = box->high.y;
    statlseg_construct(&bseg, &box->low, &point);
    dist = lseg_dt(lseg, &bseg);

    statlseg_construct(&seg, &box->high, &point);
    if ((d = lseg_dt(lseg, &seg)) < dist)
    {
        dist = d;
        memcpy(&bseg, &seg, sizeof(bseg));
    }

    point.x = box->high.x;
    point.y = box->low.y;
    statlseg_construct(&seg, &box->low, &point);
    if ((d = lseg_dt(lseg, &seg)) < dist)
    {
        dist = d;
        memcpy(&bseg, &seg, sizeof(bseg));
    }

    statlseg_construct(&seg, &box->high, &point);
    if ((d = lseg_dt(lseg, &seg)) < dist)
    {
        dist = d;
        memcpy(&bseg, &seg, sizeof(bseg));
    }

    /* OK, we now have the closest line segment on the box boundary */
    PG_RETURN_DATUM(DirectFunctionCall2(close_lseg,
                                        LsegPGetDatum(lseg),
                                        LsegPGetDatum(&bseg)));
}

Datum close_sl ( PG_FUNCTION_ARGS   ) 

Definition at line 3037 of file geo_ops.c.

References dist_pl_internal(), interpt_sl(), LSEG::p, PG_GETARG_LINE_P, PG_GETARG_LSEG_P, PG_RETURN_POINT_P, and point_copy().

{
    LSEG       *lseg = PG_GETARG_LSEG_P(0);
    LINE       *line = PG_GETARG_LINE_P(1);
    Point      *result;
    float8      d1,
                d2;

    result = interpt_sl(lseg, line);
    if (result)
        PG_RETURN_POINT_P(result);

    d1 = dist_pl_internal(&lseg->p[0], line);
    d2 = dist_pl_internal(&lseg->p[1], line);
    if (d1 < d2)
        result = point_copy(&lseg->p[0]);
    else
        result = point_copy(&lseg->p[1]);

    PG_RETURN_POINT_P(result);
}

Datum construct_point ( PG_FUNCTION_ARGS   ) 
Datum cr_circle ( PG_FUNCTION_ARGS   ) 

Definition at line 5084 of file geo_ops.c.

References CIRCLE::center, palloc(), PG_GETARG_FLOAT8, PG_GETARG_POINT_P, PG_RETURN_CIRCLE_P, CIRCLE::radius, Point::x, and Point::y.

{
    Point      *center = PG_GETARG_POINT_P(0);
    float8      radius = PG_GETARG_FLOAT8(1);
    CIRCLE     *result;

    result = (CIRCLE *) palloc(sizeof(CIRCLE));

    result->center.x = center->x;
    result->center.y = center->y;
    result->radius = radius;

    PG_RETURN_CIRCLE_P(result);
}

Datum dist_cpoly ( PG_FUNCTION_ARGS   ) 

Definition at line 2650 of file geo_ops.c.

References CIRCLE::center, dist_ps_internal(), i, POLYGON::npts, LSEG::p, POLYGON::p, PG_GETARG_CIRCLE_P, PG_GETARG_POLYGON_P, PG_RETURN_FLOAT8, point_inside(), CIRCLE::radius, Point::x, and Point::y.

{
    CIRCLE     *circle = PG_GETARG_CIRCLE_P(0);
    POLYGON    *poly = PG_GETARG_POLYGON_P(1);
    float8      result;
    float8      d;
    int         i;
    LSEG        seg;

    if (point_inside(&(circle->center), poly->npts, poly->p) != 0)
    {
#ifdef GEODEBUG
        printf("dist_cpoly- center inside of polygon\n");
#endif
        PG_RETURN_FLOAT8(0.0);
    }

    /* initialize distance with segment between first and last points */
    seg.p[0].x = poly->p[0].x;
    seg.p[0].y = poly->p[0].y;
    seg.p[1].x = poly->p[poly->npts - 1].x;
    seg.p[1].y = poly->p[poly->npts - 1].y;
    result = dist_ps_internal(&circle->center, &seg);
#ifdef GEODEBUG
    printf("dist_cpoly- segment 0/n distance is %f\n", result);
#endif

    /* check distances for other segments */
    for (i = 0; (i < poly->npts - 1); i++)
    {
        seg.p[0].x = poly->p[i].x;
        seg.p[0].y = poly->p[i].y;
        seg.p[1].x = poly->p[i + 1].x;
        seg.p[1].y = poly->p[i + 1].y;
        d = dist_ps_internal(&circle->center, &seg);
#ifdef GEODEBUG
        printf("dist_cpoly- segment %d distance is %f\n", (i + 1), d);
#endif
        if (d < result)
            result = d;
    }

    result -= circle->radius;
    if (result < 0)
        result = 0;

    PG_RETURN_FLOAT8(result);
}

Datum dist_lb ( PG_FUNCTION_ARGS   ) 

Definition at line 2633 of file geo_ops.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_BOX_P, PG_GETARG_LINE_P, and PG_RETURN_NULL.

{
#ifdef NOT_USED
    LINE       *line = PG_GETARG_LINE_P(0);
    BOX        *box = PG_GETARG_BOX_P(1);
#endif

    /* need to think about this one for a while */
    ereport(ERROR,
            (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
             errmsg("function \"dist_lb\" not implemented")));

    PG_RETURN_NULL();
}

Datum dist_pb ( PG_FUNCTION_ARGS   ) 
Datum dist_pc ( PG_FUNCTION_ARGS   ) 

Definition at line 5041 of file geo_ops.c.

References CIRCLE::center, PG_GETARG_CIRCLE_P, PG_GETARG_POINT_P, PG_RETURN_FLOAT8, point_dt(), and CIRCLE::radius.

{
    Point      *point = PG_GETARG_POINT_P(0);
    CIRCLE     *circle = PG_GETARG_CIRCLE_P(1);
    float8      result;

    result = point_dt(point, &circle->center) - circle->radius;
    if (result < 0)
        result = 0;
    PG_RETURN_FLOAT8(result);
}

Datum dist_pl ( PG_FUNCTION_ARGS   ) 
static double dist_pl_internal ( Point pt,
LINE line 
) [static]

Definition at line 2448 of file geo_ops.c.

References LINE::A, LINE::B, LINE::C, HYPOT, Point::x, and Point::y.

Referenced by close_ls(), close_sl(), dist_pl(), dist_sl(), and line_distance().

{
    return (line->A * pt->x + line->B * pt->y + line->C) /
        HYPOT(line->A, line->B);
}

Datum dist_ppath ( PG_FUNCTION_ARGS   ) 

Definition at line 2520 of file geo_ops.c.

References Assert, PATH::closed, dist_ps_internal(), i, PATH::npts, PATH::p, PG_GETARG_PATH_P, PG_GETARG_POINT_P, PG_RETURN_FLOAT8, PG_RETURN_NULL, point_dt(), and statlseg_construct().

{
    Point      *pt = PG_GETARG_POINT_P(0);
    PATH       *path = PG_GETARG_PATH_P(1);
    float8      result = 0.0;   /* keep compiler quiet */
    bool        have_min = false;
    float8      tmp;
    int         i;
    LSEG        lseg;

    switch (path->npts)
    {
        case 0:
            /* no points in path? then result is undefined... */
            PG_RETURN_NULL();
        case 1:
            /* one point in path? then get distance between two points... */
            result = point_dt(pt, &path->p[0]);
            break;
        default:
            /* make sure the path makes sense... */
            Assert(path->npts > 1);

            /*
             * the distance from a point to a path is the smallest distance
             * from the point to any of its constituent segments.
             */
            for (i = 0; i < path->npts; i++)
            {
                int         iprev;

                if (i > 0)
                    iprev = i - 1;
                else
                {
                    if (!path->closed)
                        continue;
                    iprev = path->npts - 1;     /* include the closure segment */
                }

                statlseg_construct(&lseg, &path->p[iprev], &path->p[i]);
                tmp = dist_ps_internal(pt, &lseg);
                if (!have_min || tmp < result)
                {
                    result = tmp;
                    have_min = true;
                }
            }
            break;
    }
    PG_RETURN_FLOAT8(result);
}

Datum dist_ps ( PG_FUNCTION_ARGS   ) 
static double dist_ps_internal ( Point pt,
LSEG lseg 
) [static]

Definition at line 2464 of file geo_ops.c.

References LINE::A, LINE::B, LINE::C, interpt_sl(), line_construct_pm(), NULL, LSEG::p, point_dt(), Point::x, and Point::y.

Referenced by close_lseg(), close_pb(), dist_cpoly(), dist_ppath(), dist_ps(), and lseg_dt().

{
    double      m;              /* slope of perp. */
    LINE       *ln;
    double      result,
                tmpdist;
    Point      *ip;

    /*
     * Construct a line perpendicular to the input segment and through the
     * input point
     */
    if (lseg->p[1].x == lseg->p[0].x)
        m = 0;
    else if (lseg->p[1].y == lseg->p[0].y)
        m = (double) DBL_MAX;   /* slope is infinite */
    else
        m = (lseg->p[0].x - lseg->p[1].x) / (lseg->p[1].y - lseg->p[0].y);
    ln = line_construct_pm(pt, m);

#ifdef GEODEBUG
    printf("dist_ps- line is A=%g B=%g C=%g from (point) slope (%f,%f) %g\n",
           ln->A, ln->B, ln->C, pt->x, pt->y, m);
#endif

    /*
     * Calculate distance to the line segment or to the nearest endpoint of
     * the segment.
     */

    /* intersection is on the line segment? */
    if ((ip = interpt_sl(lseg, ln)) != NULL)
    {
        /* yes, so use distance to the intersection point */
        result = point_dt(pt, ip);
#ifdef GEODEBUG
        printf("dist_ps- distance is %f to intersection point is (%f,%f)\n",
               result, ip->x, ip->y);
#endif
    }
    else
    {
        /* no, so use distance to the nearer endpoint */
        result = point_dt(pt, &lseg->p[0]);
        tmpdist = point_dt(pt, &lseg->p[1]);
        if (tmpdist < result)
            result = tmpdist;
    }

    return result;
}

Datum dist_sb ( PG_FUNCTION_ARGS   ) 
Datum dist_sl ( PG_FUNCTION_ARGS   ) 

Definition at line 2591 of file geo_ops.c.

References dist_pl_internal(), has_interpt_sl(), LSEG::p, PG_GETARG_LINE_P, PG_GETARG_LSEG_P, and PG_RETURN_FLOAT8.

{
    LSEG       *lseg = PG_GETARG_LSEG_P(0);
    LINE       *line = PG_GETARG_LINE_P(1);
    float8      result,
                d2;

    if (has_interpt_sl(lseg, line))
        result = 0.0;
    else
    {
        result = dist_pl_internal(&lseg->p[0], line);
        d2 = dist_pl_internal(&lseg->p[1], line);
        /* XXX shouldn't we take the min not max? */
        if (d2 > result)
            result = d2;
    }

    PG_RETURN_FLOAT8(result);
}

static bool has_interpt_sl ( LSEG lseg,
LINE line 
) [static]

Definition at line 2742 of file geo_ops.c.

References interpt_sl().

Referenced by dist_sl(), inter_lb(), and inter_sl().

{
    Point      *tmp;

    tmp = interpt_sl(lseg, line);
    if (tmp)
        return true;
    return false;
}

Datum inter_lb ( PG_FUNCTION_ARGS   ) 

Definition at line 3368 of file geo_ops.c.

References has_interpt_sl(), BOX::high, BOX::low, PG_GETARG_BOX_P, PG_GETARG_LINE_P, PG_RETURN_BOOL, statlseg_construct(), Point::x, and Point::y.

{
    LINE       *line = PG_GETARG_LINE_P(0);
    BOX        *box = PG_GETARG_BOX_P(1);
    LSEG        bseg;
    Point       p1,
                p2;

    /* pairwise check lseg intersections */
    p1.x = box->low.x;
    p1.y = box->low.y;
    p2.x = box->low.x;
    p2.y = box->high.y;
    statlseg_construct(&bseg, &p1, &p2);
    if (has_interpt_sl(&bseg, line))
        PG_RETURN_BOOL(true);
    p1.x = box->high.x;
    p1.y = box->high.y;
    statlseg_construct(&bseg, &p1, &p2);
    if (has_interpt_sl(&bseg, line))
        PG_RETURN_BOOL(true);
    p2.x = box->high.x;
    p2.y = box->low.y;
    statlseg_construct(&bseg, &p1, &p2);
    if (has_interpt_sl(&bseg, line))
        PG_RETURN_BOOL(true);
    p1.x = box->low.x;
    p1.y = box->low.y;
    statlseg_construct(&bseg, &p1, &p2);
    if (has_interpt_sl(&bseg, line))
        PG_RETURN_BOOL(true);

    /* if we dropped through, no intersection */
    PG_RETURN_BOOL(false);
}

Datum inter_sb ( PG_FUNCTION_ARGS   ) 

Definition at line 3313 of file geo_ops.c.

References box_ov(), BoxPGetDatum, DatumGetBool, DirectFunctionCall2, BOX::high, BOX::low, lseg_intersect_internal(), Max, Min, on_pb(), LSEG::p, PG_GETARG_BOX_P, PG_GETARG_LSEG_P, PG_RETURN_BOOL, PointPGetDatum, statlseg_construct(), Point::x, and Point::y.

Referenced by close_sb().

{
    LSEG       *lseg = PG_GETARG_LSEG_P(0);
    BOX        *box = PG_GETARG_BOX_P(1);
    BOX         lbox;
    LSEG        bseg;
    Point       point;

    lbox.low.x = Min(lseg->p[0].x, lseg->p[1].x);
    lbox.low.y = Min(lseg->p[0].y, lseg->p[1].y);
    lbox.high.x = Max(lseg->p[0].x, lseg->p[1].x);
    lbox.high.y = Max(lseg->p[0].y, lseg->p[1].y);

    /* nothing close to overlap? then not going to intersect */
    if (!box_ov(&lbox, box))
        PG_RETURN_BOOL(false);

    /* an endpoint of segment is inside box? then clearly intersects */
    if (DatumGetBool(DirectFunctionCall2(on_pb,
                                         PointPGetDatum(&lseg->p[0]),
                                         BoxPGetDatum(box))) ||
        DatumGetBool(DirectFunctionCall2(on_pb,
                                         PointPGetDatum(&lseg->p[1]),
                                         BoxPGetDatum(box))))
        PG_RETURN_BOOL(true);

    /* pairwise check lseg intersections */
    point.x = box->low.x;
    point.y = box->high.y;
    statlseg_construct(&bseg, &box->low, &point);
    if (lseg_intersect_internal(&bseg, lseg))
        PG_RETURN_BOOL(true);

    statlseg_construct(&bseg, &box->high, &point);
    if (lseg_intersect_internal(&bseg, lseg))
        PG_RETURN_BOOL(true);

    point.x = box->high.x;
    point.y = box->low.y;
    statlseg_construct(&bseg, &box->low, &point);
    if (lseg_intersect_internal(&bseg, lseg))
        PG_RETURN_BOOL(true);

    statlseg_construct(&bseg, &box->high, &point);
    if (lseg_intersect_internal(&bseg, lseg))
        PG_RETURN_BOOL(true);

    /* if we dropped through, no two segs intersected */
    PG_RETURN_BOOL(false);
}

Datum inter_sl ( PG_FUNCTION_ARGS   ) 

Definition at line 3294 of file geo_ops.c.

References has_interpt_sl(), PG_GETARG_LINE_P, PG_GETARG_LSEG_P, and PG_RETURN_BOOL.

{
    LSEG       *lseg = PG_GETARG_LSEG_P(0);
    LINE       *line = PG_GETARG_LINE_P(1);

    PG_RETURN_BOOL(has_interpt_sl(lseg, line));
}

static Point * interpt_sl ( LSEG lseg,
LINE line 
) [static]

Definition at line 2709 of file geo_ops.c.

References LINE::A, LINE::B, LINE::C, line_construct_pts(), line_interpt_internal(), on_ps_internal(), LSEG::p, PointerIsValid, Point::x, and Point::y.

Referenced by close_ls(), close_ps(), close_sl(), dist_ps_internal(), has_interpt_sl(), and lseg_intersect_internal().

{
    LINE        tmp;
    Point      *p;

    line_construct_pts(&tmp, &lseg->p[0], &lseg->p[1]);
    p = line_interpt_internal(&tmp, line);
#ifdef GEODEBUG
    printf("interpt_sl- segment is (%.*g %.*g) (%.*g %.*g)\n",
           DBL_DIG, lseg->p[0].x, DBL_DIG, lseg->p[0].y, DBL_DIG, lseg->p[1].x, DBL_DIG, lseg->p[1].y);
    printf("interpt_sl- segment becomes line A=%.*g B=%.*g C=%.*g\n",
           DBL_DIG, tmp.A, DBL_DIG, tmp.B, DBL_DIG, tmp.C);
#endif
    if (PointerIsValid(p))
    {
#ifdef GEODEBUG
        printf("interpt_sl- intersection point is (%.*g %.*g)\n", DBL_DIG, p->x, DBL_DIG, p->y);
#endif
        if (on_ps_internal(p, lseg))
        {
#ifdef GEODEBUG
            printf("interpt_sl- intersection point is on segment\n");
#endif
        }
        else
            p = NULL;
    }

    return p;
}

static LINE * line_construct_pm ( Point pt,
double  m 
) [static]

Definition at line 1066 of file geo_ops.c.

References LINE::A, LINE::B, LINE::C, LINE::m, palloc(), Point::x, and Point::y.

Referenced by close_pl(), close_ps(), and dist_ps_internal().

{
    LINE       *result = (LINE *) palloc(sizeof(LINE));

    if (m == DBL_MAX)
    {
        /* vertical - use "x = C" */
        result->A = -1;
        result->B = 0;
        result->C = pt->x;
    }
    else
    {
        /* use "mx - y + yinter = 0" */
        result->A = m;
        result->B = -1.0;
        result->C = pt->y - m * pt->x;
    }

#ifdef NOT_USED
    result->m = m;
#endif

    return result;
}

Datum line_construct_pp ( PG_FUNCTION_ARGS   ) 

Definition at line 1144 of file geo_ops.c.

References line_construct_pts(), palloc(), PG_GETARG_POINT_P, and PG_RETURN_LINE_P.

{
    Point      *pt1 = PG_GETARG_POINT_P(0);
    Point      *pt2 = PG_GETARG_POINT_P(1);
    LINE       *result = (LINE *) palloc(sizeof(LINE));

    line_construct_pts(result, pt1, pt2);
    PG_RETURN_LINE_P(result);
}

static void line_construct_pts ( LINE line,
Point pt1,
Point pt2 
) [static]

Definition at line 1096 of file geo_ops.c.

References LINE::A, LINE::B, LINE::C, FPeq, LINE::m, Point::x, and Point::y.

Referenced by interpt_sl(), line_construct_pp(), line_in(), lseg_interpt_internal(), and lseg_intersect_internal().

{
    if (FPeq(pt1->x, pt2->x))
    {                           /* vertical */
        /* use "x = C" */
        line->A = -1;
        line->B = 0;
        line->C = pt1->x;
#ifdef NOT_USED
        line->m = DBL_MAX;
#endif
#ifdef GEODEBUG
        printf("line_construct_pts- line is vertical\n");
#endif
    }
    else if (FPeq(pt1->y, pt2->y))
    {                           /* horizontal */
        /* use "y = C" */
        line->A = 0;
        line->B = -1;
        line->C = pt1->y;
#ifdef NOT_USED
        line->m = 0.0;
#endif
#ifdef GEODEBUG
        printf("line_construct_pts- line is horizontal\n");
#endif
    }
    else
    {
        /* use "mx - y + yinter = 0" */
        line->A = (pt2->y - pt1->y) / (pt2->x - pt1->x);
        line->B = -1.0;
        line->C = pt1->y - line->A * pt1->x;
#ifdef NOT_USED
        line->m = line->A;
#endif
#ifdef GEODEBUG
        printf("line_construct_pts- line is neither vertical nor horizontal (diffs x=%.*g, y=%.*g\n",
               DBL_DIG, (pt2->x - pt1->x), DBL_DIG, (pt2->y - pt1->y));
#endif
    }
}

Datum line_distance ( PG_FUNCTION_ARGS   ) 
Datum line_eq ( PG_FUNCTION_ARGS   ) 

Definition at line 1222 of file geo_ops.c.

References LINE::A, LINE::B, LINE::C, FPeq, FPzero, PG_GETARG_LINE_P, and PG_RETURN_BOOL.

{
    LINE       *l1 = PG_GETARG_LINE_P(0);
    LINE       *l2 = PG_GETARG_LINE_P(1);
    double      k;

    if (!FPzero(l2->A))
        k = l1->A / l2->A;
    else if (!FPzero(l2->B))
        k = l1->B / l2->B;
    else if (!FPzero(l2->C))
        k = l1->C / l2->C;
    else
        k = 1.0;

    PG_RETURN_BOOL(FPeq(l1->A, k * l2->A) &&
                   FPeq(l1->B, k * l2->B) &&
                   FPeq(l1->C, k * l2->C));
}

Datum line_horizontal ( PG_FUNCTION_ARGS   ) 

Definition at line 1214 of file geo_ops.c.

References LINE::A, FPzero, PG_GETARG_LINE_P, and PG_RETURN_BOOL.

{
    LINE       *line = PG_GETARG_LINE_P(0);

    PG_RETURN_BOOL(FPzero(line->A));
}

Datum line_in ( PG_FUNCTION_ARGS   ) 

Definition at line 934 of file geo_ops.c.

References ereport, errcode(), errmsg(), ERROR, line_construct_pts(), LSEG::p, palloc(), path_decode(), PG_GETARG_CSTRING, PG_RETURN_LINE_P, and TRUE.

{
#ifdef ENABLE_LINE_TYPE
    char       *str = PG_GETARG_CSTRING(0);
#endif
    LINE       *line;

#ifdef ENABLE_LINE_TYPE
    /* when fixed, modify "not implemented", catalog/pg_type.h and SGML */
    LSEG        lseg;
    int         isopen;
    char       *s;

    if ((!path_decode(TRUE, 2, str, &isopen, &s, &(lseg.p[0])))
        || (*s != '\0'))
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                 errmsg("invalid input syntax for type line: \"%s\"", str)));

    line = (LINE *) palloc(sizeof(LINE));
    line_construct_pts(line, &lseg.p[0], &lseg.p[1]);
#else
    ereport(ERROR,
            (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
             errmsg("type \"line\" not yet implemented")));

    line = NULL;
#endif

    PG_RETURN_LINE_P(line);
}

Datum line_interpt ( PG_FUNCTION_ARGS   ) 

Definition at line 1273 of file geo_ops.c.

References line_interpt_internal(), NULL, PG_GETARG_LINE_P, PG_RETURN_NULL, and PG_RETURN_POINT_P.

{
    LINE       *l1 = PG_GETARG_LINE_P(0);
    LINE       *l2 = PG_GETARG_LINE_P(1);
    Point      *result;

    result = line_interpt_internal(l1, l2);

    if (result == NULL)
        PG_RETURN_NULL();
    PG_RETURN_POINT_P(result);
}

static Point * line_interpt_internal ( LINE l1,
LINE l2 
) [static]

Definition at line 1292 of file geo_ops.c.

References LINE::A, LINE::B, LINE::C, DatumGetBool, DirectFunctionCall2, FPzero, line_parallel(), LinePGetDatum, LINE::m, and point_construct().

Referenced by close_pl(), interpt_sl(), line_interpt(), and lseg_interpt_internal().

{
    Point      *result;
    double      x,
                y;

    /*
     * NOTE: if the lines are identical then we will find they are parallel
     * and report "no intersection".  This is a little weird, but since
     * there's no *unique* intersection, maybe it's appropriate behavior.
     */
    if (DatumGetBool(DirectFunctionCall2(line_parallel,
                                         LinePGetDatum(l1),
                                         LinePGetDatum(l2))))
        return NULL;

#ifdef NOT_USED
    if (FPzero(l1->B))          /* l1 vertical? */
        result = point_construct(l2->m * l1->C + l2->C, l1->C);
    else if (FPzero(l2->B))     /* l2 vertical? */
        result = point_construct(l1->m * l2->C + l1->C, l2->C);
    else
    {
        x = (l1->C - l2->C) / (l2->A - l1->A);
        result = point_construct(x, l1->m * x + l1->C);
    }
#endif

    if (FPzero(l1->B))          /* l1 vertical? */
    {
        x = l1->C;
        y = (l2->A * x + l2->C);
    }
    else if (FPzero(l2->B))     /* l2 vertical? */
    {
        x = l2->C;
        y = (l1->A * x + l1->C);
    }
    else
    {
        x = (l1->C - l2->C) / (l2->A - l1->A);
        y = (l1->A * x + l1->C);
    }
    result = point_construct(x, y);

#ifdef GEODEBUG
    printf("line_interpt- lines are A=%.*g, B=%.*g, C=%.*g, A=%.*g, B=%.*g, C=%.*g\n",
           DBL_DIG, l1->A, DBL_DIG, l1->B, DBL_DIG, l1->C, DBL_DIG, l2->A, DBL_DIG, l2->B, DBL_DIG, l2->C);
    printf("line_interpt- lines intersect at (%.*g,%.*g)\n", DBL_DIG, x, DBL_DIG, y);
#endif

    return result;
}

Datum line_intersect ( PG_FUNCTION_ARGS   ) 
Datum line_out ( PG_FUNCTION_ARGS   ) 

Definition at line 968 of file geo_ops.c.

References LINE::A, LINE::B, ereport, errcode(), errmsg(), ERROR, FPzero, path_encode(), PG_GETARG_LINE_P, PG_RETURN_CSTRING, and TRUE.

{
#ifdef ENABLE_LINE_TYPE
    LINE       *line = PG_GETARG_LINE_P(0);
#endif
    char       *result;

#ifdef ENABLE_LINE_TYPE
    /* when fixed, modify "not implemented", catalog/pg_type.h and SGML */
    LSEG        lseg;

    if (FPzero(line->B))
    {                           /* vertical */
        /* use "x = C" */
        result->A = -1;
        result->B = 0;
        result->C = pt1->x;
#ifdef GEODEBUG
        printf("line_out- line is vertical\n");
#endif
#ifdef NOT_USED
        result->m = DBL_MAX;
#endif

    }
    else if (FPzero(line->A))
    {                           /* horizontal */
        /* use "x = C" */
        result->A = 0;
        result->B = -1;
        result->C = pt1->y;
#ifdef GEODEBUG
        printf("line_out- line is horizontal\n");
#endif
#ifdef NOT_USED
        result->m = 0.0;
#endif

    }
    else
    {
    }

    if (FPzero(line->A))        /* horizontal? */
    {
    }
    else if (FPzero(line->B))   /* vertical? */
    {
    }
    else
    {
    }

    return path_encode(TRUE, 2, (Point *) &(ls->p[0]));
#else
    ereport(ERROR,
            (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
             errmsg("type \"line\" not yet implemented")));
    result = NULL;
#endif

    PG_RETURN_CSTRING(result);
}

Datum line_parallel ( PG_FUNCTION_ARGS   ) 

Definition at line 1171 of file geo_ops.c.

References LINE::A, LINE::B, FPeq, FPzero, LINE::m, PG_GETARG_LINE_P, and PG_RETURN_BOOL.

Referenced by line_distance(), line_interpt_internal(), and line_intersect().

{
    LINE       *l1 = PG_GETARG_LINE_P(0);
    LINE       *l2 = PG_GETARG_LINE_P(1);

#ifdef NOT_USED
    PG_RETURN_BOOL(FPeq(l1->m, l2->m));
#endif
    if (FPzero(l1->B))
        PG_RETURN_BOOL(FPzero(l2->B));

    PG_RETURN_BOOL(FPeq(l2->A, l1->A * (l2->B / l1->B)));
}

Datum line_perp ( PG_FUNCTION_ARGS   ) 

Definition at line 1186 of file geo_ops.c.

References LINE::A, LINE::B, FPeq, FPzero, LINE::m, PG_GETARG_LINE_P, and PG_RETURN_BOOL.

{
    LINE       *l1 = PG_GETARG_LINE_P(0);
    LINE       *l2 = PG_GETARG_LINE_P(1);

#ifdef NOT_USED
    if (l1->m)
        PG_RETURN_BOOL(FPeq(l2->m / l1->m, -1.0));
    else if (l2->m)
        PG_RETURN_BOOL(FPeq(l1->m / l2->m, -1.0));
#endif
    if (FPzero(l1->A))
        PG_RETURN_BOOL(FPzero(l2->B));
    else if (FPzero(l1->B))
        PG_RETURN_BOOL(FPzero(l2->A));

    PG_RETURN_BOOL(FPeq(((l1->A * l2->B) / (l1->B * l2->A)), -1.0));
}

Datum line_recv ( PG_FUNCTION_ARGS   ) 

Definition at line 1036 of file geo_ops.c.

References ereport, errcode(), errmsg(), and ERROR.

{
    ereport(ERROR,
            (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
             errmsg("type \"line\" not yet implemented")));
    return 0;
}

Datum line_send ( PG_FUNCTION_ARGS   ) 

Definition at line 1048 of file geo_ops.c.

References ereport, errcode(), errmsg(), and ERROR.

{
    ereport(ERROR,
            (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
             errmsg("type \"line\" not yet implemented")));
    return 0;
}

Datum line_vertical ( PG_FUNCTION_ARGS   ) 

Definition at line 1206 of file geo_ops.c.

References LINE::B, FPzero, PG_GETARG_LINE_P, and PG_RETURN_BOOL.

{
    LINE       *line = PG_GETARG_LINE_P(0);

    PG_RETURN_BOOL(FPzero(line->B));
}

Datum lseg_center ( PG_FUNCTION_ARGS   ) 

Definition at line 2348 of file geo_ops.c.

References LSEG::p, palloc(), PG_GETARG_LSEG_P, PG_RETURN_POINT_P, Point::x, and Point::y.

{
    LSEG       *lseg = PG_GETARG_LSEG_P(0);
    Point      *result;

    result = (Point *) palloc(sizeof(Point));

    result->x = (lseg->p[0].x + lseg->p[1].x) / 2.0;
    result->y = (lseg->p[0].y + lseg->p[1].y) / 2.0;

    PG_RETURN_POINT_P(result);
}

Datum lseg_construct ( PG_FUNCTION_ARGS   ) 

Definition at line 2102 of file geo_ops.c.

References LSEG::m, LSEG::p, palloc(), PG_GETARG_POINT_P, PG_RETURN_LSEG_P, point_sl(), Point::x, and Point::y.

{
    Point      *pt1 = PG_GETARG_POINT_P(0);
    Point      *pt2 = PG_GETARG_POINT_P(1);
    LSEG       *result = (LSEG *) palloc(sizeof(LSEG));

    result->p[0].x = pt1->x;
    result->p[0].y = pt1->y;
    result->p[1].x = pt2->x;
    result->p[1].y = pt2->y;

#ifdef NOT_USED
    result->m = point_sl(pt1, pt2);
#endif

    PG_RETURN_LSEG_P(result);
}

static int lseg_crossing ( double  x,
double  y,
double  px,
double  py 
) [static]

Definition at line 5309 of file geo_ops.c.

References FPge, FPgt, FPle, FPlt, FPzero, and POINT_ON_POLYGON.

Referenced by point_inside().

{
    double      z;
    int         y_sign;

    if (FPzero(y))
    {                           /* y == 0, on X axis */
        if (FPzero(x))          /* (x,y) is (0,0)? */
            return POINT_ON_POLYGON;
        else if (FPgt(x, 0))
        {                       /* x > 0 */
            if (FPzero(prev_y)) /* y and prev_y are zero */
                /* prev_x > 0? */
                return FPgt(prev_x, 0) ? 0 : POINT_ON_POLYGON;
            return FPlt(prev_y, 0) ? 1 : -1;
        }
        else
        {                       /* x < 0, x not on positive X axis */
            if (FPzero(prev_y))
                /* prev_x < 0? */
                return FPlt(prev_x, 0) ? 0 : POINT_ON_POLYGON;
            return 0;
        }
    }
    else
    {                           /* y != 0 */
        /* compute y crossing direction from previous point */
        y_sign = FPgt(y, 0) ? 1 : -1;

        if (FPzero(prev_y))
            /* previous point was on X axis, so new point is either off or on */
            return FPlt(prev_x, 0) ? 0 : y_sign;
        else if (FPgt(y_sign * prev_y, 0))
            /* both above or below X axis */
            return 0;           /* same sign */
        else
        {                       /* y and prev_y cross X-axis */
            if (FPge(x, 0) && FPgt(prev_x, 0))
                /* both non-negative so cross positive X-axis */
                return 2 * y_sign;
            if (FPlt(x, 0) && FPle(prev_x, 0))
                /* both non-positive so do not cross positive X-axis */
                return 0;

            /* x and y cross axises, see URL above point_inside() */
            z = (x - prev_x) * y - (y - prev_y) * x;
            if (FPzero(z))
                return POINT_ON_POLYGON;
            return FPgt((y_sign * z), 0) ? 0 : 2 * y_sign;
        }
    }
}

Datum lseg_distance ( PG_FUNCTION_ARGS   ) 

Definition at line 2312 of file geo_ops.c.

References lseg_dt(), PG_GETARG_LSEG_P, and PG_RETURN_FLOAT8.

Referenced by path_distance(), and regress_path_dist().

static double lseg_dt ( LSEG l1,
LSEG l2 
) [static]

Definition at line 2326 of file geo_ops.c.

References dist_ps_internal(), lseg_intersect_internal(), Min, and LSEG::p.

Referenced by close_sb(), and lseg_distance().

{
    double      result,
                d;

    if (lseg_intersect_internal(l1, l2))
        return 0.0;

    d = dist_ps_internal(&l1->p[0], l2);
    result = d;
    d = dist_ps_internal(&l1->p[1], l2);
    result = Min(result, d);
    d = dist_ps_internal(&l2->p[0], l1);
    result = Min(result, d);
    d = dist_ps_internal(&l2->p[1], l1);
    result = Min(result, d);

    return result;
}

Datum lseg_eq ( PG_FUNCTION_ARGS   ) 

Definition at line 2238 of file geo_ops.c.

References FPeq, LSEG::p, PG_GETARG_LSEG_P, PG_RETURN_BOOL, Point::x, and Point::y.

{
    LSEG       *l1 = PG_GETARG_LSEG_P(0);
    LSEG       *l2 = PG_GETARG_LSEG_P(1);

    PG_RETURN_BOOL(FPeq(l1->p[0].x, l2->p[0].x) &&
                   FPeq(l1->p[0].y, l2->p[0].y) &&
                   FPeq(l1->p[1].x, l2->p[1].x) &&
                   FPeq(l1->p[1].y, l2->p[1].y));
}

Datum lseg_ge ( PG_FUNCTION_ARGS   ) 

Definition at line 2292 of file geo_ops.c.

References FPge, LSEG::p, PG_GETARG_LSEG_P, PG_RETURN_BOOL, and point_dt().

{
    LSEG       *l1 = PG_GETARG_LSEG_P(0);
    LSEG       *l2 = PG_GETARG_LSEG_P(1);

    PG_RETURN_BOOL(FPge(point_dt(&l1->p[0], &l1->p[1]),
                        point_dt(&l2->p[0], &l2->p[1])));
}

Datum lseg_gt ( PG_FUNCTION_ARGS   ) 

Definition at line 2282 of file geo_ops.c.

References FPgt, LSEG::p, PG_GETARG_LSEG_P, PG_RETURN_BOOL, and point_dt().

{
    LSEG       *l1 = PG_GETARG_LSEG_P(0);
    LSEG       *l2 = PG_GETARG_LSEG_P(1);

    PG_RETURN_BOOL(FPgt(point_dt(&l1->p[0], &l1->p[1]),
                        point_dt(&l2->p[0], &l2->p[1])));
}

Datum lseg_horizontal ( PG_FUNCTION_ARGS   ) 

Definition at line 2229 of file geo_ops.c.

References FPeq, LSEG::p, PG_GETARG_LSEG_P, PG_RETURN_BOOL, and Point::y.

{
    LSEG       *lseg = PG_GETARG_LSEG_P(0);

    PG_RETURN_BOOL(FPeq(lseg->p[0].y, lseg->p[1].y));
}

Datum lseg_in ( PG_FUNCTION_ARGS   ) 

Definition at line 2026 of file geo_ops.c.

References ereport, errcode(), errmsg(), ERROR, LSEG::m, LSEG::p, palloc(), path_decode(), PG_GETARG_CSTRING, PG_RETURN_LSEG_P, point_sl(), and TRUE.

{
    char       *str = PG_GETARG_CSTRING(0);
    LSEG       *lseg;
    int         isopen;
    char       *s;

    lseg = (LSEG *) palloc(sizeof(LSEG));

    if ((!path_decode(TRUE, 2, str, &isopen, &s, &(lseg->p[0])))
        || (*s != '\0'))
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                 errmsg("invalid input syntax for type lseg: \"%s\"", str)));

#ifdef NOT_USED
    lseg->m = point_sl(&lseg->p[0], &lseg->p[1]);
#endif

    PG_RETURN_LSEG_P(lseg);
}

static bool lseg_inside_poly ( Point a,
Point b,
POLYGON poly,
int  start 
) [static]

Definition at line 3891 of file geo_ops.c.

References i, lseg_interpt_internal(), POLYGON::npts, NULL, on_ps_internal(), POLYGON::p, LSEG::p, pfree(), point_inside(), touched_lseg_inside_poly(), Point::x, and Point::y.

Referenced by poly_contain(), and touched_lseg_inside_poly().

{
    LSEG        s,
                t;
    int         i;
    bool        res = true,
                intersection = false;

    t.p[0] = *a;
    t.p[1] = *b;
    s.p[0] = poly->p[(start == 0) ? (poly->npts - 1) : (start - 1)];

    for (i = start; i < poly->npts && res; i++)
    {
        Point      *interpt;

        s.p[1] = poly->p[i];

        if (on_ps_internal(t.p, &s))
        {
            if (on_ps_internal(t.p + 1, &s))
                return true;    /* t is contained by s */

            /* Y-cross */
            res = touched_lseg_inside_poly(t.p, t.p + 1, &s, poly, i + 1);
        }
        else if (on_ps_internal(t.p + 1, &s))
        {
            /* Y-cross */
            res = touched_lseg_inside_poly(t.p + 1, t.p, &s, poly, i + 1);
        }
        else if ((interpt = lseg_interpt_internal(&t, &s)) != NULL)
        {
            /*
             * segments are X-crossing, go to check each subsegment
             */

            intersection = true;
            res = lseg_inside_poly(t.p, interpt, poly, i + 1);
            if (res)
                res = lseg_inside_poly(t.p + 1, interpt, poly, i + 1);
            pfree(interpt);
        }

        s.p[0] = s.p[1];
    }

    if (res && !intersection)
    {
        Point       p;

        /*
         * if X-intersection wasn't found  then check central point of tested
         * segment. In opposite case we already check all subsegments
         */
        p.x = (t.p[0].x + t.p[1].x) / 2.0;
        p.y = (t.p[0].y + t.p[1].y) / 2.0;

        res = point_inside(&p, poly->npts, poly->p);
    }

    return res;
}

Datum lseg_interpt ( PG_FUNCTION_ARGS   ) 

Definition at line 2413 of file geo_ops.c.

References lseg_interpt_internal(), PG_GETARG_LSEG_P, PG_RETURN_NULL, PG_RETURN_POINT_P, and PointerIsValid.

Referenced by interpt_pp().

{
    LSEG       *l1 = PG_GETARG_LSEG_P(0);
    LSEG       *l2 = PG_GETARG_LSEG_P(1);
    Point      *result;

    result = lseg_interpt_internal(l1, l2);
    if (!PointerIsValid(result))
        PG_RETURN_NULL();

    PG_RETURN_POINT_P(result);
}

static Point * lseg_interpt_internal ( LSEG l1,
LSEG l2 
) [static]

Definition at line 2362 of file geo_ops.c.

References FPeq, line_construct_pts(), line_interpt_internal(), on_ps_internal(), LSEG::p, pfree(), PointerIsValid, Point::x, and Point::y.

Referenced by lseg_inside_poly(), and lseg_interpt().

{
    Point      *result;
    LINE        tmp1,
                tmp2;

    /*
     * Find the intersection of the appropriate lines, if any.
     */
    line_construct_pts(&tmp1, &l1->p[0], &l1->p[1]);
    line_construct_pts(&tmp2, &l2->p[0], &l2->p[1]);
    result = line_interpt_internal(&tmp1, &tmp2);
    if (!PointerIsValid(result))
        return NULL;

    /*
     * If the line intersection point isn't within l1 (or equivalently l2),
     * there is no valid segment intersection point at all.
     */
    if (!on_ps_internal(result, l1) ||
        !on_ps_internal(result, l2))
    {
        pfree(result);
        return NULL;
    }

    /*
     * If there is an intersection, then check explicitly for matching
     * endpoints since there may be rounding effects with annoying lsb
     * residue. - tgl 1997-07-09
     */
    if ((FPeq(l1->p[0].x, l2->p[0].x) && FPeq(l1->p[0].y, l2->p[0].y)) ||
        (FPeq(l1->p[0].x, l2->p[1].x) && FPeq(l1->p[0].y, l2->p[1].y)))
    {
        result->x = l1->p[0].x;
        result->y = l1->p[0].y;
    }
    else if ((FPeq(l1->p[1].x, l2->p[0].x) && FPeq(l1->p[1].y, l2->p[0].y)) ||
             (FPeq(l1->p[1].x, l2->p[1].x) && FPeq(l1->p[1].y, l2->p[1].y)))
    {
        result->x = l1->p[1].x;
        result->y = l1->p[1].y;
    }

    return result;
}

Datum lseg_intersect ( PG_FUNCTION_ARGS   ) 

Definition at line 2151 of file geo_ops.c.

References lseg_intersect_internal(), PG_GETARG_LSEG_P, and PG_RETURN_BOOL.

Referenced by interpt_pp().

static bool lseg_intersect_internal ( LSEG l1,
LSEG l2 
) [static]

Definition at line 2160 of file geo_ops.c.

References interpt_sl(), line_construct_pts(), NULL, on_ps_internal(), and LSEG::p.

Referenced by inter_sb(), lseg_dt(), lseg_intersect(), path_inter(), and poly_overlap().

{
    LINE        ln;
    Point      *interpt;
    bool        retval;

    line_construct_pts(&ln, &l2->p[0], &l2->p[1]);
    interpt = interpt_sl(l1, &ln);

    if (interpt != NULL && on_ps_internal(interpt, l2))
        retval = true;          /* interpt on l1 and l2 */
    else
        retval = false;
    return retval;
}

Datum lseg_le ( PG_FUNCTION_ARGS   ) 

Definition at line 2272 of file geo_ops.c.

References FPle, LSEG::p, PG_GETARG_LSEG_P, PG_RETURN_BOOL, and point_dt().

{
    LSEG       *l1 = PG_GETARG_LSEG_P(0);
    LSEG       *l2 = PG_GETARG_LSEG_P(1);

    PG_RETURN_BOOL(FPle(point_dt(&l1->p[0], &l1->p[1]),
                        point_dt(&l2->p[0], &l2->p[1])));
}

Datum lseg_length ( PG_FUNCTION_ARGS   ) 

Definition at line 2135 of file geo_ops.c.

References LSEG::p, PG_GETARG_LSEG_P, PG_RETURN_FLOAT8, and point_dt().

{
    LSEG       *lseg = PG_GETARG_LSEG_P(0);

    PG_RETURN_FLOAT8(point_dt(&lseg->p[0], &lseg->p[1]));
}

Datum lseg_lt ( PG_FUNCTION_ARGS   ) 

Definition at line 2262 of file geo_ops.c.

References FPlt, LSEG::p, PG_GETARG_LSEG_P, PG_RETURN_BOOL, and point_dt().

{
    LSEG       *l1 = PG_GETARG_LSEG_P(0);
    LSEG       *l2 = PG_GETARG_LSEG_P(1);

    PG_RETURN_BOOL(FPlt(point_dt(&l1->p[0], &l1->p[1]),
                        point_dt(&l2->p[0], &l2->p[1])));
}

Datum lseg_ne ( PG_FUNCTION_ARGS   ) 

Definition at line 2250 of file geo_ops.c.

References FPeq, LSEG::p, PG_GETARG_LSEG_P, PG_RETURN_BOOL, Point::x, and Point::y.

{
    LSEG       *l1 = PG_GETARG_LSEG_P(0);
    LSEG       *l2 = PG_GETARG_LSEG_P(1);

    PG_RETURN_BOOL(!FPeq(l1->p[0].x, l2->p[0].x) ||
                   !FPeq(l1->p[0].y, l2->p[0].y) ||
                   !FPeq(l1->p[1].x, l2->p[1].x) ||
                   !FPeq(l1->p[1].y, l2->p[1].y));
}

Datum lseg_out ( PG_FUNCTION_ARGS   ) 

Definition at line 2050 of file geo_ops.c.

References FALSE, LSEG::p, path_encode(), PG_GETARG_LSEG_P, and PG_RETURN_CSTRING.

{
    LSEG       *ls = PG_GETARG_LSEG_P(0);

    PG_RETURN_CSTRING(path_encode(FALSE, 2, (Point *) &(ls->p[0])));
}

Datum lseg_parallel ( PG_FUNCTION_ARGS   ) 

Definition at line 2177 of file geo_ops.c.

References FPeq, LSEG::m, LSEG::p, PG_GETARG_LSEG_P, PG_RETURN_BOOL, and point_sl().

{
    LSEG       *l1 = PG_GETARG_LSEG_P(0);
    LSEG       *l2 = PG_GETARG_LSEG_P(1);

#ifdef NOT_USED
    PG_RETURN_BOOL(FPeq(l1->m, l2->m));
#endif
    PG_RETURN_BOOL(FPeq(point_sl(&l1->p[0], &l1->p[1]),
                        point_sl(&l2->p[0], &l2->p[1])));
}

Datum lseg_perp ( PG_FUNCTION_ARGS   ) 

Definition at line 2199 of file geo_ops.c.

References FPeq, FPzero, LSEG::p, PG_GETARG_LSEG_P, PG_RETURN_BOOL, and point_sl().

{
    LSEG       *l1 = PG_GETARG_LSEG_P(0);
    LSEG       *l2 = PG_GETARG_LSEG_P(1);
    double      m1,
                m2;

    m1 = point_sl(&(l1->p[0]), &(l1->p[1]));
    m2 = point_sl(&(l2->p[0]), &(l2->p[1]));

#ifdef GEODEBUG
    printf("lseg_perp- slopes are %g and %g\n", m1, m2);
#endif
    if (FPzero(m1))
        PG_RETURN_BOOL(FPeq(m2, DBL_MAX));
    else if (FPzero(m2))
        PG_RETURN_BOOL(FPeq(m1, DBL_MAX));

    PG_RETURN_BOOL(FPeq(m1 / m2, -1.0));
}

Datum lseg_recv ( PG_FUNCTION_ARGS   ) 

Definition at line 2061 of file geo_ops.c.

References buf, LSEG::m, LSEG::p, palloc(), PG_GETARG_POINTER, PG_RETURN_LSEG_P, point_sl(), pq_getmsgfloat8(), Point::x, and Point::y.

{
    StringInfo  buf = (StringInfo) PG_GETARG_POINTER(0);
    LSEG       *lseg;

    lseg = (LSEG *) palloc(sizeof(LSEG));

    lseg->p[0].x = pq_getmsgfloat8(buf);
    lseg->p[0].y = pq_getmsgfloat8(buf);
    lseg->p[1].x = pq_getmsgfloat8(buf);
    lseg->p[1].y = pq_getmsgfloat8(buf);

#ifdef NOT_USED
    lseg->m = point_sl(&lseg->p[0], &lseg->p[1]);
#endif

    PG_RETURN_LSEG_P(lseg);
}

Datum lseg_send ( PG_FUNCTION_ARGS   ) 
Datum lseg_vertical ( PG_FUNCTION_ARGS   ) 

Definition at line 2221 of file geo_ops.c.

References FPeq, LSEG::p, PG_GETARG_LSEG_P, PG_RETURN_BOOL, and Point::x.

{
    LSEG       *lseg = PG_GETARG_LSEG_P(0);

    PG_RETURN_BOOL(FPeq(lseg->p[0].x, lseg->p[1].x));
}

static void make_bound_box ( POLYGON poly  )  [static]

Definition at line 3416 of file geo_ops.c.

References POLYGON::boundbox, box_fill(), ereport, errcode(), errmsg(), ERROR, i, POLYGON::npts, POLYGON::p, Point::x, and Point::y.

Referenced by circle_poly(), path_poly(), poly_in(), and poly_recv().

{
    int         i;
    double      x1,
                y1,
                x2,
                y2;

    if (poly->npts > 0)
    {
        x2 = x1 = poly->p[0].x;
        y2 = y1 = poly->p[0].y;
        for (i = 1; i < poly->npts; i++)
        {
            if (poly->p[i].x < x1)
                x1 = poly->p[i].x;
            if (poly->p[i].x > x2)
                x2 = poly->p[i].x;
            if (poly->p[i].y < y1)
                y1 = poly->p[i].y;
            if (poly->p[i].y > y2)
                y2 = poly->p[i].y;
        }

        box_fill(&(poly->boundbox), x1, x2, y1, y2);
    }
    else
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("cannot create bounding box for empty polygon")));
}

Datum on_pb ( PG_FUNCTION_ARGS   ) 

Definition at line 3200 of file geo_ops.c.

References BOX::high, BOX::low, PG_GETARG_BOX_P, PG_GETARG_POINT_P, PG_RETURN_BOOL, Point::x, and Point::y.

Referenced by close_pb(), inter_sb(), and on_sb().

{
    Point      *pt = PG_GETARG_POINT_P(0);
    BOX        *box = PG_GETARG_BOX_P(1);

    PG_RETURN_BOOL(pt->x <= box->high.x && pt->x >= box->low.x &&
                   pt->y <= box->high.y && pt->y >= box->low.y);
}

Datum on_pl ( PG_FUNCTION_ARGS   ) 

Definition at line 3170 of file geo_ops.c.

References LINE::A, LINE::B, LINE::C, FPzero, PG_GETARG_LINE_P, PG_GETARG_POINT_P, PG_RETURN_BOOL, Point::x, and Point::y.

Referenced by on_sl().

{
    Point      *pt = PG_GETARG_POINT_P(0);
    LINE       *line = PG_GETARG_LINE_P(1);

    PG_RETURN_BOOL(FPzero(line->A * pt->x + line->B * pt->y + line->C));
}

Datum on_ppath ( PG_FUNCTION_ARGS   ) 

Definition at line 3231 of file geo_ops.c.

References PATH::closed, FPeq, i, PATH::npts, PATH::p, PG_GETARG_PATH_P, PG_GETARG_POINT_P, PG_RETURN_BOOL, point_dt(), and point_inside().

{
    Point      *pt = PG_GETARG_POINT_P(0);
    PATH       *path = PG_GETARG_PATH_P(1);
    int         i,
                n;
    double      a,
                b;

    /*-- OPEN --*/
    if (!path->closed)
    {
        n = path->npts - 1;
        a = point_dt(pt, &path->p[0]);
        for (i = 0; i < n; i++)
        {
            b = point_dt(pt, &path->p[i + 1]);
            if (FPeq(a + b,
                     point_dt(&path->p[i], &path->p[i + 1])))
                PG_RETURN_BOOL(true);
            a = b;
        }
        PG_RETURN_BOOL(false);
    }

    /*-- CLOSED --*/
    PG_RETURN_BOOL(point_inside(pt, path->npts, path->p) != 0);
}

Datum on_ps ( PG_FUNCTION_ARGS   ) 

Definition at line 3184 of file geo_ops.c.

References on_ps_internal(), PG_GETARG_LSEG_P, PG_GETARG_POINT_P, and PG_RETURN_BOOL.

static bool on_ps_internal ( Point pt,
LSEG lseg 
) [static]

Definition at line 3193 of file geo_ops.c.

References FPeq, LSEG::p, and point_dt().

Referenced by interpt_sl(), lseg_inside_poly(), lseg_interpt_internal(), lseg_intersect_internal(), on_ps(), and touched_lseg_inside_poly().

{
    return FPeq(point_dt(pt, &lseg->p[0]) + point_dt(pt, &lseg->p[1]),
                point_dt(&lseg->p[0], &lseg->p[1]));
}

Datum on_sb ( PG_FUNCTION_ARGS   ) 
Datum on_sl ( PG_FUNCTION_ARGS   ) 
static int pair_count ( char *  s,
char  delim 
) [static]

Definition at line 349 of file geo_ops.c.

References NULL.

Referenced by path_in(), and poly_in().

{
    int         ndelim = 0;

    while ((s = strchr(s, delim)) != NULL)
    {
        ndelim++;
        s++;
    }
    return (ndelim % 2) ? ((ndelim + 1) / 2) : -1;
}

static int pair_decode ( char *  str,
float8 x,
float8 y,
char **  s 
) [static]

Definition at line 155 of file geo_ops.c.

References DELIM, LDELIM, NULL, PointerIsValid, and RDELIM.

Referenced by circle_in(), path_decode(), and point_in().

{
    int         has_delim;
    char       *cp;

    if (!PointerIsValid(str))
        return FALSE;

    while (isspace((unsigned char) *str))
        str++;
    if ((has_delim = (*str == LDELIM)))
        str++;

    while (isspace((unsigned char) *str))
        str++;
    *x = strtod(str, &cp);
    if (cp <= str)
        return FALSE;
    while (isspace((unsigned char) *cp))
        cp++;
    if (*cp++ != DELIM)
        return FALSE;
    while (isspace((unsigned char) *cp))
        cp++;
    *y = strtod(cp, &str);
    if (str <= cp)
        return FALSE;
    while (isspace((unsigned char) *str))
        str++;
    if (has_delim)
    {
        if (*str != RDELIM)
            return FALSE;
        str++;
        while (isspace((unsigned char) *str))
            str++;
    }
    if (s != NULL)
        *s = str;

    return TRUE;
}

static int pair_encode ( float8  x,
float8  y,
char *  str 
) [static]

Definition at line 199 of file geo_ops.c.

References extra_float_digits.

Referenced by circle_out(), and path_encode().

{
    int         ndig = DBL_DIG + extra_float_digits;

    if (ndig < 1)
        ndig = 1;

    sprintf(str, "%.*g,%.*g", ndig, x, ndig, y);
    return TRUE;
}

Datum path_add ( PG_FUNCTION_ARGS   ) 

Definition at line 4226 of file geo_ops.c.

References PATH::closed, PATH::dummy, ereport, errcode(), errmsg(), ERROR, i, PATH::npts, offsetof, PATH::p, palloc(), PG_GETARG_PATH_P, PG_RETURN_NULL, PG_RETURN_PATH_P, SET_VARSIZE, Point::x, and Point::y.

{
    PATH       *p1 = PG_GETARG_PATH_P(0);
    PATH       *p2 = PG_GETARG_PATH_P(1);
    PATH       *result;
    int         size,
                base_size;
    int         i;

    if (p1->closed || p2->closed)
        PG_RETURN_NULL();

    base_size = sizeof(p1->p[0]) * (p1->npts + p2->npts);
    size = offsetof(PATH, p[0]) +base_size;

    /* Check for integer overflow */
    if (base_size / sizeof(p1->p[0]) != (p1->npts + p2->npts) ||
        size <= base_size)
        ereport(ERROR,
                (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
                 errmsg("too many points requested")));

    result = (PATH *) palloc(size);

    SET_VARSIZE(result, size);
    result->npts = (p1->npts + p2->npts);
    result->closed = p1->closed;
    /* prevent instability in unused pad bytes */
    result->dummy = 0;

    for (i = 0; i < p1->npts; i++)
    {
        result->p[i].x = p1->p[i].x;
        result->p[i].y = p1->p[i].y;
    }
    for (i = 0; i < p2->npts; i++)
    {
        result->p[i + p1->npts].x = p2->p[i].x;
        result->p[i + p1->npts].y = p2->p[i].y;
    }

    PG_RETURN_PATH_P(result);
}

Datum path_add_pt ( PG_FUNCTION_ARGS   ) 

Definition at line 4274 of file geo_ops.c.

References i, PATH::npts, PATH::p, PG_GETARG_PATH_P_COPY, PG_GETARG_POINT_P, PG_RETURN_PATH_P, Point::x, and Point::y.

{
    PATH       *path = PG_GETARG_PATH_P_COPY(0);
    Point      *point = PG_GETARG_POINT_P(1);
    int         i;

    for (i = 0; i < path->npts; i++)
    {
        path->p[i].x += point->x;
        path->p[i].y += point->y;
    }

    PG_RETURN_PATH_P(path);
}

Datum path_area ( PG_FUNCTION_ARGS   ) 

Definition at line 1370 of file geo_ops.c.

References PATH::closed, i, PATH::npts, PATH::p, PG_GETARG_PATH_P, PG_RETURN_FLOAT8, PG_RETURN_NULL, Point::x, and Point::y.

{
    PATH       *path = PG_GETARG_PATH_P(0);
    double      area = 0.0;
    int         i,
                j;

    if (!path->closed)
        PG_RETURN_NULL();

    for (i = 0; i < path->npts; i++)
    {
        j = (i + 1) % path->npts;
        area += path->p[i].x * path->p[j].y;
        area -= path->p[i].y * path->p[j].x;
    }

    area *= 0.5;
    PG_RETURN_FLOAT8(area < 0.0 ? -area : area);
}

Datum path_center ( PG_FUNCTION_ARGS   ) 

Definition at line 4350 of file geo_ops.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_PATH_P, and PG_RETURN_NULL.

{
#ifdef NOT_USED
    PATH       *path = PG_GETARG_PATH_P(0);
#endif

    ereport(ERROR,
            (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
             errmsg("function \"path_center\" not implemented")));

    PG_RETURN_NULL();
}

Datum path_close ( PG_FUNCTION_ARGS   ) 

Definition at line 1593 of file geo_ops.c.

References PATH::closed, PG_GETARG_PATH_P_COPY, and PG_RETURN_PATH_P.

{
    PATH       *path = PG_GETARG_PATH_P_COPY(0);

    path->closed = TRUE;

    PG_RETURN_PATH_P(path);
}

static int path_decode ( int  opentype,
int  npts,
char *  str,
int *  isopen,
char **  ss,
Point p 
) [static]

Definition at line 211 of file geo_ops.c.

References DELIM, FALSE, i, LDELIM, LDELIM_EP, pair_decode(), RDELIM, Point::x, and Point::y.

Referenced by box_in(), line_in(), lseg_in(), path_in(), and poly_in().

{
    int         depth = 0;
    char       *s,
               *cp;
    int         i;

    s = str;
    while (isspace((unsigned char) *s))
        s++;
    if ((*isopen = (*s == LDELIM_EP)))
    {
        /* no open delimiter allowed? */
        if (!opentype)
            return FALSE;
        depth++;
        s++;
        while (isspace((unsigned char) *s))
            s++;

    }
    else if (*s == LDELIM)
    {
        cp = (s + 1);
        while (isspace((unsigned char) *cp))
            cp++;
        if (*cp == LDELIM)
        {
#ifdef NOT_USED
            /* nested delimiters with only one point? */
            if (npts <= 1)
                return FALSE;
#endif
            depth++;
            s = cp;
        }
        else if (strrchr(s, LDELIM) == s)
        {
            depth++;
            s = cp;
        }
    }

    for (i = 0; i < npts; i++)
    {
        if (!pair_decode(s, &(p->x), &(p->y), &s))
            return FALSE;

        if (*s == DELIM)
            s++;
        p++;
    }

    while (depth > 0)
    {
        if ((*s == RDELIM)
            || ((*s == RDELIM_EP) && (*isopen) && (depth == 1)))
        {
            depth--;
            s++;
            while (isspace((unsigned char) *s))
                s++;
        }
        else
            return FALSE;
    }
    *ss = s;

    return TRUE;
}   /* path_decode() */

Datum path_distance ( PG_FUNCTION_ARGS   ) 

Definition at line 1697 of file geo_ops.c.

References PATH::closed, DatumGetFloat8, DirectFunctionCall2, i, lseg_distance(), LsegPGetDatum, PATH::npts, PATH::p, PG_GETARG_PATH_P, PG_RETURN_FLOAT8, PG_RETURN_NULL, and statlseg_construct().

{
    PATH       *p1 = PG_GETARG_PATH_P(0);
    PATH       *p2 = PG_GETARG_PATH_P(1);
    float8      min = 0.0;      /* initialize to keep compiler quiet */
    bool        have_min = false;
    float8      tmp;
    int         i,
                j;
    LSEG        seg1,
                seg2;

    for (i = 0; i < p1->npts; i++)
    {
        int         iprev;

        if (i > 0)
            iprev = i - 1;
        else
        {
            if (!p1->closed)
                continue;
            iprev = p1->npts - 1;       /* include the closure segment */
        }

        for (j = 0; j < p2->npts; j++)
        {
            int         jprev;

            if (j > 0)
                jprev = j - 1;
            else
            {
                if (!p2->closed)
                    continue;
                jprev = p2->npts - 1;   /* include the closure segment */
            }

            statlseg_construct(&seg1, &p1->p[iprev], &p1->p[i]);
            statlseg_construct(&seg2, &p2->p[jprev], &p2->p[j]);

            tmp = DatumGetFloat8(DirectFunctionCall2(lseg_distance,
                                                     LsegPGetDatum(&seg1),
                                                     LsegPGetDatum(&seg2)));
            if (!have_min || tmp < min)
            {
                min = tmp;
                have_min = true;
            }
        }
    }

    if (!have_min)
        PG_RETURN_NULL();

    PG_RETURN_FLOAT8(min);
}

Datum path_div_pt ( PG_FUNCTION_ARGS   ) 

Definition at line 4329 of file geo_ops.c.

References DatumGetPointP, DirectFunctionCall2, i, PATH::npts, PATH::p, PG_GETARG_PATH_P_COPY, PG_GETARG_POINT_P, PG_RETURN_PATH_P, point_div(), PointPGetDatum, Point::x, and Point::y.

{
    PATH       *path = PG_GETARG_PATH_P_COPY(0);
    Point      *point = PG_GETARG_POINT_P(1);
    Point      *p;
    int         i;

    for (i = 0; i < path->npts; i++)
    {
        p = DatumGetPointP(DirectFunctionCall2(point_div,
                                               PointPGetDatum(&path->p[i]),
                                               PointPGetDatum(point)));
        path->p[i].x = p->x;
        path->p[i].y = p->y;
    }

    PG_RETURN_PATH_P(path);
}

static char * path_encode ( bool  closed,
int  npts,
Point pt 
) [static]

Definition at line 283 of file geo_ops.c.

References ereport, errcode(), errmsg(), ERROR, FALSE, i, P_MAXLEN, pair_encode(), palloc(), TRUE, Point::x, and Point::y.

Referenced by box_out(), line_out(), lseg_out(), path_out(), point_out(), and poly_out().

{
    int         size = npts * (P_MAXLEN + 3) + 2;
    char       *result;
    char       *cp;
    int         i;

    /* Check for integer overflow */
    if ((size - 2) / npts != (P_MAXLEN + 3))
        ereport(ERROR,
                (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
                 errmsg("too many points requested")));

    result = palloc(size);

    cp = result;
    switch (closed)
    {
        case TRUE:
            *cp++ = LDELIM;
            break;
        case FALSE:
            *cp++ = LDELIM_EP;
            break;
        default:
            break;
    }

    for (i = 0; i < npts; i++)
    {
        *cp++ = LDELIM;
        if (!pair_encode(pt->x, pt->y, cp))
            ereport(ERROR,
                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                     errmsg("could not format \"path\" value")));

        cp += strlen(cp);
        *cp++ = RDELIM;
        *cp++ = DELIM;
        pt++;
    }
    cp--;
    switch (closed)
    {
        case TRUE:
            *cp++ = RDELIM;
            break;
        case FALSE:
            *cp++ = RDELIM_EP;
            break;
        default:
            break;
    }
    *cp = '\0';

    return result;
}   /* path_encode() */

Datum path_in ( PG_FUNCTION_ARGS   ) 

Definition at line 1393 of file geo_ops.c.

References PATH::closed, PATH::dummy, ereport, errcode(), errmsg(), ERROR, LDELIM, PATH::npts, offsetof, PATH::p, pair_count(), palloc(), path_decode(), PG_GETARG_CSTRING, PG_RETURN_PATH_P, RDELIM, SET_VARSIZE, and TRUE.

Referenced by poly2path().

{
    char       *str = PG_GETARG_CSTRING(0);
    PATH       *path;
    int         isopen;
    char       *s;
    int         npts;
    int         size;
    int         depth = 0;

    if ((npts = pair_count(str, ',')) <= 0)
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                 errmsg("invalid input syntax for type path: \"%s\"", str)));

    s = str;
    while (isspace((unsigned char) *s))
        s++;

    /* skip single leading paren */
    if ((*s == LDELIM) && (strrchr(s, LDELIM) == s))
    {
        s++;
        depth++;
    }

    size = offsetof(PATH, p[0]) +sizeof(path->p[0]) * npts;
    path = (PATH *) palloc(size);

    SET_VARSIZE(path, size);
    path->npts = npts;

    if ((!path_decode(TRUE, npts, s, &isopen, &s, &(path->p[0])))
    && (!((depth == 0) && (*s == '\0'))) && !((depth >= 1) && (*s == RDELIM)))
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                 errmsg("invalid input syntax for type path: \"%s\"", str)));

    path->closed = (!isopen);
    /* prevent instability in unused pad bytes */
    path->dummy = 0;

    PG_RETURN_PATH_P(path);
}

Datum path_inter ( PG_FUNCTION_ARGS   ) 

Definition at line 1619 of file geo_ops.c.

References box_ov(), PATH::closed, BOX::high, i, BOX::low, lseg_intersect_internal(), Max, Min, PATH::npts, PATH::p, PG_GETARG_PATH_P, PG_RETURN_BOOL, statlseg_construct(), Point::x, and Point::y.

{
    PATH       *p1 = PG_GETARG_PATH_P(0);
    PATH       *p2 = PG_GETARG_PATH_P(1);
    BOX         b1,
                b2;
    int         i,
                j;
    LSEG        seg1,
                seg2;

    if (p1->npts <= 0 || p2->npts <= 0)
        PG_RETURN_BOOL(false);

    b1.high.x = b1.low.x = p1->p[0].x;
    b1.high.y = b1.low.y = p1->p[0].y;
    for (i = 1; i < p1->npts; i++)
    {
        b1.high.x = Max(p1->p[i].x, b1.high.x);
        b1.high.y = Max(p1->p[i].y, b1.high.y);
        b1.low.x = Min(p1->p[i].x, b1.low.x);
        b1.low.y = Min(p1->p[i].y, b1.low.y);
    }
    b2.high.x = b2.low.x = p2->p[0].x;
    b2.high.y = b2.low.y = p2->p[0].y;
    for (i = 1; i < p2->npts; i++)
    {
        b2.high.x = Max(p2->p[i].x, b2.high.x);
        b2.high.y = Max(p2->p[i].y, b2.high.y);
        b2.low.x = Min(p2->p[i].x, b2.low.x);
        b2.low.y = Min(p2->p[i].y, b2.low.y);
    }
    if (!box_ov(&b1, &b2))
        PG_RETURN_BOOL(false);

    /* pairwise check lseg intersections */
    for (i = 0; i < p1->npts; i++)
    {
        int         iprev;

        if (i > 0)
            iprev = i - 1;
        else
        {
            if (!p1->closed)
                continue;
            iprev = p1->npts - 1;       /* include the closure segment */
        }

        for (j = 0; j < p2->npts; j++)
        {
            int         jprev;

            if (j > 0)
                jprev = j - 1;
            else
            {
                if (!p2->closed)
                    continue;
                jprev = p2->npts - 1;   /* include the closure segment */
            }

            statlseg_construct(&seg1, &p1->p[iprev], &p1->p[i]);
            statlseg_construct(&seg2, &p2->p[jprev], &p2->p[j]);
            if (lseg_intersect_internal(&seg1, &seg2))
                PG_RETURN_BOOL(true);
        }
    }

    /* if we dropped through, no two segs intersected */
    PG_RETURN_BOOL(false);
}

Datum path_isclosed ( PG_FUNCTION_ARGS   ) 

Definition at line 1568 of file geo_ops.c.

References PATH::closed, PG_GETARG_PATH_P, and PG_RETURN_BOOL.

{
    PATH       *path = PG_GETARG_PATH_P(0);

    PG_RETURN_BOOL(path->closed);
}

Datum path_isopen ( PG_FUNCTION_ARGS   ) 

Definition at line 1576 of file geo_ops.c.

References PATH::closed, PG_GETARG_PATH_P, and PG_RETURN_BOOL.

{
    PATH       *path = PG_GETARG_PATH_P(0);

    PG_RETURN_BOOL(!path->closed);
}

Datum path_length ( PG_FUNCTION_ARGS   ) 

Definition at line 1761 of file geo_ops.c.

References PATH::closed, i, PATH::npts, PATH::p, PG_GETARG_PATH_P, PG_RETURN_FLOAT8, and point_dt().

{
    PATH       *path = PG_GETARG_PATH_P(0);
    float8      result = 0.0;
    int         i;

    for (i = 0; i < path->npts; i++)
    {
        int         iprev;

        if (i > 0)
            iprev = i - 1;
        else
        {
            if (!path->closed)
                continue;
            iprev = path->npts - 1;     /* include the closure segment */
        }

        result += point_dt(&path->p[iprev], &path->p[i]);
    }

    PG_RETURN_FLOAT8(result);
}

Datum path_mul_pt ( PG_FUNCTION_ARGS   ) 

Definition at line 4309 of file geo_ops.c.

References DatumGetPointP, DirectFunctionCall2, i, PATH::npts, PATH::p, PG_GETARG_PATH_P_COPY, PG_GETARG_POINT_P, PG_RETURN_PATH_P, point_mul(), PointPGetDatum, Point::x, and Point::y.

{
    PATH       *path = PG_GETARG_PATH_P_COPY(0);
    Point      *point = PG_GETARG_POINT_P(1);
    Point      *p;
    int         i;

    for (i = 0; i < path->npts; i++)
    {
        p = DatumGetPointP(DirectFunctionCall2(point_mul,
                                               PointPGetDatum(&path->p[i]),
                                               PointPGetDatum(point)));
        path->p[i].x = p->x;
        path->p[i].y = p->y;
    }

    PG_RETURN_PATH_P(path);
}

Datum path_n_eq ( PG_FUNCTION_ARGS   ) 

Definition at line 1537 of file geo_ops.c.

References PATH::npts, PG_GETARG_PATH_P, and PG_RETURN_BOOL.

{
    PATH       *p1 = PG_GETARG_PATH_P(0);
    PATH       *p2 = PG_GETARG_PATH_P(1);

    PG_RETURN_BOOL(p1->npts == p2->npts);
}

Datum path_n_ge ( PG_FUNCTION_ARGS   ) 

Definition at line 1555 of file geo_ops.c.

References PATH::npts, PG_GETARG_PATH_P, and PG_RETURN_BOOL.

{
    PATH       *p1 = PG_GETARG_PATH_P(0);
    PATH       *p2 = PG_GETARG_PATH_P(1);

    PG_RETURN_BOOL(p1->npts >= p2->npts);
}

Datum path_n_gt ( PG_FUNCTION_ARGS   ) 

Definition at line 1528 of file geo_ops.c.

References PATH::npts, PG_GETARG_PATH_P, and PG_RETURN_BOOL.

{
    PATH       *p1 = PG_GETARG_PATH_P(0);
    PATH       *p2 = PG_GETARG_PATH_P(1);

    PG_RETURN_BOOL(p1->npts > p2->npts);
}

Datum path_n_le ( PG_FUNCTION_ARGS   ) 

Definition at line 1546 of file geo_ops.c.

References PATH::npts, PG_GETARG_PATH_P, and PG_RETURN_BOOL.

{
    PATH       *p1 = PG_GETARG_PATH_P(0);
    PATH       *p2 = PG_GETARG_PATH_P(1);

    PG_RETURN_BOOL(p1->npts <= p2->npts);
}

Datum path_n_lt ( PG_FUNCTION_ARGS   ) 

Definition at line 1519 of file geo_ops.c.

References PATH::npts, PG_GETARG_PATH_P, and PG_RETURN_BOOL.

{
    PATH       *p1 = PG_GETARG_PATH_P(0);
    PATH       *p2 = PG_GETARG_PATH_P(1);

    PG_RETURN_BOOL(p1->npts < p2->npts);
}

Datum path_npoints ( PG_FUNCTION_ARGS   ) 

Definition at line 1584 of file geo_ops.c.

References PATH::npts, PG_GETARG_PATH_P, and PG_RETURN_INT32.

{
    PATH       *path = PG_GETARG_PATH_P(0);

    PG_RETURN_INT32(path->npts);
}

Datum path_open ( PG_FUNCTION_ARGS   ) 

Definition at line 1603 of file geo_ops.c.

References PATH::closed, PG_GETARG_PATH_P_COPY, and PG_RETURN_PATH_P.

{
    PATH       *path = PG_GETARG_PATH_P_COPY(0);

    path->closed = FALSE;

    PG_RETURN_PATH_P(path);
}

Datum path_out ( PG_FUNCTION_ARGS   ) 

Definition at line 1440 of file geo_ops.c.

References PATH::closed, PATH::npts, PATH::p, path_encode(), PG_GETARG_PATH_P, and PG_RETURN_CSTRING.

{
    PATH       *path = PG_GETARG_PATH_P(0);

    PG_RETURN_CSTRING(path_encode(path->closed, path->npts, path->p));
}

Datum path_poly ( PG_FUNCTION_ARGS   ) 

Definition at line 4364 of file geo_ops.c.

References PATH::closed, ereport, errcode(), errmsg(), ERROR, i, make_bound_box(), POLYGON::npts, PATH::npts, offsetof, PATH::p, POLYGON::p, palloc(), PG_GETARG_PATH_P, PG_RETURN_POLYGON_P, SET_VARSIZE, Point::x, and Point::y.

{
    PATH       *path = PG_GETARG_PATH_P(0);
    POLYGON    *poly;
    int         size;
    int         i;

    /* This is not very consistent --- other similar cases return NULL ... */
    if (!path->closed)
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("open path cannot be converted to polygon")));

    size = offsetof(POLYGON, p[0]) +sizeof(poly->p[0]) * path->npts;
    poly = (POLYGON *) palloc(size);

    SET_VARSIZE(poly, size);
    poly->npts = path->npts;

    for (i = 0; i < path->npts; i++)
    {
        poly->p[i].x = path->p[i].x;
        poly->p[i].y = path->p[i].y;
    }

    make_bound_box(poly);

    PG_RETURN_POLYGON_P(poly);
}

Datum path_recv ( PG_FUNCTION_ARGS   ) 

Definition at line 1454 of file geo_ops.c.

References buf, PATH::closed, PATH::dummy, ereport, errcode(), errmsg(), ERROR, i, PATH::npts, offsetof, PATH::p, palloc(), PG_GETARG_POINTER, PG_RETURN_PATH_P, pq_getmsgbyte(), pq_getmsgfloat8(), pq_getmsgint(), SET_VARSIZE, Point::x, and Point::y.

{
    StringInfo  buf = (StringInfo) PG_GETARG_POINTER(0);
    PATH       *path;
    int         closed;
    int32       npts;
    int32       i;
    int         size;

    closed = pq_getmsgbyte(buf);
    npts = pq_getmsgint(buf, sizeof(int32));
    if (npts <= 0 || npts >= (int32) ((INT_MAX - offsetof(PATH, p[0])) / sizeof(Point)))
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
             errmsg("invalid number of points in external \"path\" value")));

    size = offsetof(PATH, p[0]) +sizeof(path->p[0]) * npts;
    path = (PATH *) palloc(size);

    SET_VARSIZE(path, size);
    path->npts = npts;
    path->closed = (closed ? 1 : 0);
    /* prevent instability in unused pad bytes */
    path->dummy = 0;

    for (i = 0; i < npts; i++)
    {
        path->p[i].x = pq_getmsgfloat8(buf);
        path->p[i].y = pq_getmsgfloat8(buf);
    }

    PG_RETURN_PATH_P(path);
}

Datum path_send ( PG_FUNCTION_ARGS   ) 

Definition at line 1492 of file geo_ops.c.

References buf, PATH::closed, i, PATH::npts, PATH::p, PG_GETARG_PATH_P, PG_RETURN_BYTEA_P, pq_begintypsend(), pq_endtypsend(), pq_sendbyte(), pq_sendfloat8(), pq_sendint(), Point::x, and Point::y.

{
    PATH       *path = PG_GETARG_PATH_P(0);
    StringInfoData buf;
    int32       i;

    pq_begintypsend(&buf);
    pq_sendbyte(&buf, path->closed ? 1 : 0);
    pq_sendint(&buf, path->npts, sizeof(int32));
    for (i = 0; i < path->npts; i++)
    {
        pq_sendfloat8(&buf, path->p[i].x);
        pq_sendfloat8(&buf, path->p[i].y);
    }
    PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}

Datum path_sub_pt ( PG_FUNCTION_ARGS   ) 

Definition at line 4290 of file geo_ops.c.

References i, PATH::npts, PATH::p, PG_GETARG_PATH_P_COPY, PG_GETARG_POINT_P, PG_RETURN_PATH_P, Point::x, and Point::y.

{
    PATH       *path = PG_GETARG_PATH_P_COPY(0);
    Point      *point = PG_GETARG_POINT_P(1);
    int         i;

    for (i = 0; i < path->npts; i++)
    {
        path->p[i].x -= point->x;
        path->p[i].y -= point->y;
    }

    PG_RETURN_PATH_P(path);
}

double pg_hypot ( double  x,
double  y 
)

Definition at line 5445 of file geo_ops.c.

References get_float8_infinity(), get_float8_nan(), and isinf().

{
    double      yx;

    /* Handle INF and NaN properly */
    if (isinf(x) || isinf(y))
        return get_float8_infinity();

    if (isnan(x) || isnan(y))
        return get_float8_nan();

    /* Else, drop any minus signs */
    x = fabs(x);
    y = fabs(y);

    /* Swap x and y if needed to make x the larger one */
    if (x < y)
    {
        double      temp = x;

        x = y;
        y = temp;
    }

    /*
     * If y is zero, the hypotenuse is x.  This test saves a few cycles in
     * such cases, but more importantly it also protects against
     * divide-by-zero errors, since now x >= y.
     */
    if (y == 0.0)
        return x;

    /* Determine the hypotenuse */
    yx = y / x;
    return x * sqrt(1.0 + (yx * yx));
}

static bool plist_same ( int  npts,
Point p1,
Point p2 
) [static]

Definition at line 5364 of file geo_ops.c.

References FPeq, and i.

Referenced by poly_same().

{
    int         i,
                ii,
                j;

    /* find match for first point */
    for (i = 0; i < npts; i++)
    {
        if ((FPeq(p2[i].x, p1[0].x))
            && (FPeq(p2[i].y, p1[0].y)))
        {

            /* match found? then look forward through remaining points */
            for (ii = 1, j = i + 1; ii < npts; ii++, j++)
            {
                if (j >= npts)
                    j = 0;
                if ((!FPeq(p2[j].x, p1[ii].x))
                    || (!FPeq(p2[j].y, p1[ii].y)))
                {
#ifdef GEODEBUG
                    printf("plist_same- %d failed forward match with %d\n", j, ii);
#endif
                    break;
                }
            }
#ifdef GEODEBUG
            printf("plist_same- ii = %d/%d after forward match\n", ii, npts);
#endif
            if (ii == npts)
                return TRUE;

            /* match not found forwards? then look backwards */
            for (ii = 1, j = i - 1; ii < npts; ii++, j--)
            {
                if (j < 0)
                    j = (npts - 1);
                if ((!FPeq(p2[j].x, p1[ii].x))
                    || (!FPeq(p2[j].y, p1[ii].y)))
                {
#ifdef GEODEBUG
                    printf("plist_same- %d failed reverse match with %d\n", j, ii);
#endif
                    break;
                }
            }
#ifdef GEODEBUG
            printf("plist_same- ii = %d/%d after reverse match\n", ii, npts);
#endif
            if (ii == npts)
                return TRUE;
        }
    }

    return FALSE;
}

Datum point_above ( PG_FUNCTION_ARGS   ) 

Definition at line 1915 of file geo_ops.c.

References FPgt, PG_GETARG_POINT_P, PG_RETURN_BOOL, and Point::y.

Referenced by getQuadrant(), spg_quad_inner_consistent(), and spg_quad_leaf_consistent().

{
    Point      *pt1 = PG_GETARG_POINT_P(0);
    Point      *pt2 = PG_GETARG_POINT_P(1);

    PG_RETURN_BOOL(FPgt(pt1->y, pt2->y));
}

Datum point_add ( PG_FUNCTION_ARGS   ) 

Definition at line 4066 of file geo_ops.c.

References palloc(), PG_GETARG_POINT_P, PG_RETURN_POINT_P, Point::x, and Point::y.

{
    Point      *p1 = PG_GETARG_POINT_P(0);
    Point      *p2 = PG_GETARG_POINT_P(1);
    Point      *result;

    result = (Point *) palloc(sizeof(Point));

    result->x = (p1->x + p2->x);
    result->y = (p1->y + p2->y);

    PG_RETURN_POINT_P(result);
}

Datum point_below ( PG_FUNCTION_ARGS   ) 

Definition at line 1924 of file geo_ops.c.

References FPlt, PG_GETARG_POINT_P, PG_RETURN_BOOL, and Point::y.

Referenced by getQuadrant(), spg_quad_inner_consistent(), and spg_quad_leaf_consistent().

{
    Point      *pt1 = PG_GETARG_POINT_P(0);
    Point      *pt2 = PG_GETARG_POINT_P(1);

    PG_RETURN_BOOL(FPlt(pt1->y, pt2->y));
}

static Point * point_construct ( double  x,
double  y 
) [static]

Definition at line 1861 of file geo_ops.c.

References palloc(), Point::x, and Point::y.

Referenced by construct_point(), line_distance(), and line_interpt_internal().

{
    Point      *result = (Point *) palloc(sizeof(Point));

    result->x = x;
    result->y = y;
    return result;
}

static Point * point_copy ( Point pt  )  [static]

Definition at line 1872 of file geo_ops.c.

References palloc(), PointerIsValid, Point::x, and Point::y.

Referenced by close_ls(), close_lseg(), close_ps(), and close_sl().

{
    Point      *result;

    if (!PointerIsValid(pt))
        return NULL;

    result = (Point *) palloc(sizeof(Point));

    result->x = pt->x;
    result->y = pt->y;
    return result;
}

Datum point_distance ( PG_FUNCTION_ARGS   ) 

Definition at line 1973 of file geo_ops.c.

References HYPOT, PG_GETARG_POINT_P, PG_RETURN_FLOAT8, Point::x, and Point::y.

{
    Point      *pt1 = PG_GETARG_POINT_P(0);
    Point      *pt2 = PG_GETARG_POINT_P(1);

    PG_RETURN_FLOAT8(HYPOT(pt1->x - pt2->x, pt1->y - pt2->y));
}

Datum point_div ( PG_FUNCTION_ARGS   ) 

Definition at line 4111 of file geo_ops.c.

References ereport, errcode(), errmsg(), ERROR, palloc(), PG_GETARG_POINT_P, PG_RETURN_POINT_P, Point::x, and Point::y.

Referenced by box_div(), circle_div_pt(), and path_div_pt().

{
    Point      *p1 = PG_GETARG_POINT_P(0);
    Point      *p2 = PG_GETARG_POINT_P(1);
    Point      *result;
    double      div;

    result = (Point *) palloc(sizeof(Point));

    div = (p2->x * p2->x) + (p2->y * p2->y);

    if (div == 0.0)
        ereport(ERROR,
                (errcode(ERRCODE_DIVISION_BY_ZERO),
                 errmsg("division by zero")));

    result->x = ((p1->x * p2->x) + (p1->y * p2->y)) / div;
    result->y = ((p2->x * p1->y) - (p2->y * p1->x)) / div;

    PG_RETURN_POINT_P(result);
}

double point_dt ( Point pt1,
Point pt2 
)

Definition at line 1982 of file geo_ops.c.

References HYPOT, Point::x, and Point::y.

Referenced by box_circle(), circle_contain(), circle_contain_pt(), circle_contained(), circle_distance(), circle_overlap(), dist_pb(), dist_pc(), dist_ppath(), dist_ps_internal(), lseg_ge(), lseg_gt(), lseg_le(), lseg_length(), lseg_lt(), on_ppath(), on_ps_internal(), path_length(), poly_circle(), pt_contained_circle(), pt_in_widget(), and regress_dist_ptpath().

{
#ifdef GEODEBUG
    printf("point_dt- segment (%f,%f),(%f,%f) length is %f\n",
    pt1->x, pt1->y, pt2->x, pt2->y, HYPOT(pt1->x - pt2->x, pt1->y - pt2->y));
#endif
    return HYPOT(pt1->x - pt2->x, pt1->y - pt2->y);
}

Datum point_eq ( PG_FUNCTION_ARGS   ) 

Definition at line 1951 of file geo_ops.c.

References FPeq, PG_GETARG_POINT_P, PG_RETURN_BOOL, Point::x, and Point::y.

Referenced by spg_quad_leaf_consistent().

{
    Point      *pt1 = PG_GETARG_POINT_P(0);
    Point      *pt2 = PG_GETARG_POINT_P(1);

    PG_RETURN_BOOL(FPeq(pt1->x, pt2->x) && FPeq(pt1->y, pt2->y));
}

Datum point_horiz ( PG_FUNCTION_ARGS   ) 

Definition at line 1942 of file geo_ops.c.

References FPeq, PG_GETARG_POINT_P, PG_RETURN_BOOL, and Point::y.

Referenced by getQuadrant().

{
    Point      *pt1 = PG_GETARG_POINT_P(0);
    Point      *pt2 = PG_GETARG_POINT_P(1);

    PG_RETURN_BOOL(FPeq(pt1->y, pt2->y));
}

Datum point_in ( PG_FUNCTION_ARGS   ) 

Definition at line 1800 of file geo_ops.c.

References ereport, errcode(), errmsg(), ERROR, pair_decode(), palloc(), PG_GETARG_CSTRING, PG_RETURN_POINT_P, Point::x, and Point::y.

{
    char       *str = PG_GETARG_CSTRING(0);
    Point      *point;
    double      x,
                y;
    char       *s;

    if (!pair_decode(str, &x, &y, &s) || (*s != '\0'))
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                 errmsg("invalid input syntax for type point: \"%s\"", str)));

    point = (Point *) palloc(sizeof(Point));

    point->x = x;
    point->y = y;

    PG_RETURN_POINT_P(point);
}

static int point_inside ( Point p,
int  npts,
Point plist 
) [static]

Definition at line 5251 of file geo_ops.c.

References i, lseg_crossing(), POINT_ON_POLYGON, Point::x, and Point::y.

Referenced by dist_cpoly(), lseg_inside_poly(), on_ppath(), poly_contain_pt(), poly_overlap(), and pt_contained_poly().

{
    double      x0,
                y0;
    double      prev_x,
                prev_y;
    int         i = 0;
    double      x,
                y;
    int         cross,
                total_cross = 0;

    if (npts <= 0)
        return 0;

    /* compute first polygon point relative to single point */
    x0 = plist[0].x - p->x;
    y0 = plist[0].y - p->y;

    prev_x = x0;
    prev_y = y0;
    /* loop over polygon points and aggregate total_cross */
    for (i = 1; i < npts; i++)
    {
        /* compute next polygon point relative to single point */
        x = plist[i].x - p->x;
        y = plist[i].y - p->y;

        /* compute previous to current point crossing */
        if ((cross = lseg_crossing(x, y, prev_x, prev_y)) == POINT_ON_POLYGON)
            return 2;
        total_cross += cross;

        prev_x = x;
        prev_y = y;
    }

    /* now do the first point */
    if ((cross = lseg_crossing(x0, y0, prev_x, prev_y)) == POINT_ON_POLYGON)
        return 2;
    total_cross += cross;

    if (total_cross != 0)
        return 1;
    return 0;
}

Datum point_left ( PG_FUNCTION_ARGS   ) 

Definition at line 1897 of file geo_ops.c.

References FPlt, PG_GETARG_POINT_P, PG_RETURN_BOOL, and Point::x.

Referenced by getQuadrant(), spg_quad_inner_consistent(), and spg_quad_leaf_consistent().

{
    Point      *pt1 = PG_GETARG_POINT_P(0);
    Point      *pt2 = PG_GETARG_POINT_P(1);

    PG_RETURN_BOOL(FPlt(pt1->x, pt2->x));
}

Datum point_mul ( PG_FUNCTION_ARGS   ) 

Definition at line 4096 of file geo_ops.c.

References palloc(), PG_GETARG_POINT_P, PG_RETURN_POINT_P, Point::x, and Point::y.

Referenced by box_mul(), circle_mul_pt(), and path_mul_pt().

{
    Point      *p1 = PG_GETARG_POINT_P(0);
    Point      *p2 = PG_GETARG_POINT_P(1);
    Point      *result;

    result = (Point *) palloc(sizeof(Point));

    result->x = (p1->x * p2->x) - (p1->y * p2->y);
    result->y = (p1->x * p2->y) + (p1->y * p2->x);

    PG_RETURN_POINT_P(result);
}

Datum point_ne ( PG_FUNCTION_ARGS   ) 

Definition at line 1960 of file geo_ops.c.

References FPne, PG_GETARG_POINT_P, PG_RETURN_BOOL, Point::x, and Point::y.

{
    Point      *pt1 = PG_GETARG_POINT_P(0);
    Point      *pt2 = PG_GETARG_POINT_P(1);

    PG_RETURN_BOOL(FPne(pt1->x, pt2->x) || FPne(pt1->y, pt2->y));
}

Datum point_out ( PG_FUNCTION_ARGS   ) 

Definition at line 1822 of file geo_ops.c.

References path_encode(), PG_GETARG_POINT_P, and PG_RETURN_CSTRING.

Datum point_recv ( PG_FUNCTION_ARGS   ) 

Definition at line 1833 of file geo_ops.c.

References buf, palloc(), PG_GETARG_POINTER, PG_RETURN_POINT_P, pq_getmsgfloat8(), Point::x, and Point::y.

{
    StringInfo  buf = (StringInfo) PG_GETARG_POINTER(0);
    Point      *point;

    point = (Point *) palloc(sizeof(Point));
    point->x = pq_getmsgfloat8(buf);
    point->y = pq_getmsgfloat8(buf);
    PG_RETURN_POINT_P(point);
}

Datum point_right ( PG_FUNCTION_ARGS   ) 

Definition at line 1906 of file geo_ops.c.

References FPgt, PG_GETARG_POINT_P, PG_RETURN_BOOL, and Point::x.

Referenced by getQuadrant(), spg_quad_inner_consistent(), and spg_quad_leaf_consistent().

{
    Point      *pt1 = PG_GETARG_POINT_P(0);
    Point      *pt2 = PG_GETARG_POINT_P(1);

    PG_RETURN_BOOL(FPgt(pt1->x, pt2->x));
}

Datum point_send ( PG_FUNCTION_ARGS   ) 
double point_sl ( Point pt1,
Point pt2 
)

Definition at line 2002 of file geo_ops.c.

References FPeq, Point::x, and Point::y.

Referenced by close_ps(), lseg_construct(), lseg_in(), lseg_parallel(), lseg_perp(), lseg_recv(), point_slope(), regress_lseg_construct(), and statlseg_construct().

{
    return (FPeq(pt1->x, pt2->x)
            ? (double) DBL_MAX
            : (pt1->y - pt2->y) / (pt1->x - pt2->x));
}

Datum point_slope ( PG_FUNCTION_ARGS   ) 

Definition at line 1992 of file geo_ops.c.

References PG_GETARG_POINT_P, PG_RETURN_FLOAT8, and point_sl().

Datum point_sub ( PG_FUNCTION_ARGS   ) 

Definition at line 4081 of file geo_ops.c.

References palloc(), PG_GETARG_POINT_P, PG_RETURN_POINT_P, Point::x, and Point::y.

{
    Point      *p1 = PG_GETARG_POINT_P(0);
    Point      *p2 = PG_GETARG_POINT_P(1);
    Point      *result;

    result = (Point *) palloc(sizeof(Point));

    result->x = (p1->x - p2->x);
    result->y = (p1->y - p2->y);

    PG_RETURN_POINT_P(result);
}

Datum point_vert ( PG_FUNCTION_ARGS   ) 

Definition at line 1933 of file geo_ops.c.

References FPeq, PG_GETARG_POINT_P, PG_RETURN_BOOL, and Point::x.

Referenced by getQuadrant().

{
    Point      *pt1 = PG_GETARG_POINT_P(0);
    Point      *pt2 = PG_GETARG_POINT_P(1);

    PG_RETURN_BOOL(FPeq(pt1->x, pt2->x));
}

Datum points_box ( PG_FUNCTION_ARGS   ) 

Definition at line 4141 of file geo_ops.c.

References box_construct(), PG_GETARG_POINT_P, PG_RETURN_BOX_P, Point::x, and Point::y.

{
    Point      *p1 = PG_GETARG_POINT_P(0);
    Point      *p2 = PG_GETARG_POINT_P(1);

    PG_RETURN_BOX_P(box_construct(p1->x, p2->x, p1->y, p2->y));
}

Datum poly_above ( PG_FUNCTION_ARGS   ) 

Definition at line 3705 of file geo_ops.c.

References POLYGON::boundbox, BOX::high, BOX::low, PG_FREE_IF_COPY, PG_GETARG_POLYGON_P, PG_RETURN_BOOL, and Point::y.

{
    POLYGON    *polya = PG_GETARG_POLYGON_P(0);
    POLYGON    *polyb = PG_GETARG_POLYGON_P(1);
    bool        result;

    result = polya->boundbox.low.y > polyb->boundbox.high.y;

    /*
     * Avoid leaking memory for toasted inputs ... needed for rtree indexes
     */
    PG_FREE_IF_COPY(polya, 0);
    PG_FREE_IF_COPY(polyb, 1);

    PG_RETURN_BOOL(result);
}

Datum poly_below ( PG_FUNCTION_ARGS   ) 

Definition at line 3659 of file geo_ops.c.

References POLYGON::boundbox, BOX::high, BOX::low, PG_FREE_IF_COPY, PG_GETARG_POLYGON_P, PG_RETURN_BOOL, and Point::y.

{
    POLYGON    *polya = PG_GETARG_POLYGON_P(0);
    POLYGON    *polyb = PG_GETARG_POLYGON_P(1);
    bool        result;

    result = polya->boundbox.high.y < polyb->boundbox.low.y;

    /*
     * Avoid leaking memory for toasted inputs ... needed for rtree indexes
     */
    PG_FREE_IF_COPY(polya, 0);
    PG_FREE_IF_COPY(polyb, 1);

    PG_RETURN_BOOL(result);
}

Datum poly_box ( PG_FUNCTION_ARGS   ) 

Definition at line 4427 of file geo_ops.c.

References POLYGON::boundbox, box_copy(), POLYGON::npts, PG_GETARG_POLYGON_P, PG_RETURN_BOX_P, and PG_RETURN_NULL.

{
    POLYGON    *poly = PG_GETARG_POLYGON_P(0);
    BOX        *box;

    if (poly->npts < 1)
        PG_RETURN_NULL();

    box = box_copy(&poly->boundbox);

    PG_RETURN_BOX_P(box);
}

Datum poly_center ( PG_FUNCTION_ARGS   ) 
Datum poly_circle ( PG_FUNCTION_ARGS   ) 

Definition at line 5193 of file geo_ops.c.

References CIRCLE::center, ereport, errcode(), errmsg(), ERROR, FPzero, i, POLYGON::npts, POLYGON::p, palloc(), PG_GETARG_POLYGON_P, PG_RETURN_CIRCLE_P, point_dt(), CIRCLE::radius, Point::x, and Point::y.

Referenced by poly_center().

{
    POLYGON    *poly = PG_GETARG_POLYGON_P(0);
    CIRCLE     *circle;
    int         i;

    if (poly->npts < 2)
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("cannot convert empty polygon to circle")));

    circle = (CIRCLE *) palloc(sizeof(CIRCLE));

    circle->center.x = 0;
    circle->center.y = 0;
    circle->radius = 0;

    for (i = 0; i < poly->npts; i++)
    {
        circle->center.x += poly->p[i].x;
        circle->center.y += poly->p[i].y;
    }
    circle->center.x /= poly->npts;
    circle->center.y /= poly->npts;

    for (i = 0; i < poly->npts; i++)
        circle->radius += point_dt(&poly->p[i], &circle->center);
    circle->radius /= poly->npts;

    if (FPzero(circle->radius))
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("cannot convert empty polygon to circle")));

    PG_RETURN_CIRCLE_P(circle);
}

Datum poly_contain ( PG_FUNCTION_ARGS   ) 

Definition at line 3959 of file geo_ops.c.

References POLYGON::boundbox, box_contain(), BoxPGetDatum, DatumGetBool, DirectFunctionCall2, i, lseg_inside_poly(), POLYGON::npts, POLYGON::p, LSEG::p, PG_FREE_IF_COPY, PG_GETARG_POLYGON_P, and PG_RETURN_BOOL.

Referenced by poly_contained().

{
    POLYGON    *polya = PG_GETARG_POLYGON_P(0);
    POLYGON    *polyb = PG_GETARG_POLYGON_P(1);
    bool        result;

    /*
     * Quick check to see if bounding box is contained.
     */
    if (polya->npts > 0 && polyb->npts > 0 &&
        DatumGetBool(DirectFunctionCall2(box_contain,
                                         BoxPGetDatum(&polya->boundbox),
                                         BoxPGetDatum(&polyb->boundbox))))
    {
        int         i;
        LSEG        s;

        s.p[0] = polyb->p[polyb->npts - 1];
        result = true;

        for (i = 0; i < polyb->npts && result; i++)
        {
            s.p[1] = polyb->p[i];
            result = lseg_inside_poly(s.p, s.p + 1, polya, 0);
            s.p[0] = s.p[1];
        }
    }
    else
    {
        result = false;
    }

    /*
     * Avoid leaking memory for toasted inputs ... needed for rtree indexes
     */
    PG_FREE_IF_COPY(polya, 0);
    PG_FREE_IF_COPY(polyb, 1);

    PG_RETURN_BOOL(result);
}

Datum poly_contain_pt ( PG_FUNCTION_ARGS   ) 
Datum poly_contained ( PG_FUNCTION_ARGS   ) 

Definition at line 4005 of file geo_ops.c.

References DirectFunctionCall2, PG_GETARG_DATUM, PG_RETURN_DATUM, and poly_contain().

{
    Datum       polya = PG_GETARG_DATUM(0);
    Datum       polyb = PG_GETARG_DATUM(1);

    /* Just switch the arguments and pass it off to poly_contain */
    PG_RETURN_DATUM(DirectFunctionCall2(poly_contain, polyb, polya));
}

Datum poly_distance ( PG_FUNCTION_ARGS   ) 

Definition at line 4035 of file geo_ops.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_POLYGON_P, and PG_RETURN_NULL.

{
#ifdef NOT_USED
    POLYGON    *polya = PG_GETARG_POLYGON_P(0);
    POLYGON    *polyb = PG_GETARG_POLYGON_P(1);
#endif

    ereport(ERROR,
            (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
             errmsg("function \"poly_distance\" not implemented")));

    PG_RETURN_NULL();
}

Datum poly_in ( PG_FUNCTION_ARGS   ) 

Definition at line 3457 of file geo_ops.c.

References ereport, errcode(), errmsg(), ERROR, FALSE, make_bound_box(), POLYGON::npts, offsetof, POLYGON::p, pair_count(), palloc0(), path_decode(), PG_GETARG_CSTRING, PG_RETURN_POLYGON_P, and SET_VARSIZE.

{
    char       *str = PG_GETARG_CSTRING(0);
    POLYGON    *poly;
    int         npts;
    int         size;
    int         isopen;
    char       *s;

    if ((npts = pair_count(str, ',')) <= 0)
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
              errmsg("invalid input syntax for type polygon: \"%s\"", str)));

    size = offsetof(POLYGON, p[0]) +sizeof(poly->p[0]) * npts;
    poly = (POLYGON *) palloc0(size);   /* zero any holes */

    SET_VARSIZE(poly, size);
    poly->npts = npts;

    if ((!path_decode(FALSE, npts, str, &isopen, &s, &(poly->p[0])))
        || (*s != '\0'))
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
              errmsg("invalid input syntax for type polygon: \"%s\"", str)));

    make_bound_box(poly);

    PG_RETURN_POLYGON_P(poly);
}

Datum poly_left ( PG_FUNCTION_ARGS   ) 

Definition at line 3567 of file geo_ops.c.

References POLYGON::boundbox, BOX::high, BOX::low, PG_FREE_IF_COPY, PG_GETARG_POLYGON_P, PG_RETURN_BOOL, and Point::x.

{
    POLYGON    *polya = PG_GETARG_POLYGON_P(0);
    POLYGON    *polyb = PG_GETARG_POLYGON_P(1);
    bool        result;

    result = polya->boundbox.high.x < polyb->boundbox.low.x;

    /*
     * Avoid leaking memory for toasted inputs ... needed for rtree indexes
     */
    PG_FREE_IF_COPY(polya, 0);
    PG_FREE_IF_COPY(polyb, 1);

    PG_RETURN_BOOL(result);
}

Datum poly_npoints ( PG_FUNCTION_ARGS   ) 

Definition at line 4402 of file geo_ops.c.

References POLYGON::npts, PG_GETARG_POLYGON_P, and PG_RETURN_INT32.

Datum poly_out ( PG_FUNCTION_ARGS   ) 
Datum poly_overabove ( PG_FUNCTION_ARGS   ) 

Definition at line 3728 of file geo_ops.c.

References POLYGON::boundbox, BOX::low, PG_FREE_IF_COPY, PG_GETARG_POLYGON_P, PG_RETURN_BOOL, and Point::y.

{
    POLYGON    *polya = PG_GETARG_POLYGON_P(0);
    POLYGON    *polyb = PG_GETARG_POLYGON_P(1);
    bool        result;

    result = polya->boundbox.low.y >= polyb->boundbox.low.y;

    /*
     * Avoid leaking memory for toasted inputs ... needed for rtree indexes
     */
    PG_FREE_IF_COPY(polya, 0);
    PG_FREE_IF_COPY(polyb, 1);

    PG_RETURN_BOOL(result);
}

Datum poly_overbelow ( PG_FUNCTION_ARGS   ) 

Definition at line 3682 of file geo_ops.c.

References POLYGON::boundbox, BOX::high, PG_FREE_IF_COPY, PG_GETARG_POLYGON_P, PG_RETURN_BOOL, and Point::y.

{
    POLYGON    *polya = PG_GETARG_POLYGON_P(0);
    POLYGON    *polyb = PG_GETARG_POLYGON_P(1);
    bool        result;

    result = polya->boundbox.high.y <= polyb->boundbox.high.y;

    /*
     * Avoid leaking memory for toasted inputs ... needed for rtree indexes
     */
    PG_FREE_IF_COPY(polya, 0);
    PG_FREE_IF_COPY(polyb, 1);

    PG_RETURN_BOOL(result);
}

Datum poly_overlap ( PG_FUNCTION_ARGS   ) 

Definition at line 3778 of file geo_ops.c.

References POLYGON::boundbox, box_ov(), lseg_intersect_internal(), POLYGON::npts, POLYGON::p, LSEG::p, PG_FREE_IF_COPY, PG_GETARG_POLYGON_P, PG_RETURN_BOOL, and point_inside().

{
    POLYGON    *polya = PG_GETARG_POLYGON_P(0);
    POLYGON    *polyb = PG_GETARG_POLYGON_P(1);
    bool        result;

    /* Quick check by bounding box */
    result = (polya->npts > 0 && polyb->npts > 0 &&
              box_ov(&polya->boundbox, &polyb->boundbox)) ? true : false;

    /*
     * Brute-force algorithm - try to find intersected edges, if so then
     * polygons are overlapped else check is one polygon inside other or not
     * by testing single point of them.
     */
    if (result)
    {
        int         ia,
                    ib;
        LSEG        sa,
                    sb;

        /* Init first of polya's edge with last point */
        sa.p[0] = polya->p[polya->npts - 1];
        result = false;

        for (ia = 0; ia < polya->npts && result == false; ia++)
        {
            /* Second point of polya's edge is a current one */
            sa.p[1] = polya->p[ia];

            /* Init first of polyb's edge with last point */
            sb.p[0] = polyb->p[polyb->npts - 1];

            for (ib = 0; ib < polyb->npts && result == false; ib++)
            {
                sb.p[1] = polyb->p[ib];
                result = lseg_intersect_internal(&sa, &sb);
                sb.p[0] = sb.p[1];
            }

            /*
             * move current endpoint to the first point of next edge
             */
            sa.p[0] = sa.p[1];
        }

        if (result == false)
        {
            result = (point_inside(polya->p, polyb->npts, polyb->p)
                      ||
                      point_inside(polyb->p, polya->npts, polya->p));
        }
    }

    /*
     * Avoid leaking memory for toasted inputs ... needed for rtree indexes
     */
    PG_FREE_IF_COPY(polya, 0);
    PG_FREE_IF_COPY(polyb, 1);

    PG_RETURN_BOOL(result);
}

Datum poly_overleft ( PG_FUNCTION_ARGS   ) 

Definition at line 3590 of file geo_ops.c.

References POLYGON::boundbox, BOX::high, PG_FREE_IF_COPY, PG_GETARG_POLYGON_P, PG_RETURN_BOOL, and Point::x.

{
    POLYGON    *polya = PG_GETARG_POLYGON_P(0);
    POLYGON    *polyb = PG_GETARG_POLYGON_P(1);
    bool        result;

    result = polya->boundbox.high.x <= polyb->boundbox.high.x;

    /*
     * Avoid leaking memory for toasted inputs ... needed for rtree indexes
     */
    PG_FREE_IF_COPY(polya, 0);
    PG_FREE_IF_COPY(polyb, 1);

    PG_RETURN_BOOL(result);
}

Datum poly_overright ( PG_FUNCTION_ARGS   ) 

Definition at line 3636 of file geo_ops.c.

References POLYGON::boundbox, BOX::low, PG_FREE_IF_COPY, PG_GETARG_POLYGON_P, PG_RETURN_BOOL, and Point::x.

{
    POLYGON    *polya = PG_GETARG_POLYGON_P(0);
    POLYGON    *polyb = PG_GETARG_POLYGON_P(1);
    bool        result;

    result = polya->boundbox.low.x >= polyb->boundbox.low.x;

    /*
     * Avoid leaking memory for toasted inputs ... needed for rtree indexes
     */
    PG_FREE_IF_COPY(polya, 0);
    PG_FREE_IF_COPY(polyb, 1);

    PG_RETURN_BOOL(result);
}

Datum poly_path ( PG_FUNCTION_ARGS   ) 

Definition at line 4475 of file geo_ops.c.

References PATH::closed, PATH::dummy, i, PATH::npts, POLYGON::npts, offsetof, POLYGON::p, PATH::p, palloc(), PG_GETARG_POLYGON_P, PG_RETURN_PATH_P, SET_VARSIZE, Point::x, and Point::y.

{
    POLYGON    *poly = PG_GETARG_POLYGON_P(0);
    PATH       *path;
    int         size;
    int         i;

    size = offsetof(PATH, p[0]) +sizeof(path->p[0]) * poly->npts;
    path = (PATH *) palloc(size);

    SET_VARSIZE(path, size);
    path->npts = poly->npts;
    path->closed = TRUE;
    /* prevent instability in unused pad bytes */
    path->dummy = 0;

    for (i = 0; i < poly->npts; i++)
    {
        path->p[i].x = poly->p[i].x;
        path->p[i].y = poly->p[i].y;
    }

    PG_RETURN_PATH_P(path);
}

Datum poly_recv ( PG_FUNCTION_ARGS   ) 

Definition at line 3509 of file geo_ops.c.

References buf, ereport, errcode(), errmsg(), ERROR, i, make_bound_box(), POLYGON::npts, offsetof, POLYGON::p, palloc0(), PG_GETARG_POINTER, PG_RETURN_POLYGON_P, pq_getmsgfloat8(), pq_getmsgint(), SET_VARSIZE, Point::x, and Point::y.

{
    StringInfo  buf = (StringInfo) PG_GETARG_POINTER(0);
    POLYGON    *poly;
    int32       npts;
    int32       i;
    int         size;

    npts = pq_getmsgint(buf, sizeof(int32));
    if (npts <= 0 || npts >= (int32) ((INT_MAX - offsetof(POLYGON, p[0])) / sizeof(Point)))
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
          errmsg("invalid number of points in external \"polygon\" value")));

    size = offsetof(POLYGON, p[0]) +sizeof(poly->p[0]) * npts;
    poly = (POLYGON *) palloc0(size);   /* zero any holes */

    SET_VARSIZE(poly, size);
    poly->npts = npts;

    for (i = 0; i < npts; i++)
    {
        poly->p[i].x = pq_getmsgfloat8(buf);
        poly->p[i].y = pq_getmsgfloat8(buf);
    }

    make_bound_box(poly);

    PG_RETURN_POLYGON_P(poly);
}

Datum poly_right ( PG_FUNCTION_ARGS   ) 

Definition at line 3613 of file geo_ops.c.

References POLYGON::boundbox, BOX::high, BOX::low, PG_FREE_IF_COPY, PG_GETARG_POLYGON_P, PG_RETURN_BOOL, and Point::x.

{
    POLYGON    *polya = PG_GETARG_POLYGON_P(0);
    POLYGON    *polyb = PG_GETARG_POLYGON_P(1);
    bool        result;

    result = polya->boundbox.low.x > polyb->boundbox.high.x;

    /*
     * Avoid leaking memory for toasted inputs ... needed for rtree indexes
     */
    PG_FREE_IF_COPY(polya, 0);
    PG_FREE_IF_COPY(polyb, 1);

    PG_RETURN_BOOL(result);
}

Datum poly_same ( PG_FUNCTION_ARGS   ) 

Definition at line 3754 of file geo_ops.c.

References POLYGON::npts, POLYGON::p, PG_FREE_IF_COPY, PG_GETARG_POLYGON_P, PG_RETURN_BOOL, and plist_same().

{
    POLYGON    *polya = PG_GETARG_POLYGON_P(0);
    POLYGON    *polyb = PG_GETARG_POLYGON_P(1);
    bool        result;

    if (polya->npts != polyb->npts)
        result = false;
    else
        result = plist_same(polya->npts, polya->p, polyb->p);

    /*
     * Avoid leaking memory for toasted inputs ... needed for rtree indexes
     */
    PG_FREE_IF_COPY(polya, 0);
    PG_FREE_IF_COPY(polyb, 1);

    PG_RETURN_BOOL(result);
}

Datum poly_send ( PG_FUNCTION_ARGS   ) 

Definition at line 3544 of file geo_ops.c.

References buf, i, POLYGON::npts, POLYGON::p, PG_GETARG_POLYGON_P, PG_RETURN_BYTEA_P, pq_begintypsend(), pq_endtypsend(), pq_sendfloat8(), pq_sendint(), Point::x, and Point::y.

{
    POLYGON    *poly = PG_GETARG_POLYGON_P(0);
    StringInfoData buf;
    int32       i;

    pq_begintypsend(&buf);
    pq_sendint(&buf, poly->npts, sizeof(int32));
    for (i = 0; i < poly->npts; i++)
    {
        pq_sendfloat8(&buf, poly->p[i].x);
        pq_sendfloat8(&buf, poly->p[i].y);
    }
    PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}

Datum pt_contained_circle ( PG_FUNCTION_ARGS   ) 

Definition at line 5026 of file geo_ops.c.

References CIRCLE::center, PG_GETARG_CIRCLE_P, PG_GETARG_POINT_P, PG_RETURN_BOOL, and point_dt().

{
    Point      *point = PG_GETARG_POINT_P(0);
    CIRCLE     *circle = PG_GETARG_CIRCLE_P(1);
    double      d;

    d = point_dt(&circle->center, point);
    PG_RETURN_BOOL(d <= circle->radius);
}

Datum pt_contained_poly ( PG_FUNCTION_ARGS   ) 
static int single_decode ( char *  str,
float8 x,
char **  ss 
) [static]

Definition at line 118 of file geo_ops.c.

References NULL, and PointerIsValid.

Referenced by circle_in().

{
    char       *cp;

    if (!PointerIsValid(str))
        return FALSE;

    while (isspace((unsigned char) *str))
        str++;
    *x = strtod(str, &cp);
#ifdef GEODEBUG
    printf("single_decode- (%x) try decoding %s to %g\n", (cp - str), str, *x);
#endif
    if (cp <= str)
        return FALSE;
    while (isspace((unsigned char) *cp))
        cp++;

    if (s != NULL)
        *s = cp;

    return TRUE;
}   /* single_decode() */

static int single_encode ( float8  x,
char *  str 
) [static]

Definition at line 143 of file geo_ops.c.

References extra_float_digits.

Referenced by circle_out().

{
    int         ndig = DBL_DIG + extra_float_digits;

    if (ndig < 1)
        ndig = 1;

    sprintf(str, "%.*g", ndig, x);
    return TRUE;
}   /* single_encode() */

static void statlseg_construct ( LSEG lseg,
Point pt1,
Point pt2 
) [static]

Definition at line 2122 of file geo_ops.c.

References LSEG::m, LSEG::p, point_sl(), Point::x, and Point::y.

Referenced by box_diagonal(), close_pb(), close_sb(), dist_ppath(), inter_lb(), inter_sb(), path_distance(), and path_inter().

{
    lseg->p[0].x = pt1->x;
    lseg->p[0].y = pt1->y;
    lseg->p[1].x = pt2->x;
    lseg->p[1].y = pt2->y;

#ifdef NOT_USED
    lseg->m = point_sl(pt1, pt2);
#endif
}

static bool touched_lseg_inside_poly ( Point a,
Point b,
LSEG s,
POLYGON poly,
int  start 
) [static]

Definition at line 3854 of file geo_ops.c.

References lseg_inside_poly(), on_ps_internal(), LSEG::p, and POINTEQ.

Referenced by lseg_inside_poly().

{
    /* point a is on s, b is not */
    LSEG        t;

    t.p[0] = *a;
    t.p[1] = *b;

#define POINTEQ(pt1, pt2)   (FPeq((pt1)->x, (pt2)->x) && FPeq((pt1)->y, (pt2)->y))
    if (POINTEQ(a, s->p))
    {
        if (on_ps_internal(s->p + 1, &t))
            return lseg_inside_poly(b, s->p + 1, poly, start);
    }
    else if (POINTEQ(a, s->p + 1))
    {
        if (on_ps_internal(s->p, &t))
            return lseg_inside_poly(b, s->p, poly, start);
    }
    else if (on_ps_internal(s->p, &t))
    {
        return lseg_inside_poly(b, s->p, poly, start);
    }
    else if (on_ps_internal(s->p + 1, &t))
    {
        return lseg_inside_poly(b, s->p + 1, poly, start);
    }

    return true;                /* may be not true, but that will check later */
}