blib/lib/Math/Project3D/Plot.pm

filter: Statementstmt 99.1% 116 / 117 Branchbran 76.4% 26 / 34 Conditioncond 42.8% 3 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 94.7% 18 / 19 Podpod 100% 5 / 5 totaltotal 90.3% 168 / 186 CC 23 SCAR 13.2
CC
23
Cov
92%
new
7.4
plot_list
6.0
plot_range
5.0
CRAP 3.8
↑ Summary
1
2 # See the POD documentation at the end of this
3 # document for detailed copyright information.
4 # (c) 2002-2006 Steffen Mueller, all rights reserved.
5
6 package Math::Project3D::Plot;
7
8 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11
12 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13
14 2 ▶use Math::Project3D;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Imager;
Subroutine BEGIN: called CC=1 SCAR=0.0
16
17 2 ▶use vars qw/$VERSION/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 $VERSION = '1.02';
19
20
21 # Constructor class and object method new
22 #
23 # Creates a new Math::Project3D::Plot instance and returns it.
24 # Takes a list of object attributes as arguments.
25
26 1 ▶sub new {
Subroutine new: called CC=7 SCAR=20.0
Pod documented
27 1 my $proto = shift;
28 1 ▶ my $class = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
29
30 1 my %args = @_;
31
32 # check for require attributes
33 1 my $missing = _require_attributes(\%args, 'image', 'projection');
34
35 1 ▶ croak "Required attribute $missing missing."
Branch: 1 branch
BranchTrueFalseTotal
if $missing 0 1 1
36 if $missing;
37
38 # We might croak a lot.
39 0 ▶ my $croaker = sub { croak "Attribute '$_[0]' is bad." };
Subroutine __ANON__: not called CC=1 SCAR=1.2
40
41 1 my $self = {};
42
43 # valid image and projection?
44 1 ▶ ref $args{image} or $croaker->('image');
Branch: 1 branch
BranchTrueFalseTotal
ref $args{"image"} or &$croaker("image") 1 0 1
45 1 $self->{image} = $args{image};
46
47 1 ▶ ref $args{projection} eq 'Math::Project3D' or $croaker->('projection');
Branch: 1 branch
BranchTrueFalseTotal
ref $args{"projection"} eq "Math::Project3D" or &$croaker("projection") 1 0 1
48 1 $self->{proj} = $args{projection};
49
50 # defaults
51 $self = {
52 %$self,
53 scale => 10,
54 origin_x => $self->{image}->getwidth() / 2,
55 1 origin_y => $self->{image}->getheight() / 2,
56 };
57
58 1 my @valid_args = qw(
59 origin_x origin_y
60 scale
61 );
62
63 # Take all valid args from the user input and
64 # put them into our object.
65 1 foreach my $arg (@valid_args) {
66 3 ▶ $self->{$arg} = $args{$arg} if exists $args{$arg};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{$arg} 1 2 3
67 }
68
69 1 bless $self => $class;
70
71 # get min/max logical x/y coordinates
72 1 ( $self->{min_x}, $self->{min_y} ) = $self->_g_l(0, 0);
73 ( $self->{max_x}, $self->{max_y} ) = $self->_g_l(
74 $self->{image}->getwidth(),
75 1 $self->{image}->getheight(),
76 );
77
78 1 return $self;
79 }
80
81
82 # Method plot
83 # Takes argument pairs: color => Imager color
84 # and params => array ref of params
85 # projects the point associated with the parameters.
86 # Plots the point.
87 # Returns the graphical coordinates of the point that
88 # was plotted.
89
90 33 ▶sub plot {
Subroutine plot: called CC=2 SCAR=7.0
Pod documented
91 33 my $self = shift;
92 33 my %args = @_;
93
94 33 ▶ ref $args{params} eq 'ARRAY' or
Branch: 1 branch
BranchTrueFalseTotal
ref $args{"params"} eq "ARRAY" or croak("Invalid parameters passed to plot().") 33 0 33
95 croak "Invalid parameters passed to plot().";
96
97 33 my ($coeff1, $coeff2, $distance) = $self->{proj}->project(@{$args{params}});
98 33 my ($g_x, $g_y) = $self->_l_g($coeff1, $coeff2);
99
100 33 $self->{image}->setpixel(color=>$args{color}, x=>$g_x, y=>$g_y);
101
102 33 return $g_x, $g_y;
103 }
104
105
106 # Method plot_list
107 # Takes argument pairs: color => Imager color,
108 # params => array ref of array ref of params
109 # and type => 'line' or 'points'
110 # Projects the points associated with the parameters.
111 # Plots the either points or the line connecting them.
112 # Returns 1.
113
114 5 ▶sub plot_list {
Subroutine plot_list: called CC=6 SCAR=17.9
Pod documented
115 5 my $self = shift;
116 5 my %args = @_;
117
118 5 ▶ ref $args{params} eq 'ARRAY' or
Branch: 1 branch
BranchTrueFalseTotal
ref $args{"params"} eq "ARRAY" or croak("Invalid parameters passed to plot_list().") 5 0 5
119 croak "Invalid parameters passed to plot_list().";
120
121 # Get type, default to points
122 5 my $type = $args{type};
123 5 ▶ $type ||= 'points';
Condition50%1/2
$type ||= "points"
l!l
50
MC/DC0%0/1
$type ||= "points"
$type
Truth table50%1/2
A
A= $type
Aresult
11
00
124
125 # Do some calulation on the points.
126 5 my $matrix = $self->{proj}->project_list( @{ $args{params} } );
127
128 # Cache
129 5 my ($prev_g_x, $prev_g_y);
130
131 # For every point...
132 5 ▶ for ( my $row = 1; $row <= @{$args{params}}; $row++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row <= @{$args{"params"};}) 26 5 31
133
134 # Get its coordinates
135 26 my ($g_x, $g_y) = $self->_l_g(
136 $matrix->element($row,1),
137 $matrix->element($row,2)
138 );
139
140 # Plot line or points?
141 26 ▶ if ( $type eq 'line' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'line') { } 16 10 26
142
143 $self->{image}->line(
144 color => $args{color},
145 16 ▶ x1 => $prev_g_x, y1 => $prev_g_y,
Branch: 1 branch
BranchTrueFalseTotal
if defined $prev_g_x 12 4 16
146 x2 => $g_x, y2 => $g_y,
147 ) if defined $prev_g_x;
148
149 16 ($prev_g_x, $prev_g_y) = ($g_x, $g_y);
150
151 } else {
152 10 $self->{image}->setpixel(color=>$args{color}, x=>$g_x, y=>$g_y);
153 }
154 }
155
156 5 return 1;
157 }
158
159
160 # Method plot_range
161 # Takes argument pairs: color => Imager color,
162 # params => array ref of array ref of ranges
163 # and type => 'line' or 'points'
164 # Projects the points associated with the parameter ranges.
165 # Plots the either points or the line connecting them.
166 # Returns 1.
167
168 3 ▶sub plot_range {
Subroutine plot_range: called CC=5 SCAR=16.1
Pod documented
169 3 my $self = shift;
170 3 my %args = @_;
171
172 3 ▶ ref $args{params} eq 'ARRAY' or
Branch: 1 branch
BranchTrueFalseTotal
ref $args{"params"} eq "ARRAY" or croak("Invalid parameters passed to plot_range().") 3 0 3
173 croak "Invalid parameters passed to plot_range().";
174
175 # Get type, default to points
176 3 my $type = $args{type};
177 3 ▶ $type ||= 'points';
Condition50%1/2
$type ||= "points"
l!l
30
MC/DC0%0/1
$type ||= "points"
$type
Truth table50%1/2
A
A= $type
Aresult
11
00
178
179 # Cache
180 3 my ($prev_g_x, $prev_g_y);
181
182 # This will hold the callback routine
183 3 my $callback;
184
185 # Use different callbacks for different drawing types
186 3 ▶ if ($type eq 'line') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'line') { } 1 2 3
elsif ($type eq 'multiline') { } 1 1 2
187 121 ▶ $callback = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
188 # Get its coordinates
189 121 my ($g_x, $g_y) = $self->_l_g( @_[0,1] );
190
191 # Draw the line
192 $self->{image}->line(
193 color => $args{color},
194 121 ▶ x1 => $prev_g_x, y1 => $prev_g_y,
Branch: 1 branch
BranchTrueFalseTotal
if defined $prev_g_x 120 1 121
195 x2 => $g_x, y2 => $g_y,
196 ) if defined $prev_g_x;
197
198 # cache
199 121 ($prev_g_x, $prev_g_y) = ($g_x, $g_y);
200 1 };
201 } elsif ($type eq 'multiline') {
202 121 ▶ $callback = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
203 121 my $newline = $_[3]; # Did we start a new line?
204
205 # Get its coordinates
206 121 my ($g_x, $g_y) = $self->_l_g( @_[0,1] );
207
208 # Draw the line if not a new line:
209 $self->{image}->line(
210 color => $args{color},
211 121 ▶ x1 => $prev_g_x, y1 => $prev_g_y,
Branch: 1 branch
BranchTrueFalseTotal
if defined $prev_g_x 110 11 121
212 x2 => $g_x, y2 => $g_y,
213 ) if defined $prev_g_x;
214
215 # cache
216 121 ($prev_g_x, $prev_g_y) = ($g_x, $g_y);
217 121 ▶ ($prev_g_x, $prev_g_y) = (undef, undef) if $newline;
Branch: 1 branch
BranchTrueFalseTotal
if $newline 11 110 121
218 1 };
219 } else {
220 121 ▶ $callback = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
221 # Get its coordinates
222 121 my ($g_x, $g_y) = $self->_l_g( @_[0,1] );
223
224 # draw the point
225 121 $self->{image}->setpixel(color=>$args{color}, x=>$g_x, y=>$g_y);
226 1 };
227 }
228
229 # Start the projection
230 $self->{proj}->project_range_callback(
231 $callback,
232 3 @{ $args{params} },
233 );
234
235 3 return 1;
236 }
237
238
239 # Private method _require_attributes
240 #
241 # Arguments must be a list of attribute names (strings).
242 # Tests for the existance of those attributes.
243 # Returns the missing attribute on failure, undef on success.
244
245 1 ▶sub _require_attributes {
Subroutine _require_attributes: called CC=3 SCAR=11.2
246 1 my $self = shift;
247 1 ▶ exists $self->{$_} or return $_ foreach @_;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{$_} or return $_ 2 0 2
248 1 return undef;
249 }
250
251
252 # Private method _l_g (logical to graphical)
253 # Takes an x/y pair of logical coordinates as
254 # argument and returns the corresponding graphical
255 # coordinates.
256
257 422 ▶sub _l_g {
Subroutine _l_g: called CC=1 SCAR=0.0
258 422 my $self = shift;
259 422 my $x = shift;
260 422 my $y = shift;
261
262 # A logical unit is a graphical one displaced by the origin
263 # and multiplied with the appropriate scaling factor.
264
265 422 $x = $self->{origin_x} + $x * $self->{scale};
266
267 422 $y = $self->{origin_y} - $y * $self->{scale};
268
269 422 return $x, $y;
270 }
271
272
273 # Private method _g_l (graphical to logical)
274 # Takes an x/y pair of graphical coordinates as
275 # argument and returns the corresponding
276 # logical coordinates.
277
278 2 ▶sub _g_l {
Subroutine _g_l: called CC=1 SCAR=0.0
279 2 my $self = shift;
280 2 my $x = shift;
281 2 my $y = shift;
282
283 # A graphical unit is a logical one displaced by the origin
284 # and divided by the appropriate scaling factor.
285
286 2 $x = ( $x - $self->{origin_x} ) / $self->{scale};
287
288 2 $y = ( $y - $self->{origin_y} ) / $self->{scale};
289
290 2 return $x, $y;
291 }
292
293
294 # Method plot_axis
295 #
296 # The plot_axis method draws an axis into the image. "Axis" used
297 # as in "a line that goes through the origin". Required arguments:
298 # color => Imager color to use (see Imager::Color manpage)
299 # vector => Array ref containing three vector components.
300 # (only the direction matters as the vector will
301 # be normalized by plot_axis.)
302 # length => Desired axis length.
303
304 3 ▶sub plot_axis {
Subroutine plot_axis: called CC=2 SCAR=6.9
Pod documented
305 3 my $self = shift;
306 3 my %args = @_;
307
308 3 ▶ ref $args{vector} eq 'ARRAY' or
Branch: 1 branch
BranchTrueFalseTotal
ref $args{"vector"} eq "ARRAY" or croak("Invalid vector passed to plot_axis().") 3 0 3
309 croak "Invalid vector passed to plot_axis().";
310
311 # Save original function
312 3 my $old_function = $self->{proj}->get_function();
313
314 # Directional vector of the axis
315 3 my @vector = @{ $args{vector} };
316
317 # Create new function along the axis' directional vector
318 # using only one parameter t that will be determined
319 # below
320 $self->{proj}->new_function(
321 3 't', "$vector[0]*\$t", "$vector[1]*\$t", "$vector[2]*\$t",
322 );
323
324 # Calculate the length of the unit vector
325 3 my $vector_length = sqrt( $vector[0]**2 + $vector[1]**2 + $vector[2]**2 );
326
327 # Calculate $t, the number of units needed to get
328 # a line of the correct length.
329 3 my $t = $args{length} / ( 2 * $vector_length );
330
331 # Use the plot_list method to display the axis.
332 $self->plot_list(
333 color => $args{color},
334 3 type => 'line',
335 params => [
336 [-$t], # We calculated for $t for length/2, hence
337 [$t], # we may now draw from -$t to +$t
338 ],
339 );
340
341 # Restore original function
342 3 $self->{proj}->set_function($old_function);
343
344 3 return 1;
345 }
346
347
348 2 1;
349
350 __END__
↑ Summary