<s> from datetime import datetime <EOL> import Queue <EOL> import re <EOL> import StringIO <EOL> import sys <EOL> import threading <EOL> import traceback <EOL> import wx <EOL> import koko <EOL> from koko . struct import Struct <EOL> from koko . fab . asdf import ASDF <EOL> from koko . fab . tree import MathTree <EOL> from koko . fab . fabvars import FabVars <EOL> from koko . fab . mesh import Mesh <EOL> from koko . c . region import Region <EOL> class RenderTask ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , view , script = None , cad = None ) : <EOL> """<STR_LIT>""" <EOL> if not ( bool ( script ) ^ bool ( cad ) ) : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> self . view = view <EOL> self . script = script <EOL> self . cad = cad <EOL> self . event = threading . Event ( ) <EOL> self . c_event = threading . Event ( ) <EOL> self . output = '<STR_LIT>' <EOL> self . thread = threading . Thread ( target = self . run ) <EOL> self . thread . daemon = True <EOL> self . thread . start ( ) <EOL> def run ( self ) : <EOL> """<STR_LIT>""" <EOL> start = datetime . now ( ) <EOL> koko . CANVAS . border = None <EOL> koko . GLCANVAS . border = None <EOL> koko . FRAME . status = '<STR_LIT>' <EOL> koko . EDITOR . error_marker = None <EOL> self . output += '<STR_LIT>' <EOL> make_mesh = self . cad is None or koko . GLCANVAS . loaded is False <EOL> if not self . cad and not self . cad_math ( ) : return <EOL> height = koko . FRAME . get_menu ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> shaded = koko . FRAME . get_menu ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> dual = koko . FRAME . get_menu ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if self . cad . render_mode is None : <EOL> wx . CallAfter ( height . Enable , True ) <EOL> wx . CallAfter ( shaded . Enable , True ) <EOL> wx . CallAfter ( dual . Enable , True ) <EOL> else : <EOL> wx . CallAfter ( height . Enable , False ) <EOL> wx . CallAfter ( shaded . Enable , False ) <EOL> wx . CallAfter ( dual . Enable , False ) <EOL> if self . cad . render_mode == '<STR_LIT>' and not shaded . IsChecked ( ) : <EOL> render_mode = '<STR_LIT>' <EOL> wx . CallAfter ( shaded . Check , True ) <EOL> wx . CallAfter ( koko . APP . render_mode , '<STR_LIT>' ) <EOL> elif self . cad . render_mode == '<STR_LIT>' and not height . IsChecked ( ) : <EOL> render_mode = '<STR_LIT>' <EOL> wx . CallAfter ( height . Check , True ) <EOL> wx . CallAfter ( koko . APP . render_mode , '<STR_LIT>' ) <EOL> elif shaded . IsChecked ( ) : <EOL> render_mode = '<STR_LIT>' <EOL> elif height . IsChecked ( ) : <EOL> render_mode = '<STR_LIT>' <EOL> else : <EOL> render_mode = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if '<STR_LIT>' in render_mode : <EOL> imgs = self . make_images ( ) <EOL> if self . event . is_set ( ) : return <EOL> koko . CANVAS . load_images ( imgs , self . cad . mm_per_unit ) <EOL> if make_mesh and '<STR_LIT>' in render_mode : <EOL> koko . GLCANVAS . loaded = False <EOL> images = [ ] <EOL> meshes = [ ] <EOL> try : <EOL> image_scale = max ( <EOL> ( <NUM_LIT> / expr . dx * expr . dy ) ** <NUM_LIT:0.5> for expr in self . cad . shapes <EOL> if not expr . dz <EOL> ) <EOL> except ValueError : <EOL> image_scale = <NUM_LIT:1> <EOL> for e in self . cad . shapes : <EOL> if self . event . is_set ( ) : return <EOL> if e . bounded : <EOL> meshes . append ( self . make_mesh ( e ) ) <EOL> elif ( self . cad . dx is not None and <EOL> self . cad . dy is not None ) : <EOL> images . append ( self . make_flat_image ( e , image_scale ) ) <EOL> else : <EOL> koko . FRAME . status = ( <EOL> '<STR_LIT>' <EOL> ) <EOL> koko . GLCANVAS . border = ( <NUM_LIT:255> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> return <EOL> if not self . event . is_set ( ) : <EOL> koko . GLCANVAS . clear ( ) <EOL> if images : koko . GLCANVAS . load_images ( images ) <EOL> if meshes : koko . GLCANVAS . load_meshes ( meshes ) <EOL> koko . GLCANVAS . loaded = True <EOL> koko . GLCANVAS . border = None <EOL> koko . CANVAS . border = None <EOL> koko . FRAME . status = '<STR_LIT>' <EOL> self . output += "<STR_LIT>" % ( datetime . now ( ) - start ) <EOL> if self . event . is_set ( ) : return <EOL> koko . FRAME . output = self . output <EOL> def cad_math ( self ) : <EOL> """<STR_LIT>""" <EOL> koko . FRAME . status = "<STR_LIT>" <EOL> now = datetime . now ( ) <EOL> vars = koko . PRIMS . dict <EOL> vars [ '<STR_LIT>' ] = FabVars ( ) <EOL> self . output += '<STR_LIT>' <EOL> if self . event . is_set ( ) : return <EOL> buffer = StringIO . StringIO ( ) <EOL> sys . stdout = buffer <EOL> try : <EOL> exec ( self . script , vars ) <EOL> except : <EOL> sys . stdout = sys . __stdout__ <EOL> koko . CANVAS . border = ( <NUM_LIT:255> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> koko . GLCANVAS . border = ( <NUM_LIT:255> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> errors = traceback . format_exc ( ) . split ( '<STR_LIT:\n>' ) <EOL> errors = errors [ <NUM_LIT:0> ] + '<STR_LIT:\n>' + '<STR_LIT:\n>' . join ( errors [ <NUM_LIT:3> : ] ) <EOL> for m in re . findall ( r'<STR_LIT>' , errors ) : <EOL> error_line = int ( m ) - <NUM_LIT:1> <EOL> errors = errors . replace ( koko . BASE_DIR , '<STR_LIT>' ) <EOL> self . output += buffer . getvalue ( ) + errors <EOL> try : <EOL> koko . EDITOR . error_marker = error_line <EOL> koko . FRAME . status = "<STR_LIT>" % ( error_line + <NUM_LIT:1> ) <EOL> except NameError : <EOL> koko . FRAME . status = "<STR_LIT>" <EOL> else : <EOL> try : self . cad = vars [ '<STR_LIT>' ] <EOL> except KeyError : self . cad = None <EOL> self . output += buffer . getvalue ( ) <EOL> dT = datetime . now ( ) - now <EOL> self . output += "<STR_LIT>" % dT <EOL> sys . stdout = sys . __stdout__ <EOL> koko . FRAME . output = self . output <EOL> if not self . cad : <EOL> koko . FRAME . states = '<STR_LIT>' <EOL> koko . CANVAS . border = ( <NUM_LIT:255> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> koko . GLCANVAS . border = ( <NUM_LIT:255> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> return <EOL> elif self . cad . shapes == [ ] : <EOL> koko . FRAME . status = ( '<STR_LIT>' ) <EOL> koko . CANVAS . border = ( <NUM_LIT:255> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> koko . GLCANVAS . border = ( <NUM_LIT:255> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> return <EOL> koko . FRAME . status = '<STR_LIT>' <EOL> for e in self . cad . shapes : <EOL> self . output += "<STR_LIT>" <EOL> start = datetime . now ( ) <EOL> if bool ( e . ptr ) : <EOL> self . output += '<STR_LIT>' % ( datetime . now ( ) - start ) <EOL> else : <EOL> self . output += "<STR_LIT>" <EOL> koko . FRAME . status = ( '<STR_LIT>' ) <EOL> koko . CANVAS . border = ( <NUM_LIT:255> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> self . cad = None <EOL> return False <EOL> koko . FRAME . output = self . output <EOL> return self . cad != None <EOL> def make_images ( self ) : <EOL> """<STR_LIT>""" <EOL> zmin = self . cad . zmin if self . cad . zmin is not None else <NUM_LIT:0> <EOL> zmax = self . cad . zmax if self . cad . zmax is not None else <NUM_LIT:0> <EOL> imgs = [ ] <EOL> for e in self . cad . shapes : <EOL> if self . event . is_set ( ) : return <EOL> imgs . append ( self . make_image ( e , zmin , zmax ) ) <EOL> return imgs <EOL> def make_flat_image ( self , expr , scale ) : <EOL> """<STR_LIT>""" <EOL> region = Region ( <EOL> ( expr . xmin - self . cad . border * expr . dx , <EOL> expr . ymin - self . cad . border * expr . dy , <EOL> <NUM_LIT:0> ) , <EOL> ( expr . xmax + self . cad . border * expr . dx , <EOL> expr . ymax + self . cad . border * expr . dy , <EOL> <NUM_LIT:0> ) , <EOL> scale <EOL> ) <EOL> koko . FRAME . status = '<STR_LIT>' <EOL> self . output += "<STR_LIT>" <EOL> start = datetime . now ( ) <EOL> img = expr . render ( region , interrupt = self . c_event , <EOL> mm_per_unit = self . cad . mm_per_unit ) <EOL> img . color = expr . color <EOL> dT = datetime . now ( ) - start <EOL> self . output += "<STR_LIT>" % dT <EOL> return img <EOL> def make_image ( self , expr , zmin , zmax ) : <EOL> """<STR_LIT>""" <EOL> xmin = self . view . xmin <EOL> xmax = self . view . xmax <EOL> ymin = self . view . ymin <EOL> ymax = self . view . ymax <EOL> if expr . xmin is None : xmin = xmin <EOL> else : xmin = max ( xmin , expr . xmin - self . cad . border * expr . dx ) <EOL> if expr . xmax is None : xmax = xmax <EOL> else : xmax = min ( xmax , expr . xmax + self . cad . border * expr . dx ) <EOL> if expr . ymin is None : ymin = ymin <EOL> else : ymin = max ( ymin , expr . ymin - self . cad . border * expr . dy ) <EOL> if expr . ymax is None : ymax = ymax <EOL> else : ymax = min ( ymax , expr . ymax + self . cad . border * expr . dy ) <EOL> region = Region ( ( xmin , ymin , zmin ) , ( xmax , ymax , zmax ) , <EOL> self . view . pixels_per_unit ) <EOL> koko . FRAME . status = '<STR_LIT>' <EOL> self . output += "<STR_LIT>" <EOL> start = datetime . now ( ) <EOL> img = expr . render ( region , interrupt = self . c_event , <EOL> mm_per_unit = self . cad . mm_per_unit ) <EOL> img . color = expr . color <EOL> dT = datetime . now ( ) - start <EOL> self . output += "<STR_LIT>" % dT <EOL> return img <EOL> def make_mesh ( self , expr ) : <EOL> """<STR_LIT>""" <EOL> self . output += '<STR_LIT>' <EOL> DEPTH = <NUM_LIT:0> <EOL> while DEPTH <= <NUM_LIT:4> : <EOL> region = Region ( <EOL> ( expr . xmin - self . cad . border * expr . dx , <EOL> expr . ymin - self . cad . border * expr . dy , <EOL> expr . zmin - self . cad . border * expr . dz ) , <EOL> ( expr . xmax + self . cad . border * expr . dx , <EOL> expr . ymax + self . cad . border * expr . dy , <EOL> expr . zmax + self . cad . border * expr . dz ) , <EOL> depth = DEPTH <EOL> ) <EOL> koko . FRAME . status = '<STR_LIT>' <EOL> start = datetime . now ( ) <EOL> asdf = expr . asdf ( region = region , mm_per_unit = self . cad . mm_per_unit , <EOL> interrupt = self . c_event ) <EOL> self . output += '<STR_LIT>' % ( datetime . now ( ) - start ) <EOL> if self . event . is_set ( ) : return <EOL> koko . FRAME . output = self . output <EOL> koko . FRAME . status = '<STR_LIT>' <EOL> start = datetime . now ( ) <EOL> mesh = asdf . triangulate ( interrupt = self . c_event ) <EOL> if mesh . vcount : break <EOL> else : DEPTH += <NUM_LIT:1> <EOL> mesh . source = Struct ( <EOL> type = MathTree , expr = expr . clone ( ) , <EOL> depth = DEPTH , scale = self . cad . mm_per_unit <EOL> ) <EOL> if self . event . is_set ( ) : return <EOL> self . output += '<STR_LIT>' % ( datetime . now ( ) - start ) <EOL> self . output += "<STR_LIT>" . format ( <EOL> mesh . vcount if mesh else <NUM_LIT:0> , mesh . tcount if mesh else <NUM_LIT:0> ) <EOL> koko . FRAME . output = self . output <EOL> return mesh <EOL> class RefineTask ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , mesh ) : <EOL> """<STR_LIT>""" <EOL> self . mesh = mesh <EOL> self . event = threading . Event ( ) <EOL> self . c_event = threading . Event ( ) <EOL> self . thread = threading . Thread ( target = self . run ) <EOL> self . thread . daemon = True <EOL> self . thread . start ( ) <EOL> def run ( self ) : <EOL> """<STR_LIT>""" <EOL> koko . FRAME . status = '<STR_LIT>' <EOL> self . mesh . refine ( ) <EOL> koko . GLCANVAS . reload_vbos ( ) <EOL> class CollapseTask ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , mesh ) : <EOL> """<STR_LIT>""" <EOL> self . mesh = mesh <EOL> self . event = threading . Event ( ) <EOL> self . c_event = threading . Event ( ) <EOL> self . thread = threading . Thread ( target = self . run ) <EOL> self . thread . daemon = True <EOL> self . thread . start ( ) <EOL> def run ( self ) : <EOL> """<STR_LIT>""" <EOL> koko . FRAME . status = '<STR_LIT>' <EOL> self . mesh . collapse ( ) <EOL> koko . GLCANVAS . reload_vbos ( ) </s>
