4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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.
11 *************************************************************************
12 ** Code for testing all sorts of SQLite interfaces. This code
13 ** is not included in the SQLite library.
18 /* Solely for the UNUSED_PARAMETER() macro. */
19 #include "sqliteInt.h"
21 #ifdef SQLITE_ENABLE_RTREE
23 ** Type used to cache parameter information for the "circle" r-tree geometry
26 typedef struct Circle Circle
;
40 ** Destructor function for Circle objects allocated by circle_geom().
42 static void circle_del(void *p
){
47 ** Implementation of "circle" r-tree geometry callback.
49 static int circle_geom(
50 sqlite3_rtree_geometry
*p
,
52 #ifdef SQLITE_RTREE_INT_ONLY
53 sqlite3_int64
*aCoord
,
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 */
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. */
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
;
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
;
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
;
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];
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
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.
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
;
112 pCircle
= (Circle
*)p
->pUser
;
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. */
123 double x
= (i
&0x01) ? xmax
: xmin
;
124 double y
= (i
&0x02) ? ymax
: ymin
;
127 d2
= (x
-pCircle
->centerx
)*(x
-pCircle
->centerx
);
128 d2
+= (y
-pCircle
->centery
)*(y
-pCircle
->centery
);
129 if( d2
<(pCircle
->radius
*pCircle
->radius
) ){
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. */
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
150 /* The specified bounding box does not intersect the circular region. Set
151 ** the output variable to zero and return SQLITE_OK. */
156 /* END of implementation of "circle" geometry callback.
157 **************************************************************************
158 *************************************************************************/
163 typedef struct Cube Cube
;
173 static void cube_context_free(void *p
){
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.
182 static int gHere
= 42;
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:
189 ** cube(x, y, z, width, height, depth)
191 ** The width, height and depth parameters must all be greater than zero.
193 static int cube_geom(
194 sqlite3_rtree_geometry
*p
,
196 #ifdef SQLITE_RTREE_INT_ONLY
197 sqlite3_int64
*aCoord
,
203 Cube
*pCube
= (Cube
*)p
->pUser
;
205 assert( p
->pContext
==(void *)&gHere
);
208 if( p
->nParam
!=6 || nCoord
!=6
209 || p
->aParam
[3]<=0.0 || p
->aParam
[4]<=0.0 || p
->aParam
[5]<=0.0
213 pCube
= (Cube
*)sqlite3_malloc(sizeof(Cube
));
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];
224 p
->pUser
= (void *)pCube
;
225 p
->xDelUser
= cube_context_free
;
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
242 #endif /* SQLITE_ENABLE_RTREE */
244 static int register_cube_geom(
248 Tcl_Obj
*CONST objv
[]
250 #ifndef SQLITE_ENABLE_RTREE
251 UNUSED_PARAMETER(clientData
);
252 UNUSED_PARAMETER(interp
);
253 UNUSED_PARAMETER(objc
);
254 UNUSED_PARAMETER(objv
);
256 extern int getDbPointer(Tcl_Interp
*, const char*, sqlite3
**);
257 extern const char *sqlite3ErrName(int);
262 Tcl_WrongNumArgs(interp
, 1, objv
, "DB");
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 *)sqlite3ErrName(rc
), TCL_STATIC
);
272 static int register_circle_geom(
276 Tcl_Obj
*CONST objv
[]
278 #ifndef SQLITE_ENABLE_RTREE
279 UNUSED_PARAMETER(clientData
);
280 UNUSED_PARAMETER(interp
);
281 UNUSED_PARAMETER(objc
);
282 UNUSED_PARAMETER(objv
);
284 extern int getDbPointer(Tcl_Interp
*, const char*, sqlite3
**);
285 extern const char *sqlite3ErrName(int);
290 Tcl_WrongNumArgs(interp
, 1, objv
, "DB");
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 *)sqlite3ErrName(rc
), TCL_STATIC
);
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);