在向量{(100,150),(101,152),(102,151),(105,155),(50,50),(51,55),(55,55)中给出以下一组点,(150,250),(190,260)}
我需要确定相邻点及其数量。让我们说可接受的距离已被设置为5.现在我需要以下输出:点(100,150)的频率,5个单位是4.点(50,50)的频率,5个单位是3个点的频率(150,250)在5个单位内是1个频率的点(190,260),5个单位内是1
我已经尝试过针对此问题的RTree解决方案,但无法确定将所有相邻点排除为候选项的逻辑。手段一旦我确定有四个邻居(100,150),我不想识别那些邻居的邻居。我想继续下一个价值。以下是假设:1。效率是最关注的问题2.向量是未排序的3.向量可能包含数千个点。我正在使用C ++并推动RTree的实现。请指导我如何实现解决方案
下面是代码,它代码计算向量中唯一点的邻居数。一旦确定了一个点的邻居,我需要提供指导。
include set, iostream, boost/geometry.hpp, boost/geometry/geometries/point.hpp, boost/geometry/index/rtree.hpp
using namespace std;
namespace bg = boost::geometry;
namespace bgi = boost::geometry::index;
typedef bg::model::point<int, 2, bg::cs::cartesian> point;
typedef std::pair<point, unsigned> value;
struct ltstr
{
bool operator()(const point &p1, const point &p2) const
{
return (p1.get < 0 >() < p2.get < 0 >() || p1.get < 1 >() < p2.get < 1 >());
}
};
void main()
{
vector<point> candidatePoints{ point(457, 184), point(457, 184), point(457, 184), point(457, 184), point(457, 184),
point(456, 184), point(456, 184), point(456, 184), point(456, 184), point(456, 184),
point(456, 184), point(457, 184), point(457, 184), point(457, 184), point(458, 184), point(459, 185) };
bgi::rtree< value, bgi::quadratic<16> > rtree;
set<point, ltstr> uniqueCandidatePoints;
for (int i = 0; i < candidatePoints.size(); ++i)
{
int x = candidatePoints[i].get < 0 >();
int y = candidatePoints[i].get < 1 >();
uniqueCandidatePoints.insert(point(x, y));
rtree.insert(make_pair(candidatePoints[i], i));
}
for (auto it = uniqueCandidatePoints.begin(); it != uniqueCandidatePoints.end(); ++it)
{
std::vector<value> returnedValues;
point currentItem = *it;
rtree.query(bgi::satisfies([&](value const& v) {return bg::distance(v.first, currentItem) < 5; }),
std::back_inserter(returnedValues));
cout << "Current Item: " << currentItem.get < 0 >() << "," << currentItem.get < 1 >() << "Count: " << returnedValues.size() << endl;
}
getchar();
}
R树是最有用的空间索引数据结构之一,但已证明对特定域和问题有用。话虽如此,这并不是避免说教的理由(毕竟问题可能是对实际问题的简化)。
如果您选择使用R树,则表示您正在执行域分解。就像空间填充曲线一样,您可以对手头的空间进行排序,而节点元素可以在空间附近(您离根越远)。
一个理想的解决方案是以一种形成radius=5
区域的方式构建R-Trees,但这需要自定义数据结构和STR或批量加载算法的自定义,并且类似于聚类算法。
有了boost::index
,你可以identify all neiborhoods,我会尝试详细说明代码:
#include <vector>
#include <iostream>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point.hpp>
#include <boost/geometry/geometries/box.hpp>
#include <boost/geometry/index/rtree.hpp>
namespace bg = boost::geometry;
namespace bgi = boost::geometry::index;
using point = bg::model::point < float, 2, bg::cs::cartesian > ;
Boost R-Trees有一个query
方法。虽然它设计用于执行kNN或重叠等典型查询,但您可以为其提供自定义谓词。在这里我们设计一个返回true
,如果我们查询的点是max_dist
远离base
点(在构造中指定的两个变量)
struct distance_pred
{
point const& _base;
double _threshold;
distance_pred(point const& base, double max_dist)
: _base(base)
, _threshold(max_dist)
{
}
bool operator()(point const& p) const
{
auto d = boost::geometry::distance(_base, p);
return d && d < _threshold;
}
};
// just for output
std::ostream& operator<<(std::ostream &os, point const& p)
{
os << "{ " << p.get<0>() << ", " << p.get<1>() << " }";
return os;
}
对于每一点,我们查询最多位于distance=5
的那些
int main()
{
std::vector<point> cloud {
point(100, 150), point(101, 152), point(102, 151),
point(105, 155), point( 50, 50), point( 51, 55),
point( 55, 55), point(150, 250), point(190, 260)
};
bgi::rtree<point, bgi::quadratic<16>> rtree(cloud.begin(), cloud.end());
std::vector<point> hood;
for (auto &&p : cloud)
{
hood.clear();
std::cout << "neighborhood of point " << p << "\n-----------------\n\n";
rtree.query(bgi::satisfies(distance_pred(p, 5)), std::back_inserter(hood));
// Output the results -----------------------------------------
if (!hood.empty())
{
for (auto &&pt : hood) std::cout << '\t' << pt << std::endl;
}
else
{
std::cout << "\t... is empty\n";
}
std::cout << std::endl;
}
}
如果你想要排除某些东西,我相信聚类算法会更合适,而且超出了RTrees范围。例如,如果由于靠近point1而排出的点恰好靠近point2?
然而,如果你真的想这样做,那只是一个簿记问题。定义一个这样的点
using pointI = std::pair<point, std::size_t>; // remember : geometric info first
并转换为循环
for (std::size_t i(0), i < cloud.size(); ++i)
{
if (cloud.end() != std::find(rec.begin(), rec.end(), i))
{ // you'll only be building neighorhoods for points that are not touched
// queries and recording performed on point2 types
}
}
Full code在这个逻辑中展示了问题:许多社区仍然是空的。
与上面相同可以用更少的代码实现,但更复杂(基本上我们将查询放入lambda函数并使用查询迭代器循环结果)Demo
R-tree只是一个数据结构,但不是算法,对我来说它看起来很复杂。除非你真的需要处理mirco效率,否则我会使用简单的标准算法和点矢量。 std::count_if
将是我的第一个猜测。