<s> """<STR_LIT>""" <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> import os , sys , logging <EOL> import numpy as np <EOL> import scipy . sparse as sp <EOL> import scipy <EOL> from chumpy . utils import row , col <EOL> import chumpy as ch <EOL> from chumpy . ch import Ch <EOL> from geometry import VertNormals <EOL> from chumpy import multiply , maximum <EOL> logger = logging . getLogger ( __name__ ) <EOL> def real_sh_coeff ( xyz_samples ) : <EOL> d_sqrt_pi = <NUM_LIT:2> * np . sqrt ( np . pi ) <EOL> real_coeff = np . zeros ( ( len ( xyz_samples ) , <NUM_LIT:9> ) ) <EOL> real_coeff [ : , <NUM_LIT:0> ] = <NUM_LIT:1> / d_sqrt_pi <EOL> real_coeff [ : , <NUM_LIT:1> ] = ( np . sqrt ( <NUM_LIT:3> ) / d_sqrt_pi ) * xyz_samples [ : , <NUM_LIT:0> ] <EOL> real_coeff [ : , <NUM_LIT:2> ] = ( np . sqrt ( <NUM_LIT:3> ) / d_sqrt_pi ) * xyz_samples [ : , <NUM_LIT:2> ] <EOL> real_coeff [ : , <NUM_LIT:3> ] = ( np . sqrt ( <NUM_LIT:3> ) / d_sqrt_pi ) * xyz_samples [ : , <NUM_LIT:1> ] <EOL> real_coeff [ : , <NUM_LIT:4> ] = ( np . sqrt ( <NUM_LIT:15> ) / d_sqrt_pi ) * xyz_samples [ : , <NUM_LIT:0> ] * xyz_samples [ : , <NUM_LIT:1> ] <EOL> real_coeff [ : , <NUM_LIT:5> ] = ( np . sqrt ( <NUM_LIT:15> ) / d_sqrt_pi ) * xyz_samples [ : , <NUM_LIT:1> ] * xyz_samples [ : , <NUM_LIT:2> ] <EOL> real_coeff [ : , <NUM_LIT:6> ] = ( np . sqrt ( <NUM_LIT:5> ) / ( <NUM_LIT:2> * d_sqrt_pi ) ) * ( <NUM_LIT:3> * xyz_samples [ : , <NUM_LIT:2> ] ** <NUM_LIT:2> - <NUM_LIT:1> ) <EOL> real_coeff [ : , <NUM_LIT:7> ] = ( np . sqrt ( <NUM_LIT:15> ) / d_sqrt_pi ) * xyz_samples [ : , <NUM_LIT:0> ] * xyz_samples [ : , <NUM_LIT:2> ] <EOL> real_coeff [ : , <NUM_LIT:8> ] = ( np . sqrt ( <NUM_LIT:15> ) / ( <NUM_LIT:2> * d_sqrt_pi ) ) * ( xyz_samples [ : , <NUM_LIT:0> ] ** <NUM_LIT:2> - xyz_samples [ : , <NUM_LIT:1> ] ** <NUM_LIT:2> ) <EOL> return real_coeff <EOL> class SphericalHarmonics ( Ch ) : <EOL> dterms = '<STR_LIT>' , '<STR_LIT>' <EOL> terms = [ '<STR_LIT>' ] <EOL> d_sqrt_pi = <NUM_LIT:2> * np . sqrt ( np . pi ) <EOL> K = np . array ( [ <EOL> <NUM_LIT:1.> / d_sqrt_pi , <EOL> np . sqrt ( <NUM_LIT:3> ) / d_sqrt_pi , <EOL> np . sqrt ( <NUM_LIT:3> ) / d_sqrt_pi , <EOL> np . sqrt ( <NUM_LIT:3> ) / d_sqrt_pi , <EOL> np . sqrt ( <NUM_LIT:15> ) / d_sqrt_pi , <EOL> np . sqrt ( <NUM_LIT:15> ) / d_sqrt_pi , <EOL> np . sqrt ( <NUM_LIT:5> ) / ( <NUM_LIT:2> * d_sqrt_pi ) , <EOL> np . sqrt ( <NUM_LIT:15> ) / d_sqrt_pi , <EOL> np . sqrt ( <NUM_LIT:15> ) / ( <NUM_LIT:2> * d_sqrt_pi ) ] ) <EOL> @ property <EOL> def num_channels ( self ) : <EOL> return self . light_color . size <EOL> def on_changed ( self , which ) : <EOL> if '<STR_LIT>' in which : <EOL> vn = self . vn . r . reshape ( ( - <NUM_LIT:1> , <NUM_LIT:3> ) ) <EOL> self . theta = np . arccos ( vn [ : , <NUM_LIT:2> ] ) <EOL> self . phi = np . arctan2 ( vn [ : , <NUM_LIT:1> ] , vn [ : , <NUM_LIT:0> ] ) <EOL> self . sh_coeffs = real_sh_coeff ( vn ) <EOL> self . num_verts = self . sh_coeffs . shape [ <NUM_LIT:0> ] <EOL> if '<STR_LIT>' in which or self . mtx . shape [ <NUM_LIT:1> ] != self . num_verts : <EOL> nc = self . num_channels <EOL> IS = np . arange ( self . num_verts * nc ) <EOL> JS = np . repeat ( np . arange ( self . num_verts ) , nc ) <EOL> data = ( row ( self . light_color ) * np . ones ( ( self . num_verts , nc ) ) ) . ravel ( ) <EOL> self . mtx = sp . csc_matrix ( ( data , ( IS , JS ) ) , shape = ( self . num_verts * nc , self . num_verts ) ) <EOL> def compute_r ( self ) : <EOL> comps = self . components . r <EOL> n = len ( comps ) <EOL> result = self . mtx . dot ( self . sh_coeffs [ : , : n ] . dot ( col ( self . components . r ) ) ) <EOL> result [ result < <NUM_LIT:0> ] = <NUM_LIT:0> <EOL> return result . reshape ( ( - <NUM_LIT:1> , self . num_channels ) ) <EOL> def compute_dr_wrt ( self , wrt ) : <EOL> comps = np . zeros ( <NUM_LIT:9> ) <EOL> comps [ : len ( self . components . r ) ] = self . components . r <EOL> comps = comps * self . K . ravel ( ) <EOL> if wrt is self . vn : <EOL> vn = self . vn . r . reshape ( ( - <NUM_LIT:1> , <NUM_LIT:3> ) ) <EOL> VS0 = np . ones ( self . sh_coeffs . shape [ <NUM_LIT:0> ] ) * comps [ <NUM_LIT:1> ] <EOL> VS1 = np . ones ( self . sh_coeffs . shape [ <NUM_LIT:0> ] ) * comps [ <NUM_LIT:3> ] <EOL> VS2 = np . ones ( self . sh_coeffs . shape [ <NUM_LIT:0> ] ) * comps [ <NUM_LIT:2> ] <EOL> VS0 += vn [ : , <NUM_LIT:1> ] * comps [ <NUM_LIT:4> ] <EOL> VS1 += vn [ : , <NUM_LIT:0> ] * comps [ <NUM_LIT:4> ] <EOL> VS1 += vn [ : , <NUM_LIT:2> ] * comps [ <NUM_LIT:5> ] <EOL> VS2 += vn [ : , <NUM_LIT:1> ] * comps [ <NUM_LIT:5> ] <EOL> VS2 += <NUM_LIT:6> * vn [ : , <NUM_LIT:2> ] * comps [ <NUM_LIT:6> ] <EOL> VS0 += vn [ : , <NUM_LIT:2> ] * comps [ <NUM_LIT:7> ] <EOL> VS2 += vn [ : , <NUM_LIT:0> ] * comps [ <NUM_LIT:7> ] <EOL> VS0 += <NUM_LIT> * vn [ : , <NUM_LIT:0> ] * comps [ <NUM_LIT:8> ] <EOL> VS1 -= <NUM_LIT> * vn [ : , <NUM_LIT:1> ] * comps [ <NUM_LIT:8> ] <EOL> rng = np . arange ( self . sh_coeffs . shape [ <NUM_LIT:0> ] ) <EOL> IS = np . concatenate ( ( rng , rng , rng ) ) <EOL> JS = np . concatenate ( ( rng * <NUM_LIT:3> , rng * <NUM_LIT:3> + <NUM_LIT:1> , rng * <NUM_LIT:3> + <NUM_LIT:2> ) ) <EOL> data = np . concatenate ( ( VS0 , VS1 , VS2 ) ) <EOL> result = self . mtx . dot ( sp . csc_matrix ( ( data , ( IS , JS ) ) ) ) <EOL> elif wrt is self . components : <EOL> comps = self . components . r <EOL> n = len ( comps ) <EOL> result = self . mtx . dot ( self . sh_coeffs [ : , : n ] ) <EOL> else : <EOL> return None <EOL> which = np . nonzero ( self . r . ravel ( ) > <NUM_LIT:0> ) [ <NUM_LIT:0> ] <EOL> data = np . ones_like ( which ) <EOL> gr_equal_zero = sp . csc_matrix ( ( data , ( which , which ) ) , shape = ( self . r . size , self . r . size ) ) <EOL> return gr_equal_zero . dot ( result ) <EOL> def lambertian_spotlight ( v , vn , pos , dir , spot_exponent , camcoord = False , camera_t = None , camera_rt = None ) : <EOL> """<STR_LIT>""" <EOL> if camcoord : <EOL> assert ( camera_t is not None and camera_rt is not None ) <EOL> from opendr . geometry import Rodrigues <EOL> rot = Rodrigues ( rt = camera_rt ) <EOL> pos = rot . T . dot ( pos - camera_t ) <EOL> dir = rot . T . dot ( dir ) <EOL> dir = dir / ch . sqrt ( ch . sum ( dir ** <NUM_LIT> ) ) <EOL> v_minus_light = v - pos . reshape ( ( <NUM_LIT:1> , <NUM_LIT:3> ) ) <EOL> v_distances = ch . sqrt ( ch . sum ( v_minus_light ** <NUM_LIT:2> , axis = <NUM_LIT:1> ) ) <EOL> v_minus_light_normed = v_minus_light / v_distances . reshape ( ( - <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> cosangle = v_minus_light_normed . dot ( dir . reshape ( ( <NUM_LIT:3> , <NUM_LIT:1> ) ) ) <EOL> light_dot_normal = ch . sum ( vn * v_minus_light_normed , axis = <NUM_LIT:1> ) <EOL> light_dot_normal . label = '<STR_LIT>' <EOL> cosangle . label = '<STR_LIT>' <EOL> result = light_dot_normal . ravel ( ) * cosangle . ravel ( ) ** spot_exponent <EOL> result = result / v_distances ** <NUM_LIT> <EOL> result = maximum ( result , <NUM_LIT:0.0> ) <EOL> return result <EOL> class LambertianPointLight ( Ch ) : <EOL> terms = '<STR_LIT:f>' , '<STR_LIT>' , '<STR_LIT>' <EOL> dterms = '<STR_LIT>' , '<STR_LIT:v>' , '<STR_LIT>' , '<STR_LIT>' <EOL> def on_changed ( self , which ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . add_dterm ( '<STR_LIT>' , maximum ( multiply ( a = multiply ( ) ) , <NUM_LIT:0.0> ) ) <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . ldn = LightDotNormal ( self . v . r . size / <NUM_LIT:3> ) <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> logger . info ( '<STR_LIT>' ) <EOL> self . vn = VertNormals ( f = self . f , v = self . v ) <EOL> self . vn . needs_autoupdate = True <EOL> if '<STR_LIT:v>' in which and hasattr ( self . vn , '<STR_LIT>' ) and self . vn . needs_autoupdate : <EOL> self . vn . v = self . v <EOL> ldn_args = { k : getattr ( self , k ) for k in which if k in ( '<STR_LIT>' , '<STR_LIT:v>' , '<STR_LIT>' ) } <EOL> if len ( ldn_args ) > <NUM_LIT:0> : <EOL> self . ldn . set ( ** ldn_args ) <EOL> self . _lpl . a . a . a = self . ldn . reshape ( ( - <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> if '<STR_LIT>' in which or '<STR_LIT>' in which : <EOL> self . _lpl . a . a . b = self . light_color . reshape ( ( <NUM_LIT:1> , self . num_channels ) ) <EOL> if '<STR_LIT>' in which : <EOL> self . _lpl . a . b = self . vc . reshape ( ( - <NUM_LIT:1> , self . num_channels ) ) <EOL> @ property <EOL> def num_channels ( self ) : <EOL> return self . light_color . size <EOL> def compute_r ( self ) : <EOL> return self . _lpl . r <EOL> def compute_dr_wrt ( self , wrt ) : <EOL> if wrt is self . _lpl : <EOL> return <NUM_LIT:1> <EOL> def LightDotNormal ( num_verts ) : <EOL> normalize_rows = lambda v : v / col ( ch . sqrt ( ch . sum ( v . reshape ( ( - <NUM_LIT:1> , <NUM_LIT:3> ) ) ** <NUM_LIT:2> , axis = <NUM_LIT:1> ) ) ) <EOL> sum_rows = lambda v : ch . sum ( v . reshape ( ( - <NUM_LIT:1> , <NUM_LIT:3> ) ) , axis = <NUM_LIT:1> ) <EOL> return Ch ( lambda light_pos , v , vn : <EOL> sum_rows ( normalize_rows ( light_pos . reshape ( ( <NUM_LIT:1> , <NUM_LIT:3> ) ) - v . reshape ( ( - <NUM_LIT:1> , <NUM_LIT:3> ) ) ) * vn . reshape ( ( - <NUM_LIT:1> , <NUM_LIT:3> ) ) ) ) <EOL> def main ( ) : <EOL> pass <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
