| 1 | package Algorithm::Points::MinimumDistance; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use vars qw( $VERSION ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | $VERSION = '0.01'; | |||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | =head1 NAME | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | Algorithm::Points::MinimumDistance - Works out the distance from each point to its nearest neighbour. Kinda. | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | Given a set of points in N-dimensional Euclidean space, works out for | ||||||||||||||||||||||||||||
| 13 | each point the distance to its nearest neighbour (unless its nearest | ||||||||||||||||||||||||||||
| 14 | neighbour isn't very close). The distance metric is a method; | ||||||||||||||||||||||||||||
| 15 | subclass and override it for non-Euclidean space. | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | use Algorithm::Points::MinimumDistance; | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | my @points = ( [1, 4], [3, 1], [5, 7] ); | ||||||||||||||||||||||||||||
| 22 | my $dists = Algorithm::Points::MinimumDistance->new( points => \@points ); | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | foreach my $point (@points) { | ||||||||||||||||||||||||||||
| 25 | print "($point->[0], $point->[1]: Nearest neighbour distance is " | ||||||||||||||||||||||||||||
| 26 | . $dists->distance( point => $point ) . "\n"; | ||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | print "Smallest distance between any two points is " | ||||||||||||||||||||||||||||
| 30 | . $dists->min_distance . "\n"; | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =head1 METHODS | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =over 4 | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =item B<new> | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | my @points = ( [1, 4], [3, 1], [5, 7] ); | ||||||||||||||||||||||||||||
| 39 | my $dists = Algorithm::Points::MinimumDistance->new( points => \@points, | ||||||||||||||||||||||||||||
| 40 | boxsize => 2 ); | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | C<points> should be an arrayref containing every point in your set. | ||||||||||||||||||||||||||||
| 43 | The representation of a point is as an N-element arrayref where N is | ||||||||||||||||||||||||||||
| 44 | the number of dimensions in which we are working. There is no check | ||||||||||||||||||||||||||||
| 45 | that all of your points have the right dimension. Whatever dimension | ||||||||||||||||||||||||||||
| 46 | the first point has is assumed to be the dimension of the space. So | ||||||||||||||||||||||||||||
| 47 | get it right. | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | C<boxsize> defaults to 20. | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =cut | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 4 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 54 | 4 | my ($class, %args) = @_; | |||||||||||||||||||||||||||
| 55 | 4 | my @points = @{ $args{points} }; | |||||||||||||||||||||||||||
| 56 | 4 | my $dim = scalar @{ $points[0] }; | |||||||||||||||||||||||||||
| 57 | 4 | ▶ | my $boxsize = $args{boxsize} || 20; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{'boxsize'} || 20
MC/DC0%0/1
$args{'boxsize'} || 20
$args{'boxsize'}
Truth table50%1/2
A
A= $args{'boxsize'}
| |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | # Precomputation for working out all boxes adjacent to a given box | ||||||||||||||||||||||||||||
| 60 | # (a point will be in all regions centred on its box or the | ||||||||||||||||||||||||||||
| 61 | # adjacent ones). | ||||||||||||||||||||||||||||
| 62 | # To find an adjacent box, vector-add one of these entries to it, | ||||||||||||||||||||||||||||
| 63 | # eg [1, 1] + [2, 0] - with a boxsize of 2, [3, 1] is adjacent to [1, 1]. | ||||||||||||||||||||||||||||
| 64 | 4 | my @offsets = ( [ -$boxsize ], [ 0 ] , [ $boxsize ] ); | |||||||||||||||||||||||||||
| 65 | 4 | foreach (2..$dim) { | |||||||||||||||||||||||||||
| 66 | 4 | @offsets = map { [ @$_, -$boxsize ], [ @$_, 0 ], [ @$_, $boxsize] } | |||||||||||||||||||||||||||
| 67 | @offsets; | ||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 4 | my $self = { dimensions => $dim, | |||||||||||||||||||||||||||
| 71 | points => \@points, | ||||||||||||||||||||||||||||
| 72 | boxsize => $boxsize, | ||||||||||||||||||||||||||||
| 73 | offsets => \@offsets, | ||||||||||||||||||||||||||||
| 74 | regions => { }, | ||||||||||||||||||||||||||||
| 75 | distances => { } | ||||||||||||||||||||||||||||
| 76 | }; | ||||||||||||||||||||||||||||
| 77 | 4 | bless $self, $class; | |||||||||||||||||||||||||||
| 78 | 4 | $self->_work_out_distances; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 4 | return $self; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =item B<box> | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | my @box = $nn->box( [1, 2] ); | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | Returns the identifier of the box that the point lives in. | ||||||||||||||||||||||||||||
| 88 | A box is labelled by its "bottom left-hand" corner point. | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =cut | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 36 | ▶ | sub box { | ||||||||||||||||||||||||||
|
Subroutine
box: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 93 | 36 | my ($self, $point) = @_; | |||||||||||||||||||||||||||
| 94 | 36 | my @box = map { $_ - ($_ % $self->{boxsize}) } @$point; | |||||||||||||||||||||||||||
| 95 | 36 | return @box; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 16 | ▶ | sub _offsets { | ||||||||||||||||||||||||||
|
Subroutine
_offsets: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 99 | 16 | my $self = shift; | |||||||||||||||||||||||||||
| 100 | 16 | return @{ $self->{offsets} }; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | # Accessor for the region centred on the box $args{centre}. Returns a ref to | ||||||||||||||||||||||||||||
| 104 | # an array of the points that are in that region. | ||||||||||||||||||||||||||||
| 105 | 161 | ▶ | sub region { | ||||||||||||||||||||||||||
|
Subroutine
region: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 106 | 161 | my ($self, %args) = @_; | |||||||||||||||||||||||||||
| 107 | 161 | my @centre = @{$args{centre}}; | |||||||||||||||||||||||||||
| 108 | 161 | my $key = join(",", @centre); | |||||||||||||||||||||||||||
| 109 | 161 | my $regions = $self->{regions}; | |||||||||||||||||||||||||||
| 110 | 161 | ▶ | $regions->{$key} ||= []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$regions->{$key} ||= []
MC/DC100%1/1
$regions->{$key} ||= []
$regions->{$key}
Truth table100%2/2
A
A= $regions->{$key}
| |||||||||||||||||||||||||||||
| 111 | 161 | return $regions->{$key}; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | # Shevek says: "This is where the CPU time goes, but, gentle reader, | ||||||||||||||||||||||||||||
| 115 | # please note that the complexity is LINEAR in the number of | ||||||||||||||||||||||||||||
| 116 | # points. This is shit. It's also trivial, so do it in XS." | ||||||||||||||||||||||||||||
| 117 | # Kake says: "I don't speak XS yet." | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 16 | ▶ | sub _hash { | ||||||||||||||||||||||||||
|
Subroutine
_hash: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 120 | 16 | my ($self, $point) = @_; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | # Compute the box in which $point lives. | ||||||||||||||||||||||||||||
| 123 | 16 | my @home_box = $self->box($point); | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | # $point lives in the region centred on this box, plus all surrounding | ||||||||||||||||||||||||||||
| 126 | # regions. Push it into each of these regions. A region is | ||||||||||||||||||||||||||||
| 127 | # identified by the box at its centre. | ||||||||||||||||||||||||||||
| 128 | 16 | foreach my $delta ( $self->_offsets ) { | |||||||||||||||||||||||||||
| 129 | 144 | my @centre = map { $delta->[$_] + $home_box[$_] } (0..$#home_box); | |||||||||||||||||||||||||||
| 130 | 144 | my $region = $self->region( centre => \@centre ); | |||||||||||||||||||||||||||
| 131 | 144 | push @$region, $point; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 4 | ▶ | sub _work_out_distances { | ||||||||||||||||||||||||||
|
Subroutine
_work_out_distances: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||
| 136 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 137 | 4 | my $points = $self->{points}; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | # Work out which points live in which regions. | ||||||||||||||||||||||||||||
| 140 | 4 | $self->_hash($_) foreach (@$points); | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | # For each point, check its distance from every other point inside | ||||||||||||||||||||||||||||
| 143 | # the region centred on its home box. All points outside this region | ||||||||||||||||||||||||||||
| 144 | # are at least a distance 'boxsize' away. | ||||||||||||||||||||||||||||
| 145 | 4 | foreach my $point (@$points) { | |||||||||||||||||||||||||||
| 146 | 16 | my @box = $self->box($point); | |||||||||||||||||||||||||||
| 147 | 16 | my $min; | |||||||||||||||||||||||||||
| 148 | 16 | my $region = $self->region( centre => \@box ); | |||||||||||||||||||||||||||
| 149 | 16 | foreach my $neighbour (@$region) { | |||||||||||||||||||||||||||
| 150 | 48 | ▶ | next if $neighbour == $point; # Reference equality | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 32 | my $d = $self->d($point, $neighbour); | |||||||||||||||||||||||||||
| 152 | 32 | ▶ | $min = $d if (!defined $min or $d < $min); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $min or $d < $min
MC/DC100%2/2
not defined $min or $d < $min
not defined $min
$d < $min
Truth table100%3/3
not(A) or B
A= not defined $minB= $d < $min
| |||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | 16 | ▶ | $min ||= $self->{boxsize}; | ||||||||||||||||||||||||||
|
Condition66%2/3
$min ||= $self->{"boxsize"}
MC/DC0%0/2
$min ||= $self->{"boxsize"}
$min
$self->{"boxsize"}
Truth table66%2/3
A ||= B
A= $minB= $self->{"boxsize"}
| |||||||||||||||||||||||||||||
| 155 | 16 | $self->_store_distance( point => $point, distance => $min ); | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 16 | ▶ | sub _store_distance { | ||||||||||||||||||||||||||
|
Subroutine
_store_distance: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 160 | 16 | my ($self, %args) = @_; | |||||||||||||||||||||||||||
| 161 | 16 | my ($point, $distance) = @args{ qw( point distance ) }; | |||||||||||||||||||||||||||
| 162 | 16 | my $key = join(",", @$point); | |||||||||||||||||||||||||||
| 163 | 16 | $self->{distances}{$key} = $distance; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | # Override this for a different metric. | ||||||||||||||||||||||||||||
| 167 | 32 | ▶ | sub d { | ||||||||||||||||||||||||||
|
Subroutine
d: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 168 | 32 | my ($self, $point1, $point2) = @_; | |||||||||||||||||||||||||||
| 169 | 32 | my $t = 0; | |||||||||||||||||||||||||||
| 170 | 32 | foreach (0..$#$point1) { | |||||||||||||||||||||||||||
| 171 | 64 | $t += ($point1->[$_] - $point2->[$_]) ** 2; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | 32 | return sqrt($t); | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | =item B<distance> | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | my $nn = Algorithm::Points::MinimumDistance->new( ... ); | ||||||||||||||||||||||||||||
| 179 | my $nn_dist = $nn->distance( point => [1, 4] ); | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | Returns the distance between the specified point and its nearest | ||||||||||||||||||||||||||||
| 182 | neighbour. The point should be one of your original set. There is no | ||||||||||||||||||||||||||||
| 183 | check that this is the case. Note that if a point has no particularly | ||||||||||||||||||||||||||||
| 184 | close neighbours, then C<boxsize> will be returned instead. | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | =cut | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 6 | ▶ | sub distance { | ||||||||||||||||||||||||||
|
Subroutine
distance: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 189 | 6 | my ($self, %args) = @_; | |||||||||||||||||||||||||||
| 190 | 6 | my $point = $args{point}; | |||||||||||||||||||||||||||
| 191 | 6 | my $key = join(",", @$point); | |||||||||||||||||||||||||||
| 192 | 6 | return $self->{distances}{$key}; | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | =item B<min_distance> | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | my $nn = Algorithm::Points::MinimumDistance->new( ... ); | ||||||||||||||||||||||||||||
| 198 | my $nn_dist = $nn->min_distance; | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | Returns the minimum nearest-neighbour distance for all points in the set. | ||||||||||||||||||||||||||||
| 201 | Or C<boxsize> if none of the points are close to each other. | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | =cut | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 3 | ▶ | sub min_distance { | ||||||||||||||||||||||||||
|
Subroutine
min_distance: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 206 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 207 | 3 | my $dists = $self->{distances}; | |||||||||||||||||||||||||||
| 208 | 3 | my $min; | |||||||||||||||||||||||||||
| 209 | 3 | foreach my $dist ( values %$dists ) { | |||||||||||||||||||||||||||
| 210 | 12 | ▶ | $min = $dist if (!defined $min or $dist < $min); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $min or $dist < $min
MC/DC100%2/2
not defined $min or $dist < $min
not defined $min
$dist < $min
Truth table100%3/3
not(A) or B
A= not defined $minB= $dist < $min
| |||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | 3 | return $min; | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | =back | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | =head1 ALGORITHM | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | We use the hash as an approximate conservative metric to basically do | ||||||||||||||||||||||||||||
| 220 | clipping of space. A box is one cell of the space defined by the grid | ||||||||||||||||||||||||||||
| 221 | size. A region is a box and all the neighbouring boxes in all directions, | ||||||||||||||||||||||||||||
| 222 | i.e. all the boxes b such that | ||||||||||||||||||||||||||||
| 223 | d(b, c) <= 1 in the d-infinity metric | ||||||||||||||||||||||||||||
| 224 | Noting that d(b, c) is always an integer in this case. | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | +-+-+-+-+-+ | ||||||||||||||||||||||||||||
| 227 | | | | | | | | ||||||||||||||||||||||||||||
| 228 | +-+-+-+-+-+ | ||||||||||||||||||||||||||||
| 229 | | |x|x|x| | | ||||||||||||||||||||||||||||
| 230 | +-+-+-+-+-+ | ||||||||||||||||||||||||||||
| 231 | | |x|b|x| | | ||||||||||||||||||||||||||||
| 232 | +-+-+-+-+-+ | ||||||||||||||||||||||||||||
| 233 | | |x|x|x| | | ||||||||||||||||||||||||||||
| 234 | +-+-+-+-+-+ | ||||||||||||||||||||||||||||
| 235 | | | | | | | | ||||||||||||||||||||||||||||
| 236 | +-+-+-+-+-+ | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | Now all points outside the region defined by the box b and the | ||||||||||||||||||||||||||||
| 239 | neighbours x can not be within maximum radius $C of any point in box b. | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | So we reverse the stunt and shove any point in box b into the hash | ||||||||||||||||||||||||||||
| 242 | lists for all boxes b and x so that when testing a point in any box, | ||||||||||||||||||||||||||||
| 243 | we have a list of all points in that box and any neighbouring boxes | ||||||||||||||||||||||||||||
| 244 | (the region). | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | Algorithm by Shevek, modularisation by Kake Pugh (kake@earth.li). | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | Copyright (C) 2003 Kake Pugh. All Rights Reserved. | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | This module is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 255 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | =head1 CREDITS | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | Shevek is utterly fab for telling me how to solve this problem. Greg | ||||||||||||||||||||||||||||
| 260 | McCarroll is also fab for telling me what to call the module. | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | =cut | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | 1 | 1; | |||||||||||||||||||||||||||