summaryrefslogtreecommitdiffstats
path: root/kd-forest.c
blob: 5b230bee6e2bc39d2071c42c66dd0bcc25709467 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*********************************************************************
 * kd-forest                                                         *
 * Copyright (C) 2014 Tavian Barnes <tavianator@tavianator.com>      *
 *                                                                   *
 * This program is free software. It comes without any warranty, to  *
 * the extent permitted by applicable law. You can redistribute it   *
 * and/or modify it under the terms of the Do What The Fuck You Want *
 * To Public License, Version 2, as published by Sam Hocevar. See    *
 * the COPYING file or http://www.wtfpl.net/ for more details.       *
 *********************************************************************/

#include "kd-forest.h"
#include "util.h"
#include <math.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

void
kd_node_init(kd_node_t *node, unsigned int x, unsigned int y)
{
  node->left = node->right = NULL;
  node->x = x;
  node->y = y;
  node->added = node->removed = false;
}

static size_t
kd_collect_nodes(kd_node_t *root, kd_node_t **buffer, bool include_removed)
{
  size_t count = 0;
  if (include_removed || !root->removed) {
    buffer[0] = root;
    ++count;
  }
  if (root->left) {
    count += kd_collect_nodes(root->left, buffer + count, include_removed);
  }
  if (root->right) {
    count += kd_collect_nodes(root->right, buffer + count, include_removed);
  }
  return count;
}

typedef int kd_comparator(const void *a, const void* b);

static int kd_compare0(const void *a, const void *b) {
  double aval = (*(const kd_node_t **)a)->coords[0];
  double bval = (*(const kd_node_t **)b)->coords[0];
  return (aval > bval) - (aval < bval);
}

static int kd_compare1(const void *a, const void *b) {
  double aval = (*(const kd_node_t **)a)->coords[1];
  double bval = (*(const kd_node_t **)b)->coords[1];
  return (aval > bval) - (aval < bval);
}

static int kd_compare2(const void *a, const void *b) {
  double aval = (*(const kd_node_t **)a)->coords[2];
  double bval = (*(const kd_node_t **)b)->coords[2];
  return (aval > bval) - (aval < bval);
}

static kd_comparator *kd_comparators[KD_DIMEN] = {
  kd_compare0,
  kd_compare1,
  kd_compare2,
};

// When building k-d trees, we use KD_DIMEN sorted arrays of nodes plus one
// extra array for scratch space
#define KD_BUFSIZE (KD_DIMEN + 1)

static kd_node_t *
kd_build_tree_recursive(kd_node_t **buffers[KD_BUFSIZE], size_t size, unsigned int coord)
{
  if (size == 0) {
    return NULL;
  }

  size_t split = size/2;
  size_t left_size = split, right_size = size - left_size - 1;
  kd_node_t *root = buffers[coord][split];
  for (size_t i = 0; i < size; ++i) {
    buffers[coord][i]->is_left = i < left_size;
  }

  kd_node_t **right_buffers[KD_BUFSIZE];
  for (int i = 0; i < KD_DIMEN; ++i) {
    right_buffers[i] = buffers[i] + left_size + 1;
  }

  kd_node_t **scratch = buffers[KD_DIMEN];
  right_buffers[KD_DIMEN] = scratch;

  for (size_t i = 0; i < KD_DIMEN; ++i) {
    if (i == coord) {
      continue;
    }

    kd_node_t **buffer = buffers[i];
    kd_node_t **right_buffer = right_buffers[i];
    for (size_t j = 0, k = 0, skip = 0; j < size; ++j) {
      if (buffer[j]->is_left) {
        buffer[j - skip] = buffer[j];
      } else {
        if (buffer[j] != root) {
          scratch[k] = buffer[j];
          ++k;
        }
        ++skip;
      }
    }
    for (size_t j = 0; j < right_size; ++j) {
      right_buffer[j] = scratch[j];
    }
  }

  coord = (coord + 1)%KD_DIMEN;
  root->left = kd_build_tree_recursive(buffers, left_size, coord);
  root->right = kd_build_tree_recursive(right_buffers, right_size, coord);

  return root;
}

static kd_node_t *
kd_build_tree(kd_node_t **buffers[KD_BUFSIZE], size_t size)
{
  for (int i = 1; i < KD_DIMEN; ++i) {
    memcpy(buffers[i], buffers[0], size*sizeof(kd_node_t *));
  }
  for (int i = 0; i < KD_DIMEN; ++i) {
    qsort(buffers[i], size, sizeof(kd_node_t *), kd_comparators[i]);
  }
  return kd_build_tree_recursive(buffers, size, 0);
}

static double
kd_distance_sq(kd_node_t *a, kd_node_t *b)
{
  double result = 0.0;
  for (int i = 0; i < KD_DIMEN; ++i) {
    double d = a->coords[i] - b->coords[i];
    result += d*d;
  }
  return result;
}

static void
kd_find_nearest_recursive(kd_node_t *root, kd_node_t *target, kd_node_t **best, double *limit, unsigned int coord)
{
  double dist = target->coords[coord] - root->coords[coord];
  double dist_sq = dist*dist;

  if (!root->removed) {
    double root_dist_sq = kd_distance_sq(root, target);
    if (root_dist_sq < *limit) {
      *best = root;
      *limit = root_dist_sq;
    }
  }

  coord = (coord + 1)%KD_DIMEN;

  if (root->left && (dist <= 0 || dist_sq <= *limit)) {
    kd_find_nearest_recursive(root->left, target, best, limit, coord);
  }
  if (root->right && (dist >= 0 || dist_sq <= *limit)) {
    kd_find_nearest_recursive(root->right, target, best, limit, coord);
  }
}

static void
kd_find_nearest(kd_node_t *root, kd_node_t *target, kd_node_t **best, double *limit)
{
  kd_find_nearest_recursive(root, target, best, limit, 0);
}

void
kdf_init(kd_forest_t *kdf)
{
  kdf->roots = NULL;
  kdf->size = kdf->size_est = 0;
  kdf->roots_size = kdf->roots_capacity = 0;
}

void
kdf_destroy(kd_forest_t *kdf)
{
  free(kdf->roots);
}

static size_t
kdf_collect_nodes(kd_forest_t *kdf, kd_node_t **buffer, unsigned int slot, bool include_removed)
{
  size_t count = 0;
  for (unsigned int i = 0; i < slot; ++i) {
    if (kdf->roots[i]) {
      count += kd_collect_nodes(kdf->roots[i], buffer + count, include_removed);
    }
  }
  return count;
}

static void
kdf_balance(kd_forest_t *kdf, kd_node_t *new_node, bool force)
{
  ++kdf->size;

  size_t slot, buffer_size;
  if (force) {
    buffer_size = kdf->size_est = kdf->size;
    slot = kdf->roots_size;
  } else {
    ++kdf->size_est;
    for (slot = 0; slot < kdf->roots_size; ++slot) {
      if (!kdf->roots[slot]) {
        break;
      }
    }
    buffer_size = 1 << slot;
  }

  kd_node_t **buffer = xmalloc(buffer_size*sizeof(kd_node_t *));
  buffer[0] = new_node;
  kdf_collect_nodes(kdf, buffer + 1, slot, !force);

  kd_node_t **buffers[KD_BUFSIZE];
  for (int i = 1; i < KD_BUFSIZE; ++i) {
    buffers[i] = xmalloc(buffer_size*sizeof(kd_node_t *));
  }

  if (slot >= kdf->roots_capacity) {
    kdf->roots_capacity = slot + 1;
    kdf->roots = xrealloc(kdf->roots, kdf->roots_capacity*sizeof(kd_node_t *));
  }

  size_t i, offset;
  for (i = 0, offset = 0; offset < buffer_size; ++i) {
    size_t chunk_size = 1 << i;
    if (buffer_size & chunk_size) {
      buffers[0] = buffer + offset;
      kdf->roots[i] = kd_build_tree(buffers, chunk_size);
      offset |= chunk_size;
    } else {
      kdf->roots[i] = NULL;
    }
  }
  if (force || i > kdf->roots_size) {
    kdf->roots_size = i;
  }

  free(buffer);
  for (i = 1; i < KD_BUFSIZE; ++i) {
    free(buffers[i]);
  }
}

void
kdf_insert(kd_forest_t *kdf, kd_node_t *node)
{
  node->added = true;

  // If half or more of the nodes are removed, force a complete rebalance
  bool force = (kdf->size_est + 1) >= 2*(kdf->size + 1);
  kdf_balance(kdf, node, force);
}

void
kdf_remove(kd_forest_t *kdf, kd_node_t *node)
{
  --kdf->size;
  node->removed = true;
}

kd_node_t *
kdf_find_nearest(kd_forest_t *kdf, kd_node_t *target)
{
  double limit = INFINITY;
  kd_node_t *best = NULL;

  for (unsigned int i = 0; i < kdf->roots_size; ++i) {
    kd_node_t *root = kdf->roots[i];
    if (root != NULL) {
      kd_find_nearest(root, target, &best, &limit);
    }
  }

  return best;
}