[flann] 02/21: Imported Upstream version 1.8.4

Leopold Palomo-Avellaneda leo at alaxarxa.net
Fri Jul 15 13:51:25 UTC 2016


This is an automated email from the git hooks/post-receive script.

lepalom-guest pushed a commit to branch master
in repository flann.

commit f14785c232d2b22cc2ee644e8350fdeb35d7d6b0
Author: Leopold Palomo-Avellaneda <leopold.palomo at upc.edu>
Date:   Fri Jul 15 12:50:44 2016 +0200

    Imported Upstream version 1.8.4
---
 CMakeLists.txt                                     |  207 +++
 COPYING                                            |   14 +
 ChangeLog                                          |   42 +
 README.md                                          |   43 +
 bin/download_checkmd5.py                           |   69 +
 bin/indent.sh                                      |    5 +
 bin/make_release.sh                                |    6 +
 bin/run_test.py                                    |   18 +
 bin/uncrustify.cfg                                 |  132 ++
 cmake/CMakeLists.txt                               |    6 +
 cmake/FindFlann.cmake                              |   28 +
 cmake/UseLATEX.cmake                               |  813 +++++++++++
 cmake/flann.pc.in                                  |   13 +
 cmake/flann_utils.cmake                            |  117 ++
 cmake/uninstall_target.cmake.in                    |   19 +
 doc/CMakeLists.txt                                 |   19 +
 doc/images/cmake-gui.png                           |  Bin 0 -> 26975 bytes
 doc/manual.pdf                                     |  Bin 0 -> 285221 bytes
 doc/manual.tex                                     | 1517 ++++++++++++++++++++
 doc/references.bib                                 |  210 +++
 examples/CMakeLists.txt                            |   39 +
 examples/README                                    |    6 +
 examples/flann_example.c                           |  114 ++
 examples/flann_example.cpp                         |   36 +
 examples/flann_example_mpi.cpp                     |  131 ++
 src/CMakeLists.txt                                 |   10 +
 src/cpp/CMakeLists.txt                             |  139 ++
 src/cpp/flann/algorithms/all_indices.h             |  197 +++
 src/cpp/flann/algorithms/autotuned_index.h         |  759 ++++++++++
 src/cpp/flann/algorithms/center_chooser.h          |  228 +++
 src/cpp/flann/algorithms/composite_index.h         |  239 +++
 src/cpp/flann/algorithms/dist.h                    |  787 ++++++++++
 .../algorithms/hierarchical_clustering_index.h     |  705 +++++++++
 src/cpp/flann/algorithms/kdtree_cuda_3d_index.cu   |  842 +++++++++++
 src/cpp/flann/algorithms/kdtree_cuda_3d_index.h    |  327 +++++
 src/cpp/flann/algorithms/kdtree_cuda_builder.h     |  729 ++++++++++
 src/cpp/flann/algorithms/kdtree_index.h            |  760 ++++++++++
 src/cpp/flann/algorithms/kdtree_single_index.h     |  698 +++++++++
 src/cpp/flann/algorithms/kmeans_index.h            | 1066 ++++++++++++++
 src/cpp/flann/algorithms/linear_index.h            |  163 +++
 src/cpp/flann/algorithms/lsh_index.h               |  548 +++++++
 src/cpp/flann/algorithms/nn_index.h                |  903 ++++++++++++
 src/cpp/flann/config.h                             |   38 +
 src/cpp/flann/config.h.in                          |   38 +
 src/cpp/flann/defines.h                            |  155 ++
 src/cpp/flann/flann.cpp                            |  885 ++++++++++++
 src/cpp/flann/flann.h                              |  472 ++++++
 src/cpp/flann/flann.hpp                            |  435 ++++++
 src/cpp/flann/flann_cpp.cpp                        |   30 +
 src/cpp/flann/general.h                            |  227 +++
 src/cpp/flann/io/hdf5.h                            |  231 +++
 src/cpp/flann/mpi/client.h                         |   89 ++
 src/cpp/flann/mpi/flann_mpi_client.cpp             |   85 ++
 src/cpp/flann/mpi/flann_mpi_server.cpp             |   26 +
 src/cpp/flann/mpi/index.h                          |  271 ++++
 src/cpp/flann/mpi/matrix.h                         |   54 +
 src/cpp/flann/mpi/queries.h                        |  103 ++
 src/cpp/flann/mpi/server.h                         |  153 ++
 src/cpp/flann/nn/ground_truth.h                    |   98 ++
 src/cpp/flann/nn/index_testing.h                   |  323 +++++
 src/cpp/flann/nn/simplex_downhill.h                |  186 +++
 src/cpp/flann/util/allocator.h                     |  202 +++
 src/cpp/flann/util/any.h                           |  292 ++++
 src/cpp/flann/util/cuda/heap.h                     |  139 ++
 src/cpp/flann/util/cuda/result_set.h               |  536 +++++++
 src/cpp/flann/util/cutil_math.h                    | 1328 +++++++++++++++++
 src/cpp/flann/util/dynamic_bitset.h                |  163 +++
 src/cpp/flann/util/heap.h                          |  456 ++++++
 src/cpp/flann/util/logger.h                        |  137 ++
 src/cpp/flann/util/lsh_table.h                     |  506 +++++++
 src/cpp/flann/util/matrix.h                        |  135 ++
 src/cpp/flann/util/object_factory.h                |   91 ++
 src/cpp/flann/util/params.h                        |  139 ++
 src/cpp/flann/util/random.h                        |  145 ++
 src/cpp/flann/util/result_set.h                    |  936 ++++++++++++
 src/cpp/flann/util/sampling.h                      |   72 +
 src/cpp/flann/util/saving.h                        |  135 ++
 src/cpp/flann/util/serialization.h                 |  449 ++++++
 src/cpp/flann/util/timer.h                         |   95 ++
 src/matlab/CMakeLists.txt                          |   82 ++
 src/matlab/flann_build_index.m                     |   56 +
 src/matlab/flann_free_index.m                      |   35 +
 src/matlab/flann_load_index.m                      |   33 +
 src/matlab/flann_save_index.m                      |   34 +
 src/matlab/flann_search.m                          |   84 ++
 src/matlab/flann_set_distance_type.m               |   45 +
 src/matlab/nearest_neighbors.cpp                   |  643 +++++++++
 src/matlab/test_flann.m                            |  218 +++
 src/python/CMakeLists.txt                          |   12 +
 src/python/pyflann/__init__.py                     |   31 +
 src/python/pyflann/exceptions.py                   |   34 +
 src/python/pyflann/flann_ctypes.py                 |  378 +++++
 src/python/pyflann/index.py                        |  402 ++++++
 src/python/setup.py.tpl                            |   28 +
 test/CMakeLists.txt                                |   74 +
 test/flann_autotuned_test.cpp                      |  187 +++
 test/flann_cuda_test.cu                            |  511 +++++++
 test/flann_hierarchical_test.cpp                   |  108 ++
 test/flann_kdtree_single_test.cpp                  |  305 ++++
 test/flann_kdtree_test.cpp                         |  134 ++
 test/flann_kmeans_test.cpp                         |  144 ++
 test/flann_linear_test.cpp                         |  107 ++
 test/flann_lsh_test.cpp                            |  115 ++
 test/flann_multithreaded_test.cpp                  |  311 ++++
 test/flann_tests.h                                 |  685 +++++++++
 test/memusage_clustering.py                        |   75 +
 test/memusage_nn.py                                |   77 +
 test/test_clustering.py                            |   95 ++
 test/test_index_save.py                            |  106 ++
 test/test_nn.py                                    |  118 ++
 test/test_nn_autotune.py                           |   77 +
 test/test_nn_index.py                              |   81 ++
 112 files changed, 27984 insertions(+)

diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 0000000..3279915
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,207 @@
+cmake_minimum_required(VERSION 2.6)
+
+if(COMMAND cmake_policy)
+    cmake_policy(SET CMP0003 NEW)
+endif(COMMAND cmake_policy)
+
+project(flann)
+string(TOLOWER ${PROJECT_NAME} PROJECT_NAME_LOWER)
+
+include(${PROJECT_SOURCE_DIR}/cmake/flann_utils.cmake)
+set(FLANN_VERSION 1.8.4)
+DISSECT_VERSION()
+GET_OS_INFO()
+
+# CMAKE_C_COMPILER_ID and CMAKE_CXX_COMPILER_ID are documented as internal variables subject to
+# change at: http://www.cmake.org/cmake/help/v2.8.8/cmake.html#variable:CMAKE_LANG_COMPILER_ID
+# however currently this seems the most robust way to detect the presence of the Clang compiler
+if("${CMAKE_C_COMPILER_ID}" MATCHES "Clang")
+  set(CMAKE_COMPILER_IS_CLANG 1)
+endif ()
+
+if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
+  set(CMAKE_COMPILER_IS_CLANGXX 1)
+endif ()
+
+list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
+
+# Add an "uninstall" target
+CONFIGURE_FILE ("${PROJECT_SOURCE_DIR}/cmake/uninstall_target.cmake.in"
+    "${PROJECT_BINARY_DIR}/uninstall_target.cmake" IMMEDIATE @ONLY)
+ADD_CUSTOM_TARGET (uninstall "${CMAKE_COMMAND}" -P
+    "${PROJECT_BINARY_DIR}/uninstall_target.cmake")
+
+# Set the build type.  Options are:
+#  Debug          : w/ debug symbols, w/o optimization
+#  Release        : w/o debug symbols, w/ optimization
+#  RelWithDebInfo : w/ debug symbols, w/ optimization
+#  MinSizeRel     : w/o debug symbols, w/ optimization, stripped binaries
+
+if (NOT CMAKE_BUILD_TYPE)
+    #set(CMAKE_BUILD_TYPE Release)
+    set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "Build type" FORCE)
+    #set(CMAKE_BUILD_TYPE Debug)
+endif()
+
+#set the default path for built executables to the "bin" directory
+set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
+#set the default path for built libraries to the "lib" directory
+set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
+# set output path for tests
+set(TEST_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/test)
+
+option(BUILD_C_BINDINGS "Build C bindings" ON)
+option(BUILD_PYTHON_BINDINGS "Build Python bindings" ON)
+option(BUILD_MATLAB_BINDINGS "Build Matlab bindings" ON)
+option(BUILD_CUDA_LIB "Build CUDA library" OFF)
+option(USE_OPENMP "Use OpenMP multi-threading" ON)
+option(USE_MPI "Use MPI" OFF)
+
+set(NVCC_COMPILER_BINDIR "" CACHE PATH  "Directory where nvcc should look for C++ compiler. This is passed to nvcc through the --compiler-bindir option.")
+
+if (NOT BUILD_C_BINDINGS)
+    set(BUILD_PYTHON_BINDINGS OFF)
+    set(BUILD_MATLAB_BINDINGS OFF)
+endif()
+
+
+# find python
+find_package(PythonInterp)
+if (NOT PYTHON_EXECUTABLE)
+    set(BUILD_PYTHON_BINDINGS OFF)
+endif()
+
+find_hdf5()
+if (NOT HDF5_FOUND)
+	message(WARNING "hdf5 library not found, some tests will not be run")
+else()
+    include_directories(${HDF5_INCLUDE_DIR})
+endif()
+
+if (USE_MPI OR HDF5_IS_PARALLEL)
+    find_package(MPI)
+endif()
+if (HDF5_IS_PARALLEL)
+    if (NOT MPI_FOUND)
+        message(WARNING "Found the parallel HDF5 library, but could not find the MPI library. Define the MPI_COMPILER variable to the path of your MPI compiler.")
+    endif()
+    # Parallel HDF5 needs to find the "mpi.h" header file
+    include_directories(${MPI_INCLUDE_PATH})
+endif()
+
+
+if (USE_MPI)
+    if (NOT MPI_FOUND)
+        message(WARNING "Could not find an MPI library. Define the MPI_COMPILER variable to the path of your MPI compiler.")
+        set(USE_MPI OFF)
+    endif()
+
+    if (NOT HDF5_IS_PARALLEL)
+        message(WARNING "For MPI support the Parallel HDF5 library is required.")
+        set(USE_MPI OFF)
+    endif()
+endif(USE_MPI)
+
+
+if (USE_MPI AND HDF5_IS_PARALLEL)
+    find_package(Boost COMPONENTS mpi system serialization thread REQUIRED)
+    include_directories(${Boost_INCLUDE_DIRS})
+    add_definitions("-DHAVE_MPI")
+endif()
+
+
+find_package(GTest) 
+if (NOT GTEST_FOUND)
+	message(WARNING "gtest library not found, some tests will not be run")
+endif()
+
+
+if (USE_OPENMP)
+    find_package(OpenMP)
+    if(OPENMP_FOUND)
+        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
+        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
+        set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
+    else()
+        message(WARNING "OpenMP NOT found")
+    endif()
+endif()
+
+
+# CUDA support
+if (BUILD_CUDA_LIB)
+    find_package(CUDA)
+    if (CUDA_FOUND)
+        message(STATUS "CUDA found (include: ${CUDA_INCLUDE_DIRS}, lib: ${CUDA_LIBRARIES})")
+        include_directories(${CUDA_INCLUDE_DIRS})
+    else(CUDA_FOUND)
+        message(STATUS "CUDA not found, CUDA library will not be built")
+        set(BUILD_CUDA_LIB OFF)
+    endif(CUDA_FOUND)
+endif(BUILD_CUDA_LIB)
+
+#set the C/C++ include path to the "include" directory
+include_directories(${PROJECT_SOURCE_DIR}/src/cpp)
+
+# require proper c++
+#add_definitions( "-Wall -ansi -pedantic" )
+# HDF5 uses long long which is not ansi
+if (WIN32)
+    # lots of warnings with cl.exe right now, use /W1
+    add_definitions("/W1 -D_CRT_SECURE_NO_WARNINGS -D_SCL_SECURE_NO_WARNINGS /bigobj")
+else(WIN32)
+    add_definitions( "-Wall -Wno-unknown-pragmas -Wno-unused-function" )
+endif(WIN32)
+
+add_subdirectory( cmake )
+add_subdirectory( src )
+add_subdirectory( examples )
+add_subdirectory( test )
+add_subdirectory( doc )
+
+
+# CPACK options
+
+# RPM
+find_program(RPM_PROGRAM rpm)
+if(EXISTS ${RPM_PROGRAM})
+  list(APPEND CPACK_GENERATOR "RPM")
+endif(EXISTS ${RPM_PROGRAM})
+# DEB
+find_program(DPKG_PROGRAM dpkg)
+if(EXISTS ${DPKG_PROGRAM})
+  list(APPEND CPACK_GENERATOR "DEB")
+endif(EXISTS ${DPKG_PROGRAM})
+# NSIS
+find_program(NSIS_PROGRAM makensis MakeNSIS)
+if(EXISTS ${NSIS_PROGRAM})
+  list(APPEND CPACK_GENERATOR "NSIS")
+endif(EXISTS ${NSIS_PROGRAM})
+# dpkg
+find_program(PACKAGE_MAKER_PROGRAM PackageMaker
+	    HINTS /Developer/Applications/Utilities)
+if(EXISTS ${PACKAGE_MAKER_PROGRAM})
+  list(APPEND CPACK_GENERATOR "PackageMaker")
+endif(EXISTS ${PACKAGE_MAKER_PROGRAM})
+ 
+set(CPACK_GENERATOR "${CPACK_GENERATOR}")
+set(CPACK_MONOLITHIC_INSTALL 1)
+set(CPACK_SET_DESTDIR ON)
+include(InstallRequiredSystemLibraries)
+set(CPACK_PACKAGE_CONTACT "Marius Muja")
+set(CPACK_PACKAGING_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX})
+SET(CPACK_PACKAGE_VERSION ${FLANN_VERSION})
+SET(CPACK_PACKAGE_VERSION_MAJOR ${FLANN_VERSION_MAJOR})
+SET(CPACK_PACKAGE_VERSION_MINOR ${FLANN_VERSION_MINOR})
+SET(CPACK_PACKAGE_VERSION_PATCH ${FLANN_VERSION_PATCH})
+include(CPack)
+
+
+message(STATUS "Install prefix: ${CMAKE_INSTALL_PREFIX}")
+message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
+message(STATUS "Building C bindings: ${BUILD_C_BINDINGS}")
+message(STATUS "Building python bindings: ${BUILD_PYTHON_BINDINGS}")
+message(STATUS "Building matlab bindings: ${BUILD_MATLAB_BINDINGS}")
+message(STATUS "Building CUDA library: ${BUILD_CUDA_LIB}")
+message(STATUS "Using OpenMP support: ${USE_OPENMP}")
+message(STATUS "Using MPI support: ${USE_MPI}")
diff --git a/COPYING b/COPYING
new file mode 100644
index 0000000..82cc3a5
--- /dev/null
+++ b/COPYING
@@ -0,0 +1,14 @@
+
+The BSD License
+
+Copyright (c) 2008-2011  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+Copyright (c) 2008-2011  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
+ * Neither the name of the "University of British Columbia" nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
+
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFI [...]
diff --git a/ChangeLog b/ChangeLog
new file mode 100644
index 0000000..a5b0777
--- /dev/null
+++ b/ChangeLog
@@ -0,0 +1,42 @@
+Version 1.6.11
+	* bug fixes
+
+Version 1.6.10
+	* fixed a radiusSearch bug introduced in 1.6.9
+
+Version 1.6.9
+	* bug fixes
+	* fixed radius search bug on MSVC compiler
+	* fixed windows linking problems
+
+Version 1.6.8
+	* bug fixes, low dimensional search speedup
+
+Version 1.6.7
+	* bug fixes
+
+Version 1.6.6
+	* misc bug fixes
+
+Version 1.6.5
+	* fix compilation problem on some C++ compilers
+	* fixes in the python bindings
+
+Version 1.6.4
+	* small bug fix
+
+Version 1.6.3
+	* radius search speedup
+
+Version 1.6.2
+	* slight API changes to the C++ bindings, now the main index type is templated
+	* on the distance functor which makes it easier to use custom distances
+	* new kd-tree implementation optimized for low dimensionality data
+	* experimental MPI support for cluster computing
+
+Version 1.5
+	* new C++ templated API
+	* saving/loading of indices to disk
+	* threadsafe search
+	* new distance types (thanks to Radu Bogdan Rusu and Romain Thibaux for the patch)
+	* (api change) autotuned is no longer selected by passing a precision >0, it's used when the algorithm type is set to autotuned
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..c33e142
--- /dev/null
+++ b/README.md
@@ -0,0 +1,43 @@
+FLANN - Fast Library for Approximate Nearest Neighbors
+======================================================
+
+FLANN is a library for performing fast approximate nearest neighbor searches in high dimensional spaces. It contains a collection of algorithms we found to work best for nearest neighbor search and a system for automatically choosing the best algorithm and optimum parameters depending on the dataset.
+FLANN is written in C++ and contains bindings for the following languages: C, MATLAB and Python.
+
+
+Documentation
+-------------
+
+Check FLANN web page [here](http://www.cs.ubc.ca/~mariusm/flann).
+
+Documentation on how to use the library can be found in the doc/manual.pdf file included in the release archives.
+
+More information and experimental results can be found in the following paper:
+
+  * Marius Muja and David G. Lowe, "Fast Approximate Nearest Neighbors with Automatic Algorithm Configuration", in International Conference on Computer Vision Theory and Applications (VISAPP'09), 2009 [(PDF)](http://people.cs.ubc.ca/~mariusm/uploads/FLANN/flann_visapp09.pdf) [(BibTex)](http://people.cs.ubc.ca/~mariusm/index.php/FLANN/BibTex)
+
+
+Getting FLANN
+-------------
+
+The latest version of FLANN can be downloaded from here:
+
+ *  Version 1.8.4 (15 January 2013)
+    [flann-1.8.4-src.zip](http://people.cs.ubc.ca/~mariusm/uploads/FLANN/flann-1.8.4-src.zip) (Source code)  
+    [User manual](http://people.cs.ubc.ca/~mariusm/uploads/FLANN/flann_manual-1.8.4.pdf)  
+    [Changelog](https://github.com/mariusmuja/flann/blob/master/ChangeLog)  
+
+If you want to try out the latest changes or contribute to FLANN, then it's recommended that you checkout the git source repository: `git clone git://github.com/mariusmuja/flann.git`
+
+If you just want to browse the repository, you can do so by going [here](https://github.com/mariusmuja/flann).
+
+
+Conditions of use
+-----------------
+
+FLANN is distributed under the terms of the [BSD License](https://github.com/mariusmuja/flann/blob/master/COPYING).
+
+Bug reporting
+-------------
+
+Please report bugs or feature requests using [github's issue tracker](http://github.com/mariusmuja/flann/issues).
diff --git a/bin/download_checkmd5.py b/bin/download_checkmd5.py
new file mode 100755
index 0000000..bbe4499
--- /dev/null
+++ b/bin/download_checkmd5.py
@@ -0,0 +1,69 @@
+#!/usr/bin/env python2
+
+import urllib, hashlib, sys, os
+from optparse import OptionParser
+
+class AppURLopener(urllib.FancyURLopener):
+    version ="Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.0.7) Gecko/20060909 Firefox/1.5.0.7"
+
+    def prompt_user_passwd(self, host, realm):
+        raise Exception()
+urllib._urlopener = AppURLopener()
+
+def file_md5(file):
+    m = hashlib.md5()
+    m.update(open(dest).read())
+    return m.hexdigest()
+
+
+def main():
+    parser = OptionParser(usage="usage: %prog URI dest [md5sum]", prog=sys.argv[0])
+    options, args = parser.parse_args()
+    md5sum = None
+    
+    if len(args)==2:
+        uri, dest = args
+    elif len(args)==3:
+        uri, dest, md5 = args
+    else:
+        parser.error("Wrong arguments")
+    
+    fresh = False
+    if not os.path.exists(dest):
+        print "Downloading from %s to %s..."%(uri, dest),
+        sys.stdout.flush()
+        urllib.urlretrieve(uri, dest)
+        print "done"
+        fresh = True
+        
+    if md5sum:
+        print "Computing md5sum on downloaded file",
+        sys.stdout.flush()
+        checksum = md5_file(dest)
+        print "done"
+        
+        if checksum!=md5sum:
+            if not fresh:
+                print "Checksum mismatch (%s != %s), re-downloading file %s"%(checksum, md5sum, dest),
+                sys.stdout.flush()
+                os.remove(dest)
+                urllib.urlretrieve(uri, dest)
+                print "done"
+    
+                print "Computing md5sum on downloaded file",
+                sys.stdout.flush()
+                checksum = md5_file(dest)
+                print "done"
+                
+                if checksum!=md5sum:
+                    print "ERROR, checksum mismatch (%s != %s) on %d",(checksum, md5sum, dest)
+                    return 1
+    return 0
+
+
+if __name__ == '__main__':
+    try:
+        sys.exit(main())
+    except Exception as e:
+        print "ERROR, ",e
+        sys.exit(1)
diff --git a/bin/indent.sh b/bin/indent.sh
new file mode 100755
index 0000000..f3f98ad
--- /dev/null
+++ b/bin/indent.sh
@@ -0,0 +1,5 @@
+#!/bin/bash
+
+DIR=`dirname $0`
+uncrustify --no-backup -c ${DIR}/uncrustify.cfg  $1
+
diff --git a/bin/make_release.sh b/bin/make_release.sh
new file mode 100755
index 0000000..acc69e5
--- /dev/null
+++ b/bin/make_release.sh
@@ -0,0 +1,6 @@
+#!/bin/bash
+VERSION=`grep "set(FLANN_VERSION" CMakeLists.txt  | sed 's/[^0-9]*\([0-9]*\.[0-9]*\.[0-9]*\)[^0-9]*/\1/'`
+
+echo "Creating flann-$VERSION-src.zip"
+
+git archive --prefix=flann-$VERSION-src/ -o flann-$VERSION-src.zip $VERSION-src
diff --git a/bin/run_test.py b/bin/run_test.py
new file mode 100755
index 0000000..42f37dd
--- /dev/null
+++ b/bin/run_test.py
@@ -0,0 +1,18 @@
+#!/usr/bin/env python2
+
+
+import sys
+import os.path as op
+import unittest
+
+if __name__ == "__main__":
+    if len(sys.argv)==1:
+        print "Usage: %s file"%sys.argv[0]
+        sys.exit(1)
+
+    python_path = op.abspath(op.join( op.dirname(__file__),"..","src","python"))
+    sys.path.append(python_path)
+
+    test_file = sys.argv[1]
+    sys.argv = sys.argv[1:]
+    execfile(test_file)
diff --git a/bin/uncrustify.cfg b/bin/uncrustify.cfg
new file mode 100644
index 0000000..6d4493d
--- /dev/null
+++ b/bin/uncrustify.cfg
@@ -0,0 +1,132 @@
+indent_align_string=false
+indent_braces=false
+indent_braces_no_func=false
+indent_brace_parent=false
+indent_namespace=false
+indent_extern=false
+indent_class=true
+indent_class_colon=false
+indent_else_if=false
+indent_func_call_param=false
+indent_func_def_param=false
+indent_func_proto_param=false
+indent_func_class_param=false
+indent_func_ctor_var_param=false
+indent_template_param=false
+indent_func_param_double=false
+indent_relative_single_line_comments=true
+indent_col1_comment=true
+indent_access_spec_body=false
+indent_paren_nl=false
+indent_comma_paren=false
+indent_bool_paren=false
+indent_square_nl=false
+indent_preserve_sql=false
+indent_align_assign=true
+sp_balance_nested_parens=false
+align_keep_tabs=false
+align_with_tabs=false
+align_on_tabstop=false
+align_number_left=false
+align_func_params=false
+align_same_func_call_params=false
+align_var_def_colon=false
+align_var_def_attribute=false
+align_var_def_inline=false
+align_right_cmt_mix=false
+align_on_operator=false
+align_mix_var_proto=false
+align_single_line_func=false
+align_single_line_brace=false
+align_nl_cont=false
+align_left_shift=true
+nl_collapse_empty_body=false
+nl_assign_leave_one_liners=true
+nl_class_leave_one_liners=true
+nl_enum_leave_one_liners=true
+nl_getset_leave_one_liners=true
+nl_func_leave_one_liners=true
+nl_if_leave_one_liners=true
+nl_multi_line_cond=false
+nl_multi_line_define=false
+nl_before_case=false
+nl_after_case=false
+nl_after_return=false
+nl_after_semicolon=false
+nl_after_brace_open=false
+nl_after_brace_open_cmt=false
+nl_after_vbrace_open=false
+nl_after_brace_close=false
+nl_define_macro=false
+nl_squeeze_ifdef=false
+nl_ds_struct_enum_cmt=false
+nl_ds_struct_enum_close_brace=false
+nl_create_if_one_liner=true
+nl_create_for_one_liner=true
+nl_create_while_one_liner=true
+ls_for_split_full=false
+ls_func_split_full=false
+nl_after_multiline_comment=false
+eat_blanks_after_open_brace=false
+eat_blanks_before_close_brace=false
+mod_pawn_semicolon=false
+mod_full_paren_if_bool=true
+mod_remove_extra_semicolon=true
+mod_sort_import=false
+mod_sort_using=false
+mod_sort_include=false
+mod_move_case_break=false
+mod_remove_empty_return=false
+cmt_indent_multi=true
+cmt_c_group=false
+cmt_c_nl_start=false
+cmt_c_nl_end=false
+cmt_cpp_group=false
+cmt_cpp_nl_start=false
+cmt_cpp_nl_end=false
+cmt_cpp_to_c=false
+cmt_star_cont=false
+cmt_multi_check_last=true
+cmt_insert_before_preproc=false
+pp_indent_at_level=false
+pp_region_indent_code=false
+pp_if_indent_code=false
+pp_define_at_level=false
+input_tab_size=4
+indent_columns=4
+indent_with_tabs=0
+sp_before_ptr_star=remove
+sp_between_ptr_star=remove
+sp_after_ptr_star=add
+sp_before_byref=remove
+sp_after_byref=add
+sp_after_type=ignore
+sp_else_brace=add
+sp_catch_brace=add
+sp_finally_brace=add
+sp_try_brace=add
+nl_end_of_file=add
+nl_fcall_brace=remove
+nl_enum_brace=add
+nl_struct_brace=add
+nl_union_brace=remove
+nl_if_brace=remove
+nl_brace_else=add
+nl_else_brace=remove
+nl_else_if=remove
+nl_brace_finally=add
+nl_finally_brace=remove
+nl_try_brace=remove
+nl_for_brace=remove
+nl_catch_brace=remove
+nl_brace_catch=add
+nl_while_brace=remove
+nl_do_brace=remove
+nl_brace_while=remove
+nl_switch_brace=remove
+nl_namespace_brace=add
+nl_template_class=add
+nl_class_brace=add
+nl_fdef_brace=add
+mod_full_brace_function=add
+mod_paren_on_return=remove
diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt
new file mode 100644
index 0000000..fb70f4c
--- /dev/null
+++ b/cmake/CMakeLists.txt
@@ -0,0 +1,6 @@
+set(PKG_DESC "Fast Library for Approximate Nearest Neighbors")
+set(pkg_conf_file ${CMAKE_CURRENT_BINARY_DIR}/flann.pc)
+configure_file(flann.pc.in ${pkg_conf_file} @ONLY)
+install(FILES ${pkg_conf_file}
+    DESTINATION ${FLANN_LIB_INSTALL_DIR}/pkgconfig/ COMPONENT pkgconfig)
+
diff --git a/cmake/FindFlann.cmake b/cmake/FindFlann.cmake
new file mode 100644
index 0000000..a6375c0
--- /dev/null
+++ b/cmake/FindFlann.cmake
@@ -0,0 +1,28 @@
+###############################################################################
+# Find Flann
+#
+# This sets the following variables:
+# FLANN_FOUND - True if FLANN was found.
+# FLANN_INCLUDE_DIRS - Directories containing the FLANN include files.
+# FLANN_LIBRARIES - Libraries needed to use FLANN.
+# FLANN_DEFINITIONS - Compiler flags for FLANN.
+
+find_package(PkgConfig)
+pkg_check_modules(PC_FLANN flann)
+set(FLANN_DEFINITIONS ${PC_FLANN_CFLAGS_OTHER})
+
+find_path(FLANN_INCLUDE_DIR flann/flann.hpp
+    HINTS ${PC_FLANN_INCLUDEDIR} ${PC_FLANN_INCLUDE_DIRS})
+
+find_library(FLANN_LIBRARY flann
+    HINTS ${PC_FLANN_LIBDIR} ${PC_FLANN_LIBRARY_DIRS})
+
+set(FLANN_INCLUDE_DIRS ${FLANN_INCLUDE_DIR})
+set(FLANN_LIBRARIES ${FLANN_LIBRARY})
+
+include(FindPackageHandleStandardArgs)
+find_package_handle_standard_args(Flann DEFAULT_MSG
+    FLANN_LIBRARY FLANN_INCLUDE_DIR)
+
+mark_as_advanced(FLANN_LIBRARY FLANN_INCLUDE_DIR)
+
diff --git a/cmake/UseLATEX.cmake b/cmake/UseLATEX.cmake
new file mode 100644
index 0000000..77bd709
--- /dev/null
+++ b/cmake/UseLATEX.cmake
@@ -0,0 +1,813 @@
+# File: UseLATEX.cmake
+# CMAKE commands to actually use the LaTeX compiler
+# Version: 1.7.3
+# Author: Kenneth Moreland (kmorel at sandia dot gov)
+#
+# Copyright 2004 Sandia Corporation.
+# Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
+# license for use of this work by or on behalf of the
+# U.S. Government. Redistribution and use in source and binary forms, with
+# or without modification, are permitted provided that this Notice and any
+# statement of authorship are reproduced on all copies.
+#
+# The following MACROS are defined:
+#
+# ADD_LATEX_DOCUMENT(<tex_file>
+#                       [BIBFILES <bib_files>]
+#                       [INPUTS <input_tex_files>]
+#                       [IMAGE_DIRS] <image_directories>
+#                       [IMAGES] <image_files>
+#                       [CONFIGURE] <tex_files>
+#                       [DEPENDS] <tex_files>
+#                       [USE_INDEX] [USE_GLOSSARY]
+#                       [DEFAULT_PDF] [MANGLE_TARGET_NAMES])
+#       Adds targets that compile <tex_file>.  The latex output is placed
+#       in LATEX_OUTPUT_PATH or CMAKE_CURRENT_BINARY_DIR if the former is
+#       not set.  The latex program is picky about where files are located,
+#       so all input files are copied from the source directory to the
+#       output directory.  This includes the target tex file, any tex file
+#       listed with the INPUTS option, the bibliography files listed with
+#       the BIBFILES option, and any .cls, .bst, and .clo files found in
+#       the current source directory.  Images found in the IMAGE_DIRS
+#       directories or listed by IMAGES are also copied to the output
+#       directory and coverted to an appropriate format if necessary.  Any
+#       tex files also listed with the CONFIGURE option are also processed
+#       with the CMake CONFIGURE_FILE command (with the @ONLY flag.  Any
+#       file listed in CONFIGURE but not the target tex file or listed with
+#       INPUTS has no effect. DEPENDS can be used to specify generated files
+#       that are needed to compile the latex target.
+#
+#       The following targets are made:
+#               dvi: Makes <name>.dvi
+#               pdf: Makes <name>.pdf using pdflatex.
+#               safepdf: Makes <name>.pdf using ps2pdf.  If using the default
+#                       program arguments, this will ensure all fonts are
+#                       embedded and no lossy compression has been performed
+#                       on images.
+#               ps: Makes <name>.ps
+#               html: Makes <name>.html
+#               auxclean: Deletes <name>.aux.  This is sometimes necessary
+#                       if a LaTeX error occurs and writes a bad aux file.
+#
+#       The dvi target is added to the ALL.  That is, it will be the target
+#       built by default.  If the DEFAULT_PDF argument is given, then the
+#       pdf target will be the default instead of dvi.
+#
+#       If the argument MANGLE_TARGET_NAMES is given, then each of the
+#       target names above will be mangled with the <tex_file> name.  This
+#       is to make the targets unique if ADD_LATEX_DOCUMENT is called for
+#       multiple documents.  If the argument USE_INDEX is given, then
+#       commands to build an index are made.  If the argument USE_GLOSSARY
+#       is given, then commands to build a glossary are made.
+#
+# History:
+#
+# 1.7.3 Fix some issues with interactions between makeglossaries and bibtex
+#       (thanks to Mark de Wever).
+#
+# 1.7.2 Use ps2pdf to convert eps to pdf to get around the problem with
+#       ImageMagick dropping the bounding box (thanks to Lukasz Lis).
+#
+# 1.7.1 Fixed some dependency issues.
+#
+# 1.7.0 Added DEPENDS options (thanks to Theodore Papadopoulo).
+#
+# 1.6.1 Ported the makeglossaries command to CMake and embedded the port
+#       into UseLATEX.cmake.
+#
+# 1.6.0 Allow the use of the makeglossaries command.  Thanks to Oystein
+#       S. Haaland for the patch.
+#
+# 1.5.0 Allow any type of file in the INPUTS lists, not just tex file
+#       (suggested by Eric Noulard).  As a consequence, the ability to
+#       specify tex files without the .tex extension is removed.  The removed
+#       function is of dubious value anyway.
+#
+#       When copying input files, skip over any file that exists in the
+#       binary directory but does not exist in the source directory with the
+#       assumption that these files were added by some other mechanism.  I
+#       find this useful when creating large documents with multiple
+#       chapters that I want to build separately (for speed) as I work on
+#       them.  I use the same boilerplate as the starting point for all
+#       and just copy it with different configurations.  This was what the
+#       separate ADD_LATEX_DOCUMENT method was supposed to originally be for.
+#       Since its external use is pretty much deprecated, I removed that
+#       documentation.
+#
+# 1.4.1 Copy .sty files along with the other class and package files.
+#
+# 1.4.0 Added a MANGLE_TARGET_NAMES option that will mangle the target names.
+#
+#       Fixed problem with copying bib files that became apparent with
+#       CMake 2.4.
+#
+# 1.3.0 Added a LATEX_OUTPUT_PATH variable that allows you or the user to
+#       specify where the built latex documents to go.  This is especially
+#       handy if you want to do in-source builds.
+#
+#       Removed the ADD_LATEX_IMAGES macro and absorbed the functionality
+#       into ADD_LATEX_DOCUMENT.  The old interface was always kind of
+#       clunky anyway since you had to specify the image directory in both
+#       places.  It also made supporting LATEX_OUTPUT_PATH problematic.
+#
+#       Added support for jpeg files.
+#
+# 1.2.0 Changed the configuration options yet again.  Removed the NO_CONFIGURE
+#       Replaced it with a CONFIGURE option that lists input files for which
+#       configure should be run.
+#
+#       The pdf target no longer depends on the dvi target.  This allows you
+#       to build latex documents that require pdflatex.  Also added an option
+#       to make the pdf target the default one.
+#
+# 1.1.1 Added the NO_CONFIGURE option.  The @ character can be used when
+#       specifying table column separators.  If two or more are used, then
+#       will incorrectly substitute them.
+#
+# 1.1.0 Added ability include multiple bib files.  Added ability to do copy
+#       sub-tex files for multipart tex files.
+#
+# 1.0.0 If both ps and pdf type images exist, just copy the one that
+#       matches the current render mode.  Replaced a bunch of STRING
+#       commands with GET_FILENAME_COMPONENT commands that were made to do
+#       the desired function.
+#
+# 0.4.0 First version posted to CMake Wiki.
+#
+
+#############################################################################
+# Find the location of myself while originally executing.  If you do this
+# inside of a macro, it will recode where the macro was invoked.
+#############################################################################
+SET(LATEX_USE_LATEX_LOCATION ${CMAKE_CURRENT_LIST_FILE}
+  CACHE INTERNAL "Location of UseLATEX.cmake file." FORCE
+  )
+
+#############################################################################
+# Generic helper macros
+#############################################################################
+
+# Helpful list macros.
+MACRO(LATEX_CAR var)
+  SET(${var} ${ARGV1})
+ENDMACRO(LATEX_CAR)
+MACRO(LATEX_CDR var junk)
+  SET(${var} ${ARGN})
+ENDMACRO(LATEX_CDR)
+
+MACRO(LATEX_LIST_CONTAINS var value)
+  SET(${var})
+  FOREACH (value2 ${ARGN})
+    IF (${value} STREQUAL ${value2})
+      SET(${var} TRUE)
+    ENDIF (${value} STREQUAL ${value2})
+  ENDFOREACH (value2)
+ENDMACRO(LATEX_LIST_CONTAINS)
+
+# Parse macro arguments.
+MACRO(LATEX_PARSE_ARGUMENTS prefix arg_names option_names)
+  SET(DEFAULT_ARGS)
+  FOREACH(arg_name ${arg_names})
+    SET(${prefix}_${arg_name})
+  ENDFOREACH(arg_name)
+  FOREACH(option ${option_names})
+    SET(${prefix}_${option})
+  ENDFOREACH(option)
+
+  SET(current_arg_name DEFAULT_ARGS)
+  SET(current_arg_list)
+  FOREACH(arg ${ARGN})
+    LATEX_LIST_CONTAINS(is_arg_name ${arg} ${arg_names})
+    IF (is_arg_name)
+      SET(${prefix}_${current_arg_name} ${current_arg_list})
+      SET(current_arg_name ${arg})
+      SET(current_arg_list)
+    ELSE (is_arg_name)
+      LATEX_LIST_CONTAINS(is_option ${arg} ${option_names})
+      IF (is_option)
+        SET(${prefix}_${arg} TRUE)
+      ELSE (is_option)
+        SET(current_arg_list ${current_arg_list} ${arg})
+      ENDIF (is_option)
+    ENDIF (is_arg_name)
+  ENDFOREACH(arg)
+  SET(${prefix}_${current_arg_name} ${current_arg_list})
+ENDMACRO(LATEX_PARSE_ARGUMENTS)
+
+# Match the contents of a file to a regular expression.
+MACRO(LATEX_FILE_MATCH variable filename regexp default)
+  # The FILE STRINGS command would be a bit better, but it's not supported on
+  # older versions of CMake.
+  FILE(READ ${filename} file_contents)
+  STRING(REGEX MATCHALL "${regexp}"
+    ${variable} ${file_contents}
+    )
+  IF (NOT ${variable})
+    SET(${variable} "${default}")
+  ENDIF (NOT ${variable})
+ENDMACRO(LATEX_FILE_MATCH)
+
+#############################################################################
+# Macros that perform processing during a LaTeX build.
+#############################################################################
+MACRO(LATEX_MAKEGLOSSARIES)
+  MESSAGE("**************************** In makeglossaries")
+  IF (NOT LATEX_TARGET)
+    MESSAGE(SEND_ERROR "Need to define LATEX_TARGET")
+  ENDIF (NOT LATEX_TARGET)
+
+  IF (NOT MAKEINDEX_COMPILER)
+    MESSAGE(SEND_ERROR "Need to define MAKEINDEX_COMPILER")
+  ENDIF (NOT MAKEINDEX_COMPILER)
+
+  SET(aux_file ${LATEX_TARGET}.aux)
+
+  IF (NOT EXISTS ${aux_file})
+    MESSAGE(SEND_ERROR "${aux_file} does not exist.  Run latex on your target file.")
+  ENDIF (NOT EXISTS ${aux_file})
+
+  LATEX_FILE_MATCH(newglossary_lines ${aux_file}
+    "@newglossary[ \t]*{([^}]*)}{([^}]*)}{([^}]*)}{([^}]*)}"
+    "@newglossary{main}{glg}{gls}{glo}"
+    )
+
+  LATEX_FILE_MATCH(istfile_line ${aux_file}
+    "@istfilename[ \t]*{([^}]*)}"
+    "@istfilename{${LATEX_TARGET}.ist}"
+    )
+  STRING(REGEX REPLACE "@istfilename[ \t]*{([^}]*)}" "\\1"
+    istfile ${istfile_line}
+    )
+
+  FOREACH(newglossary ${newglossary_lines})
+    STRING(REGEX REPLACE
+      "@newglossary[ \t]*{([^}]*)}{([^}]*)}{([^}]*)}{([^}]*)}"
+      "\\1" glossary_name ${newglossary}
+      )
+    STRING(REGEX REPLACE
+      "@newglossary[ \t]*{([^}]*)}{([^}]*)}{([^}]*)}{([^}]*)}"
+      "${LATEX_TARGET}.\\2" glossary_log ${newglossary}
+      )
+    STRING(REGEX REPLACE
+      "@newglossary[ \t]*{([^}]*)}{([^}]*)}{([^}]*)}{([^}]*)}"
+      "${LATEX_TARGET}.\\3" glossary_out ${newglossary}
+      )
+    STRING(REGEX REPLACE
+      "@newglossary[ \t]*{([^}]*)}{([^}]*)}{([^}]*)}{([^}]*)}"
+      "${LATEX_TARGET}.\\4" glossary_in ${newglossary}
+      )
+    MESSAGE("${MAKEINDEX_COMPILER} ${MAKEGLOSSARIES_COMPILER_FLAGS} -s ${istfile} -t ${glossary_log} -o ${glossary_out} ${glossary_in}")
+    EXEC_PROGRAM(${MAKEINDEX_COMPILER} ARGS ${MAKEGLOSSARIES_COMPILER_FLAGS}
+      -s ${istfile} -t ${glossary_log} -o ${glossary_out} ${glossary_in}
+      )
+  ENDFOREACH(newglossary)
+ENDMACRO(LATEX_MAKEGLOSSARIES)
+
+#############################################################################
+# Helper macros for establishing LaTeX build.
+#############################################################################
+
+MACRO(LATEX_NEEDIT VAR NAME)
+  IF (NOT ${VAR})
+    MESSAGE(SEND_ERROR "I need the ${NAME} command.")
+  ENDIF(NOT ${VAR})
+ENDMACRO(LATEX_NEEDIT)
+
+MACRO(LATEX_WANTIT VAR NAME)
+  IF (NOT ${VAR})
+    MESSAGE(STATUS "I could not find the ${NAME} command.")
+  ENDIF(NOT ${VAR})
+ENDMACRO(LATEX_WANTIT)
+
+MACRO(LATEX_SETUP_VARIABLES)
+  SET(LATEX_OUTPUT_PATH "${LATEX_OUTPUT_PATH}"
+    CACHE PATH "If non empty, specifies the location to place LaTeX output."
+    )
+
+  FIND_PACKAGE(LATEX)
+
+  MARK_AS_ADVANCED(CLEAR
+    LATEX_COMPILER
+    PDFLATEX_COMPILER
+    BIBTEX_COMPILER
+    MAKEINDEX_COMPILER
+    DVIPS_CONVERTER
+    PS2PDF_CONVERTER
+    LATEX2HTML_CONVERTER
+    )
+
+  LATEX_NEEDIT(LATEX_COMPILER latex)
+  LATEX_WANTIT(PDFLATEX_COMPILER pdflatex)
+  LATEX_NEEDIT(BIBTEX_COMPILER bibtex)
+  LATEX_NEEDIT(MAKEINDEX_COMPILER makeindex)
+  LATEX_WANTIT(DVIPS_CONVERTER dvips)
+  LATEX_WANTIT(PS2PDF_CONVERTER ps2pdf)
+  LATEX_WANTIT(LATEX2HTML_CONVERTER latex2html)
+
+  SET(LATEX_COMPILER_FLAGS "-interaction=batchmode"
+    CACHE STRING "Flags passed to latex.")
+  SET(PDFLATEX_COMPILER_FLAGS ${LATEX_COMPILER_FLAGS}
+    CACHE STRING "Flags passed to pdflatex.")
+  SET(BIBTEX_COMPILER_FLAGS ""
+    CACHE STRING "Flags passed to bibtex.")
+  SET(MAKEINDEX_COMPILER_FLAGS ""
+    CACHE STRING "Flags passed to makeindex.")
+  SET(MAKEGLOSSARIES_COMPILER_FLAGS ""
+    CACHE STRING "Flags passed to makeglossaries.")
+  SET(DVIPS_CONVERTER_FLAGS "-Ppdf -G0 -t letter"
+    CACHE STRING "Flags passed to dvips.")
+  SET(PS2PDF_CONVERTER_FLAGS "-dMaxSubsetPct=100 -dCompatibilityLevel=1.3 -dSubsetFonts=true -dEmbedAllFonts=true -dAutoFilterColorImages=false -dAutoFilterGrayImages=false -dColorImageFilter=/FlateEncode -dGrayImageFilter=/FlateEncode -dMonoImageFilter=/FlateEncode"
+    CACHE STRING "Flags passed to ps2pdf.")
+  SET(LATEX2HTML_CONVERTER_FLAGS ""
+    CACHE STRING "Flags passed to latex2html.")
+  MARK_AS_ADVANCED(
+    LATEX_COMPILER_FLAGS
+    PDFLATEX_COMPILER_FLAGS
+    BIBTEX_COMPILER_FLAGS
+    MAKEINDEX_COMPILER_FLAGS
+    MAKEGLOSSARIES_COMPILER_FLAGS
+    DVIPS_CONVERTER_FLAGS
+    PS2PDF_CONVERTER_FLAGS
+    LATEX2HTML_CONVERTER_FLAGS
+    )
+  SEPARATE_ARGUMENTS(LATEX_COMPILER_FLAGS)
+  SEPARATE_ARGUMENTS(PDFLATEX_COMPILER_FLAGS)
+  SEPARATE_ARGUMENTS(BIBTEX_COMPILER_FLAGS)
+  SEPARATE_ARGUMENTS(MAKEINDEX_COMPILER_FLAGS)
+  SEPARATE_ARGUMENTS(MAKEGLOSSARIES_COMPILER_FLAGS)
+  SEPARATE_ARGUMENTS(DVIPS_CONVERTER_FLAGS)
+  SEPARATE_ARGUMENTS(PS2PDF_CONVERTER_FLAGS)
+  SEPARATE_ARGUMENTS(LATEX2HTML_CONVERTER_FLAGS)
+
+  FIND_PROGRAM(IMAGEMAGICK_CONVERT convert
+    DOC "The convert program that comes with ImageMagick (available at http://www.imagemagick.org)."
+    )
+
+  OPTION(LATEX_SMALL_IMAGES
+    "If on, the raster images will be converted to 1/6 the original size.  This is because papers usually require 600 dpi images whereas most monitors only require at most 96 dpi.  Thus, smaller images make smaller files for web distributation and can make it faster to read dvi files."
+    OFF)
+  IF (LATEX_SMALL_IMAGES)
+    SET(LATEX_RASTER_SCALE 16)
+    SET(LATEX_OPPOSITE_RASTER_SCALE 100)
+  ELSE (LATEX_SMALL_IMAGES)
+    SET(LATEX_RASTER_SCALE 100)
+    SET(LATEX_OPPOSITE_RASTER_SCALE 16)
+  ENDIF (LATEX_SMALL_IMAGES)
+
+  # Just holds extensions for known image types.  They should all be lower case.
+  SET(LATEX_DVI_VECTOR_IMAGE_EXTENSIONS .eps)
+  SET(LATEX_DVI_RASTER_IMAGE_EXTENSIONS)
+  SET(LATEX_DVI_IMAGE_EXTENSIONS
+    ${LATEX_DVI_VECTOR_IMAGE_EXTENSIONS} ${LATEX_DVI_RASTER_IMAGE_EXTENSIONS})
+  SET(LATEX_PDF_VECTOR_IMAGE_EXTENSIONS .pdf)
+  SET(LATEX_PDF_RASTER_IMAGE_EXTENSIONS .png .jpeg .jpg)
+  SET(LATEX_PDF_IMAGE_EXTENSIONS
+    ${LATEX_PDF_VECTOR_IMAGE_EXTENSIONS} ${LATEX_PDF_RASTER_IMAGE_EXTENSIONS})
+  SET(LATEX_IMAGE_EXTENSIONS
+    ${LATEX_DVI_IMAGE_EXTENSIONS} ${LATEX_PDF_IMAGE_EXTENSIONS})
+ENDMACRO(LATEX_SETUP_VARIABLES)
+
+MACRO(LATEX_GET_OUTPUT_PATH var)
+  SET(${var})
+  IF (LATEX_OUTPUT_PATH)
+    IF ("${LATEX_OUTPUT_PATH}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}")
+      MESSAGE(SEND_ERROR "You cannot set LATEX_OUTPUT_PATH to the same directory that contains LaTeX input files.")
+    ELSE ("${LATEX_OUTPUT_PATH}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}")
+      SET(${var} "${LATEX_OUTPUT_PATH}")
+    ENDIF ("${LATEX_OUTPUT_PATH}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}")
+  ELSE (LATEX_OUTPUT_PATH)
+    IF ("${CMAKE_CURRENT_BINARY_DIR}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}")
+      MESSAGE(SEND_ERROR "LaTeX files must be built out of source or you must set LATEX_OUTPUT_PATH.")
+    ELSE ("${CMAKE_CURRENT_BINARY_DIR}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}")
+      SET(${var} "${CMAKE_CURRENT_BINARY_DIR}")
+    ENDIF ("${CMAKE_CURRENT_BINARY_DIR}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}")
+  ENDIF (LATEX_OUTPUT_PATH)
+ENDMACRO(LATEX_GET_OUTPUT_PATH)
+
+MACRO(LATEX_ADD_CONVERT_COMMAND output_path input_path output_extension
+        input_extension flags)
+  SET (converter ${IMAGEMAGICK_CONVERT})
+  SET (convert_flags "")
+  # ImageMagick has broken eps to pdf conversion
+  # use ps2pdf instead
+  IF (${input_extension} STREQUAL ".eps" AND ${output_extension} STREQUAL ".pdf")
+    IF (PS2PDF_CONVERTER)
+      SET (converter ${PS2PDF_CONVERTER})
+      SET (convert_flags "-dEPSCrop ${flags}")
+    ELSE (PS2PDF_CONVERTER)
+      MESSAGE(SEND_ERROR "Using postscript files with pdflatex requires ps2pdf for conversion.")
+    ENDIF (PS2PDF_CONVERTER)
+  ELSE (${input_extension} STREQUAL ".eps" AND ${output_extension} STREQUAL ".pdf")
+    SET (convert_flags ${flags})
+  ENDIF (${input_extension} STREQUAL ".eps" AND ${output_extension} STREQUAL ".pdf")
+
+  ADD_CUSTOM_COMMAND(OUTPUT ${output_path}
+    COMMAND ${converter}
+      ARGS ${convert_flags} ${input_path} ${output_path}
+    DEPENDS ${input_path}
+    )
+ENDMACRO(LATEX_ADD_CONVERT_COMMAND)
+
+# Makes custom commands to convert a file to a particular type.
+MACRO(LATEX_CONVERT_IMAGE output_files input_file output_extension convert_flags
+    output_extensions other_files)
+  SET(input_dir ${CMAKE_CURRENT_SOURCE_DIR})
+  LATEX_GET_OUTPUT_PATH(output_dir)
+
+  GET_FILENAME_COMPONENT(extension "${input_file}" EXT)
+
+  STRING(REGEX REPLACE "\\.[^.]*\$" ${output_extension} output_file
+    "${input_file}")
+
+  LATEX_LIST_CONTAINS(is_type ${extension} ${output_extensions})
+  IF (is_type)
+    IF (convert_flags)
+      LATEX_ADD_CONVERT_COMMAND(${output_dir}/${output_file}
+        ${input_dir}/${input_file} ${output_extension} ${extension}
+        "${convert_flags}")
+      SET(${output_files} ${${output_files}} ${output_dir}/${output_file})
+    ELSE (convert_flags)
+      # As a shortcut, we can just copy the file.
+      ADD_CUSTOM_COMMAND(OUTPUT ${output_dir}/${input_file}
+        COMMAND ${CMAKE_COMMAND}
+        ARGS -E copy ${input_dir}/${input_file} ${output_dir}/${input_file}
+        DEPENDS ${input_dir}/${input_file}
+        )
+      SET(${output_files} ${${output_files}} ${output_dir}/${input_file})
+    ENDIF (convert_flags)
+  ELSE (is_type)
+    SET(do_convert TRUE)
+    # Check to see if there is another input file of the appropriate type.
+    FOREACH(valid_extension ${output_extensions})
+      STRING(REGEX REPLACE "\\.[^.]*\$" ${output_extension} try_file
+        "${input_file}")
+      LATEX_LIST_CONTAINS(has_native_file "${try_file}" ${other_files})
+      IF (has_native_file)
+        SET(do_convert FALSE)
+      ENDIF (has_native_file)
+    ENDFOREACH(valid_extension)
+
+    # If we still need to convert, do it.
+    IF (do_convert)
+      LATEX_ADD_CONVERT_COMMAND(${output_dir}/${output_file}
+        ${input_dir}/${input_file} ${output_extension} ${extension}
+        "${convert_flags}")
+      SET(${output_files} ${${output_files}} ${output_dir}/${output_file})
+    ENDIF (do_convert)
+  ENDIF (is_type)
+ENDMACRO(LATEX_CONVERT_IMAGE)
+
+# Adds custom commands to process the given files for dvi and pdf builds.
+# Adds the output files to the given variables (does not replace).
+MACRO(LATEX_PROCESS_IMAGES dvi_outputs pdf_outputs)
+  LATEX_GET_OUTPUT_PATH(output_dir)
+  FOREACH(file ${ARGN})
+    IF (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${file}")
+      GET_FILENAME_COMPONENT(extension "${file}" EXT)
+      SET(convert_flags)
+
+      # Check to see if we need to downsample the image.
+      LATEX_LIST_CONTAINS(is_raster extension
+        ${LATEX_DVI_RASTER_IMAGE_EXTENSIONS}
+        ${LATEX_PDF_RASTER_IMAGE_EXTENSIONS})
+      IF (LATEX_SMALL_IMAGES)
+        IF (is_raster)
+          SET(convert_flags -resize ${LATEX_RASTER_SCALE}%)
+        ENDIF (is_raster)
+      ENDIF (LATEX_SMALL_IMAGES)
+
+      # Make sure the output directory exists.
+      GET_FILENAME_COMPONENT(path "${output_dir}/${file}" PATH)
+      MAKE_DIRECTORY("${path}")
+
+      # Do conversions for dvi.
+      LATEX_CONVERT_IMAGE(${dvi_outputs} "${file}" .eps "${convert_flags}"
+        "${LATEX_DVI_IMAGE_EXTENSIONS}" "${ARGN}")
+
+      # Do conversions for pdf.
+      IF (is_raster)
+        LATEX_CONVERT_IMAGE(${pdf_outputs} "${file}" .png "${convert_flags}"
+          "${LATEX_PDF_IMAGE_EXTENSIONS}" "${ARGN}")
+      ELSE (is_raster)
+        LATEX_CONVERT_IMAGE(${pdf_outputs} "${file}" .pdf "${convert_flags}"
+          "${LATEX_PDF_IMAGE_EXTENSIONS}" "${ARGN}")
+      ENDIF (is_raster)
+    ELSE (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${file}")
+      MESSAGE("Could not find file \"${CMAKE_CURRENT_SOURCE_DIR}/${file}\"")
+    ENDIF (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${file}")
+  ENDFOREACH(file)
+ENDMACRO(LATEX_PROCESS_IMAGES)
+
+MACRO(ADD_LATEX_IMAGES)
+  MESSAGE("The ADD_LATEX_IMAGES macro is deprecated.  Image directories are specified with LATEX_ADD_DOCUMENT.")
+ENDMACRO(ADD_LATEX_IMAGES)
+
+MACRO(LATEX_COPY_GLOBBED_FILES pattern dest)
+  FILE(GLOB file_list ${pattern})
+  FOREACH(in_file ${file_list})
+    GET_FILENAME_COMPONENT(out_file ${in_file} NAME)
+    CONFIGURE_FILE(${in_file} ${dest}/${out_file} COPYONLY)
+  ENDFOREACH(in_file)
+ENDMACRO(LATEX_COPY_GLOBBED_FILES)
+
+MACRO(LATEX_COPY_INPUT_FILE file)
+  LATEX_GET_OUTPUT_PATH(output_dir)
+
+  IF (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${file})
+    GET_FILENAME_COMPONENT(path ${file} PATH)
+    FILE(MAKE_DIRECTORY ${output_dir}/${path})
+
+    LATEX_LIST_CONTAINS(use_config ${file} ${LATEX_CONFIGURE})
+    IF (use_config)
+      CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/${file}
+        ${output_dir}/${file}
+        @ONLY
+        )
+      ADD_CUSTOM_COMMAND(OUTPUT ${output_dir}/${file}
+        COMMAND ${CMAKE_COMMAND}
+        ARGS ${CMAKE_BINARY_DIR}
+        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${file}
+        )
+    ELSE (use_config)
+      ADD_CUSTOM_COMMAND(OUTPUT ${output_dir}/${file}
+        COMMAND ${CMAKE_COMMAND}
+        ARGS -E copy ${CMAKE_CURRENT_SOURCE_DIR}/${file} ${output_dir}/${file}
+        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${file}
+        )
+    ENDIF (use_config)
+  ELSE (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${file})
+    IF (EXISTS ${output_dir}/${file})
+      # Special case: output exists but input does not.  Assume that it was
+      # created elsewhere and skip the input file copy.
+    ELSE (EXISTS ${output_dir}/${file})
+      MESSAGE("Could not find input file ${CMAKE_CURRENT_SOURCE_DIR}/${file}")
+    ENDIF (EXISTS ${output_dir}/${file})
+  ENDIF (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${file})
+ENDMACRO(LATEX_COPY_INPUT_FILE)
+
+#############################################################################
+# Commands provided by the UseLATEX.cmake "package"
+#############################################################################
+
+MACRO(LATEX_USAGE command message)
+  MESSAGE(SEND_ERROR
+    "${message}\nUsage: ${command}(<tex_file>\n           [BIBFILES <bib_file> <bib_file> ...]\n           [INPUTS <tex_file> <tex_file> ...]\n           [IMAGE_DIRS <directory1> <directory2> ...]\n           [IMAGES <image_file1> <image_file2>\n           [CONFIGURE <tex_file> <tex_file> ...]\n           [DEPENDS <tex_file> <tex_file> ...]\n           [USE_INDEX] [USE_GLOSSARY] [DEFAULT_PDF] [MANGLE_TARGET_NAMES])"
+    )
+ENDMACRO(LATEX_USAGE command message)
+
+# Parses arguments to ADD_LATEX_DOCUMENT and ADD_LATEX_TARGETS and sets the
+# variables LATEX_TARGET, LATEX_IMAGE_DIR, LATEX_BIBFILES, LATEX_DEPENDS, and
+# LATEX_INPUTS.
+MACRO(PARSE_ADD_LATEX_ARGUMENTS command)
+  LATEX_PARSE_ARGUMENTS(
+    LATEX
+    "BIBFILES;INPUTS;IMAGE_DIRS;IMAGES;CONFIGURE;DEPENDS"
+    "USE_INDEX;USE_GLOSSARY;USE_GLOSSARIES;DEFAULT_PDF;MANGLE_TARGET_NAMES"
+    ${ARGN}
+    )
+
+  # The first argument is the target latex file.
+  IF (LATEX_DEFAULT_ARGS)
+    LATEX_CAR(LATEX_MAIN_INPUT ${LATEX_DEFAULT_ARGS})
+    LATEX_CDR(LATEX_DEFAULT_ARGS ${LATEX_DEFAULT_ARGS})
+    GET_FILENAME_COMPONENT(LATEX_TARGET ${LATEX_MAIN_INPUT} NAME_WE)
+  ELSE (LATEX_DEFAULT_ARGS)
+    LATEX_USAGE(${command} "No tex file target given to ${command}.")
+  ENDIF (LATEX_DEFAULT_ARGS)
+
+  IF (LATEX_DEFAULT_ARGS)
+    LATEX_USAGE(${command} "Invalid or depricated arguments: ${LATEX_DEFAULT_ARGS}")
+  ENDIF (LATEX_DEFAULT_ARGS)
+
+  # Backward compatibility between 1.6.0 and 1.6.1.
+  IF (LATEX_USE_GLOSSARIES)
+    SET(LATEX_USE_GLOSSARY TRUE)
+  ENDIF (LATEX_USE_GLOSSARIES)
+ENDMACRO(PARSE_ADD_LATEX_ARGUMENTS)
+
+MACRO(ADD_LATEX_TARGETS)
+  LATEX_GET_OUTPUT_PATH(output_dir)
+  PARSE_ADD_LATEX_ARGUMENTS(ADD_LATEX_TARGETS ${ARGV})
+
+  # Set up target names.
+  IF (LATEX_MANGLE_TARGET_NAMES)
+    SET(dvi_target      ${LATEX_TARGET}_dvi)
+    SET(pdf_target      ${LATEX_TARGET}_pdf)
+    SET(ps_target       ${LATEX_TARGET}_ps)
+    SET(safepdf_target  ${LATEX_TARGET}_safepdf)
+    SET(html_target     ${LATEX_TARGET}_html)
+    SET(auxclean_target ${LATEX_TARGET}_auxclean)
+  ELSE (LATEX_MANGLE_TARGET_NAMES)
+    SET(dvi_target      dvi)
+    SET(pdf_target      pdf)
+    SET(ps_target       ps)
+    SET(safepdf_target  safepdf)
+    SET(html_target     html)
+    SET(auxclean_target auxclean)
+  ENDIF (LATEX_MANGLE_TARGET_NAMES)
+
+  # For each directory in LATEX_IMAGE_DIRS, glob all the image files and
+  # place them in LATEX_IMAGES.
+  FOREACH(dir ${LATEX_IMAGE_DIRS})
+    FOREACH(extension ${LATEX_IMAGE_EXTENSIONS})
+      FILE(GLOB files ${CMAKE_CURRENT_SOURCE_DIR}/${dir}/*${extension})
+      FOREACH(file ${files})
+        GET_FILENAME_COMPONENT(filename ${file} NAME)
+        SET(LATEX_IMAGES ${LATEX_IMAGES} ${dir}/${filename})
+      ENDFOREACH(file)
+    ENDFOREACH(extension)
+  ENDFOREACH(dir)
+
+  SET(dvi_images)
+  SET(pdf_images)
+  LATEX_PROCESS_IMAGES(dvi_images pdf_images ${LATEX_IMAGES})
+
+  SET(make_dvi_command
+    ${CMAKE_COMMAND} -E chdir ${output_dir}
+    ${LATEX_COMPILER} ${LATEX_COMPILER_FLAGS} ${LATEX_MAIN_INPUT})
+  SET(make_pdf_command
+    ${CMAKE_COMMAND} -E chdir ${output_dir}
+    ${PDFLATEX_COMPILER} ${PDFLATEX_COMPILER_FLAGS} ${LATEX_MAIN_INPUT})
+
+  SET(make_dvi_depends ${LATEX_DEPENDS} ${dvi_images})
+  SET(make_pdf_depends ${LATEX_DEPENDS} ${pdf_images})
+  FOREACH(input ${LATEX_MAIN_INPUT} ${LATEX_INPUTS})
+    SET(make_dvi_depends ${make_dvi_depends} ${output_dir}/${input})
+    SET(make_pdf_depends ${make_pdf_depends} ${output_dir}/${input})
+  ENDFOREACH(input)
+
+  IF (LATEX_USE_GLOSSARY)
+    FOREACH(dummy 0 1)   # Repeat these commands twice.
+      SET(make_dvi_command ${make_dvi_command}
+        COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir}
+        ${CMAKE_COMMAND}
+        -D LATEX_BUILD_COMMAND=makeglossaries
+        -D LATEX_TARGET=${LATEX_TARGET}
+        -D MAKEINDEX_COMPILER=${MAKEINDEX_COMPILER}
+        -D MAKEGLOSSARIES_COMPILER_FLAGS=${MAKEGLOSSARIES_COMPILER_FLAGS}
+        -P ${LATEX_USE_LATEX_LOCATION}
+        COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir}
+        ${LATEX_COMPILER} ${LATEX_COMPILER_FLAGS} ${LATEX_MAIN_INPUT}
+        )
+      SET(make_pdf_command ${make_pdf_command}
+        COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir}
+        ${CMAKE_COMMAND}
+        -D LATEX_BUILD_COMMAND=makeglossaries
+        -D LATEX_TARGET=${LATEX_TARGET}
+        -D MAKEINDEX_COMPILER=${MAKEINDEX_COMPILER}
+        -D MAKEGLOSSARIES_COMPILER_FLAGS=${MAKEGLOSSARIES_COMPILER_FLAGS}
+        -P ${LATEX_USE_LATEX_LOCATION}
+        COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir}
+        ${PDFLATEX_COMPILER} ${PDFLATEX_COMPILER_FLAGS} ${LATEX_MAIN_INPUT}
+        )
+    ENDFOREACH(dummy)
+  ENDIF (LATEX_USE_GLOSSARY)
+
+  IF (LATEX_BIBFILES)
+    SET(make_dvi_command ${make_dvi_command}
+      COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir}
+      ${BIBTEX_COMPILER} ${BIBTEX_COMPILER_FLAGS} ${LATEX_TARGET})
+    SET(make_pdf_command ${make_pdf_command}
+      COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir}
+      ${BIBTEX_COMPILER} ${BIBTEX_COMPILER_FLAGS} ${LATEX_TARGET})
+    FOREACH (bibfile ${LATEX_BIBFILES})
+      SET(make_dvi_depends ${make_dvi_depends} ${output_dir}/${bibfile})
+      SET(make_pdf_depends ${make_pdf_depends} ${output_dir}/${bibfile})
+    ENDFOREACH (bibfile ${LATEX_BIBFILES})
+  ENDIF (LATEX_BIBFILES)
+
+  IF (LATEX_USE_INDEX)
+    SET(make_dvi_command ${make_dvi_command}
+      COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir}
+      ${LATEX_COMPILER} ${LATEX_COMPILER_FLAGS} ${LATEX_MAIN_INPUT}
+      COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir}
+      ${MAKEINDEX_COMPILER} ${MAKEINDEX_COMPILER_FLAGS} ${LATEX_TARGET}.idx)
+    SET(make_pdf_command ${make_pdf_command}
+      COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir}
+      ${PDFLATEX_COMPILER} ${PDFLATEX_COMPILER_FLAGS} ${LATEX_MAIN_INPUT}
+      COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir}
+      ${MAKEINDEX_COMPILER} ${MAKEINDEX_COMPILER_FLAGS} ${LATEX_TARGET}.idx)
+  ENDIF (LATEX_USE_INDEX)
+
+  SET(make_dvi_command ${make_dvi_command}
+    COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir}
+    ${LATEX_COMPILER} ${LATEX_COMPILER_FLAGS} ${LATEX_MAIN_INPUT}
+    COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir}
+    ${LATEX_COMPILER} ${LATEX_COMPILER_FLAGS} ${LATEX_MAIN_INPUT})
+  SET(make_pdf_command ${make_pdf_command}
+    COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir}
+    ${PDFLATEX_COMPILER} ${PDFLATEX_COMPILER_FLAGS} ${LATEX_MAIN_INPUT}
+    COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir}
+    ${PDFLATEX_COMPILER} ${PDFLATEX_COMPILER_FLAGS} ${LATEX_MAIN_INPUT})
+
+  # Add commands and targets for building dvi outputs.
+  ADD_CUSTOM_COMMAND(OUTPUT ${output_dir}/${LATEX_TARGET}.dvi
+    COMMAND ${make_dvi_command}
+    DEPENDS ${make_dvi_depends}
+    )
+  IF (LATEX_DEFAULT_PDF)
+    ADD_CUSTOM_TARGET(${dvi_target}
+      DEPENDS ${output_dir}/${LATEX_TARGET}.dvi)
+  ELSE (LATEX_DEFAULT_PDF)
+    ADD_CUSTOM_TARGET(${dvi_target}
+      DEPENDS ${output_dir}/${LATEX_TARGET}.dvi)
+  ENDIF (LATEX_DEFAULT_PDF)
+
+  # Add commands and targets for building pdf outputs (with pdflatex).
+  IF (PDFLATEX_COMPILER)
+    ADD_CUSTOM_COMMAND(OUTPUT ${output_dir}/${LATEX_TARGET}.pdf
+      COMMAND ${make_pdf_command}
+      DEPENDS ${make_pdf_depends}
+      )
+    IF (LATEX_DEFAULT_PDF)
+      ADD_CUSTOM_TARGET(${pdf_target}
+        DEPENDS ${output_dir}/${LATEX_TARGET}.pdf)
+    ELSE (LATEX_DEFAULT_PDF)
+      ADD_CUSTOM_TARGET(${pdf_target}
+        DEPENDS ${output_dir}/${LATEX_TARGET}.pdf)
+    ENDIF (LATEX_DEFAULT_PDF)
+  ENDIF (PDFLATEX_COMPILER)
+
+  IF (DVIPS_CONVERTER)
+    ADD_CUSTOM_COMMAND(OUTPUT ${output_dir}/${LATEX_TARGET}.ps
+      COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir}
+        ${DVIPS_CONVERTER} ${DVIPS_CONVERTER_FLAGS} -o ${LATEX_TARGET}.ps ${LATEX_TARGET}.dvi
+      DEPENDS ${output_dir}/${LATEX_TARGET}.dvi)
+    ADD_CUSTOM_TARGET(${ps_target}
+      DEPENDS ${output_dir}/${LATEX_TARGET}.ps)
+    IF (PS2PDF_CONVERTER)
+      # Since both the pdf and safepdf targets have the same output, we
+      # cannot properly do the dependencies for both.  When selecting safepdf,
+      # simply force a recompile every time.
+      ADD_CUSTOM_TARGET(${safepdf_target}
+        ${CMAKE_COMMAND} -E chdir ${output_dir}
+        ${PS2PDF_CONVERTER} ${PS2PDF_CONVERTER_FLAGS} ${LATEX_TARGET}.ps ${LATEX_TARGET}.pdf
+        )
+      ADD_DEPENDENCIES(${safepdf_target} ${ps_target})
+    ENDIF (PS2PDF_CONVERTER)
+  ENDIF (DVIPS_CONVERTER)
+
+  IF (LATEX2HTML_CONVERTER)
+    ADD_CUSTOM_TARGET(${html_target}
+      ${CMAKE_COMMAND} -E chdir ${output_dir}
+      ${LATEX2HTML_CONVERTER} ${LATEX2HTML_CONVERTER_FLAGS} ${LATEX_MAIN_INPUT}
+      )
+    ADD_DEPENDENCIES(${html_target} ${LATEX_MAIN_INPUT} ${LATEX_INPUTS})
+  ENDIF (LATEX2HTML_CONVERTER)
+
+  ADD_CUSTOM_TARGET(${auxclean_target}
+    ${CMAKE_COMMAND} -E remove ${output_dir}/${LATEX_TARGET}.aux ${output_dir}/${LATEX_TARGET}.idx ${output_dir}/${LATEX_TARGET}.ind
+    )
+ENDMACRO(ADD_LATEX_TARGETS)
+
+MACRO(ADD_LATEX_DOCUMENT)
+  LATEX_GET_OUTPUT_PATH(output_dir)
+  IF (output_dir)
+    PARSE_ADD_LATEX_ARGUMENTS(ADD_LATEX_DOCUMENT ${ARGV})
+
+    LATEX_COPY_INPUT_FILE(${LATEX_MAIN_INPUT})
+
+    FOREACH (bib_file ${LATEX_BIBFILES})
+      CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/${bib_file}
+        ${output_dir}/${bib_file}
+        COPYONLY)
+      ADD_CUSTOM_COMMAND(OUTPUT ${output_dir}/${bib_file}
+        COMMAND ${CMAKE_COMMAND}
+        ARGS -E copy ${CMAKE_CURRENT_SOURCE_DIR}/${bib_file} ${output_dir}/${bib_file}
+        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${bib_file}
+        )
+    ENDFOREACH (bib_file)
+
+    FOREACH (input ${LATEX_INPUTS})
+      LATEX_COPY_INPUT_FILE(${input})
+    ENDFOREACH(input)
+
+    LATEX_COPY_GLOBBED_FILES(${CMAKE_CURRENT_SOURCE_DIR}/*.cls ${output_dir})
+    LATEX_COPY_GLOBBED_FILES(${CMAKE_CURRENT_SOURCE_DIR}/*.bst ${output_dir})
+    LATEX_COPY_GLOBBED_FILES(${CMAKE_CURRENT_SOURCE_DIR}/*.clo ${output_dir})
+    LATEX_COPY_GLOBBED_FILES(${CMAKE_CURRENT_SOURCE_DIR}/*.sty ${output_dir})
+
+    ADD_LATEX_TARGETS(${ARGV})
+  ENDIF (output_dir)
+ENDMACRO(ADD_LATEX_DOCUMENT)
+
+#############################################################################
+# Actually do stuff
+#############################################################################
+
+IF (LATEX_BUILD_COMMAND)
+  SET(command_handled)
+
+  IF ("${LATEX_BUILD_COMMAND}" STREQUAL makeglossaries)
+    LATEX_MAKEGLOSSARIES()
+    SET(command_handled TRUE)
+  ENDIF ("${LATEX_BUILD_COMMAND}" STREQUAL makeglossaries)
+
+  IF (NOT command_handled)
+    MESSAGE(SEND_ERROR "Unknown command: ${LATEX_BUILD_COMMAND}")
+  ENDIF (NOT command_handled)
+
+ELSE (LATEX_BUILD_COMMAND)
+  # Must be part of the actual configure (included from CMakeLists.txt).
+  LATEX_SETUP_VARIABLES()
+ENDIF (LATEX_BUILD_COMMAND)
diff --git a/cmake/flann.pc.in b/cmake/flann.pc.in
new file mode 100644
index 0000000..f95697f
--- /dev/null
+++ b/cmake/flann.pc.in
@@ -0,0 +1,13 @@
+# This file was generated by CMake for @PROJECT_NAME@
+prefix=@CMAKE_INSTALL_PREFIX@
+exec_prefix=${prefix}
+libdir=${prefix}/@FLANN_LIB_INSTALL_DIR@
+includedir=${prefix}/include
+
+Name: @PROJECT_NAME@
+Description: @PKG_DESC@
+Version: @FLANN_VERSION@
+Requires: @PKG_EXTERNAL_DEPS@
+Libs: -L${libdir} -lflann_cpp
+Cflags: -I${includedir}
+
diff --git a/cmake/flann_utils.cmake b/cmake/flann_utils.cmake
new file mode 100644
index 0000000..1a10939
--- /dev/null
+++ b/cmake/flann_utils.cmake
@@ -0,0 +1,117 @@
+macro(GET_OS_INFO)
+    string(REGEX MATCH "Linux" OS_IS_LINUX ${CMAKE_SYSTEM_NAME})
+    set(FLANN_LIB_INSTALL_DIR "lib")
+    set(FLANN_INCLUDE_INSTALL_DIR
+        "include/${PROJECT_NAME_LOWER}-${FLANN_MAJOR_VERSION}.${FLANN_MINOR_VERSION}")
+endmacro(GET_OS_INFO)
+
+
+macro(DISSECT_VERSION)
+    # Find version components
+    string(REGEX REPLACE "^([0-9]+).*" "\\1"
+        FLANN_VERSION_MAJOR "${FLANN_VERSION}")
+    string(REGEX REPLACE "^[0-9]+\\.([0-9]+).*" "\\1"
+        FLANN_VERSION_MINOR "${FLANN_VERSION}")
+    string(REGEX REPLACE "^[0-9]+\\.[0-9]+\\.([0-9]+)" "\\1"
+        FLANN_VERSION_PATCH ${FLANN_VERSION})
+    string(REGEX REPLACE "^[0-9]+\\.[0-9]+\\.[0-9]+(.*)" "\\1"
+        FLANN_VERSION_CANDIDATE ${FLANN_VERSION})
+    set(FLANN_SOVERSION "${FLANN_VERSION_MAJOR}.${FLANN_VERSION_MINOR}")
+endmacro(DISSECT_VERSION)
+
+
+# workaround a FindHDF5 bug
+macro(find_hdf5)
+    find_package(HDF5)
+
+    set( HDF5_IS_PARALLEL FALSE )
+    foreach( _dir ${HDF5_INCLUDE_DIRS} )
+        if( EXISTS "${_dir}/H5pubconf.h" )
+            file( STRINGS "${_dir}/H5pubconf.h" 
+                HDF5_HAVE_PARALLEL_DEFINE
+                REGEX "HAVE_PARALLEL 1" )
+            if( HDF5_HAVE_PARALLEL_DEFINE )
+                set( HDF5_IS_PARALLEL TRUE )
+            endif()
+        endif()
+    endforeach()
+    set( HDF5_IS_PARALLEL ${HDF5_IS_PARALLEL} CACHE BOOL
+        "HDF5 library compiled with parallel IO support" )
+    mark_as_advanced( HDF5_IS_PARALLEL )
+endmacro(find_hdf5)
+
+
+macro(flann_add_gtest exe)
+    # add build target
+    add_executable(${exe} EXCLUDE_FROM_ALL ${ARGN})
+    target_link_libraries(${exe} ${GTEST_LIBRARIES})
+    # add dependency to 'tests' target
+    add_dependencies(flann_gtests ${exe})
+
+    # add target for running test
+    string(REPLACE "/" "_" _testname ${exe})
+    add_custom_target(test_${_testname}
+                    COMMAND ${exe}
+                    ARGS --gtest_print_time
+                    DEPENDS ${exe}
+                    WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/test
+                    VERBATIM
+                    COMMENT "Runnint gtest test(s) ${exe}")
+    # add dependency to 'test' target
+    add_dependencies(flann_gtest test_${_testname})
+endmacro(flann_add_gtest)
+
+macro(flann_add_cuda_gtest exe)
+    # add build target
+    cuda_add_executable(${exe} EXCLUDE_FROM_ALL ${ARGN})
+    target_link_libraries(${exe} ${GTEST_LIBRARIES})
+    # add dependency to 'tests' target
+    add_dependencies(tests ${exe})
+
+    # add target for running test
+    string(REPLACE "/" "_" _testname ${exe})
+    add_custom_target(test_${_testname}
+                    COMMAND ${exe}
+                    ARGS --gtest_print_time
+                    DEPENDS ${exe}
+                    WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/test
+                    VERBATIM
+                    COMMENT "Runnint gtest test(s) ${exe}")
+    # add dependency to 'test' target
+    add_dependencies(test test_${_testname})
+endmacro(flann_add_cuda_gtest)
+
+macro(flann_add_pyunit file)
+    # find test file
+    set(_file_name _file_name-NOTFOUND)
+    find_file(_file_name ${file} ${CMAKE_CURRENT_SOURCE_DIR})
+    if(NOT _file_name)
+        message(FATAL_ERROR "Can't find pyunit file \"${file}\"")
+    endif(NOT _file_name)
+
+    # add target for running test
+    string(REPLACE "/" "_" _testname ${file})
+    add_custom_target(pyunit_${_testname}
+                    COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/bin/run_test.py ${_file_name}
+                    DEPENDS ${_file_name}
+                    WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/test
+                    VERBATIM
+                    COMMENT "Running pyunit test(s) ${file}" )
+    # add dependency to 'test' target
+    add_dependencies(pyunit_${_testname} flann)
+    add_dependencies(test pyunit_${_testname})
+endmacro(flann_add_pyunit)
+
+
+
+macro(flann_download_test_data _name _md5)
+    string(REPLACE "/" "_" _dataset_name dataset_${_name})
+    
+    add_custom_target(${_dataset_name}
+        COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/bin/download_checkmd5.py http://people.cs.ubc.ca/~mariusm/uploads/FLANN/datasets/${_name} ${TEST_OUTPUT_PATH}/${_name} ${_md5}
+        VERBATIM)
+
+    # Also make sure that downloads are done before we run any tests
+    add_dependencies(tests ${_dataset_name})
+
+endmacro(flann_download_test_data)
diff --git a/cmake/uninstall_target.cmake.in b/cmake/uninstall_target.cmake.in
new file mode 100644
index 0000000..e3db04e
--- /dev/null
+++ b/cmake/uninstall_target.cmake.in
@@ -0,0 +1,19 @@
+if(NOT EXISTS "@PROJECT_BINARY_DIR@/install_manifest.txt")
+    message(FATAL_ERROR "Cannot find install manifest: \"@PROJECT_BINARY_DIR@/install_manifest.txt\"")
+endif(NOT EXISTS "@PROJECT_BINARY_DIR@/install_manifest.txt")
+
+file(READ "@PROJECT_BINARY_DIR@/install_manifest.txt" files)
+string(REGEX REPLACE "\n" ";" files "${files}")
+foreach(file ${files})
+    message(STATUS "Uninstalling \"$ENV{DESTDIR}${file}\"")
+    if(EXISTS "$ENV{DESTDIR}${file}")
+        exec_program("@CMAKE_COMMAND@" ARGS "-E remove \"$ENV{DESTDIR}${file}\""
+            OUTPUT_VARIABLE rm_out RETURN_VALUE rm_retval)
+        if(NOT "${rm_retval}" STREQUAL 0)
+            message(FATAL_ERROR "Problem when removing \"$ENV{DESTDIR}${file}\"")
+        endif(NOT "${rm_retval}" STREQUAL 0)
+    else(EXISTS "$ENV{DESTDIR}${file}")
+        message(STATUS "File \"$ENV{DESTDIR}${file}\" does not exist.")
+    endif(EXISTS "$ENV{DESTDIR}${file}")
+endforeach(file)
+
diff --git a/doc/CMakeLists.txt b/doc/CMakeLists.txt
new file mode 100644
index 0000000..2d8eb24
--- /dev/null
+++ b/doc/CMakeLists.txt
@@ -0,0 +1,19 @@
+find_package(LATEX)
+ 
+if (EXISTS ${PDFLATEX_COMPILER} AND EXISTS ${BIBTEX_COMPILER})
+    include(${PROJECT_SOURCE_DIR}/cmake/UseLATEX.cmake)
+
+    add_latex_document(manual.tex BIBFILES references.bib IMAGE_DIRS images DEFAULT_PDF)
+
+    add_custom_command(OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/manual.pdf
+        COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/manual.pdf ${CMAKE_CURRENT_SOURCE_DIR}/manual.pdf
+        DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/manual.pdf
+    )
+    add_custom_target(doc DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/manual.pdf)
+endif()
+
+install(
+    FILES manual.pdf
+    DESTINATION share/doc/flann
+    OPTIONAL
+)
diff --git a/doc/images/cmake-gui.png b/doc/images/cmake-gui.png
new file mode 100644
index 0000000..85d9158
Binary files /dev/null and b/doc/images/cmake-gui.png differ
diff --git a/doc/manual.pdf b/doc/manual.pdf
new file mode 100644
index 0000000..8f3b2b0
Binary files /dev/null and b/doc/manual.pdf differ
diff --git a/doc/manual.tex b/doc/manual.tex
new file mode 100644
index 0000000..f24b286
--- /dev/null
+++ b/doc/manual.tex
@@ -0,0 +1,1517 @@
+\documentclass[letter,10pt]{article}
+\usepackage{latexsym}
+\usepackage{amsmath}
+\usepackage{amssymb}
+\usepackage{amsthm}
+\usepackage{amsfonts}
+\usepackage{epsfig}
+\usepackage{url}
+\usepackage{fancyvrb}
+\usepackage{hyperref}
+
+
+%opening
+\title{FLANN - Fast Library for Approximate Nearest Neighbors\\[0.5cm] User
+Manual\\[1cm]}
+
+\author{Marius Muja, mariusm at cs.ubc.ca\\David Lowe, lowe at cs.ubc.ca}
+
+
+\begin{document}
+
+
+ \begin{titlepage}
+\vspace{10cm}
+\maketitle
+\thispagestyle{empty}
+ \end{titlepage}
+
+
+\section{Introduction}
+
+We can define the \emph{nearest neighbor search (NSS)} problem in the
+following way: given a set of points $P=p_1,p_2,\dots,p_n$ in a metric
+space $X$, these points must be preprocessed in such a way that given a new
+query point $q \in X$, finding the point in $P$ that is nearest to $q$ can
+be done quickly.
+
+The problem of nearest neighbor search is one of major importance in a
+variety of applications such as image recognition, data compression,
+pattern recognition and classification, machine learning,  document
+retrieval systems, statistics and data analysis. However, solving this
+problem in high dimensional spaces seems to be a very difficult task and
+there is no algorithm that performs significantly better than the standard
+brute-force search. This has lead to an increasing interest in a class of
+algorithms that perform approximate nearest neighbor searches, which have
+proven to be a good-enough approximation in most practical applications and
+in most cases, orders of magnitude faster that the algorithms performing
+the exact searches.
+
+FLANN (Fast Library for Approximate Nearest Neighbors) is a library for
+performing fast approximate nearest neighbor searches. FLANN is written in
+the C++ programming language. FLANN can be easily used in many contexts
+through the C, MATLAB and Python bindings provided with the library.
+
+
+
+\subsection{Quick Start}
+\label{sec:quickstart}
+
+
+This section contains small examples of how to use the FLANN library from
+different programming languages (C++, C, MATLAB and Python).
+
+\begin{itemize}
+
+
+\item \textbf{C++}
+\begin{Verbatim}[fontsize=\scriptsize,frame=single]
+// file flann_example.cpp
+
+#include <flann/flann.hpp>
+#include <flann/io/hdf5.h>
+
+#include <stdio.h>
+
+int main(int argc, char** argv)
+{
+    int nn = 3;
+
+    flann::Matrix<float> dataset;
+    flann::Matrix<float> query;
+    flann::load_from_file(dataset, "dataset.hdf5","dataset");
+    flann::load_from_file(query, "dataset.hdf5","query");
+
+    flann::Matrix<int> indices(new int[query.rows*nn], query.rows, nn);
+    flann::Matrix<float> dists(new float[query.rows*nn], query.rows, nn);
+
+    // construct an randomized kd-tree index using 4 kd-trees
+    flann::Index<flann::L2<float> > index(dataset, flann::KDTreeIndexParams(4));
+    index.buildIndex();                                                                                               
+
+    // do a knn search, using 128 checks
+    index.knnSearch(query, indices, dists, nn, flann::SearchParams(128));
+
+    flann::save_to_file(indices,"result.hdf5","result");
+
+    delete[] dataset.ptr();
+    delete[] query.ptr();
+    delete[] indices.ptr();
+    delete[] dists.ptr();
+    
+    return 0;
+}
+
+\end{Verbatim}
+
+
+\item \textbf{C}
+\begin{Verbatim}[fontsize=\scriptsize,frame=single]
+/* file flann_example.c */
+
+#include "flann.h"
+#include <stdio.h>
+#include <assert.h>
+
+/* Function that reads a dataset */
+float* read_points(char* filename, int *rows, int *cols);
+
+int main(int argc, char** argv)
+{
+   int rows,cols;
+   int t_rows, t_cols;
+   float speedup;
+
+   /* read dataset points from file dataset.dat */
+   float* dataset = read_points("dataset.dat", &rows, &cols);
+   float* testset = read_points("testset.dat", &t_rows, &t_cols);
+
+   /* points in dataset and testset should have the same dimensionality */
+   assert(cols==t_cols);
+
+   /* number of nearest neighbors to search */
+   int nn = 3;
+   /* allocate memory for the nearest-neighbors indices */
+   int* result = (int*) malloc(t_rows*nn*sizeof(int));                                                               
+   /* allocate memory for the distances */
+   float* dists = (float*) malloc(t_rows*nn*sizeof(float));
+
+   /* index parameters are stored here */
+   struct FLANNParameters p = DEFAULT_FLANN_PARAMETERS;
+   p.algorithm = FLANN_INDEX_AUTOTUNED;  /* or FLANN_INDEX_KDTREE, FLANN_INDEX_KMEANS, ... /*
+   p.target_precision = 0.9;  /* want 90% target precision */
+
+   /* compute the 3 nearest-neighbors of each point in the testset */
+   flann_find_nearest_neighbors(dataset, rows, cols, testset, t_rows,
+result, dists, nn, &p);
+
+   ...
+   free(dataset);
+   free(testset);
+   free(result);
+   free(dists);
+
+   return 0;
+}
+
+\end{Verbatim}
+
+
+
+\item \textbf{MATLAB}
+\begin{Verbatim}[fontsize=\scriptsize,frame=single]
+% create random dataset and test set
+dataset = single(rand(128,10000));
+testset = single(rand(128,1000));
+
+% define index and search parameters
+params.algorithm = 'kdtree';
+params.trees = 8;
+params.checks = 64;
+
+% perform the nearest-neighbor search
+[result, dists] = flann_search(dataset,testset,5,params);
+\end{Verbatim}
+
+\item \textbf{Python}
+\begin{Verbatim}[fontsize=\scriptsize,frame=single]
+from pyflann import *
+from numpy import *
+from numpy.random import *
+
+dataset = rand(10000, 128)
+testset = rand(1000, 128)
+
+flann = FLANN()
+result,dists = flann.nn(dataset,testset,5,algorithm="kmeans",
+               branching=32, iterations=7, checks=16);
+\end{Verbatim}
+
+\end{itemize}
+
+
+
+\section{Downloading and compiling FLANN}
+\label{sec:downloading_and_compiling}
+
+FLANN can be downloaded from the following address:
+\begin{center}
+\texttt{http://www.cs.ubc.ca/$\sim$mariusm/flann}
+\end{center}
+
+After downloading and unpacking, the following files and directories should
+be present:
+\begin{itemize}
+\item \texttt{bin}: directory various for scripts and binary files
+\item \texttt{doc}: directory containg this documentation
+\item \texttt{examples}: directory containg examples of using FLANN
+\item \texttt{src}: directory containg the source files
+\item \texttt{test}: directory containg unit tests for FLANN
+\end{itemize}
+
+
+
+To compile the FLANN library the \textit{CMake}\footnote{http://www.cmake.org/} build system is required.
+Below is an example of how FLANN can be compiled on Linux (replace x.y.z with the corresponding version number).
+\begin{Verbatim}[fontsize=\scriptsize,frame=single]
+$ cd flann-x.y.z-src
+$ mkdir build
+$ cd build
+$ cmake ..
+$ make
+\end{Verbatim}
+
+On windows the steps are very similar:
+ 
+\begin{Verbatim}[fontsize=\scriptsize,frame=single]
+> "C:\Program Files\Microsoft Visual Studio 9.0\VC\vcvarsall.bat"
+> cd flann-x.y.z-src
+> mkdir build
+> cd build
+> cmake ..
+> nmake
+\end{Verbatim}
+
+There are several compile options that can be configured before FLANN is compiled, for example the build type 
+(Release, RelWithDebInfo, Debug) or whether to compile the C, Python or the MATLAB bindings. To change any of this
+options use the \texttt{cmake-gui} application after \texttt{cmake} has finished (see figure \ref{fig:cmake-gui}).
+\begin{Verbatim}[fontsize=\scriptsize,frame=single]
+> cmake-gui .
+\end{Verbatim}
+
+\begin{figure}[h]
+  \begin{center}
+    \includegraphics[width=0.9\textwidth]{images/cmake-gui.png}
+    \caption{Configuring the FLANN compile options}
+    \label{fig:cmake-gui}
+  \end{center}
+\end{figure}
+
+\subsection{Upgrading from a previous version}
+
+This section contains changes that you need to be aware of when upgrading from a previous version of FLANN.
+
+\begin{description}
+ \item[Version 1.7.0] A small breaking API change in \texttt{flann::Matrix} requires client code to be updated. In order
+to release the memory used by a matrix, use:
+\begin{Verbatim}[fontsize=\scriptsize]
+ delete[] matrix.ptr();
+\end{Verbatim}
+instead of:
+\begin{Verbatim}[fontsize=\scriptsize]
+ delete[] matrix.data;
+\end{Verbatim}
+or:
+\begin{Verbatim}[fontsize=\scriptsize]
+ matrix.free();
+\end{Verbatim}
+The member \texttt{data} of \texttt{flann::Matrix} is not publicly accessible any more, use the \texttt{ptr()} method
+to obtain the pointer to the memory buffer.
+\end{description}
+
+
+
+\subsection{Compiling FLANN with multithreading support}
+
+For taking advantage of multithreaded search, the project that uses FLANN needs to be compiled with a compiler that 
+supports the OpenMP standard and the OpenMP support must be enabled. The number of cores to be used can be selected with 
+the \texttt{cores} field in the \texttt{SearchParams} structure. By default a single core will be used. 
+Setting the \texttt{cores} field to zero will automatically use as many threads as cores available on the machine.
+
+\section{Using FLANN}
+
+\subsection{Using FLANN from C++}
+
+The core of the FLANN library is written in C++. To make use of the full power 
+and flexibility of the templated code one should use the C++ bindings if possible. 
+To use the C++ bindings you only need to include the  the library header file \texttt{flann.hpp}. An example
+of the compile command that must be used will look something like this:
+\begin{Verbatim}[fontsize=\footnotesize]
+g++ flann_example.cpp -I $FLANN_ROOT/include -o flann_example_cpp
+\end{Verbatim}
+where \texttt{\$FLANN\_ROOT} is the library main directory.
+
+The following sections describe the public C++ API.
+
+\subsubsection{flann::Index}
+\label{sec:flann::Index}
+The FLANN nearest neighbor index class. This class is used to abstract different types of nearest neighbor search
+indexes. The class is templated on the distance functor to be used for computing distances between pairs of features. 
+
+\begin{Verbatim}[fontsize=\footnotesize,frame=single]
+namespace flann
+{
+    template<typename Distance>
+    class Index 
+    {
+	typedef typename Distance::ElementType ElementType;
+	typedef typename Distance::ResultType DistanceType;
+    public:
+        Index(const IndexParams& params, Distance distance = Distance() );
+        
+        Index(const Matrix<ElementType>& points, const IndexParams& params,
+                Distance distance = Distance() );
+
+	~Index();
+
+	void buildIndex();        
+        
+        void buildIndex(const Matrix<ElementType>& points);
+        
+        void addPoints(const Matrix<ElementType>& points, 
+                       float rebuild_threshold = 2);
+        
+        void removePoint(size_t point_id);
+        
+        ElementType* getPoint(size_t point_id);
+
+	int knnSearch(const Matrix<ElementType>& queries, 
+		       Matrix<int>& indices, 
+		       Matrix<DistanceType>& dists, 
+		       size_t knn, 
+		       const SearchParams& params);
+
+        int knnSearch(const Matrix<ElementType>& queries,
+                       std::vector< std::vector<int> >& indices,
+                       std::vector<std::vector<DistanceType> >& dists,
+                       size_t knn,
+                       const SearchParams& params);
+
+	int radiusSearch(const Matrix<ElementType>& queries, 
+			 Matrix<int>& indices, 
+			 Matrix<DistanceType>& dists, 
+			 float radius, 
+			 const SearchParams& params);
+
+        int radiusSearch(const Matrix<ElementType>& queries,
+                          std::vector< std::vector<int> >& indices,
+                          std::vector<std::vector<DistanceType> >& dists,
+                          float radius,
+                          const SearchParams& params);
+
+	void save(std::string filename);
+
+	int veclen() const;
+
+	int size() const;
+
+	IndexParams getParameters() const;
+
+        flann_algorithm_t getType() const;
+
+    };
+}
+\end{Verbatim}
+
+
+\textbf{The Distance functor}
+
+The distance functor is a class whose \texttt{operator()} computes the distance between two features. If the distance is
+also a kd-tree compatible distance it should also provide an \texttt{accum\_dist()} method that computes the distance
+between individual feature dimensions. A typical distance functor looks like this (see the \texttt{dist.h} file for more
+examples):
+
+\begin{Verbatim}[fontsize=\footnotesize,frame=single]
+template<class T>
+struct L2
+{
+    typedef bool is_kdtree_distance;
+
+    typedef T ElementType;
+    typedef typename Accumulator<T>::Type ResultType;
+
+    template <typename Iterator1, typename Iterator2>
+    ResultType operator()(Iterator1 a, Iterator2 b, size_t size, 
+                          ResultType /*worst_dist*/ = -1) const
+    {
+        ResultType result = ResultType();
+        ResultType diff;
+        for(size_t i = 0; i < size; ++i ) {
+            diff = *a++ - *b++;
+            result += diff*diff;
+        }
+        return result;
+    }
+
+    template <typename U, typename V>
+    inline ResultType accum_dist(const U& a, const V& b, int) const
+    {
+        return (a-b)*(a-b);
+    }
+};
+\end{Verbatim}
+
+In addition to \texttt{operator()} and \texttt{accum\_dist()}, a distance functor should also define the
+\texttt{ElementType} and the \texttt{ResultType} as the types of the elements it operates on and the type of the result
+it computes.
+
+If a distance functor can be used as a kd-tree distance (meaning that the full distance between a pair of features can
+be accumulated from the partial distances between the individual dimensions) a typedef \texttt{is\_kdtree\_distance}
+should be present inside the distance functor. If the distance is not a kd-tree distance, but it's a distance in a
+vector space (the individual dimensions of the elements it operates on can be accessed independently) a typedef
+\texttt{is\_vector\_space\_distance} should be defined inside the functor. If neither typedef is defined, the distance
+is assumed to be a metric distance and will only be used with indexes operating on generic metric distances.
+\\
+
+
+\textbf{flann::Index::Index}
+Constructs a nearest neighbor search index for a given dataset.
+\begin{Verbatim}[fontsize=\footnotesize,frame=single]
+Index(const IndexParams& params, Distance distance = Distance() );
+
+Index(const Matrix<ElementType>& points, const IndexParams& params,
+        Distance distance = Distance() );
+\end{Verbatim}
+\begin{description}
+\item[features] Matrix containing the features(points) that should be indexed, stored in a row-major order (one point 
+on each row of the matrix). The size of the matrix is $num\_features \times dimensionality$.
+
+\item[params] Structure containing the index parameters. The type of index that will be constructed depends on the type 
+of this parameter. The possible parameter types are:
+
+\textbf{LinearIndexParams} When passing an object of this type, the index will perform a linear, brute-force search.
+\begin{Verbatim}[fontsize=\footnotesize]
+struct LinearIndexParams : public IndexParams 
+{
+};
+\end{Verbatim}
+
+\textbf{KDTreeIndexParams} When passing an object of this type the index constructed will consist of a set 
+of randomized kd-trees which will be searched in parallel.
+\begin{Verbatim}[fontsize=\footnotesize]
+struct KDTreeIndexParams : public IndexParams
+{
+      KDTreeIndexParams( int trees = 4 );
+};
+\end{Verbatim}
+\begin{description}
+ \item[trees] The number of parallel kd-trees to use. Good values are in the range [1..16]
+\end{description}
+
+\textbf{KMeansIndexParams} When passing an object of this type the index constructed will be a hierarchical k-means tree. 
+\begin{Verbatim}[fontsize=\footnotesize]
+struct KMeansIndexParams : public IndexParams
+{
+	KMeansIndexParams( int branching = 32,
+			int iterations = 11,
+			flann_centers_init_t centers_init = FLANN_CENTERS_RANDOM,
+			float cb_index = 0.2 );
+};
+\end{Verbatim}
+\begin{description}
+\item[branching]{ The branching factor to use for the hierarchical k-means tree }
+\item[iterations]{ The maximum number of iterations to use in the k-means clustering 
+		    stage when building the k-means tree. If a value of -1 is used here, it means
+		    that the k-means clustering should be iterated until convergence}
+\item[centers\_init]{ The algorithm to use for selecting the initial
+		  centers when performing a k-means clustering step. The possible values are
+		  CENTERS\_RANDOM (picks the initial cluster centers randomly), CENTERS\_GONZALES (picks the
+		  initial centers using Gonzales' algorithm) and CENTERS\_KMEANSPP (picks the initial
+		centers using the algorithm suggested in \cite{arthur_kmeanspp_2007}) }
+\item[cb\_index]{ This parameter (cluster boundary index) influences the
+		  way exploration is performed in the hierarchical kmeans tree. When \texttt{cb\_index} is zero
+		  the next kmeans domain to be explored is choosen to be the one with the closest center. 
+		  A value greater then zero also takes into account the size of the domain.}
+\end{description}
+
+
+\textbf{CompositeIndexParams} When using a parameters object of this type the index created combines the randomized
+kd-trees 
+        and the hierarchical k-means tree.
+\begin{Verbatim}[fontsize=\footnotesize]
+struct CompositeIndexParams : public IndexParams
+{
+        CompositeIndexParams( int trees = 4,
+                          int branching = 32,
+                          int iterations = 11,
+                          flann_centers_init_t centers_init = FLANN_CENTERS_RANDOM, 
+                          float cb_index = 0.2 );
+};
+\end{Verbatim}
+
+
+\textbf{KDTreeSingleIndexParams} When passing an object of this type the index will contain a single kd-tree
+optimized for searching lower dimensionality data (for example 3D point clouds)
+\begin{Verbatim}[fontsize=\footnotesize]
+struct KDTreeSingleIndexParams : public IndexParams
+{
+      KDTreeSingleIndexParams( int leaf_max_size = 10 );
+};
+\end{Verbatim}
+\begin{description}
+ \item[max\_leaf\_size] The maximum number of points to have in a leaf for not branching the tree any more.
+\end{description}
+
+\textbf{KDTreeCuda3dIndexParams} When passing an object of this type the index will be a single kd-tree that 
+is built and performs searches on a CUDA compatible GPU. Search performance is best for large numbers of search and query points.
+For more information, see section \ref{sec:flann::cuda}
+\begin{Verbatim}[fontsize=\footnotesize]
+struct KDTreeCuda3dIndexParams : public IndexParams
+{
+    KDTreeCuda3dIndexParams( int leaf_max_size = 64 );
+};
+\end{Verbatim}
+\begin{description}
+ \item[max\_leaf\_size] The maximum number of points to have in a leaf for not branching the tree any more.
+\end{description}
+
+
+\textbf{HierarchicalClusteringIndexParams} When passing an object of this type the index constructed will be a
+hierarchical clustering index. This type of index works with any metric distance and can be used for matching 
+binary features using Hamming distances.
+\begin{Verbatim}[fontsize=\footnotesize]
+struct HierarchicalClusteringIndexParams : public IndexParams
+{
+    HierarchicalClusteringIndexParams(int branching = 32,
+                              flann_centers_init_t centers_init = FLANN_CENTERS_RANDOM,
+                              int trees = 4, int leaf_max_size = 100)
+};
+\end{Verbatim}
+\begin{description}
+\item[branching]{ The branching factor to use for the hierarchical clustering tree }
+\item[centers\_init]{ The algorithm to use for selecting the initial
+                  centers when performing a k-means clustering step. The possible values are
+                  CENTERS\_RANDOM (picks the initial cluster centers randomly), CENTERS\_GONZALES (picks the
+                  initial centers using Gonzales' algorithm) and CENTERS\_KMEANSPP (picks the initial
+                centers using the algorithm suggested in \cite{arthur_kmeanspp_2007}) }
+\item[trees] The number of parallel trees to use. Good values are in the range [3..8]
+\item[leaf\_size] The maximum number of points a leaf node should contain.
+\end{description}
+
+
+\textbf{LshIndexParams} When passing an object of this type the index constructed will be a multi-probe LSH
+(Locality-Sensitive Hashing) index. This type of index can only be used for matching binary features using Hamming
+distances.
+\begin{Verbatim}[fontsize=\footnotesize]
+struct LshIndexParams : public IndexParams
+{
+    LshIndexParams(unsigned int table_number = 12, 
+                  unsigned int key_size = 20, 
+                  unsigned int multi_probe_level = 2);
+};
+\end{Verbatim}
+\begin{description}
+\item[table\_number]{ The number of hash tables to use }
+\item[key\_size]{ The length of the key in the hash tables}
+\item[multi\_probe\_level] Number of levels to use in multi-probe (0 for standard LSH)
+\end{description}
+
+
+\textbf{AutotunedIndexParams}
+  When passing an object of this type the index created is automatically tuned to offer 
+the best performance, by choosing the optimal index type (randomized kd-trees, hierarchical kmeans, linear) and parameters for the
+dataset provided.
+\begin{Verbatim}[fontsize=\footnotesize]
+struct AutotunedIndexParams : public IndexParams
+{
+	AutotunedIndexParams( float target_precision = 0.9,
+			  float build_weight = 0.01,
+			  float memory_weight = 0,
+			  float sample_fraction = 0.1 );
+};
+\end{Verbatim}
+\begin{description}
+\item[target\_precision]{ Is a number between 0 and 1 specifying the
+percentage of the approximate nearest-neighbor searches that return the
+exact nearest-neighbor. Using a higher value for this parameter gives
+more accurate results, but the search takes longer. The optimum value
+usually depends on the application. }
+
+\item[build\_weight]{ Specifies the importance of the
+index build time raported to the nearest-neighbor search time. In some
+applications it's acceptable for the index build step to take a long time
+if the subsequent searches in the index can be performed very fast. In
+other applications it's required that the index be build as fast as
+possible even if that leads to slightly longer search times.}
+
+\item[memory\_weight]{Is used to specify the tradeoff between
+time (index build time and search time) and memory used by the index. A
+value less than 1 gives more importance to the time spent and a value
+greater than 1 gives more importance to the memory usage.}
+
+\item[sample\_fraction]{Is a number between 0 and 1 indicating what fraction
+of the dataset to use in the automatic parameter configuration algorithm. Running the 
+algorithm on the full dataset gives the most accurate results, but for
+very large datasets can take longer than desired. In such case using just a fraction of the
+data helps speeding up this algorithm while still giving good approximations of the
+optimum parameters.}
+\end{description}
+
+\textbf{SavedIndexParams}
+This object type is used for loading a previously saved index from the disk.
+\begin{Verbatim}[fontsize=\footnotesize]
+struct SavedIndexParams : public IndexParams
+{
+      SavedIndexParams( std::string filename );
+};
+\end{Verbatim}
+\begin{description}
+\item[filename]{ The filename in which the index was saved. }
+\end{description}
+\end{description}
+
+\subsubsection{flann::Index::buildIndex}
+Builds the nearest neighbor search index. There are two versions of the \texttt{buildIndex} method, one that
+uses the points provided as argument and one that uses the points provided to the constructor when the object was
+constructed.
+\begin{Verbatim}[fontsize=\footnotesize,frame=single]
+void buildIndex();        
+
+void buildIndex(const Matrix<ElementType>& points);
+\end{Verbatim}
+
+\subsubsection{flann::Index::addPoints}
+The \texttt{addPoints} method can be used to incrementally add points to the index after the index was build.
+To avoid the index getting unbalanced, the \texttt{addPoints} method has the option of rebuilding the index after a
+large number of points have been added. The \texttt{rebuild\_threshold} parameter controls when the index is
+rebuild, by default this is done when it doubles in size (\texttt{rebuild\_threshold} = 2).
+
+\begin{Verbatim}[fontsize=\footnotesize,frame=single]
+void addPoints(const Matrix<ElementType>& points, float rebuild_threshold = 2);
+\end{Verbatim}
+
+\subsubsection{flann::Index::removePoint}
+The \texttt{removePoint} method removes one point with the specified \texttt{point\_id} from the index. The indices
+ (of the remaining points) returned by the nearest neighbor operations do not change when points are removed from the
+index.
+
+\begin{Verbatim}[fontsize=\footnotesize,frame=single]
+void removePoint(size_t point_id);
+\end{Verbatim}
+
+\subsubsection{flann::Index::getPoint}
+The \texttt{getPoint} method returns a pointer to the data point with the specified \texttt{point\_id}.
+
+\begin{Verbatim}[fontsize=\footnotesize,frame=single]
+ElementType* getPoint(size_t point_id);
+\end{Verbatim}
+
+\subsubsection{flann::Index::knnSearch}
+Performs a K-nearest neighbor search for a set of query points. There are two signatures for this
+method, one that takes pre-allocated \texttt{flann::Matrix} objects for returning the indices of and distances to the
+neighbors found, and one that takes \texttt{std::vector<std::vector>} that will re resized automatically as needed.
+\begin{Verbatim}[fontsize=\footnotesize,frame=single]
+int Index::knnSearch(const Matrix<ElementType>& queries,
+		Matrix<int>& indices, 
+		Matrix<DistanceType>& dists,
+		size_t knn,
+		const SearchParams& params);
+
+int Index::knnSearch(const Matrix<ElementType>& queries,
+                std::vector< std::vector<int> >& indices,
+                std::vector<std::vector<DistanceType> >& dists,
+                size_t knn,
+                const SearchParams& params);
+\end{Verbatim}
+\begin{description}
+\item[queries]{Matrix containing the query points. Size of matrix is ($num\_queries \times dimentionality $)}
+\item[indices]{Matrix that will contain the indices of the K-nearest neighbors found (size should be at least
+$num\_queries \times knn$ for the pre-allocated version).}
+\item[dists]{Matrix that will contain the distances to the K-nearest neighbors found (size should be at least
+$num\_queries \times knn$ for the pre-allocated version).}
+\item[knn]{Number of nearest neighbors to search for.}
+\item[params]{Search parameters.} Structure containing parameters used during search.
+
+\textbf{SearchParameters}
+\begin{Verbatim}[fontsize=\footnotesize]
+struct SearchParams
+{
+	SearchParams(int checks = 32,
+		  float eps = 0,
+		  bool sorted = true);
+
+    int checks;
+    float eps;
+    bool sorted;
+    int max_neighbors;
+    tri_type use_heap;
+    int cores;
+    bool matrices_in_gpu_ram;
+};
+\end{Verbatim}
+\begin{description}
+ \item[checks] specifies the maximum leafs to visit when searching for neighbours. A
+higher value for this parameter would give better search precision, but
+also take more time. For all leafs to be checked use the value \texttt{CHECKS\_UNLIMITED}.
+If automatic configuration was used when the index was created, the number of checks 
+required to achieve the specified precision was also computed, to use that value specify \texttt{CHECKS\_AUTOTUNED}.
+ \item[eps] Search for eps-approximate neighbors (only used by KDTreeSingleIndex and KDTreeCuda3dIndex).
+ \item[sorted] Used only by radius search, specifies if the neighbors returned should be sorted by distance.
+ \item[max\_neighbours] Specifies the maximum number of neighbors radius search should return (default: -1 =
+unlimited). Only used for radius search.
+ \item[use\_heap] Use a heap data structure to manage the list of neighbors internally (possible values: FLANN\_False,
+FLANN\_True, FLANN\_Undefined). A heap is more efficient for a large number of neighbors and less efficient for a small
+number of neighbors. Default value is FLANN\_Undefined, which lets the code choose the best option depending on the
+number of neighbors requested. Only used for KNN search.
+ \item[cores] How many cores to assign to the search (specify 0 for automatic core selection).
+ \item[matrices\_in\_gpu\_ram] for GPU search indicates if matrices are already in GPU ram.
+\end{description}
+\end{description}
+
+
+\subsubsection{flann::Index::radiusSearch}
+Performs a radius nearest neighbor search for a set of query points. There are two signatures for this method,
+one that takes pre-allocated \texttt{flann::Matrix} objects for returning the indices of and distances to the neighbors
+found, and one that takes \texttt{std::vector<std::vector>} that will resized automatically as needed.
+\begin{Verbatim}[fontsize=\footnotesize,frame=single]
+int Index::radiusSearch(const Matrix<ElementType>& queries,
+		  Matrix<int>& indices,
+		  Matrix<DistanceType>& dists,
+		  float radius,
+		  const SearchParams& params); 
+
+int Index::radiusSearch(const Matrix<ElementType>& queries,
+                  std::vector< std::vector<int> >& indices,
+                  std::vector<std::vector<DistanceType> >& dists,
+                  float radius,
+                  const SearchParams& params);
+\end{Verbatim}
+
+\begin{description}
+\item[queries]{The query point. Size of matrix is ($num\_queries \times dimentionality $).}
+\item[indices]{Matrix that will contain the indices of the K-nearest neighbors found. For the pre-allocated version,
+only as many neighbors are returned as many columns in this matrix. If fewer neighbors are found than
+columns in this matrix, the element after that last index returned is -1. In case of the std::vector version, the rows
+will be resized as needed to fit all the neighbors to be returned, except if the ``max\_neighbors'' search parameter is
+set.}
+\item[dists]{Matrix that will contain the distances to the K-nearest neighbors found. The same number of values are
+returned here as for the \texttt{indices} matrix.}
+\item[radius]{The search radius}
+\item[params]{Search parameters}
+\end{description}
+The method returns the number of nearest neighbors found.
+
+
+\subsubsection{flann::Index::save}
+Saves the index to a file.
+\begin{Verbatim}[fontsize=\footnotesize,frame=single]
+  void Index::save(std::string filename);
+\end{Verbatim}
+\begin{description}
+\item[filename]{The file to save the index to}
+\end{description}
+
+\subsubsection{flann::hierarchicalClustering}
+\label{flann::hierarchicalClustering}
+Clusters the given points by constructing a hierarchical k-means tree and choosing a cut in the tree that minimizes the clusters' variance.
+
+\begin{Verbatim}[fontsize=\footnotesize,frame=single]
+template <typename Distance>
+int hierarchicalClustering(const Matrix<typename Distance::ElementType>& features, 
+			Matrix<typename Distance::ResultType>& centers,
+			const KMeansIndexParams& params, 
+		        Distance d = Distance())
+\end{Verbatim}
+\begin{description}
+\item[features]{The points to be clustered}
+\item[centers]{The centers of the clusters obtained. The number of rows in this matrix represents the number of clusters desired.
+However, because of the way the cut in the hierarchical tree is choosen, the number of clusters computed will be
+ the highest number of the form $(branching-1)*k+1$ that's lower than the number of clusters desired, where $branching$ is the tree's 
+branching factor (see description of the KMeansIndexParams).  }
+\item[params]{Parameters used in the construction of the hierarchical k-means tree}
+\end{description}
+The function returns the number of clusters computed.
+
+\subsubsection{flann::KdTreeCuda3dIndex}
+\label{sec:flann::cuda}
+FLANN provides a CUDA implementation of the kd-tree build and search algorithms to improve the build and query speed for large 3d data sets. This section will provide all the necessary information to use the \texttt{KdTreeCuda3dIndex} index type.
+
+\textbf{Building:}
+If CMake detects a CUDA install during the build (see section \ref{sec:downloading_and_compiling}), a library \texttt{libflann\_cuda.so} will be built. 
+
+\textbf{Basic Usage:}
+To be able to use the new index type, you have to include the FLANN header this way:
+\begin{Verbatim}[fontsize=\footnotesize,frame=single]
+#define FLANN_USE_CUDA
+#include <flann/flann.hpp>
+\end{Verbatim}
+If you define the symbol \texttt{FLANN\_USE\_CUDA} before including the FLANN header, you will have to link \texttt{libflann\_cuda.so} or \texttt{libflann\_cuda\_s.a} with your project. 
+However, you will not have to compile your source code with \texttt{nvcc}, except if you use other CUDA code, of course.
+
+You can then create your index by using the \texttt{KDTreeCuda3dIndexParams} to create the index. The index will take care of copying all the data from and to the GPU for you, both 
+for index creation and search.
+
+A word of caution: A general guideline for deciding whether to use the CUDA kd tree or a (multi-threaded) CPU implementation is hard to give, since it depends on the combination of CPU and GPU in each system and the data sets.
+For example, on a system with a Phenom II 955 CPU and a Geforce GTX 260 GPU, the maximum search speedup on a synthetic (random) data set is a factor of about 8-9 vs the single-core CPU search, and starts to be reached at about 100k search and query points. (This includes transfer times.)
+Build time does not profit as much from the GPU acceleration; here the benefit is about 2x at 200k points, but this largely depends on the data set. The only way to know which implementation is best suited is to try it.
+
+\textbf{Advanced Usage:}
+In some cases, you might already have your data in a buffer on the GPU. In this case, you can re-use these buffers instead of copying the buffers back to system RAM for index creation and search.
+The way to do this is to pass GPU pointers via the \texttt{flann::Matrix} inputs and tell the index via the index and search params to treat the pointers as GPU pointers.
+\begin{Verbatim}[fontsize=\footnotesize,frame=single]
+thrust::device_vector<float4>  points_vector( n_points );
+// ... fill vector here...
+
+float* gpu_pointer = (float*)thrust::raw_pointer_cast(&points_vector[0]);
+flann::Matrix<float> matrix_gpu(gpu_pointer,n_points,3, 4);
+
+flann::KDTreeCuda3dIndexParams params;
+params["input_is_gpu_float4"]=true;
+flann::Index<flann::L2<float> > flannindex( matrix_gpu, params  );
+flannindex.buildIndex();
+\end{Verbatim}
+\begin{description}
+ \item First, a GPU buffer of float4 values is created and filled with points. \footnote{For index creation, only \texttt{float4} points are supported, \texttt{float3} or structure-of-array (SOA) representations are currently not supported since
+\texttt{float4} proved to be best in terms of access speed for tree creation and search.}
+ \item Then, a GPU pointer to the buffer is stored in a flann matrix with 3 columns and a stride of 4 (since the last element in the \texttt{float4} is unused).
+ \item Last, the index is created. The \texttt{input\_is\_gpu\_float4} flag tells the index to treat the matrix as a gpu buffer.
+\end{description}
+
+Similarly, you can specify GPU buffers for the search routines that return the result via flann matrices (but not for those that return them via \texttt{std::vector}s).
+To do this, the pointers in the index and dist matrices have to point to GPU buffers and the \texttt{cols} value has to be set to 3 (as we are dealing with 3d points). Here, any value for \texttt{stride} can be used.
+\begin{Verbatim}[fontsize=\footnotesize,frame=single]
+flann::Matrix<int> indices_gpu(gpu_pointer_indices,n_points, knn, stride);
+flann::Matrix<float> dists_gpu(gpu_pointer_dists,n_points, knn, stride);
+
+flann::SearchParams params;
+params.matrices_in_gpu_ram = true;
+
+flannindex.knnSearch( queries_gpu ,indices_gpu,dists_gpu,knn, params);
+\end{Verbatim}
+\begin{description}
+ \item Note that you cannot mix matrices in system and CPU ram here!
+\end{description}
+
+\textbf{Search Parameters:}
+The search routines support three parameters:
+\begin{itemize}
+ \item \texttt{eps} - used for approximate knn search. The maximum possible error is $e= d_{best} * eps$, i.e. the distance of the returned neighbor is at maximum $eps$ times larget than the distance to the real best neighbor.
+ \item \texttt{use\_heap} - used in knn and radius search. If set to true, a heap structure will be used in the search to keep track of the distance to the farthest neighbor. Beneficial with about $k>64$ elements.
+ \item \texttt{sorted} - if set to true, the results of the radius and knn searches will be sorted in ascending order by their distance to the query point.
+ \item \texttt{matrices\_in\_gpu\_ram} - set to true to indicate that all (input and output) matrices are located in GPU RAM.
+\end{itemize}
+
+
+\subsection{Using FLANN from C}
+
+FLANN can be used in C programs through the C bindings provided
+with the library. Because there is no template support in C, there
+are bindings provided for the following data types: \texttt{unsigned char},
+\texttt{int}, \texttt{float} and \texttt{double}. For each of the functions 
+below there is a corresponding version for each of the for data types, for example
+for the function:
+\begin{Verbatim}[fontsize=\footnotesize,frame=single]
+flan_index_t flann_build_index(float* dataset, int rows, int cols, float* speedup,
+	struct FLANNParameters* flann_params);
+\end{Verbatim}
+there are also the following versions:
+\begin{Verbatim}[fontsize=\footnotesize,frame=single]
+flan_index_t flann_build_index_byte(unsigned char* dataset,
+	int rows, int cols, float* speedup,
+	struct FLANNParameters* flann_params);
+flan_index_t flann_build_index_int(int* dataset,
+	int rows, int cols, float* speedup,
+	struct FLANNParameters* flann_params);
+flan_index_t flann_build_index_float(float* dataset,
+	int rows, int cols, float* speedup,
+	struct FLANNParameters* flann_params);
+flan_index_t flann_build_index_double(double* dataset,
+	int rows, int cols, float* speedup,
+	struct FLANNParameters* flann_params);
+\end{Verbatim}
+
+\subsubsection{flann\_build\_index()}
+\begin{Verbatim}[fontsize=\footnotesize,frame=single]
+flan_index_t flann_build_index(float* dataset,
+	int rows,
+	int cols,
+	float* speedup,
+	struct FLANNParameters* flann_params);
+\end{Verbatim}
+This function builds an index and return a reference to it. The arguments
+expected by this function are as follows:
+\begin{description}
+ \item[dataset, rows and cols] - are used to specify the input dataset of
+points: dataset is a pointer to a $\rm{rows} \times \rm{cols}$ matrix
+stored in row-major order (one feature on each row)
+\item [speedup] - is used to return the approximate speedup over linear
+search achieved when using the automatic index and parameter configuration
+(see section \ref{sec:flann_build_index})
+\item [flann\_params] - is a structure containing the parameters passed to
+the function. This structure is defined as follows:
+\begin{Verbatim}[fontsize=\footnotesize]
+struct FLANNParameters {
+  enum flann_algorithm_t algorithm; 	/* the algorithm to use */
+
+  /* search parameters */
+  int checks;            /* how many leafs (features) to check in one search */
+  float cb_index;        /* cluster boundary index. Used when searching the 
+			  kmeans tree */
+
+  /*  kdtree index parameters */
+  int trees;                 /* number of randomized trees to use (for kdtree) */
+
+  /* kmeans index parameters */
+  int branching;             /* branching factor (for kmeans tree) */
+  int iterations;            /* max iterations to perform in one kmeans cluetering 
+			      (kmeans tree) */
+  enum flann_centers_init_t centers_init;  /* algorithm used for picking the initial 
+				cluster centers for kmeans tree */
+
+  /* autotuned index parameters */
+  float target_precision;    /* precision desired (used for autotuning, -1 otherwise) */
+  float build_weight;        /* build tree time weighting factor */
+  float memory_weight;       /* index memory weigthing factor */
+  float sample_fraction;     /* what fraction of the dataset to use for autotuning */
+
+  /* LSH parameters */
+  unsigned int table_number_; /** The number of hash tables to use */
+  unsigned int key_size_;     /** The length of the key in the hash tables */
+  unsigned int multi_probe_level_; /** Number of levels to use in multi-probe LSH, 0 for standard LSH */
+
+  /* other parameters */
+  enum flann_log_level_t log_level;   /* determines the verbosity of each flann function */
+  long random_seed;          		/* random seed to use */
+};
+\end{Verbatim}
+
+The \texttt{algorithm} and \texttt{centers\_init} fields can take the
+following values:
+\begin{Verbatim}[fontsize=\footnotesize]
+enum flann_algorithm_t {
+	FLANN_INDEX_LINEAR = 0,
+	FLANN_INDEX_KDTREE = 1,
+	FLANN_INDEX_KMEANS = 2,
+	FLANN_INDEX_COMPOSITE = 3,
+	FLANN_INDEX_KDTREE_SINGLE = 3,
+	FLANN_INDEX_SAVED = 254,
+	FLANN_INDEX_AUTOTUNED = 255
+};
+
+enum flann_centers_init_t {
+	FLANN_CENTERS_RANDOM = 0,
+	FLANN_CENTERS_GONZALES = 1,
+	FLANN_CENTERS_KMEANSPP = 2
+};
+\end{Verbatim}
+The \texttt{algorithm} field is used to manually select the type of index
+used. The \texttt{centers\_init} field specifies how to choose the inital
+cluster centers when performing the hierarchical k-means clustering (in
+case the algorithm used is k-means): \texttt{FLANN\_CENTERS\_RANDOM} chooses the
+initial centers randomly, \texttt{FLANN\_CENTERS\_GONZALES} chooses the
+initial centers to be spaced apart from each other by using Gonzales' algorithm
+and \texttt{FLANN\_CENTERS\_KMEANSPP} chooses the initial centers using the algorithm
+proposed in \cite{arthur_kmeanspp_2007}.
+
+The fields: \texttt{checks}, \texttt{cb\_index}, \texttt{trees}, \texttt{branching},  
+\texttt{iterations}, \texttt{target\_precision}, \texttt{build\_weight},
+ \texttt{memory\_weight} and \texttt{sample\_fraction} have the
+same meaning as described in \ref{sec:flann::Index}.
+
+The \texttt{random\_seed} field contains the random seed useed to initialize the random
+number generator. 
+
+The field \texttt{log\_level} controls the verbosity of the messages generated by the FLANN
+library functions. It can take the following values:
+\begin{Verbatim}[fontsize=\footnotesize]
+enum flann_log_level_t {
+    FLANN_LOG_NONE = 0,
+    FLANN_LOG_FATAL = 1,
+    FLANN_LOG_ERROR = 2,
+    FLANN_LOG_WARN = 3,
+    FLANN_LOG_INFO = 4
+};
+\end{Verbatim}
+\end{description}
+
+
+\subsubsection{flann\_find\_nearest\_neighbors\_index()}
+\begin{Verbatim}[fontsize=\footnotesize,frame=single]
+int flann_find_nearest_neighbors_index(FLANN_INDEX index_id,
+	float* testset,
+	int trows,
+	int* indices,
+	float* dists,
+	int nn,
+	int checks,
+	struct FLANNParameters* flann_params);
+\end{Verbatim}
+This function searches for the nearest neighbors of the
+\texttt{testset} points using an index already build and referenced by
+\texttt{index\_id}. The \texttt{testset} is a matrix stored in row-major format
+with \texttt{trows} rows and the same number of columns as the dimensionality
+of the points used to build the index. The function computes \texttt{nn}
+nearest neighbors for each point in the \texttt{testset} and stores them in the
+\texttt{indices} matrix (which is a $\rm{trows} \times \rm{nn}$ matrix stored in
+row-major format). The memory for the \texttt{result} matrix must be allocated
+before the \texttt{flann\_find\_nearest\_neighbors\_index()} function is
+called. The distances to the nearest neighbors found are stored in the \texttt{dists}
+matrix. The \texttt{checks} parameter specifies how many tree traversals should
+be performed during the search.
+
+
+
+\subsubsection{flann\_find\_nearest\_neighbors()}
+\begin{Verbatim}[fontsize=\footnotesize,frame=single]
+int flann_find_nearest_neighbors(float* dataset,
+	int rows,
+	int cols,
+	float* testset,
+	int trows,
+	int* indices,
+	float* dists,
+	int nn,
+	struct FLANNParameters* flann_params);
+\end{Verbatim}
+This function is similar to the
+\texttt{flann\_find\_nearest\_neighbors\_index()} function, but instread of
+using a previously constructed index, it constructs the index, does the nearest
+neighbor search and deletes the index in one step.
+
+\subsubsection{flann\_radius\_search()}
+
+\begin{Verbatim}[fontsize=\footnotesize,frame=single]
+int flann_radius_search(FLANN_INDEX index_ptr,
+	float* query,   /* query point */
+	int* indices, /* array for storing the indices */
+	float* dists, /* similar, but for storing distances */
+	int max_nn,  /* size of arrays indices and dists */
+	float radius, /* search radius (squared radius for euclidian metric) */
+	int checks,  /* number of features to check, sets the level
+			 of approximation */
+	FLANNParameters* flann_params);
+\end{Verbatim}
+
+This function performs a radius search to single query point. The indices of the neighbors found and
+the distances to them are stored in the \texttt{indices} and dists \texttt{arrays}. The \texttt{max\_nn} parameter sets the limit of the
+neighbors that will be returned (the size of the \texttt{indices} and \texttt{dists} arrays must be at least \texttt{max\_nn}).
+
+\subsubsection{flann\_save\_index()}
+\begin{Verbatim}[fontsize=\footnotesize,frame=single]
+int flann_save_index(flann_index_t index_id,
+	char* filename);
+\end{Verbatim}
+
+This function saves an index to a file. The dataset for which the index was built is not saved with the index.
+
+\subsubsection{flann\_load\_index()}
+\begin{Verbatim}[fontsize=\footnotesize,frame=single]
+flann_index_t flann_load_index(char* filename,
+	float* dataset,
+	int rows,
+	int cols);
+\end{Verbatim}
+
+This function loads a previously saved index from a file. Since the dataset is not saved with the
+index, it must be provided to this function.
+
+
+
+\subsubsection{flann\_free\_index()}
+\begin{Verbatim}[fontsize=\footnotesize,frame=single]
+int flann_free_index(FLANN_INDEX index_id,
+	struct FLANNParameters* flann_params);
+\end{Verbatim}
+This function deletes a previously constructed index and frees all the memory
+used by it.
+
+\subsubsection{flann\_set\_distance\_type}
+\label{flann::setDistanceType}
+This function chooses the distance function to use when computing distances between
+data points.
+\begin{Verbatim}[fontsize=\footnotesize,frame=single]
+void flann_set_distance_type(enum flann_distance_t distance_type, int order);
+\end{Verbatim}
+\begin{description}
+\item[distance\_type] The distance type to use. Possible values are
+\begin{Verbatim}[fontsize=\footnotesize]
+enum flann_distance_t {
+	FLANN_DIST_EUCLIDEAN = 1,  // squared euclidean distance
+	FLANN_DIST_MANHATTAN = 2,
+	FLANN_DIST_MINKOWSKI = 3,
+	FLANN_DIST_HIST_INTERSECT  = 5,
+	FlANN_DIST_HELLINGER = 6,
+	FLANN_DIST_CHI_SQUARE  = 7,   // chi-square
+	FLANN_DIST_KULLBACK_LEIBLER  = 8,   // kullback-leibler divergence
+};
+\end{Verbatim}
+\item[order] Used in for the \texttt{FLANN\_DIST\_MINKOWSKI} distance type, to choose the order of the Minkowski distance.
+\end{description}
+
+
+
+
+\subsubsection{flann\_compute\_cluster\_centers()}
+Performs hierarchical clustering of a set of points (see \ref{flann::hierarchicalClustering}).
+\begin{Verbatim}[fontsize=\footnotesize,frame=single]
+int flann_compute_cluster_centers(float* dataset,
+      int rows,
+      int cols,
+      int clusters,
+      float* result,
+      struct FLANNParameters* flann_params);
+\end{Verbatim}
+
+
+\bigskip
+
+See section \ref{sec:quickstart} for an example of how to use the C/C++
+bindings.
+
+
+\subsection{Using FLANN from MATLAB}
+
+The FLANN library can be used from MATLAB through the following wrapper
+functions: \texttt{flann\_build\_index}, \texttt{flann\_search},
+\texttt{flann\_save\_index}, \texttt{flann\_load\_index},
+\texttt{flann\_set\_distance\_type} and \texttt{flann\_free\_index}. 
+The \texttt{flann\_build\_index} function
+creates a search index from the dataset points, \texttt{flann\_search} uses
+this index to perform nearest-neighbor searches, \texttt{flann\_save\_index} 
+and \texttt{flann\_load\_index} can be used to save and load an index
+to/from disk, \texttt{flann\_set\_distance\_type} is used to set the
+distance type to be used when building an index and
+\texttt{flann\_free\_index} deletes the index and releases the memory it
+uses.
+
+% Note that in the binary distribution of FLANN the MEX file is linked against
+% the shared version of FLANN (\texttt{flann.so} or \texttt{flann.dll}), so on Linux you must set the 
+% LD\_LIBRARY\_PATH environment variable accordingly prior to starting MATLAB. On Windows is enough
+% to have \texttt{flann.dll} in the same directory with the MEX file.
+
+The following sections describe in more detail the FLANN matlab wrapper
+functions and show examples of how they may be used.
+
+\subsubsection{flann\_build\_index}
+\label{sec:flann_build_index}
+
+This function creates a search index from the initial dataset of points,
+index used  later for fast nearest-neighbor searches in the dataset.
+
+\begin{Verbatim}[fontsize=\footnotesize,frame=single]
+[index, parameters, speedup] = flann_build_index(dataset, build_params);
+\end{Verbatim}
+
+
+The arguments passed to the \texttt{flann\_build\_index} function have the
+following meaning:
+\begin{description}
+
+\item [\texttt{dataset}] is a $d \times n$ matrix containing $n$
+$d$-dimensional points, stored in a column-major order (one feature on
+each column)
+
+\item [\texttt{build\_params}] - is a MATLAB structure containing the
+parameters passed to the function.
+
+\end{description}
+
+
+The \texttt{build\_params} is used to specify the type of index to be built and the 
+index parameters.  These have a big impact on the performance of the new search
+index (nearest-neighbor search time) and on the time and memory required to
+build the index. The optimum parameter values depend on the dataset
+characteristics (number of dimensions, distribution of points in the
+dataset) and on the application domain (desired precision for the
+approximate nearest neighbor searches). The \texttt{build\_params} argument is 
+a structure that contains one or more of the following fields:
+
+\begin{description}
+\item[\texttt{algorithm}] - the algorithm to use for building the index.
+The possible values are: \texttt{'linear'}, \texttt{'kdtree'},
+\texttt{'kmeans'}, \texttt{'composite'} or \texttt{'autotuned'}. The \texttt{'linear'} option
+does not create any index, it uses brute-force search in the original
+dataset points, \texttt{'kdtree'} creates one or more randomized kd-trees,
+\texttt{'kmeans'} creates a hierarchical kmeans clustering tree,
+\texttt{'composite'} is a mix of both kdtree and kmeans trees and the
+\texttt{'autotuned'} automatically determines the best index and optimum
+parameters using a cross-validation technique.
+
+\vspace{0.5cm}
+\hspace{-1cm} \textbf{Autotuned index:} in case the algorithm field is \texttt{'autotuned'}, the following fields
+should also be present:
+
+\item[\texttt{target\_precision}] - is a number between 0 and 1 specifying the
+percentage of the approximate nearest-neighbor searches that return the
+exact nearest-neighbor. Using a higher value for this parameter gives
+more
+accurate results, but the search takes longer. The optimum value
+usually
+depends on the application.
+
+\item[\texttt{build\_weight}] - specifies the importance of the
+index build time raported to the nearest-neighbor search time. In some
+applications it's acceptable for the index build step to take a long time
+if the subsequent searches in the index can be performed very fast. In
+other applications it's required that the index be build as fast as
+possible even if that leads to slightly longer search times. (Default
+value: 0.01)
+
+\item[\texttt{memory\_weight}] - is used to specify the tradeoff between
+time (index build time and search time) and memory used by the index. A
+value less than 1 gives more importance to the time spent and a value
+greater than 1 gives more importance to the memory usage.
+
+\item[\texttt{sample\_fraction}] - is a number between 0 and 1 indicating what fraction
+of the dataset to use in the automatic parameter configuration algorithm. Running the 
+algorithm on the full dataset gives the most accurate results, but for
+very large datasets can take longer than desired. In such case, using just a fraction of the
+data helps speeding up this algorithm, while still giving good approximations of the
+optimum parameters.
+
+\vspace{0.5cm}
+\hspace{-1cm} \textbf{Randomized kd-trees index:} in case the algorithm field is \texttt{'kdtree'}, the following fields
+should also be present:
+
+\item[\texttt{trees}] - the number of randomized kd-trees to create.
+
+\vspace{0.5cm}
+\hspace{-1cm} \textbf{Hierarchical k-means index:} in case the algorithm type is \texttt{'means'}, the following fields
+should also be present:
+
+
+\item[\texttt{branching}] - the branching factor to use for
+the hierarchical kmeans tree creation. While kdtree is always a binary
+tree, each node in the kmeans tree may have several branches depending on
+the value of this parameter. 
+
+\item[\texttt{iterations}] - the maximum number of iterations to
+use in the kmeans clustering stage when building the kmeans tree. A value
+of -1 used here means that the kmeans clustering should be performed until
+convergence.
+
+\item[\texttt{centers\_init}] - the algorithm to use for selecting the initial
+centers when performing a kmeans clustering step. The possible values are
+'random' (picks the initial cluster centers randomly), 'gonzales' (picks the
+initial centers using the Gonzales algorithm) and 'kmeanspp' (picks the initial
+centers using the algorithm suggested in \cite{arthur_kmeanspp_2007}). If this
+parameters is omitted, the default value is 'random'.
+
+\item[\texttt{cb\_index}] - this parameter (cluster boundary index) influences the
+way exploration is performed in the hierarchical kmeans tree. When \texttt{cb\_index} is zero
+the next kmeans domain to be explored is choosen to be the one with the closest center. 
+A value greater then zero also takes into account the size of the domain.
+
+\vspace{0.5cm}
+\hspace{-1cm} \textbf{Composite index:} in case the algorithm type is \texttt{'composite'}, the fields from 
+both randomized kd-tree index and hierarchical k-means index should be present.
+
+\end{description}
+
+
+The \texttt{flann\_build\_index} function returns the newly created
+\texttt{index}, the \texttt{parameters} used for creating the index and, if
+automatic configuration was used, an estimation of the \texttt{speedup}
+over linear search that is achieved when searching the index.  Since the 
+parameter estimation step is costly, it is
+possible to save the computed parameters and reuse them the
+next time an index is created from similar data points (coming from the
+same distribution).
+
+
+
+
+\subsubsection{flann\_search}
+
+This function performs nearest-neighbor searches using the index already
+created:
+\begin{Verbatim}[fontsize=\footnotesize,frame=single]
+[result, dists] = flann_search(index, testset, k, parameters);
+\end{Verbatim}
+
+The arguments required by this function are:
+\begin{description}
+\item[\texttt{index}] - the index returned by the
+\texttt{flann\_build\_index} function
+\item[\texttt{testset}] - a $d \times m$ matrix containing $m$ test points
+whose k-nearest-neighbors need to be found
+\item[\texttt{k}] - the number of nearest neighbors to be returned for each
+point from \texttt{testset}
+\item[\texttt{parameters}] - structure containing the search parameters.
+Currently it has only one member, \texttt{parameters.checks}, denoting the
+number of times the tree(s) in the index should be recursively traversed. A
+higher value for this parameter would give better search precision, but
+also take more time. If automatic configuration was used when the
+index was created, the number of checks required to achieve the specified
+precision is also computed. In such case, the parameters structure returned
+by the \texttt{flann\_build\_index} function can be passed directly to the
+\texttt{flann\_search} function.
+\end{description}
+
+The function returns two matrices, each of size $k \times m$. The first one contains, in which each column, the indexes (in the dataset matrix) of the $k$ nearest neighbors of the corresponding point from testset, while the second one contains the corresponding distances. The second matrix can be omitted when making the call if the distances to the nearest neighbors are not needed. 
+
+For the case where a single search will be performed with each index, the
+\texttt{flann\_search} function accepts the dataset instead of the index as
+first argument, in which case the index is created searched and then
+deleted in one step. In this case the parameters structure passed to the
+\texttt{flann\_search} function must also contain the fields of the
+\texttt{build\_params} structure that would normally be passed to the
+\texttt{flann\_build\_index} function if the index was build separately.
+\begin{Verbatim}
+[result, dists] = flann_search(dataset, testset, k, parameters);
+\end{Verbatim}
+
+\subsubsection{flann\_save\_index}
+
+This function saves an index to a file so that it can be reused at a later time without
+the need to recompute it. Only the index will be saved to the file, not also the data
+points for which the index was created, so for the index to be reused the data 
+points must be saved separately.
+\begin{Verbatim}[fontsize=\footnotesize,frame=single]
+flann_save_index(index, filename)
+\end{Verbatim}
+
+The argumenst required by this function are:
+\begin{description}
+\item[\texttt{index}] - the index to be saved, created by \texttt{flann\_build\_index}
+\item[\texttt{filename}] - the name of the file in which to save the index
+\end{description}
+
+
+
+\subsubsection{flann\_load\_index}
+
+This function loads a previously saved index from a file. It needs to be passed as a second 
+parameter the dataset for which the index was created, as this is not saved together with the
+index.
+\begin{Verbatim}[fontsize=\footnotesize,frame=single]
+index = flann_load_index(filename, dataset)
+\end{Verbatim}
+The argumenst required by this function are:
+\begin{description}
+\item[\texttt{filename}] - the file from which to load the index
+\item[\texttt{dataset}] - the dataset for which the index was created
+\end{description}
+
+This function returns the index object.
+
+
+\subsubsection{flann\_set\_distance\_type}
+\label{matlab:flannSetDistanceType}
+
+This function chooses the distance function to use when computing distances between
+data points.
+\begin{Verbatim}[fontsize=\footnotesize,frame=single]
+flann_set_distance_type(type, order)
+\end{Verbatim}
+The argumenst required by this function are:
+\begin{description}
+\item[\texttt{type}] - the distance type to use. Possible values are: \texttt{'euclidean'},
+		      \texttt{'manhattan'}, \texttt{'minkowski'}, \texttt{'max\_dist'} ($L\_{infinity}$ - distance
+type is not valid for kd-tree index type since it's not dimensionwise additive), 
+\texttt{'hik'} (histogram intersection kernel), \texttt{'hellinger'},\texttt{'cs'} (chi-square) and \texttt{'kl'} (Kullback-Leibler).
+
+\item[\texttt{order}] - only used if distance type is \texttt{'minkowski'} and represents the order
+	      of the minkowski distance.
+\end{description}
+
+
+
+\subsubsection{flann\_free\_index}
+
+This function must be called to delete an index and release all the memory
+used by it:
+\begin{Verbatim}[fontsize=\footnotesize,frame=single]
+flann_free_index(index);
+\end{Verbatim}
+
+\subsubsection{Examples}
+
+Let's look at a few examples showing how the functions described above are
+used:
+
+\subsubsection{Example 1:}
+
+In this example the index is constructed using automatic parameter estimation, requesting 70\% as desired precision and using the default values for the build time and memory usage factors. The index is then used to search for the nearest-neighbors of the points in the testset matrix and finally the index is deleted.
+
+\begin{Verbatim}[fontsize=\footnotesize,frame=single]
+
+dataset = single(rand(128,10000));
+testset = single(rand(128,1000));
+
+build_params.algorithm = 'autotuned';
+build_params.target_precision = 0.7;
+build_params.build_weight = 0.01;
+build_params.memory_weight = 0;
+
+[index, parameters] = flann_build_index(dataset, build_params);
+
+result = flann_search(index,testset,5,parameters);
+
+flann_free_index(index);
+
+\end{Verbatim}
+
+
+
+
+% \bibliographystyle{alpha}
+% \bibliography{references}
+
+
+\subsubsection{Example 2:}
+
+In this example the index constructed with the parameters specified manually.
+
+\begin{Verbatim}[fontsize=\footnotesize,frame=single]
+
+dataset = single(rand(128,10000));
+testset = single(rand(128,1000));
+
+index = flann_build_index(dataset,struct('algorithm','kdtree','trees',8));
+
+result = flann_search(index,testset,5,struct('checks',128));
+
+flann_free_index(index);
+
+\end{Verbatim}
+
+\subsubsection{Example 3:}
+
+In this example the index creation, searching and deletion are all performed in one step:
+
+\begin{Verbatim}[fontsize=\footnotesize,frame=single]
+
+dataset = single(rand(128,10000));
+testset = single(rand(128,1000));
+
+[result,dists] = flann_search(dataset,testset,5,struct('checks',128,'algorithm',...
+    'kmeans','branching',64,'iterations',5));
+
+\end{Verbatim}
+
+
+
+\subsection{Using FLANN from python}
+
+The python bindings are automatically installed on the system with the FLANN library
+if the option \texttt{BUILD\_PYTHON\_BINDINGS} is enabled. You may need to set the 
+\texttt{PYTHONPATH} to the location of the bindings if you installed FLANN in a
+non-standard location. The python bindings also require the numpy package to be installed.
+
+To use the python FLANN bindings the package \texttt{pyflann} must be imported
+(see the python example in section \ref{sec:quickstart}). This package contains
+a class called FLANN that handles the nearest-neighbor search operations. This
+class containg the following methods:
+\begin{description}
+\item [\texttt{def build\_index(self, dataset, **kwargs)}] :\\
+    This method builds and internally stores an index to be used for future
+nearest neighbor matchings. It erases any previously stored index, so in order
+to work with multiple indexes, multiple instances of the FLANN class must be
+used. The \texttt{dataset} argument must be a 2D numpy array or a matrix, stored in a
+row-major order (a feature on each row of the matrix). The
+rest of the arguments that can be passed to the method are the same as
+ those used in the \texttt{build\_params} structure from
+section \ref{sec:flann_build_index}. Similar to the MATLAB version, the index
+can be created using manually specified parameters or the parameters can be
+automatically computed (by specifying the target\_precision, build\_weight and
+memory\_weight arguments).
+
+The method returns a dictionary containing the parameters used to construct the
+index. In case automatic parameter selection is used, the dictionary will also
+contain the number of checks required to achieve the desired target precision
+and an estimation of the speedup over linear search that the library will
+provide.
+
+
+\item [\texttt{def nn\_index(self, testset, num\_neighbors = 1, **kwargs)}] :\\
+    This method searches for the \texttt{num\_neighbors} nearest neighbors of
+each point in \texttt{testset} using the index computed by
+\texttt{build\_index}. Additionally, a parameter called checks, denoting the
+number of times the index tree(s) should be recursivelly searched, must be
+given.
+
+Example:
+\begin{Verbatim}[fontsize=\scriptsize,frame=single]
+from pyflann import *
+from numpy import *
+from numpy.random import *
+
+dataset = rand(10000, 128)
+testset = rand(1000, 128)
+
+flann = FLANN()
+params = flann.build_index(dataset, algorithm="autotuned", target_precision=0.9, log_level = "info");
+print params
+
+result, dists = flann.nn_index(testset,5, checks=params["checks"]);
+\end{Verbatim}
+
+\item[\texttt{def nn(self, dataset, testset, num\_neighbors = 1, **kwargs)}]:\\
+    This method builds the index, performs the nearest neighbor search and
+deleted the index, all in one step.
+
+\item [\texttt{def save\_index(self, filename)}] :\\
+    This method saves the index to a file. The dataset form which the index was
+build is not saved.
+
+\item [\texttt{def load\_index(self, filename, pts)}] :\\
+    Load the index from a file. The dataset for which the index was build must also 
+be provided since is not saved with the index.
+
+\item [\texttt{def set\_distance\_type(distance\_type, order = 0)}] :\\
+    This function (part of the pyflann module) sets the distance type to be used. See
+\ref{matlab:flannSetDistanceType} for possible values of the distance\_type.
+
+\end{description}
+
+
+
+See section \ref{sec:quickstart} for an example of how to use the Python
+bindings.
+
+
+\bibliographystyle{alpha}
+\bibliography{references}
+
+\end{document}
diff --git a/doc/references.bib b/doc/references.bib
new file mode 100644
index 0000000..01098de
--- /dev/null
+++ b/doc/references.bib
@@ -0,0 +1,210 @@
+ at inproceedings{arthur_kmeanspp_2007,
+    title = {k-means++: the advantages of careful seeding},
+    booktitle = {Proceedings of the eighteenth annual ACM-SIAM symposium on Discrete algorithms},
+    publisher = {Society for Industrial and Applied Mathematics Philadelphia, PA, USA},
+    author = {D. Arthur and S. Vassilvitskii},
+    year = {2007},
+    pages = {1027--1035}
+},
+
+
+
+ at inproceedings{winder_learning_2007,
+	title = {Learning Local Image Descriptors},
+	doi = {10.1109/CVPR.2007.382971},
+	abstract = {In this paper we study interest point descriptors for image matching and 3D reconstruction. We examine the building blocks of descriptor algorithms and evaluate numerous combinations of components. Various published descriptors such as SIFT, GLOH, and Spin images can be cast into our framework. For each candidate algorithm we learn good choices for parameters using a training set consisting of patches from a multi-image 3D reconstruction where accurate ground-truth matches a [...]
+	booktitle = {CVPR},
+	author = {S.A.J. Winder and M. Brown},
+	year = {2007},
+	keywords = {3D image reconstruction,feature vectors,GLOH images,image reconstruction,local image descriptors,log polar histogramming,SIFT images,Spin images,steerable quadrature filters,vectors},
+	pages = {1-8}
+},
+
+ at article{arya_optimal_1998,
+	title = {An optimal algorithm for approximate nearest neighbor searching in fixed dimensions},
+	volume = {45},
+	url = {citeseer.ist.psu.edu/article/arya94optimal.html},
+	journal = {Journal of the ACM},
+	author = {Sunil Arya and David M. Mount and Nathan S. Netanyahu and Ruth Silverman and Angela Y. Wu},
+	year = {1998},
+	pages = {891-923}
+},
+
+ at article{lowe_sift_2004,
+	title = {Distinctive image features from scale-invariant keypoints},
+	volume = {60},
+	journal = {Int. Journal of Computer Vision},
+	author = {David G. Lowe},
+	year = {2004},
+	pages = {91-110}
+},
+
+ at inproceedings{liu_efficient_2003,
+	title = {Efficient Exact k-NN and Nonparametric Classification in High Dimensions},
+	booktitle = {Neural Information Processing Systems},
+	author = {T. Liu and A. W. Moore and A. Gray},
+	year = {2003}
+},
+
+ at inproceedings{nister_scalable_2006,
+	title = {Scalable Recognition with a Vocabulary Tree},
+	isbn = {0-7695-2597-0},
+	doi = {http://dx.doi.org/10.1109/CVPR.2006.264},
+	booktitle = {CVPR},
+	author = {David Nister and Henrik Stewenius},
+	year = {2006},
+	pages = {2161-2168}
+},
+
+ at inproceedings{beis_shape_1997,
+	title = {Shape indexing using approximate nearest-neighbor search in high dimensional spaces},
+	url = {citeseer.ist.psu.edu/beis97shape.html},
+	booktitle = {CVPR},
+	author = {Jeffrey S. Beis and David G. Lowe},
+	year = {1997},
+	pages = {1000-1006}
+},
+
+ at inproceedings{leibe_efficient_2006,
+	title = {Efficient Clustering and Matching for Object Class Recognition},
+	url = {http://www.mis.informatik.tu-darmstadt.de/Publications/leibe-efficientclustering-bmvc06.pdf},
+	abstract = {In this paper we address the problem of building object class representations based on local features and fast matching in a large database. We propose an efficient algorithm for hierarchical agglomerative clustering. We examine different agglomerative and partitional clustering strategies and compare the quality of obtained clusters. Our combination of partitional-agglomerative clustering gives significant improvement in terms of efficiency while maintaining the same qualit [...]
+    booktitle = {BMVC},
+	author = {B. Leibe and K. Mikolajczyk and B. Schiele},
+	year = {2006}
+},
+
+ at inproceedings{schindler_city-scale_2007,
+	title = {City-Scale Location Recognition},
+	doi = {10.1109/CVPR.2007.383150},
+	abstract = {We look at the problem of location recognition in a large image dataset using a vocabulary tree. This entails finding the location of a query image in a large dataset containing 3times104 streetside images of a city. We investigate how the traditional invariant feature matching approach falls down as the size of the database grows. In particular we show that by carefully selecting the vocabulary using the most informative features, retrieval performance is significantly impr [...]
+    booktitle = {CVPR},
+	journal = {Computer Vision and Pattern Recognition, 2007. CVPR '07. IEEE Conference on},
+	author = {G. Schindler and M. Brown and R. Szeliski},
+	year = {2007},
+    keywords = {feature matching,image matching,image retrieval,query image,trees (mathematics)city-scale location recognition,vocabulary tree},
+	pages = {1-7}
+},
+
+ at article{freidman_algorithm_1977,
+	title = {An Algorithm for Finding Best Matches in Logarithmic Expected Time},
+	volume = {3},
+	issn = {0098-3500},
+	doi = {http://doi.acm.org/10.1145/355744.355745},
+	journal = {ACM Trans. Math. Softw.},
+	author = {Jerome H. Freidman and Jon Louis Bentley and Raphael Ari Finkel},
+	year = {1977},
+	pages = {209--226}
+},
+
+ at inproceedings{brin_near_1995,
+	title = {Near Neighbor Search in Large Metric Spaces},
+	isbn = {1-55860-379-4},
+	booktitle = {VLDB},
+	author = {Sergey Brin},
+	year = {1995},
+	pages = {574-584}
+},
+
+ at inproceedings{liu_investigation_2004,
+	title = {An investigation of practical approximate nearest neighbor algorithms},
+	url = {citeseer.ist.psu.edu/753047.html},
+	booktitle = {Neural Information Processing Systems},
+	author = {T. Liu and A. Moore and A. Gray and K. Yang},
+	year = {2004}
+},
+
+ at article{snavely_photo_2006,
+	title = {Photo tourism: Exploring photo collections in 3{D}},
+	volume = {25},
+	journal = {ACM Transactions on Graphics (TOG)},
+	author = {N. Snavely and S. M. Seitz and R. Szeliski},
+	year = {2006},
+	pages = {835-846}
+},
+
+ at inproceedings{silpa-anan_localization_2004,
+	title = {Localization using an imagemap},
+	booktitle = {Australasian Conference on Robotics and Automation},
+	author = {C. Silpa-Anan and R. Hartley},
+	year = {2004}
+},
+
+ at inproceedings{sivic_videogoogle_2003,
+	title = {Video {G}oogle: A Text Retrieval Approach to Object Matching in Videos},
+	booktitle = {ICCV},
+	author = {J. Sivic and A. Zisserman},
+	year = {2003}
+},
+
+ at techreport{torralba_tiny_2007,
+        Author = {A. Torralba and R. Fergus and W. T. Freeman},
+        Title = {Tiny Images},
+        Institution = {CSAIL, Massachusetts Institute of Technology},
+        Year = {2007},
+        URL = {http://dspace.mit.edu/handle/1721.1/37291},
+        Number = {MIT-CSAIL-TR-2007-024}
+},
+
+
+ at article{torralba_80_million_2008,
+    author = {Antonio Torralba and Rob Fergus and William T. Freeman},
+    title = {80 Million Tiny Images: A Large Data Set for Nonparametric Object and Scene Recognition},
+    journal ={IEEE Transactions on Pattern Analysis and Machine Intelligence},
+    volume = {30},
+    number = {11},
+    issn = {0162-8828},
+    year = {2008},
+    pages = {1958-1970},
+    doi = {http://doi.ieeecomputersociety.org/10.1109/TPAMI.2008.128},
+    publisher = {IEEE Computer Society},
+    address = {Los Alamitos, CA, USA},
+}
+
+ at inproceedings{philbin_oxford_2007,
+	title = {Object retrieval with large vocabularies and fast spatial matching},
+	booktitle = {CVPR},
+	author = {J. Philbin and O. Chum and M. Isard and J. Sivic and A. Zisserman},
+	year = {2007}
+}
+
+ at article{andoni_near-optimal_2006,
+    title = {Near-Optimal Hashing Algorithms for Approximate Nearest Neighbor in High Dimensions},
+    journal = {Proceedings of the 47th Annual IEEE Symposium on Foundations of Computer Science (FOCS'06)},
+    author = {A. Andoni},
+    year = {2006},
+    pages = {459-468}
+}
+
+ at article{fukunaga_branch_1975,
+    title = {A Branch and Bound Algorithm for Computing k-Nearest Neighbors},
+    volume = {24},
+    url = {http://portal.acm.org/citation.cfm?id=1311063.1311121\&coll=GUIDE\&dl=\&CFID=5674080\&CFTOKEN=11648065},
+    abstract = {Computation of the k-nearest neighbors generally requires a large number of expensive distance computations. The method of branch and bound is implemented in the present algorithm to facilitate rapid calculation of the k-nearest neighbors, by eliminating the necesssity of calculating many distances. Experimental results demonstrate the efficiency of the algorithm. Typically, an average of only 61 distance computations were made to find the nearest neighbor of a test sampl [...]
+    journal = {IEEE Trans. Comput.},
+    author = {K. Fukunaga and P. M. Narendra},
+    year = {1975},
+    keywords = {branch and bound,distance computation,hierarchical decomposition,k-nearest neighbors,tree-search algorithm.},
+    pages = {750-753}
+}
+
+
+ at inproceedings{mikolajczyk_improving_2007,
+    title = {Improving Descriptors for Fast Tree Matching by Optimal Linear Projection},
+    isbn = {1550-5499},
+    doi = {10.1109/ICCV.2007.4408871},
+    abstract = {In this paper we propose to transform an image descriptor so that nearest neighbor (NN) search for correspondences becomes the optimal matching strategy under the assumption that inter-image deviations of corresponding descriptors have Gaussian distribution. The Euclidean NN in the transformed domain corresponds to the NN according to a truncated Mahalanobis metric in the original descriptor space. We provide theoretical justification for the proposed approach and show ex [...]
+    booktitle = {Computer Vision, 2007. ICCV 2007. IEEE 11th International Conference on},
+    journal = {Computer Vision, 2007. ICCV 2007. IEEE 11th International Conference on},
+    author = {Krystian Mikolajczyk and Jiri Matas},
+    year = {2007},
+    pages = {1-8}
+}
+
+ at inproceedings{silpa-anan_optimized_2008,
+     author = {Silpa-Anan, C. and Hartley, R.} ,
+     title = "Optimised {KD}-trees for fast image descriptor matching" ,
+     booktitle = {CVPR} ,
+     year = 2008 ,
+     url = "../Papers/PDF/SilpaAnan:CVPR08.pdf" ,
+};
\ No newline at end of file
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
new file mode 100644
index 0000000..e985bb3
--- /dev/null
+++ b/examples/CMakeLists.txt
@@ -0,0 +1,39 @@
+add_custom_target(examples ALL)
+
+
+if (BUILD_C_BINDINGS)
+    add_executable(flann_example_c flann_example.c)
+    target_link_libraries(flann_example_c flann)
+    set_target_properties(flann_example_c PROPERTIES COMPILE_FLAGS -std=c99)
+
+    add_dependencies(examples flann_example_c)
+    install (TARGETS flann_example_c DESTINATION bin )
+endif()
+
+if (HDF5_FOUND)
+    include_directories(${HDF5_INCLUDE_DIR})
+
+    add_executable(flann_example_cpp flann_example.cpp)
+    target_link_libraries(flann_example_cpp ${HDF5_LIBRARIES})
+    if (HDF5_IS_PARALLEL)
+        target_link_libraries(flann_example_cpp ${MPI_LIBRARIES})
+    endif()
+
+    add_dependencies(examples flann_example_cpp)
+    install (TARGETS flann_example_cpp DESTINATION bin)
+
+
+    if (USE_MPI AND HDF5_IS_PARALLEL)
+        add_executable(flann_example_mpi flann_example_mpi.cpp)
+        target_link_libraries(flann_example_mpi flann_cpp ${HDF5_LIBRARIES} ${MPI_LIBRARIES} ${Boost_LIBRARIES})
+
+        add_dependencies(examples flann_example_mpi)
+        install (TARGETS flann_example_mpi DESTINATION bin)
+    endif()
+else()
+    message("hdf5 library not found, not compiling flann_example.cpp")
+endif()
+
+
+
+
diff --git a/examples/README b/examples/README
new file mode 100644
index 0000000..1a9848c
--- /dev/null
+++ b/examples/README
@@ -0,0 +1,6 @@
+
+These examples use some datasets that are not included in the source distribution. You can download the datasets from here:
+
+http://people.cs.ubc.ca/~mariusm/uploads/FLANN/datasets/dataset.hdf5
+http://people.cs.ubc.ca/~mariusm/uploads/FLANN/datasets/dataset.dat
+http://people.cs.ubc.ca/~mariusm/uploads/FLANN/datasets/testset.dat
diff --git a/examples/flann_example.c b/examples/flann_example.c
new file mode 100644
index 0000000..e588359
--- /dev/null
+++ b/examples/flann_example.c
@@ -0,0 +1,114 @@
+
+
+#include <flann/flann.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+
+
+float* read_points(const char* filename, int rows, int cols)
+{
+	float* data;
+	float *p;
+	FILE* fin;
+	int i,j;
+
+    fin = fopen(filename,"r");
+    if (!fin) {
+        printf("Cannot open input file.\n");
+        exit(1);
+    }
+    
+    data = (float*) malloc(rows*cols*sizeof(float));
+    if (!data) {
+        printf("Cannot allocate memory.\n");
+        exit(1);
+    }
+    p = data;
+    
+    for (i=0;i<rows;++i) {
+        for (j=0;j<cols;++j) {
+            fscanf(fin,"%g ",p);
+            p++;
+        }
+    }
+    
+    fclose(fin);
+    
+    return data;
+}
+
+void write_results(const char* filename, int *data, int rows, int cols)
+{
+	FILE* fout;
+	int* p;
+	int i,j;
+
+    fout = fopen(filename,"w");
+    if (!fout) {
+        printf("Cannot open output file.\n");
+        exit(1);
+    }
+    
+    p = data;
+    for (i=0;i<rows;++i) {
+        for (j=0;j<cols;++j) {
+            fprintf(fout,"%d ",*p);
+            p++;
+        }
+        fprintf(fout,"\n");
+    }
+    fclose(fout);
+}
+
+
+
+int main(int argc, char** argv)
+{
+	float* dataset;
+	float* testset;
+	int nn;
+	int* result;
+	float* dists;
+	struct FLANNParameters p;
+	float speedup;
+	flann_index_t index_id;
+
+    int rows = 9000;
+    int cols = 128;
+    int tcount = 1000;
+
+    /*
+     * The files dataset.dat and testset.dat can be downloaded from:
+     * http://people.cs.ubc.ca/~mariusm/uploads/FLANN/datasets/dataset.dat
+     * http://people.cs.ubc.ca/~mariusm/uploads/FLANN/datasets/testset.dat
+     */
+    printf("Reading input data file.\n");
+    dataset = read_points("dataset.dat", rows, cols);
+    printf("Reading test data file.\n");
+    testset = read_points("testset.dat", tcount, cols);
+    
+    nn = 3;
+    result = (int*) malloc(tcount*nn*sizeof(int));
+    dists = (float*) malloc(tcount*nn*sizeof(float));
+    
+    p = DEFAULT_FLANN_PARAMETERS;
+    p.algorithm = FLANN_INDEX_KDTREE;
+    p.trees = 8;
+    p.log_level = FLANN_LOG_INFO;
+	p.checks = 64;
+    
+    printf("Computing index.\n");
+    index_id = flann_build_index(dataset, rows, cols, &speedup, &p);
+    flann_find_nearest_neighbors_index(index_id, testset, tcount, result, dists, nn, &p);
+    
+    write_results("results.dat",result, tcount, nn);
+    
+    flann_free_index(index_id, &p);
+    free(dataset);
+    free(testset);
+    free(result);
+    free(dists);
+    
+    return 0;
+}
diff --git a/examples/flann_example.cpp b/examples/flann_example.cpp
new file mode 100644
index 0000000..91b10cf
--- /dev/null
+++ b/examples/flann_example.cpp
@@ -0,0 +1,36 @@
+
+#include <flann/flann.hpp>
+#include <flann/io/hdf5.h>
+
+#include <stdio.h>
+
+using namespace flann;
+
+int main(int argc, char** argv)
+{
+    int nn = 3;
+
+    Matrix<float> dataset;
+    Matrix<float> query;
+    load_from_file(dataset, "dataset.hdf5","dataset");
+    load_from_file(query, "dataset.hdf5","query");
+
+    Matrix<int> indices(new int[query.rows*nn], query.rows, nn);
+    Matrix<float> dists(new float[query.rows*nn], query.rows, nn);
+
+    // construct an randomized kd-tree index using 4 kd-trees
+    Index<L2<float> > index(dataset, flann::KDTreeIndexParams(4));
+    index.buildIndex();                                                                                               
+
+    // do a knn search, using 128 checks
+    index.knnSearch(query, indices, dists, nn, flann::SearchParams(128));
+
+    flann::save_to_file(indices,"result.hdf5","result");
+
+    delete[] dataset.ptr();
+    delete[] query.ptr();
+    delete[] indices.ptr();
+    delete[] dists.ptr();
+    
+    return 0;
+}
diff --git a/examples/flann_example_mpi.cpp b/examples/flann_example_mpi.cpp
new file mode 100644
index 0000000..9b5839e
--- /dev/null
+++ b/examples/flann_example_mpi.cpp
@@ -0,0 +1,131 @@
+
+#include <flann/mpi/queries.h>
+#include <flann/mpi/index.h>
+
+#include <stdio.h>
+#include <sys/time.h>
+#include <boost/thread/thread.hpp>
+
+#define IF_RANK0 if (world.rank()==0)
+
+timeval start_time_;
+void start_timer(const std::string& message = "")
+{
+	if (!message.empty()) {
+		printf("%s", message.c_str());
+		fflush(stdout);
+	}
+    gettimeofday(&start_time_,NULL);
+}
+
+double stop_timer()
+{
+    timeval end_time;
+    gettimeofday(&end_time,NULL);
+
+	return double(end_time.tv_sec-start_time_.tv_sec)+ double(end_time.tv_usec-start_time_.tv_usec)/1000000;
+}
+
+float compute_precision(const flann::Matrix<int>& match, const flann::Matrix<int>& indices)
+{
+	int count = 0;
+
+	assert(match.rows == indices.rows);
+	size_t nn = std::min(match.cols, indices.cols);
+
+	for(size_t i=0; i<match.rows; ++i) {
+		for (size_t j=0;j<nn;++j) {
+			for (size_t k=0;k<nn;++k) {
+				if (match[i][j]==indices[i][k]) {
+					count ++;
+				}
+			}
+		}
+	}
+
+	return float(count)/(nn*match.rows);
+}
+
+
+
+void search(flann::mpi::Index<flann::L2<float> >* index)
+{
+	boost::mpi::communicator world;
+
+	int nn = 1;
+
+	flann::Matrix<float> query;
+		flann::Matrix<int> match;
+	//	flann::Matrix<float> gt_dists;
+		IF_RANK0 {
+			flann::load_from_file(query, "sift100K.h5","query");
+			flann::load_from_file(match, "sift100K.h5","match");
+			//	flann::load_from_file(gt_dists, "sift100K.h5","dists");
+		}
+
+
+		boost::mpi::broadcast(world, query, 0);
+		boost::mpi::broadcast(world, match, 0);
+
+		flann::Matrix<int> indices(new int[query.rows*nn], query.rows, nn);
+		flann::Matrix<float> dists(new float[query.rows*nn], query.rows, nn);
+
+		IF_RANK0 {
+			indices = flann::Matrix<int>(new int[query.rows*nn], query.rows, nn);
+			dists = flann::Matrix<float>(new float[query.rows*nn], query.rows, nn);
+		}
+
+		// do a knn search, using 128 checks0
+		IF_RANK0 start_timer("Performing search...\n");
+		index->knnSearch(query, indices, dists, nn, flann::SearchParams(128));
+		IF_RANK0 {
+			printf("Search done (%g seconds)\n", stop_timer());
+			printf("Indices size: (%d,%d)\n", (int)indices.rows, (int)indices.cols);
+			printf("Checking results\n");
+			float precision = compute_precision(match, indices);
+			printf("Precision is: %g\n", precision);
+		}
+		delete[] query.ptr();
+		delete[] match.ptr();
+
+		IF_RANK0 {
+			delete[] indices.ptr();
+			delete[] dists.ptr();
+		}
+
+}
+
+
+int main(int argc, char** argv)
+{
+	boost::mpi::environment env(argc, argv);
+	boost::mpi::communicator world;
+
+	//flann::Matrix<float> dataset;
+
+	IF_RANK0 start_timer("Loading data...\n");
+	// construct an randomized kd-tree index using 4 kd-trees
+	flann::mpi::Index<flann::L2<float> > index("sift100K.h5", "dataset", flann::KDTreeIndexParams(4));
+	//flann::load_from_file(dataset, "sift100K.h5","dataset");
+	//flann::Index<flann::L2<float>	> index( dataset, flann::KDTreeIndexParams(4));
+	world.barrier();
+	IF_RANK0 printf("Loading data done (%g seconds)\n", stop_timer());
+	IF_RANK0 printf("Index size: (%d,%d)\n", index.size(), index.veclen());
+
+	start_timer("Building index...\n");
+	index.buildIndex();
+	printf("Building index done (%g seconds)\n", stop_timer());
+    world.barrier();
+
+    printf("Searching...\n");
+
+
+    boost::thread t(boost::bind(search, &index));
+    t.join();
+    boost::thread t2(boost::bind(search, &index));
+
+
+    for(;;){};
+
+	return 0;
+}
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
new file mode 100644
index 0000000..911a573
--- /dev/null
+++ b/src/CMakeLists.txt
@@ -0,0 +1,10 @@
+
+add_subdirectory( cpp )
+
+if (BUILD_MATLAB_BINDINGS)
+    add_subdirectory( matlab )
+endif()
+
+if (BUILD_PYTHON_BINDINGS)
+    add_subdirectory( python )
+endif()
diff --git a/src/cpp/CMakeLists.txt b/src/cpp/CMakeLists.txt
new file mode 100644
index 0000000..78a204a
--- /dev/null
+++ b/src/cpp/CMakeLists.txt
@@ -0,0 +1,139 @@
+#include_directories(${CMAKE_SOURCE_DIR}/include algorithms util nn .)
+
+add_definitions(-D_FLANN_VERSION=${FLANN_VERSION})
+
+configure_file(${CMAKE_CURRENT_SOURCE_DIR}/flann/config.h.in ${CMAKE_CURRENT_SOURCE_DIR}/flann/config.h)
+
+file(GLOB_RECURSE C_SOURCES flann.cpp)
+file(GLOB_RECURSE CPP_SOURCES flann_cpp.cpp)
+file(GLOB_RECURSE CU_SOURCES *.cu)
+
+add_library(flann_cpp_s STATIC ${CPP_SOURCES})
+if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_CLANG)
+    set_target_properties(flann_cpp_s PROPERTIES COMPILE_FLAGS -fPIC)
+endif()
+set_property(TARGET flann_cpp_s PROPERTY COMPILE_DEFINITIONS FLANN_STATIC FLANN_USE_CUDA)
+
+if (BUILD_CUDA_LIB)
+    SET(CUDA_NVCC_FLAGS -DFLANN_USE_CUDA)
+    if(CMAKE_COMPILER_IS_GNUCC)
+		set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS};-Xcompiler;-fPIC;-arch=sm_13" )
+        if (NVCC_COMPILER_BINDIR)
+            set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS};--compiler-bindir=${NVCC_COMPILER_BINDIR}")
+        endif()
+    else()
+	    set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS};-arch=sm_13" )
+    endif()
+    cuda_add_library(flann_cuda_s STATIC ${CU_SOURCES})
+    set_property(TARGET flann_cuda_s PROPERTY COMPILE_DEFINITIONS FLANN_STATIC)
+endif()
+
+if(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND CMAKE_COMPILER_IS_GNUCC)
+    add_library(flann_cpp SHARED "")
+    set_target_properties(flann_cpp PROPERTIES LINKER_LANGUAGE CXX)
+    target_link_libraries(flann_cpp -Wl,-whole-archive flann_cpp_s -Wl,-no-whole-archive)
+
+    if (BUILD_CUDA_LIB)
+	    cuda_add_library(flann_cuda SHARED "")
+        set_target_properties(flann_cuda PROPERTIES LINKER_LANGUAGE CXX)
+        target_link_libraries(flann_cuda -Wl,-whole-archive flann_cuda_s -Wl,-no-whole-archive)
+        set_property(TARGET flann_cpp_s PROPERTY COMPILE_DEFINITIONS FLANN_USE_CUDA)
+    # 	target_link_libraries(flann_cuda cudpp_x86_64)
+    endif()
+else()
+    add_library(flann_cpp SHARED ${CPP_SOURCES})
+    if (BUILD_CUDA_LIB)
+		cuda_add_library(flann_cuda SHARED ${CPP_SOURCES})
+        set_property(TARGET flann_cpp PROPERTY COMPILE_DEFINITIONS FLANN_USE_CUDA)
+    endif()
+endif()
+
+set_target_properties(flann_cpp PROPERTIES
+   VERSION ${FLANN_VERSION}
+   SOVERSION ${FLANN_SOVERSION}
+   DEFINE_SYMBOL FLANN_EXPORTS
+) 
+
+if (BUILD_CUDA_LIB)
+    set_target_properties(flann_cuda PROPERTIES
+       VERSION ${FLANN_VERSION}
+       SOVERSION ${FLANN_SOVERSION}
+       DEFINE_SYMBOL FLANN_EXPORTS
+    ) 
+endif()
+
+
+if (USE_MPI AND HDF5_IS_PARALLEL)
+    add_executable(flann_mpi_server flann/mpi/flann_mpi_server.cpp)
+    target_link_libraries(flann_mpi_server flann_cpp ${HDF5_LIBRARIES} ${MPI_LIBRARIES} ${Boost_LIBRARIES})
+
+    add_executable(flann_mpi_client flann/mpi/flann_mpi_client.cpp)
+    target_link_libraries(flann_mpi_client flann_cpp ${HDF5_LIBRARIES} ${MPI_LIBRARIES} ${Boost_LIBRARIES})
+
+    install (TARGETS flann_mpi_client flann_mpi_server
+        DESTINATION bin)
+endif()
+
+
+if (BUILD_C_BINDINGS)
+    add_library(flann_s STATIC ${C_SOURCES})
+    if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_CLANG)
+        set_target_properties(flann_s PROPERTIES COMPILE_FLAGS -fPIC)
+    endif()
+    set_property(TARGET flann_s PROPERTY COMPILE_DEFINITIONS FLANN_STATIC)
+
+    if(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND CMAKE_COMPILER_IS_GNUCC)
+        add_library(flann SHARED "")
+        set_target_properties(flann PROPERTIES LINKER_LANGUAGE CXX)
+        target_link_libraries(flann -Wl,-whole-archive flann_s -Wl,-no-whole-archive)
+    else()
+        add_library(flann SHARED ${C_SOURCES})
+    endif()
+
+    set_target_properties(flann PROPERTIES
+       VERSION ${FLANN_VERSION}
+       SOVERSION ${FLANN_SOVERSION}
+       DEFINE_SYMBOL FLANN_EXPORTS
+    ) 
+endif()
+
+if(WIN32)
+if (BUILD_C_BINDINGS)
+    install (
+        TARGETS flann
+        RUNTIME DESTINATION share/flann/matlab
+    )
+endif()
+endif(WIN32)
+
+
+install (
+    TARGETS flann_cpp flann_cpp_s
+    RUNTIME DESTINATION bin
+    LIBRARY DESTINATION ${FLANN_LIB_INSTALL_DIR}
+    ARCHIVE DESTINATION ${FLANN_LIB_INSTALL_DIR}
+)
+ 
+if (BUILD_CUDA_LIB)
+    install (
+        TARGETS flann_cuda flann_cuda_s
+        RUNTIME DESTINATION bin
+        LIBRARY DESTINATION ${FLANN_LIB_INSTALL_DIR}
+        ARCHIVE DESTINATION ${FLANN_LIB_INSTALL_DIR}
+    )
+endif()
+
+if (BUILD_C_BINDINGS)
+    install (
+        TARGETS flann flann_s
+        RUNTIME DESTINATION bin
+        LIBRARY DESTINATION ${FLANN_LIB_INSTALL_DIR}
+        ARCHIVE DESTINATION ${FLANN_LIB_INSTALL_DIR}
+    )
+endif()
+
+install (
+    DIRECTORY flann
+    DESTINATION include
+    FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp"
+)
diff --git a/src/cpp/flann/algorithms/all_indices.h b/src/cpp/flann/algorithms/all_indices.h
new file mode 100644
index 0000000..d904cca
--- /dev/null
+++ b/src/cpp/flann/algorithms/all_indices.h
@@ -0,0 +1,197 @@
+/***********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2008-2009  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+ * Copyright 2008-2009  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+
+
+#ifndef FLANN_ALL_INDICES_H_
+#define FLANN_ALL_INDICES_H_
+
+#include "flann/general.h"
+
+#include "flann/algorithms/nn_index.h"
+#include "flann/algorithms/kdtree_index.h"
+#include "flann/algorithms/kdtree_single_index.h"
+#include "flann/algorithms/kmeans_index.h"
+#include "flann/algorithms/composite_index.h"
+#include "flann/algorithms/linear_index.h"
+#include "flann/algorithms/hierarchical_clustering_index.h"
+#include "flann/algorithms/lsh_index.h"
+#include "flann/algorithms/autotuned_index.h"
+#ifdef FLANN_USE_CUDA
+#include "flann/algorithms/kdtree_cuda_3d_index.h"
+#endif
+
+
+namespace flann
+{
+
+/**
+ * enable_if sfinae helper
+ */
+template<bool, typename T = void> struct enable_if{};
+template<typename T> struct enable_if<true,T> { typedef T type; };
+
+/**
+ * disable_if sfinae helper
+ */
+template<bool, typename T> struct disable_if{ typedef T type; };
+template<typename T> struct disable_if<true,T> { };
+
+/**
+ * Check if two type are the same
+ */
+template <typename T, typename U>
+struct same_type
+{
+    enum {value = false};
+};
+
+template<typename T>
+struct same_type<T,T>
+{
+    enum {value = true};
+};
+
+#define HAS_MEMBER(member) \
+    template<typename T> \
+    struct member { \
+        typedef char No; \
+        typedef long Yes; \
+        template<typename C> static Yes test( typename C::member* ); \
+        template<typename C> static No test( ... ); \
+        enum { value = sizeof (test<T>(0))==sizeof(Yes) }; \
+    };
+
+HAS_MEMBER(needs_kdtree_distance)
+HAS_MEMBER(needs_vector_space_distance)
+HAS_MEMBER(is_kdtree_distance)
+HAS_MEMBER(is_vector_space_distance)
+
+struct DummyDistance
+{
+    typedef float ElementType;
+    typedef float ResultType;
+
+    template <typename Iterator1, typename Iterator2>
+    ResultType operator()(Iterator1 a, Iterator2 b, size_t size, ResultType /*worst_dist*/ = -1) const
+    {
+        return ResultType(0);
+    }
+
+    template <typename U, typename V>
+    inline ResultType accum_dist(const U& a, const V& b, int) const
+    {
+        return ResultType(0);
+    }
+};
+
+/**
+ * Checks if an index and a distance can be used together
+ */
+template<template <typename> class Index, typename Distance, typename ElemType>
+struct valid_combination
+{
+    static const bool value = same_type<ElemType,typename Distance::ElementType>::value &&
+    				(!needs_kdtree_distance<Index<DummyDistance> >::value || is_kdtree_distance<Distance>::value) &&
+    				(!needs_vector_space_distance<Index<DummyDistance> >::value || is_kdtree_distance<Distance>::value || is_vector_space_distance<Distance>::value);
+
+};
+
+
+/*********************************************************
+ * Create index
+ **********************************************************/
+template <template<typename> class Index, typename Distance, typename T>
+inline NNIndex<Distance>* create_index_(flann::Matrix<T> data, const flann::IndexParams& params, const Distance& distance,
+		typename enable_if<valid_combination<Index,Distance,T>::value,void>::type* = 0)
+{
+    return new Index<Distance>(data, params, distance);
+}
+
+template <template<typename> class Index, typename Distance, typename T>
+inline NNIndex<Distance>* create_index_(flann::Matrix<T> data, const flann::IndexParams& params, const Distance& distance,
+		typename disable_if<valid_combination<Index,Distance,T>::value,void>::type* = 0)
+{
+    return NULL;
+}
+
+template<typename Distance>
+inline NNIndex<Distance>*
+  create_index_by_type(const flann_algorithm_t index_type,
+		const Matrix<typename Distance::ElementType>& dataset, const IndexParams& params, const Distance& distance)
+{
+	typedef typename Distance::ElementType ElementType;
+
+	NNIndex<Distance>* nnIndex;
+
+	switch (index_type) {
+
+	case FLANN_INDEX_LINEAR:
+		nnIndex = create_index_<LinearIndex,Distance,ElementType>(dataset, params, distance);
+		break;
+	case FLANN_INDEX_KDTREE_SINGLE:
+		nnIndex = create_index_<KDTreeSingleIndex,Distance,ElementType>(dataset, params, distance);
+		break;
+	case FLANN_INDEX_KDTREE:
+		nnIndex = create_index_<KDTreeIndex,Distance,ElementType>(dataset, params, distance);
+		break;
+		//! #define this symbol before including flann.h to enable GPU search algorithms. But you have
+		//! to link libflann_cuda then!
+#ifdef FLANN_USE_CUDA
+	case FLANN_INDEX_KDTREE_CUDA:
+		nnIndex = create_index_<KDTreeCuda3dIndex,Distance,ElementType>(dataset, params, distance);
+		break;
+#endif
+
+	case FLANN_INDEX_KMEANS:
+		nnIndex = create_index_<KMeansIndex,Distance,ElementType>(dataset, params, distance);
+		break;
+	case FLANN_INDEX_COMPOSITE:
+		nnIndex = create_index_<CompositeIndex,Distance,ElementType>(dataset, params, distance);
+		break;
+	case FLANN_INDEX_AUTOTUNED:
+		nnIndex = create_index_<AutotunedIndex,Distance,ElementType>(dataset, params, distance);
+		break;
+	case FLANN_INDEX_HIERARCHICAL:
+		nnIndex = create_index_<HierarchicalClusteringIndex,Distance,ElementType>(dataset, params, distance);
+		break;
+	case FLANN_INDEX_LSH:
+		nnIndex = create_index_<LshIndex,Distance,ElementType>(dataset, params, distance);
+		break;
+	default:
+		throw FLANNException("Unknown index type");
+	}
+
+    if (nnIndex==NULL) {
+    	throw FLANNException("Unsupported index/distance combination");
+    }
+    return nnIndex;
+}
+
+}
+
+#endif /* FLANN_ALL_INDICES_H_ */
diff --git a/src/cpp/flann/algorithms/autotuned_index.h b/src/cpp/flann/algorithms/autotuned_index.h
new file mode 100644
index 0000000..e96b81e
--- /dev/null
+++ b/src/cpp/flann/algorithms/autotuned_index.h
@@ -0,0 +1,759 @@
+/***********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2008-2009  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+ * Copyright 2008-2009  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+ *
+ * THE BSD LICENSE
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+#ifndef FLANN_AUTOTUNED_INDEX_H_
+#define FLANN_AUTOTUNED_INDEX_H_
+
+#include "flann/general.h"
+#include "flann/algorithms/nn_index.h"
+#include "flann/nn/ground_truth.h"
+#include "flann/nn/index_testing.h"
+#include "flann/util/sampling.h"
+#include "flann/algorithms/kdtree_index.h"
+#include "flann/algorithms/kdtree_single_index.h"
+#include "flann/algorithms/kmeans_index.h"
+#include "flann/algorithms/composite_index.h"
+#include "flann/algorithms/linear_index.h"
+#include "flann/util/logger.h"
+
+
+namespace flann
+{
+
+template<typename Distance>
+inline NNIndex<Distance>*
+  create_index_by_type(const flann_algorithm_t index_type,
+        const Matrix<typename Distance::ElementType>& dataset, const IndexParams& params, const Distance& distance = Distance());
+
+
+struct AutotunedIndexParams : public IndexParams
+{
+    AutotunedIndexParams(float target_precision = 0.8, float build_weight = 0.01, float memory_weight = 0, float sample_fraction = 0.1)
+    {
+        (*this)["algorithm"] = FLANN_INDEX_AUTOTUNED;
+        // precision desired (used for autotuning, -1 otherwise)
+        (*this)["target_precision"] = target_precision;
+        // build tree time weighting factor
+        (*this)["build_weight"] = build_weight;
+        // index memory weighting factor
+        (*this)["memory_weight"] = memory_weight;
+        // what fraction of the dataset to use for autotuning
+        (*this)["sample_fraction"] = sample_fraction;
+    }
+};
+
+
+template <typename Distance>
+class AutotunedIndex : public NNIndex<Distance>
+{
+public:
+    typedef typename Distance::ElementType ElementType;
+    typedef typename Distance::ResultType DistanceType;
+    
+    typedef NNIndex<Distance> BaseClass;
+
+    typedef AutotunedIndex<Distance> IndexType;
+
+    typedef bool needs_kdtree_distance;
+
+    AutotunedIndex(const Matrix<ElementType>& inputData, const IndexParams& params = AutotunedIndexParams(), Distance d = Distance()) :
+        BaseClass(params, d), bestIndex_(NULL), speedup_(0), dataset_(inputData)
+    {
+        target_precision_ = get_param(params, "target_precision",0.8f);
+        build_weight_ =  get_param(params,"build_weight", 0.01f);
+        memory_weight_ = get_param(params, "memory_weight", 0.0f);
+        sample_fraction_ = get_param(params,"sample_fraction", 0.1f);
+    }
+
+    AutotunedIndex(const IndexParams& params = AutotunedIndexParams(), Distance d = Distance()) :
+        BaseClass(params, d), bestIndex_(NULL), speedup_(0)
+    {
+        target_precision_ = get_param(params, "target_precision",0.8f);
+        build_weight_ =  get_param(params,"build_weight", 0.01f);
+        memory_weight_ = get_param(params, "memory_weight", 0.0f);
+        sample_fraction_ = get_param(params,"sample_fraction", 0.1f);
+    }
+
+    AutotunedIndex(const AutotunedIndex& other) : BaseClass(other),
+    		bestParams_(other.bestParams_),
+    		bestSearchParams_(other.bestSearchParams_),
+    		speedup_(other.speedup_),
+    		dataset_(other.dataset_),
+    		target_precision_(other.target_precision_),
+    		build_weight_(other.build_weight_),
+    		memory_weight_(other.memory_weight_),
+    		sample_fraction_(other.sample_fraction_)
+    {
+    		bestIndex_ = other.bestIndex_->clone();
+    }
+
+    AutotunedIndex& operator=(AutotunedIndex other)
+    {
+    	this->swap(other);
+    	return * this;
+    }
+
+    virtual ~AutotunedIndex()
+    {
+    	delete bestIndex_;
+    }
+
+    BaseClass* clone() const
+    {
+    	return new AutotunedIndex(*this);
+    }
+
+    /**
+     *          Method responsible with building the index.
+     */
+    void buildIndex()
+    {
+        bestParams_ = estimateBuildParams();
+        Logger::info("----------------------------------------------------\n");
+        Logger::info("Autotuned parameters:\n");
+        if (Logger::getLevel()>=FLANN_LOG_INFO)
+        	print_params(bestParams_);
+        Logger::info("----------------------------------------------------\n");
+
+        flann_algorithm_t index_type = get_param<flann_algorithm_t>(bestParams_,"algorithm");
+        bestIndex_ = create_index_by_type(index_type, dataset_, bestParams_, distance_);
+        bestIndex_->buildIndex();
+        speedup_ = estimateSearchParams(bestSearchParams_);
+        Logger::info("----------------------------------------------------\n");
+        Logger::info("Search parameters:\n");
+        if (Logger::getLevel()>=FLANN_LOG_INFO)
+        	print_params(bestSearchParams_);
+        Logger::info("----------------------------------------------------\n");
+        bestParams_["search_params"] = bestSearchParams_;
+        bestParams_["speedup"] = speedup_;
+    }
+    
+    void buildIndex(const Matrix<ElementType>& dataset)
+    {
+    	dataset_ = dataset;
+    	this->buildIndex();
+    }
+
+
+    void addPoints(const Matrix<ElementType>& points, float rebuild_threshold = 2)
+    {
+        if (bestIndex_) {
+            bestIndex_->addPoints(points, rebuild_threshold);
+        }
+    }
+    
+    void removePoint(size_t id)
+    {
+        if (bestIndex_) {
+            bestIndex_->removePoint(id);
+        }
+    }
+
+    
+    template<typename Archive>
+    void serialize(Archive& ar)
+    {
+    	ar.setObject(this);
+
+    	ar & *static_cast<NNIndex<Distance>*>(this);
+
+    	ar & target_precision_;
+    	ar & build_weight_;
+    	ar & memory_weight_;
+    	ar & sample_fraction_;
+
+    	flann_algorithm_t index_type;
+    	if (Archive::is_saving::value) {
+    		index_type = get_param<flann_algorithm_t>(bestParams_,"algorithm");
+    	}
+    	ar & index_type;
+    	ar & bestSearchParams_.checks;
+
+    	if (Archive::is_loading::value) {
+    		bestParams_["algorithm"] = index_type;
+
+    		index_params_["algorithm"] = getType();
+            index_params_["target_precision_"] = target_precision_;
+            index_params_["build_weight_"] = build_weight_;
+            index_params_["memory_weight_"] = memory_weight_;
+            index_params_["sample_fraction_"] = sample_fraction_;
+    	}
+    }
+
+    void saveIndex(FILE* stream)
+    {
+    	serialization::SaveArchive sa(stream);
+    	sa & *this;
+
+    	bestIndex_->saveIndex(stream);
+    }
+
+    void loadIndex(FILE* stream)
+    {
+    	serialization::LoadArchive la(stream);
+    	la & *this;
+
+        IndexParams params;
+        flann_algorithm_t index_type = get_param<flann_algorithm_t>(bestParams_,"algorithm");
+        bestIndex_ = create_index_by_type<Distance>((flann_algorithm_t)index_type, dataset_, params, distance_);
+        bestIndex_->loadIndex(stream);
+    }
+
+    int knnSearch(const Matrix<ElementType>& queries,
+            Matrix<size_t>& indices,
+            Matrix<DistanceType>& dists,
+            size_t knn,
+            const SearchParams& params) const
+    {
+        if (params.checks == FLANN_CHECKS_AUTOTUNED) {
+            return bestIndex_->knnSearch(queries, indices, dists, knn, bestSearchParams_);
+        }
+        else {
+            return bestIndex_->knnSearch(queries, indices, dists, knn, params);
+        }
+    }
+
+    int knnSearch(const Matrix<ElementType>& queries,
+            std::vector< std::vector<size_t> >& indices,
+            std::vector<std::vector<DistanceType> >& dists,
+            size_t knn,
+            const SearchParams& params) const
+    {
+        if (params.checks == FLANN_CHECKS_AUTOTUNED) {
+            return bestIndex_->knnSearch(queries, indices, dists, knn, bestSearchParams_);
+        }
+        else {
+            return bestIndex_->knnSearch(queries, indices, dists, knn, params);
+        }
+
+    }
+    
+    int radiusSearch(const Matrix<ElementType>& queries,
+            Matrix<size_t>& indices,
+            Matrix<DistanceType>& dists,
+            DistanceType radius,
+            const SearchParams& params) const
+    {
+        if (params.checks == FLANN_CHECKS_AUTOTUNED) {
+            return bestIndex_->radiusSearch(queries, indices, dists, radius, bestSearchParams_);
+        }
+        else {
+            return bestIndex_->radiusSearch(queries, indices, dists, radius, params);
+        }
+    }
+
+    int radiusSearch(const Matrix<ElementType>& queries,
+            std::vector< std::vector<size_t> >& indices,
+            std::vector<std::vector<DistanceType> >& dists,
+            DistanceType radius,
+            const SearchParams& params) const
+    {
+        if (params.checks == FLANN_CHECKS_AUTOTUNED) {
+            return bestIndex_->radiusSearch(queries, indices, dists, radius, bestSearchParams_);
+        }
+        else {
+            return bestIndex_->radiusSearch(queries, indices, dists, radius, params);
+        }        
+    }
+
+    
+    
+    /**
+     *      Method that searches for nearest-neighbors
+     */
+    void findNeighbors(ResultSet<DistanceType>& result, const ElementType* vec, const SearchParams& searchParams) const
+    {
+        // should not get here
+        assert(false);
+    }
+
+    IndexParams getParameters() const
+    {
+        return bestParams_;
+    }
+
+    FLANN_DEPRECATED SearchParams getSearchParameters() const
+    {
+        return bestSearchParams_;
+    }
+
+    FLANN_DEPRECATED float getSpeedup() const
+    {
+        return speedup_;
+    }
+
+
+    /**
+     *      Number of features in this index.
+     */
+    size_t size() const
+    {
+        return bestIndex_->size();
+    }
+
+    /**
+     *  The length of each vector in this index.
+     */
+    size_t veclen() const
+    {
+        return bestIndex_->veclen();
+    }
+
+    /**
+     * The amount of memory (in bytes) this index uses.
+     */
+    int usedMemory() const
+    {
+        return bestIndex_->usedMemory();
+    }
+
+    /**
+     * Algorithm name
+     */
+    flann_algorithm_t getType() const
+    {
+        return FLANN_INDEX_AUTOTUNED;
+    }
+
+protected:
+    void buildIndexImpl()
+    {
+        /* nothing to do here */
+    }
+
+    void freeIndex()
+    {
+        /* nothing to do here */
+    }
+
+private:
+
+    struct CostData
+    {
+        float searchTimeCost;
+        float buildTimeCost;
+        float memoryCost;
+        float totalCost;
+        IndexParams params;
+    };
+
+    void evaluate_kmeans(CostData& cost)
+    {
+        StartStopTimer t;
+        int checks;
+        const int nn = 1;
+
+        Logger::info("KMeansTree using params: max_iterations=%d, branching=%d\n",
+                     get_param<int>(cost.params,"iterations"),
+                     get_param<int>(cost.params,"branching"));
+        KMeansIndex<Distance> kmeans(sampledDataset_, cost.params, distance_);
+        // measure index build time
+        t.start();
+        kmeans.buildIndex();
+        t.stop();
+        float buildTime = (float)t.value;
+
+        // measure search time
+        float searchTime = test_index_precision(kmeans, sampledDataset_, testDataset_, gt_matches_, target_precision_, checks, distance_, nn);
+
+        float datasetMemory = float(sampledDataset_.rows * sampledDataset_.cols * sizeof(float));
+        cost.memoryCost = (kmeans.usedMemory() + datasetMemory) / datasetMemory;
+        cost.searchTimeCost = searchTime;
+        cost.buildTimeCost = buildTime;
+        Logger::info("KMeansTree buildTime=%g, searchTime=%g, build_weight=%g\n", buildTime, searchTime, build_weight_);
+    }
+
+
+    void evaluate_kdtree(CostData& cost)
+    {
+        StartStopTimer t;
+        int checks;
+        const int nn = 1;
+
+        Logger::info("KDTree using params: trees=%d\n", get_param<int>(cost.params,"trees"));
+        KDTreeIndex<Distance> kdtree(sampledDataset_, cost.params, distance_);
+
+        t.start();
+        kdtree.buildIndex();
+        t.stop();
+        float buildTime = (float)t.value;
+
+        //measure search time
+        float searchTime = test_index_precision(kdtree, sampledDataset_, testDataset_, gt_matches_, target_precision_, checks, distance_, nn);
+
+        float datasetMemory = float(sampledDataset_.rows * sampledDataset_.cols * sizeof(float));
+        cost.memoryCost = (kdtree.usedMemory() + datasetMemory) / datasetMemory;
+        cost.searchTimeCost = searchTime;
+        cost.buildTimeCost = buildTime;
+        Logger::info("KDTree buildTime=%g, searchTime=%g\n", buildTime, searchTime);
+    }
+
+
+    //    struct KMeansSimpleDownhillFunctor {
+    //
+    //        Autotune& autotuner;
+    //        KMeansSimpleDownhillFunctor(Autotune& autotuner_) : autotuner(autotuner_) {};
+    //
+    //        float operator()(int* params) {
+    //
+    //            float maxFloat = numeric_limits<float>::max();
+    //
+    //            if (params[0]<2) return maxFloat;
+    //            if (params[1]<0) return maxFloat;
+    //
+    //            CostData c;
+    //            c.params["algorithm"] = KMEANS;
+    //            c.params["centers-init"] = CENTERS_RANDOM;
+    //            c.params["branching"] = params[0];
+    //            c.params["max-iterations"] = params[1];
+    //
+    //            autotuner.evaluate_kmeans(c);
+    //
+    //            return c.timeCost;
+    //
+    //        }
+    //    };
+    //
+    //    struct KDTreeSimpleDownhillFunctor {
+    //
+    //        Autotune& autotuner;
+    //        KDTreeSimpleDownhillFunctor(Autotune& autotuner_) : autotuner(autotuner_) {};
+    //
+    //        float operator()(int* params) {
+    //            float maxFloat = numeric_limits<float>::max();
+    //
+    //            if (params[0]<1) return maxFloat;
+    //
+    //            CostData c;
+    //            c.params["algorithm"] = KDTREE;
+    //            c.params["trees"] = params[0];
+    //
+    //            autotuner.evaluate_kdtree(c);
+    //
+    //            return c.timeCost;
+    //
+    //        }
+    //    };
+
+
+
+    void optimizeKMeans(std::vector<CostData>& costs)
+    {
+        Logger::info("KMEANS, Step 1: Exploring parameter space\n");
+
+        // explore kmeans parameters space using combinations of the parameters below
+        int maxIterations[] = { 1, 5, 10, 15 };
+        int branchingFactors[] = { 16, 32, 64, 128, 256 };
+
+        int kmeansParamSpaceSize = FLANN_ARRAY_LEN(maxIterations) * FLANN_ARRAY_LEN(branchingFactors);
+        costs.reserve(costs.size() + kmeansParamSpaceSize);
+
+        // evaluate kmeans for all parameter combinations
+        for (size_t i = 0; i < FLANN_ARRAY_LEN(maxIterations); ++i) {
+            for (size_t j = 0; j < FLANN_ARRAY_LEN(branchingFactors); ++j) {
+                CostData cost;
+                cost.params["algorithm"] = FLANN_INDEX_KMEANS;
+                cost.params["centers_init"] = FLANN_CENTERS_RANDOM;
+                cost.params["iterations"] = maxIterations[i];
+                cost.params["branching"] = branchingFactors[j];
+
+                evaluate_kmeans(cost);
+                costs.push_back(cost);
+            }
+        }
+
+        //         Logger::info("KMEANS, Step 2: simplex-downhill optimization\n");
+        //
+        //         const int n = 2;
+        //         // choose initial simplex points as the best parameters so far
+        //         int kmeansNMPoints[n*(n+1)];
+        //         float kmeansVals[n+1];
+        //         for (int i=0;i<n+1;++i) {
+        //             kmeansNMPoints[i*n] = (int)kmeansCosts[i].params["branching"];
+        //             kmeansNMPoints[i*n+1] = (int)kmeansCosts[i].params["max-iterations"];
+        //             kmeansVals[i] = kmeansCosts[i].timeCost;
+        //         }
+        //         KMeansSimpleDownhillFunctor kmeans_cost_func(*this);
+        //         // run optimization
+        //         optimizeSimplexDownhill(kmeansNMPoints,n,kmeans_cost_func,kmeansVals);
+        //         // store results
+        //         for (int i=0;i<n+1;++i) {
+        //             kmeansCosts[i].params["branching"] = kmeansNMPoints[i*2];
+        //             kmeansCosts[i].params["max-iterations"] = kmeansNMPoints[i*2+1];
+        //             kmeansCosts[i].timeCost = kmeansVals[i];
+        //         }
+    }
+
+
+    void optimizeKDTree(std::vector<CostData>& costs)
+    {
+        Logger::info("KD-TREE, Step 1: Exploring parameter space\n");
+
+        // explore kd-tree parameters space using the parameters below
+        int testTrees[] = { 1, 4, 8, 16, 32 };
+
+        // evaluate kdtree for all parameter combinations
+        for (size_t i = 0; i < FLANN_ARRAY_LEN(testTrees); ++i) {
+            CostData cost;
+            cost.params["algorithm"] = FLANN_INDEX_KDTREE;
+            cost.params["trees"] = testTrees[i];
+
+            evaluate_kdtree(cost);
+            costs.push_back(cost);
+        }
+
+        //         Logger::info("KD-TREE, Step 2: simplex-downhill optimization\n");
+        //
+        //         const int n = 1;
+        //         // choose initial simplex points as the best parameters so far
+        //         int kdtreeNMPoints[n*(n+1)];
+        //         float kdtreeVals[n+1];
+        //         for (int i=0;i<n+1;++i) {
+        //             kdtreeNMPoints[i] = (int)kdtreeCosts[i].params["trees"];
+        //             kdtreeVals[i] = kdtreeCosts[i].timeCost;
+        //         }
+        //         KDTreeSimpleDownhillFunctor kdtree_cost_func(*this);
+        //         // run optimization
+        //         optimizeSimplexDownhill(kdtreeNMPoints,n,kdtree_cost_func,kdtreeVals);
+        //         // store results
+        //         for (int i=0;i<n+1;++i) {
+        //             kdtreeCosts[i].params["trees"] = kdtreeNMPoints[i];
+        //             kdtreeCosts[i].timeCost = kdtreeVals[i];
+        //         }
+    }
+
+    /**
+     *  Chooses the best nearest-neighbor algorithm and estimates the optimal
+     *  parameters to use when building the index (for a given precision).
+     *  Returns a dictionary with the optimal parameters.
+     */
+    IndexParams estimateBuildParams()
+    {
+        std::vector<CostData> costs;
+
+        int sampleSize = int(sample_fraction_ * dataset_.rows);
+        int testSampleSize = std::min(sampleSize / 10, 1000);
+
+        Logger::info("Entering autotuning, dataset size: %d, sampleSize: %d, testSampleSize: %d, target precision: %g\n", dataset_.rows, sampleSize, testSampleSize, target_precision_);
+
+        // For a very small dataset, it makes no sense to build any fancy index, just
+        // use linear search
+        if (testSampleSize < 10) {
+            Logger::info("Choosing linear, dataset too small\n");
+            return LinearIndexParams();
+        }
+
+        // We use a fraction of the original dataset to speedup the autotune algorithm
+        sampledDataset_ = random_sample(dataset_, sampleSize);
+        // We use a cross-validation approach, first we sample a testset from the dataset
+        testDataset_ = random_sample(sampledDataset_, testSampleSize, true);
+
+        // We compute the ground truth using linear search
+        Logger::info("Computing ground truth... \n");
+        gt_matches_ = Matrix<size_t>(new size_t[testDataset_.rows], testDataset_.rows, 1);
+        StartStopTimer t;
+        int repeats = 0;
+        t.reset();
+        while (t.value<0.2) {
+        	repeats++;
+            t.start();
+        	compute_ground_truth<Distance>(sampledDataset_, testDataset_, gt_matches_, 0, distance_);
+            t.stop();
+        }
+
+        CostData linear_cost;
+        linear_cost.searchTimeCost = (float)t.value/repeats;
+        linear_cost.buildTimeCost = 0;
+        linear_cost.memoryCost = 0;
+        linear_cost.params["algorithm"] = FLANN_INDEX_LINEAR;
+
+        costs.push_back(linear_cost);
+
+        // Start parameter autotune process
+        Logger::info("Autotuning parameters...\n");
+
+        optimizeKMeans(costs);
+        optimizeKDTree(costs);
+
+        float bestTimeCost = costs[0].buildTimeCost * build_weight_ + costs[0].searchTimeCost;
+        for (size_t i = 0; i < costs.size(); ++i) {
+            float timeCost = costs[i].buildTimeCost * build_weight_ + costs[i].searchTimeCost;
+            Logger::debug("Time cost: %g\n", timeCost);
+            if (timeCost < bestTimeCost) {
+                bestTimeCost = timeCost;
+            }
+        }
+        Logger::debug("Best time cost: %g\n", bestTimeCost);
+
+    	IndexParams bestParams = costs[0].params;
+        if (bestTimeCost > 0) {
+        	float bestCost = (costs[0].buildTimeCost * build_weight_ + costs[0].searchTimeCost) / bestTimeCost;
+        	for (size_t i = 0; i < costs.size(); ++i) {
+        		float crtCost = (costs[i].buildTimeCost * build_weight_ + costs[i].searchTimeCost) / bestTimeCost +
+        				memory_weight_ * costs[i].memoryCost;
+        		Logger::debug("Cost: %g\n", crtCost);
+        		if (crtCost < bestCost) {
+        			bestCost = crtCost;
+        			bestParams = costs[i].params;
+        		}
+        	}
+            Logger::debug("Best cost: %g\n", bestCost);
+        }
+
+        delete[] gt_matches_.ptr();
+        delete[] testDataset_.ptr();
+        delete[] sampledDataset_.ptr();
+
+        return bestParams;
+    }
+
+
+
+    /**
+     *  Estimates the search time parameters needed to get the desired precision.
+     *  Precondition: the index is built
+     *  Postcondition: the searchParams will have the optimum params set, also the speedup obtained over linear search.
+     */
+    float estimateSearchParams(SearchParams& searchParams)
+    {
+        const int nn = 1;
+        const size_t SAMPLE_COUNT = 1000;
+
+        assert(bestIndex_ != NULL); // must have a valid index
+
+        float speedup = 0;
+
+        int samples = (int)std::min(dataset_.rows / 10, SAMPLE_COUNT);
+        if (samples > 0) {
+            Matrix<ElementType> testDataset = random_sample(dataset_, samples);
+
+            Logger::info("Computing ground truth\n");
+
+            // we need to compute the ground truth first
+            Matrix<size_t> gt_matches(new size_t[testDataset.rows], testDataset.rows, 1);
+            StartStopTimer t;
+            int repeats = 0;
+            t.reset();
+            while (t.value<0.2) {
+            	repeats++;
+                t.start();
+            	compute_ground_truth<Distance>(dataset_, testDataset, gt_matches, 1, distance_);
+                t.stop();
+            }
+            float linear = (float)t.value/repeats;
+
+            int checks;
+            Logger::info("Estimating number of checks\n");
+
+            float searchTime;
+            float cb_index;
+            if (bestIndex_->getType() == FLANN_INDEX_KMEANS) {
+                Logger::info("KMeans algorithm, estimating cluster border factor\n");
+                KMeansIndex<Distance>* kmeans = static_cast<KMeansIndex<Distance>*>(bestIndex_);
+                float bestSearchTime = -1;
+                float best_cb_index = -1;
+                int best_checks = -1;
+                for (cb_index = 0; cb_index < 1.1f; cb_index += 0.2f) {
+                    kmeans->set_cb_index(cb_index);
+                    searchTime = test_index_precision(*kmeans, dataset_, testDataset, gt_matches, target_precision_, checks, distance_, nn, 1);
+                    if ((searchTime < bestSearchTime) || (bestSearchTime == -1)) {
+                        bestSearchTime = searchTime;
+                        best_cb_index = cb_index;
+                        best_checks = checks;
+                    }
+                }
+                searchTime = bestSearchTime;
+                cb_index = best_cb_index;
+                checks = best_checks;
+
+                kmeans->set_cb_index(best_cb_index);
+                Logger::info("Optimum cb_index: %g\n", cb_index);
+                bestParams_["cb_index"] = cb_index;
+            }
+            else {
+                searchTime = test_index_precision(*bestIndex_, dataset_, testDataset, gt_matches, target_precision_, checks, distance_, nn, 1);
+            }
+
+            Logger::info("Required number of checks: %d \n", checks);
+            searchParams.checks = checks;
+
+            speedup = linear / searchTime;
+
+            delete[] gt_matches.ptr();
+            delete[] testDataset.ptr();
+        }
+
+        return speedup;
+    }
+
+
+    void swap(AutotunedIndex& other)
+    {
+    	BaseClass::swap(other);
+    	std::swap(bestIndex_, other.bestIndex_);
+    	std::swap(bestParams_, other.bestParams_);
+    	std::swap(bestSearchParams_, other.bestSearchParams_);
+    	std::swap(speedup_, other.speedup_);
+    	std::swap(dataset_, other.dataset_);
+    	std::swap(target_precision_, other.target_precision_);
+    	std::swap(build_weight_, other.build_weight_);
+    	std::swap(memory_weight_, other.memory_weight_);
+    	std::swap(sample_fraction_, other.sample_fraction_);
+    }
+
+private:
+    NNIndex<Distance>* bestIndex_;
+
+    IndexParams bestParams_;
+    SearchParams bestSearchParams_;
+
+    Matrix<ElementType> sampledDataset_;
+    Matrix<ElementType> testDataset_;
+    Matrix<size_t> gt_matches_;
+
+    float speedup_;
+
+    /**
+     * The dataset used by this index
+     */
+    Matrix<ElementType> dataset_;
+
+    /**
+     * Index parameters
+     */
+    float target_precision_;
+    float build_weight_;
+    float memory_weight_;
+    float sample_fraction_;
+
+    USING_BASECLASS_SYMBOLS
+};
+}
+
+#endif /* FLANN_AUTOTUNED_INDEX_H_ */
diff --git a/src/cpp/flann/algorithms/center_chooser.h b/src/cpp/flann/algorithms/center_chooser.h
new file mode 100644
index 0000000..77a8417
--- /dev/null
+++ b/src/cpp/flann/algorithms/center_chooser.h
@@ -0,0 +1,228 @@
+/*
+ * center_chooser.h
+ *
+ *  Created on: 2012-11-04
+ *      Author: marius
+ */
+
+#ifndef CENTER_CHOOSER_H_
+#define CENTER_CHOOSER_H_
+
+#include <flann/util/matrix.h>
+
+namespace flann
+{
+
+template <typename Distance>
+class CenterChooser
+{
+public:
+    typedef typename Distance::ElementType ElementType;
+    typedef typename Distance::ResultType DistanceType;
+
+    CenterChooser(const Distance& distance) : distance_(distance) {};
+
+    void setDataset(const flann::Matrix<ElementType>& dataset)
+    {
+    	dataset_ = dataset;
+    }
+
+    virtual ~CenterChooser() {};
+
+    /**
+     * Chooses cluster centers
+     *
+     * @param k number of centers to choose
+     * @param indices indices of points to choose the centers from
+     * @param indices_length length of indices
+     * @param centers indices of chosen centers
+     * @param centers_length length of centers array
+     */
+	virtual void operator()(int k, int* indices, int indices_length, int* centers, int& centers_length) = 0;
+
+protected:
+	flann::Matrix<ElementType> dataset_;
+	Distance distance_;
+};
+
+
+template <typename Distance>
+class RandomCenterChooser : public CenterChooser<Distance>
+{
+public:
+    typedef typename Distance::ElementType ElementType;
+    typedef typename Distance::ResultType DistanceType;
+    using CenterChooser<Distance>::dataset_;
+    using CenterChooser<Distance>::distance_;
+
+    RandomCenterChooser(const Distance& distance) :
+    	CenterChooser<Distance>(distance) {}
+
+    void operator()(int k, int* indices, int indices_length, int* centers, int& centers_length)
+    {
+        UniqueRandom r(indices_length);
+
+        int index;
+        for (index=0; index<k; ++index) {
+            bool duplicate = true;
+            int rnd;
+            while (duplicate) {
+                duplicate = false;
+                rnd = r.next();
+                if (rnd<0) {
+                    centers_length = index;
+                    return;
+                }
+
+                centers[index] = indices[rnd];
+
+                for (int j=0; j<index; ++j) {
+                    DistanceType sq = distance_(dataset_[centers[index]], dataset_[centers[j]], dataset_.cols);
+                    if (sq<1e-16) {
+                        duplicate = true;
+                    }
+                }
+            }
+        }
+
+        centers_length = index;
+    }
+};
+
+
+
+/**
+ * Chooses the initial centers using the Gonzales algorithm.
+ */
+template <typename Distance>
+class GonzalesCenterChooser : public CenterChooser<Distance>
+{
+public:
+    typedef typename Distance::ElementType ElementType;
+    typedef typename Distance::ResultType DistanceType;
+
+    using CenterChooser<Distance>::dataset_;
+    using CenterChooser<Distance>::distance_;
+
+    GonzalesCenterChooser(const Distance& distance) : CenterChooser<Distance>( distance) {}
+
+    void operator()(int k, int* indices, int indices_length, int* centers, int& centers_length)
+    {
+        int n = indices_length;
+
+        int rnd = rand_int(n);
+        assert(rnd >=0 && rnd < n);
+
+        centers[0] = indices[rnd];
+
+        int index;
+        for (index=1; index<k; ++index) {
+
+            int best_index = -1;
+            DistanceType best_val = 0;
+            for (int j=0; j<n; ++j) {
+            	DistanceType dist = distance_(dataset_[centers[0]],dataset_[indices[j]],dataset_.cols);
+                for (int i=1; i<index; ++i) {
+                    DistanceType tmp_dist = distance_(dataset_[centers[i]],dataset_[indices[j]],dataset_.cols);
+                    if (tmp_dist<dist) {
+                        dist = tmp_dist;
+                    }
+                }
+                if (dist>best_val) {
+                    best_val = dist;
+                    best_index = j;
+                }
+            }
+            if (best_index!=-1) {
+                centers[index] = indices[best_index];
+            }
+            else {
+                break;
+            }
+        }
+        centers_length = index;
+    }
+};
+
+
+/**
+ * Chooses the initial centers using the algorithm proposed in the KMeans++ paper:
+ * Arthur, David; Vassilvitskii, Sergei - k-means++: The Advantages of Careful Seeding
+ */
+template <typename Distance>
+class KMeansppCenterChooser : public CenterChooser<Distance>
+{
+public:
+    typedef typename Distance::ElementType ElementType;
+    typedef typename Distance::ResultType DistanceType;
+
+    using CenterChooser<Distance>::dataset_;
+    using CenterChooser<Distance>::distance_;
+
+    KMeansppCenterChooser(const Distance& distance) : CenterChooser<Distance>(distance) {}
+
+    void operator()(int k, int* indices, int indices_length, int* centers, int& centers_length)
+    {
+        int n = indices_length;
+
+        double currentPot = 0;
+        DistanceType* closestDistSq = new DistanceType[n];
+
+        // Choose one random center and set the closestDistSq values
+        int index = rand_int(n);
+        assert(index >=0 && index < n);
+        centers[0] = indices[index];
+
+        for (int i = 0; i < n; i++) {
+            closestDistSq[i] = distance_(dataset_[indices[i]], dataset_[indices[index]], dataset_.cols);
+            currentPot += closestDistSq[i];
+        }
+
+
+        const int numLocalTries = 1;
+
+        // Choose each center
+        int centerCount;
+        for (centerCount = 1; centerCount < k; centerCount++) {
+
+            // Repeat several trials
+            double bestNewPot = -1;
+            int bestNewIndex = 0;
+            for (int localTrial = 0; localTrial < numLocalTries; localTrial++) {
+
+                // Choose our center - have to be slightly careful to return a valid answer even accounting
+                // for possible rounding errors
+                double randVal = rand_double(currentPot);
+                for (index = 0; index < n-1; index++) {
+                    if (randVal <= closestDistSq[index]) break;
+                    else randVal -= closestDistSq[index];
+                }
+
+                // Compute the new potential
+                double newPot = 0;
+                for (int i = 0; i < n; i++) newPot += std::min( distance_(dataset_[indices[i]], dataset_[indices[index]], dataset_.cols), closestDistSq[i] );
+
+                // Store the best result
+                if ((bestNewPot < 0)||(newPot < bestNewPot)) {
+                    bestNewPot = newPot;
+                    bestNewIndex = index;
+                }
+            }
+
+            // Add the appropriate center
+            centers[centerCount] = indices[bestNewIndex];
+            currentPot = bestNewPot;
+            for (int i = 0; i < n; i++) closestDistSq[i] = std::min( distance_(dataset_[indices[i]], dataset_[indices[bestNewIndex]], dataset_.cols), closestDistSq[i] );
+        }
+
+        centers_length = centerCount;
+
+        delete[] closestDistSq;
+    }
+};
+
+
+}
+
+
+#endif /* CENTER_CHOOSER_H_ */
diff --git a/src/cpp/flann/algorithms/composite_index.h b/src/cpp/flann/algorithms/composite_index.h
new file mode 100644
index 0000000..44ef799
--- /dev/null
+++ b/src/cpp/flann/algorithms/composite_index.h
@@ -0,0 +1,239 @@
+/***********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2008-2009  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+ * Copyright 2008-2009  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+ *
+ * THE BSD LICENSE
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+
+#ifndef FLANN_COMPOSITE_INDEX_H_
+#define FLANN_COMPOSITE_INDEX_H_
+
+#include "flann/general.h"
+#include "flann/algorithms/nn_index.h"
+#include "flann/algorithms/kdtree_index.h"
+#include "flann/algorithms/kmeans_index.h"
+
+namespace flann
+{
+
+/**
+ * Index parameters for the CompositeIndex.
+ */
+struct CompositeIndexParams : public IndexParams
+{
+    CompositeIndexParams(int trees = 4, int branching = 32, int iterations = 11,
+                         flann_centers_init_t centers_init = FLANN_CENTERS_RANDOM, float cb_index = 0.2 )
+    {
+        (*this)["algorithm"] = FLANN_INDEX_KMEANS;
+        // number of randomized trees to use (for kdtree)
+        (*this)["trees"] = trees;
+        // branching factor
+        (*this)["branching"] = branching;
+        // max iterations to perform in one kmeans clustering (kmeans tree)
+        (*this)["iterations"] = iterations;
+        // algorithm used for picking the initial cluster centers for kmeans tree
+        (*this)["centers_init"] = centers_init;
+        // cluster boundary index. Used when searching the kmeans tree
+        (*this)["cb_index"] = cb_index;
+    }
+};
+
+
+/**
+ * This index builds a kd-tree index and a k-means index and performs nearest
+ * neighbour search both indexes. This gives a slight boost in search performance
+ * as some of the neighbours that are missed by one index are found by the other.
+ */
+template <typename Distance>
+class CompositeIndex : public NNIndex<Distance>
+{
+public:
+    typedef typename Distance::ElementType ElementType;
+    typedef typename Distance::ResultType DistanceType;
+
+    typedef NNIndex<Distance> BaseClass;
+
+    typedef bool needs_kdtree_distance;
+
+    /**
+     * Index constructor
+     * @param inputData dataset containing the points to index
+     * @param params Index parameters
+     * @param d Distance functor
+     * @return
+     */
+    CompositeIndex(const IndexParams& params = CompositeIndexParams(), Distance d = Distance()) :
+    	BaseClass(params, d)
+    {
+        kdtree_index_ = new KDTreeIndex<Distance>(params, d);
+        kmeans_index_ = new KMeansIndex<Distance>(params, d);
+
+    }
+
+    CompositeIndex(const Matrix<ElementType>& inputData, const IndexParams& params = CompositeIndexParams(),
+                   Distance d = Distance()) : BaseClass(params, d)
+    {
+        kdtree_index_ = new KDTreeIndex<Distance>(inputData, params, d);
+        kmeans_index_ = new KMeansIndex<Distance>(inputData, params, d);
+    }
+
+    CompositeIndex(const CompositeIndex& other) : BaseClass(other),
+    	kmeans_index_(other.kmeans_index_), kdtree_index_(other.kdtree_index_)
+    {
+    }
+
+    CompositeIndex& operator=(CompositeIndex other)
+    {
+    	this->swap(other);
+    	return *this;
+    }
+
+    virtual ~CompositeIndex()
+    {
+        delete kdtree_index_;
+        delete kmeans_index_;
+    }
+
+    BaseClass* clone() const
+    {
+    	return new CompositeIndex(*this);
+    }
+
+    /**
+     * @return The index type
+     */
+    flann_algorithm_t getType() const
+    {
+        return FLANN_INDEX_COMPOSITE;
+    }
+
+    /**
+     * @return Size of the index
+     */
+    size_t size() const
+    {
+        return kdtree_index_->size();
+    }
+
+    /**
+     * \returns The dimensionality of the features in this index.
+     */
+    size_t veclen() const
+    {
+        return kdtree_index_->veclen();
+    }
+
+    /**
+     * \returns The amount of memory (in bytes) used by the index.
+     */
+    int usedMemory() const
+    {
+        return kmeans_index_->usedMemory() + kdtree_index_->usedMemory();
+    }
+
+    using NNIndex<Distance>::buildIndex;
+    /**
+     * \brief Builds the index
+     */
+    void buildIndex()
+    {
+        Logger::info("Building kmeans tree...\n");
+        kmeans_index_->buildIndex();
+        Logger::info("Building kdtree tree...\n");
+        kdtree_index_->buildIndex();
+    }
+    
+    void addPoints(const Matrix<ElementType>& points, float rebuild_threshold = 2)
+    {
+        kmeans_index_->addPoints(points, rebuild_threshold);
+        kdtree_index_->addPoints(points, rebuild_threshold);
+    }
+
+    void removePoint(size_t index)
+    {
+        kmeans_index_->removePoint(index);
+        kdtree_index_->removePoint(index);
+    }
+
+
+    /**
+     * \brief Saves the index to a stream
+     * \param stream The stream to save the index to
+     */
+    void saveIndex(FILE* stream)
+    {
+        kmeans_index_->saveIndex(stream);
+        kdtree_index_->saveIndex(stream);
+    }
+
+    /**
+     * \brief Loads the index from a stream
+     * \param stream The stream from which the index is loaded
+     */
+    void loadIndex(FILE* stream)
+    {
+        kmeans_index_->loadIndex(stream);
+        kdtree_index_->loadIndex(stream);
+    }
+
+    /**
+     * \brief Method that searches for nearest-neighbours
+     */
+    void findNeighbors(ResultSet<DistanceType>& result, const ElementType* vec, const SearchParams& searchParams) const
+    {
+        kmeans_index_->findNeighbors(result, vec, searchParams);
+        kdtree_index_->findNeighbors(result, vec, searchParams);
+    }
+
+protected:
+    void swap(CompositeIndex& other)
+    {
+    	std::swap(kmeans_index_, other.kmeans_index_);
+    	std::swap(kdtree_index_, other.kdtree_index_);
+    }
+
+    void buildIndexImpl()
+    {
+        /* nothing to do here */
+    }
+
+    void freeIndex()
+    {
+        /* nothing to do here */
+    }
+
+
+private:
+    /** The k-means index */
+    KMeansIndex<Distance>* kmeans_index_;
+
+    /** The kd-tree index */
+    KDTreeIndex<Distance>* kdtree_index_;
+};
+
+}
+
+#endif //FLANN_COMPOSITE_INDEX_H_
diff --git a/src/cpp/flann/algorithms/dist.h b/src/cpp/flann/algorithms/dist.h
new file mode 100644
index 0000000..046695f
--- /dev/null
+++ b/src/cpp/flann/algorithms/dist.h
@@ -0,0 +1,787 @@
+/***********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2008-2009  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+ * Copyright 2008-2009  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+ *
+ * THE BSD LICENSE
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+
+#ifndef FLANN_DIST_H_
+#define FLANN_DIST_H_
+
+#include <cmath>
+#include <cstdlib>
+#include <string.h>
+#ifdef _MSC_VER
+typedef unsigned __int32 uint32_t;
+typedef unsigned __int64 uint64_t;
+#else
+#include <stdint.h>
+#endif
+
+#include "flann/defines.h"
+
+
+namespace flann
+{
+
+template<typename T>
+struct Accumulator { typedef T Type; };
+template<>
+struct Accumulator<unsigned char>  { typedef float Type; };
+template<>
+struct Accumulator<unsigned short> { typedef float Type; };
+template<>
+struct Accumulator<unsigned int> { typedef float Type; };
+template<>
+struct Accumulator<char>   { typedef float Type; };
+template<>
+struct Accumulator<short>  { typedef float Type; };
+template<>
+struct Accumulator<int> { typedef float Type; };
+
+
+
+/**
+ * Squared Euclidean distance functor.
+ *
+ * This is the simpler, unrolled version. This is preferable for
+ * very low dimensionality data (eg 3D points)
+ */
+template<class T>
+struct L2_Simple
+{
+    typedef bool is_kdtree_distance;
+
+    typedef T ElementType;
+    typedef typename Accumulator<T>::Type ResultType;
+
+    template <typename Iterator1, typename Iterator2>
+    ResultType operator()(Iterator1 a, Iterator2 b, size_t size, ResultType /*worst_dist*/ = -1) const
+    {
+        ResultType result = ResultType();
+        ResultType diff;
+        for(size_t i = 0; i < size; ++i ) {
+            diff = *a++ - *b++;
+            result += diff*diff;
+        }
+        return result;
+    }
+
+    template <typename U, typename V>
+    inline ResultType accum_dist(const U& a, const V& b, int) const
+    {
+        return (a-b)*(a-b);
+    }
+};
+
+template<class T>
+struct L2_3D
+{
+    typedef bool is_kdtree_distance;
+
+    typedef T ElementType;
+    typedef typename Accumulator<T>::Type ResultType;
+
+    template <typename Iterator1, typename Iterator2>
+    ResultType operator()(Iterator1 a, Iterator2 b, size_t size, ResultType /*worst_dist*/ = -1) const
+    {
+        ResultType result = ResultType();        
+        ResultType diff;
+        diff = *a++ - *b++;
+        result += diff*diff;
+        diff = *a++ - *b++;
+        result += diff*diff;
+        diff = *a++ - *b++;
+        result += diff*diff;        
+        return result;
+    }
+
+    template <typename U, typename V>
+    inline ResultType accum_dist(const U& a, const V& b, int) const
+    {
+        return (a-b)*(a-b);
+    }
+};
+
+/**
+ * Squared Euclidean distance functor, optimized version
+ */
+template<class T>
+struct L2
+{
+    typedef bool is_kdtree_distance;
+
+    typedef T ElementType;
+    typedef typename Accumulator<T>::Type ResultType;
+
+    /**
+     *  Compute the squared Euclidean distance between two vectors.
+     *
+     *	This is highly optimised, with loop unrolling, as it is one
+     *	of the most expensive inner loops.
+     *
+     *	The computation of squared root at the end is omitted for
+     *	efficiency.
+     */
+    template <typename Iterator1, typename Iterator2>
+    ResultType operator()(Iterator1 a, Iterator2 b, size_t size, ResultType worst_dist = -1) const
+    {
+        ResultType result = ResultType();
+        ResultType diff0, diff1, diff2, diff3;
+        Iterator1 last = a + size;
+        Iterator1 lastgroup = last - 3;
+
+        /* Process 4 items with each loop for efficiency. */
+        while (a < lastgroup) {
+            diff0 = (ResultType)(a[0] - b[0]);
+            diff1 = (ResultType)(a[1] - b[1]);
+            diff2 = (ResultType)(a[2] - b[2]);
+            diff3 = (ResultType)(a[3] - b[3]);
+            result += diff0 * diff0 + diff1 * diff1 + diff2 * diff2 + diff3 * diff3;
+            a += 4;
+            b += 4;
+
+            if ((worst_dist>0)&&(result>worst_dist)) {
+                return result;
+            }
+        }
+        /* Process last 0-3 pixels.  Not needed for standard vector lengths. */
+        while (a < last) {
+            diff0 = (ResultType)(*a++ - *b++);
+            result += diff0 * diff0;
+        }
+        return result;
+    }
+
+    /**
+     *	Partial euclidean distance, using just one dimension. This is used by the
+     *	kd-tree when computing partial distances while traversing the tree.
+     *
+     *	Squared root is omitted for efficiency.
+     */
+    template <typename U, typename V>
+    inline ResultType accum_dist(const U& a, const V& b, int) const
+    {
+        return (a-b)*(a-b);
+    }
+};
+
+
+/*
+ * Manhattan distance functor, optimized version
+ */
+template<class T>
+struct L1
+{
+    typedef bool is_kdtree_distance;
+
+    typedef T ElementType;
+    typedef typename Accumulator<T>::Type ResultType;
+
+    /**
+     *  Compute the Manhattan (L_1) distance between two vectors.
+     *
+     *	This is highly optimised, with loop unrolling, as it is one
+     *	of the most expensive inner loops.
+     */
+    template <typename Iterator1, typename Iterator2>
+    ResultType operator()(Iterator1 a, Iterator2 b, size_t size, ResultType worst_dist = -1) const
+    {
+        ResultType result = ResultType();
+        ResultType diff0, diff1, diff2, diff3;
+        Iterator1 last = a + size;
+        Iterator1 lastgroup = last - 3;
+
+        /* Process 4 items with each loop for efficiency. */
+        while (a < lastgroup) {
+            diff0 = (ResultType)std::abs(a[0] - b[0]);
+            diff1 = (ResultType)std::abs(a[1] - b[1]);
+            diff2 = (ResultType)std::abs(a[2] - b[2]);
+            diff3 = (ResultType)std::abs(a[3] - b[3]);
+            result += diff0 + diff1 + diff2 + diff3;
+            a += 4;
+            b += 4;
+
+            if ((worst_dist>0)&&(result>worst_dist)) {
+                return result;
+            }
+        }
+        /* Process last 0-3 pixels.  Not needed for standard vector lengths. */
+        while (a < last) {
+            diff0 = (ResultType)std::abs(*a++ - *b++);
+            result += diff0;
+        }
+        return result;
+    }
+
+    /**
+     * Partial distance, used by the kd-tree.
+     */
+    template <typename U, typename V>
+    inline ResultType accum_dist(const U& a, const V& b, int) const
+    {
+        return std::abs(a-b);
+    }
+};
+
+
+
+template<class T>
+struct MinkowskiDistance
+{
+    typedef bool is_kdtree_distance;
+
+    typedef T ElementType;
+    typedef typename Accumulator<T>::Type ResultType;
+
+    int order;
+
+    MinkowskiDistance(int order_) : order(order_) {}
+
+    /**
+     *  Compute the Minkowsky (L_p) distance between two vectors.
+     *
+     *	This is highly optimised, with loop unrolling, as it is one
+     *	of the most expensive inner loops.
+     *
+     *	The computation of squared root at the end is omitted for
+     *	efficiency.
+     */
+    template <typename Iterator1, typename Iterator2>
+    ResultType operator()(Iterator1 a, Iterator2 b, size_t size, ResultType worst_dist = -1) const
+    {
+        ResultType result = ResultType();
+        ResultType diff0, diff1, diff2, diff3;
+        Iterator1 last = a + size;
+        Iterator1 lastgroup = last - 3;
+
+        /* Process 4 items with each loop for efficiency. */
+        while (a < lastgroup) {
+            diff0 = (ResultType)std::abs(a[0] - b[0]);
+            diff1 = (ResultType)std::abs(a[1] - b[1]);
+            diff2 = (ResultType)std::abs(a[2] - b[2]);
+            diff3 = (ResultType)std::abs(a[3] - b[3]);
+            result += pow(diff0,order) + pow(diff1,order) + pow(diff2,order) + pow(diff3,order);
+            a += 4;
+            b += 4;
+
+            if ((worst_dist>0)&&(result>worst_dist)) {
+                return result;
+            }
+        }
+        /* Process last 0-3 pixels.  Not needed for standard vector lengths. */
+        while (a < last) {
+            diff0 = (ResultType)std::abs(*a++ - *b++);
+            result += pow(diff0,order);
+        }
+        return result;
+    }
+
+    /**
+     * Partial distance, used by the kd-tree.
+     */
+    template <typename U, typename V>
+    inline ResultType accum_dist(const U& a, const V& b, int) const
+    {
+        return pow(static_cast<ResultType>(std::abs(a-b)),order);
+    }
+};
+
+
+
+template<class T>
+struct MaxDistance
+{
+    typedef bool is_vector_space_distance;
+
+    typedef T ElementType;
+    typedef typename Accumulator<T>::Type ResultType;
+
+    /**
+     *  Compute the max distance (L_infinity) between two vectors.
+     *
+     *  This distance is not a valid kdtree distance, it's not dimensionwise additive.
+     */
+    template <typename Iterator1, typename Iterator2>
+    ResultType operator()(Iterator1 a, Iterator2 b, size_t size, ResultType worst_dist = -1) const
+    {
+        ResultType result = ResultType();
+        ResultType diff0, diff1, diff2, diff3;
+        Iterator1 last = a + size;
+        Iterator1 lastgroup = last - 3;
+
+        /* Process 4 items with each loop for efficiency. */
+        while (a < lastgroup) {
+            diff0 = std::abs(a[0] - b[0]);
+            diff1 = std::abs(a[1] - b[1]);
+            diff2 = std::abs(a[2] - b[2]);
+            diff3 = std::abs(a[3] - b[3]);
+            if (diff0>result) {result = diff0; }
+            if (diff1>result) {result = diff1; }
+            if (diff2>result) {result = diff2; }
+            if (diff3>result) {result = diff3; }
+            a += 4;
+            b += 4;
+
+            if ((worst_dist>0)&&(result>worst_dist)) {
+                return result;
+            }
+        }
+        /* Process last 0-3 pixels.  Not needed for standard vector lengths. */
+        while (a < last) {
+            diff0 = std::abs(*a++ - *b++);
+            result = (diff0>result) ? diff0 : result;
+        }
+        return result;
+    }
+
+    /* This distance functor is not dimension-wise additive, which
+     * makes it an invalid kd-tree distance, not implementing the accum_dist method */
+
+};
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+/**
+ * Hamming distance functor - counts the bit differences between two strings - useful for the Brief descriptor
+ * bit count of A exclusive XOR'ed with B
+ */
+struct HammingLUT
+{
+    typedef unsigned char ElementType;
+    typedef int ResultType;
+
+    /** this will count the bits in a ^ b
+     */
+    ResultType operator()(const unsigned char* a, const unsigned char* b, int size) const
+    {
+        ResultType result = 0;
+        for (int i = 0; i < size; i++) {
+            result += byteBitsLookUp(a[i] ^ b[i]);
+        }
+        return result;
+    }
+
+
+    /** \brief given a byte, count the bits using a look up table
+     *  \param b the byte to count bits.  The look up table has an entry for all
+     *  values of b, where that entry is the number of bits.
+     *  \return the number of bits in byte b
+     */
+    static unsigned char byteBitsLookUp(unsigned char b)
+    {
+        static const unsigned char table[256]  = {
+            /* 0 */ 0, /* 1 */ 1, /* 2 */ 1, /* 3 */ 2,
+            /* 4 */ 1, /* 5 */ 2, /* 6 */ 2, /* 7 */ 3,
+            /* 8 */ 1, /* 9 */ 2, /* a */ 2, /* b */ 3,
+            /* c */ 2, /* d */ 3, /* e */ 3, /* f */ 4,
+            /* 10 */ 1, /* 11 */ 2, /* 12 */ 2, /* 13 */ 3,
+            /* 14 */ 2, /* 15 */ 3, /* 16 */ 3, /* 17 */ 4,
+            /* 18 */ 2, /* 19 */ 3, /* 1a */ 3, /* 1b */ 4,
+            /* 1c */ 3, /* 1d */ 4, /* 1e */ 4, /* 1f */ 5,
+            /* 20 */ 1, /* 21 */ 2, /* 22 */ 2, /* 23 */ 3,
+            /* 24 */ 2, /* 25 */ 3, /* 26 */ 3, /* 27 */ 4,
+            /* 28 */ 2, /* 29 */ 3, /* 2a */ 3, /* 2b */ 4,
+            /* 2c */ 3, /* 2d */ 4, /* 2e */ 4, /* 2f */ 5,
+            /* 30 */ 2, /* 31 */ 3, /* 32 */ 3, /* 33 */ 4,
+            /* 34 */ 3, /* 35 */ 4, /* 36 */ 4, /* 37 */ 5,
+            /* 38 */ 3, /* 39 */ 4, /* 3a */ 4, /* 3b */ 5,
+            /* 3c */ 4, /* 3d */ 5, /* 3e */ 5, /* 3f */ 6,
+            /* 40 */ 1, /* 41 */ 2, /* 42 */ 2, /* 43 */ 3,
+            /* 44 */ 2, /* 45 */ 3, /* 46 */ 3, /* 47 */ 4,
+            /* 48 */ 2, /* 49 */ 3, /* 4a */ 3, /* 4b */ 4,
+            /* 4c */ 3, /* 4d */ 4, /* 4e */ 4, /* 4f */ 5,
+            /* 50 */ 2, /* 51 */ 3, /* 52 */ 3, /* 53 */ 4,
+            /* 54 */ 3, /* 55 */ 4, /* 56 */ 4, /* 57 */ 5,
+            /* 58 */ 3, /* 59 */ 4, /* 5a */ 4, /* 5b */ 5,
+            /* 5c */ 4, /* 5d */ 5, /* 5e */ 5, /* 5f */ 6,
+            /* 60 */ 2, /* 61 */ 3, /* 62 */ 3, /* 63 */ 4,
+            /* 64 */ 3, /* 65 */ 4, /* 66 */ 4, /* 67 */ 5,
+            /* 68 */ 3, /* 69 */ 4, /* 6a */ 4, /* 6b */ 5,
+            /* 6c */ 4, /* 6d */ 5, /* 6e */ 5, /* 6f */ 6,
+            /* 70 */ 3, /* 71 */ 4, /* 72 */ 4, /* 73 */ 5,
+            /* 74 */ 4, /* 75 */ 5, /* 76 */ 5, /* 77 */ 6,
+            /* 78 */ 4, /* 79 */ 5, /* 7a */ 5, /* 7b */ 6,
+            /* 7c */ 5, /* 7d */ 6, /* 7e */ 6, /* 7f */ 7,
+            /* 80 */ 1, /* 81 */ 2, /* 82 */ 2, /* 83 */ 3,
+            /* 84 */ 2, /* 85 */ 3, /* 86 */ 3, /* 87 */ 4,
+            /* 88 */ 2, /* 89 */ 3, /* 8a */ 3, /* 8b */ 4,
+            /* 8c */ 3, /* 8d */ 4, /* 8e */ 4, /* 8f */ 5,
+            /* 90 */ 2, /* 91 */ 3, /* 92 */ 3, /* 93 */ 4,
+            /* 94 */ 3, /* 95 */ 4, /* 96 */ 4, /* 97 */ 5,
+            /* 98 */ 3, /* 99 */ 4, /* 9a */ 4, /* 9b */ 5,
+            /* 9c */ 4, /* 9d */ 5, /* 9e */ 5, /* 9f */ 6,
+            /* a0 */ 2, /* a1 */ 3, /* a2 */ 3, /* a3 */ 4,
+            /* a4 */ 3, /* a5 */ 4, /* a6 */ 4, /* a7 */ 5,
+            /* a8 */ 3, /* a9 */ 4, /* aa */ 4, /* ab */ 5,
+            /* ac */ 4, /* ad */ 5, /* ae */ 5, /* af */ 6,
+            /* b0 */ 3, /* b1 */ 4, /* b2 */ 4, /* b3 */ 5,
+            /* b4 */ 4, /* b5 */ 5, /* b6 */ 5, /* b7 */ 6,
+            /* b8 */ 4, /* b9 */ 5, /* ba */ 5, /* bb */ 6,
+            /* bc */ 5, /* bd */ 6, /* be */ 6, /* bf */ 7,
+            /* c0 */ 2, /* c1 */ 3, /* c2 */ 3, /* c3 */ 4,
+            /* c4 */ 3, /* c5 */ 4, /* c6 */ 4, /* c7 */ 5,
+            /* c8 */ 3, /* c9 */ 4, /* ca */ 4, /* cb */ 5,
+            /* cc */ 4, /* cd */ 5, /* ce */ 5, /* cf */ 6,
+            /* d0 */ 3, /* d1 */ 4, /* d2 */ 4, /* d3 */ 5,
+            /* d4 */ 4, /* d5 */ 5, /* d6 */ 5, /* d7 */ 6,
+            /* d8 */ 4, /* d9 */ 5, /* da */ 5, /* db */ 6,
+            /* dc */ 5, /* dd */ 6, /* de */ 6, /* df */ 7,
+            /* e0 */ 3, /* e1 */ 4, /* e2 */ 4, /* e3 */ 5,
+            /* e4 */ 4, /* e5 */ 5, /* e6 */ 5, /* e7 */ 6,
+            /* e8 */ 4, /* e9 */ 5, /* ea */ 5, /* eb */ 6,
+            /* ec */ 5, /* ed */ 6, /* ee */ 6, /* ef */ 7,
+            /* f0 */ 4, /* f1 */ 5, /* f2 */ 5, /* f3 */ 6,
+            /* f4 */ 5, /* f5 */ 6, /* f6 */ 6, /* f7 */ 7,
+            /* f8 */ 5, /* f9 */ 6, /* fa */ 6, /* fb */ 7,
+            /* fc */ 6, /* fd */ 7, /* fe */ 7, /* ff */ 8
+        };
+        return table[b];
+    }
+};
+
+/**
+ * Hamming distance functor (pop count between two binary vectors, i.e. xor them and count the number of bits set)
+ * That code was taken from brief.cpp in OpenCV
+ */
+template<class T>
+struct HammingPopcnt
+{
+    typedef T ElementType;
+    typedef int ResultType;
+
+    template<typename Iterator1, typename Iterator2>
+    ResultType operator()(Iterator1 a, Iterator2 b, size_t size, ResultType /*worst_dist*/ = -1) const
+    {
+        ResultType result = 0;
+#if __GNUC__
+#if ANDROID && HAVE_NEON
+        static uint64_t features = android_getCpuFeatures();
+        if ((features& ANDROID_CPU_ARM_FEATURE_NEON)) {
+            for (size_t i = 0; i < size; i += 16) {
+                uint8x16_t A_vec = vld1q_u8 (a + i);
+                uint8x16_t B_vec = vld1q_u8 (b + i);
+                //uint8x16_t veorq_u8 (uint8x16_t, uint8x16_t)
+                uint8x16_t AxorB = veorq_u8 (A_vec, B_vec);
+
+                uint8x16_t bitsSet += vcntq_u8 (AxorB);
+                //uint16x8_t vpadalq_u8 (uint16x8_t, uint8x16_t)
+                uint16x8_t bitSet8 = vpaddlq_u8 (bitsSet);
+                uint32x4_t bitSet4 = vpaddlq_u16 (bitSet8);
+
+                uint64x2_t bitSet2 = vpaddlq_u32 (bitSet4);
+                result += vgetq_lane_u64 (bitSet2,0);
+                result += vgetq_lane_u64 (bitSet2,1);
+            }
+        }
+        else
+#endif
+        //for portability just use unsigned long -- and use the __builtin_popcountll (see docs for __builtin_popcountll)
+        typedef unsigned long long pop_t;
+        const size_t modulo = size % sizeof(pop_t);
+        const pop_t* a2 = reinterpret_cast<const pop_t*> (a);
+        const pop_t* b2 = reinterpret_cast<const pop_t*> (b);
+        const pop_t* a2_end = a2 + (size / sizeof(pop_t));
+
+        for (; a2 != a2_end; ++a2, ++b2) result += __builtin_popcountll((*a2) ^ (*b2));
+
+        if (modulo) {
+            //in the case where size is not dividable by sizeof(size_t)
+            //need to mask off the bits at the end
+            pop_t a_final = 0, b_final = 0;
+            memcpy(&a_final, a2, modulo);
+            memcpy(&b_final, b2, modulo);
+            result += __builtin_popcountll(a_final ^ b_final);
+        }
+#else
+        HammingLUT lut;
+        result = lut(reinterpret_cast<const unsigned char*> (a),
+                     reinterpret_cast<const unsigned char*> (b), size * sizeof(pop_t));
+#endif
+        return result;
+    }
+};
+
+template<typename T>
+struct Hamming
+{
+    typedef T ElementType;
+    typedef unsigned int ResultType;
+
+    /** This is popcount_3() from:
+     * http://en.wikipedia.org/wiki/Hamming_weight */
+    unsigned int popcnt32(uint32_t n) const
+    {
+        n -= ((n >> 1) & 0x55555555);
+        n = (n & 0x33333333) + ((n >> 2) & 0x33333333);
+        return (((n + (n >> 4))& 0xF0F0F0F)* 0x1010101) >> 24;
+    }
+
+    unsigned int popcnt64(uint64_t n) const
+    {
+        n -= ((n >> 1) & 0x5555555555555555LL);
+        n = (n & 0x3333333333333333LL) + ((n >> 2) & 0x3333333333333333LL);
+        return (((n + (n >> 4))& 0x0f0f0f0f0f0f0f0fLL)* 0x0101010101010101LL) >> 56;
+    }
+
+    template <typename Iterator1, typename Iterator2>
+    ResultType operator()(Iterator1 a, Iterator2 b, size_t size, ResultType /*worst_dist*/ = 0) const
+    {
+#ifdef FLANN_PLATFORM_64_BIT
+        const uint64_t* pa = reinterpret_cast<const uint64_t*>(a);
+        const uint64_t* pb = reinterpret_cast<const uint64_t*>(b);
+        ResultType result = 0;
+        size /= (sizeof(uint64_t)/sizeof(unsigned char));
+        for(size_t i = 0; i < size; ++i ) {
+            result += popcnt64(*pa ^ *pb);
+            ++pa;
+            ++pb;
+        }
+#else
+        const uint32_t* pa = reinterpret_cast<const uint32_t*>(a);
+        const uint32_t* pb = reinterpret_cast<const uint32_t*>(b);
+        ResultType result = 0;
+        size /= (sizeof(uint32_t)/sizeof(unsigned char));
+        for(size_t i = 0; i < size; ++i ) {
+        	result += popcnt32(*pa ^ *pb);
+        	++pa;
+        	++pb;
+        }
+#endif
+        return result;
+    }
+};
+
+
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+template<class T>
+struct HistIntersectionDistance
+{
+    typedef bool is_kdtree_distance;
+
+    typedef T ElementType;
+    typedef typename Accumulator<T>::Type ResultType;
+
+    /**
+     *  Compute the histogram intersection distance
+     */
+    template <typename Iterator1, typename Iterator2>
+    ResultType operator()(Iterator1 a, Iterator2 b, size_t size, ResultType worst_dist = -1) const
+    {
+        ResultType result = ResultType();
+        ResultType min0, min1, min2, min3;
+        Iterator1 last = a + size;
+        Iterator1 lastgroup = last - 3;
+
+        /* Process 4 items with each loop for efficiency. */
+        while (a < lastgroup) {
+            min0 = (ResultType)(a[0] < b[0] ? a[0] : b[0]);
+            min1 = (ResultType)(a[1] < b[1] ? a[1] : b[1]);
+            min2 = (ResultType)(a[2] < b[2] ? a[2] : b[2]);
+            min3 = (ResultType)(a[3] < b[3] ? a[3] : b[3]);
+            result += min0 + min1 + min2 + min3;
+            a += 4;
+            b += 4;
+            if ((worst_dist>0)&&(result>worst_dist)) {
+                return result;
+            }
+        }
+        /* Process last 0-3 pixels.  Not needed for standard vector lengths. */
+        while (a < last) {
+            min0 = (ResultType)(*a < *b ? *a : *b);
+            result += min0;
+            ++a;
+            ++b;
+        }
+        return result;
+    }
+
+    /**
+     * Partial distance, used by the kd-tree.
+     */
+    template <typename U, typename V>
+    inline ResultType accum_dist(const U& a, const V& b, int) const
+    {
+        return a<b ? a : b;
+    }
+};
+
+
+
+template<class T>
+struct HellingerDistance
+{
+    typedef bool is_kdtree_distance;
+
+    typedef T ElementType;
+    typedef typename Accumulator<T>::Type ResultType;
+
+    /**
+     *  Compute the histogram intersection distance
+     */
+    template <typename Iterator1, typename Iterator2>
+    ResultType operator()(Iterator1 a, Iterator2 b, size_t size, ResultType /*worst_dist*/ = -1) const
+    {
+        ResultType result = ResultType();
+        ResultType diff0, diff1, diff2, diff3;
+        Iterator1 last = a + size;
+        Iterator1 lastgroup = last - 3;
+
+        /* Process 4 items with each loop for efficiency. */
+        while (a < lastgroup) {
+            diff0 = sqrt(static_cast<ResultType>(a[0])) - sqrt(static_cast<ResultType>(b[0]));
+            diff1 = sqrt(static_cast<ResultType>(a[1])) - sqrt(static_cast<ResultType>(b[1]));
+            diff2 = sqrt(static_cast<ResultType>(a[2])) - sqrt(static_cast<ResultType>(b[2]));
+            diff3 = sqrt(static_cast<ResultType>(a[3])) - sqrt(static_cast<ResultType>(b[3]));
+            result += diff0 * diff0 + diff1 * diff1 + diff2 * diff2 + diff3 * diff3;
+            a += 4;
+            b += 4;
+        }
+        while (a < last) {
+            diff0 = sqrt(static_cast<ResultType>(*a++)) - sqrt(static_cast<ResultType>(*b++));
+            result += diff0 * diff0;
+        }
+        return result;
+    }
+
+    /**
+     * Partial distance, used by the kd-tree.
+     */
+    template <typename U, typename V>
+    inline ResultType accum_dist(const U& a, const V& b, int) const
+    {
+        return sqrt(static_cast<ResultType>(a)) - sqrt(static_cast<ResultType>(b));
+    }
+};
+
+
+template<class T>
+struct ChiSquareDistance
+{
+    typedef bool is_kdtree_distance;
+
+    typedef T ElementType;
+    typedef typename Accumulator<T>::Type ResultType;
+
+    /**
+     *  Compute the chi-square distance
+     */
+    template <typename Iterator1, typename Iterator2>
+    ResultType operator()(Iterator1 a, Iterator2 b, size_t size, ResultType worst_dist = -1) const
+    {
+        ResultType result = ResultType();
+        ResultType sum, diff;
+        Iterator1 last = a + size;
+
+        while (a < last) {
+            sum = (ResultType)(*a + *b);
+            if (sum>0) {
+                diff = (ResultType)(*a - *b);
+                result += diff*diff/sum;
+            }
+            ++a;
+            ++b;
+
+            if ((worst_dist>0)&&(result>worst_dist)) {
+                return result;
+            }
+        }
+        return result;
+    }
+
+    /**
+     * Partial distance, used by the kd-tree.
+     */
+    template <typename U, typename V>
+    inline ResultType accum_dist(const U& a, const V& b, int) const
+    {
+        ResultType result = ResultType();
+        ResultType sum, diff;
+
+        sum = (ResultType)(a+b);
+        if (sum>0) {
+            diff = (ResultType)(a-b);
+            result = diff*diff/sum;
+        }
+        return result;
+    }
+};
+
+
+template<class T>
+struct KL_Divergence
+{
+    typedef bool is_kdtree_distance;
+
+    typedef T ElementType;
+    typedef typename Accumulator<T>::Type ResultType;
+
+    /**
+     *  Compute the Kullback–Leibler divergence
+     */
+    template <typename Iterator1, typename Iterator2>
+    ResultType operator()(Iterator1 a, Iterator2 b, size_t size, ResultType worst_dist = -1) const
+    {
+        ResultType result = ResultType();
+        Iterator1 last = a + size;
+
+        while (a < last) {
+            if (* a != 0) {
+                ResultType ratio = (ResultType)(*a / *b);
+                if (ratio>0) {
+                    result += *a * log(ratio);
+                }
+            }
+            ++a;
+            ++b;
+
+            if ((worst_dist>0)&&(result>worst_dist)) {
+                return result;
+            }
+        }
+        return result;
+    }
+
+    /**
+     * Partial distance, used by the kd-tree.
+     */
+    template <typename U, typename V>
+    inline ResultType accum_dist(const U& a, const V& b, int) const
+    {
+        ResultType result = ResultType();
+        ResultType ratio = (ResultType)(a / b);
+        if (ratio>0) {
+            result = a * log(ratio);
+        }
+        return result;
+    }
+};
+
+}
+
+#endif //FLANN_DIST_H_
diff --git a/src/cpp/flann/algorithms/hierarchical_clustering_index.h b/src/cpp/flann/algorithms/hierarchical_clustering_index.h
new file mode 100644
index 0000000..e730e4f
--- /dev/null
+++ b/src/cpp/flann/algorithms/hierarchical_clustering_index.h
@@ -0,0 +1,705 @@
+/***********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2008-2011  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+ * Copyright 2008-2011  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+ *
+ * THE BSD LICENSE
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+
+#ifndef FLANN_HIERARCHICAL_CLUSTERING_INDEX_H_
+#define FLANN_HIERARCHICAL_CLUSTERING_INDEX_H_
+
+#include <algorithm>
+#include <string>
+#include <map>
+#include <cassert>
+#include <limits>
+#include <cmath>
+
+#include "flann/general.h"
+#include "flann/algorithms/nn_index.h"
+#include "flann/algorithms/dist.h"
+#include "flann/util/matrix.h"
+#include "flann/util/result_set.h"
+#include "flann/util/heap.h"
+#include "flann/util/allocator.h"
+#include "flann/util/random.h"
+#include "flann/util/saving.h"
+#include "flann/util/serialization.h"
+
+namespace flann
+{
+
+struct HierarchicalClusteringIndexParams : public IndexParams
+{
+    HierarchicalClusteringIndexParams(int branching = 32,
+                                      flann_centers_init_t centers_init = FLANN_CENTERS_RANDOM,
+                                      int trees = 4, int leaf_max_size = 100)
+    {
+        (*this)["algorithm"] = FLANN_INDEX_HIERARCHICAL;
+        // The branching factor used in the hierarchical clustering
+        (*this)["branching"] = branching;
+        // Algorithm used for picking the initial cluster centers
+        (*this)["centers_init"] = centers_init;
+        // number of parallel trees to build
+        (*this)["trees"] = trees;
+        // maximum leaf size
+        (*this)["leaf_max_size"] = leaf_max_size;
+    }
+};
+
+
+
+/**
+ * Hierarchical index
+ *
+ * Contains a tree constructed through a hierarchical clustering
+ * and other information for indexing a set of points for nearest-neighbour matching.
+ */
+template <typename Distance>
+class HierarchicalClusteringIndex : public NNIndex<Distance>
+{
+public:
+    typedef typename Distance::ElementType ElementType;
+    typedef typename Distance::ResultType DistanceType;
+
+    typedef NNIndex<Distance> BaseClass;
+
+    /**
+     * Constructor.
+     *
+     * @param index_params
+     * @param d
+     */
+    HierarchicalClusteringIndex(const IndexParams& index_params = HierarchicalClusteringIndexParams(), Distance d = Distance())
+        : BaseClass(index_params, d)
+    {
+        memoryCounter_ = 0;
+
+        branching_ = get_param(index_params_,"branching",32);
+        centers_init_ = get_param(index_params_,"centers_init", FLANN_CENTERS_RANDOM);
+        trees_ = get_param(index_params_,"trees",4);
+        leaf_max_size_ = get_param(index_params_,"leaf_max_size",100);
+
+        initCenterChooser();
+    }
+
+
+    /**
+     * Index constructor
+     *
+     * Params:
+     *          inputData = dataset with the input features
+     *          params = parameters passed to the hierarchical k-means algorithm
+     */
+    HierarchicalClusteringIndex(const Matrix<ElementType>& inputData, const IndexParams& index_params = HierarchicalClusteringIndexParams(),
+                                Distance d = Distance())
+        : BaseClass(index_params, d)
+    {
+        memoryCounter_ = 0;
+
+        branching_ = get_param(index_params_,"branching",32);
+        centers_init_ = get_param(index_params_,"centers_init", FLANN_CENTERS_RANDOM);
+        trees_ = get_param(index_params_,"trees",4);
+        leaf_max_size_ = get_param(index_params_,"leaf_max_size",100);
+
+        initCenterChooser();
+        chooseCenters_->setDataset(inputData);
+        
+        setDataset(inputData);
+    }
+
+
+    HierarchicalClusteringIndex(const HierarchicalClusteringIndex& other) : BaseClass(other),
+    		memoryCounter_(other.memoryCounter_),
+    		branching_(other.branching_),
+    		trees_(other.trees_),
+    		centers_init_(other.centers_init_),
+    		leaf_max_size_(other.leaf_max_size_)
+
+    {
+    	initCenterChooser();
+        tree_roots_.resize(other.tree_roots_.size());
+        for (size_t i=0;i<tree_roots_.size();++i) {
+        	copyTree(tree_roots_[i], other.tree_roots_[i]);
+        }
+    }
+
+    HierarchicalClusteringIndex& operator=(HierarchicalClusteringIndex other)
+    {
+    	this->swap(other);
+    	return *this;
+    }
+
+
+    void initCenterChooser()
+    {
+        switch(centers_init_) {
+        case FLANN_CENTERS_RANDOM:
+        	chooseCenters_ = new RandomCenterChooser<Distance>(distance_);
+        	break;
+        case FLANN_CENTERS_GONZALES:
+        	chooseCenters_ = new GonzalesCenterChooser<Distance>(distance_);
+        	break;
+        case FLANN_CENTERS_KMEANSPP:
+            chooseCenters_ = new KMeansppCenterChooser<Distance>(distance_);
+        	break;
+        default:
+            throw FLANNException("Unknown algorithm for choosing initial centers.");
+        }
+    }
+
+    /**
+     * Index destructor.
+     *
+     * Release the memory used by the index.
+     */
+    virtual ~HierarchicalClusteringIndex()
+    {
+    	delete chooseCenters_;
+    	freeIndex();
+    }
+
+    BaseClass* clone() const
+    {
+    	return new HierarchicalClusteringIndex(*this);
+    }
+
+    /**
+     * Computes the inde memory usage
+     * Returns: memory used by the index
+     */
+    int usedMemory() const
+    {
+        return pool_.usedMemory+pool_.wastedMemory+memoryCounter_;
+    }
+    
+    using BaseClass::buildIndex;
+
+    void addPoints(const Matrix<ElementType>& points, float rebuild_threshold = 2)
+    {
+        assert(points.cols==veclen_);
+        size_t old_size = size_;
+
+        extendDataset(points);
+        
+        if (rebuild_threshold>1 && size_at_build_*rebuild_threshold<size_) {
+            buildIndex();
+        }
+        else {
+            for (size_t i=0;i<points.rows;++i) {
+                for (int j = 0; j < trees_; j++) {
+                    addPointToTree(tree_roots_[j], old_size + i);
+                }
+            }            
+        }
+    }
+
+
+    flann_algorithm_t getType() const
+    {
+        return FLANN_INDEX_HIERARCHICAL;
+    }
+
+
+    template<typename Archive>
+    void serialize(Archive& ar)
+    {
+    	ar.setObject(this);
+
+    	ar & *static_cast<NNIndex<Distance>*>(this);
+
+    	ar & branching_;
+    	ar & trees_;
+    	ar & centers_init_;
+    	ar & leaf_max_size_;
+
+    	if (Archive::is_loading::value) {
+    		tree_roots_.resize(trees_);
+    	}
+    	for (size_t i=0;i<tree_roots_.size();++i) {
+    		if (Archive::is_loading::value) {
+    			tree_roots_[i] = new(pool_) Node();
+    		}
+    		ar & *tree_roots_[i];
+    	}
+
+    	if (Archive::is_loading::value) {
+            index_params_["algorithm"] = getType();
+            index_params_["branching"] = branching_;
+            index_params_["trees"] = trees_;
+            index_params_["centers_init"] = centers_init_;
+            index_params_["leaf_size"] = leaf_max_size_;
+    	}
+    }
+
+    void saveIndex(FILE* stream)
+    {
+    	serialization::SaveArchive sa(stream);
+    	sa & *this;
+    }
+
+
+    void loadIndex(FILE* stream)
+    {
+    	serialization::LoadArchive la(stream);
+    	la & *this;
+    }
+
+
+    /**
+     * Find set of nearest neighbors to vec. Their indices are stored inside
+     * the result object.
+     *
+     * Params:
+     *     result = the result object in which the indices of the nearest-neighbors are stored
+     *     vec = the vector for which to search the nearest neighbors
+     *     searchParams = parameters that influence the search algorithm (checks)
+     */
+
+    void findNeighbors(ResultSet<DistanceType>& result, const ElementType* vec, const SearchParams& searchParams) const
+    {
+    	if (removed_) {
+    		findNeighborsWithRemoved<true>(result, vec, searchParams);
+    	}
+    	else {
+    		findNeighborsWithRemoved<false>(result, vec, searchParams);
+    	}
+    }
+
+protected:
+
+    /**
+     * Builds the index
+     */
+    void buildIndexImpl()
+    {
+        if (branching_<2) {
+            throw FLANNException("Branching factor must be at least 2");
+        }
+        tree_roots_.resize(trees_);
+        std::vector<int> indices(size_);
+        for (int i=0; i<trees_; ++i) {
+            for (size_t j=0; j<size_; ++j) {
+                indices[j] = j;
+            }
+            tree_roots_[i] = new(pool_) Node();
+            computeClustering(tree_roots_[i], &indices[0], size_);
+        }
+    }
+
+private:
+
+    struct PointInfo
+    {
+    	/** Point index */
+    	size_t index;
+    	/** Point data */
+    	ElementType* point;
+
+    private:
+    	template<typename Archive>
+    	void serialize(Archive& ar)
+    	{
+    		typedef HierarchicalClusteringIndex<Distance> Index;
+    		Index* obj = static_cast<Index*>(ar.getObject());
+
+    		ar & index;
+//    		ar & point;
+
+			if (Archive::is_loading::value) {
+				point = obj->points_[index];
+			}
+    	}
+    	friend struct serialization::access;
+    };
+
+    /**
+     * Struture representing a node in the hierarchical k-means tree.
+     */
+    struct Node
+    {
+        /**
+         * The cluster center
+         */
+    	ElementType* pivot;
+    	size_t pivot_index;
+        /**
+         * Child nodes (only for non-terminal nodes)
+         */
+        std::vector<Node*> childs;
+        /**
+         * Node points (only for terminal nodes)
+         */
+        std::vector<PointInfo> points;
+
+        /**
+         * destructor
+         * calling Node destructor explicitly
+         */
+        ~Node()
+        {
+        	for(size_t i=0; i<childs.size(); i++){
+        		childs[i]->~Node();
+        	}
+        };
+
+    private:
+    	template<typename Archive>
+    	void serialize(Archive& ar)
+    	{
+    		typedef HierarchicalClusteringIndex<Distance> Index;
+    		Index* obj = static_cast<Index*>(ar.getObject());
+    		ar & pivot_index;
+    		if (Archive::is_loading::value) {
+    			pivot = obj->points_[pivot_index];
+    		}
+    		size_t childs_size;
+    		if (Archive::is_saving::value) {
+    			childs_size = childs.size();
+    		}
+    		ar & childs_size;
+
+    		if (childs_size==0) {
+    			ar & points;
+    		}
+    		else {
+    			if (Archive::is_loading::value) {
+    				childs.resize(childs_size);
+    			}
+    			for (size_t i=0;i<childs_size;++i) {
+    				if (Archive::is_loading::value) {
+    					childs[i] = new(obj->pool_) Node();
+    				}
+    				ar & *childs[i];
+    			}
+    		}
+
+    	}
+    	friend struct serialization::access;
+    };
+    typedef Node* NodePtr;
+
+
+
+    /**
+     * Alias definition for a nicer syntax.
+     */
+    typedef BranchStruct<NodePtr, DistanceType> BranchSt;
+
+
+    /**
+     * Clears Node tree
+     * calling Node destructor explicitly
+     */
+    void freeIndex(){
+    	for (size_t i=0; i<tree_roots_.size(); ++i) {
+    		tree_roots_[i]->~Node();
+    	}
+    	pool_.free();
+    }
+
+    void copyTree(NodePtr& dst, const NodePtr& src)
+    {
+    	dst = new(pool_) Node();
+    	dst->pivot_index = src->pivot_index;
+    	dst->pivot = points_[dst->pivot_index];
+
+    	if (src->childs.size()==0) {
+    		dst->points = src->points;
+    	}
+    	else {
+    		dst->childs.resize(src->childs.size());
+    		for (size_t i=0;i<src->childs.size();++i) {
+    			copyTree(dst->childs[i], src->childs[i]);
+    		}
+    	}
+    }
+
+
+
+    void computeLabels(int* indices, int indices_length,  int* centers, int centers_length, int* labels, DistanceType& cost)
+    {
+        cost = 0;
+        for (int i=0; i<indices_length; ++i) {
+            ElementType* point = points_[indices[i]];
+            DistanceType dist = distance_(point, points_[centers[0]], veclen_);
+            labels[i] = 0;
+            for (int j=1; j<centers_length; ++j) {
+                DistanceType new_dist = distance_(point, points_[centers[j]], veclen_);
+                if (dist>new_dist) {
+                    labels[i] = j;
+                    dist = new_dist;
+                }
+            }
+            cost += dist;
+        }
+    }
+
+    /**
+     * The method responsible with actually doing the recursive hierarchical
+     * clustering
+     *
+     * Params:
+     *     node = the node to cluster
+     *     indices = indices of the points belonging to the current node
+     *     branching = the branching factor to use in the clustering
+     *
+     */
+    void computeClustering(NodePtr node, int* indices, int indices_length)
+    {
+        if (indices_length < leaf_max_size_) { // leaf node
+            node->points.resize(indices_length);
+            for (int i=0;i<indices_length;++i) {
+            	node->points[i].index = indices[i];
+            	node->points[i].point = points_[indices[i]];
+            }
+            node->childs.clear();
+            return;
+        }
+
+        std::vector<int> centers(branching_);
+        std::vector<int> labels(indices_length);
+
+        int centers_length;
+        (*chooseCenters_)(branching_, indices, indices_length, &centers[0], centers_length);
+
+        if (centers_length<branching_) {
+            node->points.resize(indices_length);
+            for (int i=0;i<indices_length;++i) {
+            	node->points[i].index = indices[i];
+            	node->points[i].point = points_[indices[i]];
+            }
+            node->childs.clear();
+            return;
+        }
+
+
+        //  assign points to clusters
+        DistanceType cost;
+        computeLabels(indices, indices_length, &centers[0], centers_length, &labels[0], cost);
+
+        node->childs.resize(branching_);
+        int start = 0;
+        int end = start;
+        for (int i=0; i<branching_; ++i) {
+            for (int j=0; j<indices_length; ++j) {
+                if (labels[j]==i) {
+                    std::swap(indices[j],indices[end]);
+                    std::swap(labels[j],labels[end]);
+                    end++;
+                }
+            }
+
+            node->childs[i] = new(pool_) Node();
+            node->childs[i]->pivot_index = centers[i];
+            node->childs[i]->pivot = points_[centers[i]];
+            node->childs[i]->points.clear();
+            computeClustering(node->childs[i],indices+start, end-start);
+            start=end;
+        }
+    }
+
+
+    template<bool with_removed>
+    void findNeighborsWithRemoved(ResultSet<DistanceType>& result, const ElementType* vec, const SearchParams& searchParams) const
+    {
+        int maxChecks = searchParams.checks;
+
+        // Priority queue storing intermediate branches in the best-bin-first search
+        Heap<BranchSt>* heap = new Heap<BranchSt>(size_);
+
+        DynamicBitset checked(size_);
+        int checks = 0;
+        for (int i=0; i<trees_; ++i) {
+            findNN<with_removed>(tree_roots_[i], result, vec, checks, maxChecks, heap, checked);
+        }
+
+        BranchSt branch;
+        while (heap->popMin(branch) && (checks<maxChecks || !result.full())) {
+            NodePtr node = branch.node;
+            findNN<with_removed>(node, result, vec, checks, maxChecks, heap, checked);
+        }
+
+        delete heap;
+    }
+
+
+    /**
+     * Performs one descent in the hierarchical k-means tree. The branches not
+     * visited are stored in a priority queue.
+     *
+     * Params:
+     *      node = node to explore
+     *      result = container for the k-nearest neighbors found
+     *      vec = query points
+     *      checks = how many points in the dataset have been checked so far
+     *      maxChecks = maximum dataset points to checks
+     */
+
+    template<bool with_removed>
+    void findNN(NodePtr node, ResultSet<DistanceType>& result, const ElementType* vec, int& checks, int maxChecks,
+                Heap<BranchSt>* heap,  DynamicBitset& checked) const
+    {
+        if (node->childs.empty()) {
+            if (checks>=maxChecks) {
+                if (result.full()) return;
+            }
+
+            for (size_t i=0; i<node->points.size(); ++i) {
+            	PointInfo& pointInfo = node->points[i];
+            	if (with_removed) {
+            		if (removed_points_.test(pointInfo.index)) continue;
+            	}
+                if (checked.test(pointInfo.index)) continue;
+                DistanceType dist = distance_(pointInfo.point, vec, veclen_);
+                result.addPoint(dist, pointInfo.index);
+                checked.set(pointInfo.index);
+                ++checks;
+            }
+        }
+        else {
+            DistanceType* domain_distances = new DistanceType[branching_];
+            int best_index = 0;
+            domain_distances[best_index] = distance_(vec, node->childs[best_index]->pivot, veclen_);
+            for (int i=1; i<branching_; ++i) {
+                domain_distances[i] = distance_(vec, node->childs[i]->pivot, veclen_);
+                if (domain_distances[i]<domain_distances[best_index]) {
+                    best_index = i;
+                }
+            }
+            for (int i=0; i<branching_; ++i) {
+                if (i!=best_index) {
+                    heap->insert(BranchSt(node->childs[i],domain_distances[i]));
+                }
+            }
+            delete[] domain_distances;
+            findNN<with_removed>(node->childs[best_index],result,vec, checks, maxChecks, heap, checked);
+        }
+    }
+    
+    void addPointToTree(NodePtr node, size_t index)
+    {
+        ElementType* point = points_[index];
+        
+        if (node->childs.empty()) { // leaf node
+        	PointInfo pointInfo;
+        	pointInfo.point = point;
+        	pointInfo.index = index;
+            node->points.push_back(pointInfo);
+
+            if (node->points.size()>=size_t(branching_)) {
+                std::vector<int> indices(node->points.size());
+
+                for (size_t i=0;i<node->points.size();++i) {
+                	indices[i] = node->points[i].index;
+                }
+                computeClustering(node, &indices[0], indices.size());
+            }
+        }
+        else {            
+            // find the closest child
+            int closest = 0;
+            ElementType* center = node->childs[closest]->pivot;
+            DistanceType dist = distance_(center, point, veclen_);
+            for (size_t i=1;i<size_t(branching_);++i) {
+                center = node->childs[i]->pivot;
+                DistanceType crt_dist = distance_(center, point, veclen_);
+                if (crt_dist<dist) {
+                    dist = crt_dist;
+                    closest = i;
+                }
+            }
+            addPointToTree(node->childs[closest], index);
+        }                
+    }
+
+    void swap(HierarchicalClusteringIndex& other)
+    {
+    	BaseClass::swap(other);
+
+    	std::swap(tree_roots_, other.tree_roots_);
+    	std::swap(pool_, other.pool_);
+    	std::swap(memoryCounter_, other.memoryCounter_);
+    	std::swap(branching_, other.branching_);
+    	std::swap(trees_, other.trees_);
+    	std::swap(centers_init_, other.centers_init_);
+    	std::swap(leaf_max_size_, other.leaf_max_size_);
+    	std::swap(chooseCenters_, other.chooseCenters_);
+    }
+
+private:
+
+    /**
+     * The root nodes in the tree.
+     */
+    std::vector<Node*> tree_roots_;
+
+    /**
+     * Pooled memory allocator.
+     *
+     * Using a pooled memory allocator is more efficient
+     * than allocating memory directly when there is a large
+     * number small of memory allocations.
+     */
+    PooledAllocator pool_;
+
+    /**
+     * Memory occupied by the index.
+     */
+    int memoryCounter_;
+
+    /** index parameters */
+    /**
+     * Branching factor to use for clustering
+     */
+    int branching_;
+    
+    /**
+     * How many parallel trees to build
+     */
+    int trees_;
+    
+    /**
+     * Algorithm to use for choosing cluster centers
+     */
+    flann_centers_init_t centers_init_;
+    
+    /**
+     * Max size of leaf nodes
+     */
+    int leaf_max_size_;
+    
+    /**
+     * Algorithm used to choose initial centers
+     */
+    CenterChooser<Distance>* chooseCenters_;
+
+    USING_BASECLASS_SYMBOLS
+};
+
+}
+
+#endif /* FLANN_HIERARCHICAL_CLUSTERING_INDEX_H_ */
diff --git a/src/cpp/flann/algorithms/kdtree_cuda_3d_index.cu b/src/cpp/flann/algorithms/kdtree_cuda_3d_index.cu
new file mode 100644
index 0000000..e9b0508
--- /dev/null
+++ b/src/cpp/flann/algorithms/kdtree_cuda_3d_index.cu
@@ -0,0 +1,842 @@
+/**********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2011       Andreas Muetzel (amuetzel at uni-koblenz.de). All rights reserved.
+ *
+ * THE BSD LICENSE
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+
+#include "kdtree_cuda_3d_index.h"
+#include <flann/algorithms/dist.h>
+#include <flann/util/cuda/result_set.h>
+// #define THRUST_DEBUG 1
+#include <cuda.h>
+#include <thrust/copy.h>
+#include <thrust/device_vector.h>
+#include <vector_types.h>
+#include <flann/util/cutil_math.h>
+#include <thrust/host_vector.h>
+#include <thrust/copy.h>
+#include <flann/util/cuda/heap.h>
+#include <thrust/scan.h>
+#include <thrust/count.h>
+#include <flann/algorithms/kdtree_cuda_builder.h>
+#include <vector_types.h>
+namespace flann
+{
+
+namespace KdTreeCudaPrivate
+{
+template< typename GPUResultSet, typename Distance >
+__device__
+void searchNeighbors(const cuda::kd_tree_builder_detail::SplitInfo* splits,
+                     const int* child1,
+                     const int* parent,
+                     const float4* aabbLow,
+                     const float4* aabbHigh, const float4* elements, const float4& q, GPUResultSet& result, const Distance& distance = Distance() )
+{
+
+    bool backtrack=false;
+    int lastNode=-1;
+    int current=0;
+
+    cuda::kd_tree_builder_detail::SplitInfo split;
+    while(true) {
+        if( current==-1 ) break;
+        split = splits[current];
+
+        float diff1;
+        if( split.split_dim==0 ) diff1=q.x- split.split_val;
+        else if( split.split_dim==1 ) diff1=q.y- split.split_val;
+        else if( split.split_dim==2 ) diff1=q.z- split.split_val;
+
+        // children are next to each other: leftChild+1 == rightChild
+        int leftChild= child1[current];
+        int bestChild=leftChild;
+        int otherChild=leftChild;
+
+        if ((diff1)<0) {
+            otherChild++;
+        }
+        else {
+            bestChild++;
+        }
+
+        if( !backtrack ) {
+            /* If this is a leaf node, then do check and return. */
+            if (leftChild==-1) {
+                for (int i=split.left; i<split.right; ++i) {
+                    float dist=distance.dist(elements[i],q);
+                    result.insert(i,dist);
+                }
+                backtrack=true;
+                lastNode=current;
+                current=parent[current];
+            }
+            else { // go to closer child node
+                lastNode=current;
+                current=bestChild;
+            }
+        }
+        else { // continue moving back up the tree or visit far node?
+              // minimum possible distance between query point and a point inside the AABB
+            float mindistsq=0;
+            float4 aabbMin=aabbLow[otherChild];
+            float4 aabbMax=aabbHigh[otherChild];
+
+            if( q.x < aabbMin.x ) mindistsq+=distance.axisDist(q.x, aabbMin.x);
+            else if( q.x > aabbMax.x ) mindistsq+=distance.axisDist(q.x, aabbMax.x);
+            if( q.y < aabbMin.y ) mindistsq+=distance.axisDist(q.y, aabbMin.y);
+            else if( q.y > aabbMax.y ) mindistsq+=distance.axisDist(q.y, aabbMax.y);
+            if( q.z < aabbMin.z ) mindistsq+=distance.axisDist(q.z, aabbMin.z);
+            else if( q.z > aabbMax.z ) mindistsq+=distance.axisDist(q.z, aabbMax.z);
+
+            //  the far node was NOT the last node (== not visited yet) AND there could be a closer point in it
+            if(( lastNode==bestChild) && (mindistsq <= result.worstDist() ) ) {
+                lastNode=current;
+                current=otherChild;
+                backtrack=false;
+            }
+            else {
+                lastNode=current;
+                current=parent[current];
+            }
+        }
+
+    }
+}
+
+
+template< typename GPUResultSet, typename Distance >
+__global__
+void nearestKernel(const cuda::kd_tree_builder_detail::SplitInfo* splits,
+                   const int* child1,
+                   const int* parent,
+                   const float4* aabbMin,
+                   const float4* aabbMax, const float4* elements, const float* query, int stride, int resultStride, int* resultIndex, float* resultDist, int querysize, GPUResultSet result, Distance dist = Distance())
+{
+    typedef float DistanceType;
+    typedef float ElementType;
+    //                  typedef DistanceType float;
+    size_t tid = blockDim.x*blockIdx.x + threadIdx.x;
+
+    if( tid >= querysize ) return;
+
+    float4 q = make_float4(query[tid*stride],query[tid*stride+1],query[tid*stride+2],0);
+
+    result.setResultLocation( resultDist, resultIndex, tid, resultStride );
+
+    searchNeighbors(splits,child1,parent,aabbMin,aabbMax,elements, q, result, dist);
+
+    result.finish();
+}
+
+}
+
+//! contains some pointers that use cuda data types and that cannot be easily
+//! forward-declared.
+//! basically it contains all GPU buffers
+template<typename Distance>
+struct KDTreeCuda3dIndex<Distance>::GpuHelper
+{
+    thrust::device_vector< cuda::kd_tree_builder_detail::SplitInfo >* gpu_splits_;
+    thrust::device_vector< int >* gpu_parent_;
+    thrust::device_vector< int >* gpu_child1_;
+    thrust::device_vector< float4 >* gpu_aabb_min_;
+    thrust::device_vector< float4 >* gpu_aabb_max_;
+    thrust::device_vector<float4>* gpu_points_;
+    thrust::device_vector<int>* gpu_vind_;
+    GpuHelper() :  gpu_splits_(0), gpu_parent_(0), gpu_child1_(0), gpu_aabb_min_(0), gpu_aabb_max_(0), gpu_points_(0), gpu_vind_(0){
+    }
+    ~GpuHelper()
+    {
+        delete gpu_splits_;
+        gpu_splits_=0;
+        delete gpu_parent_;
+        gpu_parent_=0;
+        delete gpu_child1_;
+        gpu_child1_=0;
+        delete gpu_aabb_max_;
+        gpu_aabb_max_=0;
+        delete gpu_aabb_min_;
+        gpu_aabb_min_=0;
+        delete gpu_vind_;
+        gpu_vind_=0;
+
+        delete gpu_points_;
+        gpu_points_=0;
+    }
+};
+
+//! thrust transform functor
+//! transforms indices in the internal data set back to the original indices
+struct map_indices
+{
+    const int* v_;
+
+    map_indices(const int* v) : v_(v) {
+    }
+
+    __host__ __device__
+    float operator() (const int&i) const
+    {
+        if( i>= 0 ) return v_[i];
+        else return i;
+    }
+};
+
+//! implementation of L2 distance for the CUDA kernels
+struct CudaL2
+{
+
+    static float
+    __host__ __device__
+    axisDist( float a, float b )
+    {
+        return (a-b)*(a-b);
+    }
+
+    static float
+    __host__ __device__
+    dist( float4 a, float4 b )
+    {
+        float4 diff = a-b;
+        return dot(diff,diff);
+    }
+};
+
+//! implementation of L1 distance for the CUDA kernels
+//! NOT TESTED!
+struct CudaL1
+{
+
+    static float
+    __host__ __device__
+    axisDist( float a, float b )
+    {
+        return fabs(a-b);
+    }
+
+    static float
+    __host__ __device__
+    dist( float4 a, float4 b )
+    {
+        return fabs(a.x-b.x)+fabs (a.y-b.y)+( a.z-b.z)+(a.w-b.w);
+    }
+};
+
+//! used to adapt CPU and GPU distance types.
+//! specializations define the ::type as their corresponding GPU distance type
+//! \see GpuDistance< L2<float> >, GpuDistance< L2_Simple<float> >
+template< class Distance >
+struct GpuDistance
+{
+};
+
+template<>
+struct GpuDistance< L2<float> >
+{
+    typedef CudaL2 type;
+};
+
+template<>
+struct GpuDistance< L2_Simple<float> >
+{
+    typedef CudaL2 type;
+};
+template<>
+struct GpuDistance< L1<float> >
+{
+    typedef CudaL1 type;
+};
+
+
+template< typename Distance >
+void KDTreeCuda3dIndex<Distance>::knnSearchGpu(const Matrix<ElementType>& queries, Matrix<int>& indices, Matrix<DistanceType>& dists, size_t knn, const SearchParams& params) const
+{
+    assert(indices.rows >= queries.rows);
+    assert(dists.rows >= queries.rows);
+    assert(int(indices.cols) >= knn);
+    assert( dists.cols == indices.cols && dists.stride==indices.stride );
+    
+    int istride=queries.stride/sizeof(ElementType);
+    int ostride=indices.stride/4;
+
+    bool matrices_on_gpu = params.matrices_in_gpu_ram;
+
+    int threadsPerBlock = 128;
+    int blocksPerGrid=(queries.rows+threadsPerBlock-1)/threadsPerBlock;
+
+    float epsError = 1+params.eps;
+    bool sorted = params.sorted;
+    bool use_heap = params.use_heap;
+
+    typename GpuDistance<Distance>::type distance;
+//       std::cout<<" search: "<<std::endl;
+//       std::cout<<"  rows: "<<indices.rows<<" "<<dists.rows<<" "<<queries.rows<<std::endl;
+//       std::cout<<"  cols: "<<indices.cols<<" "<<dists.cols<<" "<<queries.cols<<std::endl;
+//       std::cout<<"  stride: "<<indices.stride<<" "<<dists.stride<<" "<<queries.stride<<std::endl;
+//       std::cout<<"  stride2:"<<istride<<" "<<ostride<<std::endl;
+//       std::cout<<"  knn:"<<knn<<"  matrices_on_gpu:"<<matrices_on_gpu<<std::endl;
+
+    if( !matrices_on_gpu ) {
+        thrust::device_vector<float> queriesDev(istride* queries.rows,0);
+        thrust::copy( queries.ptr(), queries.ptr()+istride*queries.rows, queriesDev.begin() );
+        thrust::device_vector<float> distsDev(queries.rows* ostride);
+        thrust::device_vector<int> indicesDev(queries.rows* ostride);
+
+
+
+        if( knn==1  ) {
+            KdTreeCudaPrivate::nearestKernel<<<blocksPerGrid, threadsPerBlock>>> (thrust::raw_pointer_cast(&((*gpu_helper_->gpu_splits_)[0])),
+                                                                                  thrust::raw_pointer_cast(&((*gpu_helper_->gpu_child1_)[0])),
+                                                                                  thrust::raw_pointer_cast(&((*gpu_helper_->gpu_parent_)[0])),
+                                                                                  thrust::raw_pointer_cast(&((*gpu_helper_->gpu_aabb_min_)[0])),
+                                                                                  thrust::raw_pointer_cast(&((*gpu_helper_->gpu_aabb_max_)[0])),
+                                                                                  thrust::raw_pointer_cast( &((*gpu_helper_->gpu_points_)[0]) ),
+                                                                                  thrust::raw_pointer_cast(&queriesDev[0]),
+                                                                                  istride,
+                                                                                  ostride,
+                                                                                  thrust::raw_pointer_cast(&indicesDev[0]),
+                                                                                  thrust::raw_pointer_cast(&distsDev[0]),
+                                                                                  queries.rows, flann::cuda::SingleResultSet<float>(epsError),distance);
+            //                          KdTreeCudaPrivate::nearestKernel<<<blocksPerGrid, threadsPerBlock>>> (thrust::raw_pointer_cast(&((*gpu_helper_->gpu_nodes_)[0])),
+            //                                                                                                                                                                                                                                                                                          thrust::raw_pointer_cast( &((*gpu_helper_->gpu_points_)[0]) ),
+            //                                                                                                                                                                                                                                                                                          thrust::raw_pointer_cast(&queriesDev[0]),
+            //                                                                                                                                                                                                                                                                                          queries.stride,
+            //                                                                                                                                                                                                                                                                                          thrust::raw_pointer_cast(&indicesDev[0]),
+            //                                                                                                                                                                                                                                                                                          thrust::raw_pointer_cast(&distsDev[0]),
+            //                                                                                                                                                                                                                                                                                          queries.rows, epsError);
+            //
+        }
+        else {
+            if( use_heap ) {
+                KdTreeCudaPrivate::nearestKernel<<<blocksPerGrid, threadsPerBlock>>> (thrust::raw_pointer_cast(&((*gpu_helper_->gpu_splits_)[0])),
+                                                                                      thrust::raw_pointer_cast(&((*gpu_helper_->gpu_child1_)[0])),
+                                                                                      thrust::raw_pointer_cast(&((*gpu_helper_->gpu_parent_)[0])),
+                                                                                      thrust::raw_pointer_cast(&((*gpu_helper_->gpu_aabb_min_)[0])),
+                                                                                      thrust::raw_pointer_cast(&((*gpu_helper_->gpu_aabb_max_)[0])),
+                                                                                      thrust::raw_pointer_cast( &((*gpu_helper_->gpu_points_)[0]) ),
+                                                                                      thrust::raw_pointer_cast(&queriesDev[0]),
+                                                                                      istride,
+                                                                                      ostride,
+                                                                                      thrust::raw_pointer_cast(&indicesDev[0]),
+                                                                                      thrust::raw_pointer_cast(&distsDev[0]),
+                                                                                      queries.rows, flann::cuda::KnnResultSet<float, true>(knn,sorted,epsError)
+                                                                                      , distance);
+            }
+            else {
+                KdTreeCudaPrivate::nearestKernel<<<blocksPerGrid, threadsPerBlock>>> (thrust::raw_pointer_cast(&((*gpu_helper_->gpu_splits_)[0])),
+                                                                                      thrust::raw_pointer_cast(&((*gpu_helper_->gpu_child1_)[0])),
+                                                                                      thrust::raw_pointer_cast(&((*gpu_helper_->gpu_parent_)[0])),
+                                                                                      thrust::raw_pointer_cast(&((*gpu_helper_->gpu_aabb_min_)[0])),
+                                                                                      thrust::raw_pointer_cast(&((*gpu_helper_->gpu_aabb_max_)[0])),
+                                                                                      thrust::raw_pointer_cast( &((*gpu_helper_->gpu_points_)[0]) ),
+                                                                                      thrust::raw_pointer_cast(&queriesDev[0]),
+                                                                                      istride,
+                                                                                      ostride,
+                                                                                      thrust::raw_pointer_cast(&indicesDev[0]),
+                                                                                      thrust::raw_pointer_cast(&distsDev[0]),
+                                                                                      queries.rows, flann::cuda::KnnResultSet<float, false>(knn,sorted,epsError),
+                                                                                      distance
+                                                                                      );
+            }
+        }
+        thrust::copy( distsDev.begin(), distsDev.end(), dists.ptr() );
+        thrust::transform(indicesDev.begin(), indicesDev.end(), indicesDev.begin(), map_indices(thrust::raw_pointer_cast( &((*gpu_helper_->gpu_vind_))[0]) ));
+        thrust::copy( indicesDev.begin(), indicesDev.end(), indices.ptr() );
+    }
+    else {
+        thrust::device_ptr<float> qd = thrust::device_pointer_cast(queries.ptr());
+        thrust::device_ptr<float> dd = thrust::device_pointer_cast(dists.ptr());
+        thrust::device_ptr<int> id = thrust::device_pointer_cast(indices.ptr());
+
+
+
+        if( knn==1  ) {
+            KdTreeCudaPrivate::nearestKernel<<<blocksPerGrid, threadsPerBlock>>> (thrust::raw_pointer_cast(&((*gpu_helper_->gpu_splits_)[0])),
+                                                                                  thrust::raw_pointer_cast(&((*gpu_helper_->gpu_child1_)[0])),
+                                                                                  thrust::raw_pointer_cast(&((*gpu_helper_->gpu_parent_)[0])),
+                                                                                  thrust::raw_pointer_cast(&((*gpu_helper_->gpu_aabb_min_)[0])),
+                                                                                  thrust::raw_pointer_cast(&((*gpu_helper_->gpu_aabb_max_)[0])),
+                                                                                  thrust::raw_pointer_cast( &((*gpu_helper_->gpu_points_)[0]) ),
+                                                                                  qd.get(),
+                                                                                  istride,
+                                                                                  ostride,
+                                                                                  id.get(),
+                                                                                  dd.get(),
+                                                                                  queries.rows, flann::cuda::SingleResultSet<float>(epsError),distance);
+            //                          KdTreeCudaPrivate::nearestKernel<<<blocksPerGrid, threadsPerBlock>>> (thrust::raw_pointer_cast(&((*gpu_helper_->gpu_nodes_)[0])),
+            //                                                                                                                                                                                                                                                                                          thrust::raw_pointer_cast( &((*gpu_helper_->gpu_points_)[0]) ),
+            //                                                                                                                                                                                                                                                                                          thrust::raw_pointer_cast(&queriesDev[0]),
+            //                                                                                                                                                                                                                                                                                          queries.stride,
+            //                                                                                                                                                                                                                                                                                          thrust::raw_pointer_cast(&indicesDev[0]),
+            //                                                                                                                                                                                                                                                                                          thrust::raw_pointer_cast(&distsDev[0]),
+            //                                                                                                                                                                                                                                                                                          queries.rows, epsError);
+            //
+        }
+        else {
+            if( use_heap ) {
+                KdTreeCudaPrivate::nearestKernel<<<blocksPerGrid, threadsPerBlock>>> (thrust::raw_pointer_cast(&((*gpu_helper_->gpu_splits_)[0])),
+                                                                                      thrust::raw_pointer_cast(&((*gpu_helper_->gpu_child1_)[0])),
+                                                                                      thrust::raw_pointer_cast(&((*gpu_helper_->gpu_parent_)[0])),
+                                                                                      thrust::raw_pointer_cast(&((*gpu_helper_->gpu_aabb_min_)[0])),
+                                                                                      thrust::raw_pointer_cast(&((*gpu_helper_->gpu_aabb_max_)[0])),
+                                                                                      thrust::raw_pointer_cast( &((*gpu_helper_->gpu_points_)[0]) ),
+                                                                                      qd.get(),
+                                                                                      istride,
+                                                                                      ostride,
+                                                                                      id.get(),
+                                                                                      dd.get(),
+                                                                                      queries.rows, flann::cuda::KnnResultSet<float, true>(knn,sorted,epsError)
+                                                                                      , distance);
+            }
+            else {
+                KdTreeCudaPrivate::nearestKernel<<<blocksPerGrid, threadsPerBlock>>> (thrust::raw_pointer_cast(&((*gpu_helper_->gpu_splits_)[0])),
+                                                                                      thrust::raw_pointer_cast(&((*gpu_helper_->gpu_child1_)[0])),
+                                                                                      thrust::raw_pointer_cast(&((*gpu_helper_->gpu_parent_)[0])),
+                                                                                      thrust::raw_pointer_cast(&((*gpu_helper_->gpu_aabb_min_)[0])),
+                                                                                      thrust::raw_pointer_cast(&((*gpu_helper_->gpu_aabb_max_)[0])),
+                                                                                      thrust::raw_pointer_cast( &((*gpu_helper_->gpu_points_)[0]) ),
+                                                                                      qd.get(),
+                                                                                      istride,
+                                                                                      ostride,
+                                                                                      id.get(),
+                                                                                      dd.get(),
+                                                                                      queries.rows, flann::cuda::KnnResultSet<float, false>(knn,sorted,epsError),
+                                                                                      distance
+                                                                                      );
+            }
+        }
+        thrust::transform(id, id+knn*queries.rows, id, map_indices(thrust::raw_pointer_cast( &((*gpu_helper_->gpu_vind_))[0]) ));
+    }
+}
+
+
+template< typename Distance>
+int KDTreeCuda3dIndex<Distance >::radiusSearchGpu(const Matrix<ElementType>& queries, std::vector< std::vector<int> >& indices,
+                                                  std::vector<std::vector<DistanceType> >& dists, float radius, const SearchParams& params) const
+{
+    //  assert(indices.roasdfws >= queries.rows);
+    //  assert(dists.rows >= queries.rows);
+
+    int max_neighbors = params.max_neighbors;
+    bool sorted = params.sorted;
+    bool use_heap = params.use_heap;
+    if (indices.size() < queries.rows ) indices.resize(queries.rows);
+    if (dists.size() < queries.rows ) dists.resize(queries.rows);
+    
+    int istride=queries.stride/sizeof(ElementType);
+
+    thrust::device_vector<float> queriesDev(istride* queries.rows,0);
+    thrust::copy( queries.ptr(), queries.ptr()+istride*queries.rows, queriesDev.begin() );
+    thrust::device_vector<int> countsDev(queries.rows);
+
+    typename GpuDistance<Distance>::type distance;
+
+    int threadsPerBlock = 128;
+    int blocksPerGrid=(queries.rows+threadsPerBlock-1)/threadsPerBlock;
+
+
+    KdTreeCudaPrivate::nearestKernel<<<blocksPerGrid, threadsPerBlock>>> (thrust::raw_pointer_cast(&((*gpu_helper_->gpu_splits_)[0])),
+                                                                          thrust::raw_pointer_cast(&((*gpu_helper_->gpu_child1_)[0])),
+                                                                          thrust::raw_pointer_cast(&((*gpu_helper_->gpu_parent_)[0])),
+                                                                          thrust::raw_pointer_cast(&((*gpu_helper_->gpu_aabb_min_)[0])),
+                                                                          thrust::raw_pointer_cast(&((*gpu_helper_->gpu_aabb_max_)[0])),
+                                                                          thrust::raw_pointer_cast( &((*gpu_helper_->gpu_points_)[0]) ),
+                                                                          thrust::raw_pointer_cast(&queriesDev[0]),
+                                                                          istride,
+                                                                          1,
+                                                                          thrust::raw_pointer_cast(&countsDev[0]),
+                                                                          0,
+                                                                          queries.rows, flann::cuda::CountingRadiusResultSet<float>(radius,max_neighbors),
+                                                                          distance
+                                                                          );
+
+    thrust::host_vector<int> counts_host=countsDev;
+
+    if( max_neighbors!=0 ) { // we'll need this later, but the exclusive_scan will change the array
+        for( size_t i=0; i<queries.rows; i++ ) {
+            int count = counts_host[i];
+            if( count > 0 ) {
+                indices[i].resize(count);
+                dists[i].resize(count);
+            }
+            else {
+                indices[i].clear();
+                dists[i].clear();
+            }
+
+        }
+    }
+
+    int neighbors_last_elem = countsDev.back();
+    thrust::exclusive_scan( countsDev.begin(), countsDev.end(), countsDev.begin() );
+
+    size_t total_neighbors=neighbors_last_elem+countsDev.back();
+    if( max_neighbors==0 ) return total_neighbors;
+
+    thrust::device_vector<int> indicesDev(total_neighbors,-1);
+    thrust::device_vector<float> distsDev(total_neighbors,std::numeric_limits<float>::infinity());
+
+    if( max_neighbors<0 ) {
+        KdTreeCudaPrivate::nearestKernel<<<blocksPerGrid, threadsPerBlock>>> (thrust::raw_pointer_cast(&((*gpu_helper_->gpu_splits_)[0])),
+                                                                              thrust::raw_pointer_cast(&((*gpu_helper_->gpu_child1_)[0])),
+                                                                              thrust::raw_pointer_cast(&((*gpu_helper_->gpu_parent_)[0])),
+                                                                              thrust::raw_pointer_cast(&((*gpu_helper_->gpu_aabb_min_)[0])),
+                                                                              thrust::raw_pointer_cast(&((*gpu_helper_->gpu_aabb_max_)[0])),
+                                                                              thrust::raw_pointer_cast( &((*gpu_helper_->gpu_points_)[0]) ),
+                                                                              thrust::raw_pointer_cast(&queriesDev[0]),
+                                                                              istride,
+                                                                              1,
+                                                                              thrust::raw_pointer_cast(&indicesDev[0]),
+                                                                              thrust::raw_pointer_cast(&distsDev[0]),
+                                                                              queries.rows, flann::cuda::RadiusResultSet<float>(radius,thrust::raw_pointer_cast(&countsDev[0]),sorted), distance);
+    }
+    else {
+        if( use_heap ) {
+            KdTreeCudaPrivate::nearestKernel<<<blocksPerGrid, threadsPerBlock>>> (thrust::raw_pointer_cast(&((*gpu_helper_->gpu_splits_)[0])),
+                                                                                  thrust::raw_pointer_cast(&((*gpu_helper_->gpu_child1_)[0])),
+                                                                                  thrust::raw_pointer_cast(&((*gpu_helper_->gpu_parent_)[0])),
+                                                                                  thrust::raw_pointer_cast(&((*gpu_helper_->gpu_aabb_min_)[0])),
+                                                                                  thrust::raw_pointer_cast(&((*gpu_helper_->gpu_aabb_max_)[0])),
+                                                                                  thrust::raw_pointer_cast( &((*gpu_helper_->gpu_points_)[0]) ),
+                                                                                  thrust::raw_pointer_cast(&queriesDev[0]),
+                                                                                  istride,
+                                                                                  1,
+                                                                                  thrust::raw_pointer_cast(&indicesDev[0]),
+                                                                                  thrust::raw_pointer_cast(&distsDev[0]),
+                                                                                  queries.rows, flann::cuda::RadiusKnnResultSet<float, true>(radius,max_neighbors, thrust::raw_pointer_cast(&countsDev[0]),sorted), distance);
+        }
+        else {
+            KdTreeCudaPrivate::nearestKernel<<<blocksPerGrid, threadsPerBlock>>> (thrust::raw_pointer_cast(&((*gpu_helper_->gpu_splits_)[0])),
+                                                                                  thrust::raw_pointer_cast(&((*gpu_helper_->gpu_child1_)[0])),
+                                                                                  thrust::raw_pointer_cast(&((*gpu_helper_->gpu_parent_)[0])),
+                                                                                  thrust::raw_pointer_cast(&((*gpu_helper_->gpu_aabb_min_)[0])),
+                                                                                  thrust::raw_pointer_cast(&((*gpu_helper_->gpu_aabb_max_)[0])),
+                                                                                  thrust::raw_pointer_cast( &((*gpu_helper_->gpu_points_)[0]) ),
+                                                                                  thrust::raw_pointer_cast(&queriesDev[0]),
+                                                                                  istride,
+                                                                                  1,
+                                                                                  thrust::raw_pointer_cast(&indicesDev[0]),
+                                                                                  thrust::raw_pointer_cast(&distsDev[0]),
+                                                                                  queries.rows, flann::cuda::RadiusKnnResultSet<float, false>(radius,max_neighbors, thrust::raw_pointer_cast(&countsDev[0]),sorted), distance);
+        }
+    }
+    thrust::transform(indicesDev.begin(), indicesDev.end(), indicesDev.begin(), map_indices(thrust::raw_pointer_cast( &((*gpu_helper_->gpu_vind_))[0]) ));
+    thrust::host_vector<int> indices_temp = indicesDev;
+    thrust::host_vector<float> dists_temp = distsDev;
+
+    int buffer_index=0;
+    for( size_t i=0; i<queries.rows; i++ ) {
+        for( size_t j=0; j<counts_host[i]; j++ ) {
+            dists[i][j]=dists_temp[buffer_index];
+            indices[i][j]=indices_temp[buffer_index];
+            ++buffer_index;
+        }
+    }
+
+    return buffer_index;
+}
+
+//! used in the radius search to count the total number of neighbors
+struct isNotMinusOne
+{
+    __host__ __device__
+    bool operator() ( int i ){
+        return i!=-1;
+    }
+};
+
+template< typename Distance>
+int KDTreeCuda3dIndex< Distance >::radiusSearchGpu(const Matrix<ElementType>& queries, Matrix<int>& indices, Matrix<DistanceType>& dists, float radius, const SearchParams& params) const
+{
+	int max_neighbors = params.max_neighbors;
+    assert(indices.rows >= queries.rows);
+    assert(dists.rows >= queries.rows || max_neighbors==0 );
+    assert(indices.stride==dists.stride  || max_neighbors==0 );
+    assert( indices.cols==indices.stride/sizeof(int) );
+    assert(dists.rows >= queries.rows || max_neighbors==0 );
+
+    bool sorted = params.sorted;
+    bool matrices_on_gpu = params.matrices_in_gpu_ram;
+    float epsError = 1+params.eps;
+    bool use_heap = params.use_heap;
+    int istride=queries.stride/sizeof(ElementType);
+    int ostride= indices.stride/4;
+
+
+    if( max_neighbors<0 ) max_neighbors=indices.cols;
+
+    if( !matrices_on_gpu ) {
+        thrust::device_vector<float> queriesDev(istride* queries.rows,0);
+        thrust::copy( queries.ptr(), queries.ptr()+istride*queries.rows, queriesDev.begin() );
+        typename GpuDistance<Distance>::type distance;
+        int threadsPerBlock = 128;
+        int blocksPerGrid=(queries.rows+threadsPerBlock-1)/threadsPerBlock;
+        if( max_neighbors== 0 ) {
+            thrust::device_vector<int> indicesDev(queries.rows* ostride);
+            KdTreeCudaPrivate::nearestKernel<<<blocksPerGrid, threadsPerBlock>>> (thrust::raw_pointer_cast(&((*gpu_helper_->gpu_splits_)[0])),
+                                                                                  thrust::raw_pointer_cast(&((*gpu_helper_->gpu_child1_)[0])),
+                                                                                  thrust::raw_pointer_cast(&((*gpu_helper_->gpu_parent_)[0])),
+                                                                                  thrust::raw_pointer_cast(&((*gpu_helper_->gpu_aabb_min_)[0])),
+                                                                                  thrust::raw_pointer_cast(&((*gpu_helper_->gpu_aabb_max_)[0])),
+                                                                                  thrust::raw_pointer_cast( &((*gpu_helper_->gpu_points_)[0]) ),
+                                                                                  thrust::raw_pointer_cast(&queriesDev[0]),
+                                                                                  istride,
+                                                                                  ostride,
+                                                                                  thrust::raw_pointer_cast(&indicesDev[0]),
+                                                                                  0,
+                                                                                  queries.rows, flann::cuda::CountingRadiusResultSet<float>(radius,-1),
+                                                                                  distance
+                                                                                  );
+            thrust::copy( indicesDev.begin(), indicesDev.end(), indices.ptr() );
+            return thrust::reduce(indicesDev.begin(), indicesDev.end() );
+        }
+
+
+        thrust::device_vector<float> distsDev(queries.rows* max_neighbors);
+        thrust::device_vector<int> indicesDev(queries.rows* max_neighbors);
+
+        if( use_heap ) {
+            KdTreeCudaPrivate::nearestKernel<<<blocksPerGrid, threadsPerBlock>>> (thrust::raw_pointer_cast(&((*gpu_helper_->gpu_splits_)[0])),
+                                                                                  thrust::raw_pointer_cast(&((*gpu_helper_->gpu_child1_)[0])),
+                                                                                  thrust::raw_pointer_cast(&((*gpu_helper_->gpu_parent_)[0])),
+                                                                                  thrust::raw_pointer_cast(&((*gpu_helper_->gpu_aabb_min_)[0])),
+                                                                                  thrust::raw_pointer_cast(&((*gpu_helper_->gpu_aabb_max_)[0])),
+                                                                                  thrust::raw_pointer_cast( &((*gpu_helper_->gpu_points_)[0]) ),
+                                                                                  thrust::raw_pointer_cast(&queriesDev[0]),
+                                                                                  istride,
+                                                                                  ostride,
+                                                                                  thrust::raw_pointer_cast(&indicesDev[0]),
+                                                                                  thrust::raw_pointer_cast(&distsDev[0]),
+                                                                                  queries.rows, flann::cuda::KnnRadiusResultSet<float, true>(max_neighbors,sorted,epsError, radius), distance);
+        }
+        else {
+            KdTreeCudaPrivate::nearestKernel<<<blocksPerGrid, threadsPerBlock>>> (thrust::raw_pointer_cast(&((*gpu_helper_->gpu_splits_)[0])),
+                                                                                  thrust::raw_pointer_cast(&((*gpu_helper_->gpu_child1_)[0])),
+                                                                                  thrust::raw_pointer_cast(&((*gpu_helper_->gpu_parent_)[0])),
+                                                                                  thrust::raw_pointer_cast(&((*gpu_helper_->gpu_aabb_min_)[0])),
+                                                                                  thrust::raw_pointer_cast(&((*gpu_helper_->gpu_aabb_max_)[0])),
+                                                                                  thrust::raw_pointer_cast( &((*gpu_helper_->gpu_points_)[0]) ),
+                                                                                  thrust::raw_pointer_cast(&queriesDev[0]),
+                                                                                  istride,
+                                                                                  ostride,
+                                                                                  thrust::raw_pointer_cast(&indicesDev[0]),
+                                                                                  thrust::raw_pointer_cast(&distsDev[0]),
+                                                                                  queries.rows, flann::cuda::KnnRadiusResultSet<float, false>(max_neighbors,sorted,epsError, radius), distance);
+        }
+
+        thrust::copy( distsDev.begin(), distsDev.end(), dists.ptr() );
+        thrust::transform(indicesDev.begin(), indicesDev.end(), indicesDev.begin(), map_indices(thrust::raw_pointer_cast( &((*gpu_helper_->gpu_vind_))[0]) ));
+        thrust::copy( indicesDev.begin(), indicesDev.end(), indices.ptr() );
+
+        return thrust::count_if(indicesDev.begin(), indicesDev.end(), isNotMinusOne() );
+    }
+    else {
+
+        thrust::device_ptr<float> qd=thrust::device_pointer_cast(queries.ptr());
+        thrust::device_ptr<float> dd=thrust::device_pointer_cast(dists.ptr());
+        thrust::device_ptr<int> id=thrust::device_pointer_cast(indices.ptr());
+        typename GpuDistance<Distance>::type distance;
+        int threadsPerBlock = 128;
+        int blocksPerGrid=(queries.rows+threadsPerBlock-1)/threadsPerBlock;
+
+        if( max_neighbors== 0 ) {
+            thrust::device_vector<int> indicesDev(queries.rows* indices.stride);
+            KdTreeCudaPrivate::nearestKernel<<<blocksPerGrid, threadsPerBlock>>> (thrust::raw_pointer_cast(&((*gpu_helper_->gpu_splits_)[0])),
+                                                                                  thrust::raw_pointer_cast(&((*gpu_helper_->gpu_child1_)[0])),
+                                                                                  thrust::raw_pointer_cast(&((*gpu_helper_->gpu_parent_)[0])),
+                                                                                  thrust::raw_pointer_cast(&((*gpu_helper_->gpu_aabb_min_)[0])),
+                                                                                  thrust::raw_pointer_cast(&((*gpu_helper_->gpu_aabb_max_)[0])),
+                                                                                  thrust::raw_pointer_cast( &((*gpu_helper_->gpu_points_)[0]) ),
+                                                                                  qd.get(),
+                                                                                  istride,
+                                                                                  ostride,
+                                                                                  id.get(),
+                                                                                  0,
+                                                                                  queries.rows, flann::cuda::CountingRadiusResultSet<float>(radius,-1),
+                                                                                  distance
+                                                                                  );
+            thrust::copy( indicesDev.begin(), indicesDev.end(), indices.ptr() );
+            return thrust::reduce(indicesDev.begin(), indicesDev.end() );
+        }
+
+        if( use_heap ) {
+            KdTreeCudaPrivate::nearestKernel<<<blocksPerGrid, threadsPerBlock>>> (thrust::raw_pointer_cast(&((*gpu_helper_->gpu_splits_)[0])),
+                                                                                  thrust::raw_pointer_cast(&((*gpu_helper_->gpu_child1_)[0])),
+                                                                                  thrust::raw_pointer_cast(&((*gpu_helper_->gpu_parent_)[0])),
+                                                                                  thrust::raw_pointer_cast(&((*gpu_helper_->gpu_aabb_min_)[0])),
+                                                                                  thrust::raw_pointer_cast(&((*gpu_helper_->gpu_aabb_max_)[0])),
+                                                                                  thrust::raw_pointer_cast( &((*gpu_helper_->gpu_points_)[0]) ),
+                                                                                  qd.get(),
+                                                                                  istride,
+                                                                                  ostride,
+                                                                                  id.get(),
+                                                                                  dd.get(),
+                                                                                  queries.rows, flann::cuda::KnnRadiusResultSet<float, true>(max_neighbors,sorted,epsError, radius), distance);
+        }
+        else {
+            KdTreeCudaPrivate::nearestKernel<<<blocksPerGrid, threadsPerBlock>>> (thrust::raw_pointer_cast(&((*gpu_helper_->gpu_splits_)[0])),
+                                                                                  thrust::raw_pointer_cast(&((*gpu_helper_->gpu_child1_)[0])),
+                                                                                  thrust::raw_pointer_cast(&((*gpu_helper_->gpu_parent_)[0])),
+                                                                                  thrust::raw_pointer_cast(&((*gpu_helper_->gpu_aabb_min_)[0])),
+                                                                                  thrust::raw_pointer_cast(&((*gpu_helper_->gpu_aabb_max_)[0])),
+                                                                                  thrust::raw_pointer_cast( &((*gpu_helper_->gpu_points_)[0]) ),
+                                                                                  qd.get(),
+                                                                                  istride,
+                                                                                  ostride,
+                                                                                  id.get(),
+                                                                                  dd.get(),
+                                                                                  queries.rows, flann::cuda::KnnRadiusResultSet<float, false>(max_neighbors,sorted,epsError, radius), distance);
+        }
+
+        thrust::transform(id, id+max_neighbors*queries.rows, id, map_indices(thrust::raw_pointer_cast( &((*gpu_helper_->gpu_vind_))[0]) ));
+
+        return thrust::count_if(id, id+max_neighbors*queries.rows, isNotMinusOne() );
+    }
+}
+
+template<typename Distance>
+void KDTreeCuda3dIndex<Distance>::uploadTreeToGpu()
+{
+    // just make sure that no weird alignment stuff is going on...
+    // shouldn't, but who knows
+    // (I would make this a (boost) static assertion, but so far flann seems to avoid boost
+    //  assert( sizeof( KdTreeCudaPrivate::GpuNode)==sizeof( Node ) );
+    delete gpu_helper_;
+    gpu_helper_ = new GpuHelper;
+    gpu_helper_->gpu_points_=new thrust::device_vector<float4>(size_);
+    thrust::device_vector<float4> tmp(size_);
+    if( get_param(index_params_,"input_is_gpu_float4",false) ) {
+		assert( dataset_.cols == 3 && dataset_.stride==4*sizeof(float));
+        thrust::copy( thrust::device_pointer_cast((float4*)dataset_.ptr()),thrust::device_pointer_cast((float4*)(dataset_.ptr()))+size_,tmp.begin());
+        
+    }
+    else {
+        // k is limited to 4 -> use 128bit-alignment regardless of dimensionality
+        // makes cpu search about 5% slower, but gpu can read a float4 w/ a single instruction
+        // (vs a float2 and a float load for a float3 value)
+        // pad data directly to avoid having to copy and re-format the data when
+        // copying it to the GPU
+        data_ = flann::Matrix<ElementType>(new ElementType[size_*4], size_, dim_,4*4);
+        for (size_t i=0; i<size_; ++i) {
+            for (size_t j=0; j<dim_; ++j) {
+                data_[i][j] = dataset_[i][j];
+            }
+            for (size_t j=dim_; j<4; ++j) {
+                data_[i][j] = 0;
+            }
+        }
+        thrust::copy((float4*)data_.ptr(),(float4*)(data_.ptr())+size_,tmp.begin());
+    }
+
+    CudaKdTreeBuilder builder( tmp, leaf_max_size_ );
+    builder.buildTree();
+
+    gpu_helper_->gpu_splits_ = builder.splits_;
+    gpu_helper_->gpu_aabb_min_ = builder.aabb_min_;
+    gpu_helper_->gpu_aabb_max_ = builder.aabb_max_;
+    gpu_helper_->gpu_child1_ = builder.child1_;
+    gpu_helper_->gpu_parent_=builder.parent_;
+    gpu_helper_->gpu_vind_=builder.index_x_;
+    thrust::gather( builder.index_x_->begin(), builder.index_x_->end(), tmp.begin(), gpu_helper_->gpu_points_->begin());
+
+    //  gpu_helper_->gpu_nodes_=new thrust::device_vector<KdTreeCudaPrivate::GpuNode>(node_count_);
+
+
+    //  gpu_helper_->gpu_vind_=new thrust::device_vector<int>(size_);
+    //  thrust::copy( (KdTreeCudaPrivate::GpuNode*)&(tree_[0]), ((KdTreeCudaPrivate::GpuNode*)&(tree_[0]))+tree_.size(),  gpu_helper_->gpu_nodes_->begin());
+
+    //  thrust::copy(vind_.begin(),vind_.end(),gpu_helper_->gpu_vind_->begin());
+
+    //  buildGpuTree();
+}
+
+
+template<typename Distance>
+void KDTreeCuda3dIndex<Distance>::clearGpuBuffers()
+{
+    delete gpu_helper_;
+    gpu_helper_=0;
+}
+
+// explicit instantiations for distance-independent functions
+template
+void KDTreeCuda3dIndex<flann::L2<float> >::uploadTreeToGpu();
+
+template
+void KDTreeCuda3dIndex<flann::L2<float> >::clearGpuBuffers();
+
+template
+struct KDTreeCuda3dIndex<flann::L2<float> >::GpuHelper;
+
+template
+void KDTreeCuda3dIndex<flann::L2<float> >::knnSearchGpu(const Matrix<ElementType>& queries, Matrix<int>& indices, Matrix<DistanceType>& dists, size_t knn, const SearchParams& params) const;
+
+template
+int KDTreeCuda3dIndex< flann::L2<float> >::radiusSearchGpu(const Matrix<ElementType>& queries, Matrix<int>& indices, Matrix<DistanceType>& dists, float radius, const SearchParams& params) const;
+template
+int KDTreeCuda3dIndex< flann::L2<float> >::radiusSearchGpu(const Matrix<ElementType>& queries, std::vector< std::vector<int> >& indices,
+                                                           std::vector<std::vector<DistanceType> >& dists, float radius, const SearchParams& params) const;
+
+// explicit instantiations for distance-independent functions
+template
+void KDTreeCuda3dIndex<flann::L2_Simple<float> >::uploadTreeToGpu();
+
+template
+void KDTreeCuda3dIndex<flann::L2_Simple<float> >::clearGpuBuffers();
+
+template
+struct KDTreeCuda3dIndex<flann::L2_Simple<float> >::GpuHelper;
+
+template
+void KDTreeCuda3dIndex<flann::L2_Simple<float> >::knnSearchGpu(const Matrix<ElementType>& queries, Matrix<int>& indices, Matrix<DistanceType>& dists, size_t knn, const SearchParams& params) const;
+
+template
+int KDTreeCuda3dIndex< flann::L2_Simple<float> >::radiusSearchGpu(const Matrix<ElementType>& queries, Matrix<int>& indices, Matrix<DistanceType>& dists, float radius, const SearchParams& params) const;
+template
+int KDTreeCuda3dIndex< flann::L2_Simple<float> >::radiusSearchGpu(const Matrix<ElementType>& queries, std::vector< std::vector<int> >& indices,
+                                                                  std::vector<std::vector<DistanceType> >& dists, float radius, const SearchParams& params) const;
+
+
+// explicit instantiations for distance-independent functions
+template
+void KDTreeCuda3dIndex<flann::L1<float> >::uploadTreeToGpu();
+
+template
+void KDTreeCuda3dIndex<flann::L1<float> >::clearGpuBuffers();
+
+template
+struct KDTreeCuda3dIndex<flann::L1<float> >::GpuHelper;
+
+template
+void KDTreeCuda3dIndex<flann::L1<float> >::knnSearchGpu(const Matrix<ElementType>& queries, Matrix<int>& indices, Matrix<DistanceType>& dists, size_t knn, const SearchParams& params) const;
+
+template
+int KDTreeCuda3dIndex< flann::L1<float> >::radiusSearchGpu(const Matrix<ElementType>& queries, Matrix<int>& indices, Matrix<DistanceType>& dists, float radius, const SearchParams& params) const;
+template
+int KDTreeCuda3dIndex< flann::L1<float> >::radiusSearchGpu(const Matrix<ElementType>& queries, std::vector< std::vector<int> >& indices,
+                                                           std::vector<std::vector<DistanceType> >& dists, float radius, const SearchParams& params) const;
+}
diff --git a/src/cpp/flann/algorithms/kdtree_cuda_3d_index.h b/src/cpp/flann/algorithms/kdtree_cuda_3d_index.h
new file mode 100644
index 0000000..897a316
--- /dev/null
+++ b/src/cpp/flann/algorithms/kdtree_cuda_3d_index.h
@@ -0,0 +1,327 @@
+/***********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2008-2009  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+ * Copyright 2008-2009  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+ * Copyright 2011       Andreas Muetzel (amuetzel at uni-koblenz.de). All rights reserved.
+ *
+ * THE BSD LICENSE
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+
+#ifndef FLANN_KDTREE_CUDA_3D_INDEX_H_
+#define FLANN_KDTREE_CUDA_3D_INDEX_H_
+
+#include <algorithm>
+#include <map>
+#include <cassert>
+#include <cstring>
+#include "flann/general.h"
+#include "flann/algorithms/nn_index.h"
+#include "flann/util/matrix.h"
+#include "flann/util/result_set.h"
+#include "flann/util/heap.h"
+#include "flann/util/allocator.h"
+#include "flann/util/random.h"
+#include "flann/util/saving.h"
+#include "flann/util/params.h"
+
+namespace flann
+{
+
+struct KDTreeCuda3dIndexParams : public IndexParams
+{
+    KDTreeCuda3dIndexParams( int leaf_max_size = 64 )
+    {
+        (*this)["algorithm"] = FLANN_INDEX_KDTREE_CUDA;
+        (*this)["leaf_max_size"] = leaf_max_size;
+        (*this)["dim"] = 3;
+    }
+};
+
+/**
+ * Cuda KD Tree.
+ * Tree is built with GPU assistance and search is performed on the GPU, too.
+ *
+ * Usually faster than the CPU search for data (and query) sets larger than 250000-300000 points, depending
+ * on your CPU and GPU.
+ */
+template <typename Distance>
+class KDTreeCuda3dIndex : public NNIndex<Distance>
+{
+public:
+    typedef typename Distance::ElementType ElementType;
+    typedef typename Distance::ResultType DistanceType;
+
+    typedef NNIndex<Distance> BaseClass;
+
+    int visited_leafs;
+
+    typedef bool needs_kdtree_distance;
+
+    /**
+     * KDTree constructor
+     *
+     * Params:
+     *          inputData = dataset with the input features
+     *          params = parameters passed to the kdtree algorithm
+     */
+    KDTreeCuda3dIndex(const Matrix<ElementType>& inputData, const IndexParams& params = KDTreeCuda3dIndexParams(),
+                      Distance d = Distance() ) : BaseClass(params,d), dataset_(inputData), leaf_count_(0), visited_leafs(0), node_count_(0), current_node_count_(0)
+    {
+        size_ = dataset_.rows;
+        dim_ = dataset_.cols;
+
+        int dim_param = get_param(params,"dim",-1);
+        if (dim_param>0) dim_ = dim_param;
+        leaf_max_size_ = get_param(params,"leaf_max_size",10);
+        assert( dim_ == 3 );
+        gpu_helper_=0;
+    }
+
+    KDTreeCuda3dIndex(const KDTreeCuda3dIndex& other);
+    KDTreeCuda3dIndex operator=(KDTreeCuda3dIndex other);
+
+    /**
+     * Standard destructor
+     */
+    ~KDTreeCuda3dIndex()
+    {
+        delete[] data_.ptr();
+        clearGpuBuffers();
+    }
+
+    BaseClass* clone() const
+    {
+    	throw FLANNException("KDTreeCuda3dIndex cloning is not implemented");
+    }
+
+    /**
+     * Builds the index
+     */
+    void buildIndex()
+    {
+        // Create a permutable array of indices to the input vectors.
+        vind_.resize(size_);
+        for (size_t i = 0; i < size_; i++) {
+            vind_[i] = i;
+        }
+
+        leaf_count_=0;
+        node_count_=0;
+        //         computeBoundingBox(root_bbox_);
+        //                              tree_.reserve(log2((double)size_/leaf_max_size_));
+        //         divideTree(0, size_, root_bbox_,-1 );   // construct the tree
+
+        delete[] data_.ptr();
+
+        uploadTreeToGpu();
+    }
+
+    flann_algorithm_t getType() const
+    {
+        return FLANN_INDEX_KDTREE_SINGLE;
+    }
+
+
+    void removePoint(size_t index)
+    {
+    	throw FLANNException( "removePoint not implemented for this index type!" );
+    }
+
+    ElementType* getPoint(size_t id)
+    {
+    	return dataset_[id];
+    }
+
+    void saveIndex(FILE* stream)
+    {
+        throw FLANNException( "Index saving not implemented!" );
+
+    }
+
+
+    void loadIndex(FILE* stream)
+    {
+        throw FLANNException( "Index loading not implemented!" );
+    }
+
+    size_t veclen() const
+    {
+        return dim_;
+    }
+
+    /**
+     * Computes the inde memory usage
+     * Returns: memory used by the index
+     * TODO: return system or gpu RAM or both?
+     */
+    int usedMemory() const
+    {
+        //         return tree_.size()*sizeof(Node)+dataset_.rows*sizeof(int);  // pool memory and vind array memory
+        return 0;
+    }
+
+
+    /**
+     * \brief Perform k-nearest neighbor search
+     * \param[in] queries The query points for which to find the nearest neighbors
+     * \param[out] indices The indices of the nearest neighbors found
+     * \param[out] dists Distances to the nearest neighbors found
+     * \param[in] knn Number of nearest neighbors to return
+     * \param[in] params Search parameters
+     */
+    int knnSearch(const Matrix<ElementType>& queries, Matrix<int>& indices, Matrix<DistanceType>& dists, size_t knn, const SearchParams& params) const
+    {
+    	knnSearchGpu(queries,indices, dists, knn, params);
+        return knn*queries.rows; // hack...
+    }
+
+    /**
+     * \brief Perform k-nearest neighbor search
+     * \param[in] queries The query points for which to find the nearest neighbors
+     * \param[out] indices The indices of the nearest neighbors found
+     * \param[out] dists Distances to the nearest neighbors found
+     * \param[in] knn Number of nearest neighbors to return
+     * \param[in] params Search parameters
+     */
+    int knnSearch(const Matrix<ElementType>& queries,
+                          std::vector< std::vector<int> >& indices,
+                          std::vector<std::vector<DistanceType> >& dists,
+                          size_t knn,
+                          const SearchParams& params) const
+    {
+    	knnSearchGpu(queries,indices, dists, knn, params);
+        return knn*queries.rows; // hack...
+    }
+
+    /**
+     * \brief Perform k-nearest neighbor search
+     * \param[in] queries The query points for which to find the nearest neighbors
+     * \param[out] indices The indices of the nearest neighbors found
+     * \param[out] dists Distances to the nearest neighbors found
+     * \param[in] knn Number of nearest neighbors to return
+     * \param[in] params Search parameters
+     */
+    void knnSearchGpu(const Matrix<ElementType>& queries, Matrix<int>& indices, Matrix<DistanceType>& dists, size_t knn, const SearchParams& params) const;
+
+    int knnSearchGpu(const Matrix<ElementType>& queries,
+                     std::vector< std::vector<int> >& indices,
+                     std::vector<std::vector<DistanceType> >& dists,
+                     size_t knn,
+                     const SearchParams& params) const
+    {
+        flann::Matrix<int> ind( new int[knn*queries.rows], queries.rows,knn);
+        flann::Matrix<DistanceType> dist( new DistanceType[knn*queries.rows], queries.rows,knn);
+        knnSearchGpu(queries,ind,dist,knn,params);
+        for( size_t i = 0; i<queries.rows; i++ ) {
+            indices[i].resize(knn);
+            dists[i].resize(knn);
+            for( size_t j=0; j<knn; j++ ) {
+                indices[i][j]=ind[i][j];
+                dists[i][j]=dist[i][j];
+            }
+        }
+        delete [] ind.ptr();
+        delete [] dist.ptr();
+        return knn*queries.rows; // hack...
+    }
+
+    int radiusSearch(const Matrix<ElementType>& queries, Matrix<int>& indices, Matrix<DistanceType>& dists,
+                             float radius, const SearchParams& params) const
+    {
+    	return radiusSearchGpu(queries,indices, dists, radius, params);
+    }
+
+    int radiusSearch(const Matrix<ElementType>& queries, std::vector< std::vector<int> >& indices,
+                             std::vector<std::vector<DistanceType> >& dists, float radius, const SearchParams& params) const
+    {
+    	return radiusSearchGpu(queries,indices, dists, radius, params);
+    }
+
+    int radiusSearchGpu(const Matrix<ElementType>& queries, Matrix<int>& indices, Matrix<DistanceType>& dists,
+                        float radius, const SearchParams& params) const;
+
+    int radiusSearchGpu(const Matrix<ElementType>& queries, std::vector< std::vector<int> >& indices,
+                        std::vector<std::vector<DistanceType> >& dists, float radius, const SearchParams& params) const;
+
+    /**
+     * Not implemented, since it is only used by single-element searches.
+     * (but is needed b/c it is abstract in the base class)
+     */
+    void findNeighbors(ResultSet<DistanceType>& result, const ElementType* vec, const SearchParams& searchParams) const
+    {
+    }
+
+protected:
+    void buildIndexImpl()
+    {
+        /* nothing to do here */
+    }
+
+    void freeIndex()
+    {
+        /* nothing to do here */
+    }
+
+private:
+
+    void uploadTreeToGpu( );
+
+    void clearGpuBuffers( );
+
+
+
+
+private:
+
+    struct GpuHelper;
+
+    GpuHelper* gpu_helper_;
+
+    const Matrix<ElementType> dataset_;
+
+    int leaf_max_size_;
+
+    int leaf_count_;
+    int node_count_;
+    //! used by convertTreeToGpuFormat
+    int current_node_count_;
+
+
+    /**
+     *  Array of indices to vectors in the dataset.
+     */
+    std::vector<int> vind_;
+
+    Matrix<ElementType> data_;
+
+    size_t dim_;
+
+    USING_BASECLASS_SYMBOLS
+};   // class KDTreeCuda3dIndex
+
+
+}
+
+#endif //FLANN_KDTREE_SINGLE_INDEX_H_
diff --git a/src/cpp/flann/algorithms/kdtree_cuda_builder.h b/src/cpp/flann/algorithms/kdtree_cuda_builder.h
new file mode 100644
index 0000000..8bb4e83
--- /dev/null
+++ b/src/cpp/flann/algorithms/kdtree_cuda_builder.h
@@ -0,0 +1,729 @@
+/***********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2011       Andreas Muetzel (amuetzel at uni-koblenz.de). All rights reserved.
+ *
+ * THE BSD LICENSE
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+
+#ifndef FLANN_CUDA_KD_TREE_BUILDER_H_
+#define FLANN_CUDA_KD_TREE_BUILDER_H_
+#include <thrust/host_vector.h>
+#include <thrust/device_vector.h>
+#include <thrust/sort.h>
+#include <thrust/partition.h>
+#include <thrust/unique.h>
+#include <thrust/scan.h>
+#include <flann/util/cutil_math.h>
+#include <stdlib.h>
+
+// #define PRINT_DEBUG_TIMING
+
+namespace flann
+{
+//      template< typename T >
+//      void print_vector(  const thrust::device_vector<T>& v )
+//      {
+//              for( int i=0; i< v.size(); i++ )
+//              {
+//                      std::cout<<v[i]<<std::endl;
+//              }
+//      }
+//
+//      template< typename T1, typename T2 >
+//      void print_vector(  const thrust::device_vector<T1>& v1, const thrust::device_vector<T2>& v2 )
+//      {
+//              for( int i=0; i< v1.size(); i++ )
+//              {
+//                      std::cout<<i<<": "<<v1[i]<<" "<<v2[i]<<std::endl;
+//              }
+//      }
+//
+//      template< typename T1, typename T2, typename T3 >
+//      void print_vector(  const thrust::device_vector<T1>& v1, const thrust::device_vector<T2>& v2, const thrust::device_vector<T3>& v3 )
+//      {
+//              for( int i=0; i< v1.size(); i++ )
+//              {
+//                      std::cout<<i<<": "<<v1[i]<<" "<<v2[i]<<" "<<v3[i]<<std::endl;
+//              }
+//      }
+//
+//      template< typename T >
+//      void print_vector_by_index(  const thrust::device_vector<T>& v,const thrust::device_vector<int>& ind )
+//      {
+//              for( int i=0; i< v.size(); i++ )
+//              {
+//                      std::cout<<v[ind[i]]<<std::endl;
+//              }
+//      }
+
+//      std::ostream& operator <<(std::ostream& stream, const cuda::kd_tree_builder_detail::SplitInfo& s) {
+//      stream<<"(split l/r: "<< s.left <<" "<< s.right<< "  split:"<<s.split_dim<<" "<<s.split_val<<")";
+//      return stream;
+// }
+//
+//
+// std::ostream& operator <<(std::ostream& stream, const cuda::kd_tree_builder_detail::NodeInfo& s) {
+//      stream<<"(node: "<<s.child1()<<" "<<s.parent()<<" "<<s.child2()<<")";
+//      return stream;
+// }
+//
+// std::ostream& operator <<(std::ostream& stream, const float4& s) {
+//      stream<<"("<<s.x<<","<<s.y<<","<<s.z<<","<<s.w<<")";
+//      return stream;
+// }
+namespace cuda
+{
+namespace kd_tree_builder_detail
+{
+//! normal node: contains the split dimension and value
+//! leaf node: left == index of first points, right==index of last point +1
+struct SplitInfo
+{
+    union {
+        struct
+        {
+            // begin of child nodes
+            int left;
+            // end of child nodes
+            int right;
+        };
+        struct
+        {
+            int split_dim;
+            float split_val;
+        };
+    };
+
+};
+
+struct IsEven
+{
+    typedef int result_type;
+    __device__
+    int operator()(int i )
+    {
+        return (i& 1)==0;
+    }
+};
+
+struct SecondElementIsEven
+{
+    __host__ __device__
+    bool operator()( const thrust::tuple<int,int>& i )
+    {
+        return (thrust::get<1>(i)& 1)==0;
+    }
+};
+
+//! just for convenience: access a float4 by an index in [0,1,2]
+//! (casting it to a float* and accessing it by the index is way slower...)
+__host__ __device__
+float get_value_by_index( const float4& f, int i )
+{
+    switch(i) {
+    case 0:
+        return f.x;
+    case 1:
+        return f.y;
+    default:
+        return f.z;
+    }
+
+}
+
+//! mark a point as belonging to the left or right child of its current parent
+//! called after parents are split
+struct MovePointsToChildNodes
+{
+    MovePointsToChildNodes( int* child1, SplitInfo* splits, float* x, float* y, float* z, int* ox, int* oy, int* oz, int* lrx, int* lry, int* lrz )
+        : child1_(child1), splits_(splits), x_(x), y_(y), z_(z), ox_(ox), oy_(oy), oz_(oz), lrx_(lrx), lry_(lry), lrz_(lrz){}
+    //  int dim;
+    //  float threshold;
+    int* child1_;
+    SplitInfo* splits_;
+
+    // coordinate values
+    float* x_, * y_, * z_;
+    // owner indices -> which node does the point belong to?
+    int* ox_, * oy_, * oz_;
+    // temp info: will be set to 1 of a point is moved to the right child node, 0 otherwise
+    // (used later in the scan op to separate the points of the children into continuous ranges)
+    int* lrx_, * lry_, * lrz_;
+    __device__
+    void operator()( const thrust::tuple<int, int, int, int>& data )
+    {
+        int index = thrust::get<0>(data);
+        int owner = ox_[index]; // before a split, all points at the same position in the index array have the same owner
+        int point_ind1=thrust::get<1>(data);
+        int point_ind2=thrust::get<2>(data);
+        int point_ind3=thrust::get<3>(data);
+        int leftChild=child1_[owner];
+        int split_dim;
+        float dim_val1, dim_val2, dim_val3;
+        SplitInfo split;
+        lrx_[index]=0;
+        lry_[index]=0;
+        lrz_[index]=0;
+        // this element already belongs to a leaf node -> everything alright, no need to change anything
+        if( leftChild==-1 ) {
+            return;
+        }
+        // otherwise: load split data, and assign this index to the new owner
+        split = splits_[owner];
+        split_dim=split.split_dim;
+        switch( split_dim ) {
+        case 0:
+            dim_val1=x_[point_ind1];
+            dim_val2=x_[point_ind2];
+            dim_val3=x_[point_ind3];
+            break;
+        case 1:
+            dim_val1=y_[point_ind1];
+            dim_val2=y_[point_ind2];
+            dim_val3=y_[point_ind3];
+            break;
+        default:
+            dim_val1=z_[point_ind1];
+            dim_val2=z_[point_ind2];
+            dim_val3=z_[point_ind3];
+            break;
+
+        }
+
+
+        int r1=leftChild +(dim_val1 > split.split_val);
+        ox_[index]=r1;
+        int r2=leftChild+(dim_val2 > split.split_val);
+        oy_[index]=r2;
+        oz_[index]=leftChild+(dim_val3 > split.split_val);
+
+        lrx_[index] = (dim_val1 > split.split_val);
+        lry_[index] = (dim_val2 > split.split_val);
+        lrz_[index] = (dim_val3 > split.split_val);
+        //                      return thrust::make_tuple( r1, r2, leftChild+(dim_val > split.split_val) );
+    }
+};
+
+//! used to update the left/right pointers and aabb infos after the node splits
+struct SetLeftAndRightAndAABB
+{
+    int maxPoints;
+    int nElements;
+
+    SplitInfo* nodes;
+    int* counts;
+    int* labels;
+    float4* aabbMin;
+    float4* aabbMax;
+    const float* x,* y,* z;
+    const int* ix, * iy, * iz;
+
+    __host__ __device__
+    void operator()( int i )
+    {
+        int index=labels[i];
+        int right;
+        int left = counts[i];
+        nodes[index].left=left;
+        if( i < nElements-1 ) {
+            right=counts[i+1];
+        }
+        else { // index==nNodes
+            right=maxPoints;
+        }
+        nodes[index].right=right;
+        aabbMin[index].x=x[ix[left]];
+        aabbMin[index].y=y[iy[left]];
+        aabbMin[index].z=z[iz[left]];
+        aabbMax[index].x=x[ix[right-1]];
+        aabbMax[index].y=y[iy[right-1]];
+        aabbMax[index].z=z[iz[right-1]];
+    }
+};
+
+
+//! - decide whether a node has to be split
+//! if yes:
+//! - allocate child nodes
+//! - set split axis as axis of maximum aabb length
+struct SplitNodes
+{
+    int maxPointsPerNode;
+    int* node_count;
+    int* nodes_allocated;
+    int* out_of_space;
+    int* child1_;
+    int* parent_;
+    SplitInfo* splits;
+
+    __device__
+    void operator()( thrust::tuple<int&, int&,SplitInfo&,float4&,float4&, int> node ) // float4: aabbMin, aabbMax
+    {
+        int& parent=thrust::get<0>(node);
+        int& child1=thrust::get<1>(node);
+        SplitInfo& s=thrust::get<2>(node);
+        const float4& aabbMin=thrust::get<3>(node);
+        const float4& aabbMax=thrust::get<4>(node);
+        int my_index = thrust::get<5>(node);
+        bool split_node=false;
+        // first, each thread block counts the number of nodes that it needs to allocate...
+        __shared__ int block_nodes_to_allocate;
+        if( threadIdx.x== 0 ) block_nodes_to_allocate=0;
+        __syncthreads();
+
+        // don't split if all points are equal
+        // (could lead to an infinite loop, and doesn't make any sense anyway)
+        bool all_points_in_node_are_equal=aabbMin.x == aabbMax.x && aabbMin.y==aabbMax.y && aabbMin.z==aabbMax.z;
+
+        int offset_to_global=0;
+
+        // maybe this could be replaced with a reduction...
+        if(( child1==-1) &&( s.right-s.left > maxPointsPerNode) && !all_points_in_node_are_equal ) { // leaf node
+            split_node=true;
+            offset_to_global = atomicAdd( &block_nodes_to_allocate,2 );
+        }
+
+        __syncthreads();
+        __shared__ int block_left;
+        __shared__ bool enough_space;
+        // ... then the first thread tries to allocate this many nodes...
+        if( threadIdx.x==0) {
+            block_left = atomicAdd( node_count, block_nodes_to_allocate );
+            enough_space = block_left+block_nodes_to_allocate < *nodes_allocated;
+            // if it doesn't succeed, no nodes will be created by this block
+            if( !enough_space ) {
+                atomicAdd( node_count, -block_nodes_to_allocate );
+                *out_of_space=1;
+            }
+        }
+
+        __syncthreads();
+        // this thread needs to split it's node && there was enough space for all the nodes
+        // in this block.
+        //(The whole "allocate-per-block-thing" is much faster than letting each element allocate
+        // its space on its own, because shared memory atomics are A LOT faster than
+        // global mem atomics!)
+        if( split_node && enough_space ) {
+            int left = block_left + offset_to_global;
+
+            splits[left].left=s.left;
+            splits[left].right=s.right;
+            splits[left+1].left=0;
+            splits[left+1].right=0;
+
+            // split axis/position: middle of longest aabb extent
+            float4 aabbDim=aabbMax-aabbMin;
+            int maxDim=0;
+            float maxDimLength=aabbDim.x;
+            float4 splitVal=(aabbMax+aabbMin);
+            splitVal*=0.5f;
+            for( int i=1; i<=2; i++ ) {
+                float val = get_value_by_index(aabbDim,i);
+                if( val > maxDimLength ) {
+                    maxDim=i;
+                    maxDimLength=val;
+                }
+            }
+            s.split_dim=maxDim;
+            s.split_val=get_value_by_index(splitVal,maxDim);
+
+            child1_[my_index]=left;
+            splits[my_index]=s;
+
+            parent_[left]=my_index;
+            parent_[left+1]=my_index;
+            child1_[left]=-1;
+            child1_[left+1]=-1;
+        }
+    }
+};
+
+
+//! computes the scatter target address for the split operation, see Sengupta,Harris,Zhang,Owen: Scan Primitives for GPU Computing
+//! in my use case, this is about 2x as fast as thrust::partition
+struct set_addr3
+{
+    const int* val_, * f_;
+
+    int npoints_;
+    __device__
+    int operator()( int id )
+    {
+        int nf = f_[npoints_-1] + (val_[npoints_-1]);
+        int f=f_[id];
+        int t = id -f+nf;
+        return val_[id] ? f : t;
+    }
+};
+
+//! converts a float4 point (xyz) to a tuple of three float vals (used to separate the
+//! float4 input buffer into three arrays in the beginning of the tree build)
+struct pointxyz_to_px_py_pz
+{
+    __device__
+    thrust::tuple<float,float,float> operator()( const float4& val )
+    {
+        return thrust::make_tuple(val.x, val.y, val.z);
+    }
+};
+} // namespace kd_tree_builder_detail
+
+} // namespace cuda
+
+
+std::ostream& operator <<(std::ostream& stream, const cuda::kd_tree_builder_detail::SplitInfo& s)
+{
+    stream<<"(split l/r: "<< s.left <<" "<< s.right<< "  split:"<<s.split_dim<<" "<<s.split_val<<")";
+    return stream;
+}
+class CudaKdTreeBuilder
+{
+public:
+    CudaKdTreeBuilder( const thrust::device_vector<float4>& points, int max_leaf_size ) : /*out_of_space_(1,0),node_count_(1,1),*/ max_leaf_size_(max_leaf_size)
+    {
+        points_=&points;
+        int prealloc = points.size()/max_leaf_size_*16;
+        allocation_info_.resize(3);
+        allocation_info_[NodeCount]=1;
+        allocation_info_[NodesAllocated]=prealloc;
+        allocation_info_[OutOfSpace]=0;
+
+        //              std::cout<<points_->size()<<std::endl;
+
+        child1_=new thrust::device_vector<int>(prealloc,-1);
+        parent_=new thrust::device_vector<int>(prealloc,-1);
+        cuda::kd_tree_builder_detail::SplitInfo s;
+        s.left=0;
+        s.right=0;
+        splits_=new thrust::device_vector<cuda::kd_tree_builder_detail::SplitInfo>(prealloc,s);
+        s.right=points.size();
+        (*splits_)[0]=s;
+
+        aabb_min_=new thrust::device_vector<float4>(prealloc);
+        aabb_max_=new thrust::device_vector<float4>(prealloc);
+
+        index_x_=new thrust::device_vector<int>(points_->size());
+        index_y_=new thrust::device_vector<int>(points_->size());
+        index_z_=new thrust::device_vector<int>(points_->size());
+
+        owners_x_=new thrust::device_vector<int>(points_->size(),0);
+        owners_y_=new thrust::device_vector<int>(points_->size(),0);
+        owners_z_=new thrust::device_vector<int>(points_->size(),0);
+
+        leftright_x_ = new thrust::device_vector<int>(points_->size(),0);
+        leftright_y_ = new thrust::device_vector<int>(points_->size(),0);
+        leftright_z_ = new thrust::device_vector<int>(points_->size(),0);
+
+        tmp_index_=new thrust::device_vector<int>(points_->size());
+        tmp_owners_=new thrust::device_vector<int>(points_->size());
+        tmp_misc_=new thrust::device_vector<int>(points_->size());
+
+        points_x_=new thrust::device_vector<float>(points_->size());
+        points_y_=new thrust::device_vector<float>(points_->size());
+        points_z_=new thrust::device_vector<float>(points_->size());
+        delete_node_info_=false;
+    }
+
+    ~CudaKdTreeBuilder()
+    {
+        if( delete_node_info_ ) {
+            delete child1_;
+            delete parent_;
+            delete splits_;
+            delete aabb_min_;
+            delete aabb_max_;
+            delete index_x_;
+        }
+
+        delete index_y_;
+        delete index_z_;
+        delete owners_x_;
+        delete owners_y_;
+        delete owners_z_;
+        delete points_x_;
+        delete points_y_;
+        delete points_z_;
+        delete leftright_x_;
+        delete leftright_y_;
+        delete leftright_z_;
+        delete tmp_index_;
+        delete tmp_owners_;
+        delete tmp_misc_;
+    }
+
+	//! build the tree
+	//! general idea:
+	//! - build sorted lists of the points in x y and z order (to be able to compute tight AABBs in O(1) )
+	//! - while( nodes to split exist )
+	//!    - split non-child nodes along longest axis if the number of points is > max_points_per_node
+	//!    - for each point: determine whether it is in a node that was split. If yes, mark it as belonging to the left or right child node of its current parent node
+	//!    - reorder the points so that the points of a single node are continuous in the node array
+	//!    - update the left/right pointers and AABBs of all nodes
+    void buildTree()
+    {
+        //              std::cout<<"buildTree()"<<std::endl;
+        //              sleep(1);
+        //              Util::Timer stepTimer;
+        thrust::transform( points_->begin(), points_->end(), thrust::make_zip_iterator(thrust::make_tuple(points_x_->begin(), points_y_->begin(),points_z_->begin()) ), cuda::kd_tree_builder_detail::pointxyz_to_px_py_pz() );
+
+        thrust::counting_iterator<int> it(0);
+        thrust::copy( it, it+points_->size(), index_x_->begin() );
+
+        thrust::copy( index_x_->begin(), index_x_->end(), index_y_->begin() );
+        thrust::copy( index_x_->begin(), index_x_->end(), index_z_->begin() );
+
+        thrust::device_vector<float> tmpv(points_->size());
+
+        // create sorted index list -> can be used to compute AABBs in O(1)
+        thrust::copy(points_x_->begin(), points_x_->end(), tmpv.begin());
+        thrust::sort_by_key( tmpv.begin(), tmpv.end(), index_x_->begin() );
+        thrust::copy(points_y_->begin(), points_y_->end(), tmpv.begin());
+        thrust::sort_by_key( tmpv.begin(), tmpv.end(), index_y_->begin() );
+        thrust::copy(points_z_->begin(), points_z_->end(), tmpv.begin());
+        thrust::sort_by_key( tmpv.begin(), tmpv.end(), index_z_->begin() );
+
+
+        (*aabb_min_)[0]=make_float4((*points_x_)[(*index_x_)[0]],(*points_y_)[(*index_y_)[0]],(*points_z_)[(*index_z_)[0]],0);
+
+        (*aabb_max_)[0]=make_float4((*points_x_)[(*index_x_)[points_->size()-1]],(*points_y_)[(*index_y_)[points_->size()-1]],(*points_z_)[(*index_z_)[points_->size()-1]],0);
+        #ifdef PRINT_DEBUG_TIMING
+        cudaDeviceSynchronize();
+        std::cout<<" initial stuff:"<<stepTimer.elapsed()<<std::endl;
+        stepTimer.restart();
+        #endif
+        int last_node_count=0;
+        for( int i=0;; i++ ) {
+            cuda::kd_tree_builder_detail::SplitNodes sn;
+
+            sn.maxPointsPerNode=max_leaf_size_;
+            sn.node_count=thrust::raw_pointer_cast(&allocation_info_[NodeCount]);
+            sn.nodes_allocated=thrust::raw_pointer_cast(&allocation_info_[NodesAllocated]);
+            sn.out_of_space=thrust::raw_pointer_cast(&allocation_info_[OutOfSpace]);
+            sn.child1_=thrust::raw_pointer_cast(&(*child1_)[0]);
+            sn.parent_=thrust::raw_pointer_cast(&(*parent_)[0]);
+            sn.splits=thrust::raw_pointer_cast(&(*splits_)[0]);
+
+            thrust::counting_iterator<int> cit(0);
+            thrust::for_each( thrust::make_zip_iterator(thrust::make_tuple( parent_->begin(), child1_->begin(),  splits_->begin(), aabb_min_->begin(), aabb_max_->begin(), cit  )),
+                              thrust::make_zip_iterator(thrust::make_tuple( parent_->begin()+last_node_count, child1_->begin()+last_node_count,splits_->begin()+last_node_count, aabb_min_->begin()+last_node_count, aabb_max_->begin()+last_node_count,cit+last_node_count  )),
+                              sn   );
+            // copy allocation info to host
+            thrust::host_vector<int> alloc_info = allocation_info_;
+
+            if( last_node_count == alloc_info[NodeCount] ) { // no more nodes were split -> done
+                break;
+            }
+            last_node_count=alloc_info[NodeCount];
+			
+			// a node was un-splittable due to a lack of space
+            if( alloc_info[OutOfSpace]==1 ) {
+                resize_node_vectors(alloc_info[NodesAllocated]*2);
+                alloc_info[OutOfSpace]=0;
+                alloc_info[NodesAllocated]*=2;
+                allocation_info_=alloc_info;
+            }
+            #ifdef PRINT_DEBUG_TIMING
+            cudaDeviceSynchronize();
+            std::cout<<" node split:"<<stepTimer.elapsed()<<std::endl;
+            stepTimer.restart();
+            #endif
+
+            // foreach point: point was in node that was split?move it to child (leaf) node : do nothing
+            cuda::kd_tree_builder_detail::MovePointsToChildNodes sno( thrust::raw_pointer_cast(&(*child1_)[0]),
+                                                                      thrust::raw_pointer_cast(&(*splits_)[0]),
+                                                                      thrust::raw_pointer_cast(&(*points_x_)[0]),
+                                                                      thrust::raw_pointer_cast(&(*points_y_)[0]),
+                                                                      thrust::raw_pointer_cast(&(*points_z_)[0]),
+                                                                      thrust::raw_pointer_cast(&(*owners_x_)[0]),
+                                                                      thrust::raw_pointer_cast(&(*owners_y_)[0]),
+                                                                      thrust::raw_pointer_cast(&(*owners_z_)[0]),
+                                                                      thrust::raw_pointer_cast(&(*leftright_x_)[0]),
+                                                                      thrust::raw_pointer_cast(&(*leftright_y_)[0]),
+                                                                      thrust::raw_pointer_cast(&(*leftright_z_)[0])
+                                                                      );
+            thrust::counting_iterator<int> ci0(0);
+            thrust::for_each( thrust::make_zip_iterator( thrust::make_tuple( ci0, index_x_->begin(), index_y_->begin(), index_z_->begin()) ),
+                              thrust::make_zip_iterator( thrust::make_tuple( ci0+points_->size(), index_x_->end(), index_y_->end(), index_z_->end()) ),sno  );
+
+            #ifdef PRINT_DEBUG_TIMING
+            cudaDeviceSynchronize();
+            std::cout<<" set new owners:"<<stepTimer.elapsed()<<std::endl;
+            stepTimer.restart();
+            #endif
+
+            // move points around so that each leaf node's points are continuous
+            separate_left_and_right_children(*index_x_,*owners_x_,*tmp_index_,*tmp_owners_, *leftright_x_);
+            std::swap(tmp_index_, index_x_);
+            std::swap(tmp_owners_, owners_x_);
+            separate_left_and_right_children(*index_y_,*owners_y_,*tmp_index_,*tmp_owners_, *leftright_y_,false);
+            std::swap(tmp_index_, index_y_);
+            separate_left_and_right_children(*index_z_,*owners_z_,*tmp_index_,*tmp_owners_, *leftright_z_,false);
+            std::swap(tmp_index_, index_z_);
+
+            #ifdef PRINT_DEBUG_TIMING
+            cudaDeviceSynchronize();
+            std::cout<<" split:"<<stepTimer.elapsed()<<std::endl;
+            stepTimer.restart();
+            #endif
+            // calculate new AABB etc
+            update_leftright_and_aabb( *points_x_, *points_y_, *points_z_, *index_x_, *index_y_, *index_z_, *owners_x_, *splits_,*aabb_min_, *aabb_max_);
+            #ifdef PRINT_DEBUG_TIMING
+            cudaDeviceSynchronize();
+            std::cout<<" update_leftright_and_aabb:"<<stepTimer.elapsed()<<std::endl;
+            stepTimer.restart();
+            print_vector(node_count_);
+            #endif
+
+        }
+    }
+    
+	template<class Distance> 
+	friend class KDTreeCuda3dIndex;
+
+protected:
+
+
+    //! takes the partitioned nodes, and sets the left-/right info of leaf nodes, as well as the AABBs
+    void
+    update_leftright_and_aabb( const thrust::device_vector<float>& x, const thrust::device_vector<float>& y,const thrust::device_vector<float>& z,
+                               const thrust::device_vector<int>& ix, const thrust::device_vector<int>& iy,const thrust::device_vector<int>& iz,
+                               const thrust::device_vector<int>& owners,
+                               thrust::device_vector<cuda::kd_tree_builder_detail::SplitInfo>& splits, thrust::device_vector<float4>& aabbMin,thrust::device_vector<float4>& aabbMax)
+    {
+        thrust::device_vector<int>* labelsUnique=tmp_owners_;
+        thrust::device_vector<int>* countsUnique=tmp_index_;
+		// assume: points of each node are continuous in the array
+		
+		// find which nodes are here, and where each node's points begin and end
+        int unique_labels = thrust::unique_by_key_copy( owners.begin(), owners.end(), thrust::counting_iterator<int>(0), labelsUnique->begin(), countsUnique->begin()).first - labelsUnique->begin();
+
+		// update the info
+        cuda::kd_tree_builder_detail::SetLeftAndRightAndAABB s;
+        s.maxPoints=x.size();
+        s.nElements=unique_labels;
+        s.nodes=thrust::raw_pointer_cast(&(splits[0]));
+        s.counts=thrust::raw_pointer_cast(&( (*countsUnique)[0]));
+        s.labels=thrust::raw_pointer_cast(&( (*labelsUnique)[0]));
+        s.x=thrust::raw_pointer_cast(&x[0]);
+        s.y=thrust::raw_pointer_cast(&y[0]);
+        s.z=thrust::raw_pointer_cast(&z[0]);
+        s.ix=thrust::raw_pointer_cast(&ix[0]);
+        s.iy=thrust::raw_pointer_cast(&iy[0]);
+        s.iz=thrust::raw_pointer_cast(&iz[0]);
+        s.aabbMin=thrust::raw_pointer_cast(&aabbMin[0]);
+        s.aabbMax=thrust::raw_pointer_cast(&aabbMax[0]);
+
+        thrust::counting_iterator<int> it(0);
+        thrust::for_each(it, it+unique_labels, s);
+    }
+
+    //! Separates the left and right children of each node into continuous parts of the array.
+    //! More specifically, it seperates children with even and odd node indices because nodes are always
+    //! allocated in pairs -> child1==child2+1 -> child1 even and child2 odd, or vice-versa.
+    //! Since the split operation is stable, this results in continuous partitions
+    //! for all the single nodes.
+    //! (basically the split primitive according to sengupta et al)
+    //! about twice as fast as thrust::partition
+    void separate_left_and_right_children( thrust::device_vector<int>& key_in, thrust::device_vector<int>& val_in, thrust::device_vector<int>& key_out, thrust::device_vector<int>& val_out, thrust::device_vector<int>& left_right_marks, bool scatter_val_out=true )
+    {
+        thrust::device_vector<int>* f_tmp = &val_out;
+        thrust::device_vector<int>* addr_tmp = tmp_misc_;
+
+        thrust::exclusive_scan( /*thrust::make_transform_iterator(*/ left_right_marks.begin() /*,cuda::kd_tree_builder_detail::IsEven*/
+                                                                     /*())*/, /*thrust::make_transform_iterator(*/ left_right_marks.end() /*,cuda::kd_tree_builder_detail::IsEven*/
+                                                                     /*())*/,     f_tmp->begin() );
+        cuda::kd_tree_builder_detail::set_addr3 sa;
+        sa.val_=thrust::raw_pointer_cast(&left_right_marks[0]);
+        sa.f_=thrust::raw_pointer_cast(&(*f_tmp)[0]);
+        sa.npoints_=key_in.size();
+        thrust::counting_iterator<int> it(0);
+        thrust::transform(it, it+val_in.size(), addr_tmp->begin(), sa);
+
+        thrust::scatter(key_in.begin(), key_in.end(), addr_tmp->begin(), key_out.begin());
+        if( scatter_val_out ) thrust::scatter(val_in.begin(), val_in.end(), addr_tmp->begin(), val_out.begin());
+    }
+
+    //! allocates additional space in all the node-related vectors.
+    //! new_size elements will be added to all vectors.
+    void resize_node_vectors( size_t new_size )
+    {
+        size_t add = new_size - child1_->size();
+        child1_->insert(child1_->end(), add, -1);
+        parent_->insert(parent_->end(), add, -1);
+        cuda::kd_tree_builder_detail::SplitInfo s;
+        s.left=0;
+        s.right=0;
+        splits_->insert(splits_->end(), add, s);
+        float4 f;
+        aabb_min_->insert(aabb_min_->end(), add, f);
+        aabb_max_->insert(aabb_max_->end(), add, f);
+    }
+
+
+    const thrust::device_vector<float4>* points_;
+	
+	// tree data, those are stored per-node
+	
+	//! left child of each node. (right child==left child + 1, due to the alloc mechanism)
+	//! child1_[node]==-1 if node is a leaf node
+    thrust::device_vector<int>* child1_;
+	//! parent node of each node
+    thrust::device_vector<int>* parent_;
+	//! split info (dim/value or left/right pointers)
+    thrust::device_vector<cuda::kd_tree_builder_detail::SplitInfo>* splits_;
+	//! min aabb value of each node
+    thrust::device_vector<float4>* aabb_min_;
+	//! max aabb value of each node
+    thrust::device_vector<float4>* aabb_max_;
+
+    enum AllocationInfo
+    {
+        NodeCount=0,
+        NodesAllocated=1,
+        OutOfSpace=2
+    };
+    // those were put into a single vector of 3 elements so that only one mem transfer will be needed for all three of them
+    //  thrust::device_vector<int> out_of_space_;
+    //  thrust::device_vector<int> node_count_;
+    //  thrust::device_vector<int> nodes_allocated_;
+    thrust::device_vector<int> allocation_info_;
+	
+    int max_leaf_size_;
+
+	// coordinate values of the points
+    thrust::device_vector<float>* points_x_, * points_y_, * points_z_;
+	// indices
+    thrust::device_vector<int>* index_x_,  * index_y_,  * index_z_;
+	// owner node
+    thrust::device_vector<int>* owners_x_, * owners_y_, * owners_z_;
+	// contains info about whether a point was partitioned to the left or right child after a split
+    thrust::device_vector<int>* leftright_x_, * leftright_y_, * leftright_z_;
+    thrust::device_vector<int>* tmp_index_, * tmp_owners_, * tmp_misc_;
+    bool delete_node_info_;
+};
+
+
+} // namespace flann
+#endif
\ No newline at end of file
diff --git a/src/cpp/flann/algorithms/kdtree_index.h b/src/cpp/flann/algorithms/kdtree_index.h
new file mode 100644
index 0000000..42659ca
--- /dev/null
+++ b/src/cpp/flann/algorithms/kdtree_index.h
@@ -0,0 +1,760 @@
+/***********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2008-2009  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+ * Copyright 2008-2009  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+ *
+ * THE BSD LICENSE
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+
+#ifndef FLANN_KDTREE_INDEX_H_
+#define FLANN_KDTREE_INDEX_H_
+
+#include <algorithm>
+#include <map>
+#include <cassert>
+#include <cstring>
+#include <stdarg.h>
+
+#include "flann/general.h"
+#include "flann/algorithms/nn_index.h"
+#include "flann/util/dynamic_bitset.h"
+#include "flann/util/matrix.h"
+#include "flann/util/result_set.h"
+#include "flann/util/heap.h"
+#include "flann/util/allocator.h"
+#include "flann/util/random.h"
+#include "flann/util/saving.h"
+
+
+namespace flann
+{
+
+struct KDTreeIndexParams : public IndexParams
+{
+    KDTreeIndexParams(int trees = 4)
+    {
+        (*this)["algorithm"] = FLANN_INDEX_KDTREE;
+        (*this)["trees"] = trees;
+    }
+};
+
+
+/**
+ * Randomized kd-tree index
+ *
+ * Contains the k-d trees and other information for indexing a set of points
+ * for nearest-neighbor matching.
+ */
+template <typename Distance>
+class KDTreeIndex : public NNIndex<Distance>
+{
+public:
+    typedef typename Distance::ElementType ElementType;
+    typedef typename Distance::ResultType DistanceType;
+
+    typedef NNIndex<Distance> BaseClass;
+
+    typedef bool needs_kdtree_distance;
+
+
+    /**
+     * KDTree constructor
+     *
+     * Params:
+     *          inputData = dataset with the input features
+     *          params = parameters passed to the kdtree algorithm
+     */
+    KDTreeIndex(const IndexParams& params = KDTreeIndexParams(), Distance d = Distance() ) :
+    	BaseClass(params, d), mean_(NULL), var_(NULL)
+    {
+        trees_ = get_param(index_params_,"trees",4);
+    }
+
+
+    /**
+     * KDTree constructor
+     *
+     * Params:
+     *          inputData = dataset with the input features
+     *          params = parameters passed to the kdtree algorithm
+     */
+    KDTreeIndex(const Matrix<ElementType>& dataset, const IndexParams& params = KDTreeIndexParams(),
+                Distance d = Distance() ) : BaseClass(params,d ), mean_(NULL), var_(NULL)
+    {
+        trees_ = get_param(index_params_,"trees",4);
+
+        setDataset(dataset);
+    }
+
+    KDTreeIndex(const KDTreeIndex& other) : BaseClass(other),
+    		trees_(other.trees_)
+    {
+        tree_roots_.resize(other.tree_roots_.size());
+        for (size_t i=0;i<tree_roots_.size();++i) {
+        	copyTree(tree_roots_[i], other.tree_roots_[i]);
+        }
+    }
+
+    KDTreeIndex& operator=(KDTreeIndex other)
+    {
+    	this->swap(other);
+    	return *this;
+    }
+
+    /**
+     * Standard destructor
+     */
+    virtual ~KDTreeIndex()
+    {
+    	freeIndex();
+    }
+
+    BaseClass* clone() const
+    {
+    	return new KDTreeIndex(*this);
+    }
+
+    using BaseClass::buildIndex;
+    
+    void addPoints(const Matrix<ElementType>& points, float rebuild_threshold = 2)
+    {
+        assert(points.cols==veclen_);
+
+        size_t old_size = size_;
+        extendDataset(points);
+        
+        if (rebuild_threshold>1 && size_at_build_*rebuild_threshold<size_) {
+            buildIndex();
+        }
+        else {
+            for (size_t i=old_size;i<size_;++i) {
+                for (int j = 0; j < trees_; j++) {
+                    addPointToTree(tree_roots_[j], i);
+                }
+            }
+        }        
+    }
+
+    flann_algorithm_t getType() const
+    {
+        return FLANN_INDEX_KDTREE;
+    }
+
+
+    template<typename Archive>
+    void serialize(Archive& ar)
+    {
+    	ar.setObject(this);
+
+    	ar & *static_cast<NNIndex<Distance>*>(this);
+
+    	ar & trees_;
+
+    	if (Archive::is_loading::value) {
+    		tree_roots_.resize(trees_);
+    	}
+    	for (size_t i=0;i<tree_roots_.size();++i) {
+    		if (Archive::is_loading::value) {
+    			tree_roots_[i] = new(pool_) Node();
+    		}
+    		ar & *tree_roots_[i];
+    	}
+
+    	if (Archive::is_loading::value) {
+            index_params_["algorithm"] = getType();
+            index_params_["trees"] = trees_;
+    	}
+    }
+
+
+    void saveIndex(FILE* stream)
+    {
+    	serialization::SaveArchive sa(stream);
+    	sa & *this;
+    }
+
+
+    void loadIndex(FILE* stream)
+    {
+    	freeIndex();
+    	serialization::LoadArchive la(stream);
+    	la & *this;
+    }
+
+    /**
+     * Computes the inde memory usage
+     * Returns: memory used by the index
+     */
+    int usedMemory() const
+    {
+        return int(pool_.usedMemory+pool_.wastedMemory+size_*sizeof(int));  // pool memory and vind array memory
+    }
+
+    /**
+     * Find set of nearest neighbors to vec. Their indices are stored inside
+     * the result object.
+     *
+     * Params:
+     *     result = the result object in which the indices of the nearest-neighbors are stored
+     *     vec = the vector for which to search the nearest neighbors
+     *     maxCheck = the maximum number of restarts (in a best-bin-first manner)
+     */
+    void findNeighbors(ResultSet<DistanceType>& result, const ElementType* vec, const SearchParams& searchParams) const
+    {
+        int maxChecks = searchParams.checks;
+        float epsError = 1+searchParams.eps;
+
+        if (maxChecks==FLANN_CHECKS_UNLIMITED) {
+        	if (removed_) {
+        		getExactNeighbors<true>(result, vec, epsError);
+        	}
+        	else {
+        		getExactNeighbors<false>(result, vec, epsError);
+        	}
+        }
+        else {
+        	if (removed_) {
+        		getNeighbors<true>(result, vec, maxChecks, epsError);
+        	}
+        	else {
+        		getNeighbors<false>(result, vec, maxChecks, epsError);
+        	}
+        }
+    }
+
+protected:
+
+    /**
+     * Builds the index
+     */
+    void buildIndexImpl()
+    {
+        // Create a permutable array of indices to the input vectors.
+    	std::vector<int> ind(size_);
+        for (size_t i = 0; i < size_; ++i) {
+            ind[i] = int(i);
+        }
+
+        mean_ = new DistanceType[veclen_];
+        var_ = new DistanceType[veclen_];
+
+        tree_roots_.resize(trees_);
+        /* Construct the randomized trees. */
+        for (int i = 0; i < trees_; i++) {
+            /* Randomize the order of vectors to allow for unbiased sampling. */
+            std::random_shuffle(ind.begin(), ind.end());
+            tree_roots_[i] = divideTree(&ind[0], int(size_) );
+        }
+        delete[] mean_;
+        delete[] var_;
+    }
+
+    void freeIndex()
+    {
+    	for (size_t i=0;i<tree_roots_.size();++i) {
+    		// using placement new, so call destructor explicitly
+    		if (tree_roots_[i]!=NULL) tree_roots_[i]->~Node();
+    	}
+    	pool_.free();
+    }
+
+
+private:
+
+    /*--------------------- Internal Data Structures --------------------------*/
+    struct Node
+    {
+    	/**
+         * Dimension used for subdivision.
+         */
+        int divfeat;
+        /**
+         * The values used for subdivision.
+         */
+        DistanceType divval;
+        /**
+         * Point data
+         */
+        ElementType* point;
+        /**
+         * The child nodes.
+         */
+        Node* child1, * child2;
+
+        ~Node() {
+        	if (child1!=NULL) child1->~Node();
+        	if (child2!=NULL) child2->~Node();
+        }
+
+    private:
+    	template<typename Archive>
+    	void serialize(Archive& ar)
+    	{
+    		typedef KDTreeIndex<Distance> Index;
+    		Index* obj = static_cast<Index*>(ar.getObject());
+
+    		ar & divfeat;
+    		ar & divval;
+
+    		bool leaf_node = false;
+    		if (Archive::is_saving::value) {
+    			leaf_node = ((child1==NULL) && (child2==NULL));
+    		}
+    		ar & leaf_node;
+
+    		if (leaf_node) {
+    			if (Archive::is_loading::value) {
+    				point = obj->points_[divfeat];
+    			}
+    		}
+
+    		if (!leaf_node) {
+				if (Archive::is_loading::value) {
+					child1 = new(obj->pool_) Node();
+					child2 = new(obj->pool_) Node();
+				}
+    			ar & *child1;
+    			ar & *child2;
+    		}
+    	}
+    	friend struct serialization::access;
+    };
+    typedef Node* NodePtr;
+    typedef BranchStruct<NodePtr, DistanceType> BranchSt;
+    typedef BranchSt* Branch;
+
+
+    void copyTree(NodePtr& dst, const NodePtr& src)
+    {
+    	dst = new(pool_) Node();
+    	dst->divfeat = src->divfeat;
+    	dst->divval = src->divval;
+    	if (src->child1==NULL && src->child2==NULL) {
+    		dst->point = points_[dst->divfeat];
+    		dst->child1 = NULL;
+    		dst->child2 = NULL;
+    	}
+    	else {
+    		copyTree(dst->child1, src->child1);
+    		copyTree(dst->child2, src->child2);
+    	}
+    }
+
+    /**
+     * Create a tree node that subdivides the list of vecs from vind[first]
+     * to vind[last].  The routine is called recursively on each sublist.
+     * Place a pointer to this new tree node in the location pTree.
+     *
+     * Params: pTree = the new node to create
+     *                  first = index of the first vector
+     *                  last = index of the last vector
+     */
+    NodePtr divideTree(int* ind, int count)
+    {
+        NodePtr node = new(pool_) Node(); // allocate memory
+
+        /* If too few exemplars remain, then make this a leaf node. */
+        if (count == 1) {
+            node->child1 = node->child2 = NULL;    /* Mark as leaf node. */
+            node->divfeat = *ind;    /* Store index of this vec. */
+            node->point = points_[*ind];
+        }
+        else {
+            int idx;
+            int cutfeat;
+            DistanceType cutval;
+            meanSplit(ind, count, idx, cutfeat, cutval);
+
+            node->divfeat = cutfeat;
+            node->divval = cutval;
+            node->child1 = divideTree(ind, idx);
+            node->child2 = divideTree(ind+idx, count-idx);
+        }
+
+        return node;
+    }
+
+
+    /**
+     * Choose which feature to use in order to subdivide this set of vectors.
+     * Make a random choice among those with the highest variance, and use
+     * its variance as the threshold value.
+     */
+    void meanSplit(int* ind, int count, int& index, int& cutfeat, DistanceType& cutval)
+    {
+        memset(mean_,0,veclen_*sizeof(DistanceType));
+        memset(var_,0,veclen_*sizeof(DistanceType));
+
+        /* Compute mean values.  Only the first SAMPLE_MEAN values need to be
+            sampled to get a good estimate.
+         */
+        int cnt = std::min((int)SAMPLE_MEAN+1, count);
+        for (int j = 0; j < cnt; ++j) {
+            ElementType* v = points_[ind[j]];
+            for (size_t k=0; k<veclen_; ++k) {
+                mean_[k] += v[k];
+            }
+        }
+        DistanceType div_factor = DistanceType(1)/cnt;
+        for (size_t k=0; k<veclen_; ++k) {
+            mean_[k] *= div_factor;
+        }
+
+        /* Compute variances (no need to divide by count). */
+        for (int j = 0; j < cnt; ++j) {
+            ElementType* v = points_[ind[j]];
+            for (size_t k=0; k<veclen_; ++k) {
+                DistanceType dist = v[k] - mean_[k];
+                var_[k] += dist * dist;
+            }
+        }
+        /* Select one of the highest variance indices at random. */
+        cutfeat = selectDivision(var_);
+        cutval = mean_[cutfeat];
+
+        int lim1, lim2;
+        planeSplit(ind, count, cutfeat, cutval, lim1, lim2);
+
+        if (lim1>count/2) index = lim1;
+        else if (lim2<count/2) index = lim2;
+        else index = count/2;
+
+        /* If either list is empty, it means that all remaining features
+         * are identical. Split in the middle to maintain a balanced tree.
+         */
+        if ((lim1==count)||(lim2==0)) index = count/2;
+    }
+
+
+    /**
+     * Select the top RAND_DIM largest values from v and return the index of
+     * one of these selected at random.
+     */
+    int selectDivision(DistanceType* v)
+    {
+        int num = 0;
+        size_t topind[RAND_DIM];
+
+        /* Create a list of the indices of the top RAND_DIM values. */
+        for (size_t i = 0; i < veclen_; ++i) {
+            if ((num < RAND_DIM)||(v[i] > v[topind[num-1]])) {
+                /* Put this element at end of topind. */
+                if (num < RAND_DIM) {
+                    topind[num++] = i;            /* Add to list. */
+                }
+                else {
+                    topind[num-1] = i;         /* Replace last element. */
+                }
+                /* Bubble end value down to right location by repeated swapping. */
+                int j = num - 1;
+                while (j > 0  &&  v[topind[j]] > v[topind[j-1]]) {
+                    std::swap(topind[j], topind[j-1]);
+                    --j;
+                }
+            }
+        }
+        /* Select a random integer in range [0,num-1], and return that index. */
+        int rnd = rand_int(num);
+        return (int)topind[rnd];
+    }
+
+
+    /**
+     *  Subdivide the list of points by a plane perpendicular on axe corresponding
+     *  to the 'cutfeat' dimension at 'cutval' position.
+     *
+     *  On return:
+     *  dataset[ind[0..lim1-1]][cutfeat]<cutval
+     *  dataset[ind[lim1..lim2-1]][cutfeat]==cutval
+     *  dataset[ind[lim2..count]][cutfeat]>cutval
+     */
+    void planeSplit(int* ind, int count, int cutfeat, DistanceType cutval, int& lim1, int& lim2)
+    {
+        /* Move vector indices for left subtree to front of list. */
+        int left = 0;
+        int right = count-1;
+        for (;; ) {
+            while (left<=right && points_[ind[left]][cutfeat]<cutval) ++left;
+            while (left<=right && points_[ind[right]][cutfeat]>=cutval) --right;
+            if (left>right) break;
+            std::swap(ind[left], ind[right]); ++left; --right;
+        }
+        lim1 = left;
+        right = count-1;
+        for (;; ) {
+            while (left<=right && points_[ind[left]][cutfeat]<=cutval) ++left;
+            while (left<=right && points_[ind[right]][cutfeat]>cutval) --right;
+            if (left>right) break;
+            std::swap(ind[left], ind[right]); ++left; --right;
+        }
+        lim2 = left;
+    }
+
+    /**
+     * Performs an exact nearest neighbor search. The exact search performs a full
+     * traversal of the tree.
+     */
+    template<bool with_removed>
+    void getExactNeighbors(ResultSet<DistanceType>& result, const ElementType* vec, float epsError) const
+    {
+        //		checkID -= 1;  /* Set a different unique ID for each search. */
+
+        if (trees_ > 1) {
+            fprintf(stderr,"It doesn't make any sense to use more than one tree for exact search");
+        }
+        if (trees_>0) {
+            searchLevelExact<with_removed>(result, vec, tree_roots_[0], 0.0, epsError);
+        }
+    }
+
+    /**
+     * Performs the approximate nearest-neighbor search. The search is approximate
+     * because the tree traversal is abandoned after a given number of descends in
+     * the tree.
+     */
+    template<bool with_removed>
+    void getNeighbors(ResultSet<DistanceType>& result, const ElementType* vec, int maxCheck, float epsError) const
+    {
+        int i;
+        BranchSt branch;
+
+        int checkCount = 0;
+        Heap<BranchSt>* heap = new Heap<BranchSt>((int)size_);
+        DynamicBitset checked(size_);
+
+        /* Search once through each tree down to root. */
+        for (i = 0; i < trees_; ++i) {
+            searchLevel<with_removed>(result, vec, tree_roots_[i], 0, checkCount, maxCheck, epsError, heap, checked);
+        }
+
+        /* Keep searching other branches from heap until finished. */
+        while ( heap->popMin(branch) && (checkCount < maxCheck || !result.full() )) {
+            searchLevel<with_removed>(result, vec, branch.node, branch.mindist, checkCount, maxCheck, epsError, heap, checked);
+        }
+
+        delete heap;
+
+    }
+
+    /**
+     *  Search starting from a given node of the tree.  Based on any mismatches at
+     *  higher levels, all exemplars below this level must have a distance of
+     *  at least "mindistsq".
+     */
+    template<bool with_removed>
+    void searchLevel(ResultSet<DistanceType>& result_set, const ElementType* vec, NodePtr node, DistanceType mindist, int& checkCount, int maxCheck,
+                     float epsError, Heap<BranchSt>* heap, DynamicBitset& checked) const
+    {
+        if (result_set.worstDist()<mindist) {
+            //			printf("Ignoring branch, too far\n");
+            return;
+        }
+
+        /* If this is a leaf node, then do check and return. */
+        if ((node->child1 == NULL)&&(node->child2 == NULL)) {
+            int index = node->divfeat;
+            if (with_removed) {
+            	if (removed_points_.test(index)) return;
+            }
+            /*  Do not check same node more than once when searching multiple trees. */
+            if ( checked.test(index) || ((checkCount>=maxCheck)&& result_set.full()) ) return;
+            checked.set(index);
+            checkCount++;
+
+            DistanceType dist = distance_(node->point, vec, veclen_);
+            result_set.addPoint(dist,index);
+            return;
+        }
+
+        /* Which child branch should be taken first? */
+        ElementType val = vec[node->divfeat];
+        DistanceType diff = val - node->divval;
+        NodePtr bestChild = (diff < 0) ? node->child1 : node->child2;
+        NodePtr otherChild = (diff < 0) ? node->child2 : node->child1;
+
+        /* Create a branch record for the branch not taken.  Add distance
+            of this feature boundary (we don't attempt to correct for any
+            use of this feature in a parent node, which is unlikely to
+            happen and would have only a small effect).  Don't bother
+            adding more branches to heap after halfway point, as cost of
+            adding exceeds their value.
+         */
+
+        DistanceType new_distsq = mindist + distance_.accum_dist(val, node->divval, node->divfeat);
+        //		if (2 * checkCount < maxCheck  ||  !result.full()) {
+        if ((new_distsq*epsError < result_set.worstDist())||  !result_set.full()) {
+            heap->insert( BranchSt(otherChild, new_distsq) );
+        }
+
+        /* Call recursively to search next level down. */
+        searchLevel<with_removed>(result_set, vec, bestChild, mindist, checkCount, maxCheck, epsError, heap, checked);
+    }
+
+    /**
+     * Performs an exact search in the tree starting from a node.
+     */
+    template<bool with_removed>
+    void searchLevelExact(ResultSet<DistanceType>& result_set, const ElementType* vec, const NodePtr node, DistanceType mindist, const float epsError) const
+    {
+        /* If this is a leaf node, then do check and return. */
+        if ((node->child1 == NULL)&&(node->child2 == NULL)) {
+            int index = node->divfeat;
+            if (with_removed) {
+            	if (removed_points_.test(index)) return; // ignore removed points
+            }
+            DistanceType dist = distance_(node->point, vec, veclen_);
+            result_set.addPoint(dist,index);
+
+            return;
+        }
+
+        /* Which child branch should be taken first? */
+        ElementType val = vec[node->divfeat];
+        DistanceType diff = val - node->divval;
+        NodePtr bestChild = (diff < 0) ? node->child1 : node->child2;
+        NodePtr otherChild = (diff < 0) ? node->child2 : node->child1;
+
+        /* Create a branch record for the branch not taken.  Add distance
+            of this feature boundary (we don't attempt to correct for any
+            use of this feature in a parent node, which is unlikely to
+            happen and would have only a small effect).  Don't bother
+            adding more branches to heap after halfway point, as cost of
+            adding exceeds their value.
+         */
+
+        DistanceType new_distsq = mindist + distance_.accum_dist(val, node->divval, node->divfeat);
+
+        /* Call recursively to search next level down. */
+        searchLevelExact<with_removed>(result_set, vec, bestChild, mindist, epsError);
+
+        if (mindist*epsError<=result_set.worstDist()) {
+            searchLevelExact<with_removed>(result_set, vec, otherChild, new_distsq, epsError);
+        }
+    }
+    
+    void addPointToTree(NodePtr node, int ind)
+    {
+        ElementType* point = points_[ind];
+        
+        if ((node->child1==NULL) && (node->child2==NULL)) {
+            ElementType* leaf_point = node->point;
+            ElementType max_span = 0;
+            size_t div_feat = 0;
+            for (size_t i=0;i<veclen_;++i) {
+                ElementType span = abs(point[i]-leaf_point[i]);
+                if (span > max_span) {
+                    max_span = span;
+                    div_feat = i;
+                }
+            }
+            NodePtr left = new(pool_) Node();
+            left->child1 = left->child2 = NULL;
+            NodePtr right = new(pool_) Node();
+            right->child1 = right->child2 = NULL;
+
+            if (point[div_feat]<leaf_point[div_feat]) {
+                left->divfeat = ind;
+                left->point = point;
+                right->divfeat = node->divfeat;
+                right->point = node->point;
+            }
+            else {
+                left->divfeat = node->divfeat;
+                left->point = node->point;
+                right->divfeat = ind;
+                right->point = point;
+            }
+            node->divfeat = div_feat;
+            node->divval = (point[div_feat]+leaf_point[div_feat])/2;
+            node->child1 = left;
+            node->child2 = right;            
+        }
+        else {
+            if (point[node->divfeat]<node->divval) {
+                addPointToTree(node->child1,ind);
+            }
+            else {
+                addPointToTree(node->child2,ind);                
+            }
+        }
+    }
+private:
+    void swap(KDTreeIndex& other)
+    {
+    	BaseClass::swap(other);
+    	std::swap(trees_, other.trees_);
+    	std::swap(tree_roots_, other.tree_roots_);
+    	std::swap(pool_, other.pool_);
+    }
+
+private:
+
+    enum
+    {
+        /**
+         * To improve efficiency, only SAMPLE_MEAN random values are used to
+         * compute the mean and variance at each level when building a tree.
+         * A value of 100 seems to perform as well as using all values.
+         */
+        SAMPLE_MEAN = 100,
+        /**
+         * Top random dimensions to consider
+         *
+         * When creating random trees, the dimension on which to subdivide is
+         * selected at random from among the top RAND_DIM dimensions with the
+         * highest variance.  A value of 5 works well.
+         */
+        RAND_DIM=5
+    };
+
+
+    /**
+     * Number of randomized trees that are used
+     */
+    int trees_;
+
+    DistanceType* mean_;
+    DistanceType* var_;
+
+    /**
+     * Array of k-d trees used to find neighbours.
+     */
+    std::vector<NodePtr> tree_roots_;
+
+    /**
+     * Pooled memory allocator.
+     *
+     * Using a pooled memory allocator is more efficient
+     * than allocating memory directly when there is a large
+     * number small of memory allocations.
+     */
+    PooledAllocator pool_;
+
+    USING_BASECLASS_SYMBOLS
+};   // class KDTreeIndex
+
+}
+
+#endif //FLANN_KDTREE_INDEX_H_
diff --git a/src/cpp/flann/algorithms/kdtree_single_index.h b/src/cpp/flann/algorithms/kdtree_single_index.h
new file mode 100644
index 0000000..53cb1df
--- /dev/null
+++ b/src/cpp/flann/algorithms/kdtree_single_index.h
@@ -0,0 +1,698 @@
+/***********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2008-2009  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+ * Copyright 2008-2009  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+ *
+ * THE BSD LICENSE
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+
+#ifndef FLANN_KDTREE_SINGLE_INDEX_H_
+#define FLANN_KDTREE_SINGLE_INDEX_H_
+
+#include <algorithm>
+#include <map>
+#include <cassert>
+#include <cstring>
+
+#include "flann/general.h"
+#include "flann/algorithms/nn_index.h"
+#include "flann/util/matrix.h"
+#include "flann/util/result_set.h"
+#include "flann/util/heap.h"
+#include "flann/util/allocator.h"
+#include "flann/util/random.h"
+#include "flann/util/saving.h"
+
+namespace flann
+{
+
+struct KDTreeSingleIndexParams : public IndexParams
+{
+    KDTreeSingleIndexParams(int leaf_max_size = 10, bool reorder = true)
+    {
+        (*this)["algorithm"] = FLANN_INDEX_KDTREE_SINGLE;
+        (*this)["leaf_max_size"] = leaf_max_size;
+        (*this)["reorder"] = reorder;
+    }
+};
+
+
+/**
+ * Single kd-tree index
+ *
+ * Contains the k-d trees and other information for indexing a set of points
+ * for nearest-neighbor matching.
+ */
+template <typename Distance>
+class KDTreeSingleIndex : public NNIndex<Distance>
+{
+public:
+    typedef typename Distance::ElementType ElementType;
+    typedef typename Distance::ResultType DistanceType;
+
+    typedef NNIndex<Distance> BaseClass;
+
+    typedef bool needs_kdtree_distance;
+
+    /**
+     * KDTree constructor
+     *
+     * Params:
+     *          params = parameters passed to the kdtree algorithm
+     */
+    KDTreeSingleIndex(const IndexParams& params = KDTreeSingleIndexParams(), Distance d = Distance() ) :
+        BaseClass(params, d), root_node_(NULL)
+    {
+        leaf_max_size_ = get_param(params,"leaf_max_size",10);
+        reorder_ = get_param(params, "reorder", true);
+    }
+
+    /**
+     * KDTree constructor
+     *
+     * Params:
+     *          inputData = dataset with the input features
+     *          params = parameters passed to the kdtree algorithm
+     */
+    KDTreeSingleIndex(const Matrix<ElementType>& inputData, const IndexParams& params = KDTreeSingleIndexParams(),
+                      Distance d = Distance() ) : BaseClass(params, d), root_node_(NULL)
+    {
+        leaf_max_size_ = get_param(params,"leaf_max_size",10);
+        reorder_ = get_param(params, "reorder", true);
+
+        setDataset(inputData);
+    }
+
+
+    KDTreeSingleIndex(const KDTreeSingleIndex& other) : BaseClass(other),
+            leaf_max_size_(other.leaf_max_size_),
+            reorder_(other.reorder_),
+            vind_(other.vind_),
+            root_bbox_(other.root_bbox_)
+    {
+        if (reorder_) {
+            data_ = flann::Matrix<ElementType>(new ElementType[size_*veclen_], size_, veclen_);
+            std::copy(other.data_[0], other.data_[0]+size_*veclen_, data_[0]);
+        }
+        copyTree(root_node_, other.root_node_);
+    }
+
+    KDTreeSingleIndex& operator=(KDTreeSingleIndex other)
+    {
+        this->swap(other);
+        return *this;
+    }
+    
+    /**
+     * Standard destructor
+     */
+    virtual ~KDTreeSingleIndex()
+    {
+        freeIndex();
+    }
+    
+    BaseClass* clone() const
+    {
+        return new KDTreeSingleIndex(*this);
+    }
+
+    using BaseClass::buildIndex;
+
+    void addPoints(const Matrix<ElementType>& points, float rebuild_threshold = 2)
+    {
+        assert(points.cols==veclen_);
+        extendDataset(points);
+        buildIndex();
+    }
+
+    flann_algorithm_t getType() const
+    {
+        return FLANN_INDEX_KDTREE_SINGLE;
+    }
+
+
+    template<typename Archive>
+    void serialize(Archive& ar)
+    {
+        ar.setObject(this);
+
+        if (reorder_) index_params_["save_dataset"] = false;
+
+        ar & *static_cast<NNIndex<Distance>*>(this);
+
+        ar & reorder_;
+        ar & leaf_max_size_;
+        ar & root_bbox_;
+        ar & vind_;
+
+        if (reorder_) {
+            ar & data_;
+        }
+
+        if (Archive::is_loading::value) {
+            root_node_ = new(pool_) Node();
+        }
+
+        ar & *root_node_;
+
+        if (Archive::is_loading::value) {
+            index_params_["algorithm"] = getType();
+            index_params_["leaf_max_size"] = leaf_max_size_;
+            index_params_["reorder"] = reorder_;
+        }
+    }
+
+
+    void saveIndex(FILE* stream)
+    {
+        serialization::SaveArchive sa(stream);
+        sa & *this;
+    }
+
+
+    void loadIndex(FILE* stream)
+    {
+        freeIndex();
+        serialization::LoadArchive la(stream);
+        la & *this;
+    }
+
+    /**
+     * Computes the inde memory usage
+     * Returns: memory used by the index
+     */
+    int usedMemory() const
+    {
+        return pool_.usedMemory+pool_.wastedMemory+size_*sizeof(int);  // pool memory and vind array memory
+    }
+
+    /**
+     * Find set of nearest neighbors to vec. Their indices are stored inside
+     * the result object.
+     *
+     * Params:
+     *     result = the result object in which the indices of the nearest-neighbors are stored
+     *     vec = the vector for which to search the nearest neighbors
+     *     maxCheck = the maximum number of restarts (in a best-bin-first manner)
+     */
+    void findNeighbors(ResultSet<DistanceType>& result, const ElementType* vec, const SearchParams& searchParams) const
+    {
+        float epsError = 1+searchParams.eps;
+
+        std::vector<DistanceType> dists(veclen_,0);
+        DistanceType distsq = computeInitialDistances(vec, dists);
+        if (removed_) {
+            searchLevel<true>(result, vec, root_node_, distsq, dists, epsError);
+        }
+        else {
+            searchLevel<false>(result, vec, root_node_, distsq, dists, epsError);
+        }
+    }
+
+protected:
+
+    /**
+     * Builds the index
+     */
+    void buildIndexImpl()
+    {
+        // Create a permutable array of indices to the input vectors.
+        vind_.resize(size_);
+        for (size_t i = 0; i < size_; i++) {
+            vind_[i] = i;
+        }
+
+        computeBoundingBox(root_bbox_);
+        root_node_ = divideTree(0, size_, root_bbox_ );   // construct the tree
+
+        if (reorder_) {
+            data_ = flann::Matrix<ElementType>(new ElementType[size_*veclen_], size_, veclen_);
+            for (size_t i=0; i<size_; ++i) {
+                std::copy(points_[vind_[i]], points_[vind_[i]]+veclen_, data_[i]);
+            }
+        }
+    }
+
+private:
+
+
+    /*--------------------- Internal Data Structures --------------------------*/
+    struct Node
+    {
+    	/**
+    	 * Indices of points in leaf node
+    	 */
+    	int left, right;
+    	/**
+    	 * Dimension used for subdivision.
+    	 */
+    	int divfeat;
+    	/**
+    	 * The values used for subdivision.
+    	 */
+    	DistanceType divlow, divhigh;
+        /**
+         * The child nodes.
+         */
+        Node* child1, * child2;
+        
+        ~Node()
+        {
+            if (child1) child1->~Node();
+            if (child2) child2->~Node();
+        }
+
+    private:
+        template<typename Archive>
+        void serialize(Archive& ar)
+        {
+            typedef KDTreeSingleIndex<Distance> Index;
+            Index* obj = static_cast<Index*>(ar.getObject());
+
+            ar & left;
+            ar & right;
+            ar & divfeat;
+            ar & divlow;
+            ar & divhigh;
+
+            bool leaf_node = false;
+            if (Archive::is_saving::value) {
+                leaf_node = ((child1==NULL) && (child2==NULL));
+            }
+            ar & leaf_node;
+
+            if (!leaf_node) {
+                if (Archive::is_loading::value) {
+                    child1 = new(obj->pool_) Node();
+                    child2 = new(obj->pool_) Node();
+                }
+                ar & *child1;
+                ar & *child2;
+            }
+        }
+        friend struct serialization::access;
+    };
+    typedef Node* NodePtr;
+
+
+    struct Interval
+    {
+        DistanceType low, high;
+        
+    private:
+        template <typename Archive>
+        void serialize(Archive& ar)
+        {
+            ar & low;
+            ar & high;
+        }
+        friend struct serialization::access;
+    };
+
+    typedef std::vector<Interval> BoundingBox;
+
+    typedef BranchStruct<NodePtr, DistanceType> BranchSt;
+    typedef BranchSt* Branch;
+
+
+    
+    void freeIndex()
+    {
+        if (data_.ptr()) {
+            delete[] data_.ptr();
+            data_ = flann::Matrix<ElementType>();
+        }
+        if (root_node_) root_node_->~Node();
+        pool_.free();
+    }
+    
+    void copyTree(NodePtr& dst, const NodePtr& src)
+    {
+        dst = new(pool_) Node();
+        *dst = *src;
+        if (src->child1!=NULL && src->child2!=NULL) {
+            copyTree(dst->child1, src->child1);
+            copyTree(dst->child2, src->child2);
+        }
+    }
+
+
+
+    void computeBoundingBox(BoundingBox& bbox)
+    {
+        bbox.resize(veclen_);
+        for (size_t i=0; i<veclen_; ++i) {
+            bbox[i].low = (DistanceType)points_[0][i];
+            bbox[i].high = (DistanceType)points_[0][i];
+        }
+        for (size_t k=1; k<size_; ++k) {
+            for (size_t i=0; i<veclen_; ++i) {
+                if (points_[k][i]<bbox[i].low) bbox[i].low = (DistanceType)points_[k][i];
+                if (points_[k][i]>bbox[i].high) bbox[i].high = (DistanceType)points_[k][i];
+            }
+        }
+    }
+
+
+    /**
+     * Create a tree node that subdivides the list of vecs from vind[first]
+     * to vind[last].  The routine is called recursively on each sublist.
+     * Place a pointer to this new tree node in the location pTree.
+     *
+     * Params: pTree = the new node to create
+     *                  first = index of the first vector
+     *                  last = index of the last vector
+     */
+    NodePtr divideTree(int left, int right, BoundingBox& bbox)
+    {
+        NodePtr node = new (pool_) Node(); // allocate memory
+
+        /* If too few exemplars remain, then make this a leaf node. */
+        if ( (right-left) <= leaf_max_size_) {
+            node->child1 = node->child2 = NULL;    /* Mark as leaf node. */
+            node->left = left;
+            node->right = right;
+
+            // compute bounding-box of leaf points
+            for (size_t i=0; i<veclen_; ++i) {
+                bbox[i].low = (DistanceType)points_[vind_[left]][i];
+                bbox[i].high = (DistanceType)points_[vind_[left]][i];
+            }
+            for (int k=left+1; k<right; ++k) {
+                for (size_t i=0; i<veclen_; ++i) {
+                    if (bbox[i].low>points_[vind_[k]][i]) bbox[i].low=(DistanceType)points_[vind_[k]][i];
+                    if (bbox[i].high<points_[vind_[k]][i]) bbox[i].high=(DistanceType)points_[vind_[k]][i];
+                }
+            }
+        }
+        else {
+            int idx;
+            int cutfeat;
+            DistanceType cutval;
+            middleSplit(&vind_[0]+left, right-left, idx, cutfeat, cutval, bbox);
+
+            node->divfeat = cutfeat;
+
+            BoundingBox left_bbox(bbox);
+            left_bbox[cutfeat].high = cutval;
+            node->child1 = divideTree(left, left+idx, left_bbox);
+
+            BoundingBox right_bbox(bbox);
+            right_bbox[cutfeat].low = cutval;
+            node->child2 = divideTree(left+idx, right, right_bbox);
+
+            node->divlow = left_bbox[cutfeat].high;
+            node->divhigh = right_bbox[cutfeat].low;
+
+            for (size_t i=0; i<veclen_; ++i) {
+            	bbox[i].low = std::min(left_bbox[i].low, right_bbox[i].low);
+            	bbox[i].high = std::max(left_bbox[i].high, right_bbox[i].high);
+            }
+        }
+
+        return node;
+    }
+
+    void computeMinMax(int* ind, int count, int dim, ElementType& min_elem, ElementType& max_elem)
+    {
+        min_elem = points_[ind[0]][dim];
+        max_elem = points_[ind[0]][dim];
+        for (int i=1; i<count; ++i) {
+            ElementType val = points_[ind[i]][dim];
+            if (val<min_elem) min_elem = val;
+            if (val>max_elem) max_elem = val;
+        }
+    }
+
+    void middleSplit(int* ind, int count, int& index, int& cutfeat, DistanceType& cutval, const BoundingBox& bbox)
+    {
+        // find the largest span from the approximate bounding box
+        ElementType max_span = bbox[0].high-bbox[0].low;
+        cutfeat = 0;
+        cutval = (bbox[0].high+bbox[0].low)/2;
+        for (size_t i=1; i<veclen_; ++i) {
+            ElementType span = bbox[i].high-bbox[i].low;
+            if (span>max_span) {
+                max_span = span;
+                cutfeat = i;
+                cutval = (bbox[i].high+bbox[i].low)/2;
+            }
+        }
+
+        // compute exact span on the found dimension
+        ElementType min_elem, max_elem;
+        computeMinMax(ind, count, cutfeat, min_elem, max_elem);
+        cutval = (min_elem+max_elem)/2;
+        max_span = max_elem - min_elem;
+
+        // check if a dimension of a largest span exists
+        size_t k = cutfeat;
+        for (size_t i=0; i<veclen_; ++i) {
+            if (i==k) continue;
+            ElementType span = bbox[i].high-bbox[i].low;
+            if (span>max_span) {
+                computeMinMax(ind, count, i, min_elem, max_elem);
+                span = max_elem - min_elem;
+                if (span>max_span) {
+                    max_span = span;
+                    cutfeat = i;
+                    cutval = (min_elem+max_elem)/2;
+                }
+            }
+        }
+        int lim1, lim2;
+        planeSplit(ind, count, cutfeat, cutval, lim1, lim2);
+
+        if (lim1>count/2) index = lim1;
+        else if (lim2<count/2) index = lim2;
+        else index = count/2;
+        
+        assert(index > 0 && index < count);
+    }
+
+
+    void middleSplit_(int* ind, int count, int& index, int& cutfeat, DistanceType& cutval, const BoundingBox& bbox)
+    {
+        const float eps_val=0.00001f;
+        DistanceType max_span = bbox[0].high-bbox[0].low;
+        for (size_t i=1; i<veclen_; ++i) {
+            DistanceType span = bbox[i].high-bbox[i].low;
+            if (span>max_span) {
+                max_span = span;
+            }
+        }
+        DistanceType max_spread = -1;
+        cutfeat = 0;
+        for (size_t i=0; i<veclen_; ++i) {
+            DistanceType span = bbox[i].high-bbox[i].low;
+            if (span>(DistanceType)((1-eps_val)*max_span)) {
+                ElementType min_elem, max_elem;
+                computeMinMax(ind, count, cutfeat, min_elem, max_elem);
+                DistanceType spread = (DistanceType)(max_elem-min_elem);
+                if (spread>max_spread) {
+                    cutfeat = i;
+                    max_spread = spread;
+                }
+            }
+        }
+        // split in the middle
+        DistanceType split_val = (bbox[cutfeat].low+bbox[cutfeat].high)/2;
+        ElementType min_elem, max_elem;
+        computeMinMax(ind, count, cutfeat, min_elem, max_elem);
+
+        if (split_val<min_elem) cutval = (DistanceType)min_elem;
+        else if (split_val>max_elem) cutval = (DistanceType)max_elem;
+        else cutval = split_val;
+
+        int lim1, lim2;
+        planeSplit(ind, count, cutfeat, cutval, lim1, lim2);
+
+        if (lim1>count/2) index = lim1;
+        else if (lim2<count/2) index = lim2;
+        else index = count/2;
+        
+        assert(index > 0 && index < count);
+    }
+
+
+    /**
+     *  Subdivide the list of points by a plane perpendicular on axe corresponding
+     *  to the 'cutfeat' dimension at 'cutval' position.
+     *
+     *  On return:
+     *  dataset[ind[0..lim1-1]][cutfeat]<cutval
+     *  dataset[ind[lim1..lim2-1]][cutfeat]==cutval
+     *  dataset[ind[lim2..count]][cutfeat]>cutval
+     */
+    void planeSplit(int* ind, int count, int cutfeat, DistanceType cutval, int& lim1, int& lim2)
+    {
+        int left = 0;
+        int right = count-1;
+        for (;; ) {
+            while (left<=right && points_[ind[left]][cutfeat]<cutval) ++left;
+            while (left<=right && points_[ind[right]][cutfeat]>=cutval) --right;
+            if (left>right) break;
+            std::swap(ind[left], ind[right]); ++left; --right;
+        }
+
+        lim1 = left;
+        right = count-1;
+        for (;; ) {
+            while (left<=right && points_[ind[left]][cutfeat]<=cutval) ++left;
+            while (left<=right && points_[ind[right]][cutfeat]>cutval) --right;
+            if (left>right) break;
+            std::swap(ind[left], ind[right]); ++left; --right;
+        }
+        lim2 = left;
+    }
+
+    DistanceType computeInitialDistances(const ElementType* vec, std::vector<DistanceType>& dists) const
+    {
+        DistanceType distsq = 0.0;
+
+        for (size_t i = 0; i < veclen_; ++i) {
+            if (vec[i] < root_bbox_[i].low) {
+                dists[i] = distance_.accum_dist(vec[i], root_bbox_[i].low, i);
+                distsq += dists[i];
+            }
+            if (vec[i] > root_bbox_[i].high) {
+                dists[i] = distance_.accum_dist(vec[i], root_bbox_[i].high, i);
+                distsq += dists[i];
+            }
+        }
+
+        return distsq;
+    }
+
+    /**
+     * Performs an exact search in the tree starting from a node.
+     */
+    template <bool with_removed>
+    void searchLevel(ResultSet<DistanceType>& result_set, const ElementType* vec, const NodePtr node, DistanceType mindistsq,
+                     std::vector<DistanceType>& dists, const float epsError) const
+    {
+        /* If this is a leaf node, then do check and return. */
+        if ((node->child1 == NULL)&&(node->child2 == NULL)) {
+            DistanceType worst_dist = result_set.worstDist();
+            for (int i=node->left; i<node->right; ++i) {
+                if (with_removed) {
+                    if (removed_points_.test(vind_[i])) continue;
+                }
+                ElementType* point = reorder_ ? data_[i] : points_[vind_[i]];
+                DistanceType dist = distance_(vec, point, veclen_, worst_dist);
+                if (dist<worst_dist) {
+                    result_set.addPoint(dist,vind_[i]);
+                }
+            }
+            return;
+        }
+
+        /* Which child branch should be taken first? */
+        int idx = node->divfeat;
+        ElementType val = vec[idx];
+        DistanceType diff1 = val - node->divlow;
+        DistanceType diff2 = val - node->divhigh;
+
+        NodePtr bestChild;
+        NodePtr otherChild;
+        DistanceType cut_dist;
+        if ((diff1+diff2)<0) {
+            bestChild = node->child1;
+            otherChild = node->child2;
+            cut_dist = distance_.accum_dist(val, node->divhigh, idx);
+        }
+        else {
+            bestChild = node->child2;
+            otherChild = node->child1;
+            cut_dist = distance_.accum_dist( val, node->divlow, idx);
+        }
+
+        /* Call recursively to search next level down. */
+        searchLevel<with_removed>(result_set, vec, bestChild, mindistsq, dists, epsError);
+
+        DistanceType dst = dists[idx];
+        mindistsq = mindistsq + cut_dist - dst;
+        dists[idx] = cut_dist;
+        if (mindistsq*epsError<=result_set.worstDist()) {
+            searchLevel<with_removed>(result_set, vec, otherChild, mindistsq, dists, epsError);
+        }
+        dists[idx] = dst;
+    }
+
+    
+    void swap(KDTreeSingleIndex& other)
+    {
+        BaseClass::swap(other);
+        std::swap(leaf_max_size_, other.leaf_max_size_);
+        std::swap(reorder_, other.reorder_);
+        std::swap(vind_, other.vind_);
+        std::swap(data_, other.data_);
+        std::swap(root_node_, other.root_node_);
+        std::swap(root_bbox_, other.root_bbox_);
+        std::swap(pool_, other.pool_);
+    }
+    
+private:
+
+
+
+    int leaf_max_size_;
+    
+    
+    bool reorder_;
+
+    /**
+     *  Array of indices to vectors in the dataset.
+     */
+    std::vector<int> vind_;
+
+    Matrix<ElementType> data_;
+
+    /**
+     * Array of k-d trees used to find neighbours.
+     */
+    NodePtr root_node_;
+
+    /**
+     * Root bounding box
+     */
+    BoundingBox root_bbox_;
+
+    /**
+     * Pooled memory allocator.
+     *
+     * Using a pooled memory allocator is more efficient
+     * than allocating memory directly when there is a large
+     * number small of memory allocations.
+     */
+    PooledAllocator pool_;
+
+    USING_BASECLASS_SYMBOLS
+
+};   // class KDTreeSingleIndex
+
+}
+
+#endif //FLANN_KDTREE_SINGLE_INDEX_H_
diff --git a/src/cpp/flann/algorithms/kmeans_index.h b/src/cpp/flann/algorithms/kmeans_index.h
new file mode 100644
index 0000000..e5fbe30
--- /dev/null
+++ b/src/cpp/flann/algorithms/kmeans_index.h
@@ -0,0 +1,1066 @@
+/***********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2008-2009  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+ * Copyright 2008-2009  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+ *
+ * THE BSD LICENSE
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+
+#ifndef FLANN_KMEANS_INDEX_H_
+#define FLANN_KMEANS_INDEX_H_
+
+#include <algorithm>
+#include <string>
+#include <map>
+#include <cassert>
+#include <limits>
+#include <cmath>
+
+#include "flann/general.h"
+#include "flann/algorithms/nn_index.h"
+#include "flann/algorithms/dist.h"
+#include <flann/algorithms/center_chooser.h>
+#include "flann/util/matrix.h"
+#include "flann/util/result_set.h"
+#include "flann/util/heap.h"
+#include "flann/util/allocator.h"
+#include "flann/util/random.h"
+#include "flann/util/saving.h"
+#include "flann/util/logger.h"
+
+
+
+namespace flann
+{
+
+struct KMeansIndexParams : public IndexParams
+{
+    KMeansIndexParams(int branching = 32, int iterations = 11,
+                      flann_centers_init_t centers_init = FLANN_CENTERS_RANDOM, float cb_index = 0.2 )
+    {
+        (*this)["algorithm"] = FLANN_INDEX_KMEANS;
+        // branching factor
+        (*this)["branching"] = branching;
+        // max iterations to perform in one kmeans clustering (kmeans tree)
+        (*this)["iterations"] = iterations;
+        // algorithm used for picking the initial cluster centers for kmeans tree
+        (*this)["centers_init"] = centers_init;
+        // cluster boundary index. Used when searching the kmeans tree
+        (*this)["cb_index"] = cb_index;
+    }
+};
+
+
+/**
+ * Hierarchical kmeans index
+ *
+ * Contains a tree constructed through a hierarchical kmeans clustering
+ * and other information for indexing a set of points for nearest-neighbour matching.
+ */
+template <typename Distance>
+class KMeansIndex : public NNIndex<Distance>
+{
+public:
+    typedef typename Distance::ElementType ElementType;
+    typedef typename Distance::ResultType DistanceType;
+
+    typedef NNIndex<Distance> BaseClass;
+
+    typedef bool needs_vector_space_distance;
+
+
+
+    flann_algorithm_t getType() const
+    {
+        return FLANN_INDEX_KMEANS;
+    }
+
+    /**
+     * Index constructor
+     *
+     * Params:
+     *          inputData = dataset with the input features
+     *          params = parameters passed to the hierarchical k-means algorithm
+     */
+    KMeansIndex(const Matrix<ElementType>& inputData, const IndexParams& params = KMeansIndexParams(),
+                Distance d = Distance())
+        : BaseClass(params,d), root_(NULL), memoryCounter_(0)
+    {
+        branching_ = get_param(params,"branching",32);
+        iterations_ = get_param(params,"iterations",11);
+        if (iterations_<0) {
+            iterations_ = (std::numeric_limits<int>::max)();
+        }
+        centers_init_  = get_param(params,"centers_init",FLANN_CENTERS_RANDOM);
+        cb_index_  = get_param(params,"cb_index",0.4f);
+
+        initCenterChooser();
+        chooseCenters_->setDataset(inputData);
+        
+        setDataset(inputData);
+    }
+
+
+    /**
+     * Index constructor
+     *
+     * Params:
+     *          inputData = dataset with the input features
+     *          params = parameters passed to the hierarchical k-means algorithm
+     */
+    KMeansIndex(const IndexParams& params = KMeansIndexParams(), Distance d = Distance())
+        : BaseClass(params, d), root_(NULL), memoryCounter_(0)
+    {
+        branching_ = get_param(params,"branching",32);
+        iterations_ = get_param(params,"iterations",11);
+        if (iterations_<0) {
+            iterations_ = (std::numeric_limits<int>::max)();
+        }
+        centers_init_  = get_param(params,"centers_init",FLANN_CENTERS_RANDOM);
+        cb_index_  = get_param(params,"cb_index",0.4f);
+
+        initCenterChooser();
+    }
+
+
+    KMeansIndex(const KMeansIndex& other) : BaseClass(other),
+    		branching_(other.branching_),
+    		iterations_(other.iterations_),
+    		centers_init_(other.centers_init_),
+    		cb_index_(other.cb_index_),
+    		memoryCounter_(other.memoryCounter_)
+    {
+    	initCenterChooser();
+
+    	copyTree(root_, other.root_);
+    }
+
+    KMeansIndex& operator=(KMeansIndex other)
+    {
+    	this->swap(other);
+    	return *this;
+    }
+
+
+    void initCenterChooser()
+    {
+        switch(centers_init_) {
+        case FLANN_CENTERS_RANDOM:
+        	chooseCenters_ = new RandomCenterChooser<Distance>(distance_);
+        	break;
+        case FLANN_CENTERS_GONZALES:
+        	chooseCenters_ = new GonzalesCenterChooser<Distance>(distance_);
+        	break;
+        case FLANN_CENTERS_KMEANSPP:
+            chooseCenters_ = new KMeansppCenterChooser<Distance>(distance_);
+        	break;
+        default:
+            throw FLANNException("Unknown algorithm for choosing initial centers.");
+        }
+    }
+
+    /**
+     * Index destructor.
+     *
+     * Release the memory used by the index.
+     */
+    virtual ~KMeansIndex()
+    {
+    	delete chooseCenters_;
+    	freeIndex();
+    }
+
+    BaseClass* clone() const
+    {
+    	return new KMeansIndex(*this);
+    }
+
+
+    void set_cb_index( float index)
+    {
+        cb_index_ = index;
+    }
+
+    /**
+     * Computes the inde memory usage
+     * Returns: memory used by the index
+     */
+    int usedMemory() const
+    {
+        return pool_.usedMemory+pool_.wastedMemory+memoryCounter_;
+    }
+
+    using BaseClass::buildIndex;
+
+    void addPoints(const Matrix<ElementType>& points, float rebuild_threshold = 2)
+    {
+        assert(points.cols==veclen_);
+        size_t old_size = size_;
+
+        extendDataset(points);
+        
+        if (rebuild_threshold>1 && size_at_build_*rebuild_threshold<size_) {
+            buildIndex();
+        }
+        else {
+            for (size_t i=0;i<points.rows;++i) {
+                DistanceType dist = distance_(root_->pivot, points[i], veclen_);
+                addPointToTree(root_, old_size + i, dist);
+            }            
+        }
+    }
+
+    template<typename Archive>
+    void serialize(Archive& ar)
+    {
+    	ar.setObject(this);
+
+    	ar & *static_cast<NNIndex<Distance>*>(this);
+
+    	ar & branching_;
+    	ar & iterations_;
+    	ar & memoryCounter_;
+    	ar & cb_index_;
+    	ar & centers_init_;
+
+    	if (Archive::is_loading::value) {
+    		root_ = new(pool_) Node();
+    	}
+    	ar & *root_;
+
+    	if (Archive::is_loading::value) {
+            index_params_["algorithm"] = getType();
+            index_params_["branching"] = branching_;
+            index_params_["iterations"] = iterations_;
+            index_params_["centers_init"] = centers_init_;
+            index_params_["cb_index"] = cb_index_;
+    	}
+    }
+
+    void saveIndex(FILE* stream)
+    {
+    	serialization::SaveArchive sa(stream);
+    	sa & *this;
+    }
+
+    void loadIndex(FILE* stream)
+    {
+    	freeIndex();
+    	serialization::LoadArchive la(stream);
+    	la & *this;
+    }
+
+    /**
+     * Find set of nearest neighbors to vec. Their indices are stored inside
+     * the result object.
+     *
+     * Params:
+     *     result = the result object in which the indices of the nearest-neighbors are stored
+     *     vec = the vector for which to search the nearest neighbors
+     *     searchParams = parameters that influence the search algorithm (checks, cb_index)
+     */
+
+    void findNeighbors(ResultSet<DistanceType>& result, const ElementType* vec, const SearchParams& searchParams) const
+    {
+    	if (removed_) {
+    		findNeighborsWithRemoved<true>(result, vec, searchParams);
+    	}
+    	else {
+    		findNeighborsWithRemoved<false>(result, vec, searchParams);
+    	}
+
+    }
+
+    /**
+     * Clustering function that takes a cut in the hierarchical k-means
+     * tree and return the clusters centers of that clustering.
+     * Params:
+     *     numClusters = number of clusters to have in the clustering computed
+     * Returns: number of cluster centers
+     */
+    int getClusterCenters(Matrix<DistanceType>& centers)
+    {
+        int numClusters = centers.rows;
+        if (numClusters<1) {
+            throw FLANNException("Number of clusters must be at least 1");
+        }
+
+        DistanceType variance;
+        std::vector<NodePtr> clusters(numClusters);
+
+        int clusterCount = getMinVarianceClusters(root_, clusters, numClusters, variance);
+
+        Logger::info("Clusters requested: %d, returning %d\n",numClusters, clusterCount);
+
+        for (int i=0; i<clusterCount; ++i) {
+            DistanceType* center = clusters[i]->pivot;
+            for (size_t j=0; j<veclen_; ++j) {
+                centers[i][j] = center[j];
+            }
+        }
+
+        return clusterCount;
+    }
+
+protected:
+    /**
+     * Builds the index
+     */
+    void buildIndexImpl()
+    {
+        if (branching_<2) {
+            throw FLANNException("Branching factor must be at least 2");
+        }
+
+        std::vector<int> indices(size_);
+        for (size_t i=0; i<size_; ++i) {
+        	indices[i] = int(i);
+        }
+
+        root_ = new(pool_) Node();
+        computeNodeStatistics(root_, indices);
+        computeClustering(root_, &indices[0], (int)size_, branching_);
+    }
+
+private:
+
+    struct PointInfo
+    {
+    	size_t index;
+    	ElementType* point;
+    private:
+    	template<typename Archive>
+    	void serialize(Archive& ar)
+    	{
+    		typedef KMeansIndex<Distance> Index;
+    		Index* obj = static_cast<Index*>(ar.getObject());
+
+    		ar & index;
+//    		ar & point;
+
+			if (Archive::is_loading::value) point = obj->points_[index];
+    	}
+    	friend struct serialization::access;
+    };
+
+    /**
+     * Struture representing a node in the hierarchical k-means tree.
+     */
+    struct Node
+    {
+        /**
+         * The cluster center.
+         */
+        DistanceType* pivot;
+        /**
+         * The cluster radius.
+         */
+        DistanceType radius;
+        /**
+         * The cluster variance.
+         */
+        DistanceType variance;
+        /**
+         * The cluster size (number of points in the cluster)
+         */
+        int size;
+        /**
+         * Child nodes (only for non-terminal nodes)
+         */
+        std::vector<Node*> childs;
+        /**
+         * Node points (only for terminal nodes)
+         */
+        std::vector<PointInfo> points;
+        /**
+         * Level
+         */
+//        int level;
+
+        ~Node()
+        {
+            delete[] pivot;
+            if (!childs.empty()) {
+                for (size_t i=0; i<childs.size(); ++i) {
+                    childs[i]->~Node();
+                }
+            }
+        }
+
+    	template<typename Archive>
+    	void serialize(Archive& ar)
+    	{
+    		typedef KMeansIndex<Distance> Index;
+    		Index* obj = static_cast<Index*>(ar.getObject());
+
+    		if (Archive::is_loading::value) {
+    			pivot = new DistanceType[obj->veclen_];
+    		}
+    		ar & serialization::make_binary_object(pivot, obj->veclen_*sizeof(DistanceType));
+    		ar & radius;
+    		ar & variance;
+    		ar & size;
+
+    		size_t childs_size;
+    		if (Archive::is_saving::value) {
+    			childs_size = childs.size();
+    		}
+    		ar & childs_size;
+
+    		if (childs_size==0) {
+    			ar & points;
+    		}
+    		else {
+    			if (Archive::is_loading::value) {
+    				childs.resize(childs_size);
+    			}
+    			for (size_t i=0;i<childs_size;++i) {
+    				if (Archive::is_loading::value) {
+    					childs[i] = new(obj->pool_) Node();
+    				}
+    				ar & *childs[i];
+    			}
+    		}
+    	}
+    	friend struct serialization::access;
+    };
+    typedef Node* NodePtr;
+
+    /**
+     * Alias definition for a nicer syntax.
+     */
+    typedef BranchStruct<NodePtr, DistanceType> BranchSt;
+
+
+    /**
+     * Helper function
+     */
+    void freeIndex()
+    {
+    	if (root_) root_->~Node();
+    	root_ = NULL;
+    	pool_.free();
+    }
+
+    void copyTree(NodePtr& dst, const NodePtr& src)
+    {
+    	dst = new(pool_) Node();
+    	dst->pivot = new DistanceType[veclen_];
+    	std::copy(src->pivot, src->pivot+veclen_, dst->pivot);
+    	dst->radius = src->radius;
+    	dst->variance = src->variance;
+    	dst->size = src->size;
+
+    	if (src->childs.size()==0) {
+    		dst->points = src->points;
+    	}
+    	else {
+    		dst->childs.resize(src->childs.size());
+    		for (size_t i=0;i<src->childs.size();++i) {
+    			copyTree(dst->childs[i], src->childs[i]);
+    		}
+    	}
+    }
+
+
+    /**
+     * Computes the statistics of a node (mean, radius, variance).
+     *
+     * Params:
+     *     node = the node to use
+     *     indices = the indices of the points belonging to the node
+     */
+    void computeNodeStatistics(NodePtr node, const std::vector<int>& indices)
+    {
+        size_t size = indices.size();
+
+        DistanceType* mean = new DistanceType[veclen_];
+        memoryCounter_ += int(veclen_*sizeof(DistanceType));
+        memset(mean,0,veclen_*sizeof(DistanceType));
+
+        for (size_t i=0; i<size; ++i) {
+            ElementType* vec = points_[indices[i]];
+            for (size_t j=0; j<veclen_; ++j) {
+                mean[j] += vec[j];
+            }
+        }
+        DistanceType div_factor = DistanceType(1)/size;
+        for (size_t j=0; j<veclen_; ++j) {
+            mean[j] *= div_factor;
+        }
+        
+        DistanceType radius = 0;
+        DistanceType variance = 0;
+        for (size_t i=0; i<size; ++i) {
+            DistanceType dist = distance_(mean, points_[indices[i]], veclen_);
+            if (dist>radius) {
+                radius = dist;
+            }
+            variance += dist;
+        }        
+        variance /= size;
+
+        node->variance = variance;
+        node->radius = radius;
+        node->pivot = mean;
+    }
+
+
+    /**
+     * The method responsible with actually doing the recursive hierarchical
+     * clustering
+     *
+     * Params:
+     *     node = the node to cluster
+     *     indices = indices of the points belonging to the current node
+     *     branching = the branching factor to use in the clustering
+     *
+     * TODO: for 1-sized clusters don't store a cluster center (it's the same as the single cluster point)
+     */
+    void computeClustering(NodePtr node, int* indices, int indices_length, int branching)
+    {
+        node->size = indices_length;
+
+        if (indices_length < branching) {
+            node->points.resize(indices_length);
+            for (int i=0;i<indices_length;++i) {
+            	node->points[i].index = indices[i];
+            	node->points[i].point = points_[indices[i]];
+            }
+            node->childs.clear();
+            return;
+        }
+
+        std::vector<int> centers_idx(branching);
+        int centers_length;
+        (*chooseCenters_)(branching, indices, indices_length, &centers_idx[0], centers_length);
+
+        if (centers_length<branching) {
+            node->points.resize(indices_length);
+            for (int i=0;i<indices_length;++i) {
+            	node->points[i].index = indices[i];
+            	node->points[i].point = points_[indices[i]];
+            }
+            node->childs.clear();
+            return;
+        }
+
+
+        Matrix<double> dcenters(new double[branching*veclen_],branching,veclen_);
+        for (int i=0; i<centers_length; ++i) {
+            ElementType* vec = points_[centers_idx[i]];
+            for (size_t k=0; k<veclen_; ++k) {
+                dcenters[i][k] = double(vec[k]);
+            }
+        }
+
+        std::vector<DistanceType> radiuses(branching,0);
+        std::vector<int> count(branching,0);
+
+        //	assign points to clusters
+        std::vector<int> belongs_to(indices_length);
+        for (int i=0; i<indices_length; ++i) {
+
+            DistanceType sq_dist = distance_(points_[indices[i]], dcenters[0], veclen_);
+            belongs_to[i] = 0;
+            for (int j=1; j<branching; ++j) {
+                DistanceType new_sq_dist = distance_(points_[indices[i]], dcenters[j], veclen_);
+                if (sq_dist>new_sq_dist) {
+                    belongs_to[i] = j;
+                    sq_dist = new_sq_dist;
+                }
+            }
+            if (sq_dist>radiuses[belongs_to[i]]) {
+                radiuses[belongs_to[i]] = sq_dist;
+            }
+            count[belongs_to[i]]++;
+        }
+
+        bool converged = false;
+        int iteration = 0;
+        while (!converged && iteration<iterations_) {
+            converged = true;
+            iteration++;
+
+            // compute the new cluster centers
+            for (int i=0; i<branching; ++i) {
+                memset(dcenters[i],0,sizeof(double)*veclen_);
+                radiuses[i] = 0;
+            }
+            for (int i=0; i<indices_length; ++i) {
+                ElementType* vec = points_[indices[i]];
+                double* center = dcenters[belongs_to[i]];
+                for (size_t k=0; k<veclen_; ++k) {
+                    center[k] += vec[k];
+                }
+            }
+            for (int i=0; i<branching; ++i) {
+                int cnt = count[i];
+                double div_factor = 1.0/cnt;
+                for (size_t k=0; k<veclen_; ++k) {
+                    dcenters[i][k] *= div_factor;
+                }
+            }
+
+            // reassign points to clusters
+            for (int i=0; i<indices_length; ++i) {
+                DistanceType sq_dist = distance_(points_[indices[i]], dcenters[0], veclen_);
+                int new_centroid = 0;
+                for (int j=1; j<branching; ++j) {
+                    DistanceType new_sq_dist = distance_(points_[indices[i]], dcenters[j], veclen_);
+                    if (sq_dist>new_sq_dist) {
+                        new_centroid = j;
+                        sq_dist = new_sq_dist;
+                    }
+                }
+                if (sq_dist>radiuses[new_centroid]) {
+                    radiuses[new_centroid] = sq_dist;
+                }
+                if (new_centroid != belongs_to[i]) {
+                    count[belongs_to[i]]--;
+                    count[new_centroid]++;
+                    belongs_to[i] = new_centroid;
+
+                    converged = false;
+                }
+            }
+
+            for (int i=0; i<branching; ++i) {
+                // if one cluster converges to an empty cluster,
+                // move an element into that cluster
+                if (count[i]==0) {
+                    int j = (i+1)%branching;
+                    while (count[j]<=1) {
+                        j = (j+1)%branching;
+                    }
+
+                    for (int k=0; k<indices_length; ++k) {
+                        if (belongs_to[k]==j) {
+                            belongs_to[k] = i;
+                            count[j]--;
+                            count[i]++;
+                            break;
+                        }
+                    }
+                    converged = false;
+                }
+            }
+
+        }
+
+        std::vector<DistanceType*> centers(branching);
+
+        for (int i=0; i<branching; ++i) {
+            centers[i] = new DistanceType[veclen_];
+            memoryCounter_ += veclen_*sizeof(DistanceType);
+            for (size_t k=0; k<veclen_; ++k) {
+                centers[i][k] = (DistanceType)dcenters[i][k];
+            }
+        }
+
+
+        // compute kmeans clustering for each of the resulting clusters
+        node->childs.resize(branching);
+        int start = 0;
+        int end = start;
+        for (int c=0; c<branching; ++c) {
+            int s = count[c];
+
+            DistanceType variance = 0;
+            for (int i=0; i<indices_length; ++i) {
+                if (belongs_to[i]==c) {
+                    variance += distance_(centers[c], points_[indices[i]], veclen_);
+                    std::swap(indices[i],indices[end]);
+                    std::swap(belongs_to[i],belongs_to[end]);
+                    end++;
+                }
+            }
+            variance /= s;
+
+            node->childs[c] = new(pool_) Node();
+            node->childs[c]->radius = radiuses[c];
+            node->childs[c]->pivot = centers[c];
+            node->childs[c]->variance = variance;
+            computeClustering(node->childs[c],indices+start, end-start, branching);
+            start=end;
+        }
+
+        delete[] dcenters.ptr();
+    }
+
+
+    template<bool with_removed>
+    void findNeighborsWithRemoved(ResultSet<DistanceType>& result, const ElementType* vec, const SearchParams& searchParams) const
+    {
+
+        int maxChecks = searchParams.checks;
+
+        if (maxChecks==FLANN_CHECKS_UNLIMITED) {
+            findExactNN<with_removed>(root_, result, vec);
+        }
+        else {
+            // Priority queue storing intermediate branches in the best-bin-first search
+            Heap<BranchSt>* heap = new Heap<BranchSt>((int)size_);
+
+            int checks = 0;
+            findNN<with_removed>(root_, result, vec, checks, maxChecks, heap);
+
+            BranchSt branch;
+            while (heap->popMin(branch) && (checks<maxChecks || !result.full())) {
+                NodePtr node = branch.node;
+                findNN<with_removed>(node, result, vec, checks, maxChecks, heap);
+            }
+
+            delete heap;
+        }
+
+    }
+
+
+    /**
+     * Performs one descent in the hierarchical k-means tree. The branches not
+     * visited are stored in a priority queue.
+     *
+     * Params:
+     *      node = node to explore
+     *      result = container for the k-nearest neighbors found
+     *      vec = query points
+     *      checks = how many points in the dataset have been checked so far
+     *      maxChecks = maximum dataset points to checks
+     */
+
+    template<bool with_removed>
+    void findNN(NodePtr node, ResultSet<DistanceType>& result, const ElementType* vec, int& checks, int maxChecks,
+                Heap<BranchSt>* heap) const
+    {
+        // Ignore those clusters that are too far away
+        {
+            DistanceType bsq = distance_(vec, node->pivot, veclen_);
+            DistanceType rsq = node->radius;
+            DistanceType wsq = result.worstDist();
+
+            DistanceType val = bsq-rsq-wsq;
+            DistanceType val2 = val*val-4*rsq*wsq;
+
+            //if (val>0) {
+            if ((val>0)&&(val2>0)) {
+                return;
+            }
+        }
+
+        if (node->childs.empty()) {
+            if (checks>=maxChecks) {
+                if (result.full()) return;
+            }
+            for (int i=0; i<node->size; ++i) {
+            	PointInfo& point_info = node->points[i];
+                int index = point_info.index;
+                if (with_removed) {
+                	if (removed_points_.test(index)) continue;
+                }
+                DistanceType dist = distance_(point_info.point, vec, veclen_);
+                result.addPoint(dist, index);
+                ++checks;
+            }
+        }
+        else {
+            int closest_center = exploreNodeBranches(node, vec, heap);
+            findNN<with_removed>(node->childs[closest_center],result,vec, checks, maxChecks, heap);
+        }
+    }
+
+    /**
+     * Helper function that computes the nearest childs of a node to a given query point.
+     * Params:
+     *     node = the node
+     *     q = the query point
+     *     distances = array with the distances to each child node.
+     * Returns:
+     */
+    int exploreNodeBranches(NodePtr node, const ElementType* q, Heap<BranchSt>* heap) const
+    {
+        std::vector<DistanceType> domain_distances(branching_);
+        int best_index = 0;
+        domain_distances[best_index] = distance_(q, node->childs[best_index]->pivot, veclen_);
+        for (int i=1; i<branching_; ++i) {
+            domain_distances[i] = distance_(q, node->childs[i]->pivot, veclen_);
+            if (domain_distances[i]<domain_distances[best_index]) {
+                best_index = i;
+            }
+        }
+
+        //		float* best_center = node->childs[best_index]->pivot;
+        for (int i=0; i<branching_; ++i) {
+            if (i != best_index) {
+                domain_distances[i] -= cb_index_*node->childs[i]->variance;
+
+                //				float dist_to_border = getDistanceToBorder(node.childs[i].pivot,best_center,q);
+                //				if (domain_distances[i]<dist_to_border) {
+                //					domain_distances[i] = dist_to_border;
+                //				}
+                heap->insert(BranchSt(node->childs[i],domain_distances[i]));
+            }
+        }
+
+        return best_index;
+    }
+
+
+    /**
+     * Function the performs exact nearest neighbor search by traversing the entire tree.
+     */
+    template<bool with_removed>
+    void findExactNN(NodePtr node, ResultSet<DistanceType>& result, const ElementType* vec) const
+    {
+        // Ignore those clusters that are too far away
+        {
+            DistanceType bsq = distance_(vec, node->pivot, veclen_);
+            DistanceType rsq = node->radius;
+            DistanceType wsq = result.worstDist();
+
+            DistanceType val = bsq-rsq-wsq;
+            DistanceType val2 = val*val-4*rsq*wsq;
+
+            //                  if (val>0) {
+            if ((val>0)&&(val2>0)) {
+                return;
+            }
+        }
+
+        if (node->childs.empty()) {
+            for (int i=0; i<node->size; ++i) {
+            	PointInfo& point_info = node->points[i];
+                int index = point_info.index;
+                if (with_removed) {
+                	if (removed_points_.test(index)) continue;
+                }
+                DistanceType dist = distance_(point_info.point, vec, veclen_);
+                result.addPoint(dist, index);
+            }
+        }
+        else {
+            std::vector<int> sort_indices(branching_);
+            getCenterOrdering(node, vec, sort_indices);
+
+            for (int i=0; i<branching_; ++i) {
+                findExactNN<with_removed>(node->childs[sort_indices[i]],result,vec);
+            }
+
+        }
+    }
+
+
+    /**
+     * Helper function.
+     *
+     * I computes the order in which to traverse the child nodes of a particular node.
+     */
+    void getCenterOrdering(NodePtr node, const ElementType* q, std::vector<int>& sort_indices) const
+    {
+        std::vector<DistanceType> domain_distances(branching_);
+        for (int i=0; i<branching_; ++i) {
+            DistanceType dist = distance_(q, node->childs[i]->pivot, veclen_);
+
+            int j=0;
+            while (domain_distances[j]<dist && j<i) j++;
+            for (int k=i; k>j; --k) {
+                domain_distances[k] = domain_distances[k-1];
+                sort_indices[k] = sort_indices[k-1];
+            }
+            domain_distances[j] = dist;
+            sort_indices[j] = i;
+        }
+    }
+
+    /**
+     * Method that computes the squared distance from the query point q
+     * from inside region with center c to the border between this
+     * region and the region with center p
+     */
+    DistanceType getDistanceToBorder(DistanceType* p, DistanceType* c, DistanceType* q) const
+    {
+        DistanceType sum = 0;
+        DistanceType sum2 = 0;
+
+        for (int i=0; i<veclen_; ++i) {
+            DistanceType t = c[i]-p[i];
+            sum += t*(q[i]-(c[i]+p[i])/2);
+            sum2 += t*t;
+        }
+
+        return sum*sum/sum2;
+    }
+
+
+    /**
+     * Helper function the descends in the hierarchical k-means tree by spliting those clusters that minimize
+     * the overall variance of the clustering.
+     * Params:
+     *     root = root node
+     *     clusters = array with clusters centers (return value)
+     *     varianceValue = variance of the clustering (return value)
+     * Returns:
+     */
+    int getMinVarianceClusters(NodePtr root, std::vector<NodePtr>& clusters, int clusters_length, DistanceType& varianceValue) const
+    {
+        int clusterCount = 1;
+        clusters[0] = root;
+
+        DistanceType meanVariance = root->variance*root->size;
+
+        while (clusterCount<clusters_length) {
+            DistanceType minVariance = (std::numeric_limits<DistanceType>::max)();
+            int splitIndex = -1;
+
+            for (int i=0; i<clusterCount; ++i) {
+                if (!clusters[i]->childs.empty()) {
+
+                    DistanceType variance = meanVariance - clusters[i]->variance*clusters[i]->size;
+
+                    for (int j=0; j<branching_; ++j) {
+                        variance += clusters[i]->childs[j]->variance*clusters[i]->childs[j]->size;
+                    }
+                    if (variance<minVariance) {
+                        minVariance = variance;
+                        splitIndex = i;
+                    }
+                }
+            }
+
+            if (splitIndex==-1) break;
+            if ( (branching_+clusterCount-1) > clusters_length) break;
+
+            meanVariance = minVariance;
+
+            // split node
+            NodePtr toSplit = clusters[splitIndex];
+            clusters[splitIndex] = toSplit->childs[0];
+            for (int i=1; i<branching_; ++i) {
+                clusters[clusterCount++] = toSplit->childs[i];
+            }
+        }
+
+        varianceValue = meanVariance/root->size;
+        return clusterCount;
+    }
+    
+    void addPointToTree(NodePtr node, size_t index, DistanceType dist_to_pivot)
+    {
+        ElementType* point = points_[index];
+        if (dist_to_pivot>node->radius) {
+            node->radius = dist_to_pivot;
+        }
+        // if radius changed above, the variance will be an approximation
+        node->variance = (node->size*node->variance+dist_to_pivot)/(node->size+1);
+        node->size++;
+        
+        if (node->childs.empty()) { // leaf node
+        	PointInfo point_info;
+        	point_info.index = index;
+        	point_info.point = point;
+        	node->points.push_back(point_info);
+
+            std::vector<int> indices(node->points.size());
+            for (size_t i=0;i<node->points.size();++i) {
+            	indices[i] = node->points[i].index;
+            }
+            computeNodeStatistics(node, indices);
+            if (indices.size()>=size_t(branching_)) {
+                computeClustering(node, &indices[0], indices.size(), branching_);
+            }
+        }
+        else {            
+            // find the closest child
+            int closest = 0;
+            DistanceType dist = distance_(node->childs[closest]->pivot, point, veclen_);
+            for (size_t i=1;i<size_t(branching_);++i) {
+                DistanceType crt_dist = distance_(node->childs[i]->pivot, point, veclen_);
+                if (crt_dist<dist) {
+                    dist = crt_dist;
+                    closest = i;
+                }
+            }
+            addPointToTree(node->childs[closest], index, dist);
+        }                
+    }
+
+
+    void swap(KMeansIndex& other)
+    {
+    	std::swap(branching_, other.branching_);
+    	std::swap(iterations_, other.iterations_);
+    	std::swap(centers_init_, other.centers_init_);
+    	std::swap(cb_index_, other.cb_index_);
+    	std::swap(root_, other.root_);
+    	std::swap(pool_, other.pool_);
+    	std::swap(memoryCounter_, other.memoryCounter_);
+    	std::swap(chooseCenters_, other.chooseCenters_);
+    }
+
+
+private:
+    /** The branching factor used in the hierarchical k-means clustering */
+    int branching_;
+
+    /** Maximum number of iterations to use when performing k-means clustering */
+    int iterations_;
+
+    /** Algorithm for choosing the cluster centers */
+    flann_centers_init_t centers_init_;
+
+    /**
+     * Cluster border index. This is used in the tree search phase when determining
+     * the closest cluster to explore next. A zero value takes into account only
+     * the cluster centres, a value greater then zero also take into account the size
+     * of the cluster.
+     */
+    float cb_index_;
+    
+    /**
+     * The root node in the tree.
+     */
+    NodePtr root_;
+
+    /**
+     * Pooled memory allocator.
+     */
+    PooledAllocator pool_;
+
+    /**
+     * Memory occupied by the index.
+     */
+    int memoryCounter_;
+
+    /**
+     * Algorithm used to choose initial centers
+     */
+    CenterChooser<Distance>* chooseCenters_;
+
+    USING_BASECLASS_SYMBOLS
+};
+
+}
+
+#endif //FLANN_KMEANS_INDEX_H_
diff --git a/src/cpp/flann/algorithms/linear_index.h b/src/cpp/flann/algorithms/linear_index.h
new file mode 100644
index 0000000..22728e3
--- /dev/null
+++ b/src/cpp/flann/algorithms/linear_index.h
@@ -0,0 +1,163 @@
+/***********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2008-2009  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+ * Copyright 2008-2009  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+ *
+ * THE BSD LICENSE
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+
+#ifndef FLANN_LINEAR_INDEX_H_
+#define FLANN_LINEAR_INDEX_H_
+
+#include "flann/general.h"
+#include "flann/algorithms/nn_index.h"
+
+namespace flann
+{
+
+struct LinearIndexParams : public IndexParams
+{
+    LinearIndexParams()
+    {
+        (* this)["algorithm"] = FLANN_INDEX_LINEAR;
+    }
+};
+
+template <typename Distance>
+class LinearIndex : public NNIndex<Distance>
+{
+public:
+
+    typedef typename Distance::ElementType ElementType;
+    typedef typename Distance::ResultType DistanceType;
+
+    typedef NNIndex<Distance> BaseClass;
+
+    LinearIndex(const IndexParams& params = LinearIndexParams(), Distance d = Distance()) :
+    	BaseClass(params, d)
+    {
+    }
+
+    LinearIndex(const Matrix<ElementType>& input_data, const IndexParams& params = LinearIndexParams(), Distance d = Distance()) :
+    	BaseClass(params, d)
+    {
+        setDataset(input_data);
+    }
+
+    LinearIndex(const LinearIndex& other) : BaseClass(other)
+    {
+    }
+
+    LinearIndex& operator=(LinearIndex other)
+    {
+    	this->swap(other);
+    	return *this;
+    }
+
+    virtual ~LinearIndex()
+    {
+    }
+
+    BaseClass* clone() const
+    {
+    	return new LinearIndex(*this);
+    }
+
+    void addPoints(const Matrix<ElementType>& points, float rebuild_threshold = 2)
+    {
+        assert(points.cols==veclen_);
+        extendDataset(points);
+    }
+
+    flann_algorithm_t getType() const
+    {
+        return FLANN_INDEX_LINEAR;
+    }
+
+
+    int usedMemory() const
+    {
+        return 0;
+    }
+
+    template<typename Archive>
+    void serialize(Archive& ar)
+    {
+    	ar.setObject(this);
+
+    	ar & *static_cast<NNIndex<Distance>*>(this);
+
+    	if (Archive::is_loading::value) {
+            index_params_["algorithm"] = getType();
+    	}
+    }
+
+    void saveIndex(FILE* stream)
+    {
+    	serialization::SaveArchive sa(stream);
+    	sa & *this;
+    }
+
+    void loadIndex(FILE* stream)
+    {
+    	serialization::LoadArchive la(stream);
+    	la & *this;
+    }
+
+    void findNeighbors(ResultSet<DistanceType>& resultSet, const ElementType* vec, const SearchParams& /*searchParams*/) const
+    {
+    	if (removed_) {
+    		for (size_t i = 0; i < points_.size(); ++i) {
+    			if (removed_points_.test(i)) continue;
+    			DistanceType dist = distance_(points_[i], vec, veclen_);
+    			resultSet.addPoint(dist, i);
+    		}
+    	}
+    	else {
+    		for (size_t i = 0; i < points_.size(); ++i) {
+    			DistanceType dist = distance_(points_[i], vec, veclen_);
+    			resultSet.addPoint(dist, i);
+    		}
+    	}
+    }
+protected:
+    void buildIndexImpl()
+    {
+        /* nothing to do here for linear search */
+    }
+
+    void freeIndex()
+    {
+        /* nothing to do here for linear search */
+    }
+
+private:
+
+    USING_BASECLASS_SYMBOLS
+};
+
+}
+
+#endif // FLANN_LINEAR_INDEX_H_
diff --git a/src/cpp/flann/algorithms/lsh_index.h b/src/cpp/flann/algorithms/lsh_index.h
new file mode 100644
index 0000000..0c3619c
--- /dev/null
+++ b/src/cpp/flann/algorithms/lsh_index.h
@@ -0,0 +1,548 @@
+/***********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2008-2009  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+ * Copyright 2008-2009  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+ *
+ * THE BSD LICENSE
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+
+/***********************************************************************
+ * Author: Vincent Rabaud
+ *************************************************************************/
+
+#ifndef FLANN_LSH_INDEX_H_
+#define FLANN_LSH_INDEX_H_
+
+#include <algorithm>
+#include <cassert>
+#include <cstring>
+#include <map>
+#include <vector>
+
+#include "flann/general.h"
+#include "flann/algorithms/nn_index.h"
+#include "flann/util/matrix.h"
+#include "flann/util/result_set.h"
+#include "flann/util/heap.h"
+#include "flann/util/lsh_table.h"
+#include "flann/util/allocator.h"
+#include "flann/util/random.h"
+#include "flann/util/saving.h"
+
+namespace flann
+{
+
+struct LshIndexParams : public IndexParams
+{
+    LshIndexParams(unsigned int table_number = 12, unsigned int key_size = 20, unsigned int multi_probe_level = 2)
+    {
+        (* this)["algorithm"] = FLANN_INDEX_LSH;
+        // The number of hash tables to use
+        (*this)["table_number"] = table_number;
+        // The length of the key in the hash tables
+        (*this)["key_size"] = key_size;
+        // Number of levels to use in multi-probe (0 for standard LSH)
+        (*this)["multi_probe_level"] = multi_probe_level;
+    }
+};
+
+/**
+ * Randomized kd-tree index
+ *
+ * Contains the k-d trees and other information for indexing a set of points
+ * for nearest-neighbor matching.
+ */
+template<typename Distance>
+class LshIndex : public NNIndex<Distance>
+{
+public:
+    typedef typename Distance::ElementType ElementType;
+    typedef typename Distance::ResultType DistanceType;
+
+    typedef NNIndex<Distance> BaseClass;
+
+    /** Constructor
+     * @param params parameters passed to the LSH algorithm
+     * @param d the distance used
+     */
+    LshIndex(const IndexParams& params = LshIndexParams(), Distance d = Distance()) :
+    	BaseClass(params, d)
+    {
+        table_number_ = get_param<unsigned int>(index_params_,"table_number",12);
+        key_size_ = get_param<unsigned int>(index_params_,"key_size",20);
+        multi_probe_level_ = get_param<unsigned int>(index_params_,"multi_probe_level",2);
+
+        fill_xor_mask(0, key_size_, multi_probe_level_, xor_masks_);
+    }
+
+
+    /** Constructor
+     * @param input_data dataset with the input features
+     * @param params parameters passed to the LSH algorithm
+     * @param d the distance used
+     */
+    LshIndex(const Matrix<ElementType>& input_data, const IndexParams& params = LshIndexParams(), Distance d = Distance()) :
+    	BaseClass(params, d)
+    {
+        table_number_ = get_param<unsigned int>(index_params_,"table_number",12);
+        key_size_ = get_param<unsigned int>(index_params_,"key_size",20);
+        multi_probe_level_ = get_param<unsigned int>(index_params_,"multi_probe_level",2);
+
+        fill_xor_mask(0, key_size_, multi_probe_level_, xor_masks_);
+
+        setDataset(input_data);
+    }
+
+    LshIndex(const LshIndex& other) : BaseClass(other),
+    	tables_(other.tables_),
+    	table_number_(other.table_number_),
+    	key_size_(other.key_size_),
+    	multi_probe_level_(other.multi_probe_level_),
+    	xor_masks_(other.xor_masks_)
+    {
+    }
+    
+    LshIndex& operator=(LshIndex other)
+    {
+    	this->swap(other);
+    	return *this;
+    }
+
+    virtual ~LshIndex()
+    {
+    	freeIndex();
+    }
+
+
+    BaseClass* clone() const
+    {
+    	return new LshIndex(*this);
+    }
+    
+    using BaseClass::buildIndex;
+
+    void addPoints(const Matrix<ElementType>& points, float rebuild_threshold = 2)
+    {
+        assert(points.cols==veclen_);
+        size_t old_size = size_;
+
+        extendDataset(points);
+        
+        if (rebuild_threshold>1 && size_at_build_*rebuild_threshold<size_) {
+            buildIndex();
+        }
+        else {
+            for (unsigned int i = 0; i < table_number_; ++i) {
+                lsh::LshTable<ElementType>& table = tables_[i];                
+                for (size_t i=old_size;i<size_;++i) {
+                    table.add(i, points_[i]);
+                }            
+            }
+        }
+    }
+
+
+    flann_algorithm_t getType() const
+    {
+        return FLANN_INDEX_LSH;
+    }
+
+
+    template<typename Archive>
+    void serialize(Archive& ar)
+    {
+    	ar.setObject(this);
+
+    	ar & *static_cast<NNIndex<Distance>*>(this);
+
+    	ar & table_number_;
+    	ar & key_size_;
+    	ar & multi_probe_level_;
+
+    	ar & xor_masks_;
+    	ar & tables_;
+
+    	if (Archive::is_loading::value) {
+            index_params_["algorithm"] = getType();
+            index_params_["table_number"] = table_number_;
+            index_params_["key_size"] = key_size_;
+            index_params_["multi_probe_level"] = multi_probe_level_;
+    	}
+    }
+
+    void saveIndex(FILE* stream)
+    {
+    	serialization::SaveArchive sa(stream);
+    	sa & *this;
+    }
+
+    void loadIndex(FILE* stream)
+    {
+    	serialization::LoadArchive la(stream);
+    	la & *this;
+    }
+
+    /**
+     * Computes the index memory usage
+     * Returns: memory used by the index
+     */
+    int usedMemory() const
+    {
+        return size_ * sizeof(int);
+    }
+
+    /**
+     * \brief Perform k-nearest neighbor search
+     * \param[in] queries The query points for which to find the nearest neighbors
+     * \param[out] indices The indices of the nearest neighbors found
+     * \param[out] dists Distances to the nearest neighbors found
+     * \param[in] knn Number of nearest neighbors to return
+     * \param[in] params Search parameters
+     */
+    int knnSearch(const Matrix<ElementType>& queries,
+    					Matrix<size_t>& indices,
+    					Matrix<DistanceType>& dists,
+    					size_t knn,
+    					const SearchParams& params) const
+    {
+        assert(queries.cols == veclen_);
+        assert(indices.rows >= queries.rows);
+        assert(dists.rows >= queries.rows);
+        assert(indices.cols >= knn);
+        assert(dists.cols >= knn);
+
+        int count = 0;
+        if (params.use_heap==FLANN_True) {
+#pragma omp parallel num_threads(params.cores)
+        	{
+        		KNNUniqueResultSet<DistanceType> resultSet(knn);
+#pragma omp for schedule(static) reduction(+:count)
+        		for (int i = 0; i < (int)queries.rows; i++) {
+        			resultSet.clear();
+        			findNeighbors(resultSet, queries[i], params);
+        			size_t n = std::min(resultSet.size(), knn);
+        			resultSet.copy(indices[i], dists[i], n, params.sorted);
+        			indices_to_ids(indices[i], indices[i], n);
+        			count += n;
+        		}
+        	}
+        }
+        else {
+#pragma omp parallel num_threads(params.cores)
+        	{
+        		KNNResultSet<DistanceType> resultSet(knn);
+#pragma omp for schedule(static) reduction(+:count)
+        		for (int i = 0; i < (int)queries.rows; i++) {
+        			resultSet.clear();
+        			findNeighbors(resultSet, queries[i], params);
+        			size_t n = std::min(resultSet.size(), knn);
+        			resultSet.copy(indices[i], dists[i], n, params.sorted);
+        			indices_to_ids(indices[i], indices[i], n);
+        			count += n;
+        		}
+        	}
+        }
+
+        return count;
+    }
+
+    /**
+     * \brief Perform k-nearest neighbor search
+     * \param[in] queries The query points for which to find the nearest neighbors
+     * \param[out] indices The indices of the nearest neighbors found
+     * \param[out] dists Distances to the nearest neighbors found
+     * \param[in] knn Number of nearest neighbors to return
+     * \param[in] params Search parameters
+     */
+    int knnSearch(const Matrix<ElementType>& queries,
+					std::vector< std::vector<size_t> >& indices,
+					std::vector<std::vector<DistanceType> >& dists,
+    				size_t knn,
+    				const SearchParams& params) const
+    {
+        assert(queries.cols == veclen_);
+		if (indices.size() < queries.rows ) indices.resize(queries.rows);
+		if (dists.size() < queries.rows ) dists.resize(queries.rows);
+
+		int count = 0;
+		if (params.use_heap==FLANN_True) {
+#pragma omp parallel num_threads(params.cores)
+			{
+				KNNUniqueResultSet<DistanceType> resultSet(knn);
+#pragma omp for schedule(static) reduction(+:count)
+				for (int i = 0; i < (int)queries.rows; i++) {
+					resultSet.clear();
+					findNeighbors(resultSet, queries[i], params);
+					size_t n = std::min(resultSet.size(), knn);
+					indices[i].resize(n);
+					dists[i].resize(n);
+					if (n > 0) {
+						resultSet.copy(&indices[i][0], &dists[i][0], n, params.sorted);
+						indices_to_ids(&indices[i][0], &indices[i][0], n);
+					}
+					count += n;
+				}
+			}
+		}
+		else {
+#pragma omp parallel num_threads(params.cores)
+			{
+				KNNResultSet<DistanceType> resultSet(knn);
+#pragma omp for schedule(static) reduction(+:count)
+				for (int i = 0; i < (int)queries.rows; i++) {
+					resultSet.clear();
+					findNeighbors(resultSet, queries[i], params);
+					size_t n = std::min(resultSet.size(), knn);
+					indices[i].resize(n);
+					dists[i].resize(n);
+					if (n > 0) {
+						resultSet.copy(&indices[i][0], &dists[i][0], n, params.sorted);
+						indices_to_ids(&indices[i][0], &indices[i][0], n);
+					}
+					count += n;
+				}
+			}
+		}
+
+		return count;
+    }
+
+    /**
+     * Find set of nearest neighbors to vec. Their indices are stored inside
+     * the result object.
+     *
+     * Params:
+     *     result = the result object in which the indices of the nearest-neighbors are stored
+     *     vec = the vector for which to search the nearest neighbors
+     *     maxCheck = the maximum number of restarts (in a best-bin-first manner)
+     */
+    void findNeighbors(ResultSet<DistanceType>& result, const ElementType* vec, const SearchParams& /*searchParams*/) const
+    {
+        getNeighbors(vec, result);
+    }
+
+protected:
+
+    /**
+     * Builds the index
+     */
+    void buildIndexImpl()
+    {
+        tables_.resize(table_number_);
+        std::vector<std::pair<size_t,ElementType*> > features;
+        features.reserve(points_.size());
+        for (size_t i=0;i<points_.size();++i) {
+        	features.push_back(std::make_pair(i, points_[i]));
+        }
+        for (unsigned int i = 0; i < table_number_; ++i) {
+            lsh::LshTable<ElementType>& table = tables_[i];
+            table = lsh::LshTable<ElementType>(veclen_, key_size_);
+
+            // Add the features to the table
+            table.add(features);
+        }
+    }
+
+    void freeIndex()
+    {
+        /* nothing to do here */
+    }
+
+
+private:
+    /** Defines the comparator on score and index
+     */
+    typedef std::pair<float, unsigned int> ScoreIndexPair;
+    struct SortScoreIndexPairOnSecond
+    {
+        bool operator()(const ScoreIndexPair& left, const ScoreIndexPair& right) const
+        {
+            return left.second < right.second;
+        }
+    };
+
+    /** Fills the different xor masks to use when getting the neighbors in multi-probe LSH
+     * @param key the key we build neighbors from
+     * @param lowest_index the lowest index of the bit set
+     * @param level the multi-probe level we are at
+     * @param xor_masks all the xor mask
+     */
+    void fill_xor_mask(lsh::BucketKey key, int lowest_index, unsigned int level,
+                       std::vector<lsh::BucketKey>& xor_masks)
+    {
+        xor_masks.push_back(key);
+        if (level == 0) return;
+        for (int index = lowest_index - 1; index >= 0; --index) {
+            // Create a new key
+            lsh::BucketKey new_key = key | (1 << index);
+            fill_xor_mask(new_key, index, level - 1, xor_masks);
+        }
+    }
+
+    /** Performs the approximate nearest-neighbor search.
+     * @param vec the feature to analyze
+     * @param do_radius flag indicating if we check the radius too
+     * @param radius the radius if it is a radius search
+     * @param do_k flag indicating if we limit the number of nn
+     * @param k_nn the number of nearest neighbors
+     * @param checked_average used for debugging
+     */
+    void getNeighbors(const ElementType* vec, bool do_radius, float radius, bool do_k, unsigned int k_nn,
+                      float& checked_average)
+    {
+        static std::vector<ScoreIndexPair> score_index_heap;
+
+        if (do_k) {
+            unsigned int worst_score = std::numeric_limits<unsigned int>::max();
+            typename std::vector<lsh::LshTable<ElementType> >::const_iterator table = tables_.begin();
+            typename std::vector<lsh::LshTable<ElementType> >::const_iterator table_end = tables_.end();
+            for (; table != table_end; ++table) {
+                size_t key = table->getKey(vec);
+                std::vector<lsh::BucketKey>::const_iterator xor_mask = xor_masks_.begin();
+                std::vector<lsh::BucketKey>::const_iterator xor_mask_end = xor_masks_.end();
+                for (; xor_mask != xor_mask_end; ++xor_mask) {
+                    size_t sub_key = key ^ (*xor_mask);
+                    const lsh::Bucket* bucket = table->getBucketFromKey(sub_key);
+                    if (bucket == 0) continue;
+
+                    // Go over each descriptor index
+                    std::vector<lsh::FeatureIndex>::const_iterator training_index = bucket->begin();
+                    std::vector<lsh::FeatureIndex>::const_iterator last_training_index = bucket->end();
+                    DistanceType hamming_distance;
+
+                    // Process the rest of the candidates
+                    for (; training_index < last_training_index; ++training_index) {
+                    	if (removed_ && removed_points_.test(*training_index)) continue;
+                        hamming_distance = distance_(vec, points_[*training_index].point, veclen_);
+
+                        if (hamming_distance < worst_score) {
+                            // Insert the new element
+                            score_index_heap.push_back(ScoreIndexPair(hamming_distance, training_index));
+                            std::push_heap(score_index_heap.begin(), score_index_heap.end());
+
+                            if (score_index_heap.size() > (unsigned int)k_nn) {
+                                // Remove the highest distance value as we have too many elements
+                                std::pop_heap(score_index_heap.begin(), score_index_heap.end());
+                                score_index_heap.pop_back();
+                                // Keep track of the worst score
+                                worst_score = score_index_heap.front().first;
+                            }
+                        }
+                    }
+                }
+            }
+        }
+        else {
+            typename std::vector<lsh::LshTable<ElementType> >::const_iterator table = tables_.begin();
+            typename std::vector<lsh::LshTable<ElementType> >::const_iterator table_end = tables_.end();
+            for (; table != table_end; ++table) {
+                size_t key = table->getKey(vec);
+                std::vector<lsh::BucketKey>::const_iterator xor_mask = xor_masks_.begin();
+                std::vector<lsh::BucketKey>::const_iterator xor_mask_end = xor_masks_.end();
+                for (; xor_mask != xor_mask_end; ++xor_mask) {
+                    size_t sub_key = key ^ (*xor_mask);
+                    const lsh::Bucket* bucket = table->getBucketFromKey(sub_key);
+                    if (bucket == 0) continue;
+
+                    // Go over each descriptor index
+                    std::vector<lsh::FeatureIndex>::const_iterator training_index = bucket->begin();
+                    std::vector<lsh::FeatureIndex>::const_iterator last_training_index = bucket->end();
+                    DistanceType hamming_distance;
+
+                    // Process the rest of the candidates
+                    for (; training_index < last_training_index; ++training_index) {
+                    	if (removed_ && removed_points_.test(*training_index)) continue;
+                        // Compute the Hamming distance
+                        hamming_distance = distance_(vec, points_[*training_index].point, veclen_);
+                        if (hamming_distance < radius) score_index_heap.push_back(ScoreIndexPair(hamming_distance, training_index));
+                    }
+                }
+            }
+        }
+    }
+
+    /** Performs the approximate nearest-neighbor search.
+     * This is a slower version than the above as it uses the ResultSet
+     * @param vec the feature to analyze
+     */
+    void getNeighbors(const ElementType* vec, ResultSet<DistanceType>& result) const
+    {
+        typename std::vector<lsh::LshTable<ElementType> >::const_iterator table = tables_.begin();
+        typename std::vector<lsh::LshTable<ElementType> >::const_iterator table_end = tables_.end();
+        for (; table != table_end; ++table) {
+            size_t key = table->getKey(vec);
+            std::vector<lsh::BucketKey>::const_iterator xor_mask = xor_masks_.begin();
+            std::vector<lsh::BucketKey>::const_iterator xor_mask_end = xor_masks_.end();
+            for (; xor_mask != xor_mask_end; ++xor_mask) {
+                size_t sub_key = key ^ (*xor_mask);
+                const lsh::Bucket* bucket = table->getBucketFromKey(sub_key);
+                if (bucket == 0) continue;
+
+                // Go over each descriptor index
+                std::vector<lsh::FeatureIndex>::const_iterator training_index = bucket->begin();
+                std::vector<lsh::FeatureIndex>::const_iterator last_training_index = bucket->end();
+                DistanceType hamming_distance;
+
+                // Process the rest of the candidates
+                for (; training_index < last_training_index; ++training_index) {
+                	if (removed_ && removed_points_.test(*training_index)) continue;
+                    // Compute the Hamming distance
+                    hamming_distance = distance_(vec, points_[*training_index], veclen_);
+                    result.addPoint(hamming_distance, *training_index);
+                }
+            }
+        }
+    }
+
+
+    void swap(LshIndex& other)
+    {
+    	BaseClass::swap(other);
+    	std::swap(tables_, other.tables_);
+    	std::swap(size_at_build_, other.size_at_build_);
+    	std::swap(table_number_, other.table_number_);
+    	std::swap(key_size_, other.key_size_);
+    	std::swap(multi_probe_level_, other.multi_probe_level_);
+    	std::swap(xor_masks_, other.xor_masks_);
+    }
+
+    /** The different hash tables */
+    std::vector<lsh::LshTable<ElementType> > tables_;
+    
+    /** table number */
+    unsigned int table_number_;
+    /** key size */
+    unsigned int key_size_;
+    /** How far should we look for neighbors in multi-probe LSH */
+    unsigned int multi_probe_level_;
+
+    /** The XOR masks to apply to a key to get the neighboring buckets */
+    std::vector<lsh::BucketKey> xor_masks_;
+
+    USING_BASECLASS_SYMBOLS
+};
+}
+
+#endif //FLANN_LSH_INDEX_H_
diff --git a/src/cpp/flann/algorithms/nn_index.h b/src/cpp/flann/algorithms/nn_index.h
new file mode 100644
index 0000000..3e62a83
--- /dev/null
+++ b/src/cpp/flann/algorithms/nn_index.h
@@ -0,0 +1,903 @@
+/***********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2008-2009  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+ * Copyright 2008-2009  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+ *
+ * THE BSD LICENSE
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+
+#ifndef FLANN_NNINDEX_H
+#define FLANN_NNINDEX_H
+
+#include <vector>
+
+#include "flann/general.h"
+#include "flann/util/matrix.h"
+#include "flann/util/params.h"
+#include "flann/util/result_set.h"
+#include "flann/util/dynamic_bitset.h"
+#include "flann/util/saving.h"
+
+namespace flann
+{
+
+#define KNN_HEAP_THRESHOLD 250
+
+
+class IndexBase
+{
+public:
+    virtual ~IndexBase() {};
+
+    virtual size_t veclen() const = 0;
+
+    virtual size_t size() const = 0;
+
+    virtual flann_algorithm_t getType() const = 0;
+
+    virtual int usedMemory() const = 0;
+
+    virtual IndexParams getParameters() const = 0;
+
+    virtual void loadIndex(FILE* stream) = 0;
+
+    virtual void saveIndex(FILE* stream) = 0;
+};
+
+/**
+ * Nearest-neighbour index base class
+ */
+template <typename Distance>
+class NNIndex : public IndexBase
+{
+public:
+    typedef typename Distance::ElementType ElementType;
+    typedef typename Distance::ResultType DistanceType;
+
+	NNIndex(Distance d) : distance_(d), last_id_(0), size_(0), size_at_build_(0), veclen_(0),
+			removed_(false), removed_count_(0), data_ptr_(NULL)
+	{
+	}
+
+	NNIndex(const IndexParams& params, Distance d) : distance_(d), last_id_(0), size_(0), size_at_build_(0), veclen_(0),
+			index_params_(params), removed_(false), removed_count_(0), data_ptr_(NULL)
+	{
+	}
+
+	NNIndex(const NNIndex& other) :
+		distance_(other.distance_),
+		last_id_(other.last_id_),
+		size_(other.size_),
+		size_at_build_(other.size_at_build_),
+		veclen_(other.veclen_),
+		index_params_(other.index_params_),
+		removed_(other.removed_),
+		removed_points_(other.removed_points_),
+		removed_count_(other.removed_count_),
+		ids_(other.ids_),
+		points_(other.points_),
+		data_ptr_(NULL)
+	{
+		if (other.data_ptr_) {
+			data_ptr_ = new ElementType[size_*veclen_];
+			std::copy(other.data_ptr_, other.data_ptr_+size_*veclen_, data_ptr_);
+			for (size_t i=0;i<size_;++i) {
+				points_[i] = data_ptr_ + i*veclen_;
+			}
+		}
+	}
+
+	virtual ~NNIndex()
+	{
+		if (data_ptr_) {
+			delete[] data_ptr_;
+		}
+	}
+
+
+	virtual NNIndex* clone() const = 0;
+
+	/**
+	 * Builds the index
+	 */
+	virtual void buildIndex()
+	{
+    	freeIndex();
+    	cleanRemovedPoints();
+
+    	// building index
+		buildIndexImpl();
+
+        size_at_build_ = size_;
+
+	}
+
+	/**
+	 * Builds th index using using the specified dataset
+	 * @param dataset the dataset to use
+	 */
+    virtual void buildIndex(const Matrix<ElementType>& dataset)
+    {
+        setDataset(dataset);
+        this->buildIndex();
+    }
+
+	/**
+	 * @brief Incrementally add points to the index.
+	 * @param points Matrix with points to be added
+	 * @param rebuild_threshold
+	 */
+    virtual void addPoints(const Matrix<ElementType>& points, float rebuild_threshold = 2)
+    {
+        throw FLANNException("Functionality not supported by this index");
+    }
+
+    /**
+     * Remove point from the index
+     * @param index Index of point to be removed
+     */
+    virtual void removePoint(size_t id)
+    {
+    	if (!removed_) {
+    		ids_.resize(size_);
+    		for (size_t i=0;i<size_;++i) {
+    			ids_[i] = i;
+    		}
+    		removed_points_.resize(size_);
+    		removed_points_.reset();
+    		last_id_ = size_;
+        	removed_ = true;
+    	}
+
+    	size_t point_index = id_to_index(id);
+    	if (point_index!=size_t(-1) && !removed_points_.test(point_index)) {
+    		removed_points_.set(point_index);
+    		removed_count_++;
+    	}
+    }
+
+
+    /**
+     * Get point with specific id
+     * @param id
+     * @return
+     */
+    virtual ElementType* getPoint(size_t id)
+    {
+    	size_t index = id_to_index(id);
+    	if (index!=size_t(-1)) {
+    		return points_[index];
+    	}
+    	else {
+    		return NULL;
+    	}
+    }
+
+    /**
+     * @return number of features in this index.
+     */
+    inline size_t size() const
+    {
+    	return size_ - removed_count_;
+    }
+
+    /**
+     * @return The dimensionality of the features in this index.
+     */
+    inline size_t veclen() const
+    {
+        return veclen_;
+    }
+
+    /**
+     * Returns the parameters used by the index.
+     *
+     * @return The index parameters
+     */
+    IndexParams getParameters() const
+    {
+        return index_params_;
+    }
+
+
+    template<typename Archive>
+    void serialize(Archive& ar)
+    {
+    	IndexHeader header;
+
+    	if (Archive::is_saving::value) {
+    		header.data_type = flann_datatype_value<ElementType>::value;
+    		header.index_type = getType();
+    		header.rows = size_;
+    		header.cols = veclen_;
+    	}
+    	ar & header;
+
+    	// sanity checks
+    	if (Archive::is_loading::value) {
+    	    if (strcmp(header.signature,FLANN_SIGNATURE_)!=0) {
+    	        throw FLANNException("Invalid index file, wrong signature");
+    	    }
+            if (header.data_type != flann_datatype_value<ElementType>::value) {
+                throw FLANNException("Datatype of saved index is different than of the one to be created.");
+            }
+            if (header.index_type != getType()) {
+                throw FLANNException("Saved index type is different then the current index type.");
+            }
+            // TODO: check for distance type
+
+    	}
+
+    	ar & size_;
+    	ar & veclen_;
+    	ar & size_at_build_;
+
+    	bool save_dataset;
+    	if (Archive::is_saving::value) {
+    		save_dataset = get_param(index_params_,"save_dataset", false);
+    	}
+    	ar & save_dataset;
+
+    	if (save_dataset) {
+    		if (Archive::is_loading::value) {
+    			if (data_ptr_) {
+    				delete[] data_ptr_;
+    			}
+    			data_ptr_ = new ElementType[size_*veclen_];
+    			points_.resize(size_);
+        		for (size_t i=0;i<size_;++i) {
+        			points_[i] = data_ptr_ + i*veclen_;
+        		}
+    		}
+    		for (size_t i=0;i<size_;++i) {
+    			ar & serialization::make_binary_object (points_[i], veclen_*sizeof(ElementType));
+    		}
+    	} else {
+    		if (points_.size()!=size_) {
+    			throw FLANNException("Saved index does not contain the dataset and no dataset was provided.");
+    		}
+    	}
+
+    	ar & last_id_;
+    	ar & ids_;
+    	ar & removed_;
+    	if (removed_) {
+    		ar & removed_points_;
+    	}
+    	ar & removed_count_;
+    }
+
+
+    /**
+     * @brief Perform k-nearest neighbor search
+     * @param[in] queries The query points for which to find the nearest neighbors
+     * @param[out] indices The indices of the nearest neighbors found
+     * @param[out] dists Distances to the nearest neighbors found
+     * @param[in] knn Number of nearest neighbors to return
+     * @param[in] params Search parameters
+     */
+    virtual int knnSearch(const Matrix<ElementType>& queries,
+    		Matrix<size_t>& indices,
+    		Matrix<DistanceType>& dists,
+    		size_t knn,
+    		const SearchParams& params) const
+    {
+    	assert(queries.cols == veclen());
+    	assert(indices.rows >= queries.rows);
+    	assert(dists.rows >= queries.rows);
+    	assert(indices.cols >= knn);
+    	assert(dists.cols >= knn);
+    	bool use_heap;
+
+    	if (params.use_heap==FLANN_Undefined) {
+    		use_heap = (knn>KNN_HEAP_THRESHOLD)?true:false;
+    	}
+    	else {
+    		use_heap = (params.use_heap==FLANN_True)?true:false;
+    	}
+    	int count = 0;
+
+    	if (use_heap) {
+#pragma omp parallel num_threads(params.cores)
+    		{
+    			KNNResultSet2<DistanceType> resultSet(knn);
+#pragma omp for schedule(static) reduction(+:count)
+    			for (int i = 0; i < (int)queries.rows; i++) {
+    				resultSet.clear();
+    				findNeighbors(resultSet, queries[i], params);
+    				size_t n = std::min(resultSet.size(), knn);
+    				resultSet.copy(indices[i], dists[i], n, params.sorted);
+    				indices_to_ids(indices[i], indices[i], n);
+    				count += n;
+    			}
+    		}
+    	}
+    	else {
+#pragma omp parallel num_threads(params.cores)
+    		{
+    			KNNSimpleResultSet<DistanceType> resultSet(knn);
+#pragma omp for schedule(static) reduction(+:count)
+    			for (int i = 0; i < (int)queries.rows; i++) {
+    				resultSet.clear();
+    				findNeighbors(resultSet, queries[i], params);
+    				size_t n = std::min(resultSet.size(), knn);
+    				resultSet.copy(indices[i], dists[i], n, params.sorted);
+    				indices_to_ids(indices[i], indices[i], n);
+    				count += n;
+    			}
+    		}
+    	}
+    	return count;
+    }
+
+    /**
+     *
+     * @param queries
+     * @param indices
+     * @param dists
+     * @param knn
+     * @param params
+     * @return
+     */
+    int knnSearch(const Matrix<ElementType>& queries,
+                                 Matrix<int>& indices,
+                                 Matrix<DistanceType>& dists,
+                                 size_t knn,
+                           const SearchParams& params) const
+    {
+    	flann::Matrix<size_t> indices_(new size_t[indices.rows*indices.cols], indices.rows, indices.cols);
+    	int result = knnSearch(queries, indices_, dists, knn, params);
+
+    	for (size_t i=0;i<indices.rows;++i) {
+    		for (size_t j=0;j<indices.cols;++j) {
+    			indices[i][j] = indices_[i][j];
+    		}
+    	}
+        delete[] indices_.ptr();
+    	return result;
+    }
+
+
+    /**
+     * @brief Perform k-nearest neighbor search
+     * @param[in] queries The query points for which to find the nearest neighbors
+     * @param[out] indices The indices of the nearest neighbors found
+     * @param[out] dists Distances to the nearest neighbors found
+     * @param[in] knn Number of nearest neighbors to return
+     * @param[in] params Search parameters
+     */
+    int knnSearch(const Matrix<ElementType>& queries,
+					std::vector< std::vector<size_t> >& indices,
+					std::vector<std::vector<DistanceType> >& dists,
+    				size_t knn,
+    				const SearchParams& params) const
+    {
+        assert(queries.cols == veclen());
+        bool use_heap;
+        if (params.use_heap==FLANN_Undefined) {
+        	use_heap = (knn>KNN_HEAP_THRESHOLD)?true:false;
+        }
+        else {
+        	use_heap = (params.use_heap==FLANN_True)?true:false;
+        }
+
+        if (indices.size() < queries.rows ) indices.resize(queries.rows);
+		if (dists.size() < queries.rows ) dists.resize(queries.rows);
+
+		int count = 0;
+		if (use_heap) {
+#pragma omp parallel num_threads(params.cores)
+			{
+				KNNResultSet2<DistanceType> resultSet(knn);
+#pragma omp for schedule(static) reduction(+:count)
+				for (int i = 0; i < (int)queries.rows; i++) {
+					resultSet.clear();
+					findNeighbors(resultSet, queries[i], params);
+					size_t n = std::min(resultSet.size(), knn);
+					indices[i].resize(n);
+					dists[i].resize(n);
+					if (n>0) {
+						resultSet.copy(&indices[i][0], &dists[i][0], n, params.sorted);
+						indices_to_ids(&indices[i][0], &indices[i][0], n);
+					}
+					count += n;
+				}
+			}
+		}
+		else {
+#pragma omp parallel num_threads(params.cores)
+			{
+				KNNSimpleResultSet<DistanceType> resultSet(knn);
+#pragma omp for schedule(static) reduction(+:count)
+				for (int i = 0; i < (int)queries.rows; i++) {
+					resultSet.clear();
+					findNeighbors(resultSet, queries[i], params);
+					size_t n = std::min(resultSet.size(), knn);
+					indices[i].resize(n);
+					dists[i].resize(n);
+					if (n>0) {
+						resultSet.copy(&indices[i][0], &dists[i][0], n, params.sorted);
+						indices_to_ids(&indices[i][0], &indices[i][0], n);
+					}
+					count += n;
+				}
+			}
+		}
+
+		return count;
+    }
+
+
+    /**
+     *
+     * @param queries
+     * @param indices
+     * @param dists
+     * @param knn
+     * @param params
+     * @return
+     */
+    int knnSearch(const Matrix<ElementType>& queries,
+                                 std::vector< std::vector<int> >& indices,
+                                 std::vector<std::vector<DistanceType> >& dists,
+                                 size_t knn,
+                           const SearchParams& params) const
+    {
+    	std::vector<std::vector<size_t> > indices_;
+    	int result = knnSearch(queries, indices_, dists, knn, params);
+
+    	indices.resize(indices_.size());
+    	for (size_t i=0;i<indices_.size();++i) {
+            indices[i].assign(indices_[i].begin(), indices_[i].end());
+    	}
+    	return result;
+    }
+
+    /**
+     * @brief Perform radius search
+     * @param[in] query The query point
+     * @param[out] indices The indinces of the neighbors found within the given radius
+     * @param[out] dists The distances to the nearest neighbors found
+     * @param[in] radius The radius used for search
+     * @param[in] params Search parameters
+     * @return Number of neighbors found
+     */
+    int radiusSearch(const Matrix<ElementType>& queries,
+    		Matrix<size_t>& indices,
+    		Matrix<DistanceType>& dists,
+    		float radius,
+    		const SearchParams& params) const
+    {
+    	assert(queries.cols == veclen());
+    	int count = 0;
+    	size_t num_neighbors = std::min(indices.cols, dists.cols);
+    	int max_neighbors = params.max_neighbors;
+    	if (max_neighbors<0) max_neighbors = num_neighbors;
+    	else max_neighbors = std::min(max_neighbors,(int)num_neighbors);
+
+    	if (max_neighbors==0) {
+#pragma omp parallel num_threads(params.cores)
+    		{
+    			CountRadiusResultSet<DistanceType> resultSet(radius);
+#pragma omp for schedule(static) reduction(+:count)
+    			for (int i = 0; i < (int)queries.rows; i++) {
+    				resultSet.clear();
+    				findNeighbors(resultSet, queries[i], params);
+    				count += resultSet.size();
+    			}
+    		}
+    	}
+    	else {
+    		// explicitly indicated to use unbounded radius result set
+    		// and we know there'll be enough room for resulting indices and dists
+    		if (params.max_neighbors<0 && (num_neighbors>=size())) {
+#pragma omp parallel num_threads(params.cores)
+    			{
+    				RadiusResultSet<DistanceType> resultSet(radius);
+#pragma omp for schedule(static) reduction(+:count)
+    				for (int i = 0; i < (int)queries.rows; i++) {
+    					resultSet.clear();
+    					findNeighbors(resultSet, queries[i], params);
+    					size_t n = resultSet.size();
+    					count += n;
+    					if (n>num_neighbors) n = num_neighbors;
+    					resultSet.copy(indices[i], dists[i], n, params.sorted);
+
+    					// mark the next element in the output buffers as unused
+    					if (n<indices.cols) indices[i][n] = size_t(-1);
+    					if (n<dists.cols) dists[i][n] = std::numeric_limits<DistanceType>::infinity();
+    					indices_to_ids(indices[i], indices[i], n);
+    				}
+    			}
+    		}
+    		else {
+    			// number of neighbors limited to max_neighbors
+#pragma omp parallel num_threads(params.cores)
+    			{
+    				KNNRadiusResultSet<DistanceType> resultSet(radius, max_neighbors);
+#pragma omp for schedule(static) reduction(+:count)
+    				for (int i = 0; i < (int)queries.rows; i++) {
+    					resultSet.clear();
+    					findNeighbors(resultSet, queries[i], params);
+    					size_t n = resultSet.size();
+    					count += n;
+    					if ((int)n>max_neighbors) n = max_neighbors;
+    					resultSet.copy(indices[i], dists[i], n, params.sorted);
+
+    					// mark the next element in the output buffers as unused
+    					if (n<indices.cols) indices[i][n] = size_t(-1);
+    					if (n<dists.cols) dists[i][n] = std::numeric_limits<DistanceType>::infinity();
+    					indices_to_ids(indices[i], indices[i], n);
+    				}
+    			}
+    		}
+    	}
+        return count;
+    }
+
+
+    /**
+     *
+     * @param queries
+     * @param indices
+     * @param dists
+     * @param radius
+     * @param params
+     * @return
+     */
+    int radiusSearch(const Matrix<ElementType>& queries,
+                                    Matrix<int>& indices,
+                                    Matrix<DistanceType>& dists,
+                                    float radius,
+                              const SearchParams& params) const
+    {
+    	flann::Matrix<size_t> indices_(new size_t[indices.rows*indices.cols], indices.rows, indices.cols);
+    	int result = radiusSearch(queries, indices_, dists, radius, params);
+
+    	for (size_t i=0;i<indices.rows;++i) {
+    		for (size_t j=0;j<indices.cols;++j) {
+    			indices[i][j] = indices_[i][j];
+    		}
+    	}
+        delete[] indices_.ptr();
+    	return result;
+    }
+
+    /**
+     * @brief Perform radius search
+     * @param[in] query The query point
+     * @param[out] indices The indinces of the neighbors found within the given radius
+     * @param[out] dists The distances to the nearest neighbors found
+     * @param[in] radius The radius used for search
+     * @param[in] params Search parameters
+     * @return Number of neighbors found
+     */
+    int radiusSearch(const Matrix<ElementType>& queries,
+    		std::vector< std::vector<size_t> >& indices,
+    		std::vector<std::vector<DistanceType> >& dists,
+    		float radius,
+    		const SearchParams& params) const
+    {
+        assert(queries.cols == veclen());
+    	int count = 0;
+    	// just count neighbors
+    	if (params.max_neighbors==0) {
+#pragma omp parallel num_threads(params.cores)
+    		{
+    			CountRadiusResultSet<DistanceType> resultSet(radius);
+#pragma omp for schedule(static) reduction(+:count)
+    			for (int i = 0; i < (int)queries.rows; i++) {
+    				resultSet.clear();
+    				findNeighbors(resultSet, queries[i], params);
+    				count += resultSet.size();
+    			}
+    		}
+    	}
+    	else {
+    		if (indices.size() < queries.rows ) indices.resize(queries.rows);
+    		if (dists.size() < queries.rows ) dists.resize(queries.rows);
+
+    		if (params.max_neighbors<0) {
+    			// search for all neighbors
+#pragma omp parallel num_threads(params.cores)
+    			{
+    				RadiusResultSet<DistanceType> resultSet(radius);
+#pragma omp for schedule(static) reduction(+:count)
+    				for (int i = 0; i < (int)queries.rows; i++) {
+    					resultSet.clear();
+    					findNeighbors(resultSet, queries[i], params);
+    					size_t n = resultSet.size();
+    					count += n;
+    					indices[i].resize(n);
+    					dists[i].resize(n);
+    					if (n > 0) {
+    						resultSet.copy(&indices[i][0], &dists[i][0], n, params.sorted);
+        					indices_to_ids(&indices[i][0], &indices[i][0], n);
+    					}
+    				}
+    			}
+    		}
+    		else {
+    			// number of neighbors limited to max_neighbors
+#pragma omp parallel num_threads(params.cores)
+    			{
+    				KNNRadiusResultSet<DistanceType> resultSet(radius, params.max_neighbors);
+#pragma omp for schedule(static) reduction(+:count)
+    				for (int i = 0; i < (int)queries.rows; i++) {
+    					resultSet.clear();
+    					findNeighbors(resultSet, queries[i], params);
+    					size_t n = resultSet.size();
+    					count += n;
+    					if ((int)n>params.max_neighbors) n = params.max_neighbors;
+    					indices[i].resize(n);
+    					dists[i].resize(n);
+    					if (n > 0) {
+    						resultSet.copy(&indices[i][0], &dists[i][0], n, params.sorted);
+        					indices_to_ids(&indices[i][0], &indices[i][0], n);
+    					}
+    				}
+    			}
+    		}
+    	}
+    	return count;
+    }
+
+    /**
+     *
+     * @param queries
+     * @param indices
+     * @param dists
+     * @param radius
+     * @param params
+     * @return
+     */
+    int radiusSearch(const Matrix<ElementType>& queries,
+                                    std::vector< std::vector<int> >& indices,
+                                    std::vector<std::vector<DistanceType> >& dists,
+                                    float radius,
+                              const SearchParams& params) const
+    {
+    	std::vector<std::vector<size_t> > indices_;
+    	int result = radiusSearch(queries, indices_, dists, radius, params);
+
+    	indices.resize(indices_.size());
+    	for (size_t i=0;i<indices_.size();++i) {
+            indices[i].assign(indices_[i].begin(), indices_[i].end());
+    	}
+    	return result;
+    }
+
+
+    virtual void findNeighbors(ResultSet<DistanceType>& result, const ElementType* vec, const SearchParams& searchParams) const = 0;
+
+protected:
+
+    virtual void freeIndex() = 0;
+
+    virtual void buildIndexImpl() = 0;
+
+    size_t id_to_index(size_t id)
+    {
+    	if (ids_.size()==0) {
+    		return id;
+    	}
+    	size_t point_index = size_t(-1);
+    	if (ids_[id]==id) {
+    		return id;
+    	}
+    	else {
+    		// binary search
+    		size_t start = 0;
+    		size_t end = ids_.size();
+
+    		while (start<end) {
+    			size_t mid = (start+end)/2;
+    			if (ids_[mid]==id) {
+    				point_index = mid;
+    				break;
+    			}
+    			else if (ids_[mid]<id) {
+    				start = mid + 1;
+    			}
+    			else {
+    				end = mid;
+    			}
+    		}
+    	}
+    	return point_index;
+    }
+
+
+    void indices_to_ids(const size_t* in, size_t* out, size_t size) const
+    {
+		if (removed_) {
+			for (size_t i=0;i<size;++i) {
+				out[i] = ids_[in[i]];
+			}
+		}
+    }
+
+    void setDataset(const Matrix<ElementType>& dataset)
+    {
+    	size_ = dataset.rows;
+    	veclen_ = dataset.cols;
+    	last_id_ = 0;
+
+    	ids_.clear();
+    	removed_points_.clear();
+    	removed_ = false;
+    	removed_count_ = 0;
+
+    	points_.resize(size_);
+    	for (size_t i=0;i<size_;++i) {
+    		points_[i] = dataset[i];
+    	}
+    }
+
+    void extendDataset(const Matrix<ElementType>& new_points)
+    {
+    	size_t new_size = size_ + new_points.rows;
+    	if (removed_) {
+    		removed_points_.resize(new_size);
+    		ids_.resize(new_size);
+    	}
+    	points_.resize(new_size);
+    	for (size_t i=size_;i<new_size;++i) {
+    		points_[i] = new_points[i-size_];
+    		if (removed_) {
+    			ids_[i] = last_id_++;
+    			removed_points_.reset(i);
+    		}
+    	}
+    	size_ = new_size;
+    }
+
+
+    void cleanRemovedPoints()
+    {
+    	if (!removed_) return;
+
+    	size_t last_idx = 0;
+    	for (size_t i=0;i<size_;++i) {
+    		if (!removed_points_.test(i)) {
+    			points_[last_idx] = points_[i];
+    			ids_[last_idx] = ids_[i];
+    			removed_points_.reset(last_idx);
+    			++last_idx;
+    		}
+    	}
+    	points_.resize(last_idx);
+    	ids_.resize(last_idx);
+    	removed_points_.resize(last_idx);
+    	size_ = last_idx;
+    	removed_count_ = 0;
+    }
+
+    void swap(NNIndex& other)
+    {
+    	std::swap(distance_, other.distance_);
+    	std::swap(last_id_, other.last_id_);
+    	std::swap(size_, other.size_);
+    	std::swap(size_at_build_, other.size_at_build_);
+    	std::swap(veclen_, other.veclen_);
+    	std::swap(index_params_, other.index_params_);
+    	std::swap(removed_, other.removed_);
+    	std::swap(removed_points_, other.removed_points_);
+    	std::swap(removed_count_, other.removed_count_);
+    	std::swap(ids_, other.ids_);
+    	std::swap(points_, other.points_);
+    	std::swap(data_ptr_, other.data_ptr_);
+    }
+
+protected:
+
+    /**
+     * The distance functor
+     */
+    Distance distance_;
+
+
+    /**
+     * Each index point has an associated ID. IDs are assigned sequentially in
+     * increasing order. This indicates the ID assigned to the last point added to the
+     * index.
+     */
+    size_t last_id_;
+
+    /**
+     * Number of points in the index (and database)
+     */
+    size_t size_;
+
+    /**
+     * Number of features in the dataset when the index was last built.
+     */
+    size_t size_at_build_;
+
+    /**
+     * Size of one point in the index (and database)
+     */
+    size_t veclen_;
+
+    /**
+     * Parameters of the index.
+     */
+    IndexParams index_params_;
+
+    /**
+     * Flag indicating if at least a point was removed from the index
+     */
+    bool removed_;
+
+    /**
+     * Array used to mark points removed from the index
+     */
+    DynamicBitset removed_points_;
+
+    /**
+     * Number of points removed from the index
+     */
+    size_t removed_count_;
+
+    /**
+     * Array of point IDs, returned by nearest-neighbour operations
+     */
+    std::vector<size_t> ids_;
+
+    /**
+     * Point data
+     */
+    std::vector<ElementType*> points_;
+
+    /**
+     * Pointer to dataset memory if allocated by this index, otherwise NULL
+     */
+    ElementType* data_ptr_;
+
+
+};
+
+
+#define USING_BASECLASS_SYMBOLS \
+		using NNIndex<Distance>::distance_;\
+		using NNIndex<Distance>::size_;\
+		using NNIndex<Distance>::size_at_build_;\
+		using NNIndex<Distance>::veclen_;\
+		using NNIndex<Distance>::index_params_;\
+		using NNIndex<Distance>::removed_points_;\
+		using NNIndex<Distance>::ids_;\
+		using NNIndex<Distance>::removed_;\
+		using NNIndex<Distance>::points_;\
+		using NNIndex<Distance>::extendDataset;\
+		using NNIndex<Distance>::setDataset;\
+		using NNIndex<Distance>::cleanRemovedPoints;\
+		using NNIndex<Distance>::indices_to_ids;
+
+
+
+}
+
+
+#endif //FLANN_NNINDEX_H
diff --git a/src/cpp/flann/config.h b/src/cpp/flann/config.h
new file mode 100644
index 0000000..450800a
--- /dev/null
+++ b/src/cpp/flann/config.h
@@ -0,0 +1,38 @@
+/***********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2008-2011  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+ * Copyright 2008-2011  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+
+
+#ifndef FLANN_CONFIG_H_
+#define FLANN_CONFIG_H_
+
+#ifdef FLANN_VERSION_
+#undef FLANN_VERSION_
+#endif
+#define FLANN_VERSION_ "1.8.4"
+
+#endif /* FLANN_CONFIG_H_ */
diff --git a/src/cpp/flann/config.h.in b/src/cpp/flann/config.h.in
new file mode 100644
index 0000000..483d73e
--- /dev/null
+++ b/src/cpp/flann/config.h.in
@@ -0,0 +1,38 @@
+/***********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2008-2011  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+ * Copyright 2008-2011  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+
+
+#ifndef FLANN_CONFIG_H_
+#define FLANN_CONFIG_H_
+
+#ifdef FLANN_VERSION_
+#undef FLANN_VERSION_
+#endif
+#define FLANN_VERSION_ "${FLANN_VERSION}"
+
+#endif /* FLANN_CONFIG_H_ */
diff --git a/src/cpp/flann/defines.h b/src/cpp/flann/defines.h
new file mode 100644
index 0000000..c208279
--- /dev/null
+++ b/src/cpp/flann/defines.h
@@ -0,0 +1,155 @@
+/***********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2008-2011  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+ * Copyright 2008-2011  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+
+#ifndef FLANN_DEFINES_H_
+#define FLANN_DEFINES_H_
+
+#include "config.h"
+
+#ifdef FLANN_EXPORT
+#undef FLANN_EXPORT
+#endif
+#ifdef WIN32
+/* win32 dll export/import directives */
+ #ifdef FLANN_EXPORTS
+  #define FLANN_EXPORT __declspec(dllexport)
+ #elif defined(FLANN_STATIC)
+  #define FLANN_EXPORT
+ #else
+  #define FLANN_EXPORT __declspec(dllimport)
+ #endif
+#else
+/* unix needs nothing */
+ #define FLANN_EXPORT
+#endif
+
+#ifdef FLANN_DEPRECATED
+#undef FLANN_DEPRECATED
+#endif
+#ifdef __GNUC__
+#define FLANN_DEPRECATED __attribute__ ((deprecated))
+#elif defined(_MSC_VER)
+#define FLANN_DEPRECATED __declspec(deprecated)
+#else
+#pragma message("WARNING: You need to implement FLANN_DEPRECATED for this compiler")
+#define FLANN_DEPRECATED
+#endif
+
+#undef FLANN_PLATFORM_64_BIT
+#undef FLANN_PLATFORM_32_BIT
+#if __amd64__ || __x86_64__ || _WIN64 || _M_X64
+#define FLANN_PLATFORM_64_BIT
+#else
+#define FLANN_PLATFORM_32_BIT
+#endif
+
+#undef FLANN_ARRAY_LEN
+#define FLANN_ARRAY_LEN(a) (sizeof(a)/sizeof(a[0]))
+
+#ifdef __cplusplus
+namespace flann {
+#endif
+
+/* Nearest neighbour index algorithms */
+enum flann_algorithm_t
+{
+    FLANN_INDEX_LINEAR 			= 0,
+    FLANN_INDEX_KDTREE 			= 1,
+    FLANN_INDEX_KMEANS 			= 2,
+    FLANN_INDEX_COMPOSITE 		= 3,
+    FLANN_INDEX_KDTREE_SINGLE 	= 4,
+    FLANN_INDEX_HIERARCHICAL 	= 5,
+    FLANN_INDEX_LSH 			= 6,
+#ifdef FLANN_USE_CUDA
+    FLANN_INDEX_KDTREE_CUDA 	= 7,
+#endif
+    FLANN_INDEX_SAVED 			= 254,
+    FLANN_INDEX_AUTOTUNED 		= 255,
+};
+
+enum flann_centers_init_t
+{
+    FLANN_CENTERS_RANDOM = 0,
+    FLANN_CENTERS_GONZALES = 1,
+    FLANN_CENTERS_KMEANSPP = 2,
+};
+
+enum flann_log_level_t
+{
+    FLANN_LOG_NONE = 0,
+    FLANN_LOG_FATAL = 1,
+    FLANN_LOG_ERROR = 2,
+    FLANN_LOG_WARN = 3,
+    FLANN_LOG_INFO = 4,
+    FLANN_LOG_DEBUG = 5
+};
+
+enum flann_distance_t
+{
+    FLANN_DIST_EUCLIDEAN 			= 1,
+    FLANN_DIST_L2 					= 1,
+    FLANN_DIST_MANHATTAN 			= 2,
+    FLANN_DIST_L1 					= 2,
+    FLANN_DIST_MINKOWSKI 			= 3,
+    FLANN_DIST_MAX   				= 4,
+    FLANN_DIST_HIST_INTERSECT  		= 5,
+    FLANN_DIST_HELLINGER 			= 6,
+    FLANN_DIST_CHI_SQUARE		 	= 7,
+    FLANN_DIST_KULLBACK_LEIBLER  	= 8,
+    FLANN_DIST_HAMMING         		= 9,
+    FLANN_DIST_HAMMING_LUT			= 10,
+    FLANN_DIST_HAMMING_POPCNT   	= 11,
+    FLANN_DIST_L2_SIMPLE	   		= 12,
+};
+
+enum flann_datatype_t
+{
+    FLANN_NONE 		= -1,
+    FLANN_INT8 		= 0,
+    FLANN_INT16 	= 1,
+    FLANN_INT32 	= 2,
+    FLANN_INT64 	= 3,
+    FLANN_UINT8 	= 4,
+    FLANN_UINT16 	= 5,
+    FLANN_UINT32 	= 6,
+    FLANN_UINT64 	= 7,
+    FLANN_FLOAT32 	= 8,
+    FLANN_FLOAT64 	= 9
+};
+
+enum flann_checks_t {
+    FLANN_CHECKS_UNLIMITED = -1,
+    FLANN_CHECKS_AUTOTUNED = -2,
+};
+
+#ifdef __cplusplus
+}
+#endif
+
+
+#endif /* FLANN_DEFINES_H_ */
diff --git a/src/cpp/flann/flann.cpp b/src/cpp/flann/flann.cpp
new file mode 100644
index 0000000..46e1d25
--- /dev/null
+++ b/src/cpp/flann/flann.cpp
@@ -0,0 +1,885 @@
+/***********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2008-2009  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+ * Copyright 2008-2009  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+
+#define FLANN_FIRST_MATCH
+
+#include "flann.h"
+
+
+struct FLANNParameters DEFAULT_FLANN_PARAMETERS = {
+    FLANN_INDEX_KDTREE,
+    32, 0.0f,
+    0, -1, 0,
+    4, 4,
+    32, 11, FLANN_CENTERS_RANDOM, 0.2f,
+    0.9f, 0.01f, 0, 0.1f,
+    FLANN_LOG_NONE, 0
+};
+
+
+using namespace flann;
+
+
+flann::IndexParams create_parameters(FLANNParameters* p)
+{
+    flann::IndexParams params;
+
+    params["algorithm"] = p->algorithm;
+
+    params["checks"] = p->checks;
+    params["cb_index"] = p->cb_index;
+    params["eps"] = p->eps;
+
+    if (p->algorithm == FLANN_INDEX_KDTREE) {
+        params["trees"] = p->trees;
+    }
+
+    if (p->algorithm == FLANN_INDEX_KDTREE_SINGLE) {
+        params["trees"] = p->trees;
+        params["leaf_max_size"] = p->leaf_max_size;
+    }
+
+#ifdef FLANN_USE_CUDA
+    if (p->algorithm == FLANN_INDEX_KDTREE_CUDA) {
+        params["leaf_max_size"] = p->leaf_max_size;
+    }
+#endif
+
+    if (p->algorithm == FLANN_INDEX_KMEANS) {
+        params["branching"] = p->branching;
+        params["iterations"] = p->iterations;
+        params["centers_init"] = p->centers_init;
+    }
+
+    if (p->algorithm == FLANN_INDEX_AUTOTUNED) {
+        params["target_precision"] = p->target_precision;
+        params["build_weight"] = p->build_weight;
+        params["memory_weight"] = p->memory_weight;
+        params["sample_fraction"] = p->sample_fraction;
+    }
+
+    if (p->algorithm == FLANN_INDEX_HIERARCHICAL) {
+        params["branching"] = p->branching;
+        params["centers_init"] = p->centers_init;
+        params["trees"] = p->trees;
+        params["leaf_max_size"] = p->leaf_max_size;
+    }
+
+    if (p->algorithm == FLANN_INDEX_LSH) {
+        params["table_number"] = p->table_number_;
+        params["key_size"] = p->key_size_;
+        params["multi_probe_level"] = p->multi_probe_level_;
+    }
+
+    params["log_level"] = p->log_level;
+    params["random_seed"] = p->random_seed;
+
+    return params;
+}
+
+flann::SearchParams create_search_params(FLANNParameters* p)
+{
+    flann::SearchParams params;
+    params.checks = p->checks;
+    params.eps = p->eps;
+    params.sorted = p->sorted;
+    params.max_neighbors = p->max_neighbors;
+    params.cores = p->cores;
+
+    return params;
+}
+
+
+void update_flann_parameters(const IndexParams& params, FLANNParameters* flann_params)
+{
+	if (has_param(params,"algorithm")) {
+		flann_params->algorithm = get_param<flann_algorithm_t>(params,"algorithm");
+	}
+	if (has_param(params,"trees")) {
+		flann_params->trees = get_param<int>(params,"trees");
+	}
+	if (has_param(params,"leaf_max_size")) {
+		flann_params->leaf_max_size = get_param<int>(params,"leaf_max_size");
+	}
+	if (has_param(params,"branching")) {
+		flann_params->branching = get_param<int>(params,"branching");
+	}
+	if (has_param(params,"iterations")) {
+		flann_params->iterations = get_param<int>(params,"iterations");
+	}
+	if (has_param(params,"centers_init")) {
+		flann_params->centers_init = get_param<flann_centers_init_t>(params,"centers_init");
+	}
+	if (has_param(params,"target_precision")) {
+		flann_params->target_precision = get_param<float>(params,"target_precision");
+	}
+	if (has_param(params,"build_weight")) {
+		flann_params->build_weight = get_param<float>(params,"build_weight");
+	}
+	if (has_param(params,"memory_weight")) {
+		flann_params->memory_weight = get_param<float>(params,"memory_weight");
+	}
+	if (has_param(params,"sample_fraction")) {
+		flann_params->sample_fraction = get_param<float>(params,"sample_fraction");
+	}
+	if (has_param(params,"table_number")) {
+		flann_params->table_number_ = get_param<unsigned int>(params,"table_number");
+	}
+	if (has_param(params,"key_size")) {
+		flann_params->key_size_ = get_param<unsigned int>(params,"key_size");
+	}
+	if (has_param(params,"multi_probe_level")) {
+		flann_params->multi_probe_level_ = get_param<unsigned int>(params,"multi_probe_level");
+	}
+	if (has_param(params,"log_level")) {
+		flann_params->log_level = get_param<flann_log_level_t>(params,"log_level");
+	}
+	if (has_param(params,"random_seed")) {
+		flann_params->random_seed = get_param<long>(params,"random_seed");
+	}
+}
+
+
+void init_flann_parameters(FLANNParameters* p)
+{
+    if (p != NULL) {
+        flann_log_verbosity(p->log_level);
+        if (p->random_seed>0) {
+            seed_random(p->random_seed);
+        }
+    }
+}
+
+
+void flann_log_verbosity(int level)
+{
+    flann::log_verbosity(level);
+}
+
+flann_distance_t flann_distance_type = FLANN_DIST_EUCLIDEAN;
+int flann_distance_order = 3;
+
+void flann_set_distance_type(flann_distance_t distance_type, int order)
+{
+    flann_distance_type = distance_type;
+    flann_distance_order = order;
+}
+
+
+template<typename Distance>
+flann_index_t __flann_build_index(typename Distance::ElementType* dataset, int rows, int cols, float* speedup,
+                                  FLANNParameters* flann_params, Distance d = Distance())
+{
+    typedef typename Distance::ElementType ElementType;
+    try {
+
+        init_flann_parameters(flann_params);
+        if (flann_params == NULL) {
+            throw FLANNException("The flann_params argument must be non-null");
+        }
+        IndexParams params = create_parameters(flann_params);
+        Index<Distance>* index = new Index<Distance>(Matrix<ElementType>(dataset,rows,cols), params, d);
+        index->buildIndex();
+
+        if (flann_params->algorithm==FLANN_INDEX_AUTOTUNED) {
+            IndexParams params = index->getParameters();
+            update_flann_parameters(params,flann_params);
+            SearchParams search_params = get_param<SearchParams>(params,"search_params");
+            *speedup = get_param<float>(params,"speedup");
+            flann_params->checks = search_params.checks;
+            flann_params->eps = search_params.eps;
+            flann_params->cb_index = get_param<float>(params,"cb_index",0.0);
+        }
+
+        return index;
+    }
+    catch (std::runtime_error& e) {
+        Logger::error("Caught exception: %s\n",e.what());
+        return NULL;
+    }
+}
+
+template<typename T>
+flann_index_t _flann_build_index(T* dataset, int rows, int cols, float* speedup, FLANNParameters* flann_params)
+{
+    if (flann_distance_type==FLANN_DIST_EUCLIDEAN) {
+        return __flann_build_index<L2<T> >(dataset, rows, cols, speedup, flann_params);
+    }
+    else if (flann_distance_type==FLANN_DIST_MANHATTAN) {
+        return __flann_build_index<L1<T> >(dataset, rows, cols, speedup, flann_params);
+    }
+    else if (flann_distance_type==FLANN_DIST_MINKOWSKI) {
+        return __flann_build_index<MinkowskiDistance<T> >(dataset, rows, cols, speedup, flann_params, MinkowskiDistance<T>(flann_distance_order));
+    }
+    else if (flann_distance_type==FLANN_DIST_HIST_INTERSECT) {
+        return __flann_build_index<HistIntersectionDistance<T> >(dataset, rows, cols, speedup, flann_params);
+    }
+    else if (flann_distance_type==FLANN_DIST_HELLINGER) {
+        return __flann_build_index<HellingerDistance<T> >(dataset, rows, cols, speedup, flann_params);
+    }
+    else if (flann_distance_type==FLANN_DIST_CHI_SQUARE) {
+        return __flann_build_index<ChiSquareDistance<T> >(dataset, rows, cols, speedup, flann_params);
+    }
+    else if (flann_distance_type==FLANN_DIST_KULLBACK_LEIBLER) {
+        return __flann_build_index<KL_Divergence<T> >(dataset, rows, cols, speedup, flann_params);
+    }
+    else {
+        Logger::error( "Distance type unsupported in the C bindings, use the C++ bindings instead\n");
+        return NULL;
+    }
+}
+
+flann_index_t flann_build_index(float* dataset, int rows, int cols, float* speedup, FLANNParameters* flann_params)
+{
+    return _flann_build_index<float>(dataset, rows, cols, speedup, flann_params);
+}
+
+flann_index_t flann_build_index_float(float* dataset, int rows, int cols, float* speedup, FLANNParameters* flann_params)
+{
+    return _flann_build_index<float>(dataset, rows, cols, speedup, flann_params);
+}
+
+flann_index_t flann_build_index_double(double* dataset, int rows, int cols, float* speedup, FLANNParameters* flann_params)
+{
+    return _flann_build_index<double>(dataset, rows, cols, speedup, flann_params);
+}
+
+flann_index_t flann_build_index_byte(unsigned char* dataset, int rows, int cols, float* speedup, FLANNParameters* flann_params)
+{
+    return _flann_build_index<unsigned char>(dataset, rows, cols, speedup, flann_params);
+}
+
+flann_index_t flann_build_index_int(int* dataset, int rows, int cols, float* speedup, FLANNParameters* flann_params)
+{
+    return _flann_build_index<int>(dataset, rows, cols, speedup, flann_params);
+}
+
+template<typename Distance>
+int __flann_save_index(flann_index_t index_ptr, char* filename)
+{
+    try {
+        if (index_ptr==NULL) {
+            throw FLANNException("Invalid index");
+        }
+
+        Index<Distance>* index = (Index<Distance>*)index_ptr;
+        index->save(filename);
+
+        return 0;
+    }
+    catch (std::runtime_error& e) {
+        Logger::error("Caught exception: %s\n",e.what());
+        return -1;
+    }
+}
+
+template<typename T>
+int _flann_save_index(flann_index_t index_ptr, char* filename)
+{
+    if (flann_distance_type==FLANN_DIST_EUCLIDEAN) {
+        return __flann_save_index<L2<T> >(index_ptr, filename);
+    }
+    else if (flann_distance_type==FLANN_DIST_MANHATTAN) {
+        return __flann_save_index<L1<T> >(index_ptr, filename);
+    }
+    else if (flann_distance_type==FLANN_DIST_MINKOWSKI) {
+        return __flann_save_index<MinkowskiDistance<T> >(index_ptr, filename);
+    }
+    else if (flann_distance_type==FLANN_DIST_HIST_INTERSECT) {
+        return __flann_save_index<HistIntersectionDistance<T> >(index_ptr, filename);
+    }
+    else if (flann_distance_type==FLANN_DIST_HELLINGER) {
+        return __flann_save_index<HellingerDistance<T> >(index_ptr, filename);
+    }
+    else if (flann_distance_type==FLANN_DIST_CHI_SQUARE) {
+        return __flann_save_index<ChiSquareDistance<T> >(index_ptr, filename);
+    }
+    else if (flann_distance_type==FLANN_DIST_KULLBACK_LEIBLER) {
+        return __flann_save_index<KL_Divergence<T> >(index_ptr, filename);
+    }
+    else {
+        Logger::error( "Distance type unsupported in the C bindings, use the C++ bindings instead\n");
+        return -1;
+    }
+}
+
+int flann_save_index(flann_index_t index_ptr, char* filename)
+{
+    return _flann_save_index<float>(index_ptr, filename);
+}
+
+int flann_save_index_float(flann_index_t index_ptr, char* filename)
+{
+    return _flann_save_index<float>(index_ptr, filename);
+}
+
+int flann_save_index_double(flann_index_t index_ptr, char* filename)
+{
+    return _flann_save_index<double>(index_ptr, filename);
+}
+
+int flann_save_index_byte(flann_index_t index_ptr, char* filename)
+{
+    return _flann_save_index<unsigned char>(index_ptr, filename);
+}
+
+int flann_save_index_int(flann_index_t index_ptr, char* filename)
+{
+    return _flann_save_index<int>(index_ptr, filename);
+}
+
+
+template<typename Distance>
+flann_index_t __flann_load_index(char* filename, typename Distance::ElementType* dataset, int rows, int cols,
+                                 Distance d = Distance())
+{
+    try {
+        Index<Distance>* index = new Index<Distance>(Matrix<typename Distance::ElementType>(dataset,rows,cols), SavedIndexParams(filename), d);
+        return index;
+    }
+    catch (std::runtime_error& e) {
+        Logger::error("Caught exception: %s\n",e.what());
+        return NULL;
+    }
+}
+
+template<typename T>
+flann_index_t _flann_load_index(char* filename, T* dataset, int rows, int cols)
+{
+    if (flann_distance_type==FLANN_DIST_EUCLIDEAN) {
+        return __flann_load_index<L2<T> >(filename, dataset, rows, cols);
+    }
+    else if (flann_distance_type==FLANN_DIST_MANHATTAN) {
+        return __flann_load_index<L1<T> >(filename, dataset, rows, cols);
+    }
+    else if (flann_distance_type==FLANN_DIST_MINKOWSKI) {
+        return __flann_load_index<MinkowskiDistance<T> >(filename, dataset, rows, cols, MinkowskiDistance<T>(flann_distance_order));
+    }
+    else if (flann_distance_type==FLANN_DIST_HIST_INTERSECT) {
+        return __flann_load_index<HistIntersectionDistance<T> >(filename, dataset, rows, cols);
+    }
+    else if (flann_distance_type==FLANN_DIST_HELLINGER) {
+        return __flann_load_index<HellingerDistance<T> >(filename, dataset, rows, cols);
+    }
+    else if (flann_distance_type==FLANN_DIST_CHI_SQUARE) {
+        return __flann_load_index<ChiSquareDistance<T> >(filename, dataset, rows, cols);
+    }
+    else if (flann_distance_type==FLANN_DIST_KULLBACK_LEIBLER) {
+        return __flann_load_index<KL_Divergence<T> >(filename, dataset, rows, cols);
+    }
+    else {
+        Logger::error( "Distance type unsupported in the C bindings, use the C++ bindings instead\n");
+        return NULL;
+    }
+}
+
+
+flann_index_t flann_load_index(char* filename, float* dataset, int rows, int cols)
+{
+    return _flann_load_index<float>(filename, dataset, rows, cols);
+}
+
+flann_index_t flann_load_index_float(char* filename, float* dataset, int rows, int cols)
+{
+    return _flann_load_index<float>(filename, dataset, rows, cols);
+}
+
+flann_index_t flann_load_index_double(char* filename, double* dataset, int rows, int cols)
+{
+    return _flann_load_index<double>(filename, dataset, rows, cols);
+}
+
+flann_index_t flann_load_index_byte(char* filename, unsigned char* dataset, int rows, int cols)
+{
+    return _flann_load_index<unsigned char>(filename, dataset, rows, cols);
+}
+
+flann_index_t flann_load_index_int(char* filename, int* dataset, int rows, int cols)
+{
+    return _flann_load_index<int>(filename, dataset, rows, cols);
+}
+
+
+
+template<typename Distance>
+int __flann_find_nearest_neighbors(typename Distance::ElementType* dataset,  int rows, int cols, typename Distance::ElementType* testset, int tcount,
+                                   int* result, typename Distance::ResultType* dists, int nn, FLANNParameters* flann_params, Distance d = Distance())
+{
+    typedef typename Distance::ElementType ElementType;
+    typedef typename Distance::ResultType DistanceType;
+    try {
+        init_flann_parameters(flann_params);
+
+        IndexParams params = create_parameters(flann_params);
+        Index<Distance>* index = new Index<Distance>(Matrix<ElementType>(dataset,rows,cols), params, d);
+        index->buildIndex();
+        Matrix<int> m_indices(result,tcount, nn);
+        Matrix<DistanceType> m_dists(dists,tcount, nn);
+        SearchParams search_params = create_search_params(flann_params);
+        index->knnSearch(Matrix<ElementType>(testset, tcount, index->veclen()),
+                         m_indices,
+                         m_dists, nn, search_params );
+        delete index;
+        return 0;
+    }
+    catch (std::runtime_error& e) {
+        Logger::error("Caught exception: %s\n",e.what());
+        return -1;
+    }
+
+    return -1;
+}
+
+template<typename T, typename R>
+int _flann_find_nearest_neighbors(T* dataset,  int rows, int cols, T* testset, int tcount,
+                                  int* result, R* dists, int nn, FLANNParameters* flann_params)
+{
+    if (flann_distance_type==FLANN_DIST_EUCLIDEAN) {
+        return __flann_find_nearest_neighbors<L2<T> >(dataset, rows, cols, testset, tcount, result, dists, nn, flann_params);
+    }
+    else if (flann_distance_type==FLANN_DIST_MANHATTAN) {
+        return __flann_find_nearest_neighbors<L1<T> >(dataset, rows, cols, testset, tcount, result, dists, nn, flann_params);
+    }
+    else if (flann_distance_type==FLANN_DIST_MINKOWSKI) {
+        return __flann_find_nearest_neighbors<MinkowskiDistance<T> >(dataset, rows, cols, testset, tcount, result, dists, nn, flann_params, MinkowskiDistance<T>(flann_distance_order));
+    }
+    else if (flann_distance_type==FLANN_DIST_HIST_INTERSECT) {
+        return __flann_find_nearest_neighbors<HistIntersectionDistance<T> >(dataset, rows, cols, testset, tcount, result, dists, nn, flann_params);
+    }
+    else if (flann_distance_type==FLANN_DIST_HELLINGER) {
+        return __flann_find_nearest_neighbors<HellingerDistance<T> >(dataset, rows, cols, testset, tcount, result, dists, nn, flann_params);
+    }
+    else if (flann_distance_type==FLANN_DIST_CHI_SQUARE) {
+        return __flann_find_nearest_neighbors<ChiSquareDistance<T> >(dataset, rows, cols, testset, tcount, result, dists, nn, flann_params);
+    }
+    else if (flann_distance_type==FLANN_DIST_KULLBACK_LEIBLER) {
+        return __flann_find_nearest_neighbors<KL_Divergence<T> >(dataset, rows, cols, testset, tcount, result, dists, nn, flann_params);
+    }
+    else {
+        Logger::error( "Distance type unsupported in the C bindings, use the C++ bindings instead\n");
+        return -1;
+    }
+}
+
+int flann_find_nearest_neighbors(float* dataset,  int rows, int cols, float* testset, int tcount, int* result, float* dists, int nn, FLANNParameters* flann_params)
+{
+    return _flann_find_nearest_neighbors(dataset, rows, cols, testset, tcount, result, dists, nn, flann_params);
+}
+
+int flann_find_nearest_neighbors_float(float* dataset,  int rows, int cols, float* testset, int tcount, int* result, float* dists, int nn, FLANNParameters* flann_params)
+{
+    return _flann_find_nearest_neighbors(dataset, rows, cols, testset, tcount, result, dists, nn, flann_params);
+}
+
+int flann_find_nearest_neighbors_double(double* dataset,  int rows, int cols, double* testset, int tcount, int* result, double* dists, int nn, FLANNParameters* flann_params)
+{
+    return _flann_find_nearest_neighbors(dataset, rows, cols, testset, tcount, result, dists, nn, flann_params);
+}
+
+int flann_find_nearest_neighbors_byte(unsigned char* dataset,  int rows, int cols, unsigned char* testset, int tcount, int* result, float* dists, int nn, FLANNParameters* flann_params)
+{
+    return _flann_find_nearest_neighbors(dataset, rows, cols, testset, tcount, result, dists, nn, flann_params);
+}
+
+int flann_find_nearest_neighbors_int(int* dataset,  int rows, int cols, int* testset, int tcount, int* result, float* dists, int nn, FLANNParameters* flann_params)
+{
+    return _flann_find_nearest_neighbors(dataset, rows, cols, testset, tcount, result, dists, nn, flann_params);
+}
+
+
+template<typename Distance>
+int __flann_find_nearest_neighbors_index(flann_index_t index_ptr, typename Distance::ElementType* testset, int tcount,
+                                         int* result, typename Distance::ResultType* dists, int nn, FLANNParameters* flann_params)
+{
+    typedef typename Distance::ElementType ElementType;
+    typedef typename Distance::ResultType DistanceType;
+
+    try {
+        init_flann_parameters(flann_params);
+        if (index_ptr==NULL) {
+            throw FLANNException("Invalid index");
+        }
+        Index<Distance>* index = (Index<Distance>*)index_ptr;
+
+        Matrix<int> m_indices(result,tcount, nn);
+        Matrix<DistanceType> m_dists(dists, tcount, nn);
+
+        SearchParams search_params = create_search_params(flann_params);
+        index->knnSearch(Matrix<ElementType>(testset, tcount, index->veclen()),
+                         m_indices,
+                         m_dists, nn, search_params );
+
+        return 0;
+    }
+    catch (std::runtime_error& e) {
+        Logger::error("Caught exception: %s\n",e.what());
+        return -1;
+    }
+
+    return -1;
+}
+
+template<typename T, typename R>
+int _flann_find_nearest_neighbors_index(flann_index_t index_ptr, T* testset, int tcount,
+                                        int* result, R* dists, int nn, FLANNParameters* flann_params)
+{
+    if (flann_distance_type==FLANN_DIST_EUCLIDEAN) {
+        return __flann_find_nearest_neighbors_index<L2<T> >(index_ptr, testset, tcount, result, dists, nn, flann_params);
+    }
+    else if (flann_distance_type==FLANN_DIST_MANHATTAN) {
+        return __flann_find_nearest_neighbors_index<L1<T> >(index_ptr, testset, tcount, result, dists, nn, flann_params);
+    }
+    else if (flann_distance_type==FLANN_DIST_MINKOWSKI) {
+        return __flann_find_nearest_neighbors_index<MinkowskiDistance<T> >(index_ptr, testset, tcount, result, dists, nn, flann_params);
+    }
+    else if (flann_distance_type==FLANN_DIST_HIST_INTERSECT) {
+        return __flann_find_nearest_neighbors_index<HistIntersectionDistance<T> >(index_ptr, testset, tcount, result, dists, nn, flann_params);
+    }
+    else if (flann_distance_type==FLANN_DIST_HELLINGER) {
+        return __flann_find_nearest_neighbors_index<HellingerDistance<T> >(index_ptr, testset, tcount, result, dists, nn, flann_params);
+    }
+    else if (flann_distance_type==FLANN_DIST_CHI_SQUARE) {
+        return __flann_find_nearest_neighbors_index<ChiSquareDistance<T> >(index_ptr, testset, tcount, result, dists, nn, flann_params);
+    }
+    else if (flann_distance_type==FLANN_DIST_KULLBACK_LEIBLER) {
+        return __flann_find_nearest_neighbors_index<KL_Divergence<T> >(index_ptr, testset, tcount, result, dists, nn, flann_params);
+    }
+    else {
+        Logger::error( "Distance type unsupported in the C bindings, use the C++ bindings instead\n");
+        return -1;
+    }
+}
+
+
+int flann_find_nearest_neighbors_index(flann_index_t index_ptr, float* testset, int tcount, int* result, float* dists, int nn, FLANNParameters* flann_params)
+{
+    return _flann_find_nearest_neighbors_index(index_ptr, testset, tcount, result, dists, nn, flann_params);
+}
+
+int flann_find_nearest_neighbors_index_float(flann_index_t index_ptr, float* testset, int tcount, int* result, float* dists, int nn, FLANNParameters* flann_params)
+{
+    return _flann_find_nearest_neighbors_index(index_ptr, testset, tcount, result, dists, nn, flann_params);
+}
+
+int flann_find_nearest_neighbors_index_double(flann_index_t index_ptr, double* testset, int tcount, int* result, double* dists, int nn, FLANNParameters* flann_params)
+{
+    return _flann_find_nearest_neighbors_index(index_ptr, testset, tcount, result, dists, nn, flann_params);
+}
+
+int flann_find_nearest_neighbors_index_byte(flann_index_t index_ptr, unsigned char* testset, int tcount, int* result, float* dists, int nn, FLANNParameters* flann_params)
+{
+    return _flann_find_nearest_neighbors_index(index_ptr, testset, tcount, result, dists, nn, flann_params);
+}
+
+int flann_find_nearest_neighbors_index_int(flann_index_t index_ptr, int* testset, int tcount, int* result, float* dists, int nn, FLANNParameters* flann_params)
+{
+    return _flann_find_nearest_neighbors_index(index_ptr, testset, tcount, result, dists, nn, flann_params);
+}
+
+
+template<typename Distance>
+int __flann_radius_search(flann_index_t index_ptr,
+                          typename Distance::ElementType* query,
+                          int* indices,
+                          typename Distance::ResultType* dists,
+                          int max_nn,
+                          float radius,
+                          FLANNParameters* flann_params)
+{
+    typedef typename Distance::ElementType ElementType;
+    typedef typename Distance::ResultType DistanceType;
+
+    try {
+        init_flann_parameters(flann_params);
+        if (index_ptr==NULL) {
+            throw FLANNException("Invalid index");
+        }
+        Index<Distance>* index = (Index<Distance>*)index_ptr;
+
+        Matrix<int> m_indices(indices, 1, max_nn);
+        Matrix<DistanceType> m_dists(dists, 1, max_nn);
+        SearchParams search_params = create_search_params(flann_params);
+        int count = index->radiusSearch(Matrix<ElementType>(query, 1, index->veclen()),
+                                        m_indices,
+                                        m_dists, radius, search_params );
+
+
+        return count;
+    }
+    catch (std::runtime_error& e) {
+        Logger::error("Caught exception: %s\n",e.what());
+        return -1;
+    }
+}
+
+template<typename T, typename R>
+int _flann_radius_search(flann_index_t index_ptr,
+                         T* query,
+                         int* indices,
+                         R* dists,
+                         int max_nn,
+                         float radius,
+                         FLANNParameters* flann_params)
+{
+    if (flann_distance_type==FLANN_DIST_EUCLIDEAN) {
+        return __flann_radius_search<L2<T> >(index_ptr, query, indices, dists, max_nn, radius, flann_params);
+    }
+    else if (flann_distance_type==FLANN_DIST_MANHATTAN) {
+        return __flann_radius_search<L1<T> >(index_ptr, query, indices, dists, max_nn, radius, flann_params);
+    }
+    else if (flann_distance_type==FLANN_DIST_MINKOWSKI) {
+        return __flann_radius_search<MinkowskiDistance<T> >(index_ptr, query, indices, dists, max_nn, radius, flann_params);
+    }
+    else if (flann_distance_type==FLANN_DIST_HIST_INTERSECT) {
+        return __flann_radius_search<HistIntersectionDistance<T> >(index_ptr, query, indices, dists, max_nn, radius, flann_params);
+    }
+    else if (flann_distance_type==FLANN_DIST_HELLINGER) {
+        return __flann_radius_search<HellingerDistance<T> >(index_ptr, query, indices, dists, max_nn, radius, flann_params);
+    }
+    else if (flann_distance_type==FLANN_DIST_CHI_SQUARE) {
+        return __flann_radius_search<ChiSquareDistance<T> >(index_ptr, query, indices, dists, max_nn, radius, flann_params);
+    }
+    else if (flann_distance_type==FLANN_DIST_KULLBACK_LEIBLER) {
+        return __flann_radius_search<KL_Divergence<T> >(index_ptr, query, indices, dists, max_nn, radius, flann_params);
+    }
+    else {
+        Logger::error( "Distance type unsupported in the C bindings, use the C++ bindings instead\n");
+        return -1;
+    }
+}
+
+int flann_radius_search(flann_index_t index_ptr,
+                        float* query,
+                        int* indices,
+                        float* dists,
+                        int max_nn,
+                        float radius,
+                        FLANNParameters* flann_params)
+{
+    return _flann_radius_search(index_ptr, query, indices, dists, max_nn, radius, flann_params);
+}
+
+int flann_radius_search_float(flann_index_t index_ptr,
+                              float* query,
+                              int* indices,
+                              float* dists,
+                              int max_nn,
+                              float radius,
+                              FLANNParameters* flann_params)
+{
+    return _flann_radius_search(index_ptr, query, indices, dists, max_nn, radius, flann_params);
+}
+
+int flann_radius_search_double(flann_index_t index_ptr,
+                               double* query,
+                               int* indices,
+                               double* dists,
+                               int max_nn,
+                               float radius,
+                               FLANNParameters* flann_params)
+{
+    return _flann_radius_search(index_ptr, query, indices, dists, max_nn, radius, flann_params);
+}
+
+int flann_radius_search_byte(flann_index_t index_ptr,
+                             unsigned char* query,
+                             int* indices,
+                             float* dists,
+                             int max_nn,
+                             float radius,
+                             FLANNParameters* flann_params)
+{
+    return _flann_radius_search(index_ptr, query, indices, dists, max_nn, radius, flann_params);
+}
+
+int flann_radius_search_int(flann_index_t index_ptr,
+                            int* query,
+                            int* indices,
+                            float* dists,
+                            int max_nn,
+                            float radius,
+                            FLANNParameters* flann_params)
+{
+    return _flann_radius_search(index_ptr, query, indices, dists, max_nn, radius, flann_params);
+}
+
+
+template<typename Distance>
+int __flann_free_index(flann_index_t index_ptr, FLANNParameters* flann_params)
+{
+    try {
+        init_flann_parameters(flann_params);
+        if (index_ptr==NULL) {
+            throw FLANNException("Invalid index");
+        }
+        Index<Distance>* index = (Index<Distance>*)index_ptr;
+        delete index;
+
+        return 0;
+    }
+    catch (std::runtime_error& e) {
+        Logger::error("Caught exception: %s\n",e.what());
+        return -1;
+    }
+}
+
+template<typename T>
+int _flann_free_index(flann_index_t index_ptr, FLANNParameters* flann_params)
+{
+    if (flann_distance_type==FLANN_DIST_EUCLIDEAN) {
+        return __flann_free_index<L2<T> >(index_ptr, flann_params);
+    }
+    else if (flann_distance_type==FLANN_DIST_MANHATTAN) {
+        return __flann_free_index<L1<T> >(index_ptr, flann_params);
+    }
+    else if (flann_distance_type==FLANN_DIST_MINKOWSKI) {
+        return __flann_free_index<MinkowskiDistance<T> >(index_ptr, flann_params);
+    }
+    else if (flann_distance_type==FLANN_DIST_HIST_INTERSECT) {
+        return __flann_free_index<HistIntersectionDistance<T> >(index_ptr, flann_params);
+    }
+    else if (flann_distance_type==FLANN_DIST_HELLINGER) {
+        return __flann_free_index<HellingerDistance<T> >(index_ptr, flann_params);
+    }
+    else if (flann_distance_type==FLANN_DIST_CHI_SQUARE) {
+        return __flann_free_index<ChiSquareDistance<T> >(index_ptr, flann_params);
+    }
+    else if (flann_distance_type==FLANN_DIST_KULLBACK_LEIBLER) {
+        return __flann_free_index<KL_Divergence<T> >(index_ptr, flann_params);
+    }
+    else {
+        Logger::error( "Distance type unsupported in the C bindings, use the C++ bindings instead\n");
+        return -1;
+    }
+}
+
+int flann_free_index(flann_index_t index_ptr, FLANNParameters* flann_params)
+{
+    return _flann_free_index<float>(index_ptr, flann_params);
+}
+
+int flann_free_index_float(flann_index_t index_ptr, FLANNParameters* flann_params)
+{
+    return _flann_free_index<float>(index_ptr, flann_params);
+}
+
+int flann_free_index_double(flann_index_t index_ptr, FLANNParameters* flann_params)
+{
+    return _flann_free_index<double>(index_ptr, flann_params);
+}
+
+int flann_free_index_byte(flann_index_t index_ptr, FLANNParameters* flann_params)
+{
+    return _flann_free_index<unsigned char>(index_ptr, flann_params);
+}
+
+int flann_free_index_int(flann_index_t index_ptr, FLANNParameters* flann_params)
+{
+    return _flann_free_index<int>(index_ptr, flann_params);
+}
+
+
+template<typename Distance>
+int __flann_compute_cluster_centers(typename Distance::ElementType* dataset, int rows, int cols, int clusters,
+                                    typename Distance::ResultType* result, FLANNParameters* flann_params, Distance d = Distance())
+{
+    typedef typename Distance::ElementType ElementType;
+    typedef typename Distance::ResultType DistanceType;
+
+    try {
+        init_flann_parameters(flann_params);
+
+        Matrix<ElementType> inputData(dataset,rows,cols);
+        KMeansIndexParams params(flann_params->branching, flann_params->iterations, flann_params->centers_init, flann_params->cb_index);
+        Matrix<DistanceType> centers(result,clusters,cols);
+        int clusterNum = hierarchicalClustering<Distance>(inputData, centers, params, d);
+
+        return clusterNum;
+    }
+    catch (std::runtime_error& e) {
+        Logger::error("Caught exception: %s\n",e.what());
+        return -1;
+    }
+}
+
+
+template<typename T, typename R>
+int _flann_compute_cluster_centers(T* dataset, int rows, int cols, int clusters, R* result, FLANNParameters* flann_params)
+{
+    if (flann_distance_type==FLANN_DIST_EUCLIDEAN) {
+        return __flann_compute_cluster_centers<L2<T> >(dataset, rows, cols, clusters, result, flann_params);
+    }
+    else if (flann_distance_type==FLANN_DIST_MANHATTAN) {
+        return __flann_compute_cluster_centers<L1<T> >(dataset, rows, cols, clusters, result, flann_params);
+    }
+    else if (flann_distance_type==FLANN_DIST_MINKOWSKI) {
+        return __flann_compute_cluster_centers<MinkowskiDistance<T> >(dataset, rows, cols, clusters, result, flann_params, MinkowskiDistance<T>(flann_distance_order));
+    }
+    else if (flann_distance_type==FLANN_DIST_HIST_INTERSECT) {
+        return __flann_compute_cluster_centers<HistIntersectionDistance<T> >(dataset, rows, cols, clusters, result, flann_params);
+    }
+    else if (flann_distance_type==FLANN_DIST_HELLINGER) {
+        return __flann_compute_cluster_centers<HellingerDistance<T> >(dataset, rows, cols, clusters, result, flann_params);
+    }
+    else if (flann_distance_type==FLANN_DIST_CHI_SQUARE) {
+        return __flann_compute_cluster_centers<ChiSquareDistance<T> >(dataset, rows, cols, clusters, result, flann_params);
+    }
+    else if (flann_distance_type==FLANN_DIST_KULLBACK_LEIBLER) {
+        return __flann_compute_cluster_centers<KL_Divergence<T> >(dataset, rows, cols, clusters, result, flann_params);
+    }
+    else {
+        Logger::error( "Distance type unsupported in the C bindings, use the C++ bindings instead\n");
+        return -1;
+    }
+}
+
+int flann_compute_cluster_centers(float* dataset, int rows, int cols, int clusters, float* result, FLANNParameters* flann_params)
+{
+    return _flann_compute_cluster_centers(dataset, rows, cols, clusters, result, flann_params);
+}
+
+int flann_compute_cluster_centers_float(float* dataset, int rows, int cols, int clusters, float* result, FLANNParameters* flann_params)
+{
+    return _flann_compute_cluster_centers(dataset, rows, cols, clusters, result, flann_params);
+}
+
+int flann_compute_cluster_centers_double(double* dataset, int rows, int cols, int clusters, double* result, FLANNParameters* flann_params)
+{
+    return _flann_compute_cluster_centers(dataset, rows, cols, clusters, result, flann_params);
+}
+
+int flann_compute_cluster_centers_byte(unsigned char* dataset, int rows, int cols, int clusters, float* result, FLANNParameters* flann_params)
+{
+    return _flann_compute_cluster_centers(dataset, rows, cols, clusters, result, flann_params);
+}
+
+int flann_compute_cluster_centers_int(int* dataset, int rows, int cols, int clusters, float* result, FLANNParameters* flann_params)
+{
+    return _flann_compute_cluster_centers(dataset, rows, cols, clusters, result, flann_params);
+}
+
diff --git a/src/cpp/flann/flann.h b/src/cpp/flann/flann.h
new file mode 100644
index 0000000..90d1cf8
--- /dev/null
+++ b/src/cpp/flann/flann.h
@@ -0,0 +1,472 @@
+/***********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2008-2009  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+ * Copyright 2008-2009  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+ *
+ * THE BSD LICENSE
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+
+#ifndef FLANN_H_
+#define FLANN_H_
+
+#include "defines.h"
+
+#ifdef __cplusplus
+extern "C"
+{
+    using namespace flann;
+#endif
+
+
+struct FLANNParameters
+{
+    enum flann_algorithm_t algorithm; /* the algorithm to use */
+
+    /* search time parameters */
+    int checks;                /* how many leafs (features) to check in one search */
+    float eps;     /* eps parameter for eps-knn search */
+    int sorted;     /* indicates if results returned by radius search should be sorted or not */
+    int max_neighbors;  /* limits the maximum number of neighbors should be returned by radius search */
+    int cores;      /* number of paralel cores to use for searching */
+
+    /*  kdtree index parameters */
+    int trees;                 /* number of randomized trees to use (for kdtree) */
+    int leaf_max_size;
+
+    /* kmeans index parameters */
+    int branching;             /* branching factor (for kmeans tree) */
+    int iterations;            /* max iterations to perform in one kmeans cluetering (kmeans tree) */
+    enum flann_centers_init_t centers_init;  /* algorithm used for picking the initial cluster centers for kmeans tree */
+    float cb_index;            /* cluster boundary index. Used when searching the kmeans tree */
+
+    /* autotuned index parameters */
+    float target_precision;    /* precision desired (used for autotuning, -1 otherwise) */
+    float build_weight;        /* build tree time weighting factor */
+    float memory_weight;       /* index memory weigthing factor */
+    float sample_fraction;     /* what fraction of the dataset to use for autotuning */
+
+    /* LSH parameters */
+    unsigned int table_number_; /** The number of hash tables to use */
+    unsigned int key_size_;     /** The length of the key in the hash tables */
+    unsigned int multi_probe_level_; /** Number of levels to use in multi-probe LSH, 0 for standard LSH */
+
+    /* other parameters */
+    enum flann_log_level_t log_level;    /* determines the verbosity of each flann function */
+    long random_seed;            /* random seed to use */
+};
+
+
+typedef void* FLANN_INDEX; /* deprecated */
+typedef void* flann_index_t;
+
+FLANN_EXPORT extern struct FLANNParameters DEFAULT_FLANN_PARAMETERS;
+
+/**
+   Sets the log level used for all flann functions (unless
+   specified in FLANNParameters for each call
+
+   Params:
+    level = verbosity level
+ */
+FLANN_EXPORT void flann_log_verbosity(int level);
+
+
+/**
+ * Sets the distance type to use throughout FLANN.
+ * If distance type specified is MINKOWSKI, the second argument
+ * specifies which order the minkowski distance should have.
+ */
+FLANN_EXPORT void flann_set_distance_type(enum flann_distance_t distance_type, int order);
+
+
+/**
+   Builds and returns an index. It uses autotuning if the target_precision field of index_params
+   is between 0 and 1, or the parameters specified if it's -1.
+
+   Params:
+    dataset = pointer to a data set stored in row major order
+    rows = number of rows (features) in the dataset
+    cols = number of columns in the dataset (feature dimensionality)
+    speedup = speedup over linear search, estimated if using autotuning, output parameter
+    index_params = index related parameters
+    flann_params = generic flann parameters
+
+   Returns: the newly created index or a number <0 for error
+ */
+FLANN_EXPORT flann_index_t flann_build_index(float* dataset,
+                                             int rows,
+                                             int cols,
+                                             float* speedup,
+                                             struct FLANNParameters* flann_params);
+
+FLANN_EXPORT flann_index_t flann_build_index_float(float* dataset,
+                                                   int rows,
+                                                   int cols,
+                                                   float* speedup,
+                                                   struct FLANNParameters* flann_params);
+
+FLANN_EXPORT flann_index_t flann_build_index_double(double* dataset,
+                                                    int rows,
+                                                    int cols,
+                                                    float* speedup,
+                                                    struct FLANNParameters* flann_params);
+
+FLANN_EXPORT flann_index_t flann_build_index_byte(unsigned char* dataset,
+                                                  int rows,
+                                                  int cols,
+                                                  float* speedup,
+                                                  struct FLANNParameters* flann_params);
+
+FLANN_EXPORT flann_index_t flann_build_index_int(int* dataset,
+                                                 int rows,
+                                                 int cols,
+                                                 float* speedup,
+                                                 struct FLANNParameters* flann_params);
+
+/**
+ * Saves the index to a file. Only the index is saved into the file, the dataset corresponding to the index is not saved.
+ *
+ * @param index_id The index that should be saved
+ * @param filename The filename the index should be saved to
+ * @return Returns 0 on success, negative value on error.
+ */
+FLANN_EXPORT int flann_save_index(flann_index_t index_id,
+                                  char* filename);
+
+FLANN_EXPORT int flann_save_index_float(flann_index_t index_id,
+                                        char* filename);
+
+FLANN_EXPORT int flann_save_index_double(flann_index_t index_id,
+                                         char* filename);
+
+FLANN_EXPORT int flann_save_index_byte(flann_index_t index_id,
+                                       char* filename);
+
+FLANN_EXPORT int flann_save_index_int(flann_index_t index_id,
+                                      char* filename);
+
+/**
+ * Loads an index from a file.
+ *
+ * @param filename File to load the index from.
+ * @param dataset The dataset corresponding to the index.
+ * @param rows Dataset tors
+ * @param cols Dataset columns
+ * @return
+ */
+FLANN_EXPORT flann_index_t flann_load_index(char* filename,
+                                            float* dataset,
+                                            int rows,
+                                            int cols);
+
+FLANN_EXPORT flann_index_t flann_load_index_float(char* filename,
+                                                  float* dataset,
+                                                  int rows,
+                                                  int cols);
+
+FLANN_EXPORT flann_index_t flann_load_index_double(char* filename,
+                                                   double* dataset,
+                                                   int rows,
+                                                   int cols);
+
+FLANN_EXPORT flann_index_t flann_load_index_byte(char* filename,
+                                                 unsigned char* dataset,
+                                                 int rows,
+                                                 int cols);
+
+FLANN_EXPORT flann_index_t flann_load_index_int(char* filename,
+                                                int* dataset,
+                                                int rows,
+                                                int cols);
+
+
+/**
+   Builds an index and uses it to find nearest neighbors.
+
+   Params:
+    dataset = pointer to a data set stored in row major order
+    rows = number of rows (features) in the dataset
+    cols = number of columns in the dataset (feature dimensionality)
+    testset = pointer to a query set stored in row major order
+    trows = number of rows (features) in the query dataset (same dimensionality as features in the dataset)
+    indices = pointer to matrix for the indices of the nearest neighbors of the testset features in the dataset
+            (must have trows number of rows and nn number of columns)
+    nn = how many nearest neighbors to return
+    flann_params = generic flann parameters
+
+   Returns: zero or -1 for error
+ */
+FLANN_EXPORT int flann_find_nearest_neighbors(float* dataset,
+                                              int rows,
+                                              int cols,
+                                              float* testset,
+                                              int trows,
+                                              int* indices,
+                                              float* dists,
+                                              int nn,
+                                              struct FLANNParameters* flann_params);
+
+FLANN_EXPORT int flann_find_nearest_neighbors_float(float* dataset,
+                                                    int rows,
+                                                    int cols,
+                                                    float* testset,
+                                                    int trows,
+                                                    int* indices,
+                                                    float* dists,
+                                                    int nn,
+                                                    struct FLANNParameters* flann_params);
+
+FLANN_EXPORT int flann_find_nearest_neighbors_double(double* dataset,
+                                                     int rows,
+                                                     int cols,
+                                                     double* testset,
+                                                     int trows,
+                                                     int* indices,
+                                                     double* dists,
+                                                     int nn,
+                                                     struct FLANNParameters* flann_params);
+
+FLANN_EXPORT int flann_find_nearest_neighbors_byte(unsigned char* dataset,
+                                                   int rows,
+                                                   int cols,
+                                                   unsigned char* testset,
+                                                   int trows,
+                                                   int* indices,
+                                                   float* dists,
+                                                   int nn,
+                                                   struct FLANNParameters* flann_params);
+
+FLANN_EXPORT int flann_find_nearest_neighbors_int(int* dataset,
+                                                  int rows,
+                                                  int cols,
+                                                  int* testset,
+                                                  int trows,
+                                                  int* indices,
+                                                  float* dists,
+                                                  int nn,
+                                                  struct FLANNParameters* flann_params);
+
+
+/**
+   Searches for nearest neighbors using the index provided
+
+   Params:
+    index_id = the index (constructed previously using flann_build_index).
+    testset = pointer to a query set stored in row major order
+    trows = number of rows (features) in the query dataset (same dimensionality as features in the dataset)
+    indices = pointer to matrix for the indices of the nearest neighbors of the testset features in the dataset
+            (must have trows number of rows and nn number of columns)
+    dists = pointer to matrix for the distances of the nearest neighbors of the testset features in the dataset
+            (must have trows number of rows and 1 column)
+    nn = how many nearest neighbors to return
+    flann_params = generic flann parameters
+
+   Returns: zero or a number <0 for error
+ */
+FLANN_EXPORT int flann_find_nearest_neighbors_index(flann_index_t index_id,
+                                                    float* testset,
+                                                    int trows,
+                                                    int* indices,
+                                                    float* dists,
+                                                    int nn,
+                                                    struct FLANNParameters* flann_params);
+
+FLANN_EXPORT int flann_find_nearest_neighbors_index_float(flann_index_t index_id,
+                                                          float* testset,
+                                                          int trows,
+                                                          int* indices,
+                                                          float* dists,
+                                                          int nn,
+                                                          struct FLANNParameters* flann_params);
+
+FLANN_EXPORT int flann_find_nearest_neighbors_index_double(flann_index_t index_id,
+                                                           double* testset,
+                                                           int trows,
+                                                           int* indices,
+                                                           double* dists,
+                                                           int nn,
+                                                           struct FLANNParameters* flann_params);
+
+FLANN_EXPORT int flann_find_nearest_neighbors_index_byte(flann_index_t index_id,
+                                                         unsigned char* testset,
+                                                         int trows,
+                                                         int* indices,
+                                                         float* dists,
+                                                         int nn,
+                                                         struct FLANNParameters* flann_params);
+
+FLANN_EXPORT int flann_find_nearest_neighbors_index_int(flann_index_t index_id,
+                                                        int* testset,
+                                                        int trows,
+                                                        int* indices,
+                                                        float* dists,
+                                                        int nn,
+                                                        struct FLANNParameters* flann_params);
+
+
+/**
+ * Performs an radius search using an already constructed index.
+ *
+ * In case of radius search, instead of always returning a predetermined
+ * number of nearest neighbours (for example the 10 nearest neighbours), the
+ * search will return all the neighbours found within a search radius
+ * of the query point.
+ *
+ * The check parameter in the FLANNParameters below sets the level of approximation
+ * for the search by only visiting "checks" number of features in the index
+ * (the same way as for the KNN search). A lower value for checks will give
+ * a higher search speedup at the cost of potentially not returning all the
+ * neighbours in the specified radius.
+ */
+FLANN_EXPORT int flann_radius_search(flann_index_t index_ptr, /* the index */
+                                     float* query, /* query point */
+                                     int* indices, /* array for storing the indices found (will be modified) */
+                                     float* dists, /* similar, but for storing distances */
+                                     int max_nn,  /* size of arrays indices and dists */
+                                     float radius, /* search radius (squared radius for euclidian metric) */
+                                     struct FLANNParameters* flann_params);
+
+FLANN_EXPORT int flann_radius_search_float(flann_index_t index_ptr, /* the index */
+                                           float* query, /* query point */
+                                           int* indices, /* array for storing the indices found (will be modified) */
+                                           float* dists, /* similar, but for storing distances */
+                                           int max_nn,  /* size of arrays indices and dists */
+                                           float radius, /* search radius (squared radius for euclidian metric) */
+                                           struct FLANNParameters* flann_params);
+
+FLANN_EXPORT int flann_radius_search_double(flann_index_t index_ptr, /* the index */
+                                            double* query, /* query point */
+                                            int* indices, /* array for storing the indices found (will be modified) */
+                                            double* dists, /* similar, but for storing distances */
+                                            int max_nn,  /* size of arrays indices and dists */
+                                            float radius, /* search radius (squared radius for euclidian metric) */
+                                            struct FLANNParameters* flann_params);
+
+FLANN_EXPORT int flann_radius_search_byte(flann_index_t index_ptr, /* the index */
+                                          unsigned char* query, /* query point */
+                                          int* indices, /* array for storing the indices found (will be modified) */
+                                          float* dists, /* similar, but for storing distances */
+                                          int max_nn,  /* size of arrays indices and dists */
+                                          float radius, /* search radius (squared radius for euclidian metric) */
+                                          struct FLANNParameters* flann_params);
+
+FLANN_EXPORT int flann_radius_search_int(flann_index_t index_ptr, /* the index */
+                                         int* query, /* query point */
+                                         int* indices, /* array for storing the indices found (will be modified) */
+                                         float* dists, /* similar, but for storing distances */
+                                         int max_nn,  /* size of arrays indices and dists */
+                                         float radius, /* search radius (squared radius for euclidian metric) */
+                                         struct FLANNParameters* flann_params);
+
+/**
+   Deletes an index and releases the memory used by it.
+
+   Params:
+    index_id = the index (constructed previously using flann_build_index).
+    flann_params = generic flann parameters
+
+   Returns: zero or a number <0 for error
+ */
+FLANN_EXPORT int flann_free_index(flann_index_t index_id,
+                                  struct FLANNParameters* flann_params);
+
+FLANN_EXPORT int flann_free_index_float(flann_index_t index_id,
+                                        struct FLANNParameters* flann_params);
+
+FLANN_EXPORT int flann_free_index_double(flann_index_t index_id,
+                                         struct FLANNParameters* flann_params);
+
+FLANN_EXPORT int flann_free_index_byte(flann_index_t index_id,
+                                       struct FLANNParameters* flann_params);
+
+FLANN_EXPORT int flann_free_index_int(flann_index_t index_id,
+                                      struct FLANNParameters* flann_params);
+
+/**
+   Clusters the features in the dataset using a hierarchical kmeans clustering approach.
+   This is significantly faster than using a flat kmeans clustering for a large number
+   of clusters.
+
+   Params:
+    dataset = pointer to a data set stored in row major order
+    rows = number of rows (features) in the dataset
+    cols = number of columns in the dataset (feature dimensionality)
+    clusters = number of cluster to compute
+    result = memory buffer where the output cluster centers are storred
+    index_params = used to specify the kmeans tree parameters (branching factor, max number of iterations to use)
+    flann_params = generic flann parameters
+
+   Returns: number of clusters computed or a number <0 for error. This number can be different than the number of clusters requested, due to the
+    way hierarchical clusters are computed. The number of clusters returned will be the highest number of the form
+    (branch_size-1)*K+1 smaller than the number of clusters requested.
+ */
+
+FLANN_EXPORT int flann_compute_cluster_centers(float* dataset,
+                                               int rows,
+                                               int cols,
+                                               int clusters,
+                                               float* result,
+                                               struct FLANNParameters* flann_params);
+
+FLANN_EXPORT int flann_compute_cluster_centers_float(float* dataset,
+                                                     int rows,
+                                                     int cols,
+                                                     int clusters,
+                                                     float* result,
+                                                     struct FLANNParameters* flann_params);
+
+FLANN_EXPORT int flann_compute_cluster_centers_double(double* dataset,
+                                                      int rows,
+                                                      int cols,
+                                                      int clusters,
+                                                      double* result,
+                                                      struct FLANNParameters* flann_params);
+
+FLANN_EXPORT int flann_compute_cluster_centers_byte(unsigned char* dataset,
+                                                    int rows,
+                                                    int cols,
+                                                    int clusters,
+                                                    float* result,
+                                                    struct FLANNParameters* flann_params);
+
+FLANN_EXPORT int flann_compute_cluster_centers_int(int* dataset,
+                                                   int rows,
+                                                   int cols,
+                                                   int clusters,
+                                                   float* result,
+                                                   struct FLANNParameters* flann_params);
+
+
+#ifdef __cplusplus
+}
+
+
+#include "flann.hpp"
+
+#endif
+
+
+#endif /*FLANN_H_*/
+
diff --git a/src/cpp/flann/flann.hpp b/src/cpp/flann/flann.hpp
new file mode 100644
index 0000000..444e05d
--- /dev/null
+++ b/src/cpp/flann/flann.hpp
@@ -0,0 +1,435 @@
+/***********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2008-2009  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+ * Copyright 2008-2009  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+ *
+ * THE BSD LICENSE
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+
+#ifndef FLANN_HPP_
+#define FLANN_HPP_
+
+
+#include <vector>
+#include <string>
+#include <cassert>
+#include <cstdio>
+
+#include "flann/general.h"
+#include "flann/util/matrix.h"
+#include "flann/util/params.h"
+#include "flann/util/saving.h"
+
+#include "flann/algorithms/all_indices.h"
+
+namespace flann
+{
+
+/**
+ * Sets the log level used for all flann functions
+ * @param level Verbosity level
+ */
+inline void log_verbosity(int level)
+{
+    if (level >= 0) {
+        Logger::setLevel(level);
+    }
+}
+
+/**
+ * (Deprecated) Index parameters for creating a saved index.
+ */
+struct SavedIndexParams : public IndexParams
+{
+    SavedIndexParams(std::string filename)
+    {
+        (*this)["algorithm"] = FLANN_INDEX_SAVED;
+        (*this)["filename"] = filename;
+    }
+};
+
+
+
+template<typename Distance>
+class Index
+{
+public:
+    typedef typename Distance::ElementType ElementType;
+    typedef typename Distance::ResultType DistanceType;
+    typedef NNIndex<Distance> IndexType;
+
+    Index(const IndexParams& params, Distance distance = Distance() )
+        : index_params_(params)
+    {
+        flann_algorithm_t index_type = get_param<flann_algorithm_t>(params,"algorithm");
+        loaded_ = false;
+
+        Matrix<ElementType> features;
+        if (index_type == FLANN_INDEX_SAVED) {
+            nnIndex_ = load_saved_index(features, get_param<std::string>(params,"filename"), distance);
+            loaded_ = true;
+        }
+        else {
+        	flann_algorithm_t index_type = get_param<flann_algorithm_t>(params, "algorithm");
+            nnIndex_ = create_index_by_type<Distance>(index_type, features, params, distance);
+        }
+    }
+
+
+    Index(const Matrix<ElementType>& features, const IndexParams& params, Distance distance = Distance() )
+        : index_params_(params)
+    {
+        flann_algorithm_t index_type = get_param<flann_algorithm_t>(params,"algorithm");
+        loaded_ = false;
+
+        if (index_type == FLANN_INDEX_SAVED) {
+            nnIndex_ = load_saved_index(features, get_param<std::string>(params,"filename"), distance);
+            loaded_ = true;
+        }
+        else {
+        	flann_algorithm_t index_type = get_param<flann_algorithm_t>(params, "algorithm");
+            nnIndex_ = create_index_by_type<Distance>(index_type, features, params, distance);
+        }
+    }
+
+
+    Index(const Index& other) : loaded_(other.loaded_), index_params_(other.index_params_)
+    {
+    	nnIndex_ = other.nnIndex_->clone();
+    }
+
+    Index& operator=(Index other)
+    {
+    	this->swap(other);
+    	return *this;
+    }
+
+    virtual ~Index()
+    {
+        delete nnIndex_;
+    }
+
+    /**
+     * Builds the index.
+     */
+    void buildIndex()
+    {
+        if (!loaded_) {
+            nnIndex_->buildIndex();
+        }
+    }
+
+    void buildIndex(const Matrix<ElementType>& points)
+    {
+    	nnIndex_->buildIndex(points);
+    }
+
+    void addPoints(const Matrix<ElementType>& points, float rebuild_threshold = 2)
+    {
+        nnIndex_->addPoints(points, rebuild_threshold);
+    }
+
+    /**
+     * Remove point from the index
+     * @param index Index of point to be removed
+     */
+    void removePoint(size_t point_id)
+    {
+    	nnIndex_->removePoint(point_id);
+    }
+
+    /**
+     * Returns pointer to a data point with the specified id.
+     * @param point_id the id of point to retrieve
+     * @return
+     */
+    ElementType* getPoint(size_t point_id)
+    {
+    	return nnIndex_->getPoint(point_id);
+    }
+
+    /**
+     * Save index to file
+     * @param filename
+     */
+    void save(std::string filename)
+    {
+        FILE* fout = fopen(filename.c_str(), "wb");
+        if (fout == NULL) {
+            throw FLANNException("Cannot open file");
+        }
+        nnIndex_->saveIndex(fout);
+        fclose(fout);
+    }
+
+    /**
+     * \returns number of features in this index.
+     */
+    size_t veclen() const
+    {
+        return nnIndex_->veclen();
+    }
+
+    /**
+     * \returns The dimensionality of the features in this index.
+     */
+    size_t size() const
+    {
+        return nnIndex_->size();
+    }
+
+    /**
+     * \returns The index type (kdtree, kmeans,...)
+     */
+    flann_algorithm_t getType() const
+    {
+        return nnIndex_->getType();
+    }
+
+    /**
+     * \returns The amount of memory (in bytes) used by the index.
+     */
+    int usedMemory() const
+    {
+        return nnIndex_->usedMemory();
+    }
+
+
+    /**
+     * \returns The index parameters
+     */
+    IndexParams getParameters() const
+    {
+        return nnIndex_->getParameters();
+    }
+
+    /**
+     * \brief Perform k-nearest neighbor search
+     * \param[in] queries The query points for which to find the nearest neighbors
+     * \param[out] indices The indices of the nearest neighbors found
+     * \param[out] dists Distances to the nearest neighbors found
+     * \param[in] knn Number of nearest neighbors to return
+     * \param[in] params Search parameters
+     */
+    int knnSearch(const Matrix<ElementType>& queries,
+                                 Matrix<size_t>& indices,
+                                 Matrix<DistanceType>& dists,
+                                 size_t knn,
+                           const SearchParams& params) const
+    {
+    	return nnIndex_->knnSearch(queries, indices, dists, knn, params);
+    }
+
+    /**
+     *
+     * @param queries
+     * @param indices
+     * @param dists
+     * @param knn
+     * @param params
+     * @return
+     */
+    int knnSearch(const Matrix<ElementType>& queries,
+                                 Matrix<int>& indices,
+                                 Matrix<DistanceType>& dists,
+                                 size_t knn,
+                           const SearchParams& params) const
+    {
+    	return nnIndex_->knnSearch(queries, indices, dists, knn, params);
+    }
+
+    /**
+     * \brief Perform k-nearest neighbor search
+     * \param[in] queries The query points for which to find the nearest neighbors
+     * \param[out] indices The indices of the nearest neighbors found
+     * \param[out] dists Distances to the nearest neighbors found
+     * \param[in] knn Number of nearest neighbors to return
+     * \param[in] params Search parameters
+     */
+    int knnSearch(const Matrix<ElementType>& queries,
+                                 std::vector< std::vector<size_t> >& indices,
+                                 std::vector<std::vector<DistanceType> >& dists,
+                                 size_t knn,
+                           const SearchParams& params)
+    {
+    	return nnIndex_->knnSearch(queries, indices, dists, knn, params);
+    }
+
+    /**
+     *
+     * @param queries
+     * @param indices
+     * @param dists
+     * @param knn
+     * @param params
+     * @return
+     */
+    int knnSearch(const Matrix<ElementType>& queries,
+                                 std::vector< std::vector<int> >& indices,
+                                 std::vector<std::vector<DistanceType> >& dists,
+                                 size_t knn,
+                           const SearchParams& params) const
+    {
+    	return nnIndex_->knnSearch(queries, indices, dists, knn, params);
+    }
+
+    /**
+     * \brief Perform radius search
+     * \param[in] queries The query points
+     * \param[out] indices The indinces of the neighbors found within the given radius
+     * \param[out] dists The distances to the nearest neighbors found
+     * \param[in] radius The radius used for search
+     * \param[in] params Search parameters
+     * \returns Number of neighbors found
+     */
+    int radiusSearch(const Matrix<ElementType>& queries,
+                                    Matrix<size_t>& indices,
+                                    Matrix<DistanceType>& dists,
+                                    float radius,
+                              const SearchParams& params) const
+    {
+    	return nnIndex_->radiusSearch(queries, indices, dists, radius, params);
+    }
+
+    /**
+     *
+     * @param queries
+     * @param indices
+     * @param dists
+     * @param radius
+     * @param params
+     * @return
+     */
+    int radiusSearch(const Matrix<ElementType>& queries,
+                                    Matrix<int>& indices,
+                                    Matrix<DistanceType>& dists,
+                                    float radius,
+                              const SearchParams& params) const
+    {
+    	return nnIndex_->radiusSearch(queries, indices, dists, radius, params);
+    }
+
+    /**
+     * \brief Perform radius search
+     * \param[in] queries The query points
+     * \param[out] indices The indinces of the neighbors found within the given radius
+     * \param[out] dists The distances to the nearest neighbors found
+     * \param[in] radius The radius used for search
+     * \param[in] params Search parameters
+     * \returns Number of neighbors found
+     */
+    int radiusSearch(const Matrix<ElementType>& queries,
+                                    std::vector< std::vector<size_t> >& indices,
+                                    std::vector<std::vector<DistanceType> >& dists,
+                                    float radius,
+                              const SearchParams& params) const
+    {
+    	return nnIndex_->radiusSearch(queries, indices, dists, radius, params);
+    }
+
+    /**
+     *
+     * @param queries
+     * @param indices
+     * @param dists
+     * @param radius
+     * @param params
+     * @return
+     */
+    int radiusSearch(const Matrix<ElementType>& queries,
+                                    std::vector< std::vector<int> >& indices,
+                                    std::vector<std::vector<DistanceType> >& dists,
+                                    float radius,
+                              const SearchParams& params) const
+    {
+    	return nnIndex_->radiusSearch(queries, indices, dists, radius, params);
+    }
+
+private:
+    IndexType* load_saved_index(const Matrix<ElementType>& dataset, const std::string& filename, Distance distance)
+    {
+        FILE* fin = fopen(filename.c_str(), "rb");
+        if (fin == NULL) {
+            return NULL;
+        }
+        IndexHeader header = load_header(fin);
+        if (header.data_type != flann_datatype_value<ElementType>::value) {
+            throw FLANNException("Datatype of saved index is different than of the one to be created.");
+        }
+
+        IndexParams params;
+        params["algorithm"] = header.index_type;
+        IndexType* nnIndex = create_index_by_type<Distance>(header.index_type, dataset, params, distance);
+        rewind(fin);
+        nnIndex->loadIndex(fin);
+        fclose(fin);
+
+        return nnIndex;
+    }
+
+    void swap( Index& other)
+    {
+    	std::swap(nnIndex_, other.nnIndex_);
+    	std::swap(loaded_, other.loaded_);
+    	std::swap(index_params_, other.index_params_);
+    }
+
+private:
+    /** Pointer to actual index class */
+    IndexType* nnIndex_;
+    /** Indices if the index was loaded from a file */
+    bool loaded_;
+    /** Parameters passed to the index */
+    IndexParams index_params_;
+};
+
+
+
+
+
+/**
+ * Performs a hierarchical clustering of the points passed as argument and then takes a cut in the
+ * the clustering tree to return a flat clustering.
+ * @param[in] points Points to be clustered
+ * @param centers The computed cluster centres. Matrix should be preallocated and centers.rows is the
+ *  number of clusters requested.
+ * @param params Clustering parameters (The same as for flann::KMeansIndex)
+ * @param d Distance to be used for clustering (eg: flann::L2)
+ * @return number of clusters computed (can be different than clusters.rows and is the highest number
+ * of the form (branching-1)*K+1 smaller than clusters.rows).
+ */
+template <typename Distance>
+int hierarchicalClustering(const Matrix<typename Distance::ElementType>& points, Matrix<typename Distance::ResultType>& centers,
+                           const KMeansIndexParams& params, Distance d = Distance())
+{
+    KMeansIndex<Distance> kmeans(points, params, d);
+    kmeans.buildIndex();
+
+    int clusterNum = kmeans.getClusterCenters(centers);
+    return clusterNum;
+}
+
+}
+#endif /* FLANN_HPP_ */
diff --git a/src/cpp/flann/flann_cpp.cpp b/src/cpp/flann/flann_cpp.cpp
new file mode 100644
index 0000000..2ab3f73
--- /dev/null
+++ b/src/cpp/flann/flann_cpp.cpp
@@ -0,0 +1,30 @@
+/***********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2008-2009  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+ * Copyright 2008-2009  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+
+
+#include "flann/flann.hpp"
diff --git a/src/cpp/flann/general.h b/src/cpp/flann/general.h
new file mode 100644
index 0000000..d57ec52
--- /dev/null
+++ b/src/cpp/flann/general.h
@@ -0,0 +1,227 @@
+/***********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2008-2009  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+ * Copyright 2008-2009  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+ *
+ * THE BSD LICENSE
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+
+#ifndef FLANN_GENERAL_H_
+#define FLANN_GENERAL_H_
+
+#include "defines.h"
+#include <stdexcept>
+#include <cassert>
+#include <limits.h>
+
+namespace flann
+{
+
+class FLANNException : public std::runtime_error
+{
+public:
+    FLANNException(const char* message) : std::runtime_error(message) { }
+
+    FLANNException(const std::string& message) : std::runtime_error(message) { }
+};
+
+
+template <typename T>
+struct flann_datatype_value
+{
+	static const flann_datatype_t value = FLANN_NONE;
+};
+
+template<>
+struct flann_datatype_value<char>
+{
+	static const flann_datatype_t value = FLANN_INT8;
+};
+
+template<>
+struct flann_datatype_value<short>
+{
+	static const flann_datatype_t value = FLANN_INT16;
+};
+
+template<>
+struct flann_datatype_value<int>
+{
+	static const flann_datatype_t value = FLANN_INT32;
+};
+
+#ifdef LLONG_MAX
+template<>
+struct flann_datatype_value<long long>
+{
+	static const flann_datatype_t value = FLANN_INT64;
+};
+#endif
+
+template<>
+struct flann_datatype_value<unsigned char>
+{
+	static const flann_datatype_t value = FLANN_UINT8;
+};
+
+template<>
+struct flann_datatype_value<unsigned short>
+{
+	static const flann_datatype_t value = FLANN_UINT16;
+};
+
+template<>
+struct flann_datatype_value<unsigned int>
+{
+	static const flann_datatype_t value = FLANN_UINT32;
+};
+
+#ifdef ULLONG_MAX
+template<>
+struct flann_datatype_value<unsigned long long>
+{
+	static const flann_datatype_t value = FLANN_UINT64;
+};
+#endif
+
+
+template<>
+struct flann_datatype_value<float>
+{
+	static const flann_datatype_t value = FLANN_FLOAT32;
+};
+
+template<>
+struct flann_datatype_value<double>
+{
+	static const flann_datatype_t value = FLANN_FLOAT64;
+};
+
+
+
+template <flann_datatype_t datatype>
+struct flann_datatype_type
+{
+	typedef void type;
+};
+
+template<>
+struct flann_datatype_type<FLANN_INT8>
+{
+	typedef char type;
+};
+
+template<>
+struct flann_datatype_type<FLANN_INT16>
+{
+	typedef short type;
+};
+
+template<>
+struct flann_datatype_type<FLANN_INT32>
+{
+	typedef int type;
+};
+
+#ifdef LLONG_MAX
+template<>
+struct flann_datatype_type<FLANN_INT64>
+{
+	typedef long long type;
+};
+#endif
+
+template<>
+struct flann_datatype_type<FLANN_UINT8>
+{
+	typedef unsigned char type;
+};
+
+
+template<>
+struct flann_datatype_type<FLANN_UINT16>
+{
+	typedef unsigned short type;
+};
+
+template<>
+struct flann_datatype_type<FLANN_UINT32>
+{
+	typedef unsigned int type;
+};
+
+#ifdef ULLONG_MAX
+template<>
+struct flann_datatype_type<FLANN_UINT64>
+{
+	typedef unsigned long long type;
+};
+#endif
+
+template<>
+struct flann_datatype_type<FLANN_FLOAT32>
+{
+	typedef float type;
+};
+
+template<>
+struct flann_datatype_type<FLANN_FLOAT64>
+{
+	typedef double type;
+};
+
+
+inline size_t flann_datatype_size(flann_datatype_t type)
+{
+	switch (type) {
+	case FLANN_INT8:
+		return sizeof(flann_datatype_type<FLANN_INT8>::type);
+	case FLANN_INT16:
+		return sizeof(flann_datatype_type<FLANN_INT16>::type);
+	case FLANN_INT32:
+		return sizeof(flann_datatype_type<FLANN_INT32>::type);
+	case FLANN_INT64:
+		return sizeof(flann_datatype_type<FLANN_INT64>::type);
+	case FLANN_UINT8:
+		return sizeof(flann_datatype_type<FLANN_UINT8>::type);
+	case FLANN_UINT16:
+		return sizeof(flann_datatype_type<FLANN_UINT16>::type);
+	case FLANN_UINT32:
+		return sizeof(flann_datatype_type<FLANN_UINT32>::type);
+	case FLANN_UINT64:
+		return sizeof(flann_datatype_type<FLANN_UINT64>::type);
+	case FLANN_FLOAT32:
+		return sizeof(flann_datatype_type<FLANN_FLOAT32>::type);
+	case FLANN_FLOAT64:
+		return sizeof(flann_datatype_type<FLANN_FLOAT64>::type);
+	default:
+		return 0;
+	}
+}
+
+}
+
+
+#endif  /* FLANN_GENERAL_H_ */
diff --git a/src/cpp/flann/io/hdf5.h b/src/cpp/flann/io/hdf5.h
new file mode 100644
index 0000000..b8a1955
--- /dev/null
+++ b/src/cpp/flann/io/hdf5.h
@@ -0,0 +1,231 @@
+/***********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2008-2009  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+ * Copyright 2008-2009  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+
+
+#ifndef FLANN_HDF5_H_
+#define FLANN_HDF5_H_
+
+#include <hdf5.h>
+
+#include "flann/util/matrix.h"
+
+
+namespace flann
+{
+
+namespace
+{
+
+template<typename T>
+hid_t get_hdf5_type()
+{
+    throw FLANNException("Unsupported type for IO operations");
+}
+
+template<>
+hid_t get_hdf5_type<char>() { return H5T_NATIVE_CHAR; }
+template<>
+hid_t get_hdf5_type<unsigned char>() { return H5T_NATIVE_UCHAR; }
+template<>
+hid_t get_hdf5_type<short int>() { return H5T_NATIVE_SHORT; }
+template<>
+hid_t get_hdf5_type<unsigned short int>() { return H5T_NATIVE_USHORT; }
+template<>
+hid_t get_hdf5_type<int>() { return H5T_NATIVE_INT; }
+template<>
+hid_t get_hdf5_type<unsigned int>() { return H5T_NATIVE_UINT; }
+template<>
+hid_t get_hdf5_type<long>() { return H5T_NATIVE_LONG; }
+template<>
+hid_t get_hdf5_type<unsigned long>() { return H5T_NATIVE_ULONG; }
+template<>
+hid_t get_hdf5_type<float>() { return H5T_NATIVE_FLOAT; }
+template<>
+hid_t get_hdf5_type<double>() { return H5T_NATIVE_DOUBLE; }
+template<>
+hid_t get_hdf5_type<long double>() { return H5T_NATIVE_LDOUBLE; }
+}
+
+
+#define CHECK_ERROR(x,y) if ((x)<0) throw FLANNException((y));
+
+template<typename T>
+void save_to_file(const flann::Matrix<T>& dataset, const std::string& filename, const std::string& name)
+{
+
+#if H5Eset_auto_vers == 2
+    H5Eset_auto( H5E_DEFAULT, NULL, NULL );
+#else
+    H5Eset_auto( NULL, NULL );
+#endif
+
+    herr_t status;
+    hid_t file_id;
+    file_id = H5Fopen(filename.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
+    if (file_id < 0) {
+        file_id = H5Fcreate(filename.c_str(), H5F_ACC_EXCL, H5P_DEFAULT, H5P_DEFAULT);
+    }
+    CHECK_ERROR(file_id,"Error creating hdf5 file.");
+
+    hsize_t     dimsf[2];              // dataset dimensions
+    dimsf[0] = dataset.rows;
+    dimsf[1] = dataset.cols;
+
+    hid_t space_id = H5Screate_simple(2, dimsf, NULL);
+    hid_t memspace_id = H5Screate_simple(2, dimsf, NULL);
+
+    hid_t dataset_id;
+#if H5Dcreate_vers == 2
+    dataset_id = H5Dcreate2(file_id, name.c_str(), get_hdf5_type<T>(), space_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
+#else
+    dataset_id = H5Dcreate(file_id, name.c_str(), get_hdf5_type<T>(), space_id, H5P_DEFAULT);
+#endif
+
+    if (dataset_id<0) {
+#if H5Dopen_vers == 2
+        dataset_id = H5Dopen2(file_id, name.c_str(), H5P_DEFAULT);
+#else
+        dataset_id = H5Dopen(file_id, name.c_str());
+#endif
+    }
+    CHECK_ERROR(dataset_id,"Error creating or opening dataset in file.");
+
+    status = H5Dwrite(dataset_id, get_hdf5_type<T>(), memspace_id, space_id, H5P_DEFAULT, dataset.ptr() );
+    CHECK_ERROR(status, "Error writing to dataset");
+
+    H5Sclose(memspace_id);
+    H5Sclose(space_id);
+    H5Dclose(dataset_id);
+    H5Fclose(file_id);
+
+}
+
+
+template<typename T>
+void load_from_file(flann::Matrix<T>& dataset, const std::string& filename, const std::string& name)
+{
+    herr_t status;
+    hid_t file_id = H5Fopen(filename.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
+    CHECK_ERROR(file_id,"Error opening hdf5 file.");
+
+    hid_t dataset_id;
+#if H5Dopen_vers == 2
+    dataset_id = H5Dopen2(file_id, name.c_str(), H5P_DEFAULT);
+#else
+    dataset_id = H5Dopen(file_id, name.c_str());
+#endif
+    CHECK_ERROR(dataset_id,"Error opening dataset in file.");
+
+    hid_t space_id = H5Dget_space(dataset_id);
+
+    hsize_t dims_out[2];
+    H5Sget_simple_extent_dims(space_id, dims_out, NULL);
+
+    dataset = flann::Matrix<T>(new T[dims_out[0]*dims_out[1]], dims_out[0], dims_out[1]);
+
+    status = H5Dread(dataset_id, get_hdf5_type<T>(), H5S_ALL, H5S_ALL, H5P_DEFAULT, dataset[0]);
+    CHECK_ERROR(status, "Error reading dataset");
+
+    H5Sclose(space_id);
+    H5Dclose(dataset_id);
+    H5Fclose(file_id);
+}
+
+
+#ifdef HAVE_MPI
+
+namespace mpi
+{
+/**
+ * Loads a the hyperslice corresponding to this processor from a hdf5 file.
+ * @param flann_dataset Dataset where the data is loaded
+ * @param filename HDF5 file name
+ * @param name Name of dataset inside file
+ */
+template<typename T>
+void load_from_file(flann::Matrix<T>& dataset, const std::string& filename, const std::string& name)
+{
+    MPI_Comm comm  = MPI_COMM_WORLD;
+    MPI_Info info  = MPI_INFO_NULL;
+
+    int mpi_size, mpi_rank;
+    MPI_Comm_size(comm, &mpi_size);
+    MPI_Comm_rank(comm, &mpi_rank);
+
+    herr_t status;
+
+    hid_t plist_id = H5Pcreate(H5P_FILE_ACCESS);
+    H5Pset_fapl_mpio(plist_id, comm, info);
+    hid_t file_id = H5Fopen(filename.c_str(), H5F_ACC_RDWR, plist_id);
+    CHECK_ERROR(file_id,"Error opening hdf5 file.");
+    H5Pclose(plist_id);
+    hid_t dataset_id;
+#if H5Dopen_vers == 2
+    dataset_id = H5Dopen2(file_id, name.c_str(), H5P_DEFAULT);
+#else
+    dataset_id = H5Dopen(file_id, name.c_str());
+#endif
+    CHECK_ERROR(dataset_id,"Error opening dataset in file.");
+
+    hid_t space_id = H5Dget_space(dataset_id);
+    hsize_t dims[2];
+    H5Sget_simple_extent_dims(space_id, dims, NULL);
+
+    hsize_t count[2];
+    hsize_t offset[2];
+
+    hsize_t item_cnt = dims[0]/mpi_size+(dims[0]%mpi_size==0 ? 0 : 1);
+    hsize_t cnt = (mpi_rank<mpi_size-1 ? item_cnt : dims[0]-item_cnt*(mpi_size-1));
+
+    count[0] = cnt;
+    count[1] = dims[1];
+    offset[0] = mpi_rank*item_cnt;
+    offset[1] = 0;
+
+    hid_t memspace_id = H5Screate_simple(2,count,NULL);
+
+    H5Sselect_hyperslab(space_id, H5S_SELECT_SET, offset, NULL, count, NULL);
+
+    dataset = flann::Matrix<T>(new T[count[0]*count[1]], count[0], count[1]);
+
+    plist_id = H5Pcreate(H5P_DATASET_XFER);
+//    H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_COLLECTIVE);
+    status = H5Dread(dataset_id, get_hdf5_type<T>(), memspace_id, space_id, plist_id, dataset[0]);
+    CHECK_ERROR(status, "Error reading dataset");
+
+    H5Pclose(plist_id);
+    H5Sclose(space_id);
+    H5Sclose(memspace_id);
+    H5Dclose(dataset_id);
+    H5Fclose(file_id);
+}
+}
+#endif // HAVE_MPI
+} // namespace flann::mpi
+
+#endif /* FLANN_HDF5_H_ */
diff --git a/src/cpp/flann/mpi/client.h b/src/cpp/flann/mpi/client.h
new file mode 100644
index 0000000..19a410c
--- /dev/null
+++ b/src/cpp/flann/mpi/client.h
@@ -0,0 +1,89 @@
+/***********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2008-2011  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+ * Copyright 2008-2011  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+
+
+#ifndef MPI_CLIENT_H_
+#define MPI_CLIENT_H_
+
+#include <cstdlib>
+#include <boost/asio.hpp>
+#include <flann/util/matrix.h>
+#include <flann/util/params.h>
+#include "queries.h"
+
+namespace flann {
+namespace mpi {
+
+
+class Client
+{
+public:
+	Client(const std::string& host, const std::string& service)
+	{
+	    tcp::resolver resolver(io_service_);
+	    tcp::resolver::query query(tcp::v4(), host, service);
+	    iterator_ = resolver.resolve(query);
+	}
+
+
+	template<typename ElementType, typename DistanceType>
+	void knnSearch(const flann::Matrix<ElementType>& queries, flann::Matrix<int>& indices, flann::Matrix<DistanceType>& dists, int knn, const SearchParams& params)
+	{
+	    tcp::socket sock(io_service_);
+	    sock.connect(*iterator_);
+
+	    Request<ElementType> req;
+	    req.nn = knn;
+	    req.queries = queries;
+	    req.checks = params.checks;
+	    // send request
+	    write_object(sock,req);
+
+	    Response<DistanceType> resp;
+	    // read response
+	    read_object(sock, resp);
+
+	    for (size_t i=0;i<indices.rows;++i) {
+	    	for (size_t j=0;j<indices.cols;++j) {
+	    		indices[i][j] = resp.indices[i][j];
+	    		dists[i][j] = resp.dists[i][j];
+	    	}
+	    }
+	}
+
+
+private:
+	boost::asio::io_service io_service_;
+	tcp::resolver::iterator iterator_;
+};
+
+
+} //namespace mpi
+} // namespace flann
+
+#endif // MPI_CLIENT_H_
diff --git a/src/cpp/flann/mpi/flann_mpi_client.cpp b/src/cpp/flann/mpi/flann_mpi_client.cpp
new file mode 100644
index 0000000..1ac0fbd
--- /dev/null
+++ b/src/cpp/flann/mpi/flann_mpi_client.cpp
@@ -0,0 +1,85 @@
+#include <stdio.h>
+#include <time.h>
+
+#include <cstdlib>
+#include <iostream>
+#include <flann/util/params.h>
+#include <flann/io/hdf5.h>
+#include <flann/mpi/client.h>
+
+
+
+#define IF_RANK0 if (world.rank()==0)
+
+timeval start_time_;
+void start_timer(const std::string& message = "")
+{
+	if (!message.empty()) {
+		printf("%s", message.c_str());
+		fflush(stdout);
+	}
+    gettimeofday(&start_time_,NULL);
+}
+
+double stop_timer()
+{
+    timeval end_time;
+    gettimeofday(&end_time,NULL);
+
+	return double(end_time.tv_sec-start_time_.tv_sec)+ double(end_time.tv_usec-start_time_.tv_usec)/1000000;
+}
+
+float compute_precision(const flann::Matrix<int>& match, const flann::Matrix<int>& indices)
+{
+	int count = 0;
+
+	assert(match.rows == indices.rows);
+	size_t nn = std::min(match.cols, indices.cols);
+
+	for(size_t i=0; i<match.rows; ++i) {
+		for (size_t j=0;j<nn;++j) {
+			for (size_t k=0;k<nn;++k) {
+				if (match[i][j]==indices[i][k]) {
+					count ++;
+				}
+			}
+		}
+	}
+
+	return float(count)/(nn*match.rows);
+}
+
+
+int main(int argc, char* argv[])
+{
+	try {
+
+		flann::Matrix<float> query;
+		flann::Matrix<int> match;
+
+		flann::load_from_file(query, "sift100K.h5","query");
+		flann::load_from_file(match, "sift100K.h5","match");
+		//	flann::load_from_file(gt_dists, "sift100K.h5","dists");
+
+		flann::mpi::Client index("localhost","9999");
+
+		int nn = 1;
+		flann::Matrix<int> indices(new int[query.rows*nn], query.rows, nn);
+		flann::Matrix<float> dists(new float[query.rows*nn], query.rows, nn);
+
+		start_timer("Performing search...\n");
+		index.knnSearch(query, indices, dists, nn, flann::SearchParams(64));
+		printf("Search done (%g seconds)\n", stop_timer());
+
+		printf("Checking results\n");
+		float precision = compute_precision(match, indices);
+		printf("Precision is: %g\n", precision);
+
+	}
+	catch (std::exception& e) {
+		std::cerr << "Exception: " << e.what() << "\n";
+	}
+
+	return 0;
+}
+
diff --git a/src/cpp/flann/mpi/flann_mpi_server.cpp b/src/cpp/flann/mpi/flann_mpi_server.cpp
new file mode 100644
index 0000000..8fbe899
--- /dev/null
+++ b/src/cpp/flann/mpi/flann_mpi_server.cpp
@@ -0,0 +1,26 @@
+#include <boost/mpi.hpp>
+#include <flann/mpi/server.h>
+#include <stdio.h>
+#include <time.h>
+
+int main(int argc, char* argv[])
+{
+	boost::mpi::environment env(argc, argv);
+
+	try {
+		if (argc != 4) {
+			std::cout << "Usage: " << argv[0] << " <file> <dataset> <port>\n";
+			return 1;
+		}
+		flann::mpi::Server<flann::L2<float> > server(argv[1], argv[2], std::atoi(argv[3]),
+				flann::KDTreeIndexParams(4));
+
+		server.run();
+	}
+	catch (std::exception& e) {
+		std::cerr << "Exception: " << e.what() << "\n";
+	}
+
+	return 0;
+}
+
diff --git a/src/cpp/flann/mpi/index.h b/src/cpp/flann/mpi/index.h
new file mode 100644
index 0000000..19116a4
--- /dev/null
+++ b/src/cpp/flann/mpi/index.h
@@ -0,0 +1,271 @@
+/***********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2008-2010  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+ * Copyright 2008-2010  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+
+
+#ifndef FLANN_MPI_HPP_
+#define FLANN_MPI_HPP_
+
+#include <boost/mpi.hpp>
+#include <boost/serialization/array.hpp>
+#include <flann/flann.hpp>
+#include <flann/io/hdf5.h>
+
+namespace flann
+{
+namespace mpi
+{
+
+template<typename DistanceType>
+struct SearchResults
+{
+    flann::Matrix<int> indices;
+    flann::Matrix<DistanceType> dists;
+
+    template<typename Archive>
+    void serialize(Archive& ar, const unsigned int version)
+    {
+        ar& indices.rows;
+        ar& indices.cols;
+        if (Archive::is_loading::value) {
+            indices = Matrix<int>(new int[indices.rows*indices.cols], indices.rows, indices.cols);
+        }
+        ar& boost::serialization::make_array(indices.ptr(), indices.rows*indices.cols);
+        if (Archive::is_saving::value) {
+            delete[] indices.ptr();
+        }
+        ar& dists.rows;
+        ar& dists.cols;
+        if (Archive::is_loading::value) {
+            dists = Matrix<DistanceType>(new DistanceType[dists.rows*dists.cols], dists.rows, dists.cols);
+        }
+        ar& boost::serialization::make_array(dists.ptr(), dists.rows*dists.cols);
+        if (Archive::is_saving::value) {
+            delete[] dists.ptr();
+        }
+    }
+};
+
+template<typename DistanceType>
+struct ResultsMerger
+{
+    SearchResults<DistanceType> operator()(SearchResults<DistanceType> a, SearchResults<DistanceType> b)
+    {
+        SearchResults<DistanceType> results;
+        results.indices = flann::Matrix<int>(new int[a.indices.rows*a.indices.cols],a.indices.rows,a.indices.cols);
+        results.dists = flann::Matrix<DistanceType>(new DistanceType[a.dists.rows*a.dists.cols],a.dists.rows,a.dists.cols);
+
+
+        for (size_t i = 0; i < results.dists.rows; ++i) {
+            size_t idx = 0;
+            size_t a_idx = 0;
+            size_t b_idx = 0;
+            while (idx < results.dists.cols) {
+                if (a.dists[i][a_idx] <= b.dists[i][b_idx]) {
+                    results.dists[i][idx] = a.dists[i][a_idx];
+                    results.indices[i][idx] = a.indices[i][a_idx];
+                    idx++;
+                    a_idx++;
+                }
+                else {
+                    results.dists[i][idx] = b.dists[i][b_idx];
+                    results.indices[i][idx] = b.indices[i][b_idx];
+                    idx++;
+                    b_idx++;
+                }
+            }
+        }
+        delete[] a.indices.ptr();
+        delete[] a.dists.ptr();
+        delete[] b.indices.ptr();
+        delete[] b.dists.ptr();
+        return results;
+    }
+};
+
+
+
+template<typename Distance>
+class Index
+{
+    typedef typename Distance::ElementType ElementType;
+    typedef typename Distance::ResultType DistanceType;
+
+    flann::Index<Distance>* flann_index;
+    flann::Matrix<ElementType> dataset;
+    int size_;
+    int offset_;
+
+public:
+    Index(const std::string& file_name,
+          const std::string& dataset_name,
+          const IndexParams& params);
+
+    ~Index();
+
+    void buildIndex()
+    {
+        flann_index->buildIndex();
+    }
+
+    void knnSearch(const flann::Matrix<ElementType>& queries,
+                   flann::Matrix<int>& indices,
+                   flann::Matrix<DistanceType>& dists,
+                   int knn, const
+                   SearchParams& params);
+
+    int radiusSearch(const flann::Matrix<ElementType>& query,
+                     flann::Matrix<int>& indices,
+                     flann::Matrix<DistanceType>& dists,
+                     float radius,
+                     const SearchParams& params);
+
+    // void save(std::string filename);
+
+    int veclen() const
+    {
+        return flann_index->veclen();
+    }
+
+    int size() const
+    {
+        return size_;
+    }
+
+    IndexParams getIndexParameters()
+    {
+        return flann_index->getParameters();
+    }
+};
+
+
+template<typename Distance>
+Index<Distance>::Index(const std::string& file_name, const std::string& dataset_name, const IndexParams& params)
+{
+    boost::mpi::communicator world;
+    flann_algorithm_t index_type = get_param<flann_algorithm_t>(params,"algorithm");
+    if (index_type == FLANN_INDEX_SAVED) {
+        throw FLANNException("Saving/loading of MPI indexes is not currently supported.");
+    }
+    flann::mpi::load_from_file(dataset, file_name, dataset_name);
+    flann_index = new flann::Index<Distance>(dataset, params);
+
+    std::vector<int> sizes;
+    // get the sizes of all MPI indices
+    all_gather(world, (int)flann_index->size(), sizes);
+    size_ = 0;
+    offset_ = 0;
+    for (size_t i = 0; i < sizes.size(); ++i) {
+        if ((int)i < world.rank()) offset_ += sizes[i];
+        size_ += sizes[i];
+    }
+}
+
+template<typename Distance>
+Index<Distance>::~Index()
+{
+    delete flann_index;
+    delete[] dataset.ptr();
+}
+
+template<typename Distance>
+void Index<Distance>::knnSearch(const flann::Matrix<ElementType>& queries, flann::Matrix<int>& indices, flann::Matrix<DistanceType>& dists, int knn, const SearchParams& params)
+{
+    boost::mpi::communicator world;
+    flann::Matrix<int> local_indices(new int[queries.rows*knn], queries.rows, knn);
+    flann::Matrix<DistanceType> local_dists(new DistanceType[queries.rows*knn], queries.rows, knn);
+
+    flann_index->knnSearch(queries, local_indices, local_dists, knn, params);
+    for (size_t i = 0; i < local_indices.rows; ++i) {
+        for (size_t j = 0; j < local_indices.cols; ++j) {
+            local_indices[i][j] += offset_;
+        }
+    }
+    SearchResults<DistanceType> local_results;
+    local_results.indices = local_indices;
+    local_results.dists = local_dists;
+    SearchResults<DistanceType> results;
+
+    // perform MPI reduce
+    reduce(world, local_results, results, ResultsMerger<DistanceType>(), 0);
+
+    if (world.rank() == 0) {
+        for (size_t i = 0; i < results.indices.rows; ++i) {
+            for (size_t j = 0; j < results.indices.cols; ++j) {
+                indices[i][j] = results.indices[i][j];
+                dists[i][j] = results.dists[i][j];
+            }
+        }
+        delete[] results.indices.ptr();
+        delete[] results.dists.ptr();
+    }
+}
+
+template<typename Distance>
+int Index<Distance>::radiusSearch(const flann::Matrix<ElementType>& query, flann::Matrix<int>& indices, flann::Matrix<DistanceType>& dists, float radius, const SearchParams& params)
+{
+    boost::mpi::communicator world;
+    flann::Matrix<int> local_indices(new int[indices.rows*indices.cols], indices.rows, indices.cols);
+    flann::Matrix<DistanceType> local_dists(new DistanceType[dists.rows*dists.cols], dists.rows, dists.cols);
+
+    flann_index->radiusSearch(query, local_indices, local_dists, radius, params);
+    for (size_t i = 0; i < local_indices.rows; ++i) {
+        for (size_t j = 0; j < local_indices.cols; ++j) {
+            local_indices[i][j] += offset_;
+        }
+    }
+    SearchResults<DistanceType> local_results;
+    local_results.indices = local_indices;
+    local_results.dists = local_dists;
+    SearchResults<DistanceType> results;
+
+    // perform MPI reduce
+    reduce(world, local_results, results, ResultsMerger<DistanceType>(), 0);
+
+    if (world.rank() == 0) {
+        for (int i = 0; i < std::min(results.indices.rows, indices.rows); ++i) {
+            for (int j = 0; j < std::min(results.indices.cols, indices.cols); ++j) {
+                indices[i][j] = results.indices[i][j];
+                dists[i][j] = results.dists[i][j];
+            }
+        }
+        delete[] results.indices.ptr();
+        delete[] results.dists.ptr();
+    }
+    return 0;
+}
+
+}
+} //namespace flann::mpi
+
+namespace boost { namespace mpi {
+template<typename DistanceType>
+struct is_commutative<flann::mpi::ResultsMerger<DistanceType>, flann::mpi::SearchResults<DistanceType> > : mpl::true_ { };
+} } // end namespace boost::mpi
+
+
+#endif /* FLANN_MPI_HPP_ */
diff --git a/src/cpp/flann/mpi/matrix.h b/src/cpp/flann/mpi/matrix.h
new file mode 100644
index 0000000..ef4f7aa
--- /dev/null
+++ b/src/cpp/flann/mpi/matrix.h
@@ -0,0 +1,54 @@
+/***********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2008-2011  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+ * Copyright 2008-2011  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+
+
+#ifndef MPI_MATRIX_H_
+#define MPI_MATRIX_H_
+
+#include <flann/util/matrix.h>
+#include <boost/serialization/array.hpp>
+
+
+namespace boost {
+namespace serialization {
+
+template<class Archive, class T>
+void serialize(Archive & ar, flann::Matrix<T> & matrix, const unsigned int version)
+{
+	ar & matrix.rows & matrix.cols & matrix.stride;
+    if (Archive::is_loading::value) {
+        matrix = flann::Matrix<T>(new T[matrix.rows*matrix.cols], matrix.rows, matrix.cols, matrix.stride);
+    }
+    ar & boost::serialization::make_array(matrix.ptr(), matrix.rows*matrix.cols);
+}
+
+}
+}
+
+
+#endif /* MPI_MATRIX_H_ */
diff --git a/src/cpp/flann/mpi/queries.h b/src/cpp/flann/mpi/queries.h
new file mode 100644
index 0000000..c5f094d
--- /dev/null
+++ b/src/cpp/flann/mpi/queries.h
@@ -0,0 +1,103 @@
+/***********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2008-2011  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+ * Copyright 2008-2011  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+
+
+#ifndef MPI_QUERIES_H_
+#define MPI_QUERIES_H_
+
+#include <flann/mpi/matrix.h>
+#include <boost/archive/binary_iarchive.hpp>
+#include <boost/archive/binary_oarchive.hpp>
+#include <boost/asio.hpp>
+
+namespace flann
+{
+
+template<typename T>
+struct Request
+{
+	flann::Matrix<T> queries;
+	int nn;
+	int checks;
+
+	template<typename Archive>
+	void serialize(Archive& ar, const unsigned int version)
+	{
+		ar & queries & nn & checks;
+	}
+};
+
+template<typename T>
+struct Response
+{
+	flann::Matrix<int> indices;
+	flann::Matrix<T> dists;
+
+	template<typename Archive>
+	void serialize(Archive& ar, const unsigned int version)
+	{
+		ar & indices & dists;
+	}
+};
+
+
+using boost::asio::ip::tcp;
+
+template <typename T>
+void read_object(tcp::socket& sock, T& val)
+{
+	uint32_t size;
+	boost::asio::read(sock, boost::asio::buffer(&size, sizeof(size)));
+	size = ntohl(size);
+
+	boost::asio::streambuf archive_stream;
+	boost::asio::read(sock, archive_stream, boost::asio::transfer_at_least(size));
+
+	boost::archive::binary_iarchive archive(archive_stream);
+	archive >> val;
+}
+
+template <typename T>
+void write_object(tcp::socket& sock, const T& val)
+{
+	boost::asio::streambuf archive_stream;
+	boost::archive::binary_oarchive archive(archive_stream);
+	archive << val;
+
+	uint32_t size = archive_stream.size();
+	size = htonl(size);
+	boost::asio::write(sock, boost::asio::buffer(&size, sizeof(size)));
+	boost::asio::write(sock, archive_stream);
+
+}
+
+}
+
+
+
+#endif /* MPI_QUERIES_H_ */
diff --git a/src/cpp/flann/mpi/server.h b/src/cpp/flann/mpi/server.h
new file mode 100644
index 0000000..2caab43
--- /dev/null
+++ b/src/cpp/flann/mpi/server.h
@@ -0,0 +1,153 @@
+/***********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2008-2011  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+ * Copyright 2008-2011  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+
+
+#ifndef MPI_SERVER_H_
+#define MPI_SERVER_H_
+
+#include <flann/mpi/index.h>
+#include <stdio.h>
+#include <time.h>
+
+#include <cstdlib>
+#include <iostream>
+#include <boost/bind.hpp>
+#include <boost/shared_ptr.hpp>
+#include <boost/asio.hpp>
+#include <boost/thread/thread.hpp>
+
+#include "queries.h"
+
+namespace flann {
+
+namespace mpi {
+
+template<typename Distance>
+class Server
+{
+
+	typedef typename Distance::ElementType ElementType;
+	typedef typename Distance::ResultType DistanceType;
+	typedef boost::shared_ptr<tcp::socket> socket_ptr;
+	typedef flann::mpi::Index<Distance> FlannIndex;
+
+	void session(socket_ptr sock)
+	{
+		boost::mpi::communicator world;
+		try {
+			Request<ElementType> req;
+			if (world.rank()==0) {
+				read_object(*sock,req);
+				std::cout << "Received query\n";
+			}
+			// broadcast request to all MPI processes
+			boost::mpi::broadcast(world, req, 0);
+
+			Response<DistanceType> resp;
+			if (world.rank()==0) {
+				int rows = req.queries.rows;
+				int cols = req.nn;
+				resp.indices = flann::Matrix<int>(new int[rows*cols], rows, cols);
+				resp.dists = flann::Matrix<DistanceType>(new DistanceType[rows*cols], rows, cols);
+			}
+
+			std::cout << "Searching in process " << world.rank() << "\n";
+			index_->knnSearch(req.queries, resp.indices, resp.dists, req.nn, flann::SearchParams(req.checks));
+
+			if (world.rank()==0) {
+				std::cout << "Sending result\n";
+				write_object(*sock,resp);
+			}
+
+			delete[] req.queries.ptr();
+			if (world.rank()==0) {
+				delete[] resp.indices.ptr();
+				delete[] resp.dists.ptr();
+			}
+
+		}
+		catch (std::exception& e) {
+			std::cerr << "Exception in thread: " << e.what() << "\n";
+		}
+	}
+
+
+
+public:
+	Server(const std::string& filename, const std::string& dataset, short port, const IndexParams& params) :
+		port_(port)
+	{
+		boost::mpi::communicator world;
+		if (world.rank()==0) {
+			std::cout << "Reading dataset and building index...";
+			std::flush(std::cout);
+		}
+		index_ = new FlannIndex(filename, dataset, params);
+		index_->buildIndex();
+		world.barrier(); // wait for data to be loaded and indexes to be created
+		if (world.rank()==0) {
+			std::cout << "done.\n";
+		}
+	}
+
+
+	void run()
+	{
+		boost::mpi::communicator world;
+		boost::shared_ptr<boost::asio::io_service> io_service;
+		boost::shared_ptr<tcp::acceptor> acceptor;
+
+		if (world.rank()==0) {
+			io_service.reset(new boost::asio::io_service());
+			acceptor.reset(new tcp::acceptor(*io_service, tcp::endpoint(tcp::v4(), port_)));
+			std::cout << "Start listening for queries...\n";
+		}
+		for (;;) {
+			socket_ptr sock;
+			if (world.rank()==0) {
+				sock.reset(new tcp::socket(*io_service));
+				acceptor->accept(*sock);
+				std::cout << "Accepted connection\n";
+			}
+			world.barrier(); // everybody waits here for a connection
+			boost::thread t(boost::bind(&Server::session, this, sock));
+			t.join();
+		}
+
+	}
+
+private:
+	FlannIndex* index_;
+	short port_;
+};
+
+
+} // namespace mpi
+} // namespace flann
+
+#endif // MPI_SERVER_H_
diff --git a/src/cpp/flann/nn/ground_truth.h b/src/cpp/flann/nn/ground_truth.h
new file mode 100644
index 0000000..94121e8
--- /dev/null
+++ b/src/cpp/flann/nn/ground_truth.h
@@ -0,0 +1,98 @@
+/***********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2008-2009  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+ * Copyright 2008-2009  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+ *
+ * THE BSD LICENSE
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+
+#ifndef FLANN_GROUND_TRUTH_H_
+#define FLANN_GROUND_TRUTH_H_
+
+#include "flann/algorithms/dist.h"
+#include "flann/util/matrix.h"
+
+
+namespace flann
+{
+
+template <typename Distance>
+void find_nearest(const Matrix<typename Distance::ElementType>& dataset, typename Distance::ElementType* query, size_t* matches, size_t nn,
+                  size_t skip = 0, Distance distance = Distance())
+{
+    typedef typename Distance::ElementType ElementType;
+    typedef typename Distance::ResultType DistanceType;
+    int n = nn + skip;
+
+    int* match = new int[n];
+    DistanceType* dists = new DistanceType[n];
+
+    dists[0] = distance(dataset[0], query, dataset.cols);
+    match[0] = 0;
+    int dcnt = 1;
+
+    for (size_t i=1; i<dataset.rows; ++i) {
+        DistanceType tmp = distance(dataset[i], query, dataset.cols);
+
+        if (dcnt<n) {
+            match[dcnt] = i;
+            dists[dcnt++] = tmp;
+        }
+        else if (tmp < dists[dcnt-1]) {
+            dists[dcnt-1] = tmp;
+            match[dcnt-1] = i;
+        }
+
+        int j = dcnt-1;
+        // bubble up
+        while (j>=1 && dists[j]<dists[j-1]) {
+            std::swap(dists[j],dists[j-1]);
+            std::swap(match[j],match[j-1]);
+            j--;
+        }
+    }
+
+    for (size_t i=0; i<nn; ++i) {
+        matches[i] = match[i+skip];
+    }
+
+    delete[] match;
+    delete[] dists;
+}
+
+
+template <typename Distance>
+void compute_ground_truth(const Matrix<typename Distance::ElementType>& dataset, const Matrix<typename Distance::ElementType>& testset, Matrix<size_t>& matches,
+                          int skip=0, Distance d = Distance())
+{
+    for (size_t i=0; i<testset.rows; ++i) {
+        find_nearest<Distance>(dataset, testset[i], matches[i], matches.cols, skip, d);
+    }
+}
+
+
+}
+
+#endif //FLANN_GROUND_TRUTH_H_
diff --git a/src/cpp/flann/nn/index_testing.h b/src/cpp/flann/nn/index_testing.h
new file mode 100644
index 0000000..990463c
--- /dev/null
+++ b/src/cpp/flann/nn/index_testing.h
@@ -0,0 +1,323 @@
+/***********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2008-2009  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+ * Copyright 2008-2009  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+ *
+ * THE BSD LICENSE
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+
+#ifndef FLANN_INDEX_TESTING_H_
+#define FLANN_INDEX_TESTING_H_
+
+#include <cstring>
+#include <cassert>
+#include <cmath>
+
+#include "flann/util/matrix.h"
+#include "flann/algorithms/nn_index.h"
+#include "flann/util/result_set.h"
+#include "flann/util/logger.h"
+#include "flann/util/timer.h"
+
+
+namespace flann
+{
+
+inline int countCorrectMatches(size_t* neighbors, size_t* groundTruth, int n)
+{
+    int count = 0;
+    for (int i=0; i<n; ++i) {
+        for (int k=0; k<n; ++k) {
+            if (neighbors[i]==groundTruth[k]) {
+                count++;
+                break;
+            }
+        }
+    }
+    return count;
+}
+
+
+template <typename Distance>
+typename Distance::ResultType computeDistanceRaport(const Matrix<typename Distance::ElementType>& inputData, typename Distance::ElementType* target,
+		size_t* neighbors, size_t* groundTruth, int veclen, int n, const Distance& distance)
+{
+    typedef typename Distance::ResultType DistanceType;
+
+    DistanceType ret = 0;
+    for (int i=0; i<n; ++i) {
+        DistanceType den = distance(inputData[groundTruth[i]], target, veclen);
+        DistanceType num = distance(inputData[neighbors[i]], target, veclen);
+
+        if ((den==0)&&(num==0)) {
+            ret += 1;
+        }
+        else {
+            ret += num/den;
+        }
+    }
+
+    return ret;
+}
+
+template <typename Index, typename Distance>
+float search_with_ground_truth(Index& index, const Matrix<typename Distance::ElementType>& inputData,
+                               const Matrix<typename Distance::ElementType>& testData, const Matrix<size_t>& matches, int nn, int checks,
+                               float& time, typename Distance::ResultType& dist, const Distance& distance, int skipMatches)
+{
+    typedef typename Distance::ElementType ElementType;
+    typedef typename Distance::ResultType DistanceType;
+
+    if (matches.cols<size_t(nn)) {
+        Logger::info("matches.cols=%d, nn=%d\n",matches.cols,nn);
+        throw FLANNException("Ground truth is not computed for as many neighbors as requested");
+    }
+
+    SearchParams searchParams(checks);
+
+    size_t* indices = new size_t[nn+skipMatches];
+    DistanceType* dists = new DistanceType[nn+skipMatches];
+    
+    Matrix<size_t> indices_mat(indices, 1, nn+skipMatches);
+    Matrix<DistanceType> dists_mat(dists, 1, nn+skipMatches);
+        
+    size_t* neighbors = indices + skipMatches;
+
+    int correct = 0;
+    DistanceType distR = 0;
+    StartStopTimer t;
+    int repeats = 0;
+    while (t.value<0.2) {
+        repeats++;
+        t.start();
+        correct = 0;
+        distR = 0;        
+        for (size_t i = 0; i < testData.rows; i++) {
+            index.knnSearch(Matrix<ElementType>(testData[i], 1, testData.cols), indices_mat, dists_mat, nn+skipMatches, searchParams);
+
+            correct += countCorrectMatches(neighbors,matches[i], nn);
+            distR += computeDistanceRaport<Distance>(inputData, testData[i], neighbors, matches[i], testData.cols, nn, distance);
+        }
+        t.stop();
+    }
+    time = float(t.value/repeats);
+
+    delete[] indices;
+    delete[] dists;
+
+    float precicion = (float)correct/(nn*testData.rows);
+
+    dist = distR/(testData.rows*nn);
+
+    Logger::info("%8d %10.4g %10.5g %10.5g %10.5g\n",
+                 checks, precicion, time, 1000.0 * time / testData.rows, dist);
+
+    return precicion;
+}
+
+
+template <typename Index, typename Distance>
+float test_index_checks(Index& index, const Matrix<typename Distance::ElementType>& inputData,
+                        const Matrix<typename Distance::ElementType>& testData, const Matrix<size_t>& matches,
+                        int checks, float& precision, const Distance& distance, int nn = 1, int skipMatches = 0)
+{
+    typedef typename Distance::ResultType DistanceType;
+
+    Logger::info("  Nodes  Precision(%)   Time(s)   Time/vec(ms)  Mean dist\n");
+    Logger::info("---------------------------------------------------------\n");
+
+    float time = 0;
+    DistanceType dist = 0;
+    precision = search_with_ground_truth(index, inputData, testData, matches, nn, checks, time, dist, distance, skipMatches);
+
+    return time;
+}
+
+template <typename Index, typename Distance>
+float test_index_precision(Index& index, const Matrix<typename Distance::ElementType>& inputData,
+                           const Matrix<typename Distance::ElementType>& testData, const Matrix<size_t>& matches,
+                           float precision, int& checks, const Distance& distance, int nn = 1, int skipMatches = 0)
+{
+    typedef typename Distance::ResultType DistanceType;
+    const float SEARCH_EPS = 0.001f;
+
+    Logger::info("  Nodes  Precision(%)   Time(s)   Time/vec(ms)  Mean dist\n");
+    Logger::info("---------------------------------------------------------\n");
+
+    int c2 = 1;
+    float p2;
+    int c1 = 1;
+//     float p1;
+    float time;
+    DistanceType dist;
+
+    p2 = search_with_ground_truth(index, inputData, testData, matches, nn, c2, time, dist, distance, skipMatches);
+
+    if (p2>precision) {
+        Logger::info("Got as close as I can\n");
+        checks = c2;
+        return time;
+    }
+
+    while (p2<precision) {
+        c1 = c2;
+//         p1 = p2;
+        c2 *=2;
+        p2 = search_with_ground_truth(index, inputData, testData, matches, nn, c2, time, dist, distance, skipMatches);
+    }
+
+    int cx;
+    float realPrecision;
+    if (fabs(p2-precision)>SEARCH_EPS) {
+        Logger::info("Start linear estimation\n");
+        // after we got to values in the vecinity of the desired precision
+        // use linear approximation get a better estimation
+
+        cx = (c1+c2)/2;
+        realPrecision = search_with_ground_truth(index, inputData, testData, matches, nn, cx, time, dist, distance, skipMatches);
+        while (fabs(realPrecision-precision)>SEARCH_EPS) {
+
+            if (realPrecision<precision) {
+                c1 = cx;
+            }
+            else {
+                c2 = cx;
+            }
+            cx = (c1+c2)/2;
+            if (cx==c1) {
+                Logger::info("Got as close as I can\n");
+                break;
+            }
+            realPrecision = search_with_ground_truth(index, inputData, testData, matches, nn, cx, time, dist, distance, skipMatches);
+        }
+
+        c2 = cx;
+        p2 = realPrecision;
+
+    }
+    else {
+        Logger::info("No need for linear estimation\n");
+        cx = c2;
+        realPrecision = p2;
+    }
+
+    checks = cx;
+    return time;
+}
+
+
+template <typename Index, typename Distance>
+void test_index_precisions(Index& index, const Matrix<typename Distance::ElementType>& inputData,
+                           const Matrix<typename Distance::ElementType>& testData, const Matrix<int>& matches,
+                           float* precisions, int precisions_length, const Distance& distance, int nn = 1, int skipMatches = 0, float maxTime = 0)
+{
+    typedef typename Distance::ResultType DistanceType;
+
+    const float SEARCH_EPS = 0.001;
+
+    // make sure precisions array is sorted
+    std::sort(precisions, precisions+precisions_length);
+
+    int pindex = 0;
+    float precision = precisions[pindex];
+
+    Logger::info("  Nodes  Precision(%)   Time(s)   Time/vec(ms)  Mean dist\n");
+    Logger::info("---------------------------------------------------------\n");
+
+    int c2 = 1;
+    float p2;
+
+    int c1 = 1;
+    float p1;
+
+    float time;
+    DistanceType dist;
+
+    p2 = search_with_ground_truth(index, inputData, testData, matches, nn, c2, time, dist, distance, skipMatches);
+
+    // if precision for 1 run down the tree is already
+    // better then some of the requested precisions, then
+    // skip those
+    while (precisions[pindex]<p2 && pindex<precisions_length) {
+        pindex++;
+    }
+
+    if (pindex==precisions_length) {
+        Logger::info("Got as close as I can\n");
+        return;
+    }
+
+    for (int i=pindex; i<precisions_length; ++i) {
+
+        precision = precisions[i];
+        while (p2<precision) {
+            c1 = c2;
+            p1 = p2;
+            c2 *=2;
+            p2 = search_with_ground_truth(index, inputData, testData, matches, nn, c2, time, dist, distance, skipMatches);
+            if ((maxTime> 0)&&(time > maxTime)&&(p2<precision)) return;
+        }
+
+        int cx;
+        float realPrecision;
+        if (fabs(p2-precision)>SEARCH_EPS) {
+            Logger::info("Start linear estimation\n");
+            // after we got to values in the vecinity of the desired precision
+            // use linear approximation get a better estimation
+
+            cx = (c1+c2)/2;
+            realPrecision = search_with_ground_truth(index, inputData, testData, matches, nn, cx, time, dist, distance, skipMatches);
+            while (fabs(realPrecision-precision)>SEARCH_EPS) {
+
+                if (realPrecision<precision) {
+                    c1 = cx;
+                }
+                else {
+                    c2 = cx;
+                }
+                cx = (c1+c2)/2;
+                if (cx==c1) {
+                    Logger::info("Got as close as I can\n");
+                    break;
+                }
+                realPrecision = search_with_ground_truth(index, inputData, testData, matches, nn, cx, time, dist, distance, skipMatches);
+            }
+
+            c2 = cx;
+            p2 = realPrecision;
+
+        }
+        else {
+            Logger::info("No need for linear estimation\n");
+            cx = c2;
+            realPrecision = p2;
+        }
+
+    }
+}
+
+}
+
+#endif //FLANN_INDEX_TESTING_H_
diff --git a/src/cpp/flann/nn/simplex_downhill.h b/src/cpp/flann/nn/simplex_downhill.h
new file mode 100644
index 0000000..45d7ada
--- /dev/null
+++ b/src/cpp/flann/nn/simplex_downhill.h
@@ -0,0 +1,186 @@
+/***********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2008-2009  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+ * Copyright 2008-2009  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+ *
+ * THE BSD LICENSE
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+
+#ifndef FLANN_SIMPLEX_DOWNHILL_H_
+#define FLANN_SIMPLEX_DOWNHILL_H_
+
+namespace flann
+{
+
+/**
+    Adds val to array vals (and point to array points) and keeping the arrays sorted by vals.
+ */
+template <typename T>
+void addValue(int pos, float val, float* vals, T* point, T* points, int n)
+{
+    vals[pos] = val;
+    for (int i=0; i<n; ++i) {
+        points[pos*n+i] = point[i];
+    }
+
+    // bubble down
+    int j=pos;
+    while (j>0 && vals[j]<vals[j-1]) {
+        swap(vals[j],vals[j-1]);
+        for (int i=0; i<n; ++i) {
+            swap(points[j*n+i],points[(j-1)*n+i]);
+        }
+        --j;
+    }
+}
+
+
+/**
+    Simplex downhill optimization function.
+    Preconditions: points is a 2D mattrix of size (n+1) x n
+                    func is the cost function taking n an array of n params and returning float
+                    vals is the cost function in the n+1 simplex points, if NULL it will be computed
+
+    Postcondition: returns optimum value and points[0..n] are the optimum parameters
+ */
+template <typename T, typename F>
+float optimizeSimplexDownhill(T* points, int n, F func, float* vals = NULL )
+{
+    const int MAX_ITERATIONS = 10;
+
+    assert(n>0);
+
+    T* p_o = new T[n];
+    T* p_r = new T[n];
+    T* p_e = new T[n];
+
+    int alpha = 1;
+
+    int iterations = 0;
+
+    bool ownVals = false;
+    if (vals == NULL) {
+        ownVals = true;
+        vals = new float[n+1];
+        for (int i=0; i<n+1; ++i) {
+            float val = func(points+i*n);
+            addValue(i, val, vals, points+i*n, points, n);
+        }
+    }
+    int nn = n*n;
+
+    while (true) {
+
+        if (iterations++ > MAX_ITERATIONS) break;
+
+        // compute average of simplex points (except the highest point)
+        for (int j=0; j<n; ++j) {
+            p_o[j] = 0;
+            for (int i=0; i<n; ++i) {
+                p_o[i] += points[j*n+i];
+            }
+        }
+        for (int i=0; i<n; ++i) {
+            p_o[i] /= n;
+        }
+
+        bool converged = true;
+        for (int i=0; i<n; ++i) {
+            if (p_o[i] != points[nn+i]) {
+                converged = false;
+            }
+        }
+        if (converged) break;
+
+        // trying a reflection
+        for (int i=0; i<n; ++i) {
+            p_r[i] = p_o[i] + alpha*(p_o[i]-points[nn+i]);
+        }
+        float val_r = func(p_r);
+
+        if ((val_r>=vals[0])&&(val_r<vals[n])) {
+            // reflection between second highest and lowest
+            // add it to the simplex
+            Logger::info("Choosing reflection\n");
+            addValue(n, val_r,vals, p_r, points, n);
+            continue;
+        }
+
+        if (val_r<vals[0]) {
+            // value is smaller than smalest in simplex
+
+            // expand some more to see if it drops further
+            for (int i=0; i<n; ++i) {
+                p_e[i] = 2*p_r[i]-p_o[i];
+            }
+            float val_e = func(p_e);
+
+            if (val_e<val_r) {
+                Logger::info("Choosing reflection and expansion\n");
+                addValue(n, val_e,vals,p_e,points,n);
+            }
+            else {
+                Logger::info("Choosing reflection\n");
+                addValue(n, val_r,vals,p_r,points,n);
+            }
+            continue;
+        }
+        if (val_r>=vals[n]) {
+            for (int i=0; i<n; ++i) {
+                p_e[i] = (p_o[i]+points[nn+i])/2;
+            }
+            float val_e = func(p_e);
+
+            if (val_e<vals[n]) {
+                Logger::info("Choosing contraction\n");
+                addValue(n,val_e,vals,p_e,points,n);
+                continue;
+            }
+        }
+        {
+            Logger::info("Full contraction\n");
+            for (int j=1; j<=n; ++j) {
+                for (int i=0; i<n; ++i) {
+                    points[j*n+i] = (points[j*n+i]+points[i])/2;
+                }
+                float val = func(points+j*n);
+                addValue(j,val,vals,points+j*n,points,n);
+            }
+        }
+    }
+
+    float bestVal = vals[0];
+
+    delete[] p_r;
+    delete[] p_o;
+    delete[] p_e;
+    if (ownVals) delete[] vals;
+
+    return bestVal;
+}
+
+}
+
+#endif //FLANN_SIMPLEX_DOWNHILL_H_
diff --git a/src/cpp/flann/util/allocator.h b/src/cpp/flann/util/allocator.h
new file mode 100644
index 0000000..489bcb2
--- /dev/null
+++ b/src/cpp/flann/util/allocator.h
@@ -0,0 +1,202 @@
+/***********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2008-2009  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+ * Copyright 2008-2009  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+ *
+ * THE BSD LICENSE
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+
+#ifndef FLANN_ALLOCATOR_H_
+#define FLANN_ALLOCATOR_H_
+
+#include <stdlib.h>
+#include <stdio.h>
+
+
+namespace flann
+{
+
+/**
+ * Allocates (using C's malloc) a generic type T.
+ *
+ * Params:
+ *     count = number of instances to allocate.
+ * Returns: pointer (of type T*) to memory buffer
+ */
+template <typename T>
+T* allocate(size_t count = 1)
+{
+    T* mem = (T*) ::malloc(sizeof(T)*count);
+    return mem;
+}
+
+
+
+/**
+ * Pooled storage allocator
+ *
+ * The following routines allow for the efficient allocation of storage in
+ * small chunks from a specified pool.  Rather than allowing each structure
+ * to be freed individually, an entire pool of storage is freed at once.
+ * This method has two advantages over just using malloc() and free().  First,
+ * it is far more efficient for allocating small objects, as there is
+ * no overhead for remembering all the information needed to free each
+ * object or consolidating fragmented memory.  Second, the decision about
+ * how long to keep an object is made at the time of allocation, and there
+ * is no need to track down all the objects to free them.
+ *
+ */
+
+const size_t     WORDSIZE=16;
+const  size_t     BLOCKSIZE=8192;
+
+class PooledAllocator
+{
+    /* We maintain memory alignment to word boundaries by requiring that all
+        allocations be in multiples of the machine wordsize.  */
+    /* Size of machine word in bytes.  Must be power of 2. */
+    /* Minimum number of bytes requested at a time from	the system.  Must be multiple of WORDSIZE. */
+
+
+    int     remaining;  /* Number of bytes left in current block of storage. */
+    void*   base;     /* Pointer to base of current block of storage. */
+    void*   loc;      /* Current location in block to next allocate memory. */
+    int     blocksize;
+
+
+public:
+    int     usedMemory;
+    int     wastedMemory;
+
+    /**
+        Default constructor. Initializes a new pool.
+     */
+    PooledAllocator(int blocksize = BLOCKSIZE)
+    {
+        this->blocksize = blocksize;
+        remaining = 0;
+        base = NULL;
+
+        usedMemory = 0;
+        wastedMemory = 0;
+    }
+
+    /**
+     * Destructor. Frees all the memory allocated in this pool.
+     */
+    ~PooledAllocator()
+    {
+        free();
+    }
+    
+    void free()
+    {
+        void* prev;
+        while (base != NULL) {
+            prev = *((void**) base); /* Get pointer to prev block. */
+            ::free(base);
+            base = prev;
+        }
+        base = NULL;
+        remaining = 0;
+        usedMemory = 0;
+        wastedMemory = 0;
+    }
+
+    /**
+     * Returns a pointer to a piece of new memory of the given size in bytes
+     * allocated from the pool.
+     */
+    void* allocateMemory(int size)
+    {
+        int blocksize;
+
+        /* Round size up to a multiple of wordsize.  The following expression
+            only works for WORDSIZE that is a power of 2, by masking last bits of
+            incremented size to zero.
+         */
+        size = (size + (WORDSIZE - 1)) & ~(WORDSIZE - 1);
+
+        /* Check whether a new block must be allocated.  Note that the first word
+            of a block is reserved for a pointer to the previous block.
+         */
+        if (size > remaining) {
+
+            wastedMemory += remaining;
+
+            /* Allocate new storage. */
+            blocksize = (size + sizeof(void*) + (WORDSIZE-1) > BLOCKSIZE) ?
+                        size + sizeof(void*) + (WORDSIZE-1) : BLOCKSIZE;
+
+            // use the standard C malloc to allocate memory
+            void* m = ::malloc(blocksize);
+            if (!m) {
+                fprintf(stderr,"Failed to allocate memory.\n");
+                return NULL;
+            }
+
+            /* Fill first word of new block with pointer to previous block. */
+            ((void**) m)[0] = base;
+            base = m;
+
+            int shift = 0;
+            //int shift = (WORDSIZE - ( (((size_t)m) + sizeof(void*)) & (WORDSIZE-1))) & (WORDSIZE-1);
+
+            remaining = blocksize - sizeof(void*) - shift;
+            loc = ((char*)m + sizeof(void*) + shift);
+        }
+        void* rloc = loc;
+        loc = (char*)loc + size;
+        remaining -= size;
+
+        usedMemory += size;
+
+        return rloc;
+    }
+
+    /**
+     * Allocates (using this pool) a generic type T.
+     *
+     * Params:
+     *     count = number of instances to allocate.
+     * Returns: pointer (of type T*) to memory buffer
+     */
+    template <typename T>
+    T* allocate(size_t count = 1)
+    {
+        T* mem = (T*) this->allocateMemory((int)(sizeof(T)*count));
+        return mem;
+    }
+
+};
+
+}
+
+inline void* operator new (std::size_t size, flann::PooledAllocator& allocator)
+{
+    return allocator.allocateMemory(size) ;
+}
+
+#endif //FLANN_ALLOCATOR_H_
diff --git a/src/cpp/flann/util/any.h b/src/cpp/flann/util/any.h
new file mode 100644
index 0000000..cf0efec
--- /dev/null
+++ b/src/cpp/flann/util/any.h
@@ -0,0 +1,292 @@
+#ifndef FLANN_ANY_H_
+#define FLANN_ANY_H_
+/*
+ * (C) Copyright Christopher Diggins 2005-2011
+ * (C) Copyright Pablo Aguilar 2005
+ * (C) Copyright Kevlin Henney 2001
+ *
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt
+ *
+ * Adapted for FLANN by Marius Muja
+ */
+
+#include <stdexcept>
+#include <ostream>
+#include <typeinfo>
+
+namespace flann
+{
+
+namespace anyimpl
+{
+
+struct bad_any_cast : public std::runtime_error
+{
+	bad_any_cast() : std::runtime_error("Cannot convert 'any' value") { }
+};
+
+struct empty_any
+{
+};
+
+inline std::ostream& operator <<(std::ostream& out, const empty_any&)
+{
+    out << "[empty_any]";
+    return out;
+}
+
+struct base_any_policy
+{
+    virtual void static_delete(void** x) = 0;
+    virtual void copy_from_value(void const* src, void** dest) = 0;
+    virtual void clone(void* const* src, void** dest) = 0;
+    virtual void move(void* const* src, void** dest) = 0;
+    virtual void* get_value(void** src) = 0;
+    virtual ::size_t get_size() = 0;
+    virtual const std::type_info& type() = 0;
+    virtual void print(std::ostream& out, void* const* src) = 0;
+};
+
+template<typename T>
+struct typed_base_any_policy : base_any_policy
+{
+    virtual ::size_t get_size() { return sizeof(T); }
+    virtual const std::type_info& type() { return typeid(T); }
+
+};
+
+template<typename T>
+struct small_any_policy : typed_base_any_policy<T>
+{
+    virtual void static_delete(void**) { }
+    virtual void copy_from_value(void const* src, void** dest)
+    {
+        new (dest) T(* reinterpret_cast<T const*>(src));
+    }
+    virtual void clone(void* const* src, void** dest) { *dest = *src; }
+    virtual void move(void* const* src, void** dest) { *dest = *src; }
+    virtual void* get_value(void** src) { return reinterpret_cast<void*>(src); }
+    virtual void print(std::ostream& out, void* const* src) { out << *reinterpret_cast<T const*>(src); }
+};
+
+template<typename T>
+struct big_any_policy : typed_base_any_policy<T>
+{
+    virtual void static_delete(void** x)
+    {
+        if (* x) delete (* reinterpret_cast<T**>(x)); *x = NULL;
+    }
+    virtual void copy_from_value(void const* src, void** dest)
+    {
+        *dest = new T(*reinterpret_cast<T const*>(src));
+    }
+    virtual void clone(void* const* src, void** dest)
+    {
+        *dest = new T(**reinterpret_cast<T* const*>(src));
+    }
+    virtual void move(void* const* src, void** dest)
+    {
+        (*reinterpret_cast<T**>(dest))->~T();
+        **reinterpret_cast<T**>(dest) = **reinterpret_cast<T* const*>(src);
+    }
+    virtual void* get_value(void** src) { return *src; }
+    virtual void print(std::ostream& out, void* const* src) { out << *reinterpret_cast<T const*>(*src); }
+};
+
+template<typename T>
+struct choose_policy
+{
+    typedef big_any_policy<T> type;
+};
+
+template<typename T>
+struct choose_policy<T*>
+{
+    typedef small_any_policy<T*> type;
+};
+
+struct any;
+
+/// Choosing the policy for an any type is illegal, but should never happen.
+/// This is designed to throw a compiler error.
+template<>
+struct choose_policy<any>
+{
+    typedef void type;
+};
+
+/// Specializations for small types.
+#define SMALL_POLICY(TYPE) \
+    template<> \
+    struct choose_policy<TYPE> { typedef small_any_policy<TYPE> type; \
+    }
+
+SMALL_POLICY(signed char);
+SMALL_POLICY(unsigned char);
+SMALL_POLICY(signed short);
+SMALL_POLICY(unsigned short);
+SMALL_POLICY(signed int);
+SMALL_POLICY(unsigned int);
+SMALL_POLICY(signed long);
+SMALL_POLICY(unsigned long);
+SMALL_POLICY(float);
+SMALL_POLICY(bool);
+
+//#undef SMALL_POLICY
+
+/// This function will return a different policy for each type.
+template<typename T>
+base_any_policy* get_policy()
+{
+    static typename choose_policy<T>::type policy;
+    return &policy;
+}
+} // namespace anyimpl
+
+class any
+{
+	typedef any any_t; // workaround for the NVCC compiler under windows
+private:
+    // fields
+    anyimpl::base_any_policy* policy;
+    void* object;
+
+public:
+    /// Initializing constructor.
+    template <typename T>
+    any(const T& x)
+        : policy(anyimpl::get_policy<anyimpl::empty_any>()), object(NULL)
+    {
+        assign(x);
+    }
+
+    /// Empty constructor.
+    any()
+        : policy(anyimpl::get_policy<anyimpl::empty_any>()), object(NULL)
+    { }
+
+    /// Special initializing constructor for string literals.
+    any(const char* x)
+        : policy(anyimpl::get_policy<anyimpl::empty_any>()), object(NULL)
+    {
+        assign(x);
+    }
+
+    /// Copy constructor.
+    any(const any& x)
+        : policy(anyimpl::get_policy<anyimpl::empty_any>()), object(NULL)
+    {
+        assign(x);
+    }
+
+    /// Destructor.
+    ~any()
+    {
+        policy->static_delete(&object);
+    }
+
+    /// Assignment function from another any.
+    any& assign(const any& x)
+    {
+        reset();
+        policy = x.policy;
+        policy->clone(&x.object, &object);
+        return *this;
+    }
+
+    /// Assignment function.
+    template <typename T>
+    any_t& assign(const T& x)
+    {
+        reset();
+        policy = anyimpl::get_policy<T>();
+        policy->copy_from_value(&x, &object);
+        return *this;
+    }
+
+    /// Assignment operator.
+    template<typename T>
+    any_t& operator=(const T& x)
+    {
+        return assign(x);
+    }
+
+    /// Assignment operator, specialed for literal strings.
+    /// They have types like const char [6] which don't work as expected.
+    any& operator=(const char* x)
+    {
+        return assign(x);
+    }
+
+    /// Utility functions
+    any& swap(any& x)
+    {
+        std::swap(policy, x.policy);
+        std::swap(object, x.object);
+        return *this;
+    }
+
+    /// Cast operator. You can only cast to the original type.
+    template<typename T>
+    T& cast()
+    {
+        if (policy->type() != typeid(T)) throw anyimpl::bad_any_cast();
+        T* r = reinterpret_cast<T*>(policy->get_value(&object));
+        return *r;
+    }
+
+    /// Cast operator. You can only cast to the original type.
+    template<typename T>
+    const T& cast() const
+    {
+        if (policy->type() != typeid(T)) throw anyimpl::bad_any_cast();
+        void* obj = const_cast<void*>(object);
+        T* r = reinterpret_cast<T*>(policy->get_value(&obj));
+        return *r;
+    }
+
+    /// Returns true if the any contains no value.
+    bool empty() const
+    {
+        return policy->type() == typeid(anyimpl::empty_any);
+    }
+
+    /// Frees any allocated memory, and sets the value to NULL.
+    void reset()
+    {
+        policy->static_delete(&object);
+        policy = anyimpl::get_policy<anyimpl::empty_any>();
+    }
+
+    /// Returns true if the two types are the same.
+    bool compatible(const any& x) const
+    {
+        return policy->type() == x.policy->type();
+    }
+
+    /// Returns if the type is compatible with the policy
+    template<typename T>
+    bool has_type()
+    {
+        return policy->type() == typeid(T);
+    }
+
+    const std::type_info& type() const
+    {
+        return policy->type();
+    }
+
+    friend std::ostream& operator <<(std::ostream& out, const any& any_val);
+};
+
+inline std::ostream& operator <<(std::ostream& out, const any& any_val)
+{
+    any_val.policy->print(out,&any_val.object);
+    return out;
+}
+
+}
+
+#endif // FLANN_ANY_H_
diff --git a/src/cpp/flann/util/cuda/heap.h b/src/cpp/flann/util/cuda/heap.h
new file mode 100644
index 0000000..7a98c2b
--- /dev/null
+++ b/src/cpp/flann/util/cuda/heap.h
@@ -0,0 +1,139 @@
+#ifndef FLANN_UTIL_CUDA_HEAP_H
+#define FLANN_UTIL_CUDA_HEAP_H
+
+/*
+    Copyright (c) 2011, Andreas Mützel <andreas.muetzel at gmx.net>
+    All rights reserved.
+
+    Redistribution and use in source and binary forms, with or without
+    modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright
+        notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+        notice, this list of conditions and the following disclaimer in the
+        documentation and/or other materials provided with the distribution.
+ * Neither the name of the <organization> nor the
+        names of its contributors may be used to endorse or promote products
+        derived from this software without specific prior written permission.
+
+    THIS SOFTWARE IS PROVIDED BY Andreas Mützel <andreas.muetzel at gmx.net> ''AS IS'' AND ANY
+    EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+    DISCLAIMED. IN NO EVENT SHALL Andreas Mützel <andreas.muetzel at gmx.net> BE LIABLE FOR ANY
+    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+namespace flann
+{
+namespace cuda
+{
+template <class T>
+__device__ __host__ void swap( T& x, T& y )
+{
+    T t=x;
+    x=y;
+    y=t;
+}
+
+namespace heap
+{
+
+//! moves an element down the heap until all children are smaller than the elemnent
+//! if c is a less-than comparator, it do this until all children are larger
+template <class GreaterThan, class RandomAccessIterator>
+__host__ __device__ void
+sift_down( RandomAccessIterator array, size_t begin, size_t length, GreaterThan c = GreaterThan() )
+{
+
+    while( 2*begin+1 < length ) {
+        size_t left = 2*begin+1;
+        size_t right = 2*begin+2;
+        size_t largest=begin;
+        if((left < length)&& c(array[left], array[largest]) ) largest=left;
+
+        if((right < length)&& c(array[right], array[largest]) ) largest=right;
+
+        if( largest != begin ) {
+            cuda::swap( array[begin], array[largest] );
+            begin=largest;
+        }
+        else return;
+    }
+}
+
+//! creates a max-heap in the array beginning at begin of length "length"
+//! if c is a less-than comparator, it will create a min-heap
+template <class GreaterThan, class RandomAccessIterator>
+__host__ __device__ void
+make_heap( RandomAccessIterator begin, size_t length, GreaterThan c = GreaterThan() )
+{
+    int i=length/2-1;
+    while( i>=0 ) {
+        sift_down( begin, i, length, c );
+        i--;
+    }
+}
+
+
+//! verifies if the array is a max-heap
+//! if c is a less-than comparator, it will verify if it is a min-heap
+template <class GreaterThan, class RandomAccessIterator>
+__host__ __device__ bool
+is_heap( RandomAccessIterator begin, size_t length, GreaterThan c = GreaterThan() )
+{
+    for( unsigned i=0; i<length; i++ ) {
+        if((2*i+1 < length)&& c(begin[2*i+1],begin[i]) ) return false;
+        if((2*i+2 < length)&& c(begin[2*i+2],begin[i]) ) return false;
+    }
+    return true;
+}
+
+
+//! moves an element down the heap until all children are smaller than the elemnent
+//! if c is a less-than comparator, it do this until all children are larger
+template <class GreaterThan, class RandomAccessIterator, class RandomAccessIterator2>
+__host__ __device__ void
+sift_down( RandomAccessIterator key, RandomAccessIterator2 value, size_t begin, size_t length, GreaterThan c = GreaterThan() )
+{
+
+    while( 2*begin+1 < length ) {
+        size_t left = 2*begin+1;
+        size_t right = 2*begin+2;
+        size_t largest=begin;
+        if((left < length)&& c(key[left], key[largest]) ) largest=left;
+
+        if((right < length)&& c(key[right], key[largest]) ) largest=right;
+
+        if( largest != begin ) {
+            cuda::swap( key[begin], key[largest] );
+            cuda::swap( value[begin], value[largest] );
+            begin=largest;
+        }
+        else return;
+    }
+}
+
+//! creates a max-heap in the array beginning at begin of length "length"
+//! if c is a less-than comparator, it will create a min-heap
+template <class GreaterThan, class RandomAccessIterator, class RandomAccessIterator2>
+__host__ __device__ void
+make_heap( RandomAccessIterator key,  RandomAccessIterator2 value, size_t length, GreaterThan c = GreaterThan() )
+{
+    int i=length/2-1;
+    while( i>=0 ) {
+        sift_down( key, value, i, length, c );
+        i--;
+    }
+}
+
+}
+
+}
+}
+
+#endif
\ No newline at end of file
diff --git a/src/cpp/flann/util/cuda/result_set.h b/src/cpp/flann/util/cuda/result_set.h
new file mode 100644
index 0000000..9ac8111
--- /dev/null
+++ b/src/cpp/flann/util/cuda/result_set.h
@@ -0,0 +1,536 @@
+/**********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2011  Andreas Muetzel (amuetzel at uni-koblenz.de). All rights reserved.
+ *
+ * THE BSD LICENSE
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+#ifndef FLANN_UTIL_CUDA_RESULTSET_H
+#define FLANN_UTIL_CUDA_RESULTSET_H
+
+#include <flann/util/cuda/heap.h>
+#include <limits>
+
+__device__ __forceinline__
+float infinity()
+{
+       return __int_as_float(0x7f800000);
+}
+
+#ifndef INFINITY
+#define INFINITY infinity()
+#endif
+
+namespace flann
+{
+namespace cuda
+{
+//! result set for the 1nn search. Doesn't do any global memory accesses on its own,
+template< typename DistanceType >
+struct SingleResultSet
+{
+    int bestIndex;
+    DistanceType bestDist;
+    const DistanceType epsError;
+
+    __device__
+    SingleResultSet( DistanceType eps ) : bestIndex(-1),bestDist(INFINITY), epsError(eps){ }
+
+    __device__
+    inline float
+    worstDist()
+    {
+        return bestDist;
+    }
+
+    __device__
+    inline void
+    insert(int index, DistanceType dist)
+    {
+        if( dist <= bestDist ) {
+            bestIndex=index;
+            bestDist=dist;
+        }
+    }
+
+    DistanceType* resultDist;
+    int* resultIndex;
+
+    __device__
+    inline void
+    setResultLocation( DistanceType* dists, int* index, int thread, int stride )
+    {
+        resultDist=dists+thread*stride;
+        resultIndex=index+thread*stride;
+        if( stride != 1 ) {
+            for( int i=1; i<stride; i++ ) {
+                resultDist[i]=INFINITY;
+                resultIndex[i]=-1;
+            }
+        }
+    }
+
+    __device__
+    inline void
+    finish()
+    {
+        resultDist[0]=bestDist;
+        resultIndex[0]=bestIndex;
+    }
+};
+
+template< typename DistanceType >
+struct GreaterThan
+{
+    __device__
+    bool operator()(DistanceType a, DistanceType b)
+    {
+        return a>b;
+    }
+};
+
+
+// using this and the template uses 2 or 3 registers more than the direct implementation in the kNearestKernel, but
+// there is no speed difference.
+// Setting useHeap as a template parameter leads to a whole lot of things being
+// optimized away by nvcc.
+// Register counts are the same as when removing not-needed variables in explicit specializations
+// and the "if( useHeap )" branches are eliminated at compile time.
+// The downside of this: a bit more complex kernel launch code.
+template< typename DistanceType, bool useHeap >
+struct KnnResultSet
+{
+    int foundNeighbors;
+    DistanceType largestHeapDist;
+    int maxDistIndex;
+    const int k;
+    const bool sorted;
+    const DistanceType epsError;
+
+
+    __device__
+    KnnResultSet(int knn, bool sortResults, DistanceType eps) : foundNeighbors(0),largestHeapDist(INFINITY),k(knn), sorted(sortResults), epsError(eps){ }
+
+    //          __host__ __device__
+    //          KnnResultSet(const KnnResultSet& o):foundNeighbors(o.foundNeighbors),largestHeapDist(o.largestHeapDist),k(o.k){ }
+
+    __device__
+    inline DistanceType
+    worstDist()
+    {
+        return largestHeapDist;
+    }
+
+    __device__
+    inline void
+    insert(int index, DistanceType dist)
+    {
+        if( foundNeighbors<k ) {
+            resultDist[foundNeighbors]=dist;
+            resultIndex[foundNeighbors]=index;
+            if( foundNeighbors==k-1) {
+                if( useHeap ) {
+                    flann::cuda::heap::make_heap(resultDist,resultIndex,k,GreaterThan<DistanceType>());
+                    largestHeapDist=resultDist[0];
+                }
+                else {
+                    findLargestDistIndex();
+                }
+
+            }
+            foundNeighbors++;
+        }
+        else if( dist < largestHeapDist ) {
+            if( useHeap ) {
+                resultDist[0]=dist;
+                resultIndex[0]=index;
+                flann::cuda::heap::sift_down(resultDist,resultIndex,0,k,GreaterThan<DistanceType>());
+                largestHeapDist=resultDist[0];
+            }
+            else {
+                resultDist[maxDistIndex]=dist;
+                resultIndex[maxDistIndex]=index;
+                findLargestDistIndex();
+            }
+
+        }
+    }
+
+    __device__
+    void
+    findLargestDistIndex( )
+    {
+        largestHeapDist=resultDist[0];
+        maxDistIndex=0;
+        for( int i=1; i<k; i++ )
+            if( resultDist[i] > largestHeapDist ) {
+                maxDistIndex=i;
+                largestHeapDist=resultDist[i];
+            }
+    }
+
+    float* resultDist;
+    int* resultIndex;
+
+    __device__
+    inline void
+    setResultLocation( DistanceType* dists, int* index, int thread, int stride )
+    {
+        resultDist=dists+stride*thread;
+        resultIndex=index+stride*thread;
+        for( int i=0; i<stride; i++ ) {
+            resultDist[i]=INFINITY;
+            resultIndex[i]=-1;
+            //                  resultIndex[tid+i*blockDim.x]=-1;
+            //                  resultDist[tid+i*blockDim.x]=INFINITY;
+        }
+    }
+
+    __host__ __device__
+    inline void
+    finish()
+    {
+        if( sorted ) {
+            if( !useHeap ) flann::cuda::heap::make_heap(resultDist,resultIndex,k,GreaterThan<DistanceType>());
+            for( int i=k-1; i>0; i-- ) {
+                flann::cuda::swap( resultDist[0], resultDist[i] );
+                flann::cuda::swap( resultIndex[0], resultIndex[i] );
+                flann::cuda::heap::sift_down( resultDist,resultIndex, 0, i, GreaterThan<DistanceType>() );
+            }
+        }
+    }
+};
+
+template <typename DistanceType>
+struct CountingRadiusResultSet
+{
+    int count_;
+    DistanceType radius_sq_;
+    int max_neighbors_;
+
+    __device__
+    CountingRadiusResultSet(DistanceType radius, int max_neighbors) : count_(0),radius_sq_(radius), max_neighbors_(max_neighbors){ }
+
+    __device__
+    inline DistanceType
+    worstDist()
+    {
+        return radius_sq_;
+    }
+
+    __device__
+    inline void
+    insert(int index, float dist)
+    {
+        if( dist < radius_sq_ ) {
+            count_++;
+        }
+    }
+
+    int* resultIndex;
+
+    __device__
+    inline void
+    setResultLocation( DistanceType* /*dists*/, int* count, int thread, int stride )
+    {
+        resultIndex=count+thread*stride;
+    }
+
+    __device__
+    inline void
+    finish()
+    {
+        if(( max_neighbors_<=0) ||( count_<=max_neighbors_) ) resultIndex[0]=count_;
+        else resultIndex[0]=max_neighbors_;
+    }
+};
+
+template<typename DistanceType, bool useHeap>
+struct RadiusKnnResultSet
+{
+    int foundNeighbors;
+    DistanceType largestHeapDist;
+    int maxDistElem;
+    const int k;
+    const bool sorted;
+    const DistanceType radius_sq_;
+    int* segment_starts_;
+    //          int count_;
+
+
+    __device__
+    RadiusKnnResultSet(DistanceType radius, int knn, int* segment_starts, bool sortResults) : foundNeighbors(0),largestHeapDist(radius),k(knn), sorted(sortResults), radius_sq_(radius),segment_starts_(segment_starts) { }
+
+    //          __host__ __device__
+    //          KnnResultSet(const KnnResultSet& o):foundNeighbors(o.foundNeighbors),largestHeapDist(o.largestHeapDist),k(o.k){ }
+
+    __device__
+    inline DistanceType
+    worstDist()
+    {
+        return largestHeapDist;
+    }
+
+    __device__
+    inline void
+    insert(int index, DistanceType dist)
+    {
+        if( dist < radius_sq_ ) {
+            if( foundNeighbors<k ) {
+                resultDist[foundNeighbors]=dist;
+                resultIndex[foundNeighbors]=index;
+                if(( foundNeighbors==k-1) && useHeap) {
+                    if( useHeap ) {
+                        flann::cuda::heap::make_heap(resultDist,resultIndex,k,GreaterThan<DistanceType>());
+                        largestHeapDist=resultDist[0];
+                    }
+                    else {
+                        findLargestDistIndex();
+                    }
+                }
+                foundNeighbors++;
+
+            }
+            else if( dist < largestHeapDist ) {
+                if( useHeap ) {
+                    resultDist[0]=dist;
+                    resultIndex[0]=index;
+                    flann::cuda::heap::sift_down(resultDist,resultIndex,0,k,GreaterThan<DistanceType>());
+                    largestHeapDist=resultDist[0];
+                }
+                else {
+                    resultDist[maxDistElem]=dist;
+                    resultIndex[maxDistElem]=index;
+                    findLargestDistIndex();
+                }
+            }
+        }
+    }
+
+    __device__
+    void
+    findLargestDistIndex( )
+    {
+        largestHeapDist=resultDist[0];
+        maxDistElem=0;
+        for( int i=1; i<k; i++ )
+            if( resultDist[i] > largestHeapDist ) {
+                maxDistElem=i;
+                largestHeapDist=resultDist[i];
+            }
+    }
+
+
+    DistanceType* resultDist;
+    int* resultIndex;
+
+    __device__
+    inline void
+    setResultLocation( DistanceType* dists, int* index, int thread, int /*stride*/ )
+    {
+        resultDist=dists+segment_starts_[thread];
+        resultIndex=index+segment_starts_[thread];
+    }
+
+    __device__
+    inline void
+    finish()
+    {
+        if( sorted ) {
+            if( !useHeap ) flann::cuda::heap::make_heap(resultDist,resultIndex,k,GreaterThan<DistanceType>());
+            for( int i=foundNeighbors-1; i>0; i-- ) {
+                flann::cuda::swap( resultDist[0], resultDist[i] );
+                flann::cuda::swap( resultIndex[0], resultIndex[i] );
+                flann::cuda::heap::sift_down( resultDist,resultIndex, 0, i, GreaterThan<DistanceType>() );
+            }
+        }
+    }
+};
+
+// Difference to RadiusKnnResultSet: Works like KnnResultSet, doesn't pack the results densely (as the RadiusResultSet does)
+template <typename DistanceType, bool useHeap>
+struct KnnRadiusResultSet
+{
+    int foundNeighbors;
+    DistanceType largestHeapDist;
+    int maxDistIndex;
+    const int k;
+    const bool sorted;
+    const DistanceType epsError;
+    const DistanceType radius_sq;
+
+
+    __device__
+    KnnRadiusResultSet(int knn, bool sortResults, DistanceType eps, DistanceType radius) : foundNeighbors(0),largestHeapDist(radius),k(knn), sorted(sortResults), epsError(eps),radius_sq(radius){ }
+
+    //          __host__ __device__
+    //          KnnResultSet(const KnnResultSet& o):foundNeighbors(o.foundNeighbors),largestHeapDist(o.largestHeapDist),k(o.k){ }
+
+    __device__
+    inline DistanceType
+    worstDist()
+    {
+        return largestHeapDist;
+    }
+
+    __device__
+    inline void
+    insert(int index, DistanceType dist)
+    {
+        if( dist < largestHeapDist ) {
+            if( foundNeighbors<k ) {
+                resultDist[foundNeighbors]=dist;
+                resultIndex[foundNeighbors]=index;
+                if( foundNeighbors==k-1 ) {
+                    if( useHeap ) {
+                        flann::cuda::heap::make_heap(resultDist,resultIndex,k,GreaterThan<DistanceType>());
+                        largestHeapDist=resultDist[0];
+                    }
+                    else {
+                        findLargestDistIndex();
+                    }
+                }
+                foundNeighbors++;
+            }
+            else { //if( dist < largestHeapDist )
+                if( useHeap ) {
+                    resultDist[0]=dist;
+                    resultIndex[0]=index;
+                    flann::cuda::heap::sift_down(resultDist,resultIndex,0,k,GreaterThan<DistanceType>());
+                    largestHeapDist=resultDist[0];
+                }
+                else {
+                    resultDist[maxDistIndex]=dist;
+                    resultIndex[maxDistIndex]=index;
+                    findLargestDistIndex();
+                }
+            }
+        }
+    }
+    __device__
+    void
+    findLargestDistIndex( )
+    {
+        largestHeapDist=resultDist[0];
+        maxDistIndex=0;
+        for( int i=1; i<k; i++ )
+            if( resultDist[i] > largestHeapDist ) {
+                maxDistIndex=i;
+                largestHeapDist=resultDist[i];
+            }
+    }
+
+    DistanceType* resultDist;
+    int* resultIndex;
+
+    __device__
+    inline void
+    setResultLocation( DistanceType* dists, int* index, int thread, int stride )
+    {
+        resultDist=dists+stride*thread;
+        resultIndex=index+stride*thread;
+        for( int i=0; i<stride; i++ ) {
+            resultDist[i]=INFINITY;
+            resultIndex[i]=-1;
+            //                  resultIndex[tid+i*blockDim.x]=-1;
+            //                  resultDist[tid+i*blockDim.x]=INFINITY;
+        }
+    }
+
+    __device__
+    inline void
+    finish()
+    {
+        if( sorted ) {
+            if( !useHeap ) flann::cuda::heap::make_heap(resultDist,resultIndex,k,GreaterThan<DistanceType>());
+            for( int i=k-1; i>0; i-- ) {
+                flann::cuda::swap( resultDist[0], resultDist[i] );
+                flann::cuda::swap( resultIndex[0], resultIndex[i] );
+                flann::cuda::heap::sift_down( resultDist,resultIndex, 0, i, GreaterThan<DistanceType>() );
+            }
+        }
+    }
+};
+
+//! fills the radius output buffer.
+//! IMPORTANT ASSERTION: ASSUMES THAT THERE IS ENOUGH SPACE FOR EVERY NEIGHBOR! IF THIS ISN'T
+//! TRUE, USE KnnRadiusResultSet! (Otherwise, the neighbors of one element might overflow into the next element, or past the buffer.)
+template< typename DistanceType >
+struct RadiusResultSet
+{
+    DistanceType radius_sq_;
+    int* segment_starts_;
+    int count_;
+    bool sorted_;
+
+    __device__
+    RadiusResultSet(DistanceType radius, int* segment_starts, bool sorted) : radius_sq_(radius), segment_starts_(segment_starts), count_(0), sorted_(sorted){ }
+
+    __device__
+    inline DistanceType
+    worstDist()
+    {
+        return radius_sq_;
+    }
+
+    __device__
+    inline void
+    insert(int index, DistanceType dist)
+    {
+        if( dist < radius_sq_ ) {
+            resultIndex[count_]=index;
+            resultDist[count_]=dist;
+            count_++;
+        }
+    }
+
+    int* resultIndex;
+    DistanceType* resultDist;
+
+    __device__
+    inline void
+    setResultLocation( DistanceType* dists, int* index, int thread, int /*stride*/ )
+    {
+        resultIndex=index+segment_starts_[thread];
+        resultDist=dists+segment_starts_[thread];
+    }
+
+    __device__
+    inline void
+    finish()
+    {
+        if( sorted_ ) {
+            flann::cuda::heap::make_heap( resultDist,resultIndex, count_, GreaterThan<DistanceType>() );
+            for( int i=count_-1; i>0; i-- ) {
+                flann::cuda::swap( resultDist[0], resultDist[i] );
+                flann::cuda::swap( resultIndex[0], resultIndex[i] );
+                flann::cuda::heap::sift_down( resultDist,resultIndex, 0, i, GreaterThan<DistanceType>() );
+            }
+        }
+    }
+};
+}
+}
+
+#endif
diff --git a/src/cpp/flann/util/cutil_math.h b/src/cpp/flann/util/cutil_math.h
new file mode 100644
index 0000000..746f4d6
--- /dev/null
+++ b/src/cpp/flann/util/cutil_math.h
@@ -0,0 +1,1328 @@
+/*
+ * Copyright 1993-2010 NVIDIA Corporation.  All rights reserved.
+ *
+ * Please refer to the NVIDIA end user license agreement (EULA) associated
+ * with this source code for terms and conditions that govern your use of
+ * this software. Any use, reproduction, disclosure, or distribution of
+ * this software and related documentation outside the terms of the EULA
+ * is strictly prohibited.
+ *
+ */
+
+/*
+    This file implements common mathematical operations on vector types
+    (float3, float4 etc.) since these are not provided as standard by CUDA.
+
+    The syntax is modelled on the Cg standard library.
+
+    This is part of the CUTIL library and is not supported by NVIDIA.
+
+    Thanks to Linh Hah for additions and fixes.
+*/
+
+#ifndef CUTIL_MATH_H
+#define CUTIL_MATH_H
+
+#include "cuda_runtime.h"
+
+typedef unsigned int uint;
+typedef unsigned short ushort;
+
+#ifndef __CUDACC__
+#include <math.h>
+
+////////////////////////////////////////////////////////////////////////////////
+// host implementations of CUDA functions
+////////////////////////////////////////////////////////////////////////////////
+
+inline float fminf(float a, float b)
+{
+  return a < b ? a : b;
+}
+
+inline float fmaxf(float a, float b)
+{
+  return a > b ? a : b;
+}
+
+inline int max(int a, int b)
+{
+  return a > b ? a : b;
+}
+
+inline int min(int a, int b)
+{
+  return a < b ? a : b;
+}
+
+inline float rsqrtf(float x)
+{
+    return 1.0f / sqrtf(x);
+}
+#endif
+
+////////////////////////////////////////////////////////////////////////////////
+// constructors
+////////////////////////////////////////////////////////////////////////////////
+
+inline __host__ __device__ float2 make_float2(float s)
+{
+    return make_float2(s, s);
+}
+inline __host__ __device__ float2 make_float2(float3 a)
+{
+    return make_float2(a.x, a.y);
+}
+inline __host__ __device__ float2 make_float2(int2 a)
+{
+    return make_float2(float(a.x), float(a.y));
+}
+inline __host__ __device__ float2 make_float2(uint2 a)
+{
+    return make_float2(float(a.x), float(a.y));
+}
+
+inline __host__ __device__ int2 make_int2(int s)
+{
+    return make_int2(s, s);
+}
+inline __host__ __device__ int2 make_int2(int3 a)
+{
+    return make_int2(a.x, a.y);
+}
+inline __host__ __device__ int2 make_int2(uint2 a)
+{
+    return make_int2(int(a.x), int(a.y));
+}
+inline __host__ __device__ int2 make_int2(float2 a)
+{
+    return make_int2(int(a.x), int(a.y));
+}
+
+inline __host__ __device__ uint2 make_uint2(uint s)
+{
+    return make_uint2(s, s);
+}
+inline __host__ __device__ uint2 make_uint2(uint3 a)
+{
+    return make_uint2(a.x, a.y);
+}
+inline __host__ __device__ uint2 make_uint2(int2 a)
+{
+    return make_uint2(uint(a.x), uint(a.y));
+}
+
+inline __host__ __device__ float3 make_float3(float s)
+{
+    return make_float3(s, s, s);
+}
+inline __host__ __device__ float3 make_float3(float2 a)
+{
+    return make_float3(a.x, a.y, 0.0f);
+}
+inline __host__ __device__ float3 make_float3(float2 a, float s)
+{
+    return make_float3(a.x, a.y, s);
+}
+inline __host__ __device__ float3 make_float3(float4 a)
+{
+    return make_float3(a.x, a.y, a.z);
+}
+inline __host__ __device__ float3 make_float3(int3 a)
+{
+    return make_float3(float(a.x), float(a.y), float(a.z));
+}
+inline __host__ __device__ float3 make_float3(uint3 a)
+{
+    return make_float3(float(a.x), float(a.y), float(a.z));
+}
+
+inline __host__ __device__ int3 make_int3(int s)
+{
+    return make_int3(s, s, s);
+}
+inline __host__ __device__ int3 make_int3(int2 a)
+{
+    return make_int3(a.x, a.y, 0);
+}
+inline __host__ __device__ int3 make_int3(int2 a, int s)
+{
+    return make_int3(a.x, a.y, s);
+}
+inline __host__ __device__ int3 make_int3(uint3 a)
+{
+    return make_int3(int(a.x), int(a.y), int(a.z));
+}
+inline __host__ __device__ int3 make_int3(float3 a)
+{
+    return make_int3(int(a.x), int(a.y), int(a.z));
+}
+
+inline __host__ __device__ uint3 make_uint3(uint s)
+{
+    return make_uint3(s, s, s);
+}
+inline __host__ __device__ uint3 make_uint3(uint2 a)
+{
+    return make_uint3(a.x, a.y, 0);
+}
+inline __host__ __device__ uint3 make_uint3(uint2 a, uint s)
+{
+    return make_uint3(a.x, a.y, s);
+}
+inline __host__ __device__ uint3 make_uint3(uint4 a)
+{
+    return make_uint3(a.x, a.y, a.z);
+}
+inline __host__ __device__ uint3 make_uint3(int3 a)
+{
+    return make_uint3(uint(a.x), uint(a.y), uint(a.z));
+}
+
+inline __host__ __device__ float4 make_float4(float s)
+{
+    return make_float4(s, s, s, s);
+}
+inline __host__ __device__ float4 make_float4(float3 a)
+{
+    return make_float4(a.x, a.y, a.z, 0.0f);
+}
+inline __host__ __device__ float4 make_float4(float3 a, float w)
+{
+    return make_float4(a.x, a.y, a.z, w);
+}
+inline __host__ __device__ float4 make_float4(int4 a)
+{
+    return make_float4(float(a.x), float(a.y), float(a.z), float(a.w));
+}
+inline __host__ __device__ float4 make_float4(uint4 a)
+{
+    return make_float4(float(a.x), float(a.y), float(a.z), float(a.w));
+}
+
+inline __host__ __device__ int4 make_int4(int s)
+{
+    return make_int4(s, s, s, s);
+}
+inline __host__ __device__ int4 make_int4(int3 a)
+{
+    return make_int4(a.x, a.y, a.z, 0);
+}
+inline __host__ __device__ int4 make_int4(int3 a, int w)
+{
+    return make_int4(a.x, a.y, a.z, w);
+}
+inline __host__ __device__ int4 make_int4(uint4 a)
+{
+    return make_int4(int(a.x), int(a.y), int(a.z), int(a.w));
+}
+inline __host__ __device__ int4 make_int4(float4 a)
+{
+    return make_int4(int(a.x), int(a.y), int(a.z), int(a.w));
+}
+
+
+inline __host__ __device__ uint4 make_uint4(uint s)
+{
+    return make_uint4(s, s, s, s);
+}
+inline __host__ __device__ uint4 make_uint4(uint3 a)
+{
+    return make_uint4(a.x, a.y, a.z, 0);
+}
+inline __host__ __device__ uint4 make_uint4(uint3 a, uint w)
+{
+    return make_uint4(a.x, a.y, a.z, w);
+}
+inline __host__ __device__ uint4 make_uint4(int4 a)
+{
+    return make_uint4(uint(a.x), uint(a.y), uint(a.z), uint(a.w));
+}
+
+////////////////////////////////////////////////////////////////////////////////
+// negate
+////////////////////////////////////////////////////////////////////////////////
+
+inline __host__ __device__ float2 operator-(float2 &a)
+{
+    return make_float2(-a.x, -a.y);
+}
+inline __host__ __device__ int2 operator-(int2 &a)
+{
+    return make_int2(-a.x, -a.y);
+}
+inline __host__ __device__ float3 operator-(float3 &a)
+{
+    return make_float3(-a.x, -a.y, -a.z);
+}
+inline __host__ __device__ int3 operator-(int3 &a)
+{
+    return make_int3(-a.x, -a.y, -a.z);
+}
+inline __host__ __device__ float4 operator-(float4 &a)
+{
+    return make_float4(-a.x, -a.y, -a.z, -a.w);
+}
+inline __host__ __device__ int4 operator-(int4 &a)
+{
+    return make_int4(-a.x, -a.y, -a.z, -a.w);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+// addition
+////////////////////////////////////////////////////////////////////////////////
+
+inline __host__ __device__ float2 operator+(float2 a, float2 b)
+{
+    return make_float2(a.x + b.x, a.y + b.y);
+}
+inline __host__ __device__ void operator+=(float2 &a, float2 b)
+{
+    a.x += b.x; a.y += b.y;
+}
+inline __host__ __device__ float2 operator+(float2 a, float b)
+{
+    return make_float2(a.x + b, a.y + b);
+}
+inline __host__ __device__ float2 operator+(float b, float2 a)
+{
+    return make_float2(a.x + b, a.y + b);
+}
+inline __host__ __device__ void operator+=(float2 &a, float b)
+{
+    a.x += b; a.y += b;
+}
+
+inline __host__ __device__ int2 operator+(int2 a, int2 b)
+{
+    return make_int2(a.x + b.x, a.y + b.y);
+}
+inline __host__ __device__ void operator+=(int2 &a, int2 b)
+{
+    a.x += b.x; a.y += b.y;
+}
+inline __host__ __device__ int2 operator+(int2 a, int b)
+{
+    return make_int2(a.x + b, a.y + b);
+}
+inline __host__ __device__ int2 operator+(int b, int2 a)
+{
+    return make_int2(a.x + b, a.y + b);
+}
+inline __host__ __device__ void operator+=(int2 &a, int b)
+{
+    a.x += b; a.y += b;
+}
+
+inline __host__ __device__ uint2 operator+(uint2 a, uint2 b)
+{
+    return make_uint2(a.x + b.x, a.y + b.y);
+}
+inline __host__ __device__ void operator+=(uint2 &a, uint2 b)
+{
+    a.x += b.x; a.y += b.y;
+}
+inline __host__ __device__ uint2 operator+(uint2 a, uint b)
+{
+    return make_uint2(a.x + b, a.y + b);
+}
+inline __host__ __device__ uint2 operator+(uint b, uint2 a)
+{
+    return make_uint2(a.x + b, a.y + b);
+}
+inline __host__ __device__ void operator+=(uint2 &a, uint b)
+{
+    a.x += b; a.y += b;
+}
+
+
+inline __host__ __device__ float3 operator+(float3 a, float3 b)
+{
+    return make_float3(a.x + b.x, a.y + b.y, a.z + b.z);
+}
+inline __host__ __device__ void operator+=(float3 &a, float3 b)
+{
+    a.x += b.x; a.y += b.y; a.z += b.z;
+}
+inline __host__ __device__ float3 operator+(float3 a, float b)
+{
+    return make_float3(a.x + b, a.y + b, a.z + b);
+}
+inline __host__ __device__ void operator+=(float3 &a, float b)
+{
+    a.x += b; a.y += b; a.z += b;
+}
+
+inline __host__ __device__ int3 operator+(int3 a, int3 b)
+{
+    return make_int3(a.x + b.x, a.y + b.y, a.z + b.z);
+}
+inline __host__ __device__ void operator+=(int3 &a, int3 b)
+{
+    a.x += b.x; a.y += b.y; a.z += b.z;
+}
+inline __host__ __device__ int3 operator+(int3 a, int b)
+{
+    return make_int3(a.x + b, a.y + b, a.z + b);
+}
+inline __host__ __device__ void operator+=(int3 &a, int b)
+{
+    a.x += b; a.y += b; a.z += b;
+}
+
+inline __host__ __device__ uint3 operator+(uint3 a, uint3 b)
+{
+    return make_uint3(a.x + b.x, a.y + b.y, a.z + b.z);
+}
+inline __host__ __device__ void operator+=(uint3 &a, uint3 b)
+{
+    a.x += b.x; a.y += b.y; a.z += b.z;
+}
+inline __host__ __device__ uint3 operator+(uint3 a, uint b)
+{
+    return make_uint3(a.x + b, a.y + b, a.z + b);
+}
+inline __host__ __device__ void operator+=(uint3 &a, uint b)
+{
+    a.x += b; a.y += b; a.z += b;
+}
+
+inline __host__ __device__ int3 operator+(int b, int3 a)
+{
+    return make_int3(a.x + b, a.y + b, a.z + b);
+}
+inline __host__ __device__ uint3 operator+(uint b, uint3 a)
+{
+    return make_uint3(a.x + b, a.y + b, a.z + b);
+}
+inline __host__ __device__ float3 operator+(float b, float3 a)
+{
+    return make_float3(a.x + b, a.y + b, a.z + b);
+}
+
+inline __host__ __device__ float4 operator+(float4 a, float4 b)
+{
+    return make_float4(a.x + b.x, a.y + b.y, a.z + b.z,  a.w + b.w);
+}
+inline __host__ __device__ void operator+=(float4 &a, float4 b)
+{
+    a.x += b.x; a.y += b.y; a.z += b.z; a.w += b.w;
+}
+inline __host__ __device__ float4 operator+(float4 a, float b)
+{
+    return make_float4(a.x + b, a.y + b, a.z + b, a.w + b);
+}
+inline __host__ __device__ float4 operator+(float b, float4 a)
+{
+    return make_float4(a.x + b, a.y + b, a.z + b, a.w + b);
+}
+inline __host__ __device__ void operator+=(float4 &a, float b)
+{
+    a.x += b; a.y += b; a.z += b; a.w += b;
+}
+
+inline __host__ __device__ int4 operator+(int4 a, int4 b)
+{
+    return make_int4(a.x + b.x, a.y + b.y, a.z + b.z,  a.w + b.w);
+}
+inline __host__ __device__ void operator+=(int4 &a, int4 b)
+{
+    a.x += b.x; a.y += b.y; a.z += b.z; a.w += b.w;
+}
+inline __host__ __device__ int4 operator+(int4 a, int b)
+{
+    return make_int4(a.x + b, a.y + b, a.z + b,  a.w + b);
+}
+inline __host__ __device__ int4 operator+(int b, int4 a)
+{
+    return make_int4(a.x + b, a.y + b, a.z + b,  a.w + b);
+}
+inline __host__ __device__ void operator+=(int4 &a, int b)
+{
+    a.x += b; a.y += b; a.z += b; a.w += b;
+}
+
+inline __host__ __device__ uint4 operator+(uint4 a, uint4 b)
+{
+    return make_uint4(a.x + b.x, a.y + b.y, a.z + b.z,  a.w + b.w);
+}
+inline __host__ __device__ void operator+=(uint4 &a, uint4 b)
+{
+    a.x += b.x; a.y += b.y; a.z += b.z; a.w += b.w;
+}
+inline __host__ __device__ uint4 operator+(uint4 a, uint b)
+{
+    return make_uint4(a.x + b, a.y + b, a.z + b,  a.w + b);
+}
+inline __host__ __device__ uint4 operator+(uint b, uint4 a)
+{
+    return make_uint4(a.x + b, a.y + b, a.z + b,  a.w + b);
+}
+inline __host__ __device__ void operator+=(uint4 &a, uint b)
+{
+    a.x += b; a.y += b; a.z += b; a.w += b;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+// subtract
+////////////////////////////////////////////////////////////////////////////////
+
+inline __host__ __device__ float2 operator-(float2 a, float2 b)
+{
+    return make_float2(a.x - b.x, a.y - b.y);
+}
+inline __host__ __device__ void operator-=(float2 &a, float2 b)
+{
+    a.x -= b.x; a.y -= b.y;
+}
+inline __host__ __device__ float2 operator-(float2 a, float b)
+{
+    return make_float2(a.x - b, a.y - b);
+}
+inline __host__ __device__ float2 operator-(float b, float2 a)
+{
+    return make_float2(b - a.x, b - a.y);
+}
+inline __host__ __device__ void operator-=(float2 &a, float b)
+{
+    a.x -= b; a.y -= b;
+}
+
+inline __host__ __device__ int2 operator-(int2 a, int2 b)
+{
+    return make_int2(a.x - b.x, a.y - b.y);
+}
+inline __host__ __device__ void operator-=(int2 &a, int2 b)
+{
+    a.x -= b.x; a.y -= b.y;
+}
+inline __host__ __device__ int2 operator-(int2 a, int b)
+{
+    return make_int2(a.x - b, a.y - b);
+}
+inline __host__ __device__ int2 operator-(int b, int2 a)
+{
+    return make_int2(b - a.x, b - a.y);
+}
+inline __host__ __device__ void operator-=(int2 &a, int b)
+{
+    a.x -= b; a.y -= b;
+}
+
+inline __host__ __device__ uint2 operator-(uint2 a, uint2 b)
+{
+    return make_uint2(a.x - b.x, a.y - b.y);
+}
+inline __host__ __device__ void operator-=(uint2 &a, uint2 b)
+{
+    a.x -= b.x; a.y -= b.y;
+}
+inline __host__ __device__ uint2 operator-(uint2 a, uint b)
+{
+    return make_uint2(a.x - b, a.y - b);
+}
+inline __host__ __device__ uint2 operator-(uint b, uint2 a)
+{
+    return make_uint2(b - a.x, b - a.y);
+}
+inline __host__ __device__ void operator-=(uint2 &a, uint b)
+{
+    a.x -= b; a.y -= b;
+}
+
+inline __host__ __device__ float3 operator-(float3 a, float3 b)
+{
+    return make_float3(a.x - b.x, a.y - b.y, a.z - b.z);
+}
+inline __host__ __device__ void operator-=(float3 &a, float3 b)
+{
+    a.x -= b.x; a.y -= b.y; a.z -= b.z;
+}
+inline __host__ __device__ float3 operator-(float3 a, float b)
+{
+    return make_float3(a.x - b, a.y - b, a.z - b);
+}
+inline __host__ __device__ float3 operator-(float b, float3 a)
+{
+    return make_float3(b - a.x, b - a.y, b - a.z);
+}
+inline __host__ __device__ void operator-=(float3 &a, float b)
+{
+    a.x -= b; a.y -= b; a.z -= b;
+}
+
+inline __host__ __device__ int3 operator-(int3 a, int3 b)
+{
+    return make_int3(a.x - b.x, a.y - b.y, a.z - b.z);
+}
+inline __host__ __device__ void operator-=(int3 &a, int3 b)
+{
+    a.x -= b.x; a.y -= b.y; a.z -= b.z;
+}
+inline __host__ __device__ int3 operator-(int3 a, int b)
+{
+    return make_int3(a.x - b, a.y - b, a.z - b);
+}
+inline __host__ __device__ int3 operator-(int b, int3 a)
+{
+    return make_int3(b - a.x, b - a.y, b - a.z);
+}
+inline __host__ __device__ void operator-=(int3 &a, int b)
+{
+    a.x -= b; a.y -= b; a.z -= b;
+}
+
+inline __host__ __device__ uint3 operator-(uint3 a, uint3 b)
+{
+    return make_uint3(a.x - b.x, a.y - b.y, a.z - b.z);
+}
+inline __host__ __device__ void operator-=(uint3 &a, uint3 b)
+{
+    a.x -= b.x; a.y -= b.y; a.z -= b.z;
+}
+inline __host__ __device__ uint3 operator-(uint3 a, uint b)
+{
+    return make_uint3(a.x - b, a.y - b, a.z - b);
+}
+inline __host__ __device__ uint3 operator-(uint b, uint3 a)
+{
+    return make_uint3(b - a.x, b - a.y, b - a.z);
+}
+inline __host__ __device__ void operator-=(uint3 &a, uint b)
+{
+    a.x -= b; a.y -= b; a.z -= b;
+}
+
+inline __host__ __device__ float4 operator-(float4 a, float4 b)
+{
+    return make_float4(a.x - b.x, a.y - b.y, a.z - b.z,  a.w - b.w);
+}
+inline __host__ __device__ void operator-=(float4 &a, float4 b)
+{
+    a.x -= b.x; a.y -= b.y; a.z -= b.z; a.w -= b.w;
+}
+inline __host__ __device__ float4 operator-(float4 a, float b)
+{
+    return make_float4(a.x - b, a.y - b, a.z - b,  a.w - b);
+}
+inline __host__ __device__ void operator-=(float4 &a, float b)
+{
+    a.x -= b; a.y -= b; a.z -= b; a.w -= b;
+}
+
+inline __host__ __device__ int4 operator-(int4 a, int4 b)
+{
+    return make_int4(a.x - b.x, a.y - b.y, a.z - b.z,  a.w - b.w);
+}
+inline __host__ __device__ void operator-=(int4 &a, int4 b)
+{
+    a.x -= b.x; a.y -= b.y; a.z -= b.z; a.w -= b.w;
+}
+inline __host__ __device__ int4 operator-(int4 a, int b)
+{
+    return make_int4(a.x - b, a.y - b, a.z - b,  a.w - b);
+}
+inline __host__ __device__ int4 operator-(int b, int4 a)
+{
+    return make_int4(b - a.x, b - a.y, b - a.z, b - a.w);
+}
+inline __host__ __device__ void operator-=(int4 &a, int b)
+{
+    a.x -= b; a.y -= b; a.z -= b; a.w -= b;
+}
+
+inline __host__ __device__ uint4 operator-(uint4 a, uint4 b)
+{
+    return make_uint4(a.x - b.x, a.y - b.y, a.z - b.z,  a.w - b.w);
+}
+inline __host__ __device__ void operator-=(uint4 &a, uint4 b)
+{
+    a.x -= b.x; a.y -= b.y; a.z -= b.z; a.w -= b.w;
+}
+inline __host__ __device__ uint4 operator-(uint4 a, uint b)
+{
+    return make_uint4(a.x - b, a.y - b, a.z - b,  a.w - b);
+}
+inline __host__ __device__ uint4 operator-(uint b, uint4 a)
+{
+    return make_uint4(b - a.x, b - a.y, b - a.z, b - a.w);
+}
+inline __host__ __device__ void operator-=(uint4 &a, uint b)
+{
+    a.x -= b; a.y -= b; a.z -= b; a.w -= b;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+// multiply
+////////////////////////////////////////////////////////////////////////////////
+
+inline __host__ __device__ float2 operator*(float2 a, float2 b)
+{
+    return make_float2(a.x * b.x, a.y * b.y);
+}
+inline __host__ __device__ void operator*=(float2 &a, float2 b)
+{
+    a.x *= b.x; a.y *= b.y;
+}
+inline __host__ __device__ float2 operator*(float2 a, float b)
+{
+    return make_float2(a.x * b, a.y * b);
+}
+inline __host__ __device__ float2 operator*(float b, float2 a)
+{
+    return make_float2(b * a.x, b * a.y);
+}
+inline __host__ __device__ void operator*=(float2 &a, float b)
+{
+    a.x *= b; a.y *= b;
+}
+
+inline __host__ __device__ int2 operator*(int2 a, int2 b)
+{
+    return make_int2(a.x * b.x, a.y * b.y);
+}
+inline __host__ __device__ void operator*=(int2 &a, int2 b)
+{
+    a.x *= b.x; a.y *= b.y;
+}
+inline __host__ __device__ int2 operator*(int2 a, int b)
+{
+    return make_int2(a.x * b, a.y * b);
+}
+inline __host__ __device__ int2 operator*(int b, int2 a)
+{
+    return make_int2(b * a.x, b * a.y);
+}
+inline __host__ __device__ void operator*=(int2 &a, int b)
+{
+    a.x *= b; a.y *= b;
+}
+
+inline __host__ __device__ uint2 operator*(uint2 a, uint2 b)
+{
+    return make_uint2(a.x * b.x, a.y * b.y);
+}
+inline __host__ __device__ void operator*=(uint2 &a, uint2 b)
+{
+    a.x *= b.x; a.y *= b.y;
+}
+inline __host__ __device__ uint2 operator*(uint2 a, uint b)
+{
+    return make_uint2(a.x * b, a.y * b);
+}
+inline __host__ __device__ uint2 operator*(uint b, uint2 a)
+{
+    return make_uint2(b * a.x, b * a.y);
+}
+inline __host__ __device__ void operator*=(uint2 &a, uint b)
+{
+    a.x *= b; a.y *= b;
+}
+
+inline __host__ __device__ float3 operator*(float3 a, float3 b)
+{
+    return make_float3(a.x * b.x, a.y * b.y, a.z * b.z);
+}
+inline __host__ __device__ void operator*=(float3 &a, float3 b)
+{
+    a.x *= b.x; a.y *= b.y; a.z *= b.z;
+}
+inline __host__ __device__ float3 operator*(float3 a, float b)
+{
+    return make_float3(a.x * b, a.y * b, a.z * b);
+}
+inline __host__ __device__ float3 operator*(float b, float3 a)
+{
+    return make_float3(b * a.x, b * a.y, b * a.z);
+}
+inline __host__ __device__ void operator*=(float3 &a, float b)
+{
+    a.x *= b; a.y *= b; a.z *= b;
+}
+
+inline __host__ __device__ int3 operator*(int3 a, int3 b)
+{
+    return make_int3(a.x * b.x, a.y * b.y, a.z * b.z);
+}
+inline __host__ __device__ void operator*=(int3 &a, int3 b)
+{
+    a.x *= b.x; a.y *= b.y; a.z *= b.z;
+}
+inline __host__ __device__ int3 operator*(int3 a, int b)
+{
+    return make_int3(a.x * b, a.y * b, a.z * b);
+}
+inline __host__ __device__ int3 operator*(int b, int3 a)
+{
+    return make_int3(b * a.x, b * a.y, b * a.z);
+}
+inline __host__ __device__ void operator*=(int3 &a, int b)
+{
+    a.x *= b; a.y *= b; a.z *= b;
+}
+
+inline __host__ __device__ uint3 operator*(uint3 a, uint3 b)
+{
+    return make_uint3(a.x * b.x, a.y * b.y, a.z * b.z);
+}
+inline __host__ __device__ void operator*=(uint3 &a, uint3 b)
+{
+    a.x *= b.x; a.y *= b.y; a.z *= b.z;
+}
+inline __host__ __device__ uint3 operator*(uint3 a, uint b)
+{
+    return make_uint3(a.x * b, a.y * b, a.z * b);
+}
+inline __host__ __device__ uint3 operator*(uint b, uint3 a)
+{
+    return make_uint3(b * a.x, b * a.y, b * a.z);
+}
+inline __host__ __device__ void operator*=(uint3 &a, uint b)
+{
+    a.x *= b; a.y *= b; a.z *= b;
+}
+
+inline __host__ __device__ float4 operator*(float4 a, float4 b)
+{
+    return make_float4(a.x * b.x, a.y * b.y, a.z * b.z,  a.w * b.w);
+}
+inline __host__ __device__ void operator*=(float4 &a, float4 b)
+{
+    a.x *= b.x; a.y *= b.y; a.z *= b.z; a.w *= b.w;
+}
+inline __host__ __device__ float4 operator*(float4 a, float b)
+{
+    return make_float4(a.x * b, a.y * b, a.z * b,  a.w * b);
+}
+inline __host__ __device__ float4 operator*(float b, float4 a)
+{
+    return make_float4(b * a.x, b * a.y, b * a.z, b * a.w);
+}
+inline __host__ __device__ void operator*=(float4 &a, float b)
+{
+    a.x *= b; a.y *= b; a.z *= b; a.w *= b;
+}
+
+inline __host__ __device__ int4 operator*(int4 a, int4 b)
+{
+    return make_int4(a.x * b.x, a.y * b.y, a.z * b.z,  a.w * b.w);
+}
+inline __host__ __device__ void operator*=(int4 &a, int4 b)
+{
+    a.x *= b.x; a.y *= b.y; a.z *= b.z; a.w *= b.w;
+}
+inline __host__ __device__ int4 operator*(int4 a, int b)
+{
+    return make_int4(a.x * b, a.y * b, a.z * b,  a.w * b);
+}
+inline __host__ __device__ int4 operator*(int b, int4 a)
+{
+    return make_int4(b * a.x, b * a.y, b * a.z, b * a.w);
+}
+inline __host__ __device__ void operator*=(int4 &a, int b)
+{
+    a.x *= b; a.y *= b; a.z *= b; a.w *= b;
+}
+
+inline __host__ __device__ uint4 operator*(uint4 a, uint4 b)
+{
+    return make_uint4(a.x * b.x, a.y * b.y, a.z * b.z,  a.w * b.w);
+}
+inline __host__ __device__ void operator*=(uint4 &a, uint4 b)
+{
+    a.x *= b.x; a.y *= b.y; a.z *= b.z; a.w *= b.w;
+}
+inline __host__ __device__ uint4 operator*(uint4 a, uint b)
+{
+    return make_uint4(a.x * b, a.y * b, a.z * b,  a.w * b);
+}
+inline __host__ __device__ uint4 operator*(uint b, uint4 a)
+{
+    return make_uint4(b * a.x, b * a.y, b * a.z, b * a.w);
+}
+inline __host__ __device__ void operator*=(uint4 &a, uint b)
+{
+    a.x *= b; a.y *= b; a.z *= b; a.w *= b;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+// divide
+////////////////////////////////////////////////////////////////////////////////
+
+inline __host__ __device__ float2 operator/(float2 a, float2 b)
+{
+    return make_float2(a.x / b.x, a.y / b.y);
+}
+inline __host__ __device__ void operator/=(float2 &a, float2 b)
+{
+    a.x /= b.x; a.y /= b.y;
+}
+inline __host__ __device__ float2 operator/(float2 a, float b)
+{
+    return make_float2(a.x / b, a.y / b);
+}
+inline __host__ __device__ void operator/=(float2 &a, float b)
+{
+    a.x /= b; a.y /= b;
+}
+inline __host__ __device__ float2 operator/(float b, float2 a)
+{
+    return make_float2(b / a.x, b / a.y);
+}
+
+inline __host__ __device__ float3 operator/(float3 a, float3 b)
+{
+    return make_float3(a.x / b.x, a.y / b.y, a.z / b.z);
+}
+inline __host__ __device__ void operator/=(float3 &a, float3 b)
+{
+    a.x /= b.x; a.y /= b.y; a.z /= b.z;
+}
+inline __host__ __device__ float3 operator/(float3 a, float b)
+{
+    return make_float3(a.x / b, a.y / b, a.z / b);
+}
+inline __host__ __device__ void operator/=(float3 &a, float b)
+{
+    a.x /= b; a.y /= b; a.z /= b;
+}
+inline __host__ __device__ float3 operator/(float b, float3 a)
+{
+    return make_float3(b / a.x, b / a.y, b / a.z);
+}
+
+inline __host__ __device__ float4 operator/(float4 a, float4 b)
+{
+    return make_float4(a.x / b.x, a.y / b.y, a.z / b.z,  a.w / b.w);
+}
+inline __host__ __device__ void operator/=(float4 &a, float4 b)
+{
+    a.x /= b.x; a.y /= b.y; a.z /= b.z; a.w /= b.w;
+}
+inline __host__ __device__ float4 operator/(float4 a, float b)
+{
+    return make_float4(a.x / b, a.y / b, a.z / b,  a.w / b);
+}
+inline __host__ __device__ void operator/=(float4 &a, float b)
+{
+    a.x /= b; a.y /= b; a.z /= b; a.w /= b;
+}
+inline __host__ __device__ float4 operator/(float b, float4 a){
+    return make_float4(b / a.x, b / a.y, b / a.z, b / a.w);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+// min
+////////////////////////////////////////////////////////////////////////////////
+
+inline  __host__ __device__ float2 fminf(float2 a, float2 b)
+{
+	return make_float2(fminf(a.x,b.x), fminf(a.y,b.y));
+}
+inline __host__ __device__ float3 fminf(float3 a, float3 b)
+{
+	return make_float3(fminf(a.x,b.x), fminf(a.y,b.y), fminf(a.z,b.z));
+}
+inline  __host__ __device__ float4 fminf(float4 a, float4 b)
+{
+	return make_float4(fminf(a.x,b.x), fminf(a.y,b.y), fminf(a.z,b.z), fminf(a.w,b.w));
+}
+
+inline __host__ __device__ int2 min(int2 a, int2 b)
+{
+    return make_int2(min(a.x,b.x), min(a.y,b.y));
+}
+inline __host__ __device__ int3 min(int3 a, int3 b)
+{
+    return make_int3(min(a.x,b.x), min(a.y,b.y), min(a.z,b.z));
+}
+inline __host__ __device__ int4 min(int4 a, int4 b)
+{
+    return make_int4(min(a.x,b.x), min(a.y,b.y), min(a.z,b.z), min(a.w,b.w));
+}
+
+inline __host__ __device__ uint2 min(uint2 a, uint2 b)
+{
+    return make_uint2(min(a.x,b.x), min(a.y,b.y));
+}
+inline __host__ __device__ uint3 min(uint3 a, uint3 b)
+{
+    return make_uint3(min(a.x,b.x), min(a.y,b.y), min(a.z,b.z));
+}
+inline __host__ __device__ uint4 min(uint4 a, uint4 b)
+{
+    return make_uint4(min(a.x,b.x), min(a.y,b.y), min(a.z,b.z), min(a.w,b.w));
+}
+
+////////////////////////////////////////////////////////////////////////////////
+// max
+////////////////////////////////////////////////////////////////////////////////
+
+inline __host__ __device__ float2 fmaxf(float2 a, float2 b)
+{
+	return make_float2(fmaxf(a.x,b.x), fmaxf(a.y,b.y));
+}
+inline __host__ __device__ float3 fmaxf(float3 a, float3 b)
+{
+	return make_float3(fmaxf(a.x,b.x), fmaxf(a.y,b.y), fmaxf(a.z,b.z));
+}
+inline __host__ __device__ float4 fmaxf(float4 a, float4 b)
+{
+	return make_float4(fmaxf(a.x,b.x), fmaxf(a.y,b.y), fmaxf(a.z,b.z), fmaxf(a.w,b.w));
+}
+
+inline __host__ __device__ int2 max(int2 a, int2 b)
+{
+    return make_int2(max(a.x,b.x), max(a.y,b.y));
+}
+inline __host__ __device__ int3 max(int3 a, int3 b)
+{
+    return make_int3(max(a.x,b.x), max(a.y,b.y), max(a.z,b.z));
+}
+inline __host__ __device__ int4 max(int4 a, int4 b)
+{
+    return make_int4(max(a.x,b.x), max(a.y,b.y), max(a.z,b.z), max(a.w,b.w));
+}
+
+inline __host__ __device__ uint2 max(uint2 a, uint2 b)
+{
+    return make_uint2(max(a.x,b.x), max(a.y,b.y));
+}
+inline __host__ __device__ uint3 max(uint3 a, uint3 b)
+{
+    return make_uint3(max(a.x,b.x), max(a.y,b.y), max(a.z,b.z));
+}
+inline __host__ __device__ uint4 max(uint4 a, uint4 b)
+{
+    return make_uint4(max(a.x,b.x), max(a.y,b.y), max(a.z,b.z), max(a.w,b.w));
+}
+
+////////////////////////////////////////////////////////////////////////////////
+// lerp
+// - linear interpolation between a and b, based on value t in [0, 1] range
+////////////////////////////////////////////////////////////////////////////////
+
+inline __device__ __host__ float lerp(float a, float b, float t)
+{
+    return a + t*(b-a);
+}
+inline __device__ __host__ float2 lerp(float2 a, float2 b, float t)
+{
+    return a + t*(b-a);
+}
+inline __device__ __host__ float3 lerp(float3 a, float3 b, float t)
+{
+    return a + t*(b-a);
+}
+inline __device__ __host__ float4 lerp(float4 a, float4 b, float t)
+{
+    return a + t*(b-a);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+// clamp
+// - clamp the value v to be in the range [a, b]
+////////////////////////////////////////////////////////////////////////////////
+
+inline __device__ __host__ float clamp(float f, float a, float b)
+{
+    return fmaxf(a, fminf(f, b));
+}
+inline __device__ __host__ int clamp(int f, int a, int b)
+{
+    return max(a, min(f, b));
+}
+inline __device__ __host__ uint clamp(uint f, uint a, uint b)
+{
+    return max(a, min(f, b));
+}
+
+inline __device__ __host__ float2 clamp(float2 v, float a, float b)
+{
+    return make_float2(clamp(v.x, a, b), clamp(v.y, a, b));
+}
+inline __device__ __host__ float2 clamp(float2 v, float2 a, float2 b)
+{
+    return make_float2(clamp(v.x, a.x, b.x), clamp(v.y, a.y, b.y));
+}
+inline __device__ __host__ float3 clamp(float3 v, float a, float b)
+{
+    return make_float3(clamp(v.x, a, b), clamp(v.y, a, b), clamp(v.z, a, b));
+}
+inline __device__ __host__ float3 clamp(float3 v, float3 a, float3 b)
+{
+    return make_float3(clamp(v.x, a.x, b.x), clamp(v.y, a.y, b.y), clamp(v.z, a.z, b.z));
+}
+inline __device__ __host__ float4 clamp(float4 v, float a, float b)
+{
+    return make_float4(clamp(v.x, a, b), clamp(v.y, a, b), clamp(v.z, a, b), clamp(v.w, a, b));
+}
+inline __device__ __host__ float4 clamp(float4 v, float4 a, float4 b)
+{
+    return make_float4(clamp(v.x, a.x, b.x), clamp(v.y, a.y, b.y), clamp(v.z, a.z, b.z), clamp(v.w, a.w, b.w));
+}
+
+inline __device__ __host__ int2 clamp(int2 v, int a, int b)
+{
+    return make_int2(clamp(v.x, a, b), clamp(v.y, a, b));
+}
+inline __device__ __host__ int2 clamp(int2 v, int2 a, int2 b)
+{
+    return make_int2(clamp(v.x, a.x, b.x), clamp(v.y, a.y, b.y));
+}
+inline __device__ __host__ int3 clamp(int3 v, int a, int b)
+{
+    return make_int3(clamp(v.x, a, b), clamp(v.y, a, b), clamp(v.z, a, b));
+}
+inline __device__ __host__ int3 clamp(int3 v, int3 a, int3 b)
+{
+    return make_int3(clamp(v.x, a.x, b.x), clamp(v.y, a.y, b.y), clamp(v.z, a.z, b.z));
+}
+inline __device__ __host__ int4 clamp(int4 v, int a, int b)
+{
+    return make_int4(clamp(v.x, a, b), clamp(v.y, a, b), clamp(v.z, a, b), clamp(v.w, a, b));
+}
+inline __device__ __host__ int4 clamp(int4 v, int4 a, int4 b)
+{
+    return make_int4(clamp(v.x, a.x, b.x), clamp(v.y, a.y, b.y), clamp(v.z, a.z, b.z), clamp(v.w, a.w, b.w));
+}
+
+inline __device__ __host__ uint2 clamp(uint2 v, uint a, uint b)
+{
+    return make_uint2(clamp(v.x, a, b), clamp(v.y, a, b));
+}
+inline __device__ __host__ uint2 clamp(uint2 v, uint2 a, uint2 b)
+{
+    return make_uint2(clamp(v.x, a.x, b.x), clamp(v.y, a.y, b.y));
+}
+inline __device__ __host__ uint3 clamp(uint3 v, uint a, uint b)
+{
+    return make_uint3(clamp(v.x, a, b), clamp(v.y, a, b), clamp(v.z, a, b));
+}
+inline __device__ __host__ uint3 clamp(uint3 v, uint3 a, uint3 b)
+{
+    return make_uint3(clamp(v.x, a.x, b.x), clamp(v.y, a.y, b.y), clamp(v.z, a.z, b.z));
+}
+inline __device__ __host__ uint4 clamp(uint4 v, uint a, uint b)
+{
+    return make_uint4(clamp(v.x, a, b), clamp(v.y, a, b), clamp(v.z, a, b), clamp(v.w, a, b));
+}
+inline __device__ __host__ uint4 clamp(uint4 v, uint4 a, uint4 b)
+{
+    return make_uint4(clamp(v.x, a.x, b.x), clamp(v.y, a.y, b.y), clamp(v.z, a.z, b.z), clamp(v.w, a.w, b.w));
+}
+
+////////////////////////////////////////////////////////////////////////////////
+// dot product
+////////////////////////////////////////////////////////////////////////////////
+
+inline __host__ __device__ float dot(float2 a, float2 b)
+{ 
+    return a.x * b.x + a.y * b.y;
+}
+inline __host__ __device__ float dot(float3 a, float3 b)
+{ 
+    return a.x * b.x + a.y * b.y + a.z * b.z;
+}
+inline __host__ __device__ float dot(float4 a, float4 b)
+{ 
+    return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
+}
+
+inline __host__ __device__ int dot(int2 a, int2 b)
+{ 
+    return a.x * b.x + a.y * b.y;
+}
+inline __host__ __device__ int dot(int3 a, int3 b)
+{ 
+    return a.x * b.x + a.y * b.y + a.z * b.z;
+}
+inline __host__ __device__ int dot(int4 a, int4 b)
+{ 
+    return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
+}
+
+inline __host__ __device__ uint dot(uint2 a, uint2 b)
+{ 
+    return a.x * b.x + a.y * b.y;
+}
+inline __host__ __device__ uint dot(uint3 a, uint3 b)
+{ 
+    return a.x * b.x + a.y * b.y + a.z * b.z;
+}
+inline __host__ __device__ uint dot(uint4 a, uint4 b)
+{ 
+    return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+// length
+////////////////////////////////////////////////////////////////////////////////
+
+inline __host__ __device__ float length(float2 v)
+{
+    return sqrtf(dot(v, v));
+}
+inline __host__ __device__ float length(float3 v)
+{
+    return sqrtf(dot(v, v));
+}
+inline __host__ __device__ float length(float4 v)
+{
+    return sqrtf(dot(v, v));
+}
+
+////////////////////////////////////////////////////////////////////////////////
+// normalize
+////////////////////////////////////////////////////////////////////////////////
+
+inline __host__ __device__ float2 normalize(float2 v)
+{
+    float invLen = rsqrtf(dot(v, v));
+    return v * invLen;
+}
+inline __host__ __device__ float3 normalize(float3 v)
+{
+    float invLen = rsqrtf(dot(v, v));
+    return v * invLen;
+}
+inline __host__ __device__ float4 normalize(float4 v)
+{
+    float invLen = rsqrtf(dot(v, v));
+    return v * invLen;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+// floor
+////////////////////////////////////////////////////////////////////////////////
+
+inline __host__ __device__ float2 floorf(float2 v)
+{
+    return make_float2(floorf(v.x), floorf(v.y));
+}
+inline __host__ __device__ float3 floorf(float3 v)
+{
+    return make_float3(floorf(v.x), floorf(v.y), floorf(v.z));
+}
+inline __host__ __device__ float4 floorf(float4 v)
+{
+    return make_float4(floorf(v.x), floorf(v.y), floorf(v.z), floorf(v.w));
+}
+
+////////////////////////////////////////////////////////////////////////////////
+// frac - returns the fractional portion of a scalar or each vector component
+////////////////////////////////////////////////////////////////////////////////
+
+inline __host__ __device__ float fracf(float v)
+{
+    return v - floorf(v);
+}
+inline __host__ __device__ float2 fracf(float2 v)
+{
+    return make_float2(fracf(v.x), fracf(v.y));
+}
+inline __host__ __device__ float3 fracf(float3 v)
+{
+    return make_float3(fracf(v.x), fracf(v.y), fracf(v.z));
+}
+inline __host__ __device__ float4 fracf(float4 v)
+{
+    return make_float4(fracf(v.x), fracf(v.y), fracf(v.z), fracf(v.w));
+}
+
+////////////////////////////////////////////////////////////////////////////////
+// fmod
+////////////////////////////////////////////////////////////////////////////////
+
+inline __host__ __device__ float2 fmodf(float2 a, float2 b)
+{
+    return make_float2(fmodf(a.x, b.x), fmodf(a.y, b.y));
+}
+inline __host__ __device__ float3 fmodf(float3 a, float3 b)
+{
+    return make_float3(fmodf(a.x, b.x), fmodf(a.y, b.y), fmodf(a.z, b.z));
+}
+inline __host__ __device__ float4 fmodf(float4 a, float4 b)
+{
+    return make_float4(fmodf(a.x, b.x), fmodf(a.y, b.y), fmodf(a.z, b.z), fmodf(a.w, b.w));
+}
+
+////////////////////////////////////////////////////////////////////////////////
+// absolute value
+////////////////////////////////////////////////////////////////////////////////
+
+inline __host__ __device__ float2 fabs(float2 v)
+{
+	return make_float2(fabs(v.x), fabs(v.y));
+}
+inline __host__ __device__ float3 fabs(float3 v)
+{
+	return make_float3(fabs(v.x), fabs(v.y), fabs(v.z));
+}
+inline __host__ __device__ float4 fabs(float4 v)
+{
+	return make_float4(fabs(v.x), fabs(v.y), fabs(v.z), fabs(v.w));
+}
+
+inline __host__ __device__ int2 abs(int2 v)
+{
+	return make_int2(abs(v.x), abs(v.y));
+}
+inline __host__ __device__ int3 abs(int3 v)
+{
+	return make_int3(abs(v.x), abs(v.y), abs(v.z));
+}
+inline __host__ __device__ int4 abs(int4 v)
+{
+	return make_int4(abs(v.x), abs(v.y), abs(v.z), abs(v.w));
+}
+
+////////////////////////////////////////////////////////////////////////////////
+// reflect
+// - returns reflection of incident ray I around surface normal N
+// - N should be normalized, reflected vector's length is equal to length of I
+////////////////////////////////////////////////////////////////////////////////
+
+inline __host__ __device__ float3 reflect(float3 i, float3 n)
+{
+	return i - 2.0f * n * dot(n,i);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+// cross product
+////////////////////////////////////////////////////////////////////////////////
+
+inline __host__ __device__ float3 cross(float3 a, float3 b)
+{ 
+    return make_float3(a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x); 
+}
+
+////////////////////////////////////////////////////////////////////////////////
+// smoothstep
+// - returns 0 if x < a
+// - returns 1 if x > b
+// - otherwise returns smooth interpolation between 0 and 1 based on x
+////////////////////////////////////////////////////////////////////////////////
+
+inline __device__ __host__ float smoothstep(float a, float b, float x)
+{
+	float y = clamp((x - a) / (b - a), 0.0f, 1.0f);
+	return (y*y*(3.0f - (2.0f*y)));
+}
+inline __device__ __host__ float2 smoothstep(float2 a, float2 b, float2 x)
+{
+	float2 y = clamp((x - a) / (b - a), 0.0f, 1.0f);
+	return (y*y*(make_float2(3.0f) - (make_float2(2.0f)*y)));
+}
+inline __device__ __host__ float3 smoothstep(float3 a, float3 b, float3 x)
+{
+	float3 y = clamp((x - a) / (b - a), 0.0f, 1.0f);
+	return (y*y*(make_float3(3.0f) - (make_float3(2.0f)*y)));
+}
+inline __device__ __host__ float4 smoothstep(float4 a, float4 b, float4 x)
+{
+	float4 y = clamp((x - a) / (b - a), 0.0f, 1.0f);
+	return (y*y*(make_float4(3.0f) - (make_float4(2.0f)*y)));
+}
+
+#endif
diff --git a/src/cpp/flann/util/dynamic_bitset.h b/src/cpp/flann/util/dynamic_bitset.h
new file mode 100644
index 0000000..f5812fc
--- /dev/null
+++ b/src/cpp/flann/util/dynamic_bitset.h
@@ -0,0 +1,163 @@
+/***********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2008-2009  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+ * Copyright 2008-2009  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+ *
+ * THE BSD LICENSE
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+
+/***********************************************************************
+ * Author: Vincent Rabaud
+ *************************************************************************/
+
+#ifndef FLANN_DYNAMIC_BITSET_H_
+#define FLANN_DYNAMIC_BITSET_H_
+
+//#define FLANN_USE_BOOST 1
+#if FLANN_USE_BOOST
+#include <boost/dynamic_bitset.hpp>
+typedef boost::dynamic_bitset<> DynamicBitset;
+#else
+
+#include <limits.h>
+
+namespace flann {
+
+/** Class re-implementing the boost version of it
+ * This helps not depending on boost, it also does not do the bound checks
+ * and has a way to reset a block for speed
+ */
+class DynamicBitset
+{
+public:
+    /** @param default constructor
+     */
+    DynamicBitset() : size_(0)
+    {
+    }
+
+    /** @param only constructor we use in our code
+     * @param the size of the bitset (in bits)
+     */
+    DynamicBitset(size_t size)
+    {
+        resize(size);
+        reset();
+    }
+
+    /** Sets all the bits to 0
+     */
+    void clear()
+    {
+        std::fill(bitset_.begin(), bitset_.end(), 0);
+    }
+
+    /** @brief checks if the bitset is empty
+     * @return true if the bitset is empty
+     */
+    bool empty() const
+    {
+        return bitset_.empty();
+    }
+
+    /** @param set all the bits to 0
+     */
+    void reset()
+    {
+        std::fill(bitset_.begin(), bitset_.end(), 0);
+    }
+
+    /** @brief set one bit to 0
+     * @param
+     */
+    void reset(size_t index)
+    {
+        bitset_[index / cell_bit_size_] &= ~(size_t(1) << (index % cell_bit_size_));
+    }
+
+    /** @brief sets a specific bit to 0, and more bits too
+     * This function is useful when resetting a given set of bits so that the
+     * whole bitset ends up being 0: if that's the case, we don't care about setting
+     * other bits to 0
+     * @param
+     */
+    void reset_block(size_t index)
+    {
+        bitset_[index / cell_bit_size_] = 0;
+    }
+
+    /** @param resize the bitset so that it contains at least size bits
+     * @param size
+     */
+    void resize(size_t size)
+    {
+        size_ = size;
+        bitset_.resize(size / cell_bit_size_ + 1);
+    }
+
+    /** @param set a bit to true
+     * @param index the index of the bit to set to 1
+     */
+    void set(size_t index)
+    {
+        bitset_[index / cell_bit_size_] |= size_t(1) << (index % cell_bit_size_);
+    }
+
+    /** @param gives the number of contained bits
+     */
+    size_t size() const
+    {
+        return size_;
+    }
+
+    /** @param check if a bit is set
+     * @param index the index of the bit to check
+     * @return true if the bit is set
+     */
+    bool test(size_t index) const
+    {
+        return (bitset_[index / cell_bit_size_] & (size_t(1) << (index % cell_bit_size_))) != 0;
+    }
+
+private:
+    template <typename Archive>
+    void serialize(Archive& ar)
+    {
+    	ar & size_;
+    	ar & bitset_;
+    }
+    friend struct serialization::access;
+
+private:
+    std::vector<size_t> bitset_;
+    size_t size_;
+    static const unsigned int cell_bit_size_ = CHAR_BIT * sizeof(size_t);
+};
+
+} // namespace flann
+
+#endif
+
+#endif // FLANN_DYNAMIC_BITSET_H_
diff --git a/src/cpp/flann/util/heap.h b/src/cpp/flann/util/heap.h
new file mode 100644
index 0000000..d54a99c
--- /dev/null
+++ b/src/cpp/flann/util/heap.h
@@ -0,0 +1,456 @@
+/***********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2008-2009  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+ * Copyright 2008-2009  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+ *
+ * THE BSD LICENSE
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+
+#ifndef FLANN_HEAP_H_
+#define FLANN_HEAP_H_
+
+#include <algorithm>
+#include <vector>
+
+namespace flann
+{
+
+/**
+ * Priority Queue Implementation
+ *
+ * The priority queue is implemented with a heap.  A heap is a complete
+ * (full) binary tree in which each parent is less than both of its
+ * children, but the order of the children is unspecified.
+ */
+template <typename T>
+class Heap
+{
+
+    /**
+     * Storage array for the heap.
+     * Type T must be comparable.
+     */
+    std::vector<T> heap;
+    int length;
+
+    /**
+     * Number of element in the heap
+     */
+    int count;
+
+
+
+public:
+    /**
+     * Constructor.
+     *
+     * Params:
+     *     size = heap size
+     */
+
+    Heap(int size)
+    {
+        length = size;
+        heap.reserve(length);
+        count = 0;
+    }
+
+    /**
+     *
+     * Returns: heap size
+     */
+    int size()
+    {
+        return count;
+    }
+
+    /**
+     * Tests if the heap is empty
+     *
+     * Returns: true is heap empty, false otherwise
+     */
+    bool empty()
+    {
+        return size()==0;
+    }
+
+    /**
+     * Clears the heap.
+     */
+    void clear()
+    {
+        heap.clear();
+        count = 0;
+    }
+
+    struct CompareT : public std::binary_function<T,T,bool>
+    {
+        bool operator()(const T& t_1, const T& t_2) const
+        {
+            return t_2 < t_1;
+        }
+    };
+
+    /**
+     * Insert a new element in the heap.
+     *
+     * We select the next empty leaf node, and then keep moving any larger
+     * parents down until the right location is found to store this element.
+     *
+     * Params:
+     *     value = the new element to be inserted in the heap
+     */
+    void insert(const T& value)
+    {
+        /* If heap is full, then return without adding this element. */
+        if (count == length) {
+            return;
+        }
+
+        heap.push_back(value);
+        static CompareT compareT;
+        std::push_heap(heap.begin(), heap.end(), compareT);
+        ++count;
+    }
+
+
+
+    /**
+     * Returns the node of minimum value from the heap (top of the heap).
+     *
+     * Params:
+     *     value = out parameter used to return the min element
+     * Returns: false if heap empty
+     */
+    bool popMin(T& value)
+    {
+        if (count == 0) {
+            return false;
+        }
+
+        value = heap[0];
+        static CompareT compareT;
+        std::pop_heap(heap.begin(), heap.end(), compareT);
+        heap.pop_back();
+        --count;
+
+        return true;  /* Return old last node. */
+    }
+};
+
+
+template <typename T>
+class IntervalHeap
+{
+	struct Interval
+	{
+		T left;
+		T right;
+	};
+
+    /**
+     * Storage array for the heap.
+     * Type T must be comparable.
+     */
+    std::vector<Interval> heap;
+    size_t capacity_;
+    size_t size_;
+
+public:
+    /**
+     * Constructor.
+     *
+     * Params:
+     *     size = heap size
+     */
+
+    IntervalHeap(int capacity) : capacity_(capacity), size_(0)
+    {
+        heap.resize(capacity/2 + capacity%2 + 1); // 1-based indexing
+    }
+
+    /**
+     * @return Heap size
+     */
+    size_t size()
+    {
+        return size_;
+    }
+
+    /**
+     * Tests if the heap is empty
+     * @return true is heap empty, false otherwise
+     */
+    bool empty()
+    {
+        return size_==0;
+    }
+
+    /**
+     * Clears the heap.
+     */
+    void clear()
+    {
+        size_ = 0;
+    }
+
+    void insert(const T& value)
+    {
+        /* If heap is full, then return without adding this element. */
+        if (size_ == capacity_) {
+            return;
+        }
+
+        // insert into the root
+        if (size_<2) {
+        	if (size_==0) {
+        		heap[1].left = value;
+        		heap[1].right = value;
+        	}
+        	else {
+        		if (value<heap[1].left) {
+        			heap[1].left = value;
+        		}
+        		else {
+        			heap[1].right = value;
+        		}
+        	}
+        	++size_;
+        	return;
+        }
+
+        size_t last_pos = size_/2 + size_%2;
+        bool min_heap;
+
+        if (size_%2) { // odd number of elements
+        	min_heap = (value<heap[last_pos].left)? true : false;
+        }
+        else {
+        	++last_pos;
+        	min_heap = (value<heap[last_pos/2].left)? true : false;
+        }
+
+        if (min_heap) {
+        	size_t pos = last_pos;
+        	size_t par = pos/2;
+        	while (pos>1 && value < heap[par].left) {
+        		heap[pos].left = heap[par].left;
+        		pos = par;
+        		par = pos/2;
+        	}
+        	heap[pos].left = value;
+        	++size_;
+
+        	if (size_%2) { // duplicate element in last position if size is odd
+        		heap[last_pos].right = heap[last_pos].left;
+        	}
+        }
+        else {
+        	size_t pos = last_pos;
+        	size_t par = pos/2;
+        	while (pos>1 && heap[par].right < value) {
+        		heap[pos].right = heap[par].right;
+        		pos = par;
+        		par = pos/2;
+        	}
+        	heap[pos].right = value;
+        	++size_;
+
+        	if (size_%2) { // duplicate element in last position if size is odd
+        		heap[last_pos].left = heap[last_pos].right;
+        	}
+        }
+    }
+
+
+    /**
+     * Returns the node of minimum value from the heap
+     * @param value out parameter used to return the min element
+     * @return false if heap empty
+     */
+    bool popMin(T& value)
+    {
+        if (size_ == 0) {
+            return false;
+        }
+
+        value = heap[1].left;
+        size_t last_pos = size_/2 + size_%2;
+        T elem = heap[last_pos].left;
+
+        if (size_ % 2) { // odd number of elements
+        	--last_pos;
+        }
+        else {
+        	heap[last_pos].left = heap[last_pos].right;
+        }
+        --size_;
+        if (size_<2) return true;
+
+        size_t crt=1; // root node
+        size_t child = crt*2;
+
+        while (child <= last_pos) {
+        	if (child < last_pos && heap[child+1].left < heap[child].left) ++child; // pick the child with min
+
+        	if (!(heap[child].left<elem)) break;
+
+        	heap[crt].left = heap[child].left;
+        	if (heap[child].right<elem) {
+        		std::swap(elem, heap[child].right);
+        	}
+
+        	crt = child;
+    		child *= 2;
+        }
+        heap[crt].left = elem;
+        return true;
+    }
+
+
+    /**
+     * Returns the element of maximum value from the heap
+     * @param value
+     * @return false if heap empty
+     */
+    bool popMax(T& value)
+    {
+        if (size_ == 0) {
+            return false;
+        }
+
+        value = heap[1].right;
+        size_t last_pos = size_/2 + size_%2;
+        T elem = heap[last_pos].right;
+
+        if (size_%2) { // odd number of elements
+        	--last_pos;
+        }
+        else {
+        	heap[last_pos].right = heap[last_pos].left;
+        }
+        --size_;
+        if (size_<2) return true;
+
+        size_t crt=1; // root node
+        size_t child = crt*2;
+
+        while (child <= last_pos) {
+        	if (child < last_pos && heap[child].right < heap[child+1].right) ++child; // pick the child with max
+
+        	if (!(elem < heap[child].right)) break;
+
+        	heap[crt].right = heap[child].right;
+        	if (elem<heap[child].left) {
+        		std::swap(elem, heap[child].left);
+        	}
+
+        	crt = child;
+    		child *= 2;
+        }
+        heap[crt].right = elem;
+        return true;
+    }
+
+
+    bool getMin(T& value)
+    {
+    	if (size_==0) {
+    		return false;
+    	}
+    	value = heap[1].left;
+    	return true;
+    }
+
+
+    bool getMax(T& value)
+    {
+    	if (size_==0) {
+    		return false;
+    	}
+    	value = heap[1].right;
+    	return true;
+    }
+};
+
+
+template <typename T>
+class BoundedHeap
+{
+	IntervalHeap<T> interval_heap_;
+	size_t capacity_;
+public:
+	BoundedHeap(size_t capacity) : interval_heap_(capacity), capacity_(capacity)
+	{
+
+	}
+
+    /**
+     * Returns: heap size
+     */
+    int size()
+    {
+        return interval_heap_.size();
+    }
+
+    /**
+     * Tests if the heap is empty
+     * Returns: true is heap empty, false otherwise
+     */
+    bool empty()
+    {
+        return interval_heap_.empty();
+    }
+
+    /**
+     * Clears the heap.
+     */
+    void clear()
+    {
+    	interval_heap_.clear();
+    }
+
+    void insert(const T& value)
+    {
+    	if (interval_heap_.size()==capacity_) {
+    		T max;
+    		interval_heap_.getMax(max);
+    		if (max<value) return;
+   			interval_heap_.popMax(max);
+    	}
+    	interval_heap_.insert(value);
+    }
+
+    bool popMin(T& value)
+    {
+    	return interval_heap_.popMin(value);
+    }
+};
+
+
+
+}
+
+#endif //FLANN_HEAP_H_
diff --git a/src/cpp/flann/util/logger.h b/src/cpp/flann/util/logger.h
new file mode 100644
index 0000000..ef553d6
--- /dev/null
+++ b/src/cpp/flann/util/logger.h
@@ -0,0 +1,137 @@
+/***********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2008-2009  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+ * Copyright 2008-2009  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+ *
+ * THE BSD LICENSE
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+
+#ifndef FLANN_LOGGER_H
+#define FLANN_LOGGER_H
+
+#include <stdio.h>
+#include <stdarg.h>
+
+#include "flann/defines.h"
+
+
+namespace flann
+{
+
+class Logger
+{
+    Logger() : stream(stdout), logLevel(FLANN_LOG_WARN) {}
+
+    ~Logger()
+    {
+        if ((stream!=NULL)&&(stream!=stdout)) {
+            fclose(stream);
+        }
+    }
+
+    static Logger& instance()
+    {
+        static Logger logger;
+        return logger;
+    }
+
+    void _setDestination(const char* name)
+    {
+        if (name==NULL) {
+            stream = stdout;
+        }
+        else {
+            stream = fopen(name,"w");
+            if (stream == NULL) {
+                stream = stdout;
+            }
+        }
+    }
+
+    int _log(int level, const char* fmt, va_list arglist)
+    {
+        if (level > logLevel ) return -1;
+        int ret = vfprintf(stream, fmt, arglist);
+        return ret;
+    }
+
+public:
+    /**
+     * Sets the logging level. All messages with lower priority will be ignored.
+     * @param level Logging level
+     */
+    static void setLevel(int level) { instance().logLevel = level; }
+
+    /**
+     * Returns the currently set logging level.
+     * @return current logging level
+     */
+    static int getLevel() { return instance().logLevel; }
+
+    /**
+     * Sets the logging destination
+     * @param name Filename or NULL for console
+     */
+    static void setDestination(const char* name) { instance()._setDestination(name); }
+
+    /**
+     * Print log message
+     * @param level Log level
+     * @param fmt Message format
+     * @return
+     */
+    static int log(int level, const char* fmt, ...)
+    {
+        va_list arglist;
+        va_start(arglist, fmt);
+        int ret = instance()._log(level,fmt,arglist);
+        va_end(arglist);
+        return ret;
+    }
+
+#define LOG_METHOD(NAME,LEVEL) \
+    static int NAME(const char* fmt, ...) \
+    { \
+        va_list ap; \
+        va_start(ap, fmt); \
+        int ret = instance()._log(LEVEL, fmt, ap); \
+        va_end(ap); \
+        return ret; \
+    }
+
+    LOG_METHOD(fatal, FLANN_LOG_FATAL)
+    LOG_METHOD(error, FLANN_LOG_ERROR)
+    LOG_METHOD(warn, FLANN_LOG_WARN)
+    LOG_METHOD(info, FLANN_LOG_INFO)
+    LOG_METHOD(debug, FLANN_LOG_DEBUG)
+
+private:
+    FILE* stream;
+    int logLevel;
+};
+
+}
+
+#endif //FLANN_LOGGER_H
diff --git a/src/cpp/flann/util/lsh_table.h b/src/cpp/flann/util/lsh_table.h
new file mode 100644
index 0000000..8ed1764
--- /dev/null
+++ b/src/cpp/flann/util/lsh_table.h
@@ -0,0 +1,506 @@
+/***********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2008-2009  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+ * Copyright 2008-2009  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+ *
+ * THE BSD LICENSE
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+
+/***********************************************************************
+ * Author: Vincent Rabaud
+ *************************************************************************/
+
+#ifndef FLANN_LSH_TABLE_H_
+#define FLANN_LSH_TABLE_H_
+
+#include <algorithm>
+#include <iostream>
+#include <iomanip>
+#include <limits.h>
+// TODO as soon as we use C++0x, use the code in USE_UNORDERED_MAP
+#if USE_UNORDERED_MAP
+#include <unordered_map>
+#else
+#include <map>
+#endif
+#include <math.h>
+#include <stddef.h>
+
+#include "flann/util/dynamic_bitset.h"
+#include "flann/util/matrix.h"
+
+namespace flann
+{
+
+namespace lsh
+{
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+/** What is stored in an LSH bucket
+ */
+typedef uint32_t FeatureIndex;
+/** The id from which we can get a bucket back in an LSH table
+ */
+typedef unsigned int BucketKey;
+
+/** A bucket in an LSH table
+ */
+typedef std::vector<FeatureIndex> Bucket;
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+/** POD for stats about an LSH table
+ */
+struct LshStats
+{
+    std::vector<unsigned int> bucket_sizes_;
+    size_t n_buckets_;
+    size_t bucket_size_mean_;
+    size_t bucket_size_median_;
+    size_t bucket_size_min_;
+    size_t bucket_size_max_;
+    size_t bucket_size_std_dev;
+    /** Each contained vector contains three value: beginning/end for interval, number of elements in the bin
+     */
+    std::vector<std::vector<unsigned int> > size_histogram_;
+};
+
+/** Overload the << operator for LshStats
+ * @param out the streams
+ * @param stats the stats to display
+ * @return the streams
+ */
+inline std::ostream& operator <<(std::ostream& out, const LshStats& stats)
+{
+    size_t w = 20;
+    out << "Lsh Table Stats:\n" << std::setw(w) << std::setiosflags(std::ios::right) << "N buckets : "
+    << stats.n_buckets_ << "\n" << std::setw(w) << std::setiosflags(std::ios::right) << "mean size : "
+    << std::setiosflags(std::ios::left) << stats.bucket_size_mean_ << "\n" << std::setw(w)
+    << std::setiosflags(std::ios::right) << "median size : " << stats.bucket_size_median_ << "\n" << std::setw(w)
+    << std::setiosflags(std::ios::right) << "min size : " << std::setiosflags(std::ios::left)
+    << stats.bucket_size_min_ << "\n" << std::setw(w) << std::setiosflags(std::ios::right) << "max size : "
+    << std::setiosflags(std::ios::left) << stats.bucket_size_max_;
+
+    // Display the histogram
+    out << std::endl << std::setw(w) << std::setiosflags(std::ios::right) << "histogram : "
+    << std::setiosflags(std::ios::left);
+    for (std::vector<std::vector<unsigned int> >::const_iterator iterator = stats.size_histogram_.begin(), end =
+             stats.size_histogram_.end(); iterator != end; ++iterator) out << (*iterator)[0] << "-" << (*iterator)[1] << ": " << (*iterator)[2] << ",  ";
+
+    return out;
+}
+
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+/** Lsh hash table. As its key is a sub-feature, and as usually
+ * the size of it is pretty small, we keep it as a continuous memory array.
+ * The value is an index in the corpus of features (we keep it as an unsigned
+ * int for pure memory reasons, it could be a size_t)
+ */
+template<typename ElementType>
+class LshTable
+{
+public:
+    /** A container of all the feature indices. Optimized for space
+     */
+#if USE_UNORDERED_MAP
+    typedef std::unordered_map<BucketKey, Bucket> BucketsSpace;
+#else
+    typedef std::map<BucketKey, Bucket> BucketsSpace;
+#endif
+
+    /** A container of all the feature indices. Optimized for speed
+     */
+    typedef std::vector<Bucket> BucketsSpeed;
+
+    /** Default constructor
+     */
+    LshTable()
+    {
+    }
+
+    /** Default constructor
+     * Create the mask and allocate the memory
+     * @param feature_size is the size of the feature (considered as a ElementType[])
+     * @param key_size is the number of bits that are turned on in the feature
+     */
+    LshTable(unsigned int /*feature_size*/, unsigned int /*key_size*/)
+    {
+        std::cerr << "LSH is not implemented for that type" << std::endl;
+        throw;
+    }
+
+    /** Add a feature to the table
+     * @param value the value to store for that feature
+     * @param feature the feature itself
+     */
+    void add(unsigned int value, const ElementType* feature)
+    {
+        // Add the value to the corresponding bucket
+        BucketKey key = getKey(feature);
+
+        switch (speed_level_) {
+        case kArray:
+            // That means we get the buckets from an array
+            buckets_speed_[key].push_back(value);
+            break;
+        case kBitsetHash:
+            // That means we can check the bitset for the presence of a key
+            key_bitset_.set(key);
+            buckets_space_[key].push_back(value);
+            break;
+        case kHash:
+        {
+            // That means we have to check for the hash table for the presence of a key
+            buckets_space_[key].push_back(value);
+            break;
+        }
+        }
+    }
+
+    /** Add a set of features to the table
+     * @param dataset the values to store
+     */
+    void add(const std::vector< std::pair<size_t, ElementType*> >& features)
+    {
+#if USE_UNORDERED_MAP
+        buckets_space_.rehash((buckets_space_.size() + features.size()) * 1.2);
+#endif
+        // Add the features to the table
+        for (size_t i = 0; i < features.size(); ++i) {
+        	add(features[i].first, features[i].second);
+        }
+        // Now that the table is full, optimize it for speed/space
+        optimize();
+    }
+
+    /** Get a bucket given the key
+     * @param key
+     * @return
+     */
+    inline const Bucket* getBucketFromKey(BucketKey key) const
+    {
+        // Generate other buckets
+        switch (speed_level_) {
+        case kArray:
+            // That means we get the buckets from an array
+            return &buckets_speed_[key];
+            break;
+        case kBitsetHash:
+            // That means we can check the bitset for the presence of a key
+            if (key_bitset_.test(key)) return &buckets_space_.find(key)->second;
+            else return 0;
+            break;
+        case kHash:
+        {
+            // That means we have to check for the hash table for the presence of a key
+            BucketsSpace::const_iterator bucket_it, bucket_end = buckets_space_.end();
+            bucket_it = buckets_space_.find(key);
+            // Stop here if that bucket does not exist
+            if (bucket_it == bucket_end) return 0;
+            else return &bucket_it->second;
+            break;
+        }
+        }
+        return 0;
+    }
+
+    /** Compute the sub-signature of a feature
+     */
+    size_t getKey(const ElementType* /*feature*/) const
+    {
+        std::cerr << "LSH is not implemented for that type" << std::endl;
+        throw;
+        return 1;
+    }
+
+    /** Get statistics about the table
+     * @return
+     */
+    LshStats getStats() const;
+
+private:
+    /** defines the speed fo the implementation
+     * kArray uses a vector for storing data
+     * kBitsetHash uses a hash map but checks for the validity of a key with a bitset
+     * kHash uses a hash map only
+     */
+    enum SpeedLevel
+    {
+        kArray, kBitsetHash, kHash
+    };
+
+    /** Initialize some variables
+     */
+    void initialize(size_t key_size)
+    {
+        speed_level_ = kHash;
+        key_size_ = key_size;
+    }
+
+    /** Optimize the table for speed/space
+     */
+    void optimize()
+    {
+        // If we are already using the fast storage, no need to do anything
+        if (speed_level_ == kArray) return;
+
+        // Use an array if it will be more than half full
+        if (buckets_space_.size() > (unsigned int)((1 << key_size_) / 2)) {
+            speed_level_ = kArray;
+            // Fill the array version of it
+            buckets_speed_.resize(1 << key_size_);
+            for (BucketsSpace::const_iterator key_bucket = buckets_space_.begin(); key_bucket != buckets_space_.end(); ++key_bucket) buckets_speed_[key_bucket->first] = key_bucket->second;
+
+            // Empty the hash table
+            buckets_space_.clear();
+            return;
+        }
+
+        // If the bitset is going to use less than 10% of the RAM of the hash map (at least 1 size_t for the key and two
+        // for the vector) or less than 512MB (key_size_ <= 30)
+        if (((std::max(buckets_space_.size(), buckets_speed_.size()) * CHAR_BIT * 3 * sizeof(BucketKey)) / 10
+             >= size_t(1 << key_size_)) || (key_size_ <= 32)) {
+            speed_level_ = kBitsetHash;
+            key_bitset_.resize(1 << key_size_);
+            key_bitset_.reset();
+            // Try with the BucketsSpace
+            for (BucketsSpace::const_iterator key_bucket = buckets_space_.begin(); key_bucket != buckets_space_.end(); ++key_bucket) key_bitset_.set(key_bucket->first);
+        }
+        else {
+            speed_level_ = kHash;
+            key_bitset_.clear();
+        }
+    }
+
+    template<typename Archive>
+    void serialize(Archive& ar)
+    {
+    	int val;
+    	if (Archive::is_saving::value) {
+    		val = (int)speed_level_;
+    	}
+    	ar & val;
+    	if (Archive::is_loading::value) {
+    		speed_level_ = (SpeedLevel) val;
+    	}
+
+    	ar & key_size_;
+    	ar & mask_;
+
+    	if (speed_level_==kArray) {
+    		ar & buckets_speed_;
+    	}
+    	if (speed_level_==kBitsetHash || speed_level_==kHash) {
+    		ar & buckets_space_;
+    	}
+		if (speed_level_==kBitsetHash) {
+			ar & key_bitset_;
+		}
+    }
+    friend struct serialization::access;
+
+    /** The vector of all the buckets if they are held for speed
+     */
+    BucketsSpeed buckets_speed_;
+
+    /** The hash table of all the buckets in case we cannot use the speed version
+     */
+    BucketsSpace buckets_space_;
+
+    /** What is used to store the data */
+    SpeedLevel speed_level_;
+
+    /** If the subkey is small enough, it will keep track of which subkeys are set through that bitset
+     * That is just a speedup so that we don't look in the hash table (which can be mush slower that checking a bitset)
+     */
+    DynamicBitset key_bitset_;
+
+    /** The size of the sub-signature in bits
+     */
+    unsigned int key_size_;
+
+    // Members only used for the unsigned char specialization
+    /** The mask to apply to a feature to get the hash key
+     * Only used in the unsigned char case
+     */
+    std::vector<size_t> mask_;
+};
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+// Specialization for unsigned char
+
+template<>
+inline LshTable<unsigned char>::LshTable(unsigned int feature_size, unsigned int subsignature_size)
+{
+    initialize(subsignature_size);
+    // Allocate the mask
+    mask_ = std::vector<size_t>((size_t)ceil((float)(feature_size * sizeof(char)) / (float)sizeof(size_t)), 0);
+
+    // A bit brutal but fast to code
+    std::vector<size_t> indices(feature_size * CHAR_BIT);
+    for (size_t i = 0; i < feature_size * CHAR_BIT; ++i) indices[i] = i;
+    std::random_shuffle(indices.begin(), indices.end());
+
+    // Generate a random set of order of subsignature_size_ bits
+    for (unsigned int i = 0; i < key_size_; ++i) {
+        size_t index = indices[i];
+
+        // Set that bit in the mask
+        size_t divisor = CHAR_BIT * sizeof(size_t);
+        size_t idx = index / divisor; //pick the right size_t index
+        mask_[idx] |= size_t(1) << (index % divisor); //use modulo to find the bit offset
+    }
+
+    // Set to 1 if you want to display the mask for debug
+#if 0
+    {
+        size_t bcount = 0;
+        BOOST_FOREACH(size_t mask_block, mask_){
+            out << std::setw(sizeof(size_t) * CHAR_BIT / 4) << std::setfill('0') << std::hex << mask_block
+                << std::endl;
+            bcount += __builtin_popcountll(mask_block);
+        }
+        out << "bit count : " << std::dec << bcount << std::endl;
+        out << "mask size : " << mask_.size() << std::endl;
+        return out;
+    }
+#endif
+}
+
+/** Return the Subsignature of a feature
+ * @param feature the feature to analyze
+ */
+template<>
+inline size_t LshTable<unsigned char>::getKey(const unsigned char* feature) const
+{
+    // no need to check if T is dividable by sizeof(size_t) like in the Hamming
+    // distance computation as we have a mask
+    const size_t* feature_block_ptr = reinterpret_cast<const size_t*> (feature);
+
+    // Figure out the subsignature of the feature
+    // Given the feature ABCDEF, and the mask 001011, the output will be
+    // 000CEF
+    size_t subsignature = 0;
+    size_t bit_index = 1;
+
+    for (std::vector<size_t>::const_iterator pmask_block = mask_.begin(); pmask_block != mask_.end(); ++pmask_block) {
+        // get the mask and signature blocks
+        size_t feature_block = *feature_block_ptr;
+        size_t mask_block = *pmask_block;
+        while (mask_block) {
+            // Get the lowest set bit in the mask block
+            size_t lowest_bit = mask_block & (-(ptrdiff_t)mask_block);
+            // Add it to the current subsignature if necessary
+            subsignature += (feature_block & lowest_bit) ? bit_index : 0;
+            // Reset the bit in the mask block
+            mask_block ^= lowest_bit;
+            // increment the bit index for the subsignature
+            bit_index <<= 1;
+        }
+        // Check the next feature block
+        ++feature_block_ptr;
+    }
+    return subsignature;
+}
+
+template<>
+inline LshStats LshTable<unsigned char>::getStats() const
+{
+    LshStats stats;
+    stats.bucket_size_mean_ = 0;
+    if ((buckets_speed_.empty()) && (buckets_space_.empty())) {
+        stats.n_buckets_ = 0;
+        stats.bucket_size_median_ = 0;
+        stats.bucket_size_min_ = 0;
+        stats.bucket_size_max_ = 0;
+        return stats;
+    }
+
+    if (!buckets_speed_.empty()) {
+        for (BucketsSpeed::const_iterator pbucket = buckets_speed_.begin(); pbucket != buckets_speed_.end(); ++pbucket) {
+            stats.bucket_sizes_.push_back(pbucket->size());
+            stats.bucket_size_mean_ += pbucket->size();
+        }
+        stats.bucket_size_mean_ /= buckets_speed_.size();
+        stats.n_buckets_ = buckets_speed_.size();
+    }
+    else {
+        for (BucketsSpace::const_iterator x = buckets_space_.begin(); x != buckets_space_.end(); ++x) {
+            stats.bucket_sizes_.push_back(x->second.size());
+            stats.bucket_size_mean_ += x->second.size();
+        }
+        stats.bucket_size_mean_ /= buckets_space_.size();
+        stats.n_buckets_ = buckets_space_.size();
+    }
+
+    std::sort(stats.bucket_sizes_.begin(), stats.bucket_sizes_.end());
+
+    //  BOOST_FOREACH(int size, stats.bucket_sizes_)
+    //          std::cout << size << " ";
+    //  std::cout << std::endl;
+    stats.bucket_size_median_ = stats.bucket_sizes_[stats.bucket_sizes_.size() / 2];
+    stats.bucket_size_min_ = stats.bucket_sizes_.front();
+    stats.bucket_size_max_ = stats.bucket_sizes_.back();
+
+    // TODO compute mean and std
+    /*float mean, stddev;
+       stats.bucket_size_mean_ = mean;
+       stats.bucket_size_std_dev = stddev;*/
+
+    // Include a histogram of the buckets
+    unsigned int bin_start = 0;
+    unsigned int bin_end = 20;
+    bool is_new_bin = true;
+    for (std::vector<unsigned int>::iterator iterator = stats.bucket_sizes_.begin(), end = stats.bucket_sizes_.end(); iterator
+         != end; )
+        if (*iterator < bin_end) {
+            if (is_new_bin) {
+                stats.size_histogram_.push_back(std::vector<unsigned int>(3, 0));
+                stats.size_histogram_.back()[0] = bin_start;
+                stats.size_histogram_.back()[1] = bin_end - 1;
+                is_new_bin = false;
+            }
+            ++stats.size_histogram_.back()[2];
+            ++iterator;
+        }
+        else {
+            bin_start += 20;
+            bin_end += 20;
+            is_new_bin = true;
+        }
+
+    return stats;
+}
+
+// End the two namespaces
+}
+}
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+#endif /* FLANN_LSH_TABLE_H_ */
diff --git a/src/cpp/flann/util/matrix.h b/src/cpp/flann/util/matrix.h
new file mode 100644
index 0000000..3c038a7
--- /dev/null
+++ b/src/cpp/flann/util/matrix.h
@@ -0,0 +1,135 @@
+/***********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2008-2009  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+ * Copyright 2008-2009  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+ *
+ * THE BSD LICENSE
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+
+#ifndef FLANN_DATASET_H_
+#define FLANN_DATASET_H_
+
+#include "flann/general.h"
+#include "flann/util/serialization.h"
+#include <stdio.h>
+
+namespace flann
+{
+
+typedef unsigned char uchar;
+
+class Matrix_
+{
+public:
+
+	Matrix_() : rows(0), cols(0), stride(0), type(FLANN_NONE), data(NULL)
+	{
+	};
+
+    Matrix_(void* data_, size_t rows_, size_t cols_, flann_datatype_t type_, size_t stride_ = 0) :
+        rows(rows_), cols(cols_), stride(stride_), type(type_)
+    {
+    	data = static_cast<uchar*>(data_);
+
+    	if (stride==0) stride = flann_datatype_size(type)*cols;
+    }
+
+    /**
+     * Operator that returns a (pointer to a) row of the data.
+     */
+    inline void* operator[](size_t index) const
+    {
+        return data+index*stride;
+    }
+
+    void* ptr() const
+    {
+        return data;
+    }
+
+    size_t rows;
+    size_t cols;
+    size_t stride;
+    flann_datatype_t type;
+protected:
+    uchar* data;
+
+    template<typename Archive>
+    void serialize(Archive& ar)
+    {
+    	ar & rows;
+    	ar & cols;
+    	ar & stride;
+    	ar & type;
+    	if (Archive::is_loading::value) {
+    		data = new uchar[rows*stride];
+    	}
+    	ar & serialization::make_binary_object(data, rows*stride);
+    }
+    friend struct serialization::access;
+};
+
+
+/**
+ * Class that implements a simple rectangular matrix stored in a memory buffer and
+ * provides convenient matrix-like access using the [] operators.
+ *
+ * This class has the same memory structure as the un-templated class flann::Matrix_ and
+ * it's directly convertible from it.
+ */
+template <typename T>
+class Matrix : public Matrix_
+{
+public:
+    typedef T type;
+
+    Matrix() : Matrix_()
+    {
+    }
+
+    Matrix(T* data_, size_t rows_, size_t cols_, size_t stride_ = 0) :
+    	Matrix_(data_, rows_, cols_, flann_datatype_value<T>::value, stride_)
+    {
+    	if (stride==0) stride = sizeof(T)*cols;
+    }
+
+    /**
+     * Operator that returns a (pointer to a) row of the data.
+     */
+    inline T* operator[](size_t index) const
+    {
+    	return reinterpret_cast<T*>(data+index*stride);
+    }
+
+
+    T* ptr() const
+    {
+    	return reinterpret_cast<T*>(data);
+    }
+};
+
+}
+
+#endif //FLANN_DATASET_H_
diff --git a/src/cpp/flann/util/object_factory.h b/src/cpp/flann/util/object_factory.h
new file mode 100644
index 0000000..663c0b4
--- /dev/null
+++ b/src/cpp/flann/util/object_factory.h
@@ -0,0 +1,91 @@
+/***********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2008-2009  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+ * Copyright 2008-2009  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+ *
+ * THE BSD LICENSE
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+
+#ifndef FLANN_OBJECT_FACTORY_H_
+#define FLANN_OBJECT_FACTORY_H_
+
+#include <map>
+
+namespace flann
+{
+
+class CreatorNotFound
+{
+};
+
+template<typename BaseClass,
+         typename UniqueIdType,
+         typename ObjectCreator = BaseClass* (*)()>
+class ObjectFactory
+{
+    typedef ObjectFactory<BaseClass,UniqueIdType,ObjectCreator> ThisClass;
+    typedef std::map<UniqueIdType, ObjectCreator> ObjectRegistry;
+
+    // singleton class, private constructor
+    ObjectFactory() {}
+
+public:
+
+    bool subscribe(UniqueIdType id, ObjectCreator creator)
+    {
+        if (object_registry.find(id) != object_registry.end()) return false;
+
+        object_registry[id] = creator;
+        return true;
+    }
+
+    bool unregister(UniqueIdType id)
+    {
+        return object_registry.erase(id) == 1;
+    }
+
+    ObjectCreator create(UniqueIdType id)
+    {
+        typename ObjectRegistry::const_iterator iter = object_registry.find(id);
+
+        if (iter == object_registry.end()) {
+            throw CreatorNotFound();
+        }
+
+        return iter->second;
+    }
+
+    static ThisClass& instance()
+    {
+        static ThisClass the_factory;
+        return the_factory;
+    }
+private:
+    ObjectRegistry object_registry;
+};
+
+}
+
+#endif /* FLANN_OBJECT_FACTORY_H_ */
diff --git a/src/cpp/flann/util/params.h b/src/cpp/flann/util/params.h
new file mode 100644
index 0000000..6ada404
--- /dev/null
+++ b/src/cpp/flann/util/params.h
@@ -0,0 +1,139 @@
+/***********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2008-2011  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+ * Copyright 2008-2011  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+
+
+#ifndef FLANN_PARAMS_H_
+#define FLANN_PARAMS_H_
+
+#include "any.h"
+#include "flann/general.h"
+#include <iostream>
+#include <map>
+
+
+namespace flann
+{
+
+namespace anyimpl
+{
+SMALL_POLICY(flann_algorithm_t);
+SMALL_POLICY(flann_centers_init_t);
+SMALL_POLICY(flann_log_level_t);
+SMALL_POLICY(flann_datatype_t);
+}
+
+
+typedef std::map<std::string, any> IndexParams;
+
+
+typedef enum {
+	FLANN_False = 0,
+	FLANN_True = 1,
+	FLANN_Undefined
+} tri_type;
+
+
+struct SearchParams
+{
+    SearchParams(int checks_ = 32, float eps_ = 0.0, bool sorted_ = true ) :
+    	checks(checks_), eps(eps_), sorted(sorted_)
+    {
+    	max_neighbors = -1;
+    	use_heap = FLANN_Undefined;
+    	cores = 1;
+    	matrices_in_gpu_ram = false;
+    }
+
+    // how many leafs to visit when searching for neighbours (-1 for unlimited)
+    int checks;
+    // search for eps-approximate neighbours (default: 0)
+    float eps;
+    // only for radius search, require neighbours sorted by distance (default: true)
+    bool sorted;
+    // maximum number of neighbors radius search should return (-1 for unlimited)
+    int max_neighbors;
+    // use a heap to manage the result set (default: FLANN_Undefined)
+    tri_type use_heap;
+    // how many cores to assign to the search (used only if compiled with OpenMP capable compiler) (0 for auto)
+    int cores;
+    // for GPU search indicates if matrices are already in GPU ram
+    bool matrices_in_gpu_ram;
+};
+
+
+inline bool has_param(const IndexParams& params, std::string name)
+{
+	return params.find(name)!=params.end();
+}
+
+template<typename T>
+T get_param(const IndexParams& params, std::string name, const T& default_value)
+{
+    IndexParams::const_iterator it = params.find(name);
+    if (it != params.end()) {
+        return it->second.cast<T>();
+    }
+    else {
+        return default_value;
+    }
+}
+
+template<typename T>
+T get_param(const IndexParams& params, std::string name)
+{
+    IndexParams::const_iterator it = params.find(name);
+    if (it != params.end()) {
+        return it->second.cast<T>();
+    }
+    else {
+        throw FLANNException(std::string("Missing parameter '")+name+std::string("' in the parameters given"));
+    }
+}
+
+inline void print_params(const IndexParams& params)
+{
+    IndexParams::const_iterator it;
+
+    for(it=params.begin(); it!=params.end(); ++it) {
+        std::cout << it->first << " : " << it->second << std::endl;
+    }
+}
+
+inline void print_params(const SearchParams& params)
+{
+	std::cout << "checks : " << params.checks << std::endl;
+	std::cout << "eps : " << params.eps << std::endl;
+	std::cout << "sorted : " << params.sorted << std::endl;
+	std::cout << "max_neighbors : " << params.max_neighbors << std::endl;
+}
+
+
+}
+
+
+#endif /* FLANN_PARAMS_H_ */
diff --git a/src/cpp/flann/util/random.h b/src/cpp/flann/util/random.h
new file mode 100644
index 0000000..b7b51b4
--- /dev/null
+++ b/src/cpp/flann/util/random.h
@@ -0,0 +1,145 @@
+/***********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2008-2009  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+ * Copyright 2008-2009  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+ *
+ * THE BSD LICENSE
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+
+#ifndef FLANN_RANDOM_H
+#define FLANN_RANDOM_H
+
+#include <algorithm>
+#include <cstdlib>
+#include <cstddef>
+#include <vector>
+
+#include "flann/general.h"
+
+namespace flann
+{
+
+/**
+ * Seeds the random number generator
+ *  @param seed Random seed
+ */
+inline void seed_random(unsigned int seed)
+{
+    srand(seed);
+}
+
+/*
+ * Generates a random double value.
+ */
+/**
+ * Generates a random double value.
+ * @param high Upper limit
+ * @param low Lower limit
+ * @return Random double value
+ */
+inline double rand_double(double high = 1.0, double low = 0)
+{
+    return low + ((high-low) * (std::rand() / (RAND_MAX + 1.0)));
+}
+
+/**
+ * Generates a random integer value.
+ * @param high Upper limit
+ * @param low Lower limit
+ * @return Random integer value
+ */
+inline int rand_int(int high = RAND_MAX, int low = 0)
+{
+    return low + (int) ( double(high-low) * (std::rand() / (RAND_MAX + 1.0)));
+}
+
+
+class RandomGenerator
+{
+public:
+    ptrdiff_t operator() (ptrdiff_t i) { return rand_int(i); }
+};
+
+
+/**
+ * Random number generator that returns a distinct number from
+ * the [0,n) interval each time.
+ */
+class UniqueRandom
+{
+    std::vector<int> vals_;
+    int size_;
+    int counter_;
+
+public:
+    /**
+     * Constructor.
+     * @param n Size of the interval from which to generate
+     * @return
+     */
+    UniqueRandom(int n)
+    {
+        init(n);
+    }
+
+    /**
+     * Initializes the number generator.
+     * @param n the size of the interval from which to generate random numbers.
+     */
+    void init(int n)
+    {
+        static RandomGenerator generator;
+        // create and initialize an array of size n
+        vals_.resize(n);
+        size_ = n;
+        for (int i = 0; i < size_; ++i) vals_[i] = i;
+
+        // shuffle the elements in the array
+        std::random_shuffle(vals_.begin(), vals_.end(), generator);
+
+        counter_ = 0;
+    }
+
+    /**
+     * Return a distinct random integer in greater or equal to 0 and less
+     * than 'n' on each call. It should be called maximum 'n' times.
+     * Returns: a random integer
+     */
+    int next()
+    {
+        if (counter_ == size_) {
+            return -1;
+        }
+        else {
+            return vals_[counter_++];
+        }
+    }
+};
+
+}
+
+#endif //FLANN_RANDOM_H
+
+
diff --git a/src/cpp/flann/util/result_set.h b/src/cpp/flann/util/result_set.h
new file mode 100644
index 0000000..445033f
--- /dev/null
+++ b/src/cpp/flann/util/result_set.h
@@ -0,0 +1,936 @@
+/***********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2008-2009  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+ * Copyright 2008-2009  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+ *
+ * THE BSD LICENSE
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+
+#ifndef FLANN_RESULTSET_H
+#define FLANN_RESULTSET_H
+
+#include <algorithm>
+#include <cstring>
+#include <iostream>
+#include <limits>
+#include <set>
+#include <vector>
+
+namespace flann
+{
+
+/* This record represents a branch point when finding neighbors in
+    the tree.  It contains a record of the minimum distance to the query
+    point, as well as the node at which the search resumes.
+ */
+
+template <typename T, typename DistanceType>
+struct BranchStruct
+{
+    T node;           /* Tree node at which search resumes */
+    DistanceType mindist;     /* Minimum distance to query for all nodes below. */
+
+    BranchStruct() {}
+    BranchStruct(const T& aNode, DistanceType dist) : node(aNode), mindist(dist) {}
+
+    bool operator<(const BranchStruct<T, DistanceType>& rhs) const
+    {
+        return mindist<rhs.mindist;
+    }
+};
+
+
+template <typename DistanceType>
+struct DistanceIndex
+{
+    DistanceIndex(DistanceType dist, size_t index) :
+        dist_(dist), index_(index)
+    {
+    }
+    bool operator<(const DistanceIndex& dist_index) const
+    {
+        return (dist_ < dist_index.dist_) || ((dist_ == dist_index.dist_) && index_ < dist_index.index_);
+    }
+    DistanceType dist_;
+    size_t index_;
+};
+
+
+template <typename DistanceType>
+class ResultSet
+{
+public:
+    virtual ~ResultSet() {}
+
+    virtual bool full() const = 0;
+
+    virtual void addPoint(DistanceType dist, size_t index) = 0;
+
+    virtual DistanceType worstDist() const = 0;
+
+};
+
+/**
+ * KNNSimpleResultSet does not ensure that the element it holds are unique.
+ * Is used in those cases where the nearest neighbour algorithm used does not
+ * attempt to insert the same element multiple times.
+ */
+template <typename DistanceType>
+class KNNSimpleResultSet : public ResultSet<DistanceType>
+{
+public:
+	typedef DistanceIndex<DistanceType> DistIndex;
+
+	KNNSimpleResultSet(size_t capacity_) :
+        capacity_(capacity_)
+    {
+		// reserving capacity to prevent memory re-allocations
+		dist_index_.resize(capacity_, DistIndex(std::numeric_limits<DistanceType>::max(),-1));
+    	clear();
+    }
+
+    ~KNNSimpleResultSet()
+    {
+    }
+
+    /**
+     * Clears the result set
+     */
+    void clear()
+    {
+        worst_distance_ = std::numeric_limits<DistanceType>::max();
+        dist_index_[capacity_-1].dist_ = worst_distance_;
+        count_ = 0;
+    }
+
+    /**
+     *
+     * @return Number of elements in the result set
+     */
+    size_t size() const
+    {
+        return count_;
+    }
+
+    /**
+     * Radius search result set always reports full
+     * @return
+     */
+    bool full() const
+    {
+        return count_==capacity_;
+    }
+
+    /**
+     * Add a point to result set
+     * @param dist distance to point
+     * @param index index of point
+     */
+    void addPoint(DistanceType dist, size_t index)
+    {
+    	if (dist>=worst_distance_) return;
+
+        if (count_ < capacity_) ++count_;
+        size_t i;
+        for (i=count_-1; i>0; --i) {
+#ifdef FLANN_FIRST_MATCH
+            if ( (dist_index_[i-1].dist_>dist) || ((dist==dist_index_[i-1].dist_)&&(dist_index_[i-1].index_>index)) )
+#else
+            if (dist_index_[i-1].dist_>dist)
+#endif
+            {
+            	dist_index_[i] = dist_index_[i-1];
+            }
+            else break;
+        }
+        dist_index_[i].dist_ = dist;
+        dist_index_[i].index_ = index;
+        worst_distance_ = dist_index_[capacity_-1].dist_;
+    }
+
+    /**
+     * Copy indices and distances to output buffers
+     * @param indices
+     * @param dists
+     * @param num_elements Number of elements to copy
+     * @param sorted Indicates if results should be sorted
+     */
+    void copy(size_t* indices, DistanceType* dists, size_t num_elements, bool sorted = true)
+    {
+    	size_t n = std::min(count_, num_elements);
+    	for (size_t i=0; i<n; ++i) {
+    		*indices++ = dist_index_[i].index_;
+    		*dists++ = dist_index_[i].dist_;
+    	}
+    }
+
+    DistanceType worstDist() const
+    {
+    	return worst_distance_;
+    }
+
+private:
+    size_t capacity_;
+    size_t count_;
+    DistanceType worst_distance_;
+    std::vector<DistIndex> dist_index_;
+};
+
+/**
+ * K-Nearest neighbour result set. Ensures that the elements inserted are unique
+ */
+template <typename DistanceType>
+class KNNResultSet : public ResultSet<DistanceType>
+{
+public:
+	typedef DistanceIndex<DistanceType> DistIndex;
+
+    KNNResultSet(int capacity) : capacity_(capacity)
+    {
+		// reserving capacity to prevent memory re-allocations
+		dist_index_.resize(capacity_, DistIndex(std::numeric_limits<DistanceType>::max(),-1));
+    	clear();
+    }
+
+    ~KNNResultSet()
+    {
+
+    }
+
+    /**
+     * Clears the result set
+     */
+    void clear()
+    {
+        worst_distance_ = std::numeric_limits<DistanceType>::max();
+        dist_index_[capacity_-1].dist_ = worst_distance_;
+        count_ = 0;
+    }
+
+    size_t size() const
+    {
+        return count_;
+    }
+
+    bool full() const
+    {
+        return count_ == capacity_;
+    }
+
+
+    void addPoint(DistanceType dist, size_t index)
+    {
+        if (dist >= worst_distance_) return;
+        size_t i;
+        for (i = count_; i > 0; --i) {
+#ifdef FLANN_FIRST_MATCH
+            if ( (dist_index_[i-1].dist_<=dist) && ((dist!=dist_index_[i-1].dist_)||(dist_index_[i-1].index_<=index)) )
+#else
+            if (dist_index_[i-1].dist_<=dist)
+#endif
+            {
+                // Check for duplicate indices
+                size_t j = i - 1;
+                while (dist_index_[j].dist_ == dist) {
+                    if (dist_index_[j].index_ == index) {
+                        return;
+                    }
+                    --j;
+                }
+                break;
+            }
+        }
+        if (count_ < capacity_) ++count_;
+        for (size_t j = count_-1; j > i; --j) {
+            dist_index_[j] = dist_index_[j-1];
+        }
+        dist_index_[i].dist_ = dist;
+        dist_index_[i].index_ = index;
+        worst_distance_ = dist_index_[capacity_-1].dist_;
+    }
+
+    /**
+     * Copy indices and distances to output buffers
+     * @param indices
+     * @param dists
+     * @param num_elements Number of elements to copy
+     * @param sorted Indicates if results should be sorted
+     */
+    void copy(size_t* indices, DistanceType* dists, size_t num_elements, bool sorted = true)
+    {
+    	size_t n = std::min(count_, num_elements);
+    	for (size_t i=0; i<n; ++i) {
+    		*indices++ = dist_index_[i].index_;
+    		*dists++ = dist_index_[i].dist_;
+    	}
+    }
+
+    DistanceType worstDist() const
+    {
+        return worst_distance_;
+    }
+
+private:
+    size_t capacity_;
+    size_t count_;
+    DistanceType worst_distance_;
+    std::vector<DistIndex> dist_index_;
+
+};
+
+
+
+template <typename DistanceType>
+class KNNResultSet2 : public ResultSet<DistanceType>
+{
+public:
+	typedef DistanceIndex<DistanceType> DistIndex;
+
+	KNNResultSet2(size_t capacity_) :
+        capacity_(capacity_)
+    {
+		// reserving capacity to prevent memory re-allocations
+		dist_index_.reserve(capacity_);
+    	clear();
+    }
+
+    ~KNNResultSet2()
+    {
+    }
+
+    /**
+     * Clears the result set
+     */
+    void clear()
+    {
+        dist_index_.clear();
+        worst_dist_ = std::numeric_limits<DistanceType>::max();
+        is_full_ = false;
+    }
+
+    /**
+     *
+     * @return Number of elements in the result set
+     */
+    size_t size() const
+    {
+        return dist_index_.size();
+    }
+
+    /**
+     * Radius search result set always reports full
+     * @return
+     */
+    bool full() const
+    {
+        return is_full_;
+    }
+
+    /**
+     * Add another point to result set
+     * @param dist distance to point
+     * @param index index of point
+     * Pre-conditions: capacity_>0
+     */
+    void addPoint(DistanceType dist, size_t index)
+    {
+    	if (dist>=worst_dist_) return;
+
+    	if (dist_index_.size()==capacity_) {
+    		// if result set if filled to capacity, remove farthest element
+    		std::pop_heap(dist_index_.begin(), dist_index_.end());
+        	dist_index_.pop_back();
+    	}
+
+    	// add new element
+    	dist_index_.push_back(DistIndex(dist,index));
+    	if (is_full_) { // when is_full_==true, we have a heap
+    		std::push_heap(dist_index_.begin(), dist_index_.end());
+    	}
+
+    	if (dist_index_.size()==capacity_) {
+    		if (!is_full_) {
+    			std::make_heap(dist_index_.begin(), dist_index_.end());
+            	is_full_ = true;
+    		}
+    		// we replaced the farthest element, update worst distance
+        	worst_dist_ = dist_index_[0].dist_;
+        }
+    }
+
+    /**
+     * Copy indices and distances to output buffers
+     * @param indices
+     * @param dists
+     * @param num_elements Number of elements to copy
+     * @param sorted Indicates if results should be sorted
+     */
+    void copy(size_t* indices, DistanceType* dists, size_t num_elements, bool sorted = true)
+    {
+    	if (sorted) {
+    		// std::sort_heap(dist_index_.begin(), dist_index_.end());
+    		// sort seems faster here, even though dist_index_ is a heap
+    		std::sort(dist_index_.begin(), dist_index_.end());
+    	}
+    	else {
+    		if (num_elements<size()) {
+    			std::nth_element(dist_index_.begin(), dist_index_.begin()+num_elements, dist_index_.end());
+    		}
+    	}
+
+    	size_t n = std::min(dist_index_.size(), num_elements);
+    	for (size_t i=0; i<n; ++i) {
+    		*indices++ = dist_index_[i].index_;
+    		*dists++ = dist_index_[i].dist_;
+    	}
+    }
+
+    DistanceType worstDist() const
+    {
+    	return worst_dist_;
+    }
+
+private:
+    size_t capacity_;
+    DistanceType worst_dist_;
+    std::vector<DistIndex> dist_index_;
+    bool is_full_;
+};
+
+
+/**
+ * Unbounded radius result set. It will hold as many elements as
+ * are added to it.
+ */
+template <typename DistanceType>
+class RadiusResultSet : public ResultSet<DistanceType>
+{
+public:
+	typedef DistanceIndex<DistanceType> DistIndex;
+
+	RadiusResultSet(DistanceType radius_) :
+        radius_(radius_)
+    {
+		// reserving some memory to limit number of re-allocations
+		dist_index_.reserve(1024);
+    	clear();
+    }
+
+    ~RadiusResultSet()
+    {
+    }
+
+    /**
+     * Clears the result set
+     */
+    void clear()
+    {
+        dist_index_.clear();
+    }
+
+    /**
+     *
+     * @return Number of elements in the result set
+     */
+    size_t size() const
+    {
+        return dist_index_.size();
+    }
+
+    /**
+     * Radius search result set always reports full
+     * @return
+     */
+    bool full() const
+    {
+        return true;
+    }
+
+    /**
+     * Add another point to result set
+     * @param dist distance to point
+     * @param index index of point
+     * Pre-conditions: capacity_>0
+     */
+    void addPoint(DistanceType dist, size_t index)
+    {
+    	if (dist<radius_) {
+    		// add new element
+    		dist_index_.push_back(DistIndex(dist,index));
+    	}
+    }
+
+    /**
+     * Copy indices and distances to output buffers
+     * @param indices
+     * @param dists
+     * @param num_elements Number of elements to copy
+     * @param sorted Indicates if results should be sorted
+     */
+    void copy(size_t* indices, DistanceType* dists, size_t num_elements, bool sorted = true)
+    {
+    	if (sorted) {
+    		// std::sort_heap(dist_index_.begin(), dist_index_.end());
+    		// sort seems faster here, even though dist_index_ is a heap
+    		std::sort(dist_index_.begin(), dist_index_.end());
+    	}
+    	else {
+    		if (num_elements<size()) {
+    			std::nth_element(dist_index_.begin(), dist_index_.begin()+num_elements, dist_index_.end());
+    		}
+    	}
+
+    	size_t n = std::min(dist_index_.size(), num_elements);
+    	for (size_t i=0; i<n; ++i) {
+    		*indices++ = dist_index_[i].index_;
+    		*dists++ = dist_index_[i].dist_;
+    	}
+    }
+
+    DistanceType worstDist() const
+    {
+    	return radius_;
+    }
+
+private:
+    DistanceType radius_;
+    std::vector<DistIndex> dist_index_;
+};
+
+
+
+/**
+ * Bounded radius result set. It limits the number of elements
+ * it can hold to a preset capacity.
+ */
+template <typename DistanceType>
+class KNNRadiusResultSet : public ResultSet<DistanceType>
+{
+public:
+	typedef DistanceIndex<DistanceType> DistIndex;
+
+	KNNRadiusResultSet(DistanceType radius_, size_t capacity_) :
+        radius_(radius_), capacity_(capacity_)
+    {
+		// reserving capacity to prevent memory re-allocations
+		dist_index_.reserve(capacity_);
+    	clear();
+    }
+
+    ~KNNRadiusResultSet()
+    {
+    }
+
+    /**
+     * Clears the result set
+     */
+    void clear()
+    {
+        dist_index_.clear();
+        worst_dist_ = radius_;
+        is_heap_ = false;
+    }
+
+    /**
+     *
+     * @return Number of elements in the result set
+     */
+    size_t size() const
+    {
+        return dist_index_.size();
+    }
+
+    /**
+     * Radius search result set always reports full
+     * @return
+     */
+    bool full() const
+    {
+        return true;
+    }
+
+    /**
+     * Add another point to result set
+     * @param dist distance to point
+     * @param index index of point
+     * Pre-conditions: capacity_>0
+     */
+    void addPoint(DistanceType dist, size_t index)
+    {
+    	if (dist>=worst_dist_) return;
+
+    	if (dist_index_.size()==capacity_) {
+    		// if result set is filled to capacity, remove farthest element
+    		std::pop_heap(dist_index_.begin(), dist_index_.end());
+        	dist_index_.pop_back();
+    	}
+
+    	// add new element
+    	dist_index_.push_back(DistIndex(dist,index));
+    	if (is_heap_) {
+    		std::push_heap(dist_index_.begin(), dist_index_.end());
+    	}
+
+    	if (dist_index_.size()==capacity_) {
+    		// when got to full capacity, make it a heap
+    		if (!is_heap_) {
+    			std::make_heap(dist_index_.begin(), dist_index_.end());
+    			is_heap_ = true;
+    		}
+    		// we replaced the farthest element, update worst distance
+        	worst_dist_ = dist_index_[0].dist_;
+        }
+    }
+
+    /**
+     * Copy indices and distances to output buffers
+     * @param indices
+     * @param dists
+     * @param num_elements Number of elements to copy
+     * @param sorted Indicates if results should be sorted
+     */
+    void copy(size_t* indices, DistanceType* dists, size_t num_elements, bool sorted = true)
+    {
+    	if (sorted) {
+    		// std::sort_heap(dist_index_.begin(), dist_index_.end());
+    		// sort seems faster here, even though dist_index_ is a heap
+    		std::sort(dist_index_.begin(), dist_index_.end());
+    	}
+    	else {
+    		if (num_elements<size()) {
+    			std::nth_element(dist_index_.begin(), dist_index_.begin()+num_elements, dist_index_.end());
+    		}
+    	}
+
+    	size_t n = std::min(dist_index_.size(), num_elements);
+    	for (size_t i=0; i<n; ++i) {
+    		*indices++ = dist_index_[i].index_;
+    		*dists++ = dist_index_[i].dist_;
+    	}
+    }
+
+    DistanceType worstDist() const
+    {
+    	return worst_dist_;
+    }
+
+private:
+    bool is_heap_;
+    DistanceType radius_;
+    size_t capacity_;
+    DistanceType worst_dist_;
+    std::vector<DistIndex> dist_index_;
+};
+
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+/**
+ * This is a result set that only counts the neighbors within a radius.
+ */
+
+template <typename DistanceType>
+class CountRadiusResultSet : public ResultSet<DistanceType>
+{
+    DistanceType radius;
+    size_t count;
+
+public:
+    CountRadiusResultSet(DistanceType radius_ ) :
+        radius(radius_)
+    {
+        clear();
+    }
+
+    ~CountRadiusResultSet()
+    {
+    }
+
+    void clear()
+    {
+        count = 0;
+    }
+
+    size_t size() const
+    {
+        return count;
+    }
+
+    bool full() const
+    {
+        return true;
+    }
+
+    void addPoint(DistanceType dist, size_t index)
+    {
+        if (dist<radius) {
+            count++;
+        }
+    }
+
+    DistanceType worstDist() const
+    {
+        return radius;
+    }
+
+};
+
+
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+/** Class that holds the k NN neighbors
+ */
+template<typename DistanceType>
+class UniqueResultSet : public ResultSet<DistanceType>
+{
+public:
+    struct DistIndex
+    {
+        DistIndex(DistanceType dist, unsigned int index) :
+            dist_(dist), index_(index)
+        {
+        }
+        bool operator<(const DistIndex dist_index) const
+        {
+            return (dist_ < dist_index.dist_) || ((dist_ == dist_index.dist_) && index_ < dist_index.index_);
+        }
+        DistanceType dist_;
+        unsigned int index_;
+    };
+
+    /** Default cosntructor */
+    UniqueResultSet() :
+        worst_distance_(std::numeric_limits<DistanceType>::max())
+    {
+    }
+
+    /** Check the status of the set
+     * @return true if we have k NN
+     */
+    inline bool full() const
+    {
+        return is_full_;
+    }
+
+    /** Copy the set to two C arrays
+     * @param indices pointer to a C array of indices
+     * @param dist pointer to a C array of distances
+     * @param n_neighbors the number of neighbors to copy
+     */
+    void copy(size_t* indices, DistanceType* dist, int n_neighbors, bool sorted = true)
+    {
+    	if (n_neighbors<0) n_neighbors = dist_indices_.size();
+    	int i = 0;
+    	typedef typename std::set<DistIndex>::const_iterator Iterator;
+    	for (Iterator dist_index = dist_indices_.begin(), dist_index_end =
+    			dist_indices_.end(); (dist_index != dist_index_end) && (i < n_neighbors); ++dist_index, ++indices, ++dist, ++i) {
+    		*indices = dist_index->index_;
+    		*dist = dist_index->dist_;
+    	}
+    }
+
+    /** The number of neighbors in the set
+     * @return
+     */
+    size_t size() const
+    {
+        return dist_indices_.size();
+    }
+
+    /** The distance of the furthest neighbor
+     * If we don't have enough neighbors, it returns the max possible value
+     * @return
+     */
+    inline DistanceType worstDist() const
+    {
+        return worst_distance_;
+    }
+protected:
+    /** Flag to say if the set is full */
+    bool is_full_;
+
+    /** The worst distance found so far */
+    DistanceType worst_distance_;
+
+    /** The best candidates so far */
+    std::set<DistIndex> dist_indices_;
+};
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+/** Class that holds the k NN neighbors
+ * Faster than KNNResultSet as it uses a binary heap and does not maintain two arrays
+ */
+template<typename DistanceType>
+class KNNUniqueResultSet : public UniqueResultSet<DistanceType>
+{
+public:
+    /** Constructor
+     * @param capacity the number of neighbors to store at max
+     */
+    KNNUniqueResultSet(unsigned int capacity) : capacity_(capacity)
+    {
+        this->is_full_ = false;
+        this->clear();
+    }
+
+    /** Add a possible candidate to the best neighbors
+     * @param dist distance for that neighbor
+     * @param index index of that neighbor
+     */
+    inline void addPoint(DistanceType dist, size_t index)
+    {
+        // Don't do anything if we are worse than the worst
+        if (dist >= worst_distance_) return;
+        dist_indices_.insert(DistIndex(dist, index));
+
+        if (is_full_) {
+            if (dist_indices_.size() > capacity_) {
+                dist_indices_.erase(*dist_indices_.rbegin());
+                worst_distance_ = dist_indices_.rbegin()->dist_;
+            }
+        }
+        else if (dist_indices_.size() == capacity_) {
+            is_full_ = true;
+            worst_distance_ = dist_indices_.rbegin()->dist_;
+        }
+    }
+
+    /** Remove all elements in the set
+     */
+    void clear()
+    {
+        dist_indices_.clear();
+        worst_distance_ = std::numeric_limits<DistanceType>::max();
+        is_full_ = false;
+    }
+
+protected:
+    typedef typename UniqueResultSet<DistanceType>::DistIndex DistIndex;
+    using UniqueResultSet<DistanceType>::is_full_;
+    using UniqueResultSet<DistanceType>::worst_distance_;
+    using UniqueResultSet<DistanceType>::dist_indices_;
+
+    /** The number of neighbors to keep */
+    unsigned int capacity_;
+};
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+/** Class that holds the radius nearest neighbors
+ * It is more accurate than RadiusResult as it is not limited in the number of neighbors
+ */
+template<typename DistanceType>
+class RadiusUniqueResultSet : public UniqueResultSet<DistanceType>
+{
+public:
+    /** Constructor
+     * @param capacity the number of neighbors to store at max
+     */
+    RadiusUniqueResultSet(DistanceType radius) :
+        radius_(radius)
+    {
+        is_full_ = true;
+    }
+
+    /** Add a possible candidate to the best neighbors
+     * @param dist distance for that neighbor
+     * @param index index of that neighbor
+     */
+    void addPoint(DistanceType dist, size_t index)
+    {
+        if (dist < radius_) dist_indices_.insert(DistIndex(dist, index));
+    }
+
+    /** Remove all elements in the set
+     */
+    inline void clear()
+    {
+        dist_indices_.clear();
+    }
+
+
+    /** Check the status of the set
+     * @return alwys false
+     */
+    inline bool full() const
+    {
+        return true;
+    }
+
+    /** The distance of the furthest neighbor
+     * If we don't have enough neighbors, it returns the max possible value
+     * @return
+     */
+    inline DistanceType worstDist() const
+    {
+        return radius_;
+    }
+private:
+    typedef typename UniqueResultSet<DistanceType>::DistIndex DistIndex;
+    using UniqueResultSet<DistanceType>::dist_indices_;
+    using UniqueResultSet<DistanceType>::is_full_;
+
+    /** The furthest distance a neighbor can be */
+    DistanceType radius_;
+};
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+/** Class that holds the k NN neighbors within a radius distance
+ */
+template<typename DistanceType>
+class KNNRadiusUniqueResultSet : public KNNUniqueResultSet<DistanceType>
+{
+public:
+    /** Constructor
+     * @param capacity the number of neighbors to store at max
+     */
+    KNNRadiusUniqueResultSet(DistanceType radius, size_t capacity) : KNNUniqueResultSet<DistanceType>(capacity)
+    {
+        this->radius_ = radius;
+        this->clear();
+    }
+
+    /** Remove all elements in the set
+     */
+    void clear()
+    {
+        dist_indices_.clear();
+        worst_distance_ = radius_;
+        is_full_ = true;
+    }
+private:
+    using KNNUniqueResultSet<DistanceType>::dist_indices_;
+    using KNNUniqueResultSet<DistanceType>::is_full_;
+    using KNNUniqueResultSet<DistanceType>::worst_distance_;
+
+    /** The maximum distance of a neighbor */
+    DistanceType radius_;
+};
+}
+
+#endif //FLANN_RESULTSET_H
+
diff --git a/src/cpp/flann/util/sampling.h b/src/cpp/flann/util/sampling.h
new file mode 100644
index 0000000..e924603
--- /dev/null
+++ b/src/cpp/flann/util/sampling.h
@@ -0,0 +1,72 @@
+/***********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2008-2009  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+ * Copyright 2008-2009  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+
+
+#ifndef FLANN_SAMPLING_H_
+#define FLANN_SAMPLING_H_
+
+#include "flann/util/matrix.h"
+#include "flann/util/random.h"
+
+namespace flann
+{
+
+template<typename T>
+Matrix<T> random_sample(Matrix<T>& srcMatrix, size_t size, bool remove = false)
+{
+	UniqueRandom rand_unique(srcMatrix.rows);
+    Matrix<T> newSet(new T[size * srcMatrix.cols], size,srcMatrix.cols);
+
+    T* src,* dest;
+    for (size_t i=0; i<size; ++i) {
+    	size_t r;
+    	if (remove) {
+            r = static_cast<size_t>(rand_int(srcMatrix.rows-i));
+    	}
+    	else {
+    		r = static_cast<size_t>(rand_unique.next());
+    	}
+        dest = newSet[i];
+        src = srcMatrix[r];
+        std::copy(src, src+srcMatrix.cols, dest);
+        if (remove) {
+            src = srcMatrix[srcMatrix.rows-i-1];
+            dest = srcMatrix[r];
+            std::copy(src, src+srcMatrix.cols, dest);
+        }
+    }
+    if (remove) {
+        srcMatrix.rows -= size;
+    }
+    return newSet;
+}
+
+} // namespace
+
+
+#endif /* FLANN_SAMPLING_H_ */
diff --git a/src/cpp/flann/util/saving.h b/src/cpp/flann/util/saving.h
new file mode 100644
index 0000000..3cdfbda
--- /dev/null
+++ b/src/cpp/flann/util/saving.h
@@ -0,0 +1,135 @@
+/***********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2008-2009  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+ * Copyright 2008-2009  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE NNIndexGOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+
+#ifndef FLANN_SAVING_H_
+#define FLANN_SAVING_H_
+
+#include <cstring>
+#include <vector>
+#include <stdio.h>
+
+#include "flann/general.h"
+#include "flann/util/serialization.h"
+
+
+#ifdef FLANN_SIGNATURE_
+#undef FLANN_SIGNATURE_
+#endif
+#define FLANN_SIGNATURE_ "FLANN_INDEX"
+
+namespace flann
+{
+
+/**
+ * Structure representing the index header.
+ */
+struct IndexHeader
+{
+    char signature[16];
+    char version[16];
+    flann_datatype_t data_type;
+    flann_algorithm_t index_type;
+    flann_distance_t distance_type;
+    size_t rows;
+    size_t cols;
+
+
+    IndexHeader()
+	{
+        memset(signature, 0, sizeof(signature));
+        strcpy(signature, FLANN_SIGNATURE_);
+        memset(version, 0, sizeof(version));
+        strcpy(version, FLANN_VERSION_);
+	}
+
+private:
+    template<typename Archive>
+    void serialize(Archive& ar)
+    {
+    	ar & signature;
+    	ar & version;
+    	ar & data_type;
+    	ar & index_type;
+    	ar & rows;
+    	ar & cols;
+    }
+    friend struct serialization::access;
+};
+
+/**
+ * Saves index header to stream
+ *
+ * @param stream - Stream to save to
+ * @param index - The index to save
+ */
+template<typename Index>
+void save_header(FILE* stream, const Index& index)
+{
+    IndexHeader header;
+    header.data_type = flann_datatype_value<typename Index::ElementType>::value;
+    header.index_type = index.getType();
+    header.rows = index.size();
+    header.cols = index.veclen();
+
+    fwrite(&header, sizeof(header),1,stream);
+}
+
+
+/**
+ *
+ * @param stream - Stream to load from
+ * @return Index header
+ */
+inline IndexHeader load_header(FILE* stream)
+{
+    IndexHeader header;
+    int read_size = fread(&header,sizeof(header),1,stream);
+
+    if (read_size!=1) {
+        throw FLANNException("Invalid index file, cannot read");
+    }
+
+    if (strcmp(header.signature,FLANN_SIGNATURE_)!=0) {
+        throw FLANNException("Invalid index file, wrong signature");
+    }
+
+    return header;
+}
+
+
+namespace serialization
+{
+ENUM_SERIALIZER(flann_algorithm_t);
+ENUM_SERIALIZER(flann_centers_init_t);
+ENUM_SERIALIZER(flann_log_level_t);
+ENUM_SERIALIZER(flann_datatype_t);
+}
+
+}
+
+#endif /* FLANN_SAVING_H_ */
diff --git a/src/cpp/flann/util/serialization.h b/src/cpp/flann/util/serialization.h
new file mode 100644
index 0000000..885192b
--- /dev/null
+++ b/src/cpp/flann/util/serialization.h
@@ -0,0 +1,449 @@
+#ifndef SERIALIZATION_H_
+#define SERIALIZATION_H_
+
+#include <vector>
+#include <map>
+#include <stdio.h>
+
+namespace flann
+{
+namespace serialization
+{
+
+struct access 
+{
+    template<typename Archive, typename T>
+    static inline void serialize(Archive& ar, T& type)
+    {
+        type.serialize(ar);
+    }
+};
+
+
+template<typename Archive, typename T>
+inline void serialize(Archive& ar, T& type)
+{
+    access::serialize(ar,type);
+}
+
+template<typename T>
+struct Serializer
+{
+    template<typename InputArchive>
+    static inline void load(InputArchive& ar, T& val)
+    {
+        serialization::serialize(ar,val);
+    }
+    template<typename OutputArchive>
+    static inline void save(OutputArchive& ar, const T& val)
+    {
+        serialization::serialize(ar,const_cast<T&>(val));
+    }
+};
+
+#define BASIC_TYPE_SERIALIZER(type)\
+template<> \
+struct Serializer<type> \
+{\
+    template<typename InputArchive>\
+    static inline void load(InputArchive& ar, type& val)\
+    {\
+        ar.load(val);\
+    }\
+    template<typename OutputArchive>\
+    static inline void save(OutputArchive& ar, const type& val)\
+    {\
+        ar.save(val);\
+    }\
+}
+
+#define ENUM_SERIALIZER(type)\
+template<>\
+struct Serializer<type>\
+{\
+    template<typename InputArchive>\
+    static inline void load(InputArchive& ar, type& val)\
+    {\
+        int int_val;\
+        ar & int_val;\
+        val = (type) int_val;\
+    }\
+    template<typename OutputArchive>\
+    static inline void save(OutputArchive& ar, const type& val)\
+    {\
+        int int_val = (int)val;\
+        ar & int_val;\
+    }\
+}
+
+
+// declare serializers for simple types
+BASIC_TYPE_SERIALIZER(char);
+BASIC_TYPE_SERIALIZER(unsigned char);
+BASIC_TYPE_SERIALIZER(short);
+BASIC_TYPE_SERIALIZER(unsigned short);
+BASIC_TYPE_SERIALIZER(int);
+BASIC_TYPE_SERIALIZER(unsigned int);
+BASIC_TYPE_SERIALIZER(long);
+BASIC_TYPE_SERIALIZER(unsigned long);
+BASIC_TYPE_SERIALIZER(float);
+BASIC_TYPE_SERIALIZER(double);
+BASIC_TYPE_SERIALIZER(bool);
+
+
+
+// serializer for std::vector
+template<typename T>
+struct Serializer<std::vector<T> >
+{
+    template<typename InputArchive>
+    static inline void load(InputArchive& ar, std::vector<T>& val)
+    {
+        size_t size;
+        ar & size;
+        val.resize(size);
+        for (size_t i=0;i<size;++i) {
+            ar & val[i];
+        }
+    }
+
+    template<typename OutputArchive>
+    static inline void save(OutputArchive& ar, const std::vector<T>& val)
+    {
+        ar & val.size();
+        for (size_t i=0;i<val.size();++i) {
+            ar & val[i];
+        }
+    }
+};
+
+// serializer for std::vector
+template<typename K, typename V>
+struct Serializer<std::map<K,V> >
+{
+    template<typename InputArchive>
+    static inline void load(InputArchive& ar, std::map<K,V>& map_val)
+    {
+        size_t size;
+        ar & size;
+        for (size_t i = 0; i < size; ++i)
+        {
+            K key;
+            ar & key;
+            V value;
+            ar & value;
+            map_val[key] = value;
+        }
+    }
+
+    template<typename OutputArchive>
+    static inline void save(OutputArchive& ar, const std::map<K,V>& map_val)
+    {
+        ar & map_val.size();
+        for (typename std::map<K,V>::const_iterator i=map_val.begin(); i!=map_val.end(); ++i) {
+            ar & i->first;
+            ar & i->second;
+        }
+    }
+};
+
+template<typename T>
+struct Serializer<T*>
+{
+    template<typename InputArchive>
+    static inline void load(InputArchive& ar, T*& val)
+    {
+        ar.load(val);
+    }
+
+    template<typename OutputArchive>
+    static inline void save(OutputArchive& ar,  T* const& val)
+    {
+        ar.save(val);
+    }
+};
+
+template<typename T, int N>
+struct Serializer<T[N]>
+{
+    template<typename InputArchive>
+    static inline void load(InputArchive& ar, T (&val)[N])
+    {
+        ar.load(val);
+    }
+
+    template<typename OutputArchive>
+    static inline void save(OutputArchive& ar,  T const (&val)[N])
+    {
+        ar.save(val);
+    }
+};
+
+
+
+
+struct binary_object
+{
+    void const * ptr_;
+    size_t size_;
+
+    binary_object( void * const ptr, size_t size) :
+        ptr_(ptr),
+        size_(size)
+    {}
+    binary_object(const binary_object & rhs) :
+        ptr_(rhs.ptr_),
+        size_(rhs.size_)
+    {}
+
+    binary_object & operator=(const binary_object & rhs) {
+        ptr_ = rhs.ptr_;
+        size_ = rhs.size_;
+        return *this;
+    }
+};
+
+inline const binary_object make_binary_object(/* const */ void * t, size_t size){
+    return binary_object(t, size);
+}
+
+template<>
+struct Serializer<const binary_object>
+{
+    template<typename InputArchive>
+    static inline void load(InputArchive& ar, const binary_object& b)
+    {
+        ar.load_binary(const_cast<void *>(b.ptr_), b.size_);
+    }
+
+    template<typename OutputArchive>
+    static inline void save(OutputArchive& ar,  const binary_object& b)
+    {
+        ar.save_binary(b.ptr_, b.size_);
+    }
+};
+
+template<>
+struct Serializer<binary_object>
+{
+    template<typename InputArchive>
+    static inline void load(InputArchive& ar, binary_object& b)
+    {
+        ar.load_binary(const_cast<void *>(b.ptr_), b.size_);
+    }
+
+    template<typename OutputArchive>
+    static inline void save(OutputArchive& ar,  const binary_object& b)
+    {
+        ar.save_binary(b.ptr_, b.size_);
+    }
+};
+
+
+
+template <bool C_> 
+struct bool_ {
+    static const bool value = C_;
+    typedef bool value_type;
+};
+
+
+class ArchiveBase
+{
+public:
+
+	void* getObject() { return object_; }
+
+	void setObject(void* object) { object_ = object; }
+
+private:
+	void* object_;
+};
+
+
+template<typename Archive>
+class InputArchive : public ArchiveBase
+{
+protected:
+    InputArchive() {};
+public:
+    typedef bool_<true> is_loading;
+    typedef bool_<false> is_saving;
+
+    template<typename T>
+    Archive& operator& (T& val)
+    {
+        Serializer<T>::load(*static_cast<Archive*>(this),val);
+        return *static_cast<Archive*>(this);
+    }
+};
+
+
+template<typename Archive>
+class OutputArchive : public ArchiveBase
+{
+protected:
+    OutputArchive() {};
+public:
+    typedef bool_<false> is_loading;
+    typedef bool_<true> is_saving;
+    
+    template<typename T>
+    Archive& operator& (const T& val)
+    {
+        Serializer<T>::save(*static_cast<Archive*>(this),val);
+        return *static_cast<Archive*>(this);
+    }
+};
+
+
+
+class SizeArchive : public OutputArchive<SizeArchive>
+{
+    size_t size_;
+public:
+
+    SizeArchive() : size_(0)
+    {
+    }
+
+    template<typename T>
+    void save(const T& val)
+    {
+        size_ += sizeof(val);
+    }
+
+    template<typename T>
+    void save_binary(T* ptr, size_t size)
+    {
+    	size_ += size;
+    }
+
+
+    void reset()
+    {
+        size_ = 0;
+    }
+
+    size_t size()
+    {
+        return size_;
+    }
+};
+
+
+//
+//class PrintArchive : public OutputArchive<PrintArchive>
+//{
+//public:
+//    template<typename T>
+//    void save(const T& val)
+//    {
+//        std::cout << val << std::endl;
+//    }
+//
+//    template<typename T>
+//    void save_binary(T* ptr, size_t size)
+//    {
+//        std::cout << "<binary object>" << std::endl;
+//    }
+//};
+
+
+class SaveArchive : public OutputArchive<SaveArchive>
+{
+    FILE* stream_;
+    bool own_stream_;
+public:
+    SaveArchive(const char* filename)
+    {
+        stream_ = fopen(filename, "w");
+        own_stream_ = true;
+    }
+
+    SaveArchive(FILE* stream) : stream_(stream), own_stream_(false)
+    {
+    }
+
+    ~SaveArchive()
+    {
+    	if (own_stream_) {
+    		fclose(stream_);
+    	}
+    }
+
+    template<typename T>
+    void save(const T& val)
+    {
+        fwrite(&val, sizeof(val), 1, stream_);
+    }
+
+    template<typename T>
+    void save(T* const& val)
+    {
+    	// don't save pointers
+        //fwrite(&val, sizeof(val), 1, handle_);
+    }
+    
+    template<typename T>
+    void save_binary(T* ptr, size_t size)
+    {
+    	fwrite(ptr, size, 1, stream_);
+    }
+
+};
+
+
+class LoadArchive : public InputArchive<LoadArchive>
+{
+    FILE* stream_;
+    bool own_stream_;
+public:
+    LoadArchive(const char* filename)
+    {
+        stream_ = fopen(filename, "r");
+        own_stream_ = true;
+    }
+
+    LoadArchive(FILE* stream) : stream_(stream), own_stream_(false)
+    {
+    }
+
+    ~LoadArchive()
+    {
+    	if (own_stream_) {
+    		fclose(stream_);
+    	}
+    }
+
+    template<typename T>
+    void load(T& val)
+    {
+        size_t ret = fread(&val, sizeof(val), 1, stream_);
+        if (ret!=1) {
+        	throw FLANNException("Error loading from file");
+        }
+    }
+
+    template<typename T>
+    void load(T*& val)
+    {
+    	// don't load pointers
+        //fread(&val, sizeof(val), 1, handle_);
+    }
+
+    template<typename T>
+    void load_binary(T* ptr, size_t size)
+    {
+    	size_t ret = fread(ptr, size, 1, stream_);
+        if (ret!=1) {
+        	throw FLANNException("Error loading from file");
+        }
+    }
+
+
+};
+
+} // namespace serialization
+} // namespace flann
+#endif // SERIALIZATION_H_
diff --git a/src/cpp/flann/util/timer.h b/src/cpp/flann/util/timer.h
new file mode 100644
index 0000000..03dd393
--- /dev/null
+++ b/src/cpp/flann/util/timer.h
@@ -0,0 +1,95 @@
+/***********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2008-2009  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+ * Copyright 2008-2009  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+ *
+ * THE BSD LICENSE
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+
+#ifndef FLANN_TIMER_H
+#define FLANN_TIMER_H
+
+#include <time.h>
+
+
+namespace flann
+{
+
+/**
+ * A start-stop timer class.
+ *
+ * Can be used to time portions of code.
+ */
+class StartStopTimer
+{
+    clock_t startTime;
+
+public:
+    /**
+     * Value of the timer.
+     */
+    double value;
+
+
+    /**
+     * Constructor.
+     */
+    StartStopTimer()
+    {
+        reset();
+    }
+
+    /**
+     * Starts the timer.
+     */
+    void start()
+    {
+        startTime = clock();
+    }
+
+    /**
+     * Stops the timer and updates timer value.
+     */
+    double stop()
+    {
+        clock_t stopTime = clock();
+        value += ( (double)stopTime - startTime) / CLOCKS_PER_SEC;
+        
+        return value;
+    }
+
+    /**
+     * Resets the timer value to 0.
+     */
+    void reset()
+    {
+        value = 0;
+    }
+
+};
+
+}
+
+#endif // FLANN_TIMER_H
diff --git a/src/matlab/CMakeLists.txt b/src/matlab/CMakeLists.txt
new file mode 100644
index 0000000..2fc0f6a
--- /dev/null
+++ b/src/matlab/CMakeLists.txt
@@ -0,0 +1,82 @@
+#IF(WIN32)
+#    SET(MEXEXT_CMD cmd /C mexext)
+#ELSE(WIN32)
+#    SET(MEXEXT_CMD mexext)
+#ENDIF(WIN32)
+
+SET(MEX_NAME nearest_neighbors)
+
+if(WIN32)
+    find_program(MEX_CMD mex.bat)
+    find_program(MEXEXT_CMD mexext.bat)
+else()
+    find_program(MEX_CMD mex)
+    find_program(MEXEXT_CMD mexext)
+endif()
+
+find_program(OCT_CMD mkoctfile)
+
+if(MEX_CMD AND MEXEXT_CMD)
+
+    get_filename_component(MEX_REAL_CMD ${MEX_CMD} ABSOLUTE)
+    get_filename_component(MEX_PATH ${MEX_REAL_CMD} PATH)
+
+    get_filename_component(MEXEXT_REAL_CMD ${MEXEXT_CMD} ABSOLUTE)
+    get_filename_component(MEXEXT_PATH ${MEXEXT_REAL_CMD} PATH)
+
+    if (MEX_PATH STREQUAL MEXEXT_PATH)
+        message(STATUS "Found MATLAB at: " ${MEX_PATH})
+
+        EXECUTE_PROCESS(COMMAND ${MEXEXT_REAL_CMD} OUTPUT_VARIABLE MEX_EXTENSION OUTPUT_STRIP_TRAILING_WHITESPACE)
+        SET(MEX_FILE ${CMAKE_CURRENT_BINARY_DIR}/${MEX_NAME}.${MEX_EXTENSION})
+
+        if (WIN32)
+            set(MEX_BUILD_FLAGS "COMPFLAGS=\"$$COMPFLAGS ${OpenMP_CXX_FLAGS}\" LINKFLAGS=\"$$LINKFLAGS ${OpenMP_CXX_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}\"")
+        else()
+            set(MEX_BUILD_FLAGS "CFLAGS='$$CFLAGS ${OpenMP_CXX_FLAGS}' LDFLAGS='$$LDFLAGS ${OpenMP_CXX_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}'")
+        endif()
+        separate_arguments(MEX_BUILD_FLAGS)
+
+        ADD_CUSTOM_COMMAND(
+            OUTPUT ${MEX_FILE}
+            COMMAND ${MEX_REAL_CMD}
+            ARGS ${CMAKE_CURRENT_SOURCE_DIR}/${MEX_NAME}.cpp -I${PROJECT_SOURCE_DIR}/src/cpp -L${LIBRARY_OUTPUT_PATH} -lflann_s ${MEX_BUILD_FLAGS}
+            DEPENDS flann_s ${CMAKE_CURRENT_SOURCE_DIR}/${MEX_NAME}.cpp
+            COMMENT "Building MEX extension ${MEX_FILE}"
+        )
+
+        ADD_CUSTOM_TARGET(mex_${MEX_NAME} ALL DEPENDS ${MEX_FILE})
+
+        FILE(GLOB MATLAB_SOURCES *.m)
+
+        INSTALL (
+            FILES ${MEX_FILE} ${MATLAB_SOURCES}
+            DESTINATION share/flann/matlab
+        )
+    else()
+        message(WARNING "The 'mex' and 'mexext' programs have been found in different locations. It's likely that one of them is not part of the MATLAB instalation. Make sure that the 'bin' directory from the MATLAB instalation is in PATH")
+        set(BUILD_MATLAB_BINDINGS OFF) 
+    endif()
+elseif(OCT_CMD)
+  SET(MEX_FILE ${CMAKE_CURRENT_BINARY_DIR}/${MEX_NAME}.mex)
+  ADD_CUSTOM_COMMAND(
+    OUTPUT ${MEX_FILE}
+    COMMAND ${OCT_CMD}
+    ARGS ${CMAKE_CURRENT_SOURCE_DIR}/${MEX_NAME}.cpp -I${PROJECT_SOURCE_DIR}/src/cpp -L${LIBRARY_OUTPUT_PATH} -DFLANN_STATIC -lflann_s --mex
+    DEPENDS flann_s ${CMAKE_CURRENT_SOURCE_DIR}/${MEX_NAME}.cpp
+    COMMENT "Building MEX extension ${MEX_FILE}"
+    )
+
+  ADD_CUSTOM_TARGET(mex_${MEX_NAME} ALL DEPENDS ${MEX_FILE})
+
+  FILE(GLOB MATLAB_SOURCES *.m)
+
+  INSTALL (
+    FILES ${MEX_FILE} ${MATLAB_SOURCES}
+    DESTINATION share/flann/octave
+    )
+else()
+    message(WARNING "Cannot find MATLAB or Octave instalation. Make sure that the 'bin' directory from the MATLAB instalation or that mkoctfile is in PATH")
+    set(BUILD_MATLAB_BINDINGS OFF) 
+endif()
+
diff --git a/src/matlab/flann_build_index.m b/src/matlab/flann_build_index.m
new file mode 100644
index 0000000..08012fe
--- /dev/null
+++ b/src/matlab/flann_build_index.m
@@ -0,0 +1,56 @@
+function [index, params, speedup] = flann_build_index(dataset, build_params)
+%FLANN_BUILD_INDEX  Builds an index for fast approximate nearest neighbors search
+%
+% [index, params, speedup] = flann_build_index(dataset, build_params) - Constructs the
+% index from the provided 'dataset' and (optionally) computes the optimal parameters.
+
+% Marius Muja, January 2008
+
+    algos = struct( 'linear', 0, 'kdtree', 1, 'kmeans', 2, 'composite', 3, 'kdtree_single', 4, 'hierarchical', 5, 'lsh', 6, 'saved', 254, 'autotuned', 255 );
+    center_algos = struct('random', 0, 'gonzales', 1, 'kmeanspp', 2 );
+    log_levels = struct('none', 0, 'fatal', 1, 'error', 2, 'warning', 3, 'info', 4);
+    function value = id2value(map, id)
+        fields = fieldnames(map);
+        for i = 1:length(fields),
+            val = cell2mat(fields(i));
+            if map.(val) == id
+                value = val;
+                break;
+            end
+        end
+    end
+    function id = value2id(map,value)
+        id = map.(value);
+    end
+
+    default_params = struct('algorithm', 'kdtree' ,'checks', 32, 'eps', 0.0, 'sorted', 1, 'max_neighbors', -1, 'cores', 1, 'trees', 4, 'branching', 32, 'iterations', 5, 'centers_init', 'random', 'cb_index', 0.4, 'target_precision', 0.9,'build_weight', 0.01, 'memory_weight', 0, 'sample_fraction', 0.1, 'log_level', 'warning', 'random_seed', 0);
+
+    if ~isstruct(build_params)
+        error('The "build_params" argument must be a structure');
+    end
+
+    params = default_params;
+    fn = fieldnames(build_params);
+    for i = [1:length(fn)],
+        name = cell2mat(fn(i));
+        params.(name) = build_params.(name);
+    end
+    if ~isnumeric(params.algorithm),
+        params.algorithm = value2id(algos,params.algorithm);
+    end
+    if ~isnumeric(params.centers_init),
+        params.centers_init = value2id(center_algos,params.centers_init);
+    end
+    if ~isnumeric(params.log_level),
+        params.log_level = value2id(log_levels,params.log_level);
+    end
+
+    [index, params, speedup] = nearest_neighbors('build_index',dataset, params);
+
+    if isnumeric(params.algorithm),
+        params.algorithm = id2value(algos,params.algorithm);
+    end
+    if isnumeric(params.centers_init),
+        params.centers_init = id2value(center_algos,params.centers_init);
+    end
+end
diff --git a/src/matlab/flann_free_index.m b/src/matlab/flann_free_index.m
new file mode 100644
index 0000000..3cb944f
--- /dev/null
+++ b/src/matlab/flann_free_index.m
@@ -0,0 +1,35 @@
+%Copyright 2008-2009  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+%Copyright 2008-2009  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+%
+%THE BSD LICENSE
+%
+%Redistribution and use in source and binary forms, with or without
+%modification, are permitted provided that the following conditions
+%are met:
+%
+%1. Redistributions of source code must retain the above copyright
+%   notice, this list of conditions and the following disclaimer.
+%2. Redistributions in binary form must reproduce the above copyright
+%   notice, this list of conditions and the following disclaimer in the
+%   documentation and/or other materials provided with the distribution.
+%
+%THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+%IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+%OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+%IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+%INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+%NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+%DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+%THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+%(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+%THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+function flann_free_index(index_id)
+%FLANN_FREE_INDEX  Deletes the nearest-neighbors index
+%
+% Deletes an index constructed using flann_build_index.
+ 
+% Marius Muja, January 2008
+
+    nearest_neighbors('free_index',index_id);
+end
\ No newline at end of file
diff --git a/src/matlab/flann_load_index.m b/src/matlab/flann_load_index.m
new file mode 100644
index 0000000..06ab120
--- /dev/null
+++ b/src/matlab/flann_load_index.m
@@ -0,0 +1,33 @@
+%Copyright 2008-2009  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+%Copyright 2008-2009  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+%
+%THE BSD LICENSE
+%
+%Redistribution and use in source and binary forms, with or without
+%modification, are permitted provided that the following conditions
+%are met:
+%
+%1. Redistributions of source code must retain the above copyright
+%   notice, this list of conditions and the following disclaimer.
+%2. Redistributions in binary form must reproduce the above copyright
+%   notice, this list of conditions and the following disclaimer in the
+%   documentation and/or other materials provided with the distribution.
+%
+%THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+%IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+%OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+%IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+%INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+%NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+%DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+%THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+%(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+%THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+function index = flann_load_index(filename, dataset)
+%FLANN_LOAD_INDEX  Loads an index from disk
+%
+% Marius Muja, March 2009
+
+    index = nearest_neighbors('load_index', filename, dataset);
+end
\ No newline at end of file
diff --git a/src/matlab/flann_save_index.m b/src/matlab/flann_save_index.m
new file mode 100644
index 0000000..2d6f090
--- /dev/null
+++ b/src/matlab/flann_save_index.m
@@ -0,0 +1,34 @@
+%Copyright 2008-2009  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+%Copyright 2008-2009  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+%
+%THE BSD LICENSE
+%
+%Redistribution and use in source and binary forms, with or without
+%modification, are permitted provided that the following conditions
+%are met:
+%
+%1. Redistributions of source code must retain the above copyright
+%   notice, this list of conditions and the following disclaimer.
+%2. Redistributions in binary form must reproduce the above copyright
+%   notice, this list of conditions and the following disclaimer in the
+%   documentation and/or other materials provided with the distribution.
+%
+%THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+%IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+%OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+%IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+%INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+%NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+%DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+%THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+%(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+%THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+function flann_save_index(index_id, filename)
+%FLANN_SAVE_INDEX  Saves an index to disk
+%
+ 
+% Marius Muja, March 2010
+
+    nearest_neighbors('save_index',index_id, filename);
+end
\ No newline at end of file
diff --git a/src/matlab/flann_search.m b/src/matlab/flann_search.m
new file mode 100644
index 0000000..411415c
--- /dev/null
+++ b/src/matlab/flann_search.m
@@ -0,0 +1,84 @@
+%Copyright 2008-2009  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+%Copyright 2008-2009  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+%
+%THE BSD LICENSE
+%
+%Redistribution and use in source and binary forms, with or without
+%modification, are permitted provided that the following conditions
+%are met:
+%
+%1. Redistributions of source code must retain the above copyright
+%   notice, this list of conditions and the following disclaimer.
+%2. Redistributions in binary form must reproduce the above copyright
+%   notice, this list of conditions and the following disclaimer in the
+%   documentation and/or other materials provided with the distribution.
+%
+%THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+%IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+%OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+%IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+%INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+%NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+%DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+%THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+%(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+%THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+function [indices, dists] = flann_search(data, testset, n, search_params)
+%NN_SEARCH  Fast approximate nearest neighbors search
+%
+% Performs a fast approximate nearest neighbor search using an
+% index constructed using flann_build_index or directly a 
+% dataset.
+
+% Marius Muja, January 2008
+
+
+    algos = struct( 'linear', 0, 'kdtree', 1, 'kmeans', 2, 'composite', 3, 'saved', 254, 'autotuned', 255 );
+    center_algos = struct('random', 0, 'gonzales', 1, 'kmeanspp', 2 );
+    log_levels = struct('none', 0, 'fatal', 1, 'error', 2, 'warning', 3, 'info', 4);
+    function value = id2value(map, id)
+        fields = fieldnames(map);
+        for i = 1:length(fields),
+            val = cell2mat(fields(i));
+            if map.(val) == id
+                value = val;
+                break;
+            end
+        end
+    end
+    function id = value2id(map,value)
+        id = map.(value);
+    end
+
+    default_params = struct('algorithm', 'kdtree' ,'checks', 32, 'eps', 0.0, 'sorted', 1, 'max_neighbors', -1, 'cores', 1, 'trees', 4, 'branching', 32, 'iterations', 5, 'centers_init', 'random', 'cb_index', 0.4, 'target_precision', 0.9,'build_weight', 0.01, 'memory_weight', 0, 'sample_fraction', 0.1, 'log_level', 'warning', 'random_seed', 0);
+
+
+    if ~isstruct(search_params)
+        error('The "search_params" argument must be a structure');
+    end
+
+    params = default_params;
+    fn = fieldnames(search_params);
+    for i = [1:length(fn)],
+        name = cell2mat(fn(i));
+        params.(name) = search_params.(name);
+    end
+    if ~isnumeric(params.algorithm),
+        params.algorithm = value2id(algos,params.algorithm);
+    end
+    if ~isnumeric(params.centers_init),
+        params.centers_init = value2id(center_algos,params.centers_init);
+    end
+    if ~isnumeric(params.log_level),
+        params.log_level = value2id(log_levels,params.log_level);
+    end
+
+    if (size(data,1)==1 && size(data,2)==1)
+        % we already have an index
+        [indices,dists] = nearest_neighbors('index_find_nn', data, testset, n, params);
+    else
+        % create the index and search
+        [indices,dists] = nearest_neighbors('find_nn', data, testset, n, params);
+    end
+end
diff --git a/src/matlab/flann_set_distance_type.m b/src/matlab/flann_set_distance_type.m
new file mode 100644
index 0000000..0892f5a
--- /dev/null
+++ b/src/matlab/flann_set_distance_type.m
@@ -0,0 +1,45 @@
+%Copyright 2008-2009  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+%Copyright 2008-2009  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+%
+%THE BSD LICENSE
+%
+%Redistribution and use in source and binary forms, with or without
+%modification, are permitted provided that the following conditions
+%are met:
+%
+%1. Redistributions of source code must retain the above copyright
+%   notice, this list of conditions and the following disclaimer.
+%2. Redistributions in binary form must reproduce the above copyright
+%   notice, this list of conditions and the following disclaimer in the
+%   documentation and/or other materials provided with the distribution.
+%
+%THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+%IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+%OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+%IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+%INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+%NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+%DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+%THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+%(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+%THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+function flann_set_distance_type(type, order)
+%FLANN_LOAD_INDEX  Loads an index from disk
+%
+% Marius Muja, March 2009
+
+    distances = struct('euclidean', 1, 'manhattan', 2, 'minkowski', 3, 'max_dist', 4, 'hik', 5, 'hellinger', 6, 'chi_square', 7, 'cs', 7, 'kullback_leibler', 8, 'kl', 8);
+    function id = value2id(map,value)
+        id = map.(value);
+    end
+
+
+    if ~isnumeric(type),
+        type = value2id(distances,type);
+    end
+    if type~=3
+        order = 0;
+    end
+    nearest_neighbors('set_distance_type', type, order);
+end
diff --git a/src/matlab/nearest_neighbors.cpp b/src/matlab/nearest_neighbors.cpp
new file mode 100644
index 0000000..74416a8
--- /dev/null
+++ b/src/matlab/nearest_neighbors.cpp
@@ -0,0 +1,643 @@
+/*
+   Copyright 2008-2009  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+   Copyright 2008-2009  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+
+   THE BSD LICENSE
+
+   Redistribution and use in source and binary forms, with or without
+   modification, are permitted provided that the following conditions
+   are met:
+
+   1. Redistributions of source code must retain the above copyright
+   notice, this list of conditions and the following disclaimer.
+   2. Redistributions in binary form must reproduce the above copyright
+   notice, this list of conditions and the following disclaimer in the
+   documentation and/or other materials provided with the distribution.
+
+   THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+   IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+   IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/* Workaround for MSVC 10, Matlab incompatibility */
+#if (_MSC_VER >= 1600)
+#include <yvals.h>
+#define __STDC_UTF_16__
+#endif
+#include <mex.h>
+#include <flann/flann.h>
+#include <stdio.h>
+#include <string.h>
+
+
+
+struct TypedIndex
+{
+    flann_index_t index;
+    flann_datatype_t type;
+};
+
+
+template <typename T>
+static mxArray* to_mx_array(T value)
+{
+    mxArray* mat = mxCreateDoubleMatrix(1,1,mxREAL);
+    double* ptr = mxGetPr(mat);
+    *ptr = value;
+
+    return mat;
+}
+
+
+static void matlabStructToFlannStruct( const mxArray* mexParams, FLANNParameters& flannParams )
+{
+    flannParams.algorithm = (flann_algorithm_t)(int)*(mxGetPr(mxGetField(mexParams, 0,"algorithm")));
+
+    // kdtree
+    flannParams.trees = (int)*(mxGetPr(mxGetField(mexParams, 0,"trees")));
+
+    // kmeans
+    flannParams.branching = (int)*(mxGetPr(mxGetField(mexParams, 0,"branching")));
+    flannParams.iterations = (int)*(mxGetPr(mxGetField(mexParams, 0,"iterations")));
+    flannParams.centers_init = (flann_centers_init_t)(int)*(mxGetPr(mxGetField(mexParams, 0,"centers_init")));
+    flannParams.cb_index = (float)*(mxGetPr(mxGetField(mexParams, 0,"cb_index")));
+
+    // autotuned
+    flannParams.target_precision = (float)*(mxGetPr(mxGetField(mexParams, 0,"target_precision")));
+    flannParams.build_weight = (float)*(mxGetPr(mxGetField(mexParams, 0,"build_weight")));
+    flannParams.memory_weight = (float)*(mxGetPr(mxGetField(mexParams, 0,"memory_weight")));
+    flannParams.sample_fraction = (float)*(mxGetPr(mxGetField(mexParams, 0,"sample_fraction")));
+
+    // misc
+    flannParams.log_level = (flann_log_level_t)(int)*(mxGetPr(mxGetField(mexParams, 0,"log_level")));
+    flannParams.random_seed = (int)*(mxGetPr(mxGetField(mexParams, 0,"random_seed")));
+
+    // search
+    flannParams.checks = (int)*(mxGetPr(mxGetField(mexParams, 0,"checks")));
+    flannParams.eps = (float)*(mxGetPr(mxGetField(mexParams, 0,"eps")));
+    flannParams.sorted = (int)*(mxGetPr(mxGetField(mexParams, 0,"sorted")));
+    flannParams.max_neighbors = (int)*(mxGetPr(mxGetField(mexParams, 0,"max_neighbors")));
+    flannParams.cores = (int)*(mxGetPr(mxGetField(mexParams, 0,"cores")));
+}
+
+static mxArray* flannStructToMatlabStruct( const FLANNParameters& flannParams )
+{
+    const char* fieldnames[] = {"algorithm", "checks", "eps", "sorted", "max_neighbors", "cores", "trees", "leaf_max_size", "branching", "iterations", "centers_init", "cb_index"};
+    mxArray* mexParams = mxCreateStructMatrix(1, 1, sizeof(fieldnames)/sizeof(const char*), fieldnames);
+
+    mxSetField(mexParams, 0, "algorithm", to_mx_array(flannParams.algorithm));
+    mxSetField(mexParams, 0, "checks", to_mx_array(flannParams.checks));
+    mxSetField(mexParams, 0, "eps", to_mx_array(flannParams.eps));
+    mxSetField(mexParams, 0, "sorted", to_mx_array(flannParams.sorted));
+    mxSetField(mexParams, 0, "max_neighbors", to_mx_array(flannParams.max_neighbors));
+    mxSetField(mexParams, 0, "cores", to_mx_array(flannParams.cores));
+
+    mxSetField(mexParams, 0, "trees", to_mx_array(flannParams.trees));
+    mxSetField(mexParams, 0, "leaf_max_size", to_mx_array(flannParams.trees));
+    
+    mxSetField(mexParams, 0, "branching", to_mx_array(flannParams.branching));
+    mxSetField(mexParams, 0, "iterations", to_mx_array(flannParams.iterations));
+    mxSetField(mexParams, 0, "centers_init", to_mx_array(flannParams.centers_init));
+    mxSetField(mexParams, 0, "cb_index", to_mx_array(flannParams.cb_index));
+
+    return mexParams;
+}
+
+
+static void check_allowed_type(const mxArray* datasetMat)
+{
+    if (!mxIsSingle(datasetMat) &&
+        !mxIsDouble(datasetMat) &&
+        !mxIsUint8(datasetMat) &&
+        !mxIsInt32(datasetMat)) {
+        mexErrMsgTxt("Data type must be floating point single precision, floating point double precision, "
+                     "8 bit unsigned integer or 32 bit signed integer");
+    }
+}
+
+
+/**
+ * Input arguments: dataset (matrix), testset (matrix), n (int),  params (struct)
+ * Output arguments: indices(matrix), dists(matrix)
+ */
+static void _find_nn(int nOutArray, mxArray* OutArray[], int nInArray, const mxArray* InArray[])
+{
+    /* Check the number of input arguments */
+    if(nInArray != 4) {
+        mexErrMsgTxt("Incorrect number of input arguments, expecting:\n"
+                     "dataset, testset, nearest_neighbors, params");
+    }
+
+    /* Check the number of output arguments */
+    if(nOutArray > 2) {
+        mexErrMsgTxt("One or two outputs required.");
+    }
+    const mxArray* datasetMat = InArray[0];
+    const mxArray* testsetMat = InArray[1];
+    check_allowed_type(datasetMat);
+    check_allowed_type(testsetMat);
+
+    int dcount = mxGetN(datasetMat);
+    int length = mxGetM(datasetMat);
+    int tcount = mxGetN(testsetMat);
+
+    if (mxGetM(testsetMat) != length) {
+        mexErrMsgTxt("Dataset and testset features should have the same size.");
+    }
+
+    const mxArray* nnMat = InArray[2];
+
+    if ((mxGetM(nnMat)!=1)||(mxGetN(nnMat)!=1)|| !mxIsNumeric(nnMat)) {
+        mexErrMsgTxt("Number of nearest neighbors should be a scalar.");
+    }
+    int nn = (int)(*mxGetPr(nnMat));
+
+    const mxArray* pStruct = InArray[3];
+
+    if (!mxIsStruct(pStruct)) {
+        mexErrMsgTxt("Params must be a struct object.");
+    }
+
+    FLANNParameters p;
+    matlabStructToFlannStruct(pStruct, p);
+
+    int* result = (int*)malloc(tcount*nn*sizeof(int));
+    float* dists = NULL;
+    double* ddists = NULL;
+
+    /* do the search */
+    if (mxIsSingle(datasetMat)) {
+        float* dataset = (float*) mxGetData(datasetMat);
+        float* testset = (float*) mxGetData(testsetMat);
+        dists = (float*)malloc(tcount*nn*sizeof(float));
+        flann_find_nearest_neighbors_float(dataset,dcount,length,testset, tcount, result, dists, nn, &p);
+    }
+    else if (mxIsDouble(datasetMat)) {
+        double* dataset = (double*) mxGetData(datasetMat);
+        double* testset = (double*) mxGetData(testsetMat);
+        ddists = (double*)malloc(tcount*nn*sizeof(double));
+        flann_find_nearest_neighbors_double(dataset,dcount,length,testset, tcount, result, ddists, nn, &p);
+    }
+    else if (mxIsUint8(datasetMat)) {
+        unsigned char* dataset = (unsigned char*) mxGetData(datasetMat);
+        unsigned char* testset = (unsigned char*) mxGetData(testsetMat);
+        dists = (float*)malloc(tcount*nn*sizeof(float));
+        flann_find_nearest_neighbors_byte(dataset,dcount,length,testset, tcount, result, dists, nn, &p);
+    }
+    else if (mxIsInt32(datasetMat)) {
+        int* dataset = (int*) mxGetData(datasetMat);
+        int* testset = (int*) mxGetData(testsetMat);
+        dists = (float*)malloc(tcount*nn*sizeof(float));
+        flann_find_nearest_neighbors_int(dataset,dcount,length,testset, tcount, result, dists, nn, &p);
+    }
+
+    /* Allocate memory for Output Matrix */
+    OutArray[0] = mxCreateDoubleMatrix(nn, tcount, mxREAL);
+
+    /* Get pointer to Output matrix and store result */
+    double* pOut = mxGetPr(OutArray[0]);
+    for (int i=0; i<tcount*nn; ++i) {
+        pOut[i] = result[i]+1; // matlab uses 1-based indexing
+    }
+    free(result);
+
+    if (nOutArray > 1) {
+        /* Allocate memory for Output Matrix */
+        OutArray[1] = mxCreateDoubleMatrix(nn, tcount, mxREAL);
+
+        /* Get pointer to Output matrix and store result*/
+        double* pDists = mxGetPr(OutArray[1]);
+        if (dists!=NULL) {
+            for (int i=0; i<tcount*nn; ++i) {
+                pDists[i] = dists[i];
+            }
+        }
+        if (ddists!=NULL) {
+            for (int i=0; i<tcount*nn; ++i) {
+                pDists[i] = ddists[i];
+            }
+        }
+    }
+    if (dists!=NULL) free(dists);
+    if (ddists!=NULL) free(ddists);
+}
+
+/**
+ * Input arguments: index (pointer), testset (matrix), n (int),  params (struct)
+ * Output arguments: indices(matrix), dists(matrix)
+ */
+static void _index_find_nn(int nOutArray, mxArray* OutArray[], int nInArray, const mxArray* InArray[])
+{
+    /* Check the number of input arguments */
+    if(nInArray != 4) {
+        mexErrMsgTxt("Incorrect number of input arguments");
+    }
+    /* Check if there is one Output matrix */
+    if(nOutArray > 2) {
+        mexErrMsgTxt("One or two outputs required.");
+    }
+
+    const mxArray* indexMat = InArray[0];
+    TypedIndex* typedIndex = *(TypedIndex**)mxGetData(indexMat);
+
+    const mxArray* testsetMat = InArray[1];
+    check_allowed_type(testsetMat);
+
+    int tcount = mxGetN(testsetMat);
+
+    const mxArray* nnMat = InArray[2];
+
+    if ((mxGetM(nnMat)!=1)||(mxGetN(nnMat)!=1)) {
+        mexErrMsgTxt("Number of nearest neighbors should be a scalar.");
+    }
+    int nn = (int)(*mxGetPr(nnMat));
+
+    int* result = (int*)malloc(tcount*nn*sizeof(int));
+    float* dists = NULL;
+    double* ddists = NULL;
+
+    const mxArray* pStruct = InArray[3];
+
+    FLANNParameters p;
+    matlabStructToFlannStruct(pStruct, p);
+
+    if (mxIsSingle(testsetMat)) {
+        if (typedIndex->type != FLANN_FLOAT32) {
+            mexErrMsgTxt("Index type must match testset type");
+        }
+        float* testset = (float*) mxGetData(testsetMat);
+        dists = (float*)malloc(tcount*nn*sizeof(float));
+        flann_find_nearest_neighbors_index_float(typedIndex->index,testset, tcount, result, dists, nn, &p);
+    }
+    else if (mxIsDouble(testsetMat)) {
+        if (typedIndex->type != FLANN_FLOAT64) {
+            mexErrMsgTxt("Index type must match testset type");
+        }
+        double* testset = (double*) mxGetData(testsetMat);
+        ddists = (double*)malloc(tcount*nn*sizeof(double));
+        flann_find_nearest_neighbors_index_double(typedIndex->index,testset, tcount, result, ddists, nn, &p);
+    }
+    else if (mxIsUint8(testsetMat)) {
+        if (typedIndex->type != FLANN_UINT8) {
+            mexErrMsgTxt("Index type must match testset type");
+        }
+        unsigned char* testset = (unsigned char*) mxGetData(testsetMat);
+        dists = (float*)malloc(tcount*nn*sizeof(float));
+        flann_find_nearest_neighbors_index_byte(typedIndex->index,testset, tcount, result, dists, nn, &p);
+    }
+    else if (mxIsInt32(testsetMat)) {
+        if (typedIndex->type != FLANN_INT32) {
+            mexErrMsgTxt("Index type must match testset type");
+        }
+        int* testset = (int*) mxGetData(testsetMat);
+        dists = (float*)malloc(tcount*nn*sizeof(float));
+        flann_find_nearest_neighbors_index_int(typedIndex->index,testset, tcount, result, dists, nn, &p);
+    }
+
+    /* Allocate memory for Output Matrix */
+    OutArray[0] = mxCreateDoubleMatrix(nn, tcount, mxREAL);
+
+    /* Get pointer to Output matrix and store result*/
+    double* pOut = mxGetPr(OutArray[0]);
+    for (int i=0; i<tcount*nn; ++i) {
+        pOut[i] = result[i]+1; // matlab uses 1-based indexing
+    }
+    free(result);
+    if (nOutArray > 1) {
+        /* Allocate memory for Output Matrix */
+        OutArray[1] = mxCreateDoubleMatrix(nn, tcount, mxREAL);
+
+        /* Get pointer to Output matrix and store result*/
+        double* pDists = mxGetPr(OutArray[1]);
+        if (dists!=NULL) {
+            for (int i=0; i<tcount*nn; ++i) {
+                pDists[i] = dists[i];
+            }
+        }
+        if (ddists!=NULL) {
+            for (int i=0; i<tcount*nn; ++i) {
+                pDists[i] = ddists[i];
+            }
+        }
+    }
+    if (dists!=NULL) free(dists);
+    if (ddists!=NULL) free(ddists);
+}
+
+
+/**
+ * Input arguments: dataset (matrix), params (struct)
+ * Output arguments: index (pointer to index), params (struct), speedup(double)
+ */
+static void _build_index(int nOutArray, mxArray* OutArray[], int nInArray, const mxArray* InArray[])
+{
+    /* Check the number of input arguments */
+    if(nInArray != 2) {
+        mexErrMsgTxt("Incorrect number of input arguments");
+    }
+    /* Check the number of output arguments */
+    if ((nOutArray == 0)||(nOutArray > 3)) {
+        mexErrMsgTxt("Incorrect number of outputs.");
+    }
+    const mxArray* datasetMat = InArray[0];
+    check_allowed_type(datasetMat);
+
+    int dcount = mxGetN(datasetMat);
+    int length = mxGetM(datasetMat);
+
+
+    const mxArray* pStruct = InArray[1];
+
+    /* get index parameters */
+    FLANNParameters p;
+    matlabStructToFlannStruct(pStruct, p);
+
+    float speedup = -1;
+
+    TypedIndex* typedIndex = new TypedIndex();
+
+    if (mxIsSingle(datasetMat)) {
+        float* dataset = (float*) mxGetData(datasetMat);
+        typedIndex->index = flann_build_index_float(dataset,dcount,length, &speedup, &p);
+        typedIndex->type = FLANN_FLOAT32;
+    }
+    else if (mxIsDouble(datasetMat)) {
+        double* dataset = (double*) mxGetData(datasetMat);
+        typedIndex->index = flann_build_index_double(dataset,dcount,length, &speedup, &p);
+        typedIndex->type = FLANN_FLOAT64;
+    }
+    else if (mxIsUint8(datasetMat)) {
+        unsigned char* dataset = (unsigned char*) mxGetData(datasetMat);
+        typedIndex->index = flann_build_index_byte(dataset,dcount,length, &speedup, &p);
+        typedIndex->type = FLANN_UINT8;
+    }
+    else if (mxIsInt32(datasetMat)) {
+        int* dataset = (int*) mxGetData(datasetMat);
+        typedIndex->index = flann_build_index_int(dataset,dcount,length, &speedup, &p);
+        typedIndex->type = FLANN_INT32;
+    }
+
+    mxClassID classID;
+    if (sizeof(flann_index_t)==4) {
+        classID = mxUINT32_CLASS;
+    }
+    else if (sizeof(flann_index_t)==8) {
+        classID = mxUINT64_CLASS;
+    }
+
+    /* Allocate memory for Output Matrix */
+    OutArray[0] = mxCreateNumericMatrix(1, 1, classID, mxREAL);
+
+    /* Get pointer to Output matrix and store result*/
+    TypedIndex** pOut = (TypedIndex**)mxGetData(OutArray[0]);
+    pOut[0] = typedIndex;
+
+    if (nOutArray > 1) {
+        OutArray[1] = flannStructToMatlabStruct(p);
+    }
+    if (nOutArray > 2) {
+        OutArray[2] = mxCreateDoubleMatrix(1, 1, mxREAL);
+        double* pSpeedup = mxGetPr(OutArray[2]);
+
+        *pSpeedup = speedup;
+    }
+}
+
+/**
+ * Inputs: index (index pointer)
+ */
+static void _free_index(int nOutArray, mxArray* OutArray[], int nInArray, const mxArray* InArray[])
+{
+    /* Check the number of input arguments */
+    if(!((nInArray == 1)&&((mxGetN(InArray[0])*mxGetM(InArray[0]))==1))) {
+        mexErrMsgTxt("Expecting a single scalar argument: the index ID");
+    }
+    TypedIndex* typedIndex = *(TypedIndex**)mxGetData(InArray[0]);
+    if (typedIndex->type==FLANN_FLOAT32) {
+        flann_free_index_float(typedIndex->index, NULL);
+    }
+    else if (typedIndex->type==FLANN_FLOAT64) {
+        flann_free_index_double(typedIndex->index, NULL);
+    }
+    else if (typedIndex->type==FLANN_UINT8) {
+        flann_free_index_byte(typedIndex->index, NULL);
+    }
+    else if (typedIndex->type==FLANN_INT32) {
+        flann_free_index_int(typedIndex->index, NULL);
+    }
+    delete typedIndex;
+}
+
+/**
+ * Inputs: level
+ */
+static void _set_log_level(int nOutArray, mxArray* OutArray[], int nInArray, const mxArray* InArray[])
+{
+    if (nInArray != 1) {
+        mexErrMsgTxt("Incorrect number of input arguments: expecting log_level");
+    }
+
+    const mxArray* llMat = InArray[0];
+
+    if ((mxGetM(llMat)!=1)||(mxGetN(llMat)!=1)|| !mxIsNumeric(llMat)) {
+        mexErrMsgTxt("Log Level should be a scalar.");
+    }
+    int log_level = (int)(*mxGetPr(llMat));
+
+    flann_log_verbosity(log_level);
+
+}
+
+/**
+ * Inputs: type (flann_distance_t), order(int)
+ */
+static void _set_distance_type(int nOutArray, mxArray* OutArray[], int nInArray, const mxArray* InArray[])
+{
+    if( ((nInArray != 1)&&(nInArray != 2))) {
+        mexErrMsgTxt("Incorrect number of input arguments");
+    }
+
+    const mxArray* distMat = InArray[0];
+
+    if ((mxGetM(distMat)!=1)||(mxGetN(distMat)!=1)|| !mxIsNumeric(distMat)) {
+        mexErrMsgTxt("Distance type should be a scalar.");
+    }
+    int distance_type = (int)(*mxGetPr(distMat));
+
+    int order = 0;
+    if (nInArray==2) {
+        const mxArray* ordMat = InArray[1];
+        if ((mxGetM(ordMat)!=1)||(mxGetN(ordMat)!=1)|| !mxIsNumeric(ordMat)) {
+            mexErrMsgTxt("Distance order should be a scalar.");
+        }
+
+        order = (int)(*mxGetPr(ordMat));
+    }
+    flann_set_distance_type((flann_distance_t)distance_type, order);
+}
+
+
+/**
+ * Inputs: index (index pointer), filename (string)
+ */
+static void _save_index(int nOutArray, mxArray* OutArray[], int nInArray, const mxArray* InArray[])
+{
+    /* Check the number of input arguments */
+    if(nInArray != 2) {
+        mexErrMsgTxt("Incorrect number of input arguments");
+    }
+
+    const mxArray* indexMat = InArray[0];
+    TypedIndex* typedIndex = *(TypedIndex**)mxGetData(indexMat);
+
+    // get the selector
+    if(!mxIsChar(InArray[1])) {
+        mexErrMsgTxt("'filename' should be a string");
+    }
+    char filename[128];
+    mxGetString(InArray[1],filename,128);
+
+    if (typedIndex->type==FLANN_FLOAT32) {
+        flann_save_index_float(typedIndex->index, filename);
+    }
+    else if (typedIndex->type==FLANN_FLOAT64) {
+        flann_save_index_double(typedIndex->index, filename);
+    }
+    else if (typedIndex->type==FLANN_UINT8) {
+        flann_save_index_byte(typedIndex->index, filename);
+    }
+    else if (typedIndex->type==FLANN_INT32) {
+        flann_save_index_int(typedIndex->index, filename);
+    }
+}
+
+
+/**
+ * Inputs: filename (string), matrix
+ */
+static void _load_index(int nOutArray, mxArray* OutArray[], int nInArray, const mxArray* InArray[])
+{
+    if(nInArray != 2) {
+        mexErrMsgTxt("Incorrect number of input arguments");
+    }
+    // get the selector
+    if(!mxIsChar(InArray[0])) {
+        mexErrMsgTxt("'filename' should be a string");
+    }
+    char filename[128];
+    mxGetString(InArray[0],filename,128);
+
+    const mxArray* datasetMat = InArray[1];
+    check_allowed_type(datasetMat);
+
+    int dcount = mxGetN(datasetMat);
+    int length = mxGetM(datasetMat);
+
+    TypedIndex* typedIndex = new TypedIndex();
+
+    if (mxIsSingle(datasetMat)) {
+        float* dataset = (float*) mxGetData(datasetMat);
+        typedIndex->index = flann_load_index_float(filename, dataset,dcount,length);
+        typedIndex->type = FLANN_FLOAT32;
+    }
+    else if (mxIsDouble(datasetMat)) {
+        double* dataset = (double*) mxGetData(datasetMat);
+        typedIndex->index = flann_load_index_double(filename, dataset,dcount,length);
+        typedIndex->type = FLANN_FLOAT64;
+    }
+    else if (mxIsUint8(datasetMat)) {
+        unsigned char* dataset = (unsigned char*) mxGetData(datasetMat);
+        typedIndex->index = flann_load_index_byte(filename, dataset,dcount,length);
+        typedIndex->type = FLANN_UINT8;
+    }
+    else if (mxIsInt32(datasetMat)) {
+        int* dataset = (int*) mxGetData(datasetMat);
+        typedIndex->index = flann_load_index_int(filename, dataset,dcount,length);
+        typedIndex->type = FLANN_INT32;
+    }
+
+    mxClassID classID;
+    if (sizeof(flann_index_t)==4) {
+        classID = mxUINT32_CLASS;
+    }
+    else if (sizeof(flann_index_t)==8) {
+        classID = mxUINT64_CLASS;
+    }
+
+    /* Allocate memory for Output Matrix */
+    OutArray[0] = mxCreateNumericMatrix(1, 1, classID, mxREAL);
+
+    /* Get pointer to Output matrix and store result*/
+    TypedIndex** pOut = (TypedIndex**)mxGetData(OutArray[0]);
+    pOut[0] = typedIndex;
+}
+
+
+struct mexFunctionEntry
+{
+    const char* name;
+    void (* function)(int, mxArray**, int, const mxArray**);
+};
+
+static mexFunctionEntry __functionTable[] = {
+    { "find_nn", &_find_nn},
+    { "build_index", &_build_index},
+    { "index_find_nn", &_index_find_nn},
+    { "free_index", &_free_index},
+    { "save_index", &_save_index},
+    { "load_index", &_load_index},
+    { "set_log_level", &_set_log_level},
+    { "set_distance_type", &_set_distance_type},
+};
+
+
+static void print_selector_error()
+{
+    char buf[512];
+    char* msg = buf;
+
+    sprintf(msg, "%s", "Expecting first argument to be one of: ");
+    msg = buf+strlen(buf);
+    for (int i=0; i<sizeof(__functionTable)/sizeof(mexFunctionEntry); ++i) {
+        if (i!=0) {
+            sprintf(msg,", ");
+            msg = buf+strlen(buf);
+        }
+        sprintf(msg, "%s", __functionTable[i].name);
+        msg = buf+strlen(buf);
+    }
+
+    mexErrMsgTxt(buf);
+}
+
+
+void mexFunction(int nOutArray, mxArray* OutArray[], int nInArray, const mxArray* InArray[])
+{
+    // get the selector
+    if((nInArray == 0)|| !mxIsChar(InArray[0])) {
+        print_selector_error();
+    }
+    char selector[128];
+    mxGetString(InArray[0],selector,128);
+
+    // check if function with that name is present
+    int idx = 0;
+    for (idx = 0; idx<sizeof(__functionTable)/sizeof(mexFunctionEntry); ++idx) {
+        if (strcmp(__functionTable[idx].name, selector)==0) {
+            break;
+        }
+    }
+    if (idx==sizeof(__functionTable)/sizeof(mexFunctionEntry)) {
+        print_selector_error();
+    }
+
+    // now call the function
+    __functionTable[idx].function(nOutArray,OutArray, nInArray-1, InArray+1);
+}
diff --git a/src/matlab/test_flann.m b/src/matlab/test_flann.m
new file mode 100644
index 0000000..5a4833e
--- /dev/null
+++ b/src/matlab/test_flann.m
@@ -0,0 +1,218 @@
+%Copyright 2008-2009  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+%Copyright 2008-2009  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+%
+%THE BSD LICENSE
+%
+%Redistribution and use in source and binary forms, with or without
+%modification, are permitted provided that the following conditions
+%are met:
+%
+%1. Redistributions of source code must retain the above copyright
+%   notice, this list of conditions and the following disclaimer.
+%2. Redistributions in binary form must reproduce the above copyright
+%   notice, this list of conditions and the following disclaimer in the
+%   documentation and/or other materials provided with the distribution.
+%
+%THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+%IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+%OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+%IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+%INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+%NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+%DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+%THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+%(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+%THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+function test_flann
+data_path = './';
+outcome = {'FAILED!!!!!!!!!', 'PASSED'};
+
+failed = 0;
+passed = 0;
+cnt = 0;
+ok = 1;
+
+    function assert(condition)
+        if (~condition)
+            ok = 0;
+        end
+    end
+
+    function run_test(name, test)
+        ok = 1;
+        cnt = cnt + 1;
+        tic;
+        fprintf('Test %d: %s...',cnt,name);
+        test();
+        time = toc;
+        if (ok)
+            passed = passed + 1;
+        else
+            failed = failed + 1;
+        end
+        fprintf('done (%g sec) : %s\n',time,cell2mat(outcome(ok+1)))
+    end
+
+    function status
+        fprintf('-----------------\n');
+        fprintf('Passed: %d/%d\nFailed: %d/%d\n',passed,cnt,failed,cnt);
+    end
+
+
+    dataset = [];
+    testset = [];
+    function test_load_data
+        % load the datasets and testsets
+        % use single precision for better memory efficiency
+        % store the features one per column because MATLAB
+        % uses column major ordering
+        % The dataset.dat and testset.dat files can be downloaded from:
+        % http://people.cs.ubc.ca/~mariusm/uploads/FLANN/datasets/dataset.dat
+        % http://people.cs.ubc.ca/~mariusm/uploads/FLANN/datasets/testset.dat
+        dataset = single(load([data_path 'dataset.dat']))';
+        testset = single(load([data_path 'testset.dat']))';
+
+        assert(size(dataset,1) == size(testset,1));
+    end
+    run_test('Load data', at test_load_data);
+
+	match = [];
+    dists = [];
+    function test_linear_search
+        [match,dists] = flann_search(dataset, testset, 10, struct('algorithm','linear'));
+        assert(size(match,1) ==10 && size(match,2) == size(testset,2));
+    end
+    run_test('Linear search', at test_linear_search);
+
+    function test_kdtree_search
+        [result, ndists] = flann_search(dataset, testset, 10, struct('algorithm','kdtree',...
+                                                          'trees',8,...
+                                                          'checks',64));
+        n = size(match,2);
+        precision = (n-sum(abs(result(1,:)-match(1,:))>0))/n;
+        assert(precision>0.9);
+        assert(sum(~(match(1,:)-result(1,:)).*(dists(1,:)-ndists(1,:)))==0);
+    end
+    run_test('kd-tree search', at test_kdtree_search);
+    
+    function test_kmeans_search
+        [result, ndists] = flann_search(dataset, testset, 10, struct('algorithm','kmeans',...
+                                                          'branching',32,...
+                                                          'iterations',3,...
+                                                          'checks',120));
+        n = size(match,2);
+        precision = (n-sum(abs(result(1,:)-match(1,:))>0))/n;
+        assert(precision>0.9);
+        assert(sum(~(match(1,:)-result(1,:)).*(dists(1,:)-ndists(1,:)))==0);
+    end
+    run_test('k-means search', at test_kmeans_search);
+
+    
+    
+    function test_composite_search
+        [result, ndists] = flann_search(dataset, testset, 10, struct('algorithm','composite',...
+                                                          'branching',32,...
+                                                          'iterations',3,...
+                                                          'trees', 1,...
+                                                          'checks',64));
+        n = size(match,2);
+        precision = (n-sum(abs(result(1,:)-match(1,:))>0))/n;
+        assert(precision>0.9);
+        assert(sum(~(match(1,:)-result(1,:)).*(dists(1,:)-ndists(1,:)))==0);
+    end
+    run_test('composite search', at test_composite_search);
+    
+    function test_autotune_search
+        [result, ndists] = flann_search(dataset, testset, 10, struct('algorithm','autotuned',...
+                                                          'target_precision',0.95,...
+                                                          'build_weight',0.01,...
+                                                          'memory_weight',0));
+        n = size(match,2);
+        precision = (n-sum(abs(result(1,:)-match(1,:))>0))/n;
+        assert(precision>0.9);
+        assert(sum(~(match(1,:)-result(1,:)).*(dists(1,:)-ndists(1,:)))==0);
+    end
+    run_test('search with autotune', at test_autotune_search);
+    
+    function test_index_kdtree_search
+        [index, search_params ] = flann_build_index(dataset, struct('algorithm','kdtree', 'trees',8,...
+                                                          'checks',64));                                             
+        [result, ndists] = flann_search(index, testset, 10, search_params);
+        n = size(match,2);      
+        precision = (n-sum(abs(result(1,:)-match(1,:))>0))/n;
+        assert(precision>0.9);
+        assert(sum(~(match(1,:)-result(1,:)).*(dists(1,:)-ndists(1,:)))==0);
+    end
+    run_test('index kd-tree search', at test_index_kdtree_search);
+    
+    function test_index_kmeans_search
+        [index, search_params ] = flann_build_index(dataset, struct('algorithm','kmeans',...
+                                                          'branching',32,...
+                                                          'iterations',3,...
+                                                          'checks',120));                                             
+        [result, ndists] = flann_search(index, testset, 10, search_params);
+        n = size(match,2);      
+        precision = (n-sum(abs(result(1,:)-match(1,:))>0))/n;
+        assert(precision>0.9);
+        assert(sum(~(match(1,:)-result(1,:)).*(dists(1,:)-ndists(1,:)))==0);
+    end
+    run_test('index kmeans search', at test_index_kmeans_search);
+    
+    function test_index_kmeans_search_gonzales
+        [index, search_params ] = flann_build_index(dataset, struct('algorithm','kmeans',...
+                                                          'branching',32,...
+                                                          'iterations',3,...
+                                                          'checks',120,...
+                                                          'centers_init','gonzales'));                                             
+        [result, ndists] = flann_search(index, testset, 10, search_params);
+        n = size(match,2);      
+        precision = (n-sum(abs(result(1,:)-match(1,:))>0))/n;
+        assert(precision>0.9);
+        assert(sum(~(match(1,:)-result(1,:)).*(dists(1,:)-ndists(1,:)))==0);
+    end
+    run_test('index kmeans search gonzales', at test_index_kmeans_search_gonzales);
+    
+    function test_index_kmeans_search_kmeanspp
+        [index, search_params ] = flann_build_index(dataset, struct('algorithm','kmeans',...
+                                                          'branching',32,...
+                                                          'iterations',3,...
+                                                          'checks',120,...
+                                                          'centers_init','kmeanspp'));                                             
+        [result, ndists] = flann_search(index, testset, 10, search_params);
+        n = size(match,2);      
+        precision = (n-sum(abs(result(1,:)-match(1,:))>0))/n;
+        assert(precision>0.9);
+        assert(sum(~(match(1,:)-result(1,:)).*(dists(1,:)-ndists(1,:)))==0);
+    end
+    run_test('index kmeans search kmeanspp', at test_index_kmeans_search_kmeanspp);
+
+    function test_index_composite_search
+        [index, search_params ] = flann_build_index(dataset,struct('algorithm','composite',...
+                                                          'branching',32,...
+                                                          'iterations',3,...
+                                                          'trees', 1,...
+                                                          'checks',64));                                             
+        [result, ndists] = flann_search(index, testset, 10, search_params);
+        n = size(match,2);      
+        precision = (n-sum(abs(result(1,:)-match(1,:))>0))/n;
+        assert(precision>0.9);
+        assert(sum(~(match(1,:)-result(1,:)).*(dists(1,:)-ndists(1,:)))==0);
+    end
+    run_test('index composite search', at test_index_composite_search);
+    
+   function test_index_autotune_search
+        [index, search_params, speedup ] = flann_build_index(dataset,struct('algorithm','autotuned',...
+                                                          'target_precision',0.95,...
+                                                          'build_weight',0.01,...
+                                                          'memory_weight',0));
+        [result, ndists] = flann_search(index, testset, 10, search_params);
+        n = size(match,2);      
+        precision = (n-sum(abs(result(1,:)-match(1,:))>0))/n;
+        assert(precision>0.9);
+        assert(sum(~(match(1,:)-result(1,:)).*(dists(1,:)-ndists(1,:)))==0);
+    end
+    run_test('index autotune search', at test_index_autotune_search);    
+    
+    status();
+end
diff --git a/src/python/CMakeLists.txt b/src/python/CMakeLists.txt
new file mode 100644
index 0000000..5057221
--- /dev/null
+++ b/src/python/CMakeLists.txt
@@ -0,0 +1,12 @@
+configure_file( setup.py.tpl setup.py )
+
+install( DIRECTORY pyflann DESTINATION share/flann/python )
+install( FILES ${CMAKE_CURRENT_BINARY_DIR}/setup.py DESTINATION share/flann/python )
+
+
+# python instalation
+if (PYTHON_EXECUTABLE)
+    install(CODE "execute_process(
+        COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/setup.py install
+        WORKING_DIRECTORY \"${CMAKE_CURRENT_SOURCE_DIR}\")")
+endif()
diff --git a/src/python/pyflann/__init__.py b/src/python/pyflann/__init__.py
new file mode 100644
index 0000000..7636aa3
--- /dev/null
+++ b/src/python/pyflann/__init__.py
@@ -0,0 +1,31 @@
+#Copyright 2008-2009  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+#Copyright 2008-2009  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+#
+#THE BSD LICENSE
+#
+#Redistribution and use in source and binary forms, with or without
+#modification, are permitted provided that the following conditions
+#are met:
+#
+#1. Redistributions of source code must retain the above copyright
+#   notice, this list of conditions and the following disclaimer.
+#2. Redistributions in binary form must reproduce the above copyright
+#   notice, this list of conditions and the following disclaimer in the
+#   documentation and/or other materials provided with the distribution.
+#
+#THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+#IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+#OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+#IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+#INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+#NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+#DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+#THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+#(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+#THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#import sys
+#import os
+#sys.path.insert(0, os.path.split(__file__)[0]) # make python3 happy
+
+from pyflann.index import *
diff --git a/src/python/pyflann/exceptions.py b/src/python/pyflann/exceptions.py
new file mode 100644
index 0000000..8be34c1
--- /dev/null
+++ b/src/python/pyflann/exceptions.py
@@ -0,0 +1,34 @@
+#Copyright 2008-2009  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+#Copyright 2008-2009  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+#
+#THE BSD LICENSE
+#
+#Redistribution and use in source and binary forms, with or without
+#modification, are permitted provided that the following conditions
+#are met:
+#
+#1. Redistributions of source code must retain the above copyright
+#   notice, this list of conditions and the following disclaimer.
+#2. Redistributions in binary form must reproduce the above copyright
+#   notice, this list of conditions and the following disclaimer in the
+#   documentation and/or other materials provided with the distribution.
+#
+#THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+#IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+#OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+#IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+#INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+#NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+#DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+#THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+#(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+#THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+
+# This module defines exceptions that are used by the flann python bindings
+
+class FLANNException(Exception):
+    def __init__(self, *args):
+        Exception.__init__(self, *args)
+
+
diff --git a/src/python/pyflann/flann_ctypes.py b/src/python/pyflann/flann_ctypes.py
new file mode 100644
index 0000000..adfe126
--- /dev/null
+++ b/src/python/pyflann/flann_ctypes.py
@@ -0,0 +1,378 @@
+#Copyright 2008-2009  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+#Copyright 2008-2009  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+#
+#THE BSD LICENSE
+#
+#Redistribution and use in source and binary forms, with or without
+#modification, are permitted provided that the following conditions
+#are met:
+#
+#1. Redistributions of source code must retain the above copyright
+#   notice, this list of conditions and the following disclaimer.
+#2. Redistributions in binary form must reproduce the above copyright
+#   notice, this list of conditions and the following disclaimer in the
+#   documentation and/or other materials provided with the distribution.
+#
+#THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+#IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+#OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+#IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+#INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+#NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+#DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+#THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+#(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+#THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+from ctypes import *
+#from ctypes.util import find_library
+from numpy import float32, float64, uint8, int32, matrix, array, empty, reshape, require
+from numpy.ctypeslib import load_library, ndpointer
+import os
+import sys
+
+STRING = c_char_p
+
+
+class CustomStructure(Structure):
+    """
+        This class extends the functionality of the ctype's structure
+        class by adding custom default values to the fields and a way of translating
+        field types.
+    """
+    _defaults_ = {}
+    _translation_ = {}
+    
+    def __init__(self):
+        Structure.__init__(self)
+        self.__field_names = [ f for (f,t) in self._fields_]
+        self.update(self._defaults_)    
+    
+    def update(self, dict):
+        for k,v in dict.items():
+            if k in self.__field_names:
+                setattr(self,k,self.__translate(k,v))
+    
+    def __getitem__(self, k):
+        if k in self.__field_names:
+            return self.__translate_back(k,getattr(self,k))
+        
+    def __setitem__(self, k, v):
+        if k in self.__field_names:
+            setattr(self,k,self.__translate(k,v))
+        else:
+            raise KeyError("No such member: "+k)
+    
+    def keys(self):
+        return self.__field_names 
+
+    def __translate(self,k,v):
+        if k in self._translation_:
+            if v in self._translation_[k]:
+                return self._translation_[k][v]
+        return v        
+
+    def __translate_back(self,k,v):
+        if k in self._translation_:
+            for tk,tv in self._translation_[k].items():
+                if tv==v:
+                    return tk
+        return v        
+
+class FLANNParameters(CustomStructure):
+    _fields_ = [
+        ('algorithm', c_int),
+        ('checks', c_int),
+        ('eps', c_float),
+        ('sorted', c_int),
+        ('max_neighbors', c_int),
+        ('cores', c_int),
+        ('trees', c_int),
+        ('leaf_max_size', c_int),
+        ('branching', c_int),
+        ('iterations', c_int),
+        ('centers_init', c_int),
+        ('cb_index', c_float),
+        ('target_precision', c_float),
+        ('build_weight', c_float),
+        ('memory_weight', c_float),
+        ('sample_fraction', c_float),
+        ('table_number_', c_uint),
+        ('key_size_', c_uint),
+        ('multi_probe_level_', c_uint),
+        ('log_level', c_int),
+        ('random_seed', c_long),
+    ]
+    _defaults_ = {
+        'algorithm' : 'kdtree',
+        'checks' : 32,
+        'eps' : 0.0,
+        'sorted' : 1,
+        'max_neighbors' : -1,
+        'cores' : 0,
+        'trees' : 1,
+        'leaf_max_size' : 4,
+        'branching' : 32,
+        'iterations' : 5,
+        'centers_init' : 'random',
+        'cb_index' : 0.5,
+        'target_precision' : 0.9,
+        'build_weight' : 0.01,
+        'memory_weight' : 0.0,
+        'sample_fraction' : 0.1,
+        'table_number_': 12,
+        'key_size_': 20,
+        'multi_probe_level_': 2,
+        'log_level' : "warning",
+        'random_seed' : -1
+  }
+    _translation_ = {
+            "algorithm"     : {"linear"    : 0, "kdtree"    : 1, "kmeans"    : 2, "composite" : 3, "kdtree_single" : 4, "hierarchical": 5, "lsh": 6, "saved": 254, "autotuned" : 255, "default"   : 1},
+        "centers_init"  : {"random"    : 0, "gonzales"  : 1, "kmeanspp"  : 2, "default"   : 0},
+        "log_level"     : {"none"      : 0, "fatal"     : 1, "error"     : 2, "warning"   : 3, "info"      : 4, "default"   : 2}
+    }
+    
+    
+default_flags = ['C_CONTIGUOUS', 'ALIGNED']
+allowed_types = [ float32, float64, uint8, int32]   
+
+FLANN_INDEX = c_void_p
+
+def load_flann_library():
+
+    root_dir = os.path.abspath(os.path.dirname(__file__))
+    
+    libnames = ['libflann.so']
+    libdir = 'lib'
+    if sys.platform == 'win32':
+        libnames = ['flann.dll', 'libflann.dll']
+    elif sys.platform == 'darwin':
+        libnames = ['libflann.dylib']
+
+    while root_dir!=None:
+        for libname in libnames:
+            try:
+                #print "Trying ",os.path.join(root_dir,'lib',libname)
+                flannlib = cdll[os.path.join(root_dir,libdir,libname)]
+                return flannlib
+            except Exception:
+                pass
+            try:
+                flannlib = cdll[os.path.join(root_dir,"build",libdir,libname)]
+                return flannlib
+            except Exception:
+                pass
+        tmp = os.path.dirname(root_dir)
+        if tmp == root_dir:
+            root_dir = None
+        else:
+            root_dir = tmp
+
+    # if we didn't find the library so far, try loading without
+    # a full path as a last resort
+    for libname in libnames:
+        try:
+            #print "Trying",libname
+            flannlib=cdll[libname]
+            return flannlib
+        except:
+            pass
+
+    return None
+
+flannlib = load_flann_library()
+if flannlib == None:
+    raise ImportError('Cannot load dynamic library. Did you compile FLANN?')
+
+class FlannLib: pass
+flann = FlannLib()
+
+
+flannlib.flann_log_verbosity.restype = None
+flannlib.flann_log_verbosity.argtypes = [ 
+        c_int # level
+]
+
+
+
+flannlib.flann_set_distance_type.restype = None
+flannlib.flann_set_distance_type.argtypes = [ 
+        c_int,
+        c_int,        
+]
+
+type_mappings = ( ('float','float32'),
+                  ('double','float64'),
+                  ('byte','uint8'),
+                  ('int','int32') )
+
+def define_functions(str):
+    for type in type_mappings:
+        eval(compile(str%{'C':type[0],'numpy':type[1]},"<string>","exec"))
+
+flann.build_index = {}
+define_functions(r"""
+flannlib.flann_build_index_%(C)s.restype = FLANN_INDEX
+flannlib.flann_build_index_%(C)s.argtypes = [ 
+        ndpointer(%(numpy)s, ndim = 2, flags='aligned, c_contiguous'), # dataset
+        c_int, # rows
+        c_int, # cols
+        POINTER(c_float), # speedup 
+        POINTER(FLANNParameters)  # flann_params
+]
+flann.build_index[%(numpy)s] = flannlib.flann_build_index_%(C)s
+""")
+
+flann.save_index = {}
+define_functions(r"""
+flannlib.flann_save_index_%(C)s.restype = None
+flannlib.flann_save_index_%(C)s.argtypes = [
+        FLANN_INDEX, # index_id
+        c_char_p #filename                                   
+] 
+flann.save_index[%(numpy)s] = flannlib.flann_save_index_%(C)s
+""")
+
+flann.load_index = {}
+define_functions(r"""
+flannlib.flann_load_index_%(C)s.restype = FLANN_INDEX
+flannlib.flann_load_index_%(C)s.argtypes = [
+        c_char_p, #filename                                   
+        ndpointer(%(numpy)s, ndim = 2, flags='aligned, c_contiguous'), # dataset
+        c_int, # rows
+        c_int, # cols
+]
+flann.load_index[%(numpy)s] = flannlib.flann_load_index_%(C)s
+""")
+
+flann.find_nearest_neighbors = {}    
+define_functions(r"""                          
+flannlib.flann_find_nearest_neighbors_%(C)s.restype = c_int
+flannlib.flann_find_nearest_neighbors_%(C)s.argtypes = [ 
+        ndpointer(%(numpy)s, ndim = 2, flags='aligned, c_contiguous'), # dataset
+        c_int, # rows
+        c_int, # cols
+        ndpointer(%(numpy)s, ndim = 2, flags='aligned, c_contiguous'), # testset
+        c_int,  # tcount
+        ndpointer(int32, ndim = 2, flags='aligned, c_contiguous, writeable'), # result
+        ndpointer(float32, ndim = 2, flags='aligned, c_contiguous, writeable'), # dists
+        c_int, # nn
+        POINTER(FLANNParameters)  # flann_params
+]
+flann.find_nearest_neighbors[%(numpy)s] = flannlib.flann_find_nearest_neighbors_%(C)s
+""")
+
+# fix definition for the 'double' case
+
+flannlib.flann_find_nearest_neighbors_double.restype = c_int
+flannlib.flann_find_nearest_neighbors_double.argtypes = [ 
+        ndpointer(float64, ndim = 2, flags='aligned, c_contiguous'), # dataset
+        c_int, # rows
+        c_int, # cols
+        ndpointer(float64, ndim = 2, flags='aligned, c_contiguous'), # testset
+        c_int,  # tcount
+        ndpointer(int32, ndim = 2, flags='aligned, c_contiguous, writeable'), # result
+        ndpointer(float64, ndim = 2, flags='aligned, c_contiguous, writeable'), # dists
+        c_int, # nn
+        POINTER(FLANNParameters)  # flann_params
+]
+flann.find_nearest_neighbors[float64] = flannlib.flann_find_nearest_neighbors_double
+
+
+flann.find_nearest_neighbors_index = {}
+define_functions(r"""
+flannlib.flann_find_nearest_neighbors_index_%(C)s.restype = c_int
+flannlib.flann_find_nearest_neighbors_index_%(C)s.argtypes = [ 
+        FLANN_INDEX, # index_id
+        ndpointer(%(numpy)s, ndim = 2, flags='aligned, c_contiguous'), # testset
+        c_int,  # tcount
+        ndpointer(int32, ndim = 2, flags='aligned, c_contiguous, writeable'), # result
+        ndpointer(float32, ndim = 2, flags='aligned, c_contiguous, writeable'), # dists
+        c_int, # nn
+        POINTER(FLANNParameters) # flann_params
+]
+flann.find_nearest_neighbors_index[%(numpy)s] = flannlib.flann_find_nearest_neighbors_index_%(C)s
+""")
+
+flannlib.flann_find_nearest_neighbors_index_double.restype = c_int
+flannlib.flann_find_nearest_neighbors_index_double.argtypes = [ 
+        FLANN_INDEX, # index_id
+        ndpointer(float64, ndim = 2, flags='aligned, c_contiguous'), # testset
+        c_int,  # tcount
+        ndpointer(int32, ndim = 2, flags='aligned, c_contiguous, writeable'), # result
+        ndpointer(float64, ndim = 2, flags='aligned, c_contiguous, writeable'), # dists
+        c_int, # nn
+        POINTER(FLANNParameters) # flann_params
+]
+flann.find_nearest_neighbors_index[float64] = flannlib.flann_find_nearest_neighbors_index_double
+
+flann.radius_search = {}
+define_functions(r"""
+flannlib.flann_radius_search_%(C)s.restype = c_int
+flannlib.flann_radius_search_%(C)s.argtypes = [ 
+        FLANN_INDEX, # index_id
+        ndpointer(%(numpy)s, ndim = 1, flags='aligned, c_contiguous'), # query
+        ndpointer(int32, ndim = 1, flags='aligned, c_contiguous, writeable'), # indices
+        ndpointer(float32, ndim = 1, flags='aligned, c_contiguous, writeable'), # dists
+        c_int, # max_nn
+        c_float, # radius
+        POINTER(FLANNParameters) # flann_params
+]
+flann.radius_search[%(numpy)s] = flannlib.flann_radius_search_%(C)s
+""")
+
+flannlib.flann_radius_search_double.restype = c_int
+flannlib.flann_radius_search_double.argtypes = [ 
+        FLANN_INDEX, # index_id
+        ndpointer(float64, ndim = 1, flags='aligned, c_contiguous'), # query
+        ndpointer(int32, ndim = 1, flags='aligned, c_contiguous, writeable'), # indices
+        ndpointer(float64, ndim = 1, flags='aligned, c_contiguous, writeable'), # dists
+        c_int, # max_nn
+        c_float, # radius
+        POINTER(FLANNParameters) # flann_params
+]
+flann.radius_search[float64] = flannlib.flann_radius_search_double
+
+
+flann.compute_cluster_centers = {}
+define_functions(r"""
+flannlib.flann_compute_cluster_centers_%(C)s.restype = c_int
+flannlib.flann_compute_cluster_centers_%(C)s.argtypes = [ 
+        ndpointer(%(numpy)s, ndim = 2, flags='aligned, c_contiguous'), # dataset
+        c_int,  # rows
+        c_int,  # cols
+        c_int,  # clusters 
+        ndpointer(float32, flags='aligned, c_contiguous, writeable'), # result
+        POINTER(FLANNParameters)  # flann_params
+]
+flann.compute_cluster_centers[%(numpy)s] = flannlib.flann_compute_cluster_centers_%(C)s
+""")
+# double is an exception
+flannlib.flann_compute_cluster_centers_double.restype = c_int
+flannlib.flann_compute_cluster_centers_double.argtypes = [ 
+        ndpointer(float64, ndim = 2, flags='aligned, c_contiguous'), # dataset
+        c_int,  # rows
+        c_int,  # cols
+        c_int,  # clusters 
+        ndpointer(float64, flags='aligned, c_contiguous, writeable'), # result
+        POINTER(FLANNParameters)  # flann_params
+]
+flann.compute_cluster_centers[float64] = flannlib.flann_compute_cluster_centers_double
+
+
+flann.free_index = {}
+define_functions(r"""
+flannlib.flann_free_index_%(C)s.restype = None
+flannlib.flann_free_index_%(C)s.argtypes = [ 
+        FLANN_INDEX,  # index_id
+        POINTER(FLANNParameters) # flann_params
+]
+flann.free_index[%(numpy)s] = flannlib.flann_free_index_%(C)s
+""")
+
+
+def ensure_2d_array(array, flags, **kwargs):
+    array = require(array, requirements = flags, **kwargs) 
+    if len(array.shape) == 1:
+        array = array.reshape(-1,array.size)
+    return array
diff --git a/src/python/pyflann/index.py b/src/python/pyflann/index.py
new file mode 100644
index 0000000..a587f34
--- /dev/null
+++ b/src/python/pyflann/index.py
@@ -0,0 +1,402 @@
+#Copyright 2008-2010  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+#Copyright 2008-2010  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+#
+#THE BSD LICENSE
+#
+#Redistribution and use in source and binary forms, with or without
+#modification, are permitted provided that the following conditions
+#are met:
+#
+#1. Redistributions of source code must retain the above copyright
+#   notice, this list of conditions and the following disclaimer.
+#2. Redistributions in binary form must reproduce the above copyright
+#   notice, this list of conditions and the following disclaimer in the
+#   documentation and/or other materials provided with the distribution.
+#
+#THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+#IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+#OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+#IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+#INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+#NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+#DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+#THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+#(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+#THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+from pyflann.flann_ctypes import *
+from pyflann.exceptions import *
+import numpy.random as _rn
+
+
+index_type = int32
+
+def set_distance_type(distance_type, order = 0):
+    """
+    Sets the distance type used. Possible values: euclidean, manhattan, minkowski, max_dist, 
+    hik, hellinger, cs, kl.
+    """
+    
+    distance_translation = { "euclidean" : 1, 
+                            "manhattan" : 2, 
+                            "minkowski" : 3,
+                            "max_dist" : 4,
+                            "hik" : 5,
+                            "hellinger" : 6,
+                            "chi_square" : 7,
+                            "cs" : 7,
+                            "kullback_leibler" : 8,
+                            "kl" : 8,
+                            }
+    if type(distance_type)==str:
+        distance_type = distance_translation[distance_type]
+
+    flannlib.flann_set_distance_type(distance_type,order)
+
+
+
+def to_bytes(string):
+    if sys.hexversion > 0x03000000:
+        return bytes(string,'utf-8')
+    return string
+
+# This class is derived from an initial implementation by Hoyt Koepke (hoytak at cs.ubc.ca)
+class FLANN:
+    """
+    This class defines a python interface to the FLANN lirary.
+    """
+    __rn_gen = _rn.RandomState()
+    
+    _as_parameter_ = property( lambda self: self.__curindex )
+
+    def __init__(self, **kwargs):
+        """
+        Constructor for the class and returns a class that can bind to
+        the flann libraries.  Any keyword arguments passed to __init__
+        override the global defaults given.
+        """
+        
+        self.__rn_gen.seed()
+
+        self.__curindex = None
+        self.__curindex_data = None
+        self.__curindex_type = None
+        
+        self.__flann_parameters = FLANNParameters()        
+        self.__flann_parameters.update(kwargs)
+
+    def __del__(self):
+        self.delete_index()
+
+        
+    ################################################################################
+    # actual workhorse functions
+
+    def nn(self, pts, qpts, num_neighbors = 1, **kwargs):
+        """
+        Returns the num_neighbors nearest points in dataset for each point
+        in testset.
+        """
+        
+        if not pts.dtype.type in allowed_types:
+            raise FLANNException("Cannot handle type: %s"%pts.dtype)
+
+        if not qpts.dtype.type in allowed_types:
+            raise FLANNException("Cannot handle type: %s"%pts.dtype)
+
+        if pts.dtype != qpts.dtype:
+            raise FLANNException("Data and query must have the same type")
+        
+        pts = ensure_2d_array(pts,default_flags) 
+        qpts = ensure_2d_array(qpts,default_flags) 
+
+        npts, dim = pts.shape
+        nqpts = qpts.shape[0]
+
+        assert(qpts.shape[1] == dim)
+        assert(npts >= num_neighbors)
+
+        result = empty( (nqpts, num_neighbors), dtype=index_type)
+        if pts.dtype==float64:
+            dists = empty( (nqpts, num_neighbors), dtype=float64)
+        else:
+            dists = empty( (nqpts, num_neighbors), dtype=float32)
+                
+        self.__flann_parameters.update(kwargs)
+
+        flann.find_nearest_neighbors[pts.dtype.type](pts, npts, dim, 
+                                                     qpts, nqpts, result, dists, num_neighbors, 
+                                                     pointer(self.__flann_parameters))
+
+        if num_neighbors == 1:
+            return (result.reshape( nqpts ), dists.reshape(nqpts))
+        else:
+            return (result,dists)
+
+
+    def build_index(self, pts, **kwargs):
+        """
+        This builds and internally stores an index to be used for
+        future nearest neighbor matchings.  It erases any previously
+        stored indexes, so use multiple instances of this class to
+        work with multiple stored indices.  Use nn_index(...) to find
+        the nearest neighbors in this index.
+
+        pts is a 2d numpy array or matrix. All the computation is done
+        in float32 type, but pts may be any type that is convertable
+        to float32. 
+        """
+        
+        if not pts.dtype.type in allowed_types:
+            raise FLANNException("Cannot handle type: %s"%pts.dtype)
+
+        pts = ensure_2d_array(pts,default_flags) 
+        npts, dim = pts.shape
+        
+        self.__ensureRandomSeed(kwargs)
+        
+        self.__flann_parameters.update(kwargs)
+
+        if self.__curindex != None:
+            flann.free_index[self.__curindex_type](self.__curindex, pointer(self.__flann_parameters))
+            self.__curindex = None
+                
+        speedup = c_float(0)
+        self.__curindex = flann.build_index[pts.dtype.type](pts, npts, dim, byref(speedup), pointer(self.__flann_parameters))
+        self.__curindex_data = pts
+        self.__curindex_type = pts.dtype.type
+        
+        params = dict(self.__flann_parameters)
+        params["speedup"] = speedup.value
+        
+        return params
+
+
+    def save_index(self, filename):
+        """
+        This saves the index to a disk file.
+        """
+        if self.__curindex != None:
+            flann.save_index[self.__curindex_type](self.__curindex, c_char_p(to_bytes(filename)))
+
+    def load_index(self, filename, pts):
+        """
+        Loads an index previously saved to disk.
+        """
+                
+        if not pts.dtype.type in allowed_types:
+            raise FLANNException("Cannot handle type: %s"%pts.dtype)
+
+        pts = ensure_2d_array(pts,default_flags) 
+        npts, dim = pts.shape
+
+        if self.__curindex != None:
+            flann.free_index[self.__curindex_type](self.__curindex, pointer(self.__flann_parameters))
+            self.__curindex = None
+            self.__curindex_data = None
+            self.__curindex_type = None
+        
+        self.__curindex = flann.load_index[pts.dtype.type](c_char_p(to_bytes(filename)), pts, npts, dim)
+        self.__curindex_data = pts
+        self.__curindex_type = pts.dtype.type
+
+    def nn_index(self, qpts, num_neighbors = 1, **kwargs):
+        """
+        For each point in querypts, (which may be a single point), it
+        returns the num_neighbors nearest points in the index built by
+        calling build_index.
+        """
+
+        if self.__curindex == None:
+            raise FLANNException("build_index(...) method not called first or current index deleted.")
+
+        if not qpts.dtype.type in allowed_types:
+            raise FLANNException("Cannot handle type: %s"%qpts.dtype)
+
+        if self.__curindex_type != qpts.dtype.type:
+            raise FLANNException("Index and query must have the same type")
+
+        qpts = ensure_2d_array(qpts,default_flags) 
+
+        npts, dim = self.__curindex_data.shape
+
+        if qpts.size == dim:
+            qpts.reshape(1, dim)
+
+        nqpts = qpts.shape[0]
+
+        assert(qpts.shape[1] == dim)
+        assert(npts >= num_neighbors)
+        
+        result = empty( (nqpts, num_neighbors), dtype=index_type)
+        if self.__curindex_type==float64:
+            dists = empty( (nqpts, num_neighbors), dtype=float64)
+        else:
+            dists = empty( (nqpts, num_neighbors), dtype=float32)
+
+        self.__flann_parameters.update(kwargs)
+
+        flann.find_nearest_neighbors_index[self.__curindex_type](self.__curindex, 
+                    qpts, nqpts,
+                    result, dists, num_neighbors,
+                    pointer(self.__flann_parameters))
+
+        if num_neighbors == 1:
+            return (result.reshape( nqpts ), dists.reshape( nqpts ))
+        else:
+            return (result,dists)
+        
+        
+    def nn_radius(self, query, radius, **kwargs):
+        
+        if self.__curindex == None:
+            raise FLANNException("build_index(...) method not called first or current index deleted.")
+
+        if not query.dtype.type in allowed_types:
+            raise FLANNException("Cannot handle type: %s"%query.dtype)
+
+        if self.__curindex_type != query.dtype.type:
+            raise FLANNException("Index and query must have the same type")
+
+        npts, dim = self.__curindex_data.shape        
+        assert(query.shape[0]==dim)
+        
+        result = empty( npts, dtype=index_type)
+        if self.__curindex_type==float64:
+            dists = empty( npts, dtype=float64)
+        else:
+            dists = empty( npts, dtype=float32)
+        
+        self.__flann_parameters.update(kwargs)
+
+        nn = flann.radius_search[self.__curindex_type](self.__curindex, query, 
+                                         result, dists, npts,
+                                         radius, pointer(self.__flann_parameters))
+        
+        
+        return (result[0:nn],dists[0:nn])
+
+    def delete_index(self, **kwargs):
+        """
+        Deletes the current index freeing all the momory it uses. 
+        The memory used by the dataset that was indexed is not freed.
+        """
+
+        self.__flann_parameters.update(kwargs)
+        
+        if self.__curindex != None:
+            flann.free_index[self.__curindex_type](self.__curindex, pointer(self.__flann_parameters))
+            self.__curindex = None
+            self.__curindex_data = None
+
+    ##########################################################################################
+    # Clustering functions
+
+    def kmeans(self, pts, num_clusters, max_iterations = None,
+               dtype = None, **kwargs):
+        """
+        Runs kmeans on pts with num_clusters centroids.  Returns a
+        numpy array of size num_clusters x dim.  
+
+        If max_iterations is not None, the algorithm terminates after
+        the given number of iterations regardless of convergence.  The
+        default is to run until convergence.
+
+        If dtype is None (the default), the array returned is the same
+        type as pts.  Otherwise, the returned array is of type dtype.  
+
+        """
+        
+        if int(num_clusters) != num_clusters or num_clusters < 1:
+            raise FLANNException('num_clusters must be an integer >= 1')
+        
+        if num_clusters == 1:
+            if dtype == None or dtype == pts.dtype:
+                return mean(pts, 0).reshape(1, pts.shape[1])
+            else:
+                return dtype(mean(pts, 0).reshape(1, pts.shape[1]))
+
+        return self.hierarchical_kmeans(pts, int(num_clusters), 1, 
+                                        max_iterations, 
+                                        dtype, **kwargs)
+        
+    def hierarchical_kmeans(self, pts, branch_size, num_branches,
+                            max_iterations = None, 
+                            dtype = None, **kwargs):
+        """
+        Clusters the data by using multiple runs of kmeans to
+        recursively partition the dataset.  The number of resulting
+        clusters is given by (branch_size-1)*num_branches+1.
+        
+        This method can be significantly faster when the number of
+        desired clusters is quite large (e.g. a hundred or more).
+        Higher branch sizes are slower but may give better results.
+
+        If dtype is None (the default), the array returned is the same
+        type as pts.  Otherwise, the returned array is of type dtype.  
+        
+        """
+        
+        # First verify the paremeters are sensible.
+
+        if not pts.dtype.type in allowed_types:
+            raise FLANNException("Cannot handle type: %s"%pts.dtype)
+
+        if int(branch_size) != branch_size or branch_size < 2:
+            raise FLANNException('branch_size must be an integer >= 2.')
+
+        branch_size = int(branch_size)
+
+        if int(num_branches) != num_branches or num_branches < 1:
+            raise FLANNException('num_branches must be an integer >= 1.')
+
+        num_branches = int(num_branches)
+
+        if max_iterations == None: 
+            max_iterations = -1
+        else:
+            max_iterations = int(max_iterations)
+
+
+        # init the arrays and starting values
+        pts = ensure_2d_array(pts,default_flags) 
+        npts, dim = pts.shape
+        num_clusters = (branch_size-1)*num_branches+1;
+        
+        if pts.dtype.type == float64:
+            result = empty( (num_clusters, dim), dtype=float64)
+        else:
+            result = empty( (num_clusters, dim), dtype=float32)
+
+        # set all the parameters appropriately
+        
+        self.__ensureRandomSeed(kwargs)
+        
+        params = {"iterations"       : max_iterations,
+                    "algorithm"        : 'kmeans',
+                    "branching"        : branch_size,
+                    "random_seed"      : kwargs['random_seed']}
+        
+        self.__flann_parameters.update(params)
+        
+        numclusters = flann.compute_cluster_centers[pts.dtype.type](pts, npts, dim,
+                                        num_clusters, result, 
+                                        pointer(self.__flann_parameters))
+        if numclusters <= 0:
+            raise FLANNException('Error occured during clustering procedure.')
+
+        if dtype == None:
+            return result
+        else:
+            return dtype(result)
+        
+    ##########################################################################################
+    # internal bookkeeping functions
+
+        
+    def __ensureRandomSeed(self, kwargs):
+        if not 'random_seed' in kwargs:
+            kwargs['random_seed'] = self.__rn_gen.randint(2**30)
+        
+
+
diff --git a/src/python/setup.py.tpl b/src/python/setup.py.tpl
new file mode 100644
index 0000000..bb73744
--- /dev/null
+++ b/src/python/setup.py.tpl
@@ -0,0 +1,28 @@
+#!/usr/bin/env python2
+
+from distutils.core import setup
+from os.path import exists, abspath, dirname, join
+import os
+import sys
+
+
+def find_path():
+    lib_paths = [ os.path.abspath('@LIBRARY_OUTPUT_PATH@'), abspath(join(dirname(dirname(sys.argv[0])), '../../../lib')) ]
+    possible_libs = ['libflann.so', 'flann.dll', 'libflann.dll', 'libflann.dylib']
+
+    for path in lib_paths:
+        for lib in possible_libs:
+            if exists(join(path,lib)):
+                return path
+
+setup(name='flann',
+      version='@FLANN_VERSION@',
+      description='Fast Library for Approximate Nearest Neighbors',
+      author='Marius Muja',
+      author_email='mariusm at cs.ubc.ca',
+      license='BSD',
+      url='http://www.cs.ubc.ca/~mariusm/flann/',
+      packages=['pyflann', 'pyflann.lib'],
+      package_dir={'pyflann.lib': find_path() },
+      package_data={'pyflann.lib': ['libflann.so', 'flann.dll', 'libflann.dll', 'libflann.dylib']}, 
+)
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
new file mode 100644
index 0000000..72187dd
--- /dev/null
+++ b/test/CMakeLists.txt
@@ -0,0 +1,74 @@
+
+add_custom_target(tests)
+add_custom_target(flann_gtests)
+add_custom_target(test)
+add_custom_target(flann_gtest)
+
+add_dependencies(tests flann_gtests)
+add_dependencies(test flann_gtest)
+add_dependencies(test tests)
+
+set(EXECUTABLE_OUTPUT_PATH ${TEST_OUTPUT_PATH})
+
+if (PYTHON_EXECUTABLE) 
+    flann_download_test_data(sift10K.h5 0964a910946d2dd5fe28337507a8abc3)
+    flann_download_test_data(sift10K_byte.h5 f835e0148df4618a81f67febfda2b4d0)
+    flann_download_test_data(sift100K.h5 ae2b08f93f3d9f89f5d68566b0406102)
+    flann_download_test_data(sift100K_byte.h5 b772255fd2044e9d2a5a0183953e4705)
+    flann_download_test_data(cloud.h5 dfc77bad391a3ae739a9874f4a5dc0d7)
+    flann_download_test_data(brief100K.h5 e1e781c0955917bc2f0a27b6344c2342)
+endif()
+
+if (GTEST_FOUND AND HDF5_FOUND)
+	include_directories(${HDF5_INCLUDE_DIR})
+
+    set(TEST_LIBRARIES "${HDF5_LIBRARIES}")
+    if (HDF5_IS_PARALLEL)
+        set(TEST_LIBRARIES "${TEST_LIBRARIES};${MPI_LIBRARIES}")
+    endif()
+	
+    flann_add_gtest(flann_linear_test flann_linear_test.cpp)
+    target_link_libraries(flann_linear_test flann_cpp ${TEST_LIBRARIES})
+
+	flann_add_gtest(flann_kdtree_test flann_kdtree_test.cpp)
+    target_link_libraries(flann_kdtree_test flann_cpp ${TEST_LIBRARIES})
+
+	flann_add_gtest(flann_kmeans_test flann_kmeans_test.cpp)
+    target_link_libraries(flann_kmeans_test flann_cpp ${TEST_LIBRARIES})
+
+	flann_add_gtest(flann_kdtree_single_test flann_kdtree_single_test.cpp)
+    target_link_libraries(flann_kdtree_single_test flann_cpp ${TEST_LIBRARIES})
+
+	flann_add_gtest(flann_hierarchical_test flann_hierarchical_test.cpp)
+    target_link_libraries(flann_hierarchical_test flann_cpp ${TEST_LIBRARIES})
+
+	flann_add_gtest(flann_lsh_test flann_lsh_test.cpp)
+    target_link_libraries(flann_lsh_test flann_cpp ${TEST_LIBRARIES})
+
+    flann_add_gtest(flann_autotuned_test flann_autotuned_test.cpp)
+    target_link_libraries(flann_autotuned_test flann_cpp ${TEST_LIBRARIES})
+
+    if (OPENMP_FOUND)
+        flann_add_gtest(flann_multithreaded_test flann_multithreaded_test.cpp)
+        target_link_libraries(flann_multithreaded_test flann_cpp ${TEST_LIBRARIES})
+    endif()
+
+endif()
+
+if (GTEST_FOUND AND HDF5_FOUND AND BUILD_CUDA_LIB)
+	set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS};-Xcompiler;-fPIC;-arch=sm_13" )
+    if (NVCC_COMPILER_BINDIR)
+        set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS};--compiler-bindir=${NVCC_COMPILER_BINDIR}")
+    endif()
+	flann_add_cuda_gtest(flann_cuda_test flann_cuda_test.cu)
+	target_link_libraries(flann_cuda_test flann_cpp ${HDF5_LIBRARIES} flann_cuda)
+endif()
+
+#---------- pyunit tests --------------
+if (PYTHON_EXECUTABLE) 
+    flann_add_pyunit(test_nn.py)
+    flann_add_pyunit(test_nn_index.py)
+    flann_add_pyunit(test_index_save.py)
+    flann_add_pyunit(test_nn_autotune.py)
+    flann_add_pyunit(test_clustering.py)
+endif()
diff --git a/test/flann_autotuned_test.cpp b/test/flann_autotuned_test.cpp
new file mode 100644
index 0000000..28f4fc7
--- /dev/null
+++ b/test/flann_autotuned_test.cpp
@@ -0,0 +1,187 @@
+#include <gtest/gtest.h>
+#include <time.h>
+
+#include <flann/flann.h>
+#include <flann/io/hdf5.h>
+#include <flann/nn/ground_truth.h>
+
+#include "flann_tests.h"
+
+using namespace flann;
+
+
+
+
+
+class Autotuned_SIFT100K : public FLANNTestFixture {
+protected:
+    flann::Matrix<float> data;
+    flann::Matrix<float> query;
+    flann::Matrix<size_t> match;
+    flann::Matrix<float> dists;
+    flann::Matrix<size_t> indices;
+
+    void SetUp()
+    {
+        dists = flann::Matrix<float>(new float[1000*5], 1000, 5);
+        indices = flann::Matrix<size_t>(new size_t[1000*5], 1000, 5);
+        printf("Reading test data...");
+        fflush(stdout);
+        flann::load_from_file(data, "sift100K.h5","dataset");
+        flann::load_from_file(query,"sift100K.h5","query");
+        flann::load_from_file(match,"sift100K.h5","match");
+        printf("done\n");
+    }
+
+    void TearDown()
+    {
+        delete[] data.ptr();
+        delete[] query.ptr();
+        delete[] match.ptr();
+        delete[] dists.ptr();
+        delete[] indices.ptr();
+    }
+};
+
+
+TEST_F(Autotuned_SIFT100K, TestSearch)
+{
+    flann::log_verbosity(FLANN_LOG_INFO);
+
+    Index<L2<float> > index(data, flann::AutotunedIndexParams(0.8,0.01,0,0.1)); // 80% precision
+
+    start_timer("Building autotuned index...");
+    index.buildIndex();
+    printf("done (%g seconds)\n", stop_timer());
+
+    index.save("autotuned.idx");
+
+    start_timer("Searching KNN...");
+    index.knnSearch(query, indices, dists, 5, flann::SearchParams(FLANN_CHECKS_AUTOTUNED) );
+    printf("done (%g seconds)\n", stop_timer());
+
+    float precision = compute_precision(match, indices);
+    EXPECT_GE(precision, 0.75);
+    printf("Precision: %g\n", precision);
+}
+
+
+TEST_F(Autotuned_SIFT100K, SavedTest)
+{
+    float precision;
+
+    // -------------------------------------
+    // autotuned index
+    printf("Loading autotuned index\n");
+    flann::Index<L2<float> > autotuned_index(data, flann::SavedIndexParams("autotuned.idx"));
+
+    const flann::IndexParams index_params = autotuned_index.getParameters();
+    printf("The index has the following parameters:\n");
+    flann::print_params(index_params);
+
+    printf("Index type is: %d\n", autotuned_index.getType());
+
+    start_timer("Searching KNN...");
+    autotuned_index.knnSearch(query, indices, dists, 5, flann::SearchParams(-2) );
+    printf("done (%g seconds)\n", stop_timer());
+
+    precision = compute_precision(match, indices);
+    EXPECT_GE(precision, 0.75);
+    printf("Precision: %g\n", precision);
+}
+
+TEST_F(Autotuned_SIFT100K, TestCopy)
+{
+    float precision;
+
+    // -------------------------------------
+    // autotuned index
+    printf("Loading autotuned index\n");
+    flann::Index<L2<float> > index(data, flann::SavedIndexParams("autotuned.idx"));
+
+    start_timer("Searching KNN...");
+    index.knnSearch(query, indices, dists, 5, flann::SearchParams(-2) );
+    printf("done (%g seconds)\n", stop_timer());
+
+    precision = compute_precision(match, indices);
+    EXPECT_GE(precision, 0.75);
+    printf("Precision: %g\n", precision);
+
+
+    // test copy constructor
+    Index<L2<float> > index2(index);
+
+    start_timer("Searching KNN...");
+    index2.knnSearch(query, indices, dists, 5, flann::SearchParams(-2) );
+    printf("done (%g seconds)\n", stop_timer());
+
+    float precision2 = compute_precision(match, indices);
+    printf("Precision: %g\n", precision2);
+    EXPECT_EQ(precision, precision2);
+
+    // test assignment operator
+    Index<L2<float> > index3(data, flann::LinearIndexParams());
+    index3 = index;
+
+    start_timer("Searching KNN...");
+    index3.knnSearch(query, indices, dists, 5, flann::SearchParams(-2) );
+    printf("done (%g seconds)\n", stop_timer());
+
+    float precision3 = compute_precision(match, indices);
+    printf("Precision: %g\n", precision3);
+    EXPECT_EQ(precision, precision3);
+}
+
+
+TEST_F(Autotuned_SIFT100K, TestCopy2)
+{
+    float precision;
+
+    // -------------------------------------
+    // autotuned index
+    printf("Loading autotuned index\n");
+    flann::AutotunedIndex<L2<float> > index(data);
+    FILE* f = fopen("autotuned.idx", "r");
+    index.loadIndex(f);
+    fclose(f);
+
+    start_timer("Searching KNN...");
+    index.knnSearch(query, indices, dists, 5, flann::SearchParams(-2) );
+    printf("done (%g seconds)\n", stop_timer());
+
+    precision = compute_precision(match, indices);
+    EXPECT_GE(precision, 0.75);
+    printf("Precision: %g\n", precision);
+
+
+    // test copy constructor
+    AutotunedIndex<L2<float> > index2(index);
+
+    start_timer("Searching KNN...");
+    index2.knnSearch(query, indices, dists, 5, flann::SearchParams(-2) );
+    printf("done (%g seconds)\n", stop_timer());
+
+    float precision2 = compute_precision(match, indices);
+    printf("Precision: %g\n", precision2);
+    EXPECT_EQ(precision, precision2);
+
+    // test assignment operator
+    AutotunedIndex<L2<float> > index3(data);
+    index3 = index;
+
+    start_timer("Searching KNN...");
+    index3.knnSearch(query, indices, dists, 5, flann::SearchParams(-2) );
+    printf("done (%g seconds)\n", stop_timer());
+
+    float precision3 = compute_precision(match, indices);
+    printf("Precision: %g\n", precision3);
+    EXPECT_EQ(precision, precision3);
+}
+
+
+
+int main(int argc, char** argv)
+{
+    testing::InitGoogleTest(&argc, argv);
+    return RUN_ALL_TESTS();
+}
diff --git a/test/flann_cuda_test.cu b/test/flann_cuda_test.cu
new file mode 100644
index 0000000..3045000
--- /dev/null
+++ b/test/flann_cuda_test.cu
@@ -0,0 +1,511 @@
+#include <gtest/gtest.h>
+#include <time.h>
+#define FLANN_USE_CUDA
+#include <flann/flann.h>
+#include <flann/io/hdf5.h>
+#include <flann/nn/ground_truth.h>
+#include <thrust/host_vector.h>
+#include <thrust/device_vector.h>
+#include <vector_functions.h>
+
+using namespace flann;
+
+
+float compute_precision(const flann::Matrix<int>& match, const flann::Matrix<int>& indices)
+{
+    int count = 0;
+
+    assert(match.rows == indices.rows);
+    size_t nn = std::min(match.cols, indices.cols);
+
+    for (size_t i=0; i<match.rows; ++i) {
+        for (size_t j=0;j<nn;++j) {
+            for (size_t k=0;k<nn;++k) {
+                if (match[i][j]==indices[i][k]) {
+                    count ++;
+                }
+                else
+				{
+// 					std::cout<<i<<":"<<match[i][j]<<"!="<<indices[i][k]<<std::endl;
+				}
+            }
+        }
+    }
+
+    return float(count)/(nn*match.rows);
+}
+
+struct smallerWithTolerance
+{
+	float tol;
+	bool operator()(float a, float b )
+	{
+		return a<(b-tol);
+	}
+};
+
+/** @brief Compare the distances for match accuracies
+ * This is more precise: e.g. when you ask for the top 10 neighbors and they all get the same distance,
+ * you might have 100 other neighbors that are at the same distance and simply matching the indices is not the way to go
+ * @param gt_dists the ground truth best distances
+ * @param dists the distances of the computed nearest neighbors
+ * @param tol tolerance at which distanceare considered equal
+ * @return
+ */
+template<typename T>
+float computePrecisionDiscrete(const flann::Matrix<T>& gt_dists, const flann::Matrix<T>& dists, float tol)
+{
+  int count = 0;
+
+  assert(gt_dists.rows == dists.rows);
+  size_t nn = std::min(gt_dists.cols, dists.cols);
+  std::vector<T> gt_sorted_dists(nn), sorted_dists(nn), intersection(nn);
+
+  smallerWithTolerance swt;
+  swt.tol=tol;
+  for (size_t i = 0; i < gt_dists.rows; ++i)
+  {
+    std::copy(gt_dists[i], gt_dists[i] + nn, gt_sorted_dists.begin());
+    std::sort(gt_sorted_dists.begin(), gt_sorted_dists.end());
+    std::copy(dists[i], dists[i] + nn, sorted_dists.begin());
+    std::sort(sorted_dists.begin(), sorted_dists.end());
+    typename std::vector<T>::iterator end = std::set_intersection(gt_sorted_dists.begin(), gt_sorted_dists.end(),
+                                                             sorted_dists.begin(), sorted_dists.end(),
+                                                             intersection.begin(),swt);
+    count += (end - intersection.begin());
+  }
+
+  return float(count) / (nn * gt_dists.rows);
+}
+
+class FLANNTestFixture : public ::testing::Test {
+protected:
+    clock_t start_time_;
+
+    void start_timer(const std::string& message = "")
+    {
+        if (!message.empty()) {
+            printf("%s", message.c_str());
+            fflush(stdout);
+        }
+        start_time_ = clock();
+    }
+
+    double stop_timer()
+    {
+        return double(clock()-start_time_)/CLOCKS_PER_SEC;
+    }
+
+};
+
+
+
+class Flann_3D : public FLANNTestFixture {
+protected:
+    flann::Matrix<float> data;
+    flann::Matrix<float> query;
+    flann::Matrix<int> match;
+    flann::Matrix<float> dists;
+    flann::Matrix<int> indices;
+
+    void SetUp()
+    {
+        printf("Reading test data...");
+        fflush(stdout);
+        flann::load_from_file(data, "cloud.h5","dataset");
+        flann::load_from_file(query,"cloud.h5","query");
+        flann::load_from_file(match,"cloud.h5","indices");
+
+        dists = flann::Matrix<float>(new float[query.rows*5], query.rows, 5);
+        indices = flann::Matrix<int>(new int[query.rows*5], query.rows, 5);
+        printf("done\n");
+    }
+
+    void TearDown()
+    {
+        delete[] data.ptr();
+        delete[] query.ptr();
+        delete[] match.ptr();
+        delete[] dists.ptr();
+        delete[] indices.ptr();
+    }
+};
+
+
+TEST_F(Flann_3D, KDTreeSingleTest)
+{
+    flann::Index<L2_Simple<float> > index(data, flann::KDTreeSingleIndexParams(12, false));
+    start_timer("Building kd-tree index...");
+    index.buildIndex();
+    printf("done (%g seconds)\n", stop_timer());
+
+    start_timer("Searching KNN...");
+    index.knnSearch(query, indices, dists, 5, flann::SearchParams(-1) );
+    printf("done (%g seconds)\n", stop_timer());
+
+    float precision = compute_precision(match, indices);
+    EXPECT_GE(precision, 0.99);
+    printf("Precision: %g\n", precision);
+}
+
+
+TEST_F(Flann_3D, KDTreeCudaTest)
+{
+    flann::Index<L2_Simple<float> > index(data, flann::KDTreeCuda3dIndexParams());
+    start_timer("Building kd-tree index...");
+    index.buildIndex();
+    printf("done (%g seconds)\n", stop_timer());
+
+    start_timer("Searching KNN...");
+    index.knnSearch(query, indices, dists, 5, flann::SearchParams(-1) );
+    printf("done (%g seconds)\n", stop_timer());
+
+    float precision = compute_precision(match, indices);
+    EXPECT_GE(precision, 0.99);
+    printf("Precision: %g\n", precision);
+}
+
+
+class Flann_3D_Random_Cloud : public FLANNTestFixture {
+protected:
+    flann::Matrix<float> data;
+    flann::Matrix<float> query;
+    flann::Matrix<float> dists;
+    flann::Matrix<int> indices;
+	flann::Matrix<float> gt_dists;
+	flann::Matrix<int> gt_indices;
+
+    void SetUp()
+    {
+		const int n_points=10000;
+		printf("creating random point cloud (%d points)...", n_points);
+		data = flann::Matrix<float>(new float[n_points*3], n_points, 3);
+		srand(1);
+		for( int i=0; i<n_points; i++ )
+		{
+			data[i][0]=rand()/float(RAND_MAX);
+			data[i][1]=rand()/float(RAND_MAX);
+			data[i][2]=rand()/float(RAND_MAX);
+// 			std::cout<<data[i][0]<<" "<<data[i][1]<<" "<<data[i][2]<<std::endl;
+		}
+		
+		query= flann::Matrix<float>(new float[n_points*3], n_points, 3);
+		for( int i=0; i<n_points; i++ )
+		{
+			query[i][0]=data[i][0];//float(rand())/RAND_MAX;
+			query[i][1]=data[i][1];//float(rand())/RAND_MAX;
+			query[i][2]=data[i][2];//float(rand())/RAND_MAX;
+// 			std::cout<<query[i][0]<<" "<<query[i][1]<<" "<<query[i][2]<<std::endl;
+		}
+		
+		
+        printf("done\n");
+		
+		const int max_nn = 16;
+		
+        dists = flann::Matrix<float>(new float[query.rows*max_nn], query.rows, max_nn);
+		gt_dists = flann::Matrix<float>(new float[query.rows*max_nn], query.rows, max_nn);
+        indices = flann::Matrix<int>(new int[query.rows*max_nn], query.rows, max_nn);
+		gt_indices = flann::Matrix<int>(new int[query.rows*max_nn], query.rows, max_nn);
+		
+		
+		Index<L2<float> > index(data, flann::LinearIndexParams());
+		start_timer("Building linear index...");
+		index.buildIndex();
+		printf("done (%g seconds)\n", stop_timer());
+		
+		start_timer("Searching KNN...");
+		index.knnSearch(data, gt_indices, gt_dists, max_nn, flann::SearchParams() );
+// 		for( int i=0; i<gt_dists.rows; i++ )
+// 		{
+// 			std::cout<<gt_indices[i][0]<<" "<<gt_dists[i][0]<<std::endl;
+// 		}
+		printf("done (%g seconds)\n", stop_timer());
+    }
+
+    void TearDown()
+    {
+        delete[] data.ptr();
+        delete[] query.ptr();
+        delete[] dists.ptr();
+		delete[] gt_dists.ptr();
+        delete[] indices.ptr();
+		delete[] gt_indices.ptr();
+		
+    }
+};
+
+TEST_F(Flann_3D_Random_Cloud, Test1NN)
+{
+    flann::Index<L2_Simple<float> > index(data, flann::KDTreeCuda3dIndexParams());
+    start_timer("Building kd-tree index...");
+    index.buildIndex();
+    printf("done (%g seconds)\n", stop_timer());
+
+    start_timer("Searching KNN...");
+	indices.cols=1;
+	dists.cols=1;
+    index.knnSearch(query, indices, dists, 1, flann::SearchParams() );
+    printf("done (%g seconds)\n", stop_timer());
+
+//     float precision = compute_precision(gt_indices,indices);
+	float precision = computePrecisionDiscrete(gt_dists,dists, 0);
+    EXPECT_GE(precision, 0.99);
+    printf("Precision: %g\n", precision);
+	
+}
+
+TEST_F(Flann_3D_Random_Cloud, Test4NN)
+{
+    flann::Index<L2_Simple<float> > index(data, flann::KDTreeCuda3dIndexParams());
+    start_timer("Building kd-tree index...");
+    index.buildIndex();
+    printf("done (%g seconds)\n", stop_timer());
+
+    start_timer("Searching KNN...");
+	indices.cols=4;
+	dists.cols=4;
+    index.knnSearch(query, indices, dists, 4, flann::SearchParams() );
+    printf("done (%g seconds)\n", stop_timer());
+
+//     float precision = compute_precision(gt_indices,indices);
+	float precision = computePrecisionDiscrete(gt_dists,dists, 1e-08);
+    EXPECT_GE(precision, 0.99);
+    printf("Precision: %g\n", precision);
+	
+}
+
+TEST_F(Flann_3D_Random_Cloud, Test4NNGpuBuffers)
+{
+	thrust::host_vector<float4> data_host(data.rows);
+	for( int i=0; i<data.rows; i++ )
+	{
+		data_host[i]=make_float4(data[i][0],data[i][1],data[i][2],0);
+	}
+	thrust::device_vector<float4> data_device = data_host;
+	thrust::host_vector<float4> query_host(data.rows);
+	for( int i=0; i<data.rows; i++ )
+	{
+		query_host[i]=make_float4(query[i][0],query[i][1],query[i][2],0);
+	}
+	thrust::device_vector<float4> query_device = query_host;
+	
+	flann::Matrix<float> data_device_matrix( (float*)thrust::raw_pointer_cast(&data_device[0]),data.rows,3,4*4);
+	flann::Matrix<float> query_device_matrix( (float*)thrust::raw_pointer_cast(&query_device[0]),data.rows,3,4*4);
+	
+	flann::KDTreeCuda3dIndexParams index_params;
+	index_params["input_is_gpu_float4"]=true;
+	flann::Index<L2_Simple<float> > index(data_device_matrix, index_params);
+    start_timer("Building kd-tree index...");
+    index.buildIndex();
+    printf("done (%g seconds)\n", stop_timer());
+
+	
+	thrust::device_vector<int> indices_device(query.rows*4);
+	thrust::device_vector<float> dists_device(query.rows*4);
+	flann::Matrix<int> indices_device_matrix( (int*)thrust::raw_pointer_cast(&indices_device[0]),query.rows,4);
+	flann::Matrix<float> dists_device_matrix( (float*)thrust::raw_pointer_cast(&dists_device[0]),query.rows,4);
+	
+    start_timer("Searching KNN...");
+	indices.cols=4;
+	dists.cols=4;
+	flann::SearchParams sp;
+	sp.matrices_in_gpu_ram=true;
+    index.knnSearch(query_device_matrix, indices_device_matrix, dists_device_matrix, 4, sp );
+    printf("done (%g seconds)\n", stop_timer());
+	
+	flann::Matrix<int> indices_host( new int[ query.rows*4],query.rows,4 );
+	flann::Matrix<float> dists_host( new float[ query.rows*4],query.rows,4 );
+	
+	thrust::copy( dists_device.begin(), dists_device.end(), dists_host.ptr() );
+	thrust::copy( indices_device.begin(), indices_device.end(), indices_host.ptr() );
+
+//     float precision = compute_precision(gt_indices,indices);
+	float precision = computePrecisionDiscrete(gt_dists,dists_host, 1e-08);
+    EXPECT_GE(precision, 0.99);
+    printf("Precision: %g\n", precision);
+	delete [] indices_host.ptr();
+	delete [] dists_host.ptr();
+}
+
+TEST_F(Flann_3D_Random_Cloud, TestRadiusSearchVector)
+{
+    flann::Index<L2_Simple<float> > index(data, flann::KDTreeCuda3dIndexParams());
+    start_timer("Building kd-tree index...");
+    index.buildIndex();
+    printf("done (%g seconds)\n", stop_timer());
+	float r = 0.1;
+	std::vector< std::vector<int> > indices;
+	std::vector< std::vector<float> > dists;
+	start_timer("Radius search, r=0.1");
+	index.radiusSearch( query, indices,dists, r*r, flann::SearchParams() );
+	printf("done (%g seconds)", stop_timer());
+
+	start_timer("verifying results...");
+	for( int i=0; i<query.rows; i++ )
+	{
+		for( int j=0; j<data.rows; j++ )
+		{
+			float dist = 0;
+			for( int k=0; k<3; k++ )
+				dist += (query[i][k]-data[j][k])*(query[i][k]-data[j][k]);
+			if( dist < r*r )
+			{
+				EXPECT_TRUE( std::find( indices[i].begin(), indices[i].end(), j )!=indices[i].end() );
+			}
+			else
+			{
+				EXPECT_TRUE( std::find( indices[i].begin(), indices[i].end(), j )==indices[i].end() );
+			}
+		}
+	}
+	printf("done (%g seconds)\n", stop_timer());
+	
+	r=0.05;
+	start_timer("Radius search, r=0.05");
+	index.radiusSearch( query, indices,dists, r*r, flann::SearchParams() );
+	printf("done (%g seconds)", stop_timer());
+	
+	start_timer("verifying results...");
+	for( int i=0; i<query.rows; i++ )
+	{
+		for( int j=0; j<data.rows; j++ )
+		{
+			// for each pair of query and data points: either the distance between them
+			// is smaller than r AND the point is in the result set, or 
+			// the distance is larger and it is not.
+			float dist = 0;
+			for( int k=0; k<3; k++ )
+				dist += (query[i][k]-data[j][k])*(query[i][k]-data[j][k]);
+			if( dist < r*r )
+			{
+				EXPECT_TRUE( std::find( indices[i].begin(), indices[i].end(), j )!=indices[i].end() );
+			}
+			else
+			{
+				EXPECT_TRUE( std::find( indices[i].begin(), indices[i].end(), j )==indices[i].end() );
+			}
+		}
+	}
+	printf("done (%g seconds)\n", stop_timer());
+}
+
+TEST_F(Flann_3D_Random_Cloud, TestRadiusSearchMatrix)
+{
+    flann::Index<L2_Simple<float> > index(data, flann::KDTreeCuda3dIndexParams());
+    start_timer("Building kd-tree index...");
+    index.buildIndex();
+    printf("done (%g seconds)\n", stop_timer());
+	float r = 0.05;
+	flann::Matrix<int> counts( new int[query.rows], query.rows,1);
+	flann::Matrix<float> dummy( 0,0,0 );
+	flann::SearchParams counting_params;
+	counting_params.max_neighbors=0;
+	start_timer("counting neighbors...");
+	index.radiusSearch( query, counts,dummy, r*r, counting_params );
+	printf("done (%g seconds)", stop_timer());
+	
+	int max_neighbors=0;
+	for( int i=0; i<query.rows; i++ )
+	{
+		max_neighbors = std::max(max_neighbors, counts[i][0]);
+	}
+	EXPECT_TRUE(max_neighbors > 0 );
+	flann::Matrix<int> indices( new int[max_neighbors*query.rows], query.rows, max_neighbors );
+	flann::Matrix<float> dists( new float[max_neighbors*query.rows], query.rows, max_neighbors );
+		
+	start_timer("Radius search, r=0.05");
+	index.radiusSearch( query, indices,dists, r*r, flann::SearchParams() );
+	printf("done (%g seconds)", stop_timer());
+
+	start_timer("verifying results...");
+	for( int i=0; i<query.rows; i++ )
+	{
+		for( int j=0; j<data.rows; j++ )
+		{
+			// for each pair of query and data points: either the distance between them
+			// is smaller than r AND the point is in the result set, or 
+			// the distance is larger and it is not.
+			float dist = 0;
+			for( int k=0; k<3; k++ )
+				dist += (query[i][k]-data[j][k])*(query[i][k]-data[j][k]);
+			if( dist < r*r )
+			{
+				EXPECT_TRUE( std::find( indices[i], indices[i]+max_neighbors, j )!=indices[i]+max_neighbors );
+			}
+			else
+			{
+				EXPECT_TRUE( std::find( indices[i], indices[i]+max_neighbors, j )==indices[i]+max_neighbors );
+			}
+		}
+	}
+	printf("done (%g seconds)\n", stop_timer());
+	delete []counts.ptr();
+	delete []indices.ptr();
+	delete []dists.ptr();
+}
+
+TEST_F(Flann_3D, TestRadiusSearch)
+{
+    flann::Index<L2_Simple<float> > index(data, flann::KDTreeCuda3dIndexParams());
+    start_timer("Building kd-tree index...");
+    index.buildIndex();
+    printf("done (%g seconds)\n", stop_timer());
+	float r = 0.02;
+	std::vector< std::vector<int> > indices;
+	std::vector< std::vector<float> > dists;
+	start_timer("Radius search, r=0.02...");
+	index.radiusSearch( query, indices,dists, r*r, flann::SearchParams() );
+	printf("done (%g seconds)\n", stop_timer());
+	
+	start_timer("verifying results...");
+	for( int i=0; i<query.rows; i++ )
+	{
+		for( int j=0; j<data.rows; j++ )
+		{
+			float dist = 0;
+			for( int k=0; k<3; k++ )
+				dist += (query[i][k]-data[j][k])*(query[i][k]-data[j][k]);
+			if( dist < r*r )
+			{
+				EXPECT_TRUE( std::find( indices[i].begin(), indices[i].end(), j )!=indices[i].end() );
+			}
+			else
+			{
+				EXPECT_TRUE( std::find( indices[i].begin(), indices[i].end(), j )==indices[i].end() );
+			}
+		}
+	}
+	printf("done (%g seconds)\n", stop_timer());
+	
+	r=0.01;
+	start_timer("Radius search, r=0.01");
+	index.radiusSearch( query, indices,dists, r*r, flann::SearchParams() );
+	printf("done (%g seconds)\n", stop_timer());
+	
+	start_timer("verifying results...");
+	for( int i=0; i<query.rows; i++ )
+	{
+		for( int j=0; j<data.rows; j++ )
+		{
+			float dist = 0;
+			for( int k=0; k<3; k++ )
+				dist += (query[i][k]-data[j][k])*(query[i][k]-data[j][k]);
+			if( dist < r*r )
+			{
+				EXPECT_TRUE( std::find( indices[i].begin(), indices[i].end(), j )!=indices[i].end() );
+			}
+			else
+			{
+				EXPECT_TRUE( std::find( indices[i].begin(), indices[i].end(), j )==indices[i].end() );
+			}
+		}
+	}
+	printf("done (%g seconds)\n", stop_timer());
+}
+
+int main(int argc, char** argv)
+{
+    testing::InitGoogleTest(&argc, argv);
+    return RUN_ALL_TESTS();
+}
diff --git a/test/flann_hierarchical_test.cpp b/test/flann_hierarchical_test.cpp
new file mode 100644
index 0000000..0170bf2
--- /dev/null
+++ b/test/flann_hierarchical_test.cpp
@@ -0,0 +1,108 @@
+#include <gtest/gtest.h>
+#include <time.h>
+
+#include <flann/flann.h>
+#include <flann/io/hdf5.h>
+
+#include "flann_tests.h"
+
+using namespace flann;
+
+
+class HierarchicalIndex_Brief100K : public FLANNTestFixture
+{
+protected:
+	typedef flann::Hamming<unsigned char> Distance;
+	typedef Distance::ElementType ElementType;
+	typedef Distance::ResultType DistanceType;
+	flann::Matrix<unsigned char> data;
+	flann::Matrix<unsigned char> query;
+	flann::Matrix<size_t> gt_indices;
+	flann::Matrix<DistanceType> dists;
+	flann::Matrix<DistanceType> gt_dists;
+	flann::Matrix<size_t> indices;
+	unsigned int k_nn_;
+
+	void SetUp()
+	{
+		k_nn_ = 3;
+		printf("Reading test data...");
+		fflush(stdout);
+		flann::load_from_file(data, "brief100K.h5", "dataset");
+		flann::load_from_file(query, "brief100K.h5", "query");
+		printf("done\n");
+
+		flann::Index<Distance> index(data, flann::LinearIndexParams());
+		index.buildIndex();
+
+		start_timer("Searching KNN for ground truth...");
+		gt_indices = flann::Matrix<size_t>(new size_t[query.rows * k_nn_], query.rows, k_nn_);
+		gt_dists = flann::Matrix<DistanceType>(new DistanceType[query.rows * k_nn_], query.rows, k_nn_);
+		index.knnSearch(query, gt_indices, gt_dists, k_nn_, flann::SearchParams(-1));
+		printf("done (%g seconds)\n", stop_timer());
+
+		dists = flann::Matrix<DistanceType>(new DistanceType[query.rows * k_nn_], query.rows, k_nn_);
+		indices = flann::Matrix<size_t>(new size_t[query.rows * k_nn_], query.rows, k_nn_);
+	}
+
+	void TearDown()
+	{
+		delete[] data.ptr();
+		delete[] query.ptr();
+		delete[] dists.ptr();
+		delete[] indices.ptr();
+		delete[] gt_indices.ptr();
+		delete[] gt_dists.ptr();
+	}
+};
+
+
+TEST_F(HierarchicalIndex_Brief100K, TestSearch)
+{
+	TestSearch<Distance>(data, flann::HierarchicalClusteringIndexParams(),
+			query, indices, dists, k_nn_, flann::SearchParams(2000), 0.9, gt_indices, gt_dists);
+}
+
+TEST_F(HierarchicalIndex_Brief100K, TestAddIncremental)
+{
+	TestAddIncremental<Distance>(data, flann::HierarchicalClusteringIndexParams(),
+			query, indices, dists, k_nn_, flann::SearchParams(2000), 0.87, gt_indices, gt_dists);
+}
+
+TEST_F(HierarchicalIndex_Brief100K, TestAddIncremental2)
+{
+	TestAddIncremental2<Distance>(data, flann::HierarchicalClusteringIndexParams(),
+			query, indices, dists, k_nn_, flann::SearchParams(2000), 0.87, gt_indices, gt_dists);
+}
+
+TEST_F(HierarchicalIndex_Brief100K, TestRemove)
+{
+	TestRemove<Distance>(data, flann::HierarchicalClusteringIndexParams(),
+			query, indices, dists, k_nn_, flann::SearchParams(2000));
+}
+
+TEST_F(HierarchicalIndex_Brief100K, TestSave)
+{
+	TestSave<Distance>(data, flann::HierarchicalClusteringIndexParams(),
+			query, indices, dists, k_nn_, flann::SearchParams(2000), 0.87, gt_indices, gt_dists);
+}
+
+
+TEST_F(HierarchicalIndex_Brief100K, TestCopy)
+{
+	TestCopy<Distance>(data, flann::HierarchicalClusteringIndexParams(),
+			query, indices, dists, k_nn_, flann::SearchParams(2000), 0.87, gt_indices, gt_dists);
+}
+
+TEST_F(HierarchicalIndex_Brief100K, TestCopy2)
+{
+	TestCopy2<flann::HierarchicalClusteringIndex<Distance> >(data, flann::HierarchicalClusteringIndexParams(),
+			query, indices, dists, k_nn_, flann::SearchParams(2000), 0.87, gt_indices, gt_dists);
+}
+
+
+int main(int argc, char** argv)
+{
+	testing::InitGoogleTest(&argc, argv);
+	return RUN_ALL_TESTS();
+}
diff --git a/test/flann_kdtree_single_test.cpp b/test/flann_kdtree_single_test.cpp
new file mode 100644
index 0000000..0388481
--- /dev/null
+++ b/test/flann_kdtree_single_test.cpp
@@ -0,0 +1,305 @@
+#include <gtest/gtest.h>
+#include <time.h>
+
+#include <flann/flann.h>
+#include <flann/io/hdf5.h>
+
+#include "flann_tests.h"
+
+using namespace flann;
+
+class KDTreeSingle :public DatasetTestFixture<float, float> {
+protected:
+	KDTreeSingle() : DatasetTestFixture("cloud.h5") {}
+};
+
+TEST_F(KDTreeSingle, TestSearch)
+{
+	TestSearch<L2_Simple<float> >(data, flann::KDTreeSingleIndexParams(12, false),
+			query, indices, dists, knn, flann::SearchParams(-1), 0.99, gt_indices);
+}
+
+TEST_F(KDTreeSingle, TestSearchPadded)
+{
+    flann::Matrix<float> data_padded;
+    flann::load_from_file(data_padded, "cloud.h5", "dataset_padded");
+    flann::Matrix<float> data2(data_padded.ptr(), data_padded.rows, 3, data_padded.cols*sizeof(float));
+
+	TestSearch<L2_Simple<float> >(data2, flann::KDTreeSingleIndexParams(12, false),
+			query, indices, dists, knn, flann::SearchParams(-1), 0.99, gt_indices);
+
+    delete[] data_padded.ptr();
+}
+
+TEST_F(KDTreeSingle, TestAddIncremental)
+{
+	TestAddIncremental<L2_Simple<float> >(data, flann::KDTreeSingleIndexParams(12, false),
+			query, indices, dists, knn, flann::SearchParams(-1), 0.99, gt_indices);
+}
+
+TEST_F(KDTreeSingle, TestAddIncremental2)
+{
+	TestAddIncremental2<L2_Simple<float> >(data, flann::KDTreeSingleIndexParams(12, false),
+			query, indices, dists, knn, flann::SearchParams(-1), 0.99, gt_indices);
+}
+
+
+TEST_F(KDTreeSingle, TestRemove)
+{
+	TestRemove<L2_Simple<float> >(data, flann::KDTreeSingleIndexParams(12, false),
+			query, indices, dists, knn, flann::SearchParams(-1));
+}
+
+
+TEST_F(KDTreeSingle, TestSave)
+{
+	TestSave<L2_Simple<float> >(data, flann::KDTreeSingleIndexParams(12, false),
+			query, indices, dists, knn, flann::SearchParams(-1), 0.99, gt_indices);
+}
+
+TEST_F(KDTreeSingle, TestSearchReorder)
+{
+	TestSearch<L2_Simple<float> >(data, flann::KDTreeSingleIndexParams(12, true),
+			query, indices, dists, knn, flann::SearchParams(-1), 0.99, gt_indices);
+}
+
+TEST_F(KDTreeSingle, TestSaveReorder)
+{
+	TestSave<L2_Simple<float> >(data, flann::KDTreeSingleIndexParams(12, true),
+			query, indices, dists, knn, flann::SearchParams(-1), 0.99, gt_indices);
+}
+
+TEST_F(KDTreeSingle, TestCopy)
+{
+	TestCopy<L2_Simple<float> >(data, flann::KDTreeSingleIndexParams(12, false),
+			query, indices, dists, knn, flann::SearchParams(-1), 0.99, gt_indices);
+
+	// repeat tests with reorder=true
+	TestCopy<L2_Simple<float> >(data, flann::KDTreeSingleIndexParams(12, true),
+			query, indices, dists, knn, flann::SearchParams(-1), 0.99, gt_indices);
+}
+
+TEST_F(KDTreeSingle, TestCopy2)
+{
+	TestCopy2<flann::KDTreeSingleIndex<L2_Simple<float> > >(data, flann::KDTreeSingleIndexParams(12, false),
+			query, indices, dists, knn, flann::SearchParams(-1), 0.99, gt_indices);
+
+	// repeat tests with reorder=true
+	TestCopy2<flann::KDTreeSingleIndex<L2_Simple<float> > >(data, flann::KDTreeSingleIndexParams(12, true),
+			query, indices, dists, knn, flann::SearchParams(-1), 0.99, gt_indices);
+}
+
+TEST_F(KDTreeSingle, TestNoNeighbours)
+{
+    flann::Index<L2_Simple<float> > index(data, flann::KDTreeSingleIndexParams(12, false));
+    start_timer("Building kd-tree index...");
+    index.buildIndex();
+    printf("done (%g seconds)\n", stop_timer());
+
+    start_timer("Searching KNN...");
+    index.knnSearch(query, indices, dists, knn, flann::SearchParams(-1) );
+    printf("done (%g seconds)\n", stop_timer());
+
+
+    float min_dist = dists[0][0];
+    for (size_t i=0;i<query.rows;++i) {
+    	min_dist = std::min(min_dist, dists[i][0]);
+    }
+
+    start_timer("Searching radius smaller than minimum distance...");
+    int count = index.radiusSearch(query, indices, dists, min_dist/2, flann::SearchParams(-1) );
+    printf("done (%g seconds)\n", stop_timer());
+
+    EXPECT_EQ(0,count);
+    for (size_t i=0;i<dists.rows;++i) {
+    	EXPECT_EQ(dists[i][0],std::numeric_limits<float>::infinity());
+    }
+
+    std::vector<std::vector<size_t> > indices2;
+    std::vector<std::vector<float> > dists2;
+    start_timer("Searching radius smaller than minimum distance...");
+    int count2 = index.radiusSearch(query, indices2, dists2, min_dist/2, flann::SearchParams(-1) );
+    printf("done (%g seconds)\n", stop_timer());
+
+    EXPECT_EQ(0,count2);
+    for (size_t i=0;i<indices2.size();++i) {
+    	EXPECT_EQ(indices2[i].size(),0);
+    }
+}
+
+
+// Test cases for 3D point clouds, borrowed from PCL test_kdtree
+struct MyPoint
+{
+  MyPoint (float x, float y, float z) {this->x=x; this->y=y; this->z=z;}
+
+  float x,y,z;
+};
+
+
+class KDTreeSinglePointCloud : public FLANNTestFixture
+{
+public:
+	void SetUp()
+	{
+		float resolution = 0.1f;
+		for (float z = -0.5f; z <= 0.5f; z += resolution)
+			for (float y = -0.5f; y <= 0.5f; y += resolution)
+				for (float x = -0.5f; x <= 0.5f; x += resolution)
+					cloud_.push_back (MyPoint (x, y, z));
+
+		cloud_mat_ = flann::Matrix<float>(&cloud_[0].x, cloud_.size(), 3);
+
+
+		srand(static_cast<unsigned int> (time (NULL)));
+		// Randomly create a new point cloud
+		for (size_t i = 0; i < 640*480; ++i)
+			cloud_big_.push_back (MyPoint (static_cast<float> (1024 * rand () / (RAND_MAX + 1.0)),
+					static_cast<float> (1024 * rand () / (RAND_MAX + 1.0)),
+					static_cast<float> (1024 * rand () / (RAND_MAX + 1.0))));
+
+
+		cloud_big_mat_ = flann::Matrix<float>(&cloud_big_[0].x, cloud_big_.size(), 3);
+	}
+
+	std::vector<MyPoint> cloud_;
+	flann::Matrix<float> cloud_mat_;
+
+	std::vector<MyPoint> cloud_big_;
+	flann::Matrix<float> cloud_big_mat_;
+};
+
+TEST_F(KDTreeSinglePointCloud, TestRadiusSearch)
+{
+	std::vector<std::vector<int> > k_indices;
+	std::vector<std::vector<float> > k_distances;
+
+	{
+	    flann::Index<L2_Simple<float> > index(cloud_mat_, flann::KDTreeSingleIndexParams(12, false));
+	    index.buildIndex();
+
+	    L2_Simple<float> euclideanDistance;
+		MyPoint test_point(0.0f, 0.0f, 0.0f);
+		flann::Matrix<float> test_mat(&test_point.x, 1, 3);
+		double max_dist = 0.15*0.15;
+		std::set<int> brute_force_result;
+		for (unsigned int i=0; i<cloud_mat_.rows; ++i)
+			if (euclideanDistance(cloud_mat_[i], test_mat[0], 3) < max_dist)
+				brute_force_result.insert(i);
+		index.radiusSearch (test_mat, k_indices, k_distances, max_dist, flann::SearchParams(-1));
+
+		for (size_t i = 0; i < k_indices[0].size (); ++i)
+		{
+			std::set<int>::iterator brute_force_result_it = brute_force_result.find (k_indices[0][i]);
+			bool ok = brute_force_result_it != brute_force_result.end ();
+			//if (!ok)  cerr << k_indices[i] << " is not correct...\n";
+			//else      cerr << k_indices[i] << " is correct...\n";
+			EXPECT_EQ (ok, true);
+			if (ok)
+				brute_force_result.erase (brute_force_result_it);
+		}
+		//for (set<int>::const_iterator it=brute_force_result.begin(); it!=brute_force_result.end(); ++it)
+		//cerr << "FLANN missed "<<*it<<"\n";
+
+		bool error = brute_force_result.size () > 0;
+		//if (error)  cerr << "Missed too many neighbors!\n";
+		EXPECT_EQ (error, false);
+	}
+
+	{
+	    flann::Index<L2_Simple<float> > index(flann::KDTreeSingleIndexParams(15));
+	    index.buildIndex(cloud_big_mat_);
+
+	    start_timer("radiusSearch...");
+	    flann::SearchParams params(-1);
+	    index.radiusSearch(cloud_big_mat_, k_indices, k_distances, 0.1*0.1, params);
+	    printf("done (%g seconds)\n", stop_timer());
+	}
+
+	{
+	    flann::Index<L2_Simple<float> > index(flann::KDTreeSingleIndexParams(15));
+	    index.buildIndex(cloud_big_mat_);
+
+	    start_timer("radiusSearch (max neighbors in radius)...");
+	    flann::SearchParams params(-1);
+	    params.max_neighbors = 10;
+	    index.radiusSearch(cloud_big_mat_, k_indices, k_distances, 0.1*0.1, params);
+	    printf("done (%g seconds)\n", stop_timer());
+	}
+
+	{
+	    flann::Index<L2_Simple<float> > index(flann::KDTreeSingleIndexParams(15));
+	    index.buildIndex(cloud_big_mat_);
+
+	    start_timer("radiusSearch (unsorted results)...");
+	    flann::SearchParams params(-1);
+	    params.sorted = false;
+	    index.radiusSearch(cloud_big_mat_, k_indices, k_distances, 0.1*0.1, params);
+	    printf("done (%g seconds)\n", stop_timer());
+	}
+}
+
+
+TEST_F(KDTreeSinglePointCloud, TestKNearestSearch)
+{
+	unsigned int no_of_neighbors = 20;
+
+	{
+		flann::Index<L2_Simple<float> > index(cloud_mat_, flann::KDTreeSingleIndexParams(12, false));
+		index.buildIndex();
+
+		L2_Simple<float> euclideanDistance;
+		MyPoint test_point (0.01f, 0.01f, 0.01f);
+		flann::Matrix<float> test_mat(&test_point.x, 1, 3);
+
+		std:: multimap<float, int> sorted_brute_force_result;
+		for (size_t i = 0; i < cloud_.size (); ++i)
+		{
+			float distance = euclideanDistance (cloud_mat_[i], test_mat[0],3);
+			sorted_brute_force_result.insert (std::make_pair (distance, static_cast<int> (i)));
+		}
+		float max_dist = 0.0f;
+		unsigned int counter = 0;
+		for (std::multimap<float, int>::iterator it = sorted_brute_force_result.begin (); it != sorted_brute_force_result.end () && counter < no_of_neighbors; ++it)
+		{
+			max_dist = std::max (max_dist, it->first);
+			++counter;
+		}
+
+		std::vector< std::vector<int> > k_indices(1);
+		k_indices[0].resize (no_of_neighbors);
+		std::vector<std::vector<float> > k_distances(1);
+		k_distances[0].resize (no_of_neighbors);
+		index.knnSearch(test_mat, k_indices, k_distances, no_of_neighbors, flann::SearchParams(-1));
+		//if (k_indices.size() != no_of_neighbors)  cerr << "Found "<<k_indices.size()<<" instead of "<<no_of_neighbors<<" neighbors.\n";
+		EXPECT_EQ (k_indices[0].size (), no_of_neighbors);
+
+		// Check if all found neighbors have distance smaller than max_dist
+		for (size_t i = 0; i < k_indices[0].size (); ++i)
+		{
+			float* point = cloud_mat_[k_indices[0][i]];
+			bool ok = euclideanDistance (test_mat[0], point, 3) <= max_dist;
+			EXPECT_EQ (ok, true);
+		}
+	}
+
+	{
+		flann::Index<L2_Simple<float> > index(flann::KDTreeSingleIndexParams(15));
+		index.buildIndex(cloud_big_mat_);
+
+		start_timer("K nearest neighbour search...");
+		flann::SearchParams params(-1);
+		params.sorted = false;
+		std::vector<std::vector<int> > k_indices;
+		std::vector<std::vector<float> > k_distances;
+		index.knnSearch (cloud_big_mat_, k_indices, k_distances, no_of_neighbors, params);
+		printf("done (%g seconds)\n", stop_timer());
+	}
+
+}
+
+int main(int argc, char** argv)
+{
+    testing::InitGoogleTest(&argc, argv);
+    return RUN_ALL_TESTS();
+}
diff --git a/test/flann_kdtree_test.cpp b/test/flann_kdtree_test.cpp
new file mode 100644
index 0000000..1d9e6e6
--- /dev/null
+++ b/test/flann_kdtree_test.cpp
@@ -0,0 +1,134 @@
+#include <gtest/gtest.h>
+#include <time.h>
+
+#include <flann/flann.h>
+#include <flann/io/hdf5.h>
+
+#include "flann_tests.h"
+
+using namespace flann;
+
+/**
+ * Test fixture for SIFT 10K dataset
+ */
+class KDTree_SIFT10K : public DatasetTestFixture<float, float> {
+protected:
+	KDTree_SIFT10K() : DatasetTestFixture("sift10K.h5") {}
+};
+
+TEST_F(KDTree_SIFT10K, TestSearch)
+{
+	TestSearch<flann::L2<float> >(data, flann::KDTreeIndexParams(4), query, indices,
+			dists, knn, flann::SearchParams(256), 0.75, gt_indices);
+}
+
+TEST_F(KDTree_SIFT10K, TestAddIncremental)
+{
+	TestAddIncremental<flann::L2<float> >(data, flann::KDTreeIndexParams(4), query, indices,
+			dists, knn, flann::SearchParams(256), 0.75, gt_indices);
+}
+
+TEST_F(KDTree_SIFT10K, TestAddIncremental2)
+{
+	TestAddIncremental2<flann::L2<float> >(data, flann::KDTreeIndexParams(4), query, indices,
+			dists, knn, flann::SearchParams(256), 0.75, gt_indices);
+}
+
+TEST_F(KDTree_SIFT10K, TestRemove)
+{
+	TestRemove<flann::L2<float> >(data, flann::KDTreeIndexParams(4), query, indices,
+			dists, knn, flann::SearchParams(256) );
+}
+
+
+TEST_F(KDTree_SIFT10K, TestSave)
+{
+	TestSave<flann::L2<float> >(data, flann::KDTreeIndexParams(4), query, indices,
+			dists, knn, flann::SearchParams(256), 0.75, gt_indices);
+}
+
+
+TEST_F(KDTree_SIFT10K, TestCopy)
+{
+	TestCopy<flann::L2<float> >(data, flann::KDTreeIndexParams(4), query, indices,
+			dists, knn, flann::SearchParams(256), 0.75, gt_indices);
+}
+
+TEST_F(KDTree_SIFT10K, TestCopy2)
+{
+	TestCopy2<KDTreeIndex<L2<float> > >(data, flann::KDTreeIndexParams(4), query, indices,
+			dists, knn, flann::SearchParams(256), 0.75, gt_indices);
+}
+
+/**
+ * Test fixture for SIFT 100K dataset
+ */
+class KDTree_SIFT100K : public DatasetTestFixture<float, float> {
+protected:
+	KDTree_SIFT100K() : DatasetTestFixture("sift100K.h5") {}
+};
+
+
+TEST_F(KDTree_SIFT100K, TestSearch)
+{
+	TestSearch<flann::L2<float> >(data, flann::KDTreeIndexParams(4), query, indices,
+			dists, knn, flann::SearchParams(128), 0.75, gt_indices);
+}
+
+
+TEST_F(KDTree_SIFT100K, TestAddIncremental)
+{
+	TestAddIncremental<flann::L2<float> >(data, flann::KDTreeIndexParams(4), query, indices,
+			dists, knn, flann::SearchParams(256), 0.75, gt_indices);
+}
+
+
+
+TEST_F(KDTree_SIFT100K, TestAddIncremental2)
+{
+	TestAddIncremental2<flann::L2<float> >(data, flann::KDTreeIndexParams(4), query, indices,
+			dists, knn, flann::SearchParams(256), 0.75, gt_indices);
+}
+
+
+TEST_F(KDTree_SIFT100K, TestRemove)
+{
+	TestRemove<flann::L2<float> >(data, flann::KDTreeIndexParams(4), query, indices,
+			dists, knn, flann::SearchParams(128) );
+}
+
+
+/**
+ * Test fixture for SIFT 10K dataset with byte feature elements
+ */
+class KDTree_SIFT10K_byte : public DatasetTestFixture<unsigned char, float> {
+protected:
+	KDTree_SIFT10K_byte() : DatasetTestFixture("sift10K_byte.h5") {}
+};
+
+
+TEST_F(KDTree_SIFT10K_byte, TestSearch)
+{
+	TestSearch<flann::L2<unsigned char> >(data, flann::KDTreeIndexParams(4), query, indices,
+			dists, knn, flann::SearchParams(256), 0.75, gt_indices);
+}
+
+
+class KDTree_SIFT100K_byte : public DatasetTestFixture<unsigned char, float> {
+protected:
+	KDTree_SIFT100K_byte() : DatasetTestFixture("sift100K_byte.h5") {}
+};
+
+
+TEST_F(KDTree_SIFT100K_byte, TestSearch)
+{
+	TestSearch<flann::L2<unsigned char> >(data, flann::KDTreeIndexParams(4), query, indices,
+			dists, knn, flann::SearchParams(128), 0.75, gt_indices);
+}
+
+
+int main(int argc, char** argv)
+{
+    testing::InitGoogleTest(&argc, argv);
+    return RUN_ALL_TESTS();
+}
diff --git a/test/flann_kmeans_test.cpp b/test/flann_kmeans_test.cpp
new file mode 100644
index 0000000..c0f8295
--- /dev/null
+++ b/test/flann_kmeans_test.cpp
@@ -0,0 +1,144 @@
+#include <gtest/gtest.h>
+#include <time.h>
+
+#include <flann/flann.h>
+#include <flann/io/hdf5.h>
+
+#include "flann_tests.h"
+
+using namespace flann;
+
+/**
+ * Test fixture for SIFT 10K dataset
+ */
+class KMeans_SIFT10K : public DatasetTestFixture<float, float> {
+protected:
+	KMeans_SIFT10K() : DatasetTestFixture("sift10K.h5") {}
+};
+
+
+
+
+TEST_F(KMeans_SIFT10K, TestSearch)
+{
+	TestSearch<flann::L2<float> >(data, flann::KMeansIndexParams(7, 3, FLANN_CENTERS_RANDOM, 0.4),
+			query, indices, dists, knn, flann::SearchParams(128), 0.75, gt_indices);
+}
+
+
+TEST_F(KMeans_SIFT10K, TestAddIncremental)
+{
+	TestAddIncremental<flann::L2<float> >(data, flann::KMeansIndexParams(7, 3, FLANN_CENTERS_RANDOM, 0.4),
+			query, indices, dists, knn, flann::SearchParams(110), 0.75, gt_indices);
+}
+
+TEST_F(KMeans_SIFT10K, TestAddIncremental2)
+{
+	TestAddIncremental2<flann::L2<float> >(data, flann::KMeansIndexParams(7, 3, FLANN_CENTERS_RANDOM, 0.4),
+			query, indices, dists, knn, flann::SearchParams(110), 0.75, gt_indices);
+}
+
+TEST_F(KMeans_SIFT10K, TestRemove)
+{
+	TestRemove<flann::L2<float> >(data, flann::KMeansIndexParams(7, 3, FLANN_CENTERS_RANDOM, 0.4),
+			query, indices, dists, knn, flann::SearchParams(128));
+}
+
+
+
+TEST_F(KMeans_SIFT10K, TestSave)
+{
+	TestSave<flann::L2<float> >(data, flann::KMeansIndexParams(7, 3, FLANN_CENTERS_RANDOM, 0.4),
+			query, indices, dists, knn, flann::SearchParams(128), 0.75, gt_indices);
+}
+
+
+TEST_F(KMeans_SIFT10K, TestCopy)
+{
+	TestCopy<flann::L2<float> >(data, flann::KMeansIndexParams(7, 3, FLANN_CENTERS_RANDOM, 0.4),
+			query, indices, dists, knn, flann::SearchParams(128), 0.75, gt_indices);
+}
+
+
+TEST_F(KMeans_SIFT10K, TestCopy2)
+{
+	TestCopy2<KMeansIndex<flann::L2<float> > >(data, flann::KMeansIndexParams(7, 3, FLANN_CENTERS_RANDOM, 0.4),
+			query, indices, dists, knn, flann::SearchParams(128), 0.75, gt_indices);
+}
+
+/**
+ * Test fixture for SIFT 100K dataset
+ */
+class KMeans_SIFT100K : public DatasetTestFixture<float, float> {
+protected:
+	KMeans_SIFT100K() : DatasetTestFixture("sift100K.h5") {}
+};
+
+
+TEST_F(KMeans_SIFT100K, TestSearch)
+{
+	TestSearch<flann::L2<float> >(data, flann::KMeansIndexParams(32, 11, FLANN_CENTERS_RANDOM, 0.4),
+			query, indices, dists, knn, flann::SearchParams(96), 0.75, gt_indices);
+}
+
+
+TEST_F(KMeans_SIFT100K, TestAddIncremental)
+{
+	TestAddIncremental<flann::L2<float> >(data, flann::KMeansIndexParams(32, 11, FLANN_CENTERS_RANDOM, 0.4),
+			query, indices, dists, knn, flann::SearchParams(128), 0.75, gt_indices);
+}
+
+TEST_F(KMeans_SIFT100K, TestAddIncremental2)
+{
+	TestAddIncremental2<flann::L2<float> >(data, flann::KMeansIndexParams(32, 11, FLANN_CENTERS_RANDOM, 0.4),
+			query, indices, dists, knn, flann::SearchParams(128), 0.75, gt_indices);
+}
+
+
+TEST_F(KMeans_SIFT100K, TestRemove)
+{
+	TestRemove<flann::L2<float> >(data, flann::KMeansIndexParams(32, 11, FLANN_CENTERS_RANDOM, 0.4), query, indices,
+			dists, knn, flann::SearchParams(128) );
+}
+
+TEST_F(KMeans_SIFT100K, TestSave)
+{
+	TestSave<flann::L2<float> >(data, flann::KMeansIndexParams(32, 11, FLANN_CENTERS_RANDOM, 0.4),
+			query, indices, dists, knn, flann::SearchParams(96), 0.75, gt_indices);
+}
+
+
+
+/**
+ * Test fixture for SIFT 10K dataset with byte feature elements
+ */
+class KMeans_SIFT10K_byte :  public DatasetTestFixture<unsigned char, float> {
+protected:
+	KMeans_SIFT10K_byte() : DatasetTestFixture("sift10K_byte.h5") {}
+};
+
+TEST_F(KMeans_SIFT10K_byte, TestSearch)
+{
+	TestSearch<flann::L2<unsigned char> >(data, flann::KMeansIndexParams(7, 3, FLANN_CENTERS_RANDOM, 0.4),
+			query, indices, dists, knn, flann::SearchParams(128), 0.75, gt_indices);
+}
+
+
+
+class KMeans_SIFT100K_byte : public DatasetTestFixture<unsigned char, float> {
+protected:
+	KMeans_SIFT100K_byte() : DatasetTestFixture("sift100K_byte.h5") {}
+};
+
+TEST_F(KMeans_SIFT100K_byte, TestSearch)
+{
+	TestSearch<flann::L2<unsigned char> >(data, flann::KMeansIndexParams(32, 11, FLANN_CENTERS_RANDOM, 0.4),
+			query, indices, dists, knn, flann::SearchParams(80), 0.75, gt_indices);
+}
+
+
+int main(int argc, char** argv)
+{
+    testing::InitGoogleTest(&argc, argv);
+    return RUN_ALL_TESTS();
+}
diff --git a/test/flann_linear_test.cpp b/test/flann_linear_test.cpp
new file mode 100644
index 0000000..1209d50
--- /dev/null
+++ b/test/flann_linear_test.cpp
@@ -0,0 +1,107 @@
+#include <gtest/gtest.h>
+#include <time.h>
+
+#include <flann/flann.h>
+#include <flann/io/hdf5.h>
+
+#include "flann_tests.h"
+
+using namespace flann;
+
+/**
+ * Test fixture for SIFT 10K dataset
+ */
+class Linear_SIFT10K : public DatasetTestFixture<float, float> {
+protected:
+	Linear_SIFT10K() : DatasetTestFixture("sift10K.h5") {}
+};
+
+
+TEST_F(Linear_SIFT10K, TestSearch)
+{
+	TestSearch<flann::L2<float> >(data, flann::LinearIndexParams(),
+			query, indices, dists, knn, flann::SearchParams(0), 1.0, gt_indices);
+}
+
+TEST_F(Linear_SIFT10K, TestRemove)
+{
+	TestRemove<flann::L2<float> >(data, flann::LinearIndexParams(),
+			query, indices, dists, knn, flann::SearchParams(0));
+}
+
+
+TEST_F(Linear_SIFT10K, TestSave)
+{
+	TestSave<flann::L2<float> >(data, flann::LinearIndexParams(),
+			query, indices, dists, knn, flann::SearchParams(0), 1.0, gt_indices);
+}
+
+TEST_F(Linear_SIFT10K, TestCopy)
+{
+	TestCopy<flann::L2<float> >(data, flann::LinearIndexParams(),
+			query, indices, dists, knn, flann::SearchParams(0), 1.0, gt_indices);
+}
+
+
+TEST_F(Linear_SIFT10K, TestCopy2)
+{
+	TestCopy<flann::L2<float> >(data, flann::LinearIndexParams(),
+			query, indices, dists, knn, flann::SearchParams(0), 1.0, gt_indices);
+}
+
+
+/**
+ * Test fixture for SIFT 100K dataset
+ */
+class Linear_SIFT100K :  public DatasetTestFixture<float, float> {
+protected:
+	Linear_SIFT100K() : DatasetTestFixture("sift100K.h5") {}
+};
+
+
+TEST_F(Linear_SIFT100K, TestSearch)
+{
+	TestSearch<flann::L2<float> >(data, flann::LinearIndexParams(),
+			query, indices, dists, knn, flann::SearchParams(0), 1.0, gt_indices);
+}
+
+
+/**
+ * Test fixture for SIFT 10K dataset with byte feature elements
+ */
+class Linear_SIFT10K_byte : public DatasetTestFixture<unsigned char, float> {
+protected:
+	Linear_SIFT10K_byte() : DatasetTestFixture("sift10K_byte.h5") {}
+};
+
+
+
+TEST_F(Linear_SIFT10K_byte, Linear)
+{
+	TestSearch<flann::L2<unsigned char> >(data, flann::LinearIndexParams(),
+			query, indices, dists, knn, flann::SearchParams(0), 1.0, gt_indices);
+}
+
+
+
+
+class Linear_SIFT100K_byte : public DatasetTestFixture<unsigned char, float> {
+protected:
+	Linear_SIFT100K_byte() : DatasetTestFixture("sift100K_byte.h5") {}
+};
+
+
+
+TEST_F(Linear_SIFT100K_byte, TestSearch)
+{
+	TestSearch<flann::L2<unsigned char> >(data, flann::LinearIndexParams(),
+			query, indices, dists, knn, flann::SearchParams(0), 1.0, gt_indices);
+}
+
+
+
+int main(int argc, char** argv)
+{
+    testing::InitGoogleTest(&argc, argv);
+    return RUN_ALL_TESTS();
+}
diff --git a/test/flann_lsh_test.cpp b/test/flann_lsh_test.cpp
new file mode 100644
index 0000000..4e3ef82
--- /dev/null
+++ b/test/flann_lsh_test.cpp
@@ -0,0 +1,115 @@
+#include <gtest/gtest.h>
+#include <time.h>
+
+#include <flann/flann.h>
+#include <flann/io/hdf5.h>
+
+#include "flann_tests.h"
+
+using namespace flann;
+
+
+class LshIndex_Brief100K : public FLANNTestFixture
+{
+protected:
+	typedef flann::Hamming<unsigned char> Distance;
+	typedef Distance::ElementType ElementType;
+	typedef Distance::ResultType DistanceType;
+	flann::Matrix<unsigned char> data;
+	flann::Matrix<unsigned char> query;
+	flann::Matrix<size_t> gt_indices;
+	flann::Matrix<DistanceType> dists;
+	flann::Matrix<DistanceType> gt_dists;
+	flann::Matrix<size_t> indices;
+	unsigned int k_nn_;
+
+	void SetUp()
+	{
+		k_nn_ = 3;
+		printf("Reading test data...");
+		fflush(stdout);
+		flann::load_from_file(data, "brief100K.h5", "dataset");
+		flann::load_from_file(query, "brief100K.h5", "query");
+
+		dists = flann::Matrix<DistanceType>(new DistanceType[query.rows * k_nn_], query.rows, k_nn_);
+		indices = flann::Matrix<size_t>(new size_t[query.rows * k_nn_], query.rows, k_nn_);
+
+		printf("done\n");
+
+		// The matches are bogus so we compute them the hard way
+		//    flann::load_from_file(match,"brief100K.h5","indices");
+
+		flann::Index<Distance> index(data, flann::LinearIndexParams());
+		index.buildIndex();
+
+		start_timer("Searching KNN for ground truth...");
+		gt_indices = flann::Matrix<size_t>(new size_t[query.rows * k_nn_], query.rows, k_nn_);
+		gt_dists = flann::Matrix<DistanceType>(new DistanceType[query.rows * k_nn_], query.rows, k_nn_);
+		index.knnSearch(query, gt_indices, gt_dists, k_nn_, flann::SearchParams(-1));
+		printf("done (%g seconds)\n", stop_timer());
+	}
+
+  void TearDown()
+  {
+    delete[] data.ptr();
+    delete[] query.ptr();
+    delete[] gt_indices.ptr();
+    delete[] gt_dists.ptr();
+    delete[] dists.ptr();
+    delete[] indices.ptr();
+  }
+};
+
+
+TEST_F(LshIndex_Brief100K, TestSearch)
+{
+	TestSearch<Distance>(data, flann::LshIndexParams(12, 20, 2),
+			query, indices, dists, k_nn_, flann::SearchParams(-1), 0.9, gt_indices, gt_dists);
+}
+
+
+TEST_F(LshIndex_Brief100K, TestAddIncremental)
+{
+	TestAddIncremental<Distance>(data, flann::LshIndexParams(12, 20, 2),
+			query, indices, dists, k_nn_, flann::SearchParams(-1), 0.9, gt_indices, gt_dists);
+}
+
+TEST_F(LshIndex_Brief100K, TestIncremental2)
+{
+	TestAddIncremental2<Distance>(data, flann::LshIndexParams(12, 20, 2),
+			query, indices, dists, k_nn_, flann::SearchParams(-1), 0.9, gt_indices, gt_dists);
+}
+
+
+TEST_F(LshIndex_Brief100K, TestRemove)
+{
+	TestRemove<Distance>(data, flann::LshIndexParams(12, 20, 2),
+			query, indices, dists, k_nn_, flann::SearchParams(-1));
+}
+
+
+TEST_F(LshIndex_Brief100K, TestSave)
+{
+	TestSave<Distance>(data, flann::LshIndexParams(12, 20, 2),
+			query, indices, dists, k_nn_, flann::SearchParams(-1), 0.9, gt_indices, gt_dists);
+}
+
+
+TEST_F(LshIndex_Brief100K, TestCopy)
+{
+	TestCopy<Distance>(data, flann::LshIndexParams(12, 20, 2),
+			query, indices, dists, k_nn_, flann::SearchParams(-1), 0.9, gt_indices, gt_dists);
+}
+
+
+TEST_F(LshIndex_Brief100K, TestCopy2)
+{
+	TestCopy2<flann::LshIndex<Distance> >(data, flann::LshIndexParams(12, 20, 2),
+			query, indices, dists, k_nn_, flann::SearchParams(-1), 0.9, gt_indices, gt_dists);
+}
+
+int main(int argc, char** argv)
+{
+    testing::InitGoogleTest(&argc, argv);
+    return RUN_ALL_TESTS();
+}
diff --git a/test/flann_multithreaded_test.cpp b/test/flann_multithreaded_test.cpp
new file mode 100644
index 0000000..4d412e4
--- /dev/null
+++ b/test/flann_multithreaded_test.cpp
@@ -0,0 +1,311 @@
+#include <gtest/gtest.h>
+#include <time.h>
+
+#include <flann/flann.h>
+#include <flann/io/hdf5.h>
+#include <flann/nn/ground_truth.h>
+
+using namespace flann;
+
+template<typename T>
+float compute_precision(const flann::Matrix<T>& match, const flann::Matrix<T>& indices)
+{
+    int count = 0;
+
+    assert(match.rows == indices.rows);
+    size_t nn = std::min(match.cols, indices.cols);
+
+    for (size_t i=0; i<match.rows; ++i) {
+        for (size_t j=0;j<nn;++j) {
+            for (size_t k=0;k<nn;++k) {
+                if (match[i][j]==indices[i][k]) {
+                    count ++;
+                }
+            }
+        }
+    }
+
+    return float(count)/(nn*match.rows);
+}
+
+class FLANNTestFixture : public ::testing::Test {
+protected:
+    timespec ts_;
+
+    void start_timer(const std::string& message = "")
+    {
+        if (!message.empty()) {
+            printf("%s", message.c_str());
+            fflush(stdout);
+        }
+        clock_gettime(CLOCK_REALTIME, &ts_);
+    }
+
+    double stop_timer()
+    {
+		timespec ts2;
+        clock_gettime(CLOCK_REALTIME, &ts2);
+        return double((ts2.tv_sec-ts_.tv_sec)+(ts2.tv_nsec-ts_.tv_nsec)/1e9);
+    }
+
+};
+
+
+/* Test Fixture which loads the cloud.h5 cloud as data and query matrix */
+class FlannTest : public FLANNTestFixture {
+protected:
+    flann::Matrix<float> data_;
+    flann::Matrix<float> query_;
+    flann::Matrix<size_t> match_;
+    flann::Matrix<float> dists_;
+    flann::Matrix<size_t> indices_;
+
+    int knn_;
+
+    void SetUp()
+    {
+        knn_ = 5;
+
+        printf("Reading test data...");
+        fflush(stdout);
+        flann::load_from_file(data_, "cloud.h5","dataset");
+        flann::load_from_file(query_,"cloud.h5","query");
+        flann::load_from_file(match_,"cloud.h5","match");
+
+        dists_ = flann::Matrix<float>(new float[query_.rows*knn_], query_.rows, knn_);
+        indices_ = flann::Matrix<size_t>(new size_t[query_.rows*knn_], query_.rows, knn_);
+
+        printf("done\n");
+    }
+
+    void TearDown()
+    {
+        delete[] data_.ptr();
+        delete[] query_.ptr();
+        delete[] match_.ptr();
+        delete[] dists_.ptr();
+        delete[] indices_.ptr();
+    }
+
+};
+
+TEST_F(FlannTest, HandlesSingleCoreSearch)
+{
+    flann::Index<L2_Simple<float> > index(data_, flann::KDTreeSingleIndexParams(50, false));
+    start_timer("Building kd-tree index...");
+    index.buildIndex();
+    printf("done (%g seconds)\n", stop_timer());
+
+    int checks = -1;
+    float eps = 0.0f;
+    bool sorted = true;
+    int cores = 1;
+
+    start_timer("Searching KNN...");
+    SearchParams params(checks,eps,sorted);
+    params.cores = cores;
+    index.knnSearch(query_, indices_, dists_, knn_, params);
+    printf("done (%g seconds)\n", stop_timer());
+
+    float precision = compute_precision(match_, indices_);
+    EXPECT_GE(precision, 0.99);
+    printf("Precision: %g\n", precision);
+}
+
+TEST_F(FlannTest, HandlesMultiCoreSearch)
+{
+    flann::Index<L2_Simple<float> > index(data_, flann::KDTreeSingleIndexParams(50, false));
+    start_timer("Building kd-tree index...");
+    index.buildIndex();
+    printf("done (%g seconds)\n", stop_timer());
+
+    int checks = -1;
+    float eps = 0.0f;
+    bool sorted = true;
+    int cores = 2;
+
+    start_timer("Searching KNN...");
+    SearchParams params(checks,eps,sorted);
+    params.cores = cores;
+    index.knnSearch(query_, indices_, dists_, knn_, params);
+    printf("done (%g seconds)\n", stop_timer());
+
+    float precision = compute_precision(match_, indices_);
+    EXPECT_GE(precision, 0.99);
+    printf("Precision: %g\n", precision);
+}
+
+
+/* Test Fixture which loads the cloud.h5 cloud as data and query matrix and holds two dists
+   and indices matrices for comparing single and multi core KNN search */
+class FlannCompareKnnTest : public FLANNTestFixture {
+protected:
+    flann::Matrix<float> data_;
+    flann::Matrix<float> query_;
+    flann::Matrix<float> dists_single_;
+    flann::Matrix<size_t> indices_single_;
+    flann::Matrix<float> dists_multi_;
+    flann::Matrix<size_t> indices_multi_;
+
+    int knn_;
+
+    void SetUp()
+    {
+        knn_ = 5;
+
+        printf("Reading test data...");
+        fflush(stdout);
+        flann::load_from_file(data_, "cloud.h5","dataset");
+        flann::load_from_file(query_,"cloud.h5","query");
+
+        dists_single_ = flann::Matrix<float>(new float[query_.rows*knn_], query_.rows, knn_);
+        indices_single_ = flann::Matrix<size_t>(new size_t[query_.rows*knn_], query_.rows, knn_);
+        dists_multi_ = flann::Matrix<float>(new float[query_.rows*knn_], query_.rows, knn_);
+        indices_multi_ = flann::Matrix<size_t>(new size_t[query_.rows*knn_], query_.rows, knn_);
+
+        printf("done\n");
+    }
+
+    void TearDown()
+    {
+        delete[] data_.ptr();
+        delete[] query_.ptr();
+        delete[] dists_single_.ptr();
+        delete[] indices_single_.ptr();
+        delete[] dists_multi_.ptr();
+        delete[] indices_multi_.ptr();
+    }
+
+};
+
+
+
+TEST_F(FlannCompareKnnTest, CompareMultiSingleCoreKnnSearch)
+{
+    flann::Index<L2_Simple<float> > index(data_, flann::KDTreeSingleIndexParams(50, false));
+    start_timer("Building kd-tree index...");
+    index.buildIndex();
+    printf("done (%g seconds)\n", stop_timer());
+
+    SearchParams params;
+    params.checks = -1;
+    params.eps = 0.0f;
+    params.sorted = true;
+
+    start_timer("Searching KNN (single core)...");
+    params.cores = 1;
+    int single_neighbor_count = index.knnSearch(query_, indices_single_, dists_single_, knn_, params);
+    printf("done (%g seconds)\n", stop_timer());
+
+    start_timer("Searching KNN (multi core)...");
+    params.cores = 0;
+    int multi_neighbor_count = index.knnSearch(query_, indices_multi_, dists_multi_, knn_, params);
+    printf("done (%g seconds)\n", stop_timer());
+
+    EXPECT_EQ(single_neighbor_count, multi_neighbor_count);
+
+    printf("Checking results...\n");
+    float precision = compute_precision(indices_single_, indices_multi_);
+    EXPECT_GE(precision, 0.99);
+    printf("Precision: %g\n", precision);
+}
+
+/* Test Fixture which loads the cloud.h5 cloud as data and query matrix and holds two dists
+   and indices matrices for comparing single and multi core radius search */
+class FlannCompareRadiusTest : public FLANNTestFixture {
+protected:
+    flann::Matrix<float> data_;
+    flann::Matrix<float> query_;
+    flann::Matrix<float> dists_single_;
+    flann::Matrix<int> indices_single_;
+    flann::Matrix<float> dists_multi_;
+    flann::Matrix<int> indices_multi_;
+
+    float radius_;
+
+    void SetUp()
+    {
+        radius_ = 0.1f;
+
+        printf("Reading test data...");
+        fflush(stdout);
+        flann::load_from_file(data_, "cloud.h5","dataset");
+        flann::load_from_file(query_,"cloud.h5","query");
+
+        int reserve_size = data_.rows / 1000;
+
+        dists_single_ = flann::Matrix<float>(new float[query_.rows*reserve_size], query_.rows, reserve_size);
+        indices_single_ = flann::Matrix<int>(new int[query_.rows*reserve_size], query_.rows, reserve_size);
+        dists_multi_ = flann::Matrix<float>(new float[query_.rows*reserve_size], query_.rows, reserve_size);
+        indices_multi_ = flann::Matrix<int>(new int[query_.rows*reserve_size], query_.rows, reserve_size);
+
+        printf("done\n");
+    }
+
+    void TearDown()
+    {
+        delete[] data_.ptr();
+        delete[] query_.ptr();
+        delete[] dists_single_.ptr();
+        delete[] indices_single_.ptr();
+        delete[] dists_multi_.ptr();
+        delete[] indices_multi_.ptr();
+    }
+
+    void runTest(const flann::Index<L2_Simple<float> >& index, SearchParams params)
+    {
+        start_timer("Searching Radius (single core)...");
+        params.cores = 1;
+        int single_neighbor_count = index.radiusSearch(query_, indices_single_, dists_single_, radius_, params);
+        printf("done (%g seconds)\n", stop_timer());
+
+        start_timer("Searching Radius (multi core)...");
+        params.cores = 0;
+        int multi_neighbor_count = index.radiusSearch(query_, indices_multi_, dists_multi_, radius_, params);
+        printf("done (%g seconds)\n", stop_timer());
+
+        EXPECT_EQ(single_neighbor_count, multi_neighbor_count);
+
+        printf("Checking results...\n");
+        float precision = compute_precision(indices_single_, indices_multi_);
+        EXPECT_GE(precision, 0.99);
+        printf("Precision: %g\n", precision);
+    }
+};
+
+TEST_F(FlannCompareRadiusTest, CompareMultiSingleCoreRadiusSearchSorted)
+{
+    flann::Index<L2_Simple<float> > index(data_, flann::KDTreeSingleIndexParams(50, false));
+    start_timer("Building kd-tree index...");
+    index.buildIndex();
+    printf("done (%g seconds)\n", stop_timer());
+
+    SearchParams params;
+    params.checks = -1;
+    params.eps = 0.0f;
+    params.sorted = true;
+
+    runTest(index, params);
+}
+
+TEST_F(FlannCompareRadiusTest, CompareMultiSingleCoreRadiusSearchUnsorted)
+{
+    flann::Index<L2_Simple<float> > index(data_, flann::KDTreeSingleIndexParams(50, false));
+    start_timer("Building kd-tree index...");
+    index.buildIndex();
+    printf("done (%g seconds)\n", stop_timer());
+
+    SearchParams params;
+    params.checks = -1;
+    params.eps = 0.0f;
+    params.sorted = false;
+
+    runTest(index, params);
+}
+
+
+int main(int argc, char** argv)
+{
+    testing::InitGoogleTest(&argc, argv);
+    return RUN_ALL_TESTS();
+}
diff --git a/test/flann_tests.h b/test/flann_tests.h
new file mode 100644
index 0000000..c5f3a3b
--- /dev/null
+++ b/test/flann_tests.h
@@ -0,0 +1,685 @@
+/***********************************************************************
+ * Software License Agreement (BSD License)
+ *
+ * Copyright 2008-2012  Marius Muja (mariusm at cs.ubc.ca). All rights reserved.
+ * Copyright 2008-2012  David G. Lowe (lowe at cs.ubc.ca). All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *************************************************************************/
+
+
+#ifndef FLANN_TESTS_H_
+#define FLANN_TESTS_H_
+
+#include <flann/util/matrix.h>
+#include <vector>
+#include <set>
+
+
+template<typename T>
+float compute_precision(const flann::Matrix<T>& match, const flann::Matrix<T>& indices)
+{
+	int count = 0;
+
+	assert(match.rows == indices.rows);
+	size_t nn = std::min(match.cols, indices.cols);
+
+	for (size_t i=0; i<match.rows; ++i) {
+		for (size_t j=0;j<nn;++j) {
+			for (size_t k=0;k<nn;++k) {
+				if (match[i][j]==indices[i][k]) {
+					count ++;
+				}
+			}
+		}
+	}
+
+	return float(count)/(nn*match.rows);
+}
+
+/** @brief Compare the distances for match accuracies
+ * This is more precise: e.g. when you ask for the top 10 neighbors and they all get the same distance,
+ * you might have 100 other neighbors that are at the same distance and simply matching the indices is not the way to go
+ * @param gt_dists the ground truth best distances
+ * @param dists the distances of the computed nearest neighbors
+ * @param tol tolerance at which distanceare considered equal
+ * @return
+ */
+template<typename T>
+float computePrecisionDiscrete(const flann::Matrix<T>& gt_dists, const flann::Matrix<T>& dists)
+{
+	int count = 0;
+
+	assert(gt_dists.rows == dists.rows);
+	size_t nn = std::min(gt_dists.cols, dists.cols);
+	std::vector<T> gt_sorted_dists(nn), sorted_dists(nn), intersection(nn);
+
+	for (size_t i = 0; i < gt_dists.rows; ++i)
+	{
+		std::copy(gt_dists[i], gt_dists[i] + nn, gt_sorted_dists.begin());
+		std::sort(gt_sorted_dists.begin(), gt_sorted_dists.end());
+		std::copy(dists[i], dists[i] + nn, sorted_dists.begin());
+		std::sort(sorted_dists.begin(), sorted_dists.end());
+		typename std::vector<T>::iterator end = std::set_intersection(gt_sorted_dists.begin(), gt_sorted_dists.end(),
+				sorted_dists.begin(), sorted_dists.end(),
+				intersection.begin());
+		count += (end - intersection.begin());
+	}
+
+	return float(count) / (nn * gt_dists.rows);
+}
+
+
+const char* index_type_to_name(flann_algorithm_t index_type)
+{
+	switch (index_type) {
+	case FLANN_INDEX_LINEAR: return "linear";
+	case FLANN_INDEX_KDTREE: return "randomized kd-tree";
+	case FLANN_INDEX_KMEANS: return "k-means";
+	case FLANN_INDEX_COMPOSITE: return "composite";
+	case FLANN_INDEX_KDTREE_SINGLE: return "single kd-tree";
+	case FLANN_INDEX_HIERARCHICAL: return "hierarchical";
+	case FLANN_INDEX_LSH: return "LSH";
+#ifdef FLANN_USE_CUDA
+	case FLANN_INDEX_KDTREE_CUDA: return "kd-tree CUDA";
+#endif
+	case FLANN_INDEX_SAVED: return "saved";
+	case FLANN_INDEX_AUTOTUNED: return "autotuned";
+	default: return "(unknown)";
+	}
+}
+
+
+class FLANNTestFixture : public ::testing::Test {
+protected:
+	clock_t start_time_;
+
+	void start_timer(const std::string& message = "")
+	{
+		if (!message.empty()) {
+			printf("%s", message.c_str());
+			fflush(stdout);
+		}
+		start_time_ = clock();
+	}
+
+	double stop_timer()
+	{
+		return double(clock()-start_time_)/CLOCKS_PER_SEC;
+	}
+
+
+	template<typename Distance>
+	void TestSearch(const flann::Matrix<typename Distance::ElementType>& data,
+			const flann::IndexParams& index_params,
+			const flann::Matrix<typename Distance::ElementType>& query,
+			flann::Matrix<size_t>& indices,
+			flann::Matrix<typename Distance::ResultType>& dists,
+			size_t knn,
+			const flann::SearchParams& search_params,
+			float expected_precision,
+			const flann::Matrix<size_t>& gt_indices,
+			const flann::Matrix<typename Distance::ResultType>& gt_dists = flann::Matrix<typename Distance::ResultType>())
+	{
+		flann::seed_random(0);
+		Index<Distance> index(data, index_params);
+		char message[256];
+		const char* index_name = index_type_to_name(index.getType());
+		sprintf(message, "Building %s index... ", index_name);
+		start_timer( message );
+		index.buildIndex();
+		printf("done (%g seconds)\n", stop_timer());
+
+		start_timer("Searching KNN...");
+		index.knnSearch(query, indices, dists, knn, search_params );
+		printf("done (%g seconds)\n", stop_timer());
+
+		float precision;
+		if (gt_dists.ptr()==NULL) {
+			precision = compute_precision(gt_indices, indices);
+		}
+		else {
+			precision = computePrecisionDiscrete(gt_dists, dists);
+		}
+		EXPECT_GE(precision, expected_precision);
+		printf("Precision: %g\n", precision);
+	}
+
+	template<typename Distance>
+	void TestAddIncremental(const flann::Matrix<typename Distance::ElementType>& data,
+			const flann::IndexParams& index_params,
+			const flann::Matrix<typename Distance::ElementType>& query,
+			flann::Matrix<size_t>& indices,
+			flann::Matrix<typename Distance::ResultType>& dists,
+			size_t knn,
+			const flann::SearchParams& search_params,
+			float expected_precision,
+			flann::Matrix<size_t>& gt_indices,
+			const flann::Matrix<typename Distance::ResultType>& gt_dists = flann::Matrix<typename Distance::ResultType>())
+	{
+		flann::seed_random(0);
+		size_t size1 = data.rows/2-1;
+		size_t size2 = data.rows-size1;
+		Matrix<typename Distance::ElementType> data1(data[0], size1, data.cols);
+		Matrix<typename Distance::ElementType> data2(data[size1], size2, data.cols);
+		Index<Distance> index(data1, index_params);
+		char message[256];
+		const char* index_name = index_type_to_name(index.getType());
+		sprintf(message, "Building %s index... ", index_name);
+		start_timer( message );
+		index.buildIndex();
+		EXPECT_EQ(index.size(), data1.rows);
+
+		index.addPoints(data2);
+		printf("done (%g seconds)\n", stop_timer());
+
+		EXPECT_EQ(index.size(), data.rows);
+
+		start_timer("Searching KNN...");
+		index.knnSearch(query, indices, dists, knn, search_params);
+		printf("done (%g seconds)\n", stop_timer());
+
+		float precision;
+		if (gt_dists.ptr()==NULL) {
+			precision = compute_precision(gt_indices, indices);
+		}
+		else {
+			precision = computePrecisionDiscrete(gt_dists, dists);
+		}
+		EXPECT_GE(precision, expected_precision);
+		printf("Precision: %g\n", precision);
+
+		for (size_t i=0;i<indices.rows;++i) {
+			for (size_t j=0;j<indices.cols;++j) {
+				EXPECT_EQ(index.getPoint(indices[i][j]), data[indices[i][j]]);
+			}
+		}
+
+		// save and re-load the index and make sure everything still checks out
+		index.save("test_saved_index.idx");
+		Index<Distance > index2(data, flann::SavedIndexParams("test_saved_index.idx"));
+		index2.buildIndex();
+
+		EXPECT_EQ(index2.size(), data.rows);
+
+		flann::Matrix<size_t> indices2(new size_t[query.rows*knn], query.rows, knn);
+		flann::Matrix<typename Distance::ResultType> dists2(new typename Distance::ResultType[query.rows*knn], query.rows, knn);
+		start_timer("Searching KNN after saving and reloading index...");
+		index2.knnSearch(query, indices2, dists2, knn, search_params );
+		printf("done (%g seconds)\n", stop_timer());
+
+		for (size_t i=0;i<indices.rows;++i) {
+			for (size_t j=0;j<indices.cols;++j) {
+				EXPECT_EQ(indices[i][j], indices2[i][j]);
+			}
+		}
+		delete[] indices2.ptr();
+		delete[] dists2.ptr();
+	}
+
+	template<typename Distance>
+	void TestAddIncremental2(const flann::Matrix<typename Distance::ElementType>& data,
+			const flann::IndexParams& index_params,
+			const flann::Matrix<typename Distance::ElementType>& query,
+			flann::Matrix<size_t>& indices,
+			flann::Matrix<typename Distance::ResultType>& dists,
+			size_t knn,
+			const flann::SearchParams& search_params,
+			float expected_precision,
+			flann::Matrix<size_t>& gt_indices,
+			const flann::Matrix<typename Distance::ResultType>& gt_dists = flann::Matrix<typename Distance::ResultType>())
+	{
+		flann::seed_random(0);
+		size_t size1 = data.rows/2+1;
+		size_t size2 = data.rows-size1;
+		Matrix<typename Distance::ElementType> data1(data[0], size1, data.cols);
+		Matrix<typename Distance::ElementType> data2(data[size1], size2, data.cols);
+		Index<Distance> index(data1, index_params);
+		char message[256];
+		const char* index_name = index_type_to_name(index.getType());
+		sprintf(message, "Building %s index... ", index_name);
+		start_timer( message );
+		index.buildIndex();
+		EXPECT_EQ(index.size(), data1.rows);
+
+		index.addPoints(data2);
+		printf("done (%g seconds)\n", stop_timer());
+
+		EXPECT_EQ(index.size(), data.rows);
+
+		start_timer("Searching KNN...");
+		index.knnSearch(query, indices, dists, knn, search_params);
+		printf("done (%g seconds)\n", stop_timer());
+
+		float precision;
+		if (gt_dists.ptr()==NULL) {
+			precision = compute_precision(gt_indices, indices);
+		}
+		else {
+			precision = computePrecisionDiscrete(gt_dists, dists);
+		}
+		EXPECT_GE(precision, expected_precision);
+		printf("Precision: %g\n", precision);
+
+		for (size_t i=0;i<indices.rows;++i) {
+			for (size_t j=0;j<indices.cols;++j) {
+				EXPECT_EQ(index.getPoint(indices[i][j]), data[indices[i][j]]);
+			}
+		}
+
+		// save and re-load the index and make sure everything still checks out
+		index.save("test_saved_index.idx");
+		Index<Distance > index2(data, flann::SavedIndexParams("test_saved_index.idx"));
+		index2.buildIndex();
+
+		EXPECT_EQ(index2.size(), data.rows);
+
+		flann::Matrix<size_t> indices2(new size_t[query.rows*knn], query.rows, knn);
+		flann::Matrix<typename Distance::ResultType> dists2(new typename Distance::ResultType[query.rows*knn], query.rows, knn);
+		start_timer("Searching KNN after saving and reloading index...");
+		index2.knnSearch(query, indices2, dists2, knn, search_params );
+		printf("done (%g seconds)\n", stop_timer());
+
+		for (size_t i=0;i<indices.rows;++i) {
+			for (size_t j=0;j<indices.cols;++j) {
+				EXPECT_EQ(indices[i][j], indices2[i][j]);
+			}
+		}
+		delete[] indices2.ptr();
+		delete[] dists2.ptr();
+	}
+
+
+	template<typename Distance>
+	void TestSave(const flann::Matrix<typename Distance::ElementType>& data,
+			const flann::IndexParams& index_params,
+			const flann::Matrix<typename Distance::ElementType>& query,
+			flann::Matrix<size_t>& indices,
+			flann::Matrix<typename Distance::ResultType>& dists,
+			size_t knn,
+			const flann::SearchParams& search_params,
+			float expected_precision,
+			flann::Matrix<size_t>& gt_indices,
+			const flann::Matrix<typename Distance::ResultType>& gt_dists = flann::Matrix<typename Distance::ResultType>())
+	{
+		flann::seed_random(0);
+		Index<Distance> index(data, index_params);
+		char message[256];
+		const char* index_name = index_type_to_name(index.getType());
+		sprintf(message, "Building %s index... ", index_name);
+		start_timer( message );
+		index.buildIndex();
+		printf("done (%g seconds)\n", stop_timer());
+
+		EXPECT_EQ(index.size(), data.rows);
+
+		start_timer("Searching KNN...");
+		index.knnSearch(query, indices, dists, knn, search_params );
+		printf("done (%g seconds)\n", stop_timer());
+
+		float precision;
+		if (gt_dists.ptr()==NULL) {
+			precision = compute_precision(gt_indices, indices);
+		}
+		else {
+			precision = computePrecisionDiscrete(gt_dists, dists);
+		}
+		printf("Precision: %g\n", precision);
+		EXPECT_GE(precision, expected_precision);
+
+		printf("Saving index\n");
+		index.save("test_saved_index.idx");
+
+		printf("Loading index\n");
+		Index<Distance > index2(data, flann::SavedIndexParams("test_saved_index.idx"));
+		index2.buildIndex();
+
+		EXPECT_EQ(index2.size(), data.rows);
+
+		flann::Matrix<size_t> indices2(new size_t[query.rows*knn], query.rows, knn);
+		flann::Matrix<typename Distance::ResultType> dists2(new typename Distance::ResultType[query.rows*knn], query.rows, knn);
+		start_timer("Searching KNN after saving and reloading index...");
+		index2.knnSearch(query, indices2, dists2, knn, search_params );
+		printf("done (%g seconds)\n", stop_timer());
+
+		float precision2;
+		if (gt_dists.ptr()==NULL) {
+			precision2 = compute_precision(gt_indices, indices2);
+		}
+		else {
+			precision2 = computePrecisionDiscrete(gt_dists, dists2);
+		}
+		printf("Precision: %g\n", precision2);
+		EXPECT_EQ(precision, precision2);
+
+		for (size_t i=0;i<indices.rows;++i) {
+			for (size_t j=0;j<indices.cols;++j) {
+				EXPECT_EQ(indices[i][j], indices2[i][j]);
+			}
+		}
+		delete[] indices2.ptr();
+		delete[] dists2.ptr();
+	}
+
+
+	template<typename Distance>
+	void TestCopy(const flann::Matrix<typename Distance::ElementType>& data,
+			const flann::IndexParams& index_params,
+			const flann::Matrix<typename Distance::ElementType>& query,
+			flann::Matrix<size_t>& indices,
+			flann::Matrix<typename Distance::ResultType>& dists,
+			size_t knn,
+			const flann::SearchParams& search_params,
+			float expected_precision,
+			flann::Matrix<size_t>& gt_indices,
+			const flann::Matrix<typename Distance::ResultType>& gt_dists = flann::Matrix<typename Distance::ResultType>())
+	{
+		flann::seed_random(0);
+		Index<Distance> index(data, index_params);
+		char message[256];
+		const char* index_name = index_type_to_name(index.getType());
+		sprintf(message, "Building %s index... ", index_name);
+		start_timer( message );
+		index.buildIndex();
+		printf("done (%g seconds)\n", stop_timer());
+
+		start_timer("Searching KNN...");
+		index.knnSearch(query, indices, dists, knn, search_params );
+		printf("done (%g seconds)\n", stop_timer());
+
+		float precision;
+		if (gt_dists.ptr()==NULL) {
+			precision = compute_precision(gt_indices, indices);
+		}
+		else {
+			precision = computePrecisionDiscrete(gt_dists, dists);
+		}
+		printf("Precision: %g\n", precision);
+		EXPECT_GE(precision, expected_precision);
+
+		// test copy constructor
+		Index<Distance> index2(index);
+
+		start_timer("Searching KNN...");
+		index2.knnSearch(query, indices, dists, knn, search_params );
+		printf("done (%g seconds)\n", stop_timer());
+
+		float precision2;
+		if (gt_dists.ptr()==NULL) {
+			precision2 = compute_precision(gt_indices, indices);
+		}
+		else {
+			precision2 = computePrecisionDiscrete(gt_dists, dists);
+		}
+		printf("Precision: %g\n", precision2);
+		EXPECT_EQ(precision, precision2);
+
+		// test assignment operator
+		Index<Distance > index3(data, index_params);
+		index3 = index;
+
+		start_timer("Searching KNN...");
+		index3.knnSearch(query, indices, dists, knn, search_params );
+		printf("done (%g seconds)\n", stop_timer());
+
+		float precision3;
+		if (gt_dists.ptr()==NULL) {
+			precision3 = compute_precision(gt_indices, indices);
+		}
+		else {
+			precision3 = computePrecisionDiscrete(gt_dists, dists);
+		}
+		printf("Precision: %g\n", precision3);
+		EXPECT_EQ(precision, precision3);
+	}
+
+
+	template<typename Index>
+	void TestCopy2(const flann::Matrix<typename Index::ElementType>& data,
+			const flann::IndexParams& index_params,
+			const flann::Matrix<typename Index::ElementType>& query,
+			flann::Matrix<size_t>& indices,
+			flann::Matrix<typename Index::DistanceType>& dists,
+			size_t knn,
+			const flann::SearchParams& search_params,
+			float expected_precision,
+			flann::Matrix<size_t>& gt_indices,
+			const flann::Matrix<typename Index::DistanceType>& gt_dists = flann::Matrix<typename Index::DistanceType>())
+	{
+		flann::seed_random(0);
+		Index index(data, index_params);
+		char message[256];
+		const char* index_name = index_type_to_name(index.getType());
+		sprintf(message, "Building %s index... ", index_name);
+		start_timer( message );
+		index.buildIndex();
+		printf("done (%g seconds)\n", stop_timer());
+
+		start_timer("Searching KNN...");
+		index.knnSearch(query, indices, dists, knn, search_params );
+		printf("done (%g seconds)\n", stop_timer());
+
+		float precision;
+		if (gt_dists.ptr()==NULL) {
+			precision = compute_precision(gt_indices, indices);
+		}
+		else {
+			precision = computePrecisionDiscrete(gt_dists, dists);
+		}
+		printf("Precision: %g\n", precision);
+		EXPECT_GE(precision, expected_precision);
+
+		// test copy constructor
+		Index index2(index);
+
+		start_timer("Searching KNN...");
+		index2.knnSearch(query, indices, dists, knn, search_params );
+		printf("done (%g seconds)\n", stop_timer());
+
+		float precision2;
+		if (gt_dists.ptr()==NULL) {
+			precision2 = compute_precision(gt_indices, indices);
+		}
+		else {
+			precision2 = computePrecisionDiscrete(gt_dists, dists);
+		}
+		printf("Precision: %g\n", precision2);
+		EXPECT_EQ(precision, precision2);
+
+		// test assignment operator
+		Index index3(data, index_params);
+		index3 = index;
+
+		start_timer("Searching KNN...");
+		index3.knnSearch(query, indices, dists, knn, search_params );
+		printf("done (%g seconds)\n", stop_timer());
+
+		float precision3;
+		if (gt_dists.ptr()==NULL) {
+			precision3 = compute_precision(gt_indices, indices);
+		}
+		else {
+			precision3 = computePrecisionDiscrete(gt_dists, dists);
+		}
+		printf("Precision: %g\n", precision3);
+		EXPECT_EQ(precision, precision3);
+	}
+
+	template<typename Distance>
+	void TestRemove(const flann::Matrix<typename Distance::ElementType>& data,
+			const flann::IndexParams& index_params,
+			const flann::Matrix<typename Distance::ElementType>& query,
+			flann::Matrix<size_t>& indices,
+			flann::Matrix<typename Distance::ResultType>& dists,
+			size_t knn,
+			const flann::SearchParams& search_params)
+	{
+		flann::seed_random(0);
+		Index< Distance > index(data, index_params);
+		char message[256];
+		const char* index_name = index_type_to_name(index.getType());
+		sprintf(message, "Building %s index... ", index_name);
+		start_timer( message );
+		index.buildIndex();
+		printf("done (%g seconds)\n", stop_timer());
+
+		start_timer("Searching KNN before removing points...");
+		index.knnSearch(query, indices, dists, knn, search_params );
+		printf("done (%g seconds)\n", stop_timer());
+
+		EXPECT_EQ(index.size(), data.rows);
+
+		// remove about 50% of neighbours found
+		std::set<size_t> neighbors;
+		for (size_t i=0;i<indices.rows;++i) {
+			for (size_t j=0;j<indices.cols;++j) {
+				if (rand_double()<0.5) {
+					neighbors.insert(indices[i][j]);
+				}
+			}
+		}
+
+		flann::DynamicBitset removed(data.rows);
+
+		for (std::set<size_t>::iterator it = neighbors.begin(); it!=neighbors.end();++it) {
+			index.removePoint(*it);
+			removed.set(*it);
+		}
+
+		// also remove 10% of the initial points
+		size_t offset = data.rows/10;
+		for (size_t i=0;i<offset;++i) {
+			index.removePoint(i);
+			removed.set(i);
+		}
+
+		size_t new_size = 0;
+		for (size_t i=0;i<removed.size();++i) {
+			if (!removed.test(i)) ++new_size;
+		}
+
+		EXPECT_EQ(index.size(), new_size);
+
+		start_timer("Searching KNN after remove points...");
+		index.knnSearch(query, indices, dists, knn, search_params );
+		printf("done (%g seconds)\n", stop_timer());
+
+		for (size_t i=0;i<indices.rows;++i) {
+			for (size_t j=0;j<indices.cols;++j) {
+				EXPECT_GE(indices[i][j], offset);
+				EXPECT_TRUE(neighbors.find(indices[i][j])==neighbors.end());
+				EXPECT_EQ(index.getPoint(indices[i][j]), data[indices[i][j]]);
+			}
+		}
+
+		// save and re-load the index and make sure everything still checks out
+		index.save("test_saved_index.idx");
+		flann::seed_random(0);
+		Index< Distance > index2(data, flann::SavedIndexParams("test_saved_index.idx"));
+		index2.buildIndex();
+
+		EXPECT_EQ(index2.size(), new_size);
+
+		flann::Matrix<size_t> indices2(new size_t[query.rows*knn], query.rows, knn);
+		flann::Matrix<typename Distance::ResultType> dists2(new typename Distance::ResultType[query.rows*knn], query.rows, knn);
+		start_timer("Searching KNN after saving and reloading index...");
+		index2.knnSearch(query, indices2, dists2, knn, search_params );
+		printf("done (%g seconds)\n", stop_timer());
+
+		for (size_t i=0;i<indices.rows;++i) {
+			for (size_t j=0;j<indices.cols;++j) {
+				EXPECT_EQ(indices[i][j], indices2[i][j]);
+			}
+		}
+		delete[] indices2.ptr();
+		delete[] dists2.ptr();
+
+
+		// rebuild index
+		index.buildIndex();
+
+		EXPECT_EQ(index.size(), new_size);
+
+		start_timer("Searching KNN after remove points and rebuild index...");
+		index.knnSearch(query, indices, dists, knn, search_params );
+		printf("done (%g seconds)\n", stop_timer());
+
+		for (size_t i=0;i<indices.rows;++i) {
+			for (size_t j=0;j<indices.cols;++j) {
+				EXPECT_GE(indices[i][j], offset);
+				EXPECT_TRUE(neighbors.find(indices[i][j])==neighbors.end());
+				EXPECT_EQ(index.getPoint(indices[i][j]), data[indices[i][j]]);
+			}
+		}
+	}
+
+};
+
+
+template<typename ElementType, typename DistanceType>
+class DatasetTestFixture : public FLANNTestFixture
+{
+protected:
+
+	std::string filename_;
+
+	flann::Matrix<ElementType> data;
+	flann::Matrix<ElementType> query;
+	flann::Matrix<size_t> gt_indices;
+	flann::Matrix<DistanceType> dists;
+	flann::Matrix<size_t> indices;
+
+	int knn;
+
+	DatasetTestFixture(const std::string& filename) : filename_(filename), knn(5)
+	{
+	}
+
+
+	void SetUp()
+	{
+		knn = 5;
+		printf("Reading test data...");
+		fflush(stdout);
+		flann::load_from_file(data, filename_.c_str(), "dataset");
+		flann::load_from_file(query, filename_.c_str(), "query");
+		flann::load_from_file(gt_indices, filename_.c_str(), "match");
+
+		dists = flann::Matrix<DistanceType>(new DistanceType[query.rows*knn], query.rows, knn);
+		indices = flann::Matrix<size_t>(new size_t[query.rows*knn], query.rows, knn);
+
+		printf("done\n");
+	}
+
+	void TearDown()
+	{
+		delete[] data.ptr();
+		delete[] query.ptr();
+		delete[] gt_indices.ptr();
+		delete[] dists.ptr();
+		delete[] indices.ptr();
+
+	}
+};
+
+#endif /* FLANN_TESTS_H_ */
+
diff --git a/test/memusage_clustering.py b/test/memusage_clustering.py
new file mode 100755
index 0000000..1b02bbd
--- /dev/null
+++ b/test/memusage_clustering.py
@@ -0,0 +1,75 @@
+#!/usr/bin/env python
+import sys
+from os.path import *
+from pyflann import *
+from guppy import hpy
+from numpy.random import rand
+
+import os, gc
+
+_proc_status = '/proc/%d/status' % os.getpid()
+
+_scale = {'kB': 1024.0, 'mB': 1024.0*1024.0,
+          'KB': 1024.0, 'MB': 1024.0*1024.0}
+
+def _VmB(VmKey):
+    '''Private.
+    '''
+    global _proc_status, _scale
+     # get pseudo file  /proc/<pid>/status
+    try:
+        t = open(_proc_status)
+        v = t.read()
+        t.close()
+    except:
+        return 0.0  # non-Linux?
+     # get VmKey line e.g. 'VmRSS:  9999  kB\n ...'
+    i = v.index(VmKey)
+    v = v[i:].split(None, 3)  # whitespace
+    if len(v) < 3:
+        return 0.0  # invalid format?
+     # convert Vm value to bytes
+    return float(v[1]) * _scale[v[2]]
+
+
+def memory(since=0.0):
+    '''Return memory usage in bytes.
+    '''
+    return _VmB('VmSize:') - since
+
+
+def resident(since=0.0):
+    '''Return resident memory usage in bytes.
+    '''
+    return _VmB('VmRSS:') - since
+
+
+def stacksize(since=0.0):
+    '''Return stack size in bytes.
+    '''
+    return _VmB('VmStk:') - since
+
+
+
+if __name__ == '__main__':
+
+    print 'Profiling Memory usage for pyflann; CTRL-C to stop.'
+    print 'Increasing total process memory, relative to the python memory, '
+    print 'implies a memory leak in the external libs.'
+    print 'Increasing python memory implies a memory leak in the python code.'
+    
+    h = hpy()
+
+    while True:
+        s = str(h.heap())
+
+        print 'Python: %s;    Process Total: %s' % (s[:s.find('\n')], memory())
+        
+        X = rand(30000, 2)
+        pf = FLANN()
+        cl = pf.kmeans(X, 20)
+        del X
+        del cl
+        del pf
+        gc.collect()
+        
diff --git a/test/memusage_nn.py b/test/memusage_nn.py
new file mode 100755
index 0000000..8d5933f
--- /dev/null
+++ b/test/memusage_nn.py
@@ -0,0 +1,77 @@
+#!/usr/bin/env python
+import sys
+from os.path import *
+from pyflann import *
+from guppy import hpy
+from numpy.random import rand
+
+import os, gc
+
+_proc_status = '/proc/%d/status' % os.getpid()
+
+_scale = {'kB': 1024.0, 'mB': 1024.0*1024.0,
+          'KB': 1024.0, 'MB': 1024.0*1024.0}
+
+def _VmB(VmKey):
+    '''Private.
+    '''
+    global _proc_status, _scale
+     # get pseudo file  /proc/<pid>/status
+    try:
+        t = open(_proc_status)
+        v = t.read()
+        t.close()
+    except:
+        return 0.0  # non-Linux?
+     # get VmKey line e.g. 'VmRSS:  9999  kB\n ...'
+    i = v.index(VmKey)
+    v = v[i:].split(None, 3)  # whitespace
+    if len(v) < 3:
+        return 0.0  # invalid format?
+     # convert Vm value to bytes
+    return float(v[1]) * _scale[v[2]]
+
+
+def memory(since=0.0):
+    '''Return memory usage in bytes.
+    '''
+    return _VmB('VmSize:') - since
+
+
+def resident(since=0.0):
+    '''Return resident memory usage in bytes.
+    '''
+    return _VmB('VmRSS:') - since
+
+
+def stacksize(since=0.0):
+    '''Return stack size in bytes.
+    '''
+    return _VmB('VmStk:') - since
+
+
+
+if __name__ == '__main__':
+
+    print 'Profiling Memory usage for pyflann; CTRL-C to stop.'
+    print 'Increasing total process memory, relative to the python memory, '
+    print 'implies a memory leak in the external libs.'
+    print 'Increasing python memory implies a memory leak in the python code.'
+    
+    h = hpy()
+
+    while True:
+        s = str(h.heap())
+
+        print 'Python: %s;    Process Total: %s' % (s[:s.find('\n')], memory())
+        
+        X1 = rand(50000, 2)
+        X2 = rand(50000, 2)
+        pf = FLANN()
+        nnlist = pf.nn(X1, X2)
+        del X1
+        del X2
+        del nnlist
+        del pf
+        gc.collect()
+        
diff --git a/test/test_clustering.py b/test/test_clustering.py
new file mode 100755
index 0000000..15cf8b4
--- /dev/null
+++ b/test/test_clustering.py
@@ -0,0 +1,95 @@
+#!/usr/bin/env python
+import sys
+from os.path import *
+from pyflann import *
+import os
+from copy import copy
+from numpy import *
+from numpy.random import *
+import unittest, time
+
+class Test_PyFLANN_clustering(unittest.TestCase):
+
+    def setUp(self):
+        self.nn = FLANN(iterations=11)
+
+    ################################################################################
+
+    def test_Rand(self):
+        x = rand(100, 10000)
+        nK = 10
+        centroids = self.nn.kmeans(x, nK)
+        self.assertTrue(len(centroids) == nK)
+        
+
+    def test2d_small(self):
+        self.__nd_random_clustering_test(2,2)
+
+    def test3d_small(self):
+        self.__nd_random_clustering_test(3,3)
+
+    def test4d_small(self):
+        self.__nd_random_clustering_test(4,4)
+
+    def test3d_large(self):
+        self.__nd_random_clustering_test(3,3, 1000)
+
+    def test10d_large(self):
+        self.__nd_random_clustering_test(10,2,10)
+
+    def test500d(self):
+        self.__nd_random_clustering_test(500,2, 10)
+
+    def __nd_random_clustering_test(self, dim, N, dup=1):
+        """
+        Make a set of random points, then pass the same ones to the
+        query points.  Each point should be closest to itself.
+        """
+        seed(0)
+        x = rand(N, dim)
+        xc = concatenate(tuple([x for i in range(dup)]))
+
+        if dup > 1: xc += randn(xc.shape[0], xc.shape[1])*0.000001/dim
+
+        rnseed = int(time.time())
+        centroids = self.nn.kmeans(xc[permutation(len(xc))], N, centers_init = "random", random_seed=2)
+        mindists = array([[ sum((d1-d2)**2) for d1 in x] for d2 in centroids]).min(0)
+        #print mindists
+        for m in mindists: self.assertAlmostEqual(m, 0.0, 1)
+
+        rnseed = int(time.time())
+        centroids = self.nn.kmeans(xc[permutation(len(xc))], N, centers_init = "gonzales", random_seed=2)
+        mindists = array([[ sum((d1-d2)**2) for d1 in x] for d2 in centroids]).min(0)
+        #print mindists
+        for m in mindists: self.assertAlmostEqual(m, 0.0, 1)
+        
+        centroids = self.nn.kmeans(xc[permutation(len(xc))], N, centers_init = "kmeanspp", random_seed=2)
+        mindists = array([[ sum((d1-d2)**2) for d1 in x] for d2 in centroids]).min(0)
+        #print mindists
+        for m in mindists: self.assertAlmostEqual(m, 0.0, 1)
+
+    def testrandomnumber_same(self):
+        
+        data = rand(1000,2) # Random, so we can get a lot of local minima
+
+        rnseed = int(time.time())
+        cl1 = self.nn.kmeans(data, 50, random_seed = rnseed)
+        cl2 = self.nn.kmeans(data, 50, random_seed = rnseed)
+
+        self.assertTrue(all(cl1 == cl2))
+
+
+    def testrandnumber_different(self):
+        
+        data = rand(1000,100) # Random, so we can get a lot of local minima
+
+        rnseed = int(time.time())
+        cl1 = self.nn.kmeans(data, 50, random_seed = rnseed)
+        cl2 = self.nn.kmeans(data, 50)
+
+        self.assertTrue(any(cl1 != cl2))
+        
+
+        
+if __name__ == '__main__':
+    unittest.main()
diff --git a/test/test_index_save.py b/test/test_index_save.py
new file mode 100755
index 0000000..9b6399a
--- /dev/null
+++ b/test/test_index_save.py
@@ -0,0 +1,106 @@
+#!/usr/bin/env python
+
+from pyflann import *
+from copy import copy
+from numpy import *
+from numpy.random import *
+import unittest
+
+
+class Test_PyFLANN_nn(unittest.TestCase):
+
+    def setUp(self):
+        self.nn = FLANN()
+
+
+class Test_PyFLANN_nn_index(unittest.TestCase):
+    
+    def testnn_index_save_kdtree_1(self):
+        self.run_nn_index_save_perturbed(64,1000, algorithm="kdtree", trees=1)
+
+    def testnn_index_save_kdtree_4(self):
+        self.run_nn_index_save_perturbed(64,1000, algorithm="kdtree", trees=4)
+
+    def testnn_index_save_kdtree_10(self):
+        self.run_nn_index_save_perturbed(64,1000, algorithm="kdtree", trees=10)
+
+
+    def testnn_index_save_kmeans_2(self):
+        self.run_nn_index_save_perturbed(64,1000, algorithm="kmeans", branching=2, iterations=11)
+
+    def testnn_index_save_kmeans_16(self):
+        self.run_nn_index_save_perturbed(64,1000, algorithm="kmeans", branching=16, iterations=11)
+    
+    def testnn_index_save_kmeans_32(self):
+        self.run_nn_index_save_perturbed(64,1000, algorithm="kmeans", branching=32, iterations=11)
+    
+    def testnn_index_save_kmeans_64(self):
+        self.run_nn_index_save_perturbed(64,1000, algorithm="kmeans", branching=64, iterations=11)
+
+
+    def testnn__save_kdtree_1(self):
+        self.run_nn_index_save_rand(64,10000,1000, algorithm="kdtree", trees=1, checks=128)    
+
+    def testnn__save_kdtree_4(self):
+        self.run_nn_index_save_rand(64,10000,1000, algorithm="kdtree", trees=4, checks=128)
+
+    def testnn__save_kdtree_10(self):
+        self.run_nn_index_save_rand(64,10000,1000, algorithm="kdtree", trees=10, checks=128)
+
+    def testnn__save_kmeans_2(self):
+        self.run_nn_index_save_rand(64,1000,1000, algorithm="kmeans", branching=2, iterations=11, checks=64)
+
+    def testnn__save_kmeans_8(self):
+        self.run_nn_index_save_rand(64,10000,1000, algorithm="kmeans", branching=8, iterations=11, checks=32)
+
+    def testnn__save_kmeans_16(self):
+        self.run_nn_index_save_rand(64,10000,1000, algorithm="kmeans", branching=16, iterations=11, checks=40)
+    
+    def testnn__save_kmeans_32(self):
+        self.run_nn_index_save_rand(64,10000,1000, algorithm="kmeans", branching=32, iterations=11, checks=56)
+
+
+
+
+    def run_nn_index_save_perturbed(self, dim, N, **kwargs):
+        
+        x = rand(N, dim)
+
+        nn = FLANN()
+        nn.build_index(x, **kwargs)
+        nn.save_index("index.dat")
+        nn.delete_index();
+
+        nn = FLANN()
+        nn.load_index("index.dat",x)
+        x_query = x + randn(x.shape[0], x.shape[1])*0.0001/dim
+        nnidx, nndist = nn.nn_index(x_query)
+        correct = all(nnidx == arange(N, dtype = index_type))
+                
+        nn.delete_index()
+        self.assertTrue(correct)
+    
+    def run_nn_index_save_rand(self, dim, N, Nq, **kwargs):
+
+        x = rand(N, dim)
+        x_query = rand(Nq,dim)
+
+        # build index, search and delete it
+        nn = FLANN()
+        nn.build_index(x, **kwargs)
+        nnidx, nndist = nn.nn_index(x_query, checks=kwargs["checks"])
+        nn.save_index("index.dat")
+        del nn
+
+
+        # now reload index and search again
+        nn = FLANN()
+        nn.load_index("index.dat",x)
+        nnidx2, nndist2 = nn.nn_index(x_query, checks=kwargs["checks"])
+        del nn
+
+        correct = all(nnidx == nnidx2)
+        self.assertTrue(correct)
+
+if __name__ == '__main__':
+    unittest.main()
diff --git a/test/test_nn.py b/test/test_nn.py
new file mode 100755
index 0000000..6ebe7e5
--- /dev/null
+++ b/test/test_nn.py
@@ -0,0 +1,118 @@
+#!/usr/bin/env python
+import sys
+from os.path import *
+import os
+from pyflann import *
+from copy import copy
+from numpy import *
+from numpy.random import *
+import unittest
+
+class Test_PyFLANN_nn(unittest.TestCase):
+
+    def setUp(self):
+        self.nn = FLANN()
+
+    ################################################################################
+    # The typical
+    
+    def test_nn_2d_10pt_kmeans(self):
+        self.__nd_random_test(2, 2, algorithm='kdtree')
+
+    def test_nn_2d_1000pt_kmeans(self):
+        self.__nd_random_test(2, 1000, algorithm='kmeans')
+
+    def test_nn_100d_1000pt_kmeans(self):
+        self.__nd_random_test(100, 1000, algorithm='kmeans')
+
+    def test_nn_500d_100pt_kmeans(self):
+        self.__nd_random_test(500, 100, algorithm='kmeans')
+
+    def test_nn_2d_1000pt_kdtree(self):
+        self.__nd_random_test(2, 1000, algorithm='kdtree')
+
+    def test_nn_100d_1000pt_kdtree(self):
+        self.__nd_random_test(100, 1000, algorithm='kdtree')
+
+    def test_nn_500d_100pt_kdtree(self):
+        self.__nd_random_test(500, 100, algorithm='kdtree')
+
+    def test_nn_2d_1000pt_linear(self):
+        self.__nd_random_test(2, 1000, algorithm='linear')
+
+    def test_nn_100d_50pt_linear(self):
+        self.__nd_random_test(100, 50, algorithm='linear')
+
+
+    def test_nn_2d_1000pt_composite(self):
+        self.__nd_random_test(2, 1000, algorithm='composite')
+
+    def test_nn_100d_1000pt_composite(self):
+        self.__nd_random_test(100, 1000, algorithm='composite')
+
+    def test_nn_500d_100pt_composite(self):
+        self.__nd_random_test(500, 100, algorithm='composite')
+
+
+    def test_nn_multtrees_2d_1000pt_kmeans(self):
+        self.__nd_random_test(2, 1000, algorithm='kmeans', trees=8)
+
+    def test_nn_multtrees_100d_1000pt_kmeans(self):
+        self.__nd_random_test(100, 1000, algorithm='kmeans', trees=8)
+
+    def test_nn_multtrees_500d_100pt_kmeans(self):
+        self.__nd_random_test(500, 100, algorithm='kmeans', trees=8)
+
+
+
+    ##########################################################################################
+    # Stress it should handle
+
+    def test_nn_stress_1d_1pt_kmeans(self):
+        self.__nd_random_test(1, 1, algorithm='kmeans')
+
+    def test_nn_stress_1d_1pt_linear(self):
+        self.__nd_random_test(1, 1, algorithm='linear')
+
+    def test_nn_stress_1d_1pt_kdtree(self):
+        self.__nd_random_test(1, 1, algorithm='kdtree')
+
+    def test_nn_stress_1d_1pt_composite(self):
+        self.__nd_random_test(1, 1, algorithm='composite')
+
+    def __nd_random_test(self, dim, N, type=float32, num_neighbors = 10, **kwargs):
+        """
+        Make a set of random points, then pass the same ones to the
+        query points.  Each point should be closest to itself.
+        """
+        seed(0)
+        x = array(rand(N, dim), dtype=type)
+        perm = permutation(N)
+        
+        idx,dists = self.nn.nn(x, x[perm], **kwargs)
+        self.assertTrue(all(idx == perm))
+
+        # Make sure it's okay if we do make all the points equal
+        x_mult_nn = concatenate([x for i in range(num_neighbors)])
+        nidx,ndists = self.nn.nn(x_mult_nn, x, num_neighbors = num_neighbors, **kwargs)
+
+        correctness = 0.0
+
+        for i in range(N):
+            correctness += float(len(set(nidx[i]).intersection([i + n*N for n in range(num_neighbors)])))/num_neighbors
+
+        self.assertTrue(correctness / N >= 0.99,
+                     'failed #1: N=%d,correctness=%f' % (N, correctness/N))
+
+        # now what happens if they are slightly off
+        x_mult_nn += randn(x_mult_nn.shape[0], x_mult_nn.shape[1])*0.0001/dim
+        n2idx,n2dists = self.nn.nn(x_mult_nn, x, num_neighbors = num_neighbors, **kwargs)
+
+        for i in range(N):
+            correctness += float(len(set(n2idx[i]).intersection([i + n*N for n in range(num_neighbors)])))/num_neighbors
+
+        self.assertTrue(correctness / N >= 0.99,
+                     'failed #2: N=%d,correctness=%f' % (N, correctness/N))
+        
+if __name__ == '__main__':
+    unittest.main()
diff --git a/test/test_nn_autotune.py b/test/test_nn_autotune.py
new file mode 100755
index 0000000..00155e6
--- /dev/null
+++ b/test/test_nn_autotune.py
@@ -0,0 +1,77 @@
+#!/usr/bin/env python
+import sys
+from os.path import *
+import os
+from pyflann import *
+from copy import copy
+from numpy import *
+from numpy.random import *
+import unittest
+
+class Test_PyFLANN_nn(unittest.TestCase):
+
+    def setUp(self):
+        self.nn = FLANN(log_level="warning")
+
+    ################################################################################
+    # The typical
+    
+    def test_nn_2d_10pt(self):
+        self.__nd_random_test_autotune(2, 2)
+        
+    def test_nn_autotune_2d_1000pt(self):
+        self.__nd_random_test_autotune(2, 1000)
+
+    def test_nn_autotune_100d_1000pt(self):
+        self.__nd_random_test_autotune(100, 1000)
+    
+    def test_nn_autotune_500d_100pt(self):
+        self.__nd_random_test_autotune(500, 100)
+    
+    #
+    #    ##########################################################################################
+    #    # Stress it should handle
+    #
+    def test_nn_stress_1d_1pt_kmeans_autotune(self):
+        self.__nd_random_test_autotune(1, 1)
+    
+    def __ensure_list(self,arg):
+        if type(arg)!=list:
+            return [arg]
+        else:
+            return arg
+
+
+    def __nd_random_test_autotune(self, dim, N, num_neighbors = 1, **kwargs):
+        """
+        Make a set of random points, then pass the same ones to the
+        query points.  Each point should be closest to itself.
+        """
+        seed(0)
+        x = rand(N, dim)
+        xq = rand(N, dim)
+        perm = permutation(N)
+
+        # compute ground truth nearest neighbors
+        gt_idx, gt_dist = self.nn.nn(x,xq, 
+                algorithm='linear', 
+                num_neighbors=num_neighbors)
+        
+        for tp in [0.70, 0.80, 0.90]:
+            nidx,ndist = self.nn.nn(x, xq, 
+                    algorithm='autotuned', 
+                    sample_fraction=1.0, 
+                    num_neighbors = num_neighbors, 
+                    target_precision = tp, checks=-2, **kwargs)
+
+            correctness = 0.0
+            for i in range(N):
+                l1 = self.__ensure_list(nidx[i])
+                l2 = self.__ensure_list(gt_idx[i])
+                correctness += float(len(set(l1).intersection(l2)))/num_neighbors
+            correctness /= N
+            self.assertTrue(correctness >= tp*0.9,
+                         'failed #1: targ_prec=%f, N=%d,correctness=%f' % (tp, N, correctness))
+        
+if __name__ == '__main__':
+    unittest.main()
diff --git a/test/test_nn_index.py b/test/test_nn_index.py
new file mode 100755
index 0000000..2290e73
--- /dev/null
+++ b/test/test_nn_index.py
@@ -0,0 +1,81 @@
+#!/usr/bin/env python
+
+from pyflann import *
+from copy import copy
+from numpy import *
+from numpy.random import *
+import unittest
+
+
+class Test_PyFLANN_nn(unittest.TestCase):
+
+    def setUp(self):
+        self.nn = FLANN()
+
+
+class Test_PyFLANN_nn_index(unittest.TestCase):
+    
+    def testnn_index(self):
+
+        dim = 10
+        N = 100
+
+        x   = rand(N, dim)
+        nn = FLANN()
+        nn.build_index(x)
+        
+        nnidx, nndist = nn.nn_index(x)
+        correct = all(nnidx == arange(N, dtype = index_type))
+                
+        nn.delete_index()
+        self.assertTrue(correct)
+
+
+    def testnn_index_random_permute(self):
+
+        numtests = 500
+        dim = 10
+        N = 100
+
+        nns = [None]*numtests
+        x   = [rand(N, dim) for i in range(numtests)]
+        correct = ones(numtests, dtype=bool_)
+        
+        for i in permutation(numtests):
+            nns[i] = FLANN()
+            nns[i].build_index(x[i])
+
+            # For kicks
+            if rand() < 0.5:
+               nns[i].kmeans(x[i], 5)
+            if rand() < 0.5:
+               nns[i].nn(x[i], x[i])
+
+
+        for i in permutation(numtests):
+            nnidx,nndist = nns[i].nn_index(x[i])
+            correct[i] = all(nnidx == arange(N, dtype = index_type))
+
+        for i in reversed(range(numtests)):
+            if rand() < 0.5:
+                nns[i].delete_index()
+            else:
+                del nns[i]
+
+        self.assertTrue(all(correct))
+
+    def testnn_index_bad_index_call_noindex(self):
+        nn = FLANN()
+        self.assertRaises(FLANNException, lambda: nn.nn_index(rand(5,5)))
+
+
+    def testnn_index_bad_index_call_delindex(self):
+        nn = FLANN()
+        nn.build_index(rand(5,5))
+        nn.delete_index()
+       
+        self.assertRaises(FLANNException, lambda: nn.nn_index(rand(5,5)))
+
+
+if __name__ == '__main__':
+    unittest.main()

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/flann.git



More information about the debian-science-commits mailing list