index.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /****************************************************************************
  2. * MODULE: R-Tree library
  3. *
  4. * AUTHOR(S): Antonin Guttman - original code
  5. * Daniel Green (green@superliminal.com) - major clean-up
  6. * and implementation of bounding spheres
  7. * Markus Metz - file-based and memory-based R*-tree
  8. *
  9. * PURPOSE: Multidimensional index
  10. *
  11. * COPYRIGHT: (C) 2010 by the GRASS Development Team
  12. *
  13. * This program is free software under the GNU General Public
  14. * License (>=v2). Read the file COPYING that comes with GRASS
  15. * for details.
  16. *****************************************************************************/
  17. #ifndef _INDEX_
  18. #define _INDEX_
  19. #include <stdio.h>
  20. #include <sys/types.h>
  21. #include <grass/config.h>
  22. /* PGSIZE is normally the natural page size of the machine */
  23. #define PGSIZE 512
  24. #define NUMDIMS 3 /* maximum number of dimensions */
  25. typedef double RectReal;
  26. /*-----------------------------------------------------------------------------
  27. | Global definitions.
  28. -----------------------------------------------------------------------------*/
  29. #ifndef TRUE
  30. #define TRUE 1
  31. #endif
  32. #ifndef FALSE
  33. #define FALSE 0
  34. #endif
  35. #define NUMSIDES 2*NUMDIMS
  36. /* max branching factor of a node */
  37. /* was (PGSIZE-(2 * sizeof(int))) / sizeof(struct Branch)
  38. * this is LFS dependent, not good
  39. * on 32 bit without LFS this is 9.69
  40. * on 32 bit with LFS and on 64 bit this is 9 */
  41. #define MAXCARD 18
  42. /* R*-tree: number of branches to be force-reinserted when adding a branch */
  43. #define FORCECARD 2
  44. /* maximum no of levels = tree depth */
  45. #define MAXLEVEL 20 /* 8^MAXLEVEL items are guaranteed to fit into the tree */
  46. #define NODETYPE(l, fd) ((l) == 0 ? 0 : ((fd) < 0 ? 1 : 2))
  47. struct Rect
  48. {
  49. RectReal boundary[NUMSIDES]; /* xmin,ymin,...,xmax,ymax,... */
  50. };
  51. struct Node; /* node for spatial index */
  52. union Child
  53. {
  54. int id; /* child id */
  55. struct Node *ptr; /* pointer to child node */
  56. off_t pos; /* position of child node in file */
  57. };
  58. struct Branch /* branch for spatial index */
  59. {
  60. struct Rect rect;
  61. union Child child;
  62. };
  63. struct Node /* node for spatial index */
  64. {
  65. int count; /* number of branches */
  66. int level; /* 0 is leaf, others positive */
  67. struct Branch branch[MAXCARD];
  68. };
  69. /*
  70. * If passed to a tree search, this callback function will be called
  71. * with the ID of each data rect that overlaps the search rect
  72. * plus whatever user specific pointer was passed to the search.
  73. * It can terminate the search early by returning 0 in which case
  74. * the search will return the number of hits found up to that point.
  75. */
  76. typedef int SearchHitCallback(int id, void *arg);
  77. struct RTree;
  78. typedef int rt_search_fn(struct RTree *, struct Rect *,
  79. SearchHitCallback *, void *);
  80. typedef int rt_insert_fn(struct Rect *, union Child, int, struct RTree *);
  81. typedef int rt_delete_fn(struct Rect *, union Child, struct RTree *);
  82. typedef int rt_valid_child_fn(union Child *);
  83. struct RTree
  84. {
  85. /* RTree setup info */
  86. int fd; /* file descriptor */
  87. unsigned char ndims; /* number of dimensions */
  88. unsigned char nsides; /* number of sides = 2 * ndims */
  89. int nodesize; /* node size in bytes */
  90. int branchsize; /* branch size in bytes */
  91. int rectsize; /* rectangle size in bytes */
  92. /* RTree info, useful to calculate space requirements */
  93. int n_nodes; /* number of nodes */
  94. int n_leafs; /* number of data items (level 0 leafs) */
  95. int rootlevel; /* root level = tree depth */
  96. /* settings for RTree building */
  97. int nodecard; /* max number of childs in node */
  98. int leafcard; /* max number of childs in leaf */
  99. int min_node_fill; /* balance criteria for node removal */
  100. int min_leaf_fill; /* balance criteria for leaf removal */
  101. int minfill_node_split; /* balance criteria for splitting */
  102. int minfill_leaf_split; /* balance criteria for splitting */
  103. /* free node positions for recycling */
  104. struct _recycle {
  105. int avail; /* number of available positions */
  106. int alloc; /* number of allcoated positions in *pos */
  107. off_t *pos; /* array of available positions */
  108. } free_nodes;
  109. /* node buffer for file-based index, three nodes per level
  110. * more than three nodes per level would require too complex cache management */
  111. struct NodeBuffer
  112. {
  113. struct Node n; /* buffered node */
  114. off_t pos; /* file position of buffered node */
  115. char dirty; /* node in buffer was modified */
  116. } nb[MAXLEVEL][3];
  117. /* usage order of buffered nodes per level
  118. * used[level][0] = most recently used
  119. * used[level][2] = least recently used */
  120. char used[MAXLEVEL][3];
  121. /* insert, delete, search */
  122. rt_insert_fn *insert_rect;
  123. rt_delete_fn *delete_rect;
  124. rt_search_fn *search_rect;
  125. rt_valid_child_fn *valid_child;
  126. struct Node *root; /* pointer to root node */
  127. off_t rootpos; /* root node position in file */
  128. };
  129. struct ListNode
  130. {
  131. struct ListNode *next;
  132. struct Node *node;
  133. };
  134. struct ListFNode
  135. {
  136. struct ListFNode *next;
  137. off_t node_pos;
  138. };
  139. struct ListBranch
  140. {
  141. struct ListBranch *next;
  142. struct Branch b;
  143. int level;
  144. };
  145. /* index.c */
  146. extern int RTreeSearch(struct RTree *, struct Rect *, SearchHitCallback *,
  147. void *);
  148. extern int RTreeInsertRect(struct Rect *, int, struct RTree *);
  149. extern int RTreeDeleteRect(struct Rect *, int, struct RTree *);
  150. extern struct RTree *RTreeNewIndex(int, off_t, int);
  151. extern void RTreeFreeIndex(struct RTree *);
  152. extern struct ListNode *RTreeNewListNode(void);
  153. extern void RTreeFreeListNode(struct ListNode *);
  154. extern void RTreeReInsertNode(struct Node *, struct ListNode **);
  155. extern void RTreeFreeListBranch(struct ListBranch *);
  156. /* indexm.c */
  157. extern int RTreeSearchM(struct RTree *, struct Rect *, SearchHitCallback *,
  158. void *);
  159. extern int RTreeInsertRectM(struct Rect *, union Child, int, struct RTree *);
  160. extern int RTreeDeleteRectM(struct Rect *, union Child, struct RTree *);
  161. extern int RTreeValidChildM(union Child *child);
  162. /* indexf.c */
  163. extern int RTreeSearchF(struct RTree *, struct Rect *, SearchHitCallback *,
  164. void *);
  165. extern int RTreeInsertRectF(struct Rect *, union Child, int, struct RTree *);
  166. extern int RTreeDeleteRectF(struct Rect *, union Child, struct RTree *);
  167. extern int RTreeValidChildF(union Child *child);
  168. /* node.c */
  169. extern struct Node *RTreeNewNode(struct RTree *, int);
  170. extern void RTreeInitNode(struct Node *, int);
  171. extern void RTreeFreeNode(struct Node *);
  172. extern void RTreeDestroyNode(struct Node *, int);
  173. extern struct Rect RTreeNodeCover(struct Node *, struct RTree *);
  174. extern int RTreeAddBranch(struct Branch *, struct Node *, struct Node **,
  175. struct ListBranch **, struct Rect *, int *, struct RTree *);
  176. extern int RTreePickBranch(struct Rect *, struct Node *, struct RTree *);
  177. extern void RTreeDisconnectBranch(struct Node *, int, struct RTree *);
  178. extern void RTreePrintNode(struct Node *, int, struct RTree *);
  179. extern void RTreeTabIn(int);
  180. /* rect.c */
  181. extern void RTreeInitRect(struct Rect *);
  182. extern struct Rect RTreeNullRect(void);
  183. extern RectReal RTreeRectArea(struct Rect *, struct RTree *);
  184. extern RectReal RTreeRectSphericalVolume(struct Rect *, struct RTree *);
  185. extern RectReal RTreeRectVolume(struct Rect *, struct RTree *);
  186. extern RectReal RTreeRectMargin(struct Rect *, struct RTree *);
  187. extern struct Rect RTreeCombineRect(struct Rect *, struct Rect *, struct RTree *);
  188. extern int RTreeCompareRect(struct Rect *, struct Rect *, struct RTree *);
  189. extern int RTreeOverlap(struct Rect *, struct Rect *, struct RTree *);
  190. extern void RTreePrintRect(struct Rect *, int);
  191. /* split.c */
  192. extern void RTreeSplitNode(struct Node *, struct Branch *, struct Node *, struct RTree *);
  193. /* card.c */
  194. extern int RTreeSetNodeMax(int, struct RTree *);
  195. extern int RTreeSetLeafMax(int, struct RTree *);
  196. extern int RTreeGetNodeMax(struct RTree *);
  197. extern int RTreeGetLeafMax(struct RTree *);
  198. /* fileio.c */
  199. extern void RTreeGetNode(struct Node *, off_t, int, struct RTree *);
  200. extern size_t RTreeReadNode(struct Node *, off_t, struct RTree *);
  201. extern void RTreePutNode(struct Node *, off_t, struct RTree *);
  202. extern size_t RTreeWriteNode(struct Node *, struct RTree *);
  203. extern size_t RTreeRewriteNode(struct Node *, off_t, struct RTree *);
  204. extern void RTreeUpdateRect(struct Rect *, struct Node *, off_t, int, struct RTree *);
  205. extern void RTreeAddNodePos(off_t, int, struct RTree *);
  206. extern off_t RTreeGetNodePos(struct RTree *);
  207. extern void RTreeFlushBuffer(struct RTree *);
  208. #endif /* _INDEX_ */