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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487 |
// *******************************************//
// *********Credits to Kyle Schouviller*******//
// *******for the original implementation*****//
// *******************************************//
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Brains.Framework.QuadTree
{
public class QuadTreeNode<T>
{
public static Rectangle GetRectangle(RectangleF rect)
{
return new Rectangle((int)rect.Left, (int)rect.Top, (int)rect.Width, (int)rect.Height);
}
/// <summary>
/// World resize delegate
/// </summary>
/// <param name="newSize">The new world size</param>
public delegate void ResizeDelegate(RectangleF newSize);
/// <summary>
/// The rectangle of this node
/// </summary>
protected RectangleF rect;
/// <summary>
/// Gets the rectangle of this node
/// </summary>
public RectangleF Rect
{
get { return rect; }
protected set { rect = value; }
}
/// <summary>
/// The maximum number of items in this node before partitioning
/// </summary>
protected int MaxItems;
/// <summary>
/// Whether or not this node has been partitioned
/// </summary>
protected bool IsPartitioned;
/// <summary>
/// The parent node
/// </summary>
public QuadTreeNode<T> ParentNode;
/// <summary>
/// The top left node
/// </summary>
protected QuadTreeNode<T> TopLeftNode;
/// <summary>
/// The top right node
/// </summary>
protected QuadTreeNode<T> TopRightNode;
/// <summary>
/// The bottom left node
/// </summary>
protected QuadTreeNode<T> BottomLeftNode;
/// <summary>
/// The bottom right node
/// </summary>
protected QuadTreeNode<T> BottomRightNode;
/// <summary>
/// The items in this node
/// </summary>
protected List<QuadTreePositionItem<T>> Items;
/// <summary>
/// Resize the world
/// </summary>
/// <param name="newSize">The new world size</param>
protected ResizeDelegate WorldResize;
/// <summary>
/// QuadTreeNode constructor
/// </summary>
/// <param name="parentNode">The parent node of this QuadTreeNode</param>
/// <param name="rect">The rectangle of the QuadTreeNode</param>
/// <param name="maxItems">Maximum number of items in the QuadTreeNode before partitioning</param>
public QuadTreeNode(QuadTreeNode<T> parentNode, RectangleF rect, int maxItems)
{
ParentNode = parentNode;
Rect = rect;
MaxItems = maxItems;
IsPartitioned = false;
Items = new List<QuadTreePositionItem<T>>();
}
/// <summary>
/// QuadTreeNode constructor
/// </summary>
/// <param name="rect">The rectangle of the QuadTreeNode</param>
/// <param name="maxItems">Maximum number of items in the QuadTreeNode before partitioning</param>
/// <param name="worldResize">The function to return the size</param>
public QuadTreeNode(RectangleF rect, int maxItems, ResizeDelegate worldResize)
{
ParentNode = null;
Rect = rect;
MaxItems = maxItems;
WorldResize = worldResize;
IsPartitioned = false;
Items = new List<QuadTreePositionItem<T>>();
}
/// <summary>
/// Insert an item in this node
/// </summary>
/// <param name="item">The item to insert</param>
public void Insert(QuadTreePositionItem<T> item)
{
// If partitioned, try to find child node to add to
if (!InsertInChild(item))
{
//item.Destroy += new QuadTreePositionItem<T>.DestroyHandler(ItemDestroy);
//item.Move += new QuadTreePositionItem<T>.MoveHandler(ItemMove);
Items.Add(item);
item.node = this;
// Check if this node needs to be partitioned
if (!IsPartitioned && Items.Count >= MaxItems)
{
Partition();
}
}
}
/// <summary>
/// Inserts an item into one of this node's children
/// </summary>
/// <param name="item">The item to insert in a child</param>
/// <returns>Whether or not the insert succeeded</returns>
protected bool InsertInChild(QuadTreePositionItem<T> item)
{
if (!IsPartitioned) return false;
if (TopLeftNode.ContainsRect(item.Rect))
TopLeftNode.Insert(item);
else if (TopRightNode.ContainsRect(item.Rect))
TopRightNode.Insert(item);
else if (BottomLeftNode.ContainsRect(item.Rect))
BottomLeftNode.Insert(item);
else if (BottomRightNode.ContainsRect(item.Rect))
BottomRightNode.Insert(item);
else return false; // insert in child failed
return true;
}
/// <summary>
/// Pushes an item down to one of this node's children
/// </summary>
/// <param name="i">The index of the item to push down</param>
/// <returns>Whether or not the push was successful</returns>
public bool PushItemDown(int i)
{
if (InsertInChild(Items[i]))
{
RemoveItem(i);
return true;
}
else return false;
}
/// <summary>
/// Push an item up to this node's parent
/// </summary>
/// <param name="i">The index of the item to push up</param>
public void PushItemUp(int i)
{
QuadTreePositionItem<T> m = Items[i];
RemoveItem(i);
ParentNode.Insert(m);
}
/// <summary>
/// Repartitions this node
/// </summary>
protected void Partition()
{
// Create the nodes
Vector2 MidPoint = Vector2.Divide(Vector2.Add(Rect.TopLeft, Rect.BottomRight), 2.0f);
TopLeftNode = new QuadTreeNode<T>(this, new RectangleF(Rect.TopLeft, MidPoint), MaxItems);
TopRightNode = new QuadTreeNode<T>(this, new RectangleF(new Vector2(MidPoint.X, Rect.Top), new Vector2(Rect.Right, MidPoint.Y)), MaxItems);
BottomLeftNode = new QuadTreeNode<T>(this, new RectangleF(new Vector2(Rect.Left, MidPoint.Y), new Vector2(MidPoint.X, Rect.Bottom)), MaxItems);
BottomRightNode = new QuadTreeNode<T>(this, new RectangleF(MidPoint, Rect.BottomRight), MaxItems);
IsPartitioned = true;
// Try to push items down to child nodes
int i = 0;
while (i < Items.Count)
{
if (!PushItemDown(i))
{
i++;
}
}
}
/// <summary>
/// Gets a list of items containing a specified point
/// </summary>
/// <param name="Point">The point</param>
/// <param name="ItemsFound">The list to add found items to (list will not be cleared first)</param>
/// <remarks>ItemsFound is assumed to be initialized, and will not be cleared</remarks>
public void GetItems(Vector2 Point, ref List<QuadTreePositionItem<T>> ItemsFound)
{
// test the point against this node
if (Rect.Contains(Point))
{
// test the point in each item
foreach (QuadTreePositionItem<T> Item in Items)
{
if (Item.Rect.Contains(Point)) ItemsFound.Add(Item);
}
// query all subtrees
if (IsPartitioned)
{
TopLeftNode.GetItems(Point, ref ItemsFound);
TopRightNode.GetItems(Point, ref ItemsFound);
BottomLeftNode.GetItems(Point, ref ItemsFound);
BottomRightNode.GetItems(Point, ref ItemsFound);
}
}
}
/// <summary>
/// Gets a list of items intersecting a specified rectangle
/// </summary>
/// <param name="Rect">The rectangle</param>
/// <param name="ItemsFound">The list to add found items to (list will not be cleared first)</param>
/// <remarks>ItemsFound is assumed to be initialized, and will not be cleared</remarks>
public void GetItems(RectangleF Rect, ref List<QuadTreePositionItem<T>> ItemsFound)
{
// test the point against this node
if (Rect.Intersects(Rect))
{
//CreamXProfiler.StartProfiling(ProfilerName.QuadTreeGetItems);
// test the point in each item
foreach (QuadTreePositionItem<T> Item in Items)
{
//object o = Item.Parent;
if (Item.Rect.Intersects(Rect))
ItemsFound.Add(Item);
}
//CreamXProfiler.StopProfiling(ProfilerName.QuadTreeGetItems);
// query all subtrees
if (IsPartitioned)
{
TopLeftNode.GetItems(Rect, ref ItemsFound);
TopRightNode.GetItems(Rect, ref ItemsFound);
BottomLeftNode.GetItems(Rect, ref ItemsFound);
BottomRightNode.GetItems(Rect, ref ItemsFound);
}
}
}
/// <summary>
/// Gets a list of all items within this node
/// </summary>
/// <param name="ItemsFound">The list to add found items to (list will not be cleared first)</param>
/// <remarks>ItemsFound is assumed to be initialized, and will not be cleared</remarks>
public void GetAllItems(ref List<QuadTreePositionItem<T>> ItemsFound)
{
ItemsFound.AddRange(Items);
// query all subtrees
if (IsPartitioned)
{
TopLeftNode.GetAllItems(ref ItemsFound);
TopRightNode.GetAllItems(ref ItemsFound);
BottomLeftNode.GetAllItems(ref ItemsFound);
BottomRightNode.GetAllItems(ref ItemsFound);
}
}
/// <summary>
/// Finds the node containing a specified item
/// </summary>
/// <param name="Item">The item to find</param>
/// <returns>The node containing the item</returns>
public QuadTreeNode<T> FindItemNode(QuadTreePositionItem<T> Item)
{
if (Items.Contains(Item)) return this;
else if (IsPartitioned)
{
QuadTreeNode<T> n = null;
// Check the nodes that could contain the item
if (TopLeftNode.ContainsRect(Item.Rect))
{
n = TopLeftNode.FindItemNode(Item);
}
if (n == null &&
TopRightNode.ContainsRect(Item.Rect))
{
n = TopRightNode.FindItemNode(Item);
}
if (n == null &&
BottomLeftNode.ContainsRect(Item.Rect))
{
n = BottomLeftNode.FindItemNode(Item);
}
if (n == null &&
BottomRightNode.ContainsRect(Item.Rect))
{
n = BottomRightNode.FindItemNode(Item);
}
return n;
}
else return null;
}
/// <summary>
/// Destroys this node
/// </summary>
public void Destroy()
{
// Destroy all child nodes
if (IsPartitioned)
{
TopLeftNode.Destroy();
TopRightNode.Destroy();
BottomLeftNode.Destroy();
BottomRightNode.Destroy();
TopLeftNode = null;
TopRightNode = null;
BottomLeftNode = null;
BottomRightNode = null;
}
// Remove all items
while (Items.Count > 0)
{
RemoveItem(0);
}
}
/// <summary>
/// Removes an item from this node
/// </summary>
/// <param name="item">The item to remove</param>
public void RemoveItem(QuadTreePositionItem<T> item)
{
// Find and remove the item
if (Items.Contains(item))
{
//item.Move -= new QuadTreePositionItem<T>.MoveHandler(ItemMove);
//item.Destroy -= new QuadTreePositionItem<T>.DestroyHandler(ItemDestroy);
Items.Remove(item);
}
}
/// <summary>
/// Removes an item from this node at a specific index
/// </summary>
/// <param name="i">the index of the item to remove</param>
protected void RemoveItem(int i)
{
if (i < Items.Count)
{
//Items[i].Move -= new QuadTreePositionItem<T>.MoveHandler(ItemMove);
//Items[i].Destroy -= new QuadTreePositionItem<T>.DestroyHandler(ItemDestroy);
Items.RemoveAt(i);
}
}
/// <summary>
/// Handles item movement
/// </summary>
/// <param name="item">The item that moved</param>
public void ItemMove(QuadTreePositionItem<T> item)
{
//CreamXProfiler.StartProfiling("QuadTree: ItemMove");
// Find the item
if (Items.Contains(item))
{
int i = Items.IndexOf(item);
// Try to push the item down to the child
if (!PushItemDown(i))
{
// otherwise, if not root, push up
if (ParentNode != null)
{
PushItemUp(i);
}
else if (!ContainsRect(item.Rect))
{
WorldResize(new RectangleF(
Vector2.Min(Rect.TopLeft, item.Rect.TopLeft) * 2,
Vector2.Max(Rect.BottomRight, item.Rect.BottomRight) * 2));
}
}
}
else
{
// this node doesn't contain that item, stop notifying it about it
//item.Move -= new QuadTreePositionItem<T>.MoveHandler(ItemMove);
}
//CreamXProfiler.StopProfiling("QuadTree: ItemMove");
}
/// <summary>
/// Handles item destruction
/// </summary>
/// <param name="item">The item that is being destroyed</param>
public void ItemDestroy(QuadTreePositionItem<T> item)
{
RemoveItem(item);
}
/// <summary>
/// Tests whether this node contains a rectangle
/// </summary>
/// <param name="rect">The rectangle to test</param>
/// <returns>Whether or not this node contains the specified rectangle</returns>
public bool ContainsRect(RectangleF rect)
{
return (rect.TopLeft.X >= Rect.TopLeft.X &&
rect.TopLeft.Y >= Rect.TopLeft.Y &&
rect.BottomRight.X <= Rect.BottomRight.X &&
rect.BottomRight.Y <= Rect.BottomRight.Y);
}
}
} |