comparison src/laser/rasterize.clj @ 7:5e167f275a93

moving to rt, hopefully I can do stuff now
author Robert McIntyre <rlm@mit.edu>
date Fri, 20 Aug 2010 06:53:41 -0400
parents 4ae2497acf7d
children 0f48db8d2a05
comparison
equal deleted inserted replaced
6:4ae2497acf7d 7:5e167f275a93
170 170
171 171
172 172
173 173
174 174
175 ;this is a sequence of rows
176
177 ;(defn span [row]
178 ; (let [sorted-row (sort #(< (first %1) (first %2)) row)]
179 ; (vector (first sorted-row) (last sorted-row))))
180
181
182 (defn row->gcode [[x-dpi y-dpi] row]
183 (let [[x1 y1] (first row)
184 [x2 y2] (last row)]
185 (str (format "GO X%.3f Y%.3f\n"
186 (float (* x1 (/ x-dpi)))
187 (float (* y1 (/ y-dpi))))
188
189 (format "G1 X%.3f Y%.3f\n"
190 (float (* x2 (/ x-dpi)))
191 (float (* y2 (/ y-dpi)))))))
192
193 (defn gather-row [row]
194 (let [base [[(first (first row)) (first (first row))]]]
195 ; (println base)
196 (reduce
197 (fn colapse [collection new-n]
198
199 (let [collection (apply vector collection)
200 prevoius (last (last collection))
201 range-start (first (last collection))]
202 ; (println new-n)
203 ; (println prevoius)
204 ; (println range-start)
205 (if (<= new-n (+ prevoius 1))
206 (do ;(println "join")
207 ;(println (butlast collection))
208 (conj (apply vector (butlast collection)) (vector range-start new-n)))
209 (conj collection (vector new-n new-n)))))
210
211 base
212 (map first row))))
213
214
215
216 (defn row->gmask [[x-dpi y-dpi] forward? row]
217
218 (let [start (float (* (/ x-dpi) (first (first row))))]
219
220 (let [preamble (if forward?
221 (str
222 (format "0 0 0 %.3f\n" start)
223 (format "0 0 1 %.3f\n" start)))
224 body
225 (for [[x y] (gather-row row)]
226 (let [x (float (* x (/ x-dpi)))
227 y (float (* y (/ x-dpi)))]
228 (if forward?
229 (str (format "0 0 1 %.3f\n" x)
230 (format "0 1 1 %.3f\n" y))
231
232 (str (format "0 0 0 %.3f\n" x)
233 (format "0 1 0 %.3f\n" y)))))]
234
235 (str preamble (str-join "" body)))))
236
237
238
239 (defn make-rows [pic]
240
241 (map (partial sort #(< (first %1) (first %2)))
242 (partition-by last
243 (sort (fn [[x1 y1][x2 y2]] (> y2 y1))
244 (map first (filter-vals (partial = black) pic))))))
245
246
247
248
249 ;sequence of numbers indicating width
250 (def mega
251
252 ;sequence of rows
253
254 (map (partial sort #(< (first %1) (first %2)))
255 (partition-by last
256 (sort (fn [[x1 y1][x2 y2]] (> y2 y1))
257 (map first (filter-vals (partial = black) pic))))))
258
259
260
261
262 (defn generate-gmask [pic]
263
264 (str "1 0 0 0\n"
265 (str-join "" (map (fn [[index row]] (row->gmask dpi (even? index) row)) (indexed (make-rows pic))))))
266
267
268 (defn generate-gcode [pic]
269 (str-join "" (map (partial row->gcode dpi) (make-rows pic))))
270
271
272