#include #include #define FLANN_USE_CUDA #include #include #include #include #include #include using namespace flann; float compute_precision(const flann::Matrix& match, const flann::Matrix& indices) { int count = 0; assert(match.rows == indices.rows); size_t nn = std::min(match.cols, indices.cols); for (size_t i=0; i data; flann::Matrix query; flann::Matrix match; flann::Matrix dists; flann::Matrix indices; void SetUp() { printf("Reading test data..."); fflush(stdout); flann::load_from_file(data, "cloud.h5","dataset"); flann::load_from_file(query,"cloud.h5","query"); flann::load_from_file(match,"cloud.h5","indices"); dists = flann::Matrix(new float[query.rows*5], query.rows, 5); indices = flann::Matrix(new int[query.rows*5], query.rows, 5); printf("done\n"); } void TearDown() { delete[] data.ptr(); delete[] query.ptr(); delete[] match.ptr(); delete[] dists.ptr(); delete[] indices.ptr(); } }; TEST_F(Flann_3D, KDTreeSingleTest) { flann::Index > index(data, flann::KDTreeSingleIndexParams(12, false)); start_timer("Building kd-tree index..."); index.buildIndex(); printf("done (%g seconds)\n", stop_timer()); start_timer("Searching KNN..."); index.knnSearch(query, indices, dists, 5, flann::SearchParams(-1) ); printf("done (%g seconds)\n", stop_timer()); float precision = compute_precision(match, indices); EXPECT_GE(precision, 0.99); printf("Precision: %g\n", precision); } TEST_F(Flann_3D, KDTreeCudaTest) { flann::Index > index(data, flann::KDTreeCuda3dIndexParams()); start_timer("Building kd-tree index..."); index.buildIndex(); printf("done (%g seconds)\n", stop_timer()); start_timer("Searching KNN..."); index.knnSearch(query, indices, dists, 5, flann::SearchParams(-1) ); printf("done (%g seconds)\n", stop_timer()); float precision = compute_precision(match, indices); EXPECT_GE(precision, 0.99); printf("Precision: %g\n", precision); } class Flann_3D_Random_Cloud : public FLANNTestFixture { protected: flann::Matrix data; flann::Matrix query; flann::Matrix dists; flann::Matrix indices; flann::Matrix gt_dists; flann::Matrix gt_indices; void SetUp() { const int n_points=10000; printf("creating random point cloud (%d points)...", n_points); data = flann::Matrix(new float[n_points*3], n_points, 3); srand(1); for( int i=0; i(new float[n_points*3], n_points, 3); for( int i=0; i(new float[query.rows*max_nn], query.rows, max_nn); gt_dists = flann::Matrix(new float[query.rows*max_nn], query.rows, max_nn); indices = flann::Matrix(new int[query.rows*max_nn], query.rows, max_nn); gt_indices = flann::Matrix(new int[query.rows*max_nn], query.rows, max_nn); Index > index(data, flann::LinearIndexParams()); start_timer("Building linear index..."); index.buildIndex(); printf("done (%g seconds)\n", stop_timer()); start_timer("Searching KNN..."); index.knnSearch(data, gt_indices, gt_dists, max_nn, flann::SearchParams() ); // for( int i=0; i > index(data, flann::KDTreeCuda3dIndexParams()); start_timer("Building kd-tree index..."); index.buildIndex(); printf("done (%g seconds)\n", stop_timer()); start_timer("Searching KNN..."); indices.cols=1; dists.cols=1; index.knnSearch(query, indices, dists, 1, flann::SearchParams() ); printf("done (%g seconds)\n", stop_timer()); // float precision = compute_precision(gt_indices,indices); float precision = computePrecisionDiscrete(gt_dists,dists, 0); EXPECT_GE(precision, 0.99); printf("Precision: %g\n", precision); } TEST_F(Flann_3D_Random_Cloud, Test4NN) { flann::Index > index(data, flann::KDTreeCuda3dIndexParams()); start_timer("Building kd-tree index..."); index.buildIndex(); printf("done (%g seconds)\n", stop_timer()); start_timer("Searching KNN..."); indices.cols=4; dists.cols=4; index.knnSearch(query, indices, dists, 4, flann::SearchParams() ); printf("done (%g seconds)\n", stop_timer()); // float precision = compute_precision(gt_indices,indices); float precision = computePrecisionDiscrete(gt_dists,dists, 1e-08); EXPECT_GE(precision, 0.99); printf("Precision: %g\n", precision); } TEST_F(Flann_3D_Random_Cloud, Test4NNGpuBuffers) { thrust::host_vector data_host(data.rows); for( int i=0; i data_device = data_host; thrust::host_vector query_host(data.rows); for( int i=0; i query_device = query_host; flann::Matrix data_device_matrix( (float*)thrust::raw_pointer_cast(&data_device[0]),data.rows,3,4*4); flann::Matrix query_device_matrix( (float*)thrust::raw_pointer_cast(&query_device[0]),data.rows,3,4*4); flann::KDTreeCuda3dIndexParams index_params; index_params["input_is_gpu_float4"]=true; flann::Index > index(data_device_matrix, index_params); start_timer("Building kd-tree index..."); index.buildIndex(); printf("done (%g seconds)\n", stop_timer()); thrust::device_vector indices_device(query.rows*4); thrust::device_vector dists_device(query.rows*4); flann::Matrix indices_device_matrix( (int*)thrust::raw_pointer_cast(&indices_device[0]),query.rows,4); flann::Matrix dists_device_matrix( (float*)thrust::raw_pointer_cast(&dists_device[0]),query.rows,4); start_timer("Searching KNN..."); indices.cols=4; dists.cols=4; flann::SearchParams sp; sp.matrices_in_gpu_ram=true; index.knnSearch(query_device_matrix, indices_device_matrix, dists_device_matrix, 4, sp ); printf("done (%g seconds)\n", stop_timer()); flann::Matrix indices_host( new int[ query.rows*4],query.rows,4 ); flann::Matrix dists_host( new float[ query.rows*4],query.rows,4 ); thrust::copy( dists_device.begin(), dists_device.end(), dists_host.ptr() ); thrust::copy( indices_device.begin(), indices_device.end(), indices_host.ptr() ); // float precision = compute_precision(gt_indices,indices); float precision = computePrecisionDiscrete(gt_dists,dists_host, 1e-08); EXPECT_GE(precision, 0.99); printf("Precision: %g\n", precision); delete [] indices_host.ptr(); delete [] dists_host.ptr(); } TEST_F(Flann_3D_Random_Cloud, TestRadiusSearchVector) { flann::Index > index(data, flann::KDTreeCuda3dIndexParams()); start_timer("Building kd-tree index..."); index.buildIndex(); printf("done (%g seconds)\n", stop_timer()); float r = 0.1; std::vector< std::vector > indices; std::vector< std::vector > dists; start_timer("Radius search, r=0.1"); index.radiusSearch( query, indices,dists, r*r, flann::SearchParams() ); printf("done (%g seconds)", stop_timer()); start_timer("verifying results..."); for( int i=0; i > index(data, flann::KDTreeCuda3dIndexParams()); start_timer("Building kd-tree index..."); index.buildIndex(); printf("done (%g seconds)\n", stop_timer()); float r = 0.05; flann::Matrix counts( new int[query.rows], query.rows,1); flann::Matrix dummy( 0,0,0 ); flann::SearchParams counting_params; counting_params.max_neighbors=0; start_timer("counting neighbors..."); index.radiusSearch( query, counts,dummy, r*r, counting_params ); printf("done (%g seconds)", stop_timer()); int max_neighbors=0; for( int i=0; i 0 ); flann::Matrix indices( new int[max_neighbors*query.rows], query.rows, max_neighbors ); flann::Matrix dists( new float[max_neighbors*query.rows], query.rows, max_neighbors ); start_timer("Radius search, r=0.05"); index.radiusSearch( query, indices,dists, r*r, flann::SearchParams() ); printf("done (%g seconds)", stop_timer()); start_timer("verifying results..."); for( int i=0; i > index(data, flann::KDTreeCuda3dIndexParams()); start_timer("Building kd-tree index..."); index.buildIndex(); printf("done (%g seconds)\n", stop_timer()); float r = 0.02; std::vector< std::vector > indices; std::vector< std::vector > dists; start_timer("Radius search, r=0.02..."); index.radiusSearch( query, indices,dists, r*r, flann::SearchParams() ); printf("done (%g seconds)\n", stop_timer()); start_timer("verifying results..."); for( int i=0; i