| 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 | 8 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 69 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 8 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 8 | ▶ | use Readonly; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 74 | 8 | 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 | 45 | ▶ | sub get_rotation_matrix { | ||||||||||||||||||||||||||
|
Subroutine
get_rotation_matrix: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 95 | 45 | my ($class, %args) = @_; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 45 | ▶ | my $is3D = (defined $args{rotate_yz} || defined $args{rotate_xz}) ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $args{"rotate_yz"} or defined $args{"rotate_xz"}
MC/DC50%1/2
defined $args{"rotate_yz"} or defined $args{"rotate_xz"}
defined $args{"rotate_yz"}
defined $args{"rotate_xz"}
Truth table66%2/3
A or B
A= defined $args{"rotate_yz"}B= defined $args{"rotate_xz"}
| |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 45 | ▶ | my $x_axis_rotation_angle = $args{rotate_yz} || 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{'rotate_yz'} || 0
MC/DC100%1/1
$args{'rotate_yz'} || 0
$args{'rotate_yz'}
Truth table100%2/2
A
A= $args{'rotate_yz'}
| |||||||||||||||||||||||||||||
| 100 | 45 | ▶ | my $y_axis_rotation_angle = $args{rotate_xz} || 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{'rotate_xz'} || 0
MC/DC100%1/1
$args{'rotate_xz'} || 0
$args{'rotate_xz'}
Truth table100%2/2
A
A= $args{'rotate_xz'}
| |||||||||||||||||||||||||||||
| 101 | 45 | ▶ | my $z_axis_rotation_angle = $args{rotate_xy} || 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{'rotate_xy'} || 0
MC/DC0%0/1
$args{'rotate_xy'} || 0
$args{'rotate_xy'}
Truth table50%1/2
A
A= $args{'rotate_xy'}
| |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 45 | my $z_axis_rotation_sin = sin $z_axis_rotation_angle; | |||||||||||||||||||||||||||
| 104 | 45 | my $z_axis_rotation_cos = cos $z_axis_rotation_angle; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 45 | my $rows; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 45 | ▶ | if ($is3D) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 43 | my $x_axis_rotation_sin = sin $x_axis_rotation_angle; | |||||||||||||||||||||||||||
| 110 | 43 | my $x_axis_rotation_cos = cos $x_axis_rotation_angle; | |||||||||||||||||||||||||||
| 111 | 43 | my $y_axis_rotation_sin = sin $y_axis_rotation_angle; | |||||||||||||||||||||||||||
| 112 | 43 | my $y_axis_rotation_cos = cos $y_axis_rotation_angle; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 43 | $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 | 2 | $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 | 45 | my $matrix = $class->new(rows => $rows); | |||||||||||||||||||||||||||
| 126 | 45 | 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 | 45 | ▶ | sub get_scaling_matrix { | ||||||||||||||||||||||||||
|
Subroutine
get_scaling_matrix: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||
| 149 | 45 | my ($class, %args) = @_; | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 45 | ▶ | my $is3D = defined $args{scale_z} ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 45 | ▶ | my $scale_x = $args{scale_x} || 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{'scale_x'} || 0
MC/DC0%0/1
$args{'scale_x'} || 0
$args{'scale_x'}
Truth table50%1/2
A
A= $args{'scale_x'}
| |||||||||||||||||||||||||||||
| 153 | 45 | ▶ | my $scale_y = $args{scale_y} || 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{'scale_y'} || 0
MC/DC0%0/1
$args{'scale_y'} || 0
$args{'scale_y'}
Truth table50%1/2
A
A= $args{'scale_y'}
| |||||||||||||||||||||||||||||
| 154 | 45 | ▶ | my $scale_z = $args{scale_z} || 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{'scale_z'} || 0
MC/DC100%1/1
$args{'scale_z'} || 0
$args{'scale_z'}
Truth table100%2/2
A
A= $args{'scale_z'}
| |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 45 | my $rows; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 45 | ▶ | if ($is3D) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 43 | $rows = [[$scale_x, 0, 0, 0], [0, $scale_y, 0, 0], [0, 0, $scale_z, 0], [0, 0, 0, 1]]; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | else { | ||||||||||||||||||||||||||||
| 162 | 2 | $rows = [[$scale_x, 0, 0], [0, $scale_y, 0], [0, 0, 1]]; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 45 | my $matrix = $class->new(rows => $rows); | |||||||||||||||||||||||||||
| 166 | 45 | 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 | 46 | ▶ | sub get_translation_matrix { | ||||||||||||||||||||||||||
|
Subroutine
get_translation_matrix: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||
| 189 | 46 | my ($class, %args) = @_; | |||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 46 | ▶ | my $is3D = defined $args{shift_z} ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | 46 | ▶ | my $shift_x = $args{shift_x} || 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{'shift_x'} || 0
MC/DC0%0/1
$args{'shift_x'} || 0
$args{'shift_x'}
Truth table50%1/2
A
A= $args{'shift_x'}
| |||||||||||||||||||||||||||||
| 193 | 46 | ▶ | my $shift_y = $args{shift_y} || 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{'shift_y'} || 0
MC/DC0%0/1
$args{'shift_y'} || 0
$args{'shift_y'}
Truth table50%1/2
A
A= $args{'shift_y'}
| |||||||||||||||||||||||||||||
| 194 | 46 | ▶ | my $shift_z = $args{shift_z} || 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{'shift_z'} || 0
MC/DC100%1/1
$args{'shift_z'} || 0
$args{'shift_z'}
Truth table100%2/2
A
A= $args{'shift_z'}
| |||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 46 | my $rows; | |||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 46 | ▶ | if ($is3D) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | 43 | $rows = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [$shift_x, $shift_y, $shift_z, 1]]; | |||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | else { | ||||||||||||||||||||||||||||
| 202 | 3 | $rows = [[1, 0, 0], [0, 1, 0], [$shift_x, $shift_y, 1]]; | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 46 | my $matrix = $class->new(rows => $rows); | |||||||||||||||||||||||||||
| 206 | 46 | 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 | 8 | ▶ | no Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | 8 | 1; | |||||||||||||||||||||||||||