74 self->size[2] = (10 * M_PI / 180);
79 self->node_max_count = max_size;
103 self->leaf_count = 0;
104 self->node_count = 0;
116 key[0] = floor(pose.
v[0] / self->size[0]);
117 key[1] = floor(pose.
v[1] / self->size[1]);
118 key[2] = floor(pose.
v[2] / self->size[2]);
155 key[0] = floor(pose.
v[0] / self->size[0]);
156 key[1] = floor(pose.
v[1] / self->size[1]);
157 key[2] = floor(pose.
v[2] / self->size[2]);
173 key[0] = floor(pose.
v[0] / self->size[0]);
174 key[1] = floor(pose.
v[1] / self->size[1]);
175 key[2] = floor(pose.
v[2] / self->size[2]);
190 if (key_a[0] != key_b[0])
192 if (key_a[1] != key_b[1])
195 if (key_a[2] != key_b[2])
219 int split, max_split;
224 assert(self->node_count < self->node_max_count);
225 node =
self->nodes +
self->node_count++;
235 for (i = 0; i < 3; i++)
236 node->
key[i] = key[i];
239 self->leaf_count += 1;
248 node->
value += value;
258 for (i = 0; i < 3; i++)
260 split = abs(key[i] - node->
key[i]);
261 if (split > max_split)
283 self->leaf_count -= 1;
361 int queue_count, cluster_count;
365 queue = calloc(self->node_count,
sizeof(queue[0]));
368 for (i = 0; i <
self->node_count; i++)
370 node =
self->nodes + i;
374 assert(queue_count < self->node_count);
375 queue[queue_count++] = node;
385 while (queue_count > 0)
387 node = queue[--queue_count];
394 node->
cluster = cluster_count++;
413 for (i = 0; i < 3 * 3 * 3; i++)
415 nkey[0] = node->
key[0] + (i / 9) - 1;
416 nkey[1] = node->
key[1] + ((i % 9) / 3) - 1;
417 nkey[2] = node->
key[2] + ((i % 9) % 3) - 1;
443 #ifdef INCLUDE_RTKGUI 447 void pf_kdtree_draw(
pf_kdtree_t *
self, rtk_fig_t *fig)
449 if (self->root != NULL)
450 pf_kdtree_draw_node(
self, self->root, fig);
464 ox = (node->
key[0] + 0.5) *
self->size[0];
465 oy = (node->
key[1] + 0.5) *
self->size[1];
467 rtk_fig_rectangle(fig, ox, oy, 0.0, self->size[0], self->size[1], 0);
472 snprintf(text,
sizeof(text),
"%d", node->
cluster);
473 rtk_fig_text(fig, ox, oy, 0.0, text);
479 pf_kdtree_draw_node(
self, node->
children[0], fig);
480 pf_kdtree_draw_node(
self, node->
children[1], fig);
static void pf_kdtree_cluster_node(pf_kdtree_t *self, pf_kdtree_node_t *node, int depth)
void pf_kdtree_insert(pf_kdtree_t *self, pf_vector_t pose, double value)
int pf_kdtree_get_cluster(pf_kdtree_t *self, pf_vector_t pose)
struct pf_kdtree_node * children[2]
static int pf_kdtree_equal(pf_kdtree_t *self, int key_a[], int key_b[])
pf_kdtree_t * pf_kdtree_alloc(int max_size)
static pf_kdtree_node_t * pf_kdtree_find_node(pf_kdtree_t *self, pf_kdtree_node_t *node, int key[])
double pf_kdtree_get_prob(pf_kdtree_t *self, pf_vector_t pose)
void pf_kdtree_clear(pf_kdtree_t *self)
static pf_kdtree_node_t * pf_kdtree_insert_node(pf_kdtree_t *self, pf_kdtree_node_t *parent, pf_kdtree_node_t *node, int key[], double value)
void pf_kdtree_free(pf_kdtree_t *self)
void pf_kdtree_cluster(pf_kdtree_t *self)