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

问题描述 投票:1回答:2

我正在初始化我的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-> x和Item-> y是double,Item-> 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.

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

c++ boost boost-geometry r-tree
2个回答
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)));
        }
    }
}

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的内容。

© www.soinside.com 2019 - 2024. All rights reserved.