c++ - 转换 Rcpp NumericMatrix 以与 Boost Geometry 一起使用

标签 c++ r boost rcpp boost-geometry

我发现如果没有漂亮的 <as> 我会迷失方向和 <wrap> Rcpp 及其相关包提供的用于不同对象类型之间转换的命令。

我有一个点矩阵,其中的行表示二维笛卡尔空间中的点:

 pointsMatrix <- matrix(runif(100,-1,1),50,50)

然后我想使用 convex_hull algorithm from boost geometry找到点的凸包。

但是,我不确定如何转换 NumericMatrix转换为 convex_hull 的一种数据类型明白。此外,我不确定如何将 Boost Geometry 的输出转换回 Rcpp 可以返回给 R 的输出。

 #include <Rcpp.h>
 #include <boost/geometry.hpp>
 #include <boost/geometry/geometries/polygon.hpp>
 using namespace Rcpp;

 BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)

 // [[Rcpp::export]]
 NumericMatrix convexHullRcpp(NumericMatrix pointsMatrix){

     typedef boost::tuple<double, double> point;
     typedef boost::geometry::model::polygon<point> polygon;

 // Some sort of conversion of pointsMatrix here to pointsMatrixBG//

     polygon hull;
     boost::geometry::convex_hull(pointsMatrixBG, hull);

     //Now to convert hull into something that Rcpp can hand back to R.//

  return hullToR;
 }

看起来boost.tuple可能是最好的选择

最佳答案

这是一个小的test.cpp文件

#include <Rcpp.h>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/point_xy.hpp>

using namespace Rcpp;

typedef boost::geometry::model::d2::point_xy<double, boost::geometry::cs::cartesian> Point;
typedef boost::geometry::model::polygon<Point, true, true> Polygon; 

namespace Rcpp {
  template <> Polygon as(SEXP pointsMatrixSEXP) {
    NumericMatrix pointsMatrix(pointsMatrixSEXP);
    Polygon pointsMatrixBG;
    for (int i = 0; i < pointsMatrix.nrow(); ++i) {
      double x = pointsMatrix(i,0);
      double y = pointsMatrix(i,1);
      Point p(x,y);
      pointsMatrixBG.outer().push_back(p); 
    }
    return (pointsMatrixBG);
  } 

  template <> SEXP wrap(const Polygon& poly) {
    const std::vector<Point>& points = poly.outer();
    NumericMatrix rmat(points.size(), 2);
    for(int i = 0; i < points.size(); ++i) {
      const Point& p = points[i];
      rmat(i,0) = p.x();
      rmat(i,1) = p.y();
    }
    return Rcpp::wrap(rmat);
  }
}

// [[Rcpp::export]]
NumericMatrix convexHullRcpp(SEXP pointsMatrixSEXP){

  // Conversion of pointsMatrix here to pointsMatrixBG
  Polygon pointsMatrixBG = as<Polygon>(pointsMatrixSEXP);

  Polygon hull;
  boost::geometry::convex_hull(pointsMatrixBG, hull);

  //Now to convert hull into something that Rcpp can hand back to R.//
  return wrap(hull);
}

然后您可以在 R 中对其进行测试。

library(Rcpp)
sourceCpp("test.cpp")
points <- c(2.0, 1.3, 2.4, 1.7, 2.8, 1.8, 3.4, 1.2, 3.7, 1.6,3.4, 2.0, 4.1, 3.0, 5.3, 2.6, 5.4, 1.2, 4.9, 0.8, 2.9, 0.7,2.0, 1.3)
points.matrix <- matrix(points, ncol=2, byrow=TRUE)
> convexHullRcpp(points.matrix)
     [,1] [,2]
[1,]  2.0  1.3
[2,]  2.4  1.7
[3,]  4.1  3.0
[4,]  5.3  2.6
[5,]  5.4  1.2
[6,]  4.9  0.8
[7,]  2.9  0.7
[8,]  2.0  1.3

关于c++ - 转换 Rcpp NumericMatrix 以与 Boost Geometry 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15195350/

相关文章:

c++ - std::bind 不会接受绑定(bind)占位符的 std::cref - 为什么?

r - Sublime 3 不与 R 接口(interface)(尝试 R-box 和 REPL)

c++ - 使用 gcc 4.1.2 抑制代码块的警告?

c++ - 从 C++ 文件中获取输入

c++ - 如何解决对自己类的构造函数和析构函数的 undefined reference 的问题?

r - 创建具有多个 ifelse() 的列

r - 在 dplyr 分组后将误差线添加到 ggplot2 条形图

c++ - 使用/clr 在 Visual Studio 2010 中 boost

c++ - Logging Guard 以限制半常量日志消息

c++ - 读取文本文件并创建迷宫