Experiments with ruby-processing (processing-2.2.1) and JRubyArt for processing-3.0

Showing posts with label context free DSL. Show all posts
Showing posts with label context free DSL. Show all posts

Wednesday, 19 December 2012

Exporting cf3ruby directly to 'large' pdf

With the processing pdf export library it is easy to export processing sketches to pdf. This is also true for ruby-processing sketches, including those using the cf3ruby library see example below:-
# levy.rb ruby-processing NB: :alpha is now implemented ruby-processing
require 'cf3', 'pdf'
include_package 'processing.pdf'

def setup_the_levy
  @levy = ContextFree.define do
    shape :start do
      levy brightness: 0.9
    end
    shape :levy do
      square alpha: 0.1
     split do
        levy  size: 1/Math.sqrt(2), rotation: -45, x: 0.5, brightness: 0.9
        rewind
        levy  size: 1/Math.sqrt(2), rotation: 45, x: 0.5, brightness: 0.9
     end
    end
  end
end


def setup
  size 2000, 2000, PDF, "levy.pdf"
  setup_the_levy
  smooth
  draw_it
end


def draw
  exit
end


def draw_it
  background 255
  @levy.render :start, size: 250,  stop_size: 2,
        start_x: width/4, start_y: height/2
end

Tuesday, 18 December 2012

Context Free Tree in cf3ruby

#################################################################
# A non deterministic sketch run it until you get a result you like
# uncomment "srand 5" to get a more deterministic result. It looked
# pretty good on my linux box (however I'm not sure how universal the 
# random seeding is in jruby)
#################################################################

require 'cf3'

def setup_the_tree
  @tree = ContextFree.define do
    shape :trunk, 20 do                         # rule has a probability weighting of 20
      circle size: 0.25, brightness: 0.5     # giving an actual probability = 0.952381
      scraggle y: -0.1                      # the minus is require by the upside down coordinate system             
    end

    shape :trunk, 1 do                      # rule has a probability weighting of 1
      branch size: 0.7                      # giving an actual probability = 0.047619  
    end

    shape :trunk, 0.02 do                    # empty rule top stop early    
    end

    shape :branch do
      split do                               # split is like a branch, rewind returns original context
        trunk rotation: 10
      rewind
        trunk rotation: -10
      end
    end

    shape :scraggle do                       # without an explicit weighting    
      trunk rotation: 5                      # probability of each scraggle rule 
    end                                      # is 0.5

    shape :scraggle do
      trunk rotation: -5
    end
  end
end

def setup
  size 600, 600
  srand 5
  smooth
  setup_the_tree
  background 255         # NB color mode here is "RGB 255", within context free definition 
  draw_it                # the color mode is "HSB 1.0", supports :hue, :saturation, :brightness
  save_frame "/home/tux/tree4.png"
end

def draw_it
  @tree.render :trunk, start_x: width/2, start_y: height * 0.9, stop_size: height/150, size: height/15
end

New context_free.rb (cf3ruby)

Here is my "y" window sketch using my modified ruby-processing library (features CF3 syntax, ie shape replaces rule, also ruby 1.9 hash syntax).
# y.rb ruby-processing
require 'cf3'
Y_TOP = -1 / Math.sqrt(3)
Y_BOT = Math.sqrt(3) / 6

def setup_the_triangle
  @triangle = ContextFree.define do
    ########  
    shape :start do
    unit brightness: 1.0
    end

    shape :unit do
      triangle size: 1.0
      split do
        unit size: 0.5, x: 0, y: Y_TOP/2, brightness: 0.8
        rewind
        unit size: 0.5, x: -0.25, y: -Y_TOP/4, brightness: 0.8
        rewind
        unit size: 0.5, x: 0.25, y: -Y_TOP/4, brightness: 0.8
      end
    end
    ########
  end
end

def setup
  size 1024, 1024
  setup_the_triangle
  no_stroke
  color_mode RGB, 1
  smooth
  draw_it
  save_frame("y.png")
end


def draw
   # Do nothing.
end

def draw_it
  background 225, 225, 0
  @triangle.render :start, size: height, stop_size: 0.5,
  start_x: width/2, start_y: height * 0.6
end

Wednesday, 12 December 2012

Fix for ruby-processing context free

The adoption of jruby-1.7.0 for the latest version of processing has required changes to the context_free.rb script. See the change on my Pembrokeshire Branch. My dragon sketch and city sketches working fine, I haven't checked any other. Here is my dragon sketch updated to ruby-1.9 syntax (NB: continues work with 1.8 syntax)



# contributed by monkstone

load_library 'context_free'

def setup_the_dragon
  @dragon = ContextFree.define do

    rule :start do
      dragon alpha: 1
    end

    rule :dragon do
      square hue: 0, brightness: 0, saturation: 1, alpha: 0.02
      split do
        dragon size: 1/Math.sqrt(2), rotation: -45, x: 0.25, y: 0.25
        rewind
        dragon size: 1/Math.sqrt(2), rotation: 135, x: 0.25, y: 0.25
        rewind
      end
    end

  end
end

def setup
  size 800, 500
  setup_the_dragon
  smooth
  draw_it
end

def draw
  # Do nothing.
end

def draw_it
  background 255
  @dragon.render :start, size: width*0.8,  stop_size: 2,
                         start_x: width/3, start_y: height/3.5
end




















Here's a sketch with a simple custom shape of an isosceles triangle
load_library 'context_free'

def setup_the_spiral
  @spiral= ContextFree.define do
    ############ Begin defining custom terminal, an isoceles triangle     
    class << self
      define_method(:isoceles) do |some_options| # isoceles triangle
        size, options = *self.get_shape_values(some_options)
        rot = options[:rotation]
        rotate(rot) if rot
        $app.triangle(-0.5 * size, -0.5 * size, -0.5 * size, 0.5 * size, 0.5 * size, 0.5 * size)
        rotate(-rot) if rot
      end
    end
    ########### End definition of custom terminal 'isoceles'
    rule :spiral do
      isoceles brightness: -1, rotation: 90
      spiral rotation: 135, size: 1/sqrt(2), x: 1/sqrt(2)
    end
  end
end

def setup
  size 800, 500
  setup_the_spiral
  draw_it
end

def draw
  # Do nothing.
end

def draw_it
  background 255
  @spiral.render :spiral, size: height, start_x: width/3, start_y: height/2
end

Sunday, 26 September 2010

A Staffordshire Knot Using Ruby Processing and A Context Free Library

A Staffordshire Knot, ruby-processing 3D context-free DSL from monkstone on Vimeo.


Using a slight variation of the previous post we get something similar to the Staffordshire Knot.


segment :rz => i, :ry => Math.sin(i*Math::PI/180)*0.2, :x => Math.sin(i*Math::PI/180)*1.5


Is the main change, although to produce the video, I was saving frames and set the size to 640*480 to match vimeo preferences. Was very jumpy when I saved frames as #####.png ...

I have since uploaded an improved version, this time using the default save_frame (hence tif format). Mencoder doesn't like this format so I batch converted the tif files to png as so:-

mogrify -format png -transparent blue *.tif


See my processing blog for the video recording details.

Saturday, 11 September 2010

3D Context Free DSL Screen Saver?

Here is an animated version of the default.es from StructureSynth in ruby-processing. Mac or windows users might want to experiment with OPENGL rendering, as this sketch exposes errors in processing P3D, I see odd missing pixels from time to time. If you don't like the holes remove the smooth, personally I prefer the holes to the glitchty non-smooth (it is a known issue with current versions of processing with both P2D and P3D renderers, something to do with tessellation algorithms).  The test_free library is as in previous post.
Nest the library as follows:-
library/test_free/test_free.rb in the folder containing the sketch
For instructions for installing ruby-processing follow the first link in the blog header.


# full_screen.rb
# An animation of default.es
# the StructureSynth default script

load_libraries :test_free
full_screen
attr_reader :xrot, :yrot, :zrot

def setup_the_spiral
  @spiral = ContextFree.define do

    rule :default do
      split do
      R1 :brightness => 1
      rewind
      R2 :brightness => 1
    end
    end

    rule :R1 do                      
      sphere :brightness => 1
      R1 :size => 0.99, :x => 0.25, :rz => 6, :ry => 6
    end

    rule :R2 do                      
      sphere :brightness => 1
      R2 :size => 0.99, :x => -0.25, :rz => 6, :ry => 6
    end

  end
end

def setup
  render_mode P3D
  smooth
  @xrot = 0.01
  @yrot = 0
  @zrot = 0
  setup_the_spiral
end

def draw
  background 0
  lights
  smooth_rotation(6.7, 7.3)
  @spiral.render :default, :start_x => 0, :start_y => 0, :start_z => -50, :size => height/350,
  :stop_size => 0.2, :color => [0, 0.8, 0.8], :rx => xrot, :ry => yrot, :rz => zrot
end

##
# Generate a vector whose components change smoothly over time in the range [ 0, 1 ].
# Each component uses a Math.sin() function to map the current time in milliseconds somewhere
# in the range [ 0, 1 ].A 'speed' factor is specified for each component.
#
def smooth_vector(s1, s2)
  mills = millis * 0.00003
  y = 0.5 * Math.sin(mills * s1) + 0.5
  z = 0.5 * Math.sin(mills * s2) + 0.5
  vec = [y, z]
end

##
# Rotate the current coordinate system.
# Uses smooth_vector() to smoothly animate the rotation.
#
def smooth_rotation(s1, s2)
  r1 = smooth_vector(s1, s2)
  @yrot = (2.0 * Math::PI * r1[0])
  @zrot = (2.0 * Math::PI * r1[1])
end






Followers

About Me

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