@@ -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',
283283loop(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%}
290306EOF
291307 @internal,
0 commit comments