29 static int rcalls = 0;
30 static int rcallsmax = 0;
33 struct kdnode *,
int,
int);
34 static int kdtree_replace(
struct kdtree *,
struct kdnode *);
35 static int kdtree_balance(
struct kdtree *,
struct kdnode *,
int);
36 static int kdtree_first(
struct kdtrav *,
double *,
int *);
37 static int kdtree_next(
struct kdtrav *,
double *,
int *);
41 if (a->
c[p] <
b->c[p])
43 if (a->
c[p] >
b->c[p])
46 return (a->
uid <
b->uid ? -1 : a->
uid >
b->uid);
53 for (i = 0; i <
t->ndims; i++) {
54 if (a->
c[i] !=
b->c[i]) {
77 static void kdtree_free_node(
struct kdnode *n)
83 static void kdtree_update_node(
struct kdtree *
t,
struct kdnode *n)
105 if (ld > rd + btol || rd > ld + btol)
119 t->csize =
ndims *
sizeof(double);
128 for (i = 0; i <
ndims - 1; i++)
129 t->nextdim[i] = i + 1;
130 t->nextdim[
ndims - 1] = 0;
149 while ((it = save) !=
NULL) {
153 kdtree_free_node(it);
184 nnew = kdtree_newnode(
t);
185 memcpy(nnew->
c,
c,
t->csize);
188 t->root = kdtree_insert2(
t,
t->root, nnew, 1, dc);
223 found = (!cmpc(&sn, n,
t) && sn.
uid == n->
uid);
225 dir = cmp(&sn, n, n->
dim) > 0;
228 s[top].n = n->
child[dir];
238 if (s[top].n->
depth == 0) {
239 kdtree_free_node(s[top].n);
245 n->child[dir] =
NULL;
248 kdtree_update_node(
t, n);
257 kdtree_replace(
t, s[top].n);
264 kdtree_update_node(
t, n);
286 while (kdtree_balance(
t, n, bmode))
294 s[top].n = n->
child[dir];
299 s[top].n = n->
child[dir];
306 kdtree_update_node(
t, n);
308 while (kdtree_balance(
t, n, bmode))
313 kdtree_update_node(
t, s[top].n);
315 if (!bmode2 && top == 0) {
351 G_debug(1,
"k-d tree optimization for %zd items, tree depth %d",
t->count,
364 while (kdtree_balance(
t, n->
child[0], level))
367 while (kdtree_balance(
t, n->
child[1], level))
377 s[top].n = n->
child[dir];
385 while (kdtree_balance(
t, n, level)) {
388 while (kdtree_balance(
t, n->
child[0], level))
390 while (kdtree_balance(
t, n->
child[1], level))
397 while (kdtree_balance(
t, n, level)) {
406 while (kdtree_balance(
t, n, level)) {
409 while (kdtree_balance(
t, n->
child[0], level))
411 while (kdtree_balance(
t, n->
child[1], level))
418 while (kdtree_balance(
t, n, level)) {
428 s[top].n = n->
child[dir];
448 while (kdtree_balance(
t, n, level)) {
451 while (kdtree_balance(
t, n->child[0], level))
453 while (kdtree_balance(
t, n->child[1], level))
456 ld = (!n->child[0] ? -1 : n->child[0]->depth);
457 rd = (!n->child[1] ? -1 : n->child[1]->depth);
458 n->depth =
MAX(ld, rd) + 1;
460 while (kdtree_balance(
t, n, level)) {
484 dir = (diffr > diffl);
487 s[top].n = n->
child[dir];
495 ld = (!n->child[0] ? -1 : n->child[0]->depth);
496 rd = (!n->child[1] ? -1 : n->child[1]->depth);
501 G_debug(1,
"k-d tree optimization: %d times balanced, new depth %d", nbal,
516 double diff, dist, maxdist;
530 sn.
uid = (int)0x80000000;
542 dir = cmp(&sn, n, n->
dim) > 0;
546 s[top].n = n->
child[dir];
562 diff = sn.
c[i] - n->
c[i];
568 while (i > 0 && d[i - 1] > dist) {
573 if (i < found && d[i] == dist &&
uid[i] == n->
uid)
584 diff = sn.
c[i] - n->
c[i];
587 }
while (i-- && dist <= maxdist);
589 if (dist < maxdist) {
591 while (i > 0 && d[i - 1] > dist) {
596 if (d[i] == dist &&
uid[i] == n->
uid)
604 if (found == k && maxdist == 0.0)
610 diff = sn.
c[(int)n->
dim] - n->
c[(
int)n->
dim];
613 if (dist <= maxdist) {
616 s[top].n = n->
child[!dir];
619 dir = cmp(&sn, n, n->
dim) > 0;
623 s[top].n = n->child[dir];
637 double maxdist,
int *skip)
650 double *d, maxdistsq;
656 sn.
uid = (int)0x80000000;
668 maxdistsq = maxdist * maxdist;
675 dir = cmp(&sn, n, n->
dim) > 0;
679 s[top].n = n->
child[dir];
694 diff = sn.
c[i] - n->
c[i];
697 }
while (i-- && dist <= maxdistsq);
699 if (dist <= maxdistsq) {
700 if (found + 1 >= k) {
706 while (i > 0 && d[i - 1] > dist) {
711 if (i < found && d[i] == dist &&
uid[i] == n->
uid)
722 diff = fabs(sn.
c[(
int)n->
dim] - n->
c[(
int)n->
dim]);
723 if (diff <= maxdist) {
726 s[top].n = n->
child[!dir];
729 dir = cmp(&sn, n, n->
dim) > 0;
733 s[top].n = n->child[dir];
753 int i, k, found, inside;
768 sn.
uid = (int)0x80000000;
784 dir = cmp(&sn, n, n->
dim) > 0;
788 s[top].n = n->
child[dir];
801 for (i = 0; i <
t->ndims; i++) {
802 if (n->
c[i] < sn.
c[i] || n->
c[i] > sn.
c[i +
t->ndims]) {
809 if (found + 1 >= k) {
821 if (n->
c[(
int)n->
dim] >= sn.
c[(
int)n->
dim] &&
822 n->
c[(
int)n->
dim] <= sn.
c[(
int)n->
dim +
t->ndims]) {
825 s[top].n = n->
child[!dir];
828 dir = cmp(&sn, n, n->
dim) > 0;
832 s[top].n = n->child[dir];
866 G_debug(1,
"k-d tree: empty tree");
868 G_debug(1,
"k-d tree: finished traversing");
875 return kdtree_first(trav,
c,
uid);
878 return kdtree_next(trav,
c,
uid);
889 int rdir, ordir, dir;
891 struct kdnode *n, *rn, * or ;
903 if (!
r->child[0] && !
r->child[1])
937 s[top].n = or->
child[ordir];
941 mindist = or->
c[(int) or->
dim] - n->
c[(
int) or->
dim];
949 if (n->dim != or->
dim)
950 dir = cmp(or, n, n->dim) > 0;
954 s[top].n = n->child[dir];
964 if ((cmp(rn, n, or->
dim) > 0) == ordir) {
966 mindist = or->
c[(int) or->
dim] - n->c[(
int) or->
dim];
973 if (n->dim != or->
dim && mindist >= fabs(n->c[(
int)n->dim] -
974 n->c[(
int)n->dim])) {
977 s[top].n = n->child[!dir];
981 if (n->dim != or->
dim)
982 dir = cmp(or, n, n->dim) > 0;
986 s[top].n = n->child[dir];
995 if (ordir && or->
c[(
int) or->
dim] > rn->
c[(
int) or->
dim])
998 if (!ordir && or->
c[(
int) or->
dim] < rn->
c[(
int) or->
dim])
1002 dir = cmp(or->
child[1], rn, or->
dim);
1006 for (i = 0; i <
t->ndims; i++)
1007 G_message(
"rn c %g, or child c %g", rn->
c[i],
1010 "Right child of old root is smaller than rn, dir is %d",
1015 dir = cmp(or->
child[0], rn, or->
dim);
1019 for (i = 0; i <
t->ndims; i++)
1020 G_message(
"rn c %g, or child c %g", rn->
c[i],
1023 "Left child of old root is larger than rn, dir is %d",
1032 if (is_leaf && rn->
depth != 0)
1034 if (!is_leaf && rn->
depth <= 0)
1045 dir = cmp(rn, n, n->dim);
1047 s[top].dir = dir > 0;
1049 s[top].n = n->child[dir > 0];
1063 s[top2 + 1].n =
NULL;
1066 memcpy(or->
c, rn->
c,
t->csize);
1080 s[top2].dir = ordir;
1089 if (s[top2].n != rn) {
1095 if (n->child[dir] != rn) {
1098 kdtree_free_node(rn);
1099 n->child[dir] =
NULL;
1102 kdtree_update_node(
t, n);
1113 if (cmp(n->child[0], n, n->dim) > 0)
1117 if (cmp(n->child[1], n, n->dim) < 1)
1123 kdtree_update_node(
t, n);
1129 static int kdtree_balance(
struct kdtree *
t,
struct kdnode *
r,
int bmode)
1141 ld = (!
r->child[0] ? -1 :
r->child[0]->depth);
1142 rd = (!
r->child[1] ? -1 :
r->child[1]->depth);
1143 old_depth =
MAX(ld, rd) + 1;
1145 if (old_depth !=
r->depth) {
1146 G_warning(
"balancing: depth is wrong: %d != %d",
r->depth, old_depth);
1147 kdtree_update_node(
t,
r);
1152 if (!
r->child[0] || !
r->child[1])
1155 ld = (!
r->child[0] ? -1 :
r->child[0]->depth);
1156 rd = (!
r->child[1] ? -1 :
r->child[1]->depth);
1157 if (ld > rd + btol) {
1160 else if (rd > ld + btol) {
1167 or = kdtree_newnode(
t);
1168 memcpy(or->
c,
r->c,
t->csize);
1170 or->
dim =
t->nextdim[
r->dim];
1172 if (!kdtree_replace(
t,
r))
1176 if (!cmp(
r, or,
r->dim)) {
1177 G_warning(
"kdtree_balance: replacement failed");
1178 kdtree_free_node(or);
1185 kdtree_insert2(
t,
r->child[!dir], or, bmode, 1);
1188 kdtree_update_node(
t,
r);
1190 if (
r->depth == old_depth) {
1191 G_debug(4,
"balancing had no effect");
1195 if (
r->depth > old_depth)
1222 if (rcallsmax < rcalls)
1240 if (!cmpc(nnew, n,
t) && (!dc || nnew->
uid == n->uid)) {
1242 G_debug(1,
"KD node exists already, nothing to do");
1243 kdtree_free_node(nnew);
1252 dir = cmp(nnew, n, n->dim) > 0;
1258 s[top].n = n->child[dir];
1266 n->child[dir] = nnew;
1267 nnew->
dim =
t->nextdim[n->dim];
1279 kdtree_update_node(
t, n);
1286 if (cmp(n->child[0], n, n->dim) > 0)
1287 G_warning(
"Insert2: Left child is larger");
1290 if (cmp(n->child[1], n, n->dim) < 1)
1291 G_warning(
"Insert2: Right child is not larger");
1311 while (kdtree_balance(
t, n, bmode))
1316 if (n->child[0] && n->child[0]->balance) {
1319 s[top].n = n->child[dir];
1321 else if (n->child[1] && n->child[1]->balance) {
1324 s[top].n = n->child[dir];
1332 while (kdtree_balance(
t, n, bmode))
1337 kdtree_update_node(
t, s[top].n);
1339 if (!bmode2 && top == 0) {
1360 static int kdtree_first(
struct kdtrav *trav,
double *
c,
int *
uid)
1377 static int kdtree_next(
struct kdtrav *trav,
double *
c,
int *
uid)
1395 if (trav->
top == 0) {
void G_free(void *)
Free allocated memory.
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_warning(const char *,...) __attribute__((format(printf
void G_message(const char *,...) __attribute__((format(printf
int G_debug(int, const char *,...) __attribute__((format(printf
void kdtree_optimize(struct kdtree *t, int level)
int kdtree_rnn(struct kdtree *t, double *c, int **puid, int *skip)
struct kdtree * kdtree_create(char ndims, int *btol)
int kdtree_traverse(struct kdtrav *trav, double *c, int *uid)
void kdtree_clear(struct kdtree *t)
int kdtree_insert(struct kdtree *t, double *c, int uid, int dc)
int kdtree_knn(struct kdtree *t, double *c, int *uid, double *d, int k, int *skip)
void kdtree_destroy(struct kdtree *t)
int kdtree_dnn(struct kdtree *t, double *c, int **puid, double **pd, double maxdist, int *skip)
int kdtree_init_trav(struct kdtrav *trav, struct kdtree *tree)
int kdtree_remove(struct kdtree *t, double *c, int uid)
Dynamic balanced k-d tree implementation.
struct kdnode * curr_node