Object3D/Parameters.pm

filter: Statementstmt 100% 51 / 51 Branchbran 100% 4 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 100% 4 / 4 totaltotal 100% 70 / 70 CC 4 SCAR 3.6
CC
4
Cov
100%
set
2.0
get
2.0
get_parameter_values
2.0
CRAP 1.4
« Object3D/Line.pm ↑ Summary Object3D/Point/Transform.pm »
1 package Vector::Object3D::Parameters;
2  
3 =head1 NAME
4  
5 Vector::Object3D::Parameters - additional vector object parameters
6  
7 =head2 SYNOPSIS
8  
9 package Vector::Object3D::Matrix;
10  
11 use Moose;
12 with 'Vector::Object3D::Parameters';
13  
14 # Define list of allowed parameter names with their default values and wrap it by an appropriate method call:
15 sub build_default_parameter_values {
16 return { precision => undef };
17 }
18  
19 # Get currently used parameter value:
20 my $param = 'precision';
21 my $value = $object->get(parameter => $param);
22  
23 # Set new parameter value:
24 my $param = 'precision';
25 my $value = 2;
26 $object->set(parameter => $param, value => $value);
27  
28 # Get complete list of allowed parameter names:
29 my @names = $object->get_parameter_names;
30  
31 # Get complete list of currently used parameter values:
32 my %values = $object->get_parameter_values;
33  
34 =head1 DESCRIPTION
35  
36 C<Vector::Object3D::Parameters> is a Moose role that is meant to be applied to the family of C<Vector::Object3D::> classes in order to provide them with additional methods supporting maintenance of additional object properties. That might for example be a parameter named C<precision>, as used by i.a. C<Vector::Object3D::Matrix> and C<Vector::Object3D::Point> classes. Understanding the importance of this parameter is illustrated in the following example:
37  
38 use Vector::Object3D::Matrix;
39  
40 # Create two almost identical matrix objects:
41 my $matrix1 = Vector::Object3D::Matrix->new(rows => [[-2, 2], [2.018, 1]]);
42 my $matrix2 = Vector::Object3D::Matrix->new(rows => [[-2, 2], [2.021, 1]]);
43  
44 # Set new precision values for both objects that will not be sufficient to consider them equal:
45 $matrix1->set(parameter => 'precision', value => 3);
46 $matrix2->set(parameter => 'precision', value => 3);
47  
48 # Comparing two almost identical matrix objects with too high precision yields false:
49 my $are_the_same = $matrix1 == $matrix2; # false
50  
51 # Set new precision values for both objects that will be good enough to have a match:
52 $matrix1->set(parameter => 'precision', value => 2);
53 $matrix2->set(parameter => 'precision', value => 2);
54  
55 # Comparing two almost identical matrix objects with accurate precision yields true:
56 my $are_the_same = $matrix1 == $matrix2; # true
57  
58 This becomes especially important when comparing different transformation matrices, which might not appear to be equal due to divisions that produce results with theoretically infinite number of decimal places, just like in this example:
59  
60 $ perl -e 'print 2/3'
61 0.666666666666667
62  
63 Since calculations of input values that feed transformation matrix data during their construction involve producing such numbers, you should always consider setting up an accurate precision value if you plan to compare them later with each other.
64  
65 The exactly same reasoning could be applied to C<Vector::Object3D::Point> objects as well.
66  
67 =head1 METHODS
68  
69 =cut
70  
71 8 our $VERSION = '0.01';
72  
73 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
75  
76 8 ▶use Storable 'dclone';
Subroutine BEGIN: called CC=1 SCAR=0.0
77  
78 8 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
79  
80 8 has _allowed_parameters => (
81 builder => '_build_allowed_parameters',
82 init_arg => undef,
83 is => 'ro',
84 isa => 'ArrayRef[Str]',
85 reader => '_get_allowed_parameters',
86 );
87  
88 8 has _parameters => (
89 builder => 'build_default_parameter_values',
90 is => 'rw',
91 isa => 'HashRef[Maybe[Int]]',
92 accessor => '_parameters',
93 );
94  
95 6755 ▶sub _build_allowed_parameters {
Subroutine _build_allowed_parameters: called CC=1 SCAR=0.0
96 6755 my ($self) = @_;
97  
98 6755 my $parameter_values = $self->build_default_parameter_values();
99  
100 6755 my @allowed_parameters = keys %{$parameter_values};
101  
102 6755 return \@allowed_parameters;
103 }
104  
105 =head2 set
106  
107 Set new parameter value:
108  
109 my $param = 'precision';
110 my $value = 2;
111 $object->set(parameter => $param, value => $value);
112  
113 =cut
114  
115 5244 ▶sub set {
Subroutine set: called CC=2 SCAR=6.9
Pod documented
116 5244 my ($self, %args) = @_;
117  
118 5244 my $parameter = $args{parameter};
119 5244 my $value = $args{value};
120  
121 5244 ▶ die qq{Unable to set unrecognized parameter value: "${parameter}"} unless $self->_is_allowed_parameter($parameter);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_allowed_parameter($parameter) 2 5242 5244
122  
123 5242 my $parameters = dclone $self->_parameters;
124 5242 $parameters->{$parameter} = $value;
125 5242 $self->_parameters($parameters);
126  
127 5240 return $value;
128 }
129  
130 =head2 get
131  
132 Get currently used parameter value:
133  
134 my $param = 'precision';
135 my $value = $object->get(parameter => $param);
136  
137 =cut
138  
139 5644 ▶sub get {
Subroutine get: called CC=2 SCAR=6.9
Pod documented
140 5644 my ($self, %args) = @_;
141  
142 5644 my $parameter = $args{parameter};
143  
144 5644 ▶ die qq{Unable to get unrecognized parameter value: "${parameter}"} unless $self->_is_allowed_parameter($parameter);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_allowed_parameter($parameter) 1 5643 5644
145  
146 5643 return $self->_parameters->{$parameter};
147 }
148  
149 10888 ▶sub _is_allowed_parameter {
Subroutine _is_allowed_parameter: called CC=1 SCAR=0.0
150 10888 my ($self, $name) = @_;
151  
152 10888 my $allowed_parameters = $self->_get_allowed_parameters;
153  
154 10888 return grep { $name eq $_ } @{$allowed_parameters};
155 }
156  
157 =head2 get_parameter_names
158  
159 Get complete list of allowed parameter names:
160  
161 my @names = $object->get_parameter_names;
162  
163 =cut
164  
165 5213 ▶sub get_parameter_names {
Subroutine get_parameter_names: called CC=1 SCAR=0.0
Pod documented
166 5213 my ($self) = @_;
167  
168 5213 my $parameter_names = $self->_get_allowed_parameters;
169  
170 5213 return @{$parameter_names};
171 }
172  
173 =head2 get_parameter_values
174  
175 Get complete list of currently used parameter values:
176  
177 my %values = $object->get_parameter_values;
178  
179 =cut
180  
181 5213 ▶sub get_parameter_values {
Subroutine get_parameter_values: called CC=2 SCAR=6.9
Pod documented
182 5213 my ($self) = @_;
183  
184 5213 my @parameter_names = $self->get_parameter_names;
185  
186 5213 my %parameter_values;
187  
188 5213 for my $name (@parameter_names) {
189 5213 $parameter_values{$name} = $self->get(parameter => $name);
190 }
191  
192 5213 return %parameter_values;
193 }
194  
195 =head1 BUGS
196  
197 There are no known bugs at the moment. Please report any bugs or feature requests.
198  
199 =head1 EXPORT
200  
201 C<Vector::Object3D::Parameters> exports nothing neither by default nor explicitly.
202  
203 =head1 SEE ALSO
204  
205 L<Vector::Object3D::Matrix>, L<Vector::Object3D::Point>.
206  
207 =head1 AUTHOR
208  
209 Pawel Krol, E<lt>pawelkrol@cpan.orgE<gt>.
210  
211 =head1 VERSION
212  
213 Version 0.01 (2012-12-24)
214  
215 =head1 COPYRIGHT AND LICENSE
216  
217 Copyright (C) 2012 by Pawel Krol.
218  
219 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.
220  
221 PLEASE NOTE THAT IT COMES WITHOUT A WARRANTY OF ANY KIND!
222  
223 =cut
224  
225 8 ▶no Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
226  
227 8 1;
« Object3D/Line.pm ↑ Summary Object3D/Point/Transform.pm »