kdtree.c 19 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. /*!
  2. * \file kdtree.c
  3. *
  4. * \brief binary search tree
  5. *
  6. * Dynamic balanced k-d tree implementation
  7. *
  8. * (C) 2014 by the GRASS Development Team
  9. *
  10. * This program is free software under the GNU General Public License
  11. * (>=v2). Read the file COPYING that comes with GRASS for details.
  12. *
  13. * \author Markus Metz
  14. */
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <math.h>
  18. #include <grass/gis.h>
  19. #include <grass/glocale.h>
  20. #include "kdtree.h"
  21. #ifdef MAX
  22. #undef MAX
  23. #endif
  24. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  25. #define KD_BTOL 6
  26. #ifdef KD_DEBUG
  27. #undef KD_DEBUG
  28. #endif
  29. static struct kdnode *kdtree_insert2(struct kdtree *, struct kdnode *,
  30. struct kdnode *, int, int);
  31. static int kdtree_replace(struct kdtree *, struct kdnode *);
  32. static int kdtree_balance(struct kdtree *, struct kdnode *);
  33. static int cmp(struct kdnode *a, struct kdnode *b, int p)
  34. {
  35. if (a->c[p] < b->c[p])
  36. return -1;
  37. if (a->c[p] > b->c[p])
  38. return 1;
  39. return (a->uid < b->uid ? -1 : a->uid > b->uid);
  40. }
  41. static int cmpc(struct kdnode *a, struct kdnode *b, struct kdtree *t)
  42. {
  43. int i;
  44. for (i = 0; i < t->ndims; i++) {
  45. if (a->c[i] != b->c[i]) {
  46. return 1;
  47. }
  48. }
  49. return 0;
  50. }
  51. static struct kdnode *kdtree_newnode(struct kdtree *t)
  52. {
  53. struct kdnode *n = G_malloc(sizeof(struct kdnode));
  54. n->c = G_malloc(t->ndims * sizeof(double));
  55. n->dim = 0;
  56. n->depth = 0;
  57. n->uid = 0;
  58. n->child[0] = NULL;
  59. n->child[1] = NULL;
  60. return n;
  61. }
  62. static void kdtree_free_node(struct kdnode *n)
  63. {
  64. G_free(n->c);
  65. G_free(n);
  66. }
  67. /* create a new kd tree with ndims dimensions,
  68. * optionally set balancing tolerance */
  69. struct kdtree *kdtree_create(char ndims, int *btol)
  70. {
  71. int i;
  72. struct kdtree *t;
  73. t = G_malloc(sizeof(struct kdtree));
  74. t->ndims = ndims;
  75. t->csize = ndims * sizeof(double);
  76. t->btol = KD_BTOL;
  77. if (btol) {
  78. t->btol = *btol;
  79. if (t->btol < 2)
  80. t->btol = 2;
  81. }
  82. t->nextdim = G_malloc(ndims * sizeof(char));
  83. for (i = 0; i < ndims - 1; i++)
  84. t->nextdim[i] = i + 1;
  85. t->nextdim[ndims - 1] = 0;
  86. t->count = 0;
  87. t->root = NULL;
  88. return t;
  89. }
  90. /* clear the tree, removing all entries */
  91. void kdtree_clear(struct kdtree *t)
  92. {
  93. struct kdnode *it;
  94. struct kdnode *save = t->root;
  95. /*
  96. Rotate away the left links so that
  97. we can treat this like the destruction
  98. of a linked list
  99. */
  100. while((it = save) != NULL) {
  101. if (it->child[0] == NULL) {
  102. /* No left links, just kill the node and move on */
  103. save = it->child[1];
  104. kdtree_free_node(it);
  105. it = NULL;
  106. }
  107. else {
  108. /* Rotate away the left link and check again */
  109. save = it->child[0];
  110. it->child[0] = save->child[1];
  111. save->child[1] = it;
  112. }
  113. }
  114. t->root = NULL;
  115. }
  116. /* destroy the tree */
  117. void kdtree_destroy(struct kdtree *t)
  118. {
  119. /* remove all entries */
  120. kdtree_clear(t);
  121. G_free(t->nextdim);
  122. G_free(t);
  123. t = NULL;
  124. }
  125. /* insert an item (coordinates c and uid) into the kd tree
  126. * dc == 1: allow duplicate coordinates */
  127. int kdtree_insert(struct kdtree *t, double *c, int uid, int dc)
  128. {
  129. struct kdnode *nnew;
  130. size_t count = t->count;
  131. nnew = kdtree_newnode(t);
  132. memcpy(nnew->c, c, t->csize);
  133. nnew->uid = uid;
  134. t->root = kdtree_insert2(t, t->root, nnew, 1, dc);
  135. return count < t->count;
  136. }
  137. /* remove an item from the kd tree
  138. * coordinates c and uid must match */
  139. int kdtree_remove(struct kdtree *t, double *c, int uid)
  140. {
  141. struct kdnode sn, *n;
  142. struct kdstack {
  143. struct kdnode *n;
  144. int dir;
  145. } s[256];
  146. int top;
  147. int dir, found;
  148. int ld, rd;
  149. sn.c = c;
  150. sn.uid = uid;
  151. /* find sn node */
  152. top = 0;
  153. s[top].n = t->root;
  154. dir = 1;
  155. found = 0;
  156. while (found) {
  157. n = s[top].n;
  158. found = (!cmpc(&sn, n, t) && sn.uid == n->uid);
  159. if (!found) {
  160. dir = cmp(&sn, n, n->dim) > 0;
  161. s[top].dir = dir;
  162. top++;
  163. s[top].n = n->child[dir];
  164. if (!s[top].n) {
  165. G_warning("Node does not exist");
  166. return 0;
  167. }
  168. }
  169. }
  170. if (!kdtree_replace(t, s[top].n)) {
  171. s[top].n = NULL;
  172. if (top) {
  173. n = s[top - 1].n;
  174. dir = s[top - 1].dir;
  175. n->child[dir] = NULL;
  176. }
  177. else
  178. t->root = NULL;
  179. return 1;
  180. }
  181. if (top) {
  182. int old_depth;
  183. top--;
  184. dir = s[top].dir;
  185. n = s[top].n;
  186. kdtree_balance(t, n->child[dir]);
  187. /* update node depth */
  188. old_depth = n->depth;
  189. ld = (!n->child[0] ? -1 : n->child[0]->depth);
  190. rd = (!n->child[1] ? -1 : n->child[1]->depth);
  191. n->depth = MAX(ld, rd) + 1;
  192. if (old_depth == n->depth)
  193. top = 0;
  194. }
  195. while (top) {
  196. top--;
  197. n = s[top].n;
  198. /* update node depth */
  199. ld = (!n->child[0] ? -1 : n->child[0]->depth);
  200. rd = (!n->child[1] ? -1 : n->child[1]->depth);
  201. n->depth = MAX(ld, rd) + 1;
  202. }
  203. kdtree_balance(t, t->root);
  204. return 1;
  205. }
  206. /* k-d tree optimization, only useful if the tree will be used heavily
  207. * (more searches than items in the tree)
  208. * level 0 = a bit, 1 = more, 2 = a lot */
  209. void kdtree_optimize(struct kdtree *t, int level)
  210. {
  211. struct kdnode *n;
  212. struct kdstack {
  213. struct kdnode *n;
  214. int dir;
  215. char v;
  216. } s[256];
  217. int dir;
  218. int top;
  219. int ld, rd;
  220. int count = 0;
  221. int bal = 0;
  222. if (!t->root)
  223. return;
  224. if (level < 0)
  225. level = 0;
  226. top = 0;
  227. s[top].n = t->root;
  228. s[top].dir = 0;
  229. s[top].v = 0;
  230. top++;
  231. /* top-down balancing */
  232. while (top) {
  233. top--;
  234. n = s[top].n;
  235. if (!s[top].v) {
  236. s[top].v = 1;
  237. while (kdtree_balance(t, n))
  238. bal++;
  239. }
  240. else {
  241. /* update node depth */
  242. ld = (!n->child[0] ? -1 : n->child[0]->depth);
  243. rd = (!n->child[1] ? -1 : n->child[1]->depth);
  244. n->depth = MAX(ld, rd) + 1;
  245. if (level) {
  246. while (kdtree_balance(t, n))
  247. bal++;
  248. }
  249. }
  250. if (s[top].dir < 2) {
  251. dir = s[top].dir;
  252. s[top].dir++;
  253. if (!s[top].n->child[dir] && s[top].dir < 2) {
  254. dir = s[top].dir;
  255. s[top].dir++;
  256. }
  257. if (s[top].n->child[dir]) {
  258. n = s[top].n;
  259. top++;
  260. s[top].n = n->child[dir];
  261. s[top].dir = 0;
  262. s[top].v = 0;
  263. top++;
  264. }
  265. }
  266. count++;
  267. }
  268. if (level < 2) {
  269. G_debug(1, "k-d tree optimization for %zd items:", t->count);
  270. G_debug(1, "%d steps, %d times balanced", count, bal);
  271. return;
  272. }
  273. /* bottom-up balancing */
  274. /* go down */
  275. top = 0;
  276. s[top].n = t->root;
  277. while (s[top].n) {
  278. n = s[top].n;
  279. s[top].dir = 0;
  280. s[top].v = 0;
  281. top++;
  282. s[top].n = n->child[0];
  283. }
  284. /* traverse */
  285. while (top) {
  286. top--;
  287. if (s[top].dir == 0) {
  288. s[top].dir = 1;
  289. /* go down the other side */
  290. top++;
  291. s[top].n = n->child[1];
  292. while (s[top].n) {
  293. n = s[top].n;
  294. s[top].dir = 0;
  295. s[top].v = 0;
  296. top++;
  297. s[top].n = n->child[0];
  298. }
  299. }
  300. else {
  301. n = s[top].n;
  302. while (kdtree_balance(t, n))
  303. bal++;
  304. }
  305. count++;
  306. }
  307. G_debug(1, "k-d tree optimization for %zd items:", t->count);
  308. G_debug(1, "%d steps, %d times balanced", count, bal);
  309. }
  310. /* find k nearest neighbors
  311. * results are stored in uid (uids) and d (distances)
  312. * optionally an uid to be skipped can be given
  313. * useful when searching for the nearest neighbor of an item
  314. * that is also in the tree */
  315. int kdtree_knn(struct kdtree *t, double *c, int *uid, double *d, int k, int *skip)
  316. {
  317. int i, found;
  318. double diff, dist, maxdist;
  319. struct kdnode sn, *n;
  320. struct kdstack {
  321. struct kdnode *n;
  322. int dir;
  323. char v;
  324. } s[256];
  325. int dir;
  326. int top;
  327. if (!t->root)
  328. return 0;
  329. sn.c = c;
  330. sn.uid = (int)0x80000000;
  331. if (skip)
  332. sn.uid = *skip;
  333. maxdist = 1.0 / 0.0;
  334. found = 0;
  335. /* go down */
  336. top = 0;
  337. s[top].n = t->root;
  338. while (s[top].n) {
  339. n = s[top].n;
  340. dir = cmp(&sn, n, n->dim) > 0;
  341. s[top].dir = dir;
  342. s[top].v = 0;
  343. top++;
  344. s[top].n = n->child[dir];
  345. }
  346. /* go back up */
  347. while (top) {
  348. top--;
  349. if (!s[top].v) {
  350. s[top].v = 1;
  351. n = s[top].n;
  352. if (n->uid != sn.uid) {
  353. dist = 0;
  354. for (i = 0; i < t->ndims; i++) {
  355. diff = sn.c[i] - n->c[i];
  356. dist += diff * diff;
  357. if (found == k && dist > maxdist)
  358. break;
  359. }
  360. if (found < k) {
  361. i = found;
  362. while (i > 0 && d[i - 1] > dist) {
  363. d[i] = d[i - 1];
  364. uid[i] = uid[i - 1];
  365. i--;
  366. }
  367. if (d[i] == dist && uid[i] == n->uid)
  368. G_fatal_error("knn: inserting duplicate");
  369. d[i] = dist;
  370. uid[i] = n->uid;
  371. maxdist = d[found];
  372. found++;
  373. }
  374. else if (dist < maxdist) {
  375. i = k - 1;
  376. while (i > 0 && d[i - 1] > dist) {
  377. d[i] = d[i - 1];
  378. uid[i] = uid[i - 1];
  379. i--;
  380. }
  381. if (d[i] == dist && uid[i] == n->uid)
  382. G_fatal_error("knn: inserting duplicate");
  383. d[i] = dist;
  384. uid[i] = n->uid;
  385. maxdist = d[k - 1];
  386. }
  387. if (found == k && maxdist == 0.0)
  388. break;
  389. }
  390. /* look on the other side ? */
  391. dir = s[top].dir;
  392. diff = sn.c[(int)n->dim] - n->c[(int)n->dim];
  393. dist = diff * diff;
  394. if (dist <= maxdist) {
  395. /* go down the other side */
  396. top++;
  397. s[top].n = n->child[!dir];
  398. while (s[top].n) {
  399. n = s[top].n;
  400. dir = cmp(&sn, n, n->dim) > 0;
  401. s[top].dir = dir;
  402. s[top].v = 0;
  403. top++;
  404. s[top].n = n->child[dir];
  405. }
  406. }
  407. }
  408. }
  409. for (i = 0; i < found; i++)
  410. d[i] = sqrt(d[i]);
  411. return found;
  412. }
  413. /* find all nearest neighbors within distance aka radius search
  414. * results are stored in puid (uids) and pd (distances)
  415. * memory is allocated as needed, the calling fn must free the memory
  416. * optionally an uid to be skipped can be given */
  417. int kdtree_dnn(struct kdtree *t, double *c, int **puid, double **pd,
  418. double maxdist, int *skip)
  419. {
  420. int i, k, found;
  421. double diff, dist;
  422. struct kdnode sn, *n;
  423. struct kdstack {
  424. struct kdnode *n;
  425. int dir;
  426. char v;
  427. } s[256];
  428. int dir;
  429. int top;
  430. int *uid;
  431. double *d, maxdistsq;
  432. if (!t->root)
  433. return 0;
  434. sn.c = c;
  435. sn.uid = (int)0x80000000;
  436. if (skip)
  437. sn.uid = *skip;
  438. *pd = NULL;
  439. *puid = NULL;
  440. k = 0;
  441. uid = NULL;
  442. d = NULL;
  443. found = 0;
  444. maxdistsq = maxdist * maxdist;
  445. /* go down */
  446. top = 0;
  447. s[top].n = t->root;
  448. while (s[top].n) {
  449. n = s[top].n;
  450. dir = cmp(&sn, n, n->dim) > 0;
  451. s[top].dir = dir;
  452. s[top].v = 0;
  453. top++;
  454. s[top].n = n->child[dir];
  455. }
  456. /* go back up */
  457. while (top) {
  458. top--;
  459. if (!s[top].v) {
  460. s[top].v = 1;
  461. n = s[top].n;
  462. if (n->uid != sn.uid) {
  463. dist = 0;
  464. for (i = 0; i < t->ndims; i++) {
  465. diff = sn.c[i] - n->c[i];
  466. dist += diff * diff;
  467. if (dist > maxdistsq)
  468. break;
  469. }
  470. if (dist <= maxdistsq) {
  471. if (found + 1 >= k) {
  472. k += 10;
  473. uid = G_realloc(uid, k * sizeof(int));
  474. d = G_realloc(d, k * sizeof(double));
  475. }
  476. i = found;
  477. while (i > 0 && d[i - 1] > dist) {
  478. d[i] = d[i - 1];
  479. uid[i] = uid[i - 1];
  480. i--;
  481. }
  482. if (i < found && d[i] == dist && uid[i] == n->uid)
  483. G_fatal_error("dnn: inserting duplicate");
  484. d[i] = dist;
  485. uid[i] = n->uid;
  486. found++;
  487. }
  488. }
  489. /* look on the other side ? */
  490. dir = s[top].dir;
  491. diff = fabs(sn.c[(int)n->dim] - n->c[(int)n->dim]);
  492. if (diff <= maxdist) {
  493. /* go down the other side */
  494. top++;
  495. s[top].n = n->child[!dir];
  496. while (s[top].n) {
  497. n = s[top].n;
  498. dir = cmp(&sn, n, n->dim) > 0;
  499. s[top].dir = dir;
  500. s[top].v = 0;
  501. top++;
  502. s[top].n = n->child[dir];
  503. }
  504. }
  505. }
  506. }
  507. for (i = 0; i < found; i++)
  508. d[i] = sqrt(d[i]);
  509. *pd = d;
  510. *puid = uid;
  511. return found;
  512. }
  513. /**********************************************/
  514. /* internal functions */
  515. /**********************************************/
  516. static int kdtree_replace(struct kdtree *t, struct kdnode *r)
  517. {
  518. double mindist;
  519. int rdir, ordir, dir;
  520. int ld, rd, old_depth;
  521. struct kdnode *n, *rn, *or;
  522. struct kdstack {
  523. struct kdnode *n;
  524. int dir;
  525. char v;
  526. } s[256];
  527. int top, top2;
  528. int is_leaf;
  529. int nr;
  530. /* find replacement for r
  531. * overwrite r, delete replacement */
  532. nr = 0;
  533. /* pick a subtree */
  534. rdir = 1;
  535. or = r;
  536. ld = (!or->child[0] ? -1 : or->child[0]->depth);
  537. rd = (!or->child[1] ? -1 : or->child[1]->depth);
  538. if (ld > rd) {
  539. rdir = 0;
  540. }
  541. if (!or->child[rdir]) {
  542. /* no replacement, delete */
  543. kdtree_free_node(r);
  544. return nr;
  545. }
  546. /* replace old root, make replacement the new root
  547. * repeat until replacement is leaf */
  548. ordir = rdir;
  549. is_leaf = 0;
  550. s[0].n = or;
  551. s[0].dir = ordir;
  552. top2 = 1;
  553. mindist = -1;
  554. while (!is_leaf) {
  555. rn = NULL;
  556. /* find replacement for old root */
  557. top = top2;
  558. s[top].n = or->child[ordir];
  559. n = s[top].n;
  560. rn = n;
  561. mindist = or->c[(int)or->dim] - n->c[(int)or->dim];
  562. if (ordir)
  563. mindist = -mindist;
  564. /* go down */
  565. while (s[top].n) {
  566. n = s[top].n;
  567. dir = !ordir;
  568. if (n->dim != or->dim)
  569. dir = cmp(or, n, n->dim) > 0;
  570. s[top].dir = dir;
  571. s[top].v = 0;
  572. top++;
  573. s[top].n = n->child[dir];
  574. }
  575. /* go back up */
  576. while (top > top2) {
  577. top--;
  578. if (!s[top].v) {
  579. s[top].v = 1;
  580. n = s[top].n;
  581. if ((cmp(rn, n, or->dim) > 0) == ordir) {
  582. rn = n;
  583. mindist = or->c[(int)or->dim] - n->c[(int)or->dim];
  584. if (ordir)
  585. mindist = -mindist;
  586. }
  587. /* look on the other side ? */
  588. dir = s[top].dir;
  589. if (n->dim != or->dim &&
  590. mindist >= fabs(n->c[(int)n->dim] - n->c[(int)n->dim])) {
  591. /* go down the other side */
  592. top++;
  593. s[top].n = n->child[!dir];
  594. while (s[top].n) {
  595. n = s[top].n;
  596. dir = !ordir;
  597. if (n->dim != or->dim)
  598. dir = cmp(or, n, n->dim) > 0;
  599. s[top].dir = dir;
  600. s[top].v = 0;
  601. top++;
  602. s[top].n = n->child[dir];
  603. }
  604. }
  605. }
  606. }
  607. #ifdef KD_DEBUG
  608. if (!rn)
  609. G_fatal_error("No replacement");
  610. if (ordir && or->c[(int)or->dim] > rn->c[(int)or->dim])
  611. G_fatal_error("rn is smaller");
  612. if (!ordir && or->c[(int)or->dim] < rn->c[(int)or->dim])
  613. G_fatal_error("rn is larger");
  614. if (or->child[1]) {
  615. dir = cmp(or->child[1], rn, or->dim);
  616. if (dir < 0) {
  617. int i;
  618. for (i = 0; i < t->ndims; i++)
  619. G_message("rn c %g, or child c %g",
  620. rn->c[i], or->child[1]->c[i]);
  621. G_fatal_error("Right child of old root is smaller than rn, dir is %d", ordir);
  622. }
  623. }
  624. if (or->child[0]) {
  625. dir = cmp(or->child[0], rn, or->dim);
  626. if (dir > 0) {
  627. int i;
  628. for (i = 0; i < t->ndims; i++)
  629. G_message("rn c %g, or child c %g",
  630. rn->c[i], or->child[0]->c[i]);
  631. G_fatal_error("Left child of old root is larger than rn, dir is %d", ordir);
  632. }
  633. }
  634. #endif
  635. is_leaf = (rn->child[0] == NULL && rn->child[1] == NULL);
  636. #ifdef KD_DEBUG
  637. if (is_leaf && rn->depth != 0)
  638. G_fatal_error("rn is leaf but depth is %d", (int)rn->depth);
  639. if (!is_leaf && rn->depth <= 0)
  640. G_fatal_error("rn is not leaf but depth is %d", (int)rn->depth);
  641. #endif
  642. nr++;
  643. /* go to replacement from or->child[ordir] */
  644. top = top2;
  645. dir = 1;
  646. while (dir) {
  647. n = s[top].n;
  648. dir = cmp(rn, n, n->dim);
  649. if (dir) {
  650. s[top].dir = dir > 0;
  651. top++;
  652. s[top].n = n->child[dir > 0];
  653. if (!s[top].n) {
  654. G_fatal_error("(Last) replacement disappeared %d", nr);
  655. }
  656. }
  657. }
  658. #ifdef KD_DEBUG
  659. if (s[top].n != rn)
  660. G_fatal_error("rn is unreachable from or");
  661. #endif
  662. top2 = top;
  663. s[top2 + 1].n = NULL;
  664. /* copy replacement to old root */
  665. memcpy(or->c, rn->c, t->csize);
  666. or->uid = rn->uid;
  667. if (!is_leaf) {
  668. /* make replacement the old root */
  669. or = rn;
  670. /* pick a subtree */
  671. ordir = 1;
  672. ld = (!or->child[0] ? -1 : or->child[0]->depth);
  673. rd = (!or->child[1] ? -1 : or->child[1]->depth);
  674. if (ld > rd) {
  675. ordir = 0;
  676. }
  677. s[top2].dir = ordir;
  678. top2++;
  679. }
  680. }
  681. if (!rn)
  682. G_fatal_error("No replacement at all");
  683. /* delete last replacement */
  684. top = top2 - 1;
  685. n = s[top].n;
  686. dir = 0;
  687. if (n->child[dir] != rn) {
  688. dir = !dir;
  689. }
  690. if (n->child[dir] != rn) {
  691. G_fatal_error("Last replacement disappeared");
  692. }
  693. n->child[dir] = NULL;
  694. kdtree_free_node(rn);
  695. t->count--;
  696. old_depth = n->depth;
  697. n->depth = (!n->child[!dir] ? 0 : n->child[!dir]->depth + 1);
  698. if (n->depth == old_depth)
  699. top = 0;
  700. #ifdef KD_DEBUG
  701. top = top2;
  702. #endif
  703. /* go back up */
  704. while (top) {
  705. top--;
  706. n = s[top].n;
  707. #ifdef KD_DEBUG
  708. /* debug directions */
  709. if (n->child[0]) {
  710. if (cmp(n->child[0], n, n->dim) > 0)
  711. G_warning("Left child is larger");
  712. }
  713. if (n->child[1]) {
  714. if (cmp(n->child[1], n, n->dim) < 1)
  715. G_warning("Right child is not larger");
  716. }
  717. #endif
  718. /* update depth */
  719. ld = (!n->child[0] ? -1 : n->child[0]->depth);
  720. rd = (!n->child[1] ? -1 : n->child[1]->depth);
  721. n->depth = MAX(ld, rd) + 1;
  722. }
  723. return nr;
  724. }
  725. static int kdtree_balance(struct kdtree *t, struct kdnode *r)
  726. {
  727. struct kdnode *or;
  728. int dir;
  729. int rd, ld;
  730. if (!r)
  731. return 0;
  732. /* subtree difference */
  733. dir = -1;
  734. ld = (!r->child[0] ? -1 : r->child[0]->depth);
  735. rd = (!r->child[1] ? -1 : r->child[1]->depth);
  736. if (ld > rd + t->btol) {
  737. dir = 0;
  738. }
  739. else if (rd > ld + t->btol) {
  740. dir = 1;
  741. }
  742. else
  743. return 0;
  744. or = kdtree_newnode(t);
  745. memcpy(or->c, r->c, t->csize);
  746. or->uid = r->uid;
  747. or->dim = t->nextdim[r->dim];
  748. if (!kdtree_replace(t, r))
  749. G_fatal_error("kdtree_balance: nothing replaced");
  750. #ifdef KD_DEBUG
  751. if (!cmp(r, or, r->dim)) {
  752. G_warning("kdtree_balance: replacement failed");
  753. kdtree_free_node(or);
  754. return 0;
  755. }
  756. #endif
  757. r->child[!dir] = kdtree_insert2(t, r->child[!dir], or, 0, 1);
  758. /* update node depth */
  759. ld = r->child[0]->depth;
  760. rd = r->child[1]->depth;
  761. r->depth = MAX(ld, rd) + 1;
  762. return 1;
  763. }
  764. static struct kdnode *kdtree_insert2(struct kdtree *t, struct kdnode *r,
  765. struct kdnode *nnew, int balance, int dc)
  766. {
  767. struct kdnode *n;
  768. struct kdstack {
  769. struct kdnode *n;
  770. int dir;
  771. } s[256];
  772. int top;
  773. int dir;
  774. int ld, rd;
  775. int old_depth;
  776. int go_back;
  777. if (!r) {
  778. r = nnew;
  779. t->count++;
  780. return r;
  781. }
  782. if (balance) {
  783. /* balance root */
  784. while (kdtree_balance(t, r));
  785. }
  786. /* find node with free child */
  787. top = 0;
  788. go_back = 0;
  789. s[top].n = r;
  790. while (s[top].n) {
  791. n = s[top].n;
  792. if (!cmpc(nnew, n, t) && (!dc || nnew->uid == n->uid)) {
  793. G_debug(1, "KD node exists already, nothing to do");
  794. kdtree_free_node(nnew);
  795. return r;
  796. }
  797. dir = cmp(nnew, n, n->dim) > 0;
  798. s[top].dir = dir;
  799. if (balance) {
  800. /* balance left subtree */
  801. while (kdtree_balance(t, n->child[0]));
  802. /* balance right subtree */
  803. while (kdtree_balance(t, n->child[1]));
  804. /* update node depth */
  805. old_depth = n->depth;
  806. ld = (!n->child[0] ? -1 : n->child[0]->depth);
  807. rd = (!n->child[1] ? -1 : n->child[1]->depth);
  808. n->depth = MAX(ld, rd) + 1;
  809. if (old_depth != n->depth)
  810. go_back = top;
  811. }
  812. top++;
  813. if (top > 255)
  814. G_fatal_error("depth too large: %d", top);
  815. s[top].n = n->child[dir];
  816. }
  817. /* insert to child pointer of parent */
  818. top--;
  819. n = s[top].n;
  820. dir = s[top].dir;
  821. n->child[dir] = nnew;
  822. nnew->dim = t->nextdim[n->dim];
  823. t->count++;
  824. old_depth = n->depth;
  825. n->depth = (!n->child[!dir] ? 1 : n->child[!dir]->depth + 1);
  826. if (balance) {
  827. /* balance root */
  828. while (kdtree_balance(t, n));
  829. }
  830. if (old_depth != n->depth)
  831. go_back = top;
  832. /* go back up */
  833. #ifdef KD_DEBUG
  834. go_back = top;
  835. #endif
  836. top = go_back;
  837. while (top) {
  838. top--;
  839. n = s[top].n;
  840. #ifdef KD_DEBUG
  841. /* debug directions */
  842. if (n->child[0]) {
  843. if (cmp(n->child[0], n, n->dim) > 0)
  844. G_warning("Insert2: Left child is larger");
  845. }
  846. if (n->child[1]) {
  847. if (cmp(n->child[1], n, n->dim) < 1)
  848. G_warning("Insert2: Right child is not larger");
  849. }
  850. #endif
  851. /* update node depth */
  852. ld = (!n->child[0] ? -1 : n->child[0]->depth);
  853. rd = (!n->child[1] ? -1 : n->child[1]->depth);
  854. n->depth = MAX(ld, rd) + 1;
  855. }
  856. return r;
  857. }