Skip to content

Commit 7f36540

Browse files
committed
replace gl_arrows with gl_triangles(gen_arrowheads)
1 parent d197a93 commit 7f36540

File tree

2 files changed

+54
-62
lines changed

2 files changed

+54
-62
lines changed

lib/PDL/Graphics/OpenGLQ.pd

Lines changed: 39 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -162,71 +162,58 @@ glEnd();',
162162
);
163163
}
164164

165-
pp_def('gl_arrows',
166-
Pars => 'coords(tri=3,n); indx indsa(); indx indsb();',
167-
OtherPars => 'float headlen; float width;',
168-
Code => <<'EOF',
165+
pp_def('gen_arrowheads',
166+
Pars => 'coords(tri=3,v); uint indsa(n); uint indsb(n); [o] trivertices(tri,nv=CALC(5*$SIZE(n))); uint [o] triindices(ni=CALC(12*$SIZE(n)))',
167+
OtherPars => 'float headlen; float width;',
168+
GenericTypes => $F,
169+
Code => <<'EOF',
169170
float hl = $COMP(headlen);
170171
float w = $COMP(width);
172+
if (w == 0)
173+
$CROAK("width invalid: cannot be 0");
171174
float tmp2[3] = { 0.000001, -0.0001, 1 };
172175
broadcastloop %{
173-
PDL_Indx a = $indsa(), b = $indsb();
174-
float tmp1[3];
175-
float norm[3];
176-
float norm2[3];
177-
float partback[3];
176+
loop(n) %{
177+
PDL_ULong a = $indsa(), b = $indsb(), nvbase = n * 5, nibase = n * 12;
178+
if (a >= $SIZE(v))
179+
$CROAK("indsa number %"IND_FLAG" out of range: %"IND_FLAG" > %"IND_FLAG, n, a, $SIZE(v)-1);
180+
if (b >= $SIZE(v))
181+
$CROAK("indsb number %"IND_FLAG" out of range: %"IND_FLAG" > %"IND_FLAG, n, b, $SIZE(v)-1);
182+
float tmp1[3], norm[3], norm2[3], partback[3];
178183
loop(tri) %{
179-
tmp1[tri] = $coords(n => a) - $coords(n => b);
180-
partback[tri] = $coords(n => b) + hl*tmp1[tri];
184+
tmp1[tri] = $coords(v => a) - $coords(v => b);
185+
partback[tri] = $coords(v => b) + hl*tmp1[tri];
181186
%}
182187
PDL_3D_CROSS(norm, tmp1, tmp2);
183188
PDL_3D_CROSS(norm2, tmp1, norm);
184189
PDL_3D_SETVECLEN(norm, w);
185190
PDL_3D_SETVECLEN(norm2, w);
186191
PDL_3D_SETVECLEN(tmp1, 1);
187-
if(w!=0) {
188-
glBegin(GL_TRIANGLES);
189-
glVertex3d( $coords(tri => 0, n => b) ,
190-
$coords(tri => 1, n => b) ,
191-
$coords(tri => 2, n => b) );
192-
glVertex3d( partback[0] + norm[0],
193-
partback[1] + norm[1],
194-
partback[2] + norm[2]);
195-
glVertex3d( partback[0] + norm2[0],
196-
partback[1] + norm2[1],
197-
partback[2] + norm2[2]);
198-
glVertex3d( $coords(tri => 0, n => b) ,
199-
$coords(tri => 1, n => b) ,
200-
$coords(tri => 2, n => b) );
201-
glVertex3d( partback[0] - norm[0],
202-
partback[1] - norm[1],
203-
partback[2] - norm[2]);
204-
glVertex3d( partback[0] - norm2[0],
205-
partback[1] - norm2[1],
206-
partback[2] - norm2[2]);
207-
glVertex3d( $coords(tri => 0, n => b) ,
208-
$coords(tri => 1, n => b) ,
209-
$coords(tri => 2, n => b) );
210-
glVertex3d( partback[0] + norm2[0],
211-
partback[1] + norm2[1],
212-
partback[2] + norm2[2]);
213-
glVertex3d( partback[0] - norm[0],
214-
partback[1] - norm[1],
215-
partback[2] - norm[2]);
216-
glVertex3d( $coords(tri => 0, n => b) ,
217-
$coords(tri => 1, n => b) ,
218-
$coords(tri => 2, n => b) );
219-
glVertex3d( partback[0] - norm2[0],
220-
partback[1] - norm2[1],
221-
partback[2] - norm2[2]);
222-
glVertex3d( partback[0] + norm[0],
223-
partback[1] + norm[1],
224-
partback[2] + norm[2]);
225-
glEnd();
226-
}
192+
loop(tri) %{ $trivertices(nv=>nvbase+0) = $coords(v => b); %}
193+
loop(tri) %{ $trivertices(nv=>nvbase+1) = partback[tri] + norm[tri]; %}
194+
loop(tri) %{ $trivertices(nv=>nvbase+2) = partback[tri] + norm2[tri]; %}
195+
loop(tri) %{ $trivertices(nv=>nvbase+3) = partback[tri] - norm[tri]; %}
196+
loop(tri) %{ $trivertices(nv=>nvbase+4) = partback[tri] - norm2[tri]; %}
197+
$triindices(ni=>nibase+0) = nvbase+0;
198+
$triindices(ni=>nibase+1) = nvbase+1;
199+
$triindices(ni=>nibase+2) = nvbase+2;
200+
$triindices(ni=>nibase+3) = nvbase+0;
201+
$triindices(ni=>nibase+4) = nvbase+3;
202+
$triindices(ni=>nibase+5) = nvbase+4;
203+
$triindices(ni=>nibase+6) = nvbase+0;
204+
$triindices(ni=>nibase+7) = nvbase+2;
205+
$triindices(ni=>nibase+8) = nvbase+3;
206+
$triindices(ni=>nibase+9) = nvbase+0;
207+
$triindices(ni=>nibase+10) = nvbase+4;
208+
$triindices(ni=>nibase+11) = nvbase+1;
209+
%}
227210
%}
228211
EOF
229-
@internal
212+
Doc => <<'EOF',
213+
=for ref
214+
215+
Generate vertices and indices, for triangles, implementing arrowheads.
216+
EOF
230217
);
231218

232219
pp_def('triangle_normals',

lib/PDL/Graphics/TriD/GL.pm

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -183,16 +183,18 @@ sub PDL::Graphics::TriD::Lattice::gdraw {
183183
my $points_clumped = $points->clump(1..$points->ndims-1);
184184
my $arrows = $points_clumped->append($points_clumped + $this->{VertexNormals}*0.1)->splitdim(0,3);
185185
glDisable(GL_LIGHTING);
186-
glColor3d(1,1,1);
187-
PDL::Graphics::OpenGLQ::gl_arrows($arrows, 0, 1, 0.5, 0.02);
186+
my ($tv, $ti) = PDL::Graphics::OpenGLQ::gen_arrowheads($arrows,0,1,
187+
0.5, 0.02);
188+
PDL::gl_triangles($tv->clump(1,2)->dice_axis(1,($ti+$ti->yvals*$tv->dim(1))->flat)->splitdim(1,3), [1,1,1]);
188189
PDL::gl_lines_col($arrows,[1,1,1]);
189190
}
190191
if (defined $this->{FaceNormals}) {
191192
my $facecentres = $faces->transpose->avgover;
192193
my $facearrows = $facecentres->append($facecentres + $this->{FaceNormals}*0.1)->splitdim(0,3);
193194
glDisable(GL_LIGHTING);
194-
glColor3d(0.5,0.5,0.5);
195-
PDL::Graphics::OpenGLQ::gl_arrows($facearrows, 0, 1, 0.5, 0.02);
195+
my ($tv, $ti) = PDL::Graphics::OpenGLQ::gen_arrowheads($facearrows,0,1,
196+
0.5, 0.02);
197+
PDL::gl_triangles($tv->clump(1,2)->dice_axis(1,($ti+$ti->yvals*$tv->dim(1))->flat)->splitdim(1,3), [0.5,0.5,0.5]);
196198
PDL::gl_lines_col($facearrows,[0.5,0.5,0.5]);
197199
}
198200
}
@@ -203,8 +205,9 @@ sub PDL::Graphics::TriD::Arrows::gdraw {
203205
my $opts = $this->{Options};
204206
glColor3d(@{$opts->{Color}});
205207
my ($from, $to) = @$opts{qw(From To)};
206-
PDL::Graphics::OpenGLQ::gl_arrows($points,$from,$to,@$opts{qw(
207-
ArrowLen ArrowWidth)});
208+
my ($tv, $ti) = PDL::Graphics::OpenGLQ::gen_arrowheads($points,$from,$to,
209+
@$opts{qw(ArrowLen ArrowWidth)});
210+
PDL::gl_triangles($tv->dice_axis(1,$ti)->splitdim(1,3), $opts->{Color});
208211
my $lines = $points->dice_axis(1,$from)->append($points->dice_axis(1,$to))->splitdim(0,3);
209212
PDL::gl_lines_col($lines,$opts->{Color});
210213
}
@@ -262,16 +265,18 @@ sub PDL::Graphics::TriD::Trigrid::gdraw {
262265
if (defined $this->{VertexNormals}) {
263266
my $arrows = $points->append($points + $this->{VertexNormals}*0.1)->splitdim(0,3);
264267
glDisable(GL_LIGHTING);
265-
glColor3d(1,1,1);
266-
PDL::Graphics::OpenGLQ::gl_arrows($arrows, 0, 1, 0.5, 0.02);
268+
my ($tv, $ti) = PDL::Graphics::OpenGLQ::gen_arrowheads($arrows,0,1,
269+
0.5, 0.02);
270+
PDL::gl_triangles($tv->clump(1,2)->dice_axis(1,($ti+$ti->yvals*$tv->dim(1))->flat)->splitdim(1,3), [1,1,1]);
267271
PDL::gl_lines_col($arrows,[1,1,1]);
268272
}
269273
if (defined $this->{FaceNormals}) {
270274
my $facecentres = $faces->transpose->avgover;
271275
my $facearrows = $facecentres->append($facecentres + $this->{FaceNormals}*0.1)->splitdim(0,3);
272276
glDisable(GL_LIGHTING);
273-
glColor3d(0.5,0.5,0.5);
274-
PDL::Graphics::OpenGLQ::gl_arrows($facearrows, 0, 1, 0.5, 0.02);
277+
my ($tv, $ti) = PDL::Graphics::OpenGLQ::gen_arrowheads($facearrows,0,1,
278+
0.5, 0.02);
279+
PDL::gl_triangles($tv->clump(1,2)->dice_axis(1,($ti+$ti->yvals*$tv->dim(1))->flat)->splitdim(1,3), [0.5,0.5,0.5]);
275280
PDL::gl_lines_col($facearrows,[0.5,0.5,0.5]);
276281
}
277282
}

0 commit comments

Comments
 (0)