5b3ce8999105 — Leonard Ritter 24 days ago
added randproj.sc
2 files changed, 160 insertions(+), 0 deletions(-)

M lib/tukan/random.sc
A => testing/randproj.sc
M lib/tukan/random.sc +3 -0
@@ 319,6 319,9 @@ inline Random (bits opts...)
                 max -1.0 u
             u - (? (orig < 0) -1.0 1.0) + 0.5
 
+        fn sign (self)
+            sign (('random self) * 2.0 - 1.0)
+
 if main-module?
     # use the default 32-bit setseq random generator
     local rnd : (Random)

          
A => testing/randproj.sc +157 -0
@@ 0,0 1,157 @@ 
+
+using import Array
+using import glm
+using import itertools
+
+import ..lib.tukan.use
+using import tukan.random
+using import tukan.color
+using import tukan.bitmap
+
+local rng : (Random)
+'seed rng 3
+
+#let z = 1.0
+#for i in (range 100)
+    %2 := z * z
+    %1 := ('random rng) * %2
+    %0 := %2 - %1
+    y := (sqrt %1) * ('sign rng)
+    x := (sqrt %0) * ('sign rng)
+    print x y
+
+let Q = 256
+N := Q * Q
+#T := 4096
+T := N
+local level0 : (Array i32)
+'resize level0 N
+local weights : (Array f32)
+'resize weights N 1.0
+
+fn coord (n)
+    _ (n % Q) (n // Q)
+fn index (x y)
+    ((y + Q) % Q) * Q + ((x + Q) % Q)
+
+fn moddist (a b q)
+    let d = (abs (a - b))
+    min d (q - d)
+
+fn... ansicolor (col : vec3)
+    let r g b = (unpack (ivec3 (* 255.0 (clamp col (vec3 0) (vec3 1)))))
+    r := (tostring r)
+    g := (tostring g)
+    b := (tostring b)
+    .. "\x1b[38;2;" r ";" g ";" b "m"
+
+local totalcount = 0.0
+phi := 2.0 / ((sqrt 5.0) + 1.0)
+for i in (range T)
+    if ((i % Q) == 0)
+        print "progress:" (i * 100 / T) "%"
+    let slots = ((countof level0) as i32)
+    #let k = ('random rng)
+    #let k = ((0.5 + phi * (i as f32)) % 1.0)
+    let k =
+        #'random rng
+        do
+            let g = 1.32471795724474602596
+            let a1 = (1.0 / g)
+            let a2 = (1.0 / (g * g))
+            /
+                index
+                    (((0.5 + a1 * (i as f32)) % 1.0) * Q) as i32
+                    (((0.5 + a2 * (i as f32)) % 1.0) * Q) as i32
+                N
+    local totalweight = 0.0
+    local maxweight = 0.0
+    for w in weights
+        totalweight += w
+        maxweight = (max maxweight w)
+    #if (i == (T - 1))
+        for y in (range Q)
+            for x in (range Q)
+                n := y * Q + x
+                let w = ((weights @ n) / maxweight)
+                io-write!
+                    ansicolor (vec3 w)
+                io-write! "██"
+            io-write! "\n"
+        if true
+            exit 0
+
+    fold (w = 0.0) for n in (range slots)
+        let inthis = (weights @ n)
+        let prob = (inthis / totalweight)
+        #let prob = (1.0 / N)
+        let w = (w + prob)
+        if (k < w)
+            level0 @ n = i
+            let cx cy = (coord n)
+
+            let maxlen = (length (vec2 Q Q))
+
+            for i u in (enumerate weights)
+                let x y = (coord i)
+                let dx = ((moddist x cx Q) / Q)
+                let dy = ((moddist y cy Q) / Q)
+                let d = (length (vec2 dx dy))
+                #let phi = (sqrt 0.04)
+                #let phi = (sqrt 0.001)
+                #let phi = (sqrt 0.1)
+                #let phi = (sqrt 0.01)
+                let g =
+                    /
+                        exp
+                            /
+                                - (pow d 2.0)
+                                2.0 * phi * phi
+                        1.0 #phi * (sqrt tau)
+                let w = (1.0 - g)
+                u = (u / maxweight) * w
+                #u *= w
+            break w
+        w
+    totalcount += 1.0
+#for y in (range Q)
+    fy := y / Q
+    for x in (range Q)
+        n := y * Q + x
+        let w = ((level0 @ n) / T)
+        io-write!
+            ansicolor w
+            #ansicolor (step fy w)
+        io-write! "██"
+    io-write! "\n"
+#print (ansicolor (vec3 1)) "\n"
+#fn... ansicolor (col : vec3)
+    let r g b = (unpack (ivec3 (* 255.0 (clamp col (vec3 0) (vec3 1)))))
+    r := (tostring r)
+    g := (tostring g)
+    b := (tostring b)
+    .. "\x1b[38;2;" r ";" g ";" b "m"
+
+let outimage =
+    Bitmap4 (ivec2 Q Q)
+for x y in (dim Q Q)
+    n := (index x y)
+    let src = (vec3 ((level0 @ n) / T))
+    let dst = ('fetch outimage x y)
+    let r g b = (unpack (ivec3 ((clamp src (vec3 0) (vec3 1)) * 255.0)))
+    dst @ 0 = r as u8
+    dst @ 1 = g as u8
+    dst @ 2 = b as u8
+    dst @ 3 = 255:u8
+'save-png outimage (.. module-dir "/rpbluenoise.png")
+
+local minv = 0x7fffffff
+local maxv = 0
+local total = 0
+for i c in (enumerate level0)
+    minv = (min minv c)
+    maxv = (max maxv c)
+    total += c
+print "min" minv "max" maxv "total" total
+
+;
  No newline at end of file