c – Nanoflann半径搜索
作者:互联网
我对nanoflann的radiusSearch函数中的参数search_radius有疑问.我的代码是这样的:
#include <iostream>
#include <vector>
#include <map>
#include "nanoflann.hpp"
#include "Eigen/Dense"
int main()
{
Eigen::MatrixXf mat(7, 2);
mat(0,0) = 0.0; mat(0,1) = 0.0;
mat(1,0) = 0.1; mat(1,1) = 0.0;
mat(2,0) = -0.1; mat(2,1) = 0.0;
mat(3,0) = 0.2; mat(3,1) = 0.0;
mat(4,0) = -0.2; mat(4,1) = 0.0;
mat(5,0) = 0.5; mat(5,1) = 0.0;
mat(6,0) = -0.5; mat(6,1) = 0.0;
std::vector<float> query_pt(2);
query_pt[0] = 0.0;
query_pt[1] = 0.0;
typedef nanoflann::KDTreeEigenMatrixAdaptor<Eigen::MatrixXf> KDTree;
KDTree index(2, mat, 10);
index.index->buildIndex();
{ // Find nearest neighbors in radius
const float search_radius = 0.1f;
std::vector<std::pair<size_t, float> > matches;
nanoflann::SearchParams params;
const size_t nMatches = index.index->radiusSearch(&query_pt[0], search_radius, matches, params);
std::cout << "RadiusSearch(): radius = " << search_radius << " -> "
<< nMatches << " matches" << std::endl;
for(size_t i = 0; i < nMatches; i++)
std::cout << "Idx[" << i << "] = " << matches[i].first
<< " dist[" << i << "] = " << matches[i].second << std::endl;
std::cout << std::endl;
}
}
我想要的是在半径为0.1的范围内,所以,我所期望的是矩阵中的前三个元素,但令我惊讶的是它返回了前5个元素.检查距离返回在我看来它不是实际距离而是距离平方(对吗?)所以我将半径平方以得到我所期望的但不幸的是它只返回第一个点.
所以我将半径从0.1 ^ 2 = 0.01增加到0.02,最后得到了我想要的点数.
现在,问题是,是否应该包括在邻里周边的点?我在哪里可以改变nanoflann的这种情况?
解决方法:
KDTreeEigenMatrixAdaptor starts like this的完整定义:
template <class MatrixType, int DIM = -1,
class Distance = nanoflann::metric_L2,
typename IndexType = size_t>
struct KDTreeEigenMatrixAdaptor
{
//...
所以,是的:默认度量是欧几里德平方距离,L2_Adaptor结构,and documented as follows:
Squared Euclidean distance functor (generic version, optimized for high-dimensionality data sets).
至于第二个问题,有两个方面.首先,当谈到浮点数时,你不应该依赖于平等(强制性参考:David Goldberg, What every computer scientist should know about floating-point arithmetic, ACM Computing Surveys, 1991).
其次是原则上你是对的. nanoflann基于FLANN,其中的源代码可以找到CountRadiusResultSet类的实现,由radiusSearch搜索方法使用.其关键方法有以下实现:
void addPoint(DistanceType dist, size_t index)
{
if (dist<radius) {
count++;
}
}
然而,似乎该问题的共同定义涉及“小于或等于”,例如在以下参考文献(Matthew T. Dickerson, David Eppstein, Algorithms for Proximity Problems in Higher Dimensions, Computational Geometry, 1996)中:
Problem 1. (Fixed-Radius Near-Neighbors Search) Given a finite set S of n distinct points in Rd and a distance
标签:nearest-neighbor,kdtree,c 来源: https://codeday.me/bug/20190830/1768234.html