Regions/DS9.pm

filter: Statementstmt 93.3% 126 / 135 Branchbran 45.0% 9 / 20 Conditioncond 53.8% 7 / 13 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 94.2% 49 / 52 Podpod 100% 1 / 1 totaltotal 84.7% 194 / 229 CC 16 SCAR 11.6
CC
16
Cov
85%
format_qstring
20.0
format_tags
12.0
format_text
4.6
CRAP 3.2
« Regions/CIAO/Variant.pm ↑ Summary Regions/CFITSIO.pm »
1 package CXC::Astro::Regions::DS9;
2  
3 # ABSTRACT: DS9 Compatible Regions
4  
5 2 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use experimental 'signatures', 'postderef', 'lexical_subs';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 our $VERSION = '0.05';
10  
11 2 package CXC::Astro::Regions::DS9::Role::Region {
12 2 ▶ use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 }
14 2 package CXC::Astro::Regions::DS9::Box {
15 2 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 }
17 2 package CXC::Astro::Regions::DS9::Annulus {
18 2 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 }
20 2 package CXC::Astro::Regions::DS9::Ellipse {
21 2 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 }
23  
24 2 ▶use constant RegionRole => __PACKAGE__ . '::Role::Region';
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 2 ▶use parent 'Exporter::Tiny';
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 2 ▶use Import::Into;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use CXC::Astro::Regions::DS9::Types qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
30 Angle
31 ArrayRef
32 ConsumerOf
33 CoordSys
34 Enum
35 Length
36 LengthPair
37 NonEmptyStr
38 OneZero
39 PointType
40 PositiveInt
41 RulerCoords
42 Tuple
43 Vertex
44 2 );
45  
46 2 ▶use CXC::Astro::Regions::DS9::Variant;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 2 ▶use List::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 2 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 0 ▶my sub croak {
Subroutine croak: not called CC=1 SCAR=6.9
52 0 require Carp;
53 0 goto \&Carp::croak;
54 }
55  
56 8 ▶my sub pkgpath ( @paths ) {
Subroutine pkgpath: called CC=1 SCAR=0.0
57 8 join q{::}, __PACKAGE__, map { ucfirst } @paths;
58 }
59  
60 194 ▶my sub args ( @args ) {
Subroutine args: called CC=2 SCAR=6.9
61 194 ▶ return @args == 1 ? ( name => $args[0] ) : @args;
Branch: 1 branch
BranchTrueFalseTotal
@args == 1 ? : 118 76 194
62 }
63  
64 17 ▶my sub format_text ( $type, $label, $values ) {
Subroutine format_text: called CC=4 SCAR=15.2
65 17 ▶ return () unless $values->@*;
Branch: 1 branch
BranchTrueFalseTotal
unless @$values 0 17 17
66 17 ▶ return $type eq 'prop' && defined $label
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'prop' && defined $label ? : 0 17 17
Condition66%2/3
$type eq "prop" and defined $label
!ll&&!rl&&r
1160
MC/DC0%0/2
$type eq "prop" and defined $label
$type eq "prop" defined $label
Truth table66%2/3
A and B
A= $type eq "prop"B= defined $label
ABresult
0X0
100
111
67 ? sprintf( '%s={%s}', $label, $values->@* )
68 : sprintf( '{%s}', $values->@* );
69 }
70  
71 0 ▶my sub format_qstring ( $type, $label, $values ) {
Subroutine format_qstring: not called CC=4 SCAR=30.0
72 0 ▶ return () unless $values->@*;
Branch: 1 branch
BranchTrueFalseTotal
unless @$values 0 0 0
73 0 ▶ return $type eq 'prop' && defined $label
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'prop' && defined $label ? : 0 0 0
Condition0%0/3
$type eq "prop" and defined $label
!ll&&!rl&&r
000
MC/DC0%0/2
$type eq "prop" and defined $label
$type eq "prop" defined $label
Truth table0%0/3
A and B
A= $type eq "prop"B= defined $label
ABresult
0X0
100
111
74 ? sprintf( '%s="%s"', $label, $values->@* )
75 : sprintf( '"%s"', $values->@* );
76 }
77  
78 0 ▶my sub format_tags ( $type, $label, $values ) {
Subroutine format_tags: not called CC=3 SCAR=24.8
79 0 ( $values, my @rest ) = $values->@*;
80 0 ▶ croak( 'too many values passed to tags' ) if @rest;
Branch: 1 branch
BranchTrueFalseTotal
if @rest 0 0 0
81 0 ▶ return () unless $values->@*;
Branch: 1 branch
BranchTrueFalseTotal
unless @$values 0 0 0
82 0 return join q{ }, map { sprintf( '%s={%s}', $label, $_ ) } $values->@*;
83 }
84  
85 22 ▶my sub ANGLE { { name => 'angle', isa => Angle, args( @_ ) } }
Subroutine ANGLE: called CC=1 SCAR=0.0
86 6 ▶my sub ANGLEPAIR { { name => 'angles', isa => Tuple [ Angle, Angle ], args( @_ ) } }
Subroutine ANGLEPAIR: called CC=1 SCAR=0.0
87 2 ▶my sub ARROW { { name => 'arrow', isa => OneZero, coerce => !!1, args( @_ ) } }
Subroutine ARROW: called CC=1 SCAR=0.0
88 4 ▶my sub ARROWS { { name => 'arrows', isa => ArrayRef [ OneZero, 2 ], args( @_ ) } }
Subroutine ARROWS: called CC=1 SCAR=0.0
89 18 ▶my sub BOOL { { name => undef, isa => OneZero, coerce => !!1, args( @_ ) } }
Subroutine BOOL: called CC=1 SCAR=0.0
90 2 ▶my sub COORDS { { name => 'coords', isa => CoordSys, coerce => !!1, args( @_ ) } }
Subroutine COORDS: called CC=1 SCAR=0.0
91 8 ▶my sub FILL { { name => 'fill', isa => OneZero, coerce => !!1, args( @_ ) } }
Subroutine FILL: called CC=1 SCAR=0.0
92 2 ▶my sub FORMAT { { name => 'format', isa => NonEmptyStr, args( @_ ) } }
Subroutine FORMAT: called CC=1 SCAR=0.0
93 16 ▶my sub LENGTH { { name => 'length', isa => Length, args( @_ ) } }
Subroutine LENGTH: called CC=1 SCAR=0.0
94 20 ▶my sub LENGTHPAIR { { name => undef, isa => LengthPair, args( @_ ) } }
Subroutine LENGTHPAIR: called CC=1 SCAR=0.0
95 4 ▶my sub LENGTHPAIR_ARRAY { { name => undef, isa => ArrayRef [LengthPair], args( @_ ) } }
Subroutine LENGTHPAIR_ARRAY: called CC=1 SCAR=0.0
96 2 ▶my sub LENGTH_ARRAY { { name => undef, isa => ArrayRef [Length], args( @_ ) } }
Subroutine LENGTH_ARRAY: called CC=1 SCAR=0.0
97 18 ▶my sub N { { name => 'n', isa => PositiveInt, args( @_ ) } }
Subroutine N: called CC=1 SCAR=0.0
98 2 ▶my sub POINT { { name => 'symbol', isa => PointType, coerce => !!1, label => 'point', args( @_ ) } }
Subroutine POINT: called CC=1 SCAR=0.0
99 4 ▶my sub POSINT { { name => undef, isa => PositiveInt, args( @_ ) } }
Subroutine POSINT: called CC=1 SCAR=0.0
100 2 ▶my sub QSTRING { { name => undef, isa => NonEmptyStr, format => \&format_text, args( @_ ) } }
Subroutine QSTRING: called CC=1 SCAR=0.0
101 2 ▶my sub RULERCOORDS {
Subroutine RULERCOORDS: called CC=1 SCAR=0.0
102 2 { name => 'coords', isa => RulerCoords, coerce => !!1, label => 'ruler', args( @_ ) };
103 }
104 2 ▶my sub STRING { { name => undef, isa => NonEmptyStr, args( @_ ) } }
Subroutine STRING: called CC=1 SCAR=0.0
105 2 ▶my sub TAGS {
Subroutine TAGS: called CC=1 SCAR=0.0
106 {
107 2 name => 'tags',
108 isa => ArrayRef [NonEmptyStr],
109 format => \&format_tags,
110 label => 'tag',
111 args( @_ ) };
112 }
113 8 ▶my sub TEXT { { name => 'text', isa => NonEmptyStr, format => \&format_text, args( @_ ) } }
Subroutine TEXT: called CC=1 SCAR=0.0
114 46 ▶my sub VERTEX { { name => undef, isa => Vertex, args( @_ ) } }
Subroutine VERTEX: called CC=1 SCAR=0.0
115 2 ▶my sub VERTICES { { name => 'vertices', isa => ArrayRef [Vertex], args( @_ ) } }
Subroutine VERTICES: called CC=1 SCAR=0.0
116  
117 2 my @CommonProps = (
118 TEXT,
119 ANGLE( 'textangle' ),
120 STRING( 'color' ),
121 { name => 'dashlist', isa => ArrayRef [PositiveInt] },
122 POSINT( name => 'linewidth', label => 'width' ),
123 QSTRING( 'font' ),
124 BOOL( 'select' ),
125 BOOL( 'highlite' ),
126 BOOL( 'dash' ),
127 BOOL( 'fixed' ),
128 BOOL( 'edit' ),
129 BOOL( 'move' ),
130 BOOL( 'rotate' ),
131 BOOL( 'delete' ),
132 BOOL( name => 'include', default => !!1 ),
133 { name => 'srctype', isa => Enum [ 'source', 'background' ], label => undef },
134 TAGS,
135 );
136  
137 2 ▶use Package::Stash;
Subroutine BEGIN: called CC=1 SCAR=0.0
138 2 our @EXPORT_OK = ( 'mkregion' );
139  
140 2 my $stash = Package::Stash->new( __PACKAGE__ );
141 42 ▶my sub REGION ( $region, %spec ) {
Subroutine REGION: called CC=3 SCAR=11.0
142  
143 42 ▶ push( ( $spec{props} //= [] )->@*, @CommonProps );
Condition100%2/2
$spec{"props"} //= []
l!l
1824
MC/DC100%1/1
$spec{"props"} //= []
$spec{"props"}
Truth table100%2/2
A
A= $spec{"props"}
Aresult
11
00
144 42 ▶ $spec{with} //= [RegionRole];
Condition100%2/2
$spec{"with"} //= ["CXC::Astro::Regions::DS9::Role::Region"]
l!l
240
MC/DC100%1/1
$spec{"with"} //= ["CXC::Astro::Regions::DS9::Role::Region"]
$spec{"with"}
Truth table100%2/2
A
A= $spec{"with"}
Aresult
11
00
145  
146 42 my $variant = Variant( $region, %spec );
147 42 Package::Stash->new( $variant )->add_symbol( q{@CARP_NOT}, [__PACKAGE__] );
148  
149 42 ▶ $stash->add_symbol( q{&} . $region, sub { $variant->new( @_ ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
150 42 push @EXPORT_OK, $region;
151 }
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171 # Annulus
172 # Usage: annulus x y inner outer n=#
173 2 REGION annulus_n => (
174 name => 'annulus',
175 params => [ VERTEX( 'center' ), LENGTHPAIR( 'annuli' ), N( label => 'n' ) ],
176 );
177  
178 # Annulus
179 # Usage: annulus x y r1 r2 r3...
180 2 REGION annulus_annuli => (
181 name => 'annulus',
182 params => [ VERTEX( 'center' ), LENGTH_ARRAY( 'annuli' ) ],
183 );
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213 # Box
214 # Usage: box x y width height angle # fill=[0|1]
215 2 REGION box_plain => (
216 name => 'box',
217 params => [ VERTEX( 'center' ), LENGTH( 'width' ), LENGTH( 'height' ), ANGLE( required => !!0 ) ],
218 props => [FILL],
219 );
220  
221 # Box Annulus
222 # Usage: box x y w1 h1 w2 h2 n=# [angle]
223 2 REGION box_n => (
224 name => 'box',
225 params => [
226 VERTEX( 'center' ),
227 LENGTHPAIR( 'inner' ),
228 LENGTHPAIR( 'outer' ),
229 N( label => 'n' ),
230 ANGLE( required => !!0 ),
231 ],
232 );
233  
234 # Box Annulus
235 # Usage: box x y w1 h1 w2 h2 w3 h3 ... [angle]
236 2 REGION box_annuli => (
237 name => 'box',
238 params => [ VERTEX( 'center' ), LENGTHPAIR_ARRAY( 'annuli' ), ANGLE( required => !!0 ), ],
239 );
240  
241  
242  
243  
244  
245  
246  
247  
248  
249  
250  
251  
252  
253 # Bpanda
254 # Usage: bpanda x y startangle stopangle nangle inner outer nradius [angle]
255 2 REGION bpanda => (
256 params => [
257 VERTEX( 'center' ),
258 ANGLEPAIR,
259 N( 'nangles' ),
260 LENGTHPAIR( 'inner' ),
261 LENGTHPAIR( 'outer' ),
262 N( 'nannuli' ),
263 ANGLE( required => !!0 ),
264 ],
265 );
266  
267  
268  
269  
270  
271  
272  
273  
274  
275 # Circle
276 # Usage: circle x y radius # fill=[0|1]
277 2 REGION circle => (
278 params => [ VERTEX( 'center' ), LENGTH( 'radius' ) ],
279 props => [FILL],
280 );
281  
282  
283  
284  
285  
286  
287  
288  
289  
290  
291  
292  
293  
294  
295  
296  
297  
298  
299  
300  
301  
302  
303  
304  
305  
306  
307  
308  
309  
310  
311 # Compass
312 # Usage: compass x1 y1 length # compass=<coordinate system> <north label> <east label> [0|1] [0|1]
313 REGION compass => (
314 params => [ VERTEX( 'base' ), LENGTH ],
315 props => [
316 COORDS( label => 'compass', default => 'physical' ),
317 TEXT( name => 'north', label => undef, default => 'N' ),
318 TEXT( name => 'east', label => undef, default => 'E' ),
319 4 ▶ ARROWS( label => undef, default => sub { [ 1, 1 ] } ),
Subroutine __ANON__: called CC=1 SCAR=0.0
320 ],
321 );
322  
323  
324  
325  
326  
327  
328  
329  
330  
331  
332  
333  
334  
335  
336  
337  
338 # Composite
339 # Usage: # composite x y angle
340 REGION composite => (
341 comment => !!1,
342 with => [], # we're not a normal region, so don't compose with RegionRole
343 params => [
344 VERTEX( 'center' ),
345 ANGLE( required => !!0 ),
346 {
347 name => 'regions',
348 isa => ArrayRef [ ConsumerOf [RegionRole] ],
349 render => !!0,
350 },
351 ],
352 around => [
353 2 ▶ render => sub ( $orig, $self ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
354 2 return [ $self->$orig, map { $_->render } $self->regions->@* ];
355 },
356 2 ],
357 );
358  
359  
360  
361  
362  
363  
364  
365  
366  
367  
368  
369  
370  
371  
372  
373  
374  
375  
376  
377  
378  
379  
380  
381  
382  
383  
384  
385  
386  
387 # Ellipse
388 # Usage: ellipse x y radius radius angle # fill=[0|1]
389 2 REGION ellipse_plain => (
390 name => 'ellipse',
391 params => [ VERTEX( 'center' ), LENGTHPAIR( 'radii' ), ANGLE( required => !!0 ) ],
392 props => [FILL],
393 );
394  
395 # Ellipse Annulus
396 # Usage: ellipse x y r11 r12 r21 r22 n=# [angle]
397 2 REGION ellipse_n => (
398 name => 'ellipse',
399 params => [
400 VERTEX( 'center' ),
401 LENGTHPAIR( 'inner' ),
402 LENGTHPAIR( 'outer' ),
403 N( label => 'n' ),
404 ANGLE( required => !!0 ),
405 ],
406 );
407  
408 # Ellipse Annulus
409 # Usage: ellipse x y r11 r12 r21 r22 r31 r32 ... [angle]
410 2 REGION ellipse_annuli => (
411 name => 'ellipse',
412 params => [ VERTEX( 'center' ), LENGTHPAIR_ARRAY( 'annuli' ), ANGLE( required => !!0 ), ],
413 );
414  
415  
416  
417  
418  
419  
420  
421  
422  
423  
424  
425  
426  
427 # Epanda
428 # Usage: epanda x y startangle stopangle nangle inner outer nradius [angle]
429 2 REGION epanda => (
430 params => [
431 VERTEX( 'center' ),
432 ANGLEPAIR,
433 N( 'nangles' ),
434 LENGTHPAIR( 'inner' ),
435 LENGTHPAIR( 'outer' ),
436 N( 'nannuli' ),
437 ANGLE( required => !!0 ),
438 ],
439 );
440  
441  
442  
443  
444  
445  
446  
447  
448  
449  
450  
451 # Line
452 # Usage: line x1 y1 x2 y2 # line=[0|1] [0|1]
453 2 REGION line => (
454 params => [ VERTEX( 'v1' ), VERTEX( 'v2' ) ],
455 props => [ ARROWS( label => 'line' ) ],
456 );
457  
458  
459  
460  
461  
462  
463  
464  
465  
466  
467  
468 # Panda
469 # Usage: panda x y startangle stopangle nangle inner outer nradius
470 2 REGION panda => (
471 params => [
472 VERTEX( 'center' ),
473 ANGLEPAIR,
474 N( 'nangles' ),
475 LENGTH( 'inner' ),
476 LENGTH( 'outer' ),
477 N( 'nannuli' ),
478 ],
479 );
480  
481  
482  
483  
484  
485  
486  
487  
488  
489  
490  
491  
492  
493  
494  
495  
496  
497 # Point
498 # Usage: point x y # point=[circle|box|diamond|cross|x|arrow|boxcircle] [size]
499 # circle point x y
500 2 REGION point => (
501 params => [ VERTEX( 'center' ) ],
502 props => [ POINT, POSINT( name => 'size', label => undef ) ],
503 );
504  
505  
506  
507  
508  
509  
510  
511  
512 # Polygon
513 # Usage: polygon x1 y1 x2 y2 x3 y3 ...# fill=[0|1]
514 2 REGION polygon => (
515 params => [VERTICES],
516 props => [FILL],
517 );
518  
519  
520  
521  
522  
523  
524  
525  
526  
527 # Projection
528 # Usage: projection x1 y1 x2 y2 width
529 2 REGION projection => ( params => [ VERTEX( 'v1' ), VERTEX( 'v2' ), LENGTH( 'width' ) ], );
530  
531  
532  
533  
534  
535  
536  
537  
538  
539  
540  
541  
542  
543 # Ruler
544 # Usage: ruler x1 y1 x2 y2 # ruler=[pixels|degrees|arcmin|arcsec] [format=<spec>]
545 2 REGION ruler => (
546 params => [ VERTEX( 'v1' ), VERTEX( 'v2' ) ],
547 props => [ RULERCOORDS, FORMAT ],
548 );
549  
550  
551  
552  
553  
554  
555  
556 # Text
557 # Usage: text x y # text={Your Text Here}
558 # text x y {Your Text Here}
559 2 REGION text => ( params => [ VERTEX( 'center' ), TEXT ], );
560  
561  
562  
563  
564  
565  
566  
567  
568  
569  
570  
571  
572  
573 # Vector
574 # Usage: vector x1 y1 length angle # vector=[0|1]
575 2 REGION vector => (
576 params => [ VERTEX( 'base' ), LENGTH, ANGLE ],
577 props => [ ARROW( label => 'vector' ) ],
578 );
579  
580 # set up dispatch classes to handle the different types of annulus and ellipse regions
581  
582 # The 'annulus' region can be
583 # * annuli with inner and outer radii and count
584 # * annuli with explicitly specified radii
585  
586 # The 'ellipse' region can be
587 # * a simple ellipse;
588 # * elliptical annuli with inner and outer radii pairs and count
589 # * elliptical annuli with explicitly specified radii pairs
590  
591 # The 'box' region can be
592 # * a simple box;
593 # * box annuli with inner and outer dims and count
594 # * box annuli with explicitly specified dims
595  
596 18 ▶my sub dispatch ( $package, %args ) {
Subroutine dispatch: called CC=4 SCAR=13.9
597 my $suffix
598 46 ▶ = ( List::Util::any { exists $args{$_} } qw( inner outer n ) )
Subroutine __ANON__: called CC=1 SCAR=0.0
599 ? 'n'
600 18 ▶ : exists $args{annuli} ? 'annuli'
Branch: 3 branches
BranchTrueFalseTotal
&List::Util::any(sub { exists $args{$_}; } , 'inner', 'outer', 'n') ? : 6 12 18
exists $args{'annuli'} ? : 6 6 12
$package =~ /Annulus/u ? : 0 6 6
601 # no such thing as annulus_plain; bounce to annulus_n and it'll
602 # croak because of bad args
603 : $package =~ /Annulus/ ? 'n'
604 : 'plain';
605  
606 18 return "${package}_${suffix}";
607 }
608  
609 2 for my $region ( 'annulus', 'ellipse', 'box' ) {
610 6 my $pkg = pkgpath( $region );
611 Package::Stash->new( $pkg )->add_symbol(
612 '&new',
613 18 ▶ sub ( $, %args ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
614 18 my $class = dispatch( $pkg, %args );
615 18 return $class->new( %args );
616 6 } );
617 16 ▶ $stash->add_symbol( q{&} . $region, sub { $pkg->new( @_ ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
618 6 push @EXPORT_OK, $region;
619 }
620  
621  
622 # No longer need this; clean it up.
623 2 undef $stash;
624  
625  
626  
627  
628  
629  
630  
631  
632  
633  
634  
635 2 ▶sub mkregion ( $shape, @args ) {
Subroutine mkregion: called CC=2 SCAR=7.6
Pod documented
636 2 my $class = pkgpath( $shape );
637 2 ▶ my $new = $class->can( 'new' ) // croak( "unknown region: $shape" );
Condition33%1/3
$class->can("new") // croak("unknown region: $shape")
l!l&&r!l&&!r
200
MC/DC0%0/2
$class->can("new") // croak("unknown region: $shape")
$class->can("new") croak("unknown region: $shape")
Truth table33%1/3
A // B
A= $class->can("new")B= croak("unknown region: $shape")
ABresult
1X1
011
000
638 2 $class->$new( @args );
639 }
640  
641 2 1;
642  
643 #
644 # This file is part of CXC-Astro-Regions
645 #
646 # This software is Copyright (c) 2023 by Smithsonian Astrophysical Observatory.
647 #
648 # This is free software, licensed under:
649 #
650 # The GNU General Public License, Version 3, June 2007
651 #
652  
653 __END__
« Regions/CIAO/Variant.pm ↑ Summary Regions/CFITSIO.pm »