-
Notifications
You must be signed in to change notification settings - Fork 4
/
jul_13_2017_experiment.clj
165 lines (128 loc) · 5.15 KB
/
jul_13_2017_experiment.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
(ns dmm.examples.quil-controlled.jul-13-2017-experiment
(:require [quil.core :as q]
[quil.middleware :as m]
[dmm.core :as dc
:refer [v-accum v-identity
down-movement up-movement
rec-map-sum]]
[clojure.core.async :as async]))
(def mouse-pressed-channel (async/chan))
(defn mouse-pressed-monitor [dummy]
(let [signal ((async/alts!! [mouse-pressed-channel] :default {}) 0)]
{:signal
(if (= signal {})
{}
{:x (signal :x) :y (signal :y) :flag 1})}))
; might want to add a transformation of :button field too
(def v-mouse-pressed-monitor (var mouse-pressed-monitor))
(defn dmm-cons [accum-style-input]
(let [old-self (accum-style-input :self)]
(if (get-in accum-style-input [:signal :flag])
{:self {:this (accum-style-input :signal) :rest old-self}}
{:self old-self})))
(def v-dmm-cons (var dmm-cons))
;;; the neurons of this type track mouse position
;;; we treat X and Y mouse coordinates as separate outputs
(defn mouse-coords [dummy]
{:mouse-x (q/mouse-x) :mouse-y (q/mouse-y)}
)
(def v-mouse-coords (var mouse-coords))
(defn init-state []
(->
{}
((fn[m]
(assoc m
:init-matrix
{v-accum {:self {:accum {v-accum {:self {:single 1}}}}}}
:mouse-tracking-neuron-hook
{v-mouse-coords
{:mouse-position {:single {v-mouse-coords {:mouse-y 1}}}}}
:mouse-pressed-monitor-hook
{v-mouse-pressed-monitor {:mouse-pressed-monitor {:single
{v-mouse-pressed-monitor {:mouse-pressed-monitor {:single 1}}}}}}
:dmm-cons-accum-connection
{v-dmm-cons {:my-list {:self {v-dmm-cons {:my-list {:self 1}}}}}}
:dmm-cons-signal-connection
{v-dmm-cons {:my-list {:signal
{v-mouse-pressed-monitor {:mouse-pressed-monitor {:signal 1}}}}}})))
((fn[m]
(assoc m :start-matrix
(rec-map-sum
(m :init-matrix)
(m :mouse-tracking-neuron-hook)
(m :mouse-pressed-monitor-hook)
(m :dmm-cons-accum-connection)
(m :dmm-cons-signal-connection)))))
((fn[m]
(assoc m :init-output
(rec-map-sum
{v-accum {:self {:single (m :start-matrix)}}}
{v-dmm-cons {:my-list {:self {:end-list 1}}}}))))))
(def state (atom (init-state)))
(defn setup []
;; Set frame rate to 1 frame per second, so that one has time to
;; ponder things.
(q/frame-rate 1)
;; setup function returns initial state. It contains
;; the initial output layer of the generalized neural network.
{:output-layer (@state :init-output)})
(defn update-state [state]
;; Update sketch state by performing one cycle of the "two-stroke engine"
;; of the generalized neural network.
(let [current-input (down-movement (:output-layer state))]
{:input-layer current-input
:output-layer (up-movement current-input)
}))
(defn extract-matrix [current-output]
(((current-output v-accum) :self) :single))
(defn extract-delta [current-output]
((((extract-matrix current-output) v-accum) :self) :delta))
(defn extract-mouse-position [current-output]
(get (current-output v-mouse-coords) :mouse-position "quil quirk"))
;; "quil quirk" - the first iteration "draw-state" happens
;; without "update-state"
(defn extract-list [current-output]
(((current-output v-dmm-cons) :my-list) :self))
(defn extract-mouse-pressed-monitor [current-output]
(current-output v-mouse-pressed-monitor))
(defn render-list [recorded-list horizontal-shift]
;;(q/text (str recorded-list) 25 25)
(when (not (recorded-list :end-list))
(q/no-fill)
(q/rect horizontal-shift (mod (+ 30 (* 50 (q/frame-count))) (q/height)) 20 20)
(q/fill 0)
(q/ellipse
(+ (int (/ (* 20 (get-in recorded-list [:this :x] 0)) (q/width))) horizontal-shift)
(+ (int (/ (* 20 (get-in recorded-list [:this :y] 0)) (q/height))) (mod (+ 30 (* 50 (q/frame-count))) (q/height)))
3 3)
(render-list (recorded-list :rest) (+ horizontal-shift 30))
))
(defn draw-state [state]
;; Clear the sketch by filling it with grey color.
;;(q/background 127)
(q/fill 127 50)
(q/rect 0 0 (q/width) (q/height))
(q/fill 0)
(q/text-size 24)
(render-list (extract-list (state :output-layer)) 50)
(let [sub-delta-row (->> state :output-layer extract-mouse-pressed-monitor)]
(q/text (str (q/frame-count) " " sub-delta-row)
50 (mod (+ 25 (* 50 (q/frame-count))) (q/height)))))
(defn mouse-pressed [state event]
#_(async/>!! mouse-pressed-channel event)
(async/go (async/>! mouse-pressed-channel event))
state)
(q/defsketch quil-try
:title "A Quil-controlled DMM"
:size [800 600]
; setup function called only once, during sketch initialization.
:setup setup
; update-state is called on each iteration before draw-state.
:update update-state
:draw draw-state
:mouse-pressed mouse-pressed
:features [:keep-on-top]
; This sketch uses functional-mode middleware.
; Check quil wiki for more info about middlewares and particularly
; fun-mode.
:middleware [m/fun-mode])