5.0

5.0 | 5 ratings Rate this file 112 downloads (last 30 days) File Size: 129.45 KB File ID: #21512

kd-tree for matlab

by Andrea Tagliasacchi

 

22 Sep 2008 (Updated 08 Dec 2008)

Code covered by BSD License  

A kd-tree mex implementation which allows for nearest neighbor, k-nearest neighbor, range and ball q

Download Now | Watch this File

File Information
Description

%------------------ DESCRIPTION -----------------%
kdtree provides a minimalistic implementation of kd-tree.
The implementation can be used either inside MATLAB by means
of MEX calls, or as a standalone tool, directly from a C/C++
program. The image on the website has been creaed with "fulltest.m"

%------------------ FUNCTIONALITIES -----------------%
This implementation offers the following functionalities:
- kdtree_build: k-d tree construction O( n log^2(n) )
- kdtree_delete: frees memory allocated by kdtree
- kdtree_nearest_neighbor: nearest neighbor query (for one or more points)
- kdtree_k_nearest_neighbors: kNN for a single query point
- kdtree_range_query: rectangular range query
- kdtree_ball_query: queries samples withing distance delta from a point

%------------------ FILE STRUCTURE -----------------%
Everyone of the scripts/functions is complete of the following:
*.cpp: the mex implementation of the sources
*.mexmaci: the compiled version of the mex (intel mac)
*.m: the comments that you can browse with the "help" command
*_demo.m: demo file to illustrate the behavior

%------------------ HOW COMPILE -----------------%
IMPORTANT NOTE: I assume you have a correctly configured MEX environment.
Compiling can be done in two ways. The first is directly inside MATLAB.
You can compile manually each of the files by calling the command mex
within the kdtree folder from the MATLAB command line. For example:

>> mex kdtree_build.cpp
 
Alternatively, if you are in a unix environment, you might also be able
to use the provided makefile. In order to do this you need to change some
of the environment variables in order to make them point to your local
MATLAB installation.
 
%------------------ DEVELOPMENT -----------------%
As mentioned the *.cpp files contain a MEX interface for MATLAB.
At the same time, a rich set of examples which run as standalone,
independently from having a MATLAB installation, is provided.
In order to compile them independetly from the MEX environment,
a preprocessor condition -D CPPONLY need to be used.
The makefile uses this flags and compiles sources in both
environments: C++ and MEX.
 
---
Feedback is greatly appreciated.

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
k-D tree

MATLAB release MATLAB 7.6 (R2008a)
Other requirements Requires a working MEX compiler. Runs on all platforms.
Zip File Content  
Other Files
kdtree/,
kdtree/CHANGES,
kdtree/fulltest.m,
kdtree/KDTree.h,
kdtree/kdtree_ball_query.cpp,
kdtree/kdtree_ball_query.m,
kdtree/kdtree_ball_query.mexmaci,
kdtree/kdtree_ball_query_demo.m,
kdtree/kdtree_build.cpp,
kdtree/kdtree_build.m,
kdtree/kdtree_build.mexmaci,
kdtree/kdtree_build_demo.m,
kdtree/kdtree_delete.cpp,
kdtree/kdtree_delete.m,
kdtree/kdtree_delete.mexmaci,
kdtree/kdtree_delete_demo.m,
kdtree/kdtree_k_nearest_neighbor_demo.m,
kdtree/kdtree_k_nearest_neighbors.cpp,
kdtree/kdtree_k_nearest_neighbors.m,
kdtree/kdtree_k_nearest_neighbors.mexmaci,
kdtree/kdtree_k_nearest_neighbors_demo.m,
kdtree/kdtree_nearest_neighbor.cpp,
kdtree/kdtree_nearest_neighbor.m,
kdtree/kdtree_nearest_neighbor.mexmaci,
kdtree/kdtree_nearest_neighbor_demo.m,
kdtree/kdtree_range_query.cpp,
kdtree/kdtree_range_query.m,
kdtree/kdtree_range_query.mexmaci,
kdtree/kdtree_range_query_demo.m,
kdtree/Makefile,
kdtree/MyHeaps.h,
kdtree/README,
kdtree/TODO
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (12)
30 Sep 2008 Gary Tam

Thank you! Work out of the box.

03 Dec 2008 Luigi Giaccari

The most complete Kd-tree on Matlab exchange.
 Thank you Andrea

21 Jan 2009 KIgor Karic

For some reason kdtree_nearest_neighbor takes extremely long time to finish. Are others having the same problem or am I the only one?

03 Feb 2009 Andrea Tagliasacchi

Thanks Kigor, I found a simple bug in the kdtree_nearest_neighbor data conversion. The query was executed MxN times instead of M times; given the query matrix Q is of size(M,N).

The updated kdtree_nearest_neighbor.cpp file can be found at
https://latis.cs.sfu.ca/svn/users/ata2/matlibs/kdtree/

The bug will be corrected in the next release

07 Feb 2009 Bing Jian

How to use the result returned by kdtree_build()
which is a double number on my MATLAB7.5.0?
Does your program require at least MATLAB7.6?
Thanks!

30 Mar 2009 Andrea Tagliasacchi

kdtree_build() returns a pointer to the kdtree. That number needs to be fed as first argument to every other query method. I haven't tried it on older versions of Matlab but I haven't used any of the new releases functionality.

17 May 2009 Florian Brucker

Worked perfectly out of the box after compiling in MATLAB as specified by the docs. Thanks a lot!

One little thing: The help for kdtree_k_nearest_neighbors says that the neighbors are returned with increasing distance order. This seems to be incorrect, my experiments tell me they are returned in decreasing distance order (e.g. if the point itself is part of the tree, then it is returned as the last of the neighbors, not as the first).

27 Aug 2009 lin ming

great work!
note: compile with visual c++ 6.0 will cause fatal error on my computer. However, VS2008 works fine.

09 Sep 2009 sun jin

kdtree_build.cpp
D:\kdtree1.2\kdtree\KDTree.h(144) : error C2374: 'dIdx' : redefinition; multiple initialization
        D:\kdtree1.2\kdtree\KDTree.h(137) : see declaration of 'dIdx'
D:\kdtree1.2\kdtree\KDTree.h(225) : error C2371: 'i' : redefinition; different basic types
        D:\kdtree1.2\kdtree\KDTree.h(223) : see declaration of 'i'
 
  D:\MATLAB\R2008A\BIN\MEX.PL: Error: Compile of 'kdtree_build.cpp' failed.
 
??? Error using ==> mex at 207
Unable to complete successfully.

15 Oct 2009 Dominic

Thanks a lot for this contribution - I've been using it a lot recently and it just works perfectly :)

22 Oct 2009 Dominic

On my System (64bit Ubuntu, Matlab R2009a) there seems to be a memory leak with this implementation:

for i = 1:1000
tree = kdtree_build(rand(1000,1));
end

grabs around 140MB of system memory, according to top. That is never released again. Do I have to release the resources myself in some way or might this be a bug?
I really depend on this implementation, so you would do me a great favour by having a look at it...

26 Oct 2009 Dominic

Ok, Shame on me, there is a method

"kdtree_delete"

that needs to be called for releasing the memory again.

Please login to add a comment or rating.
Updates
24 Sep 2008

The makefile was implying an underlying .mexmaci initialization which is valid only for mac/OSX environments. The mexext program is now used to determine the extension of the build products according to your architecture.

12 Nov 2008

Corrected few bugs in parameter passing
Greatly enhanced the preprocessing speed (tree construction) 10x
Improved examples and makefile structure.
More info in "CHANGES"

08 Dec 2008

Updates some interface bugs and the bugs on distances (second argument) returned by the query functions.

Tag Activity for this File
Tag Applied By Date/Time
kdtree Andrea Tagliasacchi 22 Oct 2008 10:20:36
kd tree Andrea Tagliasacchi 22 Oct 2008 10:20:36
utilities Andrea Tagliasacchi 22 Oct 2008 10:20:36
miscellaneous Andrea Tagliasacchi 22 Oct 2008 10:20:36
utilities Cristina McIntire 12 Dec 2008 15:33:06
mathematics Andrea Tagliasacchi 16 Dec 2008 10:58:06
data structure Andrea Tagliasacchi 22 May 2009 14:42:17
spatial data structure Andrea Tagliasacchi 22 May 2009 14:42:24
kdtree_buildcpp sun jin 09 Sep 2009 02:42:17
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com