| 1 | package Math::Vector::Real::Polyline; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Math::Vector::Real; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | our $debug = 0; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | sub as_string; | ||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use overload '""' => \&as_string; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 16 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 17 | 0 | my $self = [map V(@$_), @_]; | |||||||||||||||||||||||||||
| 18 | 0 | bless $self, $class; | |||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 0 | ▶ | sub dist2_to_point { | ||||||||||||||||||||||||||
|
Subroutine
dist2_to_point: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 23 | 0 | my $p = V(shift); | |||||||||||||||||||||||||||
| 24 | 0 | ▶ | return unless @$self; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 0 | my $min_d2 = $self->[0]->dist2($p); | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 0 | for my $i (1..$#$self) { | |||||||||||||||||||||||||||
| 28 | 0 | my $d2 = $p->dist2_to_segment($self->[$i-1], $self->[$i]); | |||||||||||||||||||||||||||
| 29 | 0 | ▶ | $min_d2 = $d2 if $d2 < $min_d2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 0 | $min_d2; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 0 | ▶ | sub dist2_to_segment { | ||||||||||||||||||||||||||
|
Subroutine
dist2_to_segment: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 36 | 0 | my ($self, $a, $b) = @_; | |||||||||||||||||||||||||||
| 37 | 0 | my $min_d2 = $self->[0]->dist2($a); | |||||||||||||||||||||||||||
| 38 | 0 | for my $i (1..$#$self) { | |||||||||||||||||||||||||||
| 39 | 0 | my $d2 = Math::Vector::Real->dist2_between_segments($a, $b, | |||||||||||||||||||||||||||
| 40 | $self->[$i - 1], $self->[$i]); | ||||||||||||||||||||||||||||
| 41 | 0 | ▶ | $min_d2 = $d2 if $d2 < $min_d2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | 0 | $min_d2; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 0 | ▶ | sub _dist2_to_polyline_brute_force { | ||||||||||||||||||||||||||
|
Subroutine
_dist2_to_polyline_brute_force: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 47 | 0 | my ($self, $other, $min_d2) = @_; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | for my $i (1..$#$self) { | |||||||||||||||||||||||||||
| 50 | 0 | my $s0 = $self->[$i - 1]; | |||||||||||||||||||||||||||
| 51 | 0 | my $s1 = $self->[$i]; | |||||||||||||||||||||||||||
| 52 | 0 | for my $j (1..$#$other) { | |||||||||||||||||||||||||||
| 53 | 0 | my $d2 = Math::Vector::Real->dist2_between_segments($s0, $s1, | |||||||||||||||||||||||||||
| 54 | $other->[$j-1], $other->[$j]); | ||||||||||||||||||||||||||||
| 55 | 0 | ▶ | $min_d2 = $d2 if $d2 < $min_d2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | 0 | $min_d2; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 1 | my $cutoff = 5; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | ▶ | sub _dump_queue { | ||||||||||||||||||||||||||
|
Subroutine
_dump_queue: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 64 | 0 | my $min_d2 = shift; | |||||||||||||||||||||||||||
| 65 | 0 | printf "Queue size: %d, min_d2: %f\n", scalar(@_), $min_d2; | |||||||||||||||||||||||||||
| 66 | 0 | for (@_) { | |||||||||||||||||||||||||||
| 67 | 0 | my $a = Math::Vector::Real::Polyline->new(@{$_->[0]}); | |||||||||||||||||||||||||||
| 68 | 0 | my $b = Math::Vector::Real::Polyline->new(@{$_->[1]}); | |||||||||||||||||||||||||||
| 69 | 0 | my $d2 = $_->[2]; | |||||||||||||||||||||||||||
| 70 | 0 | print " a: $a b: $b d2: $d2\n"; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | sub dist2_to_polyline { | ||||||||||||||||||||||||||
|
Subroutine
dist2_to_polyline: not called CC=14 SCAR=53.5
Pod
documented
| |||||||||||||||||||||||||||||
| 75 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 76 | 0 | my $other = shift; | |||||||||||||||||||||||||||
| 77 | 0 | ▶ | return unless @$self and @$other; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@$self and @$other
MC/DC0%0/2
@$self and @$other
@$self
@$other
Truth table0%0/3
A and B
A= @$selfB= @$other
| |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | return $other->dist2_to_point($self->[0]) if @$self == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | ▶ | return $self->dist2_to_point($other->[0]) if @$other == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | my $min_d2 = $self->[0]->dist2($other->[0]); | |||||||||||||||||||||||||||
| 82 | 0 | my @queue = [$self, $other, 0]; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 0 | ▶ | while (@queue) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 0 | ▶ | $debug and _dump_queue($min_d2, @queue); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 0 | my ($a, $b, $bb_d2) = @{pop @queue}; | |||||||||||||||||||||||||||
| 87 | 0 | ▶ | last if $bb_d2 >= $min_d2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | ▶ | if (@$a <= $cutoff or @$b <= $cutoff) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@$a <= $cutoff or @$b <= $cutoff
MC/DC0%0/2
@$a <= $cutoff or @$b <= $cutoff
@$a <= $cutoff
@$b <= $cutoff
Truth table0%0/3
A or B
A= @$a <= $cutoffB= @$b <= $cutoff
| |||||||||||||||||||||||||||||
| 90 | 0 | $min_d2 = _dist2_to_polyline_brute_force($a, $b, $min_d2); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | else { | ||||||||||||||||||||||||||||
| 93 | 0 | my $a_half = int(@$a / 2); | |||||||||||||||||||||||||||
| 94 | 0 | my $a0 = [@{$a}[0..$a_half]]; | |||||||||||||||||||||||||||
| 95 | 0 | my $a1 = [@{$a}[$a_half..$#$a]]; | |||||||||||||||||||||||||||
| 96 | 0 | my $b_half = int(@$b / 2); | |||||||||||||||||||||||||||
| 97 | 0 | my $b0 = [@{$b}[0..$b_half]]; | |||||||||||||||||||||||||||
| 98 | 0 | my $b1 = [@{$b}[$b_half..$#$b]]; | |||||||||||||||||||||||||||
| 99 | 0 | for my $pair ([$a0, $b0], [$a0, $b1], [$a1, $b0], [$a1, $b1]) { | |||||||||||||||||||||||||||
| 100 | 0 | my $bb_d2 = Math::Vector::Real->dist2_between_boxes(Math::Vector::Real->box(@{$pair->[0]}), | |||||||||||||||||||||||||||
| 101 | 0 | Math::Vector::Real->box(@{$pair->[1]})); | |||||||||||||||||||||||||||
| 102 | 0 | ▶ | next if $bb_d2 > $min_d2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 0 | $pair->[2] = $bb_d2; | |||||||||||||||||||||||||||
| 104 | 0 | my $i; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 0 | ▶ | for ($i = $#queue; $i >= 0; $i--) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 0 | my $pivot = $queue[$i]; | |||||||||||||||||||||||||||
| 108 | 0 | ▶ | last if $bb_d2 <= $pivot->[2]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 0 | $queue[$i + 1] = $pivot | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | 0 | $queue[$i + 1] = $pair; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | 0 | return $min_d2; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | ▶ | sub dist_to_polyline { sqrt(&dist2_to_polyline) } | ||||||||||||||||||||||||||
|
Subroutine
dist_to_polyline: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | ▶ | sub as_string { | ||||||||||||||||||||||||||
|
Subroutine
as_string: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 121 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 122 | 0 | return '['.join('-', @$self).']'; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 1 | 1; | |||||||||||||||||||||||||||
| 126 | __END__ | ||||||||||||||||||||||||||||