回答问题:Previous Answer: rectangle overlap
我使用interval_map
如下:
我有一组由R = [int start, int end, (int Top, int bottom,string id)]
定义的矩形
N.B:矩形投影到x轴==>线段
源代码:
#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;
}
我发现以下库与icl boost库完成相同的工作,但必须更简单: 下载网站: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(时间间隔(4,10,{1,2,&#34; R 1&#34;}));
答案 0 :(得分:3)
我认为你正试图将inverval库用于它不适合的东西。
您是否看过Boost几何中的rtree
实现?
query
方法支持许多空间谓词:
contains()
covered_by()
covers()
disjoint()
intersects()
overlaps()
within()
其中大多数也可以使用negated
。支持其他标准。作为特殊标准,可以使用boost::geometry::index::nearest()
,这会导致k-最近算法搜索。