KdTree for fast range searching

There are a lot of sources on the Internet for Kd-Trees and here is just one more. I don't suppose there is anything all that special about my Kd-Tree implemenation but if you just need one ready made, go ahead and use it.
I don't support removing objects from the tree. I will have to do some research on that. A Kd-Tree can be built pretty fast though and, in the past, I have just rebuilt them from time to time to take into account changes.
Kd-Trees are an ideal data structure for fast range searching. One important note when building a Kd-Tree is that you should insert your nodes more or less at random if they might possible have any ordering in them to begin with.
This Kd-Tree implementation is hard coded for three dimensions of space. To declare it you simply would do the following:
KdTree mTree;
To add elements to it you simply would do as follows:
mTree.add(x,y,z,radius,myData);
The 'radius' represents the radius of the object you are inserting. For a point, just set it to zero. This first upload doesn't do anything with the radius yet, but I will revise it later.
The 'userData' is a pointer to the object that is being represented or you could cast an integer if it represents an array index.
To find the closest object to a point call 'getNearest'.
To find the N number of closes points, in sorted order, call 'search'.
Here is the implementation. KdTree.h

1 Comments:
At 7:14 AM,
Danimal said…
It would be nice to have a remove or move method if you'd be so kind. Thanks!
Post a Comment
<< Home