/usr/share/perl5/OSM/QuadTree.pm is in libosm-gary68-perl 0.0~svn26727-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | package OSM::QuadTree;
use strict;
use Carp;
our $VERSION = 0.1;
1;
###############################
#
# sub new() - constructor
#
# Arguments are a hash:
#
# -xmin => minimum x value
# -xmax => maximum x value
# -ymin => minimum y value
# -ymax => maximum y value
# -depth => depth of tree
#
# Creating a new QuadTree objects automatically
# segments the given area into quadtrees of the
# specified depth.
#
###############################
sub new {
my $self = shift;
my $class = ref($self) || $self;
my $obj = bless {} => $class;
$obj->{BACKREF} = {};
$obj->{OBJECTS} = [];
$obj->{ORIGIN} = [0, 0];
$obj->{SCALE} = 1;
my %args = @_;
for my $arg (qw/xmin ymin xmax ymax depth/) {
unless (exists $args{"-$arg"}) {
carp "- must specify $arg";
return undef;
}
$obj->{uc $arg} = $args{"-$arg"};
}
$obj->_segment;
return $obj;
}
###############################
#
# sub _segment() - private method
#
# This method does the actual segmentation
# and stores everything internally.
#
###############################
sub _segment {
my $obj = shift;
$obj->_addLevel(
$obj->{XMIN},
$obj->{YMIN},
$obj->{XMAX},
$obj->{YMAX},
1, # current depth
0, # current index
undef, # parent index
);
}
###############################
#
# sub _addLevel() - private method
#
# This method segments a given area
# and adds a level to the tree.
#
###############################
sub _addLevel {
my ($obj,
$xmin,
$ymin,
$xmax,
$ymax,
$curDepth,
$index,
$parent,
) = @_;
$obj->{AREA} [$index] = [$xmin, $ymin, $xmax, $ymax];
$obj->{PARENT} [$index] = $parent;
$obj->{CHILDREN}[$index] = [];
$obj->{OBJECTS} [$index] = [];
if (defined $parent) {
push @{$obj->{CHILDREN}[$parent]} => $index;
}
return if $curDepth == $obj->{DEPTH};
my $xmid = $xmin + ($xmax - $xmin) / 2;
my $ymid = $ymin + ($ymax - $ymin) / 2;
# now segment in the following order (doesn't matter):
# top left, top right, bottom left, bottom right
$obj->_addLevel($xmin, $ymid, $xmid, $ymax, # tl
$curDepth + 1, 4 * $index + 1, $index);
$obj->_addLevel($xmid, $ymid, $xmax, $ymax, # tr
$curDepth + 1, 4 * $index + 2, $index);
$obj->_addLevel($xmin, $ymin, $xmid, $ymid, # bl
$curDepth + 1, 4 * $index + 3, $index);
$obj->_addLevel($xmid, $ymin, $xmax, $ymid, # br
$curDepth + 1, 4 * $index + 4, $index);
}
###############################
#
# sub add() - public method
#
# This method adds an object to the tree.
# The arguments are a unique tag to identify
# the object, and the bounding box of the object.
# It automatically assigns the proper quadtree
# sections to each object.
#
###############################
sub add {
my ($self,
$objRef,
@coords,
) = @_;
# assume that $objRef is unique.
# assume coords are (xmin, ymix, xmax, ymax).
# modify coords according to window.
@coords = $self->_adjustCoords(@coords);
($coords[0], $coords[2]) = ($coords[2], $coords[0]) if
$coords[2] < $coords[0];
($coords[1], $coords[3]) = ($coords[3], $coords[1]) if
$coords[3] < $coords[1];
$self->_addObjToChild(
0, # current index
$objRef,
@coords,
);
}
###############################
#
# sub _addObjToChild() - private method
#
# This method is used internally. Given
# a tree segment, an object and its area,
# it checks to see whether the object is to
# be included in the segment or not.
# The object is not included if it does not
# overlap the segment.
#
###############################
sub _addObjToChild {
my ($self,
$index,
$objRef,
@coords,
) = @_;
# first check if obj overlaps current segment.
# if not, return.
my ($cxmin, $cymin, $cxmax, $cymax) = @{$self->{AREA}[$index]};
return if
$coords[0] > $cxmax ||
$coords[2] < $cxmin ||
$coords[1] > $cymax ||
$coords[3] < $cymin;
# Only add the object to the segment if we are at the last
# level of the tree.
# Else, keep traversing down.
unless (@{$self->{CHILDREN}[$index]}) {
push @{$self->{OBJECTS}[$index]} => $objRef; # points from leaf to object
push @{$self->{BACKREF}{$objRef}} => $index; # points from object to leaf
} else {
# Now, traverse down the hierarchy.
for my $child (@{$self->{CHILDREN}[$index]}) {
$self->_addObjToChild(
$child,
$objRef,
@coords,
);
}
}
}
###############################
#
# sub delete() - public method
#
# This method deletes an object from the tree.
#
###############################
sub delete {
my ($self,
$objRef,
) = @_;
return unless exists $self->{BACKREF}{$objRef};
for my $i (@{$self->{BACKREF}{$objRef}}) {
$self->{OBJECTS}[$i] = grep {$_ ne $objRef} @{$self->{OBJECTS}[$i]};
}
delete $self->{BACKREF}{$objRef};
}
###############################
#
# sub getEnclosedObjects() - public method
#
# This method takes an area, and returns all objects
# enclosed in that area.
#
###############################
sub getEnclosedObjects {
my ($self,
@coords) = @_;
$self->{TEMP} = [];
@coords = $self->_adjustCoords(@coords);
$self->_checkOverlap(
0, # current index
@coords,
);
# uniquify {TEMP}.
my %temp;
@temp{@{$self->{TEMP}}} = undef;
# PS. I don't check explicitly if those objects
# are enclosed in the given area. They are just
# part of the segments that are enclosed in the
# given area. TBD.
return [keys %temp];
}
###############################
#
# sub _adjustCoords() - private method
#
# This method adjusts the given coordinates
# according to the stored window. This is used
# when we 'zoom in' to avoid searching in areas
# that are not visible in the canvas.
#
###############################
sub _adjustCoords {
my ($self, @coords) = @_;
# modify coords according to window.
$_ = $self->{ORIGIN}[0] + $_ / $self->{SCALE}
for $coords[0], $coords[2];
$_ = $self->{ORIGIN}[1] + $_ / $self->{SCALE}
for $coords[1], $coords[3];
return @coords;
}
###############################
#
# sub _checkOverlap() - private method
#
# This method checks if the given coordinates overlap
# the specified tree segment. If not, nothing happens.
# If it does overlap, then it is called recuresively
# on all the segment's children. If the segment is a
# leaf, then its associated objects are pushed onto
# a temporary array for later access.
#
###############################
sub _checkOverlap {
my ($self,
$index,
@coords,
) = @_;
# first check if obj overlaps current segment.
# if not, return.
my ($cxmin, $cymin, $cxmax, $cymax) = @{$self->{AREA}[$index]};
return if
$coords[0] >= $cxmax ||
$coords[2] <= $cxmin ||
$coords[1] >= $cymax ||
$coords[3] <= $cymin;
unless (@{$self->{CHILDREN}[$index]}) {
push @{$self->{TEMP}} => @{$self->{OBJECTS}[$index]};
} else {
# Now, traverse down the hierarchy.
for my $child (@{$self->{CHILDREN}[$index]}) {
$self->_checkOverlap(
$child,
@coords,
);
}
}
}
###############################
#
# sub setWindow() - public method
#
# This method takes an area as input, and
# sets it as the active window. All new
# calls to any method will refer to that area.
#
###############################
sub setWindow {
my ($self, $sx, $sy, $s) = @_;
$self->{ORIGIN}[0] += $sx / $self->{SCALE};
$self->{ORIGIN}[1] += $sy / $self->{SCALE};
$self->{SCALE} *= $s;
}
###############################
#
# sub setWindow() - public method
# This resets the window.
#
###############################
sub resetWindow {
my $self = shift;
$self->{ORIGIN}[$_] = 0 for 0 .. 1;
$self->{SCALE} = 1;
}
|