00001 // **************************************************************************** 00002 // This file is part of the Integrating Vision Toolkit (IVT). 00003 // 00004 // The IVT is maintained by the Karlsruhe Institute of Technology (KIT) 00005 // (www.kit.edu) in cooperation with the company Keyetech (www.keyetech.de). 00006 // 00007 // Copyright (C) 2014 Karlsruhe Institute of Technology (KIT). 00008 // All rights reserved. 00009 // 00010 // Redistribution and use in source and binary forms, with or without 00011 // modification, are permitted provided that the following conditions are met: 00012 // 00013 // 1. Redistributions of source code must retain the above copyright 00014 // notice, this list of conditions and the following disclaimer. 00015 // 00016 // 2. Redistributions in binary form must reproduce the above copyright 00017 // notice, this list of conditions and the following disclaimer in the 00018 // documentation and/or other materials provided with the distribution. 00019 // 00020 // 3. Neither the name of the KIT nor the names of its contributors may be 00021 // used to endorse or promote products derived from this software 00022 // without specific prior written permission. 00023 // 00024 // THIS SOFTWARE IS PROVIDED BY THE KIT AND CONTRIBUTORS “AS IS” AND ANY 00025 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00026 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00027 // DISCLAIMED. IN NO EVENT SHALL THE KIT OR CONTRIBUTORS BE LIABLE FOR ANY 00028 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00029 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00030 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00031 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00032 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 00033 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00034 // **************************************************************************** 00035 // **************************************************************************** 00036 // Filename: KdTree.h 00037 // Author: Kai Welke 00038 // Date: 14.04.2005 00039 // **************************************************************************** 00040 00041 00042 #ifndef _KD_TREE_H_ 00043 #define _KD_TREE_H_ 00044 00045 00046 // **************************************************************************** 00047 // Necessary includes 00048 // **************************************************************************** 00049 00050 #include "KdStructs.h" 00051 00052 00053 // **************************************************************************** 00054 // Forward declarations 00055 // **************************************************************************** 00056 00057 class CKdPriorityQueue; 00058 00059 00060 // **************************************************************************** 00061 // CKdTreeNode 00062 // **************************************************************************** 00063 00064 class CKdTreeNode 00065 { 00066 public: 00067 CKdTreeNode() 00068 { 00069 m_pLeftChild = 0; 00070 m_pRightChild = 0; 00071 m_nCutDimension = 0; 00072 m_bIsLeaf = 0; 00073 m_Bounding.fLow = 0; 00074 m_Bounding.fHigh = 0; 00075 m_nSize = 0; 00076 m_pValues = 0; 00077 } 00078 00079 ~CKdTreeNode() 00080 { 00081 if (m_pValues) 00082 delete [] m_pValues; 00083 } 00084 00085 00086 // public methods 00087 CKdTreeNode *m_pLeftChild, *m_pRightChild; 00088 00089 float m_fMedianValue; 00090 00091 KdBounding m_Bounding; 00092 00093 int m_bIsLeaf; 00094 int m_nCutDimension; 00095 00096 int m_nSize; 00097 float *m_pValues; 00098 }; 00099 00100 00101 00102 // **************************************************************************** 00103 // CKdTree 00104 // **************************************************************************** 00105 00106 class CKdTree 00107 { 00108 public: 00109 // constructor 00110 CKdTree(int nMaximumNumberOfNodes = 10000); 00111 00112 // destructor 00113 ~CKdTree(); 00114 00115 00116 // build a kd-tree 00117 // Use userdata to attach metadata to the 00118 // values. Only the dimensions of the vectors 00119 // are used to build tree and search. 00120 // In the NN results, you get the full vector, with 00121 // the userdata in the upper elements of the vector. 00122 // Casting of float values to the appropriate type 00123 // has to be handled by user. 00124 void Build(float **ppfValues, int nLow, int nHigh, int nBucketSize, int nDimensions, int nUserDataSize); 00125 00126 // nearest neighbor only 00127 void NearestNeighbor(const float *pQuery, float &fError, float*& pfNN, int nMaximumLeavesToVisit = -1); 00128 void NearestNeighborBBF(const float *pfQuery, float &fError, float *&pfNN, int nMaximumLeavesToVisit = -1); // with best bin first strategy 00129 00130 // nearest neighbor with list of best elements 00131 void NearestNeighbor(const float *pQuery, float &fError, CKdPriorityQueue *&pNNList, int nMaximumLeavesToVisit = -1); 00132 void NearestNeighborBBF(const float *pQuery, float &fError, CKdPriorityQueue *&pNNList, int nMaximumLeavesToVisit = -1); // with best bin first strategy 00133 00134 00135 private: 00136 // private methods 00137 void Dispose(); 00138 CKdTreeNode* BuildRecursive(float** ppfValues,int nLow, int nHigh, KdBoundingBox* pCurrentBoundingBox, int nCurrentDepth); 00139 void DisposeRecursive(CKdTreeNode *pNode); 00140 void NearestNeighborRecursive(CKdTreeNode* pNode, const float *pQuery); 00141 void NearestNeighborRecursiveBBF(CKdTreeNode* pNode, const float *pQuery, float fDistanceBB); 00142 00143 00144 // root node 00145 CKdTreeNode *m_pRoot; 00146 // maximum bucket size in tree 00147 int m_nBucketSize; 00148 // number of dimensions of vectors 00149 int m_nDimensions; 00150 // size of userdata in vectors 00151 int m_nUserDataSize; 00152 // m_nDimensions + m_nUserDataSize 00153 int m_nTotalVectorSize; 00154 // number of leaves 00155 int m_nLeaves; 00156 // bounding box of whole tree (calculated in build process) 00157 KdBoundingBox m_EnclosingBox; 00158 00159 // used for query recursion 00160 float m_fCurrentMinDistance; 00161 float *m_pNearestNeighbor; 00162 // used when a maximum number of nodes to visit is provided 00163 int m_nMaximumLeavesToVisit; 00164 // queue to enable second closest element 00165 CKdPriorityQueue *m_pNearestNeighborList; 00166 CKdPriorityQueue *m_pNearestNeighborList_; 00167 // needed for BBF 00168 CKdPriorityQueue *m_pNodeListBBF; 00169 }; 00170 00171 00172 00173 #endif // _KD_TREE_H_