diff --git a/R/flow_accumulation.R b/R/flow_accumulation.R index 92338c8..196b56b 100644 --- a/R/flow_accumulation.R +++ b/R/flow_accumulation.R @@ -42,16 +42,14 @@ flow_accumulation <- function(fd, edge_count <- length(fd$source) # Compute flow routing using libtopotoolbox - output <- single(prod(dims)) + output <- as.single(ezgetnal(fd, weights)) result <- .C( - "wrap_flow_accumulation_edgelist", - accR = as.single(output), # float + "wrap_traverse_down_f32_add_mul", + accR = output, # float + fractionR = as.single(rep(1, edge_count)), # float sourceR = as.integer(fd$source), # ptrdiff_t targetR = as.integer(fd$target), # ptrdiff_t - fractionR = as.single(rep(1, edge_count)), # float - weightsR = as.single(ezgetnal(fd, weights)), # float edge_countR = as.integer(edge_count), # ptrdiff_t - dimsR = as.integer(dims), # ptrdiff_t NAOK = TRUE )$accR @@ -59,4 +57,4 @@ flow_accumulation <- function(fd, fa <- fd$raster terra::values(fa) <- result GRIDobj(fa) -} \ No newline at end of file +} diff --git a/src/wrap_flow_accumulation.c b/src/wrap_flow_accumulation.c deleted file mode 100644 index cf8c9c0..0000000 --- a/src/wrap_flow_accumulation.c +++ /dev/null @@ -1,34 +0,0 @@ -#include -#include -#include - -#include "topotoolbox.h" -#include "topotoolboxr.h" - -void wrap_flow_accumulation_edgelist( - float *accR, // output - int *sourceR, // ptrdiff_t - int *targetR, // ptrdiff_t - float *fractionR, - float *weightsR, - int *edge_countR, // ptrdiff_t - int *dimsR){ // ptrdiff_t - // Transformation of integers and array allocation - ptrdiff_t dims [2]= {dimsR[0], dimsR[1]}; - ptrdiff_t edge_count = (ptrdiff_t)edge_countR[0]; - ptrdiff_t *source = R_Calloc(edge_count, ptrdiff_t); - ptrdiff_t *target = R_Calloc(edge_count, ptrdiff_t); - - // Convert sourceR and targetR to ptrdiff_t - for (ptrdiff_t idx = 0; idx < edge_count; idx++) { - source[idx] = (ptrdiff_t)sourceR[idx]; - target[idx] = (ptrdiff_t)targetR[idx]; - } - - // Flow accumulation computation using libtopotoolbox - flow_accumulation_edgelist(accR, source, target, fractionR, weightsR, edge_count, dims); - - // Free memory - R_Free(source); - R_Free(target); - } \ No newline at end of file diff --git a/src/wrap_streamquad.c b/src/wrap_streamquad.c new file mode 100644 index 0000000..24aa703 --- /dev/null +++ b/src/wrap_streamquad.c @@ -0,0 +1,32 @@ +#include +#include +#include + +#include "topotoolbox.h" +#include "topotoolboxr.h" + +void wrap_traverse_down_f32_add_mul(float *accR, // output + float *fractionR, // input + int *sourceR, // ptrdiff_t + int *targetR, // ptrdiff_t + int *edge_countR // ptrdiff_t + ) { + + // Transformation of integers and array allocation + ptrdiff_t edge_count = edge_countR[0]; + ptrdiff_t *source = R_Calloc(edge_count, ptrdiff_t); + ptrdiff_t *target = R_Calloc(edge_count, ptrdiff_t); + + // Convert sourceR and targetR to ptrdiff_t + for (ptrdiff_t idx = 0; idx < edge_count; idx++) { + source[idx] = (ptrdiff_t)sourceR[idx]; + target[idx] = (ptrdiff_t)targetR[idx]; + } + + // Flow accumulation computation using libtopotoolbox + traverse_down_f32_add_mul(accR, fractionR, source, target, edge_count); + + // Free memory + R_Free(source); + R_Free(target); +}