From 179a06c7a16b488f8f4768ee20a2a72b8d2817d2 Mon Sep 17 00:00:00 2001 From: Paul Ramsey Date: Thu, 5 Mar 2015 19:07:08 +0000 Subject: [PATCH] Centralize gbox calculations in liblwgeom git-svn-id: http://svn.osgeo.org/postgis/trunk@13313 b70326c6-7e19-0410-871a-916f4a2858ee --- liblwgeom/g_box.c | 11 +++++++++++ liblwgeom/liblwgeom.h.in | 5 +++++ postgis/lwgeom_geos.c | 20 +++++++------------- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/liblwgeom/g_box.c b/liblwgeom/g_box.c index d095950c0..8604545c9 100644 --- a/liblwgeom/g_box.c +++ b/liblwgeom/g_box.c @@ -296,6 +296,17 @@ gbox_overlaps_2d(const GBOX *g1, const GBOX *g2) return LW_TRUE; } +int +gbox_contains_2d(const GBOX *g1, const GBOX *g2) +{ + if ( ( g2->xmin < g1->xmin ) || ( g2->xmax > g1->xmax ) || + ( g2->ymin < g1->ymin ) || ( g2->ymax > g1->ymax ) ) + { + return LW_FALSE; + } + return LW_TRUE; +} + /** * Warning, this function is only good for x/y/z boxes, used * in unit testing of geodetic box generation. diff --git a/liblwgeom/liblwgeom.h.in b/liblwgeom/liblwgeom.h.in index 4fc474b6b..cf7c52afe 100644 --- a/liblwgeom/liblwgeom.h.in +++ b/liblwgeom/liblwgeom.h.in @@ -1748,6 +1748,11 @@ extern int gbox_overlaps(const GBOX *g1, const GBOX *g2); */ extern int gbox_overlaps_2d(const GBOX *g1, const GBOX *g2); +/** +* Return #LW_TRUE if the first #GBOX contains the second on the 2d plane, #LW_FALSE otherwise. +*/ +extern int gbox_contains_2d(const GBOX *g1, const GBOX *g2); + /** * Copy the values of original #GBOX into duplicate. */ diff --git a/postgis/lwgeom_geos.c b/postgis/lwgeom_geos.c index 34092d2ba..7d16dffc5 100644 --- a/postgis/lwgeom_geos.c +++ b/postgis/lwgeom_geos.c @@ -1679,8 +1679,7 @@ Datum ST_ClipByBox2d(PG_FUNCTION_ARGS) bbox2 = (GBOX *)PG_GETARG_POINTER(1); /* If bbox1 outside of bbox2, return empty */ - if ( bbox1->xmin > bbox2->xmax || bbox1->xmax < bbox2->xmin || - bbox1->ymin > bbox2->ymax || bbox1->ymax < bbox2->ymin ) + if ( ! gbox_overlaps_2d(bbox1, bbox2) ) { lwresult = lwgeom_construct_empty(lwgeom1->type, lwgeom1->srid, 0, 0); lwgeom_free(lwgeom1); @@ -1691,8 +1690,7 @@ Datum ST_ClipByBox2d(PG_FUNCTION_ARGS) } /* if bbox1 is covered by bbox2, return lwgeom1 */ - if ( bbox1->xmax <= bbox2->xmax && bbox1->xmin >= bbox2->xmin && - bbox1->ymax <= bbox2->ymax && bbox1->ymin >= bbox2->ymin ) + if ( gbox_contains_2d(box2, box1) ) { lwgeom_free(lwgeom1); PG_RETURN_POINTER(geom1); @@ -2023,7 +2021,7 @@ Datum overlaps(PG_FUNCTION_ARGS) if ( gserialized_get_gbox_p(geom1, &box1) && gserialized_get_gbox_p(geom2, &box2) ) { - if ( gbox_overlaps_2d(&box1, &box2) == LW_FALSE ) + if ( ! gbox_overlaps_2d(&box1, &box2) ) { PG_RETURN_BOOL(FALSE); } @@ -2098,8 +2096,7 @@ Datum contains(PG_FUNCTION_ARGS) if ( gserialized_get_gbox_p(geom1, &box1) && gserialized_get_gbox_p(geom2, &box2) ) { - if ( ( box2.xmin < box1.xmin ) || ( box2.xmax > box1.xmax ) || - ( box2.ymin < box1.ymin ) || ( box2.ymax > box1.ymax ) ) + if ( ! gbox_contains_2d(&box1, &box2) ) { PG_RETURN_BOOL(FALSE); } @@ -2234,8 +2231,7 @@ Datum containsproperly(PG_FUNCTION_ARGS) if ( gserialized_get_gbox_p(geom1, &box1) && gserialized_get_gbox_p(geom2, &box2) ) { - if (( box2.xmin < box1.xmin ) || ( box2.xmax > box1.xmax ) || - ( box2.ymin < box1.ymin ) || ( box2.ymax > box1.ymax )) + if ( ! gbox_contains_2d(&box1, &box2) ) PG_RETURN_BOOL(FALSE); } @@ -2324,8 +2320,7 @@ Datum covers(PG_FUNCTION_ARGS) if ( gserialized_get_gbox_p(geom1, &box1) && gserialized_get_gbox_p(geom2, &box2) ) { - if (( box2.xmin < box1.xmin ) || ( box2.xmax > box1.xmax ) || - ( box2.ymin < box1.ymin ) || ( box2.ymax > box1.ymax )) + if ( ! gbox_contains_2d(&box1, &box2) ) { PG_RETURN_BOOL(FALSE); } @@ -2479,8 +2474,7 @@ Datum coveredby(PG_FUNCTION_ARGS) if ( gserialized_get_gbox_p(geom1, &box1) && gserialized_get_gbox_p(geom2, &box2) ) { - if ( ( box1.xmin < box2.xmin ) || ( box1.xmax > box2.xmax ) || - ( box1.ymin < box2.ymin ) || ( box1.ymax > box2.ymax ) ) + if ( ! gbox_contains_2d(&box2, &box1) ) { PG_RETURN_BOOL(FALSE); } -- 2.50.1