Skip to content

Commit 8bc9afd

Browse files
committed
update Contours labelling to polyline scheme
1 parent 502b72e commit 8bc9afd

File tree

4 files changed

+29
-55
lines changed

4 files changed

+29
-55
lines changed

lib/PDL/Demos/TriD1.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ my @demo = (
149149
|],
150150

151151
[actnw => q|
152-
contour3d($z, [$x,$y,$z-1]);
152+
contour3d($z, [$x,$y,$z-1], {Labels=>[1,15]});
153153
# ...and draw contours on that
154154
# [press 'q' in the graphics window when done]
155155
|],

lib/PDL/Graphics/TriD.pm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,10 @@ ndarray specifying the contour color and C<$options> is a hash reference to
524524
a list of options documented below. Contours can also be coloured by
525525
value using the set_color_table function.
526526
527+
The C<Labels> option takes an array-ref of integers: the first is
528+
the interval between contours to label (1 does them all), the second
529+
is the interval between points on each contour to label.
530+
527531
Implemented by L<PDL::Graphics::TriD::Contours>.
528532
529533
=head2 labels3d

lib/PDL/Graphics/TriD/Contours.pm

Lines changed: 20 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use PDL::ImageND;
2828
use PDL::Graphics::TriD;
2929
use PDL::Graphics::TriD::Objects;
3030
use base qw/PDL::Graphics::TriD::GObject/;
31-
use fields qw/PathIndex ContourPathIndexEnd Labels LabelStrings/;
31+
use fields qw/PathIndex ContourPathIndexEnd LabelStart LabelStrings/;
3232

3333
$PDL::Graphics::TriD::verbose //= 0;
3434

@@ -130,7 +130,7 @@ sub new {
130130
$pcnt += 1; $pval += $thispi_maxval + 1;
131131
}
132132

133-
$this->addlabels($options->{Labels}) if defined $options->{Labels};
133+
$this->addlabels(@{$options->{Labels}}) if defined $options->{Labels};
134134

135135
$this;
136136
}
@@ -166,54 +166,25 @@ $segint defaults to 5, that is every fifth line segment will be labeled.
166166
167167
=cut
168168

169-
sub addlabels{
170-
my ($self,$labelint, $segint) = @_;
171-
172-
$labelint = 1 unless(defined $labelint);
173-
$segint = 5 unless(defined $segint);
174-
175-
my $cnt=0;
176-
177-
my $strlist;
178-
my $lp=pdl->null;
179-
180-
my $pcnt = 0;
181-
my $offset = pdl[0.5,0.5,0.5];
182-
183-
for(my $i=0; $i<= $#{$self->{ContourSegCnt}}; $i++){
184-
next unless defined $self->{ContourSegCnt}[$i];
185-
$cnt = $self->{ContourSegCnt}[$i];
186-
my $val = $self->{Options}{ContourVals}->slice("($i)");
187-
188-
my $leg = $self->{Points}->slice(":,$pcnt:$cnt");
189-
$pcnt=$cnt+1;
190-
191-
next if($i % $labelint);
192-
193-
for(my $j=0; $j< $leg->getdim(1); $j+=2){
194-
next if(($j/2) % $segint);
195-
196-
my $j1=$j+1;
197-
198-
my $lp2 = $leg->slice(":,($j)") +
199-
$offset*($leg->slice(":,($j1)") -
200-
$leg->slice(":,($j)"));
201-
202-
203-
$lp = $lp->append($lp2);
204-
# need a label string for each point
205-
push(@$strlist,$val);
206-
207-
}
208-
169+
sub addlabels {
170+
my ($self, $labelint, $segint) = @_;
171+
$labelint //= 1;
172+
$segint //= 5;
173+
my (@pi_ends, @strlist);
174+
my $lp = PDL->null;
175+
for (my $i=0; $i<= $#{$self->{ContourPathIndexEnd}}; $i++) {
176+
next unless defined $self->{ContourPathIndexEnd}[$i];
177+
push @pi_ends, $self->{PathIndex}->at($self->{ContourPathIndexEnd}[$i]);
178+
next if $i % $labelint;
179+
my ($start, $end) = (@pi_ends > 1 ? $pi_ends[-2] : 0, $pi_ends[-1]);
180+
my $lp2 = $self->{Points}->slice(":,$start:$end:$segint");
181+
push @strlist, ($self->{Options}{ContourVals}->slice("($i)")) x $lp2->dim(1);
182+
$lp = $lp->glue(1,$lp2);
209183
}
210-
if($lp->nelem>0){
211-
$self->{Points} = $self->{Points}->transpose
212-
->append($lp->reshape(3,$lp->nelem/3)->transpose)->transpose;
213-
$self->{Labels} = [$cnt+1,$cnt+$lp->nelem/3];
214-
$self->{LabelStrings} = $strlist;
215-
}
216-
184+
return if !$lp->nelem;
185+
$self->{Points} = $self->{Points}->glue(1,$lp);
186+
$self->{LabelStart} = $self->{Points}->dim(1) - $lp->dim(1);
187+
$self->{LabelStrings} = \@strlist;
217188
}
218189

219190
=head2 set_colortable($table)

lib/PDL/Graphics/TriD/GL.pm

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,10 @@ sub PDL::Graphics::TriD::Contours::gdraw {
199199
$i++;
200200
$pcnt=$ie+1;
201201
}
202-
if (defined $this->{Labels}){
203-
glColor3d(1,1,1);
204-
my $seg = sprintf ":,%d:%d",$this->{Labels}[0],$this->{Labels}[1];
205-
PDL::Graphics::OpenGLQ::gl_texts($points->slice($seg),
206-
$this->{LabelStrings});
202+
if (defined $this->{LabelStart}) {
203+
glColor3d(1,1,1);
204+
PDL::Graphics::OpenGLQ::gl_texts($points->slice(":,$this->{LabelStart}:"),
205+
$this->{LabelStrings});
207206
}
208207
}
209208

0 commit comments

Comments
 (0)