Skip to content

Commit

Permalink
more interleaving #78
Browse files Browse the repository at this point in the history
  • Loading branch information
SymbolixAU committed Apr 22, 2020
1 parent ac3b67b commit c3e58f7
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 30 deletions.
39 changes: 35 additions & 4 deletions inst/include/sfheaders/df/sfc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ namespace df {
return Rcpp::IntegerVector(); // #nocov never reached
}

/*
* sfg_n_coordinates
*
* iterates through all matrices in the sfg and sums the count of coordinates
*/
inline void sfg_n_coordinates(
SEXP& sfg,
R_xlen_t& sfg_count
Expand Down Expand Up @@ -133,7 +138,7 @@ namespace df {
//}
R_xlen_t n = lst.size();
R_xlen_t i;
Rcpp::IntegerVector res( n );
//Rcpp::IntegerVector res( n );
for( i = 0; i < n; ++i ) {
SEXP tmp_sfg = lst[i];
sfg_n_coordinates( tmp_sfg, sfg_count ); // recurse
Expand All @@ -144,8 +149,6 @@ namespace df {
Rcpp::stop("sfheaders - unsupported coordinate type"); // #nocov
}
}

//return sfg_count;
}

// if I make this cumulative, it gives me a vector where the last element
Expand All @@ -154,7 +157,6 @@ namespace df {
inline Rcpp::NumericMatrix sfc_n_coordinates(
Rcpp::List& sfc
) {

R_xlen_t cumulative_coords = 0;
R_xlen_t n = sfc.size();
Rcpp::NumericMatrix res( n, 2 );
Expand All @@ -172,6 +174,35 @@ namespace df {
return res;
}

inline Rcpp::NumericMatrix sfg_n_coordinates(
SEXP& sfg
) {

switch( TYPEOF( sfg ) ) {
case INTSXP: {}
case REALSXP: {
Rcpp::NumericMatrix res(1,1); // starts filled with 0, so we only need to assign the second column
if( Rf_isMatrix( sfg ) ) {
res(0, 0) = sfheaders::utils::sexp_n_row( sfg );
} else {
res(0, 0) = sfheaders::utils::get_sexp_length( sfg );
}
return res;
}
case VECSXP: {
if ( Rf_isNewList( sfg ) ) {
Rcpp::List lst = Rcpp::as< Rcpp::List > ( sfg );
return sfc_n_coordinates( lst );
}
}
default: {
Rcpp::stop("sfheaders - unknown sfg type");
}
}
Rcpp::NumericMatrix res; // #nocov
return res; // #nocov
}

// sfcs are a list of sfgs.
// they can be mixed, or individual.
// if indiidual, loop over each one and extract the sfgs, list by list, then collapse the lists??
Expand Down
18 changes: 17 additions & 1 deletion inst/include/sfheaders/df/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace sfheaders {
namespace utils {

// Fill vec_1 with vec_2
inline Rcpp::NumericVector fill_vector(
Rcpp::NumericVector& vec_1,
Rcpp::NumericVector& vec_2,
Expand All @@ -20,7 +21,22 @@ namespace utils {
return vec_1;
}

template <int RTYPE>
// template< int RTYPE >
// inline Rcpp::Vector< RTYPE > fill_vector(
// Rcpp::Vector< RTYPE >& vec_1,
// Rcpp::Vector< RTYPE >& vec_2,
// R_xlen_t& start_idx
// ) {
// R_xlen_t i;
// R_xlen_t n = vec_2.length();
//
// for( i = 0; i < n; ++i ) {
// vec_1[ i + start_idx ] = vec_2[ i ] ;
// }
// return vec_1;
// }

template < int RTYPE >
inline Rcpp::CharacterVector sfgClass( Rcpp::Vector<RTYPE> v ) {
return v.attr("class");
}
Expand Down
61 changes: 40 additions & 21 deletions inst/include/sfheaders/interleave/interleave.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#ifndef R_SFHEADERS_INTERLEAVE_H
#define R_SFHEADERS_INTERLEAVE_H

#include "sfheaders/df/sfc.hpp"
#include <Rcpp.h>
#include "sfheaders/df/sfc.hpp"
#include "sfheaders/utils/lists/list.hpp"

namespace sfheaders {
namespace interleave {
Expand All @@ -13,34 +14,31 @@ namespace interleave {
R_xlen_t n_row = mat.nrow();
Rcpp::Vector< RTYPE > res( n_col * n_row );
R_xlen_t i, j;
R_xlen_t position_counter = 0;
for( i = 0; i < n_row; ++i ) {
for( j = 0; j < n_col; ++j ) {
res[ j ] = mat( i, j );
res[ position_counter ] = mat( i, j );
position_counter = position_counter + 1;
}
}
return res;
}

inline SEXP interleave( Rcpp::List& lst) {
R_xlen_t i;
R_xlen_t n = lst.length();


Rcpp::NumericMatrix sfc_coordinates = sfheaders::df::sfc_n_coordinates( lst );
R_xlen_t n_geometries = sfc_coordinates.nrow();
R_xlen_t total_coordinates = sfc_coordinates( n_geometries - 1 , 1 );
total_coordinates = total_coordinates + 1;

//Rcpp::Rcout << "sfc_coordinates: " << sfc_coordinates << std::endl;

for( i = 0; i < n; ++i ) {
//SEXP sfg = sfc[ i ];

}
return Rcpp::List::create();
}
// templated version with a vector you want to fill with the result of interleaving
// requires knowing the start index.
// template < int RTYPE >
// inline SEXP interleave(
// Rcpp::Matrix< RTYPE >& mat,
// Rcpp::Matrix< RTYPE >& to_fill,
// R_xlen_t& start_index
// ) {
// Rcpp::Vector< RTYPE > interleaved = interleave( mat );
// sfheaders::utils::fill_vector( to_fill, interleaved, start_index );
// return to_fill;
// }

inline SEXP interleave( SEXP& sfg ) {

switch( TYPEOF ( sfg ) ) {
case INTSXP: {
if( Rf_isMatrix( sfg ) ) {
Expand All @@ -61,7 +59,28 @@ namespace interleave {
case VECSXP: {
if( Rf_isNewList( sfg ) ) {
Rcpp::List lst = Rcpp::as< Rcpp::List >( sfg );
return interleave( lst );
// iterate through until we get a matrix
// which can be interleaved
R_xlen_t n = lst.size();
R_xlen_t i;
Rcpp::List res( n ); // store interleaved vectors in the same nested-list structure

//R_xlen_t coordinate_counter = 0;

// Rcpp::NumericMatrix coords = sfheaders::df::sfc_n_coordinates( lst );
// Rcpp::Rcout << "sfg_coordinates: " << coords << std::endl;

Rcpp::Rcout << "n: " << n << std::endl;
for( i = 0; i < n; ++i ) {
Rcpp::Rcout << "i: " << i << std::endl;
SEXP sfg = lst[ i ];
// Rcpp::NumericMatrix coords = sfheaders::df::sfg_n_coordinates( sfg );
// Rcpp::Rcout << "sfg_coordinates: " << coords << std::endl;
res[ i ] = interleave( sfg );
}

return sfheaders::utils::unlist_list( res );
//return res;
}
}
default: {
Expand Down
45 changes: 41 additions & 4 deletions src/interleave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ SEXP rcpp_interleave( Rcpp::List sfc ) {
// can probably do this one later?
// and focus on the 'sf', because it doesn't need any extra headers / thought / logic

//Rcpp::NumericMatrix sfc_coordinates = sfheaders::df::sfc_n_coordinates( sfc );
Rcpp::NumericMatrix sfc_coordinates = sfheaders::df::sfc_n_coordinates( sfc );
Rcpp::Rcout << "sfc_coordinates: " << sfc_coordinates << std::endl;

//return sfc_coordinates;

Expand All @@ -31,6 +32,7 @@ SEXP rcpp_interleave( Rcpp::List sfc ) {
R_xlen_t i;
Rcpp::IntegerVector n_coordinates( n );
R_xlen_t sfg_count = 0; // move outside loop so it's cumulative

for( i = 0; i < n; ++i ) {
SEXP sfg = sfc[ i ];

Expand All @@ -43,7 +45,6 @@ SEXP rcpp_interleave( Rcpp::List sfc ) {
Rcpp::stop("sfheaders - interleaving only works when all geometries have the same dimension (XY(Z(M)))");
}
}

n_coordinates[ i ] = sfg_count;
sfheaders::df::sfg_n_coordinates( sfg, sfg_count );
}
Expand All @@ -55,16 +56,52 @@ SEXP rcpp_interleave( Rcpp::List sfc ) {
R_xlen_t total_coordinates = sfg_count * stride;
Rcpp::NumericVector res( total_coordinates );
R_xlen_t coordinate_counter = 0;
Rcpp::List res_list( sfc.size() );
Rcpp::List res_indices( sfc.size() );

//R_xlen_t total_coordinates = 0;

// now fill this vector with all the coorinates from the coordinates
for( i = 0; i < n; ++i ) {
SEXP sfg = sfc[ i ];

Rcpp::NumericMatrix coords = sfheaders::df::sfg_n_coordinates( sfg );
Rcpp::Rcout << "sfg_coordinates: " << coords << std::endl;
R_xlen_t n_geometries = coords.nrow();
R_xlen_t n_coordinates = coords( n_geometries - 1, 1 );
n_coordinates = n_coordinates + 1;
Rcpp::NumericVector start_indices = coords( Rcpp::_, 0 );
start_indices = start_indices + coordinate_counter;
res_indices[ i ] = start_indices;
coordinate_counter = coordinate_counter + n_coordinates;

// and do I need to account for stride?
// i.e., does Deck.GL think the start index is for the number of PAIRS, or individual coordinates?

// and the start_indices need to be cumulatively_summed

// depending on the GEOMETRY
// get each coordinate
// do I iterate each row?
SEXP temp = sfheaders::interleave::interleave( sfg );
// Rcpp::NumericVector interleaved = sfheaders::interleave::interleave( sfg );
// sfheaders::utils::fill_vector( res, interleaved, coordinate_counter );
// coordinate_counter = coordinate_counter + interleaved.length();

//sfheaders::df::sfg_n_coordinates( sfg, coordinate_counter );
//Rcpp::Rcout << "sfg_coordinates " << coordinate_counter << std::endl;

res_list[ i ] = sfheaders::interleave::interleave( sfg );
// this interleaved can be a vector, or a list of vectors

// Need to keep a list of start_indices for each sfg
}

return res;
return Rcpp::List::create(
Rcpp::_["coordinates"] = res_list,
Rcpp::_["start_indices"] = res_indices
);

//return res_list;
}


0 comments on commit c3e58f7

Please sign in to comment.