Skip to content

Commit

Permalink
PGPLOT zap last string-eval
Browse files Browse the repository at this point in the history
  • Loading branch information
mohawk2 committed Apr 12, 2024
1 parent 6a2abcb commit 75a913f
Showing 1 changed file with 19 additions and 57 deletions.
76 changes: 19 additions & 57 deletions lib/PDL/Graphics/Simple/PGPLOT.pm
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,6 @@ our $pgplot_methods = {

};

##############################
# PDL::Graphics::Simple::PGPLOT::plot

sub plot {
my $me = shift;
my $ipo = shift;
Expand All @@ -198,133 +195,102 @@ sub plot {
$po->{xtitle}= $ipo->{xtitle} if(defined($ipo->{xtitle}));
$po->{ytitle}= $ipo->{ytitle} if(defined($ipo->{ytitle}));
$po->{justify}= $ipo->{justify} if(defined($ipo->{justify}));
my %color_opts = ();
if(defined($ipo->{crange})) {
$color_opts{'MIN'} = $ipo->{crange}->[0] if(defined($ipo->{crange}->[0]));
$color_opts{'MAX'} = $ipo->{crange}->[0] if(defined($ipo->{crange}->[1]));

my %color_opts;
if (defined($ipo->{crange})) {
$color_opts{MIN} = $ipo->{crange}[0] if defined $ipo->{crange}[0];
$color_opts{MAX} = $ipo->{crange}[0] if defined $ipo->{crange}[1];
}

my $more = 0;

if($ipo->{oplot} and $me->{opt}->{type} =~ m/^f/i) {
die "The PGPLOT engine does not yet support oplot for files. Instead, \nglom all your lines together into one call to plot.\n";
}

unless($ipo->{oplot}) {

$me->{curvestyle} = 0;

$me->{logaxis} = $ipo->{logaxis};

$po->{axis} = 0;
if($ipo->{logaxis} =~ m/x/i) {
$po->{axis} += 10;
$ipo->{xrange} = [ log10($ipo->{xrange}->[0]), log10($ipo->{xrange}->[1]) ];
$ipo->{xrange} = [ map log10($_), @{$ipo->{xrange}}[0,1] ];
}
if($ipo->{logaxis} =~ m/y/i) {
$po->{axis} += 20;
$ipo->{yrange} = [ log10($ipo->{yrange}->[0]), log10($ipo->{yrange}->[1]) ];
$ipo->{yrange} = [ map log10($_), @{$ipo->{yrange}}[0,1] ];
}

$me->{obj}->release;
$me->{obj}->env(@{$ipo->{xrange}}, @{$ipo->{yrange}}, $po);
$me->{obj}->hold;
} else {
# Force a hold if we are held.
$me->{obj}->hold;
}
$me->{obj}->hold;

# ppo is "post-plot options", which are really a mix of plot and curve options.
# Currently we don't parse any plot options into it (they're handled by the "env"
# call) but if we end up doing so, it should go here. The linestyle and color
# are curve options that are autoincremented each curve.
my %ppo = ();
my $ppo = \%ppo;

while(@_) {
while (@_) {
my ($co, @data) = @{shift()};
my @extra_opts = ();

if( defined($co->{style}) and $co->{style} ) {
if ( defined($co->{style}) and $co->{style} ) {
$me->{curvestyle} = int($co->{style});
} else {
$me->{curvestyle}++;
}

$ppo->{ color } = ($me->{curvestyle}-1) % 7 + 1;
$ppo->{ color } = $me->{curvestyle}-1 % 7 + 1;
$ppo->{ linestyle } = ($me->{curvestyle}-1) % 5 + 1;


if( defined($co->{width}) and $co->{width} ) {
$ppo->{ linewidth } = int($co->{width});
}

$ppo->{ linewidth } = int($co->{width}) if $co->{width};
our $pgplot_methods;
my $pgpm = $pgplot_methods->{$co->{with}};
die "Unknown curve option 'with $co->{with}'!" unless($pgpm);

if($pgpm eq 'imag') {
for my $k(keys %color_opts) {
$po->{$k} = $color_opts{$k};
}

$ppo->{ drawwedge } = ($ipo->{wedge} != 0);

# Extract transform parameters from the corners of the image...
my $xcoords = shift(@data);
my $ycoords = shift(@data);

my $datum_pix = [0,0];
my $datum_sci = [$xcoords->at(0,0), $ycoords->at(0,0)];

my $t1 = ($xcoords->slice("(-1),(0)") - $xcoords->slice("(0),(0)")) / ($xcoords->dim(0)-1);
my $t2 = ($xcoords->slice("(0),(-1)") - $xcoords->slice("(0),(0)")) / ($xcoords->dim(1)-1);

my $t4 = ($ycoords->slice("(-1),(0)") - $ycoords->slice("(0),(0)")) / ($ycoords->dim(0)-1);
my $t5 = ($ycoords->slice("(0),(-1)") - $ycoords->slice("(0),(0)")) / ($ycoords->dim(1)-1);

my $transform = pdl(
$datum_sci->[0] - $t1 * $datum_pix->[0] - $t2 * $datum_pix->[1],
$t1, $t2,
$datum_sci->[1] - $t4 * $datum_pix->[0] - $t5 * $datum_pix->[1],
$t4, $t5
)->flat;

{ # sepia color table
my $r = (xvals(256)/255)->sqrt;
my $g = (xvals(256)/255);
my $b = (xvals(256)/255)**2;
$me->{obj}->ctab($g, $r, $g, $b);
}
}

if($me->{logaxis} =~ m/x/i) {
$data[0] = $data[0]->log10;
}
if($me->{logaxis} =~ m/y/i) {
$data[1] = $data[1]->log10;
}

if(ref($pgpm) eq 'CODE') {
&$pgpm($me, $ipo, \@data, $ppo);
$data[0] = $data[0]->log10 if $me->{logaxis} =~ m/x/i;
$data[1] = $data[1]->log10 if $me->{logaxis} =~ m/y/i;
if (ref $pgpm eq 'CODE') {
$pgpm->($me, $ipo, \@data, $ppo);
} else {
my $str= sprintf('$me->{obj}->%s(@data,%s);%s',$pgpm,'$ppo',"\n");
eval $str;
$me->{obj}->$pgpm(@data,$ppo);
}

unless($pgpm eq 'imag') {
$ppo->{linestyle}++;
$ppo->{color}++;
}

$me->{obj}->hold;
}

##############################
# End of curve plotting.
# Now place the legend if necessary.
if($ipo->{legend}) {
if ($ipo->{legend}) {
my $xp;
my $xrdiff = $ipo->{xrange}->[1] - $ipo->{xrange}->[0];
if( $ipo->{legend}=~ m/l/i ) {
Expand All @@ -334,7 +300,6 @@ sub plot {
} else {
$xp = 0.4 * $xrdiff + $ipo->{xrange}->[0];
}

my $yp;
my $yrdiff = $ipo->{yrange}->[1] - $ipo->{yrange}->[0];
if( $ipo->{legend}=~ m/t/i ) {
Expand All @@ -344,7 +309,6 @@ sub plot {
} else {
$yp = 0.6 * $yrdiff + $ipo->{yrange}->[0];
}

print "keys is [".join(",",@{$me->{keys}})."]; xp is $xp; yp is $yp\n";
$me->{obj}->legend(
$me->{keys},
Expand All @@ -354,9 +318,7 @@ sub plot {
}
);
}

$me->{obj}->release;

}

sub DESTROY {
Expand Down

0 comments on commit 75a913f

Please sign in to comment.