]> granicus.if.org Git - postgis/commitdiff
Centralize gbox calculations in liblwgeom
authorPaul Ramsey <pramsey@cleverelephant.ca>
Thu, 5 Mar 2015 19:07:08 +0000 (19:07 +0000)
committerPaul Ramsey <pramsey@cleverelephant.ca>
Thu, 5 Mar 2015 19:07:08 +0000 (19:07 +0000)
git-svn-id: http://svn.osgeo.org/postgis/trunk@13313 b70326c6-7e19-0410-871a-916f4a2858ee

liblwgeom/g_box.c
liblwgeom/liblwgeom.h.in
postgis/lwgeom_geos.c

index d095950c04969401ce20eb81ccbe76b63fa3df88..8604545c9547ced8c60d6e406c6760b737deceac 100644 (file)
@@ -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.
index 4fc474b6b877303444b3e3584fe64075a154fc60..cf7c52afe9234898a21060a92430afc7d2468159 100644 (file)
@@ -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.
 */
index 34092d2baf4b34659f56106f57b159187098547b..7d16dffc541394a1023aabab0f02460c2188efef 100644 (file)
@@ -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);
                }