]> git.itanic.dy.fi Git - sdl-planets/blob - quadtree.c
Introduce struct quadree_ops
[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         for (i = 0; i < 4; i++) {
25                 if (!node->child[i])
26                         continue;
27
28                 if (node->child[i]->parent != node) {
29                         printf("%s:%d Fatal! Tree inconsistency detected "
30                                "at child %d in node %p\n",
31                                __func__, __LINE__, i, node);
32                         trap();
33                 }
34
35                 if (node->parent) {
36                         if (node->child[i] == node->parent) {
37                                 printf("%s:%d Fatal! Tree loop detected "
38                                        "at child %d in node %p\n",
39                                        __func__, __LINE__, i, node);
40                                 trap();
41                         }
42                 }
43
44                 children += node->child[i]->children + 1;
45
46                 validate_subtree(node->child[i]);
47         }
48
49         if (node->depth < 0) {
50                 printf("%s:%d Tree statistics inconsistency detected! "
51                        "Negative depth: %ld\n",
52                        __func__, __LINE__, node->depth);
53         }
54
55         if (node->children != children) {
56                 printf("%s:%d Tree statistics inconsistency detected! "
57                        "child count mismatch. Expected %ld, got %ld\n",
58                        __func__, __LINE__, children, node->children);
59                 trap();
60         }
61
62         for (i = 0; i < 4 && node->children; i++) {
63                 if (!node->child[i])
64                         continue;
65
66                 if (node->depth == node->child[i]->depth + 1)
67                         break;
68
69                 if (node->child[i]->depth > node->depth) {
70                         printf("%s:%d Tree statistics inconsistency detected! "
71                                "child depth mismatch %ld > %ld\n",
72                                __func__, __LINE__, node->child[i]->depth,
73                                node->depth);
74                 }
75         }
76
77         if (i == 4) {
78                 printf("%s:%d Tree statistics inconsistency detected! "
79                        "child depth mismatch.",
80                        __func__, __LINE__);
81                 trap();
82         }
83 }
84
85 static void validate_tree(const struct quadtree *node)
86 {
87         if (debug)
88                 validate_subtree(quadtree_find_parent(node));
89 }
90
91 /**
92  * quadtree_add - add a node to a quadtree
93  * @parent: parent node
94  * @new: the new node to be added
95  * @compare: pointer to a comparison function
96  *
97  * Add a node to a quadtree. The tree is kept in order, the new node
98  * is placed in the end of appropriate branch. The compare function is
99  * used to compare the new node against a branch in the tree. The
100  * comparison function must have following return value depending on
101  * the position of node a compared to the position of node b:
102  *
103  * 0: upper left
104  * 1: upper right
105  * 2: lower left
106  * 3: lower right
107  *
108  * The case of nodes sharing identical coordinates is not taken into
109  * account at all.
110  */
111
112 struct quadtree *quadtree_add(struct quadtree *parent, struct quadtree *new,
113                               struct quadtree_ops *ops)
114 {
115         int ret;
116         if (parent == new)
117                 trap();
118
119         validate_tree(parent);
120
121         ret = ops->compare(parent, new);
122
123         if (ret < 0 || ret >= 4) {
124                 printf("Invalid comparison result of %d\n", ret);
125                 return 0;
126         }
127
128         if (parent->child[ret])
129                 return quadtree_add(parent->child[ret], new, ops);
130
131         parent->child[ret] = new;
132         new->parent = parent;
133
134         quadtree_recalculate_parent_stats(parent);
135
136         validate_tree(new);
137
138         return new;
139 }
140
141 /*
142  * _quadtree_reposition_reqursively - Move everything under and
143  * including a given node under the new root node
144  */
145
146 static void
147 _quadtree_reposition_reqursively(struct quadtree *root,
148                                  struct quadtree *node,
149                                  struct quadtree_ops *ops)
150 {
151         int i;
152
153         validate_tree(node);
154
155         /* First remove all children, if any */
156
157         for (i = 0; i < 4; i++) {
158                 if (!node->child[i])
159                         continue;
160
161                 if (node->child[i] == node ||
162                     node->child[i] == node->parent)
163                         trap();
164
165                 _quadtree_reposition_reqursively(root, node->child[i], ops);
166                 node->child[i] = 0;
167         }
168
169         /* There are no children left, reset stats */
170         node->depth = 0;
171         node->children = 0;
172
173         /* Then remove this node under the new root. */
174         quadtree_add(root, node, ops);
175 }
176
177 /*
178  * quadtree_del - Detach a node from the tree
179  *
180  * Return value: The new root node of the tree. If we are detaching
181  * anything but the root node of the entire tree, the returned root
182  * value will be the original root of the tree.
183  */
184 struct quadtree *quadtree_del(struct quadtree *node,
185                               struct quadtree_ops *ops)
186 {
187         struct quadtree *parent = 0;
188         int i;
189
190         /*
191          * We are deleting the root node. This means we have to select
192          * a new root node and reconstruct the entire tree under it
193          * again.
194          */
195         if (!node->parent) {
196                 for (i = 0; i < 4; i++) {
197                         if (!node->child[i])
198                                 continue;
199
200                         if (!parent) {
201                                 parent = node->child[i];
202                                 continue;
203                         }
204                         _quadtree_reposition_reqursively(parent,
205                                                          node->child[i],
206                                                          ops);
207                         node->child[i] = 0;
208                 }
209
210                 return parent;
211         }
212
213         /*
214          * We are not deleting the parent. Detach the node from the
215          * parent abd relocate the children. The node will be then
216          * detached from the tree.
217          */
218
219         for (i = 0; i < 4; i++) {
220                 if (node->parent->child[i] == node) {
221                         node->parent->child[i] = 0;
222                         break;
223                 }
224         }
225         if (i == 4) {
226                 printf("%s:%d Fatal! Tree inconsistency detected\n",
227                        __func__, __LINE__);
228                 trap();
229         }
230
231         /* Fix parent stats */
232         quadtree_recalculate_parent_stats(node->parent);
233
234         /*
235          * The sub branch is now detached from the main tree. Continue
236          * relocating the detached branch.
237          */
238
239         for (i = 0; i < 4; i++) {
240                 if (!node->child[i])
241                         continue;
242
243                 _quadtree_reposition_reqursively(node->parent, node->child[i],
244                                                  ops);
245                 node->child[i] = 0;
246         }
247
248         parent = quadtree_find_parent(node);
249         node->parent = 0;
250
251         node->children = 0;
252         node->depth = 0;
253
254         validate_tree(parent);
255         return parent;
256 }
257
258
259 static int _walk_tree(struct quadtree *head, const struct quadtree_iterator *it)
260 {
261         int direction, count = 0;
262
263         direction = it->direction(head, (struct quadtree_iterator *)it);
264
265         if ((direction & QUADTREE_UPLEFT) && head->child[0])
266                 count += _walk_tree(head->child[0], it);
267
268         if ((direction & QUADTREE_UPRIGHT) && head->child[1])
269                 count += _walk_tree(head->child[1], it);
270
271         if ((direction & QUADTREE_DOWNLEFT) && head->child[2])
272                 count += _walk_tree(head->child[2], it);
273
274         if ((direction & QUADTREE_DOWNRIGHT) && head->child[3])
275                 count += _walk_tree(head->child[3], it);
276
277         if ((direction & QUADTREE_SELF) && it->callback) {
278                 it->callback(head, (struct quadtree_iterator *)it);
279                 count++;
280         }
281
282         return count;
283 }
284
285 int walk_quadtree(const struct quadtree_iterator *it)
286 {
287         return _walk_tree(it->head, it);
288 }