forked from ill-inc/biomes-game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
knn_test.cpp
40 lines (31 loc) · 1 KB
/
knn_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "voxeloo/common/knn.hpp"
#include <algorithm>
#include <random>
#include "catch2/catch.hpp"
#include "voxeloo/common/geometry.hpp"
namespace voxeloo {
static auto random() {
static std::random_device rd;
static std::mt19937 gen(rd());
static std::uniform_real_distribution<float> distrib;
return distrib(gen);
}
TEST_CASE("Test basic BucketKnn usage", "[all]") {
auto shape = vec3(100, 100, 100);
// Generate some random points.
std::vector<Vec3f> points;
for (int i = 0; i < 1000; i += 1) {
points.push_back(shape.to<float>() * vec3(random(), random(), random()));
}
// Build the KNN data structure.
auto knn = knn::make_bucket_knn(shape, points);
for (const auto& query : points) {
auto test = knn.search(query);
auto actual = *std::min_element(
points.begin(), points.end(), [&](const auto& p, const auto& q) {
return square_norm(query - p) < square_norm(query - q);
});
REQUIRE(norm(actual - test) < 1e-3);
}
}
} // namespace voxeloo