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  
 
 

No comments:

Post a Comment