| 1 | package Vector::Object3D::Point::Transform; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Vector::Object3D::Point::Transform - Three-dimensional point object transformations | ||
| 6 | |||
| 7 | =head2 SYNOPSIS | ||
| 8 | |||
| 9 | package Vector::Object3D::Point; | ||
| 10 | |||
| 11 | use Readonly; | ||
| 12 | Readonly my $pi => 3.14159; | ||
| 13 | |||
| 14 | use Moose; | ||
| 15 | with 'Vector::Object3D::Point::Transform'; | ||
| 16 | |||
| 17 | # Calling any method from this role requires providing an object of a base class | ||
| 18 | # and results in creation of a new instance of the same class: | ||
| 19 | my $point = Vector::Object3D::Point->new(coord => [3, -2, 1]); | ||
| 20 | |||
| 21 | # Rotate point on a 2D plane: | ||
| 22 | my $rotatePoint2D = $point->rotate( | ||
| 23 | rotate_xy => 30 * ($pi / 180), | ||
| 24 | ); | ||
| 25 | |||
| 26 | # Scale point on a 2D plane: | ||
| 27 | my $scalePoint2D = $point->scale( | ||
| 28 | scale_x => 2, | ||
| 29 | scale_y => 2, | ||
| 30 | ); | ||
| 31 | |||
| 32 | # Translate point on a 2D plane: | ||
| 33 | my $translatePoint2D = $point->translate( | ||
| 34 | shift_x => -2, | ||
| 35 | shift_y => 1, | ||
| 36 | ); | ||
| 37 | |||
| 38 | # Rotate point in a 3D space: | ||
| 39 | my $rotatePoint3D = $point->rotate( | ||
| 40 | rotate_xy => 30 * ($pi / 180), | ||
| 41 | rotate_yz => -30 * ($pi / 180), | ||
| 42 | rotate_xz => 45 * ($pi / 180), | ||
| 43 | ); | ||
| 44 | |||
| 45 | # Scale point in a 3D space: | ||
| 46 | my $scalePoint3D = $point->scale( | ||
| 47 | scale_x => 2, | ||
| 48 | scale_y => 2, | ||
| 49 | scale_z => 3, | ||
| 50 | ); | ||
| 51 | |||
| 52 | # Translate point in a 3D space: | ||
| 53 | my $translatePoint3D = $point->translate( | ||
| 54 | shift_x => -2, | ||
| 55 | shift_y => 1, | ||
| 56 | shift_z => 3, | ||
| 57 | ); | ||
| 58 | |||
| 59 | =head1 DESCRIPTION | ||
| 60 | |||
| 61 | C<Vector::Object3D::Point::Transform> is a Moose role that is meant to be applied to C<Vector::Object3D::Point> class in order to provide it with additional methods supporting fundamental transformations in the 3D space, such as rotation, scaling and translation. | ||
| 62 | |||
| 63 | =head1 METHODS | ||
| 64 | |||
| 65 | =cut | ||
| 66 | |||
| 67 | 7 | our $VERSION = '0.01'; | |
| 68 | |||
| 69 | 7 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 70 | 7 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 71 | |||
| 72 | 7 | ▶ | use Moose::Role; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 73 | |||
| 74 | 7 | ▶ | use Vector::Object3D::Matrix; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 75 | |||
| 76 | 7 | ▶ | use Readonly; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 77 | 7 | Readonly our $pi => 3.14159; | |
| 78 | |||
| 79 | 129 | ▶ | sub _transform { |
|
Subroutine
_transform: called CC=1 SCAR=0.0
| |||
| 80 | 129 | my ($self, %args) = @_; | |
| 81 | |||
| 82 | 129 | my $point_matrix = $self->get_matrix; | |
| 83 | 129 | my $num_cols = $point_matrix->num_cols; | |
| 84 | |||
| 85 | 129 | $point_matrix->add(col => [1]); | |
| 86 | |||
| 87 | 129 | my $transformation_matrix = $args{transformation_matrix}; | |
| 88 | 129 | my $matrix_transformed = $point_matrix * $transformation_matrix; | |
| 89 | |||
| 90 | 129 | my $data = $matrix_transformed->get_rows; | |
| 91 | 129 | my @xyz = @{$data->[0]}[0 .. $num_cols - 1]; | |
| 92 | |||
| 93 | 129 | my $point_transformed = $self->new(coord => \@xyz); | |
| 94 | 129 | return $point_transformed; | |
| 95 | } | ||
| 96 | |||
| 97 | =head2 rotate | ||
| 98 | |||
| 99 | Rotate point on a 2D plane: | ||
| 100 | |||
| 101 | my $rotatePoint2D = $point->rotate( | ||
| 102 | rotate_xy => 30 * ($pi / 180), | ||
| 103 | ); | ||
| 104 | |||
| 105 | Rotate point in a 3D space: | ||
| 106 | |||
| 107 | my $rotatePoint3D = $point->rotate( | ||
| 108 | rotate_xy => 30 * ($pi / 180), | ||
| 109 | rotate_yz => -30 * ($pi / 180), | ||
| 110 | rotate_xz => 45 * ($pi / 180), | ||
| 111 | ); | ||
| 112 | |||
| 113 | =cut | ||
| 114 | |||
| 115 | 43 | ▶ | sub rotate { |
|
Subroutine
rotate: called CC=1 SCAR=0.0
Pod
documented
| |||
| 116 | 43 | my ($self, %args) = @_; | |
| 117 | |||
| 118 | 43 | my $rotate_matrix = Vector::Object3D::Matrix->get_rotation_matrix(%args); | |
| 119 | |||
| 120 | 43 | my $point_rotated = $self->_transform(transformation_matrix => $rotate_matrix); | |
| 121 | |||
| 122 | 43 | return $point_rotated; | |
| 123 | } | ||
| 124 | |||
| 125 | =head2 scale | ||
| 126 | |||
| 127 | Scale point on a 2D plane: | ||
| 128 | |||
| 129 | my $scalePoint2D = $point->scale( | ||
| 130 | scale_x => 2, | ||
| 131 | scale_y => 2, | ||
| 132 | ); | ||
| 133 | |||
| 134 | Scale point in a 3D space: | ||
| 135 | |||
| 136 | my $scalePoint3D = $point->scale( | ||
| 137 | scale_x => 2, | ||
| 138 | scale_y => 2, | ||
| 139 | scale_z => 3, | ||
| 140 | ); | ||
| 141 | |||
| 142 | =cut | ||
| 143 | |||
| 144 | 43 | ▶ | sub scale { |
|
Subroutine
scale: called CC=1 SCAR=0.0
Pod
documented
| |||
| 145 | 43 | my ($self, %args) = @_; | |
| 146 | |||
| 147 | 43 | my $scale_matrix = Vector::Object3D::Matrix->get_scaling_matrix(%args); | |
| 148 | |||
| 149 | 43 | my $point_scaled = $self->_transform(transformation_matrix => $scale_matrix); | |
| 150 | |||
| 151 | 43 | return $point_scaled; | |
| 152 | } | ||
| 153 | |||
| 154 | =head2 translate | ||
| 155 | |||
| 156 | Translate point on a 2D plane: | ||
| 157 | |||
| 158 | my $translatePoint2D = $point->translate( | ||
| 159 | shift_x => -2, | ||
| 160 | shift_y => 1, | ||
| 161 | ); | ||
| 162 | |||
| 163 | Translate point in a 3D space: | ||
| 164 | |||
| 165 | my $translatePoint3D = $point->translate( | ||
| 166 | shift_x => -2, | ||
| 167 | shift_y => 1, | ||
| 168 | shift_z => 3, | ||
| 169 | ); | ||
| 170 | |||
| 171 | =cut | ||
| 172 | |||
| 173 | 43 | ▶ | sub translate { |
|
Subroutine
translate: called CC=1 SCAR=0.0
Pod
documented
| |||
| 174 | 43 | my ($self, %args) = @_; | |
| 175 | |||
| 176 | 43 | my $translate_matrix = Vector::Object3D::Matrix->get_translation_matrix(%args); | |
| 177 | |||
| 178 | 43 | my $point_translated = $self->_transform(transformation_matrix => $translate_matrix); | |
| 179 | |||
| 180 | 43 | return $point_translated; | |
| 181 | } | ||
| 182 | |||
| 183 | =head1 BUGS | ||
| 184 | |||
| 185 | There are no known bugs at the moment. Please report any bugs or feature requests. | ||
| 186 | |||
| 187 | =head1 EXPORT | ||
| 188 | |||
| 189 | C<Vector::Object3D::Point::Transform> exports nothing neither by default nor explicitly. | ||
| 190 | |||
| 191 | =head1 SEE ALSO | ||
| 192 | |||
| 193 | L<Vector::Object3D::Point>. | ||
| 194 | |||
| 195 | =head1 AUTHOR | ||
| 196 | |||
| 197 | Pawel Krol, E<lt>pawelkrol@cpan.orgE<gt>. | ||
| 198 | |||
| 199 | =head1 VERSION | ||
| 200 | |||
| 201 | Version 0.01 (2012-12-24) | ||
| 202 | |||
| 203 | =head1 COPYRIGHT AND LICENSE | ||
| 204 | |||
| 205 | Copyright (C) 2012 by Pawel Krol. | ||
| 206 | |||
| 207 | 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. | ||
| 208 | |||
| 209 | PLEASE NOTE THAT IT COMES WITHOUT A WARRANTY OF ANY KIND! | ||
| 210 | |||
| 211 | =cut | ||
| 212 | |||
| 213 | 7 | ▶ | no Moose::Role; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 214 | |||
| 215 | 7 | 1; | |