Coder Social home page Coder Social logo

kdtreepp's Introduction

kdtreepp

A C++ k-d tree implementation

Build Status codecov

Usage

Search for the closest 3D point

#include <Eigen/StdVector>
#include <iostream>
#include <random>
#include <vector>

#include "kdtreepp.hpp"

using Vector3 = Eigen::Vector3d;
using AlignedBox3 = Eigen::AlignedBox3d;

int main() {
  std::vector<Vector3, Eigen::aligned_allocator<Vector3>> points;
  std::mt19937_64 randGen{size_t(42)};
  std::uniform_real_distribution<double> 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<double, 3>(
      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<double>::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

# 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:

make clean
make debug
make test

Enable additional sanitizers to catch hard-to-find bugs, for example:

export LDFLAGS="-fsanitize=address,undefined"
export CXXFLAGS="-fsanitize=address,undefined"

make

License

kdtreepp is licensed under MIT.

Made with hpp-skel.

kdtreepp's People

Contributors

jhurliman avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.