Reputation: 2441
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
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;
}
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;
}
intervals.push_back(Interval(4,10,{1,2,"r1"}));
Upvotes: 0
Views: 1860
Reputation: 392911
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?
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.
Upvotes: 3