Changeset: bd71f64d247f for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=bd71f64d247f
Modified Files:
        geom/monetdb5/geom.c
        geom/monetdb5/geom.h
        geom/monetdb5/geom.mal
        geom/monetdb5/geomBulk.c
        geom/sql/40_geom.sql
Branch: sfcgal
Log Message:

Filter join for Contains(geom, x, y, z, SRID). Fix Typo in the error message


diffs (truncated from 771 to 300 lines):

diff --git a/geom/monetdb5/geom.c b/geom/monetdb5/geom.c
--- a/geom/monetdb5/geom.c
+++ b/geom/monetdb5/geom.c
@@ -7441,9 +7441,6 @@ wkbaHEAP(Heap *heap, size_t capacity)
        HEAP_initialize(heap, capacity, 0, (int) sizeof(var_t));
 }
 
-geom_export str wkbContains_point_bat(bat *out, wkb **a, bat *point_x, bat 
*point_y);
-geom_export str wkbContains_point(bit *out, wkb **a, dbl *point_x, dbl 
*point_y);
-
 static inline double
 isLeft(double P0x, double P0y, double P1x, double P1y, double P2x, double P2y)
 {
@@ -7580,8 +7577,10 @@ pnpolyWithHoles(bat *out, int nvert, dbl
                        }
                }
 
-               if (wn)
+               if (wn) {
+                   *cs++ = 0;
                        continue;
+        }
 
                /*If not in any of the holes, check inside the Polygon */
                for (j = 0; j < nvert - 1; j++) {
@@ -7608,53 +7607,100 @@ pnpolyWithHoles(bat *out, int nvert, dbl
        return MAL_SUCCEED;
 }
 
-#define POLY_NUM_VERT 120
-#define POLY_NUM_HOLE 10
-
-str
-wkbContains_point_bat(bat *out, wkb **a, bat *point_x, bat *point_y)
-{
-       double *vert_x, *vert_y, **holes_x = NULL, **holes_y = NULL;
-       int *holes_n = NULL, j;
-       wkb *geom = NULL;
-       str msg = NULL;
-
+static str
+pnpoly_(bit *out, int nvert, dbl *vx, dbl *vy, int nholes, dbl **hx, dbl **hy, 
int *hn, double px, double py)
+{
+    int j = 0, h = 0, wn = 0;
+
+    /*First check the holes */
+    for (h = 0; h < nholes; h++) {
+        int nv = hn[h] - 1;
+        wn = 0;
+        for (j = 0; j < nv; j++) {
+            if (hy[h][j] <= py) {
+                if (hy[h][j + 1] > py)
+                    if (isLeft(hx[h][j], hy[h][j], hx[h][j + 1], hy[h][j + 1], 
px, py) > 0)
+                        ++wn;
+            } else {
+                if (hy[h][j + 1] <= py)
+                    if (isLeft(hx[h][j], hy[h][j], hx[h][j + 1], hy[h][j + 1], 
px, py) < 0)
+                        --wn;
+            }
+        }
+
+        /*It is in one of the holes */
+        if (wn) {
+            break;
+        }
+    }
+
+    if (wn) {
+        *out = 0;
+        return MAL_SUCCEED;
+    }
+
+    /*If not in any of the holes, check inside the Polygon */
+    for (j = 0; j < nvert - 1; j++) {
+        if (vy[j] <= py) {
+            if (vy[j + 1] > py)
+                if (isLeft(vx[j], vy[j], vx[j + 1], vy[j + 1], px, py) > 0)
+                    ++wn;
+        } else {
+            if (vy[j + 1] <= py)
+                if (isLeft(vx[j], vy[j], vx[j + 1], vy[j + 1], px, py) < 0)
+                    --wn;
+        }
+    }
+    *out = wn & 1;
+
+    return MAL_SUCCEED;
+}
+
+
+/*TODO: better conversion from WKB*/
+/*TODO: Check if the allocations are working*/
+static str
+getVerts(wkb *geom, vertexWKB **res)
+{
        str err = NULL;
        str geom_str = NULL;
        char *str2, *token, *subtoken;
        char *saveptr1 = NULL, *saveptr2 = NULL;
-       int nvert = 0, nholes = 0;
-
-       geom = (wkb *) *a;
+    vertexWKB *verts = NULL;
+
+    /*Check if it is a Polygon*/
 
        if ((err = wkbAsText(&geom_str, &geom, NULL)) != MAL_SUCCEED) {
                return err;
        }
+
+    verts = (vertexWKB*) GDKzalloc(sizeof(vertexWKB));
+
        geom_str = strchr(geom_str, '(');
        geom_str += 2;
 
        /*Lets get the polygon */
        token = strtok_r(geom_str, ")", &saveptr1);
-       vert_x = GDKmalloc(POLY_NUM_VERT * sizeof(double));
-       vert_y = GDKmalloc(POLY_NUM_VERT * sizeof(double));
+       verts->vert_x = GDKmalloc(POLY_NUM_VERT * sizeof(double));
+       verts->vert_y = GDKmalloc(POLY_NUM_VERT * sizeof(double));
 
        for (str2 = token;; str2 = NULL) {
                subtoken = strtok_r(str2, ",", &saveptr2);
                if (subtoken == NULL)
                        break;
-               sscanf(subtoken, "%lf %lf", &vert_x[nvert], &vert_y[nvert]);
-               nvert++;
-               if ((nvert % POLY_NUM_VERT) == 0) {
-                       vert_x = GDKrealloc(vert_x, nvert * 2 * sizeof(double));
-                       vert_y = GDKrealloc(vert_y, nvert * 2 * sizeof(double));
+               sscanf(subtoken, "%lf %lf", &(verts->vert_x[verts->nvert]), 
&(verts->vert_y[verts->nvert]));
+               verts->nvert++;
+               if ((verts->nvert % POLY_NUM_VERT) == 0) {
+                       verts->vert_x = GDKrealloc(verts->vert_x, verts->nvert 
* 2 * sizeof(double));
+                       verts->vert_y = GDKrealloc(verts->vert_y, verts->nvert 
* 2 * sizeof(double));
                }
        }
 
        token = strtok_r(NULL, ")", &saveptr1);
        if (token) {
-               holes_x = GDKzalloc(POLY_NUM_HOLE * sizeof(double *));
-               holes_y = GDKzalloc(POLY_NUM_HOLE * sizeof(double *));
-               holes_n = GDKzalloc(POLY_NUM_HOLE * sizeof(double *));
+               verts->holes_x = GDKzalloc(POLY_NUM_HOLE * sizeof(double *));
+               verts->holes_y = GDKzalloc(POLY_NUM_HOLE * sizeof(double *));
+               verts->holes_n = GDKzalloc(POLY_NUM_HOLE * sizeof(int *));
        }
        /*Lets get all the holes */
        while (token) {
@@ -7664,61 +7710,95 @@ wkbContains_point_bat(bat *out, wkb **a,
                        break;
                token++;
 
-               if (!holes_x[nholes])
-                       holes_x[nholes] = GDKzalloc(POLY_NUM_VERT * 
sizeof(double));
-               if (!holes_y[nholes])
-                       holes_y[nholes] = GDKzalloc(POLY_NUM_VERT * 
sizeof(double));
+               if (!verts->holes_x[verts->nholes])
+                       verts->holes_x[verts->nholes] = GDKzalloc(POLY_NUM_VERT 
* sizeof(double));
+               if (!verts->holes_y[verts->nholes])
+                       verts->holes_y[verts->nholes] = GDKzalloc(POLY_NUM_VERT 
* sizeof(double));
 
                for (str2 = token;; str2 = NULL) {
                        subtoken = strtok_r(str2, ",", &saveptr2);
                        if (subtoken == NULL)
                                break;
-                       sscanf(subtoken, "%lf %lf", &holes_x[nholes][nhole], 
&holes_y[nholes][nhole]);
+                       sscanf(subtoken, "%lf %lf", 
&(verts->holes_x[verts->nholes][nhole]), 
&(verts->holes_y[verts->nholes][nhole]));
                        nhole++;
                        if ((nhole % POLY_NUM_VERT) == 0) {
-                               holes_x[nholes] = GDKrealloc(holes_x[nholes], 
nhole * 2 * sizeof(double));
-                               holes_y[nholes] = GDKrealloc(holes_y[nholes], 
nhole * 2 * sizeof(double));
+                               verts->holes_x[verts->nholes] = 
GDKrealloc(verts->holes_x[verts->nholes], nhole * 2 * sizeof(double));
+                               verts->holes_y[verts->nholes] = 
GDKrealloc(verts->holes_y[verts->nholes], nhole * 2 * sizeof(double));
                        }
                }
 
-               holes_n[nholes] = nhole;
-               nholes++;
-               if ((nholes % POLY_NUM_HOLE) == 0) {
-                       holes_x = GDKrealloc(holes_x, nholes * 2 * 
sizeof(double *));
-                       holes_y = GDKrealloc(holes_y, nholes * 2 * 
sizeof(double *));
-                       holes_n = GDKrealloc(holes_n, nholes * 2 * sizeof(int));
+               verts->holes_n[verts->nholes] = nhole;
+               verts->nholes++;
+               if ((verts->nholes % POLY_NUM_HOLE) == 0) {
+                       verts->holes_x = GDKrealloc(verts->holes_x, 
verts->nholes * 2 * sizeof(double *));
+                       verts->holes_y = GDKrealloc(verts->holes_y, 
verts->nholes * 2 * sizeof(double *));
+                       verts->holes_n = GDKrealloc(verts->holes_n, 
verts->nholes * 2 * sizeof(int));
                }
                token = strtok_r(NULL, ")", &saveptr1);
        }
 
-       if (nholes)
-               msg = pnpolyWithHoles(out, (int) nvert, vert_x, vert_y, nholes, 
holes_x, holes_y, holes_n, point_x, point_y);
-       else {
-               msg = pnpoly(out, (int) nvert, vert_x, vert_y, point_x, 
point_y);
-       }
-
-       GDKfree(vert_x);
-       GDKfree(vert_y);
-       if (holes_x && holes_y && holes_n) {
-               for (j = 0; j < nholes; j++) {
-                       GDKfree(holes_x[j]);
-                       GDKfree(holes_y[j]);
+    *res = verts;
+    return MAL_SUCCEED;
+}
+
+static void
+freeVerts(vertexWKB *verts)
+{
+    int j = 0;
+
+       GDKfree(verts->vert_x);
+       GDKfree(verts->vert_y);
+       if (verts->holes_x && verts->holes_y && verts->holes_n) {
+               for (j = 0; j < verts->nholes; j++) {
+                       GDKfree(verts->holes_x[j]);
+                       GDKfree(verts->holes_y[j]);
                }
-               GDKfree(holes_x);
-               GDKfree(holes_y);
-               GDKfree(holes_n);
-       }
+               GDKfree(verts->holes_x);
+               GDKfree(verts->holes_y);
+               GDKfree(verts->holes_n);
+       }
+
+    GDKfree(verts);
+}
+
+str
+wkbContains_point_bat(bat *out, wkb **a, bat *point_x, bat *point_y)
+{
+    vertexWKB *verts = NULL;
+       wkb *geom = NULL;
+       str msg = NULL;
+
+       geom = (wkb *) *a;
+    if ((msg = getVerts(geom, &verts)) != MAL_SUCCEED) {
+        return msg;
+    }
+         
+       msg = pnpolyWithHoles(out, (int) verts->nvert, verts->vert_x, 
verts->vert_y, verts->nholes, verts->holes_x, verts->holes_y, verts->holes_n, 
point_x, point_y);
+
+    if (verts)
+        freeVerts(verts);
 
        return msg;
 }
 
 str
-wkbContains_point(bit *out, wkb **a, dbl *point_x, dbl *point_y)
-{
-       (void) a;
-       (void) point_x;
-       (void) point_y;
-       *out = TRUE;
+wkbContainsXYZ(bit *out, wkb **a, dbl *px, dbl *py, dbl *pz, int *srid)
+{
+    vertexWKB *verts = NULL;
+       wkb *geom = NULL;
+       str msg = NULL;
+       (void) pz;
+
+       geom = (wkb *) *a;
+    if ((msg = getVerts(geom, &verts)) != MAL_SUCCEED) {
+        return msg;
+    }
+         
+       msg = pnpoly_(out, (int) verts->nvert, verts->vert_x, verts->vert_y, 
verts->nholes, verts->holes_x, verts->holes_y, verts->holes_n, *px, *py);
+
+    if (verts)
+        freeVerts(verts);
+
        return MAL_SUCCEED;
 }
 
@@ -8089,24 +8169,24 @@ IntersectsXYZsubjoin_intern(bat *lres, b
     int *rSRIDs = NULL;
 
        if( (bl= BATdescriptor(*lid)) == NULL )
-               throw(MAL, "algebra.instersects", RUNTIME_OBJECT_MISSING);
+               throw(MAL, "algebra.intersects", RUNTIME_OBJECT_MISSING);
 
        if( (bx= BATdescriptor(*xid)) == NULL ){
                BBPunfix(*lid);
-               throw(MAL, "algebra.instersects", RUNTIME_OBJECT_MISSING);
+               throw(MAL, "algebra.intersects", RUNTIME_OBJECT_MISSING);
        }
 
        if( (by= BATdescriptor(*yid)) == NULL ){
                BBPunfix(*lid);
                BBPunfix(*xid);
-               throw(MAL, "algebra.instersects", RUNTIME_OBJECT_MISSING);
+               throw(MAL, "algebra.intersects", RUNTIME_OBJECT_MISSING);
        }
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to