Boids example

78 views
Skip to first unread message

marmaduke

unread,
Jun 13, 2012, 11:10:47 AM6/13/12
to glumpy-users
Hi

I was wondering if the boids example would be available?

Also, has anyone used glumpy and PyCUDA together? Any success there?


Thanks for the great library!


Marmaduke

Nicolas Rougier

unread,
Jun 13, 2012, 11:37:48 AM6/13/12
to glumpy...@googlegroups.com

Sure, it is available from the demos directory:


Nicolas

Marmaduke Woodman

unread,
Jun 13, 2012, 11:50:26 AM6/13/12
to glumpy...@googlegroups.com
Ah ok... it's not in the tarball under downloads, so I missed it..

Thanks!

Marmaduke Woodman

unread,
Jun 13, 2012, 2:11:01 PM6/13/12
to glumpy...@googlegroups.com
Hi

I am trying to combine something like the boids example with a glumpy.Image, using the subfigure capability (see the full script at https://gist.github.com/2925585), but when I draw the image, the gl points figure doesn't display. When I don't call image.draw, the gl points do display.

I've used lock/unlock pairs on both subfigures, and they work independently.. any ideas ?

Thanks,
Marmaduke

On Wed, Jun 13, 2012 at 5:37 PM, Nicolas Rougier <nicolas...@gmail.com> wrote:

Nicolas Rougier

unread,
Jun 14, 2012, 2:18:49 AM6/14/12
to glumpy...@googlegroups.com

I would need the dsi/centres.npy to test your example.
If that can help, here is the boids example with a background image.

Nicolas

-----

import numpy as np
import OpenGL.GL as gl
from scipy.spatial import cKDTree
from glumpy import figure, Trackball, image


if __name__ == '__main__':
    
    fig = figure(size=(800,800))
    frame = fig.add_frame()
    trackball = Trackball(65, 135, 1., 2.0)
    Z = np.random.random((32,32)).astype(np.float32)
    image = image.Image(Z)

    @frame.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(0.95,0.95,0.95,1)
        frame.lock()
        frame.draw()
        trackball.push()
        gl.glEnable(gl.GL_BLEND)
        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-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()
        image.draw( x=0, y=0, z=0, width=frame.width, height=frame.height )
        frame.unlock()


    @frame.timer(60.0)
    def timer(dt):
        global t

        t += 0.5*dt
        target[...] = np.array([np.sin(t),np.sin(2*t),np.cos(3*t)])*.1

        t += 0.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']

        # 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
        D,I = cKDTree(P).query(P,5)
        M = np.repeat(D < 0.05, 3, axis=1).reshape(n,5,3)
        Z = np.repeat(P,5,axis=0).reshape(n,5,3)
        R = -((P[I]-Z)*M).sum(axis=1)

        # Target : Follow target
        T = target - P

        # Predator : Move away from predator
        dP = P - predator
        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

        boids['velocity'] += 0.0005*C + 0.01*A + 0.01*R + 0.0005*T + 0.0025*dP
        boids['position'] += boids['velocity']

        fig.redraw()


    t = 0
    n = 1000
    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()

Reply all
Reply to author
Forward
0 new messages