#include <boost/geometry/geometries/box.hpp>
#include <boost/geometry/index/rtree.hpp>
#include <functional>
#include <iostream>
#include <memory>
#include <utility>
namespace bg = boost::geometry;
namespace bgi = boost::geometry::index;
template <typename T>
using point_cart_2d = bg::model::point<T, 2, bg::cs::cartesian>;
template <typename T>
using box = bg::model::box<T>;
template <typename T>
using payload = std::pair<box<point_cart_2d<double>>, T>;
template <typename T>
using vec = std::vector<T>;
struct guts {
int a = 11;
int b = 22;
};
struct guts_comp {
using result_type = bool;
bool operator()(payload<guts> const &v1, payload<guts> const &v2) const {
return bg::equals(v1.first, v2.first) && v1.second.a == v2.second.a;
}
};
using rtree = bgi::rtree<payload<guts>,
bgi::rstar<16>,
bgi::indexable<payload<guts>>,
guts_comp>;
int main() {
vec<payload<guts>> v = {{{{1.0, 1.0}, {7.0, 4.0}}, {2, 3}}};
rtree rt;
// the poem starts here
for (const auto &k : v) {
rt.insert(k);
}
// or here
std::copy(v.begin(), v.end(), bgi::inserter(rt));
}
我正在尝试将boost :: geometry与自定义类型一起使用,我定义了一个自定义类型和一个operator()来比较std :: pair,我将其放入我的rtree中,但仍然收到错误,但这并没有不编译。
我认为这应该可以,但是不能,在阅读库文档后我不知道为什么。
#include <boost/geometry.hpp>
#include <boost/geometry/index/indexable.hpp>
同时,您编写的很多内容都是多余的,因为默认的IndexableGetter和EqualTo参数已经处理了您选择的有效负载。因此,我建议的简化程序将如下所示:
#include <boost/geometry.hpp> #include <boost/geometry/geometries/point.hpp> #include <boost/geometry/geometries/box.hpp> #include <boost/geometry/index/indexable.hpp> #include <boost/geometry/index/rtree.hpp> #include <utility> struct guts { int a = 11; int b = 22; }; namespace bg = boost::geometry; namespace bgi = bg::index; using point = bg::model::point<double, 2, bg::cs::cartesian>; using box = bg::model::box<point>; using payload = std::pair<box, guts>; using rtree = bgi::rtree<payload, bgi::rstar<16>>; int main() { std::vector v { payload { box { {1.0, 1.0}, {7.0, 4.0} }, guts {2, 3} } }; rtree rt(v); for (const auto& k : v) rt.insert(k); std::copy(v.begin(), v.end(), bgi::inserter(rt)); }
注意:是否真的忽略
guts::b
的意图?在这种情况下,请考虑简单地添加内联运算符重载:
struct guts { int a = 11; int b = 22; bool operator==(guts const& other) const { return a == other.a; } };