| 1 | package Imager::Matrix2d; | ||||||||||||||||||||||||||||
| 2 | 4 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use Scalar::Util qw(reftype looks_like_number); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 4 | our $VERSION = "1.013"; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 NAME | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | Imager::Matrix2d - simple wrapper for matrix construction | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | use Imager::Matrix2d; | ||||||||||||||||||||||||||||
| 16 | $m1 = Imager::Matrix2d->identity; | ||||||||||||||||||||||||||||
| 17 | $m2 = Imager::Matrix2d->rotate(radians=>$angle, x=>$cx, y=>$cy); | ||||||||||||||||||||||||||||
| 18 | $m3 = Imager::Matrix2d->translate(x=>$dx, y=>$dy); | ||||||||||||||||||||||||||||
| 19 | $m4 = Imager::Matrix2d->shear(x=>$sx, y=>$sy); | ||||||||||||||||||||||||||||
| 20 | $m5 = Imager::Matrix2d->reflect(axis=>$axis); | ||||||||||||||||||||||||||||
| 21 | $m6 = Imager::Matrix2d->scale(x=>$xratio, y=>$yratio); | ||||||||||||||||||||||||||||
| 22 | $m8 = Imager::Matric2d->matrix($v11, $v12, $v13, | ||||||||||||||||||||||||||||
| 23 | $v21, $v22, $v23, | ||||||||||||||||||||||||||||
| 24 | $v31, $v32, $v33); | ||||||||||||||||||||||||||||
| 25 | $m6 = $m1 * $m2; | ||||||||||||||||||||||||||||
| 26 | $m7 = $m1 + $m2; | ||||||||||||||||||||||||||||
| 27 | use Imager::Matrix2d qw(:handy); | ||||||||||||||||||||||||||||
| 28 | # various m2d_* functions imported | ||||||||||||||||||||||||||||
| 29 | # where m2d_(.*) calls Imager::Matrix2d->$1() | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | This class provides a simple wrapper around a reference to an array of | ||||||||||||||||||||||||||||
| 34 | 9 coefficients, treated as a matrix: | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | [ 0, 1, 2, | ||||||||||||||||||||||||||||
| 37 | 3, 4, 5, | ||||||||||||||||||||||||||||
| 38 | 6, 7, 8 ] | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | Most of the methods in this class are constructors. The others are | ||||||||||||||||||||||||||||
| 41 | overloaded operators. | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | Note that since Imager represents images with y increasing from top to | ||||||||||||||||||||||||||||
| 44 | bottom, rotation angles are clockwise, rather than counter-clockwise. | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =over | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =cut | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 4 | our @ISA = 'Exporter'; | |||||||||||||||||||||||||||
| 51 | 4 | require Exporter; | |||||||||||||||||||||||||||
| 52 | 4 | our @EXPORT_OK = qw(m2d_rotate m2d_identity m2d_translate m2d_shear | |||||||||||||||||||||||||||
| 53 | m2d_reflect m2d_scale); | ||||||||||||||||||||||||||||
| 54 | 4 | our %EXPORT_TAGS = | |||||||||||||||||||||||||||
| 55 | ( | ||||||||||||||||||||||||||||
| 56 | handy=> [ qw(m2d_rotate m2d_identity m2d_translate m2d_shear | ||||||||||||||||||||||||||||
| 57 | m2d_reflect m2d_scale) ], | ||||||||||||||||||||||||||||
| 58 | ); | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | use overload | ||||||||||||||||||||||||||||
| 61 | 4 | ▶ | '*' => \&_mult, | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 62 | '+' => \&_add, | ||||||||||||||||||||||||||||
| 63 | '""'=>\&_string, | ||||||||||||||||||||||||||||
| 64 | 4 | "eq" => \&_eq; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | =item identity() | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | Returns the identity matrix. | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | =cut | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 1 | ▶ | sub identity { | ||||||||||||||||||||||||||
|
Subroutine
identity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 73 | 1 | return bless [ 1, 0, 0, | |||||||||||||||||||||||||||
| 74 | 0, 1, 0, | ||||||||||||||||||||||||||||
| 75 | 0, 0, 1 ], $_[0]; | ||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =item rotate(radians=>$angle) | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | =item rotate(degrees=>$angle) | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | Creates a matrix that rotates around the origin, or around the point | ||||||||||||||||||||||||||||
| 83 | (x,y) if the 'x' and 'y' parameters are provided. | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | =cut | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 8 | ▶ | sub rotate { | ||||||||||||||||||||||||||
|
Subroutine
rotate: called CC=7 SCAR=19.6
Pod
documented
| |||||||||||||||||||||||||||||
| 88 | 8 | my ($class, %opts) = @_; | |||||||||||||||||||||||||||
| 89 | 8 | my $angle; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 8 | ▶ | if (defined $opts{radians}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 92 | 3 | $angle = $opts{radians}; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | elsif (defined $opts{degrees}) { | ||||||||||||||||||||||||||||
| 95 | 5 | $angle = $opts{degrees} * 3.1415926535 / 180; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | else { | ||||||||||||||||||||||||||||
| 98 | 0 | $Imager::ERRSTR = "degrees or radians parameter required"; | |||||||||||||||||||||||||||
| 99 | 0 | return undef; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 8 | ▶ | if ($opts{'x'} || $opts{'y'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$opts{"x"} or $opts{"y"}
MC/DC100%2/2
$opts{"x"} or $opts{"y"}
$opts{"x"}
$opts{"y"}
Truth table100%3/3
A or B
A= $opts{"x"}B= $opts{"y"}
| |||||||||||||||||||||||||||||
| 103 | 3 | ▶ | $opts{'x'} ||= 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$opts{"x"} ||= 0
MC/DC100%1/1
$opts{"x"} ||= 0
$opts{"x"}
Truth table100%2/2
A
A= $opts{"x"}
| |||||||||||||||||||||||||||||
| 104 | 3 | ▶ | $opts{'y'} ||= 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$opts{"y"} ||= 0
MC/DC100%1/1
$opts{"y"} ||= 0
$opts{"y"}
Truth table100%2/2
A
A= $opts{"y"}
| |||||||||||||||||||||||||||||
| 105 | return $class->translate('x'=>$opts{'x'}, 'y'=>$opts{'y'}) | ||||||||||||||||||||||||||||
| 106 | * $class->rotate(radians=>$angle) | ||||||||||||||||||||||||||||
| 107 | 3 | * $class->translate('x'=>-$opts{'x'}, 'y'=>-$opts{'y'}); | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | else { | ||||||||||||||||||||||||||||
| 110 | 5 | my $sin = sin($angle); | |||||||||||||||||||||||||||
| 111 | 5 | my $cos = cos($angle); | |||||||||||||||||||||||||||
| 112 | 5 | return bless [ $cos, -$sin, 0, | |||||||||||||||||||||||||||
| 113 | $sin, $cos, 0, | ||||||||||||||||||||||||||||
| 114 | 0, 0, 1 ], $class; | ||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =item translate(x=>$dx, y=>$dy) | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =item translate(x=>$dx) | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =item translate(y=>$dy) | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | Translates by the specify amounts. | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =cut | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 9 | ▶ | sub translate { | ||||||||||||||||||||||||||
|
Subroutine
translate: called CC=5 SCAR=16.8
Pod
documented
| |||||||||||||||||||||||||||||
| 129 | 9 | my ($class, %opts) = @_; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 9 | ▶ | if (defined $opts{'x'} || defined $opts{'y'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $opts{"x"} or defined $opts{"y"}
MC/DC0%0/2
defined $opts{"x"} or defined $opts{"y"}
defined $opts{"x"}
defined $opts{"y"}
Truth table66%2/3
A or B
A= defined $opts{"x"}B= defined $opts{"y"}
| |||||||||||||||||||||||||||||
| 132 | 9 | ▶ | my $x = $opts{'x'} || 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$opts{'x'} || 0
MC/DC100%1/1
$opts{'x'} || 0
$opts{'x'}
Truth table100%2/2
A
A= $opts{'x'}
| |||||||||||||||||||||||||||||
| 133 | 9 | ▶ | my $y = $opts{'y'} || 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$opts{'y'} || 0
MC/DC100%1/1
$opts{'y'} || 0
$opts{'y'}
Truth table100%2/2
A
A= $opts{'y'}
| |||||||||||||||||||||||||||||
| 134 | 9 | return bless [ 1, 0, $x, | |||||||||||||||||||||||||||
| 135 | 0, 1, $y, | ||||||||||||||||||||||||||||
| 136 | 0, 0, 1 ], $class; | ||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 0 | $Imager::ERRSTR = 'x or y parameter required'; | |||||||||||||||||||||||||||
| 140 | 0 | return undef; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =item shear(x=>$sx, y=>$sy) | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =item shear(x=>$sx) | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | =item shear(y=>$sy) | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | Shear by the given amounts. | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =cut | ||||||||||||||||||||||||||||
| 152 | 1 | ▶ | sub shear { | ||||||||||||||||||||||||||
|
Subroutine
shear: called CC=5 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||
| 153 | 1 | my ($class, %opts) = @_; | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 1 | ▶ | if (defined $opts{'x'} || defined $opts{'y'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $opts{"x"} or defined $opts{"y"}
MC/DC0%0/2
defined $opts{"x"} or defined $opts{"y"}
defined $opts{"x"}
defined $opts{"y"}
Truth table33%1/3
A or B
A= defined $opts{"x"}B= defined $opts{"y"}
| |||||||||||||||||||||||||||||
| 156 | return bless [ 1, $opts{'x'}||0, 0, | ||||||||||||||||||||||||||||
| 157 | 1 | ▶ | $opts{'y'}||0, 1, 0, | ||||||||||||||||||||||||||
|
Condition50%2/4
$opts{'x'} || 0
$opts{'y'} || 0
MC/DC0%0/1
$opts{'x'} || 0
$opts{'x'}
MC/DC0%0/1
$opts{'y'} || 0
$opts{'y'}
Truth table50%1/2
A
A= $opts{'x'}
Truth table50%1/2
A
A= $opts{'y'}
| |||||||||||||||||||||||||||||
| 158 | 0, 0, 1 ], $class; | ||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | 0 | $Imager::ERRSTR = 'x and y parameters required'; | |||||||||||||||||||||||||||
| 161 | 0 | return undef; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =item reflect(axis=>$axis) | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | Reflect around the given axis, either 'x' or 'y'. | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =item reflect(radians=>$angle) | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | =item reflect(degrees=>$angle) | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | Reflect around a line drawn at the given angle from the origin. | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | =cut | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 0 | ▶ | sub reflect { | ||||||||||||||||||||||||||
|
Subroutine
reflect: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 177 | 0 | my ($class, %opts) = @_; | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 0 | ▶ | if (defined $opts{axis}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | 0 | my $result = $class->identity; | |||||||||||||||||||||||||||
| 181 | 0 | ▶ | if ($opts{axis} eq "y") { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 182 | 0 | $result->[0] = -$result->[0]; | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | elsif ($opts{axis} eq "x") { | ||||||||||||||||||||||||||||
| 185 | 0 | $result->[4] = -$result->[4]; | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | else { | ||||||||||||||||||||||||||||
| 188 | 0 | $Imager::ERRSTR = 'axis must be x or y'; | |||||||||||||||||||||||||||
| 189 | 0 | return undef; | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 0 | return $result; | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | 0 | my $angle; | |||||||||||||||||||||||||||
| 195 | 0 | ▶ | if (defined $opts{radians}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 196 | 0 | $angle = $opts{radians}; | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | elsif (defined $opts{degrees}) { | ||||||||||||||||||||||||||||
| 199 | 0 | $angle = $opts{degrees} * 3.1415926535 / 180; | |||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | else { | ||||||||||||||||||||||||||||
| 202 | 0 | $Imager::ERRSTR = 'axis, degrees or radians parameter required'; | |||||||||||||||||||||||||||
| 203 | 0 | return undef; | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | # fun with matrices | ||||||||||||||||||||||||||||
| 207 | 0 | return $class->rotate(radians=>-$angle) * $class->reflect(axis=>'x') | |||||||||||||||||||||||||||
| 208 | * $class->rotate(radians=>$angle); | ||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | =item scale(x=>$xratio, y=>$yratio) | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | Scales at the given ratios. | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | You can also specify a center for the scaling with the C<cx> and C<cy> | ||||||||||||||||||||||||||||
| 216 | parameters. | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | =cut | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 1 | ▶ | sub scale { | ||||||||||||||||||||||||||
|
Subroutine
scale: called CC=7 SCAR=25.1
Pod
documented
| |||||||||||||||||||||||||||||
| 221 | 1 | my ($class, %opts) = @_; | |||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | 1 | ▶ | if (defined $opts{'x'} || defined $opts{'y'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $opts{"x"} or defined $opts{"y"}
MC/DC0%0/2
defined $opts{"x"} or defined $opts{"y"}
defined $opts{"x"}
defined $opts{"y"}
Truth table33%1/3
A or B
A= defined $opts{"x"}B= defined $opts{"y"}
| |||||||||||||||||||||||||||||
| 224 | 1 | ▶ | $opts{'x'} = 1 unless defined $opts{'x'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 225 | 1 | ▶ | $opts{'y'} = 1 unless defined $opts{'y'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 226 | 1 | ▶ | if ($opts{cx} || $opts{cy}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$opts{"cx"} or $opts{"cy"}
MC/DC0%0/2
$opts{"cx"} or $opts{"cy"}
$opts{"cx"}
$opts{"cy"}
Truth table33%1/3
A or B
A= $opts{"cx"}B= $opts{"cy"}
| |||||||||||||||||||||||||||||
| 227 | return $class->translate('x'=>-$opts{cx}, 'y'=>-$opts{cy}) | ||||||||||||||||||||||||||||
| 228 | * $class->scale('x'=>$opts{'x'}, 'y'=>$opts{'y'}) | ||||||||||||||||||||||||||||
| 229 | 0 | * $class->translate('x'=>$opts{cx}, 'y'=>$opts{cy}); | |||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | else { | ||||||||||||||||||||||||||||
| 232 | return bless [ $opts{'x'}, 0, 0, | ||||||||||||||||||||||||||||
| 233 | 1 | 0, $opts{'y'}, 0, | |||||||||||||||||||||||||||
| 234 | 0, 0, 1 ], $class; | ||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||
| 237 | else { | ||||||||||||||||||||||||||||
| 238 | 0 | $Imager::ERRSTR = 'x or y parameter required'; | |||||||||||||||||||||||||||
| 239 | 0 | return undef; | |||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | =item matrix($v11, $v12, $v13, $v21, $v22, $v23, $v31, $v32, $v33) | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | Create a matrix with custom coefficients. | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | =cut | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | 5 | ▶ | sub matrix { | ||||||||||||||||||||||||||
|
Subroutine
matrix: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 250 | 5 | my ($class, @self) = @_; | |||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | 5 | ▶ | if (@self == 9) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 253 | 4 | return bless \@self, $class; | |||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||
| 255 | else { | ||||||||||||||||||||||||||||
| 256 | 1 | $Imager::ERRSTR = "9 coefficients required"; | |||||||||||||||||||||||||||
| 257 | 1 | return; | |||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | =item transform($x, $y) | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | Transform a point the same way matrix_transform does. | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | =cut | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | 1 | ▶ | sub transform { | ||||||||||||||||||||||||||
|
Subroutine
transform: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 268 | 1 | my ($self, $x, $y) = @_; | |||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | 1 | my $sz = $x * $self->[6] + $y * $self->[7] + $self->[8]; | |||||||||||||||||||||||||||
| 271 | 1 | my ($sx, $sy); | |||||||||||||||||||||||||||
| 272 | 1 | ▶ | if (abs($sz) > 0.000001) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 273 | 1 | $sx = ($x * $self->[0] + $y * $self->[1] + $self->[2]) / $sz; | |||||||||||||||||||||||||||
| 274 | 1 | $sy = ($x * $self->[3] + $y * $self->[4] + $self->[5]) / $sz; | |||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||
| 276 | else { | ||||||||||||||||||||||||||||
| 277 | 0 | $sx = $sy = 0; | |||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | 1 | return ($sx, $sy); | |||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | =item compose(matrix...) | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | Compose several matrices together for use in transformation. | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | For example, for three matrices: | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | my $out = Imager::Matrix2d->compose($m1, $m2, $m3); | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | is equivalent to: | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | my $out = $m3 * $m2 * $m1; | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | Returns the identity matrix if no parameters are supplied. | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | May return the supplied matrix if only one matrix is supplied. | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | =cut | ||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | 0 | ▶ | sub compose { | ||||||||||||||||||||||||||
|
Subroutine
compose: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 302 | 0 | my ($class, @in) = @_; | |||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | @in | ||||||||||||||||||||||||||||
| 305 | 0 | ▶ | or return $class->identity; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | 0 | my $out = pop @in; | |||||||||||||||||||||||||||
| 308 | 0 | for my $m (reverse @in) { | |||||||||||||||||||||||||||
| 309 | 0 | $out = $out * $m; | |||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | 0 | return $out; | |||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | =item _mult() | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | Implements the overloaded '*' operator. Internal use. | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | Currently both the left and right-hand sides of the operator must be | ||||||||||||||||||||||||||||
| 320 | an Imager::Matrix2d. | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | When composing a matrix for transformation you should multiply the | ||||||||||||||||||||||||||||
| 323 | matrices in the reverse order of the transformations: | ||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | my $shear = Imager::Matrix2d->shear(x => 0.1); | ||||||||||||||||||||||||||||
| 326 | my $rotate = Imager::Matrix2d->rotate(degrees => 45); | ||||||||||||||||||||||||||||
| 327 | my $shear_then_rotate = $rotate * $shear; | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | or use the compose method: | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | my $shear_then_rotate = Imager::Matrix2d->compose($shear, $rotate); | ||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | =cut | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | 14 | ▶ | sub _mult { | ||||||||||||||||||||||||||
|
Subroutine
_mult: called CC=10 SCAR=23.0
| |||||||||||||||||||||||||||||
| 336 | 14 | my ($left, $right, $order) = @_; | |||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | 14 | ▶ | if (ref($right)) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 339 | 11 | ▶ | if (reftype($right) eq "ARRAY") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 340 | 10 | ▶ | @$right == 9 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 341 | or croak "9 elements required in array ref"; | ||||||||||||||||||||||||||||
| 342 | 9 | ▶ | if ($order) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 343 | 1 | ($left, $right) = ($right, $left); | |||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||
| 345 | 9 | my @result; | |||||||||||||||||||||||||||
| 346 | 9 | for my $i (0..2) { | |||||||||||||||||||||||||||
| 347 | 27 | for my $j (0..2) { | |||||||||||||||||||||||||||
| 348 | 81 | my $accum = 0; | |||||||||||||||||||||||||||
| 349 | 81 | for my $k (0..2) { | |||||||||||||||||||||||||||
| 350 | 243 | $accum += $left->[3*$i + $k] * $right->[3*$k + $j]; | |||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||
| 352 | 81 | $result[3*$i+$j] = $accum; | |||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||
| 355 | 9 | return bless \@result, __PACKAGE__; | |||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||
| 357 | else { | ||||||||||||||||||||||||||||
| 358 | 1 | croak "multiply by array ref or number"; | |||||||||||||||||||||||||||
| 359 | } | ||||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||
| 361 | ▶ | elsif (defined $right && looks_like_number($right)) { | |||||||||||||||||||||||||||
|
Condition66%2/3
defined $right and &looks_like_number($right)
MC/DC50%1/2
defined $right and &looks_like_number($right)
defined $right
&looks_like_number($right)
Truth table66%2/3
A and B
A= defined $rightB= &looks_like_number($right)
| |||||||||||||||||||||||||||||
| 362 | 2 | my @result = map $_ * $right, @$left; | |||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | 2 | return bless \@result, __PACKAGE__; | |||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||
| 366 | else { | ||||||||||||||||||||||||||||
| 367 | # something we don't handle | ||||||||||||||||||||||||||||
| 368 | 1 | croak "multiply by array ref or number"; | |||||||||||||||||||||||||||
| 369 | } | ||||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | =item _add() | ||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | Implements the overloaded binary '+' operator. | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | Currently both the left and right sides of the operator must be | ||||||||||||||||||||||||||||
| 377 | Imager::Matrix2d objects. | ||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | =cut | ||||||||||||||||||||||||||||
| 380 | 0 | ▶ | sub _add { | ||||||||||||||||||||||||||
|
Subroutine
_add: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 381 | 0 | my ($left, $right, $order) = @_; | |||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | 0 | ▶ | if (ref($right) && UNIVERSAL::isa($right, __PACKAGE__)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $right and UNIVERSAL::isa($right, __PACKAGE__)
MC/DC0%0/2
ref $right and UNIVERSAL::isa($right, __PACKAGE__)
ref $right
UNIVERSAL::isa($right, __PACKAGE__)
Truth table0%0/3
A and B
A= ref $rightB= UNIVERSAL::isa($right, __PACKAGE__)
| |||||||||||||||||||||||||||||
| 384 | 0 | my @result; | |||||||||||||||||||||||||||
| 385 | 0 | for (0..8) { | |||||||||||||||||||||||||||
| 386 | 0 | push @result, $left->[$_] + $right->[$_]; | |||||||||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | 0 | return bless \@result, __PACKAGE__; | |||||||||||||||||||||||||||
| 390 | } | ||||||||||||||||||||||||||||
| 391 | else { | ||||||||||||||||||||||||||||
| 392 | 0 | return undef; | |||||||||||||||||||||||||||
| 393 | } | ||||||||||||||||||||||||||||
| 394 | } | ||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | =item _string() | ||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | Implements the overloaded stringification operator. | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | This returns a string containing 3 lines of text with no terminating | ||||||||||||||||||||||||||||
| 401 | newline. | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | I tried to make it fairly nicely formatted. You might disagree :) | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | =cut | ||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | 8 | ▶ | sub _string { | ||||||||||||||||||||||||||
|
Subroutine
_string: called CC=9 SCAR=25.1
| |||||||||||||||||||||||||||||
| 408 | 8 | my ($m) = @_; | |||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | 8 | my $maxlen = 0; | |||||||||||||||||||||||||||
| 411 | 8 | for (@$m[0..8]) { | |||||||||||||||||||||||||||
| 412 | 72 | ▶ | if (length() > $maxlen) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 413 | 12 | $maxlen = length; | |||||||||||||||||||||||||||
| 414 | } | ||||||||||||||||||||||||||||
| 415 | } | ||||||||||||||||||||||||||||
| 416 | 8 | ▶ | $maxlen <= 9 or $maxlen = 9; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | 8 | my @left = ('[ ', ' ', ' '); | |||||||||||||||||||||||||||
| 419 | 8 | my @right = ("\n", "\n", ']'); | |||||||||||||||||||||||||||
| 420 | 8 | my $out; | |||||||||||||||||||||||||||
| 421 | 8 | my $width = $maxlen+2; | |||||||||||||||||||||||||||
| 422 | 8 | for my $i (0..2) { | |||||||||||||||||||||||||||
| 423 | 24 | $out .= $left[$i]; | |||||||||||||||||||||||||||
| 424 | 24 | for my $j (0..2) { | |||||||||||||||||||||||||||
| 425 | 72 | my $val = $m->[$i*3+$j]; | |||||||||||||||||||||||||||
| 426 | 72 | ▶ | if (length $val > 9) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 427 | 0 | $val = sprintf("%9f", $val); | |||||||||||||||||||||||||||
| 428 | 0 | ▶ | if ($val =~ /\./ && $val !~ /e/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$val =~ /\./ and not $val =~ /e/i
MC/DC0%0/2
$val =~ /\./ and not $val =~ /e/i
$val =~ /\./
not $val =~ /e/i
Truth table0%0/3
A and not(B)
A= $val =~ /\./B= not $val =~ /e/i
| |||||||||||||||||||||||||||||
| 429 | 0 | $val =~ s/0+$//; | |||||||||||||||||||||||||||
| 430 | 0 | $val =~ s/\.$//; | |||||||||||||||||||||||||||
| 431 | } | ||||||||||||||||||||||||||||
| 432 | 0 | $val =~ s/^\s//; | |||||||||||||||||||||||||||
| 433 | } | ||||||||||||||||||||||||||||
| 434 | 72 | $out .= sprintf("%-${width}s", "$val, "); | |||||||||||||||||||||||||||
| 435 | } | ||||||||||||||||||||||||||||
| 436 | 24 | $out =~ s/ +\Z/ /; | |||||||||||||||||||||||||||
| 437 | 24 | $out .= $right[$i]; | |||||||||||||||||||||||||||
| 438 | } | ||||||||||||||||||||||||||||
| 439 | 8 | $out; | |||||||||||||||||||||||||||
| 440 | } | ||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | =item _eq | ||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||
| 444 | Implement the overloaded equality operator. | ||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||
| 446 | Provided for older perls that don't handle magic auto generation of eq | ||||||||||||||||||||||||||||
| 447 | from "". | ||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | =cut | ||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||
| 451 | 0 | ▶ | sub _eq { | ||||||||||||||||||||||||||
|
Subroutine
_eq: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 452 | 0 | my ($left, $right) = @_; | |||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||
| 454 | 0 | return $left . "" eq $right . ""; | |||||||||||||||||||||||||||
| 455 | } | ||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | =back | ||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||
| 459 | The following functions are shortcuts to the various constructors. | ||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | These are not methods. | ||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||
| 463 | You can import these methods with: | ||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||
| 465 | use Imager::Matrix2d ':handy'; | ||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||
| 467 | =over | ||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||
| 469 | =item m2d_identity | ||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||
| 471 | =item m2d_rotate() | ||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||
| 473 | =item m2d_translate() | ||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||
| 475 | =item m2d_shear() | ||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||
| 477 | =item m2d_reflect() | ||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||
| 479 | =item m2d_scale() | ||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||
| 481 | =back | ||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||
| 483 | =cut | ||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||
| 485 | 0 | ▶ | sub m2d_identity { | ||||||||||||||||||||||||||
|
Subroutine
m2d_identity: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 486 | 0 | return __PACKAGE__->identity; | |||||||||||||||||||||||||||
| 487 | } | ||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||
| 489 | 1 | ▶ | sub m2d_rotate { | ||||||||||||||||||||||||||
|
Subroutine
m2d_rotate: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 490 | 1 | return __PACKAGE__->rotate(@_); | |||||||||||||||||||||||||||
| 491 | } | ||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||
| 493 | 0 | ▶ | sub m2d_translate { | ||||||||||||||||||||||||||
|
Subroutine
m2d_translate: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 494 | 0 | return __PACKAGE__->translate(@_); | |||||||||||||||||||||||||||
| 495 | } | ||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||
| 497 | 0 | ▶ | sub m2d_shear { | ||||||||||||||||||||||||||
|
Subroutine
m2d_shear: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 498 | 0 | return __PACKAGE__->shear(@_); | |||||||||||||||||||||||||||
| 499 | } | ||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||
| 501 | 0 | ▶ | sub m2d_reflect { | ||||||||||||||||||||||||||
|
Subroutine
m2d_reflect: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 502 | 0 | return __PACKAGE__->reflect(@_); | |||||||||||||||||||||||||||
| 503 | } | ||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||
| 505 | 0 | ▶ | sub m2d_scale { | ||||||||||||||||||||||||||
|
Subroutine
m2d_scale: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 506 | 0 | return __PACKAGE__->scale(@_); | |||||||||||||||||||||||||||
| 507 | } | ||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||
| 509 | 4 | 1; | |||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||
| 511 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||
| 513 | Tony Cook <tony@develop-help.com> | ||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||
| 515 | =head1 BUGS | ||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||
| 517 | Needs a way to invert a matrix. | ||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||
| 519 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||
| 521 | Imager(3), Imager::Font(3) | ||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||
| 523 | http://imager.perl.org/ | ||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||
| 525 | =cut | ||||||||||||||||||||||||||||