]> granicus.if.org Git - postgis/commitdiff
Implement ptarray_clone (shallow) and update clone documentation (doxygen), have...
authorSandro Santilli <strk@keybit.net>
Wed, 6 Jul 2011 14:29:26 +0000 (14:29 +0000)
committerSandro Santilli <strk@keybit.net>
Wed, 6 Jul 2011 14:29:26 +0000 (14:29 +0000)
git-svn-id: http://svn.osgeo.org/postgis/trunk@7608 b70326c6-7e19-0410-871a-916f4a2858ee

liblwgeom/cunit/cu_homogenize.c
liblwgeom/cunit/cu_libgeom.c
liblwgeom/liblwgeom.h
liblwgeom/lwcircstring.c
liblwgeom/lwgeom.c
liblwgeom/lwline.c
liblwgeom/lwpoint.c
liblwgeom/lwpoly.c
liblwgeom/lwtriangle.c
liblwgeom/ptarray.c

index e5c28d7cbc569c9e544e43f9fa2820a1bcb90821..7b19e5f389ba311681aa8e9be68d8f153cc2b5ee 100644 (file)
@@ -33,7 +33,7 @@ static void do_geom_test(char * in, char * out)
        lwfree(tmp);
        lwgeom_free(g);
        /* See http://trac.osgeo.org/postgis/ticket/1104 */
-       lwgeom_release(h);
+       lwgeom_free(h);
 }
 
 
@@ -52,7 +52,7 @@ static void do_coll_test(char * in, char * out)
        lwfree(tmp);
        lwgeom_free(g);
        /* See http://trac.osgeo.org/postgis/ticket/1104 */
-       lwgeom_release(h);
+       lwgeom_free(h);
 }
 
 
index 8ff7619f4348d5603a45fe694d56a05becf09e8d..da38c5b6eb8a640a51fa96e8f1b244370d6adea0 100644 (file)
@@ -520,6 +520,56 @@ static void test_f2d(void)
        CU_ASSERT_DOUBLE_EQUAL(f,d, 0.0000001);
 }
 
+/*
+ * This is a test for memory leaks, can't really test
+ * w/out checking with a leak detector (ie: valgrind)
+ *
+ * See http://trac.osgeo.org/postgis/ticket/1102
+ */
+static void test_lwgeom_clone(void)
+{
+       int i;
+
+       char *ewkt[] =
+       {
+               "POINT(0 0.2)",
+               "LINESTRING(-1 -1,-1 2.5,2 2,2 -1)",
+               "MULTIPOINT(0.9 0.9,0.9 0.9,0.9 0.9,0.9 0.9,0.9 0.9,0.9 0.9)",
+               "SRID=1;MULTILINESTRING((-1 -1,-1 2.5,2 2,2 -1),(-1 -1,-1 2.5,2 2,2 -1),(-1 -1,-1 2.5,2 2,2 -1),(-1 -1,-1 2.5,2 2,2 -1))",
+               "SRID=1;MULTILINESTRING((-1 -1,-1 2.5,2 2,2 -1),(-1 -1,-1 2.5,2 2,2 -1),(-1 -1,-1 2.5,2 2,2 -1),(-1 -1,-1 2.5,2 2,2 -1))",
+               "POLYGON((-1 -1,-1 2.5,2 2,2 -1,-1 -1),(0 0,0 1,1 1,1 0,0 0))",
+               "SRID=4326;POLYGON((-1 -1,-1 2.5,2 2,2 -1,-1 -1),(0 0,0 1,1 1,1 0,0 0))",
+               "SRID=4326;POLYGON((-1 -1,-1 2.5,2 2,2 -1,-1 -1),(0 0,0 1,1 1,1 0,0 0),(-0.5 -0.5,-0.5 -0.4,-0.4 -0.4,-0.4 -0.5,-0.5 -0.5))",
+               "SRID=100000;POLYGON((-1 -1 3,-1 2.5 3,2 2 3,2 -1 3,-1 -1 3),(0 0 3,0 1 3,1 1 3,1 0 3,0 0 3),(-0.5 -0.5 3,-0.5 -0.4 3,-0.4 -0.4 3,-0.4 -0.5 3,-0.5 -0.5 3))",
+               "SRID=4326;MULTIPOLYGON(((-1 -1,-1 2.5,2 2,2 -1,-1 -1),(0 0,0 1,1 1,1 0,0 0),(-0.5 -0.5,-0.5 -0.4,-0.4 -0.4,-0.4 -0.5,-0.5 -0.5)),((-1 -1,-1 2.5,2 2,2 -1,-1 -1),(0 0,0 1,1 1,1 0,0 0),(-0.5 -0.5,-0.5 -0.4,-0.4 -0.4,-0.4 -0.5,-0.5 -0.5)))",
+               "SRID=4326;GEOMETRYCOLLECTION(POINT(0 1),POLYGON((-1 -1,-1 2.5,2 2,2 -1,-1 -1),(0 0,0 1,1 1,1 0,0 0)),MULTIPOLYGON(((-1 -1,-1 2.5,2 2,2 -1,-1 -1),(0 0,0 1,1 1,1 0,0 0),(-0.5 -0.5,-0.5 -0.4,-0.4 -0.4,-0.4 -0.5,-0.5 -0.5))))",
+               "MULTICURVE((5 5 1 3,3 5 2 2,3 3 3 1,0 3 1 1),CIRCULARSTRING(0 0 0 0,0.26794 1 3 -2,0.5857864 1.414213 1 2))",
+               "MULTISURFACE(CURVEPOLYGON(CIRCULARSTRING(-2 0,-1 -1,0 0,1 -1,2 0,0 2,-2 0),(-1 0,0 0.5,1 0,0 1,-1 0)),((7 8,10 10,6 14,4 11,7 8)))",
+               "TIN(((0 0 0,0 0 1,0 1 0,0 0 0)),((0 0 0,0 1 0,1 0 0,0 0 0)),((0 0 0,1 0 0,0 0 1,0 0 0)),((1 0 0,0 1 0,0 0 1,1 0 0)))"
+       };
+
+
+       for ( i = 0; i < (sizeof ewkt/sizeof(char *)); i++ )
+       {
+               LWGEOM *geom, *cloned;
+               char *in_ewkt;
+               char *out_ewkt;
+
+               in_ewkt = ewkt[i];
+               geom = lwgeom_from_ewkt(in_ewkt, PARSER_CHECK_NONE);
+               cloned = lwgeom_clone(geom);
+               out_ewkt = lwgeom_to_ewkt(cloned, PARSER_CHECK_NONE);
+               if (strcmp(in_ewkt, out_ewkt))
+                       fprintf(stderr, "\nExp:   %s\nObt:  %s\n", in_ewkt, out_ewkt);
+               CU_ASSERT_STRING_EQUAL(in_ewkt, out_ewkt);
+               lwfree(out_ewkt);
+               lwgeom_free(cloned);
+               lwgeom_free(geom);
+       }
+
+
+}
+
 
 /*
 ** Used by test harness to register the tests in this file.
@@ -539,6 +589,7 @@ CU_TestInfo libgeom_tests[] =
        PG_TEST(test_lwgeom_free),
        PG_TEST(test_lwgeom_flip_coordinates),
        PG_TEST(test_f2d),
+       PG_TEST(test_lwgeom_clone),
        CU_TEST_INFO_NULL
 };
 CU_SuiteInfo libgeom_suite = {"LibGeom Suite",  NULL,  NULL, libgeom_tests};
index 374e25dd12c9f2fabff454ffbeaccc4ed6b19b1e..d53cc21b2c57fe353d9099cdbf7aed50e246ce84 100644 (file)
@@ -1668,8 +1668,12 @@ char lwcollection_same(const LWCOLLECTION *p1, const LWCOLLECTION *p2);
 
 
 /**
-* Clone an LWGEOM. #POINTARRAY are not copied. #GBOX are copied
-*/
+ * @brief Clone LWGEOM object. Serialized point lists are not copied.
+ *
+ * #GBOX are copied
+ *
+ * @see ptarray_clone 
+ */
 extern LWGEOM *lwgeom_clone(const LWGEOM *lwgeom);
 
 /**
@@ -1687,6 +1691,7 @@ LWPOLY *lwpoly_clone(const LWPOLY *lwgeom);
 LWTRIANGLE *lwtriangle_clone(const LWTRIANGLE *lwgeom);
 LWCOLLECTION *lwcollection_clone(const LWCOLLECTION *lwgeom);
 LWCIRCSTRING *lwcircstring_clone(const LWCIRCSTRING *curve);
+POINTARRAY *ptarray_clone(const POINTARRAY *ptarray);
 BOX2DFLOAT4 *box2d_clone(const BOX2DFLOAT4 *lwgeom);
 POINTARRAY *ptarray_clone_deep(const POINTARRAY *ptarray);
 LWLINE *lwline_clone_deep(const LWLINE *lwgeom);
index 060dceceac4a2fc11b5b03ec186eeb1a3a631ce6..66eae324bef9f066175947c7668b7fd8837cb609 100644 (file)
@@ -590,14 +590,14 @@ void printLWCIRCSTRING(LWCIRCSTRING *curve)
        lwnotice("}");
 }
 
-/* Clone LWCIRCSTRING object.  POINTARRAY is not copied. */
+/* @brief Clone LWCIRCSTRING object. Serialized point lists are not copied.
+ *
+ * @see ptarray_clone 
+ */
 LWCIRCSTRING *
 lwcircstring_clone(const LWCIRCSTRING *g)
 {
-       LWCIRCSTRING *ret = lwalloc(sizeof(LWCIRCSTRING));
-       memcpy(ret, g, sizeof(LWCIRCSTRING));
-       if (g->bbox) ret->bbox = gbox_copy(g->bbox);
-       return ret;
+       return (LWCIRCSTRING *)lwline_clone((LWLINE *)g);
 }
 
 
index 5cee1b25c8f0f70a2df800573be446a6de013faa..6e4bd594e542b8e9e4b946dbefe7de32317da63b 100644 (file)
@@ -602,9 +602,10 @@ lwgeom_release(LWGEOM *lwgeom)
 }
 
 
-/** 
-* Clone an #LWGEOM object. #POINTARRAY are not copied. 
-*/
+/* @brief Clone LWGEOM object. Serialized point lists are not copied.
+ *
+ * @see ptarray_clone 
+ */
 LWGEOM *
 lwgeom_clone(const LWGEOM *lwgeom)
 {
index 4bc70dfc33e72e808888be2acf0fbefdef7054df..772a8a30c7e6263ef02aa5d8732d8e0f815cabdd 100644 (file)
@@ -330,7 +330,10 @@ lwline_compute_box2d_p(const LWLINE *line, BOX2DFLOAT4 *box)
        return ptarray_compute_box2d_p(line->points, box);
 }
 
-/* Clone LWLINE object. POINTARRAY is not copied. */
+/* @brief Clone LWLINE object. Serialized point lists are not copied.
+ *
+ * @see ptarray_clone 
+ */
 LWLINE *
 lwline_clone(const LWLINE *g)
 {
@@ -339,6 +342,9 @@ lwline_clone(const LWLINE *g)
        LWDEBUGF(2, "lwline_clone called with %p", g);
 
        memcpy(ret, g, sizeof(LWLINE));
+
+       ret->points = ptarray_clone(g->points);
+
        if ( g->bbox ) ret->bbox = gbox_copy(g->bbox);
        return ret;
 }
index 5549e0d15d941b4bcc62ca26f827fa670287027a..b52356e17270bb4862027280d72dc3447bc5e320 100644 (file)
@@ -397,7 +397,10 @@ lwpoint_compute_box2d_p(const LWPOINT *point, BOX2DFLOAT4 *box)
        return ptarray_compute_box2d_p(point->point, box);
 }
 
-/* Clone LWPOINT object. POINTARRAY is not copied. */
+/* @brief Clone LWPOINT object. Serialized point lists are not copied.
+ *
+ * @see ptarray_clone 
+ */
 LWPOINT *
 lwpoint_clone(const LWPOINT *g)
 {
@@ -406,6 +409,9 @@ lwpoint_clone(const LWPOINT *g)
        LWDEBUG(2, "lwpoint_clone called");
 
        memcpy(ret, g, sizeof(LWPOINT));
+
+       ret->point = ptarray_clone(g->point);
+
        if ( g->bbox ) ret->bbox = gbox_copy(g->bbox);
        return ret;
 }
index abaebdbb48bad789740a968b76328cd9b371ba85..4d392045c114fe3549eed2bce90fb9fd3ab1d3b8 100644 (file)
@@ -446,14 +446,20 @@ lwpoly_compute_box2d_p(const LWPOLY *poly, BOX2DFLOAT4 *box)
        return 1;
 }
 
-/* Clone LWLINE object. POINTARRAY are not copied, it's ring array is. */
+/* @brief Clone LWLINE object. Serialized point lists are not copied.
+ *
+ * @see ptarray_clone 
+ */
 LWPOLY *
 lwpoly_clone(const LWPOLY *g)
 {
+       int i;
        LWPOLY *ret = lwalloc(sizeof(LWPOLY));
        memcpy(ret, g, sizeof(LWPOLY));
        ret->rings = lwalloc(sizeof(POINTARRAY *)*g->nrings);
-       memcpy(ret->rings, g->rings, sizeof(POINTARRAY *)*g->nrings);
+       for ( i = 0; i < g->nrings; i++ ) {
+               ret->rings[i] = ptarray_clone(g->rings[i]);
+       }
        if ( g->bbox ) ret->bbox = gbox_copy(g->bbox);
        return ret;
 }
index 6e48dc358feeafbc96e934d5ae60d374bc679512..3d8f6fd7f4b43b7611e8bc2efd3f10a7c1c8ce35 100644 (file)
@@ -337,15 +337,15 @@ lwtriangle_compute_box2d_p(const LWTRIANGLE *triangle, BOX2DFLOAT4 *box)
        return ptarray_compute_box2d_p(triangle->points, box);
 }
 
-/* Clone LWTRIANGLE object. POINTARRAY are not copied. */
+/* @brief Clone LWTRIANGLE object. Serialized point lists are not copied.
+ *
+ * @see ptarray_clone 
+ */
 LWTRIANGLE *
 lwtriangle_clone(const LWTRIANGLE *g)
 {
-       LWTRIANGLE *ret = lwalloc(sizeof(LWTRIANGLE));
        LWDEBUGF(2, "lwtriangle_clone called with %p", g);
-       memcpy(ret, g, sizeof(LWTRIANGLE));
-       if ( g->bbox ) ret->bbox = gbox_copy(g->bbox);
-       return ret;
+       return (LWTRIANGLE *)lwline_clone((const LWLINE *)g);
 }
 
 void
index c6df1e3dc18d109eaae3b4e269c90aa9d7f7bf21..18964990fa62b328bf581f1bb634780d026e299b 100644 (file)
@@ -612,7 +612,7 @@ ptarray_merge(POINTARRAY *pa1, POINTARRAY *pa2)
 
 
 /**
- * @brief Clone a pointarray
+ * @brief Deep clone a pointarray (also clones serialized pointlist)
  */
 POINTARRAY *
 ptarray_clone_deep(const POINTARRAY *in)
@@ -635,6 +635,27 @@ ptarray_clone_deep(const POINTARRAY *in)
        return out;
 }
 
+/**
+ * @brief Clone a POINTARRAY object. Serialized pointlist is not copied.
+ */
+POINTARRAY *
+ptarray_clone(const POINTARRAY *in)
+{
+       POINTARRAY *out = lwalloc(sizeof(POINTARRAY));
+
+       LWDEBUG(3, "ptarray_clone_deep called.");
+
+       out->flags = in->flags;
+       out->npoints = in->npoints;
+       out->maxpoints = in->maxpoints;
+
+       FLAGS_SET_READONLY(out->flags, 1);
+
+       out->serialized_pointlist = in->serialized_pointlist;
+
+       return out;
+}
+
 /**
 * Check for ring closure using whatever dimensionality is declared on the 
 * pointarray.