summaryrefslogtreecommitdiff
path: root/experimental/tectonics/quadtree.c
blob: 45f11ac36e57e78ca1f2cb14cd8fb419d97171c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <math.h>
#include <stddef.h>
#include <stdlib.h>
#include "util.h"
#include "geometry.h"

bool quad_contains_point(struct quad_region_t region,
			 struct point_t pt)
{
   if (region.center.x - region.half_dim > pt.x ||
       region.center.x + region.half_dim <= pt.x)
      return false;

   if (region.center.y - region.half_dim > pt.y ||
       region.center.y + region.half_dim <= pt.y)
      return false;
   return true;
}


struct quadtree_node_t quadtree_new_node(struct point_t center,
					 double half_dim)
{
   struct quadtree_node_t node;

   node.region = (struct quad_region_t) { center, half_dim };
   node.id = -1;
   node.nw = NULL;
   node.ne = NULL;
   node.sw = NULL;
   node.se = NULL;

   return node;
}


void quadtree_subdivide(struct quadtree_node_t *node,
			struct point_t *points)
{
   node->nw = malloc(sizeof(struct quadtree_node_t));
   node->ne = malloc(sizeof(struct quadtree_node_t));
   node->sw = malloc(sizeof(struct quadtree_node_t));
   node->se = malloc(sizeof(struct quadtree_node_t));

   struct point_t center = node->region.center;
   double quarter_dim = node->region.half_dim / 2;

   struct point_t c_nw =
      { center.x - quarter_dim, center.y - quarter_dim };
   *(node->nw) = quadtree_new_node(c_nw, quarter_dim);

   struct point_t c_ne =
      { center.x + quarter_dim, center.y - quarter_dim };
   *(node->ne) = quadtree_new_node(c_ne, quarter_dim);

   struct point_t c_sw =
      { center.x - quarter_dim, center.y + quarter_dim };
   *(node->sw) = quadtree_new_node(c_sw, quarter_dim);

   struct point_t c_se =
      { center.x + quarter_dim, center.y + quarter_dim };
   *(node->se) = quadtree_new_node(c_se, quarter_dim);

   if (node->id != -1) {
   /* move point to a child */
      if (quad_contains_point(node->nw->region,
			      points[node->id]))
	 node->nw->id = node->id;
      else if (quad_contains_point(node->ne->region,
				   points[node->id]))
	 node->ne->id = node->id;
      else if (quad_contains_point(node->sw->region,
				   points[node->id]))
	 node->sw->id = node->id;
      else
	 node->se->id = node->id;
      node->id = -1;
   }
}

bool quadtree_insert(struct quadtree_node_t *node,
		     struct point_t *points, int id)
{
   if (!quad_contains_point(node->region, points[id]))
      return false;

   if (node->id == -1 && node->nw == NULL) {
      node->id = id;
      return true;
   }

   if (node->nw == NULL)
      quadtree_subdivide(node, points);

   if (quadtree_insert(node->nw, points, id)) return true;
   if (quadtree_insert(node->ne, points, id)) return true;
   if (quadtree_insert(node->sw, points, id)) return true;
   if (quadtree_insert(node->se, points, id)) return true;

   return false;
}


static int get_points_in_region(struct quadtree_node_t *node,
				int* buffer)
{
   if (node->id != -1) {
      *buffer = node->id;
      return 1;
   }

   if (node->nw != NULL) {
      int n_points = 0;
      n_points += get_points_in_region(node->nw, buffer + n_points);
      n_points += get_points_in_region(node->ne, buffer + n_points);
      n_points += get_points_in_region(node->sw, buffer + n_points);
      n_points += get_points_in_region(node->se, buffer + n_points);
      return n_points;
   }

   return 0;
}


int quadtree_get_closest(struct quadtree_node_t *node,
			 struct point_t *points,
			 struct point_t pt)
{
   if (!quad_contains_point(node->region, pt))
      return -1;

   if (node->id != -1)
      return node->id;

   int closest;
   if (node->nw != NULL) {
      if ((closest = quadtree_get_closest(node->nw, points, pt)) != -1)
	 return closest;
      if ((closest = quadtree_get_closest(node->ne, points, pt)) != -1)
	 return closest;
      if ((closest = quadtree_get_closest(node->sw, points, pt)) != -1)
	 return closest;
      if ((closest = quadtree_get_closest(node->se, points, pt)) != -1)
	 return closest;

      /* closest point is in a subregion, but not the *same*
       * subregion as the point itself, so we need to check against
       * all points contained within the region
       */

      int buffer[1000];
      int n_points = get_points_in_region(node, buffer);
      double dist = 1e9;
      for (int i=0; i<n_points; i++) {
	 int id = buffer[i];
	 double d = distance(points[id], pt);
	 if (d < dist) {
	    closest = id;
	    dist = d;
	 }
      }

      return closest;
   }

   return -1;
}


void quadtree_free(struct quadtree_node_t *node)
{
   if (node->nw != NULL) {
      quadtree_free(node->nw);
      quadtree_free(node->ne);
      quadtree_free(node->sw);
      quadtree_free(node->se);
   }

   free(node);
}