File Coverage

blib/lib/Vector/Object3D/Line.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::Line;
2              
3             =head1 NAME
4              
5             Vector::Object3D::Line - Three-dimensional line object definitions
6              
7             =head2 SYNOPSIS
8              
9             use Vector::Object3D::Line;
10              
11             # Create two endpoints of a line:
12             my $vertex1 = Vector::Object3D::Point->new(x => 3, y => -2, z => 1);
13             my $vertex2 = Vector::Object3D::Point->new(x => -1, y => 2, z => 3);
14              
15             # Create an instance of a class:
16             my $line = Vector::Object3D::Line->new(vertex1 => $vertex1, vertex2 => $vertex2);
17             my $line = Vector::Object3D::Line->new(vertices => [$vertex1, $vertex2]);
18              
19             # Create a new object as a copy of an existing object:
20             my $copy = $line->copy;
21              
22             # Get first vertex point:
23             my $vertex1 = $line->get_vertex1;
24             # Get last vertex point:
25             my $vertex2 = $line->get_vertex2;
26              
27             # Get both vertex points:
28             my @vertices = $line->get_vertices;
29              
30             # Print out formatted line data:
31             $line->print(fh => $fh, precision => $precision);
32              
33             # Compare two line objects:
34             my $are_the_same = $line1 == $line2;
35              
36             =head1 DESCRIPTION
37              
38             C<Vector::Object3D::Line> provides an abstraction layer for describing line object in a three-dimensional space by composing it from two C<Vector::Object3D::Point> objects (referred onwards as vertices).
39              
40             =head1 METHODS
41              
42             =head2 new
43              
44             Create an instance of a C<Vector::Object3D::Line> class:
45              
46             my $vertex1 = Vector::Object3D::Point->new(x => 3, y => -2, z => 1);
47             my $vertex2 = Vector::Object3D::Point->new(x => -1, y => 2, z => 3);
48              
49             my $line = Vector::Object3D::Line->new(vertex1 => $vertex1, vertex2 => $vertex2);
50             my $line = Vector::Object3D::Line->new(vertices => [$vertex1, $vertex2]);
51              
52             There are two individual means of C<Vector::Object3D::Line> object construction, provided a hash of two vertex components or a list of two point objects. When present, C<vertices> constructor parameter takes precedence over C<vertex1> and C<vertex2> points in case both values are provided at the same time.
53              
54             C<Vector::Object3D::Line> requires provision of two endpoints in order to successfully construct an object instance, there is no exception from this rule.
55              
56             =cut
57              
58             our $VERSION = '0.01';
59              
60 1     1   1409 use strict;
  1         2  
  1         34  
61 1     1   5 use warnings;
  1         2  
  1         24  
62              
63 1     1   473 use Moose;
  0            
  0            
64              
65             use overload
66             '==' => \&_comparison,
67             '!=' => \&_negative_comparison;
68              
69             has 'vertex1' => (
70             is => 'ro',
71             isa => 'Vector::Object3D::Point',
72             reader => 'get_vertex1',
73             required => 1,
74             );
75              
76             has 'vertex2' => (
77             is => 'ro',
78             isa => 'Vector::Object3D::Point',
79             reader => 'get_vertex2',
80             required => 1,
81             );
82              
83             around BUILDARGS => sub {
84             my ($orig, $class, %args) = @_;
85              
86             my $vertices = $args{vertices};
87              
88             if (defined $vertices and ref $vertices eq 'ARRAY') {
89             my @fields = qw(vertex1 vertex2);
90             @args{@fields} = @{$vertices};
91             }
92              
93             my $vertex1 = $args{vertex1};
94             my $vertex2 = $args{vertex2};
95              
96             $args{vertex1} = $vertex1->copy;
97             $args{vertex2} = $vertex2->copy;
98              
99             return $class->$orig(%args);
100             };
101              
102             =head2 copy
103              
104             Create a new C<Vector::Object3D::Line> object as a copy of an existing object:
105              
106             my $copy = $line->copy;
107              
108             =cut
109              
110             sub copy {
111             my ($self) = @_;
112              
113             my @vertices = $self->get_vertices;
114              
115             my $class = $self->meta->name;
116             my $copy = $class->new(vertices => \@vertices);
117              
118             return $copy;
119             }
120              
121             =head2 get_vertex1
122              
123             Get first vertex point:
124              
125             my $vertex1 = $line->get_vertex1;
126              
127             =head2 get_vertex2
128              
129             Get last vertex point:
130              
131             my $vertex2 = $line->get_vertex2;
132              
133             =head2 get_vertices
134              
135             Get both vertex points:
136              
137             my @vertices = $line->get_vertices;
138              
139             =cut
140              
141             sub get_vertices {
142             my ($self) = @_;
143              
144             my $vertex1 = $self->get_vertex1;
145             my $vertex2 = $self->get_vertex2;
146              
147             return ($vertex1, $vertex2);
148             }
149              
150             =head2 print
151              
152             Print out text-formatted line data (which might be, for instance, useful for debugging purposes):
153              
154             $line->print(fh => $fh, precision => $precision);
155              
156             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).
157              
158             =cut
159              
160             sub print {
161             my ($self, %args) = @_;
162              
163             my $vertex1 = $self->get_vertex1;
164             my $vertex2 = $self->get_vertex2;
165              
166             my $vertexMatrix1 = $vertex1->get_matrix;
167             my $vertexMatrix2 = $vertex2->get_matrix;
168              
169             $vertexMatrix1->print(%args);
170             $vertexMatrix2->print(%args);
171              
172             return;
173             }
174              
175             =head2 compare (==)
176              
177             Compare two line objects:
178              
179             my $are_the_same = $line1 == $line2;
180              
181             Overloaded comparison operator evaluates to true whenever two line objects are identical (both their endpoints are located at exactly same positions, note that vertex order matters as well).
182              
183             =cut
184              
185             sub _comparison {
186             my ($self, $arg) = @_;
187              
188             my $line1_point1 = $self->get_vertex1;
189             my $line1_point2 = $self->get_vertex2;
190              
191             my $line2_point1 = $arg->get_vertex1;
192             my $line2_point2 = $arg->get_vertex2;
193              
194             return $line1_point1 == $line2_point1 && $line1_point2 == $line2_point2;
195             }
196              
197             =head2 negative compare (!=)
198              
199             Compare two line objects:
200              
201             my $are_not_the_same = $line1 != $line2;
202              
203             Overloaded negative comparison operator evaluates to true whenever two line objects differ (any of their coordinates do not match).
204              
205             =cut
206              
207             sub _negative_comparison {
208             my ($self, $arg) = @_;
209              
210             return not $self->_comparison($arg);
211             }
212              
213             =head1 BUGS
214              
215             There are no known bugs at the moment. Please report any bugs or feature requests.
216              
217             =head1 EXPORT
218              
219             C<Vector::Object3D::Line> exports nothing neither by default nor explicitly.
220              
221             =head1 SEE ALSO
222              
223             L<Vector::Object3D>, L<Vector::Object3D::Point>.
224              
225             =head1 AUTHOR
226              
227             Pawel Krol, E<lt>pawelkrol@cpan.orgE<gt>.
228              
229             =head1 VERSION
230              
231             Version 0.01 (2012-12-24)
232              
233             =head1 COPYRIGHT AND LICENSE
234              
235             Copyright (C) 2012 by Pawel Krol.
236              
237             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.
238              
239             PLEASE NOTE THAT IT COMES WITHOUT A WARRANTY OF ANY KIND!
240              
241             =cut
242              
243             no Moose;
244             __PACKAGE__->meta->make_immutable;
245              
246             1;