]> git.itanic.dy.fi Git - sdl-planets/blob - quadtree.c
merge_planets: Calculate new color for the remaining planet
[sdl-planets] / quadtree.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #include "quadtree.h"
5
6 #ifdef DEBUG
7 #define debug 1
8 #else
9 #define debug 0
10 #endif
11
12 static void trap(void)
13 {
14         if (debug)
15                 exit(1);
16 }
17
18 static void validate_subtree(const struct quadtree *node)
19 {
20         int i;
21         long int children;
22         children = 0;
23
24         if (!node) {
25                 printf("Attempted to validate a null pointer\n");
26                 fflush(stdout);
27                 trap();
28         }
29
30         for (i = 0; i < 4; i++) {
31                 if (!node->child[i])
32                         continue;
33
34                 if (node->child[i]->parent != node) {
35                         printf("%s:%d Fatal! Tree inconsistency detected at "
36                                "child %d %p in node %p, incorrent parent %p\n",
37                                __func__, __LINE__, i, node->child[i], node,
38                                node->child[i]->parent);
39                         fflush(stdout);
40                         trap();
41                 }
42
43                 if (node->parent) {
44                         if (node->child[i] == node->parent) {
45                                 printf("%s:%d Fatal! Tree loop detected "
46                                        "at child %d in node %p\n",
47                                        __func__, __LINE__, i, node);
48                                 fflush(stdout);
49                                 trap();
50                         }
51                 }
52
53                 children += node->child[i]->children + 1;
54
55                 validate_subtree(node->child[i]);
56         }
57
58         if (node->depth < 0) {
59                 printf("%s:%d Tree statistics inconsistency detected! "
60                        "Negative depth: %ld\n",
61                        __func__, __LINE__, node->depth);
62         }
63
64         if (node->children != children) {
65                 printf("%s:%d Tree statistics inconsistency detected! "
66                        "child count mismatch. Expected %ld, got %ld\n",
67                        __func__, __LINE__, children, node->children);
68                 fflush(stdout);
69                 trap();
70         }
71
72         for (i = 0; i < 4 && node->children; i++) {
73                 if (!node->child[i])
74                         continue;
75
76                 if (node->depth == node->child[i]->depth + 1)
77                         break;
78
79                 if (node->child[i]->depth > node->depth) {
80                         printf("%s:%d Tree statistics inconsistency detected! "
81                                "child depth mismatch %ld > %ld\n",
82                                __func__, __LINE__, node->child[i]->depth,
83                                node->depth);
84                 }
85         }
86
87         if (i == 4) {
88                 printf("%s:%d Tree statistics inconsistency detected! "
89                        "child depth mismatch.",
90                        __func__, __LINE__);
91                 fflush(stdout);
92                 trap();
93         }
94 }
95
96 static void validate_tree(const struct quadtree *node)
97 {
98         if (debug)
99                 validate_subtree(quadtree_find_parent(node));
100 }
101
102 /**
103  * quadtree_add - add a node to a quadtree
104  * @parent: parent node
105  * @new: the new node to be added
106  * @compare: pointer to a comparison function
107  *
108  * Add a node to a quadtree. The tree is kept in order, the new node
109  * is placed in the end of appropriate branch. The compare function is
110  * used to compare the new node against a branch in the tree. The
111  * comparison function must have following return value depending on
112  * the position of node a compared to the position of node b:
113  *
114  * 0: upper left
115  * 1: upper right
116  * 2: lower left
117  * 3: lower right
118  *
119  * The case of nodes sharing identical coordinates is not taken into
120  * account at all.
121  */
122
123 struct quadtree *quadtree_add(struct quadtree *parent, struct quadtree *new,
124                               struct quadtree_ops *ops)
125 {
126         int ret, i;
127         if (parent == new)
128                 trap();
129
130         validate_tree(parent);
131
132         ret = ops->compare(parent, new);
133
134         if (ret < 0 || ret >= 4) {
135                 printf("Invalid comparison result of %d\n", ret);
136                 return 0;
137         }
138
139         if (parent->child[ret])
140                 return quadtree_add(parent->child[ret], new, ops);
141
142         parent->child[ret] = new;
143         new->parent = parent;
144         for (i = 0; i < 4; i++)
145                 new->child[i] = 0;
146
147         if (debug) {
148                 printf("adding node %p to parent %p\n", new, parent);
149                 fflush(stdout);
150         }
151
152         quadtree_recalculate_parent_stats(new, ops);
153
154         validate_tree(new);
155
156         return new;
157 }
158
159 /*
160  * _qt_optimal_del - Delete nodes from a detached subtree in optimal order
161  *
162  * All nodes are moved under the new parent node
163  */
164 static void _qt_optimal_del(struct quadtree *parent, struct quadtree *node,
165                        struct quadtree_ops *ops, int parents)
166 {
167         struct quadtree *child[4] = {0};
168         int i, children = 0, max_c, max_i;
169
170         for (i = 0; i < 4; i++) {
171                 if (!node->child[i]) {
172                         child[i] = 0;
173                         continue;
174                 }
175
176                 child[children] = node->child[i];
177                 node->child[i] = NULL;
178                 children++;
179         }
180
181         /*
182          * Try to remove the nodes in such order that the node which
183          * has child number one quarter of the parent's child numer
184          * gets deleted first.
185          */
186
187         if (node->children <= parents / 4) {
188                 if (debug) {
189                         printf("%d: Moving node %p away from parent %p\n",
190                                __LINE__, node, parent);
191                         fflush(stdout);
192                 }
193                 validate_tree(parent);
194                 quadtree_add(parent, node, ops);
195                 node = NULL;
196                 parents /= 4;
197         }
198
199         while(children) {
200                 max_c = -1;
201                 max_i = -1;
202                 for (i = 0; i < 4; i++) {
203                         if (!child[i])
204                                 continue;
205
206                         if (max_c < child[i]->children) {
207                                 max_c = child[i]->children;
208                                 max_i = i;
209                         }
210                 }
211
212                 _qt_optimal_del(parent, child[max_i], ops, parents / 4);
213                 child[max_i] = 0;
214                 children--;
215         }
216
217         if (node) {
218                 validate_tree(parent);
219                 if (debug) {
220                         printf("%d: Moving node %p away from parent %p\n",
221                                __LINE__, node, parent);
222                         fflush(stdout);
223                 }
224                 quadtree_add(parent, node, ops);
225         }
226 }
227
228 /*
229  * quadtree_del - Detach a node from the tree
230  *
231  * Return value: The new root node of the tree. If we are detaching
232  * anything but the root node of the entire tree, the returned root
233  * value will be the original root of the tree.
234  */
235 struct quadtree *quadtree_del(struct quadtree *node,
236                               struct quadtree_ops *ops)
237 {
238         struct quadtree *parent = 0;
239         int i;
240
241         if (debug) {
242                 printf("Deleting node %p under parent %p\n",
243                                node, node->parent);
244                 fflush(stdout);
245         }
246         /*
247          * We are deleting the root node. This means we have to select
248          * a new root node and reconstruct the entire tree under it
249          * again.
250          */
251         if (!node->parent) {
252                 for (i = 0; i < 4; i++) {
253                         if (!node->child[i])
254                                 continue;
255
256                         if (!parent) {
257                                 parent = node->child[i];
258                                 parent->parent = 0;
259                                 continue;
260                         }
261                         _qt_optimal_del(parent, node->child[i], ops,
262                                         node->children);
263                         node->child[i] = 0;
264                 }
265
266                 return parent;
267         }
268
269         /*
270          * We are not deleting the parent. Detach the node from the
271          * parent abd relocate the children. The node will be then
272          * detached from the tree.
273          */
274
275         for (i = 0; i < 4; i++) {
276                 if (node->parent->child[i] == node) {
277                         node->parent->child[i] = 0;
278                         break;
279                 }
280         }
281         if (i == 4)
282                 trap();
283
284         parent = node->parent;
285
286         /*
287          * The sub branch is now detached from the main tree. Fix the
288          * stats.
289          */
290         quadtree_recalculate_parent_stats(node->parent, ops);
291
292         parent = quadtree_find_parent(parent);
293
294         if (node->children) {
295                 for (i = 0; i < 4; i++) {
296                         if (!node->child[i])
297                                 continue;
298
299                         _qt_optimal_del(parent, node->child[i], ops,
300                                         node->children);
301                 }
302         }
303
304         validate_tree(parent);
305         return parent;
306 }
307
308
309 static int _walk_tree(struct quadtree *head, const struct quadtree_iterator *it)
310 {
311         int direction, count = 0;
312
313         direction = it->direction(head, (struct quadtree_iterator *)it);
314
315         if ((direction & QUADTREE_UPLEFT) && head->child[0])
316                 count += _walk_tree(head->child[0], it);
317
318         if ((direction & QUADTREE_UPRIGHT) && head->child[1])
319                 count += _walk_tree(head->child[1], it);
320
321         if ((direction & QUADTREE_DOWNLEFT) && head->child[2])
322                 count += _walk_tree(head->child[2], it);
323
324         if ((direction & QUADTREE_DOWNRIGHT) && head->child[3])
325                 count += _walk_tree(head->child[3], it);
326
327         if ((direction & QUADTREE_SELF) && it->callback) {
328                 it->callback(head, (struct quadtree_iterator *)it);
329                 count++;
330         }
331
332         return count;
333 }
334
335 int walk_quadtree(const struct quadtree_iterator *it)
336 {
337         return _walk_tree(it->head, it);
338 }