This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
operators.clj
279 lines (239 loc) · 8.29 KB
/
operators.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
;; Copyright (c) Zachary Tellman. All rights reserved.
;; The use and distribution terms for this software are covered by the
;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
;; which can be found in the file epl-v10.html at the root of this distribution.
;; By using this software in any fashion, you are agreeing to be bound by
;; the terms of this license.
;; You must not remove this notice, or any other, from this software.
(ns penumbra.translate.operators
(:use [clojure.walk]
[penumbra.geometry]
[penumbra.translate core]
[penumbra.opengl.texture :only (texture?)]
[clojure.contrib.seq-utils :only (indexed group-by separate)]
[clojure.contrib.def :only (defn-memo defvar-)]
[penumbra.translate.core])
(:require [clojure.zip :as zip]))
;;
(def *dim-element* nil)
(def *typeof-dim* nil)
(def *typeof-element* nil)
(def *typeof-param* nil)
;;utilities
(defn first= [x y]
(and (seq? x) (= (first x) y)))
(defn int? [p]
(let [cls (class p)]
(if (or (= cls Integer) (= cls Integer/TYPE)) true false)))
(defn tag= [x t]
(and (meta? x) (= t (:tag (meta x)))))
(defn apply-transforms [funs tree]
(reduce #(tree-map %2 %1) tree funs))
(defn replace-with [from to]
#(if (= from %) to))
;;elements
(defn element? [s]
(or
(and (symbol? s) (re-find #"%$|%[0-9]+" (name s)))
(and (seq? s) (< 1 (count s)) (element? (first s)))))
(defn-memo create-element [index]
(symbol (str "%" (inc index))))
(defn element-index [x]
(let [x (if (sequential? x) (first x) x)]
(if (= '% x)
0
(dec (Integer/parseInt (.substring (name x) 1))))))
(defn- process-elements
[coll]
(vec
(map
#(if (vector? %)
(add-meta (first %) :persist true)
%)
coll)))
(defn apply-element-transform [f x]
(merge-meta
x
(cond
(element? x) (or (f x) x)
(sequential? x) (walk #(apply-element-transform f %) identity x)
:else x)))
;;results
(defn- result?
"This assumes you only traverse down the last element of the tree"
[x]
(or
(vector? x)
(not (sequential? x))
(let [frst (first x)]
(and
(not (and (element? frst) (= 1 (count x))))
(not (or (sequential? frst) (#{'do 'scope} frst)))))))
(defn results [x]
(if (result? x)
(if (vector? x) x (list x))
(results (last x))))
(defn transform-results [f x]
(loop [z (zip/seq-zip x)]
(if (result? (zip/node z))
(zip/root (zip/replace z (f (results x))))
(recur (-> z zip/down zip/rightmost)))))
;;general program transformation
(defn param-dispatch [t]
(cond
(and (vector? t) (number? (first t))) :dim
(number? t) :dim
(or (texture? t) (vector? t)) :elements
(map? t) :params
(symbol? t) :symbol
(keyword? t) :keyword
:else (throw (Exception. (str "Don't recognize " (with-out-str (println t)))))))
(defn group-elements [params]
(concat
(filter #(not= :elements (param-dispatch %)) params)
(list (vec (filter #(= :elements (param-dispatch %)) params)))))
(defn process-operator [x params elements]
(->> x
(apply-transforms ;;tag elements and params
(concat
(map (fn [[k v]]
(let [x (-> k name symbol)]
(replace-with x (add-meta x :tag (*typeof-param* v)))))
params)
(map (fn [[idx e]]
(let [type (*typeof-element* e)]
(fn [x]
(when (and (element? x) (= idx (element-index x)))
(add-meta x :tag type)))))
(indexed elements))))
transform-expr))
;;special map operators
(defvar- element-convolution-expr
'(let [-half-dim (/ (dim :element) 2.0)
-start (max (float2 0.0) (- :coord (floor -half-dim)))
-end (min :dim (+ :coord (ceil -half-dim)))]
(for [(<- i (.x -start)) (< i (.x -end)) (+= i 1.0)]
(for [(<- j (.y -start)) (< j (.y -end)) (+= j 1.0)]
(let [-location (float2 i j)
-offset (- -location :coord)
-lookup (:element (+ -offset -half-dim))]
:body)))))
(defvar- radius-convolution-expr
'(let [-radius (float2 (float :radius))
-start (max (float2 0.0) (float2 (- :coord -radius)))
-end (min :dim (+ :coord -radius (float2 1.0)))]
(for [(<- i (.x -start)) (< i (.x -end)) (+= i 1.0)]
(for [(<- j (.y -start)) (< j (.y -end)) (+= j 1.0)]
(let [-location (float2 i j)
-offset (- -location :coord)]
:body)))))
(defmulti transform-convolution #(param-dispatch (second %)))
(defmethod transform-convolution :symbol [[_ element & body]]
(let [body (apply-transforms
(list
(replace-with :offset '-offset)
#(when (and (element? %) (symbol? %))
(if (= element %)
'-lookup
(list % '-location))))
body)]
(apply-transforms
(list
#(if (= :element %) element)
#(if (= :body %) body))
element-convolution-expr)))
(defmethod transform-convolution :dim [[_ radius & body]]
(let [body (apply-transforms
(list
(replace-with :offset '-offset)
#(when (and (element? %) (symbol? %))
(list % '-location)))
body)]
(apply-transforms
(list
#(if (= :radius %) radius)
#(if (= :body %) body))
radius-convolution-expr)))
;;signature
(defmulti signature (fn [& args] (->> args group-elements (map param-dispatch) vec)))
(defmethod signature [:dim :elements] [dim]
(signature {} dim []))
(defmethod signature [:elements] [& elements]
(apply signature (list* {} elements)))
(defmethod signature [:params :elements] [params & elements]
(let [elements* (process-elements elements)]
(apply signature (list* params (*dim-element* (first elements*)) elements*))))
(defmethod signature [:params :dim :elements] [params dim & elements]
(let [elements (remove empty? elements)
dim (if (number? dim) (rectangle dim) dim)]
{:signature [(map *typeof-param* params) (map *typeof-element* elements)]
:params params
:elements elements
:dim dim}))
;;defmap
(defn process-map [program params dim elements]
(let [num-elements
(->> program
(tree-filter element?)
(map #(if (symbol? %) % (first %)))
distinct
count)]
(if (not= num-elements (count elements))
(throw (Exception. (str "Expected " num-elements ", received " (count elements))))))
;;verify elements are not nil
(doseq [[idx e] (indexed elements)]
(if (nil? e)
(throw (Exception. (str "Element at position " idx " is nil")))))
(let [dim
(if (number? dim) (rectangle dim) dim)
program*
(let [program
(apply-transforms
(list
#(when (first= % 'convolve) (transform-convolution %))
#(when (first= % 'dim) (add-meta % :tag (*typeof-dim* (-> % last element-index)))))
program)]
(process-operator program params elements))
program-results
(map typeof (results program*))]
{:program program*
:results program-results}))
;;defreduce
(defvar- reduce-program
'(let [-source-coord (* (floor :coord) 2.0)
-x (> (.x -bounds) (.x -source-coord))
-y (> (.y -bounds) (.y -source-coord))]
(<- -a (% -source-coord))
(if -x
(let [-b (% (+ -source-coord (float2 1.0 0.0)))
-c -a]
:expr))
(if -y
(let [-b (% (+ -source-coord (float2 0.0 1.0)))
-c -a]
:expr))
(if (and -x -y)
(let [-b (% (+ -source-coord (float2 1.0 1.0)))
-c -a]
:expr))
-a))
(defn process-reduce [program params dim elements]
(let [program*
(let [expr (apply-transforms
(list
(replace-with '%1 '-b)
(replace-with '% '-b)
(replace-with '%2 '-c))
program)]
(process-operator
(tree-map
(fn [x]
(when (= :expr x)
(transform-results #(list '<- '-a %) expr)))
reduce-program)
params
elements))
result*
(typeof (results program))]
{:program program*
:result result*}))