Object3D.pm

filter: Statementstmt 100% 139 / 139 Branchbran 90.9% 20 / 22 Conditioncond 50.0% 4 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 22 / 22 Podpod 54.5% 6 / 11 totaltotal 92.7% 192 / 207 CC 20 SCAR 9.5
CC
20
Cov
96%
__ANON__
4.2
_comparison
4.0
get_polygon
4.0
CRAP 2.6
« Object3D/Point.pm ↑ Summary Object3D/Polygon.pm »
1 package Vector::Object3D;
2  
3 =head1 NAME
4  
5 Vector::Object3D - Three-dimensional object type definitions and operations
6  
7 =head2 SYNOPSIS
8  
9 use Vector::Object3D;
10  
11 # Create an instance of a class:
12 my $object = Vector::Object3D->new(polygons => [$polygon1, $polygon2, $polygon3]);
13  
14 # Create a new object as a copy of an existing object:
15 my $copy = $object->copy;
16  
17 # Get number of polygons that make up an object:
18 my $num_faces = $object->num_faces;
19  
20 # Get index of last polygon:
21 my $last_face_index = $object->last_face_index;
22  
23 # Get first polygon:
24 my $polygon1 = $object->get_polygon(index => 0);
25  
26 # Get last polygon:
27 my $polygonn = $object->get_polygon(index => $last_face_index);
28  
29 # Get all polygons:
30 my @polygons = $object->get_polygons;
31 my @polygons = $object->get_polygons(mode => 'all');
32  
33 # Get visible polygons only:
34 my $observer = Vector::Object3D::Point->new(x => 0, y => 0, z => 5);
35 my @polygons = $object->get_polygons(mode => 'visible', observer => $observer);
36  
37 # Print out formatted object data:
38 $object->print(fh => $fh, precision => $precision);
39  
40 # Move object a constant distance in a specified direction:
41 my $object_translated = $object->translate(
42 shift_x => -2,
43 shift_y => 1,
44 shift_z => 3,
45 );
46  
47 # Enlarge, shrink or stretch object by a scale factor:
48 my $object_scaled = $object->scale(
49 scale_x => 2,
50 scale_y => 2,
51 scale_z => 3,
52 );
53  
54 # Rotate object by a given angle around three rotation axis:
55 my $object_rotated = $object->rotate(
56 rotate_xy => 30 * ($pi / 180),
57 rotate_yz => -30 * ($pi / 180),
58 rotate_xz => 45 * ($pi / 180),
59 );
60  
61 # Project object onto a two-dimensional plane using an orthographic projection:
62 my $object2D = $object->cast(type => 'parallel');
63  
64 # Project object onto a two-dimensional plane using a perspective projection:
65 my $distance = 5;
66 my $object2D = $object->cast(type => 'perspective', distance => $distance);
67  
68 # Compare two objects:
69 my $are_the_same = $object1 == $object2;
70  
71 =head1 DESCRIPTION
72  
73 C<Vector::Object3D> provides an abstraction layer for describing objects made of polygons in a three-dimensional space. It has been primarily designed to help with rapid prototyping of simple 3D vector graphic transformations, and is most likely unsuitable for realtime calculations that usually demand high computational CPU power.
74  
75 This version of C<Vector::Object3D> package has been entirely rewritten using Moose object system and is significantly slower than its predecessor initially developed using classic Perl's object system. Main reasoning for switching over to Moose was my desire to comply with the concepts of modern Perl programming.
76  
77 =head1 METHODS
78  
79 =head2 new
80  
81 Create an instance of a C<Vector::Object3D> class:
82  
83 my $object = Vector::Object3D->new(polygons => [$polygon1, $polygon2, $polygon3]);
84  
85 C<Vector::Object3D> require provision of at least one polygon in order to successfully construct an object instance, there is no exception from this rule.
86  
87 =cut
88  
89 2 our $VERSION = '0.01';
90  
91 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
92 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
93  
94 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
95  
96 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
97 2 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
98 2 ▶use Vector::Object3D::Polygon;
Subroutine BEGIN: called CC=1 SCAR=0.0
99  
100 use overload
101 2 ▶ '==' => \&_comparison,
Subroutine BEGIN: called CC=1 SCAR=0.0
102 2 '!=' => \&_negative_comparison;
103  
104 2 has 'polygons' => (
105 is => 'ro',
106 isa => 'ArrayRef[Vector::Object3D::Polygon]',
107 reader => '_get_polygons',
108 required => 1,
109 );
110  
111 44 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=4 SCAR=14.2
112 44 my ($orig, $class, %args) = @_;
113  
114 44 my $polygons_orig = $args{polygons};
115  
116 44 my @polygons_copy;
117  
118 44 ▶ if (defined $polygons_orig and ref $polygons_orig eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $polygons_orig and ref $polygons_orig eq "ARRAY") 44 0 44
Condition33%1/3
defined $polygons_orig and ref $polygons_orig eq "ARRAY"
!ll&&!rl&&r
0044
MC/DC0%0/2
defined $polygons_orig and ref $polygons_orig eq "ARRAY"
defined $polygons_orig ref $polygons_orig eq "ARRAY"
Truth table33%1/3
A and B
A= defined $polygons_origB= ref $polygons_orig eq "ARRAY"
ABresult
0X0
100
111
119  
120 44 for my $polygon (@{$polygons_orig}) {
121 205 push @polygons_copy, $polygon->copy;
122 }
123 }
124  
125 43 $args{polygons} = \@polygons_copy;
126  
127 43 return $class->$orig(%args);
128 2 };
129  
130 43 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
131 43 my ($self) = @_;
132  
133 43 my $num_faces = $self->num_faces;
134  
135 43 ▶ if ($num_faces < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num_faces < 1) 1 42 43
136 1 croak qq{Insufficient number of polygons used to initialize object: $num_faces (expected at least 1 polygon)};
137 }
138  
139 42 return;
140 }
141  
142 =head2 copy
143  
144 Create a new C<Vector::Object3D> object as a copy of an existing object:
145  
146 my $copy = $object->copy;
147  
148 =cut
149  
150 1 ▶sub copy {
Subroutine copy: called CC=1 SCAR=0.0
Pod documented
151 1 my ($self) = @_;
152  
153 1 my $polygons = $self->_get_polygons;
154  
155 1 my $class = $self->meta->name;
156 1 my $copy = $class->new(polygons => $polygons);
157  
158 1 return $copy;
159 }
160  
161 =head2 num_faces
162  
163 Get number of polygons that make up an object:
164  
165 my $num_faces = $object->num_faces;
166  
167 =cut
168  
169 45 ▶sub num_faces {
Subroutine num_faces: called CC=1 SCAR=0.0
Pod documented
170 45 my ($self) = @_;
171  
172 45 my $faces = $self->_get_polygons;
173  
174 45 return scalar @{$faces};
175 }
176  
177 =head2 last_face_index
178  
179 Get index of last polygon:
180  
181 my $last_face_index = $object->last_face_index;
182  
183 =cut
184  
185 8 ▶sub last_face_index {
Subroutine last_face_index: called CC=1 SCAR=0.0
Pod documented
186 8 my ($self) = @_;
187  
188 8 my $faces = $self->_get_polygons;
189  
190 8 return $#{$faces};
191 }
192  
193 =head2 get_polygon
194  
195 Get C<$n>-th polygon, where C<$n> is expected to be any number between first and last polygon index:
196  
197 my $polygonn = $object->get_polygon(index => $n);
198  
199 =cut
200  
201 7 ▶sub get_polygon {
Subroutine get_polygon: called CC=4 SCAR=13.9
Pod documented
202 7 my ($self, %args) = @_;
203  
204 7 my $index = $args{index};
205  
206 7 ▶ unless (looks_like_number $index) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&looks_like_number($index)) 1 6 7
207 1 croak qq{Unable to get polygon with a non-numeric index value: $index};
208 }
209  
210 6 ▶ if ($index < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < 0) 1 5 6
211 1 croak qq{Unable to get polygon with index value below acceptable range: $index};
212 }
213  
214 5 ▶ if ($index > $self->last_face_index) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index > $self->last_face_index) 1 4 5
215 1 croak qq{Unable to get polygon with index value beyond acceptable range: $index};
216 }
217  
218 4 my @polygons = $self->get_polygons;
219  
220 4 return $polygons[$index];
221 }
222  
223 =head2 get_polygons
224  
225 Get all polygons:
226  
227 my @polygons = $object->get_polygons;
228  
229 The same effect is achieved by explicitly setting mode of getting polygons to C<all>:
230  
231 my @polygons = $object->get_polygons(mode => 'all');
232  
233 Get visible polygons only by setting mode of getting polygons to C<visible> and specifying optional observer:
234  
235 my $observer = Vector::Object3D::Point->new(x => 0, y => 0, z => 5);
236 my @polygons = $object->get_polygons(mode => 'visible', observer => $observer);
237  
238 =cut
239  
240 11 ▶sub get_polygons {
Subroutine get_polygons: called CC=4 SCAR=13.9
Pod documented
241 11 my ($self, %args) = @_;
242  
243 11 ▶ my $mode = $args{mode} || 'all';
Condition100%2/2
$args{'mode'} || 'all'
l!l
56
MC/DC100%1/1
$args{'mode'} || 'all'
$args{'mode'}
Truth table100%2/2
A
A= $args{'mode'}
Aresult
11
00
244 11 my $observer = $args{observer};
245  
246 11 ▶ unless (grep { $mode eq $_ } qw/all visible/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$mode eq $_;} "all", "visible") 1 10 11
247 1 croak qq{Invalid mode used to get polygons: $mode};
248 }
249  
250 10 my $polygons = $self->_get_polygons;
251  
252 46 my @polygons = map { $_->copy } grep {
253 50 ▶ if ($mode eq 'visible') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq 'visible') { } 15 35 50
254 15 $_->is_plane_visible(observer => $observer);
255 }
256 else {
257 35 1;
258 }
259 10 } @{$polygons};
260  
261 10 return @polygons;
262 }
263  
264 =head2 print
265  
266 Print out text-formatted object data (which might be, for instance, useful for debugging purposes):
267  
268 $object->print(fh => $fh, precision => $precision);
269  
270 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).
271  
272 =cut
273  
274 1 ▶sub print {
Subroutine print: called CC=3 SCAR=11.0
Pod documented
275 1 my ($self, %args) = @_;
276  
277 1 ▶ my $fh = $args{fh} || *STDOUT;
Condition33%1/3
$args{'fh'} || *STDOUT
l!l&&r!l&&!r
100
MC/DC0%0/2
$args{'fh'} || *STDOUT
$args{'fh'} *STDOUT
Truth table33%1/3
A || B
A= $args{'fh'}B= *STDOUT
ABresult
1X1
011
000
278  
279 1 my $stdout = select $fh;
280  
281 1 my $polygons = $self->_get_polygons;
282  
283 1 my $num_faces = $self->num_faces;
284 1 my $num_length = length $num_faces;
285  
286 1 ▶ for (my $i = 0; $i < @{$polygons}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$polygons;}) 5 1 6
287  
288 5 my $polygon = $polygons->[$i];
289  
290 5 printf "\nPolygon %0${num_length}d/%0${num_length}d:", $i + 1, $num_faces;
291  
292 5 $polygon->print(%args);
293 }
294  
295 1 select $stdout;
296  
297 1 return;
298 }
299  
300 =head2
301  
302 Move object a constant distance in a specified direction:
303  
304 my $object_translated = $object->translate(
305 shift_x => -2,
306 shift_y => 1,
307 shift_z => 3,
308 );
309  
310 =cut
311  
312 1 ▶sub translate {
Subroutine translate: called CC=2 SCAR=6.9
Pod undocumented
313 1 my ($self, %args) = @_;
314  
315 1 my $polygons = $self->_get_polygons;
316  
317 1 my @new_polygons;
318  
319 1 for my $polygon (@{$polygons}) {
320  
321 5 push @new_polygons, $polygon->translate(%args);
322 }
323  
324 1 my $object_translated = $self->new(polygons => \@new_polygons);
325  
326 1 return $object_translated;
327 }
328  
329 =head2
330  
331 Enlarge, shrink or stretch object by a scale factor:
332  
333 my $object_scaled = $object->scale(
334 scale_x => 2,
335 scale_y => 2,
336 scale_z => 3,
337 );
338  
339 =cut
340  
341 1 ▶sub scale {
Subroutine scale: called CC=2 SCAR=6.9
Pod undocumented
342 1 my ($self, %args) = @_;
343  
344 1 my $polygons = $self->_get_polygons;
345  
346 1 my @new_polygons;
347  
348 1 for my $polygon (@{$polygons}) {
349  
350 5 push @new_polygons, $polygon->scale(%args);
351 }
352  
353 1 my $object_scaled = $self->new(polygons => \@new_polygons);
354  
355 1 return $object_scaled;
356 }
357  
358 =head2
359  
360 Rotate object by a given angle around three rotation axis:
361  
362 my $object_rotated = $object->rotate(
363 rotate_xy => 30 * ($pi / 180),
364 rotate_yz => -30 * ($pi / 180),
365 rotate_xz => 45 * ($pi / 180),
366 );
367  
368 =cut
369  
370 1 ▶sub rotate {
Subroutine rotate: called CC=2 SCAR=6.9
Pod undocumented
371 1 my ($self, %args) = @_;
372  
373 1 my $polygons = $self->_get_polygons;
374  
375 1 my @new_polygons;
376  
377 1 for my $polygon (@{$polygons}) {
378  
379 5 push @new_polygons, $polygon->rotate(%args);
380 }
381  
382 1 my $object_rotated = $self->new(polygons => \@new_polygons);
383  
384 1 return $object_rotated;
385 }
386  
387 =head2
388  
389 Project object onto a two-dimensional plane using an orthographic projection:
390  
391 my $object2D = $object->cast(type => 'parallel');
392  
393 Project object onto a two-dimensional plane using a perspective projection:
394  
395 my $distance = 5;
396 my $object2D = $object->cast(type => 'perspective', distance => $distance);
397  
398 =cut
399  
400 2 ▶sub cast {
Subroutine cast: called CC=2 SCAR=6.9
Pod undocumented
401 2 my ($self, %args) = @_;
402  
403 2 my $polygons = $self->_get_polygons;
404  
405 2 my @new_polygons;
406  
407 2 for my $polygon (@{$polygons}) {
408  
409 10 push @new_polygons, $polygon->cast(%args);
410 }
411  
412 2 my $object_casted = $self->new(polygons => \@new_polygons);
413  
414 2 return $object_casted;
415 }
416  
417 =head2 compare (==)
418  
419 Compare two objects:
420  
421 my $are_the_same = $object1 == $object2;
422  
423 Overloaded comparison operator evaluates to true whenever two object objects are identical (all their endpoints are located at exactly same positions, note that polygon order matters as well).
424  
425 =cut
426  
427 9 ▶sub _comparison {
Subroutine _comparison: called CC=4 SCAR=13.9
428 9 my ($self, $arg) = @_;
429  
430 9 my $polygons1 = $self->_get_polygons;
431 9 my $polygons2 = $arg->_get_polygons;
432  
433 9 ▶ return unless @{$polygons1} == @{$polygons2};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$polygons1;} == @{$polygons2;} 2 7 9
434  
435 7 ▶ for (my $i = 0; $i < @{$polygons1}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$polygons1;}) 35 7 42
436  
437 35 my $polygon1 = $polygons1->[$i];
438 35 my $polygon2 = $polygons2->[$i];
439  
440 35 ▶ return unless $polygon1 == $polygon2;
Branch: 1 branch
BranchTrueFalseTotal
unless $polygon1 == $polygon2 0 35 35
441 }
442  
443 7 return 1;
444 }
445  
446 2 ▶sub _negative_comparison {
Subroutine _negative_comparison: called CC=1 SCAR=0.0
447 2 my ($self, $arg) = @_;
448  
449 2 return not $self->_comparison($arg);
450 }
451  
452 =head1 BUGS
453  
454 There are no known bugs at the moment. Please report any bugs or feature requests.
455  
456 =head1 EXPORT
457  
458 C<Vector::Object3D> exports nothing neither by default nor explicitly.
459  
460 =head1 SEE ALSO
461  
462 L<Vector::Object3D::Examples>, L<Vector::Object3D::Point>, L<Vector::Object3D::Polygon>.
463  
464 =head1 AUTHOR
465  
466 Pawel Krol, E<lt>pawelkrol@cpan.orgE<gt>.
467  
468 =head1 VERSION
469  
470 Version 0.01 (2012-12-24)
471  
472 =head1 COPYRIGHT AND LICENSE
473  
474 Copyright (C) 2012 by Pawel Krol.
475  
476 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.
477  
478 PLEASE NOTE THAT IT COMES WITHOUT A WARRANTY OF ANY KIND!
479  
480 =cut
481  
482 2 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
483 2 __PACKAGE__->meta->make_immutable;
484  
485 2 1;
« Object3D/Point.pm ↑ Summary Object3D/Polygon.pm »