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

Thursday, 13 January 2011

3D Hilbert in processing.py with PeasyCam control

Get processing.py here.

"""
hilbert.py is a python script for use in processing.py. 
It avoids switch, because python does not include it in the language, in my view this
is big mistake switch is very easy on the eye, I dislike chains of if/elif, however the 
alternatives in python are either dodgy or equally ugly
"""

import math
import processing.opengl
import peasy.PeasyCam as PeasyCam

# some globals
XPOS = 0
YPOS = 1
ANGLE = 2
DELTA = math.pi/2

RULES = {
'X' : '^<XF^<XFX-F^>>XFX&F+>>XFX-F>X->'
}

AXIOM = 'X'

production = ""   # need exposure at module level?


def __produce(axiom, rules):
    """
    private single pass rule substitution
    """
    output = ""
    for i in axiom:
        output += rules.get(i, i)    # second i is returned when no dict key found
    return output
    
 
def repeat(rpx, axiom, rules):
    """
    Repeat rule substitution in a recursive fashion rpx times
    """ 
    production = axiom
    for i in range(0, rpx):
        production = __produce(production, rules)
    return production
    

def render(production):        # avoiding switch and globals
    """
    Render evaluates the production string and calls box primitive
    uses affine transforms
    """
    lightSpecular(204, 204, 204) 
    specular(255, 255, 255) 
    shininess(1.0) 
    distance = 20
    
    for val in production:
        if val == "F": 
            translate(0, distance/-2, 0)
            box(distance/7, distance, distance/7)
            translate(0, distance/-2, 0)
        elif val == "+": 
            rotateX(DELTA)
        elif val == "-": 
            rotateX(-DELTA)
        elif val == ">": 
            rotateY(-DELTA)
            repeat = 1
        elif val == "<": 
            rotateY(DELTA)
        elif val == "&": 
            rotateZ(-DELTA) 
        elif val == "^": 
            rotateZ(DELTA) 
        else: 
            pass

def configure_opengl():
    hint(ENABLE_OPENGL_4X_SMOOTH)     
    hint(DISABLE_OPENGL_ERROR_REPORT) 

def setup():
    """
    The processing setup statement
    """
    size(500, 500, OPENGL)
    configure_opengl()
    cam = PeasyCam(this, height/6.5)
    cam.setMinimumDistance(height/10)
    cam.setMaximumDistance(height)    
    global production
    production = repeat(3, AXIOM, RULES)    
    noStroke()
    fill(200, 0, 180)   
    


def draw():
    """
    Render a 3D Hilbert, somewhat centered
    """
    lights()
    background(255)
    translate(-width/6, height/6, -width/10)
    pushMatrix()
    render(production)
    popMatrix()  
 

No comments:

Post a Comment

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