| 1 | package Qhull::PP; | ||
| 2 | |||
| 3 | # ABSTRACT: Pure Perl interface to Qhull | ||
| 4 | 2 | ▶ | use v5.26; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 2 | ▶ | use experimental 'signatures', 'lexical_subs', 'declared_refs'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | our $VERSION = '0.07'; | ||
| 11 | |||
| 12 | |||
| 13 | 2 | ▶ | use Exporter::Shiny 'qhull'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 2 | ▶ | use Scalar::Util 'blessed'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | 2 | ▶ | use Ref::Util 'is_arrayref'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | |||
| 17 | 2 | ▶ | use List::Util 'any'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 18 | 2 | ▶ | use Log::Any '$log'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 19 | |||
| 20 | 2 | ▶ | use Eval::Closure 'eval_closure'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 21 | 2 | ▶ | use Ref::Util 'is_hashref', 'is_plain_arrayref'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 22 | 2 | ▶ | use File::Spec; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 23 | 2 | ▶ | use Qhull::Util 'parse_output'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 24 | 2 | ▶ | use Qhull::Util::Options 'CAT_OUTPUT_FORMAT'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 25 | 2 | ▶ | use Qhull::Options; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 26 | 2 | ▶ | use System::Command; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 27 | 2 | ▶ | use Feature::Compat::Defer; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 28 | |||
| 29 | 2 | ▶ | use Alien::Qhull; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 30 | |||
| 31 | my sub croak { | ||
| 32 | require Carp; | ||
| 33 | our @CARP_NOT = qw( Qhull ); | ||
| 34 | goto \&Carp::croak; | ||
| 35 | } | ||
| 36 | |||
| 37 | use constant qhull_exe => do { | ||
| 38 | my $exe; | ||
| 39 | if ( my @bin_dirs = Alien::Qhull->bin_dir ) { | ||
| 40 | for my $dir ( @bin_dirs ) { | ||
| 41 | $exe = File::Spec->catfile( $dir, 'qhull' ); | ||
| 42 | last if -x $exe; | ||
| 43 | undef $exe; | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | _croak( q{Qhull executable 'qhull' was not found} ) | ||
| 48 | if !defined $exe; | ||
| 49 | |||
| 50 | $exe; | ||
| 51 | }; | ||
| 52 | |||
| 53 | my sub build_feed_qhull { | ||
| 54 | my @args = @_; | ||
| 55 | |||
| 56 | # individual coordinate elements | ||
| 57 | if ( @args == 1 && blessed $args[0] && $args[0]->isa( 'PDL' ) ) { | ||
| 58 | croak( 'a single NDArray argument must have dimensions == 2: got ' . $args[0]->ndims ) | ||
| 59 | if $args[0]->ndims != 2; | ||
| 60 | @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 | my $nelem; | ||
| 66 | my $ndims = @args; | ||
| 67 | croak( 'number of dimensions must be > 1' ) | ||
| 68 | if @args < 2; | ||
| 69 | |||
| 70 | my @qsub; | ||
| 71 | my @extents; | ||
| 72 | my $icoord = 0; | ||
| 73 | for my $coord ( @args ) { | ||
| 74 | if ( is_plain_arrayref( $coord ) ) { | ||
| 75 | push @extents, 0+ @$coord; | ||
| 76 | push @qsub, sprintf( q|$coord[%d][$_]|, $icoord ); | ||
| 77 | } | ||
| 78 | elsif ( blessed $coord && $coord->isa( 'PDL' ) ) { | ||
| 79 | croak( "coord[$icoord]: NDArray coordinate argument must be 1D" ) | ||
| 80 | if $coord->ndims != 1; | ||
| 81 | push @extents, $coord->nelem; | ||
| 82 | push @qsub, sprintf( q|$coord[%d]->at($_)|, $icoord ); | ||
| 83 | } | ||
| 84 | else { | ||
| 85 | croak( "coord[$icoord] is neither a Perl arrayref or an NDArray" ); | ||
| 86 | } | ||
| 87 | } | ||
| 88 | continue { $icoord++ } | ||
| 89 | |||
| 90 | # initialize value for capture. | ||
| 91 | $nelem = $extents[0]; | ||
| 92 | |||
| 93 | croak( 'coordinate arrays must have the same extent: ' . join( ', ', @extents ) ) | ||
| 94 | if any { $_ != $nelem } @extents; | ||
| 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 | croak( 'number of points must be > 1' ) | ||
| 100 | if $nelem < 2; | ||
| 101 | |||
| 102 | 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 | 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 | sub qhull ( @coords ) { | ||
| 164 | |||
| 165 | my %option = ( | ||
| 166 | raw => !!0, | ||
| 167 | passthrough => [], | ||
| 168 | trace => !!0, | ||
| 169 | qh_opts => undef, | ||
| 170 | save_input => undef, | ||
| 171 | is_hashref( $coords[-1] ) ? %{ pop @coords } : (), | ||
| 172 | ); | ||
| 173 | |||
| 174 | my $raw = delete $option{raw}; | ||
| 175 | my $trace = delete $option{trace}; | ||
| 176 | my $qh_opts = delete( $option{qh_opts} ) // []; | ||
| 177 | my $save_input = delete $option{save_input}; | ||
| 178 | |||
| 179 | if ( is_arrayref( $qh_opts ) ) { | ||
| 180 | $qh_opts = Qhull::Options->new_from_options( $qh_opts ); | ||
| 181 | my $passthrough = delete $option{passthrough}; | ||
| 182 | $qh_opts = $qh_opts->filter_args( passthrough => $passthrough ) | ||
| 183 | if !$raw; | ||
| 184 | } | ||
| 185 | elsif ( !blessed( $qh_opts ) || !$qh_opts->isa( 'Qhull::Options' ) ) { | ||
| 186 | croak( 'qh_opts must be a Qhull::Options object or an arrayref' ); | ||
| 187 | } | ||
| 188 | |||
| 189 | # default mode is convex hull, return orderd list of indices (extrema), (format => 'Fx') | ||
| 190 | $qh_opts = $qh_opts->clone_with_options( ['Fx'] ) | ||
| 191 | if !$qh_opts->has_compute && !$qh_opts->has_output_format; | ||
| 192 | |||
| 193 | croak( 'unknown options passed to qhull: ' . join( ', ', keys %option ) ) | ||
| 194 | if %option; | ||
| 195 | |||
| 196 | croak( 'specify coordinates either via "TI" or as an argument to qhull(), but not both' ) | ||
| 197 | if @coords && defined $qh_opts->input; | ||
| 198 | |||
| 199 | croak( 'no coordinates specified' ) | ||
| 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 | my ( $feed, $feed_source ) = @coords ? build_feed_qhull( @coords ) : (); | ||
| 205 | |||
| 206 | $log->is_debug | ||
| 207 | && $log->debug( 'executing qhull: ', $qh_opts->qhull_opts ); | ||
| 208 | |||
| 209 | my @cmd = ( qhull_exe, $qh_opts->qhull_opts->@* ); | ||
| 210 | my $cmd = System::Command->new( @cmd ); | ||
| 211 | if ( $feed ) { | ||
| 212 | |||
| 213 | # Log::Any says don't use newlines; ignore that advice here. | ||
| 214 | $log->trace( $feed_source ); | ||
| 215 | |||
| 216 | if ( defined $save_input ) { | ||
| 217 | open my $fh, '>', $save_input | ||
| 218 | or croak( "unable to create $save_input" ); | ||
| 219 | $feed->( $fh ); | ||
| 220 | $fh->close or croak( "error closing $save_input" ); | ||
| 221 | } | ||
| 222 | |||
| 223 | # ensure that the sub-process' sstdin is closed, regardless of | ||
| 224 | # how this block is exited. | ||
| 225 | defer { $cmd->stdin->close }; | ||
| 226 | $feed->( $cmd->stdin ); | ||
| 227 | } | ||
| 228 | |||
| 229 | my $stderr = do { | ||
| 230 | local $/ = undef; | ||
| 231 | readline( $cmd->stderr ); | ||
| 232 | }; | ||
| 233 | |||
| 234 | croak( 'error running ' . join( q{ }, @cmd ) . ': ' . $stderr ) | ||
| 235 | if length $stderr; | ||
| 236 | |||
| 237 | my $output = defined( $qh_opts->output ) | ||
| 238 | ? do { | ||
| 239 | $cmd->close; | ||
| 240 | require File::Slurper; | ||
| 241 | File::Slurper::read_binary( $qh_opts->output ); | ||
| 242 | } | ||
| 243 | : do { | ||
| 244 | local $/ = undef; | ||
| 245 | readline( $cmd->stdout ); | ||
| 246 | }; | ||
| 247 | |||
| 248 | return $output if $raw; | ||
| 249 | |||
| 250 | my @output = parse_output( | ||
| 251 | { trace => $trace }, | ||
| 252 | $output, map { $_->[1]{name} } $qh_opts->by_category->{ +CAT_OUTPUT_FORMAT }{by_position}->@*, | ||
| 253 | ); | ||
| 254 | |||
| 255 | return @output; | ||
| 256 | } | ||
| 257 | |||
| 258 | 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__ | ||
| 271 | |||
| 272 | =pod | ||
| 273 | |||
| 274 | =for :stopwords Diab Jerius Smithsonian Astrophysical Observatory passthrough qhull | ||
| 275 | |||
| 276 | =head1 NAME | ||
| 277 | |||
| 278 | Qhull::PP - Pure Perl interface to Qhull | ||
| 279 | |||
| 280 | =head1 VERSION | ||
| 281 | |||
| 282 | version 0.07 | ||
| 283 | |||
| 284 | =head1 SYNOPSIS | ||
| 285 | |||
| 286 | use Qhull 'qhull'; | ||
| 287 | |||
| 288 | # generate a convex hull and return the ordered | ||
| 289 | # indices of the points in the convex hull | ||
| 290 | my \@indices = qhull( $x, $y ); | ||
| 291 | |||
| 292 | =head1 DESCRIPTION | ||
| 293 | |||
| 294 | This is a pure-Perl interface to B<qhull>. It passes input to the | ||
| 295 | B<qhull> executable and parses and returns the results. | ||
| 296 | |||
| 297 | =head1 SUBROUTINES | ||
| 298 | |||
| 299 | =head2 qhull | ||
| 300 | |||
| 301 | @results = qhull( @coords, ?\%options ); | ||
| 302 | |||
| 303 | Make B<qhull> do something. If no options are specified, the default | ||
| 304 | is to generate a convex hull and return a list of the ordered indices | ||
| 305 | of the points on the convex hull. | ||
| 306 | |||
| 307 | The following options are available: | ||
| 308 | |||
| 309 | =over | ||
| 310 | |||
| 311 | =item * | ||
| 312 | |||
| 313 | raw => I<Boolean> | ||
| 314 | |||
| 315 | Don't parse the output; return it as a single string; | ||
| 316 | |||
| 317 | =item * | ||
| 318 | |||
| 319 | passthrough => I<array of Qhull options> | ||
| 320 | |||
| 321 | =item * | ||
| 322 | |||
| 323 | trace => I<Boolean> | ||
| 324 | |||
| 325 | Add line number info to parsed output. | ||
| 326 | |||
| 327 | =item * | ||
| 328 | |||
| 329 | qh_opts => I<array of Qhull options> or a L<Qhull::Options> object. | ||
| 330 | |||
| 331 | It's expensive to parse the options, so for repeat calls, pass in a L<Qhull::Options> object. | ||
| 332 | |||
| 333 | =back | ||
| 334 | |||
| 335 | =head1 SUPPORT | ||
| 336 | |||
| 337 | =head2 Bugs | ||
| 338 | |||
| 339 | 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> | ||
| 340 | |||
| 341 | =head2 Source | ||
| 342 | |||
| 343 | Source is available at | ||
| 344 | |||
| 345 | https://gitlab.com/djerius/p5-qhull | ||
| 346 | |||
| 347 | and may be cloned from | ||
| 348 | |||
| 349 | https://gitlab.com/djerius/p5-qhull.git | ||
| 350 | |||
| 351 | =head1 SEE ALSO | ||
| 352 | |||
| 353 | Please see those modules/websites for more information related to this module. | ||
| 354 | |||
| 355 | =over 4 | ||
| 356 | |||
| 357 | =item * | ||
| 358 | |||
| 359 | L<Qhull|Qhull> | ||
| 360 | |||
| 361 | =back | ||
| 362 | |||
| 363 | =head1 AUTHOR | ||
| 364 | |||
| 365 | Diab Jerius <djerius@cpan.org> | ||
| 366 | |||
| 367 | =head1 COPYRIGHT AND LICENSE | ||
| 368 | |||
| 369 | This software is Copyright (c) 2024 by Smithsonian Astrophysical Observatory. | ||
| 370 | |||
| 371 | This is free software, licensed under: | ||
| 372 | |||
| 373 | The GNU General Public License, Version 3, June 2007 | ||
| 374 | |||
| 375 | =cut | ||