添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
# include < nanoflann.hpp >
using namespace nanoflann ;
# include < cstdlib >
# include < ctime >
# include < iostream >
# include " KDTreeVectorOfVectorsAdaptor.h "
const int SAMPLES_DIM = 15 ;
typedef std::vector<std::vector< double >> my_vector_of_vectors_t ;
void generateRandomPointCloud (
my_vector_of_vectors_t & samples, const size_t N, const size_t dim,
const double max_range = 10.0 )
{
std::cout << " Generating " << N << " random points... " ;
samples. resize (N);
for ( size_t i = 0 ; i < N; i++)
{
samples[i]. resize (dim);
for ( size_t d = 0 ; d < dim; d++)
samples[i][d] = max_range * ( rand () % 1000 ) / ( 1000.0 );
}
std::cout << " done \n " ;
}
void kdtree_demo ( const size_t nSamples, const size_t dim)
{
my_vector_of_vectors_t samples;
const double max_range = 20 ;
// Generate points:
generateRandomPointCloud (samples, nSamples, dim, max_range);
// Query point:
std::vector< double > query_pt (dim);
for ( size_t d = 0 ; d < dim; d++)
query_pt[d] = max_range * ( rand () % 1000 ) / ( 1000.0 );
// construct a kd-tree index:
// Dimensionality set at run-time (default: L2)
// ------------------------------------------------------------
typedef KDTreeVectorOfVectorsAdaptor< my_vector_of_vectors_t , double >
my_kd_tree_t ;
my_kd_tree_t mat_index (dim /* dim */ , samples, 10 /* max leaf */ );
// do a knn search
const size_t num_results = 3 ;
std::vector< size_t > ret_indexes (num_results);
std::vector< double > out_dists_sqr (num_results);
nanoflann::KNNResultSet< double > resultSet (num_results);
resultSet. init (&ret_indexes[ 0 ], &out_dists_sqr[ 0 ]);
mat_index. index -> findNeighbors (resultSet, &query_pt[ 0 ]);
std::cout << " knnSearch(nn= " << num_results << " ): \n " ;
for ( size_t i = 0 ; i < resultSet. size (); i++)
std::cout << " ret_index[ " << i << " ]= " << ret_indexes[i]
<< " out_dist_sqr= " << out_dists_sqr[i] << std::endl;
}
int main ()
{
// Randomize Seed
srand ( static_cast < unsigned int >( time ( nullptr )));
kdtree_demo ( 1000 /* samples */ , SAMPLES_DIM /* dim */ );
}