Research
nanoflann: A Header-Only KD-Tree Library for Exact Nearest-Neighbor Search on Compile-Time Product Manifolds
Many quantities in robotics — headings, 3D orientations, rigid-body poses — do not live in ordinary Euclidean space: angles wrap at ±π, and a quaternion and its negation denote the same rotation.

- arXiv
- 2610.15215 · accepted · ROBOTICS AND AUTOMATION LETTERS
- Published
- 2026-10-10
- Authors
- José Luis Blanco-Claraco
In plain words
Many quantities in robotics — headings, 3D orientations, rigid-body poses — do not live in ordinary Euclidean space: angles wrap at ±π, and a quaternion and its negation denote the same rotation. Running a standard Euclidean KD-tree on their raw coordinates therefore returns wrong neighbors, because both the distance metric and the tree's pruning logic assume flat space.
AI summary
Overview
Research area: Robotics / computational geometry — nearest-neighbor (NN) search data structures for non-Euclidean state spaces, distributed as a C++ library.
Technical level: Intermediate. The paper assumes familiarity with KD-trees, branch-and-bound pruning, and basic Lie-group terminology (SO(2), SO(3), SE(3), quaternions), but the core ideas are explained geometrically.
Scope: The paper presents and evaluates nanoflann's extension from a Euclidean KD-tree into a header-only, dependency-free library that performs exact nearest-neighbor search on arbitrary product manifolds declared at compile time, plus an incremental self-balancing index for point sets under heavy insertion/deletion churn.
What This Paper Is About
Many quantities in robotics — headings, 3D orientations, rigid-body poses — do not live in ordinary Euclidean space: angles wrap at ±π, and a quaternion and its negation denote the same rotation. Running a standard Euclidean KD-tree on their raw coordinates therefore returns wrong neighbors, because both the distance metric and the tree's pruning logic assume flat space. The paper's goal is to make exact manifold-aware NN search a drop-in capability of a general-purpose KD-tree: the user declares the space's topology with a single C++ type alias, and the library synthesizes a distance function whose pruning is provably safe.
Key Contributions
-
A compile-time, composable product-manifold abstraction. Base spaces (ℝⁿ, SO(2), SO(3) as unit quaternions, the unit sphere Sⁿ) and arbitrary products (SE(2), SE(3), n-tori, SO(3)ᵏ, ℝ³ × S² for oriented points) are declared with one type alias and flattened at compile time into a per-coordinate topology table (
Linear,Circular,Quaternion) that costs no run-time storage. -
A coordinate-separable admissible pruning bound with zero extra per-node storage. The bound is wrap-aware for circular coordinates and double-cover-aware for quaternion blocks, and keeps the tree's node layout and memory identical to the ordinary Euclidean KD-tree — prior schemes stored per-node box projections. For quaternion blocks the separable form is too loose, so the paper reuses MPNN's block-level chordal bound (explicitly credited to Yershova and LaValle, not claimed as new) but realizes it storage-free, reconstructing the block's four intervals from the split scalars (
divLow,divHigh) the tree already stores. Exactness is proved in Lemma 1 for any weighted product metric with a per-block-monotone combination rule. -
Adapted query algorithms, including a region-carrying exact search that reconstructs child intervals from stored split values and handles the circle's ±π seam with no extra per-node storage; the tight block bound enters only at the prune gate (Eq. 3), so the O(1)-per-level Euclidean-style update is preserved.
-
An incremental self-balancing single-tree index for point sets under heavy churn — a from-scratch re-implementation of the published ikd-Tree algorithms with weight-balanced partial rebuilds, box-trim deletion, bounded memory, and an optional asynchronous rebalancing thread. Its per-subtree AABBs are exactly the regions the admissibility proof needs, so exactness carries over under churn and background rebuilds.
An alternative Bentley–Saxe forest implementation is also provided for comparison (Euclidean-only spaces).
Main Findings
-
Exactness confirmed: For SO(2), S², SO(3), SE(3) and SO(3)², with sizes up to N = 10⁶, the index returns distances identical to an exhaustive scan under the same product metric (recall 1.0, machine precision), validating Lemma 1.
-
The Euclidean-on-raw-coordinates shortcut fails in a structured way: recall collapses as ≈ 0.5^m for m rotation blocks — 0.50 for SO(3), 0.49 for SE(3), 0.26 for SO(3)². This is the signature of the quaternion double cover q ~ −q.
-
The failure is localized, not rare: on SO(2), naive recall rounds to 1.0 at N = 10⁶ under uniform queries. Restricting queries to a band around ±π of ten times the mean nearest-neighbor gap exposes it: naive drops to 0.97, 0.99 and 0.88 at N = 10³, 10⁵, 10⁶, while the proposed tree stays at 1.00.
-
Sign convention strongly affects the naive baseline: on SO(3) at N = 10⁵, naive recall is 0.49 with random quaternion signs but 0.99 with canonicalized (q_w ≥ 0) signs — canonicalization moves the seam rather than removing it. The proposed tree is insensitive: recall 1.00 and 16.9 / 17.9 / 17.6 μs under random, canonicalized and sign-continuous conventions (<6% spread).
-
Large speedups over brute force at N = 10⁶ (Table II): SO(2) 3.3 μs vs 4573 μs (1399×); S² 6.0 μs vs 2936 μs (493×); SO(3) 139 μs vs 3826 μs (27.6×); SE(3) 96 μs vs 5252 μs (54.7×); SO(3)² 747 μs vs 6505 μs (8.7×).
-
The block-level correction is essential on rotation-heavy spaces: without it, the separable per-coordinate bound inflates query time ~30× (SO(3) at N = 10⁵: 450 μs vs 15 μs with Eq. 3). Without a quaternion block, the correction is compiled out.
-
Competitive as a plain Euclidean KD-tree: on 3D KITTI Velodyne scans at N = 2×10⁶, exact queries take 1.17 μs (pico_tree 1.08, FLANN 1.18) and builds 0.22 s (pico_tree 0.19 s), versus 3.6 ms per brute-force query. At d = 128 every exact KD-tree, including this one (1.9 ms/query), approaches brute force (7.3 ms), while approximate methods stay at 46–169 μs at recall ≈ 0.99.
-
Head-to-head against prior exact-manifold libraries (N = 10⁵, Table III, SO(3) with shared geodesic ground truth): nigh is fastest at 2.4 μs; the proposed tree takes 15 μs with a 14 ms build; MPNN takes 45 μs with a 195 ms build. With the block-level bound the proposed tree is ~3× faster than MPNN and within 6–9× of nigh at N = 3×10⁵ (29 μs vs 3.3 μs).
-
The SE(3) comparison is not like-for-like: the proposed tree minimizes ‖Δt‖² + w_R²·d²_chordal, while nigh's
SE3Spaceuses the unsquared sum ‖Δt‖ + θ. Neither is monotone in the other; on uniform data they pick the same neighbor in only 0.53–0.58 of queries. Each is exact for its own metric (recall 1.00), so the authors explicitly draw no ranking conclusion — only cost: 4.2 μs vs 9.5 μs at N = 10⁵, 16.1 vs 17.1 μs at 3×10⁵, with builds ~2× faster. -
Loop-closure case study (partially reported): the evaluation uses KITTI sequences 00, 02, 05, 06, 08 and MulRan KAIST01, DCC01, Riverside01 decimated to a keyframe every 1 m, retrieving k = 8 neighbors outside a 100-frame temporal exclusion window under the weighted chordal product metric. The paper states the common workarounds lose precisely the loop-closure candidates; the numerical results of this study fall beyond the provided excerpt and are not reported here.
-
Incremental index results: the paper states it achieves bounded memory and faster updates than prior work, with an optional asynchronous tail-latency-bounding mode, and compares against the original ikd-Tree. The specific update-time and memory figures appear in a section not included in the provided text, so they are not reported here.
Methodology in Plain English
The starting point is a standard KD-tree: a binary tree that recursively splits points along the axis of largest spread, storing only a split dimension and two scalars per node, and answering queries by descending to the near child first and skipping the far child whenever a lower bound on its distance exceeds the current k-th best. That skipping is only sound if the bound never over-estimates the distance to the far region.
The paper's method is to replace that single lower-bound quantity with a topology-aware one, per coordinate:
- For a linear coordinate, keep the classical gap-to-interval bound.
- For a circular coordinate, measure the wrapped distance to the interval's endpoints, so a neighbor hiding across the ±π seam is not discarded. Because one cut value is no longer enough, the search carries the running per-coordinate region down the recursion, splitting it at each node's slab and saving/restoring it in O(1) per level.
- For a quaternion block, take the smaller of the two ambient point-to-box distances — one from the query, one from its antipode −v — which accounts for the fact that q and −q are the same rotation. The block's intervals are reconstructed from the split values already stored, so memory stays identical to a Euclidean tree.
A proof (Lemma 1) shows that summing these per-coordinate bounds never exceeds the true product-metric distance to any point in the subtree, so pruning is safe and the search is exact. The proof also covers arbitrary positive weights per block and any combination rule that is non-decreasing in each argument (including an L1 variant).
For changing point sets, the authors re-implement the ikd-Tree design: lazy tombstones for deletion, subtree axis-aligned bounding boxes for O(1) box-trim removal as a sliding window moves, and weight-balanced partial rebuilds that reclaim tombstoned nodes through a pooled allocator. An optional background thread builds a fresh tree from a snapshot while foreground operations are logged and replayed before the swap, so queries always see one complete index.
Evaluation uses an AMD Ryzen 7 2700X (8C/16T, 3.7 GHz), 48 GB RAM, Ubuntu 24.04, GCC 13.3 with -O3 -march=native, a maximum of 10 points per leaf, and timings averaged over 2000 queries after warm-up (<3% spread over 5 repetitions). Ground truth on manifolds is an exhaustive scan under the same metric; baselines (pico_tree, FLANN, libnabo, PCL, HNSW, MPNN, nigh, ikd-Tree) are pinned to specific commits in a public benchmark repository.
Why This Matters
Robotics code is full of places where someone feeds quaternions or wrapped angles into a Euclidean KD-tree and gets subtly wrong answers — not randomly wrong, but wrong exactly near the seams and antipodes where loop closures and revisits live. This paper turns "be exact on a manifold" from a planning-library speciality into a two-line change in a general-purpose header-only library, with proof, benchmark, and no memory penalty relative to the plain tree.
Real-world applications:
- SLAM and loop-closure retrieval: nearest-keyframe lookups over SE(3) trajectories, where the retrieval maintains a growing keyframe database and must not lose candidates across sign or angle seams.
- LiDAR odometry and mapping: sliding-window local maps under constant insertion and deletion, served by the incremental index with bounded memory and box-trim removal.
- Point-cloud registration and data association: correspondence search on oriented points (ℝ³ × S²) and on rigid poses, where orientation is part of the distance.
- Sampling-based motion planning: steering and nearest-vertex queries in configuration spaces that are products of Euclidean and rotational factors.
Industry relevance: the library is header-only and dependency-free, so integration cost is low for existing C++ robotics stacks; the demonstrated Euclidean performance is on par with pico_tree and FLANN on KITTI data, meaning adopting the manifold capability does not require giving up baseline speed. The incremental index targets the same niche as ikd-Tree, which underpins LiDAR-inertial odometry systems such as FAST-LIO2.
Future Directions
- Closing the gap to intrinsic methods on pure rotation. nigh's intrinsic S³ partition remains 6–9× faster on SO(3); a tighter quaternion bound that does not rely on the separable-plus-gate construction is the obvious next target, since the paper itself measures a ~30× penalty when the block correction is omitted.
- Reconciling SE(3) metric definitions. Because the proposed squared-chordal metric and nigh's unsquared ‖Δt‖ + θ agree on only ~0.55 of queries, users have no principled guidance on which to pick; characterizing when each ordering better matches a downstream task (loop closure, registration) is open.
- Extending the base-manifold set further. The design admits Sⁿ and products containing it; other candidates (e.g. other Lie groups or non-unit-scaled quaternion blocks) would test how far the "declare the topology, synthesize the bound" abstraction generalizes.
- Characterizing the asynchronous mode under real workloads. The paper proves exactness holds for every interleaving of the background rebuild and foreground operations, but the temporary imbalance during a rebuild and the roughly doubled memory footprint remain practical trade-offs worth quantifying on live mapping data.
Target Audience
Robotics and SLAM engineers who maintain C++ nearest-neighbor code and need correct queries on poses or orientations; library authors and performance engineers interested in exact NN data structures; and researchers in motion planning or registration who want manifold NN without adopting a planning-specific library. Readers need moderate comfort with KD-trees and Lie-group notation; the geometric arguments are presented with figures and worked examples.
Authors’ abstract
Nearest-neighbor (NN) search underlies countless robotics algorithms, but many state spaces are not Euclidean: orientations live on $SO(2)$ or $SO(3)$ and poses on $SE(2)$ or $SE(3)$, and a Euclidean KD-tree on their raw coordinates returns wrong neighbors. We present a header-only, dependency-free C++ KD-tree library for exact NN search on user-defined product manifolds declared at compile time: the space is composed from base manifolds ($\mathbb{R}^n$, $SO(2)$, $SO(3)$,…
The full abstract on arXivRelated papers
- Projection-Retraction MPPI: Exact Constraint-Manifold Control for Manipulators 2026-08-04 · cs.RO · Key terms of the title in common