00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 import roslib; roslib.load_manifest('rosmouse')
00030 import rospy
00031 from geometry_msgs.msg import PointStamped
00032 import pygame
00033 from pygame.locals import *
00034 from math import sqrt, pi, exp, sin
00035 import time
00036
00037 def gaussian(x, sigma):
00038
00039 mu = 1
00040 return exp(-(x-mu)**2/(2*sigma*sigma)) / (sigma*sqrt(2*pi))
00041
00042 class Line(object):
00043 brightness = 255
00044 def __init__(self, p1, p2):
00045 self.p1 = p1
00046 self.p2 = p2
00047 self.stamp = time.clock()
00048
00049 def draw(self, surface):
00050 c = (0, int(self.brightness*(self.stamp/time.clock())**2), 0)
00051 pygame.draw.aaline(surface, c, (self.p1[0], self.p1[1]-1), (self.p2[0], self.p2[1]-1))
00052 pygame.draw.aaline(surface, c, self.p1, self.p2)
00053 pygame.draw.aaline(surface, c, (self.p1[0], self.p1[1]+1), (self.p2[0], self.p2[1]+1))
00054
00055
00056 class Gaussian(object):
00057 brightness = 255
00058 def __init__(self, center, sigma=15.0, decay=False):
00059 self.sigma = sigma
00060 self.radius = self.find_radius()
00061 self.center = center
00062 self.stamp = time.clock()
00063
00064 def find_radius(self):
00065 c_max = self.find_max()
00066 c = int(self.brightness*gaussian(1, self.sigma)/c_max)
00067 r = 2
00068 while c > 0:
00069 c = int(self.brightness*gaussian(r, self.sigma)/c_max)
00070 r += 1
00071 return r
00072
00073 def find_max(self):
00074 return gaussian(1, self.sigma)
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092 def draw(self, screen):
00093 pygame.draw.circle(screen, (int(self.brightness*(self.stamp/time.clock())**2), 0, 0), self.center, int(self.sigma))
00094
00095
00096
00097 class ArtDemo(object):
00098 width = 500
00099 height = 500
00100 pos = None
00101 running = False
00102 gaussians = []
00103
00104 def __init__(self, fullscreen=False, use_mouse=True):
00105 self.use_mouse = use_mouse
00106 self.screen = pygame.display.set_mode((self.width, self.height))
00107 if fullscreen:
00108 modes = pygame.display.list_modes()
00109 pygame.display.set_mode(modes[0], FULLSCREEN)
00110
00111 pygame.mouse.set_visible(False)
00112
00113 def process_events(self, events):
00114 for event in events:
00115 if event.type == QUIT:
00116 self.running = False
00117 elif event.type == KEYUP:
00118 if event.key == 27:
00119 self.running = False
00120 elif (event.type == MOUSEMOTION) and self.use_mouse:
00121 self.pos = event.pos
00122 else:
00123
00124 pass
00125
00126 def run(self):
00127 self.running = True
00128 last_pos = self.pos
00129 while self.running:
00130 self.process_events(pygame.event.get())
00131 if not self.pos: continue
00132 if not last_pos:
00133 last_pos = self.pos
00134 continue
00135 self.screen.fill((0,0,0))
00136
00137
00138
00139
00140
00141 self.gaussians.append(Line(self.pos, last_pos))
00142 for g in self.gaussians:
00143 g.draw(self.screen)
00144 pygame.draw.circle(self.screen, (0, 255*abs(sin(time.clock()*2)), 0), self.pos, int(10*abs(sin(time.clock()*2))))
00145
00146
00147
00148
00149
00150 pygame.display.flip()
00151 if self.gaussians:
00152 last_pos = self.gaussians[-1].p1
00153 else:
00154 last_pos = self.pos
00155
00156
00157 def mouse_cb(point, d):
00158 d.pos = int(point.point.x), int(point.point.y)
00159
00160 if __name__ == '__main__':
00161 d = ArtDemo(fullscreen=True, use_mouse=True)
00162 rospy.init_node('art_demo')
00163 rospy.Subscriber('mouse', PointStamped, mouse_cb, d)
00164 d.run()