:PROPERTIES:
#+title: cellular programming test document
#+author: bsp

 See also: [[file:literate.org::*test 2 (literate)][test 2 (literate)]]

* COMMENT execute source block under cursor with C-c C-c (org-babel-execute-src-block)


* source blocks

** lisp
   #+begin_src emacs-lisp
     (defun org-xor (a b)
       "Exclusive or."
       (if a (not b) b))
   #+end_src
   #+results:
   : org-xor
   
** C
   ;; :results value raw
   #+begin_src C :results output replace
     #include <stdio.h>
     int main(void) {
        printf("hello, world!\n");
        return 0;
     }
   #+end_src
   #+results:
   : hello, world!

** Python
   #+begin_src python
   return "hello, world."
   #+end_src
   #+results:
   : hello, world.

** Shell
   #+begin_src shell :results output replace :eval query
     ls
   #+end_src
   #+results:
   #+begin_example
   cellular.org
   cellular.org.html
   literate.org
   literate.org.html
   myfs1.png
   myfs2.png
   myfunc.png
   myfunc2.png
   org-mode-unicorn.svg
   test_client.org
   test_client.org.html
   #+end_example

   #+begin_src shell :results file link :file "org-mode-unicorn.svg" :eval query
     wget -c "https://orgmode.org/resources/img/org-mode-unicorn.svg"
   #+end_src
   #+results:
   [[file:org-mode-unicorn.svg]]

** TKS
*** test 1 (hello, world.)
   #+begin_src tks :results output replace
     trace "hello, world.";
   #+end_src
   #+results:
   : hello, world.

*** test 2 (version)
   #+begin_src tks :cmd-line "-v" :results output replace
   #+end_src
   #+results:
   : 0.9.86.6 (arm64)

*** test 3 (class)
   #+begin_src tks :results output replace
     class MyClass{test{trace "this.type="+typename(this)+" this.class="+yacMetaClassName();}}
     MyClass c; c.test();
     trace [1 2 3 4];
   #+end_src
   #+results:
   : this.type=Class this.class=MyClass
   : [1, 2, 3, 4]

*** test 4 (args)
   #+begin_src tks :args "1 '2 2.5' 3" :results output replace :eval yes
     trace Arguments;
   #+end_src
   #+results:
   : ["1", "2 2.5", "3"]

*** test 5 (plot)
   #+begin_src tks :cmd-line "-ef lib:plot" :results output replace :eval yes
     function myfunc(float x) { return pow(sin(x), 3); }
     Plot.Run(myfunc, 0, 2PI);
     trace "plot finished";
   #+end_src
   #+results:
   : plot finished

*** test 6 (plot to PNG)
   #+xxbegin_src tks :cmd-line "-ef lib:plot" :results file link :file "myfunc.png" :wrap "org \n#+ATTR_HTML: :width 640"
   #+begin_src tks :cmd-line "-ef lib:plot" :results file link :file "myfunc.png"
     function myfunc(float x) { return pow(sin(x), 3) * 0.9; }
     Plot.SetSize(640, 240); Plot.SavePNG("myfunc.png", myfunc, 0, 2PI);
   #+end_src
   #+results:
   [[file:myfunc.png]]

*** test 7 (plot to PNG 2)
   #+begin_src tks :cmd-line "-ef lib:plot" :results file link :file "myfunc2.png"
     function myfunc(float x) { return sin(x + 0.25 * pow(sin(2*x), 3)) * 0.95; }
     Plot.SetSize(640, 240); Plot.SavePNG("myfunc2.png", myfunc, 0, 2PI);
   #+end_src
   #+results:
   [[file:myfunc2.png]]

*** test 8 (plot fragment shader 1)
   #+begin_src tks :cmd-line "-ep lib:plot-fs" :results output replace :eval yes
     PlotFS.Run("c = vec4(sin(p.x*M_2PI*4), fract(u_t), sin(p.y*M_2PI*9), 0,1);");
   #+end_src
   #+results:
   : 57 frames rendered.

*** test 9 (plot fragment shader 2)
   #+begin_src tks :cmd-line "-ep lib:plot-fs" :results output replace :eval yes
     String sFS <= "vec2 t; float d = 0; float s = 0.6;"
        "t = vec2(p - s*vec2(sin(u_t*1.31), sin(u_t*1.7)));"
        "d += 1.0 - clamp(sqrt(t.x*t.x + t.y*t.y), 0.0, 1.0);"

        "t = vec2(p - s*vec2(sin(u_t*-0.93), sin(u_t*1.9)));"
        "d -= 1.0 - clamp(sqrt(t.x*t.x + t.y*t.y), 0.0, 1.0);"

        "t = vec2(p - s*vec2(sin(u_t*-0.43), sin(u_t*-1.1)));"
        "d += 1.0 - clamp(sqrt(t.x*t.x + t.y*t.y), 0.0, 1.0);"

        "t = vec2(p - s*vec2(sin(u_t*0.43), sin(u_t*1.1)));"
        "d -= 1.0 - clamp(sqrt(t.x*t.x + t.y*t.y), 0.0, 1.0);"

        "if(d < 0.0) d = -d;"
        "d = clamp(pow(d, 2.3), 0.0, 2.0);"
        "c = vec4(mix(vec3(0.05+0.05*(1.0-p.y),0.2+0.1*(1.0-p.y),0.175+0.175*(1.0-p.y)), vec3(0.8+0.1*(1.0-p.x),0.7,0.2), d), 1);"

        "p = (p * 0.5) + 0.5;"
        "p *= 1.0 - p.yx;"
        "d = pow(p.x*p.y*23, 0.85+sin(u_t*0.5)*0.6) * 1.0;"
        "c *= d;"
        //"c = mix(vec4(1,1,1,1), c, d);"
        ;
     PlotFS.Run(sFS);
   #+end_src
   #+results:
   : 105 frames rendered.

*** test 10 (plot fragment shader to PNG)

   #+begin_src tks :cmd-line "-ep lib:plot-fs" :results file link :file "myfs1.png"
     String sFS <= "vec2 t; float d = 0; float s = 0.6;"
        "t = vec2(p - s*vec2(sin(u_t*1.31), sin(u_t*1.7)));"
        "d += 1.0 - clamp(sqrt(t.x*t.x + t.y*t.y), 0.0, 1.0);"

        "t = vec2(p - s*vec2(sin(u_t*-0.93), sin(u_t*1.9)));"
        "d -= 1.0 - clamp(sqrt(t.x*t.x + t.y*t.y), 0.0, 1.0);"

        "t = vec2(p - s*vec2(sin(u_t*-0.43), sin(u_t*-1.1)));"
        "d += 1.0 - clamp(sqrt(t.x*t.x + t.y*t.y), 0.0, 1.0);"

        "t = vec2(p - s*vec2(sin(u_t*0.43), sin(u_t*1.1)));"
        "d -= 1.0 - clamp(sqrt(t.x*t.x + t.y*t.y), 0.0, 1.0);"

        "if(d < 0.0) d = -d;"
        "d = clamp(pow(d, 2.3), 0.0, 2.0);"
        "c = vec4(mix(vec3(0.05+0.05*(1.0-p.y),0.2+0.1*(1.0-p.y),0.175+0.175*(1.0-p.y)), vec3(0.8+0.1*(1.0-p.x),0.7,0.2), d), 1);"

        "p = (p * 0.5) + 0.5;"
        "p *= 1.0 - p.yx;"
        "d = pow(p.x*p.y*23, 2.85+sin(u_t*0.5)*0.6) * 1.0;"
        "c = mix(vec4(1,1,1,1), c, d);"
     ;
     PlotFS.SetT(36.15); PlotFS.SetSize(640, 480);
     if 0 PlotFS.Run(sFS); else PlotFS.SavePNG("myfs1.png", sFS);
   #+end_src
   #+RESULTS:
   [[file:myfs1.png]]

*** test 11 (plot fragment shader with controls)

   #+begin_src tks :cmd-line "-ep lib:plot-fs" :results output replace :eval yes
     String sFS <= "c = vec4(u_r + u_r*sin(u_t+sin(p.x*M_2PI*0.4 * p.y))*u_scl1, "
        "u_g + u_g*sin(u_t*1.3 + sin( ((p.x*0.5)+(p.y*0.6))*M_2PI*0.5)*0.2)*u_scl2, "
        "u_b+u_b*sin(u_t*p.x*1.1)*sin(u_t*1.23*p.y)*u_scl3, 1);"
        ;
     PlotFS.SetT(122.3); PlotFS.SetSize(640, 240);
     PlotFS.AddCtl("r", 0.5);
     PlotFS.AddCtl("g", 0.6);
     PlotFS.AddCtl("b", 0.7);
     PlotFS.AddCtl("scl1", 0.05);
     PlotFS.AddCtl("scl2", 0.1);
     PlotFS.AddCtl("scl3", 0.7);
     PlotFS.SetPNGOutput("myfs2.png", 1/*bCtls*/);
     PlotFS.Run(sFS);
   #+end_src
   #+results:
   #+begin_example
   [dbg] UI.SaveScreenShotPNG: pathName="myfs2.png" off=(0,0) s=(640,460)
   [...] Texture::saveImage("myfs2.png")
   [...] exportPNG: wrote "myfs2.png"
   PlotsFS.AddCtl("r", 0.5);
   PlotsFS.AddCtl("g", 0.6);
   PlotsFS.AddCtl("b", 0.7);
   PlotsFS.AddCtl("scl1", 0.05);
   PlotsFS.AddCtl("scl2", 0.1);
   PlotsFS.AddCtl("scl3", 0.7);
   42 frames rendered.
   #+end_example
   [[file:myfs2.png]]

** TKS-remote
  * start test_host
   Must done on shell (editor hangs otherwise)
   #+begin_src shell :results output replace :eval query
     #!/bin/zsh
     #setopt NO_HUP
     tks app:test_host &!
   #+end_src
   #+results:
   #+results:
   : 

*** test 1 (live coding)
   Connects to script host ~test_host~ and evaluates some code in the context of the running host program.
   The host ~MyAPI~ provides a few members and methods to play with.
   #+begin_src tks-rs :host "test_host" :results output replace
     trace "hello, world.";
     trace "i="+MyAPI.i+" f="+MyAPI.f+" s=\""+MyAPI.s+"\"";
     trace "MyAPI.Add(23, 42)="+MyAPI.Add(23, 42);
     trace "MyAPI.Add(1.23, 4.56)="+MyAPI.Add(1.23, 4.56);
   #+end_src
   #+results:
   : hello, world.
   : i=42 f=3.14159 s="mystring"
   : MyAPI.Add(23, 42)=65
   : MyAPI.Add(1.23, 4.56)=5.79


* Link test
** TODO move to another document

*** some section
    an anchor that the link below refers to

*** links
    M-x org-store-link
    C-c C-L : insert link  (C-u C-c C-L inserts file link)
    C-c C-l : edit link
    C-c C-o : open link
    [[file:~/tmp/t.txt]]

    link to [[*some section][some section]]
    link to [[file:~/Projects/emacs_cheats.txt::org mode links]]
    ~/Projects/emacs_cheats.txt

* Notes
** COMMENT use ":results output discard" to quietly ignore stdout messages
** COMMENT ":eval query" forces confirmation
** COMMENT M-x org-toggle-inline-images
** COMMENT M-x htmlize-buffer
