Ruby Processing

Here is my blog in which I will describe my experiments with ruby-processing, find out more about ruby-processing at:- https://github.com/jashkenas/ruby-processing compatible with processing-2.2.1 and https://github.com/monkstone/cf3ruby for my version of the cfdg DSL (context-free-art)

Thursday, 28 May 2015

Making use of ruby-2.0 syntax to test weighted rule selection

def weighted_random(rules)
  total = rules.values.reduce(&:+)
  srand
  chance = rand(0..total)
  rules.each do |item, weight|
    return item unless chance > weight
    chance -= weight
  end
end

symbols = %i(bird fish turtle)        # ruby 2.0
weights = [0.5, 0.4, 0.1]             # for convenience sum = 1.0
rules = symbols.zip(weights).to_h     # ruby 2.0

count_bird = 0
count_fish = 0
count_turtle = 0

10_000.times do
  case weighted_random(rules)
  when :bird
      count_bird += 1.0
  when :fish
      count_fish += 1.0
  when :turtle
      count_turtle += 1.0
  end
end

puts format('bird   %0.4f', count_bird / 10_000)
puts format('fish   %0.4f', count_fish / 10_000)
puts format('turtle %0.4f', count_turtle / 10_000)

Result
bird   0.5001
fish   0.3976
turtle 0.1023

Tuesday, 26 May 2015

Alternative development environments for ruby-processing and JRubyArt

NetBeans

No really, netbeans can be used as a development environment for ruby-processing (and might be a sensible choice on windows). The first hurdle is to install the jruby netbeans plugin thereafter things get easier (you can use the plugin to install ruby-processing gem, but you will still need to install vanilla processing, unless using JRubyArt). NB you sometimes have to be a bit patient (ruby-plugin slows down the ide and loading gems etc can take a time).

JEdit

Not really an ide but comes close with the ruby-plugin (but the plugin is in serious need of an update ruby 1.86) see live editing and my jedit resources here for my macro and console plugins

Vim

An editor favoured by many ruby users (combine with tmux etc for true awesomeness). Create a sketch in vim, fire it up with rp5 watch and you have a nice replish way of working.
To simply run the sketch you are currently editing :!rp5 run % or to force use of jruby-complete :!rp5 --nojruby run %

Emacs

Some kind person could produce an Emacs major-mode for ruby-processing, like the processing-mode. Still popular...

Textmate / Sublime

Popular on the Mac see ruby-processing textmate bundle, which also could do with updating, but could get you started.

The Processing Ide

Not an option, Tyfkda did some preliminary work on a ruby-mode for processing, but is difficult.

Wednesday, 20 May 2015

Bleeding Edge Stuff With Processing-3.0a9

What a JRubyArt sketch might look like with processing-3.0a9, to set title need to set title of surface (which is a protected field), we make this easy by introducing a 'sketch_title' method that does the clever stuff under the hood. Also note size has moved out of setup to settings method (processing does this for you so you don't see it with vanilla processing). Looking good so far with modified 'trefoil' sketch also monjori' shader (latter requires 'k9 run monjori.rb' as expected), all running with jruby-9.0.0.0-pre2. NB without setting a title get some crazy default title like 'org.jruby.proxy.processing.core.PApplet$Proxy1'.
require 'jruby_art'

class Fred < Processing::AppGL
  def setup
    sketch_title 'Fred' # uses surface.setTitle
  end

  def draw
    background 0
    lights
    fill 200, 0, 0
    translate 150, 150
    rotate_x 0.01 * frame_count
    rotate_y 0.02 * frame_count
    box(90, 90, 90)
  end

  def settings
    size 300, 300, P3D
  end
end
# currently can't set sketch title this way use setup method
Fred.new(title: 'Fred')

Wednesday, 13 May 2015

Some observations and reflections

It now seem very likely that vanilla processing changes will be very significant (this is a good thing) and such changes will likely take some time to solidify. For that reason there is not much point in ruby-processing (or JRubyArt) trying to keep up. Everything seemed to be pointing toward a LWJGL3 (AndrĂ©s Colubri) future (but now it's changed back to jogl update 16 May 2015), and possible javafx integration (Ben Fry). Seems the LWJGL3 project is too unstable?  So near term efforts will concentrate on jruby-9.0.0.0 and ruby-2.2 syntax changes/opportunities using the existing processing-2.2.1 release.  Possible opportunity for jruby games...?

Sunday, 3 May 2015

Adding structure to processing examples (in ruby-processing)

Thinking about ruby-2.0 and structure got me thinking about processing color, but then I though about Struct and OStruct, as easy way to add structure to these processing sketches, see how this gives meaning to fill for example:-
Hue
# Hue is the color reflected from or transmitted through an object
# and is typically referred to as the name of the color (red, blue, yellow,
# etc.). Move the cursor vertically over each bar to alter its hue.
require 'ostruct'

attr_reader :bar_width, :color_array

def setup
  size 640, 360
  color_mode HSB, 360
  no_stroke
  @bar_width = 20
  @color_array = (0..width / bar_width).map do
    OpenStruct.new(hue: height, saturation: height, brightness: height)
  end
end

def draw
  color_array.each_with_index do |col, i|
    n = i * bar_width
    range = (n..n + bar_width)
    color_array[i].hue = mouse_y if range.include?(mouse_x)
    fill col.hue, col.saturation, col.brightness
    rect n, 0, bar_width, height
  end
end

Saturation
# Saturation is the strength or purity of the color and represents the
# amount of gray in proportion to the hue. A "saturated" color is pure
# and an "unsaturated" color has a large percentage of gray.
# Move the cursor vertically over each bar to alter its saturation.
require 'ostruct'

attr_reader :bar_width, :color_array

def setup
  size 640, 360
  color_mode HSB, 360
  no_stroke
  @bar_width = 20
  @color_array = (0..width / bar_width).map do |n|
    OpenStruct.new(hue: n * bar_width, saturation: height, brightness: height / 1.5)
  end
end

def draw
  color_array.each_with_index do |col, i|
    n = i * bar_width
    range = (n..n + bar_width)
    color_array[i].saturation = mouse_y if range.include? mouse_x
    fill col.hue, col.saturation, col.brightness
    rect n, 0, bar_width, height
  end
end

Brightness
# Brightness is the relative lightness or darkness of a color.
# Move the cursor vertically over each bar to alter its brightness.
require 'ostruct'

attr_reader :bar_width, :color_array

def setup
  size 640, 360
  no_stroke
  color_mode HSB, width, 100, height
  @bar_width = 20
  @color_array = (0..width / bar_width).map do |n|
    OpenStruct.new(hue: n * bar_width, saturation: 100, brightness: height)
  end
end

def draw
  color_array.each_with_index do |col, i|
    n = i * bar_width
    range = (n..n + bar_width)
    color_array[i].brightness = mouse_y if range.include? mouse_x
    fill col.hue, col.saturation, col.brightness
    rect n, 0, bar_width, height
  end
end

First class keyword arguments in ruby-processing?

In vanilla processing there are many multi parameter arguments (sometimes overloaded, usual java stuff) that can cause offence to the average rubyist, take camera for example:-
// java/processing 9 float arguments
void camera(
     float eyeX,
     float eyeY,
     float eyeZ,
     float centerX,
     float centerY,
     float centerZ,
     float upX,
     float upY,
     float upZ){...}

// default values
camera(width/2.0, height/2.0, (height/2.0) / tan(PI*30.0 / 180.0), width/2.0, height/2.0, 0, 0, 1, 0);

Since we can now use ruby-2.0 syntax (with jruby-9.0.0.0) there is scope for improvement, by using keyword arguments and Vector object (need not be Vec3D a Struct would do):-
# ruby 3 named parameters (order would be unimportant)
def camera_vec(
               eye: Vec3D.new(width / 2.0, height / 2.0, (height / 2.0) / tan(PI * 30.0 / 180.0)),
               center: Vec3D.new(width / 2.0, height / 2.0, 0),
               up: Vec3D.new(0, 1.0, 0))
  camera(eye.x, eye.y, eye.z, center.x, center.y, center.z, up.x, up.y, up.z)
end

Tuesday, 28 April 2015

Exploring the latest processing-3.0 with JRubyArt (with some success)

I have created a processing-3.0 branch to explore using the latest processing with JRubyArt and it is looking pretty good so far, JAVA2D was easy to get running but P2D and P3D modes took a bit longer. The key to running in the opengl modes was to putting the native binaries on the java.libary.path:-
jruby -Djava.library.path="/home/tux/lib/linux64" trefoil.rb
I needed to change the build to include the lwjgl jars and app.rb to accommodate the changes to processing but also unlike vanilla processing the sketch was also modified, replacing the applet convention of size(width, height, renderer) in setup with 3 methods (not required to be called in setup):-
# Old
  def setup
    size(1024, 768, P3D)
  end
#New
  def sketch_width
    1024
  end

  def sketch_height
    768
  end

  def sketch_renderer
    P3D
  end

Here is a working 'bare' JAVA2D sketch (run using k9 run keyboard.rb) which was tested with jruby-complete-9.0.0.0.pre2.
def setup
  @num_chars = 26
  @key_scale = 200.0 / @num_chars - 1.0
  @rect_width = width / 4
  no_stroke
  background 0
end

def draw
  return unless key_pressed? && ('A'..'z').include?(key)
  key_index = key.ord - (key <= 'Z' ? 'A'.ord : 'a'.ord)
  fill millis % 255
  begin_rect = map1d(key_index, (0..25), (0..width - @rect_width))
  rect begin_rect, 0, @rect_width, height
end

def sketch_width; 640; end

def sketch_height; 360; end

def sketch_renderer; JAVA2D; end

Rubocop will complain of single line definition, but here I think it makes sense! The default renderer is currently JAVA2D, but it looks like Ben is having some success with JavaFX so that might change v. soon. It should be possible to use the java Class loader to set the java.library.path as we do in load_library, but this will probably require 'k9 run sketch', however there is work to be done to even run anything other than Java2D with k9. So presently I think I will concentrate on jruby-9.0.0.0 work.....
Allowing for Ben / Andrés to progress processing-3.0, before I waste any more time.

Followers

About Me

My Photo
I am currently the lead developer of ruby-processing.