<s> """<STR_LIT>""" <EOL> from . . gloo import Texture3D , TextureEmulated3D , VertexBuffer , IndexBuffer <EOL> from . import Visual <EOL> from . shaders import Function <EOL> from . . color import get_colormap <EOL> import numpy as np <EOL> VERT_SHADER = """<STR_LIT>""" <EOL> FRAG_SHADER = """<STR_LIT>""" <EOL> MIP_SNIPPETS = dict ( <EOL> before_loop = """<STR_LIT>""" , <EOL> in_loop = """<STR_LIT>""" , <EOL> after_loop = """<STR_LIT>""" , <EOL> ) <EOL> MIP_FRAG_SHADER = FRAG_SHADER . format ( ** MIP_SNIPPETS ) <EOL> TRANSLUCENT_SNIPPETS = dict ( <EOL> before_loop = """<STR_LIT>""" , <EOL> in_loop = """<STR_LIT>""" , <EOL> after_loop = """<STR_LIT>""" , <EOL> ) <EOL> TRANSLUCENT_FRAG_SHADER = FRAG_SHADER . format ( ** TRANSLUCENT_SNIPPETS ) <EOL> ADDITIVE_SNIPPETS = dict ( <EOL> before_loop = """<STR_LIT>""" , <EOL> in_loop = """<STR_LIT>""" , <EOL> after_loop = """<STR_LIT>""" , <EOL> ) <EOL> ADDITIVE_FRAG_SHADER = FRAG_SHADER . format ( ** ADDITIVE_SNIPPETS ) <EOL> ISO_SNIPPETS = dict ( <EOL> before_loop = """<STR_LIT>""" , <EOL> in_loop = """<STR_LIT>""" , <EOL> after_loop = """<STR_LIT:U+0020>""" , <EOL> ) <EOL> ISO_FRAG_SHADER = FRAG_SHADER . format ( ** ISO_SNIPPETS ) <EOL> frag_dict = { <EOL> '<STR_LIT>' : MIP_FRAG_SHADER , <EOL> '<STR_LIT>' : ISO_FRAG_SHADER , <EOL> '<STR_LIT>' : TRANSLUCENT_FRAG_SHADER , <EOL> '<STR_LIT>' : ADDITIVE_FRAG_SHADER , <EOL> } <EOL> class VolumeVisual ( Visual ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , vol , clim = None , method = '<STR_LIT>' , threshold = None , <EOL> relative_step_size = <NUM_LIT> , cmap = '<STR_LIT>' , <EOL> emulate_texture = False ) : <EOL> tex_cls = TextureEmulated3D if emulate_texture else Texture3D <EOL> self . _vol_shape = ( ) <EOL> self . _clim = None <EOL> self . _need_vertex_update = True <EOL> self . _cmap = get_colormap ( cmap ) <EOL> self . _vertices = VertexBuffer ( ) <EOL> self . _texcoord = VertexBuffer ( <EOL> np . array ( [ <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ] , <EOL> [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> ] , <EOL> [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] , <EOL> ] , dtype = np . float32 ) ) <EOL> self . _tex = tex_cls ( ( <NUM_LIT:10> , <NUM_LIT:10> , <NUM_LIT:10> ) , interpolation = '<STR_LIT>' , <EOL> wrapping = '<STR_LIT>' ) <EOL> Visual . __init__ ( self , vcode = VERT_SHADER , fcode = "<STR_LIT>" ) <EOL> self . shared_program [ '<STR_LIT>' ] = self . _tex <EOL> self . shared_program [ '<STR_LIT>' ] = self . _vertices <EOL> self . shared_program [ '<STR_LIT>' ] = self . _texcoord <EOL> self . _draw_mode = '<STR_LIT>' <EOL> self . _index_buffer = IndexBuffer ( ) <EOL> self . set_gl_state ( '<STR_LIT>' , cull_face = False ) <EOL> self . set_data ( vol , clim ) <EOL> self . method = method <EOL> self . relative_step_size = relative_step_size <EOL> self . threshold = threshold if ( threshold is not None ) else vol . mean ( ) <EOL> self . freeze ( ) <EOL> def set_data ( self , vol , clim = None ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( vol , np . ndarray ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if not ( ( vol . ndim == <NUM_LIT:3> ) or ( vol . ndim == <NUM_LIT:4> and vol . shape [ - <NUM_LIT:1> ] <= <NUM_LIT:4> ) ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if clim is not None : <EOL> clim = np . array ( clim , float ) <EOL> if not ( clim . ndim == <NUM_LIT:1> and clim . size == <NUM_LIT:2> ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> self . _clim = tuple ( clim ) <EOL> if self . _clim is None : <EOL> self . _clim = vol . min ( ) , vol . max ( ) <EOL> vol = np . array ( vol , dtype = '<STR_LIT>' , copy = False ) <EOL> if self . _clim [ <NUM_LIT:1> ] == self . _clim [ <NUM_LIT:0> ] : <EOL> if self . _clim [ <NUM_LIT:0> ] != <NUM_LIT:0.> : <EOL> vol *= <NUM_LIT:1.0> / self . _clim [ <NUM_LIT:0> ] <EOL> else : <EOL> vol -= self . _clim [ <NUM_LIT:0> ] <EOL> vol /= self . _clim [ <NUM_LIT:1> ] - self . _clim [ <NUM_LIT:0> ] <EOL> self . _tex . set_data ( vol ) <EOL> self . shared_program [ '<STR_LIT>' ] = ( vol . shape [ <NUM_LIT:2> ] , vol . shape [ <NUM_LIT:1> ] , <EOL> vol . shape [ <NUM_LIT:0> ] ) <EOL> shape = vol . shape [ : <NUM_LIT:3> ] <EOL> if self . _vol_shape != shape : <EOL> self . _vol_shape = shape <EOL> self . _need_vertex_update = True <EOL> self . _vol_shape = shape <EOL> self . _kb_for_texture = np . prod ( self . _vol_shape ) / <NUM_LIT> <EOL> @ property <EOL> def clim ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _clim <EOL> @ property <EOL> def cmap ( self ) : <EOL> return self . _cmap <EOL> @ cmap . setter <EOL> def cmap ( self , cmap ) : <EOL> self . _cmap = get_colormap ( cmap ) <EOL> self . shared_program . frag [ '<STR_LIT>' ] = Function ( self . _cmap . glsl_map ) <EOL> self . update ( ) <EOL> @ property <EOL> def method ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _method <EOL> @ method . setter <EOL> def method ( self , method ) : <EOL> known_methods = list ( frag_dict . keys ( ) ) <EOL> if method not in known_methods : <EOL> raise ValueError ( '<STR_LIT>' % <EOL> ( known_methods , method ) ) <EOL> self . _method = method <EOL> if '<STR_LIT>' in self . shared_program : <EOL> self . shared_program [ '<STR_LIT>' ] = None <EOL> self . shared_program . frag = frag_dict [ method ] <EOL> self . shared_program . frag [ '<STR_LIT>' ] = self . _tex . glsl_sampler_type <EOL> self . shared_program . frag [ '<STR_LIT>' ] = self . _tex . glsl_sample <EOL> self . shared_program . frag [ '<STR_LIT>' ] = Function ( self . _cmap . glsl_map ) <EOL> self . update ( ) <EOL> @ property <EOL> def threshold ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _threshold <EOL> @ threshold . setter <EOL> def threshold ( self , value ) : <EOL> self . _threshold = float ( value ) <EOL> if '<STR_LIT>' in self . shared_program : <EOL> self . shared_program [ '<STR_LIT>' ] = self . _threshold <EOL> self . update ( ) <EOL> @ property <EOL> def relative_step_size ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _relative_step_size <EOL> @ relative_step_size . setter <EOL> def relative_step_size ( self , value ) : <EOL> value = float ( value ) <EOL> if value < <NUM_LIT:0.1> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> self . _relative_step_size = value <EOL> self . shared_program [ '<STR_LIT>' ] = value <EOL> def _create_vertex_data ( self ) : <EOL> """<STR_LIT>""" <EOL> shape = self . _vol_shape <EOL> x0 , x1 = - <NUM_LIT:0.5> , shape [ <NUM_LIT:2> ] - <NUM_LIT:0.5> <EOL> y0 , y1 = - <NUM_LIT:0.5> , shape [ <NUM_LIT:1> ] - <NUM_LIT:0.5> <EOL> z0 , z1 = - <NUM_LIT:0.5> , shape [ <NUM_LIT:0> ] - <NUM_LIT:0.5> <EOL> pos = np . array ( [ <EOL> [ x0 , y0 , z0 ] , <EOL> [ x1 , y0 , z0 ] , <EOL> [ x0 , y1 , z0 ] , <EOL> [ x1 , y1 , z0 ] , <EOL> [ x0 , y0 , z1 ] , <EOL> [ x1 , y0 , z1 ] , <EOL> [ x0 , y1 , z1 ] , <EOL> [ x1 , y1 , z1 ] , <EOL> ] , dtype = np . float32 ) <EOL> """<STR_LIT>""" <EOL> indices = np . array ( [ <NUM_LIT:2> , <NUM_LIT:6> , <NUM_LIT:0> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:7> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:5> , <NUM_LIT:3> , <NUM_LIT:7> ] , <EOL> dtype = np . uint32 ) <EOL> self . _vertices . set_data ( pos ) <EOL> self . _index_buffer . set_data ( indices ) <EOL> def _compute_bounds ( self , axis , view ) : <EOL> return <NUM_LIT:0> , self . _vol_shape [ axis ] <EOL> def _prepare_transforms ( self , view ) : <EOL> trs = view . transforms <EOL> view . view_program . vert [ '<STR_LIT>' ] = trs . get_transform ( ) <EOL> view_tr_f = trs . get_transform ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> view_tr_i = view_tr_f . inverse <EOL> view . view_program . vert [ '<STR_LIT>' ] = view_tr_f <EOL> view . view_program . vert [ '<STR_LIT>' ] = view_tr_i <EOL> def _prepare_draw ( self , view ) : <EOL> if self . _need_vertex_update : <EOL> self . _create_vertex_data ( ) </s>
