Skip to content

Commit e957dc2

Browse files
committed
convert Cyl and Pol to have Scale as ndarray
1 parent ff1a2a0 commit e957dc2

1 file changed

Lines changed: 52 additions & 75 deletions

File tree

lib/PDL/Graphics/TriD/Graph.pm

Lines changed: 52 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,9 @@ use fields qw(LatticeObj);
205205
sub add_lattice_axis {
206206
my ($this) = @_;
207207
my (@nadd,@nc,@ns);
208+
my @widths = $this->{Scale}->slice('0:1')->t->diff2->list;
208209
for my $dim (0..1) {
209-
my $width = $this->{Scale}[$dim][1]-$this->{Scale}[$dim][0];
210+
my $width = $widths[$dim];
210211
if ($width > 100) {
211212
$nadd[$dim] = 10;
212213
} elsif ($width>30) {
@@ -216,7 +217,7 @@ sub add_lattice_axis {
216217
} else {
217218
$nadd[$dim] = 1;
218219
}
219-
$nc[$dim] = int($this->{Scale}[$dim][0]/$nadd[$dim])*$nadd[$dim];
220+
$nc[$dim] = int($this->{Scale}->slice("$dim,0")->sclr/$nadd[$dim])*$nadd[$dim];
220221
$ns[$dim] = int($width/$nadd[$dim])+1;
221222
}
222223
# can be changed to topo heights?
@@ -248,75 +249,63 @@ sub new {
248249

249250
sub init_scale {
250251
my ($this) = @_;
251-
$this->{Scale} = [undef, undef, [100,1012.5]];
252+
$this->{Scale} = PDL->pdl(PDL::float(), 'BAD BAD 100; BAD BAD 1012.5');
252253
}
253254

254255
sub add_scale {
255256
my ($this,$data,$inds) = @_;
256257
PDL::barf "no \$inds given" if !defined $inds;
257-
my $i = 0;
258-
for (@$inds) {
259-
my $d = $data->slice("($_)");
260-
my $max = $d->max->sclr;
261-
my $min = $d->min->sclr;
262-
if ($i==1) {
263-
if ($max > 89.9999 or $min < -89.9999) {
264-
barf "Error in Latitude $max $min\n";
265-
}
266-
}
267-
if (!defined $this->{Scale}[$i]) {
268-
$this->{Scale}[$i] = [$min,$max];
269-
} else {
270-
if ($min < $this->{Scale}[$i][0]) {
271-
$this->{Scale}[$i][0] = $min;
272-
}
273-
if ($max > $this->{Scale}[$i][1]) {
274-
$this->{Scale}[$i][1] = $max;
275-
}
276-
}
277-
$i++;
258+
$data = $data->dice_axis(0, $inds);
259+
my $to_minmax = $data->clump(1..$data->ndims-1); # xyz,...
260+
$to_minmax = $to_minmax->glue(1, $this->{Scale}); # include old min/max
261+
my ($mins, $maxes) = $to_minmax->transpose->minmaxover; # each is xyz
262+
if ($maxes->slice(1) >= 90 or $mins->slice(1) <= -90) {
263+
barf "Error in Latitude ", $maxes->slice(1), " ", $mins->slice(1);
278264
}
265+
$this->{Scale} = PDL->pdl($mins, $maxes); # xyz,minmax
279266
# Should make the projection center an option
280-
$this->{Center} = [$this->{Scale}[0][0]+($this->{Scale}[0][1]-$this->{Scale}[0][0])/2,
267+
$this->{Center} = [(($maxes->slice(0) + $mins->slice(0))/2)->sclr,
281268
0];
282269
}
283270

284271
sub finish_scale {
285272
my ($this) = @_;
286273
my @dist;
287274
# Normalize the smallest differences away.
288-
for (@{$this->{Scale}}) {
275+
my $scale = $this->{Scale}->t->unpdl;
276+
for (@$scale) {
289277
if (abs($_->[0] - $_->[1]) < 0.000001) {
290278
$_->[1] = $_->[0] + 1;
291279
}
292280
push(@dist,$_->[1]-$_->[0]);
293281
}
294282
# for the z coordinate reverse the min and max values
295-
my $max = $this->{Scale}[2][0];
296-
if ($max < $this->{Scale}[2][1]) {
297-
$this->{Scale}[2][0] = $this->{Scale}[2][1];
298-
$this->{Scale}[2][1] = $max;
283+
my $max = $scale->[2][0];
284+
if ($max < $scale->[2][1]) {
285+
$scale->[2][0] = $scale->[2][1];
286+
$scale->[2][1] = $max;
299287
}
300288
# Normalize longitude and latitude scale
301289
if ($dist[1] > $dist[0]) {
302-
$this->{Scale}[0][0] -= ($dist[1]-$dist[0])/2;
303-
$this->{Scale}[0][1] += ($dist[1]-$dist[0])/2;
290+
$scale->[0][0] -= ($dist[1]-$dist[0])/2;
291+
$scale->[0][1] += ($dist[1]-$dist[0])/2;
304292
} elsif ($dist[0] > $dist[1] && $dist[0]<90) {
305-
$this->{Scale}[1][0] -= ($dist[0]-$dist[1])/2;
306-
$this->{Scale}[1][1] += ($dist[0]-$dist[1])/2;
293+
$scale->[1][0] -= ($dist[0]-$dist[1])/2;
294+
$scale->[1][1] += ($dist[0]-$dist[1])/2;
307295
} elsif ($dist[0] > $dist[1]) {
308-
$this->{Scale}[1][0] -= (90-$dist[1])/2;
309-
$this->{Scale}[1][1] += (90-$dist[1])/2;
296+
$scale->[1][0] -= (90-$dist[1])/2;
297+
$scale->[1][1] += (90-$dist[1])/2;
310298
}
299+
$this->{Scale} = PDL->pdl(PDL::float(), $scale)->t;
311300
$this->add_lattice_axis;
312301
}
313302

314303
sub transform {
315304
my ($this,$point,$data,$inds) = @_;
316305
PDL::barf "no \$inds given" if !defined $inds;
317306
barf "Wrong number of arguments to transform $this\n" if @$inds != 3;
318-
my ($longrange, $latrange) = map $this->{Scale}[$_][1] - $this->{Scale}[$_][0], 0,1;
319-
my $pressure_max = $this->{Scale}[2][1];
307+
my ($longrange, $latrange) = $this->{Scale}->slice('0:1')->t->diff2->dog;
308+
my $pressure_max = $this->{Scale}->slice('2,1');
320309
$data = $data->dice_axis(0, $inds);
321310
$point->slice("(0)") +=
322311
0.5+($data->slice("(0)")-$this->{Center}[0]) /
@@ -347,65 +336,53 @@ sub new {
347336

348337
sub init_scale {
349338
my ($this) = @_;
350-
$this->{Scale} = [undef, undef, [100,1012.5]];
339+
$this->{Scale} = PDL->pdl(PDL::float(), 'BAD BAD 100; BAD BAD 1012.5');
351340
}
352341

353342
sub add_scale {
354343
my ($this,$data,$inds) = @_;
355344
PDL::barf "no \$inds given" if !defined $inds;
356-
my $i = 0;
357-
for (@$inds) {
358-
my $d = $data->slice("($_)");
359-
my $max = $d->max->sclr;
360-
my $min = $d->min->sclr;
361-
if ($i==1) {
362-
if ($max > 89.9999 or $min < -89.9999) {
363-
barf "Error in Latitude $max $min\n";
364-
}
365-
}
366-
if (!defined $this->{Scale}[$i]) {
367-
$this->{Scale}[$i] = [$min,$max];
368-
} else {
369-
if ($min < $this->{Scale}[$i][0]) {
370-
$this->{Scale}[$i][0] = $min;
371-
}
372-
if ($max > $this->{Scale}[$i][1]) {
373-
$this->{Scale}[$i][1] = $max;
374-
}
375-
}
376-
$i++;
345+
$data = $data->dice_axis(0, $inds);
346+
my $to_minmax = $data->clump(1..$data->ndims-1); # xyz,...
347+
$to_minmax = $to_minmax->glue(1, $this->{Scale}); # include old min/max
348+
my ($mins, $maxes) = $to_minmax->transpose->minmaxover; # each is xyz
349+
if ($maxes->slice(1) >= 90 or $mins->slice(1) <= -90) {
350+
barf "Error in Latitude ", $maxes->slice(1), " ", $mins->slice(1);
377351
}
378-
$this->{Center} = [$this->{Scale}[0][0]+($this->{Scale}[0][1]-$this->{Scale}[0][0])/2,
379-
$this->{Scale}[1][0]+($this->{Scale}[1][1]-$this->{Scale}[1][0])/2];
352+
$this->{Scale} = PDL->pdl($mins, $maxes); # xyz,minmax
353+
$this->{Center} = [(($maxes->slice(0) + $mins->slice(0))/2)->sclr,
354+
(($maxes->slice(1) + $mins->slice(1))/2)->sclr];
380355
}
381356

382357
sub finish_scale {
383358
my ($this) = @_;
384359
my @dist;
385360
# Normalize the smallest differences away.
386-
for (@{$this->{Scale}}) {
361+
my $scale = $this->{Scale}->t->unpdl;
362+
for (@$scale) {
387363
if (abs($_->[0] - $_->[1]) < 0.000001) {
388364
$_->[1] = $_->[0] + 1;
389365
}
390366
push(@dist,$_->[1]-$_->[0]);
391367
}
392368
# for the z coordinate reverse the min and max values
393-
my $max = $this->{Scale}[2][0];
394-
if ($max < $this->{Scale}[2][1]) {
395-
$this->{Scale}[2][0] = $this->{Scale}[2][1];
396-
$this->{Scale}[2][1] = $max;
369+
my $max = $scale->[2][0];
370+
if ($max < $scale->[2][1]) {
371+
$scale->[2][0] = $scale->[2][1];
372+
$scale->[2][1] = $max;
397373
}
398374
# Normalize longitude and latitude scale
399375
if ($dist[1] > $dist[0]) {
400-
$this->{Scale}[0][0] -= ($dist[1]-$dist[0])/2;
401-
$this->{Scale}[0][1] += ($dist[1]-$dist[0])/2;
376+
$scale->[0][0] -= ($dist[1]-$dist[0])/2;
377+
$scale->[0][1] += ($dist[1]-$dist[0])/2;
402378
} elsif ($dist[0] > $dist[1] && $dist[0]<90) {
403-
$this->{Scale}[1][0] -= ($dist[0]-$dist[1])/2;
404-
$this->{Scale}[1][1] += ($dist[0]-$dist[1])/2;
379+
$scale->[1][0] -= ($dist[0]-$dist[1])/2;
380+
$scale->[1][1] += ($dist[0]-$dist[1])/2;
405381
} elsif ($dist[0] > $dist[1]) {
406-
$this->{Scale}[1][0] -= (90-$dist[1])/2;
407-
$this->{Scale}[1][1] += (90-$dist[1])/2;
382+
$scale->[1][0] -= (90-$dist[1])/2;
383+
$scale->[1][1] += (90-$dist[1])/2;
408384
}
385+
$this->{Scale} = PDL->pdl(PDL::float(), $scale)->t;
409386
$this->add_lattice_axis;
410387
}
411388

@@ -415,8 +392,8 @@ sub transform {
415392
my $i = 0;
416393
barf "Wrong number of arguments to transform $this\n" if @$inds != 3;
417394
$data = $data->dice_axis(0, $inds);
418-
my ($longrange, $latrange) = map $this->{Scale}[$_][1] - $this->{Scale}[$_][0], 0,1;
419-
my $pressure_max = $this->{Scale}[2][1];
395+
my ($longrange, $latrange) = $this->{Scale}->slice('0:1')->t->diff2->dog;
396+
my $pressure_max = $this->{Scale}->slice('2,1');
420397
$point->slice("(0)") +=
421398
0.5+($data->slice("(0)")-$this->{Center}[0]) /
422399
$longrange

0 commit comments

Comments
 (0)