kdtree.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*!
  2. * \file kdtree.h
  3. *
  4. * \brief Dynamic balanced k-d tree implementation
  5. *
  6. * k-d tree is a multidimensional (k-dimensional) binary search tree for
  7. * nearest neighbor search and is part of \ref btree2.
  8. *
  9. * Copyright and license:
  10. *
  11. * (C) 2014 by the GRASS Development Team
  12. *
  13. * This program is free software under the GNU General Public License
  14. * (>=v2). Read the file COPYING that comes with GRASS for details.
  15. *
  16. * \author Markus Metz
  17. *
  18. * \par References
  19. * Bentley, J. L. (1975). "Multidimensional binary search trees used for
  20. * associative searching". Communications of the ACM 18 (9): 509.
  21. * doi:10.1145/361002.361007
  22. *
  23. * \par Features
  24. * - This k-d tree is a dynamic tree:
  25. * elements can be inserted and removed any time.
  26. * - This k-d tree is balanced:
  27. * subtrees have a similar depth (the difference in subtrees' depths is
  28. * not allowed to be larger than the balancing tolerance).
  29. *
  30. * Here is a structure of basic usage:
  31. *
  32. * Create a new k-d tree:
  33. *
  34. * kdtree_create(...);
  35. *
  36. * Insert points into the tree:
  37. *
  38. * kdtree_insert(...);
  39. *
  40. * Optionally optimize the tree:
  41. *
  42. * kdtree_optimize(...);
  43. *
  44. * Search k nearest neighbors:
  45. *
  46. * kdtree_knn(...);
  47. *
  48. * Search all neighbors in radius:
  49. *
  50. * kdtree_dnn(...);
  51. *
  52. * Destroy the tree:
  53. *
  54. * kdtree_destroy(...);
  55. *
  56. * \todo
  57. * Doxygen ignores comment for last parameter after `);`.
  58. * The parameter description now goes to the end of function description.
  59. *
  60. * \todo
  61. * Include formatting to function descriptions.
  62. */
  63. /*!
  64. * \brief Node for k-d tree
  65. */
  66. struct kdnode
  67. {
  68. unsigned char dim; /*!< split dimension of this node */
  69. unsigned char depth; /*!< depth at this node */
  70. unsigned char balance; /*!< flag to indicate if balancing is needed */
  71. double *c; /*!< coordinates */
  72. int uid; /*!< unique id of this node */
  73. struct kdnode *child[2]; /*!< link to children: `[0]` for smaller, `[1]` for larger */
  74. };
  75. /*!
  76. * \brief k-d tree
  77. */
  78. struct kdtree
  79. {
  80. unsigned char ndims; /*!< number of dimensions */
  81. unsigned char *nextdim; /*!< split dimension of child nodes */
  82. int csize; /*!< size of coordinates in bytes */
  83. int btol; /*!< balancing tolerance */
  84. size_t count; /*!< number of items in the tree */
  85. struct kdnode *root; /*!< tree root */
  86. };
  87. /*!
  88. * \brief k-d tree traversal
  89. */
  90. struct kdtrav
  91. {
  92. struct kdtree *tree; /*!< tree being traversed */
  93. struct kdnode *curr_node; /*!< current node */
  94. struct kdnode *up[256]; /*!< stack of parent nodes */
  95. int top; /*!< index for stack */
  96. int first; /*!< little helper flag */
  97. };
  98. /*! creae a new k-d tree */
  99. struct kdtree *kdtree_create(char ndims, /*!< number of dimensions */
  100. int *btol /*!< optional balancing tolerance */
  101. );
  102. /*! destroy a tree */
  103. void kdtree_destroy(struct kdtree *t);
  104. /*! clear a tree, removing all entries */
  105. void kdtree_clear(struct kdtree *t);
  106. /*! insert an item (coordinates c and uid) into the k-d tree */
  107. int kdtree_insert(struct kdtree *t, /*!< k-d tree */
  108. double *c, /*!< coordinates */
  109. int uid, /*!< the point's unique id */
  110. int dc /*!< allow duplicate coordinates */
  111. );
  112. /*! remove an item from the k-d tree
  113. * coordinates c and uid must match */
  114. int kdtree_remove(struct kdtree *t, /*!< k-d tree */
  115. double *c, /*!< coordinates */
  116. int uid /*!< the point's unique id */
  117. );
  118. /*! find k nearest neighbors
  119. * results are stored in uid (uids) and d (squared distances)
  120. * optionally an uid to be skipped can be given
  121. * useful when searching for the nearest neighbors of an item
  122. * that is also in the tree */
  123. int kdtree_knn(struct kdtree *t, /*!< k-d tree */
  124. double *c, /*!< coordinates */
  125. int *uid, /*!< unique ids of the neighbors */
  126. double *d, /*!< squared distances to the neighbors */
  127. int k, /*!< number of neighbors to find */
  128. int *skip /*!< unique id to skip */
  129. );
  130. /*! find all nearest neighbors within distance aka radius search
  131. * results are stored in puid (uids) and pd (squared distances)
  132. * memory is allocated as needed, the calling fn must free the memory
  133. * optionally an uid to be skipped can be given */
  134. int kdtree_dnn(struct kdtree *t, /*!< k-d tree */
  135. double *c, /*!< coordinates */
  136. int **puid, /*!< unique ids of the neighbors */
  137. double **pd, /*!< squared distances to the neighbors */
  138. double maxdist, /*!< radius to search around the given coordinates */
  139. int *skip /*!< unique id to skip */
  140. );
  141. /*! find all nearest neighbors within range aka box search
  142. * the range is specified with min and max for each dimension as
  143. * (min1, min2, ..., minn, max1, max2, ..., maxn)
  144. * results are stored in puid (uids) and pd (squared distances)
  145. * memory is allocated as needed, the calling fn must free the memory
  146. * optionally an uid to be skipped can be given */
  147. int kdtree_rnn(struct kdtree *t, /*!< k-d tree */
  148. double *c, /*!< coordinates for range */
  149. int **puid, /*!< unique ids of the neighbors */
  150. int *skip /*!< unique id to skip */
  151. );
  152. /*! k-d tree optimization, only useful if the tree will be heavily used
  153. * (more searches than items in the tree)
  154. * level 0 = a bit, 1 = more, 2 = a lot */
  155. void kdtree_optimize(struct kdtree *t, /*!< k-d tree */
  156. int level /*!< optimization level */
  157. );
  158. /*! initialize tree traversal
  159. * (re-)sets trav structure
  160. * returns 0
  161. */
  162. int kdtree_init_trav(struct kdtrav *trav, struct kdtree *tree);
  163. /*! traverse the tree
  164. * useful to get all items in the tree non-recursively
  165. * struct kdtrav *trav needs to be initialized first
  166. * returns 1, 0 when finished
  167. */
  168. int kdtree_traverse(struct kdtrav *trav, double *c, int *uid);