diff --git a/DESCRIPTION b/DESCRIPTION index 9c7512670..bd45d15ee 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: dodgr Title: Distances on Directed Graphs -Version: 0.2.14.076 +Version: 0.2.14.077 Authors@R: c( person("Mark", "Padgham", , "mark.padgham@email.com", role = c("aut", "cre")), person("Andreas", "Petutschnig", role = "aut"), diff --git a/R/RcppExports.R b/R/RcppExports.R index f42b68f83..83690af21 100644 --- a/R/RcppExports.R +++ b/R/RcppExports.R @@ -249,6 +249,20 @@ rcpp_unique_rownames <- function(xyfrom, xyto, precision = 10L) { .Call(`_dodgr_rcpp_unique_rownames`, xyfrom, xyto, precision) } +#' rcpp_points_index_par +#' +#' Get index of nearest vertices to list of points +#' +#' @param graph Rcpp::DataFrame containing the graph +#' @param pts Rcpp::DataFrame containing the routing points +#' +#' @return 0-indexed Rcpp::NumericVector index into graph of nearest points +#' +#' @noRd +rcpp_points_index_par <- function(xy, pts) { + .Call(`_dodgr_rcpp_points_index_par`, xy, pts) +} + #' rcpp_get_sp_dists_par #' #' @noRd @@ -365,20 +379,6 @@ rcpp_sf_as_network <- function(sf_lines, pr) { .Call(`_dodgr_rcpp_sf_as_network`, sf_lines, pr) } -#' rcpp_points_index_par -#' -#' Get index of nearest vertices to list of points -#' -#' @param graph Rcpp::DataFrame containing the graph -#' @param pts Rcpp::DataFrame containing the routing points -#' -#' @return 0-indexed Rcpp::NumericVector index into graph of nearest points -#' -#' @noRd -rcpp_points_index_par <- function(xy, pts) { - .Call(`_dodgr_rcpp_points_index_par`, xy, pts) -} - #' rcpp_route_times #' #' @noRd diff --git a/codemeta.json b/codemeta.json index 9b8b148ca..9c2b8ee38 100644 --- a/codemeta.json +++ b/codemeta.json @@ -7,7 +7,7 @@ "codeRepository": "https://github.com/ATFutures/dodgr", "issueTracker": "https://github.com/ATFutures/dodgr/issues", "license": "https://spdx.org/licenses/GPL-3.0", - "version": "0.2.14.076", + "version": "0.2.14.077", "programmingLanguage": { "@type": "ComputerLanguage", "name": "R", diff --git a/src/Makevars b/src/Makevars index fc96c3db8..40a871fe6 100644 --- a/src/Makevars +++ b/src/Makevars @@ -7,7 +7,7 @@ PKG_LIBS += $(shell ${R_HOME}/bin/Rscript -e "RcppParallel::RcppParallelLibs()") OBJ_HEAPS = heaps/bheap.o heaps/fheap.o heaps/heap23.o \ heaps/triheap_ext.o heaps/triheap.o OBJ_SRC = centrality.o dgraph.o pathfinders.o dodgr-to-sf.o flows.o fund-cycles.o \ - graph-contract.o graph.o graph-sample.o RcppExports.o run_sp.o \ + graph-contract.o graph.o graph-sample.o match-points.o RcppExports.o run_sp.o \ run_sp_categorical.o sc-as-network.o sf-as-network.o turn_penalty.o OBJECTS = $(OBJ_HEAPS) $(OBJ_SRC) diff --git a/src/Makevars.win b/src/Makevars.win index d9dc78993..887d09c87 100644 --- a/src/Makevars.win +++ b/src/Makevars.win @@ -8,7 +8,7 @@ PKG_LIBS += $(shell "${R_HOME}/bin${R_ARCH_BIN}/Rscript.exe" \ OBJ_HEAPS = heaps/bheap.o heaps/fheap.o heaps/heap23.o \ heaps/triheap_ext.o heaps/triheap.o OBJ_SRC = centrality.o dgraph.o pathfinders.o dodgr-to-sf.o flows.o fund-cycles.o \ - graph-contract.o graph.o graph-sample.o RcppExports.o run_sp.o \ + graph-contract.o graph.o graph-sample.o match-points.o RcppExports.o run_sp.o \ run_sp_categorical.o sc-as-network.o sf-as-network.o turn_penalty.o OBJECTS = $(OBJ_HEAPS) $(OBJ_SRC) diff --git a/src/RcppExports.cpp b/src/RcppExports.cpp index 20bd6b228..7946ddd5a 100644 --- a/src/RcppExports.cpp +++ b/src/RcppExports.cpp @@ -166,6 +166,18 @@ BEGIN_RCPP return rcpp_result_gen; END_RCPP } +// rcpp_points_index_par +Rcpp::IntegerVector rcpp_points_index_par(const Rcpp::DataFrame& xy, Rcpp::DataFrame& pts); +RcppExport SEXP _dodgr_rcpp_points_index_par(SEXP xySEXP, SEXP ptsSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< const Rcpp::DataFrame& >::type xy(xySEXP); + Rcpp::traits::input_parameter< Rcpp::DataFrame& >::type pts(ptsSEXP); + rcpp_result_gen = Rcpp::wrap(rcpp_points_index_par(xy, pts)); + return rcpp_result_gen; +END_RCPP +} // rcpp_get_sp_dists_par Rcpp::NumericMatrix rcpp_get_sp_dists_par(const Rcpp::DataFrame graph, const Rcpp::DataFrame vert_map_in, Rcpp::IntegerVector fromi, Rcpp::IntegerVector toi_in, const std::string& heap_type, const bool is_spatial); RcppExport SEXP _dodgr_rcpp_get_sp_dists_par(SEXP graphSEXP, SEXP vert_map_inSEXP, SEXP fromiSEXP, SEXP toi_inSEXP, SEXP heap_typeSEXP, SEXP is_spatialSEXP) { @@ -313,18 +325,6 @@ BEGIN_RCPP return rcpp_result_gen; END_RCPP } -// rcpp_points_index_par -Rcpp::IntegerVector rcpp_points_index_par(const Rcpp::DataFrame& xy, Rcpp::DataFrame& pts); -RcppExport SEXP _dodgr_rcpp_points_index_par(SEXP xySEXP, SEXP ptsSEXP) { -BEGIN_RCPP - Rcpp::RObject rcpp_result_gen; - Rcpp::RNGScope rcpp_rngScope_gen; - Rcpp::traits::input_parameter< const Rcpp::DataFrame& >::type xy(xySEXP); - Rcpp::traits::input_parameter< Rcpp::DataFrame& >::type pts(ptsSEXP); - rcpp_result_gen = Rcpp::wrap(rcpp_points_index_par(xy, pts)); - return rcpp_result_gen; -END_RCPP -} // rcpp_route_times Rcpp::List rcpp_route_times(const Rcpp::DataFrame graph, bool left_side, int turn_penalty); RcppExport SEXP _dodgr_rcpp_route_times(SEXP graphSEXP, SEXP left_sideSEXP, SEXP turn_penaltySEXP) { @@ -351,6 +351,7 @@ static const R_CallMethodDef CallEntries[] = { {"_dodgr_rcpp_sample_graph", (DL_FUNC) &_dodgr_rcpp_sample_graph, 2}, {"_dodgr_rcpp_get_component_vector", (DL_FUNC) &_dodgr_rcpp_get_component_vector, 1}, {"_dodgr_rcpp_unique_rownames", (DL_FUNC) &_dodgr_rcpp_unique_rownames, 3}, + {"_dodgr_rcpp_points_index_par", (DL_FUNC) &_dodgr_rcpp_points_index_par, 2}, {"_dodgr_rcpp_get_sp_dists_par", (DL_FUNC) &_dodgr_rcpp_get_sp_dists_par, 6}, {"_dodgr_rcpp_get_sp_dists_paired_par", (DL_FUNC) &_dodgr_rcpp_get_sp_dists_paired_par, 6}, {"_dodgr_rcpp_get_iso", (DL_FUNC) &_dodgr_rcpp_get_iso, 5}, @@ -361,7 +362,6 @@ static const R_CallMethodDef CallEntries[] = { {"_dodgr_rcpp_get_sp_dists_cat_threshold", (DL_FUNC) &_dodgr_rcpp_get_sp_dists_cat_threshold, 5}, {"_dodgr_rcpp_gen_hash", (DL_FUNC) &_dodgr_rcpp_gen_hash, 2}, {"_dodgr_rcpp_sf_as_network", (DL_FUNC) &_dodgr_rcpp_sf_as_network, 2}, - {"_dodgr_rcpp_points_index_par", (DL_FUNC) &_dodgr_rcpp_points_index_par, 2}, {"_dodgr_rcpp_route_times", (DL_FUNC) &_dodgr_rcpp_route_times, 3}, {NULL, NULL, 0} }; diff --git a/src/match-points.cpp b/src/match-points.cpp new file mode 100644 index 000000000..7c900e03a --- /dev/null +++ b/src/match-points.cpp @@ -0,0 +1,79 @@ +#include "match-points.h" + +struct OnePointIndex : public RcppParallel::Worker +{ + const RcppParallel::RVector xy_x, xy_y, pt_x, pt_y; + const size_t nxy; + RcppParallel::RVector index; + + // constructor + OnePointIndex ( + const RcppParallel::RVector xy_x_in, + const RcppParallel::RVector xy_y_in, + const RcppParallel::RVector pt_x_in, + const RcppParallel::RVector pt_y_in, + const size_t nxy_in, + Rcpp::IntegerVector index_in) : + xy_x (xy_x_in), xy_y (xy_y_in), pt_x (pt_x_in), pt_y (pt_y_in), + nxy (nxy_in), index (index_in) + { + } + + // Parallel function operator + void operator() (std::size_t begin, std::size_t end) + { + for (std::size_t i = begin; i < end; i++) + { + double dmin = INFINITE_DOUBLE; + long int jmin = INFINITE_INT; + for (size_t j = 0; j < nxy; j++) + { + double dij = (xy_x [j] - pt_x [i]) * (xy_x [j] - pt_x [i]) + + (xy_y [j] - pt_y [i]) * (xy_y [j] - pt_y [i]); + if (dij < dmin) + { + dmin = dij; + jmin = static_cast (j); + } + } + index [i] = static_cast (jmin); + } + } + +}; + +//' rcpp_points_index_par +//' +//' Get index of nearest vertices to list of points +//' +//' @param graph Rcpp::DataFrame containing the graph +//' @param pts Rcpp::DataFrame containing the routing points +//' +//' @return 0-indexed Rcpp::NumericVector index into graph of nearest points +//' +//' @noRd +// [[Rcpp::export]] +Rcpp::IntegerVector rcpp_points_index_par (const Rcpp::DataFrame &xy, + Rcpp::DataFrame &pts) +{ + Rcpp::NumericVector ptx = pts ["x"]; + Rcpp::NumericVector pty = pts ["y"]; + + Rcpp::NumericVector vtx = xy ["x"]; + Rcpp::NumericVector vty = xy ["y"]; + + size_t npts = static_cast (pts.nrow ()), + nxy = static_cast (xy.nrow ()); + + //Rcpp::IntegerVector index (n, Rcpp::IntegerVector::get_na ()); + Rcpp::IntegerVector index (npts); + // Create parallel worker + OnePointIndex one_pt_indx (RcppParallel::RVector (vtx), + RcppParallel::RVector (vty), + RcppParallel::RVector (ptx), + RcppParallel::RVector (pty), nxy, index); + + RcppParallel::parallelFor (0, npts, one_pt_indx); + + return index; +} diff --git a/src/match-points.h b/src/match-points.h new file mode 100644 index 000000000..5bc76794c --- /dev/null +++ b/src/match-points.h @@ -0,0 +1,15 @@ +#include +#include + +#include +// [[Rcpp::depends(RcppParallel)]] +#include + +constexpr float INFINITE_FLOAT = std::numeric_limits::max (); +constexpr double INFINITE_DOUBLE = std::numeric_limits::max (); +constexpr int INFINITE_INT = std::numeric_limits::max (); + +Rcpp::IntegerVector rcpp_points_index (const Rcpp::DataFrame &xy, + Rcpp::DataFrame &pts); +Rcpp::IntegerVector rcpp_points_index_par (const Rcpp::DataFrame &xy, + Rcpp::DataFrame &pts); diff --git a/src/sf-as-network.cpp b/src/sf-as-network.cpp index f7b78caf6..286ae34b0 100644 --- a/src/sf-as-network.cpp +++ b/src/sf-as-network.cpp @@ -186,81 +186,3 @@ void sf::fill_one_row (const R_xlen_t ngeoms, const Rcpp::NumericMatrix &gi, if (has_names) idmat (rownum, 3) = way_names [static_cast (ngeoms)]; } - -struct OnePointIndex : public RcppParallel::Worker -{ - const RcppParallel::RVector xy_x, xy_y, pt_x, pt_y; - const size_t nxy; - RcppParallel::RVector index; - - // constructor - OnePointIndex ( - const RcppParallel::RVector xy_x_in, - const RcppParallel::RVector xy_y_in, - const RcppParallel::RVector pt_x_in, - const RcppParallel::RVector pt_y_in, - const size_t nxy_in, - Rcpp::IntegerVector index_in) : - xy_x (xy_x_in), xy_y (xy_y_in), pt_x (pt_x_in), pt_y (pt_y_in), - nxy (nxy_in), index (index_in) - { - } - - // Parallel function operator - void operator() (std::size_t begin, std::size_t end) - { - for (std::size_t i = begin; i < end; i++) - { - double dmin = INFINITE_DOUBLE; - long int jmin = INFINITE_INT; - for (size_t j = 0; j < nxy; j++) - { - double dij = (xy_x [j] - pt_x [i]) * (xy_x [j] - pt_x [i]) + - (xy_y [j] - pt_y [i]) * (xy_y [j] - pt_y [i]); - if (dij < dmin) - { - dmin = dij; - jmin = static_cast (j); - } - } - index [i] = static_cast (jmin); - } - } - -}; - -//' rcpp_points_index_par -//' -//' Get index of nearest vertices to list of points -//' -//' @param graph Rcpp::DataFrame containing the graph -//' @param pts Rcpp::DataFrame containing the routing points -//' -//' @return 0-indexed Rcpp::NumericVector index into graph of nearest points -//' -//' @noRd -// [[Rcpp::export]] -Rcpp::IntegerVector rcpp_points_index_par (const Rcpp::DataFrame &xy, - Rcpp::DataFrame &pts) -{ - Rcpp::NumericVector ptx = pts ["x"]; - Rcpp::NumericVector pty = pts ["y"]; - - Rcpp::NumericVector vtx = xy ["x"]; - Rcpp::NumericVector vty = xy ["y"]; - - size_t npts = static_cast (pts.nrow ()), - nxy = static_cast (xy.nrow ()); - - //Rcpp::IntegerVector index (n, Rcpp::IntegerVector::get_na ()); - Rcpp::IntegerVector index (npts); - // Create parallel worker - OnePointIndex one_pt_indx (RcppParallel::RVector (vtx), - RcppParallel::RVector (vty), - RcppParallel::RVector (ptx), - RcppParallel::RVector (pty), nxy, index); - - RcppParallel::parallelFor (0, npts, one_pt_indx); - - return index; -} diff --git a/src/sf-as-network.h b/src/sf-as-network.h index 8f9f6313c..0735f5204 100644 --- a/src/sf-as-network.h +++ b/src/sf-as-network.h @@ -24,7 +24,3 @@ void fill_one_row (const R_xlen_t ngeoms, const Rcpp::NumericMatrix &gi, Rcpp::List rcpp_sf_as_network (const Rcpp::List &sf_lines, const Rcpp::DataFrame &pr); -Rcpp::IntegerVector rcpp_points_index (const Rcpp::DataFrame &xy, - Rcpp::DataFrame &pts); -Rcpp::IntegerVector rcpp_points_index_par (const Rcpp::DataFrame &xy, - Rcpp::DataFrame &pts);