xref: /sqlite-3.40.0/src/test_rtree.c (revision a6ca7f2c)
1 /*
2 ** 2010 August 28
3 **
4 ** The author disclaims copyright to this source code.  In place of
5 ** a legal notice, here is a blessing:
6 **
7 **    May you do good and not evil.
8 **    May you find forgiveness for yourself and forgive others.
9 **    May you share freely, never taking more than you give.
10 **
11 *************************************************************************
12 ** Code for testing all sorts of SQLite interfaces. This code
13 ** is not included in the SQLite library.
14 */
15 
16 #include <sqlite3.h>
17 #include <tcl.h>
18 
19 /* Solely for the UNUSED_PARAMETER() macro. */
20 #include "sqliteInt.h"
21 
22 #ifdef SQLITE_ENABLE_RTREE
23 /*
24 ** Type used to cache parameter information for the "circle" r-tree geometry
25 ** callback.
26 */
27 typedef struct Circle Circle;
28 struct Circle {
29   struct Box {
30     double xmin;
31     double xmax;
32     double ymin;
33     double ymax;
34   } aBox[2];
35   double centerx;
36   double centery;
37   double radius;
38   double mxArea;
39   int eScoreType;
40 };
41 
42 /*
43 ** Destructor function for Circle objects allocated by circle_geom().
44 */
45 static void circle_del(void *p){
46   sqlite3_free(p);
47 }
48 
49 /*
50 ** Implementation of "circle" r-tree geometry callback.
51 */
52 static int circle_geom(
53   sqlite3_rtree_geometry *p,
54   int nCoord,
55   sqlite3_rtree_dbl *aCoord,
56   int *pRes
57 ){
58   int i;                          /* Iterator variable */
59   Circle *pCircle;                /* Structure defining circular region */
60   double xmin, xmax;              /* X dimensions of box being tested */
61   double ymin, ymax;              /* X dimensions of box being tested */
62 
63   xmin = aCoord[0];
64   xmax = aCoord[1];
65   ymin = aCoord[2];
66   ymax = aCoord[3];
67   pCircle = (Circle *)p->pUser;
68   if( pCircle==0 ){
69     /* If pUser is still 0, then the parameter values have not been tested
70     ** for correctness or stored into a Circle structure yet. Do this now. */
71 
72     /* This geometry callback is for use with a 2-dimensional r-tree table.
73     ** Return an error if the table does not have exactly 2 dimensions. */
74     if( nCoord!=4 ) return SQLITE_ERROR;
75 
76     /* Test that the correct number of parameters (3) have been supplied,
77     ** and that the parameters are in range (that the radius of the circle
78     ** radius is greater than zero). */
79     if( p->nParam!=3 || p->aParam[2]<0.0 ) return SQLITE_ERROR;
80 
81     /* Allocate a structure to cache parameter data in. Return SQLITE_NOMEM
82     ** if the allocation fails. */
83     pCircle = (Circle *)(p->pUser = sqlite3_malloc(sizeof(Circle)));
84     if( !pCircle ) return SQLITE_NOMEM;
85     p->xDelUser = circle_del;
86 
87     /* Record the center and radius of the circular region. One way that
88     ** tested bounding boxes that intersect the circular region are detected
89     ** is by testing if each corner of the bounding box lies within radius
90     ** units of the center of the circle. */
91     pCircle->centerx = p->aParam[0];
92     pCircle->centery = p->aParam[1];
93     pCircle->radius = p->aParam[2];
94 
95     /* Define two bounding box regions. The first, aBox[0], extends to
96     ** infinity in the X dimension. It covers the same range of the Y dimension
97     ** as the circular region. The second, aBox[1], extends to infinity in
98     ** the Y dimension and is constrained to the range of the circle in the
99     ** X dimension.
100     **
101     ** Then imagine each box is split in half along its short axis by a line
102     ** that intersects the center of the circular region. A bounding box
103     ** being tested can be said to intersect the circular region if it contains
104     ** points from each half of either of the two infinite bounding boxes.
105     */
106     pCircle->aBox[0].xmin = pCircle->centerx;
107     pCircle->aBox[0].xmax = pCircle->centerx;
108     pCircle->aBox[0].ymin = pCircle->centery + pCircle->radius;
109     pCircle->aBox[0].ymax = pCircle->centery - pCircle->radius;
110     pCircle->aBox[1].xmin = pCircle->centerx + pCircle->radius;
111     pCircle->aBox[1].xmax = pCircle->centerx - pCircle->radius;
112     pCircle->aBox[1].ymin = pCircle->centery;
113     pCircle->aBox[1].ymax = pCircle->centery;
114     pCircle->mxArea = (xmax - xmin)*(ymax - ymin) + 1.0;
115   }
116 
117   /* Check if any of the 4 corners of the bounding-box being tested lie
118   ** inside the circular region. If they do, then the bounding-box does
119   ** intersect the region of interest. Set the output variable to true and
120   ** return SQLITE_OK in this case. */
121   for(i=0; i<4; i++){
122     double x = (i&0x01) ? xmax : xmin;
123     double y = (i&0x02) ? ymax : ymin;
124     double d2;
125 
126     d2  = (x-pCircle->centerx)*(x-pCircle->centerx);
127     d2 += (y-pCircle->centery)*(y-pCircle->centery);
128     if( d2<(pCircle->radius*pCircle->radius) ){
129       *pRes = 1;
130       return SQLITE_OK;
131     }
132   }
133 
134   /* Check if the bounding box covers any other part of the circular region.
135   ** See comments above for a description of how this test works. If it does
136   ** cover part of the circular region, set the output variable to true
137   ** and return SQLITE_OK. */
138   for(i=0; i<2; i++){
139     if( xmin<=pCircle->aBox[i].xmin
140      && xmax>=pCircle->aBox[i].xmax
141      && ymin<=pCircle->aBox[i].ymin
142      && ymax>=pCircle->aBox[i].ymax
143     ){
144       *pRes = 1;
145       return SQLITE_OK;
146     }
147   }
148 
149   /* The specified bounding box does not intersect the circular region. Set
150   ** the output variable to zero and return SQLITE_OK. */
151   *pRes = 0;
152   return SQLITE_OK;
153 }
154 
155 /*
156 ** Implementation of "circle" r-tree geometry callback using the
157 ** 2nd-generation interface that allows scoring.
158 */
159 static int circle_query_func(sqlite3_rtree_query_info *p){
160   int i;                          /* Iterator variable */
161   Circle *pCircle;                /* Structure defining circular region */
162   double xmin, xmax;              /* X dimensions of box being tested */
163   double ymin, ymax;              /* X dimensions of box being tested */
164   int nWithin = 0;                /* Number of corners inside the circle */
165 
166   xmin = p->aCoord[0];
167   xmax = p->aCoord[1];
168   ymin = p->aCoord[2];
169   ymax = p->aCoord[3];
170   pCircle = (Circle *)p->pUser;
171   if( pCircle==0 ){
172     /* If pUser is still 0, then the parameter values have not been tested
173     ** for correctness or stored into a Circle structure yet. Do this now. */
174 
175     /* This geometry callback is for use with a 2-dimensional r-tree table.
176     ** Return an error if the table does not have exactly 2 dimensions. */
177     if( p->nCoord!=4 ) return SQLITE_ERROR;
178 
179     /* Test that the correct number of parameters (4) have been supplied,
180     ** and that the parameters are in range (that the radius of the circle
181     ** radius is greater than zero). */
182     if( p->nParam!=4 || p->aParam[2]<0.0 ) return SQLITE_ERROR;
183 
184     /* Allocate a structure to cache parameter data in. Return SQLITE_NOMEM
185     ** if the allocation fails. */
186     pCircle = (Circle *)(p->pUser = sqlite3_malloc(sizeof(Circle)));
187     if( !pCircle ) return SQLITE_NOMEM;
188     p->xDelUser = circle_del;
189 
190     /* Record the center and radius of the circular region. One way that
191     ** tested bounding boxes that intersect the circular region are detected
192     ** is by testing if each corner of the bounding box lies within radius
193     ** units of the center of the circle. */
194     pCircle->centerx = p->aParam[0];
195     pCircle->centery = p->aParam[1];
196     pCircle->radius = p->aParam[2];
197     pCircle->eScoreType = (int)p->aParam[3];
198 
199     /* Define two bounding box regions. The first, aBox[0], extends to
200     ** infinity in the X dimension. It covers the same range of the Y dimension
201     ** as the circular region. The second, aBox[1], extends to infinity in
202     ** the Y dimension and is constrained to the range of the circle in the
203     ** X dimension.
204     **
205     ** Then imagine each box is split in half along its short axis by a line
206     ** that intersects the center of the circular region. A bounding box
207     ** being tested can be said to intersect the circular region if it contains
208     ** points from each half of either of the two infinite bounding boxes.
209     */
210     pCircle->aBox[0].xmin = pCircle->centerx;
211     pCircle->aBox[0].xmax = pCircle->centerx;
212     pCircle->aBox[0].ymin = pCircle->centery + pCircle->radius;
213     pCircle->aBox[0].ymax = pCircle->centery - pCircle->radius;
214     pCircle->aBox[1].xmin = pCircle->centerx + pCircle->radius;
215     pCircle->aBox[1].xmax = pCircle->centerx - pCircle->radius;
216     pCircle->aBox[1].ymin = pCircle->centery;
217     pCircle->aBox[1].ymax = pCircle->centery;
218     pCircle->mxArea = 200.0*200.0;
219   }
220 
221   /* Check if any of the 4 corners of the bounding-box being tested lie
222   ** inside the circular region. If they do, then the bounding-box does
223   ** intersect the region of interest. Set the output variable to true and
224   ** return SQLITE_OK in this case. */
225   for(i=0; i<4; i++){
226     double x = (i&0x01) ? xmax : xmin;
227     double y = (i&0x02) ? ymax : ymin;
228     double d2;
229 
230     d2  = (x-pCircle->centerx)*(x-pCircle->centerx);
231     d2 += (y-pCircle->centery)*(y-pCircle->centery);
232     if( d2<(pCircle->radius*pCircle->radius) ) nWithin++;
233   }
234 
235   /* Check if the bounding box covers any other part of the circular region.
236   ** See comments above for a description of how this test works. If it does
237   ** cover part of the circular region, set the output variable to true
238   ** and return SQLITE_OK. */
239   if( nWithin==0 ){
240     for(i=0; i<2; i++){
241       if( xmin<=pCircle->aBox[i].xmin
242        && xmax>=pCircle->aBox[i].xmax
243        && ymin<=pCircle->aBox[i].ymin
244        && ymax>=pCircle->aBox[i].ymax
245       ){
246         nWithin = 1;
247         break;
248       }
249     }
250   }
251 
252   if( pCircle->eScoreType==1 ){
253     /* Depth first search */
254     p->rScore = p->iLevel;
255   }else if( pCircle->eScoreType==2 ){
256     /* Breadth first search */
257     p->rScore = 100 - p->iLevel;
258   }else{
259     /* Depth-first search, except sort the leaf nodes by area with
260     ** the largest area first */
261     if( p->iLevel==1 ){
262       p->rScore = 1.0 - (xmax-xmin)*(ymax-ymin)/pCircle->mxArea;
263       if( p->rScore<0.01 ) p->rScore = 0.01;
264     }else{
265       p->rScore = 0.0;
266     }
267   }
268   if( nWithin==0 ){
269     p->eWithin = NOT_WITHIN;
270   }else if( nWithin>=4 ){
271     p->eWithin = FULLY_WITHIN;
272   }else{
273     p->eWithin = PARTLY_WITHIN;
274   }
275   return SQLITE_OK;
276 }
277 /*
278 ** Implementation of "breadthfirstsearch" r-tree geometry callback using the
279 ** 2nd-generation interface that allows scoring.
280 **
281 **     ... WHERE id MATCH breadthfirstsearch($x0,$x1,$y0,$y1) ...
282 **
283 ** It returns all entries whose bounding boxes overlap with $x0,$x1,$y0,$y1.
284 */
285 static int bfs_query_func(sqlite3_rtree_query_info *p){
286   double x0,x1,y0,y1;        /* Dimensions of box being tested */
287   double bx0,bx1,by0,by1;    /* Boundary of the query function */
288 
289   if( p->nParam!=4 ) return SQLITE_ERROR;
290   x0 = p->aCoord[0];
291   x1 = p->aCoord[1];
292   y0 = p->aCoord[2];
293   y1 = p->aCoord[3];
294   bx0 = p->aParam[0];
295   bx1 = p->aParam[1];
296   by0 = p->aParam[2];
297   by1 = p->aParam[3];
298   p->rScore = 100 - p->iLevel;
299   if( p->eParentWithin==FULLY_WITHIN ){
300     p->eWithin = FULLY_WITHIN;
301   }else if( x0>=bx0 && x1<=bx1 && y0>=by0 && y1<=by1 ){
302     p->eWithin = FULLY_WITHIN;
303   }else if( x1>=bx0 && x0<=bx1 && y1>=by0 && y0<=by1 ){
304     p->eWithin = PARTLY_WITHIN;
305   }else{
306     p->eWithin = NOT_WITHIN;
307   }
308   return SQLITE_OK;
309 }
310 
311 /* END of implementation of "circle" geometry callback.
312 **************************************************************************
313 *************************************************************************/
314 
315 #include <assert.h>
316 #include "tcl.h"
317 
318 typedef struct Cube Cube;
319 struct Cube {
320   double x;
321   double y;
322   double z;
323   double width;
324   double height;
325   double depth;
326 };
327 
328 static void cube_context_free(void *p){
329   sqlite3_free(p);
330 }
331 
332 /*
333 ** The context pointer registered along with the 'cube' callback is
334 ** always ((void *)&gHere). This is just to facilitate testing, it is not
335 ** actually used for anything.
336 */
337 static int gHere = 42;
338 
339 /*
340 ** Implementation of a simple r-tree geom callback to test for intersection
341 ** of r-tree rows with a "cube" shape. Cubes are defined by six scalar
342 ** coordinates as follows:
343 **
344 **   cube(x, y, z, width, height, depth)
345 **
346 ** The width, height and depth parameters must all be greater than zero.
347 */
348 static int cube_geom(
349   sqlite3_rtree_geometry *p,
350   int nCoord,
351   sqlite3_rtree_dbl *aCoord,
352   int *piRes
353 ){
354   Cube *pCube = (Cube *)p->pUser;
355 
356   assert( p->pContext==(void *)&gHere );
357 
358   if( pCube==0 ){
359     if( p->nParam!=6 || nCoord!=6
360      || p->aParam[3]<=0.0 || p->aParam[4]<=0.0 || p->aParam[5]<=0.0
361     ){
362       return SQLITE_ERROR;
363     }
364     pCube = (Cube *)sqlite3_malloc(sizeof(Cube));
365     if( !pCube ){
366       return SQLITE_NOMEM;
367     }
368     pCube->x = p->aParam[0];
369     pCube->y = p->aParam[1];
370     pCube->z = p->aParam[2];
371     pCube->width = p->aParam[3];
372     pCube->height = p->aParam[4];
373     pCube->depth = p->aParam[5];
374 
375     p->pUser = (void *)pCube;
376     p->xDelUser = cube_context_free;
377   }
378 
379   assert( nCoord==6 );
380   *piRes = 0;
381   if( aCoord[0]<=(pCube->x+pCube->width)
382    && aCoord[1]>=pCube->x
383    && aCoord[2]<=(pCube->y+pCube->height)
384    && aCoord[3]>=pCube->y
385    && aCoord[4]<=(pCube->z+pCube->depth)
386    && aCoord[5]>=pCube->z
387   ){
388     *piRes = 1;
389   }
390 
391   return SQLITE_OK;
392 }
393 #endif /* SQLITE_ENABLE_RTREE */
394 
395 static int register_cube_geom(
396   void * clientData,
397   Tcl_Interp *interp,
398   int objc,
399   Tcl_Obj *CONST objv[]
400 ){
401 #ifndef SQLITE_ENABLE_RTREE
402   UNUSED_PARAMETER(clientData);
403   UNUSED_PARAMETER(interp);
404   UNUSED_PARAMETER(objc);
405   UNUSED_PARAMETER(objv);
406 #else
407   extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**);
408   extern const char *sqlite3ErrName(int);
409   sqlite3 *db;
410   int rc;
411 
412   if( objc!=2 ){
413     Tcl_WrongNumArgs(interp, 1, objv, "DB");
414     return TCL_ERROR;
415   }
416   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
417   rc = sqlite3_rtree_geometry_callback(db, "cube", cube_geom, (void *)&gHere);
418   Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
419 #endif
420   return TCL_OK;
421 }
422 
423 static int register_circle_geom(
424   void * clientData,
425   Tcl_Interp *interp,
426   int objc,
427   Tcl_Obj *CONST objv[]
428 ){
429 #ifndef SQLITE_ENABLE_RTREE
430   UNUSED_PARAMETER(clientData);
431   UNUSED_PARAMETER(interp);
432   UNUSED_PARAMETER(objc);
433   UNUSED_PARAMETER(objv);
434 #else
435   extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**);
436   extern const char *sqlite3ErrName(int);
437   sqlite3 *db;
438   int rc;
439 
440   if( objc!=2 ){
441     Tcl_WrongNumArgs(interp, 1, objv, "DB");
442     return TCL_ERROR;
443   }
444   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
445   rc = sqlite3_rtree_geometry_callback(db, "circle", circle_geom, 0);
446   if( rc==SQLITE_OK ){
447     rc = sqlite3_rtree_query_callback(db, "Qcircle",
448                                       circle_query_func, 0, 0);
449   }
450   if( rc==SQLITE_OK ){
451     rc = sqlite3_rtree_query_callback(db, "breadthfirstsearch",
452                                       bfs_query_func, 0, 0);
453   }
454   Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
455 #endif
456   return TCL_OK;
457 }
458 
459 int Sqlitetestrtree_Init(Tcl_Interp *interp){
460   Tcl_CreateObjCommand(interp, "register_cube_geom", register_cube_geom, 0, 0);
461   Tcl_CreateObjCommand(interp, "register_circle_geom",register_circle_geom,0,0);
462   return TCL_OK;
463 }
464