/* Copyright 2004 by Roger S. Bivand. */ #include "spdep.h" SEXP polypoly(SEXP p1, SEXP n01, SEXP p2, SEXP n02, SEXP snap) { int n1=INTEGER_POINTER(n01)[0], n2=INTEGER_POINTER(n02)[0], pc=0; int i, j, k=0; double sn=NUMERIC_POINTER(snap)[0], dist; double x1, x2, y1, y2, xd, yd; SEXP ans; PROTECT(ans = NEW_INTEGER(1)); pc++; for (i=0; (i < n1) && (k < 2); i++) { x1 = NUMERIC_POINTER(p1)[i]; y1 = NUMERIC_POINTER(p1)[n1 + i]; for (j=0; (j < n2) && (k < 2); j++) { x2 = NUMERIC_POINTER(p2)[j]; y2 = NUMERIC_POINTER(p2)[n2 + j]; /* dist = pythag((x1-x2), (y1-y2)); if (dist < sn) k++; if (k > 1) break;*/ /* following lines Micah Altman 2010 */ xd = x1-x2; if (fabs(xd)>sn) { continue; } yd = y1-y2; if (fabs(yd)>sn) { continue; } dist = hypot(xd, yd); if (dist <= sn) { k++; } } } INTEGER_POINTER(ans)[0] = k; UNPROTECT(pc); /* ans */ return(ans); } /* function by Micah Altman */ SEXP spOverlap(SEXP bbbi, SEXP bbbj) { int pc=0,overlap=1; double bbi[4], bbj[4]; SEXP ans; PROTECT(ans = NEW_INTEGER(1)); pc++; bbi[0] = NUMERIC_POINTER(bbbi)[0]; bbi[1] = NUMERIC_POINTER(bbbi)[1]; bbi[2] = NUMERIC_POINTER(bbbi)[2]; bbi[3] = NUMERIC_POINTER(bbbi)[3]; bbj[0] = NUMERIC_POINTER(bbbj)[0]; bbj[1] = NUMERIC_POINTER(bbbj)[1]; bbj[2] = NUMERIC_POINTER(bbbj)[2]; bbj[3] = NUMERIC_POINTER(bbbj)[3]; if ((bbi[0]>bbj[2]) | (bbi[1]>bbj[3]) | (bbi[2] 0) { INTEGER_POINTER(khit)[0] = 0; INTEGER_POINTER(nrsj)[0] = INTEGER_POINTER(nrs)[jj]; if (INTEGER_POINTER(nrsi)[0]*INTEGER_POINTER(nrsj)[0] > 0){ khit = polypoly(VECTOR_ELT(pl, i), nrsi, VECTOR_ELT(pl, jj), nrsj, dsnap); } if (INTEGER_POINTER(khit)[0] > crit) { card[i]++; card[jj]++; is[ii] = i; jjs[ii] = jj; ii++; if (ii == uBound) error("memory error, scale problem"); } } } } PROTECT(ans = NEW_LIST(nn)); pc++; for (i=0; i 1) { for (j=0; jbbj3) || (bbi2>bbj4) || (bbi3sn) { continue; } yd = y1-y2; if (fabs(yd)>sn) { continue; } dist = hypot(xd, yd); if (dist <= sn) { k++; } } } return(k); } SEXP poly_loop2(SEXP n, SEXP i_findInBox, SEXP bb, SEXP pl, SEXP nrs, SEXP dsnap, SEXP criterion, SEXP nfIBB) { int nn = INTEGER_POINTER(n)[0]; int crit = INTEGER_POINTER(criterion)[0]; /* int Scale = INTEGER_POINTER(scale)[0];*/ int uBound = (int) INTEGER_POINTER(nfIBB)[0]*2; int i, j, jj, li, pc = 0; int ii = 0; int *card, *icard, *is, *jjs, *NRS, *cNRS; double *bb1, *bb2, *bb3, *bb4, *plx, *ply; double Dsnap = NUMERIC_POINTER(dsnap)[0]; // struct bbcontainer *bbs; SEXP ans; int jhit, khit, nrsi, nrsj; int xx, yy, zz, ww; card = (int *) R_alloc((size_t) nn, sizeof(int)); icard = (int *) R_alloc((size_t) nn, sizeof(int)); is = (int *) R_alloc((size_t) uBound, sizeof(int)); jjs = (int *) R_alloc((size_t) uBound, sizeof(int)); bb1 = (double *) R_alloc((size_t) nn, sizeof(double)); bb2 = (double *) R_alloc((size_t) nn, sizeof(double)); bb3 = (double *) R_alloc((size_t) nn, sizeof(double)); bb4 = (double *) R_alloc((size_t) nn, sizeof(double)); NRS = (int *) R_alloc((size_t) nn, sizeof(int)); cNRS = (int *) R_alloc((size_t) nn, sizeof(int)); for (i=0, li=0; i 0) { khit = 0; nrsj = NRS[jj]; if (nrsi > 0 && nrsj > 0){ khit = polypolyC(&plx[cNRS[i]], &ply[cNRS[i]], nrsi, &plx[cNRS[jj]], &ply[cNRS[jj]], nrsj, Dsnap, crit+1L); } if (khit > crit) { card[i]++; card[jj]++; is[ii] = i; jjs[ii] = jj; ii++; /* if (ii == uBound) error("memory error, scale problem");*/ } } } } PROTECT(ans = NEW_LIST(nn)); pc++; for (i=0; i 1) { for (j=0; j