-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRTree.cs
141 lines (124 loc) · 3.69 KB
/
RTree.cs
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
namespace DataStructure;
public class RTree
{
public class RTreeNode
{
public List<Rectangle> Entries { get; set; }
public List<RTreeNode> Children { get; set; }
public bool IsLeaf { get; set; }
public RTreeNode(bool isLeaf)
{
Entries = new List<Rectangle>();
Children = new List<RTreeNode>();
IsLeaf = isLeaf;
}
}
private RTreeNode root;
private int maxEntries;
public RTree(int maxEntries = 4)
{
root = new RTreeNode(true);
this.maxEntries = maxEntries;
}
public void Insert(Rectangle rect)
{
RTreeNode node = ChooseLeaf(root, rect);
node.Entries.Add(rect);
if (node.Entries.Count > maxEntries)
{
SplitNode(node);
}
}
private RTreeNode ChooseLeaf(RTreeNode node, Rectangle rect)
{
if (node.IsLeaf)
{
return node;
}
RTreeNode? bestChild = null;
System.Double minAreaIncrease = System.Double.MaxValue;
foreach (RTreeNode child in node.Children)
{
Rectangle union = Rectangle.Union(child.Entries[0], rect);
System.Double areaIncrease = union.Area() - child.Entries[0].Area();
if (areaIncrease < minAreaIncrease)
{
minAreaIncrease = areaIncrease;
bestChild = child;
}
}
return ChooseLeaf(bestChild, rect);
}
private void SplitNode(RTreeNode node)
{
// Simplified linear split algorithm
List<Rectangle> entries = node.Entries;
entries.Sort((a, b) => a.MinX.CompareTo(b.MinX));
RTreeNode newNode = new RTreeNode(node.IsLeaf);
int splitIndex = entries.Count / 2;
newNode.Entries.AddRange(entries.Skip(splitIndex).ToList());
node.Entries = entries.Take(splitIndex).ToList();
if (node == root)
{
RTreeNode newRoot = new RTreeNode(false);
newRoot.Children.Add(node);
newRoot.Children.Add(newNode);
newRoot.Entries.Add(Rectangle.Union(node.Entries[0], newNode.Entries[0]));
root = newRoot;
}
else
{
RTreeNode? parent = FindParent(root, node) ?? throw new InvalidOperationException("Parent not found");
parent.Children.Add(newNode);
if (parent.Children.Count > maxEntries)
{
SplitNode(parent);
}
}
}
private RTreeNode? FindParent(RTreeNode root, RTreeNode node)
{
foreach (RTreeNode child in root.Children)
{
if (child.Children.Contains(node))
{
return root;
}
else
{
RTreeNode? parent = FindParent(child, node);
if (parent != null)
{
return parent;
}
}
}
return null;
}
public List<Rectangle> Search(Rectangle query)
{
List<Rectangle> result = new List<Rectangle>();
Search(root, query, result);
return result;
}
private static void Search(RTreeNode node, Rectangle query, List<Rectangle> result)
{
foreach (Rectangle entry in node.Entries)
{
if (query.Intersects(entry))
{
if (node.IsLeaf)
{
result.Add(entry);
}
else
{
foreach (RTreeNode child in node.Children)
{
Search(child, query, result);
}
}
}
}
}
}