blib/lib/Qhull/Util.pm

filter: Statementstmt 93.5% 115 / 123 Branchbran 58.3% 35 / 60 Conditioncond 50.0% 6 / 12 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 86.6% 13 / 15 Podpod 100% 4 / 4 totaltotal 78.8% 175 / 222 CC 37 SCAR 21.8
CC
37
Cov
80%
parse_size
13.6
parse_output_facets
13.1
parse_output_vertices
8.6
CRAP 8.8
« blib/lib/Qhull/Options.pm ↑ Summary blib/lib/Qhull/PP.pm »
1 package Qhull::Util;
2  
3 # ABSTRACT: Various bits and pieces
4  
5 5 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use experimental 'signatures', 'lexical_subs', 'declared_refs', 'refaliasing';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Ref::Util qw( is_arrayref is_hashref );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Log::Any '$log';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 our $VERSION = '0.07';
13  
14 5 ▶use Qhull::Util::Options ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 5 ▶use Exporter::Shiny qw( parse_output supported_output_format );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 5 our @CARP_NOT = qw( Qhull::PP Qhull::Options );
19  
20 my sub croak {
21 0 ▶ require Carp;
Subroutine croak: not called CC=1 SCAR=6.9
22 0 goto \&Carp::croak;
23 }
24  
25 my sub parse_output_facets;
26 my sub parse_output_vertices;
27  
28 5 my %Parser = (
29 f => {
30 func => \&parse_output_facets,
31 },
32 p => {
33 func => \&parse_output_vertices,
34 },
35 Fx => {
36 func => \&parse_extreme_points,
37 },
38 FS => {
39 func => \&parse_size,
40 },
41 );
42  
43  
44  
45  
46  
47  
48  
49  
50  
51 ▶sub supported_output_format( $format ) {
Subroutine supported_output_format: not called CC=1 SCAR=6.9
Pod documented
52 0 return defined $Parser{$format};
53 }
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82 ▶sub parse_output( $options, $output, @output_args ) {
Subroutine parse_output: called CC=3 SCAR=11.2
Pod documented
83  
84 4 my $line_no = 1;
85 4 my @output;
86 4 for my $arg ( @output_args ) {
87 4 ▶ my $parser = $Parser{$arg} // croak( "unsupported output format: $arg" );
Condition33%1/3
$Parser{$arg} // croak("unsupported output format: $arg")
l!l&&r!l&&!r
400
MC/DC0%0/2
$Parser{$arg} // croak("unsupported output format: $arg")
$Parser{$arg} croak("unsupported output format: $arg")
Truth table33%1/3
A // B
A= $Parser{$arg}B= croak("unsupported output format: $arg")
ABresult
1X1
011
000
88 4 ( $line_no, my $results ) = $parser->{func}->( $options, \$output, $line_no );
89 4 push @output, $results;
90 }
91  
92 4 return @output;
93 }
94 ## no critic( RegularExpressions::ProhibitEscapedMetacharacters )
95 5 my $qr_Record = qr/\G\n*([^\n]*?)$/mx;
96 5 my $qr_Point = qr/ p(\d+) \(v (\d+) \) /mx;
97 5 my $qr_PointRecord = qr/\G\n*- \h* $qr_Point \h* : \h+ ($Num) \h+ ($Num) \h* $ /mx;
98 5 my $qr_Facet = qr/f(\d+)/mx;
99 5 my $qr_FacetHeader = qr/\G\n*- \h $qr_Facet \h* $/mx;
100 5 my $qr_FacetAttr = qr/\G\n* \h+ - \h (?<attr>[^:]+) : \h+ /x;
101  
102 5 my %qr_FacetAttr = (
103 'flags' => qr/\G \h* (\w+) \h*/mx,
104 'normal' => qr/\G \h* ($Num) \h*/mx,
105 'offset' => qr/\G \h* ($Num) \h*/mx,
106 'vertices' => qr/\G \h* $qr_Point \h*/mx,
107 'neighboring facets' => qr/\G \h* $qr_Facet \h*/mx,
108 );
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
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 ▶sub parse_output_facets ( $option, $buf_ref, $line_no ) {
Subroutine parse_output_facets: called CC=12 SCAR=25.7
156 1 my \$buffer = $buf_ref;
157  
158 1 ▶ croak( "end of data in qhull output at line $line_no" )
Branch: 1 branch
BranchTrueFalseTotal
unless $buffer =~ /$qr_Record/gu 0 1 1
159 if $buffer !~ /$qr_Record/g;
160  
161 1 ▶ if ( ( my $contents = $1 ) !~ /^Vertices and facets/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ((my $contents = $1) =~ /^Vertices and facets/u) 0 1 1
162 0 croak(
163 "out of sync in qhull output at line $line_no;" . " expected 'Vertices and facets', got: $contents",
164 );
165 }
166  
167 # read vertices
168 1 ++$line_no;
169 1 my ( @vertices, @facets );
170 1 ▶ while ( $buffer =~ m/$qr_PointRecord/gc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buffer =~ /$qr_PointRecord/cgu) 100 1 101
171 100 my ( $point, $vertex, @coords ) = @{^CAPTURE};
172 push @vertices,
173 {
174 100 ▶ ( $option->{trace} ? ( line_no => $line_no ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
$option->{'trace'} ? : 0 100 100
175 point => $point,
176 vertex => $vertex,
177 coords => \@coords,
178 };
179 }
180 continue {
181 100 ++$line_no;
182 }
183  
184 1 ▶ croak( "missing vertices at line $line_no" )
Branch: 1 branch
BranchTrueFalseTotal
unless @vertices 0 1 1
185 if !@vertices;
186  
187 1 ▶ while ( $buffer =~ m/$qr_FacetHeader/gc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buffer =~ /$qr_FacetHeader/cgu) 100 1 101
188  
189 100 my $id = $1;
190  
191 100 my %line_no = ( id => $line_no );
192 100 my %facet = ( id => $id, );
193 100 ▶ $facet{line_no} = \%line_no if $option->{trace};
Branch: 1 branch
BranchTrueFalseTotal
if $option->{"trace"} 0 100 100
194  
195 100 ++$line_no;
196 100 ▶ while ( $buffer =~ m/$qr_FacetAttr/gc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buffer =~ /$qr_FacetAttr/cgu) 500 100 600
197  
198 500 my $attr = $+{attr};
199 500 ▶ my $qr = $qr_FacetAttr{$attr} // croak( "unknown facet attribute at line $line_no: $attr" );
Condition33%1/3
$qr_FacetAttr{$attr} // croak("unknown facet attribute at line ${line_no}: $attr")
l!l&&r!l&&!r
50000
MC/DC0%0/2
$qr_FacetAttr{$attr} // croak("unknown facet attribute at line ${line_no}: $attr")
$qr_FacetAttr{$attr} croak("unknown facet attribute at line ${line_no}: $attr")
Truth table33%1/3
A // B
A= $qr_FacetAttr{$attr}B= croak("unknown facet attribute at line ${line_no}: $attr")
ABresult
1X1
011
000
200  
201 500 my @values = $buffer =~ m/$qr/gc;
202 500 ▶ @values || croak( "unparseable facet attribute value at line $line_no" );
Branch: 1 branch
BranchTrueFalseTotal
@values or croak "unparseable facet attribute value at line $line_no" 0 500 500
203  
204 500 $facet{$attr} = \@values;
205 500 $line_no{$attr} = $line_no;
206 }
207 continue {
208 500 ++$line_no;
209 }
210  
211 100 push @facets, \%facet;
212 }
213  
214 1 ▶ croak( "missing facets at line $line_no" )
Branch: 1 branch
BranchTrueFalseTotal
unless @vertices 0 1 1
215 if !@vertices;
216  
217 1 return $line_no, { vertices => \@vertices, facets => \@facets };
218 }
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235 ▶sub parse_output_vertices( $option, $buf_ref, $line_no ) {
Subroutine parse_output_vertices: called CC=8 SCAR=21.5
236 1 my \$buffer = $buf_ref;
237  
238 1 my @results;
239 1 for ( 0, 1 ) {
240 2 ▶ croak( "end of data in qhull output at line $line_no" )
Branch: 1 branch
BranchTrueFalseTotal
unless $buffer =~ /$qr_Record \n/gux 0 2 2
241 if $buffer !~ /$qr_Record \n/gx;
242  
243 2 ▶ if ( ( my $contents = $1 ) !~ /\h* ($Int) \h*/x ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ((my $contents = $1) =~ /\h* ($Qhull::Util::Int) \h*/ux) 0 2 2
244 0 croak( "out of sync in qhull output at line $line_no;" . " expected an integer, got: $contents", );
245 }
246 ## no critic (RegularExpressions::ProhibitCaptureWithoutTest)
247 2 push @results, $1;
248 }
249 continue {
250 2 ++$line_no;
251 }
252  
253 1 my $nelem = $results[-1];
254  
255 # read vertices
256 1 my ( @vertices );
257 1 ▶ while ( $nelem-- and my @coords = $buffer =~ m/\G \h* ($Num) \h*?/xmgc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nelem-- and my(@coords) = $buffer =~ /\G \h* ($Qhull::Util::Num) \h*?/cgmux) 100 1 101
Condition66%2/3
$nelem-- and my(@coords) = $buffer =~ /\G \h* ($Qhull::Util::Num) \h*?/cgmux
!ll&&!rl&&r
10100
MC/DC50%1/2
$nelem-- and my(@coords) = $buffer =~ /\G \h* ($Qhull::Util::Num) \h*?/cgmux
$nelem-- my(@coords) = $buffer =~ /\G \h* ($Qhull::Util::Num) \h*?/cgmux
Truth table66%2/3
A and B
A= $nelem--B= my(@coords) = $buffer =~ /\G \h* ($Qhull::Util::Num) \h*?/cgmux
ABresult
0X0
100
111
258 100 ▶ push @vertices, [ ( $option->{trace} ? $line_no : () ), ( map 0+ $_, @coords ), ];
Branch: 1 branch
BranchTrueFalseTotal
$option->{'trace'} ? : 0 100 100
259 100 $buffer =~ /\n+/gc;
260 }
261 continue {
262 100 ++$line_no;
263 }
264  
265 1 ▶ croak( "missing vertices at line $line_no" )
Branch: 1 branch
BranchTrueFalseTotal
unless @vertices 0 1 1
266 if !@vertices;
267  
268 1 return $line_no, \@vertices;
269  
270 }
271  
272  
273  
274  
275  
276  
277  
278  
279  
280  
281  
282  
283  
284  
285  
286  
287 ▶sub parse_extreme_points( $option, $buf_ref, $line_no ) {
Subroutine parse_extreme_points: called CC=7 SCAR=20.4
Pod documented
288 1 my \$buffer = $buf_ref;
289  
290 1 ▶ croak( "end of data in qhull output at line $line_no" )
Branch: 1 branch
BranchTrueFalseTotal
unless $buffer =~ /$qr_Record \n/gux 0 1 1
291 if $buffer !~ /$qr_Record \n/gx;
292  
293 1 ▶ if ( ( my $contents = $1 ) !~ /\h* ($Int) \h*/x ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ((my $contents = $1) =~ /\h* ($Qhull::Util::Int) \h*/ux) 0 1 1
294 0 croak( "out of sync in qhull output at line $line_no;" . " expected an integer, got: $contents", );
295 }
296  
297 ## no critic (RegularExpressions::ProhibitCaptureWithoutTest)
298 1 my $nelem = $1;
299 1 ++$line_no;
300  
301 # read vertex indices
302 1 my ( @vertices );
303 1 ▶ while ( $nelem-- and $buffer =~ m/\G \h* ($Int) \h*?\n/xgm ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nelem-- and $buffer =~ /\G \h* ($Qhull::Util::Int) \h*?\n/gmux) 100 1 101
Condition66%2/3
$nelem-- and $buffer =~ /\G \h* ($Qhull::Util::Int) \h*?\n/gmux
!ll&&!rl&&r
10100
MC/DC50%1/2
$nelem-- and $buffer =~ /\G \h* ($Qhull::Util::Int) \h*?\n/gmux
$nelem-- $buffer =~ /\G \h* ($Qhull::Util::Int) \h*?\n/gmux
Truth table66%2/3
A and B
A= $nelem--B= $buffer =~ /\G \h* ($Qhull::Util::Int) \h*?\n/gmux
ABresult
0X0
100
111
304 100 ▶ push @vertices, $option->{trace} ? [ $line_no, $1 ] : $1;
Branch: 1 branch
BranchTrueFalseTotal
$option->{'trace'} ? : 0 100 100
305 }
306 continue {
307 100 ++$line_no;
308 }
309  
310 1 ▶ croak( "missing vertices at line $line_no" )
Branch: 1 branch
BranchTrueFalseTotal
unless @vertices 0 1 1
311 if !@vertices;
312  
313 1 return $line_no, \@vertices;
314  
315 }
316  
317  
318  
319  
320  
321  
322  
323  
324  
325  
326  
327  
328  
329  
330  
331  
332  
333  
334  
335  
336  
337  
338  
339  
340  
341  
342  
343  
344  
345  
346  
347  
348 ▶sub parse_size( $option, $buf_ref, $line_no ) {
Subroutine parse_size: called CC=11 SCAR=26.1
Pod documented
349 1 my \$buffer = $buf_ref;
350  
351 ## no critic (RegularExpressions::ProhibitCaptureWithoutTest)
352  
353 1 ▶ croak( "end of data in qhull output at line $line_no" )
Branch: 1 branch
BranchTrueFalseTotal
unless $buffer =~ /$qr_Record \n/gux 0 1 1
354 if $buffer !~ /$qr_Record \n/gx;
355  
356 1 my %results;
357  
358 1 my $contents = $1;
359 1 my @ints = $contents =~ /\G\h* ($Int) \h*/gxc;
360 @ints
361 1 ▶ or
Branch: 1 branch
BranchTrueFalseTotal
@ints or croak "out of sync in qhull output at line $line_no;" . " expected an integer, got: $contents" 0 1 1
362 croak( "out of sync in qhull output at line $line_no;" . " expected an integer, got: $contents",
363 );
364  
365 1 ▶ $contents =~ /\G\h*$/gcx
Branch: 1 branch
BranchTrueFalseTotal
$contents =~ /\G\h*$/cgux or croak "junk at end of line ${line_no}: " . substr($contents, pos $contents) 0 1 1
366 or croak( "junk at end of line $line_no: " . substr( $contents, pos( $contents ) ) );
367  
368 1 my $nints = shift @ints;
369 1 ▶ $nints == @ints
Branch: 1 branch
BranchTrueFalseTotal
$nints == @ints or croak "expected $nints integers; got " . @ints 0 1 1
370 or croak( "expected $nints integers; got " . @ints );
371  
372 1 ++$line_no;
373  
374 1 ▶ croak( "end of data in qhull output at line $line_no" )
Branch: 1 branch
BranchTrueFalseTotal
unless $buffer =~ /$qr_Record \n/gux 0 1 1
375 if $buffer !~ /$qr_Record \n/gx;
376  
377 ## no critic (RegularExpressions::ProhibitUnusedCapture)
378 1 ▶ if ( ( $contents = $1 ) !~ /\h* ($Int) \h*/gx ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (($contents = $1) =~ /\h* ($Qhull::Util::Int) \h*/gux) 0 1 1
379 0 croak( "out of sync in qhull output at line $line_no;" . " expected an integer, got: $contents", );
380 }
381  
382 1 my $nelem = $1;
383  
384 1 my @reals = $contents =~ /\G\h* ($Num) \h*/cgmx;
385  
386 1 ▶ $contents =~ /\G\h*$/gcx
Branch: 1 branch
BranchTrueFalseTotal
$contents =~ /\G\h*$/cgux or croak "junk at end of line ${line_no}: " . substr($contents, pos $contents) 0 1 1
387 or croak( "junk at end of line $line_no: " . substr( $contents, pos( $contents ) ) );
388  
389 @reals
390 1 ▶ or croak(
Branch: 1 branch
BranchTrueFalseTotal
@reals or croak sprintf("out of sync in qhull output at line %d; expected %d real numbers, got: %s", $line_no, $nelem, substr($contents, pos $contents)) 0 1 1
391 sprintf(
392 'out of sync in qhull output at line %d; expected %d real numbers, got: %s',
393 $line_no, $nelem, substr( $contents, pos( $contents ) ) ) );
394 1 ▶ $nelem == @reals
Branch: 1 branch
BranchTrueFalseTotal
$nelem == @reals or croak sprintf("expected %d real numbers at line %d; got %d", $nelem, $line_no, 0 + @reals) 0 1 1
395 or croak( sprintf( 'expected %d real numbers at line %d; got %d', $nelem, $line_no, 0+ @reals ) );
396  
397 1 @results{ 'area', 'volume' } = @reals;
398  
399 1 ▶ if ( $option->{trace} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option->{"trace"}) 0 1 1
400 $results{line_no} = {
401 0 area => $line_no,
402 volume => $line_no,
403 };
404 }
405  
406 1 ++$line_no;
407 1 return $line_no, \%results;
408 }
409  
410 5 1;
411  
412 #
413 # This file is part of Qhull
414 #
415 # This software is Copyright (c) 2024 by Smithsonian Astrophysical Observatory.
416 #
417 # This is free software, licensed under:
418 #
419 # The GNU General Public License, Version 3, June 2007
420 #
421  
422 __END__
423  
424 =pod
425  
426 =for :stopwords Diab Jerius Smithsonian Astrophysical Observatory coords qhull
427  
428 =head1 NAME
429  
430 Qhull::Util - Various bits and pieces
431  
432 =head1 VERSION
433  
434 version 0.07
435  
436 =head1 SYNOPSIS
437  
438 =head1 SUBROUTINES
439  
440 =head2 supported_output_format
441  
442 $bool = supported_output_format( $format );
443  
444 Returns true if parsing for C<$format> (a Qhull output format flag) is supported.
445  
446 =head2 parse_output
447  
448 @results = parse_output( \%options, $output, @output_formats );
449  
450 Parses output from B<qhull> based on the specified B<output_formats> (e.g. C<p>, C<f>, C<Fx>), which must be
451 in the same order as specified to B<qhull>.
452  
453 Returns a list of parsed data, one element per output format. See the
454 B<parse_output_*> routines for the structure of the parsed data.
455  
456 The following options are available:
457  
458 =over
459  
460 =item *
461  
462 trace
463  
464 Add line number information to the parsed output which identifies the
465 input line containing the data. Each output format will record it
466 differently.
467  
468 =back
469  
470 =head2 parse_output_facets
471  
472 ( $line_no, \%parsed_data ) = parse_output_facets( \%option, \$buffer, $line_no );
473  
474 Parse C<f> (facets & vertices) formatted output. B<pos($buffer)> must
475 be the offset into B<$buffer> where the data start. B<$line_no> is
476 the line number corresponding to that offset. The updated line number
477 is returned as well as the parsed data.
478  
479 The parsed data are in the following structure:
480  
481 { vertices => \@vertices, facets => \@facets }
482  
483 B<@vertices> is an array of hashes, on per vertex, each with the following elements:
484  
485 =over
486  
487 =item *
488 point id
489  
490 =item *
491  
492 vertex id
493  
494 =item *
495  
496 coords - an arrayref of coordinate values
497  
498 =item *
499  
500 line_no - only present if the trace option is set;
501  
502 =back
503  
504 B<@facets> is an array of hashes, on per facet. The entries in a
505 facet hash will depend upon the format parameters passed to B<qhull>.
506  
507 If the B<trace> option is specified, then there will be an additional
508 entry B<line_no> which is a hash keyed off of the names of the facet
509 entry attributes, whose values are the line numbers in the input they
510 appear on.
511  
512 =head2 parse_output_vertices
513  
514 ( $line_no, \@vertices ) = parse_output_vertices( \%option, \$buffer, $line_no );
515  
516 Parse C<p> (vertices) formatted output. B<pos($buffer)> must be the
517 offset into B<$buffer> where the data start. B<$line_no> is the line
518 number corresponding to that offset. The updated line number is
519 returned as well as the parsed data.
520  
521 B<@vertices> is an array of coordinate arrayrefs, one per vertex. If
522 the B<trace> option is specified, the first element in the coordinate
523 array is the line number in the output the data were parsed from.
524  
525 =head2 parse_extreme_points
526  
527 ( $line_no, \@indices ) = parse_extreme_points( \%option, \$buffer, $line_no );
528  
529 Parse C<Fx> (extreme point) formatted output. B<pos($buffer)> must be the
530 offset into B<$buffer> where the data start. B<$line_no> is the line
531 number corresponding to that offset. The updated line number is
532 returned as well as the parsed data.
533  
534 B<@indices> is an array of indices, one per extreme point. If
535 the B<trace> option is specified, the elements in B<@indices> are array refs,
536 with the first element the line number in the output the indices were parsed from.
537  
538 =head2 parse_size
539  
540 ( $line_no, \%sizes ) = parse_size( \%option, \$buffer, $line_no );
541  
542 Parse C<FS> (size) formatted output. B<pos($buffer)> must be the
543 offset into B<$buffer> where the data start. B<$line_no> is the line
544 number corresponding to that offset. The updated line number is
545 returned as well as the parsed data.
546  
547 B<\%size> is a hash with the following entries:
548  
549 =over
550  
551 =item * area
552  
553 =item * volume
554  
555 =item *
556  
557 line_no - only present if the trace option is set;
558  
559 =back
560  
561 If the B<trace> option is specified, then there will be an additional
562 entry B<line_no> which is a hash keyed off of the names of the other
563 attributes, whose values are the line numbers in the input they
564 appear on.
565  
566 =head1 SUPPORT
567  
568 =head2 Bugs
569  
570 Please report any bugs or feature requests to bug-qhull@rt.cpan.org or through the web interface at: L<https://rt.cpan.org/Public/Dist/Display.html?Name=Qhull>
571  
572 =head2 Source
573  
574 Source is available at
575  
576 https://gitlab.com/djerius/p5-qhull
577  
578 and may be cloned from
579  
580 https://gitlab.com/djerius/p5-qhull.git
581  
582 =head1 SEE ALSO
583  
584 Please see those modules/websites for more information related to this module.
585  
586 =over 4
587  
588 =item *
589  
590 L<Qhull|Qhull>
591  
592 =item *
593  
594 L<<=cut|<=cut>
595  
596 =back
597  
598 =head1 AUTHOR
599  
600 Diab Jerius <djerius@cpan.org>
601  
602 =head1 COPYRIGHT AND LICENSE
603  
604 This software is Copyright (c) 2024 by Smithsonian Astrophysical Observatory.
605  
606 This is free software, licensed under:
607  
608 The GNU General Public License, Version 3, June 2007
609  
610 =cut
« blib/lib/Qhull/Options.pm ↑ Summary blib/lib/Qhull/PP.pm »