| 1 | package Vector::Object3D::Polygon; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | Vector::Object3D::Polygon - Three-dimensional polygon object definitions and operations | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head2 SYNOPSIS | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | use Vector::Object3D::Polygon; | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | # Create polygon vertices: | ||||||||||||||||||||||||||||
| 12 | my $vertex1 = Vector::Object3D::Point->new(x => -1, y => 2, z => 3); | ||||||||||||||||||||||||||||
| 13 | my $vertex2 = Vector::Object3D::Point->new(x => 3, y => -1, z => -2); | ||||||||||||||||||||||||||||
| 14 | my $vertex3 = Vector::Object3D::Point->new(x => 2, y => 1, z => 1); | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | # Create an instance of a class: | ||||||||||||||||||||||||||||
| 17 | my $polygon = Vector::Object3D::Polygon->new(vertices => [$vertex1, $vertex2, $vertex3]); | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # Create a new object as a copy of an existing object: | ||||||||||||||||||||||||||||
| 20 | my $copy = $polygon->copy; | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # Get number of polygon vertices: | ||||||||||||||||||||||||||||
| 23 | my $num_vertices = $polygon->num_vertices; | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # Get index of last polygon vertex: | ||||||||||||||||||||||||||||
| 26 | my $last_vertex_index = $polygon->last_vertex_index; | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | # Get first vertex point: | ||||||||||||||||||||||||||||
| 29 | my $vertex1 = $polygon->get_vertex(index => 0); | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | # Get last vertex point: | ||||||||||||||||||||||||||||
| 32 | my $vertexn = $polygon->get_vertex(index => $last_vertex_index); | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # Get all vertex points: | ||||||||||||||||||||||||||||
| 35 | my @vertices = $polygon->get_vertices; | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | # Get polygon data as a set of line objects connecting vertices in construction order: | ||||||||||||||||||||||||||||
| 38 | my @lines = $polygon->get_lines; | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # Print out formatted polygon data: | ||||||||||||||||||||||||||||
| 41 | $polygon->print(fh => $fh, precision => $precision); | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | # Move polygon a constant distance in a specified direction: | ||||||||||||||||||||||||||||
| 44 | my $polygon_translated = $polygon->translate( | ||||||||||||||||||||||||||||
| 45 | shift_x => -2, | ||||||||||||||||||||||||||||
| 46 | shift_y => 1, | ||||||||||||||||||||||||||||
| 47 | shift_z => 3, | ||||||||||||||||||||||||||||
| 48 | ); | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # Enlarge, shrink or stretch polygon by a scale factor: | ||||||||||||||||||||||||||||
| 51 | my $polygon_scaled = $polygon->scale( | ||||||||||||||||||||||||||||
| 52 | scale_x => 2, | ||||||||||||||||||||||||||||
| 53 | scale_y => 2, | ||||||||||||||||||||||||||||
| 54 | scale_z => 3, | ||||||||||||||||||||||||||||
| 55 | ); | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # Rotate polygon by a given angle around three rotation axis: | ||||||||||||||||||||||||||||
| 58 | my $polygon_rotated = $polygon->rotate( | ||||||||||||||||||||||||||||
| 59 | rotate_xy => 30 * ($pi / 180), | ||||||||||||||||||||||||||||
| 60 | rotate_yz => -30 * ($pi / 180), | ||||||||||||||||||||||||||||
| 61 | rotate_xz => 45 * ($pi / 180), | ||||||||||||||||||||||||||||
| 62 | ); | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | # Project polygon onto a two-dimensional plane using an orthographic projection: | ||||||||||||||||||||||||||||
| 65 | my $polygon2D = $polygon->cast(type => 'parallel'); | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # Project polygon onto a two-dimensional plane using a perspective projection: | ||||||||||||||||||||||||||||
| 68 | my $distance = 5; | ||||||||||||||||||||||||||||
| 69 | my $polygon2D = $polygon->cast(type => 'perspective', distance => $distance); | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | # Check whether polygon's plane is visible to the observer: | ||||||||||||||||||||||||||||
| 72 | my $observer = Vector::Object3D::Point->new(x => 0, y => 0, z => $distance); | ||||||||||||||||||||||||||||
| 73 | my $is_plane_visible = $polygon->is_plane_visible(observer => $observer); | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | # Get point coordinates located exactly in the middle of a polygon's plane: | ||||||||||||||||||||||||||||
| 76 | my $middle_point = $polygon->get_middle_point; | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | # Get vector normal to a polygon's plane: | ||||||||||||||||||||||||||||
| 79 | my $normal_vector = $polygon->get_normal_vector; | ||||||||||||||||||||||||||||
| 80 | my $normal_vector = $polygon->get_orthogonal_vector; | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # Compare two polygon objects: | ||||||||||||||||||||||||||||
| 83 | my $are_the_same = $polygon1 == $polygon2; | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | C<Vector::Object3D::Polygon> provides an abstraction layer for describing polygon object in a three-dimensional space by composing it from any number of C<Vector::Object3D::Point> objects (referred onwards as vertices). | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =head1 METHODS | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =head2 new | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | Create an instance of a C<Vector::Object3D::Polygon> class: | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | my $vertex1 = Vector::Object3D::Point->new(x => -1, y => 2, z => 3); | ||||||||||||||||||||||||||||
| 96 | my $vertex2 = Vector::Object3D::Point->new(x => 3, y => -1, z => -2); | ||||||||||||||||||||||||||||
| 97 | my $vertex3 = Vector::Object3D::Point->new(x => 2, y => 1, z => 1); | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | my $polygon = Vector::Object3D::Polygon->new(vertices => [$vertex1, $vertex2, $vertex3]); | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | C<Vector::Object3D::Polygon> requires provision of at least three endpoints in order to successfully construct an object instance, there is no exception from this rule. Furthermore, it is assumed that all vertex points are located on the same plane. This rule is neither enforced nor validated, however this assumption impacts all related calculations, i.a. normal vector computation. | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =cut | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 3 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 108 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 3 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 3 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 113 | 3 | ▶ | use Math::VectorReal; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 114 | 3 | ▶ | use Scalar::Util qw(looks_like_number); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 115 | 3 | ▶ | use Vector::Object3D::Point; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | use overload | ||||||||||||||||||||||||||||
| 118 | 3 | ▶ | '==' => \&_comparison, | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 119 | 3 | '!=' => \&_negative_comparison; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 3 | has 'vertices' => ( | |||||||||||||||||||||||||||
| 122 | is => 'ro', | ||||||||||||||||||||||||||||
| 123 | isa => 'ArrayRef[Vector::Object3D::Point]', | ||||||||||||||||||||||||||||
| 124 | reader => '_get_vertices', | ||||||||||||||||||||||||||||
| 125 | required => 1, | ||||||||||||||||||||||||||||
| 126 | ); | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 531 | ▶ | around BUILDARGS => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||
| 129 | 531 | my ($orig, $class, %args) = @_; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 531 | my $vertices_orig = $args{vertices}; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 531 | my @vertices_copy; | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 531 | ▶ | if (defined $vertices_orig and ref $vertices_orig eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $vertices_orig and ref $vertices_orig eq "ARRAY"
MC/DC0%0/2
defined $vertices_orig and ref $vertices_orig eq "ARRAY"
defined $vertices_orig
ref $vertices_orig eq "ARRAY"
Truth table33%1/3
A and B
A= defined $vertices_origB= ref $vertices_orig eq "ARRAY"
| |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 531 | for my $vertex (@{$vertices_orig}) { | |||||||||||||||||||||||||||
| 138 | 1707 | push @vertices_copy, $vertex->copy; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 530 | $args{vertices} = \@vertices_copy; | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 530 | return $class->$orig(%args); | |||||||||||||||||||||||||||
| 145 | 3 | }; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 530 | ▶ | sub BUILD { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 148 | 530 | my ($self) = @_; | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 530 | my $num_vertices = $self->num_vertices; | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 530 | ▶ | if ($num_vertices < 3) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 1 | croak qq{Insufficient number of vertices used to initialize polygon object: $num_vertices (expected at least 3 points)}; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 529 | my $num_2D_vertices = $self->_count_2D_vertices; | |||||||||||||||||||||||||||
| 157 | 529 | my $num_3D_vertices = $self->_count_3D_vertices; | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 529 | ▶ | if ($num_2D_vertices > 0 && $num_3D_vertices > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$num_2D_vertices > 0 and $num_3D_vertices > 0
MC/DC100%2/2
$num_2D_vertices > 0 and $num_3D_vertices > 0
$num_2D_vertices > 0
$num_3D_vertices > 0
Truth table100%3/3
A and B
A= $num_2D_vertices > 0B= $num_3D_vertices > 0
| |||||||||||||||||||||||||||||
| 160 | 1 | croak qq{Initializing polygon object with mixed-up 2D/3D point coordinates: ${num_2D_vertices} 2D vertices and ${num_3D_vertices} 3D vertices (expected more consistent approach)}; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 528 | return; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | =head2 copy | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | Create a new C<Vector::Object3D::Polygon> object as a copy of an existing object: | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | my $copy = $polygon->copy; | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =cut | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 252 | ▶ | sub copy { | ||||||||||||||||||||||||||
|
Subroutine
copy: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 175 | 252 | my ($self) = @_; | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 252 | my $vertices = $self->_get_vertices; | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 252 | my $class = $self->meta->name; | |||||||||||||||||||||||||||
| 180 | 252 | my $copy = $class->new(vertices => $vertices); | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 252 | return $copy; | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =head2 num_vertices | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | Get number of polygon vertices: | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | my $num_vertices = $polygon->num_vertices; | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | =cut | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 545 | ▶ | sub num_vertices { | ||||||||||||||||||||||||||
|
Subroutine
num_vertices: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 194 | 545 | my ($self) = @_; | |||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 545 | my $vertices = $self->_get_vertices; | |||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 545 | return scalar @{$vertices}; | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 529 | ▶ | sub _count_2D_vertices { | ||||||||||||||||||||||||||
|
Subroutine
_count_2D_vertices: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 202 | 529 | my ($self) = @_; | |||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 1704 | ▶ | my $check = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 205 | 1704 | my ($vertex) = @_; | |||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 1704 | return not defined $vertex->get_z; | |||||||||||||||||||||||||||
| 208 | 529 | }; | |||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 529 | return $self->_count_vertices($check); | |||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | 529 | ▶ | sub _count_3D_vertices { | ||||||||||||||||||||||||||
|
Subroutine
_count_3D_vertices: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 214 | 529 | my ($self) = @_; | |||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 1704 | ▶ | my $check = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 217 | 1704 | my ($vertex) = @_; | |||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 1704 | return defined $vertex->get_z; | |||||||||||||||||||||||||||
| 220 | 529 | }; | |||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | 529 | return $self->_count_vertices($check); | |||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 1058 | ▶ | sub _count_vertices { | ||||||||||||||||||||||||||
|
Subroutine
_count_vertices: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 226 | 1058 | my ($self, $check) = @_; | |||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 1058 | my @vertices = $self->get_vertices; | |||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 1058 | my $count = grep { $check->($_) } @vertices; | |||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 1058 | return $count; | |||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | =head2 last_vertex_index | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | Get index of last polygon vertex: | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | my $last_vertex_index = $polygon->last_vertex_index; | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | =cut | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | 8 | ▶ | sub last_vertex_index { | ||||||||||||||||||||||||||
|
Subroutine
last_vertex_index: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 244 | 8 | my ($self) = @_; | |||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | 8 | my $vertices = $self->_get_vertices; | |||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | 8 | return $#{$vertices}; | |||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | =head2 get_vertex | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | Get C<$n>-th vertex point, where C<$n> is expected to be any number between first and last vertex index: | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | my $vertexn = $polygon->get_vertex(index => $n); | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | =cut | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | 6 | ▶ | sub get_vertex { | ||||||||||||||||||||||||||
|
Subroutine
get_vertex: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 260 | 6 | my ($self, %args) = @_; | |||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | 6 | my $index = $args{index}; | |||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | 6 | ▶ | unless (looks_like_number $index) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 265 | 1 | croak qq{Unable to get vertex point with a non-numeric index value: $index}; | |||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | 5 | ▶ | if ($index < 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 269 | 1 | croak qq{Unable to get vertex point with index value below acceptable range: $index}; | |||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | 4 | ▶ | if ($index > $self->last_vertex_index) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 273 | 1 | croak qq{Unable to get vertex point with index value beyond acceptable range: $index}; | |||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | 3 | my @vertices = $self->get_vertices; | |||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | 3 | return $vertices[$index]; | |||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | =head2 get_vertices | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | Get all vertex points: | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | my @vertices = $polygon->get_vertices; | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | =cut | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | 1064 | ▶ | sub get_vertices { | ||||||||||||||||||||||||||
|
Subroutine
get_vertices: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 290 | 1064 | my ($self) = @_; | |||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | 1064 | my $vertices = $self->_get_vertices; | |||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | 1064 | return map { $_->copy } @{$vertices}; | |||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | =head2 get_lines | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | Get polygon data as a set of line objects connecting vertices in construction order: | ||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | my @lines = $polygon->get_lines; | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | =cut | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | 1 | ▶ | sub get_lines { | ||||||||||||||||||||||||||
|
Subroutine
get_lines: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 306 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | 1 | my $vertices = $self->_get_vertices; | |||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | 1 | my $last_vertex_index = $self->last_vertex_index; | |||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | 1 | my @lines; | |||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | 1 | ▶ | for (my $i = 0; $i <= $last_vertex_index; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | 3 | my @endpoints = ($vertices->[$i]); | |||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | 3 | ▶ | if ($i == $last_vertex_index) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 319 | 1 | push @endpoints, $vertices->[0]; | |||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||
| 321 | else { | ||||||||||||||||||||||||||||
| 322 | 2 | push @endpoints, $vertices->[$i + 1]; | |||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | 3 | my $line = Vector::Object3D::Line->new(vertices => \@endpoints); | |||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | 3 | push @lines, $line; | |||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | 1 | return @lines; | |||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | =head2 print | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | Print out text-formatted polygon data (which might be, for instance, useful for debugging purposes): | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | $polygon->print(fh => $fh, precision => $precision); | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | C<fh> defaults to the standard output. C<precision> is intended for internal use by string format specifier that outputs individual point coordinates as decimal floating points, and defaults to 2 (unless adjusted individually for each vertex). | ||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | =cut | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | 8 | ▶ | sub print { | ||||||||||||||||||||||||||
|
Subroutine
print: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 344 | 8 | my ($self, %args) = @_; | |||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | 8 | my $vertices = $self->_get_vertices; | |||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | 8 | for my $vertex (@{$vertices}) { | |||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | 25 | $vertex->print(%args); | |||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | 8 | return; | |||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | =head2 | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | Move polygon a constant distance in a specified direction: | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | my $polygon_translated = $polygon->translate( | ||||||||||||||||||||||||||||
| 361 | shift_x => -2, | ||||||||||||||||||||||||||||
| 362 | shift_y => 1, | ||||||||||||||||||||||||||||
| 363 | shift_z => 3, | ||||||||||||||||||||||||||||
| 364 | ); | ||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | =cut | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | 11 | ▶ | sub translate { | ||||||||||||||||||||||||||
|
Subroutine
translate: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 369 | 11 | my ($self, %args) = @_; | |||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | 11 | my $vertices = $self->_get_vertices; | |||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | 11 | my @new_vertices; | |||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | 11 | for my $vertex (@{$vertices}) { | |||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | 36 | push @new_vertices, $vertex->translate(%args); | |||||||||||||||||||||||||||
| 378 | } | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | 11 | my $polygon_translated = $self->new(vertices => \@new_vertices); | |||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | 11 | return $polygon_translated; | |||||||||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | =head2 | ||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | Enlarge, shrink or stretch polygon by a scale factor: | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | my $polygon_scaled = $polygon->scale( | ||||||||||||||||||||||||||||
| 390 | scale_x => 2, | ||||||||||||||||||||||||||||
| 391 | scale_y => 2, | ||||||||||||||||||||||||||||
| 392 | scale_z => 3, | ||||||||||||||||||||||||||||
| 393 | ); | ||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | =cut | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | 11 | ▶ | sub scale { | ||||||||||||||||||||||||||
|
Subroutine
scale: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 398 | 11 | my ($self, %args) = @_; | |||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | 11 | my $vertices = $self->_get_vertices; | |||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | 11 | my @new_vertices; | |||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | 11 | for my $vertex (@{$vertices}) { | |||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | 36 | push @new_vertices, $vertex->scale(%args); | |||||||||||||||||||||||||||
| 407 | } | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | 11 | my $polygon_scaled = $self->new(vertices => \@new_vertices); | |||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | 11 | return $polygon_scaled; | |||||||||||||||||||||||||||
| 412 | } | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | =head2 | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | Rotate polygon by a given angle around three rotation axis: | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | my $polygon_rotated = $polygon->rotate( | ||||||||||||||||||||||||||||
| 419 | rotate_xy => 30 * ($pi / 180), | ||||||||||||||||||||||||||||
| 420 | rotate_yz => -30 * ($pi / 180), | ||||||||||||||||||||||||||||
| 421 | rotate_xz => 45 * ($pi / 180), | ||||||||||||||||||||||||||||
| 422 | ); | ||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | =cut | ||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | 11 | ▶ | sub rotate { | ||||||||||||||||||||||||||
|
Subroutine
rotate: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 427 | 11 | my ($self, %args) = @_; | |||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | 11 | my $vertices = $self->_get_vertices; | |||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | 11 | my @new_vertices; | |||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | 11 | for my $vertex (@{$vertices}) { | |||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | 36 | push @new_vertices, $vertex->rotate(%args); | |||||||||||||||||||||||||||
| 436 | } | ||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | 11 | my $polygon_rotated = $self->new(vertices => \@new_vertices); | |||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | 11 | return $polygon_rotated; | |||||||||||||||||||||||||||
| 441 | } | ||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||
| 443 | =head2 | ||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | Project polygon onto a two-dimensional plane using an orthographic projection: | ||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | my $polygon2D = $polygon->cast(type => 'parallel'); | ||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | Project polygon onto a two-dimensional plane using a perspective projection: | ||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||
| 451 | my $distance = 5; | ||||||||||||||||||||||||||||
| 452 | my $polygon2D = $polygon->cast(type => 'perspective', distance => $distance); | ||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||
| 454 | =cut | ||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||
| 456 | 22 | ▶ | sub cast { | ||||||||||||||||||||||||||
|
Subroutine
cast: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 457 | 22 | my ($self, %args) = @_; | |||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||
| 459 | 22 | my $vertices = $self->_get_vertices; | |||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | 22 | my @new_vertices; | |||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||
| 463 | 22 | for my $vertex (@{$vertices}) { | |||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||
| 465 | 72 | push @new_vertices, $vertex->cast(%args); | |||||||||||||||||||||||||||
| 466 | } | ||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||
| 468 | 22 | my $polygon_casted = $self->new(vertices => \@new_vertices); | |||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | 22 | return $polygon_casted; | |||||||||||||||||||||||||||
| 471 | } | ||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||
| 473 | =head2 | ||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||
| 475 | Check whether polygon's plane is visible to the observer located at the given point: | ||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||
| 477 | my $observer = Vector::Object3D::Point->new(x => 0, y => 0, z => 5); | ||||||||||||||||||||||||||||
| 478 | my $is_plane_visible = $polygon->is_plane_visible(observer => $observer); | ||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | =cut | ||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||
| 482 | 17 | ▶ | sub is_plane_visible { | ||||||||||||||||||||||||||
|
Subroutine
is_plane_visible: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 483 | 17 | my ($self, %args) = @_; | |||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||
| 485 | 17 | my $observer = $args{observer}; | |||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||
| 487 | 17 | my $N = $self->get_orthogonal_vector; | |||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||
| 489 | 17 | ▶ | unless (defined $observer) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||
| 491 | 5 | ▶ | if ($N->z > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 492 | 4 | return 1; | |||||||||||||||||||||||||||
| 493 | } | ||||||||||||||||||||||||||||
| 494 | else { | ||||||||||||||||||||||||||||
| 495 | 1 | return 0; | |||||||||||||||||||||||||||
| 496 | } | ||||||||||||||||||||||||||||
| 497 | } | ||||||||||||||||||||||||||||
| 498 | else { | ||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||
| 500 | # Check angle between normal and observer vectors: | ||||||||||||||||||||||||||||
| 501 | 12 | my ($normal_x, $normal_y, $normal_z) = $N->array; | |||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||
| 503 | # First let's get another vector from one of vertices to an observer's eyes: | ||||||||||||||||||||||||||||
| 504 | 12 | my $observer_vector = _get_vector_from_polygon_to_observer($self, $observer); | |||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||
| 506 | # SK = N razy (skalarnie) R = n1*r1+n2*r2+n3*r3 | ||||||||||||||||||||||||||||
| 507 | 12 | my $sk = $observer_vector->{x} * $normal_x + $observer_vector->{y} * $normal_y + $observer_vector->{z} * $normal_z; | |||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||
| 509 | 12 | ▶ | if ($sk <= 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 510 | 8 | return 1; | |||||||||||||||||||||||||||
| 511 | } | ||||||||||||||||||||||||||||
| 512 | else { | ||||||||||||||||||||||||||||
| 513 | 4 | return 0; | |||||||||||||||||||||||||||
| 514 | } | ||||||||||||||||||||||||||||
| 515 | } | ||||||||||||||||||||||||||||
| 516 | } | ||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||
| 518 | 12 | ▶ | sub _get_vector_from_polygon_to_observer { | ||||||||||||||||||||||||||
|
Subroutine
_get_vector_from_polygon_to_observer: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 519 | 12 | my ($self, $observer_point) = @_; | |||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||
| 521 | # Get middle point from a polygon: | ||||||||||||||||||||||||||||
| 522 | 12 | my $polygon_point = $self->get_middle_point; | |||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||
| 524 | # Calculate vector directed from polygon to observer: | ||||||||||||||||||||||||||||
| 525 | 12 | my ($x1, $y1, $z1) = $observer_point->get_xyz; | |||||||||||||||||||||||||||
| 526 | 12 | my ($x2, $y2, $z2) = $polygon_point->get_xyz; | |||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||
| 528 | # my $v = vector($vx, $vy, $vz); | ||||||||||||||||||||||||||||
| 529 | 12 | my $vx = $x2 - $x1; | |||||||||||||||||||||||||||
| 530 | 12 | my $vy = $y2 - $y1; | |||||||||||||||||||||||||||
| 531 | 12 | my $vz = $z2 - $z1; | |||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||
| 533 | 12 | my %v = ( | |||||||||||||||||||||||||||
| 534 | x => $vx, | ||||||||||||||||||||||||||||
| 535 | y => $vy, | ||||||||||||||||||||||||||||
| 536 | z => $vz, | ||||||||||||||||||||||||||||
| 537 | ); | ||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||
| 539 | 12 | return \%v; | |||||||||||||||||||||||||||
| 540 | } | ||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||
| 542 | =head2 get_middle_point | ||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||
| 544 | Get point coordinates located exactly in the middle of a polygon's plane (remember assumption that all vertex points are located on the same plane): | ||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||
| 546 | my $middle_point = $polygon->get_middle_point; | ||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||
| 548 | =cut | ||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||
| 550 | 14 | ▶ | sub get_middle_point { | ||||||||||||||||||||||||||
|
Subroutine
get_middle_point: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 551 | 14 | my ($self) = @_; | |||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||
| 553 | 14 | my $vertices = $self->_get_vertices; | |||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||
| 555 | 14 | my ($total_x, $total_y, $total_z); | |||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||
| 557 | 14 | for my $vertex (@{$vertices}) { | |||||||||||||||||||||||||||
| 558 | 45 | my ($x, $y, $z) = $vertex->get_xyz; | |||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||
| 560 | 45 | $total_x += $x; | |||||||||||||||||||||||||||
| 561 | 45 | $total_y += $y; | |||||||||||||||||||||||||||
| 562 | 45 | $total_z += $z; | |||||||||||||||||||||||||||
| 563 | } | ||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||
| 565 | 14 | my $num_vertices = $self->num_vertices; | |||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||
| 567 | 14 | $total_x /= $num_vertices; | |||||||||||||||||||||||||||
| 568 | 14 | $total_y /= $num_vertices; | |||||||||||||||||||||||||||
| 569 | 14 | $total_z /= $num_vertices; | |||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||
| 571 | 14 | my $point = Vector::Object3D::Point->new(x => $total_x, y => $total_y, z => $total_z); | |||||||||||||||||||||||||||
| 572 | 14 | return $point; | |||||||||||||||||||||||||||
| 573 | } | ||||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||
| 575 | =head2 get_normal_vector | ||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||
| 577 | Get vector normal to a polygon's plane (remember assumption that all vertex points are located on the same plane): | ||||||||||||||||||||||||||||
| 578 | |||||||||||||||||||||||||||||
| 579 | my $normal_vector = $polygon->get_normal_vector; | ||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||
| 581 | Result of calling this method is a L<Math::VectorReal> object instance. You may access individual x, y, z elements of the vector as a list of values using C<array> method: | ||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||
| 583 | my ($x, $y, $z) = $normal_vector->array; | ||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||
| 585 | =cut | ||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||
| 587 | 19 | ▶ | sub get_normal_vector { | ||||||||||||||||||||||||||
|
Subroutine
get_normal_vector: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 588 | 19 | my ($self) = @_; | |||||||||||||||||||||||||||
| 589 | |||||||||||||||||||||||||||||
| 590 | 19 | my $vertices = $self->_get_vertices; | |||||||||||||||||||||||||||
| 591 | |||||||||||||||||||||||||||||
| 592 | 19 | my $vertex1 = $vertices->[0]; | |||||||||||||||||||||||||||
| 593 | 19 | my $vertex2 = $vertices->[1]; | |||||||||||||||||||||||||||
| 594 | 19 | my $vertex3 = $vertices->[2]; | |||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||
| 596 | 19 | my ($x1, $y1, $z1) = $vertex1->get_xyz; | |||||||||||||||||||||||||||
| 597 | 19 | my ($x2, $y2, $z2) = $vertex2->get_xyz; | |||||||||||||||||||||||||||
| 598 | 19 | my ($x3, $y3, $z3) = $vertex3->get_xyz; | |||||||||||||||||||||||||||
| 599 | |||||||||||||||||||||||||||||
| 600 | 19 | my $v1 = vector($x1, $y1, $z1); | |||||||||||||||||||||||||||
| 601 | 19 | my $v2 = vector($x2, $y2, $z2); | |||||||||||||||||||||||||||
| 602 | 19 | my $v3 = vector($x3, $y3, $z3); | |||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||
| 604 | 19 | my $U = $v3 - $v2; | |||||||||||||||||||||||||||
| 605 | 19 | my $V = $v1 - $v2; | |||||||||||||||||||||||||||
| 606 | 19 | my $N = $V x $U; | |||||||||||||||||||||||||||
| 607 | |||||||||||||||||||||||||||||
| 608 | 19 | return $N; | |||||||||||||||||||||||||||
| 609 | } | ||||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||
| 611 | =head2 get_orthogonal_vector | ||||||||||||||||||||||||||||
| 612 | |||||||||||||||||||||||||||||
| 613 | Get vector normal to a polygon's plane: | ||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||
| 615 | my $normal_vector = $polygon->get_orthogonal_vector; | ||||||||||||||||||||||||||||
| 616 | |||||||||||||||||||||||||||||
| 617 | This is an alias for C<get_normal_vector>. | ||||||||||||||||||||||||||||
| 618 | |||||||||||||||||||||||||||||
| 619 | =cut | ||||||||||||||||||||||||||||
| 620 | |||||||||||||||||||||||||||||
| 621 | 18 | ▶ | sub get_orthogonal_vector { | ||||||||||||||||||||||||||
|
Subroutine
get_orthogonal_vector: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 622 | 18 | my ($self) = @_; | |||||||||||||||||||||||||||
| 623 | |||||||||||||||||||||||||||||
| 624 | 18 | return $self->get_normal_vector; | |||||||||||||||||||||||||||
| 625 | } | ||||||||||||||||||||||||||||
| 626 | |||||||||||||||||||||||||||||
| 627 | =head2 compare (==) | ||||||||||||||||||||||||||||
| 628 | |||||||||||||||||||||||||||||
| 629 | Compare two polygon objects: | ||||||||||||||||||||||||||||
| 630 | |||||||||||||||||||||||||||||
| 631 | my $are_the_same = $polygon1 == $polygon2; | ||||||||||||||||||||||||||||
| 632 | |||||||||||||||||||||||||||||
| 633 | Overloaded comparison operator evaluates to true whenever two polygon objects are identical (all their endpoints are located at exactly same positions, note that vertex order matters as well). | ||||||||||||||||||||||||||||
| 634 | |||||||||||||||||||||||||||||
| 635 | =cut | ||||||||||||||||||||||||||||
| 636 | |||||||||||||||||||||||||||||
| 637 | 48 | ▶ | sub _comparison { | ||||||||||||||||||||||||||
|
Subroutine
_comparison: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 638 | 48 | my ($self, $arg) = @_; | |||||||||||||||||||||||||||
| 639 | |||||||||||||||||||||||||||||
| 640 | 48 | my $vertices1 = $self->_get_vertices; | |||||||||||||||||||||||||||
| 641 | 48 | my $vertices2 = $arg->_get_vertices; | |||||||||||||||||||||||||||
| 642 | |||||||||||||||||||||||||||||
| 643 | 48 | ▶ | return unless @{$vertices1} == @{$vertices2}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 644 | |||||||||||||||||||||||||||||
| 645 | 48 | ▶ | for (my $i = 0; $i < @{$vertices1}; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 646 | |||||||||||||||||||||||||||||
| 647 | 157 | my $vertex1 = $vertices1->[$i]; | |||||||||||||||||||||||||||
| 648 | 157 | my $vertex2 = $vertices2->[$i]; | |||||||||||||||||||||||||||
| 649 | |||||||||||||||||||||||||||||
| 650 | 157 | ▶ | return unless $vertex1 == $vertex2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 651 | } | ||||||||||||||||||||||||||||
| 652 | |||||||||||||||||||||||||||||
| 653 | 46 | return 1; | |||||||||||||||||||||||||||
| 654 | } | ||||||||||||||||||||||||||||
| 655 | |||||||||||||||||||||||||||||
| 656 | 2 | ▶ | sub _negative_comparison { | ||||||||||||||||||||||||||
|
Subroutine
_negative_comparison: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 657 | 2 | my ($self, $arg) = @_; | |||||||||||||||||||||||||||
| 658 | |||||||||||||||||||||||||||||
| 659 | 2 | return not $self->_comparison($arg); | |||||||||||||||||||||||||||
| 660 | } | ||||||||||||||||||||||||||||
| 661 | |||||||||||||||||||||||||||||
| 662 | =head1 BUGS | ||||||||||||||||||||||||||||
| 663 | |||||||||||||||||||||||||||||
| 664 | There are no known bugs at the moment. Please report any bugs or feature requests. | ||||||||||||||||||||||||||||
| 665 | |||||||||||||||||||||||||||||
| 666 | =head1 EXPORT | ||||||||||||||||||||||||||||
| 667 | |||||||||||||||||||||||||||||
| 668 | C<Vector::Object3D::Polygon> exports nothing neither by default nor explicitly. | ||||||||||||||||||||||||||||
| 669 | |||||||||||||||||||||||||||||
| 670 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 671 | |||||||||||||||||||||||||||||
| 672 | L<Math::VectorReal>, L<Vector::Object3D>, L<Vector::Object3D::Point>. | ||||||||||||||||||||||||||||
| 673 | |||||||||||||||||||||||||||||
| 674 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 675 | |||||||||||||||||||||||||||||
| 676 | Pawel Krol, E<lt>pawelkrol@cpan.orgE<gt>. | ||||||||||||||||||||||||||||
| 677 | |||||||||||||||||||||||||||||
| 678 | =head1 VERSION | ||||||||||||||||||||||||||||
| 679 | |||||||||||||||||||||||||||||
| 680 | Version 0.01 (2012-12-24) | ||||||||||||||||||||||||||||
| 681 | |||||||||||||||||||||||||||||
| 682 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 683 | |||||||||||||||||||||||||||||
| 684 | Copyright (C) 2012 by Pawel Krol. | ||||||||||||||||||||||||||||
| 685 | |||||||||||||||||||||||||||||
| 686 | 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. | ||||||||||||||||||||||||||||
| 687 | |||||||||||||||||||||||||||||
| 688 | PLEASE NOTE THAT IT COMES WITHOUT A WARRANTY OF ANY KIND! | ||||||||||||||||||||||||||||
| 689 | |||||||||||||||||||||||||||||
| 690 | =cut | ||||||||||||||||||||||||||||
| 691 | |||||||||||||||||||||||||||||
| 692 | 3 | ▶ | no Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 693 | 3 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 694 | |||||||||||||||||||||||||||||
| 695 | 3 | 1; | |||||||||||||||||||||||||||