File: quad_tree.h

package info (click to toggle)
btanks 0.9.8083-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 43,616 kB
  • sloc: cpp: 46,425; ansic: 12,005; xml: 4,262; python: 313; sh: 13; makefile: 13
file content (304 lines) | stat: -rw-r--r-- 8,121 bytes parent folder | download | duplicates (5)
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#ifndef QUAD_TREE_H__
#define QUAD_TREE_H__

/* Battle Tanks Game
 * Copyright (C) 2006-2009 Battle Tanks team
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

/* 
 * Additional rights can be granted beyond the GNU General Public License 
 * on the terms provided in the Exception. If you modify this file, 
 * you may extend this exception to your version of the file, 
 * but you are not obligated to do so. If you do not wish to provide this
 * exception without modification, you must delete this exception statement
 * from your version and license this file solely under the GPL without exception. 
*/

#include <set>
#include <assert.h>
#include <stdio.h>

template<typename T, typename V>
struct quad_rect {
	T x0, y0, x1, y1;
	V value;
	
	quad_rect() : x0(0), y0(0), x1(0), y1(0), value(0) {}
	quad_rect(const V& v) : x0(0), y0(0), x1(0), y1(0), value(v) {}
	quad_rect(const T& x0, const T& y0, const T& x1, const T& y1, const V& v) : x0(x0), y0(y0), x1(x1), y1(y1), value(v) {}
	
	inline bool operator<(const quad_rect &other) const {
		if (x0 != other.x0)
			return x0 < other.x0;
		if (y0 != other.y0)
			return x0 < other.x0;
		if (x1 != other.x1)
			return x1 < other.x1;
		return y1 < other.y1;
	}

	inline bool operator==(const quad_rect &other) const {
		return y0 == other.y0 && y1 == other.y1 && x0 == other.x0 && x1 == other.x1;
	}

	inline bool valid() const {
		return x0 < x1 && y0 < y1;
	}
	
	bool contains(const quad_rect & other) const {
		return x0 <= other.x0 && x1 >= other.x1 && y0 <= other.y0 && y1 >= other.y1;
	}

	inline bool intersects(const quad_rect & other) const {
		return !( x0 >= other.x1 || x1 <= other.x0 ||
				y0 >= other.y1 || y1 <= other.y0 );
	}
	
	inline void clear() {
		x0 = 0;
		x1 = 0;
		y0 = 0;
		y1 = 0;
	}
};

template<typename T, typename V, int capacity> 
struct quad_node {
	typedef quad_rect<T, V> rect_type;
	typedef quad_node<T, V, capacity> node_type;
	typedef std::list<rect_type> rects_type;
	
	
	rect_type area;
	rects_type rects;
	quad_node * child[4];
	size_t children_count;
	
	quad_node(const rect_type &area) : area(area), rects(), children_count(0) {
		child[0] = NULL;
		child[1] = NULL;
		child[2] = NULL;
		child[3] = NULL;
	}
	~quad_node() {
		for(int i = 0; i < 4; ++i) {
			delete child[i];
			child[i] = NULL;
		}
	}
	
	void set_size(const T & w, const T & h) {
		clear();
		area.x1 = w;
		area.y1 = h;
	}
	
	void clear() {
		area.clear();
		rects.clear();
		for(int i = 0; i < 4; ++i) {
			delete child[i];
			child[i] = NULL;
		}
		children_count = NULL;
	}
	
	void merge(std::set<V> &result) const {
		if (child[0] != NULL)
			for(int j = 0; j < 4; ++j) {
				child[j]->merge(result);
			}
			
		for(typename rects_type::const_iterator i = rects.begin(); i != rects.end(); ++i) {
			const rect_type &r = *i;
			result.insert(r.value);
		}
	}
	
	void search(std::set<V> & result, const rect_type &s_area) const {
		if (!area.intersects(s_area))
			return;

		if (child[0] != NULL)
		for(int j = 0; j < 4; ++j) {
			if (s_area.contains(child[j]->area)) {
				//move all area to result :)
				child[j]->merge(result);
			} else {
				child[j]->search(result, s_area);
			}
		}

		for(typename rects_type::const_iterator i = rects.begin(); i != rects.end(); ++i) {
			const rect_type & rect = *i;
			if (rect.intersects(s_area))
				result.insert(rect.value);
		}
	}
	
	void split() {
		assert(child[0] == NULL);
		
		int w = area.x1 - area.x0, h = area.y1 - area.y0;
		if (w < 2 || h < 2)
			return;

		//printf("splitting node %d,%d,%d,%d (%u)\n", area.x0, area.y0, area.x1, area.y1, (unsigned)children_count);
		
		int hx = (w - 1) / 2 + 1;
		int hy = (h - 1) / 2 + 1;
		child[0] = new quad_node(rect_type(area.x0, 		area.y0, 		area.x0 + hx, 	area.y0 + hy, NULL));
		child[1] = new quad_node(rect_type(area.x0 + hx, 	area.y0, 		area.x1, 		area.y0 + hy, NULL));
		child[2] = new quad_node(rect_type(area.x0, 		area.y0 + hy, 	area.x0 + hx, 	area.y1, NULL));
		child[3] = new quad_node(rect_type(area.x0 + hx, 	area.y0 + hy, 	area.x1, 		area.y1, NULL));
	}
	
	bool insert(const rect_type & object) {
		if (!area.contains(object))
			return false;
		
		if (child[0] == NULL) 
			split();

		if (child[0] != NULL)
		for(int i = 0; i < 4; ++i) {
			if (child[i]->insert(object)) {
				++children_count;
				return true;
			}
		}
	
		//printf("insert %d,%d,%d,%d into %d,%d,%d,%d [%d]\n", object.x0, object.y0, object.x1, object.y1, area.x0, area.y0, area.x1, area.y1, children_count);
		rects.push_back(object);
		++children_count;
		return true;
	}

	bool erase(const rect_type & object) {
		if (!area.contains(object))
			return false;

		if (child[0] != NULL)
		for(int i = 0; i < 4; ++i) 
			if (child[i]->erase(object)) {
				--children_count;
				return true;
			}
		
		//printf("erase %d,%d,%d,%d into %d,%d,%d,%d [%d]\n", object.x0, object.y0, object.x1, object.y1, area.x0, area.y0, area.x1, area.y1, children_count);
		for(typename rects_type::iterator i = rects.begin(); i != rects.end(); ++i) {
			const rect_type &r = *i;
			if (r == object && r.value == object.value) {
				i = rects.erase(i);
				--children_count;
				return true;
			}
		}
		return false;
	}
	
};

template<typename T, typename V, int capacity>
class quad_tree {
public: 
	typedef quad_rect<T, V> rect_type;
	typedef quad_node<T, V, capacity> node_type;
	
	inline quad_tree() : root(rect_type()) {}
	inline void set_size(const T &w, const T &h) {
		clear();
		root.set_size(w, h);
	}
	
	inline void clear() {
		root.clear();
	}
	
	inline void search(std::set<V> & result, const rect_type &area) const {
		if (!area.valid())
			return;

		if (root.area.contains(area)) {
			root.search(result, area);
		} else {
			rect_type child[4]; //splitting rectangle
			int n = split(child, area);
			for(int i = 0; i < n; ++i) {
				root.search(result, child[i]);
			}
		}
	}
	
	inline void insert(const rect_type & object) {
		if (!object.valid())
			return;
		if (root.area.contains(object)) {
			root.insert(object);
		} else {
			rect_type child[4]; //splitting rectangle
			int n = split(child, object);
			for(int i = 0; i < n; ++i) {
				root.insert(child[i]);
			}
		}
	}

	inline void erase(const rect_type & object) {
		if (!object.valid())
			return;
		if (root.area.contains(object)) {
			root.erase(object);
		} else {
			rect_type child[4]; //splitting rectangle
			int n = split(child, object);
			for(int i = 0; i < n; ++i) {
				root.erase(child[i]);
			}
		}
	}
	
protected: 
	inline int split(rect_type * child, const rect_type &rect) const {
		bool has_x = rect.x1 > root.area.x1;
		bool has_y = rect.y1 > root.area.y1;
		if (has_x && has_y) {
			child[0] = rect_type(rect.x0, rect.y0, root.area.x1, root.area.y1, rect.value);
			child[1] = rect_type(0, rect.y0, rect.x1 - root.area.x1, root.area.y1, rect.value);

			child[2] = rect_type(rect.x0, 0, root.area.x1, rect.y1 - root.area.y1, rect.value);
			child[3] = rect_type(0, 0, rect.x1 - root.area.x1, rect.y1 - root.area.y1, rect.value); 
			return 4;
		} else if (has_x) {
			child[0] = rect_type(rect.x0, rect.y0, root.area.x1, rect.y1, rect.value);
			child[1] = rect_type(0, rect.y0, rect.x1 - root.area.x1, rect.y1, rect.value);
			return 2;
		} else if (has_y) {
			child[0] = rect_type(rect.x0, rect.y0, rect.x1, root.area.y1, rect.value);
			child[1] = rect_type(rect.x0, 0, rect.x1, rect.y1 - root.area.y1, rect.value);
			return 2;
		} else {
			child[0] = rect;
			return 1;
		}
	}

	node_type root;
};

#endif