Skip to content

Commit

Permalink
better iteration style for get values
Browse files Browse the repository at this point in the history
  • Loading branch information
mdsumner committed May 12, 2024
1 parent efe0cea commit d469581
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]", 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
Expand Down
16 changes: 11 additions & 5 deletions src/coordinates.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit d469581

Please sign in to comment.