Skip to content

Commit

Permalink
prototype for hypertidy#9
Browse files Browse the repository at this point in the history
  • Loading branch information
dcooley committed May 3, 2020
1 parent 7f86d61 commit 58a13ad
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 1 deletion.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ Encoding: UTF-8
LazyData: true
ByteCompile: true
LinkingTo:
Rcpp
Rcpp,
sfheaders
Depends: R (>= 3.0.0)
Imports:
Rcpp
Expand Down
4 changes: 4 additions & 0 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ earcut_sfg <- function(sfg) {
.Call(`_decido_earcut_sfg`, sfg)
}

earcut_sfc <- function(sfc) {
.Call(`_decido_earcut_sfc`, sfc)
}

12 changes: 12 additions & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,22 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// earcut_sfc
SEXP earcut_sfc(Rcpp::List& sfc);
RcppExport SEXP _decido_earcut_sfc(SEXP sfcSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< Rcpp::List& >::type sfc(sfcSEXP);
rcpp_result_gen = Rcpp::wrap(earcut_sfc(sfc));
return rcpp_result_gen;
END_RCPP
}

static const R_CallMethodDef CallEntries[] = {
{"_decido_earcut_cpp", (DL_FUNC) &_decido_earcut_cpp, 4},
{"_decido_earcut_sfg", (DL_FUNC) &_decido_earcut_sfg, 1},
{"_decido_earcut_sfc", (DL_FUNC) &_decido_earcut_sfc, 1},
{NULL, NULL, 0}
};

Expand Down
55 changes: 55 additions & 0 deletions src/decido.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ using namespace Rcpp;

#include "decido/decido.hpp"


#include "sfheaders/df/sfg.hpp"
#include "sfheaders/sfg/multipolygon/sfg_multipolygon.hpp"
#include "sfheaders/sfc/sfc.hpp"

// [[Rcpp::export]]
Rcpp::IntegerVector earcut_cpp(
NumericVector x,
Expand All @@ -21,3 +26,53 @@ Rcpp::IntegerVector earcut_sfg( SEXP& sfg ) {
return decido::api::earcut( sfg );
}

// [[Rcpp::export]]
SEXP earcut_sfc( Rcpp::List& sfc ) {
// expecting an sfc objet

Rcpp::List attributes = sfheaders::sfc::get_sfc_attributes( sfc );

// TODO
// - cast to POLYGON
// - for each sfg
// - tringulate
// - convert to sfg_POLYGON
R_xlen_t i;
R_xlen_t n = sfc.length();
Rcpp::List res( n );
for( i = 0; i < n; ++i ) {
SEXP poly = sfc[ i ];
Rcpp::DataFrame df = sfheaders::df::sfg_to_df( poly );
Rcpp::IntegerVector indices = decido::api::earcut( poly );
R_xlen_t n_triangles = indices.length() / 3;

Rcpp::NumericVector x = df["x"];
Rcpp::NumericVector y = df["y"];
Rcpp::NumericVector xx = x[ indices ];
Rcpp::NumericVector yy = y[ indices ];

Rcpp::IntegerVector triangle_idx = Rcpp::seq(1, n_triangles );
Rcpp::IntegerVector triangle_ids = Rcpp::rep_each( triangle_idx, 3 );

Rcpp::DataFrame df_tri = Rcpp::DataFrame::create(
Rcpp::_["triangle_id"] = triangle_ids,
Rcpp::_["x"] = xx,
Rcpp::_["y"] = yy
);

Rcpp::StringVector geometry_cols {"x","y"};
Rcpp::String polygon_id = "triangle_id";
Rcpp::String line_id = polygon_id;

std::string xyzm = "XY";
bool close = false;

res[ i ] = sfheaders::sfg::sfg_multipolygon( df_tri, geometry_cols, polygon_id, line_id, xyzm, close );

}

sfheaders::sfc::attach_sfc_attributes( res, attributes );

return res;

}

0 comments on commit 58a13ad

Please sign in to comment.