PostgreSQL Source Code git master
Loading...
Searching...
No Matches
spgproc.c File Reference
#include "postgres.h"
#include <math.h>
#include "access/spgist_private.h"
#include "utils/float.h"
#include "utils/fmgrprotos.h"
#include "utils/geo_decls.h"
Include dependency graph for spgproc.c:

Go to the source code of this file.

Macros

#define point_point_distance(p1, p2)
 

Functions

static double point_box_distance (Point *point, BOX *box)
 
doublespg_key_orderbys_distances (Datum key, bool isLeaf, ScanKey orderbys, int norderbys)
 
BOXbox_copy (BOX *orig)
 

Macro Definition Documentation

◆ point_point_distance

#define point_point_distance (   p1,
  p2 
)
Value:
#define DirectFunctionCall2(func, arg1, arg2)
Definition fmgr.h:686
static Datum PointPGetDatum(const Point *X)
Definition geo_decls.h:180
Datum point_distance(PG_FUNCTION_ARGS)
Definition geo_ops.c:1993
static float8 DatumGetFloat8(Datum X)
Definition postgres.h:495
static int fb(int x)

Definition at line 25 of file spgproc.c.

31{
32 double dx,
33 dy;
34
35 if (isnan(point->x) || isnan(box->low.x) ||
36 isnan(point->y) || isnan(box->low.y))
37 return get_float8_nan();
38
39 if (point->x < box->low.x)
40 dx = box->low.x - point->x;
41 else if (point->x > box->high.x)
42 dx = point->x - box->high.x;
43 else
44 dx = 0.0;
45
46 if (point->y < box->low.y)
47 dy = box->low.y - point->y;
48 else if (point->y > box->high.y)
49 dy = point->y - box->high.y;
50 else
51 dy = 0.0;
52
53 return hypot(dx, dy);
54}
55
56/*
57 * Returns distances from given key to array of ordering scan keys. Leaf key
58 * is expected to be point, non-leaf key is expected to be box. Scan key
59 * arguments are expected to be points.
60 */
61double *
62spg_key_orderbys_distances(Datum key, bool isLeaf,
63 ScanKey orderbys, int norderbys)
64{
65 int sk_num;
66 double *distances = palloc_array(double, norderbys),
67 *distance = distances;
68
69 for (sk_num = 0; sk_num < norderbys; ++sk_num, ++orderbys, ++distance)
70 {
71 Point *point = DatumGetPointP(orderbys->sk_argument);
72
73 *distance = isLeaf ? point_point_distance(point, DatumGetPointP(key))
75 }
76
77 return distances;
78}
79
80BOX *
82{
83 BOX *result = palloc_object(BOX);
84
85 *result = *orig;
86 return result;
87}
#define palloc_object(type)
Definition fe_memutils.h:74
#define palloc_array(type, count)
Definition fe_memutils.h:76
static float8 get_float8_nan(void)
Definition float.h:84
static Point * DatumGetPointP(Datum X)
Definition geo_decls.h:175
static BOX * DatumGetBoxP(Datum X)
Definition geo_decls.h:233
uint64_t Datum
Definition postgres.h:70
BOX * box_copy(BOX *orig)
Definition spgproc.c:82
#define point_point_distance(p1, p2)
Definition spgproc.c:25
double * spg_key_orderbys_distances(Datum key, bool isLeaf, ScanKey orderbys, int norderbys)
Definition spgproc.c:63
static double point_box_distance(Point *point, BOX *box)
Definition spgproc.c:31

Function Documentation

◆ box_copy()

BOX * box_copy ( BOX orig)

Definition at line 82 of file spgproc.c.

83{
84 BOX *result = palloc_object(BOX);
85
86 *result = *orig;
87 return result;
88}

References fb(), and palloc_object.

Referenced by spg_kd_inner_consistent(), and spg_quad_inner_consistent().

◆ point_box_distance()

static double point_box_distance ( Point point,
BOX box 
)
static

Definition at line 31 of file spgproc.c.

32{
33 double dx,
34 dy;
35
36 if (isnan(point->x) || isnan(box->low.x) ||
37 isnan(point->y) || isnan(box->low.y))
38 return get_float8_nan();
39
40 if (point->x < box->low.x)
41 dx = box->low.x - point->x;
42 else if (point->x > box->high.x)
43 dx = point->x - box->high.x;
44 else
45 dx = 0.0;
46
47 if (point->y < box->low.y)
48 dy = box->low.y - point->y;
49 else if (point->y > box->high.y)
50 dy = point->y - box->high.y;
51 else
52 dy = 0.0;
53
54 return hypot(dx, dy);
55}

References fb(), and get_float8_nan().

Referenced by spg_key_orderbys_distances().

◆ spg_key_orderbys_distances()

double * spg_key_orderbys_distances ( Datum  key,
bool  isLeaf,
ScanKey  orderbys,
int  norderbys 
)

Definition at line 63 of file spgproc.c.

65{
66 int sk_num;
67 double *distances = palloc_array(double, norderbys),
68 *distance = distances;
69
70 for (sk_num = 0; sk_num < norderbys; ++sk_num, ++orderbys, ++distance)
71 {
72 Point *point = DatumGetPointP(orderbys->sk_argument);
73
74 *distance = isLeaf ? point_point_distance(point, DatumGetPointP(key))
76 }
77
78 return distances;
79}

References DatumGetBoxP(), DatumGetPointP(), fb(), palloc_array, point_box_distance(), point_point_distance, and ScanKeyData::sk_argument.

Referenced by spg_box_quad_leaf_consistent(), spg_kd_inner_consistent(), spg_quad_inner_consistent(), and spg_quad_leaf_consistent().