naev
0.11.5
src
quadtree.h
1
/*
2
* This code was initially authored by the Stackoverflow user dragon-energy and posted under following page:
3
* https://stackoverflow.com/questions/41946007/efficient-and-well-explained-implementation-of-a-quadtree-for-2d-collision-det
4
*
5
* As for the license, the author has kindly noted:
6
*
7
* "Oh and feel free to use this code I post however you want, even for commercial projects.
8
* I would really love it if people let me know if they find it useful, but do as you wish."
9
*
10
* And generally all Stackoverflow-posted code is by default licensed with CC BY-SA 4.0:
11
* https://creativecommons.org/licenses/by-sa/4.0/
12
*/
13
#pragma once
14
15
#include "intlist.h"
16
17
typedef
struct
Quadtree
Quadtree
;
18
19
struct
Quadtree
20
{
21
// Stores all the nodes in the quadtree. The first node in this
22
// sequence is always the root.
23
IntList
nodes;
24
25
// Stores all the elements in the quadtree.
26
IntList
elts;
27
28
// Stores all the element nodes in the quadtree.
29
IntList
enodes;
30
31
// Stores the quadtree extents.
32
int
root_mx, root_my, root_sx, root_sy;
33
34
// Maximum allowed elements in a leaf before the leaf is subdivided/split unless
35
// the leaf is at the maximum allowed tree depth.
36
int
max_elements;
37
38
// Stores the maximum depth allowed for the quadtree.
39
int
max_depth;
40
41
// Temporary buffer used for queries.
42
43
char
* temp;
44
45
// Stores the size of the temporary buffer.
46
int
temp_size;
47
};
48
49
// Function signature used for traversing a tree node.
50
typedef
void
QtNodeFunc(
Quadtree
* qt,
void
* user_data,
int
node,
int
depth,
int
mx,
int
my,
int
sx,
int
sy );
51
52
// Creates a quadtree with the requested extents, maximum elements per leaf, and maximum tree depth.
53
void
qt_create(
Quadtree
* qt,
int
x1,
int
y1,
int
x2,
int
y2,
int
max_elements,
int
max_depth );
54
55
// Destroys the quadtree.
56
void
qt_destroy(
Quadtree
* qt );
57
58
// Clears the quadtree making it empty.
59
void
qt_clear(
Quadtree
* qt );
60
61
// Inserts a new element to the tree.
62
// Returns an index to the new element.
63
int
qt_insert(
Quadtree
* qt,
int
id
,
int
x1,
int
y1,
int
x2,
int
y2 );
64
65
// Removes the specified element from the tree.
66
void
qt_remove(
Quadtree
* qt,
int
element );
67
68
// Cleans up the tree, removing empty leaves.
69
void
qt_cleanup(
Quadtree
* qt );
70
71
// Outputs a list of elements found in the specified rectangle.
72
void
qt_query(
Quadtree
* qt,
IntList
* out,
int
x1,
int
y1,
int
x2,
int
y2 );
73
74
// Traverses all the nodes in the tree, calling 'branch' for branch nodes and 'leaf'
75
// for leaf nodes.
76
void
qt_traverse(
Quadtree
* qt,
void
* user_data, QtNodeFunc* branch, QtNodeFunc* leaf);
IntList
Definition
intlist.h:19
Quadtree
Definition
quadtree.h:20
Generated by
1.12.0