Taffeta/Transform/Affine2D.pm untested

filter: Statementstmt 0.0% 0 / 77 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 7 Podpod n/a totaltotal 0.0% 0 / 137 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 use Renard::Incunabula::Common::Setup;
2 package Intertangle::Taffeta::Transform::Affine2D;
3 # ABSTRACT: A 2D affine transform
4 $Intertangle::Taffeta::Transform::Affine2D::VERSION = '0.001';
5 use Mu;
6 use Renard::Incunabula::Common::Types qw(InstanceOf Str Bool);
7 use Intertangle::Yarn::Types qw(Point);
8 use Intertangle::Yarn::Graphene;
9 use List::AllUtils qw(all);
10  
11 has matrix => (
12 is => 'ro',
13 isa => InstanceOf['Intertangle::Yarn::Graphene::Matrix'],
14 default => sub {
15 my $m = Intertangle::Yarn::Graphene::Matrix->new;
16 $m->init_identity;
17 $m;
18 },
19 );
20  
21 lazy cairo_matrix => method() {
22 require Cairo;
23  
24 my (
25 $is_2d_affine,
26 $xx, $yx,
27 $xy, $yy,
28 $x0, $y0
29 ) = $self->matrix->to_2d;
30  
31 die "Not a 2D affine matrix" unless $is_2d_affine;
32  
33 my $cairo_matrix = Cairo::Matrix->init(
34 $xx, $yx,
35 $xy, $yy,
36 $x0, $y0
37 );
38  
39 $cairo_matrix;
40 }, isa => InstanceOf['Cairo::Matrix'];
41  
42 lazy svg_transform => method() {
43 # / a c e \
44 # | b d f |
45 # \ 0 0 1 /
46 my (
47 $is_2d_affine,
48 $xx, $yx, # a c
49 $xy, $yy, # b d
50  
51 $x0, # e
52 $y0 # f
53 ) = $self->matrix->to_2d;
54  
55 die "Not a 2D affine matrix" unless $is_2d_affine;
56  
57 my (
58 $m_a , $m_c,
59 $m_b , $m_d,
60  
61 $m_e,
62 $m_f
63 ) = (
64 $xx, $yx,
65 $xy, $yy,
66  
67 $x0,
68 $y0
69 );
70  
71 return sprintf("matrix(%f, %f, %f, %f, %f, %f)",
72 $m_a, $m_b,
73 $m_c, $m_d,
74 $m_e, $m_f,
75 );
76 }, isa => Str;
77  
78 lazy is_identity => method() {
79 $self->matrix->is_identity;
80 }, isa => Bool;
81  
82 around BUILDARGS => fun( $orig, $class, %args ) {
83 my $matrix;
84  
85 my $matrix_set = 0;
86  
87 if( exists $args{matrix} ) {
88 if( $matrix_set ) {
89 die "Can not use multiple approaches to construct matrix";
90 } else {
91 $matrix_set = 1;
92 }
93  
94 #$class->_is_matrix_affine( $args{matrix} );
95 $matrix = $args{matrix};
96 }
97  
98 if( exists $args{matrix_abcdef} ) {
99 if( $matrix_set ) {
100 die "Can not use multiple approaches to construct matrix";
101 } else {
102 $matrix_set = 1;
103 }
104  
105 $matrix = Intertangle::Yarn::Graphene::Matrix->new;
106 $matrix->init_from_2d(
107 $args{matrix_abcdef}{a} // 1, $args{matrix_abcdef}{c} // 0,
108 $args{matrix_abcdef}{b} // 0, $args{matrix_abcdef}{d} // 1,
109  
110 $args{matrix_abcdef}{e} // 0 , $args{matrix_abcdef}{f} // 0,
111 );
112 }
113  
114 if( exists $args{matrix_xy} ) {
115 if( $matrix_set ) {
116 die "Can not use multiple approaches to construct matrix";
117 } else {
118 $matrix_set = 1;
119 }
120  
121 $matrix = Intertangle::Yarn::Graphene::Matrix->new;
122 $matrix->init_from_2d(
123 $args{matrix_xy}{xx} // 1,
124 $args{matrix_xy}{yx} // 0,
125 $args{matrix_xy}{xy} // 0,
126 $args{matrix_xy}{yy} // 1,
127 $args{matrix_xy}{x0} // 0,
128 $args{matrix_xy}{y0} // 0,
129 );
130 }
131  
132 $args{matrix} = $matrix if defined $matrix;
133  
134 return $class->$orig(%args);
135 };
136  
137 classmethod _is_matrix_affine( $matrix ) {
138 $class->_is_matrix_affine_graphene( $matrix );
139 }
140  
141 classmethod _is_matrix_affine_graphene( $matrix ) {
142 die "Not a 2D affine matrix" unless $matrix->is_2d;
143 }
144  
145 classmethod _is_matrix_affine_exact( $matrix ) {
146 my $ma = $matrix->to_ArrayRef;
147 # NOTE this uses exact checking instead of floating point
148 # approximation. This should be fine because these are values
149 # that are set by the user.
150 unless(
151 (
152 all { $_ == 0 } (
153 $ma->[0][2], $ma->[0][3],
154 $ma->[1][2], $ma->[1][3],
155 $ma->[2][0], $ma->[2][1],
156 $ma->[3][2], $ma->[2][3],
157 )
158 )
159 &&
160 (
161 all { $_ == 1 } (
162 $ma->[2][2], $ma->[3][3],
163 )
164 )
165 ) {
166 die "Not a 2D affine transform matrix";
167 }
168 }
169  
170 method compose( $transform ) {
171 Intertangle::Taffeta::Transform::Affine2D->new(
172 matrix => ( $self->matrix x $transform->matrix ),
173 );
174 }
175  
176 method compose_premultiply( $transform ) {
177 Intertangle::Taffeta::Transform::Affine2D->new(
178 matrix => ( $transform->matrix x $self->matrix ),
179 );
180 }
181  
182 method apply_to_bounds( $bounds ) {
183 $self->matrix->transform_bounds( $bounds );
184 }
185  
186 method apply_to_point( (Point->coercibles) $point ) {
187 $self->matrix->transform_point( Point->coerce($point) );
188 }
189  
190 method apply_to_vec3( $vec ) {
191 $self->matrix->transform_vec3( $vec );
192 }
193  
194  
195 1;
196  
197 __END__