12 static void _trap(int line)
15 printf("Trapped from line %d, use debugger to get backtrace\n",
22 #define trap() _trap(__LINE__)
24 static int quadtree_compare_coord(const struct vector *a,
25 const struct vector *b)
41 static int quadtree_compare(const struct quadtree *a, const struct quadtree *b)
43 return quadtree_compare_coord(&a->pos, &b->pos);
46 static void validate_subtree(const struct quadtree *node)
53 printf("Attempted to validate a null pointer\n");
58 for (i = 0; i < 4; i++) {
62 if (node->child[i]->parent != node) {
63 printf("%s:%d Fatal! Tree inconsistency detected at "
64 "child %d %p in node %p, incorrent parent %p\n",
65 __func__, __LINE__, i, node->child[i], node,
66 node->child[i]->parent);
72 if (node->child[i] == node->parent) {
73 printf("%s:%d Fatal! Tree loop detected "
74 "at child %d in node %p\n",
75 __func__, __LINE__, i, node);
81 dir = quadtree_compare(node, node->child[i]);
84 printf("%s:%d Fatal! Spatial inconsistency detected "
85 "at child %d in node %p\n"
86 "parent: (%f %f), child (%f %f), "
88 __func__, __LINE__, i, node,
89 node->pos.x, node->pos.y,
90 node->child[i]->pos.x, node->child[i]->pos.y,
96 children += node->child[i]->children + 1;
98 validate_subtree(node->child[i]);
101 if (node->depth < 0) {
102 printf("%s:%d Tree statistics inconsistency detected! "
103 "Negative depth: %ld\n",
104 __func__, __LINE__, node->depth);
107 if (node->children != children) {
108 printf("%s:%d Tree statistics inconsistency detected! "
109 "child count mismatch. Expected %ld, got %ld\n",
110 __func__, __LINE__, children, node->children);
115 for (i = 0; i < 4 && node->children; i++) {
119 if (node->depth == node->child[i]->depth + 1)
122 if (node->child[i]->depth > node->depth) {
123 printf("%s:%d Tree statistics inconsistency detected! "
124 "child depth mismatch %ld > %ld\n",
125 __func__, __LINE__, node->child[i]->depth,
131 printf("%s:%d Tree statistics inconsistency detected! "
132 "child depth mismatch.",
139 static void validate_tree(const struct quadtree *node)
142 validate_subtree(quadtree_find_parent(node));
146 * quadtree_add - add a node to a quadtree
147 * @parent: parent node
148 * @new: the new node to be added
150 * Add a node to a quadtree. The tree is kept in order, the new node
151 * is placed in the end of appropriate branch.
153 * The case of nodes sharing identical coordinates is not taken into
157 struct quadtree *quadtree_add(struct quadtree *parent, struct quadtree *new,
158 struct quadtree_ops *ops)
164 validate_tree(parent);
166 ret = quadtree_compare(parent, new);
168 if (ret < 0 || ret >= 4) {
169 printf("Invalid comparison result of %d\n", ret);
173 if (parent->child[ret])
174 return quadtree_add(parent->child[ret], new, ops);
176 parent->child[ret] = new;
177 new->parent = parent;
178 for (i = 0; i < 4; i++)
182 printf("adding node %p to parent %p\n", new, parent);
186 quadtree_recalculate_parent_stats(new, ops);
194 * _qt_optimal_del - Delete nodes from a detached subtree in optimal order
196 * All nodes are moved under the new parent node
198 static void _qt_optimal_del(struct quadtree *parent, struct quadtree *node,
199 struct quadtree_ops *ops, int parents)
201 struct quadtree *child[4] = {0};
202 int i, children = 0, max_c, max_i;
204 for (i = 0; i < 4; i++) {
205 if (!node->child[i]) {
210 child[children] = node->child[i];
211 node->child[i] = NULL;
216 * Try to remove the nodes in such order that the node which
217 * has child number one quarter of the parent's child numer
218 * gets deleted first.
221 if (node->children <= parents / 4) {
223 printf("%d: Moving node %p away from parent %p\n",
224 __LINE__, node, parent);
227 validate_tree(parent);
228 quadtree_add(parent, node, ops);
236 for (i = 0; i < 4; i++) {
240 if (max_c < child[i]->children) {
241 max_c = child[i]->children;
246 _qt_optimal_del(parent, child[max_i], ops, parents / 4);
252 validate_tree(parent);
254 printf("%d: Moving node %p away from parent %p\n",
255 __LINE__, node, parent);
258 quadtree_add(parent, node, ops);
263 * quadtree_del - Detach a node from the tree
265 * Return value: The new root node of the tree. If we are detaching
266 * anything but the root node of the entire tree, the returned root
267 * value will be the original root of the tree.
269 struct quadtree *quadtree_del(struct quadtree *node,
270 struct quadtree_ops *ops)
272 struct quadtree *parent = 0;
276 printf("Deleting node %p under parent %p\n",
281 * We are deleting the root node. This means we have to select
282 * a new root node and reconstruct the entire tree under it
286 for (i = 0; i < 4; i++) {
291 parent = node->child[i];
295 _qt_optimal_del(parent, node->child[i], ops,
304 * We are not deleting the parent. Detach the node from the
305 * parent abd relocate the children. The node will be then
306 * detached from the tree.
309 for (i = 0; i < 4; i++) {
310 if (node->parent->child[i] == node) {
311 node->parent->child[i] = 0;
318 parent = node->parent;
321 * The sub branch is now detached from the main tree. Fix the
324 quadtree_recalculate_parent_stats(node->parent, ops);
326 parent = quadtree_find_parent(parent);
328 if (node->children) {
329 for (i = 0; i < 4; i++) {
333 _qt_optimal_del(parent, node->child[i], ops,
338 validate_tree(parent);
343 static int _walk_tree(struct quadtree *head, const struct quadtree_iterator *it)
345 int direction, count = 0;
347 direction = it->direction(head, (struct quadtree_iterator *)it);
349 if ((direction & QUADTREE_UPLEFT) && head->child[0])
350 count += _walk_tree(head->child[0], it);
352 if ((direction & QUADTREE_UPRIGHT) && head->child[1])
353 count += _walk_tree(head->child[1], it);
355 if ((direction & QUADTREE_DOWNLEFT) && head->child[2])
356 count += _walk_tree(head->child[2], it);
358 if ((direction & QUADTREE_DOWNRIGHT) && head->child[3])
359 count += _walk_tree(head->child[3], it);
361 if ((direction & QUADTREE_SELF) && it->callback) {
362 it->callback(head, (struct quadtree_iterator *)it);
369 int walk_quadtree(const struct quadtree_iterator *it)
371 return _walk_tree(it->head, it);