view src/laser/rasterize.clj @ 15:8ad629298649

major refactoring
author Robert McIntyre <rlm@mit.edu>
date Sun, 29 Aug 2010 19:02:26 -0400
parents db745c95aabd
children 52f544d05414
line wrap: on
line source
1 (ns laser.rasterize
2 (:use [rlm
3 image-utils
4 map-utils]
5 [clojure.contrib
6 [str-utils :only [str-join re-gsub]]
7 [seq :only [indexed]]
8 [math]])
9 (:import [ij ImagePlus IJ]))
11 ;(import '(java.io File))
12 ;(import '(org.apache.commons.io FileUtils))
13 ;(import '(javax.imageio ImageIO) )
16 (set! *print-length* 20)
17 (def feed 120)
18 (def dpi [500, 500])
21 ;;; this process is divided into two tasks,
22 ;;; creating the raster g-code, which sweeps back and forth
23 ;;; and creating the gmask, which turns the laser on and off.
26 ;;; we'll be using frame-hashes, which represent picutres as
27 ;;; a 3D vector field over 2D space, with the vectors representing
28 ;;; the rgb values at that particulat point.
30 (defn select-row
31 "returns a frame hash that is just a single line at the chosen y"
32 [y window]
33 (filter-keys (comp (partial = y) last) window))
35 (defn make-rows [pic]
36 (map (partial sort #(< (first %1) (first %2)))
37 (partition-by last
38 (sort (fn [[x1 y1][x2 y2]] (> y2 y1))
39 (map first (filter-vals (partial = black) pic))))))
42 ;;; generate rastering g-code
44 (defn raster-preamble []
45 (str-join \newline
46 ["M63 P0\nG61"
47 (str "F" feed)
48 "M101"
49 "M3 S1\n"]))
51 (defn raster-epilogue []
52 (str-join \newline
53 ["M63 P0"
54 "M5"
55 "M2\n"]))
57 (defn raster-comment [string]
58 (str "(" (re-gsub #"[()]" "" string) ")"))
61 ;this is a sequence of rows
63 ;(defn span [row]
64 ; (let [sorted-row (sort #(< (first %1) (first %2)) row)]
65 ; (vector (first sorted-row) (last sorted-row))))
69 (defn row->gcode [[x-dpi y-dpi] forward? row]
71 (let [[x1 y1] (if forward? (last row) (first row))
72 [x2 y2] (if forward? (first row) (last row))]
75 ; (let [[x1 y1] (first row)
76 ; [x2 y2] (last row)
77 ; x2 (+ x2 (* x-dpi 0.318))]
80 ; (println x2)
81 (str (format "G0 X%.3f Y%.3f\n"
82 (float (* x1 (/ x-dpi)))
83 (float (* y1 (/ y-dpi))))
85 (format "G1 X%.3f Y%.3f\n"
86 (float (* x2 (/ x-dpi)))
87 (float (* y2 (/ y-dpi)))))))
90 (defn generate-gcode [pic]
91 (str (raster-preamble)
92 (str-join ""
93 (map
94 (fn [[index row]]
95 (row->gcode dpi (even? index) row))
96 (indexed (make-rows pic))))
97 (raster-epilogue)))
117 (defn gather-row [row]
118 (let [base [[(first (first row)) (first (first row))]]]
119 ; (println base)
120 (reduce
121 (fn colapse [collection new-n]
123 (let [collection (apply vector collection)
124 prevoius (last (last collection))
125 range-start (first (last collection))]
126 ; (println new-n)
127 ; (println prevoius)
128 ; (println range-start)
129 (if (<= new-n (+ prevoius 1))
130 (do ;(println "join")
131 ;(println (butlast collection))
132 (conj (apply vector (butlast collection))
133 (vector range-start new-n)))
134 (conj collection (vector new-n new-n)))))
136 base
137 (map first row))))
142 (defn row->gmask [[x-dpi y-dpi] forward? row]
143 (let [start (float (* (/ x-dpi) (first (first
144 (if forward?
145 (reverse row) row)))))]
146 (let [preamble (if-not forward?
147 (format "0 0 0 %.3f\n" start)
148 (format "0 0 1 %.3f\n" start))
149 body
150 (for [[x y]
151 (if forward?
152 (reverse (gather-row row))
153 (gather-row row))]
154 (let [x (float (* x (/ x-dpi)))
155 y (float (* y (/ x-dpi)))]
156 ;; x (+ x 0.159)];; shift by a small margin.
157 (if-not forward?
158 (str (format "0 0 1 %.3f\n" x)
159 (format "0 1 1 %.3f\n" y))
161 (str (format "0 0 0 %.3f\n" y)
162 (format "0 1 0 %.3f\n" x)))))]
164 (str preamble (str-join "" body)))))
167 (defn generate-gmask [pic]
168 (str "1 0 0 0\n"
169 (str-join "" (map (fn [[index row]]
170 (row->gmask dpi (even? index) row))
171 (indexed (make-rows pic))))))
176 ;;;; testing
178 (defn generate-files [pic]
179 (println "made-image")
180 (spit "/home/r/kevin/out.ngc" (generate-gcode pic))
181 (println "/home/r/kevin/out.ngc")
182 (spit "/home/r/kevin/out.gmask" (generate-gmask pic))
183 (println "/home/r/kevin/out.gmask")
184 pic)
186 (defn update-state []
187 (def sing "/home/r/kevin/sing.png")
188 (def pic (frame-hash (ImagePlus. sing)))
189 (def pic (b&w pic)))
191 (defn compare-gen-fn [n f cmp]
192 (let [theirs (re-split #"\n" (slurp cmp))
193 ours (re-split #"\n" (f pic))]
194 (println (format "%1$-25s%2$s" "OURS" "THEIRS"))
195 (println "_______________________________________")
196 (dorun (map (fn [[us them]] (println
197 (format "%1$-25s%2$s" us them)))
198 (take n (partition 2 (interleave ours theirs)))))))
200 (defn compare-gcode [n]
201 (compare-gen-fn n generate-gcode "/home/r/kevin/reference.ngc"))
202 (defn compare-gmask [n]
203 (compare-gen-fn n generate-gmask "/home/r/kevin/reference.gmask"))