Source

fungus / fungus_advection.py

Full commit
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# encoding: utf-8

"""Showing the moving blobs.

Requires ./blobs_on_plb_velocity_field.py in the path.
"""

from sys import path
from os import listdir
from time import time
import sys
path.append("/home/arne/Projekte/Diplom/sonstiges/Textursynthese")
path.append("/home/arne/Diplomarbeit/sonstiges/Textursynthese")

#### Call the correct fungus_game when called from the command line or clicked in a GUI ####

if __name__ == "__main__": 
    # Call this Scene via fungus_game
    # For this to work, the main scene inside the module MUST be the class with the name "Scene"
    from fungus_core import call_this_scene
    # pass the commandline args
    from sys import argv
    call_this_scene(__file__, argv)


#### Imports ####

from fungus_core import Sprite
from pyglet.window import key
from pyglet import gl

# display
from fungus_scene import BaseScene

# simulation
from synth_blobs import setup, advect_blobs
#from blobs_on_plb_velocity_field import Simulator, blobs

from optparse import OptionParser
from sys import argv
parser = OptionParser()

parser.add_option("--check-coarse",
                  action="store_true", dest="coarse", default=False,
                  help="Check the coarse matrizes")

(options, args) = parser.parse_args()



### Scene data

IMAGE_BLOB = "blob_transparent_borders_monotone_klein.png"

### The Scene itself. 

class Scene(BaseScene): 
    """A dummy scene - mostly just the Scene API."""
    def __init__(self, core, *args, **kwds): 
        """Initialize the scene with a core object for basic functions."""
        super(Scene, self).__init__(core, *args, **kwds)

        # set the background color to white
        gl.glClearColor(255, 255, 255, 0)

        # and the simulated blobs along with additional synthesizers for new blobs
	if options.coarse: num = 1
	else: num = 30
        self.blobs, self.synth, self.velocity_matrizes = setup(picklepath=None, #"datafile-tmp.pickle",
                                                               num=num, 
                                                               synth_pickle="../Textursynthese/datafile_6000_synthesizers.pickle", #"datafile_6000_synthesizers.pickle",
							       vel_pickle="../Textursynthese/datafile_6000_velocity.pickle") # datafile_6000_velocity.pickle")#tracing_resultfile="../Textursynthese/tests/TraceResult_6000-2.dat", num=30)#picklepath="../Textursynthese/datafile.pickle")
	
        self.step = 0
        self.t = time()

    def add_blob(self, blob):
        """Add a new blob"""
        blobby = self.core.sprite(IMAGE_BLOB, x=blob.x, y=blob.y)
        self.visible.append(blobby)

    def update(self): 
        """Update the stats of all scene objects."""
        # show FPS every tenth frame
        if not self.step%10: 
            t = time()
            print "FPS:", 10/(t - self.t)
            self.t = t

	if options.coarse:
	    if not self.visible:
		blobby = self.core.sprite(IMAGE_BLOB, x=1, y=1)
		self.visible.append(blobby)
	    b = self.visible[0]
	    for n in self.velocity_matrizes:
		step = self.step % len(n)
		vel = n[step]
		for x in range(32):
		    for y in range(32):
			for z in range(32): 
			    if vel[x*32**2 + y*32 + z]:
				#print x, y, z, step, len(n)
				b.x, b.y = 32*x, 32*y
				b.draw()
	    print step
	    self.step += 1
	    return

        # update the backend data
        self.blobs = advect_blobs(self.blobs, self.step, self.synth, self.velocity_matrizes)
        self.step += 1

        # synchronize the number of blobs with the backend
        for blob in self.blobs[len(self.visible):]:
            self.add_blob(blob)
        for blob in self.visible[len(self.blobs):]:
            self.visible.remove(blob)

        # synchronize positions
        for i in range(len(self.blobs)):
            self.visible[i].x = self.blobs[i].x# + self.blobs[i].x*0.707
            self.visible[i].y = self.blobs[i].z
            #self.visible[i].scale = 100/self.blobs[i].x