Graph/Xrt3d.pm

filter: Statementstmt 23.0% 42 / 182 Branchbran 2.1% 2 / 94 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 1 totaltotal 16.8% 51 / 302 CC 53 SCAR 76.8
CC
53
Cov
15%
xrt3d
2401.7
BEGIN
1.0
BEGIN
1.0
CRAP 2157.5
« Graph/Xrt2d.pm ↑ Summary Graph/Gnuplot.pm »
1 ## Xrt3d.pm is a sub-module of Graph.pm. It has all the subroutines
2 ## needed for the Xrt3d part of the package.
3 ##
4 ## $Id: Xrt3d.pm,v 1.30 2006/06/07 21:09:33 emile Exp $ $Name: $
5 ##
6 ## This software product is developed by Michael Young and David Moore,
7 ## and copyrighted(C) 1998 by the University of California, San Diego
8 ## (UCSD), with all rights reserved. UCSD administers the CAIDA grant,
9 ## NCR-9711092, under which part of this code was developed.
10 ##
11 ## There is no charge for this software. You can redistribute it and/or
12 ## modify it under the terms of the GNU General Public License, v. 2 dated
13 ## June 1991 which is incorporated by reference herein. This software is
14 ## distributed WITHOUT ANY WARRANTY, IMPLIED OR EXPRESS, OF MERCHANTABILITY
15 ## OR FITNESS FOR A PARTICULAR PURPOSE or that the use of it will not
16 ## infringe on any third party's intellectual property rights.
17 ##
18 ## You should have received a copy of the GNU GPL along with this program.
19 ##
20 ##
21 ## IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY
22 ## PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
23 ## DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS
24 ## SOFTWARE, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF
25 ## THE POSSIBILITY OF SUCH DAMAGE.
26 ##
27 ## THE SOFTWARE PROVIDED HEREIN IS ON AN "AS IS" BASIS, AND THE
28 ## UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE,
29 ## SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. THE UNIVERSITY
30 ## OF CALIFORNIA MAKES NO REPRESENTATIONS AND EXTENDS NO WARRANTIES
31 ## OF ANY KIND, EITHER IMPLIED OR EXPRESS, INCLUDING, BUT NOT LIMITED
32 ## TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A
33 ## PARTICULAR PURPOSE, OR THAT THE USE OF THE SOFTWARE WILL NOT INFRINGE
34 ## ANY PATENT, TRADEMARK OR OTHER RIGHTS.
35 ##
36 ##
37 ## Contact: graph-dev@caida.org
38 ##
39 ##
40 package Chart::Graph::Xrt3d;
41 4 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 4 @ISA = qw(Exporter);
44 4 @EXPORT = qw();
45 4 @EXPORT_OK = qw(&xrt3d);
46  
47 4 ▶use FileHandle; # to create generic filehandles
Subroutine BEGIN: called CC=1 SCAR=0.0
48 4 ▶use Carp; # for carp() and croak()
Subroutine BEGIN: called CC=1 SCAR=0.0
49 4 ▶use Chart::Graph::Utils qw(:UTILS); # get global subs and variables
Subroutine BEGIN: called CC=1 SCAR=0.0
50 4 ▶use Chart::Graph::XrtUtils qw(:UTILS);
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 4 $cvs_Id = '$Id: Xrt3d.pm,v 1.30 2006/06/07 21:09:33 emile Exp $';
53 4 $cvs_Author = '$Author: emile $';
54 4 $cvs_Name = '$Name: $';
55 4 $cvs_Revision = '$Revision: 1.30 $';
56  
57 4 $VERSION = 3.2;
58  
59 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 #
62 # xrt graphing package
63 #
64  
65  
66 4 my %def_xrt_global_opts; # xrt specific globals
67  
68 4 %def_xrt_global_opts = (
69 "output file" => "untitled-xrt3d.gif",
70 "output type" => "gif",
71 "x-axis title" => "x-axis",
72 "y-axis title" => "y-axis",
73 "z-axis title" => "z-axis",
74 "x-min" => "0",
75 "y-min" => "0",
76 "x-step" => "1",
77 "y-step" => "1",
78 "x-ticks" => undef,
79 "y-ticks" => undef,
80 "header" => ["header"],
81 "footer" => ["footer"],
82 );
83 #
84 #
85 # Subroutine: xrt()
86 #
87 # Description: this is the main function you will be calling from
88 # our scripts. please see
89 # www.caida.org/Tools/Graph/ for a full description
90 # and how-to of this subroutine
91 #
92  
93 4 ▶sub xrt3d {
Subroutine xrt3d: called CC=53 SCAR=77.8
Pod undocumented
94 4 my $user_global_opts_ref = shift;
95 4 my $data_set_ref = shift;
96 4 my $matrix_data_ref;
97 my $data_filename;
98 4 my (%global_opts);
99
100 # variables to be written to the command file
101 4 my ($plot_file, $x_axis, $y_axis, $z_axis, $x_step, $y_step);
102 4 my ($x_min, $y_min, $x_ticks, $y_ticks, $header, $footer);
103 4 my ($x_cnt, $y_cnt, $hdr_cnt, $ftr_cnt);
104 4 my ($output_file);
105  
106 4 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 4 4
107 0 carp 'Too many arguments. Usage: xrt3d(\%options, \@data_set)';
108 0 return 0;
109 }
110  
111 4 _make_tmpdir("_Xrt3d_");
112  
113 # set paths for external programs
114 4 ▶ if (not _set_xrtpaths("xrt3d")) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_set_xrtpaths("xrt3d")) 4 0 4
115 4 _cleanup_tmpdir();
116 4 return 0;
117 }
118
119 # check first arg for hash
120 0 ▶ if (ref($user_global_opts_ref) ne "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $user_global_opts_ref ne "HASH") 0 0 0
121 0 carp "Global options must be a hash.";
122 0 _cleanup_tmpdir();
123 0 return 0;
124 }
125  
126 # call to combine user options with default options
127 0 %global_opts = _mesh_opts($user_global_opts_ref, \%def_xrt_global_opts);
128
129 # check for values in command file
130 0 ▶ while (my ($key, $value) = each %global_opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %global_opts) 0 0 0
131
132 0 ▶ if ($key eq "output file") {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "output file") 0 0 0
133 0 $output_file = $value;
134 0 ▶ unless (defined $global_opts{"output type"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $global_opts{"output type"}) 0 0 0
135 0 carp "Must have an output type defined";
136 0 _cleanup_tmpdir();
137 0 return 0;
138 }
139 }
140
141 # If the file is PostScript ... what XRT makes is PostScript
142 0 ▶ if ($global_opts{"output type"} eq "ps") {
Branch: 2 branches
BranchTrueFalseTotal
if ($global_opts{'output type'} eq 'ps') { } 0 0 0
elsif ($global_opts{'output type'} eq 'gif' or $global_opts{'output type'} eq 'xwd' or $global_opts{'output type'} eq 'png' or $global_opts{'output type'} eq 'jpg') { } 0 0 0
143 0 $plot_file = _make_tmpfile("plot", "ps");
144 }
145 # For all raster formats XRT starts out with
146 # X-Windows XWD format.
147 ▶ elsif (($global_opts{"output type"} eq "gif") or
Condition0%0/9
$global_opts{"output type"} eq "gif" or $global_opts{"output type"} eq "xwd" or $global_opts{"output type"} eq "png" or $global_opts{"output type"} eq "jpg"
l!l&&r!l&&!r
000
$global_opts{"output type"} eq "gif" or $global_opts{"output type"} eq "xwd" or $global_opts{"output type"} eq "png"
l!l&&r!l&&!r
000
$global_opts{"output type"} eq "gif" or $global_opts{"output type"} eq "xwd"
l!l&&r!l&&!r
000
MC/DC0%0/4
$global_opts{"output type"} eq "gif" or $global_opts{"output type"} eq "xwd" or $global_opts{"output type"} eq "png" or $global_opts{"output type"} eq "jpg"
$global_opts{"output type"} eq "gif" $global_opts{"output type"} eq "xwd" $global_opts{"output type"} eq "png" $global_opts{"output type"} eq "jpg"
Truth table0%0/5
A or B or C or D
A= $global_opts{"output type"} eq "gif"B= $global_opts{"output type"} eq "xwd"C= $global_opts{"output type"} eq "png"D= $global_opts{"output type"} eq "jpg"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
148 ($global_opts{"output type"} eq "xwd") or
149 ($global_opts{"output type"} eq "png") or
150 ($global_opts{"output type"} eq "jpg")
151 ) {
152 0 $plot_file = _make_tmpfile("plot", "xwd");
153 } else {
154 # Default is XWD
155 0 carp "Unknown output type, defaulting to xwd";
156 0 $plot_file = _make_tmpfile("plot", "xwd");
157 }
158  
159  
160 0 ▶ if ($key eq "x-axis title") {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "x-axis title") 0 0 0
161 0 ▶ if(defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
162 0 $x_axis = $value;
163 }
164 }
165
166 0 ▶ if ($key eq "y-axis title") {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "y-axis title") 0 0 0
167 0 ▶ if(defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
168 0 $y_axis = $value;
169 }
170 }
171
172 0 ▶ if ($key eq "z-axis title") {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "z-axis title") 0 0 0
173 0 ▶ if(defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
174 0 $z_axis = $value;
175 }
176 }
177
178 0 ▶ if ($key eq "x-min") {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "x-min") 0 0 0
179 0 ▶ if(defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
180 0 $x_min = $value;
181 }
182 }
183
184 0 ▶ if ($key eq "y-min") {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "y-min") 0 0 0
185 0 ▶ if(defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
186 0 $y_min = $value;
187 }
188 }
189
190 0 ▶ if ($key eq "x-step") {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "x-step") 0 0 0
191 0 ▶ if(defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
192 0 $x_step = $value;
193 }
194 }
195
196 0 ▶ if ($key eq "y-step") {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "y-step") 0 0 0
197 0 ▶ if(defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
198 0 $y_step = $value;
199 }
200 }
201
202 0 ▶ if ($key eq "x-ticks") {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "x-ticks") 0 0 0
203 0 ▶ if(defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
204 0 $x_ticks = $value;
205 }
206 }
207
208 0 ▶ if ($key eq "y-ticks") {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "y-ticks") 0 0 0
209 0 ▶ if(defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
210 0 $y_ticks = $value;
211 }
212 }
213
214 0 ▶ if ($key eq "header") {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "header") 0 0 0
215 0 ▶ if(defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
216 0 $header = $value;
217 }
218 }
219
220 0 ▶ if ($key eq "footer") {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "footer") 0 0 0
221 0 ▶ if(defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
222 0 $footer = $value;
223 }
224 }
225 }
226
227  
228 # Extract options for data.
229 0 my $data_opts = shift @{$data_set_ref};
230 0 ▶ while (my ($key, $value) = each %{$data_opts}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$data_opts;}) 0 0 0
231 0 ▶ if ($key eq "type") {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "type") 0 0 0
232 0 ▶ if ($value eq "matrix") {
Branch: 2 branches
BranchTrueFalseTotal
if ($value eq 'matrix') { } 0 0 0
elsif ($value eq 'file') { } 0 0 0
233 0 $matrix_data_ref = $data_set_ref;
234 }
235 elsif ($value eq "file") {
236 0 $data_filename = pop @{$data_set_ref};
237 } else {
238 0 carp "Unsupported or unknown format for data";
239 }
240 }
241 }
242
243 # because xrt allows multiline headers
244 # get the length of the header array
245 # each line of the header is one index
246 # in the array
247  
248 0 $hdr_cnt = $#{$global_opts{"header"}} + 1;
249 0 $ftr_cnt = $#{$global_opts{"footer"}} + 1;
250
251  
252 0 ▶ if (defined($matrix_data_ref)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $matrix_data_ref) { } 0 0 0
253 # get the number of columns and number of rows
254 # and verify that each row has same number of
255 # columns
256
257 0 $x_cnt = $#{$matrix_data_ref} + 1;
258 0 my $tmp = $#{$matrix_data_ref->[0]} + 1;
259
260 0 foreach my $i (@{$matrix_data_ref}) {
261 0 ▶ if ($tmp != $#{$i} + 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp != $#{$i;} + 1) 0 0 0
262 0 carp "each row must have the same number of columns";
263 0 _cleanup_tmpdir();
264 0 return 0;
265 }
266 }
267
268 0 $y_cnt = $tmp;
269
270  
271 # verify that number of tick marks == corresponds
272 # to that of xy matrix. One tick mark for every x
273 # y.
274
275 0 ▶ if (not _verify_ticks($x_cnt, $global_opts{"x-ticks"})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_verify_ticks($x_cnt, $global_opts{"x-ticks"})) 0 0 0
276 0 _cleanup_tmpdir();
277 0 return 0;
278 }
279
280 0 ▶ if (not _verify_ticks($y_cnt, $global_opts{"y-ticks"})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_verify_ticks($y_cnt, $global_opts{"y-ticks"})) 0 0 0
281 0 _cleanup_tmpdir();
282 0 return 0;
283 }
284 } else {
285 # XXX
286 # Poor man's hack to compute rows and columns in data file. This will
287 # make a second pass through file, but is probably faster than doing it
288 # in Perl.
289 0 my ($lead, $words, $bytes);
290 0 ($lead, $x_cnt, $words, $bytes) = split(/\D+/, `wc $data_filename`);
291 0 ▶ if (($x_cnt > 0) and ($words > 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x_cnt > 0 and $words > 0) { } 0 0 0
Condition0%0/3
$x_cnt > 0 and $words > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$x_cnt > 0 and $words > 0
$x_cnt > 0 $words > 0
Truth table0%0/3
A and B
A= $x_cnt > 0B= $words > 0
ABresult
0X0
100
111
292 0 $x_cnt++;
293 0 $y_cnt = $words/$x_cnt;
294 } else {
295 0 $x_cnt = 0;
296 0 $y_cnt = 0;
297 0 carp "Cannot compute number of rows and/or columns in file data";
298 }
299 }
300  
301 ##
302 ## print command file using this format
303 ##
304
305 # output.file
306 # x_min (normally 0)
307 # y_min (normally 0)
308 # x_step (normally 1)
309 # y_step (normally 1)
310 # x_cnt (number of rows of input)
311 # y_cnt (number of columns of input)
312 # data11 data12 data13 data14 .... (x by y matrix of doubles)
313 # data21 data22 data23 ....
314 # .
315 # .
316 # .
317 # datax1 datax2 ... dataxy
318 # Number of header lines (multiple header lines available)
319 # header1
320 # header2
321 # ...
322 # Number of header lines (multiple header lines available)
323 # foot1
324 # foot2
325 # ...
326 # Title of x-axis
327 # Title of y-axis
328 # Title of z-axis
329 # xlabel0 (x_cnt number of labels for ticks along x-axis)
330 # ...
331 # xlabelx
332 # ylabel0 (y_cnt number of labels for ticks along y-axis)
333 # ...
334 # ylabely
335
336 # create command file and open file handle
337 0 my $command_file = _make_tmpfile("command");
338 0 my $handle = new FileHandle;
339 0 ▶ if (not $handle->open(">$command_file")) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($handle->open(">$command_file")) 0 0 0
340 0 carp "could not open $command_file";
341 0 _cleanup_tmpdir();
342 0 return 0;
343 }
344
345 0 print $handle "$plot_file\n";
346 0 print $handle "$x_min\n";
347 0 print $handle "$y_min\n";
348 0 print $handle "$x_step\n";
349 0 print $handle "$y_step\n";
350 0 print $handle "$x_cnt\n";
351 0 print $handle "$y_cnt\n";
352 0 ▶ if (defined($matrix_data_ref)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $matrix_data_ref) { } 0 0 0
353 0 _print_matrix($handle, @{$matrix_data_ref});
354 } else {
355 0 _transfer_file($handle, $data_filename)
356 }
357 0 print $handle "$hdr_cnt\n";
358 0 _print_array($handle, @{$header});
359 0 print $handle "$ftr_cnt\n";
360 0 _print_array($handle, @{$footer});
361 0 print $handle "$x_axis\n";
362 0 print $handle "$y_axis\n";
363 0 print $handle "$z_axis\n";
364 0 _print_array($handle, @{$x_ticks});
365 0 _print_array($handle, @{$y_ticks});
366 0 $handle->close();
367  
368 # call xrt and convert file to gif
369 0 ▶ if (not _exec_xrt3d($command_file)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_exec_xrt3d($command_file)) 0 0 0
370 0 _cleanup_tmpdir();
371 0 return 0;
372 }
373 0 my $graph_format = $global_opts{"output type"};
374 0 ▶ if ($graph_format eq "ps") {
Branch: 2 branches
BranchTrueFalseTotal
if ($graph_format eq 'ps') { } 0 0 0
elsif ($graph_format eq 'xwd') { } 0 0 0
375 0 ▶ if (not _chk_status(system("cp $plot_file $output_file"))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_chk_status(system("cp $plot_file $output_file"))) 0 0 0
376 0 _cleanup_tmpdir();
377 0 return 0;
378 }
379 } elsif ($graph_format eq "xwd") {
380 0 ▶ if (not _chk_status(system("cp $plot_file $output_file"))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_chk_status(system("cp $plot_file $output_file"))) 0 0 0
381 0 _cleanup_tmpdir();
382 0 return 0;
383 }
384 } else {
385 0 ▶ if(not _convert_raster($graph_format, $plot_file, $output_file)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_convert_raster($graph_format, $plot_file, $output_file)) 0 0 0
386 0 _cleanup_tmpdir();
387 0 return 0;
388 }
389 }
390  
391 0 _cleanup_tmpdir();
392 0 return 1;
393 }
394
395 4 1;
396  
397  
398 __END__
« Graph/Xrt2d.pm ↑ Summary Graph/Gnuplot.pm »