<s> import numpy as np <EOL> def compact ( vertices , indices , tolerance = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> n = len ( vertices ) <EOL> V = np . zeros ( n , dtype = [ ( "<STR_LIT>" , np . float32 , <NUM_LIT:3> ) ] ) <EOL> V [ "<STR_LIT>" ] [ : , <NUM_LIT:0> ] = vertices [ : , <NUM_LIT:0> ] <EOL> V [ "<STR_LIT>" ] [ : , <NUM_LIT:1> ] = vertices [ : , <NUM_LIT:1> ] <EOL> V [ "<STR_LIT>" ] [ : , <NUM_LIT:2> ] = vertices [ : , <NUM_LIT:2> ] <EOL> epsilon = <NUM_LIT> <EOL> decimals = int ( np . log ( epsilon ) / np . log ( <NUM_LIT:1> / <NUM_LIT> ) ) <EOL> V_ = np . zeros_like ( V ) <EOL> X = V [ "<STR_LIT>" ] [ : , <NUM_LIT:0> ] . round ( decimals = decimals ) <EOL> X [ np . where ( abs ( X ) < epsilon ) ] = <NUM_LIT:0> <EOL> V_ [ "<STR_LIT>" ] [ : , <NUM_LIT:0> ] = X <EOL> Y = V [ "<STR_LIT>" ] [ : , <NUM_LIT:1> ] . round ( decimals = decimals ) <EOL> Y [ np . where ( abs ( Y ) < epsilon ) ] = <NUM_LIT:0> <EOL> V_ [ "<STR_LIT>" ] [ : , <NUM_LIT:1> ] = Y <EOL> Z = V [ "<STR_LIT>" ] [ : , <NUM_LIT:2> ] . round ( decimals = decimals ) <EOL> Z [ np . where ( abs ( Z ) < epsilon ) ] = <NUM_LIT:0> <EOL> V_ [ "<STR_LIT>" ] [ : , <NUM_LIT:2> ] = Z <EOL> U , RI = np . unique ( V_ , return_inverse = True ) <EOL> indices = indices . ravel ( ) <EOL> I_ = indices . copy ( ) . ravel ( ) <EOL> for i in range ( len ( indices ) ) : <EOL> I_ [ i ] = RI [ indices [ i ] ] <EOL> I_ = I_ . reshape ( len ( indices ) / <NUM_LIT:3> , <NUM_LIT:3> ) <EOL> return U . view ( np . float32 ) . reshape ( len ( U ) , <NUM_LIT:3> ) , I_ , RI <EOL> def normals ( vertices , indices ) : <EOL> """<STR_LIT>""" <EOL> vertices , indices , mapping = compact ( vertices , indices ) <EOL> T = vertices [ indices ] <EOL> N = np . cross ( T [ : , <NUM_LIT:1> ] - T [ : , <NUM_LIT:0> ] , T [ : , <NUM_LIT:2> ] - T [ : , <NUM_LIT:0> ] ) <EOL> L = np . sqrt ( np . sum ( N * N , axis = <NUM_LIT:1> ) ) <EOL> L [ L == <NUM_LIT:0> ] = <NUM_LIT:1.0> <EOL> N /= L [ : , np . newaxis ] <EOL> normals = np . zeros_like ( vertices ) <EOL> normals [ indices [ : , <NUM_LIT:0> ] ] += N <EOL> normals [ indices [ : , <NUM_LIT:1> ] ] += N <EOL> normals [ indices [ : , <NUM_LIT:2> ] ] += N <EOL> L = np . sqrt ( np . sum ( normals * normals , axis = <NUM_LIT:1> ) ) <EOL> L [ L == <NUM_LIT:0> ] = <NUM_LIT:1.0> <EOL> normals /= L [ : , np . newaxis ] <EOL> return normals [ mapping ] </s>
