<s> __copyright__ = "<STR_LIT>" <EOL> import wx <EOL> import numpy <EOL> from Cura . util import printableObject <EOL> def supportedExtensions ( ) : <EOL> return [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> class convertImageDialog ( wx . Dialog ) : <EOL> def __init__ ( self , parent , filename ) : <EOL> super ( convertImageDialog , self ) . __init__ ( None , title = "<STR_LIT>" ) <EOL> wx . EVT_CLOSE ( self , self . OnClose ) <EOL> self . parent = parent <EOL> self . filename = filename <EOL> image = wx . Image ( filename ) <EOL> w , h = image . GetWidth ( ) - <NUM_LIT:1> , image . GetHeight ( ) - <NUM_LIT:1> <EOL> self . aspectRatio = float ( w ) / float ( h ) <EOL> p = wx . Panel ( self ) <EOL> self . SetSizer ( wx . BoxSizer ( ) ) <EOL> self . GetSizer ( ) . Add ( p , <NUM_LIT:1> , flag = wx . EXPAND ) <EOL> s = wx . GridBagSizer ( <NUM_LIT:2> , <NUM_LIT:2> ) <EOL> p . SetSizer ( s ) <EOL> s . Add ( wx . StaticText ( p , - <NUM_LIT:1> , _ ( '<STR_LIT>' ) ) , pos = ( <NUM_LIT:0> , <NUM_LIT:0> ) , flag = wx . LEFT | wx . TOP | wx . RIGHT , border = <NUM_LIT:5> ) <EOL> self . heightInput = wx . TextCtrl ( p , - <NUM_LIT:1> , '<STR_LIT>' ) <EOL> s . Add ( self . heightInput , pos = ( <NUM_LIT:0> , <NUM_LIT:1> ) , flag = wx . LEFT | wx . TOP | wx . RIGHT | wx . EXPAND , border = <NUM_LIT:5> ) <EOL> s . Add ( wx . StaticText ( p , - <NUM_LIT:1> , _ ( '<STR_LIT>' ) ) , pos = ( <NUM_LIT:1> , <NUM_LIT:0> ) , flag = wx . LEFT | wx . TOP | wx . RIGHT , border = <NUM_LIT:5> ) <EOL> self . baseHeightInput = wx . TextCtrl ( p , - <NUM_LIT:1> , '<STR_LIT:1.0>' ) <EOL> s . Add ( self . baseHeightInput , pos = ( <NUM_LIT:1> , <NUM_LIT:1> ) , flag = wx . LEFT | wx . TOP | wx . RIGHT | wx . EXPAND , border = <NUM_LIT:5> ) <EOL> s . Add ( wx . StaticText ( p , - <NUM_LIT:1> , _ ( '<STR_LIT>' ) ) , pos = ( <NUM_LIT:2> , <NUM_LIT:0> ) , flag = wx . LEFT | wx . TOP | wx . RIGHT , border = <NUM_LIT:5> ) <EOL> self . widthInput = wx . TextCtrl ( p , - <NUM_LIT:1> , str ( w * <NUM_LIT> ) ) <EOL> s . Add ( self . widthInput , pos = ( <NUM_LIT:2> , <NUM_LIT:1> ) , flag = wx . LEFT | wx . TOP | wx . RIGHT | wx . EXPAND , border = <NUM_LIT:5> ) <EOL> s . Add ( wx . StaticText ( p , - <NUM_LIT:1> , _ ( '<STR_LIT>' ) ) , pos = ( <NUM_LIT:3> , <NUM_LIT:0> ) , flag = wx . LEFT | wx . TOP | wx . RIGHT , border = <NUM_LIT:5> ) <EOL> self . depthInput = wx . TextCtrl ( p , - <NUM_LIT:1> , str ( h * <NUM_LIT> ) ) <EOL> s . Add ( self . depthInput , pos = ( <NUM_LIT:3> , <NUM_LIT:1> ) , flag = wx . LEFT | wx . TOP | wx . RIGHT | wx . EXPAND , border = <NUM_LIT:5> ) <EOL> options = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . invertInput = wx . ComboBox ( p , - <NUM_LIT:1> , options [ <NUM_LIT:0> ] , choices = options , style = wx . CB_DROPDOWN | wx . CB_READONLY ) <EOL> s . Add ( self . invertInput , pos = ( <NUM_LIT:4> , <NUM_LIT:1> ) , flag = wx . LEFT | wx . TOP | wx . RIGHT | wx . EXPAND , border = <NUM_LIT:5> ) <EOL> self . invertInput . SetSelection ( <NUM_LIT:0> ) <EOL> options = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . smoothInput = wx . ComboBox ( p , - <NUM_LIT:1> , options [ <NUM_LIT:0> ] , choices = options , style = wx . CB_DROPDOWN | wx . CB_READONLY ) <EOL> s . Add ( self . smoothInput , pos = ( <NUM_LIT:5> , <NUM_LIT:1> ) , flag = wx . LEFT | wx . TOP | wx . RIGHT | wx . EXPAND , border = <NUM_LIT:5> ) <EOL> self . smoothInput . SetSelection ( <NUM_LIT:0> ) <EOL> self . okButton = wx . Button ( p , - <NUM_LIT:1> , '<STR_LIT>' ) <EOL> s . Add ( self . okButton , pos = ( <NUM_LIT:6> , <NUM_LIT:1> ) , flag = wx . ALL , border = <NUM_LIT:5> ) <EOL> self . okButton . Bind ( wx . EVT_BUTTON , self . OnOkClick ) <EOL> self . widthInput . Bind ( wx . EVT_TEXT , self . OnWidthEnter ) <EOL> self . depthInput . Bind ( wx . EVT_TEXT , self . OnDepthEnter ) <EOL> self . Fit ( ) <EOL> self . Centre ( ) <EOL> def OnClose ( self , e ) : <EOL> self . Destroy ( ) <EOL> def OnOkClick ( self , e ) : <EOL> self . Close ( ) <EOL> height = float ( self . heightInput . GetValue ( ) ) <EOL> width = float ( self . widthInput . GetValue ( ) ) <EOL> blur = self . smoothInput . GetSelection ( ) <EOL> blur *= blur <EOL> invert = self . invertInput . GetSelection ( ) == <NUM_LIT:0> <EOL> baseHeight = float ( self . baseHeightInput . GetValue ( ) ) <EOL> obj = convertImage ( self . filename , height , width , blur , invert , baseHeight ) <EOL> self . parent . _scene . add ( obj ) <EOL> self . parent . _scene . centerAll ( ) <EOL> self . parent . sceneUpdated ( ) <EOL> def OnWidthEnter ( self , e ) : <EOL> try : <EOL> w = float ( self . widthInput . GetValue ( ) ) <EOL> except ValueError : <EOL> return <EOL> h = w / self . aspectRatio <EOL> self . depthInput . SetValue ( str ( h ) ) <EOL> def OnDepthEnter ( self , e ) : <EOL> try : <EOL> h = float ( self . depthInput . GetValue ( ) ) <EOL> except ValueError : <EOL> return <EOL> w = h * self . aspectRatio <EOL> self . widthInput . SetValue ( str ( w ) ) <EOL> def convertImage ( filename , height = <NUM_LIT> , width = <NUM_LIT> , blur = <NUM_LIT:0> , invert = False , baseHeight = <NUM_LIT:1.0> ) : <EOL> image = wx . Image ( filename ) <EOL> image . ConvertToGreyscale ( ) <EOL> if image . GetHeight ( ) > <NUM_LIT> : <EOL> image . Rescale ( image . GetWidth ( ) * <NUM_LIT> / image . GetHeight ( ) , <NUM_LIT> , wx . IMAGE_QUALITY_HIGH ) <EOL> if image . GetWidth ( ) > <NUM_LIT> : <EOL> image . Rescale ( <NUM_LIT> , image . GetHeight ( ) * <NUM_LIT> / image . GetWidth ( ) , wx . IMAGE_QUALITY_HIGH ) <EOL> if blur > <NUM_LIT:0> : <EOL> image = image . Blur ( blur ) <EOL> z = numpy . fromstring ( image . GetData ( ) , numpy . uint8 ) <EOL> z = numpy . array ( z [ : : <NUM_LIT:3> ] , numpy . float32 ) <EOL> if invert : <EOL> z = <NUM_LIT:255> - z <EOL> pMin , pMax = numpy . min ( z ) , numpy . max ( z ) <EOL> if pMax == pMin : <EOL> pMax += <NUM_LIT:1.0> <EOL> z = ( ( z - pMin ) * height / ( pMax - pMin ) ) + baseHeight <EOL> w , h = image . GetWidth ( ) , image . GetHeight ( ) <EOL> scale = width / ( image . GetWidth ( ) - <NUM_LIT:1> ) <EOL> n = w * h <EOL> y , x = numpy . mgrid [ <NUM_LIT:0> : h , <NUM_LIT:0> : w ] <EOL> x = numpy . array ( x , numpy . float32 , copy = False ) * scale <EOL> y = numpy . array ( y , numpy . float32 , copy = False ) * - scale <EOL> v0 = numpy . concatenate ( ( x . reshape ( ( n , <NUM_LIT:1> ) ) , y . reshape ( ( n , <NUM_LIT:1> ) ) , z . reshape ( ( n , <NUM_LIT:1> ) ) ) , <NUM_LIT:1> ) <EOL> v0 = v0 . reshape ( ( h , w , <NUM_LIT:3> ) ) <EOL> v1 = v0 [ <NUM_LIT:0> : - <NUM_LIT:1> , <NUM_LIT:0> : - <NUM_LIT:1> , : ] <EOL> v2 = v0 [ <NUM_LIT:0> : - <NUM_LIT:1> , <NUM_LIT:1> : , : ] <EOL> v3 = v0 [ <NUM_LIT:1> : , <NUM_LIT:0> : - <NUM_LIT:1> , : ] <EOL> v4 = v0 [ <NUM_LIT:1> : , <NUM_LIT:1> : , : ] <EOL> obj = printableObject . printableObject ( filename ) <EOL> m = obj . _addMesh ( ) <EOL> m . _prepareFaceCount ( ( w - <NUM_LIT:1> ) * ( h - <NUM_LIT:1> ) * <NUM_LIT:2> + <NUM_LIT:2> + ( w - <NUM_LIT:1> ) * <NUM_LIT:4> + ( h - <NUM_LIT:1> ) * <NUM_LIT:4> ) <EOL> m . vertexes = numpy . array ( numpy . concatenate ( ( v1 , v3 , v2 , v2 , v3 , v4 ) , <NUM_LIT:2> ) . reshape ( ( ( w - <NUM_LIT:1> ) * ( h - <NUM_LIT:1> ) * <NUM_LIT:6> , <NUM_LIT:3> ) ) , numpy . float32 , copy = False ) <EOL> m . vertexes = numpy . concatenate ( ( m . vertexes , numpy . zeros ( ( ( <NUM_LIT:2> + ( w - <NUM_LIT:1> ) * <NUM_LIT:4> + ( h - <NUM_LIT:1> ) * <NUM_LIT:4> ) * <NUM_LIT:3> , <NUM_LIT:3> ) , numpy . float32 ) ) ) <EOL> m . vertexCount = ( w - <NUM_LIT:1> ) * ( h - <NUM_LIT:1> ) * <NUM_LIT:6> <EOL> x = ( w - <NUM_LIT:1> ) * scale <EOL> y = ( h - <NUM_LIT:1> ) * - scale <EOL> m . _addFace ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , x , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , y , <NUM_LIT:0> ) <EOL> m . _addFace ( x , y , <NUM_LIT:0> , <NUM_LIT:0> , y , <NUM_LIT:0> , x , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> for n in xrange ( <NUM_LIT:0> , w - <NUM_LIT:1> ) : <EOL> x = n * scale <EOL> i = w * h - w + n <EOL> m . _addFace ( x + scale , <NUM_LIT:0> , <NUM_LIT:0> , x , <NUM_LIT:0> , <NUM_LIT:0> , x , <NUM_LIT:0> , z [ n ] ) <EOL> m . _addFace ( x + scale , <NUM_LIT:0> , <NUM_LIT:0> , x , <NUM_LIT:0> , z [ n ] , x + scale , <NUM_LIT:0> , z [ n + <NUM_LIT:1> ] ) <EOL> m . _addFace ( x + scale , y , <NUM_LIT:0> , x , y , z [ i ] , x , y , <NUM_LIT:0> ) <EOL> m . _addFace ( x + scale , y , <NUM_LIT:0> , x + scale , y , z [ i + <NUM_LIT:1> ] , x , y , z [ i ] ) <EOL> x = ( w - <NUM_LIT:1> ) * scale <EOL> for n in xrange ( <NUM_LIT:0> , h - <NUM_LIT:1> ) : <EOL> y = n * - scale <EOL> i = n * w + w - <NUM_LIT:1> <EOL> m . _addFace ( <NUM_LIT:0> , y - scale , <NUM_LIT:0> , <NUM_LIT:0> , y , z [ n * w ] , <NUM_LIT:0> , y , <NUM_LIT:0> ) <EOL> m . _addFace ( <NUM_LIT:0> , y - scale , <NUM_LIT:0> , <NUM_LIT:0> , y - scale , z [ n * w + w ] , <NUM_LIT:0> , y , z [ n * w ] ) <EOL> m . _addFace ( x , y - scale , <NUM_LIT:0> , x , y , <NUM_LIT:0> , x , y , z [ i ] ) <EOL> m . _addFace ( x , y - scale , <NUM_LIT:0> , x , y , z [ i ] , x , y - scale , z [ i + w ] ) <EOL> obj . _postProcessAfterLoad ( ) <EOL> return obj </s>
