Jump to content
  • Advertisement
Sign in to follow this  

Nearest neighbour in 3d Kd-Tree

This topic is 4956 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

There has just been a thread about nearest neighbor queries on gdalgorithms. This is what I posted there:

--- snip ---
K-d trees is the standard solution for (k-)nearest neighbor queries.
I would not try to mess around with anything else.

In short, you do the following:

You search the space with a shrinking sphere query, the radius being
determined by the (k-th) closest neighbor found so far. Each node of
the tree is conceptually associated with an n-dimensional box. You
search the tree recursively near to far (where near corresponds to
the node box in which the sphere center lies). You cull far nodes
for which the closest point on the n-dimensional box lies outside
the query sphere. When you reach a leaf, you search all points
inside the leaf.

Code for this is given on pages 320-321 of my book (less the shrinking
sphere bit, which is trivial to add). It's only about 10 lines of code.
--- end snip ---

The nearest neighbor algorithm for k-d trees is also described in the original paper by Friedman et al. as available for download here:


Unfortunately, it's not the most readable of explanations, but its still worth a look.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!