File Coverage

blib/lib/Vector/Object3D/Point/Cast.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package Vector::Object3D::Point::Cast;
2              
3             =head1 NAME
4              
5             Vector::Object3D::Point::Cast - Three-dimensional point object casting into two-dimensional surface areas
6              
7             =head2 SYNOPSIS
8              
9             package Vector::Object3D::Point;
10              
11             use Moose;
12             with 'Vector::Object3D::Point::Cast';
13              
14             # Calling any method from this role requires providing an object of a base class
15             # and results in creation of a new instance of the same class:
16             my $point = Vector::Object3D::Point->new(coord => [-2, 3, 1]);
17              
18             # Project point onto a two-dimensional plane using an orthographic projection:
19             my $point2D = $point->cast(type => 'parallel');
20              
21             # Project point onto a two-dimensional plane using a perspective projection:
22             my $distance = 5;
23             my $point2D = $point->cast(type => 'perspective', distance => $distance);
24              
25             =head1 DESCRIPTION
26              
27             C<Vector::Object3D::Point::Cast> is a Moose role that is meant to be applied to C<Vector::Object3D::Point> class in order to provide it with additional methods of mapping three-dimensional points to a two-dimensional plane.
28              
29             =head1 METHODS
30              
31             =cut
32              
33             our $VERSION = '0.01';
34              
35 1     1   1427 use strict;
  1         2  
  1         63  
36 1     1   6 use warnings;
  1         1  
  1         26  
37              
38 1     1   438 use Moose::Role;
  0            
  0            
39              
40             use Carp qw(croak);
41              
42             use Vector::Object3D::Matrix;
43              
44             =head2 cast
45              
46             Project point onto a two-dimensional plane using an orthographic projection:
47              
48             my $point2D = $point->cast(type => 'parallel');
49              
50             Project point onto a two-dimensional plane using a perspective projection:
51              
52             my $distance = 5;
53             my $point2D = $point->cast(type => 'perspective', distance => $distance);
54              
55             =cut
56              
57             sub cast {
58             my ($self, %args) = @_;
59              
60             my $type = $args{type};
61              
62             if ($type eq 'parallel') {
63             my $point2D = $self->_cast_parallel(%args);
64             return $point2D;
65             }
66             elsif ($type eq 'perspective') {
67             my $point2D = $self->_cast_perspective(%args);
68             return $point2D;
69             }
70             else {
71             croak qq{Invalid projection type: "${type}"};
72             }
73             }
74              
75             sub _cast_parallel {
76             my ($self, %args) = @_;
77              
78             my @xy = $self->get_xy;
79              
80             my $point2D = $self->new(coord => \@xy);
81             return $point2D;
82             }
83              
84             sub _cast_perspective {
85             my ($self, %args) = @_;
86              
87             my $distance = $args{distance};
88              
89             my $x = $self->get_x;
90             my $y = $self->get_y;
91             my $z = $self->get_z || 0.00001; # avoid division by zero exception
92              
93             my $x_casted = $distance * $x / $z;
94             my $y_casted = $distance * $y / $z;
95              
96             my $point2D = $self->new(x => $x_casted, y => $y_casted);
97             return $point2D;
98             }
99              
100             =head1 BUGS
101              
102             There are no known bugs at the moment. Please report any bugs or feature requests.
103              
104             =head1 EXPORT
105              
106             C<Vector::Object3D::Point::Cast> exports nothing neither by default nor explicitly.
107              
108             =head1 SEE ALSO
109              
110             L<Vector::Object3D::Point>.
111              
112             =head1 AUTHOR
113              
114             Pawel Krol, E<lt>pawelkrol@cpan.orgE<gt>.
115              
116             =head1 VERSION
117              
118             Version 0.01 (2012-12-24)
119              
120             =head1 COPYRIGHT AND LICENSE
121              
122             Copyright (C) 2012 by Pawel Krol.
123              
124             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.
125              
126             PLEASE NOTE THAT IT COMES WITHOUT A WARRANTY OF ANY KIND!
127              
128             =cut
129              
130             no Moose::Role;
131              
132             1;