Package SmootLight :: Package pixelmappers :: Module SimpleMapper
[hide private]
[frames] | no frames]

Source Code for Module SmootLight.pixelmappers.SimpleMapper

 1  from operationscore.PixelMapper import * 
 2  import util.Geo as Geo 
 3  import math 
 4  import sys 
5 -class SimpleMapper(PixelMapper):
6 """SimpleMapper is a PixelMapper which maps events to the nearest Pixel. It also supports 7 strings of the form: 8 {x}>5, {y}<10, {x}*{y}<{x}, etc. (Conditions, separated by commas. Standard python syntax such 9 as and and or may also be 10 used). You may use 'math.' functions such as math.sqrt, etc. It also accepts lists of strings"""
11 - def mappingFunction(self, eventLocation, screen):
12 if type(eventLocation) == type(tuple()): 13 bestDist = sys.maxint 14 bestPixel = None 15 [x,y] = eventLocation 16 for (x,pixel) in screen.pixelsInRange(x-self['CutoffDist'], \ 17 x+self['CutoffDist']): 18 pixelDist = Geo.dist(pixel.location, eventLocation) 19 if pixelDist < bestDist: 20 bestPixel = pixel 21 bestDist = pixelDist 22 if bestPixel != None: 23 return [(bestPixel,1)] 24 else: 25 return [] 26 else: 27 #{x}>5,{y}<k 28 ret = [] 29 if not isinstance(eventLocation, list): 30 eventLocation = eventLocation.replace('{x}', 'pixel.location[0]') 31 eventLocation = eventLocation.replace('{y}', 'pixel.location[1]') 32 conditions = eventLocation.split(',') 33 else: 34 conditions = eventLocation #TODO: check for lists of strings 35 conditionLambdas = [eval('lambda pixel:'+condition) for condition in conditions] 36 37 for pixel in screen: 38 try: 39 pixelValid = True 40 for p in conditionLambdas: 41 if p(pixel) == False: 42 pixelValid = False 43 continue 44 if pixelValid: 45 ret.append((pixel, 1)) 46 except Exception as exp: 47 exp.message += 'Bad Event Condition' 48 raise exp 49 return ret
50