indexm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. #include <stdlib.h>
  18. #include <assert.h>
  19. #include <grass/gis.h>
  20. #include "index.h"
  21. //#include "card.h"
  22. int RTreeValidChildM(union RTree_Child *child)
  23. {
  24. return (child->ptr != NULL);
  25. }
  26. /*
  27. * Search in an index tree for all data retangles that
  28. * overlap the argument rectangle.
  29. * Return the number of qualifying data rects.
  30. */
  31. int RTreeSearchM(struct RTree *t, struct RTree_Rect *r,
  32. SearchHitCallback *shcb, void *cbarg)
  33. {
  34. struct RTree_Node *n;
  35. int hitCount = 0, notfound;
  36. int i;
  37. int top = 0;
  38. struct mstack *s = t->ms;
  39. /* stack size of t->rootlevel + 1 is enough because of depth first search */
  40. /* only one node per level on stack at any given time */
  41. /* add root node position to stack */
  42. s[top].sn = t->root;
  43. s[top].branch_id = i = 0;
  44. n = s[top].sn;
  45. while (top >= 0) {
  46. n = s[top].sn;
  47. if (s[top].sn->level > 0) { /* this is an internal node in the tree */
  48. notfound = 1;
  49. for (i = s[top].branch_id; i < t->nodecard; i++) {
  50. if (s[top].sn->branch[i].child.ptr &&
  51. RTreeOverlap(r, &(s[top].sn->branch[i].rect), t)) {
  52. s[top++].branch_id = i + 1;
  53. /* add next node to stack */
  54. s[top].sn = n->branch[i].child.ptr;
  55. s[top].branch_id = 0;
  56. notfound = 0;
  57. break;
  58. }
  59. }
  60. if (notfound) {
  61. /* nothing else found, go back up */
  62. s[top].branch_id = t->nodecard;
  63. top--;
  64. }
  65. }
  66. else { /* this is a leaf node */
  67. for (i = 0; i < t->leafcard; i++) {
  68. if (s[top].sn->branch[i].child.id &&
  69. RTreeOverlap(r, &(s[top].sn->branch[i].rect), t)) {
  70. hitCount++;
  71. if (shcb) { /* call the user-provided callback */
  72. if (!shcb(s[top].sn->branch[i].child.id,
  73. &s[top].sn->branch[i].rect, cbarg)) {
  74. /* callback wants to terminate search early */
  75. return hitCount;
  76. }
  77. }
  78. }
  79. }
  80. top--;
  81. }
  82. }
  83. return hitCount;
  84. }
  85. /*
  86. * Inserts a new data rectangle into the index structure.
  87. * Non-recursively descends tree.
  88. * Returns 0 if node was not split and nothing was removed.
  89. * Returns 1 if root node was split. Old node updated to become one of two.
  90. * Returns 2 if branches need to be reinserted.
  91. * The level argument specifies the number of steps up from the leaf
  92. * level to insert; e.g. a data rectangle goes in at level = 0.
  93. */
  94. static int RTreeInsertRect2M(struct RTree_Rect *r, union RTree_Child child, int level,
  95. struct RTree_Node **newnode,
  96. struct RTree *t,
  97. struct RTree_ListBranch **ee, char *overflow)
  98. {
  99. int i;
  100. struct RTree_Node *n, *n2;
  101. struct RTree_Rect *cover;
  102. int top = 0, down = 0;
  103. int result;
  104. struct RTree_Branch *b = &(t->tmpb2);
  105. struct mstack *s = t->ms;
  106. /* add root node to stack */
  107. s[top].sn = t->root;
  108. /* go down to level of insertion */
  109. while (s[top].sn->level > level) {
  110. n = s[top].sn;
  111. i = RTreePickBranch(r, n, t);
  112. s[top++].branch_id = i;
  113. /* add next node to stack */
  114. s[top].sn = n->branch[i].child.ptr;
  115. }
  116. /* Have reached level for insertion. Remove p rectangles or split */
  117. RTreeCopyRect(&(b->rect), r, t);
  118. /* child field of leaves contains tid of data record */
  119. b->child = child;
  120. /* add branch, may split node or remove branches */
  121. cover = NULL;
  122. if (top)
  123. cover = &(s[top - 1].sn->branch[s[top - 1].branch_id].rect);
  124. result = RTreeAddBranch(b, s[top].sn, &n2, ee, cover, overflow, t);
  125. /* update node count */
  126. if (result == 1) {
  127. t->n_nodes++;
  128. }
  129. /* go back up */
  130. while (top) {
  131. down = top--;
  132. i = s[top].branch_id;
  133. if (result == 0) { /* branch was added */
  134. RTreeExpandRect(&(s[top].sn->branch[i].rect), r, t);
  135. }
  136. else if (result == 2) { /* branches were removed */
  137. /* get node cover of previous node */
  138. RTreeNodeCover(s[down].sn, &(s[top].sn->branch[i].rect), t);
  139. }
  140. else if (result == 1) { /* node was split */
  141. /* get node cover of previous node */
  142. RTreeNodeCover(s[down].sn, &(s[top].sn->branch[i].rect), t);
  143. /* add new branch for new node previously added by RTreeAddBranch() */
  144. b->child.ptr = n2;
  145. RTreeNodeCover(b->child.ptr, &(b->rect), t);
  146. /* add branch, may split node or remove branches */
  147. cover = NULL;
  148. if (top)
  149. cover = &(s[top - 1].sn->branch[s[top - 1].branch_id].rect);
  150. result =
  151. RTreeAddBranch(b, s[top].sn, &n2, ee, cover, overflow, t);
  152. /* update node count */
  153. if (result == 1) {
  154. t->n_nodes++;
  155. }
  156. }
  157. }
  158. *newnode = n2;
  159. return result;
  160. }
  161. /*
  162. * Insert a data rectangle into an index structure.
  163. * RTreeInsertRect provides for splitting the root;
  164. * returns 1 if root was split, 0 if it was not.
  165. * The level argument specifies the number of steps up from the leaf
  166. * level to insert; e.g. a data rectangle goes in at level = 0.
  167. * RTreeInsertRect2 does the actual insertion.
  168. */
  169. int RTreeInsertRectM(struct RTree_Rect *r, union RTree_Child child, int level,
  170. struct RTree *t)
  171. {
  172. struct RTree_Node *newnode, *newroot;
  173. struct RTree_ListBranch *reInsertList = NULL;
  174. struct RTree_ListBranch *e;
  175. int result;
  176. char overflow[MAXLEVEL];
  177. struct RTree_Branch *b = &(t->tmpb1);
  178. /* R*-tree forced reinsertion: for each level only once */
  179. memset(overflow, t->overflow, MAXLEVEL);
  180. result = RTreeInsertRect2M(r, child, level, &newnode, t,
  181. &reInsertList, overflow);
  182. if (result == 1) { /* root split */
  183. /* grow a new root, & tree taller */
  184. t->rootlevel++;
  185. newroot = RTreeAllocNode(t, t->rootlevel);
  186. newroot->level = t->rootlevel;
  187. /* branch for old root */
  188. RTreeNodeCover(t->root, &(b->rect), t);
  189. b->child.ptr = t->root;
  190. RTreeAddBranch(b, newroot, NULL, NULL, NULL, NULL, t);
  191. /* branch for new node created by RTreeInsertRect2() */
  192. RTreeNodeCover(newnode, &(b->rect), t);
  193. b->child.ptr = newnode;
  194. RTreeAddBranch(b, newroot, NULL, NULL, NULL, NULL, t);
  195. /* set new root node */
  196. t->root = newroot;
  197. t->n_nodes++;
  198. return result;
  199. }
  200. if (result == 2) { /* branches were removed */
  201. while (reInsertList) {
  202. /* get next branch in list */
  203. RTreeCopyBranch(b, &(reInsertList->b), t);
  204. level = reInsertList->level;
  205. e = reInsertList;
  206. reInsertList = reInsertList->next;
  207. RTreeFreeListBranch(e);
  208. /* reinsert branches */
  209. result =
  210. RTreeInsertRect2M(&(b->rect), b->child, level, &newnode, t,
  211. &reInsertList, overflow);
  212. if (result == 1) { /* root split */
  213. /* grow a new root, & tree taller */
  214. t->rootlevel++;
  215. newroot = RTreeAllocNode(t, t->rootlevel);
  216. newroot->level = t->rootlevel;
  217. /* branch for old root */
  218. RTreeNodeCover(t->root, &(b->rect), t);
  219. b->child.ptr = t->root;
  220. RTreeAddBranch(b, newroot, NULL, NULL, NULL, NULL, t);
  221. /* branch for new node created by RTreeInsertRect2() */
  222. RTreeNodeCover(newnode, &(b->rect), t);
  223. b->child.ptr = newnode;
  224. RTreeAddBranch(b, newroot, NULL, NULL, NULL, NULL, t);
  225. /* set new root node */
  226. t->root = newroot;
  227. t->n_nodes++;
  228. }
  229. }
  230. }
  231. return result;
  232. }
  233. /*
  234. * Delete a rectangle from non-root part of an index structure.
  235. * Called by RTreeDeleteRect. Descends tree non-recursively,
  236. * merges branches on the way back up.
  237. * Returns 1 if record not found, 0 if success.
  238. */
  239. static int
  240. RTreeDeleteRect2M(struct RTree_Rect *r, union RTree_Child child, struct RTree *t,
  241. struct RTree_ListNode **ee)
  242. {
  243. int i, notfound = 1;
  244. struct RTree_Node *n;
  245. int top = 0, down = 0;
  246. int minfill;
  247. struct mstack *s = t->ms;
  248. assert(ee);
  249. /* add root node position to stack */
  250. s[top].sn = t->root;
  251. s[top].branch_id = 0;
  252. n = s[top].sn;
  253. while (notfound && top >= 0) {
  254. /* go down to level 0, remember path */
  255. if (s[top].sn->level > 0) {
  256. n = s[top].sn;
  257. for (i = s[top].branch_id; i < t->nodecard; i++) {
  258. if (n->branch[i].child.ptr &&
  259. RTreeOverlap(r, &(n->branch[i].rect), t)) {
  260. s[top++].branch_id = i + 1;
  261. /* add next node to stack */
  262. s[top].sn = n->branch[i].child.ptr;
  263. s[top].branch_id = 0;
  264. notfound = 0;
  265. break;
  266. }
  267. }
  268. if (notfound) {
  269. /* nothing else found, go back up */
  270. s[top].branch_id = t->nodecard;
  271. top--;
  272. }
  273. else /* found a way down but not yet the item */
  274. notfound = 1;
  275. }
  276. else {
  277. for (i = 0; i < t->leafcard; i++) {
  278. if (s[top].sn->branch[i].child.id &&
  279. s[top].sn->branch[i].child.id == child.id) { /* found item */
  280. RTreeDisconnectBranch(s[top].sn, i, t);
  281. t->n_leafs--;
  282. notfound = 0;
  283. break;
  284. }
  285. }
  286. if (notfound) /* continue searching */
  287. top--;
  288. }
  289. }
  290. if (notfound) {
  291. return notfound;
  292. }
  293. /* go back up */
  294. while (top) {
  295. down = top;
  296. top--;
  297. i = s[top].branch_id - 1;
  298. assert(s[down].sn->level == s[top].sn->level - 1);
  299. minfill = (s[down].sn->level ? t->min_node_fill : t->min_leaf_fill);
  300. if (s[down].sn->count >= minfill) {
  301. /* just update node cover */
  302. RTreeNodeCover(s[down].sn, &(s[top].sn->branch[i].rect), t);
  303. }
  304. else {
  305. /* not enough entries in child, eliminate child node */
  306. RTreeReInsertNode(s[top].sn->branch[i].child.ptr, ee);
  307. RTreeDisconnectBranch(s[top].sn, i, t);
  308. }
  309. }
  310. return notfound;
  311. }
  312. /*
  313. * should be called by RTreeDeleteRect() only
  314. *
  315. * Delete a data rectangle from an index structure.
  316. * Pass in a pointer to a Rect, the tid of the record, ptr RTree.
  317. * Returns 1 if record not found, 0 if success.
  318. * RTreeDeleteRect1 provides for eliminating the root.
  319. */
  320. int RTreeDeleteRectM(struct RTree_Rect *r, union RTree_Child child, struct RTree *t)
  321. {
  322. int i;
  323. struct RTree_Node *n;
  324. struct RTree_ListNode *reInsertList = NULL;
  325. struct RTree_ListNode *e;
  326. if (!RTreeDeleteRect2M(r, child, t, &reInsertList)) {
  327. /* found and deleted a data item */
  328. /* reinsert any branches from eliminated nodes */
  329. while (reInsertList) {
  330. t->n_nodes--;
  331. n = reInsertList->node;
  332. if (n->level > 0) { /* reinsert node branches */
  333. for (i = 0; i < t->nodecard; i++) {
  334. if (n->branch[i].child.ptr) {
  335. RTreeInsertRectM(&(n->branch[i].rect),
  336. n->branch[i].child, n->level, t);
  337. }
  338. }
  339. }
  340. else { /* reinsert leaf branches */
  341. for (i = 0; i < t->leafcard; i++) {
  342. if (n->branch[i].child.id) {
  343. RTreeInsertRectM(&(n->branch[i].rect),
  344. n->branch[i].child, n->level, t);
  345. }
  346. }
  347. }
  348. e = reInsertList;
  349. reInsertList = reInsertList->next;
  350. RTreeFreeNode(e->node);
  351. RTreeFreeListNode(e);
  352. }
  353. /* check for redundant root (not leaf, 1 child) and eliminate */
  354. n = t->root;
  355. if (n->count == 1 && n->level > 0) {
  356. for (i = 0; i < t->nodecard; i++) {
  357. if (n->branch[i].child.ptr)
  358. break;
  359. }
  360. t->root = n->branch[i].child.ptr;
  361. RTreeFreeNode(n);
  362. t->rootlevel--;
  363. }
  364. return 0;
  365. }
  366. return 1;
  367. }