meshsearch¶
meshsearch is a C++17 library, with Python bindings, for exact neighbour
searches in three-dimensional point sets. On ten million points, on the
machine of the performance page, it builds its index seven to
eight and a half times faster than scipy’s cKDTree at a cell side of one
mean separation, finds the nearest object 1.6 to 2.0 times faster, and
answers radius queries on clustered data 5.8 times faster.
It achieves this with a cell list, a uniform mesh of cubic cells over the domain, so that each query examines only the cells that can hold a result. Results are reproducible: the same input gives the same results, in the same order, on Linux and macOS, on x86-64 and ARM. Any axis of the domain may be periodic.
Install¶
Python (3.10 or later; numpy is the only dependency):
pip install meshsearch
Wheels are published for glibc Linux (x86_64 and aarch64) and macOS (arm64 and x86_64); elsewhere pip builds the module from source, which needs CMake 3.16 or later and a C++17 compiler. The wheels include free-threaded CPython 3.14t, where the module runs without re-enabling the GIL (threads). Built and tested on Linux (GCC, Clang) and macOS (Apple clang); other platforms are untested.
C++, with CMake 3.16 or later. Build and install the library:
cmake -S . -B build
cmake --build build
cmake --install build --prefix /path/to/prefix
The install puts the static library libmeshsearch.a in lib/ (or the
platform’s library directory), the headers meshsearch/MeshGrid.h and
meshsearch/Version.h in include/, and the CMake package in
lib/cmake/meshsearch/. Find it from your own project, configured with
-DCMAKE_PREFIX_PATH=/path/to/prefix if the prefix is not a standard one:
find_package(meshsearch 1.0 CONFIG REQUIRED)
target_link_libraries(your_target PRIVATE meshsearch::meshsearch)
Or build it as part of your project, under the same target name:
include(FetchContent)
FetchContent_Declare(meshsearch
GIT_REPOSITORY https://github.com/SimoSartori/meshsearch.git
GIT_TAG v1.0.0)
FetchContent_MakeAvailable(meshsearch)
target_link_libraries(your_target PRIVATE meshsearch::meshsearch)
The option MESHSEARCH_INSTALL (on by default) determines whether meshsearch adds
its install rules to the build that includes it. Its tests and examples
(MESHSEARCH_BUILD_TESTS, MESHSEARCH_BUILD_EXAMPLES) are built by default
only when meshsearch is the top-level project.
Or use the meshsearch that pip installed: its wheel carries the headers, the
sources and a CMake package that builds meshsearch::meshsearch in your tree.
Configure with -Dmeshsearch_DIR="$(python -m meshsearch --cmake-dir)"; a
scikit-build-core package that lists meshsearch among its build requirements
finds it with no setting. The
guide
explains both.
Built as part of your project, by FetchContent or by the CMake package of a
pip install, meshsearch takes your build type: configure with
-DCMAKE_BUILD_TYPE=Release, or another optimised configuration, or it
compiles unoptimised.
A request for version 1.0 accepts any 1.x release. Within 1.x the interface stays source-compatible: code written against 1.0 compiles with every later 1.x, but must be recompiled.
A first grid¶
In Python, a grid is built from three coordinate arrays, the limits of the box and a cell side. Objects are identified by their position in the arrays:
import numpy as np
import meshsearch
rng = np.random.default_rng(1)
x, y, z = rng.random((3, 1000)) # 1000 points in the unit cube
grid = meshsearch.MeshGrid(x, y, z, 0.2, limits=[[0, 1], [0, 1], [0, 1]])
grid.nearest_object(0.5, 0.5, 0.5) # the index of the nearest object
grid.nearest_objects(10, 0.5, 0.5, 0.5) # the ten nearest, nearest first
grid.close_objects(0.5, 0.5, 0.5, 0.15) # every object within 0.15
grid.count_objects(0.5, 0.5, 0.5, 0.15) # how many there are
qx, qy, qz = rng.random((3, 200)) # 200 query points at once
nearest = grid.nearest_object(qx, qy, qz) # one index per query, in order
The same in C++:
#include "meshsearch/MeshGrid.h"
#include <array>
#include <cstdio>
#include <random>
#include <vector>
int main ()
{
std::mt19937 gen(1);
std::uniform_real_distribution<double> unit(0., 1.);
std::vector<double> X, Y, Z;
for (int i=0; i<1000; ++i) {
X.push_back(unit(gen));
Y.push_back(unit(gen));
Z.push_back(unit(gen));
}
meshsearch::MeshGrid::Options options;
options.limits = std::array<std::array<double,2>,3>{{{0.,1.}, {0.,1.}, {0.,1.}}};
meshsearch::MeshGrid grid(X, Y, Z, 0.2, options);
const unsigned int nearest = grid.nearestObject(0.5, 0.5, 0.5);
const std::vector<unsigned int> ten = grid.nearestObjects(10, 0.5, 0.5, 0.5);
const std::vector<unsigned int> ball = grid.closeObjects(0.5, 0.5, 0.5, 0.15);
const size_t counted = grid.countObjects(0.5, 0.5, 0.5, 0.15);
std::printf("nearest %u, %zu objects within 0.15\n", nearest, ball.size());
return ten.size() == 10 && ball.size() == counted ? 0 : 1;
}
The cell side, 0.2 here, is two mean separations of these points, the typical distance between neighbours. The guide explains how to choose it, and why the limits are given explicitly.
The pages¶
Guide: the concepts and their use, in the order a new user needs them, with starting values.
C++ reference and Python reference: every member, its arguments and the errors it raises.
Names side by side: the C++ and Python spelling of each member.
Performance: the cost of construction, memory and queries, against scipy’s
cKDTreeand scikit-learn’sBallTree.