Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 转换Rcpp NumericMatrix以用于Boost几何体_C++_R_Boost_Rcpp_Boost Geometry - Fatal编程技术网

C++ 转换Rcpp NumericMatrix以用于Boost几何体

C++ 转换Rcpp NumericMatrix以用于Boost几何体,c++,r,boost,rcpp,boost-geometry,C++,R,Boost,Rcpp,Boost Geometry,我发现,如果没有Rcpp及其相关包为不同对象类型之间的转换提供的nice和命令,我会迷失方向 我有一个点矩阵,其中的行表示二维笛卡尔空间中的点: pointsMatrix <- matrix(runif(100,-1,1),50,50) 看起来boost.tuple可能是最好的选择一般来说,一旦您选择了R不知道的类型,您需要构建自己的自定义转换器函数as()和wrap() 正如评论中所指出的,有一个完整的小插曲专门介绍它。包中还有一些例子,例如关于自定义as()和wrap()。我将从C

我发现,如果没有Rcpp及其相关包为不同对象类型之间的转换提供的nice
命令,我会迷失方向

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

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

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

一般来说,一旦您选择了R不知道的类型,您需要构建自己的自定义转换器函数
as()
wrap()


正如评论中所指出的,有一个完整的小插曲专门介绍它。包中还有一些例子,例如关于自定义
as()
wrap()
。我将从C++中的Boost几何示例开始,了解它们的数据结构是如何填充的,然后从C++中填充它们。没有捷径可走。没有免费午餐的定理仍然成立

一般来说,一旦转到R不知道的类型,就需要构建自己的自定义转换器函数
as()
wrap()


正如评论中所指出的,有一个完整的小插曲专门介绍它。包中还有一些例子,例如关于自定义
as()
wrap()
。我将从C++中的Boost几何示例开始,了解它们的数据结构是如何填充的,然后从C++中填充它们。没有捷径可走。没有免费午餐的定理仍然成立

这里有一个小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);
}
#包括
#包括
#包括
#包括
使用名称空间Rcpp;
typedef boost::geometry::model::d2::point_xy point;
typedef boost::geometry::model::polygon;
名称空间Rcpp{
模板多边形为(SEXP pointsMatrixSEXP){
数值矩阵pointsMatrix(pointsMatrix-Exp);
多边形点矩阵xbg;
对于(int i=0;i
然后你可以在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
库(Rcpp)
sourceCpp(“test.cpp”)

点这里有一个小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);
}
#包括
#包括
#包括
#包括
使用名称空间Rcpp;
typedef boost::geometry::model::d2::point_xy point;
typedef boost::geometry::model::polygon;
名称空间Rcpp{
模板多边形为(SEXP pointsMatrixSEXP){
数值矩阵pointsMatrix(pointsMatrix-Exp);
多边形点矩阵xbg;
对于(int i=0;i
然后你可以在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
库(Rcpp)
sourceCpp(“test.cpp”)

我对Rcpp不太了解,但我相信有了Boost.Geometry,你可以不用任何转换就完成它。AFAIU一些任意数据可以表示为数值矩阵。您可以实现一定数量的视图/代理,例如对NumericMatrix的引用(只是为了能够以各种方式表示矩阵)。这些视图可以适应Boost.geometry-Polygon、Linestring等中的各种几何概念。然后您可以将其传递到任意Boost.geometry算法中,大致如下所示:bg::凸包(view_as_Polygon(pointsMatrix)、view_as_Polygon(hullToR))以及,也许你可以自己写一个,我对Rcpp不太了解,但我相信有了Boost.Geometry,你可以不用任何转换就完成它。AFAIU一些任意数据可以表示为数值矩阵。您可以实现一定数量的视图/代理,例如对NumericMatrix的引用(只是为了能够以各种方式表示矩阵)。这些视图可以适应Boost.geometry-Polygon、Linestring等的各种几何概念。然后您可以将其传递到任意的Boost.geometry算法中,大致如下所示:bg::凸包(view_as_Polygon(pointsMatrix),view_as_Polygon(hullToR))我已经通过了和。我想我正站在解决问题的门槛上。我有一个简短的问题,可以加快我的进度。包中的
as
wrap
扩展在哪里?我在RcppBDT中看到了您是如何做到这一点的,但在中没有看到。我假设必须在
BH
中的某个地方提供这些内容,因为,当您运行示例时。它似乎只是工作,不需要来回转换。事实证明,我需要的许多数据类型都在
BH