| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Vector::Object3D::Matrix::Transform; |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
Vector::Object3D::Matrix::Transform - construction of graphical transformation matrices |
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head2 SYNOPSIS |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
package Vector::Object3D::Matrix; |
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
use Readonly; |
|
12
|
|
|
|
|
|
|
Readonly my $pi => 3.14159; |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
use Moose; |
|
15
|
|
|
|
|
|
|
with 'Vector::Object3D::Matrix::Transform'; |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# Calling any method from this role results in creating an instance of the given class: |
|
18
|
|
|
|
|
|
|
my $class = 'Vector::Object3D::Matrix'; |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# Construct rotation matrix on a 2D plane: |
|
21
|
|
|
|
|
|
|
my $rotateMatrix2D = $class->get_rotation_matrix( |
|
22
|
|
|
|
|
|
|
rotate_xy => (30 * $pi / 180), |
|
23
|
|
|
|
|
|
|
); |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# Construct scaling matrix on a 2D plane: |
|
26
|
|
|
|
|
|
|
my $scaleMatrix2D = $class->get_scaling_matrix( |
|
27
|
|
|
|
|
|
|
scale_x => 2, |
|
28
|
|
|
|
|
|
|
scale_y => 2, |
|
29
|
|
|
|
|
|
|
); |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
# Construct translation matrix on a 2D plane: |
|
32
|
|
|
|
|
|
|
my $translateMatrix2D = $class->get_translation_matrix( |
|
33
|
|
|
|
|
|
|
shift_x => -2, |
|
34
|
|
|
|
|
|
|
shift_y => 1, |
|
35
|
|
|
|
|
|
|
); |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
# Construct rotation matrix in a 3D space: |
|
38
|
|
|
|
|
|
|
my $rotateMatrix3D = $class->get_rotation_matrix( |
|
39
|
|
|
|
|
|
|
rotate_xy => (30 * $pi / 180), |
|
40
|
|
|
|
|
|
|
rotate_yz => -30 * ($pi / 180), |
|
41
|
|
|
|
|
|
|
rotate_xz => 45 * ($pi / 180), |
|
42
|
|
|
|
|
|
|
); |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# Construct scaling matrix in a 3D space: |
|
45
|
|
|
|
|
|
|
my $scaleMatrix3D = $class->get_scaling_matrix( |
|
46
|
|
|
|
|
|
|
scale_x => 2, |
|
47
|
|
|
|
|
|
|
scale_y => 2, |
|
48
|
|
|
|
|
|
|
scale_z => 3, |
|
49
|
|
|
|
|
|
|
); |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
# Construct translation matrix in a 3D space: |
|
52
|
|
|
|
|
|
|
my $translateMatrix3D = $class->get_translation_matrix( |
|
53
|
|
|
|
|
|
|
shift_x => -2, |
|
54
|
|
|
|
|
|
|
shift_y => 1, |
|
55
|
|
|
|
|
|
|
shift_z => 3, |
|
56
|
|
|
|
|
|
|
); |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
C<Vector::Object3D::Matrix::Transform> is a Moose role that is meant to be applied to C<Vector::Object3D::Matrix> class in order to provide it with additional methods supporting construction of graphical transformation matrices, which currently handle rotation, scaling and translation functionalities. |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=head1 METHODS |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=cut |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
|
67
|
|
|
|
|
|
|
|
|
68
|
1
|
|
|
1
|
|
1651
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
65
|
|
|
69
|
1
|
|
|
1
|
|
7
|
use warnings; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
36
|
|
|
70
|
|
|
|
|
|
|
|
|
71
|
1
|
|
|
1
|
|
428
|
use Moose::Role; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
use Readonly; |
|
74
|
|
|
|
|
|
|
Readonly our $pi => 3.14159; |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head2 get_rotation_matrix |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
Construct rotation matrix on a 2D plane: |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
my $rotateMatrix2D = $class->get_rotation_matrix( |
|
81
|
|
|
|
|
|
|
rotate_xy => (30 * $pi / 180), |
|
82
|
|
|
|
|
|
|
); |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
Construct rotation matrix in a 3D space: |
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
my $rotateMatrix3D = $class->get_rotation_matrix( |
|
87
|
|
|
|
|
|
|
rotate_xy => (30 * $pi / 180), |
|
88
|
|
|
|
|
|
|
rotate_yz => -30 * ($pi / 180), |
|
89
|
|
|
|
|
|
|
rotate_xz => 45 * ($pi / 180), |
|
90
|
|
|
|
|
|
|
); |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=cut |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub get_rotation_matrix { |
|
95
|
|
|
|
|
|
|
my ($class, %args) = @_; |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
my $is3D = (defined $args{rotate_yz} || defined $args{rotate_xz}) ? 1 : 0; |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
my $x_axis_rotation_angle = $args{rotate_yz} || 0; |
|
100
|
|
|
|
|
|
|
my $y_axis_rotation_angle = $args{rotate_xz} || 0; |
|
101
|
|
|
|
|
|
|
my $z_axis_rotation_angle = $args{rotate_xy} || 0; |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
my $z_axis_rotation_sin = sin $z_axis_rotation_angle; |
|
104
|
|
|
|
|
|
|
my $z_axis_rotation_cos = cos $z_axis_rotation_angle; |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
my $rows; |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
if ($is3D) { |
|
109
|
|
|
|
|
|
|
my $x_axis_rotation_sin = sin $x_axis_rotation_angle; |
|
110
|
|
|
|
|
|
|
my $x_axis_rotation_cos = cos $x_axis_rotation_angle; |
|
111
|
|
|
|
|
|
|
my $y_axis_rotation_sin = sin $y_axis_rotation_angle; |
|
112
|
|
|
|
|
|
|
my $y_axis_rotation_cos = cos $y_axis_rotation_angle; |
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
$rows = [ |
|
115
|
|
|
|
|
|
|
[$y_axis_rotation_cos * $z_axis_rotation_cos, -$z_axis_rotation_sin, -$y_axis_rotation_sin, 0], |
|
116
|
|
|
|
|
|
|
[$z_axis_rotation_sin, $x_axis_rotation_cos * $z_axis_rotation_cos, $x_axis_rotation_sin, 0], |
|
117
|
|
|
|
|
|
|
[$y_axis_rotation_sin, -$x_axis_rotation_sin, $x_axis_rotation_cos * $y_axis_rotation_cos, 0], |
|
118
|
|
|
|
|
|
|
[0, 0, 0, 1], |
|
119
|
|
|
|
|
|
|
]; |
|
120
|
|
|
|
|
|
|
} |
|
121
|
|
|
|
|
|
|
else { |
|
122
|
|
|
|
|
|
|
$rows = [[$z_axis_rotation_cos, -$z_axis_rotation_sin, 0], [$z_axis_rotation_sin, $z_axis_rotation_cos, 0], [0, 0, 1]]; |
|
123
|
|
|
|
|
|
|
} |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
my $matrix = $class->new(rows => $rows); |
|
126
|
|
|
|
|
|
|
return $matrix; |
|
127
|
|
|
|
|
|
|
} |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=head2 get_scaling_matrix |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
Construct scaling matrix on a 2D plane: |
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
my $scaleMatrix2D = $class->get_scaling_matrix( |
|
134
|
|
|
|
|
|
|
scale_x => 2, |
|
135
|
|
|
|
|
|
|
scale_y => 2, |
|
136
|
|
|
|
|
|
|
); |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
Construct scaling matrix in a 3D space: |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
my $scaleMatrix3D = $class->get_scaling_matrix( |
|
141
|
|
|
|
|
|
|
scale_x => 2, |
|
142
|
|
|
|
|
|
|
scale_y => 2, |
|
143
|
|
|
|
|
|
|
scale_z => 3, |
|
144
|
|
|
|
|
|
|
); |
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=cut |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
sub get_scaling_matrix { |
|
149
|
|
|
|
|
|
|
my ($class, %args) = @_; |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
my $is3D = defined $args{scale_z} ? 1 : 0; |
|
152
|
|
|
|
|
|
|
my $scale_x = $args{scale_x} || 0; |
|
153
|
|
|
|
|
|
|
my $scale_y = $args{scale_y} || 0; |
|
154
|
|
|
|
|
|
|
my $scale_z = $args{scale_z} || 0; |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
my $rows; |
|
157
|
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
if ($is3D) { |
|
159
|
|
|
|
|
|
|
$rows = [[$scale_x, 0, 0, 0], [0, $scale_y, 0, 0], [0, 0, $scale_z, 0], [0, 0, 0, 1]]; |
|
160
|
|
|
|
|
|
|
} |
|
161
|
|
|
|
|
|
|
else { |
|
162
|
|
|
|
|
|
|
$rows = [[$scale_x, 0, 0], [0, $scale_y, 0], [0, 0, 1]]; |
|
163
|
|
|
|
|
|
|
} |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
my $matrix = $class->new(rows => $rows); |
|
166
|
|
|
|
|
|
|
return $matrix; |
|
167
|
|
|
|
|
|
|
} |
|
168
|
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=head2 get_translation_matrix |
|
170
|
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
Construct translation matrix on a 2D plane: |
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
my $translateMatrix2D = $class->get_translation_matrix( |
|
174
|
|
|
|
|
|
|
shift_x => -2, |
|
175
|
|
|
|
|
|
|
shift_y => 1, |
|
176
|
|
|
|
|
|
|
); |
|
177
|
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
Construct translation matrix in a 3D space: |
|
179
|
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
my $translateMatrix3D = $class->get_translation_matrix( |
|
181
|
|
|
|
|
|
|
shift_x => -2, |
|
182
|
|
|
|
|
|
|
shift_y => 1, |
|
183
|
|
|
|
|
|
|
shift_z => 3, |
|
184
|
|
|
|
|
|
|
); |
|
185
|
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=cut |
|
187
|
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
sub get_translation_matrix { |
|
189
|
|
|
|
|
|
|
my ($class, %args) = @_; |
|
190
|
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
my $is3D = defined $args{shift_z} ? 1 : 0; |
|
192
|
|
|
|
|
|
|
my $shift_x = $args{shift_x} || 0; |
|
193
|
|
|
|
|
|
|
my $shift_y = $args{shift_y} || 0; |
|
194
|
|
|
|
|
|
|
my $shift_z = $args{shift_z} || 0; |
|
195
|
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
my $rows; |
|
197
|
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
if ($is3D) { |
|
199
|
|
|
|
|
|
|
$rows = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [$shift_x, $shift_y, $shift_z, 1]]; |
|
200
|
|
|
|
|
|
|
} |
|
201
|
|
|
|
|
|
|
else { |
|
202
|
|
|
|
|
|
|
$rows = [[1, 0, 0], [0, 1, 0], [$shift_x, $shift_y, 1]]; |
|
203
|
|
|
|
|
|
|
} |
|
204
|
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
my $matrix = $class->new(rows => $rows); |
|
206
|
|
|
|
|
|
|
return $matrix; |
|
207
|
|
|
|
|
|
|
} |
|
208
|
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
=head1 BUGS |
|
210
|
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
There are no known bugs at the moment. Please report any bugs or feature requests. |
|
212
|
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
=head1 EXPORT |
|
214
|
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
C<Vector::Object3D::Matrix::Transform> exports nothing neither by default nor explicitly. |
|
216
|
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
218
|
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
L<Math::VectorReal>, L<Vector::Object3D::Matrix>. |
|
220
|
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=head1 AUTHOR |
|
222
|
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
Pawel Krol, E<lt>pawelkrol@cpan.orgE<gt>. |
|
224
|
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
=head1 VERSION |
|
226
|
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
Version 0.01 (2012-12-24) |
|
228
|
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
|
230
|
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
Copyright (C) 2012 by Pawel Krol. |
|
232
|
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
This library is free open source software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.6 or, at your option, any later version of Perl 5 you may have available. |
|
234
|
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
PLEASE NOTE THAT IT COMES WITHOUT A WARRANTY OF ANY KIND! |
|
236
|
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
=cut |
|
238
|
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
no Moose::Role; |
|
240
|
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
1; |