| 1 |
/** @file kdtree.h |
| 2 |
** @brief KD-tree (@ref kdtree) |
| 3 |
** @author Andrea Vedaldi, David Novotny |
| 4 |
**/ |
| 5 |
|
| 6 |
/* |
| 7 |
Copyright (C) 2007-12 Andrea Vedaldi and Brian Fulkerson. |
| 8 |
All rights reserved. |
| 9 |
|
| 10 |
This file is part of the VLFeat library and is made available under |
| 11 |
the terms of the BSD license (see the COPYING file). |
| 12 |
*/ |
| 13 |
|
| 14 |
#ifndef VL_KDTREE_H |
| 15 |
#define VL_KDTREE_H |
| 16 |
|
| 17 |
#include "generic.h" |
| 18 |
#include "mathop.h" |
| 19 |
|
| 20 |
#define VL_KDTREE_SPLIT_HEAP_SIZE 5 |
| 21 |
#define VL_KDTREE_VARIANCE_EST_NUM_SAMPLES 1024 |
| 22 |
|
| 23 |
typedef struct _VlKDTreeNode VlKDTreeNode ; |
| 24 |
typedef struct _VlKDTreeSplitDimension VlKDTreeSplitDimension ; |
| 25 |
typedef struct _VlKDTreeDataIndexEntry VlKDTreeDataIndexEntry ; |
| 26 |
typedef struct _VlKDForestSearchState VlKDForestSearchState ; |
| 27 |
|
| 28 |
struct _VlKDTreeNode |
| 29 |
{ |
| 30 |
vl_uindex parent ; |
| 31 |
vl_index lowerChild ; |
| 32 |
vl_index upperChild ; |
| 33 |
unsigned int splitDimension ; |
| 34 |
double splitThreshold ; |
| 35 |
double lowerBound ; |
| 36 |
double upperBound ; |
| 37 |
} ; |
| 38 |
|
| 39 |
struct _VlKDTreeSplitDimension |
| 40 |
{ |
| 41 |
unsigned int dimension ; |
| 42 |
double mean ; |
| 43 |
double variance ; |
| 44 |
} ; |
| 45 |
|
| 46 |
struct _VlKDTreeDataIndexEntry |
| 47 |
{ |
| 48 |
vl_index index ; |
| 49 |
double value ; |
| 50 |
} ; |
| 51 |
|
| 52 |
/** @brief Thresholding method */ |
| 53 |
typedef enum _VlKDTreeThresholdingMethod |
| 54 |
{ |
| 55 |
VL_KDTREE_MEDIAN, |
| 56 |
VL_KDTREE_MEAN |
| 57 |
} VlKDTreeThresholdingMethod ; |
| 58 |
|
| 59 |
/** @brief Neighbor of a query point */ |
| 60 |
typedef struct _VlKDForestNeighbor { |
| 61 |
double distance ; /**< distance to the query point */ |
| 62 |
vl_uindex index ; /**< index of the neighbor in the KDTree data */ |
| 63 |
} VlKDForestNeighbor ; |
| 64 |
|
| 65 |
typedef struct _VlKDTree |
| 66 |
{ |
| 67 |
VlKDTreeNode * nodes ; |
| 68 |
vl_size numUsedNodes ; |
| 69 |
vl_size numAllocatedNodes ; |
| 70 |
VlKDTreeDataIndexEntry * dataIndex ; |
| 71 |
unsigned int depth ; |
| 72 |
} VlKDTree ; |
| 73 |
|
| 74 |
struct _VlKDForestSearchState |
| 75 |
{ |
| 76 |
VlKDTree * tree ; |
| 77 |
vl_uindex nodeIndex ; |
| 78 |
double distanceLowerBound ; |
| 79 |
} ; |
| 80 |
|
| 81 |
struct _VlKDForestSearcher; |
| 82 |
|
| 83 |
/** @brief KDForest object */ |
| 84 |
typedef struct _VlKDForest |
| 85 |
{ |
| 86 |
vl_size dimension ; |
| 87 |
|
| 88 |
/* random number generator */ |
| 89 |
VlRand * rand ; |
| 90 |
|
| 91 |
/* indexed data */ |
| 92 |
vl_type dataType ; |
| 93 |
void const * data ; |
| 94 |
vl_size numData ; |
| 95 |
VlVectorComparisonType distance; |
| 96 |
void (*distanceFunction)(void) ; |
| 97 |
|
| 98 |
/* tree structure */ |
| 99 |
VlKDTree ** trees ; |
| 100 |
vl_size numTrees ; |
| 101 |
|
| 102 |
/* build */ |
| 103 |
VlKDTreeThresholdingMethod thresholdingMethod ; |
| 104 |
VlKDTreeSplitDimension splitHeapArray [VL_KDTREE_SPLIT_HEAP_SIZE] ; |
| 105 |
vl_size splitHeapNumNodes ; |
| 106 |
vl_size splitHeapSize ; |
| 107 |
vl_size maxNumNodes; |
| 108 |
|
| 109 |
/* query */ |
| 110 |
vl_size searchMaxNumComparisons ; |
| 111 |
vl_size numSearchers; |
| 112 |
struct _VlKDForestSearcher * headSearcher ; /* head of the double linked list with searchers */ |
| 113 |
|
| 114 |
} VlKDForest ; |
| 115 |
|
| 116 |
/** @brief ::VlKDForest searcher object */ |
| 117 |
typedef struct _VlKDForestSearcher |
| 118 |
{ |
| 119 |
/* maintain a linked list of searchers for later disposal*/ |
| 120 |
struct _VlKDForestSearcher * next; |
| 121 |
struct _VlKDForestSearcher * previous; |
| 122 |
|
| 123 |
vl_uindex * searchIdBook ; |
| 124 |
VlKDForestSearchState * searchHeapArray ; |
| 125 |
VlKDForest * forest; |
| 126 |
|
| 127 |
vl_size searchNumComparisons; |
| 128 |
vl_size searchNumRecursions ; |
| 129 |
vl_size searchNumSimplifications ; |
| 130 |
|
| 131 |
vl_size searchHeapNumNodes ; |
| 132 |
vl_uindex searchId ; |
| 133 |
} VlKDForestSearcher ; |
| 134 |
|
| 135 |
/** @name Creating, copying and disposing |
| 136 |
** @{ */ |
| 137 |
VL_EXPORT VlKDForest * vl_kdforest_new (vl_type dataType, |
| 138 |
vl_size dimension, vl_size numTrees, VlVectorComparisonType normType) ; |
| 139 |
VL_EXPORT VlKDForestSearcher * vl_kdforest_new_searcher (VlKDForest * kdforest); |
| 140 |
VL_EXPORT void vl_kdforest_delete (VlKDForest * self) ; |
| 141 |
VL_EXPORT void vl_kdforestsearcher_delete (VlKDForestSearcher * searcher) ; |
| 142 |
/** @} */ |
| 143 |
|
| 144 |
/** @name Building and querying |
| 145 |
** @{ */ |
| 146 |
VL_EXPORT void vl_kdforest_build (VlKDForest * self, |
| 147 |
vl_size numData, |
| 148 |
void const * data) ; |
| 149 |
|
| 150 |
VL_EXPORT vl_size vl_kdforest_query (VlKDForest * self, |
| 151 |
VlKDForestNeighbor * neighbors, |
| 152 |
vl_size numNeighbors, |
| 153 |
void const * query) ; |
| 154 |
|
| 155 |
VL_EXPORT vl_size vl_kdforest_query_with_array (VlKDForest * self, |
| 156 |
vl_uint32 * index, |
| 157 |
vl_size numNeighbors, |
| 158 |
vl_size numQueries, |
| 159 |
void * distance, |
| 160 |
void const * queries) ; |
| 161 |
|
| 162 |
VL_EXPORT vl_size vl_kdforestsearcher_query (VlKDForestSearcher * self, |
| 163 |
VlKDForestNeighbor * neighbors, |
| 164 |
vl_size numNeighbors, |
| 165 |
void const * query) ; |
| 166 |
/** @} */ |
| 167 |
|
| 168 |
/** @name Retrieving and setting parameters |
| 169 |
** @{ */ |
| 170 |
VL_EXPORT vl_size vl_kdforest_get_depth_of_tree (VlKDForest const * self, vl_uindex treeIndex) ; |
| 171 |
VL_EXPORT vl_size vl_kdforest_get_num_nodes_of_tree (VlKDForest const * self, vl_uindex treeIndex) ; |
| 172 |
VL_EXPORT vl_size vl_kdforest_get_num_trees (VlKDForest const * self) ; |
| 173 |
VL_EXPORT vl_size vl_kdforest_get_data_dimension (VlKDForest const * self) ; |
| 174 |
VL_EXPORT vl_type vl_kdforest_get_data_type (VlKDForest const * self) ; |
| 175 |
VL_EXPORT void vl_kdforest_set_max_num_comparisons (VlKDForest * self, vl_size n) ; |
| 176 |
VL_EXPORT vl_size vl_kdforest_get_max_num_comparisons (VlKDForest * self) ; |
| 177 |
VL_EXPORT void vl_kdforest_set_thresholding_method (VlKDForest * self, VlKDTreeThresholdingMethod method) ; |
| 178 |
VL_EXPORT VlKDTreeThresholdingMethod vl_kdforest_get_thresholding_method (VlKDForest const * self) ; |
| 179 |
VL_EXPORT VlKDForest * vl_kdforest_searcher_get_forest (VlKDForestSearcher const * self) ; |
| 180 |
VL_EXPORT VlKDForestSearcher * vl_kdforest_get_searcher (VlKDForest const * self, vl_uindex pos) ; |
| 181 |
/** @} */ |
| 182 |
|
| 183 |
|
| 184 |
/* VL_KDTREE_H */ |
| 185 |
#endif |