具有适应的std :: pair点类型的R-Tree查询

时间:2018-03-11 17:20:43

标签: c++ boost boost-geometry

我正在使用以下(Rcpp)代码进行编译时断言。此代码将10 ^ 6个随机点插入到一个向量中,从该向量构建和r-tree,然后查询以(1 / 3,1 / 3)和(2 / 3,2 / 3)为界的方框落下的点

// [[Rcpp::depends(BH)]]
// [[Rcpp::plugins(cpp11)]]

#include <Rcpp.h>
#include <vector>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/register/box.hpp>
#include <boost/geometry/geometries/register/point.hpp>

namespace bg = boost::geometry;
namespace bgi = boost::geometry::index;

using key_type = std::pair<double, double>;
using box_type = std::pair<key_type, key_type>;
using range_type = std::vector<key_type>;

BOOST_GEOMETRY_REGISTER_POINT_2D(key_type, double,
                                 bg::cs::cartesian,
                                 first, second);

BOOST_GEOMETRY_REGISTER_BOX(box_type, key_type, first, second);

// [[Rcpp::export]]
void test_it()
{
  range_type data;

  for (int i = 0; i != 1e6; ++i)
    data.emplace_back(R::runif(0, 1), R::runif(0, 1));

  using rtree_type = bgi::rtree<key_type, bgi::linear<16>>;

  rtree_type rt(data);

  key_type p1 = std::make_pair(0.33, 0.33),
    p2 = std::make_pair(0.66, 0.66);

  box_type roi = std::make_pair(p1, p2);

  range_type res;

  rt.query(bgi::contains(roi), std::back_inserter(res));

}

在没有最后一个查询行的情况下编译正常。编译器输出是:

In file included from geom_test.cpp:7:
In file included from /usr/local/include/boost/geometry.hpp:17:
In file included from /usr/local/include/boost/geometry/geometry.hpp:50:
In file included from /usr/local/include/boost/geometry/strategies/strategies.hpp:33:
In file included from /usr/local/include/boost/geometry/strategies/disjoint.hpp:24:
In file included from /usr/local/include/boost/geometry/strategies/relate.hpp:23:
/usr/local/include/boost/geometry/strategies/within.hpp:80:5: error: no matching function for call to 'assertion_failed'
    BOOST_MPL_ASSERT_MSG
    ^~~~~~~~~~~~~~~~~~~~
/usr/local/include/boost/mpl/assert.hpp:435:48: note: expanded from macro 'BOOST_MPL_ASSERT_MSG'
#define BOOST_MPL_ASSERT_MSG( c, msg, types_ ) \
                                               ^
/usr/local/include/boost/mpl/assert.hpp:429:9: note: expanded from macro '\
BOOST_MPL_ASSERT_MSG_IMPL'
        boost::mpl::assertion_failed<(c)>( BOOST_PP_CAT(mpl_assert_arg,counter)::assert_arg() ) \
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/local/include/boost/mpl/assert.hpp:60:58: note: expanded from macro '\
BOOST_MPL_AUX_ASSERT_CONSTANT'
#   define BOOST_MPL_AUX_ASSERT_CONSTANT(T, expr) enum { expr }
                                                         ^~~~

1 个答案:

答案 0 :(得分:0)

你说

  

然后查询以(1 / 3,1 / 3)和(2 / 3,2 / 3)为界的方框落下的点

然而,您的代码正在寻找包含&#34;包含&#34;一个盒子。

你可能意味着:

rt.query(bgi::within(roi), std::back_inserter(res));

演示

<强> Live On Coliru

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/register/box.hpp>
#include <boost/geometry/geometries/register/point.hpp>
#include <vector>
#include <random>
#include <iostream>

namespace bg = boost::geometry;
namespace bgi = boost::geometry::index;

using key_type = std::pair<double, double>;
using box_type = std::pair<key_type, key_type>;
using range_type = std::vector<key_type>;

BOOST_GEOMETRY_REGISTER_POINT_2D(key_type, double, bg::cs::cartesian, first, second)
BOOST_GEOMETRY_REGISTER_BOX(box_type, key_type, first, second)

int main() {
    std::mt19937 engine { std::random_device{}() };
    std::uniform_real_distribution<double> dist(0,1.0);

    range_type data;

    for (int i = 0; i != 1e6; ++i)
        data.emplace_back(dist(engine), dist(engine));

    using rtree_type = bgi::rtree<key_type, bgi::linear<16> >;

    rtree_type rt(data);

    box_type const roi {{0.33, 0.33}, {0.66,0.66}};

    range_type res;
    rt.query(bgi::within(roi), std::back_inserter(res));

    std::cout << res.size() << " results\n";
}

打印类似

的内容
109073 results