1

I'm initializing my rtree with

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();

And then i fill it like this

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 and Item->y are doubles and Item->id is Integer. When I run my code I am getting the following error:

/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.

Could anyone pls give me a hint what went wrong? I have totally no idea.

genpfault
  • 51,148
  • 11
  • 85
  • 139
Lars
  • 11
  • 2
  • I can't make heads or tails out of this. Is an `ItemRelation` a subclass of `Item` now? – sehe May 30 '15 at 15:40
  • 1
    Oh, my mistake. ItemRelation=Item*, like you guessed in the code-example below. Sorry. – Lars May 31 '15 at 14:52

2 Answers2

1

The assertion failure informs you that bounds of a value you're trying to insert are invalid. That is that for some dimension Box's max coordinate is lesser than min coordinate. You may check it before inserting a new value into the 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;
}

For the future, you should learn how to debug your program. The debugger would stop in a place where the assertion fails. You could use the call stack to go into the rtree::insert call in the main function. Then you would be able to see what actually you're trying to insert into the rtree.

Adam Wulkiewicz
  • 2,068
  • 18
  • 24
0

Did you mean

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

(note the missing parens?). See also Most vexing parse: why doesn't A a(()); work?


Beyond this I've seen no such problem:

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)));
        }
    }
}
Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633
  • It was not about the missing parens. I accidentally added them to my code-example. The Problem still persists. – Lars May 31 '15 at 14:45
  • The first item is inserted correctly. The error is happening while inserting the second. – Lars May 31 '15 at 14:57
  • @Lars the code shown runs. It's inserting 6 items. You can see it running live. Please use this to spot the relevant differences. – sehe May 31 '15 at 15:50