From 549a5e32d482a89202a466c0174a7829fec6dc64 Mon Sep 17 00:00:00 2001 From: Yahor Zabalotski Date: Wed, 30 Oct 2024 14:53:17 +0300 Subject: [PATCH] Fix middleSplit_ for same points --- include/nanoflann.hpp | 4 +++- tests/test_main.cpp | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/include/nanoflann.hpp b/include/nanoflann.hpp index ced16be..05748d9 100644 --- a/include/nanoflann.hpp +++ b/include/nanoflann.hpp @@ -1135,6 +1135,8 @@ class KDTreeBaseClass NodePtr divideTree( Derived& obj, const Offset left, const Offset right, BoundingBox& bbox) { + assert(left < obj.dataset_.kdtree_get_point_count()); + NodePtr node = obj.pool_.template allocate(); // allocate memory const auto dims = (DIM > 0 ? DIM : obj.dim_); @@ -1306,7 +1308,7 @@ class KDTreeBaseClass for (Dimension i = 0; i < dims; ++i) { ElementType span = bbox[i].high - bbox[i].low; - if (span > (1 - EPS) * max_span) + if (span >= (1 - EPS) * max_span) { ElementType min_elem_, max_elem_; computeMinMax(obj, ind, count, i, min_elem_, max_elem_); diff --git a/tests/test_main.cpp b/tests/test_main.cpp index e91abaf..ac27547 100644 --- a/tests/test_main.cpp +++ b/tests/test_main.cpp @@ -815,3 +815,22 @@ TEST(kdtree, L2_concurrent_build_vs_L2) L2_concurrent_build_vs_L2_test(100, 7); } } + +TEST(kdtree, same_points) +{ + using num_t = double; + using point_cloud_t = PointCloud; + using kdtree_t = KDTreeSingleIndexAdaptor< + L2_Simple_Adaptor, point_cloud_t, 3 /* dim */>; + + point_cloud_t cloud; + cloud.pts.resize(16); + for (size_t i = 0; i < 16; ++i) + { + cloud.pts[i].x = -1.; + cloud.pts[i].y = 0.; + cloud.pts[i].z = 1.; + } + + kdtree_t idx(3 /*dim*/, cloud); +}