def main( timestamp, src_file_format, errlog_path_format, filename_format, devuser_path_format="/data1/logs/transformsaaslogs/devuserlog/%(yyyymmdd)s/%(hhmm)s.log" ): transform = Transform(timestamp=timestamp * 1000) # 毫秒 yyyymmdd = time.strftime('%Y%m%d', time.localtime(timestamp)) yyyymmddhhmm = time.strftime('%Y%m%d%H%M', time.localtime(timestamp)) hhmm = time.strftime('%H%M', time.localtime(timestamp)) src_file = src_file_format % { "yyyymmdd": yyyymmdd, "yyyymmddhhmm": yyyymmddhhmm } errlog_path = errlog_path_format % {"yyyymmdd": yyyymmdd, "hhmm": hhmm} devuser_path = devuser_path_format % {"yyyymmdd": yyyymmdd, "hhmm": hhmm} print src_file # with open(src_file) as f: errlognum = 0 for line in JHOpen().readLines(src_file): if not line: continue try: logs = transform.transform(line) for item in logs: datatype = item['jhd_datatype'] item['jhd_userkey'] = item['jhd_userkey'].strip() filename = filename_format % { "yyyymmdd": yyyymmdd, "hhmm": hhmm, 'datatype': datatype } # unicode 转码中文 try: line_out = json.dumps(item, ensure_ascii=False).encode('utf-8') except: line_out = json.dumps(item, ensure_ascii=False) if item.get("isdevuser", False) == False: LogStore(filename, line_out) else: LogStore(devuser_path, line_out) except: import traceback exc_type, exc_value, exc_traceback = sys.exc_info() errinfo = traceback.format_exception(exc_type, exc_value, exc_traceback) errinfo.append(line) LogStore(errlog_path, json.dumps(map(lambda item: item.strip(), errinfo))) errlognum += 1 if errlognum: print("\t".join([ "@" + yyyymmddhhmm, "errlognum: %d" % errlognum, "err info: %s" % errlog_path ])) print("".join(["endline", '-' * 10])) LogStore.finished(iszip=True)
def __init__(self, **argd): """x.__init__(...) initializes x; see x.__class__.__doc__ for signature""" super(Container, self).__init__() # get transformation data and convert to vectors self.position = Vector(*argd.get("position", (0, 0, 0))) self.rotation = Vector(*argd.get("rotation", (0.0, 0.0, 0.0))) self.scaling = Vector(*argd.get("scaling", (1, 1, 1))) # for detection of changes self.oldrot = Vector() self.oldpos = Vector() self.oldscaling = Vector() # inital apply trasformations self.transform = Transform() self.components = [] self.rel_positions = {} self.rel_rotations = {} self.rel_scalings = {} self.poscomms = {} self.rotcomms = {} self.scacomms = {} contents = argd.get("contents", None) if contents is not None: for (comp, params) in contents.items(): self.addElement(comp, **params)
def applyTransforms(self): """ Use the objects translation/rotation/scaling values to generate a new transformation Matrix if changes have happened. """ # generate new transformation matrix if needed if self.oldscaling != self.scaling or self.oldrot != self.rotation or self.oldpos != self.position: self.transform = Transform() self.transform.applyScaling(self.scaling) self.transform.applyRotation(self.rotation) self.transform.applyTranslation(self.position) if self.oldscaling != self.scaling: self.send(self.scaling.toTuple(), "scaling") self.oldscaling = self.scaling.copy() if self.oldrot != self.rotation: self.send(self.rotation.toTuple(), "rotation") self.oldrot = self.rotation.copy() if self.oldpos != self.position: self.send(self.position.toTuple(), "position") self.oldpos = self.position.copy() # send new transform to display service transform_update = { "TRANSFORM_UPDATE": True, "objectid": id(self), "transform": self.transform } self.send(transform_update, "display_signal")
def __init__(self, **argd): """x.__init__(...) initializes x; see x.__class__.__doc__ for signature""" super(OpenGLComponent, self).__init__() # get transformation data and convert to vectors self.size = Vector(*argd.get("size", (0, 0, 0))) self.position = Vector(*argd.get("position", (0, 0, 0))) self.rotation = Vector(*argd.get("rotation", (0.0, 0.0, 0.0))) self.scaling = Vector(*argd.get("scaling", (1, 1, 1))) # for detection of changes self.oldrot = Vector() self.oldpos = Vector() self.oldscaling = Vector() self.transform = Transform() # name (mostly for debugging) self.name = argd.get("name", "nameless") # create clock self.clock = pygame.time.Clock() self.frametime = 0.0 # get display service displayservice = OpenGLDisplay.getDisplayService() # link display_signal to displayservice self.link((self, "display_signal"), displayservice)
def transformPanelStructure(self): self.transform = Transform(self.transformSliderPanel) self.comboBox = ComboBox(self.comboBoxPanel, choices = ["---", \ "Chorus", \ "Harmonizer", \ "FrequencyShift", \ "Reverb", \ "Distortion"])
def __init__(self): GameObject.init_object(self) # Components definitions self.transform = Transform(Vector2(0, 0, 0), Vector2(0, 0, 0), Vector2(0, 0, 0)) self.sprite = Resources.load("flamme.png") self.sprite = pygame.transform.scale(self.sprite, (50, 50)) self.animator = AnimatorController()
def main( timestamp, src_logpath_format="/data1/nginxlogs/jhsaaslogs_h5/access_jhlogs.%(yyyymmddhhmm)s", errlog_path_format="/data1/logs/transformh5/err/%(yyyymmdd)s/%(hhmm)s.err", filename_format="/data1/logs/transformh5/%(datatype)s/%(yyyymmdd)s/%(hhmm)s.log" ): yyyymmddhhmm = time.strftime('%Y%m%d%H%M', time.localtime(timestamp)) yyyymmdd = time.strftime('%Y%m%d', time.localtime(timestamp)) hhmm = time.strftime('%H%M', time.localtime(timestamp)) src_logpath = src_logpath_format % { "yyyymmddhhmm": yyyymmddhhmm, "yyyymmdd": yyyymmdd } errlog_path = errlog_path_format % { "yyyymmddhhmm": yyyymmddhhmm, "yyyymmdd": yyyymmdd, "hhmm": hhmm } print src_logpath if src_logpath.endswith(".gz"): src_logpath_file = gzip.open(src_logpath) else: src_logpath_file = open(src_logpath) try: transform = Transform(timestamp=timestamp) except: transform = Transform() for line in src_logpath_file: try: # ip = line.split(",")[0].strip() # # 如果为内网ip,做单独处理 # try: # if ip.startswith("127"): # ip = ip_pattern.search(line).group(1) # except: # import traceback # print(traceback.print_exc()) data = transform.transform(line) # data = json.loads(lod_line) # data["ip"] = ip if not data: continue datatype = data["appkey"] filename = filename_format % { "datatype": datatype, "yyyymmdd": yyyymmdd, "hhmm": hhmm } LogStore(filename, json.dumps(data)) except Exception, e: LogStore(errlog_path, "%s, %s" % (e, line.strip()))
def applyTransforms(self): """ Use the objects translation/rotation/scaling values to generate a new transformation Matrix if changes have happened. """ # generate new transformation matrix if needed if self.oldscaling != self.scaling or self.oldrot != self.rotation or self.oldpos != self.position: self.transform = Transform() self.transform.applyScaling(self.scaling) self.transform.applyRotation(self.rotation) self.transform.applyTranslation(self.position) self.oldpos = self.position.copy() self.oldrot = self.rotation.copy() self.oldscaling = self.scaling.copy() self.rearangeContents()
def tilesDisplacedHeuristic(self, state): transform = Transform() currentPuzzleState = transform.convertStringToEightPuzzle(state) goalPuzzleState = transform.convertStringToEightPuzzle( self.goalState.puzzleState) h = 0 for i in range(3): for j in range(3): if currentPuzzleState[i][j] != goalPuzzleState[i][j]: h += 1 if currentPuzzleState[i][j] == 0 and currentPuzzleState[i][ j] != goalPuzzleState[i][j] and isTileInclude == False: h -= 1 return h
def market_etl(): transform = Transform() transform.extract() transform.transform() transform.load() print("完成市场主题处理") print()
def getGlobalTransform(self, relativeTo=None): """Return global transformation (rotation angle+translation) required to move from relative state to current state. If relative state isn't specified, then we use the state of the ROI when mouse is pressed.""" if relativeTo == None: relativeTo = self.preMoveState st = self.getState() ## this is only allowed because we will be comparing the two relativeTo['scale'] = relativeTo['size'] st['scale'] = st['size'] t1 = Transform(relativeTo) t2 = Transform(st) return t2 / t1
def __init__(self, fanout, depth, spread=default_spread, rootColor=default_rootColor, edgeColor=default_edgeColor, scale=default_scale, actor=None): Component.__init__(self, actor) self.fanout = fanout self.depth = depth self.spread = spread self.rootColor = rootColor self.edgeColor = edgeColor self.scale = scale self.rootScale = self.scale # NOTE this will also apply to children #self.childScale = np.float32([1.0, 1.0, 1.0]) # NOTE this will get compounded if the radial tree is a true hierarchy, better to use scale = 1 (default) # Recursively generate radial tree treeRoot = self.createRadialTree( self.fanout, self.depth, self.spread, isRoot=True) # creates full hierarchy and returns root actor treeRoot.components['Transform'] = Transform( rotation=np.random.uniform(-pi / 2, pi / 2, size=3), scale=self.rootScale, actor=treeRoot ) # NOTE random rotation ensures child vectors are not always generated close to the same canonical vectors treeRoot.components['Material'] = Material(color=self.rootColor, actor=treeRoot) treeRoot.components['Mesh'] = Mesh.getMesh(src=self.treeRootModelFile, actor=treeRoot) # Attach this hierarchy to current actor self.actor.children.append(treeRoot)
def __init__(self, **argd): """x.__init__(...) initializes x; see x.__class__.__doc__ for signature""" super(Container, self).__init__() # get transformation data and convert to vectors self.position = Vector( *argd.get("position", (0,0,0)) ) self.rotation = Vector( *argd.get("rotation", (0.0,0.0,0.0)) ) self.scaling = Vector( *argd.get("scaling", (1,1,1) ) ) # for detection of changes self.oldrot = Vector() self.oldpos = Vector() self.oldscaling = Vector() # inital apply trasformations self.transform = Transform() self.components = [] self.rel_positions = {} self.rel_rotations = {} self.rel_scalings = {} self.poscomms = {} self.rotcomms = {} self.scacomms = {} contents = argd.get("contents", None) if contents is not None: for (comp, params) in contents.items(): self.addElement(comp, **params)
class test: def __init__(self): GameObject.init_object(self) # Components definitions self.transform = Transform(Vector2(0, 0, 0), Vector2(0, 0, 0), Vector2(0, 0, 0)) self.sprite = Resources.load("flamme.png") self.sprite = pygame.transform.scale(self.sprite, (50, 50)) self.animator = AnimatorController() def update(self): self.transform.translate(Vector2(1, 1, 0)) if Input.get_key(K_SPACE): self.transform.translate(Vector2(0, -5, 0)) pass
def __init__(self, yml=None, variables=None, switch_expect=None): if yml is not None: self.__variables = variables self.__variables.set_vars(yml.get('vars', [])) self.__opts = Opts(yml.get('opts', []), variables) self.__set_input(yml['input']) self.__transform = Transform( yml.get('transform', ''), variables, self.__opts.get('delimiter', None)) self.__query = yml.get('query', '') self.__preview = yml.get('preview', '') self.__bind = Bind(yml.get('bind', {}), self.__variables, self.__transform) self.__output = Output(yml.get('output', {}), variables) self.__switch_expect = switch_expect
def __init__(self, **argd): """x.__init__(...) initializes x; see x.__class__.__doc__ for signature""" super(OpenGLComponent, self).__init__() # get transformation data and convert to vectors self.size = Vector( *argd.get("size", (0,0,0)) ) self.position = Vector( *argd.get("position", (0,0,0)) ) self.rotation = Vector( *argd.get("rotation", (0.0,0.0,0.0)) ) self.scaling = Vector( *argd.get("scaling", (1,1,1) ) ) # for detection of changes self.oldrot = Vector() self.oldpos = Vector() self.oldscaling = Vector() self.transform = Transform() # name (mostly for debugging) self.name = argd.get("name", "nameless") # create clock self.clock = pygame.time.Clock() self.frametime = 0.0 # get display service displayservice = OpenGLDisplay.getDisplayService() # link display_signal to displayservice self.link((self,"display_signal"), displayservice)
def __init__(self): GameObject.init_object(self) # Components definitions self.transform = Transform(Vector2(100, 100, 1), Vector2(0, 0, 0), Vector2(0, 0, 0)) self.sprite = Resources.load("pompier.png") self.sprite = pygame.transform.scale(self.sprite, (100, 100))
def __init__(self, width, height, fname, config, scale=1): """ Constructor Parameters: ----------- width : int The width of the canvas we're displaying to the user; a separate width will be used when saving the file height : int The height of the canvas we're displaying to the user; a fname : String The OSM file name config : Configuration The configuration file we use to get all relevant settings scale : float This determines how much you scale the pen widths. You want the output image to look the same as the GUI, so we scale the pen widths accordingly """ # Initial variables root = ET.parse(fname).getroot() transform = None nodes = {} ways = {} relations = {} for child in root: if child.tag == 'node': node = Node(child.attrib, transform) nodes[node.ID] = node elif child.tag == 'way': way = Way(child) ways[way.ID] = way elif child.tag == 'relation': relation = Relation(child) relations[relation.ID] = relation elif child.tag == 'bounds': transform = Transform(float(child.attrib['minlat']), float(child.attrib['maxlat']), float(child.attrib['minlon']), float(child.attrib['maxlon']), width, height) # Bind class variables self._copyright = c.COPYRIGHT self._attribution = c.ATTRIBUTION self._license = c.LICENSE self._transform = transform self._nodes = nodes self._ways = ways self._relations = relations self._fname = fname self._scale = scale self._config = config
def __init__(self, x, y): super(ExtraLife, self).__init__() tempTransform = Transform(rotation=0, speed=0, rotationSpeed=0, x=x, y=y) self.obj = mgr.Managers.getInstance().objects.Instantiate( "ExtraLife", transform=tempTransform) self.shipThatCollected = None
def __init__(self, position, mass=1, body_count=-1, color=(1, 1, 1), ecc=1.0): self.transform = Transform(pos=position, rot=-1) self.velocity = 0 * mathutils.Vector( -0.25 * Universe.dt * self.position + (Universe.dt / 15) * Transform.random_vector()) self.mass = mass self.body_count = body_count self.color = np.array(color, dtype=np.float32) self.ecc = ecc if self.body_count < 0: self.body_count = 150000 # buffer stuff self.body_positions = np.ndarray((self.body_count, 4), dtype=np.float32) self.body_velocities = np.ndarray((self.body_count, 4), dtype=np.float32) self.body_colors = np.ndarray((self.body_count, 4), dtype=np.float32) self.body_positions_vbo = vbo.VBO(data=self.body_positions, usage=gl.GL_DYNAMIC_DRAW, target=gl.GL_ARRAY_BUFFER) self.body_velocities_vbo = vbo.VBO(data=self.body_velocities, usage=gl.GL_DYNAMIC_DRAW, target=gl.GL_ARRAY_BUFFER) self.body_colors_vbo = vbo.VBO(data=self.body_colors, usage=gl.GL_DYNAMIC_DRAW, target=gl.GL_ARRAY_BUFFER) self.body_positions_cl_buffer = None self.body_velocities_cl_buffer = None self.body_colors_cl_buffer = None self.body_positions_vbo.bind() self.body_velocities_vbo.bind() self.body_colors_vbo.bind() self.vertex_array = gl.glGenVertexArrays(1)
def printExtremeState(initialState, finalState): print("Start State : ") startState = Transform().convertStringToEightPuzzle( initialState.puzzleState) for i in range(3): for j in range(3): if startState[i][j] == 0: print("B", end=" ") else: print("T" + str(startState[i][j]), end=" ") print() print("Goal State : ") goalState = Transform().convertStringToEightPuzzle(finalState.puzzleState) for i in range(3): for j in range(3): if goalState[i][j] == 0: print("B", end=" ") else: print("T" + str(goalState[i][j]), end=" ") print()
def main( timestamp, src_logpath_format="/data1/nginxlogs/jhlogs/access_jhlogs.%(yyyymmddhhmm)s" ): yyyymmddhhmm = time.strftime('%Y%m%d%H%M', time.localtime(timestamp)) yyyymmdd = time.strftime('%Y%m%d', time.localtime(timestamp)) hhmm = time.strftime('%H%M', time.localtime(timestamp)) src_logpath = src_logpath_format % { "yyyymmddhhmm": yyyymmddhhmm, "yyyymmdd": yyyymmdd } errlog_path = errlog_path_format % { "yyyymmddhhmm": yyyymmddhhmm, "yyyymmdd": yyyymmdd, "hhmm": hhmm } transform = Transform() if src_logpath.endswith(".gz"): src_logpath_file = gzip.open(src_logpath) else: src_logpath_file = open(src_logpath) for line in src_logpath_file: try: for log in transform.transform(line): datatype = log["jhd_datatype"] # if datatype == "guaeng": # print datatype, src_logpath_format # print log filename = filename_format % { "yyyymmdd": yyyymmdd, "hhmm": hhmm, "datatype": datatype } # print filename log_line = json.dumps(log) LogStore(filename, log_line) except Exception, e: # import traceback # print traceback.print_exc() LogStore(errlog_path, "%s, %s" % (e, line))
def manhattanHeuristic(self, state): transform = Transform() currentPuzzleState = transform.convertStringToEightPuzzle(state) goalPuzzleState = transform.convertStringToEightPuzzle( self.goalState.puzzleState) currentCoOrdinate = numpy.arange(18).reshape((9, 2)) for i in range(3): for j in range(3): currentCoOrdinate[currentPuzzleState[i][j]][0] = i currentCoOrdinate[currentPuzzleState[i][j]][1] = j h = 0 for i in range(3): for j in range(3): if goalPuzzleState[i][j] != 0: h += abs(i - currentCoOrdinate[goalPuzzleState[i][j]][0]) + \ abs(j - currentCoOrdinate[goalPuzzleState[i][j]][1]) if goalPuzzleState[i][j] == 0 and isTileInclude: h += abs(i - currentCoOrdinate[goalPuzzleState[i][j]][0]) + \ abs(j - currentCoOrdinate[goalPuzzleState[i][j]][1]) return h
def __init__(self, name='obj'): self.id = GameObject.globalId GameObject.globalId += 2 self.name = name self.isActive = True self.components = [] self.path = None self.takesPartInAnimCalcs = True self.add_component(Transform())
def getAllSuccessor(self): x = [1, -1, 0, 0] y = [0, -0, 1, -1] puzzleMatrix = Transform().convertStringToEightPuzzle(self.puzzleState) for i in range(3): for j in range(3): if puzzleMatrix[i][j] == 0: blankX = i blankY = j break successorState = [] for (xMove, yMove) in zip(x, y): if 0 <= blankX + xMove < 3 and 0 <= blankY + yMove < 3: successorPuzzleMat = deepcopy(puzzleMatrix) temp = successorPuzzleMat[blankX + xMove][blankY + yMove] successorPuzzleMat[blankX + xMove][blankY + yMove] = 0 successorPuzzleMat[blankX][blankY] = temp successorState.append( Transform().convertEightPuzzleToString(successorPuzzleMat)) return successorState
def __init__(self, x, y): super(FireRateSpeedUp, self).__init__() tempTransform = Transform(rotation=0, speed=0, rotationSpeed=0, x=x, y=y) self.obj = mgr.Managers.getInstance().objects.Instantiate( "FireRateSpeedUp", transform=tempTransform) self.expirationTime = None self.shipThatCollected = None self.collected = False self.durationTime = 30 self.fireRateMultiplier = 2
def _init(self): global shader, x_id, y_id, z_id global projection_matrix_id, modelview_matrix_id global color_id # shader setup shader = gl.shaders.compileProgram( shaders.compileShader(vertex_shader, gl.GL_VERTEX_SHADER), shaders.compileShader(fragment_shader, gl.GL_FRAGMENT_SHADER), ) gl.glLinkProgram(shader) # get memory offsets for shader variables color_id = gl.glGetAttribLocation(shader, b'color') x_id = gl.glGetUniformLocation(shader, b'x') y_id = gl.glGetUniformLocation(shader, b'y') z_id = gl.glGetUniformLocation(shader, b'z') modelview_matrix_id = gl.glGetUniformLocation(shader, b'modelview_matrix') projection_matrix_id = gl.glGetUniformLocation(shader, b'projection_matrix') # initialize the scene galaxies = [] for i in range(3): pos = Transform.random_vector() * 5 #pos = mathutils.Vector((0, 0, 0)) galaxies.append( Galaxy(position=pos, mass=1, body_count=-1, color=self.COLORS[i])) self.universe = Universe(galaxies) gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE) gl.glEnable(gl.GL_PROGRAM_POINT_SIZE) #gl.glEnable(gl.GL_DEPTH_TEST) #gl.glDepthFunc(gl.GL_LESS) #gl.glDepthMask(gl.GL_FALSE) #gl.glEnable(gl.GL_DEPTH_CLAMP) gl.glEnable(gl.GL_POINT_SMOOTH) gl.glEnable(gl.GL_LINE_SMOOTH) #gl.glPointSize(2.0) gl.glLineWidth(1.0) self.last_time = time.time()
def main(timestamp, src_file_format=src_file_format): transform = Transform() yyyymmdd = time.strftime('%Y%m%d', time.localtime(timestamp)) yyyymmddhhmm = time.strftime('%Y%m%d%H%M', time.localtime(timestamp)) hhmm = time.strftime('%H%M', time.localtime(timestamp)) src_file = src_file_format % { "yyyymmdd": yyyymmdd, "yyyymmddhhmm": yyyymmddhhmm } errlog_path = errlog_path_format % {"yyyymmdd": yyyymmdd, "hhmm": hhmm} with open(src_file) as f: for line in f: try: for item in transform.transform(line): datatype = item['jhd_datatype'] filename = filename_format % { "yyyymmdd": yyyymmdd, "hhmm": hhmm, 'datatype': datatype } line_out = json.dumps(item, ensure_ascii=False) LogStore(filename, line_out) except Exception, e: LogStore(errlog_path, "%s, %s" % (e, line))
def applyGlobalTransform(self, tr): st = self.getState() st['scale'] = st['size'] st = Transform(st) #trans = QtGui.QTransform() #trans.translate(*translate) #trans.rotate(-rotate) #x2, y2 = trans.map(*st['pos']) #self.setAngle(st['angle']+rotate*np.pi/180.) #self.setPos([x2, y2]) st = (st * tr).saveState() st['size'] = st['scale'] self.setState(st)
def printOptimalPath(state, depth, puzzleStateMapWithItsParent): if state is None: return depth else: totalState = printOptimalPath(puzzleStateMapWithItsParent[state], depth + 1, puzzleStateMapWithItsParent) eightPuzzleConfiguration = Transform().convertStringToEightPuzzle( state) for i in range(3): for j in range(3): if eightPuzzleConfiguration[i][j] == 0: print("B", end=" ") else: print("T" + str(eightPuzzleConfiguration[i][j]), end=" ") print() print("###########################") return totalState
class Driver: if __name__ == '__main__': # Extracting data from 5 excel files extract = Extract() ds,d,os,sa,ea= extract.getAllData() # Transforming data to obtain additional columns transform = Transform() ds,d= transform.transformAllData(ds,d) # Dropping unnecessary columns dropColumns = DropColumns() drivingSearch,delivery,orders,startAddresses,endAddresses= dropColumns.dropAllColumns(ds,d,os,sa,ea) # joining the 5 tables merge=Merge() finalData = merge.mergeAllTables(drivingSearch,delivery,orders,startAddresses,endAddresses) # Converting the date columns from Object type to DateTime finalData= transform.transformdate(finalData) sqlload=SQL_Load() sqlload.loadDataToStaging(finalData)
def on_data(self, data): dict_data = json.loads(data) tweet_label = Transform.get_sentiment(Transform, dict_data["text"]) if tweet_label != 'uncategorized' and ('RT @' not in dict_data['text']): #print(tweet_label + " : " + dict_data["text"]) self.es.index( index="tweets_data", doc_type="tweet", body={ "author": dict_data["user"]["screen_name"], "date": dict_data["created_at"], "label": tweet_label, "message": dict_data["text"], "company_name": keyword }, ) return True
class Container(Axon.AdaptiveCommsComponent.AdaptiveCommsComponent): """\ Container(...) -> A new Container component. A container to control several OpenGLComponents. Keyword arguments: - position -- Initial container position (default=(0,0,0)). - rotation -- Initial container rotation (default=(0,0,0)). - scaling -- Initial container scaling (default=(1,1,1)). - contents -- Nested dictionary of contained components. """ Inboxes = { "inbox": "", "control": "For shutdown messages", "position" : "receive position triple (x,y,z)", "rotation": "receive rotation triple (x,y,z)", "scaling": "receive scaling triple (x,y,z)", "rel_position" : "receive position triple (x,y,z)", "rel_rotation": "receive rotation triple (x,y,z)", "rel_scaling": "receive scaling triple (x,y,z)", } Outboxes = { "outbox": "", "signal": "For shutdown messages" } def __init__(self, **argd): """x.__init__(...) initializes x; see x.__class__.__doc__ for signature""" super(Container, self).__init__() # get transformation data and convert to vectors self.position = Vector( *argd.get("position", (0,0,0)) ) self.rotation = Vector( *argd.get("rotation", (0.0,0.0,0.0)) ) self.scaling = Vector( *argd.get("scaling", (1,1,1) ) ) # for detection of changes self.oldrot = Vector() self.oldpos = Vector() self.oldscaling = Vector() # inital apply trasformations self.transform = Transform() self.components = [] self.rel_positions = {} self.rel_rotations = {} self.rel_scalings = {} self.poscomms = {} self.rotcomms = {} self.scacomms = {} contents = argd.get("contents", None) if contents is not None: for (comp, params) in contents.items(): self.addElement(comp, **params) def main(self): while 1: while self.dataReady("control"): cmsg = self.recv("control") if isinstance(cmsg, producerFinished) or isinstance(cmsg, shutdownMicroprocess): self.send(cmsg, "signal") return self.handleMovement() self.applyTransforms() yield 1 def handleMovement(self): """ Handle movement commands received by corresponding inboxes. """ while self.dataReady("position"): pos = self.recv("position") self.position = Vector(*pos) while self.dataReady("rotation"): rot = self.recv("rotation") self.rotation = Vector(*rot) while self.dataReady("scaling"): scaling = self.recv("scaling") self.scaling = Vector(*scaling) while self.dataReady("rel_position"): self.position += Vector(*self.recv("rel_position")) while self.dataReady("rel_rotation"): self.rotation += Vector(*self.recv("rel_rotation")) while self.dataReady("rel_scaling"): self.scaling = Vector(*self.recv("rel_scaling")) def applyTransforms(self): """ Use the objects translation/rotation/scaling values to generate a new transformation Matrix if changes have happened. """ # generate new transformation matrix if needed if self.oldscaling != self.scaling or self.oldrot != self.rotation or self.oldpos != self.position: self.transform = Transform() self.transform.applyScaling(self.scaling) self.transform.applyRotation(self.rotation) self.transform.applyTranslation(self.position) self.oldpos = self.position.copy() self.oldrot = self.rotation.copy() self.oldscaling = self.scaling.copy() self.rearangeContents() def rearangeContents(self): for comp in self.components: trans = self.transform.transformVector(self.rel_positions[comp]) self.send(trans.toTuple(), self.poscomms[comp]) # self.send(self.rotation.toTuple(), self.rotcomms[comp]) self.send(self.scaling.toTuple(), self.scacomms[comp]) def addElement(self, comp, position=(0,0,0), rotation=(0,0,0), scaling=(1,1,1) ): self.components.append(comp) self.rel_positions[comp] = Vector( *position ) self.rel_rotations[comp] = Vector( *rotation ) self.rel_scalings[comp] = Vector( *scaling ) self.poscomms[comp] = self.addOutbox("pos") self.link( (self, self.poscomms[comp]), (comp, "position") ) # self.rotcomms[comp] = self.addOutbox("rot") # self.link( (self, self.rotcomms[comp]), (comp, "rotation") ) self.scacomms[comp] = self.addOutbox("sca") self.link( (self, self.scacomms[comp]), (comp, "scaling") ) self.rearangeContents() def removeElement(self, comp): self.components.remove(comp) self.rel_positions.pop(comp) self.rel_rotations.pop(comp) self.rel_scalings.pop(comp) # todo: unlink self.poscomms.pop(comp) self.rotcomms.pop(comp) self.scacomms.pop(comp) self.rearangeContents()
class ZVISelectFrame(wx.Frame): """class extending wx.Frame for highest level handling of GUI components """ ID_RELATIVEMOTION = wx.NewId() ID_EDIT_CAMERA_SETTINGS = wx.NewId() ID_EDIT_SMARTSEM_SETTINGS = wx.NewId() ID_SORTPOINTS = wx.NewId() ID_SHOWNUMBERS = wx.NewId() ID_SAVETRANSFORM = wx.NewId() ID_EDITTRANSFORM = wx.NewId() ID_FLIPVERT = wx.NewId() ID_FULLRES = wx.NewId() ID_SAVE_SETTINGS = wx.NewId() def __init__(self, parent, title): """default init function for a wx.Frame keywords: parent)parent window to associate it with title) title of the """ #default metadata info and image file, remove for release #default_meta="" #default_image="" #recursively call old init function wx.Frame.__init__(self, parent, title=title, size=(1400,885),pos=(5,5)) self.cfg = wx.Config('settings') #setup menu menubar = wx.MenuBar() options = wx.Menu() transformMenu = wx.Menu() SmartSEM_Menu = wx.Menu() #setup the menu options self.relative_motion = options.Append(self.ID_RELATIVEMOTION, 'Relative motion?', 'Move points in the ribbon relative to the apparent curvature, else in absolution coordinates',kind=wx.ITEM_CHECK) self.sort_points = options.Append(self.ID_SORTPOINTS,'Sort positions?','Should the program automatically sort the positions by their X coordinate from right to left?',kind=wx.ITEM_CHECK) self.show_numbers = options.Append(self.ID_SHOWNUMBERS,'Show numbers?','Display a number next to each position to show the ordering',kind=wx.ITEM_CHECK) self.flipvert = options.Append(self.ID_FLIPVERT,'Flip Image Vertically?','Display the image flipped vertically relative to the way it was meant to be displayed',kind=wx.ITEM_CHECK) self.fullResOpt = options.Append(self.ID_FULLRES,'Load full resolution (speed vs memory)','Rather than loading a 10x downsampled ',kind=wx.ITEM_CHECK) self.saveSettings = options.Append(self.ID_SAVE_SETTINGS,'Save Settings','Saves current configuration settings to config file that will be loaded automatically',kind=wx.ITEM_NORMAL) options.Check(self.ID_RELATIVEMOTION,self.cfg.ReadBool('relativemotion',True)) options.Check(self.ID_SORTPOINTS,True) options.Check(self.ID_SHOWNUMBERS,False) options.Check(self.ID_FLIPVERT,self.cfg.ReadBool('flipvert',False)) options.Check(self.ID_FULLRES,self.cfg.ReadBool('fullres',False)) self.edit_transform = options.Append(self.ID_EDIT_CAMERA_SETTINGS,'Edit Camera Properties...','Edit the size of the camera chip and the pixel size',kind=wx.ITEM_NORMAL) self.Bind(wx.EVT_MENU, self.SaveSettings, id=self.ID_SAVE_SETTINGS) self.Bind(wx.EVT_MENU, self.ToggleRelativeMotion, id=self.ID_RELATIVEMOTION) self.Bind(wx.EVT_MENU, self.ToggleSortOption, id=self.ID_SORTPOINTS) self.Bind(wx.EVT_MENU, self.ToggleShowNumbers,id=self.ID_SHOWNUMBERS) self.Bind(wx.EVT_MENU, self.EditCameraSettings, id=self.ID_EDIT_CAMERA_SETTINGS) self.save_transformed = transformMenu.Append(self.ID_SAVETRANSFORM,'Save Transformed?',\ 'Rather than save the coordinates in the original space, save a transformed set of coordinates according to transform configured in set_transform...',kind=wx.ITEM_CHECK) transformMenu.Check(self.ID_SAVETRANSFORM,self.cfg.ReadBool('savetransform',False)) self.edit_camera_settings = transformMenu.Append(self.ID_EDITTRANSFORM,'Edit Transform...',\ 'Edit the transform used to save transformed coordinates, by setting corresponding points and fitting a model',kind=wx.ITEM_NORMAL) self.Bind(wx.EVT_MENU, self.EditTransform, id=self.ID_EDITTRANSFORM) self.Transform = Transform() self.Transform.load_settings(self.cfg) self.edit_smartsem_settings = SmartSEM_Menu.Append(self.ID_EDIT_SMARTSEM_SETTINGS,'Edit SmartSEMSettings',\ 'Edit the settings used to set the magnification, rotation,tilt, Z position, and working distance of SEM software in position list',kind=wx.ITEM_NORMAL) self.Bind(wx.EVT_MENU, self.EditSmartSEMSettings, id=self.ID_EDIT_SMARTSEM_SETTINGS) menubar.Append(options, '&Options') menubar.Append(transformMenu,'&Transform') menubar.Append(SmartSEM_Menu,'&Platform Options') self.SetMenuBar(menubar) #setup a mosaic panel self.mosaicCanvas=MosaicPanel(self,config=self.cfg) #setup a file picker for the metadata selector #self.meta_label=wx.StaticText(self,id=wx.ID_ANY,label="metadata file") #self.meta_filepicker=wx.FilePickerCtrl(self,message='Select a metadata file',\ #path="",name='metadataFilePickerCtrl1',\ #style=wx.FLP_USE_TEXTCTRL, size=wx.Size(300,20),wildcard='*.*') #self.meta_filepicker.SetPath(self.cfg.Read('default_metadatapath',"")) #self.meta_formatBox=wx.ComboBox(self,id=wx.ID_ANY,value='ZeissXML',\ #size=wx.DefaultSize,choices=['ZVI','ZeissXML','SimpleCSV','ZeissCZI'], name='File Format For Meta Data') #self.meta_formatBox.SetEditable(False) #self.meta_load_button=wx.Button(self,id=wx.ID_ANY,label="Load",name="metadata load") #self.meta_enter_button=wx.Button(self,id=wx.ID_ANY,label="Edit",name="manual meta") #define the image file picker components self.imgCollectLabel=wx.StaticText(self,id=wx.ID_ANY,label="image collection directory") self.imgCollectDirPicker=wx.DirPickerCtrl(self,message='Select a directory to store images',\ path="",name='imgCollectPickerCtrl1',\ style=wx.FLP_USE_TEXTCTRL, size=wx.Size(300,20)) self.imgCollectDirPicker.SetPath(self.cfg.Read('default_imagepath',"")) self.imgCollect_load_button=wx.Button(self,id=wx.ID_ANY,label="Load",name="imgCollect load") #wire up the button to the "OnLoad" button self.Bind(wx.EVT_BUTTON, self.OnImageCollectLoad,self.imgCollect_load_button) #self.Bind(wx.EVT_BUTTON, self.OnMetaLoad,self.meta_load_button) #self.Bind(wx.EVT_BUTTON, self.OnEditImageMetadata,self.meta_enter_button) #define the array picker components self.array_label=wx.StaticText(self,id=wx.ID_ANY,label="array file") self.array_filepicker=wx.FilePickerCtrl(self,message='Select an array file',\ path="",name='arrayFilePickerCtrl1',\ style=wx.FLP_USE_TEXTCTRL, size=wx.Size(300,20),wildcard='*.*') self.array_filepicker.SetPath(self.cfg.Read('default_arraypath',"")) self.array_load_button=wx.Button(self,id=wx.ID_ANY,label="Load",name="load button") self.array_formatBox=wx.ComboBox(self,id=wx.ID_ANY,value='AxioVision',\ size=wx.DefaultSize,choices=['uManager','AxioVision','SmartSEM','OMX','ZEN'], name='File Format For Position List') self.array_formatBox.SetEditable(False) self.array_save_button=wx.Button(self,id=wx.ID_ANY,label="Save",name="save button") self.array_saveframes_button=wx.Button(self,id=wx.ID_ANY,label="Save Frames",name="save-frames button") #wire up the button to the "OnLoad" button self.Bind(wx.EVT_BUTTON, self.OnArrayLoad,self.array_load_button) self.Bind(wx.EVT_BUTTON, self.OnArraySave,self.array_save_button) self.Bind(wx.EVT_BUTTON, self.OnArraySaveFrames,self.array_saveframes_button) #define a horizontal sizer for them and place the file picker components in there #self.meta_filepickersizer=wx.BoxSizer(wx.HORIZONTAL) #self.meta_filepickersizer.Add(self.meta_label,0,wx.EXPAND) #self.meta_filepickersizer.Add(self.meta_filepicker,1,wx.EXPAND) #self.meta_filepickersizer.Add(wx.StaticText(self,id=wx.ID_ANY,label="Metadata Format:")) #self.meta_filepickersizer.Add(self.meta_formatBox,0,wx.EXPAND) #self.meta_filepickersizer.Add(self.meta_load_button,0,wx.EXPAND) #self.meta_filepickersizer.Add(self.meta_enter_button,0,wx.EXPAND) #define a horizontal sizer for them and place the file picker components in there self.imgCollect_filepickersizer=wx.BoxSizer(wx.HORIZONTAL) self.imgCollect_filepickersizer.Add(self.imgCollectLabel,0,wx.EXPAND) self.imgCollect_filepickersizer.Add(self.imgCollectDirPicker,1,wx.EXPAND) self.imgCollect_filepickersizer.Add(self.imgCollect_load_button,0,wx.EXPAND) #define a horizontal sizer for them and place the file picker components in there self.array_filepickersizer=wx.BoxSizer(wx.HORIZONTAL) self.array_filepickersizer.Add(self.array_label,0,wx.EXPAND) self.array_filepickersizer.Add(self.array_filepicker,1,wx.EXPAND) self.array_filepickersizer.Add(wx.StaticText(self,id=wx.ID_ANY,label="Format:")) self.array_filepickersizer.Add(self.array_formatBox,0,wx.EXPAND) self.array_filepickersizer.Add(self.array_load_button,0,wx.EXPAND) self.array_filepickersizer.Add(self.array_save_button,0,wx.EXPAND) self.array_filepickersizer.Add(self.array_saveframes_button,0,wx.EXPAND) #define the overall vertical sizer for the frame self.sizer = wx.BoxSizer(wx.VERTICAL) #place the filepickersizer into the vertical arrangement self.sizer.Add(self.imgCollect_filepickersizer,0,wx.EXPAND) #self.sizer.Add(self.meta_filepickersizer,0,wx.EXPAND) self.sizer.Add(self.array_filepickersizer,0,wx.EXPAND) self.sizer.Add(self.mosaicCanvas.get_toolbar(), 0, wx.LEFT | wx.EXPAND) self.sizer.Add(self.mosaicCanvas, 0, wx.EXPAND) #self.poslist_set=False #set the overall sizer and autofit everything self.SetSizer(self.sizer) self.SetAutoLayout(1) self.Bind(wx.EVT_CHAR_HOOK, self.OnKeyPress) #self.sizer.Fit(self) self.Show(True) self.SmartSEMSettings=SmartSEMSettings() #self.OnImageLoad() #self.OnArrayLoad() #self.mosaicCanvas.draw() def SaveSettings(self,event="none"): #save the transform parameters self.Transform.save_settings(self.cfg) #save the menu options self.cfg.WriteBool('relativemotion',self.relative_motion.IsChecked()) #self.cfg.WriteBool('flipvert',self.flipvert.IsChecked()) #self.cfg.WriteBool('fullres',self.fullResOpt.IsChecked()) self.cfg.WriteBool('savetransform',self.save_transformed.IsChecked()) #save the camera settings #self.mosaicCanvas.posList.camera_settings.save_settings(self.cfg) #save the mosaic options self.mosaicCanvas.posList.mosaic_settings.save_settings(self.cfg) #save the SEMSettings self.SmartSEMSettings.save_settings(self.cfg) self.cfg.Write('default_imagepath',self.imgCollectDirPicker.GetPath()) #self.cfg.Write('default_metadatapath',self.meta_filepicker.GetPath()) self.cfg.Write('default_arraypath',self.array_filepicker.GetPath()) def OnKeyPress(self,event="none"): """forward the key press event to the mosaicCanvas handler""" mpos=wx.GetMousePosition() mcrect=self.mosaicCanvas.GetScreenRect() if mcrect.Contains(mpos): self.mosaicCanvas.OnKeyPress(event) else: event.Skip() def OnArrayLoad(self,event="none"): """event handler for the array load button""" if self.array_formatBox.GetValue()=='AxioVision': self.mosaicCanvas.posList.add_from_file(self.array_filepicker.GetPath()) elif self.array_formatBox.GetValue()=='OMX': print "not yet implemented" elif self.array_formatBox.GetValue()=='SmartSEM': SEMsetting=self.mosaicCanvas.posList.add_from_file_SmartSEM(self.array_filepicker.GetPath()) self.SmartSEMSettings=SEMsetting elif self.array_formatBox.GetValue()=='ZEN': self.mosaicCanvas.posList.add_from_file_ZEN(self.array_filepicker.GetPath()) self.mosaicCanvas.draw() def OnArraySave(self,event): """event handler for the array save button""" if self.array_formatBox.GetValue()=='AxioVision': if self.save_transformed.IsChecked(): self.mosaicCanvas.posList.save_position_list(self.array_filepicker.GetPath(),trans=self.Transform) else: self.mosaicCanvas.posList.save_position_list(self.array_filepicker.GetPath()) elif self.array_formatBox.GetValue()=='OMX': if self.save_transformed.IsChecked(): self.mosaicCanvas.posList.save_position_list_OMX(self.array_filepicker.GetPath(),trans=self.Transform); else: self.mosaicCanvas.posList.save_position_list_OMX(self.array_filepicker.GetPath(),trans=None); elif self.array_formatBox.GetValue()=='SmartSEM': if self.save_transformed.IsChecked(): self.mosaicCanvas.posList.save_position_list_SmartSEM(self.array_filepicker.GetPath(),SEMS=self.SmartSEMSettings,trans=self.Transform) else: self.mosaicCanvas.posList.save_position_list_SmartSEM(self.array_filepicker.GetPath(),SEMS=self.SmartSEMSettings,trans=None) elif self.array_formatBox.GetValue()=='ZEN': if self.save_transformed.IsChecked(): self.mosaicCanvas.posList.save_position_list_ZENczsh(self.array_filepicker.GetPath(),trans=self.Transform,planePoints=self.planePoints) else: self.mosaicCanvas.posList.save_position_list_ZENczsh(self.array_filepicker.GetPath(),trans=None,planePoints=self.planePoints) elif self.array_formatBox.GetValue()=='uManager': if self.save_transformed.IsChecked(): self.mosaicCanvas.posList.save_position_list_uM(self.array_filepicker.GetPath(),trans=self.Transform) else: self.mosaicCanvas.posList.save_position_list_uM(self.array_filepicker.GetPath(),trans=None) def OnImageCollectLoad(self,event): path=self.imgCollectDirPicker.GetPath() self.mosaicCanvas.OnLoad(path) def OnArraySaveFrames(self,event): if self.array_formatBox.GetValue()=='AxioVision': if self.save_transformed.IsChecked(): self.mosaicCanvas.posList.save_frame_list(self.array_filepicker.GetPath(),trans=self.Transform) else: self.mosaicCanvas.posList.save_frame_list(self.array_filepicker.GetPath()) elif self.array_formatBox.GetValue()=='OMX': if self.save_transformed.IsChecked(): self.mosaicCanvas.posList.save_frame_list_OMX(self.array_filepicker.GetPath(),trans=self.Transform); else: self.mosaicCanvas.posList.save_frame_list_OMX(self.array_filepicker.GetPath(),trans=None); elif self.array_formatBox.GetValue()=='SmartSEM': if self.save_transformed.IsChecked(): self.mosaicCanvas.posList.save_frame_list_SmartSEM(self.array_filepicker.GetPath(),SEMS=self.SmartSEMSettings,trans=self.Transform) else: self.mosaicCanvas.posList.save_frame_list_SmartSEM(self.array_filepicker.GetPath(),SEMS=self.SmartSEMSettings,trans=None) def ToggleRelativeMotion(self,event): """event handler for handling the toggling of the relative motion""" if self.relative_motion.IsChecked(): self.mosaicCanvas.relative_motion=(True) else: self.mosaicCanvas.relative_motion=(False) def ToggleSortOption(self,event): """event handler for handling the toggling of the relative motion""" if self.sort_points.IsChecked(): self.mosaicCanvas.posList.dosort=(True) else: self.mosaicCanvas.posList.dosort=(False) def ToggleShowNumbers(self,event): if self.show_numbers.IsChecked(): self.mosaicCanvas.posList.setNumberVisibility(True) else: self.mosaicCanvas.posList.setNumberVisibility(False) self.mosaicCanvas.draw() def EditCameraSettings(self,event): """event handler for clicking the camera setting menu button""" dlg = ChangeCameraSettings(None, -1, title="Camera Settings", settings=self.mosaicCanvas.camera_settings) dlg.ShowModal() del self.posList.camera_settings #passes the settings to the position list self.mosaicCanvas.posList.set_camera_settings(dlg.GetSettings()) dlg.Destroy() def EditSmartSEMSettings(self,event): dlg = ChangeSEMSettings(None, -1, title="Smart SEM Settings", settings=self.SmartSEMSettings) dlg.ShowModal() del self.SmartSEMSettings #passes the settings to the position list self.SmartSEMSettings=dlg.GetSettings() dlg.Destroy() def EditTransform(self,event): """event handler for clicking the edit transform menu button""" dlg = ChangeTransform(None, -1,title="Adjust Transform") dlg.ShowModal() #passes the settings to the position list #(pts_from,pts_to,transformType,flipVert,flipHoriz)=dlg.GetTransformInfo() #print transformType self.Transform=dlg.getTransform() #for index,pt in enumerate(pts_from): # (xp,yp)=self.Transform.transform(pt.x,pt.y) # print("%5.5f,%5.5f -> %5.5f,%5.5f (%5.5f, %5.5f)"%(pt.x,pt.y,xp,yp,pts_to[index].x,pts_to[index].y)) dlg.Destroy()
def __init__(self, parent, title): """default init function for a wx.Frame keywords: parent)parent window to associate it with title) title of the """ #default metadata info and image file, remove for release #default_meta="" #default_image="" #recursively call old init function wx.Frame.__init__(self, parent, title=title, size=(1400,885),pos=(5,5)) self.cfg = wx.Config('settings') #setup menu menubar = wx.MenuBar() options = wx.Menu() transformMenu = wx.Menu() SmartSEM_Menu = wx.Menu() #setup the menu options self.relative_motion = options.Append(self.ID_RELATIVEMOTION, 'Relative motion?', 'Move points in the ribbon relative to the apparent curvature, else in absolution coordinates',kind=wx.ITEM_CHECK) self.sort_points = options.Append(self.ID_SORTPOINTS,'Sort positions?','Should the program automatically sort the positions by their X coordinate from right to left?',kind=wx.ITEM_CHECK) self.show_numbers = options.Append(self.ID_SHOWNUMBERS,'Show numbers?','Display a number next to each position to show the ordering',kind=wx.ITEM_CHECK) self.flipvert = options.Append(self.ID_FLIPVERT,'Flip Image Vertically?','Display the image flipped vertically relative to the way it was meant to be displayed',kind=wx.ITEM_CHECK) self.fullResOpt = options.Append(self.ID_FULLRES,'Load full resolution (speed vs memory)','Rather than loading a 10x downsampled ',kind=wx.ITEM_CHECK) self.saveSettings = options.Append(self.ID_SAVE_SETTINGS,'Save Settings','Saves current configuration settings to config file that will be loaded automatically',kind=wx.ITEM_NORMAL) options.Check(self.ID_RELATIVEMOTION,self.cfg.ReadBool('relativemotion',True)) options.Check(self.ID_SORTPOINTS,True) options.Check(self.ID_SHOWNUMBERS,False) options.Check(self.ID_FLIPVERT,self.cfg.ReadBool('flipvert',False)) options.Check(self.ID_FULLRES,self.cfg.ReadBool('fullres',False)) self.edit_transform = options.Append(self.ID_EDIT_CAMERA_SETTINGS,'Edit Camera Properties...','Edit the size of the camera chip and the pixel size',kind=wx.ITEM_NORMAL) self.Bind(wx.EVT_MENU, self.SaveSettings, id=self.ID_SAVE_SETTINGS) self.Bind(wx.EVT_MENU, self.ToggleRelativeMotion, id=self.ID_RELATIVEMOTION) self.Bind(wx.EVT_MENU, self.ToggleSortOption, id=self.ID_SORTPOINTS) self.Bind(wx.EVT_MENU, self.ToggleShowNumbers,id=self.ID_SHOWNUMBERS) self.Bind(wx.EVT_MENU, self.EditCameraSettings, id=self.ID_EDIT_CAMERA_SETTINGS) self.save_transformed = transformMenu.Append(self.ID_SAVETRANSFORM,'Save Transformed?',\ 'Rather than save the coordinates in the original space, save a transformed set of coordinates according to transform configured in set_transform...',kind=wx.ITEM_CHECK) transformMenu.Check(self.ID_SAVETRANSFORM,self.cfg.ReadBool('savetransform',False)) self.edit_camera_settings = transformMenu.Append(self.ID_EDITTRANSFORM,'Edit Transform...',\ 'Edit the transform used to save transformed coordinates, by setting corresponding points and fitting a model',kind=wx.ITEM_NORMAL) self.Bind(wx.EVT_MENU, self.EditTransform, id=self.ID_EDITTRANSFORM) self.Transform = Transform() self.Transform.load_settings(self.cfg) self.edit_smartsem_settings = SmartSEM_Menu.Append(self.ID_EDIT_SMARTSEM_SETTINGS,'Edit SmartSEMSettings',\ 'Edit the settings used to set the magnification, rotation,tilt, Z position, and working distance of SEM software in position list',kind=wx.ITEM_NORMAL) self.Bind(wx.EVT_MENU, self.EditSmartSEMSettings, id=self.ID_EDIT_SMARTSEM_SETTINGS) menubar.Append(options, '&Options') menubar.Append(transformMenu,'&Transform') menubar.Append(SmartSEM_Menu,'&Platform Options') self.SetMenuBar(menubar) #setup a mosaic panel self.mosaicCanvas=MosaicPanel(self,config=self.cfg) #setup a file picker for the metadata selector #self.meta_label=wx.StaticText(self,id=wx.ID_ANY,label="metadata file") #self.meta_filepicker=wx.FilePickerCtrl(self,message='Select a metadata file',\ #path="",name='metadataFilePickerCtrl1',\ #style=wx.FLP_USE_TEXTCTRL, size=wx.Size(300,20),wildcard='*.*') #self.meta_filepicker.SetPath(self.cfg.Read('default_metadatapath',"")) #self.meta_formatBox=wx.ComboBox(self,id=wx.ID_ANY,value='ZeissXML',\ #size=wx.DefaultSize,choices=['ZVI','ZeissXML','SimpleCSV','ZeissCZI'], name='File Format For Meta Data') #self.meta_formatBox.SetEditable(False) #self.meta_load_button=wx.Button(self,id=wx.ID_ANY,label="Load",name="metadata load") #self.meta_enter_button=wx.Button(self,id=wx.ID_ANY,label="Edit",name="manual meta") #define the image file picker components self.imgCollectLabel=wx.StaticText(self,id=wx.ID_ANY,label="image collection directory") self.imgCollectDirPicker=wx.DirPickerCtrl(self,message='Select a directory to store images',\ path="",name='imgCollectPickerCtrl1',\ style=wx.FLP_USE_TEXTCTRL, size=wx.Size(300,20)) self.imgCollectDirPicker.SetPath(self.cfg.Read('default_imagepath',"")) self.imgCollect_load_button=wx.Button(self,id=wx.ID_ANY,label="Load",name="imgCollect load") #wire up the button to the "OnLoad" button self.Bind(wx.EVT_BUTTON, self.OnImageCollectLoad,self.imgCollect_load_button) #self.Bind(wx.EVT_BUTTON, self.OnMetaLoad,self.meta_load_button) #self.Bind(wx.EVT_BUTTON, self.OnEditImageMetadata,self.meta_enter_button) #define the array picker components self.array_label=wx.StaticText(self,id=wx.ID_ANY,label="array file") self.array_filepicker=wx.FilePickerCtrl(self,message='Select an array file',\ path="",name='arrayFilePickerCtrl1',\ style=wx.FLP_USE_TEXTCTRL, size=wx.Size(300,20),wildcard='*.*') self.array_filepicker.SetPath(self.cfg.Read('default_arraypath',"")) self.array_load_button=wx.Button(self,id=wx.ID_ANY,label="Load",name="load button") self.array_formatBox=wx.ComboBox(self,id=wx.ID_ANY,value='AxioVision',\ size=wx.DefaultSize,choices=['uManager','AxioVision','SmartSEM','OMX','ZEN'], name='File Format For Position List') self.array_formatBox.SetEditable(False) self.array_save_button=wx.Button(self,id=wx.ID_ANY,label="Save",name="save button") self.array_saveframes_button=wx.Button(self,id=wx.ID_ANY,label="Save Frames",name="save-frames button") #wire up the button to the "OnLoad" button self.Bind(wx.EVT_BUTTON, self.OnArrayLoad,self.array_load_button) self.Bind(wx.EVT_BUTTON, self.OnArraySave,self.array_save_button) self.Bind(wx.EVT_BUTTON, self.OnArraySaveFrames,self.array_saveframes_button) #define a horizontal sizer for them and place the file picker components in there #self.meta_filepickersizer=wx.BoxSizer(wx.HORIZONTAL) #self.meta_filepickersizer.Add(self.meta_label,0,wx.EXPAND) #self.meta_filepickersizer.Add(self.meta_filepicker,1,wx.EXPAND) #self.meta_filepickersizer.Add(wx.StaticText(self,id=wx.ID_ANY,label="Metadata Format:")) #self.meta_filepickersizer.Add(self.meta_formatBox,0,wx.EXPAND) #self.meta_filepickersizer.Add(self.meta_load_button,0,wx.EXPAND) #self.meta_filepickersizer.Add(self.meta_enter_button,0,wx.EXPAND) #define a horizontal sizer for them and place the file picker components in there self.imgCollect_filepickersizer=wx.BoxSizer(wx.HORIZONTAL) self.imgCollect_filepickersizer.Add(self.imgCollectLabel,0,wx.EXPAND) self.imgCollect_filepickersizer.Add(self.imgCollectDirPicker,1,wx.EXPAND) self.imgCollect_filepickersizer.Add(self.imgCollect_load_button,0,wx.EXPAND) #define a horizontal sizer for them and place the file picker components in there self.array_filepickersizer=wx.BoxSizer(wx.HORIZONTAL) self.array_filepickersizer.Add(self.array_label,0,wx.EXPAND) self.array_filepickersizer.Add(self.array_filepicker,1,wx.EXPAND) self.array_filepickersizer.Add(wx.StaticText(self,id=wx.ID_ANY,label="Format:")) self.array_filepickersizer.Add(self.array_formatBox,0,wx.EXPAND) self.array_filepickersizer.Add(self.array_load_button,0,wx.EXPAND) self.array_filepickersizer.Add(self.array_save_button,0,wx.EXPAND) self.array_filepickersizer.Add(self.array_saveframes_button,0,wx.EXPAND) #define the overall vertical sizer for the frame self.sizer = wx.BoxSizer(wx.VERTICAL) #place the filepickersizer into the vertical arrangement self.sizer.Add(self.imgCollect_filepickersizer,0,wx.EXPAND) #self.sizer.Add(self.meta_filepickersizer,0,wx.EXPAND) self.sizer.Add(self.array_filepickersizer,0,wx.EXPAND) self.sizer.Add(self.mosaicCanvas.get_toolbar(), 0, wx.LEFT | wx.EXPAND) self.sizer.Add(self.mosaicCanvas, 0, wx.EXPAND) #self.poslist_set=False #set the overall sizer and autofit everything self.SetSizer(self.sizer) self.SetAutoLayout(1) self.Bind(wx.EVT_CHAR_HOOK, self.OnKeyPress) #self.sizer.Fit(self) self.Show(True) self.SmartSEMSettings=SmartSEMSettings()
class OpenGLComponent(Axon.AdaptiveCommsComponent.AdaptiveCommsComponent): """\ OpenGLComponent(...) -> create a new OpenGL component (not very useful though; it is rather designed to inherit from). This components implements the interaction with the OpenGLDisplay service that is needed to setup, draw and move an object using OpenGL. Keyword arguments: - size -- three dimensional size of component (default=(0,0,0)) - rotation -- rotation of component around (x,y,z) axis (defaul=(0,0,0)) - scaling -- scaling along the (x,y,z) axis (default=(1,1,1)) - position -- three dimensional position (default=(0,0,0)) - name -- name of component (mostly for debugging, default="nameless") """ Inboxes = { "inbox": "not used", "control": "For shutdown messages", "callback": "for the response after a displayrequest", "events": "Input events", "position" : "receive position triple (x,y,z)", "rotation": "receive rotation triple (x,y,z)", "scaling": "receive scaling triple (x,y,z)", "rel_position" : "receive position triple (x,y,z)", "rel_rotation": "receive rotation triple (x,y,z)", "rel_scaling": "receive scaling triple (x,y,z)", } Outboxes = { "outbox": "not used", "signal": "For shutdown messages", "display_signal" : "Outbox used for communicating to the display surface", "position" : "send position status when updated", "rotation": "send rotation status when updated", "scaling": "send scaling status when updated", } def __init__(self, **argd): """x.__init__(...) initializes x; see x.__class__.__doc__ for signature""" super(OpenGLComponent, self).__init__() # get transformation data and convert to vectors self.size = Vector( *argd.get("size", (0,0,0)) ) self.position = Vector( *argd.get("position", (0,0,0)) ) self.rotation = Vector( *argd.get("rotation", (0.0,0.0,0.0)) ) self.scaling = Vector( *argd.get("scaling", (1,1,1) ) ) # for detection of changes self.oldrot = Vector() self.oldpos = Vector() self.oldscaling = Vector() self.transform = Transform() # name (mostly for debugging) self.name = argd.get("name", "nameless") # create clock self.clock = pygame.time.Clock() self.frametime = 0.0 # get display service displayservice = OpenGLDisplay.getDisplayService() # link display_signal to displayservice self.link((self,"display_signal"), displayservice) def main(self): # create display request self.disprequest = { "OGL_DISPLAYREQUEST" : True, "objectid" : id(self), "callback" : (self,"callback"), "events" : (self, "events"), "size": self.size } # send display request self.send(self.disprequest, "display_signal") # inital apply trasformations self.applyTransforms() # setup function from derived objects self.setup() # initial draw to display list self.redraw() # wait for response on displayrequest while not self.dataReady("callback"): yield 1 self.identifier = self.recv("callback") while 1: yield 1 while self.dataReady("control"): cmsg = self.recv("control") if isinstance(cmsg, producerFinished) or isinstance(cmsg, shutdownMicroprocess): self.send(cmsg, "signal") return self.frametime = float(self.clock.tick())/1000.0 self.handleMovement() self.handleEvents() self.applyTransforms() # frame function from derived objects self.frame() while not self.anyReady(): self.pause() yield 1 def applyTransforms(self): """ Use the objects translation/rotation/scaling values to generate a new transformation Matrix if changes have happened. """ # generate new transformation matrix if needed if self.oldscaling != self.scaling or self.oldrot != self.rotation or self.oldpos != self.position: self.transform = Transform() self.transform.applyScaling(self.scaling) self.transform.applyRotation(self.rotation) self.transform.applyTranslation(self.position) if self.oldscaling != self.scaling: self.send(self.scaling.toTuple(), "scaling") self.oldscaling = self.scaling.copy() if self.oldrot != self.rotation: self.send(self.rotation.toTuple(), "rotation") self.oldrot = self.rotation.copy() if self.oldpos != self.position: self.send(self.position.toTuple(), "position") self.oldpos = self.position.copy() # send new transform to display service transform_update = { "TRANSFORM_UPDATE": True, "objectid": id(self), "transform": self.transform } self.send(transform_update, "display_signal") def handleMovement(self): """ Handle movement commands received by corresponding inboxes. """ while self.dataReady("position"): pos = self.recv("position") self.position = Vector(*pos) while self.dataReady("rotation"): rot = self.recv("rotation") self.rotation = Vector(*rot) while self.dataReady("scaling"): scaling = self.recv("scaling") self.scaling = Vector(*scaling) while self.dataReady("rel_position"): self.position += Vector(*self.recv("rel_position")) while self.dataReady("rel_rotation"): self.rotation += Vector(*self.recv("rel_rotation")) while self.dataReady("rel_scaling"): self.scaling = Vector(*self.recv("rel_scaling")) ## # Methods to be used by derived objects ## def addListenEvents(self, events): """\ Sends listening request for pygame events to the display service. The events parameter is expected to be a list of pygame event constants. """ for event in events: self.send({"ADDLISTENEVENT":event, "objectid":id(self)}, "display_signal") def removeListenEvents(self, events): """\ Sends stop listening request for pygame events to the display service. The events parameter is expected to be a list of pygame event constants. """ for event in events: self.send({"REMOVELISTENEVENT":event, "objectid":id(self)}, "display_signal") def redraw(self): """\ Invoke draw() and save its commands to a newly generated displaylist. The displaylist name is then sent to the display service via a "DISPLAYLIST_UPDATE" request. """ # display list id displaylist = glGenLists(1); # draw object to its displaylist glNewList(displaylist, GL_COMPILE) self.draw() glEndList() dl_update = { "DISPLAYLIST_UPDATE": True, "objectid": id(self), "displaylist": displaylist } self.send(dl_update, "display_signal") ## # Method stubs to be overridden by derived objects ## def handleEvents(self): """ Method stub Override this method to do event handling inside. Should look like this:: while self.dataReady("events"): event = self.recv("events") # handle event ... """ pass def draw(self): """ Method stub Override this method for drawing. Only use commands which are needed for drawing. Will not draw directly but be saved to a displaylist. Therefore, make sure not to use any commands which cannot be stored in displaylists (unlikely anyway). """ pass def setup(self): """ Method stub Override this method for component setup. It will be called on the first scheduling of the component. """ pass def frame(self): """ Method stub Override this method for operations you want to do every frame. It will be called every time the component is scheduled. Do not include infinite loops, the method has to return every time it gets called. """ pass
def __init__(self, **argd): """x.__init__(...) initializes x; see x.__class__.__doc__ for signature""" super(OpenGLDisplay,self).__init__() self.caption = argd.get("title", "http://kamaelia.sourceforge.net") self.width = argd.get("width",800) self.height = argd.get("height",600) self.background_colour = argd.get("background_colour", (255,255,255)) self.fullscreen = pygame.FULLSCREEN * argd.get("fullscreen", 0) self.show_fps = argd.get("show_fps", True) self.limit_fps = argd.get("limit_fps", 0) self.fps_delay = 0.0 # for framerate limitation self.clock = pygame.time.Clock() # 3D component handling self.ogl_objects = [] self.ogl_names = {} self.ogl_sizes = {} self.ogl_displaylists = {} self.ogl_transforms = {} self.ogl_nextName = 1 # Eventspies (receive events of other components) self.eventspies = [] # pygame component handling self.pygame_surfaces = [] self.pygame_objectid_to_surface = {} self.pygame_sizes = {} self.pygame_positions = {} self.pygame_pow2surfaces = {} self.pygame_texnames = {} # used for surface positioning self.next_position = (0,0) # pygame wrapping self.wrappedsurfaces = [] self.wrapper_requestcomms = {} self.pending_wrapperrequests = {} # Event handling self.eventcomms = {} self.eventservices = {} self.eventswanted = {} self.hitall = argd.get("hitall", False) # determine projection parameters self.nearPlaneDist = argd.get("near", 1.0) self.farPlaneDist = argd.get("far", 100.0) self.perspectiveAngle = argd.get("perspective", 45.0) self.aspectRatio = float(self.width)/float(self.height) global pi self.farPlaneHeight = self.farPlaneDist*2.0/tan(pi/2.0-self.perspectiveAngle*pi/360.0) self.farPlaneWidth = self.farPlaneHeight*self.aspectRatio self.farPlaneScaling = self.farPlaneWidth/self.width # determine viewer position and orientation self.viewerposition = Vector(*argd.get("viewerposition", (0,0,0))) self.lookat = Vector(*argd.get("lookat", (0,0,-self.farPlaneDist))) self.up = Vector(*argd.get("up", (0,1,0))) # transform used to correct the mouse click positions self.coordCorrectionTransform = Transform() self.coordCorrectionTransform.setLookAtRotation(-self.viewerposition, self.lookat, self.up) # initialize the display pygame.init() display = pygame.display.set_mode((self.width, self.height), self.fullscreen| pygame.DOUBLEBUF | pygame.OPENGL) pygame.display.set_caption(self.caption) pygame.mixer.quit() glClearColor(self.background_colour[0],self.background_colour[1],self.background_colour[2],1) glClearDepth(1.0) glEnable(GL_DEPTH_TEST) glDepthFunc(GL_LEQUAL) glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) # make fog settings if enabled fog_dists = argd.get("fog", None) if fog_dists is not None: glEnable(GL_FOG) glFog(GL_FOG_MODE, GL_LINEAR) glHint(GL_FOG_HINT, GL_NICEST) glFog(GL_FOG_START, fog_dists[0]) glFog(GL_FOG_END, fog_dists[1]) glFog(GL_FOG_DENSITY, argd.get("fog_density", 1.0) ) fog_colour = argd.get("fog_colour", (255,255,255)) fog_colour = [float(x)/255.0 for x in fog_colour] glFog(GL_FOG_COLOR, fog_colour) # set projection matrix glMatrixMode(GL_PROJECTION) glLoadIdentity() self.setProjection()
class OpenGLDisplay(Axon.AdaptiveCommsComponent.AdaptiveCommsComponent): """\ OpenGLDisplay(...) -> new OpenGLDisplay component Use OpenGLDisplay.getDisplayService(...) in preference as it returns an existing instance, or automatically creates a new one. Or create your own and register it with setDisplayService(...) Keyword arguments (all optional): - title -- caption of window (default=http://kamaelia.sourceforge.net) - width -- pixels width (default=800) - height -- pixels height (default=600) - background_colour -- (r,g,b) background colour (default=(255,255,255)) - fullscreen -- set to True to start up fullscreen, not windowed (default=False) - show_fps -- show frames per second in window title (default=True) - limit_fps -- maximum frame rate (default=60) Projection parameters - near -- distance to near plane (default=1.0) - far -- distance to far plane (default=100.0) - perspective -- perspective angle (default=45.0) Viewer position and orientation - viewerposition -- position of viewer (default=(0,0,0)) - lookat -- look at point (default= (0,0,-self.farPlaneDist)) - up -- up vector (default(0,1,0)) Fog - fog -- tuple of fog distances (start, end). if not set, fog is disabled (default) - fog_colour -- (r,g,b) fog colour (default=(255,255,255) ) - fog_density -- fog density (default=0.35) Event processing - hitall -- boolean, if false, only the nearest object under the cursor gets activated (default=False) """ Inboxes = { "inbox" : "Default inbox, not currently used", "control" : "NOT USED", "notify" : "For reception of requests for surfaces, overlays and events", "events" : "For reception of pygame events", } Outboxes = { "outbox" : "NOT USED", "signal" : "NOT USED", } def setDisplayService(display, tracker = None): """\ Sets the given pygamedisplay as the service for the selected tracker or the default one. (static method) """ if not tracker: tracker = _cat.coordinatingassistanttracker.getcat() tracker.registerService("ogl_display", display, "notify") setDisplayService = staticmethod(setDisplayService) def getDisplayService(tracker=None): # STATIC METHOD """\ Returns any live pygamedisplay registered with the specified (or default) tracker, or creates one for the system to use. (static method) """ if tracker is None: tracker = _cat.coordinatingassistanttracker.getcat() try: service = tracker.retrieveService("ogl_display") return service except KeyError: display = OpenGLDisplay() display.activate() OpenGLDisplay.setDisplayService(display, tracker) service=(display,"notify") return service getDisplayService = staticmethod(getDisplayService) def __init__(self, **argd): """x.__init__(...) initializes x; see x.__class__.__doc__ for signature""" super(OpenGLDisplay,self).__init__() self.caption = argd.get("title", "http://kamaelia.sourceforge.net") self.width = argd.get("width",800) self.height = argd.get("height",600) self.background_colour = argd.get("background_colour", (255,255,255)) self.fullscreen = pygame.FULLSCREEN * argd.get("fullscreen", 0) self.show_fps = argd.get("show_fps", True) self.limit_fps = argd.get("limit_fps", 0) self.fps_delay = 0.0 # for framerate limitation self.clock = pygame.time.Clock() # 3D component handling self.ogl_objects = [] self.ogl_names = {} self.ogl_sizes = {} self.ogl_displaylists = {} self.ogl_transforms = {} self.ogl_nextName = 1 # Eventspies (receive events of other components) self.eventspies = [] # pygame component handling self.pygame_surfaces = [] self.pygame_objectid_to_surface = {} self.pygame_sizes = {} self.pygame_positions = {} self.pygame_pow2surfaces = {} self.pygame_texnames = {} # used for surface positioning self.next_position = (0,0) # pygame wrapping self.wrappedsurfaces = [] self.wrapper_requestcomms = {} self.pending_wrapperrequests = {} # Event handling self.eventcomms = {} self.eventservices = {} self.eventswanted = {} self.hitall = argd.get("hitall", False) # determine projection parameters self.nearPlaneDist = argd.get("near", 1.0) self.farPlaneDist = argd.get("far", 100.0) self.perspectiveAngle = argd.get("perspective", 45.0) self.aspectRatio = float(self.width)/float(self.height) global pi self.farPlaneHeight = self.farPlaneDist*2.0/tan(pi/2.0-self.perspectiveAngle*pi/360.0) self.farPlaneWidth = self.farPlaneHeight*self.aspectRatio self.farPlaneScaling = self.farPlaneWidth/self.width # determine viewer position and orientation self.viewerposition = Vector(*argd.get("viewerposition", (0,0,0))) self.lookat = Vector(*argd.get("lookat", (0,0,-self.farPlaneDist))) self.up = Vector(*argd.get("up", (0,1,0))) # transform used to correct the mouse click positions self.coordCorrectionTransform = Transform() self.coordCorrectionTransform.setLookAtRotation(-self.viewerposition, self.lookat, self.up) # initialize the display pygame.init() display = pygame.display.set_mode((self.width, self.height), self.fullscreen| pygame.DOUBLEBUF | pygame.OPENGL) pygame.display.set_caption(self.caption) pygame.mixer.quit() glClearColor(self.background_colour[0],self.background_colour[1],self.background_colour[2],1) glClearDepth(1.0) glEnable(GL_DEPTH_TEST) glDepthFunc(GL_LEQUAL) glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) # make fog settings if enabled fog_dists = argd.get("fog", None) if fog_dists is not None: glEnable(GL_FOG) glFog(GL_FOG_MODE, GL_LINEAR) glHint(GL_FOG_HINT, GL_NICEST) glFog(GL_FOG_START, fog_dists[0]) glFog(GL_FOG_END, fog_dists[1]) glFog(GL_FOG_DENSITY, argd.get("fog_density", 1.0) ) fog_colour = argd.get("fog_colour", (255,255,255)) fog_colour = [float(x)/255.0 for x in fog_colour] glFog(GL_FOG_COLOR, fog_colour) # set projection matrix glMatrixMode(GL_PROJECTION) glLoadIdentity() self.setProjection() def main(self): """Main loop.""" while 1: # limit and show fps (if enabled) self.fps_delay += self.clock.tick(self.limit_fps) if self.show_fps and self.fps_delay > 1000.0: pygame.display.set_caption("%s FPS:%d" % (self.caption, self.clock.get_fps()) ) self.fps_delay = 0 self.handleRequests() self.handleEvents() self.updateDisplay() yield 1 def handleRequests(self): """ Handles service requests. """ while self.dataReady("notify"): message = self.recv("notify") if message.get("OGL_DISPLAYREQUEST", False): self.handleRequest_OGL_DISPLAYREQUEST(message) elif message.get("EVENTSPYREQUEST", False): self.handleRequest_EVENTSPYREQUEST(message) elif message.get("DISPLAYLIST_UPDATE", False): ident = message.get("objectid") try: glDeleteLists(self.ogl_displaylists[ident], 1) except KeyError: pass self.ogl_displaylists[ident] = message.get("displaylist") elif message.get("TRANSFORM_UPDATE", False): ident = message.get("objectid") self.ogl_transforms[ident] = message.get("transform") elif message.get("ADDLISTENEVENT", False): ident = message.get("objectid") if ident is None: ident = id(message.get("surface", None)) if ident in self.wrappedsurfaces: self.send(message, self.wrapper_requestcomms[id(surface)]) else: self.eventswanted[ident][message.get("ADDLISTENEVENT")] = True elif message.get("REMOVELISTENEVENT", False): ident = message.get("objectid") if ident is None: ident = message.get("surface", None) if ident in self.wrappedsurfaces: self.send(message, self.wrapper_requestcomms[id(surface)]) else: self.eventswanted[ident][message.get("REMOVELISTENEVENT")] = False elif message.get("DISPLAYREQUEST", False): self.handleRequest_DISPLAYREQUEST(message) elif message.get("REDRAW", False): surface = message["surface"] self.updatePygameTexture(surface, self.pygame_pow2surfaces[id(surface)], self.pygame_texnames[id(surface)]) elif message.get("WRAPPERREQUEST", False): self.handleRequest_WRAPPERREQUEST(message) def handleEvents(self): """ Handles pygame input events. """ # pre-fetch all waiting events in one go events = [] for event in pygame.event.get(): if event.type == pygame.QUIT: # On pygame.QUIT send shutdownMicroprocess to signal self.send(Axon.Ipc.shutdownMicroprocess(), "signal") else: events.append(event) self.handleOGLComponentEvents(events) self.handlePygameComponentEvents(events) def updateDisplay(self): """ Draws all components, updates screen, clears the backbuffer and depthbuffer . """ self.drawOpenGLComponents() self.drawPygameSurfaces() # show frame glFlush() pygame.display.flip() # clear drawing buffer glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) def genIdentifier(self): """ Returns a unique number. """ ogl_name = self.ogl_nextName self.ogl_nextName += 1 return ogl_name def handleRequest_OGL_DISPLAYREQUEST(self, message): ident = message.get("objectid") eventservice = message.get("events", None) callbackservice = message.get("callback") # store object self.ogl_objects.append(ident) self.ogl_sizes[ident] = message.get("size") # generate and store an ogl name for the requesting object ogl_name = self.genIdentifier() self.ogl_names[ident] = ogl_name # link to eventcallback if eventservice is not None: eventcomms = self.addOutbox("eventsfeedback") self.eventcomms[ident] = eventcomms self.eventswanted[ident] = {} self.link((self,eventcomms), eventservice) # link callback service callbackcomms = self.addOutbox("displayerfeedback") self.link((self, callbackcomms), callbackservice) # send response (generated name) self.send(ogl_name, callbackcomms) def handleRequest_EVENTSPYREQUEST(self, message): ident = message.get("objectid") target = message.get("target") eventservice = message.get("events", None) callbackservice = message.get("callback") # store object id self.eventspies.append(ident) # link eventservice if eventservice is not None: eventcomms = self.addOutbox("eventsfeedback") self.eventcomms[ident] = eventcomms self.eventswanted[ident] = {} self.link((self,eventcomms), eventservice) # link callbackservice callbackcomms = self.addOutbox("displayerfeedback") self.link((self, callbackcomms), callbackservice) # send response ogl_name = self.ogl_names[target] self.send(ogl_name, callbackcomms) def calcPow2Size(self, size): """ Calculates the power of 2 dimensions for a given size. """ pow2size = (int(2**(ceil(log(size[0], 2)))), int(2**(ceil(log(size[1], 2))))) return pow2size def handleRequest_DISPLAYREQUEST(self, message): callbackservice = message["callback"] eventservice = message.get("events", None) size = message["size"] alpha = message.get("alpha", 255) transparency = message.get("transparency", None) # create surface surface = pygame.Surface(size) self.pygame_surfaces.append(surface) self.pygame_sizes[id(surface)] = size position = message.get("position", self.surfacePosition(surface)) # save objectid of requesting component # (this is needed for wrapperrequests) objectid = id(callbackservice[0]) self.pygame_objectid_to_surface[objectid] = surface # create another surface, with dimensions a power of two # this is needed because otherwise texturing is REALLY slow pow2size = self.calcPow2Size(size) pow2surface = pygame.Surface(pow2size) self.pygame_pow2surfaces[id(surface)] = pow2surface # handle transparency surface.set_alpha(alpha) if transparency: surface.set_colorkey(transparency) # save position self.pygame_positions[id(surface)] = position # handle event comms if eventservice is not None: # save eventservice for later use self.eventservices[id(surface)] = eventservice # link eventservice eventcomms = self.addOutbox("eventsfeedback") self.eventswanted[id(surface)] = {} self.link((self,eventcomms), eventservice) self.eventcomms[id(surface)] = eventcomms # generate texture name texname = glGenTextures(1) self.pygame_texnames[id(surface)] = texname # link callback communications callbackcomms = self.addOutbox("displayerfeedback") self.link((self, callbackcomms), callbackservice) # send response self.send(surface, callbackcomms) # if there is a wrapper request waiting for this component, handle it if objectid in self.pending_wrapperrequests: self.handleRequest_WRAPPERREQUEST(self.pending_wrapperrequests[objectid]) self.pending_wrapperrequests.pop(objectid) def handleRequest_WRAPPERREQUEST(self, message): objectid = message.get("wrap_objectid") # if the component that shall be wrapped is not registered yet, # save wrapper request for later handling try: surface = self.pygame_objectid_to_surface[objectid] except KeyError: self.pending_wrapperrequests[objectid] = message else: callbackservice = message["wrapcallback"] eventrequestservice = message["eventrequests"] # store surface to wrap self.wrappedsurfaces.append(id(surface)) # get and link callback comms callbackcomms = self.addOutbox("wrapfeedback") self.link((self,callbackcomms), callbackservice) # get and link eventrequest comms eventrequestcomms = self.addOutbox("eventrequests") self.link((self,eventrequestcomms), eventrequestservice) self.wrapper_requestcomms[id(surface)] = eventrequestcomms # find corresponding pow2surface pow2surface = self.pygame_pow2surfaces[id(surface)] #determine texture coordinate lengths tex_w = float(surface.get_width())/float(pow2surface.get_width()) tex_h = float(surface.get_height())/float(pow2surface.get_height()) # generate response response = { "texname": self.pygame_texnames[id(surface)], "texsize": (tex_w, tex_h), "size": self.pygame_sizes[id(surface)] } try: response["eventservice"] = self.eventservices[id(surface)] response["eventswanted"] = self.eventswanted[id(surface)] except KeyError: response["eventservice"] = None response["eventswanted"] = None # send response self.send(response, callbackcomms) def setProjection(self): """ Sets projection matrix. """ glMatrixMode(GL_PROJECTION) gluPerspective(self.perspectiveAngle, self.aspectRatio, self.nearPlaneDist, self.farPlaneDist) # apply viewer transforms gluLookAt( *(self.viewerposition.toTuple() + self.lookat.toTuple() + self.up.toTuple() ) ) def doPicking(self, pos): """\ Uses OpenGL picking to determine objects that have been hit by mouse pointer. see e.g. OpenGL Redbook """ # object picking glSelectBuffer(512) glRenderMode(GL_SELECT) # prepare matrices glMatrixMode(GL_PROJECTION) glPushMatrix() glLoadIdentity() gluPickMatrix(pos[0], self.height-pos[1], 1, 1) self.setProjection() # "draw" objects in select mode glInitNames() glPushName(0) glMatrixMode(GL_MODELVIEW) glPushMatrix() for obj in self.ogl_objects: try: glLoadMatrixf(self.ogl_transforms[obj].getMatrix()) glLoadName(self.ogl_names[obj]) glCallList(self.ogl_displaylists[obj]) except KeyError: pass glPopMatrix() # restore matrices glMatrixMode(GL_PROJECTION) glPopMatrix() # force completion glFlush() # process hits hits = glRenderMode(GL_RENDER) hitlist = [] if self.hitall: # list of hit objects hitlist = [hit[2][0] for hit in hits] else: nearest = 4294967295 for hit in hits: if hit[0] < nearest: nearest = hit[0] hitlist = [hit[2][0]] return hitlist def drawPygameSurfaces(self): """ Draws all surfaces of registered pygame components on top of everything else. """ # disable depth testing temporarely to ensure that pygame components # are on top of everything glDisable(GL_DEPTH_TEST) glEnable(GL_TEXTURE_2D) for surface in self.pygame_surfaces: # skip surfaces which get wrapped if id(surface) in self.wrappedsurfaces: continue # get needed vars texname = self.pygame_texnames[id(surface)] position = self.pygame_positions[id(surface)] size = self.pygame_sizes[id(surface)] pow2surface = self.pygame_pow2surfaces[id(surface)] # create texture if not already done if not glIsTexture(texname): self.updatePygameTexture(surface, pow2surface, texname) glBindTexture(GL_TEXTURE_2D, texname) # determine surface positions on far Plane l = position[0]*self.farPlaneScaling-self.farPlaneWidth/2 t = -position[1]*self.farPlaneScaling+self.farPlaneHeight/2 r = l + size[0]*self.farPlaneScaling b = t - size[1]*self.farPlaneScaling #determine texture coordinates tex_w = float(size[0])/float(pow2surface.get_width()) tex_h = float(size[1])/float(pow2surface.get_height()) # draw just the texture, no background glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE) # draw faces glBegin(GL_QUADS) glColor3f(1, 0, 0) glTexCoord2f(0.0, 1.0-tex_h); glVertex3f( l, b, -self.farPlaneDist) glTexCoord2f(tex_w, 1.0-tex_h); glVertex3f( r, b, -self.farPlaneDist) glTexCoord2f(tex_w, 1.0); glVertex3f( r, t, -self.farPlaneDist) glTexCoord2f(0.0, 1.0); glVertex3f( l, t, -self.farPlaneDist) glEnd() glDisable(GL_TEXTURE_2D) glEnable(GL_DEPTH_TEST) def drawOpenGLComponents(self): """ Draws all registered OpenGL components with their set transformation matrix. """ glMatrixMode(GL_MODELVIEW) glPushMatrix() for obj in self.ogl_objects: try: # load transform of component glLoadMatrixf(self.ogl_transforms[obj].getMatrix()) # call displaylist of component glCallList(self.ogl_displaylists[obj]) except KeyError: pass glPopMatrix() def updatePygameTexture(self, surface, pow2surface, texname): """ Converts the surface of a pygame component to an OpenGL texture. """ # print "UPDATE", texname # blit component surface to power of 2 sized surface pow2surface.blit(surface, (0,0)) # set surface as texture glEnable(GL_TEXTURE_2D) glBindTexture(GL_TEXTURE_2D, texname) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) textureData = pygame.image.tostring(pow2surface, "RGBX", 1) glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, pow2surface.get_width(), pow2surface.get_height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData ); glDisable(GL_TEXTURE_2D) def surfacePosition(self,surface): """Returns a suggested position for a surface. No guarantees its any good!""" position = self.next_position self.next_position = position[0]+50, position[1]+50 return position def handleOGLComponentEvents(self, events): """ Prepare and send input events for OpenGL components. """ # Handle OpenGL component events for event in events: if event.type in [ pygame.KEYDOWN, pygame.KEYUP, pygame.MOUSEMOTION, pygame.MOUSEBUTTONUP, pygame.MOUSEBUTTONDOWN ]: # compose event data e = Bunch() e.type = event.type if event.type in [ pygame.KEYDOWN, pygame.KEYUP ]: # key is the only data in keyevents e.key = event.key else: # type is one of pygame.MOUSEMOTION, pygame.MOUSEBUTTONUP, pygame.MOUSEBUTTONDOWN # position e.pos = event.pos # add viewer position e.viewerposition = self.viewerposition # determine intersection ray xclick = float(event.pos[0]-self.width/2)*self.farPlaneWidth/float(self.width) yclick = float(-event.pos[1]+self.height/2)*self.farPlaneHeight/float(self.height) untransformed_dir = Vector(xclick, yclick, -self.farPlaneDist) e.direction = self.coordCorrectionTransform.transformVector(untransformed_dir) # determine which objects have been hit e.hitobjects = self.doPicking(event.pos) # set specific event fields if event.type in [pygame.MOUSEBUTTONUP, pygame.MOUSEBUTTONDOWN]: e.button = event.button if event.type == pygame.MOUSEMOTION: e.rel = event.rel e.buttons = event.buttons # send events to objects for ident in self.ogl_objects + self.eventspies: try: if self.eventswanted[ident][e.type]: self.send(e, self.eventcomms[ident]) except KeyError: pass def handlePygameComponentEvents(self, events): """ Prepare and send input events for pygame components. """ # Handle Pygame events for surface in self.pygame_surfaces: try: eventcomms = self.eventcomms[id(surface)] if eventcomms is not None: bundle = [] for event in events: wanted = False try: wanted = self.eventswanted[id(surface)][event.type] except KeyError: pass if wanted: # if event contains positional information, remap it # for the surface's coordiate origin if event.type in [ pygame.MOUSEMOTION, pygame.MOUSEBUTTONUP, pygame.MOUSEBUTTONDOWN ]: # skip wrapped components (they get events from their wrappers) if id(surface) in self.wrappedsurfaces: continue # assemble event e = Bunch() e.type = event.type pos = event.pos[0],event.pos[1] surfpos = self.pygame_positions[id(surface)] # determine position on surface e.pos = ( pos[0]-surfpos[0], pos[1]-surfpos[1] ) if event.type == pygame.MOUSEMOTION: e.rel = event.rel if event.type == pygame.MOUSEMOTION: e.buttons = event.buttons else: e.button = event.button event = e bundle.append(event) # only send events to listener if we've actually got some if bundle != []: self.send(bundle, eventcomms) except KeyError: pass