diff --git a/DESCRIPTION b/DESCRIPTION index 3bade2d..9e5c921 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: vaster Title: Tools for Raster Grid Logic -Version: 0.0.2.9001 +Version: 0.0.2.9002 Authors@R: c(person("Michael", "Sumner", email = "mdsumner@gmail.com", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-2471-7511"))) Description: Provides raster grid logic, the grid operations that don't require access to materialized data, i.e. most of them. Grids are arrays with dimension and extent, and many operations are functions of just the dimension 'nrows', 'ncols' or diff --git a/src/coordinates.c b/src/coordinates.c index a1494cb..b945a72 100644 --- a/src/coordinates.c +++ b/src/coordinates.c @@ -8,13 +8,19 @@ SEXP bin_from_float(SEXP bins, SEXP range, SEXP coord) { SEXP out; out = PROTECT(Rf_allocVector(REALSXP, nn)); + // save the index, as per https://github.com/hadley/r-internals/blob/master/vectors.md#get-and-set-values + double* rout = REAL(out); + double* rcoord = REAL(coord); + double cmax = REAL(range)[1]; + double cmin = REAL(range)[0]; + double rbin = INTEGER(bins)[0]; for (int i = 0; i < nn; i++) { - if (REAL(coord)[i] == REAL(range)[1]) { - REAL(out)[i] = INTEGER(bins)[0] - 1; - } else if ((REAL(coord)[i] > REAL(range)[1]) | (REAL(coord)[i] < REAL(range)[0])) { - REAL(out)[i] = R_NaReal; + if (rcoord[i] == cmax) { + rout[i] = rbin - 1; + } else if ((rcoord[i] > cmax) | (rcoord[i] < cmin)) { + rout[i] = R_NaReal; } else { - REAL(out)[i] = trunc((REAL(range)[1] - REAL(coord)[i])/scl); + rout[i] = trunc((cmax - rcoord[i])/scl); } } UNPROTECT(1);