interval_map boost库

时间:2014-04-24 14:22:27

标签: c++ boost set intervals

回答问题:Previous Answer: rectangle overlap

我使用interval_map如下: 我有一组由R = [int start, int end, (int Top, int bottom,string id)]定义的矩形 N.B:矩形投影到x轴==>线段

  • 我想将这些线段存储在inverval_map结构中
  • 对于查询线段,我想找到与其重叠的所有线段

源代码:

#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库

我发现以下库与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;
}

实施例

  • start = 4;
  • 停止= 10;
  • 结构{1,2,&#34; rc1&#34;};

intervals.push_back(时间间隔(4,10,{1,2,&#34; R 1&#34;}));

1 个答案:

答案 0 :(得分:3)

我认为你正试图将inverval库用于它不适合的东西。

您是否看过Boost几何中的rtree实现?

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

query方法支持许多空间谓词:

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

其中大多数也可以使用negated。支持其他标准。作为特殊标准,可以使用boost::geometry::index::nearest(),这会导致k-最近算法搜索。