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