上次是 knnsearch 自定義要找到最近的幾個點,
這次是找半徑內的所有點~


#include <cmath>
#include <cstdio>
#include <flann/flann.hpp>
#include <boost/timer/timer.hpp>
#include <boost/random/linear_congruential.hpp>
#include <boost/random/uniform_int.hpp>
#include <boost/random/uniform_real.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/generator_iterator.hpp>
#include <boost/random/mersenne_twister.hpp>

int main(int argc, char** argv)
{
    boost::random::mt19937 rng;
    boost::random::uniform_real_distribution<> uniform_rand(-10000, 10000);
    // 你要找幾個最近點用nn
    int nn = 100;
    // 資料總數
    const int rows = 30000;
    // 資料維度
    const int cols = 3;
    // 查找的位置數
    const int qsize = 2;
    double* rdata = new double[cols * rows];
    double* qdata = new double[cols * qsize];
    // 產生亂數資料
    {
        boost::timer::auto_cpu_timer t;
        for (int i = 0; i < rows; ++i)
        {
            for (int j = 0; j < cols; ++j)
            {
                rdata[i * cols + j] = uniform_rand(rng) * 0.01f;
            }
        }
        for (int i = 0; i < qsize; ++i)
        {
            for (int j = 0; j < cols; ++j)
            {
                qdata[i * cols + j] = uniform_rand(rng) * 0.01f;
                printf("%d %f\n", i * cols + j, qdata[i * cols + j]);
            }
        }
    }
    // 初始化
    flann::Matrix<double> dataset(rdata, rows, cols);
    flann::Matrix<double> query(qdata, qsize, cols);
    flann::Matrix<int> indices(new int[query.rows * nn], query.rows, nn);
    flann::Matrix<double> dists(new double[query.rows * nn], query.rows, nn);
    // construct an randomized kd-tree index using 4 kd-trees
    flann::Index<flann::L2<double> > index(dataset, flann::KDTreeIndexParams(1));
    {
        boost::timer::auto_cpu_timer t;
        index.buildIndex();
    }
    int res = 0;
    printf("index.knnSearch\n");
    {
        boost::timer::auto_cpu_timer t;
        res = index.radiusSearch(query, indices, dists, 100, flann::SearchParams());
    }
    for (int i = 0; i < 2; ++i)
    {
        for (int j = 0; j < nn; ++j)
        {
            int idx = indices[i][j];
            if (idx > 0)
            {
                printf("index:%d distance:%.2f position:(%.2f, %.2f, %.2f)\n",
                       idx, dists[i][j], rdata[idx * cols], rdata[idx * cols + 1],
                       rdata[idx * cols + 2]);
            }
            else // idx == -1
            {
                break;
            }
        }
    }
    delete[] dataset.ptr();
    delete[] query.ptr();
    delete[] indices.ptr();
    delete[] dists.ptr();
    return 0;
}

arrow
arrow
    全站熱搜

    讓地獄深紅的天亮 發表在 痞客邦 留言(0) 人氣()