]> git.itanic.dy.fi Git - sdl-planets/blob - quadtree.h
Move position vector from planet to quadtree
[sdl-planets] / quadtree.h
1 #ifndef _QUADTREE_H_
2 #define _QUADTREE_H_
3
4 #include <string.h>
5
6 #include "utils.h"
7 #include "vector.h"
8
9 struct quadtree {
10         struct vector pos;
11         struct quadtree *child[4];
12         struct quadtree *parent;
13
14         /* statistics */
15         long int children;      /* The total number of children */
16         long int depth;         /* The deepest subtree branch */
17 };
18
19 struct quadtree_ops {
20         /*
21          * Comparison function that is needed to find out the correct
22          * location for a node in the tree
23          */
24         int (*compare)(struct quadtree *a, struct quadtree *b);
25
26         /*
27          * Calculates required statistical information for a node
28          */
29         void (*recalculate_stats)(struct quadtree *node);
30 };
31
32 static inline void init_quadtree(struct quadtree *t)
33 {
34         memset(t, 0, sizeof(*t));
35 }
36
37 #define QUADTREE_UPLEFT         0x1
38 #define QUADTREE_UPRIGHT        0x2
39 #define QUADTREE_DOWNLEFT       0x4
40 #define QUADTREE_DOWNRIGHT      0x8
41 #define QUADTREE_SELF           0x10
42
43 struct quadtree_iterator {
44         struct quadtree *head;
45         void *ptr;
46
47         int (*direction)(struct quadtree *head, struct quadtree_iterator *it);
48         void (*callback)(struct quadtree *head, struct quadtree_iterator *it);
49 };
50
51 struct quadtree *quadtree_add(struct quadtree *parent, struct quadtree *new,
52                               struct quadtree_ops *ops);
53
54 struct quadtree *quadtree_del(struct quadtree *node, struct quadtree_ops *ops);
55
56 int walk_quadtree(const struct quadtree_iterator *iterator);
57
58
59 /* quadtree_find_parent - return the highest parent of the node */
60 static inline struct quadtree *quadtree_find_parent(const struct quadtree *node)
61 {
62         struct quadtree *t = (struct quadtree *)node;
63         while (t->parent)
64                 t = t->parent;
65
66         return t;
67 }
68
69 /*
70  * Recursively walk through the tree and propagate changes made to the
71  * given node up until the highest parent.
72  */
73 static inline void quadtree_recalculate_parent_stats(struct quadtree *node,
74                                                      struct quadtree_ops *ops)
75 {
76         int i;
77
78         while (node) {
79                 node->depth = 0;
80                 node->children = 0;
81
82                 for (i = 0; i < 4; i++) {
83                         if (!node->child[i])
84                                 continue;
85
86                         node->depth = MAX(node->depth,
87                                           node->child[i]->depth + 1);
88                         node->children += node->child[i]->children + 1;
89                 }
90
91                 if (ops->recalculate_stats)
92                         ops->recalculate_stats(node);
93
94                 node = node->parent;
95         }
96 }
97
98 #endif