xref: /sqlite-3.40.0/src/test_rtree.c (revision f439fbda)
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 
18 /* Solely for the UNUSED_PARAMETER() macro. */
19 #include "sqliteInt.h"
20 
21 #ifdef SQLITE_ENABLE_RTREE
22 /*
23 ** Type used to cache parameter information for the "circle" r-tree geometry
24 ** callback.
25 */
26 typedef struct Circle Circle;
27 struct Circle {
28   struct Box {
29     double xmin;
30     double xmax;
31     double ymin;
32     double ymax;
33   } aBox[2];
34   double centerx;
35   double centery;
36   double radius;
37 };
38 
39 /*
40 ** Destructor function for Circle objects allocated by circle_geom().
41 */
42 static void circle_del(void *p){
43   sqlite3_free(p);
44 }
45 
46 /*
47 ** Implementation of "circle" r-tree geometry callback.
48 */
49 static int circle_geom(
50   sqlite3_rtree_geometry *p,
51   int nCoord,
52 #ifdef SQLITE_RTREE_INT_ONLY
53   sqlite3_int64 *aCoord,
54 #else
55   double *aCoord,
56 #endif
57   int *pRes
58 ){
59   int i;                          /* Iterator variable */
60   Circle *pCircle;                /* Structure defining circular region */
61   double xmin, xmax;              /* X dimensions of box being tested */
62   double ymin, ymax;              /* X dimensions of box being tested */
63 
64   if( p->pUser==0 ){
65     /* If pUser is still 0, then the parameter values have not been tested
66     ** for correctness or stored into a Circle structure yet. Do this now. */
67 
68     /* This geometry callback is for use with a 2-dimensional r-tree table.
69     ** Return an error if the table does not have exactly 2 dimensions. */
70     if( nCoord!=4 ) return SQLITE_ERROR;
71 
72     /* Test that the correct number of parameters (3) have been supplied,
73     ** and that the parameters are in range (that the radius of the circle
74     ** radius is greater than zero). */
75     if( p->nParam!=3 || p->aParam[2]<0.0 ) return SQLITE_ERROR;
76 
77     /* Allocate a structure to cache parameter data in. Return SQLITE_NOMEM
78     ** if the allocation fails. */
79     pCircle = (Circle *)(p->pUser = sqlite3_malloc(sizeof(Circle)));
80     if( !pCircle ) return SQLITE_NOMEM;
81     p->xDelUser = circle_del;
82 
83     /* Record the center and radius of the circular region. One way that
84     ** tested bounding boxes that intersect the circular region are detected
85     ** is by testing if each corner of the bounding box lies within radius
86     ** units of the center of the circle. */
87     pCircle->centerx = p->aParam[0];
88     pCircle->centery = p->aParam[1];
89     pCircle->radius = p->aParam[2];
90 
91     /* Define two bounding box regions. The first, aBox[0], extends to
92     ** infinity in the X dimension. It covers the same range of the Y dimension
93     ** as the circular region. The second, aBox[1], extends to infinity in
94     ** the Y dimension and is constrained to the range of the circle in the
95     ** X dimension.
96     **
97     ** Then imagine each box is split in half along its short axis by a line
98     ** that intersects the center of the circular region. A bounding box
99     ** being tested can be said to intersect the circular region if it contains
100     ** points from each half of either of the two infinite bounding boxes.
101     */
102     pCircle->aBox[0].xmin = pCircle->centerx;
103     pCircle->aBox[0].xmax = pCircle->centerx;
104     pCircle->aBox[0].ymin = pCircle->centery + pCircle->radius;
105     pCircle->aBox[0].ymax = pCircle->centery - pCircle->radius;
106     pCircle->aBox[1].xmin = pCircle->centerx + pCircle->radius;
107     pCircle->aBox[1].xmax = pCircle->centerx - pCircle->radius;
108     pCircle->aBox[1].ymin = pCircle->centery;
109     pCircle->aBox[1].ymax = pCircle->centery;
110   }
111 
112   pCircle = (Circle *)p->pUser;
113   xmin = aCoord[0];
114   xmax = aCoord[1];
115   ymin = aCoord[2];
116   ymax = aCoord[3];
117 
118   /* Check if any of the 4 corners of the bounding-box being tested lie
119   ** inside the circular region. If they do, then the bounding-box does
120   ** intersect the region of interest. Set the output variable to true and
121   ** return SQLITE_OK in this case. */
122   for(i=0; i<4; i++){
123     double x = (i&0x01) ? xmax : xmin;
124     double y = (i&0x02) ? ymax : ymin;
125     double d2;
126 
127     d2  = (x-pCircle->centerx)*(x-pCircle->centerx);
128     d2 += (y-pCircle->centery)*(y-pCircle->centery);
129     if( d2<(pCircle->radius*pCircle->radius) ){
130       *pRes = 1;
131       return SQLITE_OK;
132     }
133   }
134 
135   /* Check if the bounding box covers any other part of the circular region.
136   ** See comments above for a description of how this test works. If it does
137   ** cover part of the circular region, set the output variable to true
138   ** and return SQLITE_OK. */
139   for(i=0; i<2; i++){
140     if( xmin<=pCircle->aBox[i].xmin
141      && xmax>=pCircle->aBox[i].xmax
142      && ymin<=pCircle->aBox[i].ymin
143      && ymax>=pCircle->aBox[i].ymax
144     ){
145       *pRes = 1;
146       return SQLITE_OK;
147     }
148   }
149 
150   /* The specified bounding box does not intersect the circular region. Set
151   ** the output variable to zero and return SQLITE_OK. */
152   *pRes = 0;
153   return SQLITE_OK;
154 }
155 
156 /* END of implementation of "circle" geometry callback.
157 **************************************************************************
158 *************************************************************************/
159 
160 #include <assert.h>
161 #include "tcl.h"
162 
163 typedef struct Cube Cube;
164 struct Cube {
165   double x;
166   double y;
167   double z;
168   double width;
169   double height;
170   double depth;
171 };
172 
173 static void cube_context_free(void *p){
174   sqlite3_free(p);
175 }
176 
177 /*
178 ** The context pointer registered along with the 'cube' callback is
179 ** always ((void *)&gHere). This is just to facilitate testing, it is not
180 ** actually used for anything.
181 */
182 static int gHere = 42;
183 
184 /*
185 ** Implementation of a simple r-tree geom callback to test for intersection
186 ** of r-tree rows with a "cube" shape. Cubes are defined by six scalar
187 ** coordinates as follows:
188 **
189 **   cube(x, y, z, width, height, depth)
190 **
191 ** The width, height and depth parameters must all be greater than zero.
192 */
193 static int cube_geom(
194   sqlite3_rtree_geometry *p,
195   int nCoord,
196 #ifdef SQLITE_RTREE_INT_ONLY
197   sqlite3_int64 *aCoord,
198 #else
199   double *aCoord,
200 #endif
201   int *piRes
202 ){
203   Cube *pCube = (Cube *)p->pUser;
204 
205   assert( p->pContext==(void *)&gHere );
206 
207   if( pCube==0 ){
208     if( p->nParam!=6 || nCoord!=6
209      || p->aParam[3]<=0.0 || p->aParam[4]<=0.0 || p->aParam[5]<=0.0
210     ){
211       return SQLITE_ERROR;
212     }
213     pCube = (Cube *)sqlite3_malloc(sizeof(Cube));
214     if( !pCube ){
215       return SQLITE_NOMEM;
216     }
217     pCube->x = p->aParam[0];
218     pCube->y = p->aParam[1];
219     pCube->z = p->aParam[2];
220     pCube->width = p->aParam[3];
221     pCube->height = p->aParam[4];
222     pCube->depth = p->aParam[5];
223 
224     p->pUser = (void *)pCube;
225     p->xDelUser = cube_context_free;
226   }
227 
228   assert( nCoord==6 );
229   *piRes = 0;
230   if( aCoord[0]<=(pCube->x+pCube->width)
231    && aCoord[1]>=pCube->x
232    && aCoord[2]<=(pCube->y+pCube->height)
233    && aCoord[3]>=pCube->y
234    && aCoord[4]<=(pCube->z+pCube->depth)
235    && aCoord[5]>=pCube->z
236   ){
237     *piRes = 1;
238   }
239 
240   return SQLITE_OK;
241 }
242 #endif /* SQLITE_ENABLE_RTREE */
243 
244 static int register_cube_geom(
245   void * clientData,
246   Tcl_Interp *interp,
247   int objc,
248   Tcl_Obj *CONST objv[]
249 ){
250 #ifndef SQLITE_ENABLE_RTREE
251   UNUSED_PARAMETER(clientData);
252   UNUSED_PARAMETER(interp);
253   UNUSED_PARAMETER(objc);
254   UNUSED_PARAMETER(objv);
255 #else
256   extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**);
257   extern const char *sqlite3TestErrorName(int);
258   sqlite3 *db;
259   int rc;
260 
261   if( objc!=2 ){
262     Tcl_WrongNumArgs(interp, 1, objv, "DB");
263     return TCL_ERROR;
264   }
265   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
266   rc = sqlite3_rtree_geometry_callback(db, "cube", cube_geom, (void *)&gHere);
267   Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);
268 #endif
269   return TCL_OK;
270 }
271 
272 static int register_circle_geom(
273   void * clientData,
274   Tcl_Interp *interp,
275   int objc,
276   Tcl_Obj *CONST objv[]
277 ){
278 #ifndef SQLITE_ENABLE_RTREE
279   UNUSED_PARAMETER(clientData);
280   UNUSED_PARAMETER(interp);
281   UNUSED_PARAMETER(objc);
282   UNUSED_PARAMETER(objv);
283 #else
284   extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**);
285   extern const char *sqlite3TestErrorName(int);
286   sqlite3 *db;
287   int rc;
288 
289   if( objc!=2 ){
290     Tcl_WrongNumArgs(interp, 1, objv, "DB");
291     return TCL_ERROR;
292   }
293   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
294   rc = sqlite3_rtree_geometry_callback(db, "circle", circle_geom, 0);
295   Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);
296 #endif
297   return TCL_OK;
298 }
299 
300 int Sqlitetestrtree_Init(Tcl_Interp *interp){
301   Tcl_CreateObjCommand(interp, "register_cube_geom", register_cube_geom, 0, 0);
302   Tcl_CreateObjCommand(interp, "register_circle_geom",register_circle_geom,0,0);
303   return TCL_OK;
304 }
305