Changeset: eae2ca44ae56 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=eae2ca44ae56
Modified Files:
        sql/backends/monet5/UDF/ssdb.c
Branch: ssdb
Log Message:

Intersects function.


diffs (149 lines):

diff --git a/sql/backends/monet5/UDF/ssdb.c b/sql/backends/monet5/UDF/ssdb.c
--- a/sql/backends/monet5/UDF/ssdb.c
+++ b/sql/backends/monet5/UDF/ssdb.c
@@ -71,12 +71,142 @@ _append_bat(mvc *sql, sql_table *t, char
 str
 SSDBintersects(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
 { /* intersects(tin:str, tout:str, xstart:int, ystart:int, xlen:int, 
ylen:int):void */
-       (void) cntxt;
+       /*(void) cntxt;
        (void) mb;
        (void) stk;
-       (void) pci;
+       (void) pci;*/
 
-       return MAL_SUCCEED;
+       mvc *sql = NULL;
+       sql_table *tin = NULL, *tout = NULL;
+       str msg = getSQLContext(cntxt, mb, &sql, NULL);
+       str tin_name  = *(str *) getArgReference(stk, pci, pci->retc + 0);
+       str tout_name = *(str *) getArgReference(stk, pci, pci->retc + 1);
+       int xstart = *(int *) getArgReference(stk, pci, pci->retc + 2); 
+       int ystart = *(int *) getArgReference(stk, pci, pci->retc + 3);
+       int xlen = *(int *) getArgReference(stk, pci, pci->retc + 4);
+       int ylen = *(int *) getArgReference(stk, pci, pci->retc + 5);   
+
+
+       int linex[] = {xstart,xstart,xstart+xlen,xstart+xlen,xstart};
+        int liney[] = {ystart,ystart+ylen,ystart+ylen,ystart,ystart};
+
+       BAT *tin_obsid = NULL, *tin_x = NULL, *tin_y = NULL;
+       BAT *tout_obsid = NULL;
+
+       BUN idx = 0, nr_points = 0, nr_obs=0;
+       unsigned int *obsid = NULL;
+       int *tin_obsid_t = NULL, *tin_x_t = NULL, *tin_y_t = NULL;
+       int *tout_obsid_t = NULL;
+       int j = 0, cur_obs = -1, inter_obsid = 0;
+
+       if (msg)
+               return msg;
+       if (pci->argc - pci->retc != 6)
+               return sql_message("INTERSECTS(): 6 parameters expected, got 
%d", pci->argc - pci->retc);
+       if (!tin_name)
+               return sql_message("INTERSECTS(): missing name of the input 
table");
+       if (!tout_name)
+               return sql_message("INTERSECTS(): missing name of the output 
table");
+
+       if(!(tin = _bind_table(sql, "tmp", tin_name)))
+               return sql_message("42S02!INTERSECTS(): no such table '%s'", 
tin_name);
+       if(!(tout = _bind_table(sql, NULL, tout_name)))
+               return sql_message("42S02!INTERSECTS(): no such table '%s'", 
tout_name);
+
+       /* get the BATs of all columns of the input table 'tin' */
+       /* RD_INS means we want the insert BATs, since all input tables are
+        *   declared as TEMP */
+       if(!(tin_obsid = _bind_bat(sql, tin, "obsid", TYPE_int, RD_INS))) {
+               msg = sql_message("42S22!INTERSECTS(): no such column '%s.%s'", 
tin_name, "obsid");
+               goto CLEANUP_RETURN;
+       }
+       if(!(tin_x = _bind_bat(sql, tin, "x", TYPE_int, RD_INS))) {
+               msg = sql_message("42S22!INTERSECTS(): no such column '%s.%s'", 
tin_name, "x");
+               goto CLEANUP_RETURN;
+       }
+       if(!(tin_y = _bind_bat(sql, tin, "y", TYPE_int, RD_INS))) {
+               msg = sql_message("42S22!INTERSECTS(): no such column '%s.%s'", 
tin_name, "y");
+               goto CLEANUP_RETURN;
+       }
+
+       tin_obsid_t = (int*)Tloc(tin_obsid, BUNfirst(tin_obsid));
+       tin_x_t = (int*)Tloc(tin_x, BUNfirst(tin_x));
+       tin_y_t = (int*)Tloc(tin_y, BUNfirst(tin_y));
+
+       nr_points = BATcount(tin_obsid);
+       if(!(obsid = GDKmalloc(nr_points * sizeof(unsigned int)))) {
+               msg = sql_message("INTERSECTS(): GDKmalloc 'obsid' failed");
+               goto CLEANUP_RETURN;
+       }
+
+       /* Create BATs for all columns of the output table 'tout_obs' */
+       nr_obs=tin_obsid_t[nr_points-1]+1;
+        tout_obsid = BATnew(TYPE_void, TYPE_int, nr_obs);
+        /* pointers directly to the BATs' tails */
+        tout_obsid_t = (int*)Tloc(tout_obsid, BUNfirst(tout_obsid));
+
+
+       /* First pass over all input pixels, to group the pixels into
+        * observations and assign them a unique obsid */
+       for(j=0;j<4;j++)
+       {
+               for (idx = 0; idx < nr_points; idx++) 
+               {
+                       int x,y,x_next,y_next, obs,obs_next;
+                       float D,l,m;
+                       obs = tin_obsid_t[idx];
+                       obs_next = tin_obsid_t[idx+1];
+                       if (obs==obs_next && cur_obs!=obs)
+                       {
+                               x = tin_x_t[idx];
+                               y = tin_y_t[idx];
+                               x_next = tin_x_t[idx+1];
+                               y_next = tin_y_t[idx+1];
+
+                               
D=((linex[j]-linex[j+1])*(y_next-y))-((liney[j]-liney[j+1])*(x_next-x));
+
+                       /*Find l,m using Cramer's rule*/
+                               if(D!=0)
+                               {
+                                       
l=(1/D)*(((x_next-linex[j+1])*(y_next-y))-((y_next-liney[j+1])*(x_next-x)));
+                                       
m=(1/D)*(((linex[j]-linex[j+1])*(y_next-liney[j+1]))-((liney[j]-liney[j+1])*(x_next-linex[j+1])));
+
+                                       if (l>=0 && l<=1 && m>=0 && m<=1)
+                                       {
+                                               //printf("YES: Line from 
(%d,%d) to (%d,%d) intersects with polygon on edge from (%d,%d) to 
(%d,%d)!!!\n",linex[j],liney[j],linex[j+1],liney[j+1],x,y,x_next,y_next);
+                                               cur_obs=obs;
+                                               tout_obsid_t[inter_obsid] = 
cur_obs;
+                                               inter_obsid++;
+                                       }
+                               }
+                       }
+               }
+       }
+
+
+       /* Set proper properties for output BATs */
+       BATsetcount(tout_obsid, inter_obsid+1);
+       BATseqbase(tout_obsid, 0);
+       tout_obsid->tsorted = 1;
+       tout_obsid->trevsorted = 0;
+       tout_obsid->T->nil = 0;
+       tout_obsid->T->nonil = 1;
+       BATkey(BATmirror(tout_obsid), 1);
+
+       /* Finally, append the result BATs to the result table */
+       msg = _append_bat(sql, tout, "obsid", tout_obsid);
+       
+
+
+
+CLEANUP_RETURN:
+       if(tin_obsid) BBPunfix(tin_obsid->batCacheid);
+       if(tin_x) BBPunfix(tin_x->batCacheid);
+       if(tin_y) BBPunfix(tin_y->batCacheid);
+       if(tout_obsid) BBPunfix(tout_obsid->batCacheid);
+       if(obsid) GDKfree(obsid);
+
+       return msg ? msg : MAL_SUCCEED;
 }
 
 str
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
http://mail.monetdb.org/mailman/listinfo/checkin-list

Reply via email to