Question

In response to question: Previous Answer: rectangle overlap

I am using the interval_map as follows: I have a set of rectangles defined by R = [int start, int end, (int Top, int bottom,string id)] N.B: Rectangles are projected onto the x-axis==>line segments

  • I want to store these Line segments in an inverval_map structure
  • For a query line segment I want to find all the line segments that overlap with it

Source code:

#include "boost/icl/interval.hpp"
#include "boost/icl/interval_map.hpp"
#include <set>
using namespace std;

struct Position{
    int top;
    int bottom;
    string id;
};

int main(int argc, char* argv[])
{
    std::set<Position> ids1;
    ids1.insert({10,10,"line1"});
    std::set<Position> ids2;
    ids2.insert({200,10,"line2"});
    boost::icl::interval_map<int, std::set<Position>> mymap;
    auto i1 = boost::icl::interval<int>::closed(2, 7);
    auto i2 = boost::icl::interval<int>::closed(3, 8);
    mymap += make_pair(i1, ids1);
    mymap += make_pair(i2, ids2);

    return 0;
}

Another Library, and a solution but by not using the icl boost library

I found the following library which does the same work as the icl boost library but must simpler: download site: Interval Tree

#include <iostream>
#include <fstream>
#include "IntervalTree.h"

using namespace std;

struct Position
{
    int x;
    int y;
    string id;
};

int main()
{
    vector<Interval<Position>> intervals;
    intervals.push_back(Interval<Position>(4,10,{1,2,"r1"}));
    intervals.push_back(Interval<Position>(6,10,{-6,-3,"r2"}));
    intervals.push_back(Interval<Position>(8,10,{5,6,"r3"}));

    vector<Interval<Position> > results;
    vector<string> value;
    int start = 4;
    int stop = 10;

    IntervalTree<Position> tree(intervals);
   // tree.findContained(start, stop, results);
    tree.findOverlapping(start, stop, results);
    cout << "found " << results.size() << " overlapping intervals" << endl;
}

Example

  • start = 4;
  • Stop = 10;
  • structure {1,2,"rc1"};

intervals.push_back(Interval(4,10,{1,2,"r1"}));

Was it helpful?

Solution

I think you're trying to use the inverval library for something it's not quite designed for.

Have you looked at the rtree implementation from Boost Geometry?

http://www.boost.org/doc/libs/1_55_0/libs/geometry/doc/html/geometry/reference/spatial_indexes/boost__geometry__index__rtree.html

The query method supports a number of spatial predicates:

  • contains()
  • covered_by()
  • covers()
  • disjoint()
  • intersects()
  • overlaps()
  • within()

Most of which can also be used negated. Additional criteria are supported. As a special criterion boost::geometry::index::nearest() can be used, which results in a k-nearest algorithm search.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top