Skip to content

Commit 8cc2dc9

Browse files
committed
make Trigrid only a container
1 parent 06f909d commit 8cc2dc9

File tree

2 files changed

+27
-41
lines changed

2 files changed

+27
-41
lines changed

lib/PDL/Graphics/TriD/GL.pm

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,8 @@ sub PDL::Graphics::TriD::Triangles::gdraw {
208208
my ($this,$points) = @_;
209209
my $options = $this->{Options};
210210
my $shading = $options->{Shading};
211-
my $colours = $this->{Colors}->clump(1..$this->{Colors}->ndims-1)->dice_axis(1,$this->{Faceidx}->flat)->splitdim(1,3);
211+
my $colours = $this->{Colors};
212+
$colours = $colours->clump(1..$this->{Colors}->ndims-1)->dice_axis(1,$this->{Faceidx}->flat)->splitdim(1,3) if $colours->ndims > 1;
212213
glShadeModel($shading == 1 ? GL_FLAT : GL_SMOOTH) if $shading;
213214
my $f = 'PDL::gl_triangles';
214215
$f .= '_wn' if $shading > 2;
@@ -254,33 +255,6 @@ sub PDL::Graphics::TriD::Contours::gdraw {
254255
}
255256
}
256257

257-
sub PDL::Graphics::TriD::Trigrid::gdraw {
258-
my($this,$points) = @_;
259-
my $faces = $points->dice_axis(1,$this->{Faceidx}->flat)->splitdim(1,3);
260-
my $colours = $this->{Colors}->dice_axis(1,$this->{Faceidx}->flat)->splitdim(1,3);
261-
my $options = $this->{Options};
262-
if ($options->{ShowNormals}) {
263-
die "No normals to show!" if !grep defined $this->{$_}, qw(FaceNormals VertexNormals);
264-
if (defined $this->{VertexNormals}) {
265-
my $arrows = $points->append($points + $this->{VertexNormals}*0.1)->splitdim(0,3)->clump(1,2);
266-
glDisable(GL_LIGHTING);
267-
my ($tv, $ti) = PDL::Graphics::OpenGLQ::gen_arrowheads($arrows,sequence(ulong,2,$points->dim(1))->t->dog,
268-
0.5, 0.2);
269-
PDL::gl_triangles($tv->dice_axis(1,$ti)->splitdim(1,3), [1,1,1]);
270-
PDL::gl_lines_col($arrows,[1,1,1]);
271-
}
272-
if (defined $this->{FaceNormals}) {
273-
my $facecentres = $faces->transpose->avgover;
274-
my $facearrows = $facecentres->append($facecentres + $this->{FaceNormals}*0.1)->splitdim(0,3)->clump(1,2);
275-
glDisable(GL_LIGHTING);
276-
my ($tv, $ti) = PDL::Graphics::OpenGLQ::gen_arrowheads($facearrows,sequence(ulong,2,$facecentres->dim(1))->t->dog,
277-
0.5, 0.2);
278-
PDL::gl_triangles($tv->dice_axis(1,$ti)->splitdim(1,3), [0.5,0.5,0.5]);
279-
PDL::gl_lines_col($facearrows,[0.5,0.5,0.5]);
280-
}
281-
}
282-
}
283-
284258
# A special construct which always faces the display and takes the entire window
285259
# The quick method is to use texturing for the good effect.
286260
sub PDL::Graphics::TriD::Image::gdraw {

lib/PDL/Graphics/TriD/Objects.pm

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ This provides the following class hierarchy:
1212
1313
PDL::Graphics::TriD::Object base class for containers
1414
├ PDL::Graphics::TriD::Arrows lines with arrowheads
15+
├ PDL::Graphics::TriD::Trigrid polygons
1516
└ PDL::Graphics::TriD::GObject (abstract) base class for drawables
1617
1718
PDL::Graphics::TriD::GObject (abstract) base class for drawables
1819
├ PDL::Graphics::TriD::Points individual points
1920
├ PDL::Graphics::TriD::Spheres fat 3D points :)
2021
├ PDL::Graphics::TriD::Lines separate lines
2122
├ PDL::Graphics::TriD::LineStrip continuous paths
22-
├ PDL::Graphics::TriD::Trigrid polygons
2323
├ PDL::Graphics::TriD::Triangles just polygons
2424
├ PDL::Graphics::TriD::Lattice colored lattice, maybe filled/shaded
2525
└ PDL::Graphics::TriD::Labels text labels
@@ -120,27 +120,39 @@ sub get_valid_options { +{
120120

121121
package PDL::Graphics::TriD::Trigrid;
122122
use PDL::Graphics::OpenGLQ;
123-
use base qw/PDL::Graphics::TriD::GObject/;
124-
use fields qw/Faceidx FaceNormals VertexNormals/;
123+
use base qw/PDL::Graphics::TriD::Object/;
125124
sub new {
126125
my $options = ref($_[-1]) eq 'HASH' ? pop : {};
127126
my ($type,$points,$faceidx,$colors) = @_;
128-
my $this = $type->SUPER::new($points,$colors,$options);
127+
my $this = $type->SUPER::new($options);
129128
# faceidx is 2D pdl of indices into points for each face
130-
$faceidx = $this->{Faceidx} = $faceidx->ulong;
129+
$faceidx = $faceidx->ulong;
131130
$options = $this->{Options};
132-
if ($options->{ShowNormals}) {
133-
my ($fn, $vn) = triangle_normals($this->{Points}, $faceidx);
134-
$this->{VertexNormals} = $vn if $options->{Smooth} or $options->{ShowNormals};
135-
$this->{FaceNormals} = $fn if !$options->{Smooth} or $options->{ShowNormals};
136-
}
137131
my %less = %$options; delete @less{qw(ShowNormals Lines)};
138132
$less{Shading} = 3 if $options->{Shading};
139133
$this->add_object(PDL::Graphics::TriD::Triangles->new($points, $faceidx->clump(1..$faceidx->ndims-1), $colors, \%less));
140-
if ($options->{Lines}) {
134+
if ($options->{Lines} or $options->{ShowNormals}) {
141135
$points = PDL::Graphics::TriD::realcoords($type->r_type,$points);
142-
my $faces = $points->dice_axis(1,$this->{Faceidx}->flat)->splitdim(1,3);
143-
$this->add_object(PDL::Graphics::TriD::Lines->new($faces->dice_axis(1,[0,1,2,0]), PDL::float(0,0,0)));
136+
my $faces = $points->dice_axis(1,$faceidx->flat)->splitdim(1,3);
137+
if ($options->{Lines}) {
138+
$this->add_object(PDL::Graphics::TriD::Lines->new($faces->dice_axis(1,[0,1,2,0]), PDL::float(0,0,0)));
139+
}
140+
if ($options->{ShowNormals}) {
141+
my ($fn, $vn) = triangle_normals($points, $faceidx);
142+
my $facecentres = $faces->transpose->avgover;
143+
my $facearrows = $facecentres->append($facecentres + $fn*0.1)->splitdim(0,3)->clump(1,2);
144+
my ($fromind, $toind) = PDL->sequence(PDL::ulong,2,$facecentres->dim(1))->t->dog;
145+
$this->add_object(PDL::Graphics::TriD::Arrows->new(
146+
$facearrows, PDL::float(0.5,0.5,0.5),
147+
{ From=>$fromind, To=>$toind, ArrowLen => 0.5, ArrowWidth => 0.2 },
148+
));
149+
my $vertarrows = $points->append($points + $vn*0.1)->splitdim(0,3)->clump(1,2);
150+
($fromind, $toind) = PDL->sequence(PDL::ulong,2,$points->dim(1))->t->dog;
151+
$this->add_object(PDL::Graphics::TriD::Arrows->new(
152+
$vertarrows, PDL::float(1,1,1),
153+
{ From=>$fromind, To=>$toind, ArrowLen => 0.5, ArrowWidth => 0.2 },
154+
));
155+
}
144156
}
145157
$this;
146158
}

0 commit comments

Comments
 (0)