Qhull/PP.pm

filter: Statementstmt 89.6% 121 / 135 Branchbran 53.7% 29 / 54 Conditioncond 47.8% 11 / 23 MC/DCmcdc 14.2% 2 / 14 Subroutinesub 95.6% 22 / 23 Podpod 100% 1 / 1 totaltotal 74.4% 186 / 250 CC 38 SCAR 28.9
CC
38
Cov
76%
build_feed_qhull
32.9
qhull
27.7
BEGIN
5.3
CRAP 18.1
↑ Summary Qhull/Util/Options.pm »
1 package Qhull::PP;
2  
3 # ABSTRACT: Pure Perl interface to Qhull
4 3 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use experimental 'signatures', 'lexical_subs', 'declared_refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 our $VERSION = '0.08';
11  
12  
13 3 ▶use Exporter::Shiny 'qhull';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Ref::Util 'is_arrayref';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 3 ▶use List::Util 'any';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use Log::Any '$log';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 3 ▶use Eval::Closure 'eval_closure';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use Ref::Util 'is_hashref', 'is_plain_arrayref';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶use Qhull::Util 'parse_output';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use Qhull::Util::Options 'CAT_OUTPUT_FORMAT';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 3 ▶use Qhull::Options;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 3 ▶use System::Command;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 3 ▶use Feature::Compat::Defer;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 3 ▶use Alien::Qhull;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 0 ▶my sub croak {
Subroutine croak: not called CC=1 SCAR=6.9
32 0 require Carp;
33 0 our @CARP_NOT = qw( Qhull );
34 0 goto \&Carp::croak;
35 }
36  
37 3 ▶use constant qhull_exe => do {
Subroutine BEGIN: called CC=5 SCAR=16.7
38 3 my $exe;
39 3 ▶ if ( my @bin_dirs = Alien::Qhull->bin_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@bin_dirs) = "Alien::Qhull"->bin_dir) 3 0 3
40 3 for my $dir ( @bin_dirs ) {
41 3 $exe = File::Spec->catfile( $dir, 'qhull' );
42 3 ▶ last if -x $exe;
Branch: 1 branch
BranchTrueFalseTotal
if -x $exe 3 0 3
43 0 undef $exe;
44 }
45 }
46  
47 3 ▶ _croak( q{Qhull executable 'qhull' was not found} )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $exe 0 3 3
48 if !defined $exe;
49  
50 3 $exe;
51 3 };
52  
53 2 ▶my sub build_feed_qhull {
Subroutine build_feed_qhull: called CC=13 SCAR=34.9
54 2 my @args = @_;
55  
56 # individual coordinate elements
57 2 ▶ if ( @args == 1 && blessed $args[0] && $args[0]->isa( 'PDL' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args == 1 and builtin::blessed($args[0]) and $args[0]->isa("PDL")) 0 2 2
Condition33%2/6
@args == 1 and builtin::blessed($args[0]) and $args[0]->isa("PDL")
!ll&&!rl&&r
200
@args == 1 and builtin::blessed($args[0])
!ll&&!rl&&r
200
MC/DC0%0/3
@args == 1 and builtin::blessed($args[0]) and $args[0]->isa("PDL")
@args == 1 builtin::blessed($args[0]) $args[0]->isa("PDL")
Truth table25%1/4
A and B and C
A= @args == 1B= builtin::blessed($args[0])C= $args[0]->isa("PDL")
ABCresult
0XX0
10X0
1100
1111
58 0 ▶ croak( 'a single NDArray argument must have dimensions == 2: got ' . $args[0]->ndims )
Branch: 1 branch
BranchTrueFalseTotal
if $args[0]->ndims != 2 0 0 0
59 if $args[0]->ndims != 2;
60 0 @args = $args[0]->dog;
61 }
62  
63 # these are captured in the constructed feed_qhull() sub; $nelem
64 # is determined after scanning the input
65 2 my $nelem;
66 2 my $ndims = @args;
67 2 ▶ croak( 'number of dimensions must be > 1' )
Branch: 1 branch
BranchTrueFalseTotal
if @args < 2 0 2 2
68 if @args < 2;
69  
70 2 my @qsub;
71 my @extents;
72 2 my $icoord = 0;
73 2 for my $coord ( @args ) {
74 4 ▶ if ( is_plain_arrayref( $coord ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_plain_arrayref($coord)) { } 4 0 4
elsif (builtin::blessed($coord) and $coord->isa('PDL')) { } 0 0 0
75 4 push @extents, 0+ @$coord;
76 4 push @qsub, sprintf( q|$coord[%d][$_]|, $icoord );
77 }
78 ▶ elsif ( blessed $coord && $coord->isa( 'PDL' ) ) {
Condition0%0/3
builtin::blessed($coord) and $coord->isa("PDL")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($coord) and $coord->isa("PDL")
builtin::blessed($coord) $coord->isa("PDL")
Truth table0%0/3
A and B
A= builtin::blessed($coord)B= $coord->isa("PDL")
ABresult
0X0
100
111
79 0 ▶ croak( "coord[$icoord]: NDArray coordinate argument must be 1D" )
Branch: 1 branch
BranchTrueFalseTotal
if $coord->ndims != 1 0 0 0
80 if $coord->ndims != 1;
81 0 push @extents, $coord->nelem;
82 0 push @qsub, sprintf( q|$coord[%d]->at($_)|, $icoord );
83 }
84 else {
85 0 croak( "coord[$icoord] is neither a Perl arrayref or an NDArray" );
86 }
87 }
88 4 continue { $icoord++ }
89  
90 # initialize value for capture.
91 2 $nelem = $extents[0];
92  
93 croak( 'coordinate arrays must have the same extent: ' . join( ', ', @extents ) )
94 2 ▶ if any { $_ != $nelem } @extents;
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { $_ != $nelem; } , @extents) 0 2 2
95  
96 # actually the following is not quite true. depending upon what
97 # the caller is asking to be computed, the minimum number of
98 # points may be larger.
99 2 ▶ croak( 'number of points must be > 1' )
Branch: 1 branch
BranchTrueFalseTotal
if $nelem < 2 0 2 2
100 if $nelem < 2;
101  
102 2 my $source = join( "\n",
103 q| use experimental 'signatures'; |,
104 q| sub ($fh) { |,
105 q| $fh->say( $ndims ); |,
106 q| $fh->say( $nelem ); |,
107 q| $fh->say( join( "\t", |,
108 join( ', ', @qsub ),
109 qq|) ) for (0..$nelem-1)|, q|}|, );
110  
111 return (
112 2 eval_closure(
113 source => $source,
114 environment => {
115 '@coord' => \@args,
116 '$ndims' => \$ndims,
117 '$nelem' => \$nelem,
118 },
119 ),
120 $source,
121 );
122 }
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162 ## no critic( Subroutines::ProhibitExcessComplexity )
163 9 ▶sub qhull ( @coords ) {
Subroutine qhull: called CC=22 SCAR=33.2
Pod documented
164  
165 my %option = (
166 raw => !!0,
167 passthrough => [],
168 trace => !!0,
169 qh_opts => undef,
170 save_input => undef,
171 9 ▶ is_hashref( $coords[-1] ) ? %{ pop @coords } : (),
Branch: 1 branch
BranchTrueFalseTotal
is_hashref($coords[-1]) ? : 8 1 9
172 );
173  
174 9 my $raw = delete $option{raw};
175 9 my $trace = delete $option{trace};
176 9 ▶ my $qh_opts = delete( $option{qh_opts} ) // [];
Condition100%2/2
delete $option{"qh_opts"} // []
l!l
81
MC/DC100%1/1
delete $option{"qh_opts"} // []
delete $option{"qh_opts"}
Truth table100%2/2
A
A= delete $option{"qh_opts"}
Aresult
11
00
177 9 my $save_input = delete $option{save_input};
178 9 my $passthrough = delete $option{passthrough};
179  
180 9 ▶ if ( is_arrayref( $qh_opts ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_arrayref($qh_opts)) { } 6 3 9
elsif (not builtin::blessed($qh_opts) && $qh_opts->isa('Qhull::Options')) { } 0 3 3
181 6 $qh_opts = Qhull::Options->new_from_options( $qh_opts );
182 }
183 ▶ elsif ( !blessed( $qh_opts ) || !$qh_opts->isa( 'Qhull::Options' ) ) {
Condition33%1/3
builtin::blessed($qh_opts) && $qh_opts->isa('Qhull::Options')
!ll&&!rl&&r
003
MC/DC0%0/2
builtin::blessed($qh_opts) && $qh_opts->isa('Qhull::Options')
builtin::blessed($qh_opts) $qh_opts->isa('Qhull::Options')
Truth table33%1/3
A && B
A= builtin::blessed($qh_opts)B= $qh_opts->isa('Qhull::Options')
ABresult
0X0
100
111
184 0 croak( 'qh_opts must be a Qhull::Options object or an arrayref' );
185 }
186  
187 9 $qh_opts = $qh_opts->filter_args( passthrough => $passthrough );
188  
189 # default mode is convex hull, return orderd list of indices (extrema), (format => 'Fx')
190 9 ▶ $qh_opts = $qh_opts->clone_with_options( ['Fx'] )
Branch: 1 branch
BranchTrueFalseTotal
unless $qh_opts->has_compute or $qh_opts->has_output_format 5 4 9
Condition66%2/3
$qh_opts->has_compute or $qh_opts->has_output_format
l!l&&r!l&&!r
045
MC/DC50%1/2
$qh_opts->has_compute or $qh_opts->has_output_format
$qh_opts->has_compute $qh_opts->has_output_format
Truth table66%2/3
A or B
A= $qh_opts->has_computeB= $qh_opts->has_output_format
ABresult
1X1
011
000
191 if !$qh_opts->has_compute && !$qh_opts->has_output_format;
192  
193 9 ▶ croak( 'unknown options passed to qhull: ' . join( ', ', keys %option ) )
Branch: 1 branch
BranchTrueFalseTotal
if %option 0 9 9
194 if %option;
195  
196 9 ▶ croak( 'specify coordinates either via "TI" or as an argument to qhull(), but not both' )
Branch: 1 branch
BranchTrueFalseTotal
if @coords and defined $qh_opts->input 0 9 9
Condition66%2/3
@coords and defined $qh_opts->input
!ll&&!rl&&r
720
MC/DC0%0/2
@coords and defined $qh_opts->input
@coords defined $qh_opts->input
Truth table66%2/3
A and B
A= @coordsB= defined $qh_opts->input
ABresult
0X0
100
111
197 if @coords && defined $qh_opts->input;
198  
199 9 ▶ croak( 'no coordinates specified' )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $qh_opts->input or @coords 0 9 9
Condition66%2/3
defined $qh_opts->input or @coords
l!l&&r!l&&!r
720
MC/DC0%0/2
defined $qh_opts->input or @coords
defined $qh_opts->input @coords
Truth table66%2/3
A or B
A= defined $qh_opts->inputB= @coords
ABresult
1X1
011
000
200 if !defined $qh_opts->input && !@coords;
201  
202 # if there are errors in creating the feed routine, die before starting up
203 # qhull
204 9 ▶ my ( $feed, $feed_source ) = @coords ? build_feed_qhull( @coords ) : ();
Branch: 1 branch
BranchTrueFalseTotal
@coords ? : 2 7 9
205  
206 9 ▶ $log->is_debug
Branch: 1 branch
BranchTrueFalseTotal
$Qhull::PP::log->is_debug and $Qhull::PP::log->debug("executing qhull: ", $qh_opts->qhull_opts) 0 9 9
207 && $log->debug( 'executing qhull: ', $qh_opts->qhull_opts );
208  
209 9 my @cmd = ( qhull_exe, $qh_opts->qhull_opts->@* );
210 9 my $cmd = System::Command->new( @cmd );
211 9 ▶ if ( $feed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($feed) 2 7 9
212  
213 # Log::Any says don't use newlines; ignore that advice here.
214 2 $log->trace( $feed_source );
215  
216 2 ▶ if ( defined $save_input ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $save_input) 0 2 2
217 0 ▶ open my $fh, '>', $save_input
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $save_input or croak "unable to create $save_input" 0 0 0
218 or croak( "unable to create $save_input" );
219 0 $feed->( $fh );
220 0 ▶ $fh->close or croak( "error closing $save_input" );
Branch: 1 branch
BranchTrueFalseTotal
$fh->close or croak "error closing $save_input" 0 0 0
221 }
222  
223 # ensure that the sub-process' sstdin is closed, regardless of
224 # how this block is exited.
225 2 defer { $cmd->stdin->close };
226 2 $feed->( $cmd->stdin );
227 }
228  
229 9 my $stderr = do {
230 9 local $/ = undef;
231 9 readline( $cmd->stderr );
232 };
233  
234 9 ▶ croak( 'error running ' . join( q{ }, @cmd ) . ': ' . $stderr )
Branch: 1 branch
BranchTrueFalseTotal
if length $stderr 0 9 9
235 if length $stderr;
236  
237 my $output = defined( $qh_opts->output )
238 ? do {
239 4 $cmd->close;
240 4 require File::Slurper;
241 4 File::Slurper::read_binary( $qh_opts->output );
242 }
243 9 ▶ : do {
Branch: 1 branch
BranchTrueFalseTotal
defined $qh_opts->output ? : 4 5 9
244 5 local $/ = undef;
245 5 readline( $cmd->stdout );
246 };
247  
248 9 ▶ return $output if $raw;
Branch: 1 branch
BranchTrueFalseTotal
if $raw 2 7 9
249  
250 my @output = parse_output(
251 { trace => $trace },
252 7 $output, map { $_->[1]{name} } $qh_opts->by_category->{ +CAT_OUTPUT_FORMAT }{by_position}->@*,
253 7 );
254  
255 7 return @output;
256 }
257  
258 3 1;
259  
260 #
261 # This file is part of Qhull
262 #
263 # This software is Copyright (c) 2024 by Smithsonian Astrophysical Observatory.
264 #
265 # This is free software, licensed under:
266 #
267 # The GNU General Public License, Version 3, June 2007
268 #
269  
270 __END__
↑ Summary Qhull/Util/Options.pm »