错误 - 自定义数据类型作为 boost::geometry 的负载

Error - Custom Data Type as payload with boost::geometry

#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 and EqualTo 参数已经处理了您选择的负载。

因此,我建议的简化程序如下所示:

Live On Coliru

#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));
}

Note:

Was ignoring guts::b really the intent?

In that case, consider simply adding the inline operator overload:

struct guts {
    int a = 11;
    int b = 22;

    bool operator==(guts const& other) const { return a == other.a; }
};