Datum spg_quad_choose(PG_FUNCTION_ARGS) { spgChooseIn *in = (spgChooseIn *) PG_GETARG_POINTER(0); spgChooseOut *out = (spgChooseOut *) PG_GETARG_POINTER(1); Point *inPoint = DatumGetPointP(in->datum), *centroid; if (in->allTheSame) { out->resultType = spgMatchNode; /* nodeN will be set by core */ out->result.matchNode.levelAdd = 0; out->result.matchNode.restDatum = PointPGetDatum(inPoint); PG_RETURN_VOID(); } Assert(in->hasPrefix); centroid = DatumGetPointP(in->prefixDatum); Assert(in->nNodes == 4); out->resultType = spgMatchNode; out->result.matchNode.nodeN = getQuadrant(centroid, inPoint) - 1; out->result.matchNode.levelAdd = 0; out->result.matchNode.restDatum = PointPGetDatum(inPoint); PG_RETURN_VOID(); }
Datum spg_quad_picksplit(PG_FUNCTION_ARGS) { spgPickSplitIn *in = (spgPickSplitIn*)PG_GETARG_POINTER(0); spgPickSplitOut *out = (spgPickSplitOut*)PG_GETARG_POINTER(1); int i; Point *centroid; int nQuadrants[] = {0, 0, 0, 0}; centroid = palloc(sizeof(*centroid)); for(i=0; i<in->nTuples; i++) { Point *p = DatumGetPointP(in->datums[i]); if (i==0) { *centroid = *p; } else { centroid->x += p->x; centroid->y += p->y; } } centroid->x /= (double)in->nTuples; centroid->y /= (double)in->nTuples; out->hasPrefix = true; out->prefixDatum = PointPGetDatum(centroid); out->nNodes = 4; out->nodeDatums = palloc(sizeof(Datum) * 4); for(i=0; i<4; i++) out->nodeDatums[i] = Int16GetDatum((int2)i); out->mapTuplesToNodes = palloc(sizeof(int) * in->nTuples); out->leafTupleDatums = palloc(sizeof(Datum) * in->nTuples); for(i=0; i<in->nTuples; i++) { Point *p = DatumGetPointP(in->datums[i]), *op; int2 quadrant = getQuadrant(centroid, p) - 1; op = palloc(sizeof(*op)); *op = *p; out->leafTupleDatums[ i ] = PointPGetDatum(op); out->mapTuplesToNodes[ i ] = quadrant; nQuadrants[quadrant]++; } for(i=0; i<4; i++) { if (nQuadrants[i] == in->nTuples) elog(ERROR, "Could not support some strange corner cases, will fix in future"); } PG_RETURN_VOID(); }
Datum spg_quad_picksplit(PG_FUNCTION_ARGS) { spgPickSplitIn *in = (spgPickSplitIn *) PG_GETARG_POINTER(0); spgPickSplitOut *out = (spgPickSplitOut *) PG_GETARG_POINTER(1); int i; Point *centroid; #ifdef USE_MEDIAN /* Use the median values of x and y as the centroid point */ Point **sorted; sorted = palloc(sizeof(*sorted) * in->nTuples); for (i = 0; i < in->nTuples; i++) sorted[i] = DatumGetPointP(in->datums[i]); centroid = palloc(sizeof(*centroid)); qsort(sorted, in->nTuples, sizeof(*sorted), x_cmp); centroid->x = sorted[in->nTuples >> 1]->x; qsort(sorted, in->nTuples, sizeof(*sorted), y_cmp); centroid->y = sorted[in->nTuples >> 1]->y; #else /* Use the average values of x and y as the centroid point */ centroid = palloc0(sizeof(*centroid)); for (i = 0; i < in->nTuples; i++) { centroid->x += DatumGetPointP(in->datums[i])->x; centroid->y += DatumGetPointP(in->datums[i])->y; } centroid->x /= in->nTuples; centroid->y /= in->nTuples; #endif out->hasPrefix = true; out->prefixDatum = PointPGetDatum(centroid); out->nNodes = 4; out->nodeLabels = NULL; /* we don't need node labels */ out->mapTuplesToNodes = palloc(sizeof(int) * in->nTuples); out->leafTupleDatums = palloc(sizeof(Datum) * in->nTuples); for (i = 0; i < in->nTuples; i++) { Point *p = DatumGetPointP(in->datums[i]); int quadrant = getQuadrant(centroid, p) - 1; out->leafTupleDatums[i] = PointPGetDatum(p); out->mapTuplesToNodes[i] = quadrant; } PG_RETURN_VOID(); }
Datum pt_in_widget(PG_FUNCTION_ARGS) { Point *point = PG_GETARG_POINT_P(0); WIDGET *widget = (WIDGET *) PG_GETARG_POINTER(1); float8 distance; distance = DatumGetFloat8(DirectFunctionCall2(point_distance, PointPGetDatum(point), PointPGetDatum(&widget->center))); PG_RETURN_BOOL(distance < widget->radius); }
Datum spg_quad_choose(PG_FUNCTION_ARGS) { spgChooseIn *in = (spgChooseIn*)PG_GETARG_POINTER(0); spgChooseOut *out = (spgChooseOut*)PG_GETARG_POINTER(1); Point *inPoint = DatumGetPointP(in->datum), *centroid; Point *d; Assert(in->hasPrefix); centroid = DatumGetPointP(in->prefixDatum); Assert(in->nNodes == 4); d = palloc(sizeof(*d)); *d = *inPoint; out->resultType = spgMatchNode; out->result.matchNode.nodeN = getQuadrant(centroid, inPoint) - 1; out->result.matchNode.levelAdd = 0; out->result.matchNode.restDatum = PointPGetDatum(d); PG_RETURN_VOID(); }
Datum spg_kd_picksplit(PG_FUNCTION_ARGS) { spgPickSplitIn *in = (spgPickSplitIn *) PG_GETARG_POINTER(0); spgPickSplitOut *out = (spgPickSplitOut *) PG_GETARG_POINTER(1); int i; int middle; SortedPoint *sorted; double coord; sorted = palloc(sizeof(*sorted) * in->nTuples); for (i = 0; i < in->nTuples; i++) { sorted[i].p = DatumGetPointP(in->datums[i]); sorted[i].i = i; } qsort(sorted, in->nTuples, sizeof(*sorted), (in->level % 2) ? x_cmp : y_cmp); middle = in->nTuples >> 1; coord = (in->level % 2) ? sorted[middle].p->x : sorted[middle].p->y; out->hasPrefix = true; out->prefixDatum = Float8GetDatum(coord); out->nNodes = 2; out->nodeLabels = NULL; /* we don't need node labels */ out->mapTuplesToNodes = palloc(sizeof(int) * in->nTuples); out->leafTupleDatums = palloc(sizeof(Datum) * in->nTuples); /* * Note: points that have coordinates exactly equal to coord may get * classified into either node, depending on where they happen to fall in * the sorted list. This is okay as long as the inner_consistent function * descends into both sides for such cases. This is better than the * alternative of trying to have an exact boundary, because it keeps the * tree balanced even when we have many instances of the same point value. * So we should never trigger the allTheSame logic. */ for (i = 0; i < in->nTuples; i++) { Point *p = sorted[i].p; int n = sorted[i].i; out->mapTuplesToNodes[n] = (i < middle) ? 0 : 1; out->leafTupleDatums[n] = PointPGetDatum(p); } PG_RETURN_VOID(); }
Datum regress_dist_ptpath(PG_FUNCTION_ARGS) { Point *pt = PG_GETARG_POINT_P(0); PATH *path = PG_GETARG_PATH_P(1); float8 result = 0.0; /* keep compiler quiet */ float8 tmp; int i; LSEG lseg; switch (path->npts) { case 0: PG_RETURN_NULL(); case 1: result = point_dt(pt, &path->p[0]); break; default: /* * the distance from a point to a path is the smallest distance * from the point to any of its constituent segments. */ Assert(path->npts > 1); for (i = 0; i < path->npts - 1; ++i) { regress_lseg_construct(&lseg, &path->p[i], &path->p[i + 1]); tmp = DatumGetFloat8(DirectFunctionCall2(dist_ps, PointPGetDatum(pt), LsegPGetDatum(&lseg))); if (i == 0 || tmp < result) result = tmp; } break; } PG_RETURN_FLOAT8(result); }
Datum spg_kd_choose(PG_FUNCTION_ARGS) { spgChooseIn *in = (spgChooseIn *) PG_GETARG_POINTER(0); spgChooseOut *out = (spgChooseOut *) PG_GETARG_POINTER(1); Point *inPoint = DatumGetPointP(in->datum); double coord; if (in->allTheSame) elog(ERROR, "allTheSame should not occur for k-d trees"); Assert(in->hasPrefix); coord = DatumGetFloat8(in->prefixDatum); Assert(in->nNodes == 2); out->resultType = spgMatchNode; out->result.matchNode.nodeN = (getSide(coord, in->level % 2, inPoint) > 0) ? 0 : 1; out->result.matchNode.levelAdd = 1; out->result.matchNode.restDatum = PointPGetDatum(inPoint); PG_RETURN_VOID(); }
Datum gist_point_consistent(PG_FUNCTION_ARGS) { GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0); StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2); bool result; bool *recheck = (bool *) PG_GETARG_POINTER(4); StrategyNumber strategyGroup = strategy / GeoStrategyNumberOffset; switch (strategyGroup) { case PointStrategyNumberGroup: result = gist_point_consistent_internal(strategy % GeoStrategyNumberOffset, GIST_LEAF(entry), DatumGetBoxP(entry->key), PG_GETARG_POINT_P(1)); *recheck = false; break; case BoxStrategyNumberGroup: result = DatumGetBool(DirectFunctionCall5( gist_box_consistent, PointerGetDatum(entry), PG_GETARG_DATUM(1), Int16GetDatum(RTOverlapStrategyNumber), 0, PointerGetDatum(recheck))); break; case PolygonStrategyNumberGroup: { POLYGON *query = PG_GETARG_POLYGON_P(1); result = DatumGetBool(DirectFunctionCall5( gist_poly_consistent, PointerGetDatum(entry), PolygonPGetDatum(query), Int16GetDatum(RTOverlapStrategyNumber), 0, PointerGetDatum(recheck))); if (GIST_LEAF(entry) && result) { /* * We are on leaf page and quick check shows overlapping * of polygon's bounding box and point */ BOX *box = DatumGetBoxP(entry->key); Assert(box->high.x == box->low.x && box->high.y == box->low.y); result = DatumGetBool(DirectFunctionCall2( poly_contain_pt, PolygonPGetDatum(query), PointPGetDatum(&box->high))); *recheck = false; } } break; case CircleStrategyNumberGroup: { CIRCLE *query = PG_GETARG_CIRCLE_P(1); result = DatumGetBool(DirectFunctionCall5( gist_circle_consistent, PointerGetDatum(entry), CirclePGetDatum(query), Int16GetDatum(RTOverlapStrategyNumber), 0, PointerGetDatum(recheck))); if (GIST_LEAF(entry) && result) { /* * We are on leaf page and quick check shows overlapping * of polygon's bounding box and point */ BOX *box = DatumGetBoxP(entry->key); Assert(box->high.x == box->low.x && box->high.y == box->low.y); result = DatumGetBool(DirectFunctionCall2( circle_contain_pt, CirclePGetDatum(query), PointPGetDatum(&box->high))); *recheck = false; } } break; default: result = false; /* silence compiler warning */ elog(ERROR, "unknown strategy number: %d", strategy); } PG_RETURN_BOOL(result); }
Datum gist_point_consistent(PG_FUNCTION_ARGS) { GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0); StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2); bool *recheck = (bool *) PG_GETARG_POINTER(4); bool result; StrategyNumber strategyGroup = strategy / GeoStrategyNumberOffset; switch (strategyGroup) { case PointStrategyNumberGroup: result = gist_point_consistent_internal(strategy % GeoStrategyNumberOffset, GIST_LEAF(entry), DatumGetBoxP(entry->key), PG_GETARG_POINT_P(1)); *recheck = false; break; case BoxStrategyNumberGroup: { /* * The only operator___ in this group is point <@ box (on_pb), so * we needn't examine strategy again. * * For historical reasons, on_pb uses exact rather than fuzzy * comparisons. We could use box_overlap when at an internal * page, but that would lead to possibly visiting child pages * uselessly, because box_overlap uses fuzzy comparisons. * Instead we write a non-fuzzy overlap test. The same code * will also serve for leaf-page tests, since leaf keys have * high == low. */ BOX *query, *key; query = PG_GETARG_BOX_P(1); key = DatumGetBoxP(entry->key); result = (key->high.x >= query->low.x && key->low.x <= query->high.x && key->high.y >= query->low.y && key->low.y <= query->high.y); *recheck = false; } break; case PolygonStrategyNumberGroup: { POLYGON *query = PG_GETARG_POLYGON_P(1); result = DatumGetBool(DirectFunctionCall5( gist_poly_consistent, PointerGetDatum(entry), PolygonPGetDatum(query), Int16GetDatum(RTOverlapStrategyNumber), 0, PointerGetDatum(recheck))); if (GIST_LEAF(entry) && result) { /* * We are on leaf page and quick check shows overlapping * of polygon's bounding box and point */ BOX *box = DatumGetBoxP(entry->key); Assert(box->high.x == box->low.x && box->high.y == box->low.y); result = DatumGetBool(DirectFunctionCall2( poly_contain_pt, PolygonPGetDatum(query), PointPGetDatum(&box->high))); *recheck = false; } } break; case CircleStrategyNumberGroup: { CIRCLE *query = PG_GETARG_CIRCLE_P(1); result = DatumGetBool(DirectFunctionCall5( gist_circle_consistent, PointerGetDatum(entry), CirclePGetDatum(query), Int16GetDatum(RTOverlapStrategyNumber), 0, PointerGetDatum(recheck))); if (GIST_LEAF(entry) && result) { /* * We are on leaf page and quick check shows overlapping * of polygon's bounding box and point */ BOX *box = DatumGetBoxP(entry->key); Assert(box->high.x == box->low.x && box->high.y == box->low.y); result = DatumGetBool(DirectFunctionCall2( circle_contain_pt, CirclePGetDatum(query), PointPGetDatum(&box->high))); *recheck = false; } } break; default: elog(ERROR, "unrecognized strategy number: %d", strategy); result = false; /* keep compiler quiet */ break; } PG_RETURN_BOOL(result); }