library(geometries)
library(Rcpp)

The shape headers are in

#include "geometries/shapes/shapes.hpp"

Including this header will give you all the functions for creating ‘shapes’ from matrices and data.frames.

  • geometries::shapes::get_vec() - creates a vector
  • geometries::shapes::to_mat() - creates a matrix
  • geometries::shapes::to_listMat() - creates lists of matrices
  • geometries::shapes::to_listListMat() - creates lists of lists of matrices

All these examples show you how to include the header and call the functions using Rcpp code. The same principles apply in a .cpp file in a package.

Lines

df <- data.frame(
  x = 1:10
  , y = 10:1
  , z = 21:30
  , m = 30:21
  , val = letters[1:10]
  , id = c( rep(1,5), rep(2,5) )
)

df
#     x  y  z  m val id
# 1   1 10 21 30   a  1
# 2   2  9 22 29   b  1
# 3   3  8 23 28   c  1
# 4   4  7 24 27   d  1
# 5   5  6 25 26   e  1
# 6   6  5 26 25   f  2
# 7   7  4 27 24   g  2
# 8   8  3 28 23   h  2
# 9   9  2 29 22   i  2
# 10 10  1 30 21   j  2

library(Rcpp)

cppFunction(
  depends = "geometries"
  , includes = '#include "geometries/shapes/shapes.hpp"'
  , code = '
    SEXP line( SEXP df, SEXP geometry_cols ) {
      return geometries::shapes::to_mat( df, geometry_cols );
    }
  '
)

line( df, c("x","y") )
#       [,1] [,2]
#  [1,]    1   10
#  [2,]    2    9
#  [3,]    3    8
#  [4,]    4    7
#  [5,]    5    6
#  [6,]    6    5
#  [7,]    7    4
#  [8,]    8    3
#  [9,]    9    2
# [10,]   10    1

Polygon

cppFunction(
  depends = "geometries"
  , includes = '#include "geometries/shapes/shapes.hpp"'
  , code = '
    SEXP polygon( SEXP df, SEXP geometry_cols, SEXP line_id ) {
      return geometries::shapes::to_listMat( df, geometry_cols, line_id );
    }
  '
)

polygon( df, c("x","y"), c("id") )
# [[1]]
#      [,1] [,2]
# [1,]    1   10
# [2,]    2    9
# [3,]    3    8
# [4,]    4    7
# [5,]    5    6
# 
# [[2]]
#      [,1] [,2]
# [1,]    6    5
# [2,]    7    4
# [3,]    8    3
# [4,]    9    2
# [5,]   10    1

MultiPolygon

cppFunction(
  depends = "geometries"
  , includes = '#include "geometries/shapes/shapes.hpp"'
  , code = '
    SEXP multipolygon( SEXP df, SEXP geometry_cols, SEXP polygon_id, SEXP line_id ) {
      return geometries::shapes::to_listListMat( df, geometry_cols, polygon_id, line_id );
    }
  '
)

multipolygon( df, c("x","y"), c("id"),  c("id") )
# [[1]]
# [[1]][[1]]
#      [,1] [,2]
# [1,]    1   10
# [2,]    2    9
# [3,]    3    8
# [4,]    4    7
# [5,]    5    6
# 
# 
# [[2]]
# [[2]][[1]]
#      [,1] [,2]
# [1,]    6    5
# [2,]    7    4
# [3,]    8    3
# [4,]    9    2
# [5,]   10    1