node.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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 <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <assert.h>
  21. #include <grass/gis.h>
  22. #include "index.h"
  23. #include "split.h"
  24. #include "card.h"
  25. /* rectangle distances for forced removal */
  26. struct dist
  27. {
  28. int id; /* branch id */
  29. RectReal distance; /* distance to node center */
  30. };
  31. /* Initialize one branch cell in an internal node. */
  32. static void RTreeInitNodeBranchM(struct RTree_Branch *b, struct RTree *t)
  33. {
  34. RTreeInitRect(&(b->rect), t);
  35. b->child.ptr = NULL;
  36. }
  37. /* Initialize one branch cell in an internal node. */
  38. static void RTreeInitNodeBranchF(struct RTree_Branch *b, struct RTree *t)
  39. {
  40. RTreeInitRect(&(b->rect), t);
  41. b->child.pos = -1;
  42. }
  43. /* Initialize one branch cell in a leaf node. */
  44. static void RTreeInitLeafBranch(struct RTree_Branch *b, struct RTree *t)
  45. {
  46. RTreeInitRect(&(b->rect), t);
  47. b->child.id = 0;
  48. }
  49. static void (*RTreeInitBranch[3]) (struct RTree_Branch *b, struct RTree *t) = {
  50. RTreeInitLeafBranch, RTreeInitNodeBranchM, RTreeInitNodeBranchF
  51. };
  52. /* Initialize a Node structure. */
  53. /* type = 1: leaf, type = 2: internal, memory, type = 3: internal, file */
  54. void RTreeInitNode(struct RTree *t, struct RTree_Node *n, int type)
  55. {
  56. int i;
  57. n->count = 0;
  58. n->level = -1;
  59. for (i = 0; i < MAXCARD; i++)
  60. RTreeInitBranch[type](&(n->branch[i]), t);
  61. }
  62. /* Make a new node and initialize to have all branch cells empty. */
  63. struct RTree_Node *RTreeNewNode(struct RTree *t, int level)
  64. {
  65. int i;
  66. struct RTree_Node *n;
  67. n = (struct RTree_Node *)malloc(sizeof(struct RTree_Node));
  68. assert(n);
  69. n->count = 0;
  70. n->level = level;
  71. for (i = 0; i < MAXCARD; i++) {
  72. RTreeNewRect(&(n->branch[i].rect), t);
  73. RTreeInitBranch[NODETYPE(level, t->fd)](&(n->branch[i]), t);
  74. }
  75. return n;
  76. }
  77. void RTreeFreeNode(struct RTree_Node *n)
  78. {
  79. int i;
  80. assert(n);
  81. for (i = 0; i < MAXCARD; i++)
  82. free(n->branch[i].rect.boundary);
  83. free(n);
  84. }
  85. /* copy node 2 to node 1 */
  86. void RTreeCopyNode(struct RTree_Node *n1, struct RTree_Node *n2, struct RTree *t)
  87. {
  88. int i;
  89. assert(n1 && n2);
  90. n1->count = n2->count;
  91. n1->level = n2->level;
  92. for (i = 0; i < MAXCARD; i++) {
  93. RTreeCopyBranch(&(n1->branch[i]), &(n2->branch[i]), t);
  94. }
  95. }
  96. /* copy branch 2 to branch 1 */
  97. void RTreeCopyBranch(struct RTree_Branch *b1, struct RTree_Branch *b2, struct RTree *t)
  98. {
  99. b1->child = b2->child;
  100. RTreeCopyRect(&(b1->rect), &(b2->rect), t);
  101. }
  102. /*
  103. * Find the smallest rectangle that includes all rectangles in
  104. * branches of a node.
  105. */
  106. void RTreeNodeCover(struct RTree_Node *n, struct RTree_Rect *r, struct RTree *t)
  107. {
  108. int i, first_time = 1;
  109. if ((n)->level > 0) { /* internal node */
  110. for (i = 0; i < t->nodecard; i++) {
  111. if (t->valid_child(&(n->branch[i].child))) {
  112. if (first_time) {
  113. RTreeCopyRect(r, &(n->branch[i].rect), t);
  114. first_time = 0;
  115. }
  116. else
  117. RTreeExpandRect(r, &(n->branch[i].rect), t);
  118. }
  119. }
  120. }
  121. else { /* leaf */
  122. for (i = 0; i < t->leafcard; i++) {
  123. if (n->branch[i].child.id) {
  124. if (first_time) {
  125. RTreeCopyRect(r, &(n->branch[i].rect), t);
  126. first_time = 0;
  127. }
  128. else
  129. RTreeExpandRect(r, &(n->branch[i].rect), t);
  130. }
  131. }
  132. }
  133. }
  134. /*
  135. * Idea from R*-tree, modified: not overlap size but overlap number
  136. *
  137. * Pick a branch from leaf nodes (current node has level 1). Pick the
  138. * one that will result in the smallest number of overlapping siblings.
  139. * This will result in the least ambiguous node covering the new
  140. * rectangle, improving search speed.
  141. * In case of a tie, pick the one which needs the smallest increase in
  142. * area to accomodate the new rectangle, then the smallest area before,
  143. * to get the best resolution when searching.
  144. */
  145. static int RTreePickLeafBranch(struct RTree_Rect *r, struct RTree_Node *n, struct RTree *t)
  146. {
  147. struct RTree_Rect *rr;
  148. int i, j;
  149. RectReal increase, bestIncr = -1, area, bestArea = 0;
  150. int best = 0, bestoverlap;
  151. int overlap;
  152. bestoverlap = t->nodecard + 1;
  153. /* get the branch that will overlap with the smallest number of
  154. * sibling branches when including the new rectangle */
  155. for (i = 0; i < t->nodecard; i++) {
  156. if (t->valid_child(&(n->branch[i].child))) {
  157. rr = &n->branch[i].rect;
  158. RTreeCombineRect(r, rr, &(t->orect), t);
  159. area = RTreeRectSphericalVolume(rr, t);
  160. increase = RTreeRectSphericalVolume(&(t->orect), t) - area;
  161. overlap = 0;
  162. for (j = 0; j < t->leafcard; j++) {
  163. if (j != i) {
  164. rr = &n->branch[j].rect;
  165. overlap += RTreeOverlap(&(t->orect), rr, t);
  166. }
  167. }
  168. if (overlap < bestoverlap) {
  169. best = i;
  170. bestoverlap = overlap;
  171. bestArea = area;
  172. bestIncr = increase;
  173. }
  174. else if (overlap == bestoverlap) {
  175. /* resolve ties */
  176. if (increase < bestIncr) {
  177. best = i;
  178. bestArea = area;
  179. bestIncr = increase;
  180. }
  181. else if (increase == bestIncr && area < bestArea) {
  182. best = i;
  183. bestArea = area;
  184. }
  185. }
  186. }
  187. }
  188. return best;
  189. }
  190. /*
  191. * Pick a branch. Pick the one that will need the smallest increase
  192. * in area to accomodate the new rectangle. This will result in the
  193. * least total area for the covering rectangles in the current node.
  194. * In case of a tie, pick the one which was smaller before, to get
  195. * the best resolution when searching.
  196. */
  197. int RTreePickBranch(struct RTree_Rect *r, struct RTree_Node *n, struct RTree *t)
  198. {
  199. struct RTree_Rect *rr;
  200. int i, first_time = 1;
  201. RectReal increase, bestIncr = (RectReal) -1, area, bestArea = 0;
  202. int best = 0;
  203. assert((n)->level > 0); /* must not be called on leaf node */
  204. if ((n)->level == 1)
  205. return RTreePickLeafBranch(r, n, t);
  206. for (i = 0; i < t->nodecard; i++) {
  207. if (t->valid_child(&(n->branch[i].child))) {
  208. rr = &n->branch[i].rect;
  209. area = RTreeRectSphericalVolume(rr, t);
  210. RTreeCombineRect(r, rr, &(t->orect), t);
  211. increase = RTreeRectSphericalVolume(&(t->orect), t) - area;
  212. if (increase < bestIncr || first_time) {
  213. best = i;
  214. bestArea = area;
  215. bestIncr = increase;
  216. first_time = 0;
  217. }
  218. else if (increase == bestIncr && area < bestArea) {
  219. best = i;
  220. bestArea = area;
  221. }
  222. }
  223. }
  224. return best;
  225. }
  226. /* Disconnect a dependent node. */
  227. void RTreeDisconnectBranch(struct RTree_Node *n, int i, struct RTree *t)
  228. {
  229. if ((n)->level > 0) {
  230. assert(n && i >= 0 && i < t->nodecard);
  231. assert(t->valid_child(&(n->branch[i].child)));
  232. if (t->fd < 0)
  233. RTreeInitNodeBranchM(&(n->branch[i]), t);
  234. else
  235. RTreeInitNodeBranchF(&(n->branch[i]), t);
  236. }
  237. else {
  238. assert(n && i >= 0 && i < t->leafcard);
  239. assert(n->branch[i].child.id);
  240. RTreeInitLeafBranch(&(n->branch[i]), t);
  241. }
  242. n->count--;
  243. }
  244. /* Destroy (free) node recursively. */
  245. /* NOTE: only needed for memory based index */
  246. void RTreeDestroyNode(struct RTree_Node *n, int nodes)
  247. {
  248. int i;
  249. if (n->level > 0) { /* it is not leaf -> destroy childs */
  250. for (i = 0; i < nodes; i++) {
  251. if (n->branch[i].child.ptr) {
  252. RTreeDestroyNode(n->branch[i].child.ptr, nodes);
  253. }
  254. }
  255. }
  256. /* Free this node */
  257. RTreeFreeNode(n);
  258. return;
  259. }
  260. /****************************************************************
  261. * *
  262. * R*-tree: force remove FORCECARD branches for reinsertion *
  263. * *
  264. ****************************************************************/
  265. /*
  266. * swap dist structs
  267. */
  268. static void RTreeSwapDist(struct dist *a, struct dist *b)
  269. {
  270. struct dist c;
  271. c = *a;
  272. *a = *b;
  273. *b = c;
  274. }
  275. /*
  276. * check if dist is sorted ascending to distance
  277. */
  278. static int RTreeDistIsSorted(struct dist *d, int first, int last)
  279. {
  280. int i;
  281. for (i = first; i < last; i++) {
  282. if (d[i].distance > d[i + 1].distance)
  283. return 0;
  284. }
  285. return 1;
  286. }
  287. /*
  288. * partition dist for quicksort on distance
  289. */
  290. static int RTreePartitionDist(struct dist *d, int first, int last)
  291. {
  292. int pivot, mid = ((first + last) >> 1);
  293. int larger, smaller;
  294. if (last - first == 1) { /* only two items in list */
  295. if (d[first].distance > d[last].distance) {
  296. RTreeSwapDist(&(d[first]), &(d[last]));
  297. }
  298. return last;
  299. }
  300. /* Larger of two */
  301. larger = pivot = mid;
  302. smaller = first;
  303. if (d[first].distance > d[mid].distance) {
  304. larger = pivot = first;
  305. smaller = mid;
  306. }
  307. if (d[larger].distance > d[last].distance) {
  308. /* larger is largest, get the larger of smaller and last */
  309. pivot = last;
  310. if (d[smaller].distance > d[last].distance) {
  311. pivot = smaller;
  312. }
  313. }
  314. if (pivot != last) {
  315. RTreeSwapDist(&(d[pivot]), &(d[last]));
  316. }
  317. pivot = first;
  318. while (first < last) {
  319. if (d[first].distance <= d[last].distance) {
  320. if (pivot != first) {
  321. RTreeSwapDist(&(d[pivot]), &(d[first]));
  322. }
  323. pivot++;
  324. }
  325. ++first;
  326. }
  327. if (pivot != last) {
  328. RTreeSwapDist(&(d[pivot]), &(d[last]));
  329. }
  330. return pivot;
  331. }
  332. /*
  333. * quicksort dist struct ascending by distance
  334. * n is last valid index
  335. */
  336. static void RTreeQuicksortDist(struct dist *d, int n)
  337. {
  338. int pivot, first, last;
  339. int s_first[MAXCARD + 1], s_last[MAXCARD + 1], stacksize;
  340. s_first[0] = 0;
  341. s_last[0] = n;
  342. stacksize = 1;
  343. /* use stack */
  344. while (stacksize) {
  345. stacksize--;
  346. first = s_first[stacksize];
  347. last = s_last[stacksize];
  348. if (first < last) {
  349. if (!RTreeDistIsSorted(d, first, last)) {
  350. pivot = RTreePartitionDist(d, first, last);
  351. s_first[stacksize] = first;
  352. s_last[stacksize] = pivot - 1;
  353. stacksize++;
  354. s_first[stacksize] = pivot + 1;
  355. s_last[stacksize] = last;
  356. stacksize++;
  357. }
  358. }
  359. }
  360. }
  361. /*
  362. * Allocate space for a branch in the list used in InsertRect to
  363. * store branches of nodes that are too full.
  364. */
  365. static struct RTree_ListBranch *RTreeNewListBranch(struct RTree *t)
  366. {
  367. struct RTree_ListBranch *p =
  368. (struct RTree_ListBranch *)malloc(sizeof(struct RTree_ListBranch));
  369. assert(p);
  370. RTreeNewRect(&(p->b.rect), t);
  371. return p;
  372. }
  373. /*
  374. * Add a branch to the reinsertion list. It will later
  375. * be reinserted into the index structure.
  376. */
  377. static void RTreeReInsertBranch(struct RTree_Branch b, int level,
  378. struct RTree_ListBranch **ee, struct RTree *t)
  379. {
  380. register struct RTree_ListBranch *l;
  381. l = RTreeNewListBranch(t);
  382. RTreeCopyBranch(&(l->b), &b, t);
  383. l->level = level;
  384. l->next = *ee;
  385. *ee = l;
  386. }
  387. /*
  388. * Remove branches from a node. Select the 2 branches whose rectangle
  389. * center is farthest away from node cover center.
  390. * Old node updated.
  391. */
  392. static void RTreeRemoveBranches(struct RTree_Node *n, struct RTree_Branch *b,
  393. struct RTree_ListBranch **ee, struct RTree_Rect *cover,
  394. struct RTree *t)
  395. {
  396. int i, j, maxkids, type;
  397. RectReal center_r, delta;
  398. struct dist rdist[MAXCARD + 1];
  399. struct RTree_Rect *new_cover = &(t->orect);
  400. RectReal *center_n = t->center_n;
  401. assert(cover);
  402. maxkids = MAXKIDS((n)->level, t);
  403. type = NODETYPE((n)->level, t->fd);
  404. assert(n->count == maxkids); /* must be full */
  405. RTreeCombineRect(cover, &(b->rect), new_cover, t);
  406. /* center coords of node cover */
  407. for (j = 0; j < t->ndims; j++) {
  408. center_n[j] = (new_cover->boundary[j + t->ndims_alloc] + new_cover->boundary[j]) / 2;
  409. }
  410. /* compute distances of child rectangle centers to node cover center */
  411. for (i = 0; i < maxkids; i++) {
  412. RTreeCopyBranch(&(t->BranchBuf[i]), &(n->branch[i]), t);
  413. rdist[i].distance = 0;
  414. rdist[i].id = i;
  415. for (j = 0; j < t->ndims; j++) {
  416. center_r =
  417. (t->BranchBuf[i].rect.boundary[j + t->ndims_alloc] +
  418. t->BranchBuf[i].rect.boundary[j]) / 2;
  419. delta = center_n[j] - center_r;
  420. rdist[i].distance += delta * delta;
  421. }
  422. RTreeInitBranch[type](&(n->branch[i]), t);
  423. }
  424. /* new branch */
  425. RTreeCopyBranch(&(t->BranchBuf[maxkids]), b, t);
  426. rdist[maxkids].distance = 0;
  427. for (j = 0; j < t->ndims; j++) {
  428. center_r =
  429. (b->rect.boundary[j + t->ndims_alloc] +
  430. b->rect.boundary[j]) / 2;
  431. delta = center_n[j] - center_r;
  432. rdist[maxkids].distance += delta * delta;
  433. }
  434. rdist[maxkids].id = maxkids;
  435. /* quicksort dist */
  436. RTreeQuicksortDist(rdist, maxkids);
  437. /* put largest three in branch list, farthest from center first */
  438. for (i = 0; i < FORCECARD; i++) {
  439. RTreeReInsertBranch(t->BranchBuf[rdist[maxkids - i].id], n->level, ee, t);
  440. }
  441. /* put remaining in node, closest to center first */
  442. for (i = 0; i < maxkids - FORCECARD + 1; i++) {
  443. RTreeCopyBranch(&(n->branch[i]), &(t->BranchBuf[rdist[i].id]), t);
  444. }
  445. n->count = maxkids - FORCECARD + 1;
  446. }
  447. /*
  448. * Add a branch to a node. Split the node if necessary.
  449. * Returns 0 if node not split. Old node updated.
  450. * Returns 1 if node split, sets *new_node to address of new node.
  451. * Old node updated, becomes one of two.
  452. * Returns 2 if branches were removed for forced reinsertion
  453. */
  454. int RTreeAddBranch(struct RTree_Branch *b, struct RTree_Node *n,
  455. struct RTree_Node **newnode, struct RTree_ListBranch **ee,
  456. struct RTree_Rect *cover, char *overflow, struct RTree *t)
  457. {
  458. int i, maxkids;
  459. maxkids = MAXKIDS((n)->level, t);
  460. if (n->count < maxkids) { /* split won't be necessary */
  461. if ((n)->level > 0) { /* internal node */
  462. for (i = 0; i < maxkids; i++) { /* find empty branch */
  463. if (!t->valid_child(&(n->branch[i].child))) {
  464. /* copy branch */
  465. n->branch[i].child = b->child;
  466. RTreeCopyRect(&(n->branch[i].rect), &(b->rect), t);
  467. n->count++;
  468. break;
  469. }
  470. }
  471. return 0;
  472. }
  473. else if ((n)->level == 0) { /* leaf */
  474. for (i = 0; i < maxkids; i++) { /* find empty branch */
  475. if (n->branch[i].child.id == 0) {
  476. /* copy branch */
  477. n->branch[i].child = b->child;
  478. RTreeCopyRect(&(n->branch[i].rect), &(b->rect), t);
  479. n->count++;
  480. break;
  481. }
  482. }
  483. return 0;
  484. }
  485. }
  486. else {
  487. if (n->level < t->rootlevel && overflow[n->level]) {
  488. /* R*-tree forced reinsert */
  489. RTreeRemoveBranches(n, b, ee, cover, t);
  490. overflow[n->level] = 0;
  491. return 2;
  492. }
  493. else {
  494. if (t->fd > -1)
  495. RTreeInitNode(t, *newnode, NODETYPE(n->level, t->fd));
  496. else
  497. *newnode = RTreeNewNode(t, (n)->level);
  498. RTreeSplitNode(n, b, *newnode, t);
  499. return 1;
  500. }
  501. }
  502. /* should not be reached */
  503. assert(0);
  504. return -1;
  505. }
  506. /*
  507. * for debugging only: print items to stdout
  508. */
  509. void RTreeTabIn(int depth)
  510. {
  511. int i;
  512. for (i = 0; i < depth; i++)
  513. putchar('\t');
  514. }
  515. static void RTreePrintBranch(struct RTree_Branch *b, int depth, struct RTree *t)
  516. {
  517. RTreePrintRect(&(b->rect), depth, t);
  518. RTreePrintNode(b->child.ptr, depth, t);
  519. }
  520. /* Print out the data in a node. */
  521. void RTreePrintNode(struct RTree_Node *n, int depth, struct RTree *t)
  522. {
  523. int i, maxkids;
  524. RTreeTabIn(depth);
  525. maxkids = (n->level > 0 ? t->nodecard : t->leafcard);
  526. fprintf(stdout, "node");
  527. if (n->level == 0)
  528. fprintf(stdout, " LEAF");
  529. else if (n->level > 0)
  530. fprintf(stdout, " NONLEAF");
  531. else
  532. fprintf(stdout, " TYPE=?");
  533. fprintf(stdout, " level=%d count=%d", n->level, n->count);
  534. for (i = 0; i < maxkids; i++) {
  535. if (n->level == 0) {
  536. RTreeTabIn(depth);
  537. RTreePrintRect(&(n->branch[i].rect), depth, t);
  538. fprintf(stdout, "\t%d: data id = %d\n", i,
  539. n->branch[i].child.id);
  540. }
  541. else {
  542. RTreeTabIn(depth);
  543. fprintf(stdout, "branch %d\n", i);
  544. RTreePrintBranch(&(n->branch[i]), depth + 1, t);
  545. }
  546. }
  547. }