jlblancoc/nanoflann

nanoflann: a C++11 header-only library for Nearest Neighbor (NN) search with KD-trees

nanoflann: A Fast C++ Library for Nearest-Neighbor Search

nanoflann is a header-only C++ library for building KD-trees — a data structure that organizes points in space to quickly find the closest points to a given query. It's designed for speed and memory efficiency, especially for point clouds and other geometric data.

What Does It Do?

  • Builds KD-trees from datasets of points (2D, 3D, or N-dimensional).
  • Searches for nearest neighbors — given a query point, it finds the closest points in the dataset.
  • Supports two main search methods:
    • knnSearch(): find the k nearest neighbors.
    • radiusSearch(): find all points within a given radius.
  • Returns exact results by default; an optional approximate mode (with a tolerance parameter eps) is available.
  • Can handle dynamic point clouds that change over time (e.g., sliding-window LiDAR maps) without rebuilding the entire tree from scratch.
  • Supports non-Euclidean spaces like rotation groups (SO(2), SO(3)), SE(2), SE(3), spheres, tori, and products of these — all with exact geodesic distances.
  • Can save and load built indices to disk for later use.

Key Features

  • Header-only: No compilation or installation needed — just include nanoflann.hpp in your code.
  • Fast: Uses template metaprogramming (CRTP) and inlined methods to avoid virtual function overhead, making it faster than the original FLANN library.
  • Memory-efficient: Works directly with your data via an adaptor interface — no need to copy the dataset into a separate structure.
  • Flexible: Works with many data types (including Eigen::Matrix, std::vector, custom classes) and supports custom distance metrics.
  • Thread-safe for querying.

How It Works

  • You provide a class that gives nanoflann access to your data (via an adaptor).
  • nanoflann builds a KD-tree index from that data.
  • You then query the index for nearest neighbors.
  • The library uses templates to let the compiler optimize loops and avoid runtime overhead.

What It Can't Do

  • Only supports the distance metrics listed (L1, L2, and the manifold metrics) — no other custom metrics beyond those.
  • Only a C++ interface — no C, MATLAB, or Python bindings.
  • No automatic algorithm configuration (like the original FLANN's auto-tuning).

Installation

  • Easiest: Clone the repository and copy include/nanoflann.hpp into your project.
  • Package managers:
    • Debian/Ubuntu: sudo apt install libnanoflann-dev
    • macOS (Homebrew): brew install nanoflann
    • macOS (MacPorts): sudo port install nanoflann
    • conda-forge: conda install -c conda-forge nanoflann
  • CMake: You can also use it via CMake's find_package or by adding the source directory.

Usage Examples

The README provides many example programs, including:

  • Basic point cloud search (pointcloud_example.cpp)
  • Dynamic point clouds (dynamic_pointcloud_example.cpp)
  • Manifold searches (SO(2), SO(3), SE(3), S²) — require C++17
  • Custom distance metrics and result sets
  • Working directly with Eigen::Matrix or std::vector

Additional Notes

  • License: BSD (open source).
  • Origin: A fork of the FLANN library, with performance improvements.
  • Citing: If used in research, please cite the paper listed in the README.
  • ROS 2: Available as a vendor package for several ROS 2 distributions (Humble, Jazzy, etc.).

For full details, see the README and the Doxygen documentation.

Related

  • Project
  • Project
  • Project
  • Project
  • Project