Skip to content

Commit

Permalink
WIP tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jlblancoc committed Nov 27, 2023
1 parent efb66d4 commit e01a423
Showing 1 changed file with 83 additions and 7 deletions.
90 changes: 83 additions & 7 deletions tests/test_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,8 @@ void L2_dynamic_vs_bruteforce_test(const size_t nSamples)
}

template <typename NUM>
void L2_concurrent_build_vs_bruteforce_test(const size_t nSamples,
const size_t DIM)
void L2_concurrent_build_vs_bruteforce_test(
const size_t nSamples, const size_t DIM)
{
std::vector<std::vector<NUM>> samples;

Expand All @@ -406,7 +406,8 @@ void L2_concurrent_build_vs_bruteforce_test(const size_t nSamples,
typedef KDTreeVectorOfVectorsAdaptor<std::vector<std::vector<NUM>>, NUM>
my_kd_tree_t;

my_kd_tree_t mat_index(DIM /*dim*/, samples, 10 /* max leaf */, 0 /* concurrent build */);
my_kd_tree_t mat_index(
DIM /*dim*/, samples, 10 /* max leaf */, 0 /* concurrent build */);

// do a knn search
const size_t num_results = 1;
Expand Down Expand Up @@ -443,8 +444,7 @@ void L2_concurrent_build_vs_bruteforce_test(const size_t nSamples,
}

template <typename NUM>
void L2_concurrent_build_vs_L2_test(const size_t nSamples,
const size_t DIM)
void L2_concurrent_build_vs_L2_test(const size_t nSamples, const size_t DIM)
{
std::vector<std::vector<NUM>> samples;

Expand All @@ -464,8 +464,8 @@ void L2_concurrent_build_vs_L2_test(const size_t nSamples,
typedef KDTreeVectorOfVectorsAdaptor<std::vector<std::vector<NUM>>, NUM>
my_kd_tree_t;

my_kd_tree_t mat_index_concurrent_build(DIM /*dim*/, samples,
10 /* max leaf */, 0 /* concurrent build */);
my_kd_tree_t mat_index_concurrent_build(
DIM /*dim*/, samples, 10 /* max leaf */, 0 /* concurrent build */);
my_kd_tree_t mat_index(DIM /*dim*/, samples, 10 /* max leaf */);

// Compare:
Expand Down Expand Up @@ -670,3 +670,79 @@ TEST(kdtree, L2_concurrent_build_vs_L2)
L2_concurrent_build_vs_L2_test<double>(100, 7);
}
}

template <typename NUM>
void L2_rknn_vs_bruteforce_test(
const size_t nSamples, const size_t DIM, const size_t NUM_CLOSEST,
const NUM maxRadiusSqr)
{
std::vector<std::vector<NUM>> samples;

const NUM max_range = NUM(20.0);

// Generate points:
generateRandomPointCloud(samples, nSamples, DIM, max_range);

// Query point:
std::vector<NUM> query_pt(DIM);
for (size_t d = 0; d < DIM; d++)
query_pt[d] = static_cast<NUM>(max_range * (rand() % 1000) / (1000.0));

// construct a kd-tree index:
// Dimensionality set at run-time (default: L2)
// ------------------------------------------------------------
typedef KDTreeVectorOfVectorsAdaptor<std::vector<std::vector<NUM>>, NUM>
my_kd_tree_t;

my_kd_tree_t mat_index(
DIM /*dim*/, samples, 10 /* max leaf */, 0 /* concurrent build */);

// do a knn search
const size_t num_results = NUM_CLOSEST;
std::vector<size_t> ret_indexes(num_results);
std::vector<NUM> out_dists_sqr(num_results);

const auto nFound = mat_index.index->rknnSearch(
&query_pt[0], num_results, ret_indexes.data(), out_dists_sqr.data(),
maxRadiusSqr);

// Brute force:
double min_dist_L2 = std::numeric_limits<double>::max();
size_t min_idx = std::numeric_limits<size_t>::max();
std::map<> xxx;

{
for (size_t i = 0; i < nSamples; i++)
{
double dist = 0.0;
for (size_t d = 0; d < DIM; d++)
dist += (query_pt[d] - samples[i][d]) *
(query_pt[d] - samples[i][d]);
if (dist < min_dist_L2)
{
min_dist_L2 = dist;
min_idx = i;
}
}
ASSERT_TRUE(min_idx != std::numeric_limits<size_t>::max());
}

// Compare:
EXPECT_EQ(min_idx, ret_indexes[0]);
EXPECT_NEAR(min_dist_L2, out_dists_sqr[0], 1e-3);
}

TEST(kdtree, L2_rknn_vs_bruteforce_test)
{
srand(static_cast<unsigned int>(time(nullptr)));
for (int i = 0; i < 10; i++)
{
L2_rknn_vs_bruteforce_test<float>(100, 2);
L2_rknn_vs_bruteforce_test<float>(100, 3);
L2_rknn_vs_bruteforce_test<float>(100, 7);

L2_rknn_vs_bruteforce_test<double>(100, 2);
L2_rknn_vs_bruteforce_test<double>(100, 3);
L2_rknn_vs_bruteforce_test<double>(100, 7);
}
}

0 comments on commit e01a423

Please sign in to comment.