Click here to Skip to main content
Licence CPOL
First Posted 13 May 2009
Views 14,930
Downloads 193
Bookmarked 17 times

3D Interval KD-Tree

By | 21 May 2009 | Article
A KD-Tree which stores axis aligned boxes.

Introduction

A C# KD-Tree for storing 3D axis aligned bounding boxes easily. KD-Tree is an efficient method for finding bounding boxes in 3d space compared to iterating through all boxes and evaluating their intersection with the search volume. There are two files in the attached download. One which contains the implementation and another for Unit Test cases. You can directly copy these to your projects. 

Background

There seems to be a lack of simple Open Source KD-Tree implementations for C#. This KD-tree implementation was created to be used in a Metaverse Exchange Protocol (MXP) reference implementation. You may find an improvement version from http://www.bubblecloud.org/ under the Source Code section.

Using the code

The KD-Tree is used in a similar manner as a Dictionary.

IntervalKDTree<string> tree = 
  new IntervalKDTree<string>(100, 10);

IList<TestBox> testBoxes = new List<TestBox>();
for (double i = -100; i < 100; i += 0.3)
{
    TestBox box = new TestBox();
    box.minX = i;
    box.minY = i;
    box.minZ = i;
    box.maxX = i + 1;
    box.maxY = i + 2;
    box.maxZ = i + 3;
    box.value = "test" + i;
    testBoxes.Add(box);
    tree.Put(box.minX, box.minY, box.minZ, box.maxX, 
             box.maxY, box.maxZ, box.value);
}

HashSet<string> foundValues;
foundValues = tree.GetValues(-10, -10, -10, 10, 10, 10, 
              new HashSet<string>());

Points of Interest

It seems a KD-Tree can be implemented with a relatively small amount of code. Please help me improve it further by providing bug fixes and improvement ideas. Patches are welcome as well.

History

Initial version and Unit Test case was implemented end of May 2009.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Tommi Laukkanen

Architect

Finland Finland

Member

Web and rich client solutions since 2002 with .NET and Java.
 
Open source contributor to Visual Nunit, OpenSimulator, MXP protocol and IdealistViewer.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionHow do you use it? Pinmembersalasmig1:39 9 Feb '11  
GeneralBug fixed PinmemberTommi Laukkanen10:01 5 Mar '10  
General[My vote of 1] My vote of 1 PinmemberJohnny J.0:36 22 May '09  
GeneralNice, clean implementation PinmemberJean-Paul Mikkers11:29 13 May '09  
GeneralRe: Nice, clean implementation PinmemberTommi Laukkanen8:09 21 May '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 21 May 2009
Article Copyright 2009 by Tommi Laukkanen
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid