File size: 6,579 Bytes
d4035c1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | /** @file kdtree.h
** @brief KD-tree
** @author Andrea Vedaldi
**/
/* AUTORIGHTS
Copyright (C) 2007-09 Andrea Vedaldi and Brian Fulkerson
This file is part of VLFeat, available in the terms of the GNU
General Public License version 2.
*/
#ifndef VL_KDTREE_H
#define VL_KDTREE_H
#include "generic.h"
#define VL_KDTREE_SPLIT_HEALP_SIZE 5
typedef struct _VlKDTreeNode VlKDTreeNode ;
typedef struct _VlKDTreeSplitDimension VlKDTreeSplitDimension ;
typedef struct _VlKDTreeDataIndexEntry VlKDTreeDataIndexEntry ;
typedef struct _VlKDForestSearchState VlKDForestSearchState ;
struct _VlKDTreeNode
{
int unsigned parent ;
int lowerChild ;
int upperChild ;
int unsigned splitDimension ;
float splitThreshold ;
float lowerBound ;
float upperBound ;
} ;
struct _VlKDTreeSplitDimension
{
int unsigned dimension ;
float mean ;
float variance ;
} ;
struct _VlKDTreeDataIndexEntry
{
int index ;
float value ;
} ;
/** @brief Thresholding method */
typedef enum _VlKDTreeThresholdingMethod
{
VL_KDTREE_MEDIAN,
VL_KDTREE_MEAN
} VlKDTreeThresholdingMethod ;
/** @brief Neighbor of a query point */
typedef struct _VlKDForestNeighbor {
float distance ; /**< distance to the query point */
unsigned int index ; /**< index of the neighbor in the KDTree data */
} VlKDForestNeighbor ;
typedef struct _VlKDTree
{
VlKDTreeNode * nodes ;
int unsigned numUsedNodes ;
int unsigned numAllocatedNodes ;
VlKDTreeDataIndexEntry * dataIndex ;
int depth ;
} VlKDTree ;
struct _VlKDForestSearchState
{
VlKDTree * tree ;
unsigned int nodeIndex ;
float distanceLowerBound ;
} ;
/** @brief KDForest object */
typedef struct _VlKDForest
{
int numDimensions ;
/* indexed data */
float const * data ;
int numData ;
/* tree structure */
VlKDTree ** trees ;
unsigned int numTrees ;
/* build */
VlKDTreeThresholdingMethod thresholdingMethod ;
VlKDTreeSplitDimension splitHeapArray [VL_KDTREE_SPLIT_HEALP_SIZE] ;
int unsigned splitHeapNumNodes ;
int unsigned splitHeapSize ;
/* querying */
VlKDForestSearchState * searchHeapArray ;
int unsigned searchHeapNumNodes ;
int unsigned searchId ;
int unsigned * searchIdBook ;
int unsigned searchMaxNumComparisons ;
int unsigned searchNumComparisons;
int unsigned searchNumRecursions ;
int unsigned searchNumSimplifications ;
} VlKDForest ;
/** @name Creatind and disposing
** @{ */
VL_EXPORT VlKDForest * vl_kdforest_new (int unsigned numDimensions, int unsigned numTrees) ;
VL_EXPORT void vl_kdforest_delete (VlKDForest * self) ;
/** @} */
/** @name Building and querying
** @{ */
VL_EXPORT void vl_kdforest_build (VlKDForest * self, int numData, float const * data) ;
VL_EXPORT int vl_kdforest_query (VlKDForest * self,
VlKDForestNeighbor * neighbors,
int unsigned numNeighbors,
float const * query) ;
/** @} */
/** @name Retrieving and setting parameters
** @{ */
VL_INLINE int unsigned vl_kdforest_get_depth_of_tree (VlKDForest const * self, int unsigned treeIndex) ;
VL_INLINE int unsigned vl_kdforest_get_num_nodes_of_tree (VlKDForest const * self, int unsigned treeIndex) ;
VL_INLINE int unsigned vl_kdforest_get_num_trees (VlKDForest const * self) ;
VL_INLINE void vl_kdforest_set_max_num_comparisons (VlKDForest * self, int unsigned n) ;
VL_INLINE int unsigned vl_kdforest_get_max_num_comparisons (VlKDForest * self) ;
VL_INLINE void vl_kdforest_set_thresholding_method (VlKDForest * self, VlKDTreeThresholdingMethod method) ;
VL_INLINE VlKDTreeThresholdingMethod vl_kdforest_get_thresholding_method (VlKDForest const * self) ;
/** @} */
/** ------------------------------------------------------------------
** @brief Get the number of nodes of a given tree
** @param self KDForest object.
** @param treeIndex index of the tree.
** @return number of trees.
**/
int unsigned
vl_kdforest_get_num_nodes_of_tree (VlKDForest const * self, int unsigned treeIndex)
{
assert (treeIndex < self->numTrees) ;
return self->trees[treeIndex]->numUsedNodes ;
}
/** ------------------------------------------------------------------
** @brief Get the detph of a given tree
** @param self KDForest object.
** @param treeIndex index of the tree.
** @return number of trees.
**/
int unsigned
vl_kdforest_get_depth_of_tree (VlKDForest const * self, int unsigned treeIndex)
{
assert (treeIndex < self->numTrees) ;
return self->trees[treeIndex]->depth ;
}
/** ------------------------------------------------------------------
** @brief Get the number of trees in the forest
**
** @param self KDForest object.
** @return number of trees.
**/
int unsigned
vl_kdforest_get_num_trees (VlKDForest const * self)
{
return self->numTrees ;
}
/** ------------------------------------------------------------------
** @brief Set the maximum number of comparisons for a search
**
** @param self KDForest object.
** @param n maximum number of leaves.
**
** This function sets the maximum number of comparisons for a
** nearest neighbor search. Setting it to 0 means unbounded comparisons.
**
** @sa ::vl_kdforest_query, ::vl_kdforest_get_max_num_comparisons.
**/
void
vl_kdforest_set_max_num_comparisons (VlKDForest * self, int unsigned n)
{
self->searchMaxNumComparisons = n ;
}
/** ------------------------------------------------------------------
** @brief Get the maximum number of comparisons for a search
**
** @param self KDForest object.
** @return maximum number of leaves.
**
** @sa ::vl_kdforest_set_max_num_comparisons.
**/
int unsigned
vl_kdforest_get_max_num_comparisons (VlKDForest * self)
{
return self->searchMaxNumComparisons ;
}
/** ------------------------------------------------------------------
** @brief Set the thresholding method
** @param self KDForest object.
** @param method one of ::VlKDTreeThresholdingMethod.
**
** @sa ::vl_kdforest_get_thresholding_method
**/
VL_INLINE void
vl_kdforest_set_thresholding_method (VlKDForest * self, VlKDTreeThresholdingMethod method)
{
assert(VL_KDTREE_MEDIAN <= method && method <= VL_KDTREE_MEAN) ;
self->thresholdingMethod = method ;
}
/** ------------------------------------------------------------------
** @brief Get the thresholding method
**
** @param self KDForest object.
** @return thresholding method.
**
** @sa ::vl_kdforest_set_thresholding_method
**/
VL_INLINE VlKDTreeThresholdingMethod
vl_kdforest_get_thresholding_method (VlKDForest const * self)
{
return self->thresholdingMethod ;
}
/* VL_KDTREE_H */
#endif
|