index.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 - R*-tree
  8. *
  9. * PURPOSE: Multidimensional index
  10. *
  11. * COPYRIGHT: (C) 2009 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 <sys/types.h>
  20. /* PGSIZE is normally the natural page size of the machine */
  21. #define PGSIZE 512
  22. #define NUMDIMS 3 /* maximum number of dimensions */
  23. /* typedef float RectReal; */
  24. typedef double RectReal;
  25. /*-----------------------------------------------------------------------------
  26. | Global definitions.
  27. -----------------------------------------------------------------------------*/
  28. #ifndef TRUE
  29. #define TRUE 1
  30. #endif
  31. #ifndef FALSE
  32. #define FALSE 0
  33. #endif
  34. #define NUMSIDES 2*NUMDIMS
  35. /* max branching factor of a node */
  36. /* was (PGSIZE-(2 * sizeof(int))) / sizeof(struct Branch)
  37. * this is LFS dependent, not good
  38. * on 32 bit without LFS this is 9.69
  39. * on 32 bit with LFS and on 64 bit this is 9 */
  40. #define MAXCARD 9
  41. /* R*-tree: number of branches to be force-reinserted when adding a branch */
  42. #define FORCECARD 2
  43. /* maximum no of levels = tree depth */
  44. #define MAXLEVEL 20 /* 4^MAXLEVEL items are guaranteed to fit into the tree */
  45. struct Rect
  46. {
  47. RectReal boundary[NUMSIDES]; /* xmin,ymin,...,xmax,ymax,... */
  48. };
  49. struct Node; /* node for memory based index */
  50. union Child
  51. {
  52. int id; /* child id */
  53. struct Node *ptr; /* pointer to child node */
  54. };
  55. struct Branch /* branch for memory based index */
  56. {
  57. struct Rect rect;
  58. union Child child;
  59. };
  60. struct Node /* node for memory based index */
  61. {
  62. int count; /* number of branches */
  63. int level; /* 0 is leaf, others positive */
  64. struct Branch branch[MAXCARD];
  65. };
  66. struct RTree
  67. {
  68. /* RTree setup info */
  69. unsigned char ndims; /* number of dimensions */
  70. unsigned char nsides; /* number of sides = 2 * ndims */
  71. int nodesize; /* node size in bytes */
  72. int branchsize; /* branch size in bytes */
  73. int rectsize; /* rectangle size in bytes */
  74. /* RTree info, useful to calculate space requirements */
  75. unsigned int n_nodes; /* number of nodes */
  76. unsigned int n_leafs; /* number of data items (level 0 leafs) */
  77. int n_levels; /* n levels = root level */
  78. /* settings for RTree building */
  79. int nodecard; /* max number of childs in node */
  80. int leafcard; /* max number of childs in leaf */
  81. int min_node_fill; /* balance criteria for node splitting */
  82. int min_leaf_fill; /* balance criteria for leaf splitting */
  83. struct Node *root; /* pointer to root node */
  84. off_t rootpos; /* root node position in file */
  85. };
  86. struct ListNode
  87. {
  88. struct ListNode *next;
  89. struct Node *node;
  90. };
  91. struct ListBranch
  92. {
  93. struct ListBranch *next;
  94. struct Branch b;
  95. int level;
  96. };
  97. /*
  98. * If passed to a tree search, this callback function will be called
  99. * with the ID of each data rect that overlaps the search rect
  100. * plus whatever user specific pointer was passed to the search.
  101. * It can terminate the search early by returning 0 in which case
  102. * the search will return the number of hits found up to that point.
  103. */
  104. typedef int (*SearchHitCallback) (int id, void *arg);
  105. /* index.c */
  106. extern int RTreeSearch(struct RTree *, struct Rect *, SearchHitCallback,
  107. void *);
  108. extern int RTreeInsertRect(struct Rect *, int, struct RTree *t);
  109. extern int RTreeDeleteRect(struct Rect *, int, struct RTree *t);
  110. extern struct RTree *RTreeNewIndex(int);
  111. void RTreeFreeIndex(struct RTree *);
  112. /* node.c */
  113. extern struct Node *RTreeNewNode(struct RTree *, int);
  114. extern void RTreeInitNode(struct Node *, int);
  115. extern void RTreeFreeNode(struct Node *);
  116. extern void RTreeDestroyNode(struct Node *, int);
  117. extern struct Rect RTreeNodeCover(struct Node *, struct RTree *);
  118. extern int RTreeAddBranch(struct Branch *, struct Node *, struct Node **,
  119. struct ListBranch **, struct Rect *, int *, struct RTree *);
  120. extern int RTreePickBranch(struct Rect *, struct Node *, struct RTree *);
  121. extern void RTreeDisconnectBranch(struct Node *, int, struct RTree *);
  122. extern void RTreePrintNode(struct Node *, int, struct RTree *);
  123. extern void RTreeTabIn(int);
  124. /* rect.c */
  125. extern void RTreeInitRect(struct Rect *);
  126. extern struct Rect RTreeNullRect(void);
  127. extern RectReal RTreeRectArea(struct Rect *, struct RTree *);
  128. extern RectReal RTreeRectSphericalVolume(struct Rect *, struct RTree *);
  129. extern RectReal RTreeRectVolume(struct Rect *, struct RTree *);
  130. extern RectReal RTreeRectMargin(struct Rect *, struct RTree *);
  131. extern struct Rect RTreeCombineRect(struct Rect *, struct Rect *, struct RTree *);
  132. extern int RTreeOverlap(struct Rect *, struct Rect *, struct RTree *);
  133. extern void RTreePrintRect(struct Rect *, int);
  134. /* split.c */
  135. extern void RTreeSplitNode(struct Node *, struct Branch *, struct Node *, struct RTree *);
  136. /* card.c */
  137. extern int RTreeSetNodeMax(int, struct RTree *);
  138. extern int RTreeSetLeafMax(int, struct RTree *);
  139. extern int RTreeGetNodeMax(struct RTree *);
  140. extern int RTreeGetLeafMax(struct RTree *);
  141. #endif /* _INDEX_ */