node.c 16 KB

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