将元素插入boost :: geometry :: index :: rtree时获取“可索引无效”

时间:2015-05-30 11:02:32

标签: c++ boost boost-geometry r-tree

我正在用

初始化我的rtree
namespace boostGEO = boost::geometry;
typedef boostGEO::model::point<double, 2, boostGEO::cs::cartesian> point;
typedef boostGEO::model::box<point> box;
typedef std::pair<box, std::pair<int, int>> edgesValue;
boostGEO::index::rtree< edgesValue, boostGEO::index::rstar<16> > tree();

然后我就像这样填写

for( int i = 0; i < items.size(); i++ )
{
    Item* item = items.at(i);
    std::vector<ItemRelation> relations = item->getRelations();

    point ps1( item->x, item->y );
    Item* relatedItem;
    for( int j = 0; j < relations.size(); j++ )
    {
        relatedItem = relations.at(j);
        point ps2( relatedItem->x, relatedItem->y );
        box bounds( ps1, ps2 );
        edgesTree.insert( std::make_pair( bounds, std::make_pair(item->id, relatedItem->id) ) );
    }
}

Item-&gt; x和Item-&gt; y是double,Item-&gt; id是Integer。 当我运行我的代码时,我收到以下错误:

/opt/bp/boost_1_58_0/boost/geometry/index/rtree.hpp:1248: void boost::geometry::index::rtree<Value, Options, IndexableGetter, EqualTo, Allocator>::raw_insert(const value_type&) [with Value = std::pair<boost::geometry::model::box<boost::geometry::model::point<double, 2ul, boost::geometry::cs::cartesian> >, std::pair<int, int> >; Parameters = boost::geometry::index::rstar<16ul>; IndexableGetter = boost::geometry::index::indexable<std::pair<boost::geometry::model::box<boost::geometry::model::point<double, 2ul, boost::geometry::cs::cartesian> >, std::pair<int, int> > >; EqualTo = boost::geometry::index::equal_to<std::pair<boost::geometry::model::box<boost::geometry::model::point<double, 2ul, boost::geometry::cs::cartesian> >, std::pair<int, int> > >; Allocator = std::allocator<std::pair<boost::geometry::model::box<boost::geometry::model::point<double, 2ul, boost::geometry::cs::cartesian> >, std::pair<int, int> > >; boost::geometry::index::rtree<Value, Options, IndexableGetter, EqualTo, Allocator>::value_type = std::pair<boost::geometry::model::box<boost::geometry::model::point<double, 2ul, boost::geometry::cs::cartesian> >, std::pair<int, int> >]: Assertion `(detail::is_valid(m_members.translator()(value)))&&("Indexable is invalid")' failed.

有人可以给我一个暗示出了什么问题吗?我完全不知道。

2 个答案:

答案 0 :(得分:0)

你的意思是

boostGEO::index::rtree<edgesValue, boostGEO::index::rstar<16> > tree;

(请注意丢失的parens?)。另请参阅Most vexing parse: why doesn't A a(()); work?

除此之外,我还没有看到这样的问题:

<强> Live On Coliru

#include <boost/geometry.hpp>
#include <boost/geometry/geometry.hpp>
#include <string>

using namespace boost;
namespace boostGEO = boost::geometry;
typedef boostGEO::model::point<double, 2, boostGEO::cs::cartesian> point;
typedef boostGEO::model::box<point> box;
typedef std::pair<box, std::pair<int, int> > edgesValue;

struct Item;
using ItemRelation = Item*;

struct Item {
    double x, y;
    int id;
    std::vector<ItemRelation> _rel;
    std::vector<ItemRelation> getRelations() const { return {}; }
};

int main() {
    Item item1  { 1, 2, 1, {} },
         item2  { 1, 4, 2, {} },
         item3  { 1, 6, 2, {} },
         item4  { 2, 1, 4, {} },
         item5  { 2, 3, 5, {} },
         item6  { 2, 5, 5, {} };

    std::vector<Item*> items { &item1, &item2, &item3, &item4, &item5, &item6, };

    item3._rel = { &item1, &item2 };
    item6._rel = { &item4, &item5 };

    boostGEO::index::rtree<edgesValue, boostGEO::index::rstar<16> > edgesTree;

    for (size_t i = 0; i < items.size(); i++) {
        Item *item = items.at(i);
        std::vector<ItemRelation> relations = item->getRelations();

        point ps1(item->x, item->y);
        Item *relatedItem;
        for (size_t j = 0; j < relations.size(); j++) {
            relatedItem = relations.at(j);
            point ps2(relatedItem->x, relatedItem->y);
            box bounds(ps1, ps2);
            edgesTree.insert(std::make_pair(bounds, std::make_pair(item->id, relatedItem->id)));
        }
    }
}

答案 1 :(得分:0)

断言失败会通知您,您尝试插入的值的边界无效。也就是说,对于某些尺寸,Box的最大坐标小于min坐标。您可以在将新值插入rtree之前进行检查:

if (bg::get<0>(ps1) > bg::get<0>(ps2) || bg::get<1>(ps1) > bg::get<1>(ps2))
{
    std::cerr << "invalid bounds" << std::endl;
    continue;
}

将来,您应该学习如何调试程序。调试器将在断言失败的地方停止。您可以使用调用堆栈进入rtree::insert函数中的main调用。然后,您将能够看到实际上您要插入rtree的内容。