node.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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) / 2;
  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. if (d[first].distance > d[mid].distance) {
  302. larger = pivot = first;
  303. smaller = mid;
  304. }
  305. else {
  306. larger = pivot = mid;
  307. smaller = first;
  308. }
  309. if (d[larger].distance > d[last].distance) {
  310. /* larger is largest, get the larger of smaller and last */
  311. if (d[smaller].distance > d[last].distance) {
  312. pivot = smaller;
  313. }
  314. else {
  315. pivot = last;
  316. }
  317. }
  318. if (pivot != last) {
  319. RTreeSwapDist(&(d[pivot]), &(d[last]));
  320. }
  321. pivot = first;
  322. while (first < last) {
  323. if (d[first].distance <= d[last].distance) {
  324. if (pivot != first) {
  325. RTreeSwapDist(&(d[pivot]), &(d[first]));
  326. }
  327. pivot++;
  328. }
  329. ++first;
  330. }
  331. if (pivot != last) {
  332. RTreeSwapDist(&(d[pivot]), &(d[last]));
  333. }
  334. return pivot;
  335. }
  336. /*
  337. * quicksort dist struct ascending by distance
  338. * n is last valid index
  339. */
  340. static void RTreeQuicksortDist(struct dist *d, int n)
  341. {
  342. int pivot, first, last;
  343. int s_first[MAXCARD + 1], s_last[MAXCARD + 1], stacksize;
  344. s_first[0] = 0;
  345. s_last[0] = n;
  346. stacksize = 1;
  347. /* use stack */
  348. while (stacksize) {
  349. stacksize--;
  350. first = s_first[stacksize];
  351. last = s_last[stacksize];
  352. if (first < last) {
  353. if (!RTreeDistIsSorted(d, first, last)) {
  354. pivot = RTreePartitionDist(d, first, last);
  355. s_first[stacksize] = first;
  356. s_last[stacksize] = pivot - 1;
  357. stacksize++;
  358. s_first[stacksize] = pivot + 1;
  359. s_last[stacksize] = last;
  360. stacksize++;
  361. }
  362. }
  363. }
  364. }
  365. /*
  366. * Allocate space for a branch in the list used in InsertRect to
  367. * store branches of nodes that are too full.
  368. */
  369. static struct RTree_ListBranch *RTreeNewListBranch(struct RTree *t)
  370. {
  371. struct RTree_ListBranch *p =
  372. (struct RTree_ListBranch *)malloc(sizeof(struct RTree_ListBranch));
  373. assert(p);
  374. RTreeNewRect(&(p->b.rect), t);
  375. return p;
  376. }
  377. /*
  378. * Add a branch to the reinsertion list. It will later
  379. * be reinserted into the index structure.
  380. */
  381. static void RTreeReInsertBranch(struct RTree_Branch b, int level,
  382. struct RTree_ListBranch **ee, struct RTree *t)
  383. {
  384. register struct RTree_ListBranch *l;
  385. l = RTreeNewListBranch(t);
  386. RTreeCopyBranch(&(l->b), &b, t);
  387. l->level = level;
  388. l->next = *ee;
  389. *ee = l;
  390. }
  391. /*
  392. * Remove branches from a node. Select the 2 branches whose rectangle
  393. * center is farthest away from node cover center.
  394. * Old node updated.
  395. */
  396. static void RTreeRemoveBranches(struct RTree_Node *n, struct RTree_Branch *b,
  397. struct RTree_ListBranch **ee, struct RTree_Rect *cover,
  398. struct RTree *t)
  399. {
  400. int i, j, maxkids, type;
  401. RectReal center_r, delta;
  402. struct dist rdist[MAXCARD + 1];
  403. struct RTree_Rect *new_cover = &(t->orect);
  404. RectReal *center_n = t->center_n;
  405. assert(cover);
  406. maxkids = MAXKIDS((n)->level, t);
  407. type = NODETYPE((n)->level, t->fd);
  408. assert(n->count == maxkids); /* must be full */
  409. RTreeCombineRect(cover, &(b->rect), new_cover, t);
  410. /* center coords of node cover */
  411. for (j = 0; j < t->ndims; j++) {
  412. center_n[j] = (new_cover->boundary[j + t->ndims_alloc] + new_cover->boundary[j]) / 2;
  413. }
  414. /* compute distances of child rectangle centers to node cover center */
  415. for (i = 0; i < maxkids; i++) {
  416. RTreeCopyBranch(&(t->BranchBuf[i]), &(n->branch[i]), t);
  417. rdist[i].distance = 0;
  418. rdist[i].id = i;
  419. for (j = 0; j < t->ndims; j++) {
  420. center_r =
  421. (t->BranchBuf[i].rect.boundary[j + t->ndims_alloc] +
  422. t->BranchBuf[i].rect.boundary[j]) / 2;
  423. delta = center_n[j] - center_r;
  424. rdist[i].distance += delta * delta;
  425. }
  426. RTreeInitBranch[type](&(n->branch[i]), t);
  427. }
  428. /* new branch */
  429. RTreeCopyBranch(&(t->BranchBuf[maxkids]), b, t);
  430. rdist[maxkids].distance = 0;
  431. for (j = 0; j < t->ndims; j++) {
  432. center_r =
  433. (b->rect.boundary[j + t->ndims_alloc] +
  434. b->rect.boundary[j]) / 2;
  435. delta = center_n[j] - center_r;
  436. rdist[maxkids].distance += delta * delta;
  437. }
  438. rdist[maxkids].id = maxkids;
  439. /* quicksort dist */
  440. RTreeQuicksortDist(rdist, maxkids);
  441. /* put largest three in branch list, farthest from center first */
  442. for (i = 0; i < FORCECARD; i++) {
  443. RTreeReInsertBranch(t->BranchBuf[rdist[maxkids - i].id], n->level, ee, t);
  444. }
  445. /* put remaining in node, closest to center first */
  446. for (i = 0; i < maxkids - FORCECARD + 1; i++) {
  447. RTreeCopyBranch(&(n->branch[i]), &(t->BranchBuf[rdist[i].id]), t);
  448. }
  449. n->count = maxkids - FORCECARD + 1;
  450. }
  451. /*
  452. * Add a branch to a node. Split the node if necessary.
  453. * Returns 0 if node not split. Old node updated.
  454. * Returns 1 if node split, sets *new_node to address of new node.
  455. * Old node updated, becomes one of two.
  456. * Returns 2 if branches were removed for forced reinsertion
  457. */
  458. int RTreeAddBranch(struct RTree_Branch *b, struct RTree_Node *n,
  459. struct RTree_Node **newnode, struct RTree_ListBranch **ee,
  460. struct RTree_Rect *cover, int *overflow, struct RTree *t)
  461. {
  462. int i, maxkids;
  463. maxkids = MAXKIDS((n)->level, t);
  464. if (n->count < maxkids) { /* split won't be necessary */
  465. if ((n)->level > 0) { /* internal node */
  466. for (i = 0; i < maxkids; i++) { /* find empty branch */
  467. if (!t->valid_child(&(n->branch[i].child))) {
  468. /* copy branch */
  469. n->branch[i].child = b->child;
  470. RTreeCopyRect(&(n->branch[i].rect), &(b->rect), t);
  471. n->count++;
  472. break;
  473. }
  474. }
  475. return 0;
  476. }
  477. else if ((n)->level == 0) { /* leaf */
  478. for (i = 0; i < maxkids; i++) { /* find empty branch */
  479. if (n->branch[i].child.id == 0) {
  480. /* copy branch */
  481. n->branch[i].child = b->child;
  482. RTreeCopyRect(&(n->branch[i].rect), &(b->rect), t);
  483. n->count++;
  484. break;
  485. }
  486. }
  487. return 0;
  488. }
  489. }
  490. else {
  491. if (n->level < t->rootlevel && overflow[n->level]) {
  492. /* R*-tree forced reinsert */
  493. RTreeRemoveBranches(n, b, ee, cover, t);
  494. overflow[n->level] = 0;
  495. return 2;
  496. }
  497. else {
  498. if (t->fd > -1)
  499. RTreeInitNode(t, *newnode, NODETYPE(n->level, t->fd));
  500. else
  501. *newnode = RTreeNewNode(t, (n)->level);
  502. RTreeSplitNode(n, b, *newnode, t);
  503. return 1;
  504. }
  505. }
  506. /* should not be reached */
  507. assert(0);
  508. return -1;
  509. }
  510. /*
  511. * for debugging only: print items to stdout
  512. */
  513. void RTreeTabIn(int depth)
  514. {
  515. int i;
  516. for (i = 0; i < depth; i++)
  517. putchar('\t');
  518. }
  519. static void RTreePrintBranch(struct RTree_Branch *b, int depth, struct RTree *t)
  520. {
  521. RTreePrintRect(&(b->rect), depth, t);
  522. RTreePrintNode(b->child.ptr, depth, t);
  523. }
  524. /* Print out the data in a node. */
  525. void RTreePrintNode(struct RTree_Node *n, int depth, struct RTree *t)
  526. {
  527. int i, maxkids;
  528. RTreeTabIn(depth);
  529. maxkids = (n->level > 0 ? t->nodecard : t->leafcard);
  530. fprintf(stdout, "node");
  531. if (n->level == 0)
  532. fprintf(stdout, " LEAF");
  533. else if (n->level > 0)
  534. fprintf(stdout, " NONLEAF");
  535. else
  536. fprintf(stdout, " TYPE=?");
  537. fprintf(stdout, " level=%d count=%d", n->level, n->count);
  538. for (i = 0; i < maxkids; i++) {
  539. if (n->level == 0) {
  540. RTreeTabIn(depth);
  541. RTreePrintRect(&(n->branch[i].rect), depth, t);
  542. fprintf(stdout, "\t%d: data id = %d\n", i,
  543. n->branch[i].child.id);
  544. }
  545. else {
  546. RTreeTabIn(depth);
  547. fprintf(stdout, "branch %d\n", i);
  548. RTreePrintBranch(&(n->branch[i]), depth + 1, t);
  549. }
  550. }
  551. }