Friday 30 November 2012

Getting Slightly More Up To Date

The released version of jython is languishing at 2.5.2 (I suspect the jython version number might have some sort of relationship to the version of python supported?). This is likely the case because I was surprised when processing.py did not support "string".format(i). Out of interest I recompiled processing.py wth jython-2.7.2a2 and "string".format is supported.  So going forward I intend to use only this or more recent version of jython, since regular python is moving toward version 3.4 (a step change ahead) seemingly some python 3.0 goodness will be back-ported to 2.7 so by the time jython-2.7 is released it will be somewhat more compatible.

Thursday 29 November 2012

Gray Scott Diffusion (Toxiclibs and Processing.py)

   1 """
   2 <p>GrayScottToneMap shows how to use the ColorGradient & ToneMap classes of the
   3 colorutils package to create a tone map for rendering the results of
   4 the Gray-Scott reaction-diffusion.</p>
   5 
   6 <p><strong>Usage:</strong><ul>
   7 <li>click + drag mouse to draw dots used as simulation seed</li>
   8 <li>press any key to reset</li>
   9 </ul></p>
  10 
  11 <p>UPDATES:<ul>
  12 <li>2011-01-18 using ToneMap.getToneMappedArray()</li>
  13 </ul></p>
  14 """
  15 
  16 """ 
  17 Copyright (c) 2010 Karsten Schmidt
  18 
  19 This demo & library is free software you can redistribute it and/or
  20 modify it under the terms of the GNU Lesser General Public
  21 License as published by the Free Software Foundation either
  22 version 2.1 of the License, or (at your option) any later version.
  23 
  24 http:#creativecommons.org/licenses/LGPL/2.1/
  25 
  26 This library is distributed in the hope that it will be useful,
  27 but WITHOUT ANY WARRANTY without even the implied warranty of
  28 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  29 Lesser General Public License for more details.
  30 
  31 You should have received a copy of the GNU Lesser General Public
  32 License along with this library if not, write to the Free Software
  33 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  34 """
  35 import toxi.color.ColorGradient as ColorGradient
  36 import toxi.color.NamedColor as NamedColor
  37 import toxi.color.ToneMap as ToneMap
  38 import toxi.sim.grayscott.GrayScott as GrayScott
  39 
  40 NUM_ITERATIONS = 10 # NB: non-inclusive range in python
  41 
  42 def setup():
  43     size(256,256)
  44     global gs, toneMap
  45     gs = GrayScott(width,height,False)
  46     gs.setCoefficients(0.021,0.076,0.12,0.06)
  47     # create a color gradient for 256 values
  48     grad = ColorGradient()
  49     # NamedColors are preset colors, but any TColor can be added
  50     # see javadocs for list of names:
  51     # http://toxiclibs.org/docs/colorutils/toxi/color/NamedColor.html
  52     grad.addColorAt(0,NamedColor.BLACK)
  53     grad.addColorAt(128,NamedColor.RED)
  54     grad.addColorAt(192,NamedColor.YELLOW)
  55     grad.addColorAt(255,NamedColor.WHITE)
  56     # this gradient is used to map simulation values to colors
  57     # the first 2 parameters define the min/max values of the
  58     # input range (Gray-Scott produces values in the interval of 0.0 - 0.5)
  59     # setting the max = 0.33 increases the contrast
  60     toneMap = ToneMap(0,0.33,grad)
  61     
  62 def draw():
  63     if (mousePressed):
  64         gs.setRect(mouseX, mouseY,20,20)        
  65     loadPixels()
  66     # update the simulation a few time steps
  67     for i in range(0, NUM_ITERATIONS):
  68       gs.update(1)  
  69     # read out the V result array
  70     # and use tone map to render colours
  71     toneMap.getToneMappedArray(gs.v,pixels)
  72     updatePixels()
  73   
  74 def keyPressed():
  75     gs.reset()
 
 
















Here is another Gray Scott Diffusion Sketch

   1 """
   2  * <p>GrayScottImage uses the seedImage() method to use a bitmap as simulation seed.
   3  * In this demo the image is re-applied every frame and the user can adjust the 
   4  * F coefficient of the reaction diffusion to produce different patterns emerging
   5  * from the boundary of the bitmapped seed. Unlike some other GS demos provided,
   6  * this one also uses a wrapped simulation space, creating tiled patterns.</p>
   7  *
   8  * <p><strong>usage:</strong></p>
   9  * <ul>
  10  * <li>click + drag mouse to locally disturb the simulation</li>
  11  * <li>press 1-9 to adjust the F parameter of the simulation</li> 
  12  * <li>press any other key to reset</li>
  13  * </ul>
  14  *
  15  * <p>UPDATES:<ul>
  16  * <li>2011-01-18 using ToneMap.getToneMappedArray()</li>
  17  * </ul></p>
  18 """
  19 
  20 """ 
  21 Copyright (c) 2010 Karsten Schmidt
  22 
  23 This demo & library is free software you can redistribute it and/or
  24 modify it under the terms of the GNU Lesser General Public
  25 License as published by the Free Software Foundation either
  26 version 2.1 of the License, or (at your option) any later version.
  27 
  28 http:#creativecommons.org/licenses/LGPL/2.1/
  29 
  30 This library is distributed in the hope that it will be useful,
  31 but WITHOUT ANY WARRANTY without even the implied warranty of
  32 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  33 Lesser General Public License for more details.
  34 
  35 You should have received a copy of the GNU Lesser General Public
  36 License along with this library if not, write to the Free Software
  37 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  38 """
  39 import toxi.color.ColorGradient as ColorGradient
  40 import toxi.color.NamedColor as NamedColor
  41 import toxi.color.ToneMap as ToneMap
  42 import toxi.sim.grayscott.GrayScott as GrayScott
  43 
  44 def setup():
  45     size(256,256)
  46     global gs, img, toneMap
  47     gs = GrayScott(width,height,True)
  48     img = loadImage("ti_yong.png")
  49     # create a duo-tone gradient map with 256 steps
  50     toneMap = ToneMap(0,0.33,NamedColor.CRIMSON,NamedColor.WHITE,256)
  51     
  52 def draw():
  53     gs.seedImage(img.pixels,img.width,img.height)
  54     if (mousePressed):
  55         gs.setRect(mouseX, mouseY,20,20)        
  56     loadPixels()
  57     for i in range(0, 10):
  58         gs.update(1)
  59     # read out the V result array
  60     # and use tone map to render colours
  61     toneMap.getToneMappedArray(gs.v,pixels)
  62     updatePixels()
  63 
  64 def keyPressed():
  65     if (ord(key)>=49 and ord(key)<=57):
  66         gs.setF(0.02 + (ord(key) - 48) * 0.001)
  67     else:
  68         gs.reset() 
  69  
 
 

Yet another Generative Design Sketch in Processing.py

This is a sketch you definetly need to run to fully appreciate it.
   1 import generativedesign.Node as Node
   2 import generativedesign.Spring as Spring
   3 import generativedesign.Attractor as Attractor
   4 
   5 """
   6 part of the example files of the generativedesign library. 
   7 shows how to use the classes Node, Spring and Attractor in 3d space.
   8 """
   9 
  10 def setup():
  11     """
  12     Setup Sketch smooth should be called here
  13     """
  14     size(512, 512, P3D)
  15     lights()
  16     global nodeA, nodeB, attractor, spring
  17     smooth(8)
  18     fill(0)
  19     nodeA = Node(random(width), random(height), random(-200, 200))
  20     nodeB = Node(random(width), random(height), random(-200, 200))
  21     nodeA.setStrength(-2)
  22     nodeB.setStrength(-2)
  23     nodeA.setDamping(0.1)
  24     nodeB.setDamping(0.1)
  25     nodeA.setBoundary(0, 0, -300, width, height, 300)
  26     nodeB.setBoundary(0, 0, -300, width, height, 300)
  27     
  28     spring = Spring(nodeA, nodeB)
  29     spring.setStiffness(0.7)
  30     spring.setDamping(0.9)
  31     spring.setLength(100)
  32     
  33     attractor = Attractor(width/2, height/2, 0)
  34     attractor.setMode(Attractor.SMOOTH)
  35     attractor.setRadius(200)
  36     attractor.setStrength(5)
  37 
  38 
  39 def draw():
  40     """
  41     The processing draw loop
  42     """
  43     background(50, 50, 200)
  44     lights()
  45     ambientLight(100, 100, 100)
  46     ambient(30)
  47     specular(30)
  48     if (mousePressed == True):
  49         nodeA.x = mouseX
  50         nodeA.y = mouseY
  51         nodeA.z = mouseY - 256
  52     # attraction between nodes
  53     nodeA.attract(nodeB)
  54     nodeB.attract(nodeA)
  55     # update spring
  56     spring.update()
  57     # attract
  58     attractor.attract(nodeA)
  59     attractor.attract(nodeB)
  60     # update node positions
  61     nodeA.update()
  62     nodeB.update()
  63     # draw attractor
  64     stroke(0, 50)
  65     strokeWeight(1)
  66     noFill()
  67     line(attractor.x-10, attractor.y, attractor.x+10, attractor.y)
  68     line(attractor.x, attractor.y-10, attractor.x, attractor.y+10)
  69     ellipse(attractor.x, attractor.y, attractor.radius*2, attractor.radius*2)
  70     # draw spring
  71     stroke(255, 0, 0, 255)
  72     strokeWeight(4)
  73     line(nodeA.x, nodeA.y, nodeA.z, nodeB.x, nodeB.y, nodeB.z)
  74     # draw nodes
  75     noStroke()
  76     fill(212)
  77     pushMatrix()
  78     translate(nodeA.x, nodeA.y, nodeA.z)
  79     sphere(20)
  80     popMatrix()
  81     pushMatrix()
  82     translate(nodeB.x, nodeB.y, nodeB.z)
  83     sphere(20)
  84     popMatrix()