node.c 14 KB

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