Skip to content

Instantly share code, notes, and snippets.

@wchristian
Last active December 29, 2015 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wchristian/258ed61440dffa5bb952 to your computer and use it in GitHub Desktop.
Save wchristian/258ed61440dffa5bb952 to your computer and use it in GitHub Desktop.
use strict;
use warnings;
use PDL;
use PDL::Matrix 'mpdl';
use PDL::Core 'howbig';
my $pIndices = pdl( #
0, 3, 1,
1, 3, 2,
2, 3, 0,
1, 2, 0,
)->long;
my $pVertices = mpdl( #
[ -1, -1, 0.5773, 0, 0, 0, 0, 0 ],
[ 0, -1, -1.15475, 0.5, 0, 0, 0, 0 ],
[ 1, -1, 0.5773, 1, 0, 0, 0, 0 ],
[ 0, 1, 0, 0.5, 1, 0, 0, 0 ],
)->float;
# Accumulate each triangle normal into each of the triangle vertices
for ( my $i = 0 ; $i < $pIndices->nelem ; $i += 3 ) {
my $Index0 = $pIndices->at( $i );
my $Index1 = $pIndices->at( $i + 1 );
my $Index2 = $pIndices->at( $i + 2 );
my $v1 = $pVertices->slice( "($Index1),0:2" ) - $pVertices->slice( "($Index0),0:2" );
my $v2 = $pVertices->slice( "($Index2),0:2" ) - $pVertices->slice( "($Index0),0:2" );
my $Normal = $v1->crossp( $v2 );
$Normal = $Normal->norm;
print $Normal;
$pVertices->slice( "($Index0),5:7" ) += $Normal;
$pVertices->slice( "($Index1),5:7" ) += $Normal;
$pVertices->slice( "($Index2),5:7" ) += $Normal;
}
# Normalize all the vertex normals
for my $i ( 0 .. $pVertices->dim( 0 ) - 1 ) {
$pVertices->slice( "($i),5:7" ) = $pVertices->slice( "($i),5:7" )->norm;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment