forked from Jaybro/pico_tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkd_tree_dynamic_arrays.cpp
72 lines (58 loc) · 2.25 KB
/
kd_tree_dynamic_arrays.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <memory>
#include <pico_tree/array_traits.hpp>
#include <pico_tree/kd_tree.hpp>
#include <pico_tree/map_traits.hpp>
// This example shows how to work with dynamic size arrays. Support is provided
// for working with an array of scalars or an array of points.
void ArrayOfScalars() {
std::size_t count = 6;
constexpr std::size_t Dim = 2;
// Here we create an array of scalars that will be interpreted as a set of
// points: {{0, 1}, {2, 3}, ...}
std::unique_ptr<double[]> data = std::make_unique<double[]>(count * Dim);
for (std::size_t i = 0; i < (count * Dim); ++i) {
data[i] = static_cast<double>(i);
}
// If Dim equals pico_tree::kDynamicSize, then SpaceMap will need a 3rd
// argument: The spatial dimension known at run time.
pico_tree::SpaceMap<pico_tree::PointMap<double, Dim>> map(data.get(), count);
std::size_t max_leaf_size = 3;
pico_tree::KdTree<pico_tree::SpaceMap<pico_tree::PointMap<double, Dim>>> tree(
map, max_leaf_size);
// If Dim equals pico_tree::kDynamicSize, then PointMap will need a 2nd
// argument: The spatial dimension known at run time.
std::size_t index = 2;
pico_tree::PointMap<double, Dim> query(data.get() + index * Dim);
pico_tree::Neighbor<int, double> nn;
tree.SearchNn(query, nn);
// Prints index 2.
std::cout << "Index closest point: " << nn.index << std::endl;
}
void ArrayOfPoints() {
std::size_t count = 6;
constexpr std::size_t Dim = 2;
// Here we create an array of points: {{0, 1}, {2, 3}, ...}
std::unique_ptr<std::array<double, Dim>[]> data =
std::make_unique<std::array<double, Dim>[]>(count);
for (std::size_t i = 0; i < count; ++i) {
for (std::size_t j = 0; j < Dim; ++j) {
data[i][j] = static_cast<double>(i * Dim + j);
}
}
pico_tree::SpaceMap<std::array<double, Dim>> map(data.get(), count);
std::size_t max_leaf_size = 3;
pico_tree::KdTree<pico_tree::SpaceMap<std::array<double, Dim>>> tree(
map, max_leaf_size);
std::size_t index = 1;
std::array<double, Dim> const& query = data[index];
pico_tree::Neighbor<int, double> nn;
tree.SearchNn(query, nn);
// Prints index 1.
std::cout << "Index closest point: " << nn.index << std::endl;
}
int main() {
ArrayOfScalars();
ArrayOfPoints();
return 0;
}