Page 1 of 1

ContextFree in Scheme

Posted: Mon Mar 09, 2009 3:41 pm
by dharmatech
Hello!

I have an implementation of the ContextFree semantics in Scheme. Rendering is done via OpenGL.

Here's a screenshot of Larceny Scheme rendering the 'spirales' cfdg:

http://proteus.freeshell.org/abstractin ... irales.png

The code for the model in Scheme is given below.

I say the "semantics" are implemented because it doesn't accept the input in CFDG language, but rather as actual Scheme code.

The implementation and examples are available as part of a framework called 'abstracting':

http://github.com/dharmatech/abstracting/tree/master

Anybody who's curious to actually play with it, just follow the directions in the README. If you want to browse the code, it's in 'examples/cfdg'.

I think it's interesting that the implementation clocks in at 265 lines. :-)

Feedback welcome!

Ed

Code: Select all


(cfdg-model

 (define (chunk)
   (block (lambda () (circle)))
   (block (lambda () (size 0.3) (flip 60.0) (line))))

 (define (a1)

   (when (iterate?)

         (block
          (lambda ()
            (size 0.95)
            (x 2.0)
            (rotate 12)
            (brightness 0.5)
            (hue 10.0)
            (saturation 1.5)
            (a1)))

         (block (lambda () (chunk)))))

 (define (line)

   (alpha -0.3)

   (block (lambda () (rotate   0) (a1)))
   (block (lambda () (rotate 120) (a1)))
   (block (lambda () (rotate 240) (a1))))
   
 (set! background (lambda () (brightness -1)))

 (set! viewport (vec -20 40 -20 40))

 (set! start-shape (lambda () (line)))

 (set! threshold 0.05))

Re: ContextFree in Scheme

Posted: Thu Jun 10, 2010 12:01 pm
by dharmatech
Hello,

I recently worked on a version of this using Ypsilon Scheme which has bindings to Cairo. Here's an example. The implementation is on github.

Here's an older article about the OpenGL based implentation.

Finally, here's a list of ContextFreeArt inspired systems I've found.

Ed

Re: ContextFree in Scheme

Posted: Sat Jun 12, 2010 12:01 pm
by Monkstone
Excellent, well so far as I've experimented. I have had lot of experience with the ruby-processing context free DSL. So I was very interested to give your scheme implementation a go. Here's the code for the Levy curve fractal that I produced.

Code: Select all

;Levy fractal
;By monkstone
;

(import (psilab cfdg core)
        (psilab cfdg rule))

(rule levy
  (1 (square (hue 0))
     (levy 
      (rotate 45) 
      (scale (/ 1 (sqrt 2)))
      (brightness 0.1)
      (saturation 0.1)
      (x 0.5)
     )
     (levy 
      (rotate -45) 
      (scale (/ 1 (sqrt 2)))
      (brightness 0.1)
      (saturation 0.1)
      (x 0.5)
     )      
   )
)


(init-cfdg)

(background (brightness -1))

(bounds -1.2 1.2 -1.2 1.2)

(start-shape levy)
Image

The result is not entirely as expected as there seem to be the odd bit of "curve" missing, but I don't know Scheme and including installing ypsilon Scheme and your library it only took about an hour to figure it out. 8)
Now I've got an excuse for exploring the Scheme language. I think I know what the problem is re the shape, I want 100% probability of square and 50% probability of each recursive levy but so far my experiments lead to syntax error, perhaps you can put me right?

Update 13th June, I've done some more experimenting, and this time I've produced a half decent Levy Carpet see my blog http://martinpblogformasswritingproject ... silon.html
which also shows off my evolving syntax highlight mode for jEdit.

Re: ContextFree in Scheme

Posted: Sun Jun 13, 2010 1:58 pm
by dharmatech
Monkstone wrote:I have had lot of experience with the ruby-processing context free DSL.
Oh wow... I've had you're Ruby Processing blog in my newsreader for a while now! :-)

Ed

Re: ContextFree in Scheme

Posted: Sun Jun 13, 2010 2:31 pm
by dharmatech
Monkstone wrote: The result is not entirely as expected as there seem to be the odd bit of "curve" missing, but I don't know Scheme and including installing ypsilon Scheme and your library it only took about an hour to figure it out. 8)
Now I've got an excuse for exploring the Scheme language. I think I know what the problem is re the shape, I want 100% probability of square and 50% probability of each recursive levy but so far my experiments lead to syntax error, perhaps you can put me right?
I'm impressed that you got it working while being new to Scheme!

Thanks for testing it out. This example in particular is interesting because it reveals a difference in the rendering model as I've implemented it. Other examples I've ported from the gallery seem to render roughly the same. I'll have to investigate the official CFDG rendering model more closely.

Here's an outline of the original code you posted:

Code: Select all

(rule levy
  (1 (square (hue 0))
     (levy (rotate 45) ...)
     (levy (rotate -45) ...)))
In the the Scheme version, the first recursive call seems to "dominate" the other in the resulting picture. The 'rule' macro call there roughly expands into a procedure:

Code: Select all

(define (rule)
  (square)
  (save) (rotate  45) (levy) (restore)
  (save) (rotate -45) (levy) (restore))
I.e. the implementation is thus doing a depth-first traversal of the model. The official CFDG seems to draw the model in a breadth-first way.

After some experimenting with your code, one way I found to help the other branch "shine through" is to reduce the alpha a bit. And looking at your blog post, it seems you also took this route! :-)

By the way... The 'import' form I have in my examples only pulls in the '(psilab cfdg core)' and '(psilab cfdg rule)' libraries. You can also pull in '(rnrs)' to make the full Scheme language available in your scripts:

Code: Select all

(import (rnrs)
        (psilab cfdg core)
        (psilab cfdg rule))
Let me know if you have any questions along the way!

Ed

Re: ContextFree in Scheme

Posted: Mon Jun 14, 2010 6:31 am
by Monkstone
Thanks for the reply, interesting that the ruby-processing DSL implementation also seems to manage the Levy correctly see http://learning-ruby-processing.blogspo ... rch?q=levy one of my old posts. The rewind feature of ruby processing restores the context before the second recursive call to the levy rule. The tip regarding importing regular Scheme was also welcome, I kind of guessed something of the kind, but my learning curve on Scheme is very steep so every little helps.
One thing missing from ruby-processing is the z attribute (depth sorting) which comes in handy in regular cfdg to ensure the desired order of rendition. I expect that is possible with opengl, but possibly not with cairo? Looking at your cairo libs custom terminals (ie other than square, circle & triangle) should be possible.

Re: ContextFree in Scheme

Posted: Mon Jun 14, 2010 9:15 am
by MtnViewJohn
Monkstone wrote:...
One thing missing from ruby-processing is the z attribute (depth sorting) which comes in handy in regular cfdg to ensure the desired order of rendition. I expect that is possible with opengl, but possibly not with cairo? Looking at your cairo libs custom terminals (ie other than square, circle & triangle) should be possible.
The cfdg implementation of z happens before drawing, so it is independent of graphics. We just take the list of primitive shapes and paths and stable-sort it on z.

Re: ContextFree in Scheme

Posted: Mon Jun 14, 2010 3:09 pm
by dharmatech
Monkstone wrote:The tip regarding importing regular Scheme was also welcome, I kind of guessed something of the kind, but my learning curve on Scheme is very steep so every little helps.
As you probably know, there's a bunch of Scheme books and alot of material on the net. However, Ypsilon is an implementation of the R6RS standard in particular. The 4th edition of the book The Scheme Programming Language by Kent Dybvig has been updated to cover R6RS. It's available online.

Re: ContextFree in Scheme

Posted: Thu Jun 17, 2010 8:44 am
by Monkstone
Thanks for the book recommendation, I'm very slowly working through it, however I managed to produce another sketch that kind of exploits the different rendition by ruby-processing and now scheme, I'm not sure I ever managed to reproduce it with cfdg.
Image
Image is of the scheme version.
Scheme code is on my blog http://martinpblogformasswritingproject ... -free.html.
The ruby processing version was produced whilst I was experimenting with a Sierpinski triangle http://learning-ruby-processing.blogspo ... ssing.html. Bright window Scheme, dark window ruby processing. I should try a cfdg version I suspect z would come in useful to keep the asymmetry.

Re: ContextFree in Scheme

Posted: Thu Jun 17, 2010 12:39 pm
by dharmatech
Monkstone wrote:however I managed to produce another sketch that kind of exploits the different rendition by ruby-processing and now scheme, I'm not sure I ever managed to reproduce it with cfdg.
Scheme code is on my blog http://martinpblogformasswritingproject ... -free.html.
Wow... that's a nice one!