node.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. *
  8. * PURPOSE: Multidimensional index
  9. *
  10. * COPYRIGHT: (C) 2001 by the GRASS Development Team
  11. *
  12. * This program is free software under the GNU General Public
  13. * License (>=v2). Read the file COPYING that comes with GRASS
  14. * for details.
  15. *****************************************************************************/
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include "assert.h"
  19. #include "index.h"
  20. #include "card.h"
  21. /* Initialize one branch cell in a node. */
  22. static void RTreeInitBranch(struct Branch *b)
  23. {
  24. RTreeInitRect(&(b->rect));
  25. b->child = NULL;
  26. }
  27. /* Initialize a Node structure. */
  28. void RTreeInitNode(struct Node *N)
  29. {
  30. register struct Node *n = N;
  31. register int i;
  32. n->count = 0;
  33. n->level = -1;
  34. for (i = 0; i < MAXCARD; i++)
  35. RTreeInitBranch(&(n->branch[i]));
  36. }
  37. /* Make a new node and initialize to have all branch cells empty. */
  38. struct Node *RTreeNewNode(void)
  39. {
  40. register struct Node *n;
  41. /* n = new Node; */
  42. n = (struct Node *)malloc(sizeof(struct Node));
  43. assert(n);
  44. RTreeInitNode(n);
  45. return n;
  46. }
  47. void RTreeFreeNode(struct Node *p)
  48. {
  49. assert(p);
  50. /* delete p; */
  51. free(p);
  52. }
  53. static void RTreePrintBranch(struct Branch *b, int depth)
  54. {
  55. RTreePrintRect(&(b->rect), depth);
  56. RTreePrintNode(b->child, depth);
  57. }
  58. extern void RTreeTabIn(int depth)
  59. {
  60. int i;
  61. for (i = 0; i < depth; i++)
  62. putchar('\t');
  63. }
  64. /* Print out the data in a node. */
  65. void RTreePrintNode(struct Node *n, int depth)
  66. {
  67. int i;
  68. assert(n);
  69. RTreeTabIn(depth);
  70. fprintf(stdout, "node");
  71. if (n->level == 0)
  72. fprintf(stdout, " LEAF");
  73. else if (n->level > 0)
  74. fprintf(stdout, " NONLEAF");
  75. else
  76. fprintf(stdout, " TYPE=?");
  77. fprintf(stdout, " level=%d count=%d address=%o\n", n->level, n->count,
  78. (unsigned)n);
  79. for (i = 0; i < n->count; i++) {
  80. if (n->level == 0) {
  81. /* RTreeTabIn(depth); */
  82. /* fprintf (stdout, "\t%d: data = %d\n", i, n->branch[i].child); */
  83. }
  84. else {
  85. RTreeTabIn(depth);
  86. fprintf(stdout, "branch %d\n", i);
  87. RTreePrintBranch(&n->branch[i], depth + 1);
  88. }
  89. }
  90. }
  91. /*
  92. * Find the smallest rectangle that includes all rectangles in
  93. * branches of a node.
  94. */
  95. struct Rect RTreeNodeCover(struct Node *N)
  96. {
  97. register struct Node *n = N;
  98. register int i, first_time = 1;
  99. struct Rect r;
  100. assert(n);
  101. RTreeInitRect(&r);
  102. for (i = 0; i < MAXKIDS(n); i++)
  103. if (n->branch[i].child) {
  104. if (first_time) {
  105. r = n->branch[i].rect;
  106. first_time = 0;
  107. }
  108. else
  109. r = RTreeCombineRect(&r, &(n->branch[i].rect));
  110. }
  111. return r;
  112. }
  113. /*
  114. * Pick a branch. Pick the one that will need the smallest increase
  115. * in area to accomodate the new rectangle. This will result in the
  116. * least total area for the covering rectangles in the current node.
  117. * In case of a tie, pick the one which was smaller before, to get
  118. * the best resolution when searching.
  119. */
  120. int RTreePickBranch(struct Rect *R, struct Node *N)
  121. {
  122. register struct Rect *r = R;
  123. register struct Node *n = N;
  124. register struct Rect *rr;
  125. register int i, first_time = 1;
  126. RectReal increase, bestIncr = (RectReal) - 1, area, bestArea = 0;
  127. int best = 0;
  128. struct Rect tmp_rect;
  129. assert(r && n);
  130. for (i = 0; i < MAXKIDS(n); i++) {
  131. if (n->branch[i].child) {
  132. rr = &n->branch[i].rect;
  133. area = RTreeRectSphericalVolume(rr);
  134. tmp_rect = RTreeCombineRect(r, rr);
  135. increase = RTreeRectSphericalVolume(&tmp_rect) - area;
  136. if (increase < bestIncr || first_time) {
  137. best = i;
  138. bestArea = area;
  139. bestIncr = increase;
  140. first_time = 0;
  141. }
  142. else if (increase == bestIncr && area < bestArea) {
  143. best = i;
  144. bestArea = area;
  145. bestIncr = increase;
  146. }
  147. }
  148. }
  149. return best;
  150. }
  151. /*
  152. * Add a branch to a node. Split the node if necessary.
  153. * Returns 0 if node not split. Old node updated.
  154. * Returns 1 if node split, sets *new_node to address of new node.
  155. * Old node updated, becomes one of two.
  156. */
  157. int RTreeAddBranch(struct Branch *B, struct Node *N, struct Node **New_node)
  158. {
  159. register struct Branch *b = B;
  160. register struct Node *n = N;
  161. register struct Node **new_node = New_node;
  162. register int i;
  163. assert(b);
  164. assert(n);
  165. if (n->count < MAXKIDS(n)) { /* split won't be necessary */
  166. for (i = 0; i < MAXKIDS(n); i++) { /* find empty branch */
  167. if (n->branch[i].child == NULL) {
  168. n->branch[i] = *b;
  169. n->count++;
  170. break;
  171. }
  172. }
  173. return 0;
  174. }
  175. else {
  176. assert(new_node);
  177. RTreeSplitNode(n, b, new_node);
  178. return 1;
  179. }
  180. }
  181. /* Disconnect a dependent node. */
  182. void RTreeDisconnectBranch(struct Node *n, int i)
  183. {
  184. assert(n && i >= 0 && i < MAXKIDS(n));
  185. assert(n->branch[i].child);
  186. RTreeInitBranch(&(n->branch[i]));
  187. n->count--;
  188. }
  189. /* Destroy (free) node recursively. */
  190. void RTreeDestroyNode(struct Node *n)
  191. {
  192. int i;
  193. if (n->level > 0) { /* it is not leaf -> destroy childs */
  194. for (i = 0; i < NODECARD; i++) {
  195. if (n->branch[i].child) {
  196. RTreeDestroyNode(n->branch[i].child);
  197. }
  198. }
  199. }
  200. /* Free this node */
  201. RTreeFreeNode(n);
  202. }