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