]> granicus.if.org Git - postgis/commitdiff
Added box3d_to_box2df_p to reduce number of required memcpys
authorSandro Santilli <strk@keybit.net>
Thu, 16 Sep 2004 10:42:08 +0000 (10:42 +0000)
committerSandro Santilli <strk@keybit.net>
Thu, 16 Sep 2004 10:42:08 +0000 (10:42 +0000)
git-svn-id: http://svn.osgeo.org/postgis/trunk@827 b70326c6-7e19-0410-871a-916f4a2858ee

lwgeom/lwgeom.h
lwgeom/lwgeom_api.c

index 5ca6b387618e1c1d73638a4a437a5ddd37cef108..a45784a47b1bbc360f5cabaae26f9eb7d97d8728 100644 (file)
@@ -527,6 +527,7 @@ extern BOX3D *lw_geom_getBB_inspected(LWGEOM_INSPECTED *inspected);
 // between float8 and float4 representations.
 
 extern BOX2DFLOAT4 *box3d_to_box2df(BOX3D *box);
+extern int box3d_to_box2df_p(BOX3D *box, BOX2DFLOAT4 *res);
 extern BOX2DFLOAT4 *box_to_box2df(BOX *box);  // postgresql standard type
 
 extern BOX3D box2df_to_box3d(BOX2DFLOAT4 *box);
index e134c9ce643c1d87792a66dd747c5ec2014f2c00..62530d469a3245129ac0a5a2861951ccfdc65623 100644 (file)
@@ -181,7 +181,7 @@ BOX2DFLOAT4 *box3d_to_box2df(BOX3D *box)
 
        if (box == NULL)
        {
-               elog(NOTICE, "box3d_to_box2df got NUL box");
+               elog(ERROR, "box3d_to_box2df got NUL box");
                return result;
        }
 
@@ -194,6 +194,26 @@ BOX2DFLOAT4 *box3d_to_box2df(BOX3D *box)
        return result;
 }
 
+// Convert BOX3D to BOX2D using pre-allocated BOX2D
+// returned box2d is allocated with 'palloc'
+// return 0 on error (NULL input box)
+int box3d_to_box2df_p(BOX3D *box, BOX2DFLOAT4 *result)
+{
+       if (box == NULL)
+       {
+               elog(NOTICE, "box3d_to_box2df got NUL box");
+               return 0;
+       }
+
+       result->xmin = nextDown_f(box->xmin);
+       result->ymin = nextDown_f(box->ymin);
+
+       result->xmax = nextUp_f(box->xmax);
+       result->ymax = nextUp_f(box->ymax);
+
+       return 1;
+}
+
 
 //convert postgresql BOX to BOX2D
 BOX2DFLOAT4 *box_to_box2df(BOX *box)
@@ -348,7 +368,6 @@ BOX2DFLOAT4 getbox2d(char *serialized_form)
        char *loc;
        BOX2DFLOAT4 result;
        BOX3D *box3d;
-       BOX2DFLOAT4 *box;
 
        loc = serialized_form+1;
 
@@ -366,11 +385,19 @@ BOX2DFLOAT4 getbox2d(char *serialized_form)
 //elog(NOTICE,"getbox2d -- computing bbox");
        box3d = lw_geom_getBB_simple(serialized_form);
 //elog(NOTICE,"lw_geom_getBB_simple got bbox3d(%.15g %.15g,%.15g %.15g)",box3d->xmin,box3d->ymin,box3d->xmax,box3d->ymax);
-       box = box3d_to_box2df(box3d);
+
+       if ( ! box3d_to_box2df_p(box3d, &result) )
+       {
+               elog(ERROR, "Error converting box3d to box2df");
+       }
+
+       //box = box3d_to_box2df(box3d);
 //elog(NOTICE,"box3d made box2d(%.15g %.15g,%.15g %.15g)",box->xmin,box->ymin,box->xmax,box->ymax);
-       memcpy(&result,box, sizeof(BOX2DFLOAT4));
+       //memcpy(&result,box, sizeof(BOX2DFLOAT4));
+       //pfree(box);
+
        pfree(box3d);
-       pfree(box);
+
        return result;
 }
 
@@ -381,7 +408,6 @@ getbox2d_p(char *serialized_form, BOX2DFLOAT4 *box)
        unsigned char type = (unsigned char) serialized_form[0];
        char *loc;
        BOX3D *box3d;
-       BOX2DFLOAT4 *box2;
 
        loc = serialized_form+1;
 
@@ -400,11 +426,18 @@ getbox2d_p(char *serialized_form, BOX2DFLOAT4 *box)
        {
                return 0;
        }
-       box2 = box3d_to_box2df(box3d);
 
-       memcpy(box,box2, sizeof(BOX2DFLOAT4));
+       if ( ! box3d_to_box2df_p(box3d, box) )
+       {
+               return 0;
+       }
+
+       //box2 = box3d_to_box2df(box3d);
+       //memcpy(box,box2, sizeof(BOX2DFLOAT4));
+       //pfree(box2);
+
        pfree(box3d);
-       pfree(box2);
+
        return 1;
 }