<s> from __future__ import division <EOL> import numpy as np <EOL> from ... util import transforms <EOL> from ... geometry import Rect <EOL> from . _util import arg_to_vec4 , as_vec4 <EOL> from . base_transform import BaseTransform <EOL> class NullTransform ( BaseTransform ) : <EOL> """<STR_LIT>""" <EOL> glsl_map = "<STR_LIT>" <EOL> glsl_imap = "<STR_LIT>" <EOL> Linear = True <EOL> Orthogonal = True <EOL> NonScaling = True <EOL> Isometric = True <EOL> @ arg_to_vec4 <EOL> def map ( self , coords ) : <EOL> """<STR_LIT>""" <EOL> return coords <EOL> def imap ( self , coords ) : <EOL> """<STR_LIT>""" <EOL> return coords <EOL> def __mul__ ( self , tr ) : <EOL> return tr <EOL> def __rmul__ ( self , tr ) : <EOL> return tr <EOL> class STTransform ( BaseTransform ) : <EOL> """<STR_LIT>""" <EOL> glsl_map = """<STR_LIT>""" <EOL> glsl_imap = """<STR_LIT>""" <EOL> Linear = True <EOL> Orthogonal = True <EOL> NonScaling = False <EOL> Isometric = False <EOL> def __init__ ( self , scale = None , translate = None ) : <EOL> super ( STTransform , self ) . __init__ ( ) <EOL> self . _scale = np . ones ( <NUM_LIT:4> , dtype = np . float32 ) <EOL> self . _translate = np . zeros ( <NUM_LIT:4> , dtype = np . float32 ) <EOL> s = ( ( <NUM_LIT:1.0> , <NUM_LIT:1.0> , <NUM_LIT:1.0> , <NUM_LIT:1.0> ) if scale is None else <EOL> as_vec4 ( scale , default = ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ) ) ) <EOL> t = ( ( <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> ) if translate is None else <EOL> as_vec4 ( translate , default = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) ) ) <EOL> self . _set_st ( s , t ) <EOL> self . _update_shaders ( ) <EOL> @ arg_to_vec4 <EOL> def map ( self , coords ) : <EOL> """<STR_LIT>""" <EOL> m = np . empty ( coords . shape ) <EOL> m [ : , : <NUM_LIT:3> ] = ( coords [ : , : <NUM_LIT:3> ] * self . scale [ np . newaxis , : <NUM_LIT:3> ] + <EOL> coords [ : , <NUM_LIT:3> : ] * self . translate [ np . newaxis , : <NUM_LIT:3> ] ) <EOL> m [ : , <NUM_LIT:3> ] = coords [ : , <NUM_LIT:3> ] <EOL> return m <EOL> @ arg_to_vec4 <EOL> def imap ( self , coords ) : <EOL> """<STR_LIT>""" <EOL> m = np . empty ( coords . shape ) <EOL> m [ : , : <NUM_LIT:3> ] = ( ( coords [ : , : <NUM_LIT:3> ] - <EOL> coords [ : , <NUM_LIT:3> : ] * self . translate [ np . newaxis , : <NUM_LIT:3> ] ) / <EOL> self . scale [ np . newaxis , : <NUM_LIT:3> ] ) <EOL> m [ : , <NUM_LIT:3> ] = coords [ : , <NUM_LIT:3> ] <EOL> return m <EOL> def shader_map ( self ) : <EOL> return self . _shader_map <EOL> def shader_imap ( self ) : <EOL> return self . _shader_imap <EOL> @ property <EOL> def scale ( self ) : <EOL> return self . _scale . copy ( ) <EOL> @ scale . setter <EOL> def scale ( self , s ) : <EOL> s = as_vec4 ( s , default = ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> self . _set_st ( scale = s ) <EOL> @ property <EOL> def translate ( self ) : <EOL> return self . _translate . copy ( ) <EOL> @ translate . setter <EOL> def translate ( self , t ) : <EOL> t = as_vec4 ( t , default = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> self . _set_st ( translate = t ) <EOL> def _set_st ( self , scale = None , translate = None , update = True ) : <EOL> need_update = False <EOL> if scale is not None and not np . all ( scale == self . _scale ) : <EOL> self . _scale [ : ] = scale <EOL> need_update = True <EOL> if translate is not None and not np . all ( translate == self . _translate ) : <EOL> self . _translate [ : ] = translate <EOL> need_update = True <EOL> if update and need_update : <EOL> self . _update_shaders ( ) <EOL> self . update ( ) <EOL> def _update_shaders ( self ) : <EOL> self . _shader_map [ '<STR_LIT>' ] = self . scale <EOL> self . _shader_map [ '<STR_LIT>' ] = self . translate <EOL> self . _shader_imap [ '<STR_LIT>' ] = self . scale <EOL> self . _shader_imap [ '<STR_LIT>' ] = self . translate <EOL> def move ( self , move ) : <EOL> """<STR_LIT>""" <EOL> move = as_vec4 ( move , default = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> self . translate = self . translate + move <EOL> def zoom ( self , zoom , center = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) , mapped = True ) : <EOL> """<STR_LIT>""" <EOL> zoom = as_vec4 ( zoom , default = ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> center = as_vec4 ( center , default = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> scale = self . scale * zoom <EOL> if mapped : <EOL> trans = center - ( center - self . translate ) * zoom <EOL> else : <EOL> trans = self . scale * ( <NUM_LIT:1> - zoom ) * center + self . translate <EOL> self . _set_st ( scale = scale , translate = trans ) <EOL> def as_matrix ( self ) : <EOL> m = MatrixTransform ( ) <EOL> m . scale ( self . scale ) <EOL> m . translate ( self . translate ) <EOL> return m <EOL> @ classmethod <EOL> def from_mapping ( cls , x0 , x1 ) : <EOL> """<STR_LIT>""" <EOL> t = cls ( ) <EOL> t . set_mapping ( x0 , x1 ) <EOL> return t <EOL> def set_mapping ( self , x0 , x1 , update = True ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( x0 , Rect ) : <EOL> x0 = x0 . _transform_in ( ) [ : <NUM_LIT:3> ] <EOL> if isinstance ( x1 , Rect ) : <EOL> x1 = x1 . _transform_in ( ) [ : <NUM_LIT:3> ] <EOL> x0 = np . asarray ( x0 ) <EOL> x1 = np . asarray ( x1 ) <EOL> if ( x0 . ndim != <NUM_LIT:2> or x0 . shape [ <NUM_LIT:0> ] != <NUM_LIT:2> or x1 . ndim != <NUM_LIT:2> or <EOL> x1 . shape [ <NUM_LIT:0> ] != <NUM_LIT:2> ) : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> denom = x0 [ <NUM_LIT:1> ] - x0 [ <NUM_LIT:0> ] <EOL> mask = denom == <NUM_LIT:0> <EOL> denom [ mask ] = <NUM_LIT:1.0> <EOL> s = ( x1 [ <NUM_LIT:1> ] - x1 [ <NUM_LIT:0> ] ) / denom <EOL> s [ mask ] = <NUM_LIT:1.0> <EOL> s [ x0 [ <NUM_LIT:1> ] == x0 [ <NUM_LIT:0> ] ] = <NUM_LIT:1.0> <EOL> t = x1 [ <NUM_LIT:0> ] - s * x0 [ <NUM_LIT:0> ] <EOL> s = as_vec4 ( s , default = ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> t = as_vec4 ( t , default = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> self . _set_st ( scale = s , translate = t , update = update ) <EOL> def __mul__ ( self , tr ) : <EOL> if isinstance ( tr , STTransform ) : <EOL> s = self . scale * tr . scale <EOL> t = self . translate + ( tr . translate * self . scale ) <EOL> return STTransform ( scale = s , translate = t ) <EOL> elif isinstance ( tr , MatrixTransform ) : <EOL> return self . as_matrix ( ) * tr <EOL> else : <EOL> return super ( STTransform , self ) . __mul__ ( tr ) <EOL> def __rmul__ ( self , tr ) : <EOL> if isinstance ( tr , MatrixTransform ) : <EOL> return tr * self . as_matrix ( ) <EOL> return super ( STTransform , self ) . __rmul__ ( tr ) <EOL> def __repr__ ( self ) : <EOL> return ( "<STR_LIT>" <EOL> % ( self . scale , self . translate , id ( self ) ) ) <EOL> class MatrixTransform ( BaseTransform ) : <EOL> """<STR_LIT>""" <EOL> glsl_map = """<STR_LIT>""" <EOL> glsl_imap = """<STR_LIT>""" <EOL> Linear = True <EOL> Orthogonal = False <EOL> NonScaling = False <EOL> Isometric = False <EOL> def __init__ ( self , matrix = None ) : <EOL> super ( MatrixTransform , self ) . __init__ ( ) <EOL> if matrix is not None : <EOL> self . matrix = matrix <EOL> else : <EOL> self . reset ( ) <EOL> @ arg_to_vec4 <EOL> def map ( self , coords ) : <EOL> """<STR_LIT>""" <EOL> return np . dot ( coords , self . matrix ) <EOL> @ arg_to_vec4 <EOL> def imap ( self , coords ) : <EOL> """<STR_LIT>""" <EOL> return np . dot ( coords , self . inv_matrix ) <EOL> def shader_map ( self ) : <EOL> fn = super ( MatrixTransform , self ) . shader_map ( ) <EOL> fn [ '<STR_LIT>' ] = self . matrix <EOL> return fn <EOL> def shader_imap ( self ) : <EOL> fn = super ( MatrixTransform , self ) . shader_imap ( ) <EOL> fn [ '<STR_LIT>' ] = self . inv_matrix <EOL> return fn <EOL> @ property <EOL> def matrix ( self ) : <EOL> return self . _matrix <EOL> @ matrix . setter <EOL> def matrix ( self , m ) : <EOL> self . _matrix = m <EOL> self . _inv_matrix = None <EOL> self . shader_map ( ) <EOL> self . shader_imap ( ) <EOL> self . update ( ) <EOL> @ property <EOL> def inv_matrix ( self ) : <EOL> if self . _inv_matrix is None : <EOL> self . _inv_matrix = np . linalg . inv ( self . matrix ) <EOL> return self . _inv_matrix <EOL> @ arg_to_vec4 <EOL> def translate ( self , pos ) : <EOL> """<STR_LIT>""" <EOL> self . matrix = np . dot ( self . matrix , transforms . translate ( pos [ <NUM_LIT:0> , : <NUM_LIT:3> ] ) ) <EOL> def scale ( self , scale , center = None ) : <EOL> """<STR_LIT>""" <EOL> scale = transforms . scale ( as_vec4 ( scale , default = ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ) ) [ <NUM_LIT:0> , : <NUM_LIT:3> ] ) <EOL> if center is not None : <EOL> center = as_vec4 ( center ) [ <NUM_LIT:0> , : <NUM_LIT:3> ] <EOL> scale = np . dot ( np . dot ( transforms . translate ( - center ) , scale ) , <EOL> transforms . translate ( center ) ) <EOL> self . matrix = np . dot ( self . matrix , scale ) <EOL> def rotate ( self , angle , axis ) : <EOL> """<STR_LIT>""" <EOL> self . matrix = np . dot ( self . matrix , transforms . rotate ( angle , axis ) ) <EOL> def set_mapping ( self , points1 , points2 ) : <EOL> """<STR_LIT>""" <EOL> self . matrix = transforms . affine_map ( points1 , points2 ) . T <EOL> def set_ortho ( self , l , r , b , t , n , f ) : <EOL> """<STR_LIT>""" <EOL> self . matrix = transforms . ortho ( l , r , b , t , n , f ) <EOL> def reset ( self ) : <EOL> self . matrix = np . eye ( <NUM_LIT:4> ) <EOL> def __mul__ ( self , tr ) : <EOL> if ( isinstance ( tr , MatrixTransform ) and not <EOL> any ( tr . matrix [ : <NUM_LIT:3> , <NUM_LIT:3> ] != <NUM_LIT:0> ) ) : <EOL> return MatrixTransform ( matrix = np . dot ( tr . matrix , self . matrix ) ) <EOL> else : <EOL> return tr . __rmul__ ( self ) <EOL> def __repr__ ( self ) : <EOL> s = "<STR_LIT>" % self . __class__ . __name__ <EOL> indent = "<STR_LIT:U+0020>" * len ( s ) <EOL> s += str ( list ( self . matrix [ <NUM_LIT:0> ] ) ) + "<STR_LIT>" <EOL> s += indent + str ( list ( self . matrix [ <NUM_LIT:1> ] ) ) + "<STR_LIT>" <EOL> s += indent + str ( list ( self . matrix [ <NUM_LIT:2> ] ) ) + "<STR_LIT>" <EOL> s += indent + str ( list ( self . matrix [ <NUM_LIT:3> ] ) ) + "<STR_LIT>" % id ( self ) <EOL> return s <EOL> def set_perspective ( self , fov , aspect , near , far ) : <EOL> """<STR_LIT>""" <EOL> self . matrix = transforms . perspective ( fov , aspect , near , far ) <EOL> def set_frustum ( self , l , r , b , t , n , f ) : <EOL> """<STR_LIT>""" <EOL> self . matrix = transforms . frustum ( l , r , b , t , n , f ) </s>
