https://github.com/jhurliman/kdtreepp
k-d tree for C++ and Eigen
https://github.com/jhurliman/kdtreepp
cpp17-library eigen kdtree
Last synced: 2 months ago
JSON representation
k-d tree for C++ and Eigen
- Host: GitHub
- URL: https://github.com/jhurliman/kdtreepp
- Owner: jhurliman
- License: mit
- Created: 2021-01-27T06:14:06.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2021-02-12T00:48:23.000Z (over 4 years ago)
- Last Synced: 2025-01-28T22:19:41.559Z (4 months ago)
- Topics: cpp17-library, eigen, kdtree
- Language: CMake
- Homepage:
- Size: 69.3 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# kdtreepp
### A C++ k-d tree implementation
[](https://travis-ci.com/jhurliman/kdtreepp)
[](https://codecov.io/gh/jhurliman/kdtreepp)## Usage
### Search for the closest 3D point
```cpp
#include
#include
#include
#include#include "kdtreepp.hpp"
using Vector3 = Eigen::Vector3d;
using AlignedBox3 = Eigen::AlignedBox3d;int main() {
std::vector> points;
std::mt19937_64 randGen{size_t(42)};
std::uniform_real_distribution dist{-1000.0, 1000.0};// Make random points
points.resize(size_t(5000));
for (auto& point : points) {
point << dist(randGen), dist(randGen), dist(randGen);
}// Construct a k-d tree from 3d points
const auto node = kdtreepp::MakeEigenKdTreeNode(
points.begin(), points.end(), [](const Vector3& p) { return p; },
[](const Vector3& p) { return p; });// Create a random query point
const Vector3 queryPoint{dist(randGen), dist(randGen), dist(randGen)};// Find the closest point to the given query point
double minDistSq = std::numeric_limits::max();
Vector3 closestPoint;
node.visit(
[&minDistSq, queryPoint](const AlignedBox3& bounds) {
return bounds.squaredExteriorDistance(queryPoint) < minDistSq;
},
[&minDistSq, &closestPoint, queryPoint](const Vector3& point) {
const double rSq = (point - queryPoint).squaredNorm();
if (rSq < minDistSq) {
minDistSq = rSq;
closestPoint = point;
}
});std::cout << "Closest point to " << queryPoint << " is " << closestPoint << "\n";
}
```## Test
```shell
# build test binaries
make# run tests
make test# run bench tests
make bench
```The default test binaries will be built in release mode. You can make Debug test binaries as well:
```shell
make clean
make debug
make test
```Enable additional sanitizers to catch hard-to-find bugs, for example:
```shell
export LDFLAGS="-fsanitize=address,undefined"
export CXXFLAGS="-fsanitize=address,undefined"make
```# License
kdtreepp is licensed under [MIT](https://opensource.org/licenses/MIT).
Made with [hpp-skel](https://github.com/mapbox/hpp-skel).