art_demo.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # Copyright (c) 2013, Oregon State University
00003 # All rights reserved.
00004 
00005 # Redistribution and use in source and binary forms, with or without
00006 # modification, are permitted provided that the following conditions are met:
00007 #     * Redistributions of source code must retain the above copyright
00008 #       notice, this list of conditions and the following disclaimer.
00009 #     * Redistributions in binary form must reproduce the above copyright
00010 #       notice, this list of conditions and the following disclaimer in the
00011 #       documentation and/or other materials provided with the distribution.
00012 #     * Neither the name of the Oregon State University nor the
00013 #       names of its contributors may be used to endorse or promote products
00014 #       derived from this software without specific prior written permission.
00015 
00016 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00017 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00018 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 # DISCLAIMED. IN NO EVENT SHALL OREGON STATE UNIVERSITY BE LIABLE FOR ANY
00020 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00021 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00022 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00023 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00024 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00025 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 # Author Dan Lazewatsky/lazewatd@engr.orst.edu
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         # sigma = 15.0
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         # def draw(self, screen):
00077         #       c_max = self.find_max()
00078         #       TRANSPARENT = (255,0,255)
00079         #       for r in range(self.radius, 0, -1):
00080         #               s = pygame.Surface((r,r))
00081         #               # s.fill(TRANSPARENT)
00082         #               # s.set_colorkey(TRANSPARENT)
00083         #               red = int(self.brightness*gaussian(r, self.sigma)/c_max)
00084         #               c = (self.brightness, 0, 0)
00085         #               # pygame.draw.circle(screen, pygame.Color(self.brightness, 0, 0, 255-red), self.center, r)
00086         #               # s.fill((255, 255, 255, 255-red))
00087         #               
00088         #               # pygame.draw.circle(s, (0, self.brightness, 0, 255-red), self.center, r)
00089         #               pygame.draw.circle(s, (red,0,0), (r/2,r/2), r)
00090         #               s.set_alpha(red)
00091         #               screen.blit(s, (self.center[0]-r/2, self.center[1]-r/2))
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                                 # print event
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                         # pygame.draw.circle(self.screen, (255,0,0), (50,50), 5)
00137                         # self.draw_gaussian(50, self.pos)
00138                         # self.gaussians.append(Gaussian(self.pos))
00139                         # for g in self.gaussians:
00140                         #       g.draw(self.screen)
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                         #if len(self.gaussians) > 100:
00147                         #       Line(self.pos, self.gaussians[-100].p1).draw(self.screen)
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()


rosmouse
Author(s): Daniel Lazewatsky
autogenerated on Mon Oct 6 2014 00:22:57