#include "sqlite3.h"
#if defined(INCLUDE_SQLITE_TCL_H)
# include "sqlite_tcl.h"
#else
# include "tcl.h"
#endif
#include "sqliteInt.h"
#ifdef SQLITE_ENABLE_RTREE
typedef struct Circle Circle;
struct Circle {
struct Box {
double xmin;
double xmax;
double ymin;
double ymax;
} aBox[2];
double centerx;
double centery;
double radius;
double mxArea;
int eScoreType;
};
static void circle_del(void *p){
sqlite3_free(p);
}
static int circle_geom(
sqlite3_rtree_geometry *p,
int nCoord,
sqlite3_rtree_dbl *aCoord,
int *pRes
){
int i;
Circle *pCircle;
double xmin, xmax;
double ymin, ymax;
xmin = aCoord[0];
xmax = aCoord[1];
ymin = aCoord[2];
ymax = aCoord[3];
pCircle = (Circle *)p->pUser;
if( pCircle==0 ){
if( nCoord!=4 ) return SQLITE_ERROR;
if( p->nParam!=3 || p->aParam[2]<0.0 ) return SQLITE_ERROR;
pCircle = (Circle *)(p->pUser = sqlite3_malloc(sizeof(Circle)));
if( !pCircle ) return SQLITE_NOMEM;
p->xDelUser = circle_del;
pCircle->centerx = p->aParam[0];
pCircle->centery = p->aParam[1];
pCircle->radius = p->aParam[2];
pCircle->aBox[0].xmin = pCircle->centerx;
pCircle->aBox[0].xmax = pCircle->centerx;
pCircle->aBox[0].ymin = pCircle->centery + pCircle->radius;
pCircle->aBox[0].ymax = pCircle->centery - pCircle->radius;
pCircle->aBox[1].xmin = pCircle->centerx + pCircle->radius;
pCircle->aBox[1].xmax = pCircle->centerx - pCircle->radius;
pCircle->aBox[1].ymin = pCircle->centery;
pCircle->aBox[1].ymax = pCircle->centery;
pCircle->mxArea = (xmax - xmin)*(ymax - ymin) + 1.0;
}
for(i=0; i<4; i++){
double x = (i&0x01) ? xmax : xmin;
double y = (i&0x02) ? ymax : ymin;
double d2;
d2 = (x-pCircle->centerx)*(x-pCircle->centerx);
d2 += (y-pCircle->centery)*(y-pCircle->centery);
if( d2<(pCircle->radius*pCircle->radius) ){
*pRes = 1;
return SQLITE_OK;
}
}
for(i=0; i<2; i++){
if( xmin<=pCircle->aBox[i].xmin
&& xmax>=pCircle->aBox[i].xmax
&& ymin<=pCircle->aBox[i].ymin
&& ymax>=pCircle->aBox[i].ymax
){
*pRes = 1;
return SQLITE_OK;
}
}
*pRes = 0;
return SQLITE_OK;
}
static int circle_query_func(sqlite3_rtree_query_info *p){
int i;
Circle *pCircle;
double xmin, xmax;
double ymin, ymax;
int nWithin = 0;
xmin = p->aCoord[0];
xmax = p->aCoord[1];
ymin = p->aCoord[2];
ymax = p->aCoord[3];
pCircle = (Circle *)p->pUser;
if( pCircle==0 ){
if( p->nCoord!=4 ) return SQLITE_ERROR;
if( p->nParam!=4 && p->nParam!=1 ) return SQLITE_ERROR;
pCircle = (Circle *)(p->pUser = sqlite3_malloc(sizeof(Circle)));
if( !pCircle ) return SQLITE_NOMEM;
p->xDelUser = circle_del;
if( p->nParam==4 ){
pCircle->centerx = p->aParam[0];
pCircle->centery = p->aParam[1];
pCircle->radius = p->aParam[2];
pCircle->eScoreType = (int)p->aParam[3];
}else{
const char *z = (const char*)sqlite3_value_text(p->apSqlParam[0]);
pCircle->centerx = 0.0;
pCircle->centery = 0.0;
pCircle->radius = 0.0;
pCircle->eScoreType = 0;
while( z && z[0] ){
if( z[0]=='r' && z[1]==':' ){
pCircle->radius = atof(&z[2]);
}else if( z[0]=='x' && z[1]==':' ){
pCircle->centerx = atof(&z[2]);
}else if( z[0]=='y' && z[1]==':' ){
pCircle->centery = atof(&z[2]);
}else if( z[0]=='e' && z[1]==':' ){
pCircle->eScoreType = (int)atof(&z[2]);
}else if( z[0]==' ' ){
z++;
continue;
}
while( z[0]!=0 && z[0]!=' ' ) z++;
while( z[0]==' ' ) z++;
}
}
if( pCircle->radius<0.0 ){
sqlite3_free(pCircle);
return SQLITE_NOMEM;
}
pCircle->aBox[0].xmin = pCircle->centerx;
pCircle->aBox[0].xmax = pCircle->centerx;
pCircle->aBox[0].ymin = pCircle->centery + pCircle->radius;
pCircle->aBox[0].ymax = pCircle->centery - pCircle->radius;
pCircle->aBox[1].xmin = pCircle->centerx + pCircle->radius;
pCircle->aBox[1].xmax = pCircle->centerx - pCircle->radius;
pCircle->aBox[1].ymin = pCircle->centery;
pCircle->aBox[1].ymax = pCircle->centery;
pCircle->mxArea = 200.0*200.0;
}
for(i=0; i<4; i++){
double x = (i&0x01) ? xmax : xmin;
double y = (i&0x02) ? ymax : ymin;
double d2;
d2 = (x-pCircle->centerx)*(x-pCircle->centerx);
d2 += (y-pCircle->centery)*(y-pCircle->centery);
if( d2<(pCircle->radius*pCircle->radius) ) nWithin++;
}
if( nWithin==0 ){
for(i=0; i<2; i++){
if( xmin<=pCircle->aBox[i].xmin
&& xmax>=pCircle->aBox[i].xmax
&& ymin<=pCircle->aBox[i].ymin
&& ymax>=pCircle->aBox[i].ymax
){
nWithin = 1;
break;
}
}
}
if( pCircle->eScoreType==1 ){
p->rScore = p->iLevel;
}else if( pCircle->eScoreType==2 ){
p->rScore = 100 - p->iLevel;
}else if( pCircle->eScoreType==3 ){
if( p->iLevel==1 ){
p->rScore = 1.0 - (xmax-xmin)*(ymax-ymin)/pCircle->mxArea;
if( p->rScore<0.01 ) p->rScore = 0.01;
}else{
p->rScore = 0.0;
}
}else if( pCircle->eScoreType==4 ){
p->rScore = p->iLevel;
if( p->iRowid&1 ) nWithin = 0;
}else{
p->rScore = 100 - p->iLevel;
if( p->iRowid&1 ) nWithin = 0;
}
if( nWithin==0 ){
p->eWithin = NOT_WITHIN;
}else if( nWithin>=4 ){
p->eWithin = FULLY_WITHIN;
}else{
p->eWithin = PARTLY_WITHIN;
}
return SQLITE_OK;
}
static int bfs_query_func(sqlite3_rtree_query_info *p){
double x0,x1,y0,y1;
double bx0,bx1,by0,by1;
if( p->nParam!=4 ) return SQLITE_ERROR;
x0 = p->aCoord[0];
x1 = p->aCoord[1];
y0 = p->aCoord[2];
y1 = p->aCoord[3];
bx0 = p->aParam[0];
bx1 = p->aParam[1];
by0 = p->aParam[2];
by1 = p->aParam[3];
p->rScore = 100 - p->iLevel;
if( p->eParentWithin==FULLY_WITHIN ){
p->eWithin = FULLY_WITHIN;
}else if( x0>=bx0 && x1<=bx1 && y0>=by0 && y1<=by1 ){
p->eWithin = FULLY_WITHIN;
}else if( x1>=bx0 && x0<=bx1 && y1>=by0 && y0<=by1 ){
p->eWithin = PARTLY_WITHIN;
}else{
p->eWithin = NOT_WITHIN;
}
return SQLITE_OK;
}
#include <assert.h>
#if defined(INCLUDE_SQLITE_TCL_H)
# include "sqlite_tcl.h"
#else
# include "tcl.h"
#endif
typedef struct Cube Cube;
struct Cube {
double x;
double y;
double z;
double width;
double height;
double depth;
};
static void cube_context_free(void *p){
sqlite3_free(p);
}
static int gHere = 42;
static int cube_geom(
sqlite3_rtree_geometry *p,
int nCoord,
sqlite3_rtree_dbl *aCoord,
int *piRes
){
Cube *pCube = (Cube *)p->pUser;
assert( p->pContext==(void *)&gHere );
if( pCube==0 ){
if( p->nParam!=6 || nCoord!=6
|| p->aParam[3]<=0.0 || p->aParam[4]<=0.0 || p->aParam[5]<=0.0
){
return SQLITE_ERROR;
}
pCube = (Cube *)sqlite3_malloc(sizeof(Cube));
if( !pCube ){
return SQLITE_NOMEM;
}
pCube->x = p->aParam[0];
pCube->y = p->aParam[1];
pCube->z = p->aParam[2];
pCube->width = p->aParam[3];
pCube->height = p->aParam[4];
pCube->depth = p->aParam[5];
p->pUser = (void *)pCube;
p->xDelUser = cube_context_free;
}
assert( nCoord==6 );
*piRes = 0;
if( aCoord[0]<=(pCube->x+pCube->width)
&& aCoord[1]>=pCube->x
&& aCoord[2]<=(pCube->y+pCube->height)
&& aCoord[3]>=pCube->y
&& aCoord[4]<=(pCube->z+pCube->depth)
&& aCoord[5]>=pCube->z
){
*piRes = 1;
}
return SQLITE_OK;
}
#endif
static int SQLITE_TCLAPI register_cube_geom(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
#ifndef SQLITE_ENABLE_RTREE
UNUSED_PARAMETER(clientData);
UNUSED_PARAMETER(interp);
UNUSED_PARAMETER(objc);
UNUSED_PARAMETER(objv);
#else
extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**);
extern const char *sqlite3ErrName(int);
sqlite3 *db;
int rc;
if( objc!=2 ){
Tcl_WrongNumArgs(interp, 1, objv, "DB");
return TCL_ERROR;
}
if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
rc = sqlite3_rtree_geometry_callback(db, "cube", cube_geom, (void *)&gHere);
Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
#endif
return TCL_OK;
}
static int SQLITE_TCLAPI register_circle_geom(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
#ifndef SQLITE_ENABLE_RTREE
UNUSED_PARAMETER(clientData);
UNUSED_PARAMETER(interp);
UNUSED_PARAMETER(objc);
UNUSED_PARAMETER(objv);
#else
extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**);
extern const char *sqlite3ErrName(int);
sqlite3 *db;
int rc;
if( objc!=2 ){
Tcl_WrongNumArgs(interp, 1, objv, "DB");
return TCL_ERROR;
}
if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
rc = sqlite3_rtree_geometry_callback(db, "circle", circle_geom, 0);
if( rc==SQLITE_OK ){
rc = sqlite3_rtree_query_callback(db, "Qcircle",
circle_query_func, 0, 0);
}
if( rc==SQLITE_OK ){
rc = sqlite3_rtree_query_callback(db, "breadthfirstsearch",
bfs_query_func, 0, 0);
}
Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
#endif
return TCL_OK;
}
int Sqlitetestrtree_Init(Tcl_Interp *interp){
Tcl_CreateObjCommand(interp, "register_cube_geom", register_cube_geom, 0, 0);
Tcl_CreateObjCommand(interp, "register_circle_geom",register_circle_geom,0,0);
return TCL_OK;
}