| | |
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | #include "kdtree.h" |
| | #include "generic.h" |
| | #include "random.h" |
| | #include "mathop.h" |
| |
|
| | #include <stdlib.h> |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | #define NAME vl_kdforest_search_heap |
| | #define T VlKDForestSearchState |
| | #define CMP(x,y) (x->distanceLowerBound - y->distanceLowerBound) |
| | #include "heap-t.h" |
| | #undef CMP |
| | #undef T |
| | #undef NAME |
| |
|
| | #define NAME vl_kdtree_split_heap |
| | #define T VlKDTreeSplitDimension |
| | #define CMP(x,y) (x->variance - y->variance) |
| | #include "heap-t.h" |
| | #undef CMP |
| | #undef T |
| | #undef NAME |
| |
|
| | #define NAME vl_kdforest_neighbor_heap |
| | #define T VlKDForestNeighbor |
| | #define CMP(x,y) (y->distance - x->distance) |
| | #include "heap-t.h" |
| | #undef CMP |
| | #undef T |
| | #undef NAME |
| |
|
| | |
| | |
| | |
| | |
| | |
| |
|
| | int unsigned |
| | vl_kdtree_node_new (VlKDTree * tree, int unsigned parentIndex) |
| | { |
| | VlKDTreeNode * node = NULL ; |
| | int unsigned nodeIndex = tree->numUsedNodes ; |
| | tree -> numUsedNodes += 1 ; |
| |
|
| | assert (tree->numUsedNodes <= tree->numAllocatedNodes) ; |
| |
|
| | node = tree->nodes + nodeIndex ; |
| | node -> parent = parentIndex ; |
| | node -> lowerChild = 0 ; |
| | node -> upperChild = 0 ; |
| | node -> splitDimension = 0 ; |
| | node -> splitThreshold = 0 ; |
| | return nodeIndex ; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| |
|
| | static int |
| | vl_kdtree_compare_index_entries (void const * a, |
| | void const * b) |
| | { |
| | float delta = |
| | ((VlKDTreeDataIndexEntry const*)a) -> value - |
| | ((VlKDTreeDataIndexEntry const*)b) -> value ; |
| | if (delta < 0) return -1 ; |
| | if (delta > 0) return +1 ; |
| | return 0 ; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | static void |
| | vl_kdtree_build_recursively |
| | (VlKDForest * forest, |
| | VlKDTree * tree, unsigned int nodeIndex, |
| | unsigned int dataBegin, unsigned int dataEnd, |
| | unsigned int depth) |
| | { |
| | int d, i, medianIndex, splitIndex ; |
| | VlKDTreeNode * node = tree->nodes + nodeIndex ; |
| | VlKDTreeSplitDimension * splitDimension ; |
| |
|
| | |
| | if (dataEnd - dataBegin <= 1) { |
| | if (tree->depth < depth) tree->depth = depth ; |
| | node->lowerChild = - dataBegin - 1; |
| | node->upperChild = - dataEnd - 1 ; |
| | return ; |
| | } |
| |
|
| | |
| | forest->splitHeapNumNodes = 0 ; |
| | for (d = 0 ; d < forest->numDimensions ; ++ d) { |
| | float mean = 0 ; |
| | float secondMoment = 0 ; |
| | float variance = 0 ; |
| | for (i = dataBegin ; i < dataEnd ; ++ i) { |
| | int di = tree -> dataIndex [i] .index ; |
| | float const datum = forest->data [di * forest->numDimensions + d] ; |
| | mean += datum ; |
| | secondMoment += datum * datum ; |
| | } |
| | mean /= (dataEnd - dataBegin) ; |
| | secondMoment /= (dataEnd - dataBegin) ; |
| | variance = secondMoment - mean * mean ; |
| |
|
| | |
| | if (forest->splitHeapNumNodes < forest->splitHeapSize) { |
| | VlKDTreeSplitDimension * splitDimension |
| | = forest->splitHeapArray + forest->splitHeapNumNodes ; |
| | splitDimension->dimension = d ; |
| | splitDimension->mean = mean ; |
| | splitDimension->variance = variance ; |
| | vl_kdtree_split_heap_push (forest->splitHeapArray, &forest->splitHeapNumNodes) ; |
| | } else { |
| | VlKDTreeSplitDimension * splitDimension = forest->splitHeapArray + 0 ; |
| | if (splitDimension->variance < variance) { |
| | splitDimension->dimension = d ; |
| | splitDimension->mean = mean ; |
| | splitDimension->variance = variance ; |
| | vl_kdtree_split_heap_update (forest->splitHeapArray, forest->splitHeapNumNodes, 0) ; |
| | } |
| | } |
| | } |
| |
|
| | |
| | splitDimension = forest->splitHeapArray |
| | + (vl_rand_uint32() % VL_MIN(forest->splitHeapSize, forest->splitHeapNumNodes)) ; |
| |
|
| | |
| | if (splitDimension->variance == 0) { |
| | node->lowerChild = - dataBegin - 1 ; |
| | node->upperChild = - dataEnd - 1 ; |
| | return ; |
| | } |
| | node->splitDimension = splitDimension->dimension ; |
| |
|
| | |
| | for (i = dataBegin ; i < dataEnd ; ++ i) { |
| | int di = tree->dataIndex [i] .index ; |
| | float datum = forest->data [di * forest->numDimensions + splitDimension->dimension] ; |
| | tree->dataIndex [i] .value = datum ; |
| | } |
| | qsort (tree->dataIndex + dataBegin, |
| | dataEnd - dataBegin, |
| | sizeof (VlKDTreeDataIndexEntry), |
| | vl_kdtree_compare_index_entries) ; |
| |
|
| | |
| | switch (forest->thresholdingMethod) { |
| | case VL_KDTREE_MEAN : |
| | node->splitThreshold = splitDimension->mean ; |
| | for (splitIndex = dataBegin ; |
| | splitIndex < dataEnd && tree->dataIndex[splitIndex].value <= node->splitThreshold ; |
| | ++ splitIndex) ; |
| | splitIndex -= 1 ; |
| | |
| | |
| | |
| | |
| | |
| | |
| | if (dataBegin <= splitIndex && splitIndex + 1 < dataEnd) break ; |
| |
|
| | case VL_KDTREE_MEDIAN : |
| | medianIndex = (dataBegin + dataEnd - 1) / 2 ; |
| | splitIndex = medianIndex ; |
| | node -> splitThreshold = tree->dataIndex[medianIndex].value ; |
| | break ; |
| |
|
| | default: |
| | assert(0) ; |
| | } |
| |
|
| | |
| | node->lowerChild = vl_kdtree_node_new (tree, nodeIndex) ; |
| | vl_kdtree_build_recursively (forest, tree, node->lowerChild, dataBegin, splitIndex + 1, depth + 1) ; |
| |
|
| | node->upperChild = vl_kdtree_node_new (tree, nodeIndex) ; |
| | vl_kdtree_build_recursively (forest, tree, node->upperChild, splitIndex + 1, dataEnd, depth + 1) ; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | VL_EXPORT |
| | VlKDForest * |
| | vl_kdforest_new (int unsigned numDimensions, int unsigned numTrees) |
| | { |
| | VlKDForest * self = vl_malloc (sizeof(VlKDForest)) ; |
| |
|
| | self -> numData = 0 ; |
| | self -> data = 0 ; |
| | self -> numDimensions = numDimensions ; |
| | self -> numTrees = numTrees ; |
| | self -> trees = 0 ; |
| | self -> thresholdingMethod = VL_KDTREE_MEDIAN ; |
| | self -> splitHeapSize = (numTrees == 1) ? 1 : VL_KDTREE_SPLIT_HEALP_SIZE ; |
| | self -> splitHeapNumNodes = 0 ; |
| |
|
| | self -> searchHeapArray = 0 ; |
| | self -> searchHeapNumNodes = 0 ; |
| |
|
| |
|
| | self -> searchMaxNumComparisons = 0 ; |
| | self -> searchIdBook = 0 ; |
| | self -> searchId = 0 ; |
| |
|
| | return self ; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| |
|
| | VL_EXPORT void |
| | vl_kdforest_delete (VlKDForest * self) |
| | { |
| | int ti ; |
| | if (self->searchIdBook) vl_free (self->searchIdBook) ; |
| | if (self->trees) { |
| | for (ti = 0 ; ti < self->numTrees ; ++ ti) { |
| | if (self->trees[ti]) { |
| | if (self->trees[ti]->nodes) vl_free (self->trees[ti]->nodes) ; |
| | if (self->trees[ti]->dataIndex) vl_free (self->trees[ti]->dataIndex) ; |
| | } |
| | } |
| | vl_free (self->trees) ; |
| | } |
| | vl_free (self) ; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | VL_EXPORT void |
| | vl_kdforest_build (VlKDForest * self, int numData, float const * data) |
| | { |
| | int di, ti ; |
| |
|
| | |
| | self->data = data ; |
| | self->numData = numData ; |
| | self->trees = vl_malloc (sizeof(VlKDTree*) * self->numTrees) ; |
| |
|
| | for (ti = 0 ; ti < self->numTrees ; ++ ti) { |
| | self->trees[ti] = vl_malloc (sizeof(VlKDTree)) ; |
| | self->trees[ti]->dataIndex = vl_malloc (sizeof(VlKDTreeDataIndexEntry) * self->numData) ; |
| | for (di = 0 ; di < self->numData ; ++ di) { |
| | self->trees[ti]->dataIndex[di].index = di ; |
| | } |
| | self->trees[ti]->numUsedNodes = 0 ; |
| | |
| | self->trees[ti]->numAllocatedNodes = 2 * self->numData - 1 ; |
| | self->trees[ti]->nodes = vl_malloc (sizeof(VlKDTreeNode) * self->trees[ti]->numAllocatedNodes) ; |
| | self->trees[ti]->depth = 0 ; |
| | vl_kdtree_build_recursively (self, self->trees[ti], |
| | vl_kdtree_node_new(self->trees[ti], 0), 0, |
| | self->numData, 0) ; |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| |
|
| | VL_INLINE |
| | float calc_dist2 (float const * a, float const * b, int unsigned N) |
| | { |
| | float acc = 0 ; |
| | float d0, d1, d2, d3 ; |
| | float const * age = a + N - 3 ; |
| | float const * ae = a + N ; |
| |
|
| | while (a < age) { |
| | d0 = *a++ - *b++ ; |
| | d1 = *a++ - *b++ ; |
| | d2 = *a++ - *b++ ; |
| | d3 = *a++ - *b++ ; |
| | acc += d0 * d0 ; |
| | acc += d1 * d1 ; |
| | acc += d2 * d2 ; |
| | acc += d3 * d3 ; |
| | } |
| | while (a < ae) { |
| | d0 = *a++ - *b++ ; |
| | acc += d0 * d0 ; |
| | } |
| | return acc ; |
| | } |
| |
|
| | |
| | |
| | |
| |
|
| | VL_EXPORT int |
| | vl_kdforest_query_recursively (VlKDForest * self, |
| | VlKDTree * tree, |
| | int unsigned nodeIndex, |
| | VlKDForestNeighbor * neighbors, |
| | int unsigned numNeighbors, |
| | int unsigned * numAddedNeighbors, |
| | float dist, |
| | float const * query) |
| | { |
| | VlKDTreeNode const * node = tree->nodes + nodeIndex ; |
| | int unsigned i = node->splitDimension ; |
| | int nextChild, saveChild ; |
| | float delta, saveDist ; |
| | float x = query [i] ; |
| | float x1 = node->lowerBound ; |
| | float x2 = node->splitThreshold ; |
| | float x3 = node->upperBound ; |
| | VlKDForestSearchState * searchState ; |
| |
|
| | self->searchNumRecursions ++ ; |
| |
|
| | |
| | if (node->lowerChild < 0) { |
| | int begin = - node->lowerChild - 1 ; |
| | int end = - node->upperChild - 1 ; |
| | int iter ; |
| |
|
| | for (iter = begin ; |
| | iter < end && |
| | (self->searchMaxNumComparisons == 0 || |
| | self->searchNumComparisons < self->searchMaxNumComparisons) ; |
| | ++ iter) { |
| |
|
| | int unsigned di = tree->dataIndex [iter].index ; |
| | float const * datum = self->data + di * self->numDimensions ; |
| |
|
| | |
| | |
| | if (self->searchIdBook [di] == self->searchId) continue ; |
| | self->searchIdBook [di] = self->searchId ; |
| |
|
| | |
| | dist = calc_dist2 (query, datum, self->numDimensions) ; |
| | self->searchNumComparisons += 1 ; |
| |
|
| | |
| | if (*numAddedNeighbors < numNeighbors) { |
| | VlKDForestNeighbor * newNeighbor = neighbors + *numAddedNeighbors ; |
| | newNeighbor->index = di ; |
| | newNeighbor->distance = dist ; |
| | vl_kdforest_neighbor_heap_push (neighbors, numAddedNeighbors) ; |
| | } else { |
| | VlKDForestNeighbor * largestNeighbor = neighbors + 0 ; |
| | if (largestNeighbor->distance > dist) { |
| | largestNeighbor->index = di ; |
| | largestNeighbor->distance = dist ; |
| | vl_kdforest_neighbor_heap_update (neighbors, *numAddedNeighbors, 0) ; |
| | } |
| | } |
| | } |
| |
|
| | return nodeIndex ; |
| | } |
| |
|
| | #if 0 |
| | assert (x1 <= x2 && x2 <= x3) ; |
| | assert (node->lowerChild >= 0) ; |
| | assert (node->upperChild >= 0) ; |
| | #endif |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | delta = x - x2 ; |
| | saveDist = dist + delta*delta ; |
| |
|
| | if (x <= x2) { |
| | nextChild = node->lowerChild ; |
| | saveChild = node->upperChild ; |
| | if (x <= x1) { |
| | delta = x - x1 ; |
| | saveDist -= delta*delta ; |
| | } |
| | } else { |
| | nextChild = node->upperChild ; |
| | saveChild = node->lowerChild ; |
| | if (x > x3) { |
| | delta = x - x3 ; |
| | saveDist -= delta*delta ; |
| | } |
| | } |
| |
|
| | if (*numAddedNeighbors < numNeighbors || neighbors[0].distance > saveDist) { |
| | searchState = self->searchHeapArray + self->searchHeapNumNodes ; |
| | searchState->tree = tree ; |
| | searchState->nodeIndex = saveChild ; |
| | searchState->distanceLowerBound = saveDist ; |
| | vl_kdforest_search_heap_push (self->searchHeapArray, |
| | &self->searchHeapNumNodes) ; |
| | } |
| |
|
| | return vl_kdforest_query_recursively (self, |
| | tree, |
| | nextChild, |
| | neighbors, |
| | numNeighbors, |
| | numAddedNeighbors, |
| | dist, |
| | query) ; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | static void |
| | vl_kdtree_calc_bounds_recursively (VlKDTree * tree, |
| | int unsigned nodeIndex, float * searchBounds) |
| | { |
| | VlKDTreeNode * node = tree->nodes + nodeIndex ; |
| | int unsigned i = node->splitDimension ; |
| | float t = node->splitThreshold ; |
| |
|
| | node->lowerBound = searchBounds [2 * i + 0] ; |
| | node->upperBound = searchBounds [2 * i + 1] ; |
| |
|
| | if (node->lowerChild > 0) { |
| | searchBounds [2 * i + 1] = t ; |
| | vl_kdtree_calc_bounds_recursively (tree, node->lowerChild, searchBounds) ; |
| | searchBounds [2 * i + 1] = node->upperBound ; |
| | } |
| | if (node->upperChild > 0) { |
| | searchBounds [2 * i + 0] = t ; |
| | vl_kdtree_calc_bounds_recursively (tree, node->upperChild, searchBounds) ; |
| | searchBounds [2 * i + 0] = node->lowerBound ; |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | VL_EXPORT int |
| | vl_kdforest_query (VlKDForest * self, |
| | VlKDForestNeighbor * neighbors, |
| | int unsigned numNeighbors, |
| | float const * query) |
| | { |
| | int i, ti ; |
| | vl_bool exactSearch = (self->searchMaxNumComparisons == 0) ; |
| | VlKDForestSearchState * searchState ; |
| | int unsigned numAddedNeighbors = 0 ; |
| |
|
| | assert (neighbors) ; |
| | assert (numNeighbors > 0) ; |
| | assert (query) ; |
| |
|
| | |
| | self -> searchId += 1 ; |
| | self -> searchNumRecursions = 0 ; |
| |
|
| | if (! self -> searchHeapArray) { |
| | |
| | |
| | int maxNumNodes = 0 ; |
| | for (ti = 0 ; ti < self->numTrees ; ++ti) { |
| | maxNumNodes += self->trees[ti]->numUsedNodes ; |
| | } |
| | self -> searchHeapArray = vl_malloc (sizeof(VlKDForestSearchState) * maxNumNodes) ; |
| | self -> searchIdBook = vl_calloc (sizeof(unsigned int), self->numData) ; |
| |
|
| | for (ti = 0 ; ti < self->numTrees ; ++ti) { |
| | float * searchBounds = vl_malloc(sizeof(float) * 2 * self->numDimensions) ; |
| | float * iter = searchBounds ; |
| | float * end = iter + 2 * self->numDimensions ; |
| | while (iter < end) { |
| | *iter++ = - VL_INFINITY_F ; |
| | *iter++ = + VL_INFINITY_F ; |
| | } |
| | vl_kdtree_calc_bounds_recursively (self->trees[ti], 0, searchBounds) ; |
| | vl_free (searchBounds) ; |
| | } |
| | } |
| |
|
| | self->searchNumComparisons = 0 ; |
| | self->searchNumSimplifications = 0 ; |
| |
|
| | |
| | self->searchHeapNumNodes = 0 ; |
| | for (ti = 0 ; ti < self->numTrees ; ++ ti) { |
| | searchState = self->searchHeapArray + self->searchHeapNumNodes ; |
| | searchState -> tree = self->trees[ti] ; |
| | searchState -> nodeIndex = 0 ; |
| | searchState -> distanceLowerBound = 0 ; |
| | vl_kdforest_search_heap_push (self->searchHeapArray, &self->searchHeapNumNodes) ; |
| | } |
| |
|
| | |
| | while (exactSearch || self->searchNumComparisons < self->searchMaxNumComparisons) |
| | { |
| | |
| | VlKDForestSearchState * searchState = vl_kdforest_search_heap_pop |
| | (self->searchHeapArray, &self->searchHeapNumNodes) ; |
| |
|
| | |
| | if (searchState == NULL) { |
| | break ; |
| | } |
| |
|
| | |
| | if (numAddedNeighbors == numNeighbors && |
| | neighbors[0].distance < searchState->distanceLowerBound) { |
| | self->searchNumSimplifications ++ ; |
| | break ; |
| | } |
| |
|
| | vl_kdforest_query_recursively (self, |
| | searchState->tree, |
| | searchState->nodeIndex, |
| | neighbors, |
| | numNeighbors, |
| | &numAddedNeighbors, |
| | searchState->distanceLowerBound, |
| | query) ; |
| | } |
| |
|
| | |
| | for (i = numAddedNeighbors ; i < numNeighbors ; ++ i) { |
| | neighbors[i].index = -1 ; |
| | neighbors[i].distance = VL_NAN_F ; |
| | } |
| | while (numAddedNeighbors) { |
| | vl_kdforest_neighbor_heap_pop (neighbors, &numAddedNeighbors) ; |
| | } |
| |
|
| | return self->searchNumComparisons ; |
| | } |
| |
|