Skip to content

Commit 2993ade

Browse files
committed
Added KdTree build options:
* Updated the split rule class template to be a constructor argument. * Builds can now terminate based maximum leaf depth instead of maximum leaf size. * The bounds of the input space can be customized.
1 parent 68ecf53 commit 2993ade

File tree

20 files changed

+296
-145
lines changed

20 files changed

+296
-145
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ KdTree:
3131
* Support for topological spaces with identifications. E.g., points on the circle `[-pi, pi]`.
3232
* Available distance functions: `L1`, `L2Squared`, `LInf`, `SO2`, and `SE2Squared`.
3333
* Metrics can be customized.
34-
* Multiple tree splitting rules: `kLongestMedian`, `kMidpoint` and `kSlidingMidpoint`.
34+
* Multiple tree splitting rules: `median_max_side_t`, `midpoint_max_side_t` and `sliding_midpoint_max_side_t`.
3535
* Compile time and run time known dimensions.
3636
* Static tree builds.
3737
* Thread safe queries.

examples/benchmark/bm_pico_kd_tree.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,17 @@ using PicoKdTreeRtSldMid = pico_tree::KdTree<PicoRtSpace<PointX>>;
2626
// ****************************************************************************
2727

2828
BENCHMARK_DEFINE_F(BmPicoKdTree, BuildCtSldMid)(benchmark::State& state) {
29-
int max_leaf_size = state.range(0);
29+
pico_tree::max_leaf_size_t max_leaf_size =
30+
static_cast<std::size_t>(state.range(0));
3031

3132
for (auto _ : state) {
3233
PicoKdTreeCtSldMid<PointX> tree(points_tree_, max_leaf_size);
3334
}
3435
}
3536

3637
BENCHMARK_DEFINE_F(BmPicoKdTree, BuildRtSldMid)(benchmark::State& state) {
37-
int max_leaf_size = state.range(0);
38+
pico_tree::max_leaf_size_t max_leaf_size =
39+
static_cast<std::size_t>(state.range(0));
3840

3941
for (auto _ : state) {
4042
PicoKdTreeRtSldMid<PointX> tree(
@@ -58,8 +60,9 @@ BENCHMARK_REGISTER_F(BmPicoKdTree, BuildRtSldMid)
5860
// ****************************************************************************
5961

6062
BENCHMARK_DEFINE_F(BmPicoKdTree, KnnCtSldMid)(benchmark::State& state) {
61-
int max_leaf_size = state.range(0);
62-
int knn_count = state.range(1);
63+
pico_tree::max_leaf_size_t max_leaf_size =
64+
static_cast<std::size_t>(state.range(0));
65+
std::size_t knn_count = static_cast<std::size_t>(state.range(1));
6366

6467
PicoKdTreeCtSldMid<PointX> tree(points_tree_, max_leaf_size);
6568

@@ -105,7 +108,8 @@ BENCHMARK_REGISTER_F(BmPicoKdTree, KnnCtSldMid)
105108
// ****************************************************************************
106109

107110
BENCHMARK_DEFINE_F(BmPicoKdTree, RadiusCtSldMid)(benchmark::State& state) {
108-
int max_leaf_size = state.range(0);
111+
pico_tree::max_leaf_size_t max_leaf_size =
112+
static_cast<std::size_t>(state.range(0));
109113
Scalar radius = static_cast<Scalar>(state.range(1)) / Scalar(10.0);
110114
Scalar squared = radius * radius;
111115

@@ -143,7 +147,8 @@ BENCHMARK_REGISTER_F(BmPicoKdTree, RadiusCtSldMid)
143147
// ****************************************************************************
144148

145149
BENCHMARK_DEFINE_F(BmPicoKdTree, BoxCtSldMid)(benchmark::State& state) {
146-
int max_leaf_size = state.range(0);
150+
pico_tree::max_leaf_size_t max_leaf_size =
151+
static_cast<std::size_t>(state.range(0));
147152
Scalar radius = static_cast<Scalar>(state.range(1)) / Scalar(10.0);
148153

149154
PicoKdTreeCtSldMid<PointX> tree(points_tree_, max_leaf_size);
@@ -172,7 +177,8 @@ BENCHMARK_REGISTER_F(BmPicoKdTree, BoxCtSldMid)
172177
->Args({14, 15});
173178

174179
BENCHMARK_DEFINE_F(BmPicoKdTree, BoxRtSldMid)(benchmark::State& state) {
175-
int max_leaf_size = state.range(0);
180+
pico_tree::max_leaf_size_t max_leaf_size =
181+
static_cast<std::size_t>(state.range(0));
176182
Scalar radius = static_cast<Scalar>(state.range(1)) / Scalar(10.0);
177183

178184
PicoKdTreeRtSldMid<PointX> tree(

examples/eigen/eigen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ using PointsMapRowMajor = Eigen::Map<Eigen::Matrix<
2323
std::size_t const kRunCount = 1024 * 1024;
2424
std::size_t const kNumPoints = 1024 * 1024 * 2;
2525
float const kArea = 1000.0;
26-
std::size_t const kMaxLeafCount = 16;
26+
pico_tree::max_leaf_size_t const kMaxLeafCount = 16;
2727

2828
template <typename PointX>
2929
std::vector<PointX> GenerateRandomEigenN(

examples/kd_forest/kd_forest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void RunKdTree(
4444
std::vector<Vector_> const& train,
4545
std::vector<Vector_> const& test,
4646
std::string const& fn_nns_gt,
47-
std::size_t tree_max_leaf_size,
47+
pico_tree::max_leaf_size_t tree_max_leaf_size,
4848
std::vector<pico_tree::Neighbor<int, Scalar_>>& nns) {
4949
using Space = std::reference_wrapper<std::vector<Vector_> const>;
5050

examples/kd_tree/kd_tree_creation.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,17 @@ auto MakePointSet() { return Space{{0.0f, 1.0f, 2.0f}, {3.0f, 4.0f, 5.0f}}; }
4040
// In this example, the creation of a KdTree results in a copy of the input
4141
// point set. The KdTree has full ownership of the copy.
4242
void BuildKdTreeWithCopy() {
43-
int max_leaf_size = 12;
4443
auto points = MakePointSet();
4544

46-
pico_tree::KdTree<Space> tree(points, max_leaf_size);
45+
pico_tree::KdTree<Space> tree(points, pico_tree::max_leaf_size_t(12));
4746

4847
QueryTree(tree);
4948
}
5049

5150
// In this example, the point sets are not copied but moved into the KdTrees
5251
// when they are created. Each tree has full ownership of the moved point set.
5352
void BuildKdTreeWithMove() {
54-
int max_leaf_size = 12;
53+
pico_tree::max_leaf_size_t max_leaf_size = 12;
5554
auto points = MakePointSet();
5655

5756
pico_tree::KdTree<Space> tree1(std::move(points), max_leaf_size);
@@ -66,7 +65,7 @@ void BuildKdTreeWithMove() {
6665
// only a reference is copied by a KdTree. Each tree only has shallow ownership
6766
// of the input point set.
6867
void BuildKdTreeWithReference() {
69-
int max_leaf_size = 12;
68+
pico_tree::max_leaf_size_t max_leaf_size = 12;
7069
auto points = MakePointSet();
7170

7271
// By reference.
@@ -83,7 +82,7 @@ void BuildKdTreeWithReference() {
8382
// This example shows that the type of the input point set may be deduced by the
8483
// compiler.
8584
void SpaceTypeDeduction() {
86-
int max_leaf_size = 12;
85+
pico_tree::max_leaf_size_t max_leaf_size = 12;
8786
auto points = MakePointSet();
8887

8988
// The type of the first class template argument, the space type, is

examples/kd_tree/kd_tree_custom_point_type.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,10 @@ struct PointTraits<PointXYZ> {
3131
} // namespace pico_tree
3232

3333
int main() {
34-
std::size_t max_leaf_size = 12;
3534
std::vector<PointXYZ> points{{0.0f, 1.0f, 2.0f}, {3.0f, 4.0f, 5.0f}};
3635

3736
pico_tree::KdTree<std::reference_wrapper<std::vector<PointXYZ>>> tree(
38-
points, max_leaf_size);
37+
points, pico_tree::max_leaf_size_t(12));
3938

4039
PointXYZ query{4.0f, 4.0f, 4.0f};
4140
pico_tree::Neighbor<int, float> nn;

examples/kd_tree/kd_tree_custom_search_visitor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ int main() {
4747
using PointX = Point2f;
4848
using Scalar = typename PointX::ScalarType;
4949

50-
std::size_t max_leaf_size = 12;
50+
pico_tree::max_leaf_size_t max_leaf_size = 12;
5151
std::size_t point_count = 1024 * 1024;
5252
Scalar area_size = 1000;
5353

examples/kd_tree/kd_tree_custom_space_type.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,11 @@ struct SpaceTraits<std::deque<std::array<Scalar_, Dim_>>> {
3737
} // namespace pico_tree
3838

3939
int main() {
40-
std::size_t max_leaf_size = 1;
4140
std::deque<std::array<float, 2>> points{
4241
{0.0f, 1.0f}, {2.0f, 3.0f}, {4.0f, 5.0f}};
4342

4443
pico_tree::KdTree<std::reference_wrapper<std::deque<std::array<float, 2>>>>
45-
tree(points, max_leaf_size);
44+
tree(points, pico_tree::max_leaf_size_t(1));
4645

4746
std::array<float, 2> query{4.0f, 4.0f};
4847
pico_tree::Neighbor<int, float> nn;

examples/kd_tree/kd_tree_dynamic_arrays.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ void ArrayOfScalars() {
2222
// argument: The spatial dimension known at run time.
2323
pico_tree::SpaceMap<pico_tree::PointMap<double, Dim>> map(data.get(), count);
2424

25-
std::size_t max_leaf_size = 3;
2625
pico_tree::KdTree<pico_tree::SpaceMap<pico_tree::PointMap<double, Dim>>> tree(
27-
map, max_leaf_size);
26+
map, pico_tree::max_leaf_size_t(3));
2827

2928
// If Dim equals pico_tree::kDynamicSize, then PointMap will need a 2nd
3029
// argument: The spatial dimension known at run time.
@@ -52,9 +51,8 @@ void ArrayOfPoints() {
5251

5352
pico_tree::SpaceMap<std::array<double, Dim>> map(data.get(), count);
5453

55-
std::size_t max_leaf_size = 3;
5654
pico_tree::KdTree<pico_tree::SpaceMap<std::array<double, Dim>>> tree(
57-
map, max_leaf_size);
55+
map, pico_tree::max_leaf_size_t(3));
5856

5957
std::size_t index = 1;
6058
std::array<double, Dim> const& query = data[index];

examples/kd_tree/kd_tree_minimal.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// used for storing points and a point can be either an array or an std::array.
1010

1111
int main() {
12-
std::size_t max_leaf_size = 12;
12+
pico_tree::max_leaf_size_t max_leaf_size = 12;
1313
std::vector<std::array<float, 3>> points{
1414
{0.0f, 1.0f, 2.0f}, {3.0f, 4.0f, 5.0f}};
1515

0 commit comments

Comments
 (0)