Skip to content

Commit ea2a3a5

Browse files
committed
change to triangle_normals that calculates both vertex and face
1 parent b71ed69 commit ea2a3a5

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

lib/PDL/Graphics/OpenGLQ.pd

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -276,16 +276,32 @@ 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)',
279+
pp_def('triangle_normals',
280+
Pars => 'vertices(tri=3,v); uint faceidx(trivert=3,f); [o] facenorm(tri,f); [o] vertnorm(tri,v)',
281281
GenericTypes => $F,
282282
Code => <<'EOF',
283283
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(); %}
284+
loop(f) %{
285+
float tmp0_1[3], tmp0_2[3], tmpcross[3], magn;
286+
PDL_ULong vertidx[3];
287+
loop(trivert) %{
288+
if ((vertidx[trivert] = $faceidx()) >= $SIZE(v))
289+
$CROAK("faceidx number %"IND_FLAG",%"IND_FLAG" out of range: %"IND_FLAG" > %"IND_FLAG, f, trivert, vertidx[trivert], $SIZE(v)-1);
290+
%}
291+
loop(tri) %{
292+
tmp0_1[tri] = $vertices(v => vertidx[1]) - $vertices(v => vertidx[0]);
293+
tmp0_2[tri] = $vertices(v => vertidx[2]) - $vertices(v => vertidx[0]);
294+
%}
295+
tmpcross[0] = tmp0_1[1]*tmp0_2[2] - tmp0_2[1]*tmp0_1[2];
296+
tmpcross[1] = -(tmp0_1[0]*tmp0_2[2] - tmp0_2[0]*tmp0_1[2]);
297+
tmpcross[2] = tmp0_1[0]*tmp0_2[1] - tmp0_2[0]*tmp0_1[1];
298+
magn = sqrtf(tmpcross[0]*tmpcross[0] + tmpcross[1]*tmpcross[1] + tmpcross[2]*tmpcross[2]);
299+
loop(tri) %{ $facenorm() = tmpcross[tri] / magn; %}
300+
loop(trivert,tri) %{ $vertnorm(v=>vertidx[trivert]) += $facenorm(); %}
301+
%}
302+
loop(v) %{
303+
float magn = sqrtf($vertnorm(tri=>0)*$vertnorm(tri=>0) + $vertnorm(tri=>1)*$vertnorm(tri=>1) + $vertnorm(tri=>2)*$vertnorm(tri=>2));
304+
loop(tri) %{ $vertnorm() /= magn; %}
289305
%}
290306
EOF
291307
@internal,

lib/PDL/Graphics/TriD/Objects.pm

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,9 @@ sub get_valid_options { +{
159159
sub cdummies { $_[1]->dummy(1,$_[2]->getdim(1)); }
160160
sub smoothn { my ($this, $faces) = @_;
161161
my ($points, $faceidx) = @$this{qw(Points Faceidx)};
162-
# faces is 3D pdl slices of points, giving cart coords of face verts
163-
my @p = $faces->mv(1,-1)->dog;
164-
my $fn = ($p[1]-$p[0])->crossp($p[2]-$p[1])->norm; # flat faces, >= 3 points
162+
my ($fn, $vn) = triangle_normals($this->{Points}, $faceidx);
165163
$this->{FaceNormals} = $fn if $this->{Options}{ShowNormals};
166-
vertex_normals($this->{Points}, $fn, $faceidx)->norm;
164+
$vn;
167165
}
168166

169167
package PDL::Graphics::TriD::Lattice;

0 commit comments

Comments
 (0)