Object3D/Line.pm

filter: Statementstmt 100% 57 / 57 Branchbran 100% 2 / 2 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 11 / 11 Podpod 100% 3 / 3 totaltotal 95.1% 79 / 83 CC 4 SCAR 4.5
CC
4
Cov
97%
__ANON__
3.0
_comparison
2.0
BEGIN
1.0
CRAP 1.6
« Object3D/Point/Cast.pm ↑ Summary Object3D/Parameters.pm »
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 3 our $VERSION = '0.01';
59  
60 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 use overload
66 3 ▶ '==' => \&_comparison,
Subroutine BEGIN: called CC=1 SCAR=0.0
67 3 '!=' => \&_negative_comparison;
68  
69 3 has 'vertex1' => (
70 is => 'ro',
71 isa => 'Vector::Object3D::Point',
72 reader => 'get_vertex1',
73 required => 1,
74 );
75  
76 3 has 'vertex2' => (
77 is => 'ro',
78 isa => 'Vector::Object3D::Point',
79 reader => 'get_vertex2',
80 required => 1,
81 );
82  
83 37 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
84 37 my ($orig, $class, %args) = @_;
85  
86 37 my $vertices = $args{vertices};
87  
88 37 ▶ if (defined $vertices and ref $vertices eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $vertices and ref $vertices eq "ARRAY") 9 28 37
Condition66%2/3
defined $vertices and ref $vertices eq "ARRAY"
!ll&&!rl&&r
2809
MC/DC50%1/2
defined $vertices and ref $vertices eq "ARRAY"
defined $vertices ref $vertices eq "ARRAY"
Truth table66%2/3
A and B
A= defined $verticesB= ref $vertices eq "ARRAY"
ABresult
0X0
100
111
89 9 my @fields = qw(vertex1 vertex2);
90 9 @args{@fields} = @{$vertices};
91 }
92  
93 37 my $vertex1 = $args{vertex1};
94 37 my $vertex2 = $args{vertex2};
95  
96 37 $args{vertex1} = $vertex1->copy;
97 36 $args{vertex2} = $vertex2->copy;
98  
99 34 return $class->$orig(%args);
100 3 };
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 2 ▶sub copy {
Subroutine copy: called CC=1 SCAR=0.0
Pod documented
111 2 my ($self) = @_;
112  
113 2 my @vertices = $self->get_vertices;
114  
115 2 my $class = $self->meta->name;
116 2 my $copy = $class->new(vertices => \@vertices);
117  
118 2 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 5 ▶sub get_vertices {
Subroutine get_vertices: called CC=1 SCAR=0.0
Pod documented
142 5 my ($self) = @_;
143  
144 5 my $vertex1 = $self->get_vertex1;
145 5 my $vertex2 = $self->get_vertex2;
146  
147 5 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 3 ▶sub print {
Subroutine print: called CC=1 SCAR=0.0
Pod documented
161 3 my ($self, %args) = @_;
162  
163 3 my $vertex1 = $self->get_vertex1;
164 3 my $vertex2 = $self->get_vertex2;
165  
166 3 my $vertexMatrix1 = $vertex1->get_matrix;
167 3 my $vertexMatrix2 = $vertex2->get_matrix;
168  
169 3 $vertexMatrix1->print(%args);
170 3 $vertexMatrix2->print(%args);
171  
172 3 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 8 ▶sub _comparison {
Subroutine _comparison: called CC=2 SCAR=7.0
186 8 my ($self, $arg) = @_;
187  
188 8 my $line1_point1 = $self->get_vertex1;
189 8 my $line1_point2 = $self->get_vertex2;
190  
191 8 my $line2_point1 = $arg->get_vertex1;
192 8 my $line2_point2 = $arg->get_vertex2;
193  
194 8 ▶ return $line1_point1 == $line2_point1 && $line1_point2 == $line2_point2;
Condition66%2/3
$line1_point1 == $line2_point1 && $line1_point2 == $line2_point2
!ll&&!rl&&r
035
MC/DC50%1/2
$line1_point1 == $line2_point1 && $line1_point2 == $line2_point2
$line1_point1 == $line2_point1 $line1_point2 == $line2_point2
Truth table66%2/3
A && B
A= $line1_point1 == $line2_point1B= $line1_point2 == $line2_point2
ABresult
0X0
100
111
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 3 ▶sub _negative_comparison {
Subroutine _negative_comparison: called CC=1 SCAR=0.0
208 3 my ($self, $arg) = @_;
209  
210 3 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 3 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
244 3 __PACKAGE__->meta->make_immutable;
245  
246 3 1;
« Object3D/Point/Cast.pm ↑ Summary Object3D/Parameters.pm »