-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRTDirNode.java
194 lines (163 loc) · 6 KB
/
RTDirNode.java
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
package rtree;
import java.util.ArrayList;
import java.util.List;
import rtree.Constants;
public class RTDirNode extends RTNode {
protected List<RTNode> children;
public RTDirNode(RTree rtree, RTNode parent, int level) {
super(rtree, parent, level);
children = new ArrayList<RTNode>();
}
public RTNode getChild(int index) {
return children.get(index);
}
@Override
public RTDataNode chooseLeaf(Rectangle rectangle) {
int index;
switch (rtree.getTreeType()) {
case Constants.RTREE_LINEAR:
case Constants.RTREE_QUADRATIC:
case Constants.RTREE_EXPONENTIAL:
index = findLeastEnlargement(rectangle);
break;
case Constants.RSTAR:
if (level == 1)
{
index = findLeastOverlap(rectangle);
} else {
index = findLeastEnlargement(rectangle);
}
break;
default:
throw new IllegalStateException("Invalid tree type.");
}
insertIndex = index;
return getChild(index).chooseLeaf(rectangle);
}
private int findLeastOverlap(Rectangle rectangle) {
float overlap = Float.POSITIVE_INFINITY;
int sel = -1;
for (int i = 0; i < usedSpace; i++) {
RTNode node = getChild(i);
float ol = 0;
for (int j = 0; j < node.datas.length; j++) {
ol += rectangle.intersectingArea(node.datas[j]);
}
if (ol < overlap) {
overlap = ol;
sel = i;
}
else if (ol == overlap) {
double area1 = datas[i].getUnionRectangle(rectangle).getArea() - datas[i].getArea();
double area2 = datas[sel].getUnionRectangle(rectangle).getArea() - datas[sel].getArea();
if (area1 == area2) {
sel = (datas[sel].getArea() <= datas[i].getArea()) ? sel : i;
} else {
sel = (area1 < area2) ? i : sel;
}
}
}
return sel;
}
private int findLeastEnlargement(Rectangle rectangle) {
double area = Double.POSITIVE_INFINITY;
int sel = -1;
for (int i = 0; i < usedSpace; i++) {
double enlargement = datas[i].getUnionRectangle(rectangle).getArea() - datas[i].getArea();
if (enlargement < area) {
area = enlargement;
sel = i;
} else if (enlargement == area) {
sel = (datas[sel].getArea() < datas[i].getArea()) ? sel : i;
}
}
return sel;
}
public void adjustTree(RTNode node1, RTNode node2) {
datas[insertIndex] = node1.getNodeRectangle();
children.set(insertIndex, node1);
if (node2 != null) {
insert(node2);
}
else if (!isRoot()) {
RTDirNode parent = (RTDirNode) getParent();
parent.adjustTree(this, null);
}
}
protected boolean insert(RTNode node) {
if (usedSpace < rtree.getNodeCapacity()) {
datas[usedSpace++] = node.getNodeRectangle();
children.add(node);
node.parent = this;
RTDirNode parent = (RTDirNode) getParent();
if (parent != null)
{
parent.adjustTree(this, null);
}
return false;
} else {
RTDirNode[] a = splitIndex(node);
RTDirNode n = a[0];
RTDirNode nn = a[1];
if (isRoot()) {
RTDirNode newRoot = new RTDirNode(rtree, Constants.NULL, level + 1);
newRoot.addData(n.getNodeRectangle());
newRoot.addData(nn.getNodeRectangle());
newRoot.children.add(n);
newRoot.children.add(nn);
n.parent = newRoot;
nn.parent = newRoot;
rtree.setRoot(newRoot);
} else {
RTDirNode p = (RTDirNode) getParent();
p.adjustTree(n, nn);
}
}
return true;
}
private RTDirNode[] splitIndex(RTNode node) {
int[][] group = null;
switch (rtree.getTreeType()) {
case Constants.RTREE_LINEAR:
break;
case Constants.RTREE_QUADRATIC:
group = quadraticSplit(node.getNodeRectangle());
children.add(node);
node.parent = this;
break;
case Constants.RTREE_EXPONENTIAL:
break;
case Constants.RSTAR:
break;
default:
throw new IllegalStateException("Invalid tree type.");
}
RTDirNode index1 = new RTDirNode(rtree, parent, level);
RTDirNode index2 = new RTDirNode(rtree, parent, level);
int[] group1 = group[0];
int[] group2 = group[1];
for (int i = 0; i < group1.length; i++) {
index1.addData(datas[group1[i]]);
index1.children.add(this.children.get(group1[i]));
this.children.get(group1[i]).parent = index1;
}
for (int i = 0; i < group2.length; i++) {
index2.addData(datas[group2[i]]);
index2.children.add(this.children.get(group2[i]));
this.children.get(group2[i]).parent = index2;
}
return new RTDirNode[] { index1, index2 };
}
@Override
protected RTDataNode findLeaf(Rectangle rectangle) {
for (int i = 0; i < usedSpace; i++) {
if (datas[i].enclosure(rectangle)) {
deleteIndex = i;
RTDataNode leaf = children.get(i).findLeaf(rectangle);
if (leaf != null)
return leaf;
}
}
return null;
}
}