Skip to content

Commit

Permalink
split src match-points fns out to match-points.cpp for #103
Browse files Browse the repository at this point in the history
  • Loading branch information
mpadge committed Aug 9, 2022
1 parent f7164ff commit 0c2dc72
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 113 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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", , "[email protected]", role = c("aut", "cre")),
person("Andreas", "Petutschnig", role = "aut"),
Expand Down
28 changes: 14 additions & 14 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/Makevars
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion src/Makevars.win
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
26 changes: 13 additions & 13 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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},
Expand All @@ -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}
};
Expand Down
79 changes: 79 additions & 0 deletions src/match-points.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include "match-points.h"

struct OnePointIndex : public RcppParallel::Worker
{
const RcppParallel::RVector <double> xy_x, xy_y, pt_x, pt_y;
const size_t nxy;
RcppParallel::RVector <int> index;

// constructor
OnePointIndex (
const RcppParallel::RVector <double> xy_x_in,
const RcppParallel::RVector <double> xy_y_in,
const RcppParallel::RVector <double> pt_x_in,
const RcppParallel::RVector <double> 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 <long int> (j);
}
}
index [i] = static_cast <int> (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 <size_t> (pts.nrow ()),
nxy = static_cast <size_t> (xy.nrow ());

//Rcpp::IntegerVector index (n, Rcpp::IntegerVector::get_na ());
Rcpp::IntegerVector index (npts);
// Create parallel worker
OnePointIndex one_pt_indx (RcppParallel::RVector <double> (vtx),
RcppParallel::RVector <double> (vty),
RcppParallel::RVector <double> (ptx),
RcppParallel::RVector <double> (pty), nxy, index);

RcppParallel::parallelFor (0, npts, one_pt_indx);

return index;
}
15 changes: 15 additions & 0 deletions src/match-points.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <string>
#include <cmath>

#include <Rcpp.h>
// [[Rcpp::depends(RcppParallel)]]
#include <RcppParallel.h>

constexpr float INFINITE_FLOAT = std::numeric_limits<float>::max ();
constexpr double INFINITE_DOUBLE = std::numeric_limits<double>::max ();
constexpr int INFINITE_INT = std::numeric_limits<int>::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);
78 changes: 0 additions & 78 deletions src/sf-as-network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <size_t> (ngeoms)];
}

struct OnePointIndex : public RcppParallel::Worker
{
const RcppParallel::RVector <double> xy_x, xy_y, pt_x, pt_y;
const size_t nxy;
RcppParallel::RVector <int> index;

// constructor
OnePointIndex (
const RcppParallel::RVector <double> xy_x_in,
const RcppParallel::RVector <double> xy_y_in,
const RcppParallel::RVector <double> pt_x_in,
const RcppParallel::RVector <double> 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 <long int> (j);
}
}
index [i] = static_cast <int> (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 <size_t> (pts.nrow ()),
nxy = static_cast <size_t> (xy.nrow ());

//Rcpp::IntegerVector index (n, Rcpp::IntegerVector::get_na ());
Rcpp::IntegerVector index (npts);
// Create parallel worker
OnePointIndex one_pt_indx (RcppParallel::RVector <double> (vtx),
RcppParallel::RVector <double> (vty),
RcppParallel::RVector <double> (ptx),
RcppParallel::RVector <double> (pty), nxy, index);

RcppParallel::parallelFor (0, npts, one_pt_indx);

return index;
}
4 changes: 0 additions & 4 deletions src/sf-as-network.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

0 comments on commit 0c2dc72

Please sign in to comment.