[glumpy] push by Nicolas.Rougier@gmail.com - Added close event and stopable event loop on 2012-06-26 07:17 GMT

3 views
Skip to first unread message

glu...@googlecode.com

unread,
Jun 26, 2012, 3:17:36 AM6/26/12
to nicolas...@inria.fr
Revision: 6ec0a6080984
Author: Nicolas Rougier <Nicolas...@inria.fr>
Date: Tue Jun 26 00:17:22 2012
Log: Added close event and stopable event loop

http://code.google.com/p/glumpy/source/detail?r=6ec0a6080984

Added:
/demos/exit.py
Modified:
/demos/axis-2d.py
/demos/axis-3d.py
/demos/boids.py
/glumpy/window/backend_glut.py
/glumpy/window/window.py

=======================================
--- /dev/null
+++ /demos/exit.py Tue Jun 26 00:17:22 2012
@@ -0,0 +1,49 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+#
-----------------------------------------------------------------------------
+# glumpy is an OpenGL framework for the fast visualization of numpy arrays.
+# Copyright (C) 2009-2011 Nicolas P. Rougier. All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
met:
+#
+# 1. Redistributions of source code must retain the above copyright notice,
+# this list of conditions and the following disclaimer.
+#
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY NICOLAS P. ROUGIER ''AS IS'' AND ANY
EXPRESS OR
+# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF
+# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
NO
+# EVENT SHALL NICOLAS P. ROUGIER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES;
+# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
AND
+# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#
+# The views and conclusions contained in the software and documentation are
+# those of the authors and should not be interpreted as representing
official
+# policies, either expressed or implied, of Nicolas P. Rougier.
+#
-----------------------------------------------------------------------------
+import numpy, glumpy
+
+fig = glumpy.figure( (512,512) )
+Z = numpy.random.random((32,32)).astype(numpy.float32)
+image = glumpy.image.Image(Z)
+
+...@fig.event
+def on_draw():
+ fig.clear()
+ image.update()
+ image.draw( x=0, y=0, z=0, width=fig.width, height=fig.height )
+
+...@fig.event
+def on_close():
+ print "Close event !"
+
+glumpy.show()
+print "Done !"
=======================================
--- /demos/axis-2d.py Thu Mar 22 01:27:10 2012
+++ /demos/axis-2d.py Tue Jun 26 00:17:22 2012
@@ -40,27 +40,34 @@

if __name__ == '__main__':

- fig = figure(size=(800,800))
- frame = fig.add_frame() #aspect=1)
+ fig = figure(size=(512,512))
+ frame = fig.add_frame()
+ grid_offset = np.array([0., 0.])

@fig.event
def on_draw():
fig.clear(0.90,0.90,0.90,1.00)

+ @frame.event
+ def on_mouse_drag(x, y, dx, dy, button):
+ global grid_offset
+ grid_offset += dx/float(frame.width), dy/float(frame.height)
+ fig.redraw()
+
@frame.event
def on_draw():
frame.lock()
frame.draw()

w,h = frame.width, frame.height
- aspect = float(w)/float(h)
+ aspect = float(h)/float(w)
axis.vertices['position'][1] = w, 0, 0
axis.vertices['position'][2] = w, h, 0
axis.vertices['position'][3] = 0, h, 0
axis.upload()

- major_grid = np.array([5.0, 5.0*aspect])
- minor_grid = np.array([50.0, 50.0*aspect])
+ major_grid = np.array([5.0/aspect, 5.0])
+ minor_grid = np.array([50.0/aspect, 50.0])
major_grid_color = np.array([0.75, 0.75, 0.75, 1.0])
minor_grid_color = np.array([0.75, 0.75, 0.75, 1.0])
major_grid_width = 1.50
@@ -69,22 +76,23 @@
minor_tick_size = 0.5/minor_grid
major_tick_color = np.array([0.0, 0.0, 0.0, 1.0])
minor_tick_color = np.array([0.0, 0.0, 0.0, 1.0])
- major_tick_width = 1.50
+ major_tick_width = 1.25
minor_tick_width = 0.25

shader.bind()
- shader.uniformf( 'major_grid', *major_grid )
- shader.uniformf( 'minor_grid', *minor_grid )
- shader.uniformf( 'major_grid_width', major_grid_width )
- shader.uniformf( 'minor_grid_width', minor_grid_width )
- shader.uniformf( 'major_grid_color', *major_grid_color )
- shader.uniformf( 'minor_grid_color', *minor_grid_color )
- shader.uniformf( 'major_tick_size', *major_tick_size )
- shader.uniformf( 'minor_tick_size', *minor_tick_size )
- shader.uniformf( 'major_tick_width', major_tick_width )
- shader.uniformf( 'minor_tick_width', minor_tick_width )
- shader.uniformf( 'major_tick_color', *major_tick_color )
- shader.uniformf( 'minor_tick_color', *minor_tick_color )
+ shader.uniformf( 'major_grid', *major_grid )
+ shader.uniformf( 'minor_grid', *minor_grid )
+ shader.uniformf( 'grid_offset', *grid_offset )
+ shader.uniformf( 'major_grid_width', major_grid_width )
+ shader.uniformf( 'minor_grid_width', minor_grid_width )
+ shader.uniformf( 'major_grid_color', *major_grid_color )
+ shader.uniformf( 'minor_grid_color', *minor_grid_color )
+ shader.uniformf( 'major_tick_size', *major_tick_size )
+ shader.uniformf( 'minor_tick_size', *minor_tick_size )
+ shader.uniformf( 'major_tick_width', major_tick_width )
+ shader.uniformf( 'minor_tick_width', minor_tick_width )
+ shader.uniformf( 'major_tick_color', *major_tick_color )
+ shader.uniformf( 'minor_tick_color', *minor_tick_color )
axis.draw( gl.GL_TRIANGLES )
shader.unbind()

@@ -93,9 +101,9 @@

w,h = 256,256
V = np.array( [ ((0, 0, 0), (0,0)),
- ((w, 0, 0), (0,1)),
+ ((w, 0, 0), (1,0)),
((w, h, 0), (1,1)),
- ((0, h, 0), (1,0)) ],
+ ((0, h, 0), (0,1)) ],
dtype = [('position','f4',3),
('tex_coord','f4',2)] )
I = np.array( [0,1,2, 0,2,3 ], dtype=np.uint32 )
@@ -112,6 +120,7 @@

fragment = """
varying vec2 T;
+ uniform vec2 grid_offset;
uniform vec2 major_grid, minor_grid;
uniform float major_grid_width, minor_grid_width;
uniform vec4 major_grid_color, minor_grid_color;
@@ -123,8 +132,10 @@
vec2 f, df, m;
float c, c1, c2, c3, c4, c5, c6;

+ vec2 t = T - grid_offset;
+
// Major grid
- f = abs( fract (T*major_grid - vec2(0.5,0.5) ) - 0.5 );
+ f = abs( fract (t*major_grid - 0.5 ) - 0.5);
df = fwidth(T*major_grid);
c = max(1.0,major_grid_width)/2.0;
m = smoothstep( -(c-0.5)*df, +(c+0.5)*df, f );
@@ -132,7 +143,7 @@
c1 = 1.0 - c + c*m.x*m.y;

// Minor grid
- f = abs( fract( T*minor_grid - vec2(0.5,0.5) )-0.5 );
+ f = abs( fract( t*minor_grid - 0.5 ) - 0.5 );
df = fwidth( T*minor_grid );
c = max(1.0,minor_grid_width)/2.0;
m = smoothstep( -(c-0.5)*df, +(c+0.5)*df, f );
@@ -141,7 +152,7 @@

// Major ticks
c3 = c4 = 1.0;
- f = abs( fract( T*major_grid - vec2(0.5,0.5) )-0.5 );
+ f = abs( fract( t*major_grid - 0.5 ) - 0.5 );
df = fwidth( T*major_grid );
// c = max(1.0,major_tick_width)/2.0;
// m = smoothstep( -(c-0.5)*df, +(c+0.5)*df, f );
@@ -160,7 +171,7 @@

// Minor ticks
c5 = c6 = 1.0;
- f = abs( fract( T*minor_grid - vec2(0.5,0.5) )-0.5 );
+ f = abs( fract( t*minor_grid - 0.5 ) - 0.5 );
df = fwidth( T*minor_grid );
// c = max(1.0,minor_tick_width)/2.0;
// m = smoothstep( -(c-0.5)*df, +(c+0.5)*df, f );
@@ -177,8 +188,8 @@
c6 = 1.0 - c + c*m.y;
}

- c = min( min(c1,c2), min(c3,c4) );
- c = min( c, min(c5,c6) );
+ c = min( min(c5,c6), min(c3,c4) );
+ c = min( c, min(c1,c2) );

vec4 color;
if( (c == c3) || (c == c4) )
=======================================
--- /demos/axis-3d.py Thu Mar 22 01:27:10 2012
+++ /demos/axis-3d.py Tue Jun 26 00:17:22 2012
@@ -40,7 +40,7 @@

if __name__ == '__main__':

- fig = figure(size=(800,800))
+ fig = figure(size=(512,512))
trackball = Trackball(65, 135, 1., 3.)

@fig.event
@@ -72,11 +72,11 @@
minor_grid_color = np.array([0.75, 0.75, 0.75, 1.0])
major_grid_width = 1.50
minor_grid_width = 0.25
- major_tick_size = 1.5/minor_grid
- minor_tick_size = 0.5/minor_grid
- major_tick_color = np.array([0.0, 0.0, 0.0, 1.0])
- minor_tick_color = np.array([0.0, 0.0, 0.0, 1.0])
- major_tick_width = 1.50
+ major_tick_size = 1.0/minor_grid
+ minor_tick_size = 0.0/minor_grid
+ major_tick_color = np.array([0.5, 0.5, 0.5, 1.0])
+ minor_tick_color = np.array([0.5, 0.5, 0.5, 1.0])
+ major_tick_width = 1.00
minor_tick_width = 0.25

shader.bind()
=======================================
--- /demos/boids.py Fri Mar 23 03:53:45 2012
+++ /demos/boids.py Tue Jun 26 00:17:22 2012
@@ -96,7 +96,7 @@

# Predator : Move away from predator
dP = P - predator
- D = np.maximum(0, 0.35 - np.sqrt(dP[:,0]**2
+dP[:,1]**2+dP[:,2]**2) )
+ D = np.maximum(0, 0.1 - np.sqrt(dP[:,0]**2 +dP[:,1]**2+dP[:,2]**2)
)
D = np.repeat(D,3,axis=0).reshape(n,3)
dP *= D

=======================================
--- /glumpy/window/backend_glut.py Tue Apr 10 23:06:01 2012
+++ /glumpy/window/backend_glut.py Tue Jun 26 00:17:22 2012
@@ -126,6 +126,12 @@
self._id = glut.glutCreateWindow( self._title )
glut.glutShowWindow( )

+ if bool(glut.glutSetOption):
+ glut.glutSetOption(glut.GLUT_ACTION_ON_WINDOW_CLOSE,
+ glut.GLUT_ACTION_CONTINUE_EXECUTION)
+ glut.glutSetOption(glut.GLUT_ACTION_GLUTMAINLOOP_RETURNS,
+ glut.GLUT_ACTION_CONTINUE_EXECUTION)
+ glut.glutWMCloseFunc( self._close )
glut.glutDisplayFunc( self._display )
glut.glutReshapeFunc( self._reshape )
glut.glutKeyboardFunc( self._keyboard )
@@ -180,7 +186,7 @@
modifiers = self._modifiers_translate(modifiers)
state= self.dispatch_event('on_key_press', symbol, modifiers)
if not state and symbol == key.ESCAPE:
- sys.exit()
+ self.stop()

def _keyboard_up( self, code, x, y ):
modifiers = glut.glutGetModifiers()
@@ -251,6 +257,9 @@
self.dispatch_event('on_draw')
glut.glutSwapBuffers()

+ def _close( self ):
+ self.dispatch_event('on_close')
+ self.stop()

def _idle(self):
t = glut.glutGet(glut.GLUT_ELAPSED_TIME)
@@ -538,18 +547,19 @@

# Dispatch init event
self.dispatch_event('on_init')
-
+
+ self._stop_mainloop = False
if not self._interactive:
- glut.glutMainLoop()
+ #glut.glutMainLoop()
+ while not self._stop_mainloop:
+ glut.glutCheckLoop()


def stop(self):
'''Exit mainloop'''
- if (glut.glutLeaveMainLoop):
- glut.glutLeaveMainLoop()
- else:
- raise RuntimeError(
- '''Your GLUT implementation does not allow to stops the
main loop''')
+ if bool( glut.glutLeaveMainLoop ):
+ glut.glutLeaveMainLoop( )
+ self._stop_mainloop = True



=======================================
--- /glumpy/window/window.py Wed Sep 21 02:52:40 2011
+++ /glumpy/window/window.py Tue Jun 26 00:17:22 2012
@@ -384,4 +384,5 @@
Window.register_event_type('on_resize')
Window.register_event_type('on_draw')
Window.register_event_type('on_idle')
-
+Window.register_event_type('on_close')
+
Reply all
Reply to author
Forward
0 new messages