rlm@18
|
1 (ns laser.rasterize
|
rlm@18
|
2 (:use [rlm
|
rlm@18
|
3 image-utils
|
rlm@18
|
4 map-utils]
|
rlm@18
|
5 [clojure.contrib
|
rlm@18
|
6 [str-utils :only [str-join re-gsub]]
|
rlm@18
|
7 [seq :only [indexed]]
|
rlm@18
|
8 [math]
|
rlm@18
|
9 [def]
|
rlm@18
|
10 ])
|
rlm@18
|
11 (:import [ij ImagePlus IJ]))
|
rlm@18
|
12
|
rlm@18
|
13 ;(import '(java.io File))
|
rlm@18
|
14 ;(import '(org.apache.commons.io FileUtils))
|
rlm@18
|
15 ;(import '(javax.imageio ImageIO) )
|
rlm@18
|
16
|
rlm@18
|
17
|
rlm@18
|
18 (set! *print-length* 20)
|
rlm@18
|
19 (def feed 120)
|
rlm@18
|
20 (def dpi [500, 500])
|
rlm@18
|
21
|
rlm@18
|
22 (def paramaters {:x-dpi 500 :y-dpi 500 :margin 0 :x-offset 0 :y-offset 0})
|
rlm@18
|
23
|
rlm@18
|
24 ;;; this process is divided into two tasks,
|
rlm@18
|
25 ;;; creating the raster g-code, which sweeps back and forth
|
rlm@18
|
26 ;;; and creating the gmask, which turns the laser on and off.
|
rlm@18
|
27
|
rlm@18
|
28
|
rlm@18
|
29 ;;; we'll be using frame-hashes, which represent picutres as
|
rlm@18
|
30 ;;; a 3D vector field over 2D space, with the vectors representing
|
rlm@18
|
31 ;;; the rgb values at that particular point.
|
rlm@18
|
32
|
rlm@18
|
33 (defn select-row
|
rlm@18
|
34 "returns a frame hash that is just a single line at the chosen y"
|
rlm@18
|
35 [y window]
|
rlm@18
|
36 (reduce
|
rlm@18
|
37 (fn [old-map number]
|
rlm@18
|
38 (let [pixel (get window [number y] nil)]
|
rlm@18
|
39 (if-not (nil? pixel)
|
rlm@18
|
40 (into old-map {[number y] pixel})
|
rlm@18
|
41 old-map)))
|
rlm@18
|
42 {}
|
rlm@18
|
43 (range (width window))))
|
rlm@18
|
44
|
rlm@18
|
45 (defn make-rows [pic]
|
rlm@18
|
46 (map (partial sort #(< (first %1) (first %2)))
|
rlm@18
|
47 (partition-by last
|
rlm@18
|
48 (sort (fn [[x1 y1][x2 y2]] (> y2 y1))
|
rlm@18
|
49 (map first (filter-vals (partial = black) pic))))))
|
rlm@18
|
50
|
rlm@18
|
51 ;;; generate rastering g-code
|
rlm@18
|
52
|
rlm@18
|
53 (defn raster-preamble []
|
rlm@18
|
54 (str-join \newline ["M63 P0\nG61" (str "F" feed) "M101" "M3 S1\n"]))
|
rlm@18
|
55
|
rlm@18
|
56 (defn raster-epilogue []
|
rlm@18
|
57 (str-join \newline ["M63 P0" "M5" "M2\n"]))
|
rlm@18
|
58
|
rlm@18
|
59 (defn raster-comment
|
rlm@18
|
60 "wrap a statement in PARENTHENSIS to make it a comment in gcode.
|
rlm@18
|
61 parenthesis themselves aren't allowed in comments.
|
rlm@18
|
62 Oh the humanity!!"
|
rlm@18
|
63 [string]
|
rlm@18
|
64 (str "(" (re-gsub #"[()]" "" string) ")"))
|
rlm@18
|
65
|
rlm@18
|
66 (defn rows
|
rlm@18
|
67 "creates a sequence of one dimensional vector fields which
|
rlm@18
|
68 represent the rows of a picture"
|
rlm@18
|
69 [pic]
|
rlm@18
|
70 (let [non-empty-rows (apply sorted-set (map (comp last first) pic))]
|
rlm@18
|
71 (pmap (fn [n] (select-row n pic)) non-empty-rows)))
|
rlm@18
|
72
|
rlm@18
|
73
|
rlm@18
|
74
|
rlm@18
|
75
|
rlm@18
|
76
|
rlm@18
|
77 (defn row->gcode [{:keys [x-dpi y-dpi margin x-offset y-offset]} row]
|
rlm@18
|
78 (let [pixels (keys row)
|
rlm@18
|
79 x2 0
|
rlm@18
|
80 [_ y2] (first pixels)
|
rlm@18
|
81 [_ y1] (first pixels)
|
rlm@18
|
82 x1 533]
|
rlm@18
|
83
|
rlm@18
|
84 ;(let [ordered-row
|
rlm@18
|
85 ; (sort-map-by (fn [[x1 _] [x2 _]] (> x2 x1)) row)]
|
rlm@18
|
86
|
rlm@18
|
87 (let [;[x1 y1] (last (keys ordered-row))
|
rlm@18
|
88 ;[x2 y2] (first (keys ordered-row))
|
rlm@18
|
89 [x1 y1 x2 y2] (if (odd? y1) [x2 y2 x1 y1] [x1 y1 x2 y2])]
|
rlm@18
|
90
|
rlm@18
|
91 (str (format "G0 X%.3f Y%.3f\n"
|
rlm@18
|
92 (float (* x1 (/ x-dpi)))
|
rlm@18
|
93 (float (* y1 (/ y-dpi))))
|
rlm@18
|
94
|
rlm@18
|
95 (format "G1 X%.3f Y%.3f\n"
|
rlm@18
|
96 (float (* x2 (/ x-dpi)))
|
rlm@18
|
97 (float (* y2 (/ y-dpi))))))))
|
rlm@18
|
98
|
rlm@18
|
99
|
rlm@18
|
100 (defn pic->gcode [paramaters pic]
|
rlm@18
|
101 (reduce (fn [gcode current-height]
|
rlm@18
|
102 (let [current-row (select-row current-height pic)]
|
rlm@18
|
103 (if-not (empty? current-row)
|
rlm@18
|
104 (let [new-code (row->gcode paramaters current-row)]
|
rlm@18
|
105 (println new-code)
|
rlm@18
|
106 (str gcode new-code))
|
rlm@18
|
107 gcode)))
|
rlm@18
|
108 ""
|
rlm@18
|
109 (range (height pic))))
|
rlm@18
|
110
|
rlm@18
|
111
|
rlm@18
|
112
|
rlm@18
|
113
|
rlm@18
|
114 (defn pic->gcode
|
rlm@18
|
115
|
rlm@18
|
116
|
rlm@18
|
117
|
rlm@18
|
118 ;(defn pic->gcode [paramaters pic]
|
rlm@18
|
119
|
rlm@18
|
120
|
rlm@18
|
121 (defn generate-gcode [pic]
|
rlm@18
|
122 (str (raster-preamble)
|
rlm@18
|
123 (row->gcode paramaters pic)
|
rlm@18
|
124 (raster-epilogue)))
|
rlm@18
|
125
|
rlm@18
|
126
|
rlm@18
|
127
|
rlm@18
|
128
|
rlm@18
|
129
|
rlm@18
|
130
|
rlm@18
|
131
|
rlm@18
|
132
|
rlm@18
|
133
|
rlm@18
|
134
|
rlm@18
|
135
|
rlm@18
|
136
|
rlm@18
|
137
|
rlm@18
|
138
|
rlm@18
|
139
|
rlm@18
|
140
|
rlm@18
|
141
|
rlm@18
|
142
|
rlm@18
|
143
|
rlm@18
|
144 (defn gather-row [row]
|
rlm@18
|
145 (let [base [[(first (first row)) (first (first row))]]]
|
rlm@18
|
146 ; (println base)
|
rlm@18
|
147 (reduce
|
rlm@18
|
148 (fn colapse [collection new-n]
|
rlm@18
|
149
|
rlm@18
|
150 (let [collection (apply vector collection)
|
rlm@18
|
151 prevoius (last (last collection))
|
rlm@18
|
152 range-start (first (last collection))]
|
rlm@18
|
153 ; (println new-n)
|
rlm@18
|
154 ; (println prevoius)
|
rlm@18
|
155 ; (println range-start)
|
rlm@18
|
156 (if (<= new-n (+ prevoius 1))
|
rlm@18
|
157 (do ;(println "join")
|
rlm@18
|
158 ;(println (butlast collection))
|
rlm@18
|
159 (conj (apply vector (butlast collection))
|
rlm@18
|
160 (vector range-start new-n)))
|
rlm@18
|
161 (conj collection (vector new-n new-n)))))
|
rlm@18
|
162
|
rlm@18
|
163 base
|
rlm@18
|
164 (map first row))))
|
rlm@18
|
165
|
rlm@18
|
166
|
rlm@18
|
167
|
rlm@18
|
168
|
rlm@18
|
169 (defn row->gmask [[x-dpi y-dpi] forward? row]
|
rlm@18
|
170 (let [start (float (* (/ x-dpi) (first (first
|
rlm@18
|
171 (if forward?
|
rlm@18
|
172 (reverse row) row)))))]
|
rlm@18
|
173 (let [preamble (if-not forward?
|
rlm@18
|
174 (format "0 0 0 %.3f\n" start)
|
rlm@18
|
175 (format "0 0 1 %.3f\n" start))
|
rlm@18
|
176 body
|
rlm@18
|
177 (for [[x y]
|
rlm@18
|
178 (if forward?
|
rlm@18
|
179 (reverse (gather-row row))
|
rlm@18
|
180 (gather-row row))]
|
rlm@18
|
181 (let [x (float (* x (/ x-dpi)))
|
rlm@18
|
182 y (float (* y (/ x-dpi)))]
|
rlm@18
|
183 ;; x (+ x 0.159)];; shift by a small margin.
|
rlm@18
|
184 (if-not forward?
|
rlm@18
|
185 (str (format "0 0 1 %.3f\n" x)
|
rlm@18
|
186 (format "0 1 1 %.3f\n" y))
|
rlm@18
|
187
|
rlm@18
|
188 (str (format "0 0 0 %.3f\n" y)
|
rlm@18
|
189 (format "0 1 0 %.3f\n" x)))))]
|
rlm@18
|
190
|
rlm@18
|
191 (str preamble (str-join "" body)))))
|
rlm@18
|
192
|
rlm@18
|
193
|
rlm@18
|
194 (defn generate-gmask [pic]
|
rlm@18
|
195 (str "1 0 0 0\n"
|
rlm@18
|
196 (str-join "" (map (fn [[index row]]
|
rlm@18
|
197 (row->gmask dpi (even? index) row))
|
rlm@18
|
198 (indexed (make-rows pic))))))
|
rlm@18
|
199
|
rlm@18
|
200
|
rlm@18
|
201
|
rlm@18
|
202
|
rlm@18
|
203 ;;;; testing
|
rlm@18
|
204
|
rlm@18
|
205 (defn generate-files [pic]
|
rlm@18
|
206 (println "made-image")
|
rlm@18
|
207 (spit "/home/r/kevin/out.ngc" (generate-gcode pic))
|
rlm@18
|
208 (println "/home/r/kevin/out.ngc")
|
rlm@18
|
209 (spit "/home/r/kevin/out.gmask" (generate-gmask pic))
|
rlm@18
|
210 (println "/home/r/kevin/out.gmask")
|
rlm@18
|
211 pic)
|
rlm@18
|
212
|
rlm@18
|
213 (defn update-state []
|
rlm@18
|
214 (def sing "/home/r/kevin/sing.png")
|
rlm@18
|
215 (def pic (frame-hash (ImagePlus. sing)))
|
rlm@18
|
216 (def pic (b&w pic))
|
rlm@18
|
217 (def pic (filter-vals (partial = black) pic)))
|
rlm@18
|
218
|
rlm@18
|
219 (defn compare-gen-fn
|
rlm@18
|
220 ([n f cmp]
|
rlm@18
|
221 (let [theirs (re-split #"\n" (slurp cmp))
|
rlm@18
|
222 ours (re-split #"\n" (f pic))]
|
rlm@18
|
223 (println (format "%1$-25s%2$s" "OURS" "THEIRS"))
|
rlm@18
|
224 (println "_______________________________________")
|
rlm@18
|
225 (dorun (map (fn [[us them]] (println
|
rlm@18
|
226 (format "%1$-25s%2$s" us them)))
|
rlm@18
|
227 (take n (partition 2 (interleave ours theirs))))))))
|
rlm@18
|
228
|
rlm@18
|
229 (defn compare-gcode
|
rlm@18
|
230 ([] (compare-gcode 25))
|
rlm@18
|
231 ([n] (compare-gen-fn n generate-gcode "/home/r/kevin/reference.ngc")))
|
rlm@18
|
232
|
rlm@18
|
233 (defn compare-gmask
|
rlm@18
|
234 ([] compare-gmask 25)
|
rlm@18
|
235 ([n] (compare-gen-fn n generate-gmask "/home/r/kevin/reference.gmask")))
|
rlm@18
|
236
|
rlm@18
|
237
|
rlm@18
|
238
|
rlm@18
|
239
|
rlm@18
|
240
|