Changeset 8240 for spike

Show
Ignore:
Timestamp:
11/24/11 14:04:15 (6 months ago)
Author:
pramsey
Message:

Double box experiment complete

Location:
spike/pramsey/doublebox
Files:
15 modified

Legend:

Unmodified
Added
Removed
  • spike/pramsey/doublebox/liblwgeom/cunit/cu_geodetic.c

    r8029 r8240  
    582582        type = gserialized_get_type(g); 
    583583        CU_ASSERT_EQUAL( type, POLYGONTYPE ); 
    584         inspect = (double*)g; 
    585         CU_ASSERT_EQUAL(inspect[9], 2.5); 
     584        /* data + box size + type + nrings + npoints1 + npoints2 */ 
     585        inspect = (double*)(g->data + gbox_serialized_size(g->flags) + 4 + 4 + 4 + 4); 
     586        CU_ASSERT_EQUAL(inspect[3], 2.5); 
    586587        lwgeom_free(geom); 
    587588        lwfree(g); 
     
    592593        type = gserialized_get_type(g); 
    593594        CU_ASSERT_EQUAL( type, MULTILINETYPE ); 
    594         inspect = (double*)g; 
    595         CU_ASSERT_EQUAL(inspect[12], 0.1); 
     595        /* data + box size + type + ngeoms + subtype + npoints + 2 points + type + npoints */ 
     596        inspect = (double*)(g->data + gbox_serialized_size(g->flags) + 4 + 4 + 4 + 4 + 8*4 + 4 + 4); 
     597        CU_ASSERT_EQUAL(inspect[1], 0.1); 
    596598        lwgeom_free(geom); 
    597599        lwfree(g); 
  • spike/pramsey/doublebox/liblwgeom/cunit/cu_libgeom.c

    r8200 r8240  
    172172{ 
    173173        uint8_t flags = gflags(0, 0, 0); 
    174         CU_ASSERT_EQUAL(gbox_serialized_size(flags),16); 
     174        CU_ASSERT_EQUAL(gbox_serialized_size(flags),32); 
    175175        FLAGS_SET_BBOX(flags, 1); 
    176         CU_ASSERT_EQUAL(gbox_serialized_size(flags),16); 
     176        CU_ASSERT_EQUAL(gbox_serialized_size(flags),32); 
    177177        FLAGS_SET_Z(flags, 1); 
    178         CU_ASSERT_EQUAL(gbox_serialized_size(flags),24); 
     178        CU_ASSERT_EQUAL(gbox_serialized_size(flags),48); 
    179179        FLAGS_SET_M(flags, 1); 
    180         CU_ASSERT_EQUAL(gbox_serialized_size(flags),32); 
     180        CU_ASSERT_EQUAL(gbox_serialized_size(flags),64); 
    181181        FLAGS_SET_GEODETIC(flags, 1); 
    182         CU_ASSERT_EQUAL(gbox_serialized_size(flags),24); 
     182        CU_ASSERT_EQUAL(gbox_serialized_size(flags),48); 
    183183 
    184184} 
  • spike/pramsey/doublebox/liblwgeom/g_box.c

    r8053 r8240  
    7878        g->ymin -= d; 
    7979        g->ymax += d; 
    80         if ( FLAGS_GET_Z(g->flags) ) 
     80        if ( FLAGS_GET_GEODETIC(g->flags) ) 
    8181        { 
    8282                g->zmin -= d; 
    8383                g->zmax += d; 
    8484        } 
    85         if ( FLAGS_GET_M(g->flags) ) 
    86         { 
    87                 g->mmin -= d; 
    88                 g->mmax += d; 
     85        else 
     86        { 
     87                if ( FLAGS_GET_Z(g->flags) ) 
     88                { 
     89                        g->zmin -= d; 
     90                        g->zmax += d; 
     91                } 
     92                if ( FLAGS_GET_M(g->flags) ) 
     93                { 
     94                        g->mmin -= d; 
     95                        g->mmax += d; 
     96                } 
    8997        } 
    9098} 
     
    296304{ 
    297305        if ( FLAGS_GET_GEODETIC(flags) ) 
    298                 return 6 * sizeof(float); 
     306                return 6 * sizeof(double); 
    299307        else 
    300                 return 2 * FLAGS_NDIMS(flags) * sizeof(float); 
     308                return 2 * FLAGS_NDIMS(flags) * sizeof(double); 
    301309} 
    302310 
  • spike/pramsey/doublebox/liblwgeom/g_serialized.c

    r8204 r8240  
    156156        { 
    157157                int i = 0; 
    158                 float *fbox = (float*)(g->data); 
     158                double *fbox = (double*)(g->data); 
    159159                gbox->xmin = fbox[i++]; 
    160160                gbox->xmax = fbox[i++]; 
     
    202202                                gbox->mmin = gbox->mmax = dptr[i++]; 
    203203                        } 
    204                         gbox_float_round(gbox); 
     204//                      gbox_float_round(gbox); 
    205205                        return LW_SUCCESS; 
    206206                } 
     
    242242                                gbox->zmax = FP_MAX(dptr[i], dptr[i+ndims]); 
    243243                        } 
    244                         gbox_float_round(gbox); 
     244//                      gbox_float_round(gbox); 
    245245                        return LW_SUCCESS; 
    246246                } 
     
    251251                        return LW_FAILURE; 
    252252                } 
     253                /* We could also do single-entry two-point multi-linestrings */ 
     254                else if ( type == MULTILINETYPE ) 
     255                { 
     256                        /* TODO: Make this actually happen */ 
     257                        return LW_FAILURE; 
     258                } 
    253259        } 
    254260        return LW_FAILURE; 
     
    268274                lwgeom = lwgeom_from_gserialized(geom); 
    269275                ret = lwgeom_calculate_gbox(lwgeom, box); 
    270                 gbox_float_round(box); 
     276//              gbox_float_round(box); 
    271277                lwgeom_free(lwgeom); 
    272278        } 
     
    716722} 
    717723 
    718 static size_t gserialized_from_gbox(const GBOX *gbox, uint8_t *buf) 
    719 { 
    720         uint8_t *loc = buf; 
    721         float f; 
     724size_t gserialized_from_gbox(const GBOX *gbox, uint8_t *buf) 
     725{ 
     726        double* loc = (double*)buf; 
    722727        size_t return_size; 
    723728 
    724729        assert(buf); 
    725730 
    726         f = next_float_down(gbox->xmin); 
    727         memcpy(loc, &f, sizeof(float)); 
    728         loc += sizeof(float); 
    729  
    730         f = next_float_up(gbox->xmax); 
    731         memcpy(loc, &f, sizeof(float)); 
    732         loc += sizeof(float); 
    733  
    734         f = next_float_down(gbox->ymin); 
    735         memcpy(loc, &f, sizeof(float)); 
    736         loc += sizeof(float); 
    737  
    738         f = next_float_up(gbox->ymax); 
    739         memcpy(loc, &f, sizeof(float)); 
    740         loc += sizeof(float); 
     731        *loc = gbox->xmin; loc++; 
     732        *loc = gbox->xmax; loc++; 
     733        *loc = gbox->ymin; loc++; 
     734        *loc = gbox->ymax; loc++; 
    741735 
    742736        if ( FLAGS_GET_GEODETIC(gbox->flags) ) 
    743737        { 
    744                 f = next_float_down(gbox->zmin); 
    745                 memcpy(loc, &f, sizeof(float)); 
    746                 loc += sizeof(float); 
    747  
    748                 f = next_float_up(gbox->zmax); 
    749                 memcpy(loc, &f, sizeof(float)); 
    750                 loc += sizeof(float); 
    751  
    752                 return_size = (size_t)(loc - buf); 
     738                *loc = gbox->zmin; loc++; 
     739                *loc = gbox->zmax; loc++; 
     740 
     741                return_size = (size_t)(((uint8_t*)loc) - buf); 
    753742                LWDEBUGF(4, "returning size %d", return_size); 
    754743                return return_size; 
     
    757746        if ( FLAGS_GET_Z(gbox->flags) ) 
    758747        { 
    759                 f = next_float_down(gbox->zmin); 
    760                 memcpy(loc, &f, sizeof(float)); 
    761                 loc += sizeof(float); 
    762  
    763                 f = next_float_up(gbox->zmax); 
    764                 memcpy(loc, &f, sizeof(float)); 
    765                 loc += sizeof(float); 
    766  
     748                *loc = gbox->zmin; loc++; 
     749                *loc = gbox->zmax; loc++; 
    767750        } 
    768751 
    769752        if ( FLAGS_GET_M(gbox->flags) ) 
    770753        { 
    771                 f = next_float_down(gbox->mmin); 
    772                 memcpy(loc, &f, sizeof(float)); 
    773                 loc += sizeof(float); 
    774  
    775                 f = next_float_up(gbox->mmax); 
    776                 memcpy(loc, &f, sizeof(float)); 
    777                 loc += sizeof(float); 
    778         } 
    779         return_size = (size_t)(loc - buf); 
     754                *loc = gbox->mmin; loc++; 
     755                *loc = gbox->mmax; loc++; 
     756        } 
     757        return_size = (size_t)(((uint8_t*)loc) - buf); 
    780758        LWDEBUGF(4, "returning size %d", return_size); 
    781759        return return_size; 
     
    796774        ** See if we need a bounding box, add one if we don't have one. 
    797775        */ 
    798         if ( (! geom->bbox) && lwgeom_needs_bbox(geom) && (!lwgeom_is_empty(geom)) ) 
    799         { 
     776        if ( lwgeom_needs_bbox(geom) ) 
    800777                lwgeom_add_bbox(geom); 
    801         } 
     778//      else 
     779//              lwgeom_drop_bbox(geom); 
    802780         
    803781        /* 
  • spike/pramsey/doublebox/liblwgeom/liblwgeom.h.in

    r8229 r8240  
    13841384 
    13851385/** 
     1386* Write a serialized box into a byte buffer and return the size of 
     1387* the segment written. 
     1388*/ 
     1389extern size_t gserialized_from_gbox(const GBOX *gbox, uint8_t *buf); 
     1390 
     1391/** 
    13861392* Return a copy of the input serialized geometry.  
    13871393*/  
  • spike/pramsey/doublebox/liblwgeom/lwgeom.c

    r8046 r8240  
    912912{ 
    913913        assert(geom); 
    914         if ( geom->type == POINTTYPE ) 
     914        if ( geom->type == POINTTYPE || lwgeom_is_empty(geom) ) 
    915915        { 
    916916                return LW_FALSE; 
    917917        } 
     918 
    918919        return LW_TRUE; 
    919920} 
  • spike/pramsey/doublebox/libpgcommon/gserialized_gist.c

    r7786 r8240  
    4949#endif 
    5050 
     51/** 
     52* How many dimensions should a bounding box for this flag set have? 
     53* Geodetic objects always have 3D boxes. Other objects have boxes 
     54* with the same dimensionality as their coordinates. 
     55*/ 
     56static inline int 
     57gbox_ndims(uint8_t flags) 
     58{ 
     59        return FLAGS_GET_GEODETIC(flags) ? 3 : FLAGS_NDIMS(flags); 
     60} 
    5161 
    5262/** 
    5363* Given a #GSERIALIZED datum, as quickly as possible (peaking into the top 
    5464* of the memory) return the gbox extents. Does not deserialize the geometry, 
    55 * but <em>WARNING</em> returns a slightly larger bounding box than actually 
    56 * encompasses the objects. For geography objects returns geocentric bounding 
     65* For geography objects returns geocentric bounding 
    5766* box, for geometry objects returns cartesian bounding box. 
    5867*/ 
     
    6069gserialized_datum_get_gbox_p(Datum gsdatum, GBOX *gbox) 
    6170{ 
    62         char gboxmem[GIDX_MAX_SIZE]; 
    63         GIDX *gidx = (GIDX*)gboxmem; 
     71        GSERIALIZED *gpart; 
     72        int result = LW_SUCCESS; 
     73 
     74        POSTGIS_DEBUG(4, "entered function"); 
     75 
     76        /* 
     77        ** The most info we need is the 8 bytes of serialized header plus the 64 bytes 
     78        ** of floats necessary to hold the 8 floats of the largest XYZM index 
     79        ** bounding box, so 72 bytes. 
     80        */ 
     81        gpart = (GSERIALIZED*)PG_DETOAST_DATUM_SLICE(gsdatum, 0, 72); 
     82 
     83        POSTGIS_DEBUGF(4, "got flags %d", gpart->flags); 
     84 
     85        /* Do we even have a serialized bounding box? */ 
     86        if ( FLAGS_GET_BBOX(gpart->flags) ) 
     87        { 
     88                /* Yes! Copy it out into the GBOX! */ 
     89                double *d = (double*)(gpart->data); 
     90                gbox->flags = gpart->flags; 
     91                gbox->xmin = *d; d++; 
     92                gbox->xmax = *d; d++; 
     93                gbox->ymin = *d; d++; 
     94                gbox->ymax = *d; d++; 
     95                 
     96                if ( FLAGS_GET_GEODETIC(gpart->flags) ) 
     97                { 
     98                        gbox->zmin = *d; d++; 
     99                        gbox->zmax = *d; d++; 
     100                        return LW_SUCCESS; 
     101                } 
     102                if ( FLAGS_GET_Z(gpart->flags) ) 
     103                { 
     104                        gbox->zmin = *d; d++; 
     105                        gbox->zmax = *d; d++; 
     106                } 
     107                if ( FLAGS_GET_M(gpart->flags) ) 
     108                { 
     109                        gbox->mmin = *d; d++; 
     110                        gbox->mmax = *d; d++; 
     111                } 
     112                result = LW_SUCCESS; 
     113                 
     114        } 
     115        else 
     116        { 
     117                /* No, we need to calculate it from the full object. */ 
     118                GSERIALIZED *g = (GSERIALIZED*)PG_DETOAST_DATUM(gsdatum); 
     119                LWGEOM *lwgeom = lwgeom_from_gserialized(g); 
     120                if ( lwgeom_calculate_gbox(lwgeom, gbox) == LW_FAILURE ) 
     121                { 
     122                        POSTGIS_DEBUG(4, "could not calculate bbox, returning failure"); 
     123                        lwgeom_free(lwgeom); 
     124                        return LW_FAILURE; 
     125                } 
     126                lwgeom_free(lwgeom); 
     127                result = LW_SUCCESS; 
     128        } 
    64129         
    65         if( LW_FAILURE == gserialized_datum_get_gidx_p(gsdatum, gidx) ) 
    66                 return LW_FAILURE; 
    67                  
    68         gbox_from_gidx(gidx, gbox); 
    69         return LW_SUCCESS; 
    70 } 
    71  
     130        if ( result == LW_SUCCESS ) 
     131        { 
     132                POSTGIS_DEBUGF(4, "got gbox %s", gbox_to_string(gidx)); 
     133        } 
     134 
     135        return result; 
     136} 
    72137 
    73138/** 
     
    79144* like mismatched dimensions. 
    80145*/ 
    81 GSERIALIZED* gserialized_set_gidx(GSERIALIZED *g, GIDX *gidx) 
    82 { 
    83         int g_ndims = (FLAGS_GET_GEODETIC(g->flags) ? 3 : FLAGS_NDIMS(g->flags)); 
    84         int box_ndims = GIDX_NDIMS(gidx); 
     146GSERIALIZED* gserialized_set_gbox(GSERIALIZED *g, GBOX *gbox) 
     147{ 
     148        int g_ndims = gbox_ndims(g->flags); 
     149        int box_ndims = gbox_ndims(gbox->flags); 
    85150        GSERIALIZED *g_out = NULL; 
    86         size_t box_size = 2 * g_ndims * sizeof(float); 
     151        size_t box_size = gbox_serialized_size(g->flags); 
     152        size_t write_size; 
    87153 
    88154        /* The dimensionality of the inputs has to match or we are SOL. */ 
     
    117183 
    118184        /* Now write the gidx values into the memory segement */ 
    119         memcpy(g_out->data, gidx->c, box_size); 
     185        write_size = gserialized_from_gbox(gbox, (uint8_t*)g_out->data); 
     186 
     187        /* Write length should match memory segment length */ 
     188        if ( write_size != box_size ) 
     189        { 
     190                return NULL; 
     191        } 
    120192 
    121193        return g_out; 
    122194} 
    123  
    124195 
    125196/** 
     
    127198* allocated #GSERIALIZED every time. 
    128199*/ 
    129 GSERIALIZED* gserialized_drop_gidx(GSERIALIZED *g) 
    130 { 
    131         int g_ndims = (FLAGS_GET_GEODETIC(g->flags) ? 3 : FLAGS_NDIMS(g->flags)); 
    132         size_t box_size = 2 * g_ndims * sizeof(float); 
     200GSERIALIZED* gserialized_drop_gbox(GSERIALIZED *g) 
     201{ 
     202        size_t box_size = gbox_serialized_size(g->flags);; 
    133203        size_t g_out_size = VARSIZE(g) - box_size; 
    134204        GSERIALIZED *g_out = palloc(g_out_size); 
     
    158228 
    159229 
    160 /* Convert a double-based GBOX into a float-based GIDX, 
    161    ensuring the float box is larger than the double box */ 
    162 static int gidx_from_gbox_p(GBOX box, GIDX *a) 
    163 { 
    164         int ndims; 
    165  
    166         ndims = (FLAGS_GET_GEODETIC(box.flags) ? 3 : FLAGS_NDIMS(box.flags)); 
     230/** 
     231* Convert a double-based GBOX into a float-based GIDX, 
     232* ensuring the float box is larger than the double box  
     233*/ 
     234static int  
     235gidx_from_gbox(const GBOX *box, GIDX *a) 
     236{ 
     237        int ndims = gbox_ndims(box->flags); 
     238 
    167239        SET_VARSIZE(a, VARHDRSZ + ndims * 2 * sizeof(float)); 
    168240 
    169         GIDX_SET_MIN(a,0,next_float_down(box.xmin)); 
    170         GIDX_SET_MAX(a,0,next_float_up(box.xmax)); 
    171         GIDX_SET_MIN(a,1,next_float_down(box.ymin)); 
    172         GIDX_SET_MAX(a,1,next_float_up(box.ymax)); 
     241        GIDX_SET_MIN(a,0,next_float_down(box->xmin)); 
     242        GIDX_SET_MAX(a,0,next_float_up(box->xmax)); 
     243        GIDX_SET_MIN(a,1,next_float_down(box->ymin)); 
     244        GIDX_SET_MAX(a,1,next_float_up(box->ymax)); 
    173245 
    174246        /* Geodetic indexes are always 3d, geocentric x/y/z */ 
    175         if ( FLAGS_GET_GEODETIC(box.flags) ) 
    176         { 
    177                 GIDX_SET_MIN(a,2,next_float_down(box.zmin)); 
    178                 GIDX_SET_MAX(a,2,next_float_up(box.zmax)); 
     247        if ( FLAGS_GET_GEODETIC(box->flags) ) 
     248        { 
     249                GIDX_SET_MIN(a,2,next_float_down(box->zmin)); 
     250                GIDX_SET_MAX(a,2,next_float_up(box->zmax)); 
    179251        } 
    180252        else 
    181253        { 
    182254                /* Cartesian with Z implies Z is third dimension */ 
    183                 if ( FLAGS_GET_Z(box.flags) ) 
    184                 { 
    185                         GIDX_SET_MIN(a,2,next_float_down(box.zmin)); 
    186                         GIDX_SET_MAX(a,2,next_float_up(box.zmax)); 
    187                         if ( FLAGS_GET_M(box.flags) ) 
     255                if ( FLAGS_GET_Z(box->flags) ) 
     256                { 
     257                        GIDX_SET_MIN(a,2,next_float_down(box->zmin)); 
     258                        GIDX_SET_MAX(a,2,next_float_up(box->zmax)); 
     259                        if ( FLAGS_GET_M(box->flags) ) 
    188260                        { 
    189                                 GIDX_SET_MIN(a,3,next_float_down(box.mmin)); 
    190                                 GIDX_SET_MAX(a,3,next_float_up(box.mmax)); 
     261                                GIDX_SET_MIN(a,3,next_float_down(box->mmin)); 
     262                                GIDX_SET_MAX(a,3,next_float_up(box->mmax)); 
    191263                        } 
    192264                } 
    193265                /* Unless there's no Z, in which case M is third dimension */ 
    194                 else if ( FLAGS_GET_M(box.flags) ) 
    195                 { 
    196                         GIDX_SET_MIN(a,2,next_float_down(box.mmin)); 
    197                         GIDX_SET_MAX(a,2,next_float_up(box.mmax)); 
    198                 } 
    199         } 
    200  
    201         POSTGIS_DEBUGF(5, "converted %s to %s", gbox_to_string(&box), gidx_to_string(a)); 
     266                else if ( FLAGS_GET_M(box->flags) ) 
     267                { 
     268                        GIDX_SET_MIN(a,2,next_float_down(box->mmin)); 
     269                        GIDX_SET_MAX(a,2,next_float_up(box->mmax)); 
     270                } 
     271        } 
     272 
     273        POSTGIS_DEBUGF(5, "converted %s to %s", gbox_to_string(box), gidx_to_string(a)); 
    202274 
    203275        return LW_SUCCESS; 
    204 } 
    205  
    206  
    207 GIDX* gidx_from_gbox(GBOX box) 
    208 { 
    209         int     ndims; 
    210         GIDX *a; 
    211  
    212         ndims = (FLAGS_GET_GEODETIC(box.flags) ? 3 : FLAGS_NDIMS(box.flags)); 
    213         a = gidx_new(ndims); 
    214         gidx_from_gbox_p(box, a); 
    215         return a; 
    216276} 
    217277 
     
    236296gserialized_datum_get_gidx_p(Datum gsdatum, GIDX *gidx) 
    237297{ 
    238         GSERIALIZED *gpart; 
    239         int result = LW_SUCCESS; 
    240  
    241         POSTGIS_DEBUG(4, "entered function"); 
    242  
    243         /* 
    244         ** The most info we need is the 8 bytes of serialized header plus the 32 bytes 
    245         ** of floats necessary to hold the 8 floats of the largest XYZM index 
    246         ** bounding box, so 40 bytes. 
    247         */ 
    248         gpart = (GSERIALIZED*)PG_DETOAST_DATUM_SLICE(gsdatum, 0, 40); 
    249  
    250         POSTGIS_DEBUGF(4, "got flags %d", gpart->flags); 
    251  
    252         /* Do we even have a serialized bounding box? */ 
    253         if ( FLAGS_GET_BBOX(gpart->flags) ) 
    254         { 
    255                 /* Yes! Copy it out into the GIDX! */ 
    256                 const size_t size = gbox_serialized_size(gpart->flags); 
    257                 POSTGIS_DEBUG(4, "copying box out of serialization"); 
    258                 memcpy(gidx->c, gpart->data, size); 
    259                 SET_VARSIZE(gidx, VARHDRSZ + size); 
    260                 result = LW_SUCCESS; 
    261         } 
    262         else 
    263         { 
    264                 /* No, we need to calculate it from the full object. */ 
    265                 GSERIALIZED *g = (GSERIALIZED*)PG_DETOAST_DATUM(gsdatum); 
    266                 LWGEOM *lwgeom = lwgeom_from_gserialized(g); 
    267                 GBOX gbox; 
    268                 if ( lwgeom_calculate_gbox(lwgeom, &gbox) == LW_FAILURE ) 
    269                 { 
    270                         POSTGIS_DEBUG(4, "could not calculate bbox, returning failure"); 
    271                         lwgeom_free(lwgeom); 
    272                         return LW_FAILURE; 
    273                 } 
    274                 lwgeom_free(lwgeom); 
    275                 result = gidx_from_gbox_p(gbox, gidx); 
    276         } 
    277          
    278         if ( result == LW_SUCCESS ) 
    279         { 
    280                 POSTGIS_DEBUGF(4, "got gidx %s", gidx_to_string(gidx)); 
    281         } 
    282  
    283         return result; 
    284 } 
    285  
    286  
    287 /* 
    288 ** Peak into a geography to find the bounding box. If the 
    289 ** box is there, copy it out and return it. If not, calculate the box from the 
    290 ** full geography and return the box based on that. If no box is available, 
    291 ** return LW_FAILURE, otherwise LW_SUCCESS. 
    292 */ 
    293 int gserialized_get_gidx_p(GSERIALIZED *g, GIDX *gidx) 
    294 { 
    295         int result = LW_SUCCESS; 
    296  
    297         POSTGIS_DEBUG(4, "entered function"); 
    298  
    299         POSTGIS_DEBUGF(4, "got flags %d", g->flags); 
    300  
    301         if ( FLAGS_GET_BBOX(g->flags) ) 
    302         { 
    303                 int ndims = FLAGS_NDIMS_BOX(g->flags); 
    304                 const size_t size = 2 * ndims * sizeof(float); 
    305                 POSTGIS_DEBUG(4, "copying box out of serialization"); 
    306                 memcpy(gidx->c, g->data, size); 
    307                 SET_VARSIZE(gidx, VARHDRSZ + size); 
    308         } 
    309         else 
    310         { 
    311                 /* No, we need to calculate it from the full object. */ 
    312                 LWGEOM *lwgeom = lwgeom_from_gserialized(g); 
    313                 GBOX gbox; 
    314                 if ( lwgeom_calculate_gbox(lwgeom, &gbox) == LW_FAILURE ) 
    315                 { 
    316                         POSTGIS_DEBUG(4, "could not calculate bbox, returning failure"); 
    317                         lwgeom_free(lwgeom); 
    318                         return LW_FAILURE; 
    319                 } 
    320                 lwgeom_free(lwgeom); 
    321                 result = gidx_from_gbox_p(gbox, gidx); 
    322         } 
    323         if ( result == LW_SUCCESS ) 
    324         { 
    325                 POSTGIS_DEBUGF(4, "got gidx %s", gidx_to_string(gidx)); 
    326         } 
    327  
    328         return result; 
     298        GBOX gbox; 
     299 
     300        if ( (gserialized_datum_get_gbox_p(gsdatum, &gbox) == LW_SUCCESS) && 
     301         (gidx_from_gbox(&gbox, gidx) == LW_SUCCESS) ) 
     302        { 
     303                return LW_SUCCESS; 
     304        } 
     305        return LW_FAILURE; 
    329306} 
    330307 
  • spike/pramsey/doublebox/libpgcommon/gserialized_gist.h

    r7786 r8240  
    5454/* Convert a gidx to a gbox */ 
    5555void gbox_from_gidx(GIDX *gidx, GBOX *gbox); 
    56 /* Convert a gbox to a new gidx */ 
    57 GIDX* gidx_from_gbox(GBOX box); 
    5856/* Increase the size of a GIDX */ 
    5957void gidx_expand(GIDX *a, float d); 
     
    8886int gserialized_datum_get_gidx_p(Datum gserialized_datum, GIDX *gidx); 
    8987 
    90 /* Pull out the gidx bounding box from an already de-toasted geography */ 
    91 int gserialized_get_gidx_p(GSERIALIZED *g, GIDX *gidx); 
    9288/* Copy a new bounding box into an existing gserialized */ 
    93 GSERIALIZED* gserialized_set_gidx(GSERIALIZED *g, GIDX *gidx); 
     89GSERIALIZED* gserialized_set_gbox(GSERIALIZED *g, GBOX *gbox); 
    9490 
    9591/* Pull out a gbox bounding box as fast as possible. */ 
    9692int gserialized_datum_get_gbox_p(Datum gsdatum, GBOX *gbox); 
    97 /* Given two datums, do they overlap? Computed very fast using embedded boxes. */ 
    98 /* int gserialized_datum_overlaps(Datum gs1, Datum gs2); */ 
     93 
    9994/* Remove the box from a disk serialization */ 
    100 GSERIALIZED* gserialized_drop_gidx(GSERIALIZED *g); 
     95GSERIALIZED* gserialized_drop_gbox(GSERIALIZED *g); 
    10196 
    10297 
  • spike/pramsey/doublebox/libpgcommon/lwgeom_pg.h

    r8067 r8240  
    8787*/ 
    8888 
    89 /** 
    90 * Remove the embedded bounding box  
     89/**  
     90* Remove the box from a disk serialization  
    9191*/ 
    92 GSERIALIZED* gserialized_drop_gidx(GSERIALIZED *g); 
     92GSERIALIZED* gserialized_drop_gbox(GSERIALIZED *g); 
    9393 
    9494/** 
  • spike/pramsey/doublebox/postgis/geography_measurement.c

    r8066 r8240  
    186186        g_out = gserialized_expand(g, distance); 
    187187 
    188         /* If the expansion fails, the return our input */ 
     188        /* If the expansion fails, then return our input */ 
    189189        if ( g_out == NULL ) 
    190190        { 
  • spike/pramsey/doublebox/postgis/gserialized_gist_2d.c

    r7910 r8240  
    203203/* Convert a double-based GBOX into a float-based BOX2DF, 
    204204   ensuring the float box is larger than the double box */ 
    205 static inline int box2df_from_gbox_p(GBOX *box, BOX2DF *a) 
     205static inline int box2df_from_gbox_p(const GBOX *box, BOX2DF *a) 
    206206{ 
    207207        a->xmin = next_float_down(box->xmin); 
     
    479479gserialized_datum_get_box2df_p(Datum gsdatum, BOX2DF *box2df) 
    480480{ 
    481         GSERIALIZED *gpart; 
    482         uint8_t flags; 
    483         int result = LW_SUCCESS; 
    484  
    485         POSTGIS_DEBUG(4, "entered function"); 
    486  
    487         /* 
    488         ** The most info we need is the 8 bytes of serialized header plus the  
    489         ** of floats necessary to hold the bounding box. 
    490         */ 
    491         gpart = (GSERIALIZED*)PG_DETOAST_DATUM_SLICE(gsdatum, 0, 8 + sizeof(BOX2DF)); 
    492         flags = gpart->flags; 
    493  
    494         POSTGIS_DEBUGF(4, "got flags %d", gpart->flags); 
    495  
    496         /* Do we even have a serialized bounding box? */ 
    497         if ( FLAGS_GET_BBOX(flags) ) 
    498         { 
    499                 /* Yes! Copy it out into the box! */ 
    500                 POSTGIS_DEBUG(4, "copying box out of serialization"); 
    501                 memcpy(box2df, gpart->data, sizeof(BOX2DF)); 
    502                 result = LW_SUCCESS; 
    503         } 
    504         else 
    505         { 
    506                 /* No, we need to calculate it from the full object. */ 
    507                 GBOX gbox; 
    508                 GSERIALIZED *g = (GSERIALIZED*)PG_DETOAST_DATUM(gsdatum); 
    509                 LWGEOM *lwgeom = lwgeom_from_gserialized(g); 
    510                 if ( lwgeom_calculate_gbox(lwgeom, &gbox) == LW_FAILURE ) 
    511                 { 
    512                         POSTGIS_DEBUG(4, "could not calculate bbox, returning failure"); 
    513                         lwgeom_free(lwgeom); 
    514                         return LW_FAILURE; 
    515                 } 
    516                 lwgeom_free(lwgeom); 
    517                 result = box2df_from_gbox_p(&gbox, box2df); 
    518         } 
    519          
    520         if ( result == LW_SUCCESS ) 
    521         { 
    522                 POSTGIS_DEBUGF(4, "got box2df %s", box2df_to_string(box2df)); 
    523         } 
    524  
    525         return result; 
    526 } 
     481        GBOX gbox; 
     482 
     483        if ( (gserialized_datum_get_gbox_p(gsdatum, &gbox) == LW_SUCCESS) && 
     484         (box2df_from_gbox_p(&gbox, box2df) == LW_SUCCESS) ) 
     485        { 
     486                return LW_SUCCESS; 
     487        } 
     488        return LW_FAILURE; 
     489}        
    527490 
    528491 
  • spike/pramsey/doublebox/postgis/gserialized_gist_nd.c

    r7786 r8240  
    400400gserialized_expand(GSERIALIZED *g, double distance) 
    401401{ 
    402         char boxmem[GIDX_MAX_SIZE]; 
    403         GIDX *gidx = (GIDX*)boxmem; 
    404         float fdistance = (float)distance; 
     402        GBOX gbox; 
    405403 
    406404        /* Get our bounding box out of the geography, return right away if 
    407405           input is an EMPTY geometry. */ 
    408         if ( gserialized_get_gidx_p(g, gidx) == LW_FAILURE ) 
     406        if ( gserialized_get_gbox_p(g, &gbox) == LW_FAILURE ) 
    409407        { 
    410408                return g; 
    411409        } 
    412410         
    413         gidx_expand(gidx, fdistance); 
    414  
    415         return gserialized_set_gidx(g, gidx); 
     411        gbox_expand(&gbox, distance); 
     412 
     413        return gserialized_set_gbox(g, &gbox); 
    416414} 
    417415 
     
    422420/* 
    423421** '~' and operator function. Based on two serialized return true if 
    424 ** the first is contained by the second. 
     422** the second is contained by the first. 
    425423*/ 
    426424PG_FUNCTION_INFO_V1(gserialized_within); 
  • spike/pramsey/doublebox/postgis/lwgeom_functions_basic.c

    r8065 r8240  
    20442044        /* Pull only a small amount of the tuple, enough to get the type. */ 
    20452045        /* header + srid/flags + bbox? + type number */ 
    2046         size = VARHDRSZ + 8 + 32 + 4;   
     2046        size = VARHDRSZ + 8 + 64 + 4;   
    20472047 
    20482048        geom = (GSERIALIZED*)PG_DETOAST_DATUM_SLICE(PG_GETARG_DATUM(0), 0, size); 
  • spike/pramsey/doublebox/postgis/lwgeom_inout.c

    r8051 r8240  
    390390        lwgeom_add_bbox(lwgeom); 
    391391        result = geometry_serialize(lwgeom); 
     392        lwgeom_free(lwgeom); 
    392393         
    393394        PG_FREE_IF_COPY(geom, 0); 
     
    405406                PG_RETURN_POINTER(geom); 
    406407         
    407         PG_RETURN_POINTER(gserialized_drop_gidx(geom)); 
     408        PG_RETURN_POINTER(gserialized_drop_gbox(geom)); 
    408409} 
    409410 
  • spike/pramsey/doublebox/regress/lwgeom_regress_expected

    r7389 r8240  
    11BOX(0 0.1,11 12) 
    22BOX3D(0 0.1 -55,11 12 12) 
    3 11184 
    4 15824 
    5 20464 
    6 15824 
    7 11184 
     311200 
     415848 
     520496 
     615848 
     711200