ContextFree in Scheme

If you have a design you're proud of, share the cfdg file here. It's also a good place to ask for feedback and collaborate.

Moderators: MtnViewJohn, chris, mtnviewmark

Post Reply
dharmatech
Posts: 6
Joined: Mon Mar 09, 2009 3:26 pm

ContextFree in Scheme

Post 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))

dharmatech
Posts: 6
Joined: Mon Mar 09, 2009 3:26 pm

Re: ContextFree in Scheme

Post 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

User avatar
Monkstone
Posts: 13
Joined: Fri Jul 03, 2009 10:40 pm
Location: Pembrokeshire, UK
Contact:

Re: ContextFree in Scheme

Post 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.

dharmatech
Posts: 6
Joined: Mon Mar 09, 2009 3:26 pm

Re: ContextFree in Scheme

Post 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

dharmatech
Posts: 6
Joined: Mon Mar 09, 2009 3:26 pm

Re: ContextFree in Scheme

Post 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

User avatar
Monkstone
Posts: 13
Joined: Fri Jul 03, 2009 10:40 pm
Location: Pembrokeshire, UK
Contact:

Re: ContextFree in Scheme

Post 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.

User avatar
MtnViewJohn
Site Admin
Posts: 882
Joined: Fri May 06, 2005 2:26 pm
Location: Mountain View, California
Contact:

Re: ContextFree in Scheme

Post 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.

dharmatech
Posts: 6
Joined: Mon Mar 09, 2009 3:26 pm

Re: ContextFree in Scheme

Post 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.

User avatar
Monkstone
Posts: 13
Joined: Fri Jul 03, 2009 10:40 pm
Location: Pembrokeshire, UK
Contact:

Re: ContextFree in Scheme

Post 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.

dharmatech
Posts: 6
Joined: Mon Mar 09, 2009 3:26 pm

Re: ContextFree in Scheme

Post 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!

Post Reply