Skip to content

Commit d10c6c3

Browse files
committed
PP vertex_normals for efficient vertex-normal calculation
1 parent be83d85 commit d10c6c3

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

lib/PDL/Graphics/OpenGLQ.pd

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ only for internal use - see source
2323

2424
=head1 AUTHOR
2525

26-
Copyright (C) 1997,1998 Tuomas J. Lukka.
26+
Copyright (C) 1997,1998 Tuomas J. Lukka.
2727
All rights reserved. There is no warranty. You are allowed
2828
to redistribute this software / documentation under certain
29-
conditions. For details, see the file COPYING in the PDL
30-
distribution. If this file is separated from the PDL distribution,
29+
conditions. For details, see the file COPYING in the PDL
30+
distribution. If this file is separated from the PDL distribution,
3131
the copyright notice should be included in the file.
3232

3333
=cut
@@ -188,7 +188,7 @@ pp_def('gl_arrows',
188188
float tmp2[3] = { 0.000001, -0.0001, 1 };
189189
broadcastloop %{
190190
PDL_Indx a = $indsa(), b = $indsb();
191-
float tmp1[3];
191+
float tmp1[3];
192192
float norm[3];
193193
float norm2[3];
194194
float normlen,origlen,norm2len;
@@ -276,4 +276,19 @@ pp_def('gl_arrows',
276276
@internal
277277
);
278278

279+
pp_def('vertex_normals',
280+
Pars => 'vertices(tri=3,v); facenormals(tri,f); uint faceidx(trivert=3,f); [o] vertnorm(tri,v)',
281+
GenericTypes => $F,
282+
Code => <<'EOF',
283+
loop(v,tri) %{ $vertnorm() = 0.0f; %}
284+
loop(f,trivert) %{
285+
PDL_ULong vertidx = $faceidx();
286+
if (vertidx >= $SIZE(v))
287+
$CROAK("faceidx number %"IND_FLAG",%"IND_FLAG" out of range: %"IND_FLAG" > %"IND_FLAG, f, trivert, vertidx, $SIZE(v)-1);
288+
loop(tri) %{ $vertnorm(v=>vertidx) += $facenormals(); %}
289+
%}
290+
EOF
291+
@internal,
292+
);
293+
279294
pp_done();

lib/PDL/Graphics/TriD/Objects.pm

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ sub get_valid_options { +{
136136
}}
137137

138138
package PDL::Graphics::TriD::Trigrid;
139+
use PDL::Graphics::OpenGLQ;
139140
use base qw/PDL::Graphics::TriD::GObject/;
140141
use fields qw/Faceidx FaceNormals Normals/;
141142
sub new {
@@ -155,17 +156,14 @@ sub get_valid_options { +{
155156
ShowNormals => 0,
156157
Lighting => 1,
157158
}}
158-
sub cdummies { # called with (type,colors,points)
159-
return $_[1]->dummy(1,$_[2]->getdim(1)); }
159+
sub cdummies { $_[1]->dummy(1,$_[2]->getdim(1)); }
160160
sub smoothn { my ($this, $faces) = @_;
161161
my ($points, $faceidx) = @$this{qw(Points Faceidx)};
162162
# faces is 3D pdl slices of points, giving cart coords of face verts
163163
my @p = $faces->mv(1,-1)->dog;
164164
my $fn = ($p[1]-$p[0])->crossp($p[2]-$p[1])->norm; # flat faces, >= 3 points
165165
$this->{FaceNormals} = $fn if $this->{Options}{ShowNormals};
166-
PDL::cat(
167-
map $fn->dice_axis(1,($faceidx==$_)->whichND->slice('(1)'))->mv(1,0)->sumover->norm,
168-
0..($points->dim(1)-1) );
166+
vertex_normals($this->{Points}, $fn, $faceidx)->norm;
169167
}
170168

171169
package PDL::Graphics::TriD::Lattice;

0 commit comments

Comments
 (0)