Skip to content

Commit

Permalink
for #222
Browse files Browse the repository at this point in the history
  • Loading branch information
rhijmans committed Dec 7, 2021
1 parent de8faaf commit d49f975
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
8 changes: 6 additions & 2 deletions src/focal_fun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ std::vector<double> do_focal_fun(std::vector<double> d, Rcpp::NumericMatrix w, s
size_t q = 0;
for (int j = -wr; j <= wr; j++) {
for (int k = -wc; k <= wc; k++) {
x[q] = d[j * ncol + k + i] * w[q];
if (!std::isnan(w[q])) {
x[q] = d[j * ncol + k + i] * w[q];
}
q++;
}
}
Expand Down Expand Up @@ -80,7 +82,9 @@ std::vector<double> do_focal_fun(std::vector<double> d, Rcpp::NumericMatrix w, s
size_t q = 0;
for (int j = -wr; j <= wr; j++) {
for (int k = -wc; k <= wc; k++) {
x[q] = d[j * ncol + k + i] * w[q];
if (!std::isnan(w[q])) {
x[q] = d[j * ncol + k + i] * w[q];
}
q++;
}
}
Expand Down
36 changes: 23 additions & 13 deletions src/focal_sum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@ std::vector<double> do_focal_sum(std::vector<double> d, Rcpp::NumericMatrix w, s
size_t p = 0;
for (int j = -wr; j <= wr; j++) {
for (int k = -wc; k <= wc; k++) {
double a = d[j * ncol + k + i];
if ( !std::isnan(a) ) {
val[i] += a * w[q];
p++;
if (!std::isnan(w[q])) {
double a = d[j * ncol + k + i];
if ( !std::isnan(a) ) {
val[i] += a * w[q];
p++;
}
q++;
}
q++;
}
}
if (p==0) {
Expand Down Expand Up @@ -85,10 +87,12 @@ std::vector<double> do_focal_sum(std::vector<double> d, Rcpp::NumericMatrix w, s
val[i] = 0;
for (int j = -wr; j <= wr; j++) {
for (int k = -wc; k <= wc; k++) {
double a = d[j * ncol + k + i];
if ( !std::isnan(a) ) {
val[i] += a * w[q];
p++;
if (!std::isnan(w[q])) {
double a = d[j * ncol + k + i];
if ( !std::isnan(a) ) {
val[i] += a * w[q];
p++;
}
}
q++;
}
Expand Down Expand Up @@ -130,16 +134,20 @@ std::vector<double> do_focal_sum(std::vector<double> d, Rcpp::NumericMatrix w, s
for (int j = -wr; j <= wr; j++) {
bool jnot0 = j != 0;
for (int k = -wc; k <= wc; k++) {
if (jnot0 && (k != 0)) {
val[i] += d[j * ncol + k + i] * w[q];
if (!std::isnan(w[q])) {
if (jnot0 && (k != 0)) {
val[i] += d[j * ncol + k + i] * w[q];
}
q++;
}
}
}
} else {
for (int j = -wr; j <= wr; j++) {
for (int k = -wc; k <= wc; k++) {
val[i] += d[j * ncol + k + i] * w[q];
if (!std::isnan(w[q])) {
val[i] += d[j * ncol + k + i] * w[q];
}
q++;
}
}
Expand Down Expand Up @@ -170,7 +178,9 @@ std::vector<double> do_focal_sum(std::vector<double> d, Rcpp::NumericMatrix w, s
size_t q = 0;
for (int j = -wr; j <= wr; j++) {
for (int k = -wc; k <= wc; k++) {
val[i] += d[j * ncol + k + i] * w[q];
if (!std::isnan(w[q])) {
val[i] += d[j * ncol + k + i] * w[q];
}
q++;
}
}
Expand Down

0 comments on commit d49f975

Please sign in to comment.