| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Algorithm::QuadTree::PP; |
|
2
|
|
|
|
|
|
|
$Algorithm::QuadTree::PP::VERSION = '1.0'; |
|
3
|
8
|
|
|
8
|
|
44
|
use strict; |
|
|
8
|
|
|
|
|
14
|
|
|
|
8
|
|
|
|
|
209
|
|
|
4
|
8
|
|
|
8
|
|
28
|
use warnings; |
|
|
8
|
|
|
|
|
9
|
|
|
|
8
|
|
|
|
|
299
|
|
|
5
|
8
|
|
|
8
|
|
28
|
use Exporter qw(import); |
|
|
8
|
|
|
|
|
10
|
|
|
|
8
|
|
|
|
|
225
|
|
|
6
|
|
|
|
|
|
|
|
|
7
|
8
|
|
|
8
|
|
38
|
use Scalar::Util qw(weaken); |
|
|
8
|
|
|
|
|
44
|
|
|
|
8
|
|
|
|
|
636
|
|
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our @EXPORT = qw( |
|
10
|
|
|
|
|
|
|
_AQT_init |
|
11
|
|
|
|
|
|
|
_AQT_deinit |
|
12
|
|
|
|
|
|
|
_AQT_addObject |
|
13
|
|
|
|
|
|
|
_AQT_findObjects |
|
14
|
|
|
|
|
|
|
_AQT_delete |
|
15
|
|
|
|
|
|
|
_AQT_clear |
|
16
|
|
|
|
|
|
|
); |
|
17
|
|
|
|
|
|
|
|
|
18
|
8
|
|
|
8
|
|
50
|
use constant UNIQUE_RESULTS => 1; |
|
|
8
|
|
|
|
|
14
|
|
|
|
8
|
|
|
|
|
686
|
|
|
19
|
|
|
|
|
|
|
|
|
20
|
8
|
|
|
8
|
|
36
|
use constant SHAPE_CIRCLE => 1; |
|
|
8
|
|
|
|
|
18
|
|
|
|
8
|
|
|
|
|
309
|
|
|
21
|
8
|
|
|
8
|
|
40
|
use constant SHAPE_RECTANGLE => 2; |
|
|
8
|
|
|
|
|
8
|
|
|
|
8
|
|
|
|
|
10116
|
|
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub _buildShape |
|
24
|
|
|
|
|
|
|
{ |
|
25
|
231
|
|
|
231
|
|
377
|
my (@coords) = @_; |
|
26
|
231
|
|
|
|
|
441
|
pop @coords while @coords > 4; |
|
27
|
|
|
|
|
|
|
|
|
28
|
231
|
100
|
|
|
|
346
|
my $shape_type = @coords == 3 ? SHAPE_CIRCLE : SHAPE_RECTANGLE; |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# pre-calculate some of the circle characteristics |
|
31
|
231
|
100
|
|
|
|
389
|
if ($shape_type == SHAPE_CIRCLE) { |
|
32
|
19
|
|
|
|
|
32
|
my $contained_radius = $coords[2] / sqrt(2); |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
# inner box for this circle - fully contained within the circle |
|
35
|
19
|
|
|
|
|
70
|
unshift @coords, ( |
|
36
|
|
|
|
|
|
|
$coords[0] - $contained_radius, |
|
37
|
|
|
|
|
|
|
$coords[1] - $contained_radius, |
|
38
|
|
|
|
|
|
|
$coords[0] + $contained_radius, |
|
39
|
|
|
|
|
|
|
$coords[1] + $contained_radius, |
|
40
|
|
|
|
|
|
|
); |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# avoid squaring the radius on each iteration |
|
43
|
19
|
|
|
|
|
96
|
$coords[7] = $coords[6] ** 2; |
|
44
|
|
|
|
|
|
|
} |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
# -1 is always shape type. We use array for speed. |
|
47
|
231
|
|
|
|
|
298
|
push @coords, $shape_type; |
|
48
|
231
|
|
|
|
|
479
|
return \@coords; |
|
49
|
|
|
|
|
|
|
} |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub _shapesOverlap |
|
52
|
|
|
|
|
|
|
{ |
|
53
|
1126
|
|
|
1126
|
|
1242
|
my ($s1, $s2) = @_; |
|
54
|
1126
|
|
|
|
|
1163
|
my $type = $s1->[-1]; |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
# same element |
|
57
|
1126
|
100
|
|
|
|
1419
|
if ($type == $s2->[-1]) { |
|
58
|
1046
|
100
|
|
|
|
1500
|
if ($type == SHAPE_CIRCLE) { |
|
|
|
50
|
|
|
|
|
|
|
59
|
3
|
|
|
|
|
5
|
my $dist_x = $s1->[4] - $s2->[4]; |
|
60
|
3
|
|
|
|
|
2
|
my $dist_y = $s1->[5] - $s2->[5]; |
|
61
|
3
|
|
|
|
|
6
|
my $diagonal = $s1->[6] + $s2->[6]; |
|
62
|
|
|
|
|
|
|
|
|
63
|
3
|
|
|
|
|
18
|
return $dist_x ** 2 + $dist_y ** 2 |
|
64
|
|
|
|
|
|
|
<= $diagonal ** 2; |
|
65
|
|
|
|
|
|
|
} |
|
66
|
|
|
|
|
|
|
elsif ($type == SHAPE_RECTANGLE) { |
|
67
|
1043
|
|
100
|
|
|
4031
|
return $s1->[0] <= $s2->[2] && |
|
68
|
|
|
|
|
|
|
$s1->[2] >= $s2->[0] && |
|
69
|
|
|
|
|
|
|
$s1->[1] <= $s2->[3] && |
|
70
|
|
|
|
|
|
|
$s1->[3] >= $s2->[1]; |
|
71
|
|
|
|
|
|
|
} |
|
72
|
|
|
|
|
|
|
} |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# different elements - circle first |
|
75
|
80
|
50
|
|
|
|
116
|
($s1, $s2) = ($s2, $s1) |
|
76
|
|
|
|
|
|
|
unless $type == SHAPE_CIRCLE; |
|
77
|
|
|
|
|
|
|
|
|
78
|
80
|
100
|
|
|
|
160
|
my $cx = $s1->[4] < $s2->[0] |
|
|
|
100
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
? $s2->[0] - $s1->[4] |
|
80
|
|
|
|
|
|
|
: $s1->[4] > $s2->[2] |
|
81
|
|
|
|
|
|
|
? $s2->[2] - $s1->[4] |
|
82
|
|
|
|
|
|
|
: 0 |
|
83
|
|
|
|
|
|
|
; |
|
84
|
|
|
|
|
|
|
|
|
85
|
80
|
100
|
|
|
|
157
|
my $cy = $s1->[5] < $s2->[1] |
|
|
|
100
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
? $s2->[1] - $s1->[5] |
|
87
|
|
|
|
|
|
|
: $s1->[5] > $s2->[3] |
|
88
|
|
|
|
|
|
|
? $s2->[3] - $s1->[5] |
|
89
|
|
|
|
|
|
|
: 0 |
|
90
|
|
|
|
|
|
|
; |
|
91
|
|
|
|
|
|
|
|
|
92
|
80
|
|
|
|
|
275
|
return $cx ** 2 + $cy ** 2 |
|
93
|
|
|
|
|
|
|
<= $s1->[7]; |
|
94
|
|
|
|
|
|
|
} |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub _shapeContained |
|
97
|
|
|
|
|
|
|
{ |
|
98
|
1122
|
|
|
1122
|
|
1244
|
my ($inner_s, $s) = @_; |
|
99
|
|
|
|
|
|
|
|
|
100
|
1122
|
|
100
|
|
|
2271
|
return $s->[0] <= $inner_s->[0] && |
|
101
|
|
|
|
|
|
|
$s->[2] >= $inner_s->[2] && |
|
102
|
|
|
|
|
|
|
$s->[1] <= $inner_s->[1] && |
|
103
|
|
|
|
|
|
|
$s->[3] >= $inner_s->[3]; |
|
104
|
|
|
|
|
|
|
} |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
# recursive method which adds levels to the quadtree |
|
107
|
|
|
|
|
|
|
sub _addLevel |
|
108
|
|
|
|
|
|
|
{ |
|
109
|
94
|
|
|
94
|
|
149
|
my ($self, $depth, $parent, @coords) = @_; |
|
110
|
94
|
|
|
|
|
131
|
my $node = { |
|
111
|
|
|
|
|
|
|
PARENT => $parent, |
|
112
|
|
|
|
|
|
|
OBJECTS => [], |
|
113
|
|
|
|
|
|
|
HAS_OBJECTS => 0, |
|
114
|
|
|
|
|
|
|
AREA => _buildShape(@coords), |
|
115
|
|
|
|
|
|
|
DEPTH => $depth, |
|
116
|
|
|
|
|
|
|
}; |
|
117
|
|
|
|
|
|
|
|
|
118
|
94
|
100
|
|
|
|
162
|
weaken $node->{PARENT} if $parent; |
|
119
|
|
|
|
|
|
|
|
|
120
|
94
|
100
|
|
|
|
137
|
if ($depth < $self->{DEPTH}) { |
|
121
|
22
|
|
|
|
|
37
|
my ($xmin, $ymin, $xmax, $ymax) = @coords; |
|
122
|
22
|
|
|
|
|
43
|
my $xmid = $xmin + ($xmax - $xmin) / 2; |
|
123
|
22
|
|
|
|
|
29
|
my $ymid = $ymin + ($ymax - $ymin) / 2; |
|
124
|
22
|
|
|
|
|
23
|
$depth += 1; |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
# segment in the following order: |
|
127
|
|
|
|
|
|
|
# top left, top right, bottom left, bottom right |
|
128
|
|
|
|
|
|
|
$node->{CHILDREN} = [ |
|
129
|
22
|
|
|
|
|
83
|
_addLevel($self, $depth, $node, $xmin, $ymid, $xmid, $ymax), |
|
130
|
|
|
|
|
|
|
_addLevel($self, $depth, $node, $xmid, $ymid, $xmax, $ymax), |
|
131
|
|
|
|
|
|
|
_addLevel($self, $depth, $node, $xmin, $ymin, $xmid, $ymid), |
|
132
|
|
|
|
|
|
|
_addLevel($self, $depth, $node, $xmid, $ymin, $xmax, $ymid), |
|
133
|
|
|
|
|
|
|
]; |
|
134
|
|
|
|
|
|
|
} |
|
135
|
|
|
|
|
|
|
|
|
136
|
94
|
|
|
|
|
196
|
return $node; |
|
137
|
|
|
|
|
|
|
} |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
# this private method executes $code on every leaf node of the tree |
|
140
|
|
|
|
|
|
|
# which is within the circular shape |
|
141
|
|
|
|
|
|
|
sub _loopOnNodes |
|
142
|
|
|
|
|
|
|
{ |
|
143
|
138
|
|
|
138
|
|
198
|
my ($self, $finding, $shape) = @_; |
|
144
|
|
|
|
|
|
|
|
|
145
|
138
|
|
|
|
|
152
|
my @nodes; |
|
146
|
138
|
|
|
|
|
212
|
my @loopargs = $self->{ROOT}; |
|
147
|
138
|
|
|
|
|
230
|
my @loopargs_contained; |
|
148
|
|
|
|
|
|
|
my $fully_contained; |
|
149
|
138
|
|
|
|
|
0
|
my $current; |
|
150
|
|
|
|
|
|
|
|
|
151
|
138
|
|
|
|
|
271
|
while ($current = shift @loopargs) { |
|
152
|
1186
|
100
|
100
|
|
|
2272
|
next if $finding && !$current->{HAS_OBJECTS}; |
|
153
|
|
|
|
|
|
|
|
|
154
|
1122
|
|
|
|
|
1399
|
$fully_contained = _shapeContained($current->{AREA}, $shape); |
|
155
|
1122
|
100
|
100
|
|
|
1870
|
next if !$fully_contained && !_shapesOverlap($shape, $current->{AREA}); |
|
156
|
|
|
|
|
|
|
|
|
157
|
464
|
100
|
|
|
|
561
|
if ($finding) { |
|
158
|
293
|
|
|
|
|
377
|
push @nodes, $current; |
|
159
|
293
|
100
|
|
|
|
572
|
next unless $current->{CHILDREN}; |
|
160
|
|
|
|
|
|
|
|
|
161
|
155
|
100
|
|
|
|
215
|
if ($fully_contained) { |
|
162
|
2
|
|
|
|
|
3
|
push @loopargs_contained, @{$current->{CHILDREN}}; |
|
|
2
|
|
|
|
|
5
|
|
|
163
|
|
|
|
|
|
|
} |
|
164
|
|
|
|
|
|
|
else { |
|
165
|
153
|
|
|
|
|
160
|
push @loopargs, @{$current->{CHILDREN}}; |
|
|
153
|
|
|
|
|
335
|
|
|
166
|
|
|
|
|
|
|
} |
|
167
|
|
|
|
|
|
|
} |
|
168
|
|
|
|
|
|
|
else { |
|
169
|
171
|
|
|
|
|
205
|
$current->{HAS_OBJECTS} = 1; |
|
170
|
171
|
100
|
66
|
|
|
328
|
if ($fully_contained || !$current->{CHILDREN}) { |
|
171
|
62
|
|
|
|
|
109
|
push @nodes, $current; |
|
172
|
|
|
|
|
|
|
} |
|
173
|
|
|
|
|
|
|
else { |
|
174
|
109
|
|
|
|
|
102
|
push @loopargs, @{$current->{CHILDREN}}; |
|
|
109
|
|
|
|
|
221
|
|
|
175
|
|
|
|
|
|
|
} |
|
176
|
|
|
|
|
|
|
} |
|
177
|
|
|
|
|
|
|
} |
|
178
|
|
|
|
|
|
|
|
|
179
|
138
|
100
|
|
|
|
207
|
if ($finding) { |
|
180
|
81
|
|
|
|
|
152
|
while (my $current = shift @loopargs_contained) { |
|
181
|
28
|
100
|
|
|
|
40
|
next if !$current->{HAS_OBJECTS}; |
|
182
|
|
|
|
|
|
|
|
|
183
|
22
|
|
|
|
|
22
|
push @nodes, $current; |
|
184
|
5
|
|
|
|
|
17
|
push @loopargs_contained, @{$current->{CHILDREN}} |
|
185
|
22
|
100
|
|
|
|
32
|
if $current->{CHILDREN}; |
|
186
|
|
|
|
|
|
|
} |
|
187
|
|
|
|
|
|
|
} |
|
188
|
|
|
|
|
|
|
|
|
189
|
138
|
|
|
|
|
279
|
return \@nodes; |
|
190
|
|
|
|
|
|
|
} |
|
191
|
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
sub _clearHasObjects |
|
193
|
|
|
|
|
|
|
{ |
|
194
|
2
|
|
|
2
|
|
3
|
my $node = shift; |
|
195
|
|
|
|
|
|
|
|
|
196
|
2
|
50
|
|
|
|
3
|
if ($node->{CHILDREN}) { |
|
197
|
2
|
|
|
|
|
3
|
for my $child (@{$node->{CHILDREN}}) { |
|
|
2
|
|
|
|
|
2
|
|
|
198
|
2
|
50
|
|
|
|
5
|
return if $child->{HAS_OBJECTS}; |
|
199
|
|
|
|
|
|
|
} |
|
200
|
|
|
|
|
|
|
} |
|
201
|
|
|
|
|
|
|
|
|
202
|
0
|
|
|
|
|
0
|
$node->{HAS_OBJECTS} = 0; |
|
203
|
0
|
0
|
|
|
|
0
|
if ($node->{PARENT}) { |
|
204
|
0
|
|
|
|
|
0
|
_clearHasObjects($node->{PARENT}); |
|
205
|
|
|
|
|
|
|
} |
|
206
|
|
|
|
|
|
|
} |
|
207
|
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
sub _AQT_init |
|
209
|
|
|
|
|
|
|
{ |
|
210
|
6
|
|
|
6
|
|
14
|
my $obj = shift; |
|
211
|
|
|
|
|
|
|
|
|
212
|
6
|
|
|
|
|
17
|
$obj->{BACKREF} = {}; |
|
213
|
|
|
|
|
|
|
$obj->{ROOT} = _addLevel( |
|
214
|
|
|
|
|
|
|
$obj, |
|
215
|
|
|
|
|
|
|
1, #current depth |
|
216
|
|
|
|
|
|
|
undef, # parent - none |
|
217
|
|
|
|
|
|
|
$obj->{XMIN}, |
|
218
|
|
|
|
|
|
|
$obj->{YMIN}, |
|
219
|
|
|
|
|
|
|
$obj->{XMAX}, |
|
220
|
|
|
|
|
|
|
$obj->{YMAX}, |
|
221
|
6
|
|
|
|
|
49
|
); |
|
222
|
|
|
|
|
|
|
} |
|
223
|
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
sub _AQT_deinit |
|
225
|
|
|
|
6
|
|
|
{ |
|
226
|
|
|
|
|
|
|
# do nothing in PP implementation |
|
227
|
|
|
|
|
|
|
} |
|
228
|
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
sub _AQT_addObject |
|
230
|
|
|
|
|
|
|
{ |
|
231
|
57
|
|
|
57
|
|
78
|
my ($self, $object, @coords) = @_; |
|
232
|
57
|
|
|
|
|
82
|
my $shape = _buildShape(@coords); |
|
233
|
|
|
|
|
|
|
|
|
234
|
57
|
|
|
|
|
84
|
my $nodes = _loopOnNodes($self, 0, $shape); |
|
235
|
57
|
|
|
|
|
81
|
for my $node (@$nodes) { |
|
236
|
62
|
|
|
|
|
71
|
push @{$node->{OBJECTS}}, $object; |
|
|
62
|
|
|
|
|
115
|
|
|
237
|
|
|
|
|
|
|
} |
|
238
|
|
|
|
|
|
|
|
|
239
|
57
|
50
|
|
|
|
201
|
$self->{BACKREF}{$object} = $shape |
|
240
|
|
|
|
|
|
|
unless @$nodes == 0; |
|
241
|
|
|
|
|
|
|
} |
|
242
|
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
sub _AQT_findObjects |
|
244
|
|
|
|
|
|
|
{ |
|
245
|
80
|
|
|
80
|
|
132
|
my ($self, @coords) = @_; |
|
246
|
80
|
|
|
|
|
171
|
my $shape = _buildShape(@coords); |
|
247
|
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
# map returned nodes to an array containing all of |
|
249
|
|
|
|
|
|
|
# their objects |
|
250
|
80
|
|
|
|
|
119
|
my %hash; |
|
251
|
80
|
|
|
|
|
88
|
foreach my $node (@{_loopOnNodes($self, 1, $shape)}) { |
|
|
80
|
|
|
|
|
143
|
|
|
252
|
312
|
|
|
|
|
298
|
foreach my $object (@{$node->{OBJECTS}}) { |
|
|
312
|
|
|
|
|
438
|
|
|
253
|
170
|
|
|
|
|
323
|
$hash{$object} = $object; |
|
254
|
|
|
|
|
|
|
} |
|
255
|
|
|
|
|
|
|
} |
|
256
|
|
|
|
|
|
|
|
|
257
|
80
|
100
|
|
|
|
227
|
if ($self->{CHECK}) { |
|
258
|
6
|
|
|
|
|
11
|
my $backref = $self->{BACKREF}; |
|
259
|
6
|
|
|
|
|
13
|
foreach my $key (keys %hash) { |
|
260
|
|
|
|
|
|
|
delete $hash{$key} |
|
261
|
7
|
100
|
|
|
|
15
|
unless _shapesOverlap($shape, $backref->{$key}); |
|
262
|
|
|
|
|
|
|
} |
|
263
|
|
|
|
|
|
|
} |
|
264
|
|
|
|
|
|
|
|
|
265
|
80
|
|
|
|
|
431
|
return [values %hash]; |
|
266
|
|
|
|
|
|
|
} |
|
267
|
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
sub _AQT_delete |
|
269
|
|
|
|
|
|
|
{ |
|
270
|
1
|
|
|
1
|
|
2
|
my ($self, $object) = @_; |
|
271
|
|
|
|
|
|
|
|
|
272
|
1
|
50
|
|
|
|
3
|
return unless exists $self->{BACKREF}{$object}; |
|
273
|
|
|
|
|
|
|
|
|
274
|
1
|
|
|
|
|
2
|
for my $node (@{_loopOnNodes($self, 1, $self->{BACKREF}{$object})}) { |
|
|
1
|
|
|
|
|
3
|
|
|
275
|
3
|
|
|
|
|
3
|
@{$node->{OBJECTS}} = grep {$_ ne $object} @{$node->{OBJECTS}}; |
|
|
3
|
|
|
|
|
5
|
|
|
|
2
|
|
|
|
|
4
|
|
|
|
3
|
|
|
|
|
19
|
|
|
276
|
3
|
100
|
|
|
|
3
|
_clearHasObjects($node) if !@{$node->{OBJECTS}}; |
|
|
3
|
|
|
|
|
8
|
|
|
277
|
|
|
|
|
|
|
} |
|
278
|
|
|
|
|
|
|
|
|
279
|
1
|
|
|
|
|
4
|
delete $self->{BACKREF}{$object}; |
|
280
|
|
|
|
|
|
|
} |
|
281
|
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
sub _AQT_clear |
|
283
|
|
|
|
|
|
|
{ |
|
284
|
3
|
|
|
3
|
|
8
|
my ($self) = @_; |
|
285
|
|
|
|
|
|
|
|
|
286
|
3
|
|
|
|
|
11
|
my @loopargs = $self->{ROOT}; |
|
287
|
3
|
|
|
|
|
33
|
while (my $current = shift @loopargs) { |
|
288
|
31
|
100
|
|
|
|
86
|
next unless $current->{HAS_OBJECTS}; |
|
289
|
|
|
|
|
|
|
|
|
290
|
30
|
|
|
|
|
32
|
@{$current->{OBJECTS}} = (); |
|
|
30
|
|
|
|
|
48
|
|
|
291
|
30
|
|
|
|
|
33
|
$current->{HAS_OBJECTS} = 0; |
|
292
|
|
|
|
|
|
|
|
|
293
|
30
|
100
|
|
|
|
77
|
if ($current->{CHILDREN}) { |
|
294
|
7
|
|
|
|
|
9
|
push @loopargs, @{$current->{CHILDREN}}; |
|
|
7
|
|
|
|
|
19
|
|
|
295
|
|
|
|
|
|
|
} |
|
296
|
|
|
|
|
|
|
} |
|
297
|
|
|
|
|
|
|
|
|
298
|
3
|
|
|
|
|
7
|
%{$self->{BACKREF}} = (); |
|
|
3
|
|
|
|
|
25
|
|
|
299
|
|
|
|
|
|
|
} |
|
300
|
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
1; |
|
302
|
|
|
|
|
|
|
|