xref: /sqlite-3.40.0/src/test_rtree.c (revision 7617e4a8)
19508daa9Sdan /*
29508daa9Sdan ** 2010 August 28
39508daa9Sdan **
49508daa9Sdan ** The author disclaims copyright to this source code.  In place of
59508daa9Sdan ** a legal notice, here is a blessing:
69508daa9Sdan **
79508daa9Sdan **    May you do good and not evil.
89508daa9Sdan **    May you find forgiveness for yourself and forgive others.
99508daa9Sdan **    May you share freely, never taking more than you give.
109508daa9Sdan **
119508daa9Sdan *************************************************************************
129508daa9Sdan ** Code for testing all sorts of SQLite interfaces. This code
139508daa9Sdan ** is not included in the SQLite library.
149508daa9Sdan */
1518ec96b3Sdan 
16883ad049Sdrh #include "sqlite3.h"
1752b1dbb5Smistachkin #if defined(INCLUDE_SQLITE_TCL_H)
1852b1dbb5Smistachkin #  include "sqlite_tcl.h"
1952b1dbb5Smistachkin #else
2052b1dbb5Smistachkin #  include "tcl.h"
2152b1dbb5Smistachkin #endif
2218ec96b3Sdan 
23bd2aaf9aSshaneh /* Solely for the UNUSED_PARAMETER() macro. */
24bd2aaf9aSshaneh #include "sqliteInt.h"
25bd2aaf9aSshaneh 
26860e332cSdrh #ifdef SQLITE_ENABLE_RTREE
2718ec96b3Sdan /*
2818ec96b3Sdan ** Type used to cache parameter information for the "circle" r-tree geometry
2918ec96b3Sdan ** callback.
3018ec96b3Sdan */
3118ec96b3Sdan typedef struct Circle Circle;
3218ec96b3Sdan struct Circle {
3318ec96b3Sdan   struct Box {
3418ec96b3Sdan     double xmin;
3518ec96b3Sdan     double xmax;
3618ec96b3Sdan     double ymin;
3718ec96b3Sdan     double ymax;
3818ec96b3Sdan   } aBox[2];
3918ec96b3Sdan   double centerx;
4018ec96b3Sdan   double centery;
4118ec96b3Sdan   double radius;
4265e6b0ddSdrh   double mxArea;
4365e6b0ddSdrh   int eScoreType;
4418ec96b3Sdan };
4518ec96b3Sdan 
4618ec96b3Sdan /*
4718ec96b3Sdan ** Destructor function for Circle objects allocated by circle_geom().
4818ec96b3Sdan */
circle_del(void * p)4918ec96b3Sdan static void circle_del(void *p){
5018ec96b3Sdan   sqlite3_free(p);
5118ec96b3Sdan }
5218ec96b3Sdan 
5318ec96b3Sdan /*
5418ec96b3Sdan ** Implementation of "circle" r-tree geometry callback.
5518ec96b3Sdan */
circle_geom(sqlite3_rtree_geometry * p,int nCoord,sqlite3_rtree_dbl * aCoord,int * pRes)5618ec96b3Sdan static int circle_geom(
5718ec96b3Sdan   sqlite3_rtree_geometry *p,
5818ec96b3Sdan   int nCoord,
5965e6b0ddSdrh   sqlite3_rtree_dbl *aCoord,
6018ec96b3Sdan   int *pRes
6118ec96b3Sdan ){
6218ec96b3Sdan   int i;                          /* Iterator variable */
6318ec96b3Sdan   Circle *pCircle;                /* Structure defining circular region */
6418ec96b3Sdan   double xmin, xmax;              /* X dimensions of box being tested */
6518ec96b3Sdan   double ymin, ymax;              /* X dimensions of box being tested */
6618ec96b3Sdan 
6765e6b0ddSdrh   xmin = aCoord[0];
6865e6b0ddSdrh   xmax = aCoord[1];
6965e6b0ddSdrh   ymin = aCoord[2];
7065e6b0ddSdrh   ymax = aCoord[3];
7165e6b0ddSdrh   pCircle = (Circle *)p->pUser;
7265e6b0ddSdrh   if( pCircle==0 ){
7318ec96b3Sdan     /* If pUser is still 0, then the parameter values have not been tested
7418ec96b3Sdan     ** for correctness or stored into a Circle structure yet. Do this now. */
7518ec96b3Sdan 
7618ec96b3Sdan     /* This geometry callback is for use with a 2-dimensional r-tree table.
7718ec96b3Sdan     ** Return an error if the table does not have exactly 2 dimensions. */
7818ec96b3Sdan     if( nCoord!=4 ) return SQLITE_ERROR;
7918ec96b3Sdan 
8018ec96b3Sdan     /* Test that the correct number of parameters (3) have been supplied,
8118ec96b3Sdan     ** and that the parameters are in range (that the radius of the circle
8218ec96b3Sdan     ** radius is greater than zero). */
8318ec96b3Sdan     if( p->nParam!=3 || p->aParam[2]<0.0 ) return SQLITE_ERROR;
8418ec96b3Sdan 
8518ec96b3Sdan     /* Allocate a structure to cache parameter data in. Return SQLITE_NOMEM
8618ec96b3Sdan     ** if the allocation fails. */
8718ec96b3Sdan     pCircle = (Circle *)(p->pUser = sqlite3_malloc(sizeof(Circle)));
8818ec96b3Sdan     if( !pCircle ) return SQLITE_NOMEM;
8918ec96b3Sdan     p->xDelUser = circle_del;
9018ec96b3Sdan 
9118ec96b3Sdan     /* Record the center and radius of the circular region. One way that
9218ec96b3Sdan     ** tested bounding boxes that intersect the circular region are detected
933308b6acSdrh     ** is by testing if each corner of the bounding box lies within radius
9418ec96b3Sdan     ** units of the center of the circle. */
9518ec96b3Sdan     pCircle->centerx = p->aParam[0];
9618ec96b3Sdan     pCircle->centery = p->aParam[1];
9718ec96b3Sdan     pCircle->radius = p->aParam[2];
9818ec96b3Sdan 
9918ec96b3Sdan     /* Define two bounding box regions. The first, aBox[0], extends to
10018ec96b3Sdan     ** infinity in the X dimension. It covers the same range of the Y dimension
10118ec96b3Sdan     ** as the circular region. The second, aBox[1], extends to infinity in
10218ec96b3Sdan     ** the Y dimension and is constrained to the range of the circle in the
10318ec96b3Sdan     ** X dimension.
10418ec96b3Sdan     **
10518ec96b3Sdan     ** Then imagine each box is split in half along its short axis by a line
10618ec96b3Sdan     ** that intersects the center of the circular region. A bounding box
10718ec96b3Sdan     ** being tested can be said to intersect the circular region if it contains
10818ec96b3Sdan     ** points from each half of either of the two infinite bounding boxes.
10918ec96b3Sdan     */
11018ec96b3Sdan     pCircle->aBox[0].xmin = pCircle->centerx;
11118ec96b3Sdan     pCircle->aBox[0].xmax = pCircle->centerx;
11218ec96b3Sdan     pCircle->aBox[0].ymin = pCircle->centery + pCircle->radius;
11318ec96b3Sdan     pCircle->aBox[0].ymax = pCircle->centery - pCircle->radius;
11418ec96b3Sdan     pCircle->aBox[1].xmin = pCircle->centerx + pCircle->radius;
11518ec96b3Sdan     pCircle->aBox[1].xmax = pCircle->centerx - pCircle->radius;
11618ec96b3Sdan     pCircle->aBox[1].ymin = pCircle->centery;
11718ec96b3Sdan     pCircle->aBox[1].ymax = pCircle->centery;
11865e6b0ddSdrh     pCircle->mxArea = (xmax - xmin)*(ymax - ymin) + 1.0;
11918ec96b3Sdan   }
12018ec96b3Sdan 
12118ec96b3Sdan   /* Check if any of the 4 corners of the bounding-box being tested lie
12218ec96b3Sdan   ** inside the circular region. If they do, then the bounding-box does
12318ec96b3Sdan   ** intersect the region of interest. Set the output variable to true and
12418ec96b3Sdan   ** return SQLITE_OK in this case. */
12518ec96b3Sdan   for(i=0; i<4; i++){
12618ec96b3Sdan     double x = (i&0x01) ? xmax : xmin;
12718ec96b3Sdan     double y = (i&0x02) ? ymax : ymin;
12818ec96b3Sdan     double d2;
12918ec96b3Sdan 
13018ec96b3Sdan     d2  = (x-pCircle->centerx)*(x-pCircle->centerx);
13118ec96b3Sdan     d2 += (y-pCircle->centery)*(y-pCircle->centery);
13218ec96b3Sdan     if( d2<(pCircle->radius*pCircle->radius) ){
13318ec96b3Sdan       *pRes = 1;
13418ec96b3Sdan       return SQLITE_OK;
13518ec96b3Sdan     }
13618ec96b3Sdan   }
13718ec96b3Sdan 
13818ec96b3Sdan   /* Check if the bounding box covers any other part of the circular region.
13918ec96b3Sdan   ** See comments above for a description of how this test works. If it does
14018ec96b3Sdan   ** cover part of the circular region, set the output variable to true
14118ec96b3Sdan   ** and return SQLITE_OK. */
14218ec96b3Sdan   for(i=0; i<2; i++){
14318ec96b3Sdan     if( xmin<=pCircle->aBox[i].xmin
14418ec96b3Sdan      && xmax>=pCircle->aBox[i].xmax
14518ec96b3Sdan      && ymin<=pCircle->aBox[i].ymin
14618ec96b3Sdan      && ymax>=pCircle->aBox[i].ymax
14718ec96b3Sdan     ){
14818ec96b3Sdan       *pRes = 1;
14918ec96b3Sdan       return SQLITE_OK;
15018ec96b3Sdan     }
15118ec96b3Sdan   }
15218ec96b3Sdan 
15318ec96b3Sdan   /* The specified bounding box does not intersect the circular region. Set
15418ec96b3Sdan   ** the output variable to zero and return SQLITE_OK. */
15518ec96b3Sdan   *pRes = 0;
15618ec96b3Sdan   return SQLITE_OK;
15718ec96b3Sdan }
15818ec96b3Sdan 
15965e6b0ddSdrh /*
16065e6b0ddSdrh ** Implementation of "circle" r-tree geometry callback using the
16165e6b0ddSdrh ** 2nd-generation interface that allows scoring.
1624f03f413Sdrh **
1634f03f413Sdrh ** Two calling forms:
1644f03f413Sdrh **
1654f03f413Sdrh **          Qcircle(X,Y,Radius,eType)        -- All values are doubles
1664f03f413Sdrh **          Qcircle('x:X y:Y r:R e:ETYPE')   -- Single string parameter
16765e6b0ddSdrh */
circle_query_func(sqlite3_rtree_query_info * p)16865e6b0ddSdrh static int circle_query_func(sqlite3_rtree_query_info *p){
16965e6b0ddSdrh   int i;                          /* Iterator variable */
17065e6b0ddSdrh   Circle *pCircle;                /* Structure defining circular region */
17165e6b0ddSdrh   double xmin, xmax;              /* X dimensions of box being tested */
17265e6b0ddSdrh   double ymin, ymax;              /* X dimensions of box being tested */
17365e6b0ddSdrh   int nWithin = 0;                /* Number of corners inside the circle */
17465e6b0ddSdrh 
17565e6b0ddSdrh   xmin = p->aCoord[0];
17665e6b0ddSdrh   xmax = p->aCoord[1];
17765e6b0ddSdrh   ymin = p->aCoord[2];
17865e6b0ddSdrh   ymax = p->aCoord[3];
17965e6b0ddSdrh   pCircle = (Circle *)p->pUser;
18065e6b0ddSdrh   if( pCircle==0 ){
18165e6b0ddSdrh     /* If pUser is still 0, then the parameter values have not been tested
18265e6b0ddSdrh     ** for correctness or stored into a Circle structure yet. Do this now. */
18365e6b0ddSdrh 
18465e6b0ddSdrh     /* This geometry callback is for use with a 2-dimensional r-tree table.
18565e6b0ddSdrh     ** Return an error if the table does not have exactly 2 dimensions. */
18665e6b0ddSdrh     if( p->nCoord!=4 ) return SQLITE_ERROR;
18765e6b0ddSdrh 
1884f03f413Sdrh     /* Test that the correct number of parameters (1 or 4) have been supplied.
1894f03f413Sdrh     */
1904f03f413Sdrh     if( p->nParam!=4 && p->nParam!=1 ) return SQLITE_ERROR;
19165e6b0ddSdrh 
19265e6b0ddSdrh     /* Allocate a structure to cache parameter data in. Return SQLITE_NOMEM
19365e6b0ddSdrh     ** if the allocation fails. */
19465e6b0ddSdrh     pCircle = (Circle *)(p->pUser = sqlite3_malloc(sizeof(Circle)));
19565e6b0ddSdrh     if( !pCircle ) return SQLITE_NOMEM;
19665e6b0ddSdrh     p->xDelUser = circle_del;
19765e6b0ddSdrh 
19865e6b0ddSdrh     /* Record the center and radius of the circular region. One way that
19965e6b0ddSdrh     ** tested bounding boxes that intersect the circular region are detected
20065e6b0ddSdrh     ** is by testing if each corner of the bounding box lies within radius
20165e6b0ddSdrh     ** units of the center of the circle. */
2024f03f413Sdrh     if( p->nParam==4 ){
20365e6b0ddSdrh       pCircle->centerx = p->aParam[0];
20465e6b0ddSdrh       pCircle->centery = p->aParam[1];
20565e6b0ddSdrh       pCircle->radius = p->aParam[2];
20665e6b0ddSdrh       pCircle->eScoreType = (int)p->aParam[3];
2074f03f413Sdrh     }else{
2084f03f413Sdrh       const char *z = (const char*)sqlite3_value_text(p->apSqlParam[0]);
2094f03f413Sdrh       pCircle->centerx = 0.0;
2104f03f413Sdrh       pCircle->centery = 0.0;
2114f03f413Sdrh       pCircle->radius = 0.0;
2124f03f413Sdrh       pCircle->eScoreType = 0;
2134f03f413Sdrh       while( z && z[0] ){
2144f03f413Sdrh         if( z[0]=='r' && z[1]==':' ){
2154f03f413Sdrh           pCircle->radius = atof(&z[2]);
2164f03f413Sdrh         }else if( z[0]=='x' && z[1]==':' ){
2174f03f413Sdrh           pCircle->centerx = atof(&z[2]);
2184f03f413Sdrh         }else if( z[0]=='y' && z[1]==':' ){
2194f03f413Sdrh           pCircle->centery = atof(&z[2]);
2204f03f413Sdrh         }else if( z[0]=='e' && z[1]==':' ){
2214f03f413Sdrh           pCircle->eScoreType = (int)atof(&z[2]);
2224f03f413Sdrh         }else if( z[0]==' ' ){
2234f03f413Sdrh           z++;
2244f03f413Sdrh           continue;
2254f03f413Sdrh         }
2264f03f413Sdrh         while( z[0]!=0 && z[0]!=' ' ) z++;
2274f03f413Sdrh         while( z[0]==' ' ) z++;
2284f03f413Sdrh       }
2294f03f413Sdrh     }
2304f03f413Sdrh     if( pCircle->radius<0.0 ){
2314f03f413Sdrh       sqlite3_free(pCircle);
2324f03f413Sdrh       return SQLITE_NOMEM;
2334f03f413Sdrh     }
23465e6b0ddSdrh 
23565e6b0ddSdrh     /* Define two bounding box regions. The first, aBox[0], extends to
23665e6b0ddSdrh     ** infinity in the X dimension. It covers the same range of the Y dimension
23765e6b0ddSdrh     ** as the circular region. The second, aBox[1], extends to infinity in
23865e6b0ddSdrh     ** the Y dimension and is constrained to the range of the circle in the
23965e6b0ddSdrh     ** X dimension.
24065e6b0ddSdrh     **
24165e6b0ddSdrh     ** Then imagine each box is split in half along its short axis by a line
24265e6b0ddSdrh     ** that intersects the center of the circular region. A bounding box
24365e6b0ddSdrh     ** being tested can be said to intersect the circular region if it contains
24465e6b0ddSdrh     ** points from each half of either of the two infinite bounding boxes.
24565e6b0ddSdrh     */
24665e6b0ddSdrh     pCircle->aBox[0].xmin = pCircle->centerx;
24765e6b0ddSdrh     pCircle->aBox[0].xmax = pCircle->centerx;
24865e6b0ddSdrh     pCircle->aBox[0].ymin = pCircle->centery + pCircle->radius;
24965e6b0ddSdrh     pCircle->aBox[0].ymax = pCircle->centery - pCircle->radius;
25065e6b0ddSdrh     pCircle->aBox[1].xmin = pCircle->centerx + pCircle->radius;
25165e6b0ddSdrh     pCircle->aBox[1].xmax = pCircle->centerx - pCircle->radius;
25265e6b0ddSdrh     pCircle->aBox[1].ymin = pCircle->centery;
25365e6b0ddSdrh     pCircle->aBox[1].ymax = pCircle->centery;
25465e6b0ddSdrh     pCircle->mxArea = 200.0*200.0;
25565e6b0ddSdrh   }
25665e6b0ddSdrh 
25765e6b0ddSdrh   /* Check if any of the 4 corners of the bounding-box being tested lie
25865e6b0ddSdrh   ** inside the circular region. If they do, then the bounding-box does
25965e6b0ddSdrh   ** intersect the region of interest. Set the output variable to true and
26065e6b0ddSdrh   ** return SQLITE_OK in this case. */
26165e6b0ddSdrh   for(i=0; i<4; i++){
26265e6b0ddSdrh     double x = (i&0x01) ? xmax : xmin;
26365e6b0ddSdrh     double y = (i&0x02) ? ymax : ymin;
26465e6b0ddSdrh     double d2;
26565e6b0ddSdrh 
26665e6b0ddSdrh     d2  = (x-pCircle->centerx)*(x-pCircle->centerx);
26765e6b0ddSdrh     d2 += (y-pCircle->centery)*(y-pCircle->centery);
26865e6b0ddSdrh     if( d2<(pCircle->radius*pCircle->radius) ) nWithin++;
26965e6b0ddSdrh   }
27065e6b0ddSdrh 
27165e6b0ddSdrh   /* Check if the bounding box covers any other part of the circular region.
27265e6b0ddSdrh   ** See comments above for a description of how this test works. If it does
27365e6b0ddSdrh   ** cover part of the circular region, set the output variable to true
27465e6b0ddSdrh   ** and return SQLITE_OK. */
27565e6b0ddSdrh   if( nWithin==0 ){
27665e6b0ddSdrh     for(i=0; i<2; i++){
27765e6b0ddSdrh       if( xmin<=pCircle->aBox[i].xmin
27865e6b0ddSdrh        && xmax>=pCircle->aBox[i].xmax
27965e6b0ddSdrh        && ymin<=pCircle->aBox[i].ymin
28065e6b0ddSdrh        && ymax>=pCircle->aBox[i].ymax
28165e6b0ddSdrh       ){
28265e6b0ddSdrh         nWithin = 1;
28365e6b0ddSdrh         break;
28465e6b0ddSdrh       }
28565e6b0ddSdrh     }
28665e6b0ddSdrh   }
28765e6b0ddSdrh 
28865e6b0ddSdrh   if( pCircle->eScoreType==1 ){
28965e6b0ddSdrh     /* Depth first search */
29065e6b0ddSdrh     p->rScore = p->iLevel;
29165e6b0ddSdrh   }else if( pCircle->eScoreType==2 ){
29265e6b0ddSdrh     /* Breadth first search */
29365e6b0ddSdrh     p->rScore = 100 - p->iLevel;
29465e6b0ddSdrh   }else if( pCircle->eScoreType==3 ){
29565e6b0ddSdrh     /* Depth-first search, except sort the leaf nodes by area with
29665e6b0ddSdrh     ** the largest area first */
29765e6b0ddSdrh     if( p->iLevel==1 ){
29865e6b0ddSdrh       p->rScore = 1.0 - (xmax-xmin)*(ymax-ymin)/pCircle->mxArea;
29965e6b0ddSdrh       if( p->rScore<0.01 ) p->rScore = 0.01;
30065e6b0ddSdrh     }else{
30165e6b0ddSdrh       p->rScore = 0.0;
30265e6b0ddSdrh     }
30365e6b0ddSdrh   }else if( pCircle->eScoreType==4 ){
30465e6b0ddSdrh     /* Depth-first search, except exclude odd rowids */
30565e6b0ddSdrh     p->rScore = p->iLevel;
30665e6b0ddSdrh     if( p->iRowid&1 ) nWithin = 0;
30765e6b0ddSdrh   }else{
30865e6b0ddSdrh     /* Breadth-first search, except exclude odd rowids */
30965e6b0ddSdrh     p->rScore = 100 - p->iLevel;
31065e6b0ddSdrh     if( p->iRowid&1 ) nWithin = 0;
31165e6b0ddSdrh   }
31265e6b0ddSdrh   if( nWithin==0 ){
31365e6b0ddSdrh     p->eWithin = NOT_WITHIN;
31465e6b0ddSdrh   }else if( nWithin>=4 ){
31565e6b0ddSdrh     p->eWithin = FULLY_WITHIN;
31665e6b0ddSdrh   }else{
31765e6b0ddSdrh     p->eWithin = PARTLY_WITHIN;
31865e6b0ddSdrh   }
31965e6b0ddSdrh   return SQLITE_OK;
32065e6b0ddSdrh }
32165e6b0ddSdrh /*
32265e6b0ddSdrh ** Implementation of "breadthfirstsearch" r-tree geometry callback using the
32365e6b0ddSdrh ** 2nd-generation interface that allows scoring.
32465e6b0ddSdrh **
32565e6b0ddSdrh **     ... WHERE id MATCH breadthfirstsearch($x0,$x1,$y0,$y1) ...
32665e6b0ddSdrh **
32765e6b0ddSdrh ** It returns all entries whose bounding boxes overlap with $x0,$x1,$y0,$y1.
32865e6b0ddSdrh */
bfs_query_func(sqlite3_rtree_query_info * p)32965e6b0ddSdrh static int bfs_query_func(sqlite3_rtree_query_info *p){
33065e6b0ddSdrh   double x0,x1,y0,y1;        /* Dimensions of box being tested */
33165e6b0ddSdrh   double bx0,bx1,by0,by1;    /* Boundary of the query function */
33265e6b0ddSdrh 
33365e6b0ddSdrh   if( p->nParam!=4 ) return SQLITE_ERROR;
33465e6b0ddSdrh   x0 = p->aCoord[0];
33565e6b0ddSdrh   x1 = p->aCoord[1];
33665e6b0ddSdrh   y0 = p->aCoord[2];
33765e6b0ddSdrh   y1 = p->aCoord[3];
33865e6b0ddSdrh   bx0 = p->aParam[0];
33965e6b0ddSdrh   bx1 = p->aParam[1];
34065e6b0ddSdrh   by0 = p->aParam[2];
34165e6b0ddSdrh   by1 = p->aParam[3];
34265e6b0ddSdrh   p->rScore = 100 - p->iLevel;
34365e6b0ddSdrh   if( p->eParentWithin==FULLY_WITHIN ){
34465e6b0ddSdrh     p->eWithin = FULLY_WITHIN;
34565e6b0ddSdrh   }else if( x0>=bx0 && x1<=bx1 && y0>=by0 && y1<=by1 ){
34665e6b0ddSdrh     p->eWithin = FULLY_WITHIN;
34765e6b0ddSdrh   }else if( x1>=bx0 && x0<=bx1 && y1>=by0 && y0<=by1 ){
34865e6b0ddSdrh     p->eWithin = PARTLY_WITHIN;
34965e6b0ddSdrh   }else{
35065e6b0ddSdrh     p->eWithin = NOT_WITHIN;
35165e6b0ddSdrh   }
35265e6b0ddSdrh   return SQLITE_OK;
35365e6b0ddSdrh }
35465e6b0ddSdrh 
35518ec96b3Sdan /* END of implementation of "circle" geometry callback.
35618ec96b3Sdan **************************************************************************
35718ec96b3Sdan *************************************************************************/
35818ec96b3Sdan 
3599508daa9Sdan #include <assert.h>
36052b1dbb5Smistachkin #if defined(INCLUDE_SQLITE_TCL_H)
36152b1dbb5Smistachkin #  include "sqlite_tcl.h"
36252b1dbb5Smistachkin #else
3639508daa9Sdan #  include "tcl.h"
36452b1dbb5Smistachkin #endif
3659508daa9Sdan 
3669508daa9Sdan typedef struct Cube Cube;
3679508daa9Sdan struct Cube {
3689508daa9Sdan   double x;
3699508daa9Sdan   double y;
3709508daa9Sdan   double z;
3719508daa9Sdan   double width;
3729508daa9Sdan   double height;
3739508daa9Sdan   double depth;
3749508daa9Sdan };
3759508daa9Sdan 
cube_context_free(void * p)3769508daa9Sdan static void cube_context_free(void *p){
3779508daa9Sdan   sqlite3_free(p);
3789508daa9Sdan }
3799508daa9Sdan 
3807bddb755Sdan /*
3817bddb755Sdan ** The context pointer registered along with the 'cube' callback is
3827bddb755Sdan ** always ((void *)&gHere). This is just to facilitate testing, it is not
3837bddb755Sdan ** actually used for anything.
3847bddb755Sdan */
3859508daa9Sdan static int gHere = 42;
3869508daa9Sdan 
3879508daa9Sdan /*
3889508daa9Sdan ** Implementation of a simple r-tree geom callback to test for intersection
3899508daa9Sdan ** of r-tree rows with a "cube" shape. Cubes are defined by six scalar
3909508daa9Sdan ** coordinates as follows:
3919508daa9Sdan **
3929508daa9Sdan **   cube(x, y, z, width, height, depth)
3939508daa9Sdan **
3949508daa9Sdan ** The width, height and depth parameters must all be greater than zero.
3959508daa9Sdan */
cube_geom(sqlite3_rtree_geometry * p,int nCoord,sqlite3_rtree_dbl * aCoord,int * piRes)3969508daa9Sdan static int cube_geom(
397c223b8f1Sdan   sqlite3_rtree_geometry *p,
3989508daa9Sdan   int nCoord,
39965e6b0ddSdrh   sqlite3_rtree_dbl *aCoord,
4009508daa9Sdan   int *piRes
4019508daa9Sdan ){
4029508daa9Sdan   Cube *pCube = (Cube *)p->pUser;
4039508daa9Sdan 
4049508daa9Sdan   assert( p->pContext==(void *)&gHere );
4059508daa9Sdan 
4069508daa9Sdan   if( pCube==0 ){
4079508daa9Sdan     if( p->nParam!=6 || nCoord!=6
4089508daa9Sdan      || p->aParam[3]<=0.0 || p->aParam[4]<=0.0 || p->aParam[5]<=0.0
4099508daa9Sdan     ){
4109508daa9Sdan       return SQLITE_ERROR;
4119508daa9Sdan     }
4129508daa9Sdan     pCube = (Cube *)sqlite3_malloc(sizeof(Cube));
4139508daa9Sdan     if( !pCube ){
4149508daa9Sdan       return SQLITE_NOMEM;
4159508daa9Sdan     }
4169508daa9Sdan     pCube->x = p->aParam[0];
4179508daa9Sdan     pCube->y = p->aParam[1];
4189508daa9Sdan     pCube->z = p->aParam[2];
4199508daa9Sdan     pCube->width = p->aParam[3];
4209508daa9Sdan     pCube->height = p->aParam[4];
4219508daa9Sdan     pCube->depth = p->aParam[5];
4229508daa9Sdan 
4239508daa9Sdan     p->pUser = (void *)pCube;
4249508daa9Sdan     p->xDelUser = cube_context_free;
4259508daa9Sdan   }
4269508daa9Sdan 
4279508daa9Sdan   assert( nCoord==6 );
4289508daa9Sdan   *piRes = 0;
4299508daa9Sdan   if( aCoord[0]<=(pCube->x+pCube->width)
4309508daa9Sdan    && aCoord[1]>=pCube->x
4319508daa9Sdan    && aCoord[2]<=(pCube->y+pCube->height)
4329508daa9Sdan    && aCoord[3]>=pCube->y
4339508daa9Sdan    && aCoord[4]<=(pCube->z+pCube->depth)
4349508daa9Sdan    && aCoord[5]>=pCube->z
4359508daa9Sdan   ){
4369508daa9Sdan     *piRes = 1;
4379508daa9Sdan   }
4389508daa9Sdan 
4399508daa9Sdan   return SQLITE_OK;
4409508daa9Sdan }
441860e332cSdrh #endif /* SQLITE_ENABLE_RTREE */
4429508daa9Sdan 
register_cube_geom(void * clientData,Tcl_Interp * interp,int objc,Tcl_Obj * CONST objv[])443*7617e4a8Smistachkin static int SQLITE_TCLAPI register_cube_geom(
4449508daa9Sdan   void * clientData,
4459508daa9Sdan   Tcl_Interp *interp,
4469508daa9Sdan   int objc,
4479508daa9Sdan   Tcl_Obj *CONST objv[]
4489508daa9Sdan ){
449bd2aaf9aSshaneh #ifndef SQLITE_ENABLE_RTREE
450bd2aaf9aSshaneh   UNUSED_PARAMETER(clientData);
451bd2aaf9aSshaneh   UNUSED_PARAMETER(interp);
452bd2aaf9aSshaneh   UNUSED_PARAMETER(objc);
453bd2aaf9aSshaneh   UNUSED_PARAMETER(objv);
454bd2aaf9aSshaneh #else
4559508daa9Sdan   extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**);
456e84d8d32Smistachkin   extern const char *sqlite3ErrName(int);
4579508daa9Sdan   sqlite3 *db;
4587bddb755Sdan   int rc;
4599508daa9Sdan 
4609508daa9Sdan   if( objc!=2 ){
4619508daa9Sdan     Tcl_WrongNumArgs(interp, 1, objv, "DB");
4629508daa9Sdan     return TCL_ERROR;
4639508daa9Sdan   }
4649508daa9Sdan   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4657bddb755Sdan   rc = sqlite3_rtree_geometry_callback(db, "cube", cube_geom, (void *)&gHere);
466e84d8d32Smistachkin   Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
46718ec96b3Sdan #endif
46818ec96b3Sdan   return TCL_OK;
46918ec96b3Sdan }
47018ec96b3Sdan 
register_circle_geom(void * clientData,Tcl_Interp * interp,int objc,Tcl_Obj * CONST objv[])471*7617e4a8Smistachkin static int SQLITE_TCLAPI register_circle_geom(
47218ec96b3Sdan   void * clientData,
47318ec96b3Sdan   Tcl_Interp *interp,
47418ec96b3Sdan   int objc,
47518ec96b3Sdan   Tcl_Obj *CONST objv[]
47618ec96b3Sdan ){
477bd2aaf9aSshaneh #ifndef SQLITE_ENABLE_RTREE
478bd2aaf9aSshaneh   UNUSED_PARAMETER(clientData);
479bd2aaf9aSshaneh   UNUSED_PARAMETER(interp);
480bd2aaf9aSshaneh   UNUSED_PARAMETER(objc);
481bd2aaf9aSshaneh   UNUSED_PARAMETER(objv);
482bd2aaf9aSshaneh #else
48318ec96b3Sdan   extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**);
484e84d8d32Smistachkin   extern const char *sqlite3ErrName(int);
48518ec96b3Sdan   sqlite3 *db;
48618ec96b3Sdan   int rc;
48718ec96b3Sdan 
48818ec96b3Sdan   if( objc!=2 ){
48918ec96b3Sdan     Tcl_WrongNumArgs(interp, 1, objv, "DB");
49018ec96b3Sdan     return TCL_ERROR;
49118ec96b3Sdan   }
49218ec96b3Sdan   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
49318ec96b3Sdan   rc = sqlite3_rtree_geometry_callback(db, "circle", circle_geom, 0);
49465e6b0ddSdrh   if( rc==SQLITE_OK ){
49565e6b0ddSdrh     rc = sqlite3_rtree_query_callback(db, "Qcircle",
49665e6b0ddSdrh                                       circle_query_func, 0, 0);
49765e6b0ddSdrh   }
49865e6b0ddSdrh   if( rc==SQLITE_OK ){
49965e6b0ddSdrh     rc = sqlite3_rtree_query_callback(db, "breadthfirstsearch",
50065e6b0ddSdrh                                       bfs_query_func, 0, 0);
50165e6b0ddSdrh   }
502e84d8d32Smistachkin   Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
5039508daa9Sdan #endif
5049508daa9Sdan   return TCL_OK;
5059508daa9Sdan }
5069508daa9Sdan 
Sqlitetestrtree_Init(Tcl_Interp * interp)5079508daa9Sdan int Sqlitetestrtree_Init(Tcl_Interp *interp){
5089508daa9Sdan   Tcl_CreateObjCommand(interp, "register_cube_geom", register_cube_geom, 0, 0);
50918ec96b3Sdan   Tcl_CreateObjCommand(interp, "register_circle_geom",register_circle_geom,0,0);
5109508daa9Sdan   return TCL_OK;
5119508daa9Sdan }
512