From 2736307c1d6d67868ca54a3df951f9e959efedd0 Mon Sep 17 00:00:00 2001 From: rcoh Date: Mon, 20 Dec 2010 14:50:08 -0500 Subject: Util cleanup is done! Util.py is now refactored into the util module. Woo! RCOH --- Util.py | 53 ---------------------------------------- behaviors/DecayBehavior.py | 3 ++- behaviors/EchoBehavior.py | 3 ++- behaviors/RunningBehavior.py | 5 ++-- inputs/PygameInput.py | 7 +++--- inputs/TCPInput.py | 3 ++- operationscore/PixelAssembler.py | 3 ++- operationscore/PixelEvent.py | 8 ++++++ pixelcore/PixelStrip.py | 6 +++-- pixelcore/Screen.py | 8 +++--- pixelmappers/GaussianMapper.py | 5 ++-- pixelmappers/SimpleMapper.py | 2 +- util/Geo.py | 15 ++++++++++++ util/Search.py | 8 ++++++ util/Strings.py | 1 + 15 files changed, 60 insertions(+), 70 deletions(-) delete mode 100644 Util.py create mode 100644 util/Geo.py create mode 100644 util/Search.py create mode 100644 util/Strings.py diff --git a/Util.py b/Util.py deleted file mode 100644 index 8aa4d8a..0000000 --- a/Util.py +++ /dev/null @@ -1,53 +0,0 @@ -import pdb -from xml.etree.ElementTree import ElementTree -import math,struct -from bisect import * -#import json # json.loads() to decode string; json.dumps() to encode data -import socket -import random -from pygame.locals import * -from pixelevents.StepEvent import * - -classArgsMem = {} -UNI = 0 -colorByteMem = {} -CONFIG_PATH = 'config/' -kinetDict = {'flags': 0, 'startcode': 0, 'pad':0} -componentDict = {} -#Only for rough estimates. Kindof lazy on specifics. -def pointWithinBoundingBox(point, bb): #this could be in 4 lines, but I'm lazy. - return sum([(point[i % 2] <= bb[i]) == (i>1) for i in range(4)]) == 4 -print pointWithinBoundingBox((118,21), (10,8,298,42)) -def addLocations(l1,l2): - return tuple([l1[i]+l2[i] for i in range(len(l1))]) -def addPixelEventIfMissing(responseDict): - if not 'PixelEvent' in responseDict: - if 'Color' in responseDict: - color = responseDict['Color'] - else: - raise Exception('Need Color. Probably') - responseDict['PixelEvent'] = StepEvent.generate(300, color) -def gaussian(x,height,center,width): - a=height - b=center - c=width - return a*math.exp(-((x-b)**2)/(2*c**2)) -def dist(l1, l2): - return math.sqrt(sum([(l1[i]-l2[i])**2 for i in range(len(l1))])) -def find_le(a, x): - 'Find rightmost value less than or equal to x' - return bisect_right(a, x)-1 - -def find_ge(a, x): - 'Find leftmost value greater than x' - return bisect_left(a, x) -def testXMLParse(fileName): - #pdb.set_trace() - config = ElementTree() - config.parse(fileName) - print generateArgDict(config.find('ChildElement')) - print generateArgDict(config.find('Renderer')) -##CONSTANTS## -location = 'Location' - - diff --git a/behaviors/DecayBehavior.py b/behaviors/DecayBehavior.py index edc833d..56e1686 100644 --- a/behaviors/DecayBehavior.py +++ b/behaviors/DecayBehavior.py @@ -1,5 +1,6 @@ from operationscore.Behavior import * from pixelevents.DecayEvent import * +import util.Strings as Strings import Util import pdb class DecayBehavior(Behavior): @@ -7,7 +8,7 @@ class DecayBehavior(Behavior): ret = [] for sensory in sensorInputs: outDict = {} - outDict[Util.location] = sensory[Util.location] + outDict[Strings.LOCATION] = sensory[Strings.LOCATION] outDict['PixelEvent'] = \ DecayEvent.generate(self['DecayType'],self['Coefficient'], sensory['Color']) ret.append(outDict) diff --git a/behaviors/EchoBehavior.py b/behaviors/EchoBehavior.py index a11558a..002f8fb 100644 --- a/behaviors/EchoBehavior.py +++ b/behaviors/EchoBehavior.py @@ -1,4 +1,5 @@ from operationscore.Behavior import * +import util.Strings as Strings import Util import pdb class EchoBehavior(Behavior): @@ -6,7 +7,7 @@ class EchoBehavior(Behavior): ret = [] for sensory in sensorInputs: outDict = {} - outDict[Util.location] = sensory[Util.location] + outDict[Strings.LOCATION] = sensory[Strings.LOCATION] outDict['Color'] = (255,0,0) ret.append(outDict) return ret diff --git a/behaviors/RunningBehavior.py b/behaviors/RunningBehavior.py index 4f51898..1969162 100644 --- a/behaviors/RunningBehavior.py +++ b/behaviors/RunningBehavior.py @@ -1,5 +1,6 @@ from operationscore.Behavior import * import util.ComponentRegistry as compReg +import util.Geo as Geo import pdb import Util class RunningBehavior(Behavior): @@ -13,9 +14,9 @@ class RunningBehavior(Behavior): outDict['Dir'] = 1 #to the right if not 'StepSize' in outDict: outDict['StepSize'] = self['StepSize'] - outDict['Location']= Util.addLocations(outDict['Location'], + outDict['Location']= Geo.addLocations(outDict['Location'], (outDict['StepSize']*outDict['Dir'],0)) - if not Util.pointWithinBoundingBox(outDict['Location'], \ + if not Geo.pointWithinBoundingBox(outDict['Location'], \ compReg.getComponent('Screen').getSize()): outDict['Dir'] *= -1 ret.append(outDict) diff --git a/inputs/PygameInput.py b/inputs/PygameInput.py index 1f438d6..f69d0f5 100644 --- a/inputs/PygameInput.py +++ b/inputs/PygameInput.py @@ -1,4 +1,5 @@ import time, Util +import util.Strings as Strings from operationscore.Input import * import pygame from pygame.locals import * @@ -8,13 +9,13 @@ class PygameInput(Input): def sensingLoop(self): #try: if self['FollowMouse']: - self.respond({Util.location: pygame.mouse.get_pos()}) + self.respond({Strings.LOCATION: pygame.mouse.get_pos()}) return for event in pygame.event.get(): if event.type is KEYDOWN: - self.respond({Util.location: (5,5),'Key': event.key}) + self.respond({Strings.LOCATION: (5,5),'Key': event.key}) if event.type is MOUSEBUTTONDOWN: - self.respond({Util.location: pygame.mouse.get_pos()}) + self.respond({Strings.LOCATION: pygame.mouse.get_pos()}) #except: #raise Exception('Pygame not initialized. Pygame must be \ #initialized.') diff --git a/inputs/TCPInput.py b/inputs/TCPInput.py index acd6243..9f62825 100644 --- a/inputs/TCPInput.py +++ b/inputs/TCPInput.py @@ -1,4 +1,5 @@ import Util +import util.Strings as Strings from operationscore.Input import * import socket, json, time class TCPInput(Input): @@ -25,7 +26,7 @@ class TCPInput(Input): if self.IS_RESPONDING == 1: # if 'responding', respond to the received data dataDict = json.loads(data) # socketDict = {'data':dataDict, 'address':self.address} - socketDict = {Util.location: (100 * (1 - dataDict['x'] / 10), 25 * (1 + dataDict['y'] / 10))} # like PygameInput + socketDict = {Strings.LOCATION: (100 * (1 - dataDict['x'] / 10), 25 * (1 + dataDict['y'] / 10))} # like PygameInput self.respond(socketDict) else: diff --git a/operationscore/PixelAssembler.py b/operationscore/PixelAssembler.py index b6e35ac..c8563fb 100644 --- a/operationscore/PixelAssembler.py +++ b/operationscore/PixelAssembler.py @@ -1,4 +1,5 @@ from operationscore.SmootCoreObject import * +import util.Geo as Geo import Util import pdb class PixelAssembler(SmootCoreObject): @@ -17,7 +18,7 @@ class PixelAssembler(SmootCoreObject): if newLocation == None: raise Exception('Location cannot be null. layoutFunc not \ defined or improperly defined.') - if Util.dist(newLocation, locations[-1]) > \ + if Geo.dist(newLocation, locations[-1]) > \ self['pixelToPixelSpacing']: raise Exception('Illegal pixel location. Distance \ between adjacent pixels must be less than \ diff --git a/operationscore/PixelEvent.py b/operationscore/PixelEvent.py index 27e6e4a..e2b852a 100644 --- a/operationscore/PixelEvent.py +++ b/operationscore/PixelEvent.py @@ -16,4 +16,12 @@ class PixelEvent(SmootCoreObject): return self.__class__(newDict) def state(self,timeDelay): pass + @staticmethod + def addPixelEventIfMissing(responseDict): + if not 'PixelEvent' in responseDict: + if 'Color' in responseDict: + color = responseDict['Color'] + else: + raise Exception('Need Color. Probably') + responseDict['PixelEvent'] = StepEvent.generate(300, color) diff --git a/pixelcore/PixelStrip.py b/pixelcore/PixelStrip.py index c82a87a..cfab948 100644 --- a/pixelcore/PixelStrip.py +++ b/pixelcore/PixelStrip.py @@ -1,4 +1,6 @@ from pixelcore.Pixel import * +import util.Strings as Strings +import util.Geo as Geo from pixelevents.StepEvent import * import pygame import math @@ -21,7 +23,7 @@ class PixelStrip: [l.turnOnFor(time) for l in self.pixels] #TODO: add test-on method to #pixels def respond(self, responseInfo): - location = responseInfo[Util.location] + location = responseInfo[Strings.LOCATION] if not 'PixelEvent' in responseInfo: if 'Color' in responseInfo: color = responseInfo['Color'] @@ -32,7 +34,7 @@ class PixelStrip: pixel.processInput(responseInfo['PixelEvent'], 0) #TODO: z-index def getPixelNearest(self, location): - dists = [(Util.dist(location, pixel.location), pixel) for pixel in self.pixels] + dists = [(Geo.dist(location, pixel.location), pixel) for pixel in self.pixels] dists.sort() return dists[0] #just for now. diff --git a/pixelcore/Screen.py b/pixelcore/Screen.py index 92805a8..a20cc72 100644 --- a/pixelcore/Screen.py +++ b/pixelcore/Screen.py @@ -1,5 +1,7 @@ from pixelcore.Pixel import * from pixelcore.PixelStrip import * +from operationscore.PixelEvent import * +import util.Search as Search import itertools #Class representing a collection of Pixels grouped into PixelStrips. Needs a #PixelMapper, currently set via setMapper by may be migrated into the argDict. @@ -17,8 +19,8 @@ class Screen: self.computeXSortedPixels() #Returns (pixelIndex, pixel). Does a binary search. def pixelsInRange(self, minX, maxX): - minIndex = Util.find_ge(self.xPixelLocs, minX) - maxIndex = Util.find_le(self.xPixelLocs, maxX)+1 + minIndex = Search.find_ge(self.xPixelLocs, minX) + maxIndex = Search.find_le(self.xPixelLocs, maxX)+1 return self.xSortedPixels[minIndex:maxIndex] def computeXSortedPixels(self): for pixel in self: @@ -70,7 +72,7 @@ class Screen: pass #pdb.set_trace() pixelWeightList = self.mapper.mapEvent(responseInfo['Location'], self) - Util.addPixelEventIfMissing(responseInfo) + PixelEvent.addPixelEventIfMissing(responseInfo) for (pixel, weight) in pixelWeightList: pixel.processInput(responseInfo['PixelEvent'].scale(weight), 0) #TODO: z-index diff --git a/pixelmappers/GaussianMapper.py b/pixelmappers/GaussianMapper.py index 04bd447..e94235e 100644 --- a/pixelmappers/GaussianMapper.py +++ b/pixelmappers/GaussianMapper.py @@ -1,4 +1,5 @@ from operationscore.PixelMapper import * +import util.Geo as Geo import Util class GaussianMapper(PixelMapper): def mappingFunction(self, eventLocation, screen): @@ -6,8 +7,8 @@ class GaussianMapper(PixelMapper): [x,y] = eventLocation for (x,pixel) in screen.pixelsInRange(x-self['CutoffDist'], \ x+self['CutoffDist']): - pixelDist = Util.dist(pixel.location, eventLocation) + pixelDist = Geo.dist(pixel.location, eventLocation) if pixelDist < self['CutoffDist']: - w = Util.gaussian(pixelDist, self['Height'], 0, self['Width']) + w = Geo.gaussian(pixelDist, self['Height'], 0, self['Width']) returnPixels.append((pixel, w)) return returnPixels diff --git a/pixelmappers/SimpleMapper.py b/pixelmappers/SimpleMapper.py index 7d730f1..4b5377d 100644 --- a/pixelmappers/SimpleMapper.py +++ b/pixelmappers/SimpleMapper.py @@ -5,7 +5,7 @@ class SimpleMapper(PixelMapper): bestDist = 10**10 #don't kill me, I'm lazy bestPixel = None for pixel in screen: - pixelDist = Util.dist(pixel.location, eventLocation) + pixelDist = Geo.dist(pixel.location, eventLocation) if pixelDist < bestDist: bestPixel = pixel bestDist = pixelDist diff --git a/util/Geo.py b/util/Geo.py new file mode 100644 index 0000000..885c585 --- /dev/null +++ b/util/Geo.py @@ -0,0 +1,15 @@ +#Geometry code +import math +from bisect import * +def pointWithinBoundingBox(point, bb): #this could be in 4 lines, but I'm lazy. + return sum([(point[i % 2] <= bb[i]) == (i>1) for i in range(4)]) == 4 +print pointWithinBoundingBox((118,21), (10,8,298,42)) +def addLocations(l1,l2): + return tuple([l1[i]+l2[i] for i in range(len(l1))]) +def gaussian(x,height,center,width): + a=height + b=center + c=width + return a*math.exp(-((x-b)**2)/(2*c**2)) +def dist(l1, l2): + return math.sqrt(sum([(l1[i]-l2[i])**2 for i in range(len(l1))])) diff --git a/util/Search.py b/util/Search.py new file mode 100644 index 0000000..25882da --- /dev/null +++ b/util/Search.py @@ -0,0 +1,8 @@ +from bisect import * +def find_le(a, x): + 'Find rightmost value less than or equal to x' + return bisect_right(a, x)-1 + +def find_ge(a, x): + 'Find leftmost value greater than x' + return bisect_left(a, x) diff --git a/util/Strings.py b/util/Strings.py new file mode 100644 index 0000000..1331db4 --- /dev/null +++ b/util/Strings.py @@ -0,0 +1 @@ +LOCATION = 'Location' -- cgit v1.2.3