test_rtree.c 8.7 KB

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