Recently I've been looking at Povray, pyprocessing, and cfdg (version 3.0) as tools for creating digital images. I have branched two separate blogs where I mainly explore jruby + processing and processing.py

Tuesday, 2 February 2010

An improved (stochastic) 3D tree using LSystems and PeasyCam

My library project is currently still available at Kenai, but I've just been officially warned that Oracle (new owners in case you hadn't heard) are pulling the plug on Kenai apart from their private use. A pity because it was well very well integrated with NetBeans ide. I will probably transfer my project to GitHub because I know it very well from collaborating on ruby-processing projects (it is unquestionably the place for ruby projects).

   1 import processing.opengl.*;
   2 import java.text.*;
   3 import lsystem.*;  // My custom LSystem library available at Kenai version 0.5.0
   4                    // http://kenai.com/projects/l-system-utilities/downloads
   5 import peasy.*;    // PeasyCam custom library v0.8.1 available at 
   6                    // http://mrfeinberg.com/peasycam/#download
   7 PeasyCam cam;
   8 Grammar grammar;
   9 
  10 float distance = 72;
  11 int depth = 3;
  12 float theta = radians(23);
  13 float phi = radians(27);
  14 String production = "";
  15 
  16 void setup() {
  17   size(800, 800, OPENGL);
  18   configureOpenGL();  
  19   cam = new PeasyCam(this, 100);  
  20   cam.setMinimumDistance(100);
  21   cam.setMaximumDistance(500);
  22   setupGrammar();
  23   noStroke();  
  24 }
  25 
  26 void configureOpenGL(){
  27   hint(ENABLE_OPENGL_4X_SMOOTH);
  28   hint(DISABLE_OPENGL_ERROR_REPORT);
  29 }
  30 
  31 void setupGrammar(){
  32   grammar = new StochasticGrammar(this, "F");   // this only required to allow applet to call dispose()
  33   grammar.addRule('F', "F[&+F]F>>[->F]>>[->F]>>[&F]");
  34   grammar.addRule('F', "F>[&+F]F[->F]>[->F]>[&F]");
  35   grammar.addRule('F', "F>>[&+F]>F[->F][->F][&F]");
  36   production = grammar.createGrammar(depth);
  37   distance *= pow(0.5, depth);
  38 }
  39 
  40 void draw() {
  41   background(0);
  42   lights();
  43   render();
  44 }
  45 
  46 void render() {
  47   translate(0, 40, 0);  // center the plant empirically set looks OK to me
  48   fill(0, 175, 0); 
  49   lightSpecular(0, 204, 0); 
  50   specular(0, 255, 0); 
  51   shininess(1.0); 
  52   CharacterIterator it = new StringCharacterIterator(production);
  53   for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
  54     switch (ch) {
  55     case 'F': 
  56       translate(0, distance/-2, 0);
  57       box(distance/9, distance, distance/9);
  58       translate(0, distance/-2, 0);
  59       break;
  60     case '-':
  61       rotateX(theta);
  62       break;
  63     case '+':
  64       rotateX(-theta);
  65       break;
  66     case '>':
  67       rotateY(theta);
  68       break;
  69     case '<':
  70       rotateY(-theta);
  71       break;
  72     case '&':
  73       rotateZ(-phi);
  74       break;
  75     case '^':
  76       rotateZ(phi);
  77       break;
  78     case '[':
  79       pushMatrix();
  80       break;
  81     case ']':
  82       popMatrix();
  83       break;   
  84     default:
  85       System.err.println("character " + ch + " not in grammar");
  86     }
  87   }
  88 }



Follow this link to see an animated version.

9 comments:

  1. hi, can u help me? i have a question about how to determine the probability of stochastic L-systems?

    ReplyDelete
  2. For a java implementation checkout the StochasticGrammar class of my LSystems Utilities for processing. (Available at project Kenai), the download includes java source code and javadocs.

    ReplyDelete
  3. See similar logic in python here:- http://martinpblogformasswritingproject.blogspot.com/2011/02/grammarpy-using-pseudo-file-alternative.html

    Or in ruby here:-
    http://learning-ruby-processing.blogspot.com/2009/12/stoc.html

    ReplyDelete
  4. can you build GUI with this script?

    thanks before

    ReplyDelete
  5. No reason why not, you could use G4PTool by PeterLager http://www.lagers.org.uk/g4ptool/videos/index.html
    for example.

    ReplyDelete
  6. How to change box form into a tube?

    ReplyDelete
  7. Here's how I did it for Rod Hilbert http://www.openprocessing.org/visuals/?visualID=30845, if you wan't open ended tube leave off the sphere/hemispheres.

    ReplyDelete
  8. can i change this to 2D? how do that?
    i need stochastic plant using L-system on 2D

    ReplyDelete
  9. It seems from other of your comments, you've already found 2D plants, it might be worth your while checking out my ruby processing blog, where I've published lots of examples, albeit using a simplified libraries written in ruby.

    ReplyDelete

Followers

Blog Archive

About Me

My photo
Pembrokeshire, United Kingdom
I have developed JRubyArt and propane new versions of ruby-processing for JRuby-9.1.5.0 and processing-3.2.2