blib/lib/Geometry/AffineTransform.pm

filter: Statementstmt 100% 72 / 72 Branchbran 83.3% 5 / 6 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 19 / 19 Podpod 64.2% 9 / 14 totaltotal 92.2% 107 / 116 CC 6 SCAR 4.1
CC
6
Cov
98%
concatenate
3.0
new
2.0
invert
2.0
CRAP 1.5
↑ Summary
1 package Geometry::AffineTransform;
2  
3 1 our $VERSION = '1.4';
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 Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Hash::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Math::Trig ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 Geometry::AffineTransform - Affine Transformation to map 2D coordinates to other 2D coordinates
15  
16 =head1 SYNOPSIS
17  
18 use Geometry::AffineTransform;
19  
20 my $t = Geometry::AffineTransform->new();
21 $t->translate($delta_x, $delta_y);
22 $t->rotate($degrees);
23 my $t2 = Geometry::AffineTransform->new()->scale(3.1, 2.3);
24 $t->concatenate($t2);
25 my ($x1, $y1, $x2, $y2, ...) = $t->transform($x1, $y1, $x2, $y2, ...);
26  
27 =head1 DESCRIPTION
28  
29 Geometry::AffineTransform instances represent 2D affine transformations
30 that map 2D coordinates to other 2D coordinates. The references in
31 L</SEE ALSO> provide more information about affine transformations.
32  
33 You create a new instance with L</new>, configure it to perform the desired transformation
34 with a combination of L</scale>, L</rotate> and L</translate> and then perform the actual
35 transformation on one or more x/y coordinate pairs with L</transform>.
36  
37 The state of a newly created instance represents the identity transform,
38 that is, it transforms all input coordinates to the same output coordinates.
39  
40 Most methods return the instance so that you can chain method calls:
41  
42 my $t = Geometry::AffineTransform->new();
43 $t->scale(...)->translate(...)->rotate(...);
44  
45 ($x, $y) = Geometry::AffineTransform->new()->rotate(..)->transform($x, $y);
46  
47 =cut
48  
49  
50 =head1 METHODS
51  
52 =head2 new
53  
54 Constructor, returns a new instance configured with an identity transform.
55  
56 You can optionally supply any of the six specifiable parts of the transformation matrix.
57 The six values in the first two columns are the specifiable values:
58  
59 [ m11 m21 0 ]
60 [ m21 m22 0 ]
61 [ tx ty 1 ]
62  
63 The constructor lets you initialize them with key/value parameters:
64  
65 my $t = Geometry::AffineTransform->new(tx => 10, ty => 15);
66  
67 By default, the identity transform represented by this matrix is used:
68  
69 [ 1 0 0 ]
70 [ 0 1 0 ]
71 [ 0 0 1 ]
72  
73 In other words, invoking the constructor without arguments is equivalent to this:
74  
75 my $t = Geometry::AffineTransform->new(
76 m11 => 1,
77 m12 => 0,
78 m21 => 0,
79 m22 => 1,
80 tx => 0,
81 ty => 0
82 );
83  
84 =cut
85  
86 16 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
87 16 my $self = shift;
88 16 my (%args) = @_;
89  
90 16 ▶ my $class = ref($self) || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
4120
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
91 16 $self = bless {m11 => 1, m12 => 0, m21 => 0, m22 => 1, tx => 0, ty => 0, %args}, $class;
92 # $self->init();
93 16 Hash::Util::lock_keys(%$self);
94  
95 16 return $self;
96 }
97  
98  
99 # hook for subclasses
100 # sub init {
101 # }
102  
103  
104  
105  
106 =head2 clone
107  
108 Returns a clone of the instance.
109  
110 =cut
111  
112 4 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
113 4 my $self = shift;
114 4 return $self->new()->set_matrix_2x3($self->matrix_2x3());
115 }
116  
117  
118  
119 =head2 invert
120  
121 Inverts the state of the transformation.
122  
123 my $inverted_clone = $t->clone()->invert();
124  
125 =cut
126  
127 4 ▶sub invert {
Subroutine invert: called CC=2 SCAR=6.9
Pod documented
128 4 my $self = shift;
129  
130 4 my $det = $self->determinant();
131
132 4 ▶ croak "Unable to invert this transform (zero determinant)" unless $det;
Branch: 1 branch
BranchTrueFalseTotal
unless $det 1 3 4
133  
134 return $self->set_matrix_2x3(
135 $self->{m22} / $det, # 11
136 -$self->{m12} / $det, # 12
137 -$self->{m21} / $det, # 21
138 $self->{m11} / $det, # 22
139 ($self->{m21} * $self->{ty} - $self->{m22} * $self->{tx}) / $det,
140 3 ($self->{m12} * $self->{tx} - $self->{m11} * $self->{ty}) / $det,
141 );
142 }
143  
144  
145  
146 =head2 transform
147  
148 Transform one or more coordinate pairs according to the current state.
149  
150 This method expects an even number of positional parameters, each pair
151 representing the x and y coordinates of a point.
152  
153 Returns the transformed list of coordinates in the same form as the input list.
154  
155 my @output = $t->transform(2, 4, 10, 20);
156  
157 =cut
158  
159 13 ▶sub transform {
Subroutine transform: called CC=2 SCAR=6.9
Pod documented
160 13 my $self = shift;
161 13 my (@pairs) = @_;
162  
163 13 my @result;
164 13 ▶ while (my ($x, $y) = splice(@pairs, 0, 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@pairs, 0, 2)) 15 13 28
165 15 my $x2 = $self->{m11} * $x + $self->{m21} * $y + $self->{tx};
166 15 my $y2 = $self->{m12} * $x + $self->{m22} * $y + $self->{ty};
167 15 push @result, $x2, $y2;
168 }
169  
170 13 return @result;
171 }
172  
173  
174  
175  
176 # concatenate another transformation matrix to the current state.
177 # Takes the six specifiable parts of the 3x3 transformation matrix.
178 13 ▶sub concatenate_matrix_2x3 {
Subroutine concatenate_matrix_2x3: called CC=1 SCAR=0.0
Pod undocumented
179 13 my $self = shift;
180 13 my ($m11, $m12, $m21, $m22, $tx, $ty) = @_;
181 13 my $a = [$self->matrix_2x3()];
182 13 my $b = [$m11, $m12, $m21, $m22, $tx, $ty];
183 13 return $self->set_matrix_2x3($self->matrix_multiply($a, $b));
184 }
185  
186  
187 =head2 concatenate
188  
189 Combine the receiver's state with that of another transformation instance.
190  
191 This method expects a list of one or more C<Geometry::AffineTransform>
192 instances and combines the transformation of each one with the receiver's
193 in the given order.
194  
195 Returns C<$self>.
196  
197 =cut
198  
199 1 ▶sub concatenate {
Subroutine concatenate: called CC=3 SCAR=11.0
Pod documented
200 1 my $self = shift;
201 1 my @transforms = @_;
202 1 foreach my $t (@transforms) {
203 1 ▶ croak "Expecting argument of type Geometry::AffineTransform" unless (ref $t);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $t 0 1 1
204 1 $self->concatenate_matrix_2x3($t->matrix_2x3()) ;
205 }
206 1 return $self;
207 }
208  
209  
210 =head2 scale
211  
212 Adds a scaling transformation.
213  
214 This method expects positional parameters.
215  
216 =over
217  
218 =item sx
219  
220 The scaling factor for the x dimension.
221  
222 =item sy
223  
224 The scaling factor for the y dimension.
225  
226 =back
227  
228 Returns C<$self>.
229  
230 =cut
231  
232 5 ▶sub scale {
Subroutine scale: called CC=1 SCAR=0.0
Pod documented
233 5 my $self = shift;
234 5 my ($sx, $sy) = @_;
235 5 return $self->concatenate_matrix_2x3($sx, 0, 0, $sy, 0, 0);
236 }
237  
238  
239 =head2 translate
240  
241 Adds a translation transformation, i.e. the transformation shifts
242 the input coordinates by a constant amount.
243  
244 This method expects positional parameters.
245  
246 =over
247  
248 =item tx
249  
250 The offset for the x dimension.
251  
252 =item ty
253  
254 The offset for the y dimension.
255  
256 =back
257  
258 Returns C<$self>.
259  
260 =cut
261  
262 2 ▶sub translate {
Subroutine translate: called CC=1 SCAR=0.0
Pod documented
263 2 my $self = shift;
264 2 my ($tx, $ty) = @_;
265 2 return $self->concatenate_matrix_2x3(1, 0, 0, 1, $tx, $ty);
266 }
267  
268  
269  
270  
271 =head2 rotate
272  
273 Adds a rotation transformation.
274  
275 This method expects positional parameters.
276  
277 =over
278  
279 =item angle
280  
281 The rotation angle in degrees. With no other transformation active,
282 positive values rotate counterclockwise.
283  
284 =back
285  
286 Returns C<$self>.
287  
288 =cut
289  
290 4 ▶sub rotate {
Subroutine rotate: called CC=1 SCAR=0.0
Pod documented
291 4 my $self = shift;
292 4 my ($degrees) = @_;
293 4 my $rad = Math::Trig::deg2rad($degrees);
294 4 return $self->concatenate_matrix_2x3(cos($rad), sin($rad), -sin($rad), cos($rad), 0, 0);
295 }
296  
297  
298  
299 # returns the 6 specifiable parts of the transformation matrix
300 25 ▶sub matrix_2x3 {
Subroutine matrix_2x3: called CC=1 SCAR=0.0
Pod undocumented
301 25 my $self = shift;
302 25 return $self->{m11}, $self->{m12}, $self->{m21}, $self->{m22}, $self->{tx}, $self->{ty};
303 }
304  
305  
306 # returns the determinant of the matrix
307 6 ▶sub determinant {
Subroutine determinant: called CC=1 SCAR=0.0
Pod undocumented
308 6 my $self = shift;
309 6 return $self->{m11} * $self->{m22} - $self->{m12} * $self->{m21};
310 }
311  
312  
313 # sets the 6 specifiable parts of the transformation matrix
314 26 ▶sub set_matrix_2x3 {
Subroutine set_matrix_2x3: called CC=1 SCAR=0.0
Pod undocumented
315 26 my $self = shift;
316 ($self->{m11}, $self->{m12},
317 $self->{m21}, $self->{m22},
318 26 $self->{tx}, $self->{ty}) = @_;
319 26 return $self;
320 }
321  
322  
323 =head2 matrix
324  
325 Returns the current value of the 3 x 3 transformation matrix, including the
326 third, fixed column, as a 9-element list:
327  
328 my ($m11, $m12, undef,
329 $m21, $m22, undef,
330 $tx, $ty, undef) = $t->matrix();
331  
332 =cut
333  
334 1 ▶sub matrix {
Subroutine matrix: called CC=1 SCAR=0.0
Pod documented
335 1 my $self = shift;
336 1 return $self->{m11}, $self->{m12}, 0, $self->{m21}, $self->{m22}, 0, $self->{tx}, $self->{ty}, 1;
337 }
338  
339  
340  
341  
342 # a simplified multiply that assumes the fixed 0 0 1 third column
343 14 ▶sub matrix_multiply {
Subroutine matrix_multiply: called CC=1 SCAR=0.0
Pod undocumented
344 14 my $self = shift;
345 14 my ($a, $b) = @_;
346  
347 # a11 a12 0
348 # a21 a22 0
349 # a31 a32 1
350 #
351 # b11 b12 0
352 # b21 b22 0
353 # b31 b32 1
354  
355 14 my ($a11, $a12, $a21, $a22, $a31, $a32) = @$a;
356 14 my ($b11, $b12, $b21, $b22, $b31, $b32) = @$b;
357  
358 return
359 14 ($a11 * $b11 + $a12 * $b21), ($a11 * $b12 + $a12 * $b22),
360 ($a21 * $b11 + $a22 * $b21), ($a21 * $b12 + $a22 * $b22),
361 ($a31 * $b11 + $a32 * $b21 + $b31), ($a31 * $b12 + $a32 * $b22 + $b32),
362 ;
363  
364 }
365  
366  
367 =head1 SEE ALSO
368  
369 =over
370  
371 =item Apple Quartz 2D Programming Guide - The Math Behind the Matrices
372  
373 L<http://developer.apple.com/library/mac/DOCUMENTATION/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_affine/dq_affine.html#//apple_ref/doc/uid/TP30001066-CH204-CJBECIAD>
374  
375 =item Sun Java java.awt.geom.AffineTransform
376  
377 L<http://download.oracle.com/javase/1.4.2/docs/api/java/awt/geom/AffineTransform.html>
378  
379 =item Wikipedia - Matrix Multiplication
380  
381 L<http://en.wikipedia.org/wiki/Matrix_(mathematics)#Matrix_multiplication.2C_linear_equations_and_linear_transformations>
382  
383 =back
384  
385  
386  
387 =head1 AUTHOR
388  
389 Marc Liyanage <liyanage@cpan.org>
390  
391 =head1 COPYRIGHT AND LICENSE
392  
393 Copyright 2008 Marc Liyanage.
394  
395 Distributed under the Artistic License 2.
396  
397 =cut
398  
399  
400 1 1;
↑ Summary