http://code.google.com/p/glumpy/source/detail?r=8fa077ff6a87
Added:
/demos/axis-2d.py
/demos/axis-3d.py
/demos/boids.py
/demos/histogram.py
=======================================
--- /dev/null
+++ /demos/axis-2d.py Thu Mar 22 01:27:10 2012
@@ -0,0 +1,197 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+#
-----------------------------------------------------------------------------
+# glumpy is an OpenGL framework for the fast visualization of numpy arrays.
+# Copyright (C) 2009-2012 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.
+#
-----------------------------------------------------------------------------
+'''
+This is a demonstration for a grid shader that will be included in glumpy
soon.
+'''
+import numpy as np
+import OpenGL.GL as gl
+from glumpy import figure
+from glumpy.graphics import VertexBuffer, Shader
+
+
+if __name__ == '__main__':
+
+ fig = figure(size=(800,800))
+ frame = fig.add_frame() #aspect=1)
+
+ @fig.event
+ def on_draw():
+ fig.clear(0.90,0.90,0.90,1.00)
+
+ @frame.event
+ def on_draw():
+ frame.lock()
+ frame.draw()
+
+ w,h = frame.width, frame.height
+ aspect = float(w)/float(h)
+ 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_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
+ minor_grid_width = 0.25
+ major_tick_size = 1.0/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
+ 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 )
+ axis.draw( gl.GL_TRIANGLES )
+ shader.unbind()
+
+ frame.unlock()
+
+
+ w,h = 256,256
+ V = np.array( [ ((0, 0, 0), (0,0)),
+ ((w, 0, 0), (0,1)),
+ ((w, h, 0), (1,1)),
+ ((0, h, 0), (1,0)) ],
+ dtype = [('position','f4',3),
+ ('tex_coord','f4',2)] )
+ I = np.array( [0,1,2, 0,2,3 ], dtype=np.uint32 )
+ axis = VertexBuffer(V,I)
+
+ vertex = """
+ varying vec2 T;
+ void main()
+ {
+ gl_FrontColor = gl_Color;
+ gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex;
+ T = gl_MultiTexCoord0.st;
+ }"""
+
+ fragment = """
+ varying vec2 T;
+ uniform vec2 major_grid, minor_grid;
+ uniform float major_grid_width, minor_grid_width;
+ uniform vec4 major_grid_color, minor_grid_color;
+ uniform vec2 major_tick_size, minor_tick_size;
+ uniform float major_tick_width, minor_tick_width;
+ uniform vec4 major_tick_color, minor_tick_color;
+ void main()
+ {
+ vec2 f, df, m;
+ float c, c1, c2, c3, c4, c5, c6;
+
+ // Major grid
+ f = abs( fract (T*major_grid - vec2(0.5,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 );
+ c = min(major_grid_width,1.0);
+ c1 = 1.0 - c + c*m.x*m.y;
+
+ // Minor grid
+ f = abs( fract( T*minor_grid - vec2(0.5,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 );
+ c = min(minor_grid_width,1.0);
+ c2 = 1.0 - c + c*m.x*m.y;
+
+ // Major ticks
+ c3 = c4 = 1.0;
+ f = abs( fract( T*major_grid - vec2(0.5,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 );
+ c = max(1.0,major_tick_width)/2.0;
+ m = smoothstep( (c+0.0)*df, +(c+0.5)*df, f );
+ c = min(major_tick_width,1.0);
+ if( ( T.y < major_tick_size.y ) || ( T.y >
(1.0-major_tick_size.y) ) )
+ {
+ c3 = 1.0 - c + c*m.x;
+ }
+
+ if( ( T.x < major_tick_size.x ) || ( T.x >
(1.0-major_tick_size.x) ) )
+ {
+ c4 = 1.0 - c + c*m.y;
+ }
+
+ // Minor ticks
+ c5 = c6 = 1.0;
+ f = abs( fract( T*minor_grid - vec2(0.5,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 );
+ c = max(1.0,minor_tick_width)/2.0;
+ m = smoothstep( (c+0.0)*df, +(c+0.5)*df, f );
+ c = min(minor_tick_width,1.0);
+ if( ( T.y < minor_tick_size.y ) || ( T.y >
(1.0-minor_tick_size.y) ) )
+ {
+ c5 = 1.0 - c + c*m.x;
+ }
+
+ if( ( T.x < minor_tick_size.x ) || ( T.x >
(1.0-minor_tick_size.x) ) )
+ {
+ c6 = 1.0 - c + c*m.y;
+ }
+
+ c = min( min(c1,c2), min(c3,c4) );
+ c = min( c, min(c5,c6) );
+
+ vec4 color;
+ if( (c == c3) || (c == c4) )
+ color = major_tick_color;
+ else if( (c == c5) || (c == c6) )
+ color = minor_tick_color;
+ else if( c == c2 )
+ color = minor_grid_color;
+ else
+ color = major_grid_color;
+
+ gl_FragColor = mix( color, vec4(1.0,1.0,1.0,1.0), c );
+ }"""
+ shader = Shader(vertex,fragment)
+
+ fig.show()
=======================================
--- /dev/null
+++ /demos/axis-3d.py Thu Mar 22 01:27:10 2012
@@ -0,0 +1,216 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+#
-----------------------------------------------------------------------------
+# glumpy is an OpenGL framework for the fast visualization of numpy arrays.
+# Copyright (C) 2009-2012 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.
+#
-----------------------------------------------------------------------------
+'''
+This is a demonstration for a grid shader that will be included in glumpy
soon.
+'''
+import numpy as np
+import OpenGL.GL as gl
+from glumpy import figure, Trackball
+from glumpy.graphics import VertexBuffer, Shader
+
+
+if __name__ == '__main__':
+
+ fig = figure(size=(800,800))
+ trackball = Trackball(65, 135, 1., 3.)
+
+ @fig.event
+ def on_init():
+ gl.glEnable( gl.GL_BLEND )
+ gl.glEnable(gl.GL_NORMALIZE)
+ gl.glEnable( gl.GL_LINE_SMOOTH )
+ gl.glEnable( gl.GL_COLOR_MATERIAL )
+ gl.glColorMaterial ( gl.GL_FRONT_AND_BACK,
gl.GL_AMBIENT_AND_DIFFUSE )
+ gl.glBlendFunc (gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
+ gl.glEnable (gl.GL_LIGHTING)
+ gl.glEnable (gl.GL_LIGHT0)
+ gl.glEnable (gl.GL_LIGHT1)
+ gl.glEnable (gl.GL_LIGHT2)
+
+ @fig.event
+ def on_mouse_drag(x, y, dx, dy, button):
+ trackball.drag_to(x,y,dx,dy)
+ fig.redraw()
+
+ @fig.event
+ def on_draw():
+ fig.clear()
+ trackball.push()
+
+ major_grid = np.array([5.0, 5.0])
+ minor_grid = np.array([50.0, 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
+ 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
+ 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 )
+ vbuffer.draw( gl.GL_TRIANGLES, 'pnt' );
+ shader.unbind()
+
+ trackball.pop()
+
+ V = np.array( [ ((-.5, -.5, -.5), (0,1,0), (0,0)),
+ ((+.5, -.5, -.5), (0,1,0), (0,1)),
+ ((+.5, -.5, +.5), (0,1,0), (1,1)),
+ ((-.5, -.5, +.5), (0,1,0), (1,0)),
+
+ ((-.5, -.5, -.5), (1,0,0), (0,0)),
+ ((-.5, +.5, -.5), (1,0,0), (0,1)),
+ ((-.5, +.5, +.5), (1,0,0), (1,1)),
+ ((-.5, -.5, +.5), (1,0,0), (1,0)),
+
+ ((-.5, -.5, -.5), (0,0,1), (0,0)),
+ ((-.5, +.5, -.5), (0,0,1), (0,1)),
+ ((+.5, +.5, -.5), (0,0,1), (1,1)),
+ ((+.5, -.5, -.5), (0,0,1), (1,0)) ],
+ dtype = [('position','f4',3),
+ ('normal','f4',3),
+ ('tex_coord','f4',2)] )
+ I = np.array( [0,1,2, 0,2,3,
+ 4,5,6, 4,6,7,
+ 8,9,10, 8,10,11], dtype=np.uint32 )
+ vbuffer = VertexBuffer(V,I)
+
+
+ vertex = """
+ varying vec2 T;
+ void main()
+ {
+ gl_FrontColor = gl_Color;
+ gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex;
+ T = gl_MultiTexCoord0.st;
+ }"""
+
+ fragment = """
+ varying vec2 T;
+ uniform vec2 major_grid, minor_grid;
+ uniform float major_grid_width, minor_grid_width;
+ uniform vec4 major_grid_color, minor_grid_color;
+ uniform vec2 major_tick_size, minor_tick_size;
+ uniform float major_tick_width, minor_tick_width;
+ uniform vec4 major_tick_color, minor_tick_color;
+ void main()
+ {
+ vec2 f, df, m;
+ float c, c1, c2, c3, c4, c5, c6;
+
+ // Major grid
+ f = abs( fract (T*major_grid - vec2(0.5,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 );
+ c = min(major_grid_width,1.0);
+ c1 = 1.0 - c + c*m.x*m.y;
+
+ // Minor grid
+ f = abs( fract( T*minor_grid - vec2(0.5,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 );
+ c = min(minor_grid_width,1.0);
+ c2 = 1.0 - c + c*m.x*m.y;
+
+ // Major ticks
+ c3 = c4 = 1.0;
+ f = abs( fract( T*major_grid - vec2(0.5,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 );
+ c = max(1.0,major_tick_width)/2.0;
+ m = smoothstep( c*df, +(c+0.5)*df, f );
+ c = min(major_tick_width,1.0);
+ if( ( T.y < major_tick_size.y ) || ( T.y >
(1.0-major_tick_size.y) ) )
+ {
+ c3 = 1.0 - c + c*m.x;
+ }
+
+ if( ( T.x < major_tick_size.x ) || ( T.x >
(1.0-major_tick_size.x) ) )
+ {
+ c4 = 1.0 - c + c*m.y;
+ }
+
+ // Minor ticks
+ c5 = c6 = 1.0;
+ f = abs( fract( T*minor_grid - vec2(0.5,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 );
+ c = max(1.0,minor_tick_width)/2.0;
+ m = smoothstep( c*df, +(c+0.5)*df, f );
+ c = min(minor_tick_width,1.0);
+ if( ( T.y < minor_tick_size.y ) || ( T.y >
(1.0-minor_tick_size.y) ) )
+ {
+ c5 = 1.0 - c + c*m.x;
+ }
+
+ if( ( T.x < minor_tick_size.x ) || ( T.x >
(1.0-minor_tick_size.x) ) )
+ {
+ c6 = 1.0 - c + c*m.y;
+ }
+
+ c = min( min(c1,c2), min(c3,c4) );
+ c = min( c, min(c5,c6) );
+
+ vec4 color;
+ if( (c == c3) || (c == c4) )
+ color = major_tick_color;
+ else if( (c == c5) || (c == c6) )
+ color = minor_tick_color;
+ else if( c == c2 )
+ color = minor_grid_color;
+ else
+ color = major_grid_color;
+
+ gl_FragColor = mix( color, vec4(1.0,1.0,1.0,1.0), c );
+ }"""
+ shader = Shader(vertex,fragment)
+
+ fig.show()
=======================================
--- /dev/null
+++ /demos/boids.py Thu Mar 22 01:27:10 2012
@@ -0,0 +1,123 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+#
-----------------------------------------------------------------------------
+# glumpy is an OpenGL framework for the fast visualization of numpy arrays.
+# Copyright (C) 2009-2012 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 as np
+import OpenGL.GL as gl
+from glumpy import figure, Trackball
+from scipy.spatial.distance import cdist
+
+
+if __name__ == '__main__':
+
+ fig = figure(size=(800,800))
+ trackball = Trackball(65, 135, 1., 2.5)
+
+ @fig.event
+ def on_mouse_drag(x, y, dx, dy, button):
+ trackball.drag_to(x,y,dx,dy)
+ fig.redraw()
+
+ @fig.event
+ def on_draw():
+ fig.clear()
+ trackball.push()
+ gl.glEnable(gl.GL_POINT_SMOOTH)
+ for i,P in enumerate([boids['position'],
+ boids['position_1'],
+ boids['position_2']]):
+ gl.glColor(0+i*.25,0+i*.25,0+i*.25,0.5)
+ gl.glPointSize(6.0-2*i)
+ gl.glBegin(gl.GL_POINTS)
+ for j in range(len(boids)):
+ gl.glVertex( P[j,0], P[j,1], P[j,2] )
+ gl.glEnd()
+ trackball.pop()
+
+ @fig.timer(60.0)
+ def timer(dt):
+ move(boids, dt)
+ fig.redraw()
+
+ def move(boids,dt):
+ global t
+
+ t += 0.5*dt
+ target[...] = np.array([np.sin(t),np.sin(2*t),np.cos(3*t)])*.1
+ t += .5*dt
+ predator[...] = np.array([np.sin(t),np.sin(2*t),np.cos(3*t)])*.2
+
+ boids['position_2'] = boids['position_1']
+ boids['position_1'] = boids['position']
+ n = len(boids)
+ P = boids['position']
+ V = boids['velocity']
+ D = cdist(P,P)
+
+ Mr = (D < 0.05)
+ Mr = np.repeat(Mr,3,axis=1).reshape(n,n,3)
+ # Nr = Mr.sum(axis=0)
+
+ # Cohesion: steer to move toward the average position of local
flockmates
+ C = -(P - P.sum(axis=0)/n)
+ # Alignment: steer towards the average heading of local flockmates
+ A = -(V - V.sum(axis=0)/n)
+ # Repulsion: steer to avoid crowding local flockmates
+ R = -((np.repeat(P,n,axis=0).reshape(n,n,3)-P)*Mr).sum(axis=0)
+
+ # Target
+ T = target - P
+
+ # Predator
+ dP = P - predator
+ D = np.maximum(0, .4 - np.sqrt(dP[:,0]**2 +dP[:,1]**2+dP[:,2]**2) )
+ dP[:,0] *= D
+ dP[:,1] *= D
+ dP[:,2] *= D
+
+ boids['velocity'] += 0.0005*C + 0.01*A + 0.01*R + 0.0005*T +
0.005*dP
+ boids['position'] += boids['velocity']
+
+
+ t = 0
+ n = 500
+ boids = np.zeros(n, [ ('position', 'f4', 3),
+ ('position_1', 'f4', 3),
+ ('position_2', 'f4', 3),
+ ('velocity', 'f4', 3)] )
+ boids['position'] = np.random.uniform(-0.25, +0.25, (n,3))
+ boids['velocity'] = np.random.uniform(-0.00, +0.00, (n,3))
+ target = np.zeros(3)
+ predator = np.zeros(3)
+
+
+ fig.show()
=======================================
--- /dev/null
+++ /demos/histogram.py Thu Mar 22 01:27:10 2012
@@ -0,0 +1,186 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+#
-----------------------------------------------------------------------------
+# glumpy is an OpenGL framework for the fast visualization of numpy arrays.
+# Copyright (C) 2009-2012 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 as np
+import OpenGL.GL as gl
+from glumpy import figure, Trackball
+from glumpy.graphics import VertexBuffer, Shader
+
+
+def cube(center=(0.0,0.0,0.0), size=(0.5,0.5,1.0), color = (1.0,1.0,1.0)):
+ """
+ Returns vertices, fill indices and outlines indices for a cube with
given
+ center, size and color.
+ """
+ cx,cy,cz = np.array(center)
+ sx,sy,sz = np.array(size)/2.0
+ p = ( (cx+sx, cy+sy, cz+sz),
+ (cx-sx, cy+sy, cz+sz),
+ (cx-sx, cy-sy, cz+sz),
+ (cx+sx, cy-sy, cz+sz),
+ (cx+sx, cy-sy, cz-sz),
+ (cx+sx, cy+sy, cz-sz),
+ (cx-sx, cy+sy, cz-sz),
+ (cx-sx, cy-sy, cz-sz) )
+ n = ( ( 0, 0, 1), (1, 0, 0), ( 0, 1, 0),
+ (-1, 0, 1), (0,-1, 0), ( 0, 0,-1) )
+ c = np.resize(color, (8,3))
+ vertices = np.array(
+ [ (p[0],n[0],c[0]), (p[1],n[0],c[1]), (p[2],n[0],c[2]),
(p[3],n[0],c[3]),
+ (p[0],n[1],c[0]), (p[3],n[1],c[3]), (p[4],n[1],c[4]),
(p[5],n[1],c[5]),
+ (p[0],n[2],c[0]), (p[5],n[2],c[5]), (p[6],n[2],c[6]),
(p[1],n[2],c[1]),
+ (p[1],n[3],c[1]), (p[6],n[3],c[6]), (p[7],n[3],c[7]),
(p[2],n[3],c[2]),
+ (p[7],n[4],c[7]), (p[4],n[4],c[4]), (p[3],n[4],c[3]),
(p[2],n[4],c[2]),
+ (p[4],n[5],c[4]), (p[7],n[5],c[7]), (p[6],n[5],c[6]),
(p[5],n[5],c[5]) ],
+ dtype = [('position','f4',3), ('normal','f4',3), ('color','f4',3)]
)
+ I = np.repeat(np.arange(0,24,4),6).reshape(6,6) + [0,1,2,0,2,3]
+ fill = I.astype(np.uint32).ravel()
+ I = np.repeat(np.arange(0,24,4),8).reshape(6,8) + [0,1,1,2,2,3,3,0]
+ outline = I.astype(np.uint32).ravel()
+ return vertices, fill, outline
+
+
+def bars(Z):
+ """
+ Each bar has:
+ - 24 vertices
+ - 36 indices for fill shape
+ - 48 indices for outline
+ """
+ nx,ny = Z.shape
+ vertices = np.zeros((nx*ny,24), dtype = [('position','f4',3),
+ ('normal','f4',3),
+ ('color','f4',3)] )
+ fill = np.zeros((nx*ny,36)).astype(np.uint32)
+ outline = np.zeros((nx*ny,48)).astype(np.uint32)
+ v,f,o = cube( (-0.5,-0.5,0.0), ( 1./nx, 1./ny, 0))
+ fill[:] = (np.arange(0,nx*ny)*24).reshape(nx*ny,1) + f
+ outline[:] = (np.arange(0,nx*ny)*24).reshape(nx*ny,1) + o
+ X,Y = np.mgrid[0:nx,0:ny]
+ X = np.repeat(X,24).reshape(nx*ny,24)
+ Y = np.repeat(Y,24).reshape(nx*ny,24)
+ vertices[...] = v
+ vertices['position'][:,:,0] += X/float(nx)+.5/nx
+ vertices['position'][:,:,1] += Y/float(ny)+.5/ny
+
+ Z = np.repeat(Z,12).reshape(nx*ny,12)
+ vertices['position'][:,[0,1,2,3,4,5,8,11,12,15,18,19],2] =
Z-Z.min()+0.001
+
+ return VertexBuffer(vertices,fill), VertexBuffer(vertices,outline)
+
+
+def gaussian(shape=(25,25), width=0.15, center=0.0):
+ if type(shape) in [float,int]:
+ shape = (shape,)
+ if type(width) in [float,int]:
+ width = (width,)*len(shape)
+ if type(center) in [float,int]:
+ center = (center,)*len(shape)
+ grid=[]
+ for size in shape:
+ grid.append (slice(0,size))
+ C = np.mgrid[tuple(grid)]
+ R = np.zeros(shape)
+ for i,size in enumerate(shape):
+ if shape[i] > 1:
+ R += (((C[i]/float(size-1))*2 - 1 - center[i])/width[i])**2
+ return np.exp(-R/2)
+
+
+#
-----------------------------------------------------------------------------
+if __name__ == '__main__':
+
+
+ fig = figure(size=(800,800))
+ trackball = Trackball(65, 135, 1., 2.5)
+ Z = 0.25*gaussian((32,32), center = (.5,.0))
+ fill, outline = bars(Z)
+ t = 0
+
+ @fig.event
+ def on_init():
+ gl.glEnable( gl.GL_BLEND )
+ gl.glEnable(gl.GL_NORMALIZE)
+ gl.glEnable( gl.GL_LINE_SMOOTH )
+ gl.glEnable( gl.GL_COLOR_MATERIAL )
+ gl.glColorMaterial ( gl.GL_FRONT_AND_BACK,
gl.GL_AMBIENT_AND_DIFFUSE )
+ gl.glBlendFunc (gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
+ gl.glEnable (gl.GL_LIGHTING)
+ gl.glEnable (gl.GL_LIGHT0)
+ gl.glEnable (gl.GL_LIGHT1)
+ gl.glEnable (gl.GL_LIGHT2)
+
+ @fig.event
+ def on_mouse_drag(x, y, dx, dy, button):
+ trackball.drag_to(x,y,dx,dy)
+ fig.redraw()
+
+ @fig.event
+ def on_draw():
+ fig.clear()
+ trackball.push()
+ gl.glLightfv (gl.GL_LIGHT0, gl.GL_POSITION,(-1.0,1.0, 1.0, 1.0))
+ gl.glLightfv (gl.GL_LIGHT1, gl.GL_POSITION,(1.0, 1.0, 1.0, 1.0))
+ gl.glLightfv (gl.GL_LIGHT2, gl.GL_POSITION,(0.0,-1.0, 1.0, 1.0))
+ gl.glEnable (gl.GL_LIGHTING)
+
+ gl.glEnable( gl.GL_POLYGON_OFFSET_FILL )
+ gl.glPolygonOffset (1, 1)
+ gl.glPolygonMode( gl.GL_FRONT_AND_BACK, gl.GL_FILL )
+ gl.glColor( 1.0, 1.0, 1.0, 1.0 )
+ fill.draw( gl.GL_TRIANGLES, 'pnc' )
+ gl.glDisable( gl.GL_LIGHTING )
+ gl.glDisable( gl.GL_POLYGON_OFFSET_FILL )
+ gl.glPolygonMode( gl.GL_FRONT_AND_BACK, gl.GL_LINE )
+ gl.glDepthMask( gl.GL_FALSE )
+ gl.glColor( 0.0, 0.0, 0.0, 0.25 )
+ outline.draw( gl.GL_LINES, 'p' )
+ gl.glDepthMask( gl.GL_TRUE )
+
+ trackball.pop()
+
+ @fig.timer(60.0)
+ def timer(dt):
+ global t
+ t = t+dt
+ vertices = fill.vertices
+ c,s = np.cos(t), np.sin(t)
+ T = 0.25*gaussian(shape = Z.shape, center = (0.5*c,0.5*s))
+ T = np.repeat(T,12).reshape(T.size,12)
+ vertices['position'][:,[0,1,2,3,4,5,8,11,12,15,18,19],2] =
T-T.min()+0.001
+ fill.upload()
+ outline.upload()
+ fig.redraw()
+
+
+ fig.show()