<s> from opendr . camera import ProjectPoints <EOL> import unittest <EOL> visualize = False <EOL> class TestOcclusion ( unittest . TestCase ) : <EOL> def test_occlusion ( self ) : <EOL> if visualize : <EOL> import matplotlib . pyplot as plt <EOL> plt . ion ( ) <EOL> import chumpy as ch <EOL> import numpy as np <EOL> from opendr . renderer import TexturedRenderer , ColoredRenderer <EOL> rn = ColoredRenderer ( ) <EOL> from util_tests import get_earthmesh <EOL> m = get_earthmesh ( trans = ch . array ( [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:4> ] ) , rotation = ch . zeros ( <NUM_LIT:3> ) ) <EOL> rn . texture_image = m . texture_image <EOL> rn . ft = m . ft <EOL> rn . vt = m . vt <EOL> m . v [ : , <NUM_LIT:2> ] = np . mean ( m . v [ : , <NUM_LIT:2> ] ) <EOL> t0 = ch . array ( [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT> ] ) <EOL> t1 = ch . array ( [ - <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT> ] ) <EOL> v0 = ch . array ( m . v ) + t0 <EOL> if False : <EOL> v1 = ch . array ( m . v * <NUM_LIT> + np . array ( [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT> ] ) ) + t1 <EOL> else : <EOL> v1 = ch . array ( m . v ) + t1 <EOL> vc0 = v0 * <NUM_LIT:0> + np . array ( [ [ <NUM_LIT> , <NUM_LIT:0> , <NUM_LIT:0> ] ] ) <EOL> vc1 = v1 * <NUM_LIT:0> + np . array ( [ [ <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT:0> ] ] ) <EOL> vc = ch . vstack ( ( vc0 , vc1 ) ) <EOL> v = ch . vstack ( ( v0 , v1 ) ) <EOL> f = np . vstack ( ( m . f , m . f + len ( v0 ) ) ) <EOL> w , h = ( <NUM_LIT> , <NUM_LIT> ) <EOL> rn . camera = ProjectPoints ( v = v , rt = ch . zeros ( <NUM_LIT:3> ) , t = ch . zeros ( <NUM_LIT:3> ) , f = ch . array ( [ w , w ] ) / <NUM_LIT> , c = ch . array ( [ w , h ] ) / <NUM_LIT> , k = ch . zeros ( <NUM_LIT:5> ) ) <EOL> rn . camera . t = ch . array ( [ <NUM_LIT:0> , <NUM_LIT:0> , - <NUM_LIT> ] ) <EOL> rn . frustum = { '<STR_LIT>' : <NUM_LIT:1.> , '<STR_LIT>' : <NUM_LIT> , '<STR_LIT:width>' : w , '<STR_LIT>' : h } <EOL> m . vc = v . r * <NUM_LIT:0> + np . array ( [ [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ] ] ) <EOL> rn . set ( v = v , f = f , vc = vc ) <EOL> t0 [ : ] = np . array ( [ <NUM_LIT> , <NUM_LIT:0> , <NUM_LIT> - <NUM_LIT> ] ) <EOL> t1 [ : ] = np . array ( [ - <NUM_LIT> , <NUM_LIT:0> , <NUM_LIT> + <NUM_LIT> ] ) <EOL> target = rn . r <EOL> if visualize : <EOL> plt . figure ( ) <EOL> plt . imshow ( target ) <EOL> plt . title ( '<STR_LIT:target>' ) <EOL> plt . figure ( ) <EOL> plt . show ( ) <EOL> im_orig = rn . r . copy ( ) <EOL> from cvwrap import cv2 <EOL> tr = t0 <EOL> eps_emp = <NUM_LIT> <EOL> eps_pred = <NUM_LIT> <EOL> blur = lambda x : x <EOL> for tr in [ t0 , t1 ] : <EOL> if tr is t0 : <EOL> sum_limits = np . array ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) <EOL> else : <EOL> sum_limits = [ <NUM_LIT:1.> , <NUM_LIT> , <NUM_LIT> ] <EOL> if visualize : <EOL> plt . figure ( ) <EOL> for i in range ( <NUM_LIT:3> ) : <EOL> dr_pred = np . array ( rn . dr_wrt ( tr [ i ] ) . todense ( ) ) . reshape ( rn . shape ) * eps_pred <EOL> dr_pred = blur ( dr_pred ) <EOL> tr [ i ] = tr [ i ] . r + eps_emp / <NUM_LIT> <EOL> rn_greater = rn . r . copy ( ) <EOL> tr [ i ] = tr [ i ] . r - eps_emp / <NUM_LIT:1.> <EOL> rn_lesser = rn . r . copy ( ) <EOL> tr [ i ] = tr [ i ] . r + eps_emp / <NUM_LIT> <EOL> dr_emp = blur ( ( rn_greater - rn_lesser ) * eps_pred / eps_emp ) <EOL> dr_pred_shown = np . clip ( dr_pred , - <NUM_LIT> , <NUM_LIT> ) + <NUM_LIT> <EOL> dr_emp_shown = np . clip ( dr_emp , - <NUM_LIT> , <NUM_LIT> ) + <NUM_LIT> <EOL> if visualize : <EOL> plt . subplot ( <NUM_LIT:3> , <NUM_LIT:3> , i + <NUM_LIT:1> ) <EOL> plt . imshow ( dr_pred_shown ) <EOL> plt . title ( '<STR_LIT>' ) <EOL> plt . axis ( '<STR_LIT>' ) <EOL> plt . subplot ( <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:3> + i + <NUM_LIT:1> ) <EOL> plt . imshow ( dr_emp_shown ) <EOL> plt . title ( '<STR_LIT>' ) <EOL> plt . axis ( '<STR_LIT>' ) <EOL> plt . subplot ( <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:6> + i + <NUM_LIT:1> ) <EOL> diff = np . abs ( dr_emp - dr_pred ) <EOL> if visualize : <EOL> plt . imshow ( diff ) <EOL> diff = diff . ravel ( ) <EOL> if visualize : <EOL> plt . title ( '<STR_LIT>' % ( np . sum ( diff ) ) ) <EOL> plt . axis ( '<STR_LIT>' ) <EOL> self . assertTrue ( np . sum ( diff ) < sum_limits [ i ] ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> visualize = True <EOL> suite = unittest . TestLoader ( ) . loadTestsFromTestCase ( TestOcclusion ) <EOL> unittest . TextTestRunner ( verbosity = <NUM_LIT:2> ) . run ( suite ) <EOL> import pdb ; pdb . set_trace ( ) </s>
