Giter Club home page Giter Club logo

kdtree's Introduction

KDTree

Swift 3.1 Version License Platform Carthage compatible CI Status

Swift implementation of a k-dimensional binary space partitioning tree. The KDTree is implemented as an immutable enum, inspired by functional trees from objc.io. KDTree algorithm according to Wikipedia and ubilabs js example.

If you want to know more, you can watch this talk I gave at funswiftconf.

Visual animation of creating a KDTree:

Example Illustration

Very simply put, a tree is created by:

  1. find the median point in the current direction
  2. bisect along the hyperplane(=line in 2D) going through the current median
  3. go one step deeper, same thing with next axis

The blue lines go through nodes that partition the plane vertically, the red ones for horizontal partitions.

Usage

Import the package in your *.swift file:

import KDTree

Make sure your data values conforom to

public protocol KDTreePoint: Equatable {
  static var dimensions: Int { get }
  func kdDimension(dimension: Int) -> Double
  func squaredDistance(otherPoint: Self) -> Double
}

(CGPoint conforms to KDTreePoint as part of the package)

Then you can grow your own Tree:

extension CustomDataPoint: KDTreePoint { ... }

let dataValues: [CustomDataPoint] = ...

var tree: KDTree<CGPoint> = KDTree(values: dataValues)

Then you can insert(), remove(), map(), filter(), reduce() and forEach() on this tree with the expected results, as KDTree conforms to Sequence.

Applications

K-Nearest Neighbour:

Example Illustration

Given a KDTree:

var tree: KDTree<CGPoint> = KDTree(values: points)

we can retrieve the nearest Neighbour to a test point like so

let nearest: CGPoint? = tree.nearest(to: point)

or the get the 10 nearest neighbours

let nearestPoints: [CGPoint] = tree.nearestK(10, to: point)

Complexity is O(log N), while brute-force searching through an Array is of cource O(N).

Preliminary performance results can be gained by running the unit tests, the load example has 10.000 random points in [-1,1]x[-1,1] and find the nearest points for 500 test points:

Performance Results

Tesselations:

Tesselation Example

Range-Search:

It's also possible to use the KDTree to search for a range of values using:

let pointsInRange: [CGPoint] = tree.elementsInRange([(0.2, 0.4), (0.45, 0.75)])

One example is based on the HYG Database of 120 thousend stars. Here we see a piece of the sky around 10h right ascension and 35° declination where the KDTree algorithm can be used to both get the stars in the area the iPhone screen is pointing at and also find the closest Star to a position the user is tapping via NN:

Examples of Implementation

The example app in the Example folder shows some demo implementations on iOS.

KDTree can also be used as a package in a server-side-swift app. StarsOnKitura is one example that returns stars from the HYG database of 120'000 stars using ascension/declination parameters. Live available running on a 64MB instance on IBM Bluemix.

Installation

Cocoapods

KDTree is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "KDTree"

To run the example project, clone the repo, and run pod install from the Example directory first.


Swift package manager

Add the following to your Package.swift dependencies

.Package(url: "https://github.com/Bersaelor/KDTree", majorVersion: 0, minor: 5),

Carthage

To add KDTree using Carthage add the following to your Cartfile:

github "Bersaelor/KDTree"

License

KDTree is available under the MIT license. See the LICENSE file for more info.

kdtree's People

Contributors

bersaelor avatar aplusm avatar hsnetzer avatar

Watchers

Enrique Galindo avatar James Cloos avatar Agustin Hernandez avatar Rodrigo Lavista avatar Diego Ernst avatar Joaquin Rocco avatar Santiago avatar Mathias Claassen avatar Mike avatar Karina Fernandez avatar Alvaro Señorale avatar Joaquin Aguirre avatar  avatar

Forkers

isabella232

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.