view src/laser/rasterize.clj @ 19:3b255dcd6c50

getting closer
author Robert McIntyre <rlm@mit.edu>
date Mon, 30 Aug 2010 00:09:06 -0400
parents 962e223bab0d
children 6bed8ceb51a9
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 re-split]]
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])
19 (def paramaters {:x-dpi 500 :y-dpi 500 :margin 0 :x-offset 1 :y-offset 1})
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 [{:keys [x-dpi y-dpi margin x-offset y-offset]} forward? row]
71 (let [[x1 y1] (if forward? (last row) (first row))
72 [x2 y2] (if forward? (first row) (last row))]
74 (let [x1 (* x1 (/ x-dpi))
75 x2 (* x2 (/ x-dpi))
76 y1 (* y1 (/ y-dpi))
77 y2 (* y2(/ y-dpi))]
79 (let [x1 (+ x1 x-offset)
80 x2 (+ x2 x-offset)
81 y1 (+ y1 y-offset)
82 y2 (+ y2 y-offset)]
85 (str (format "G0 X%.3f Y%.3f\n"
86 (float x1 )
87 (float y1))
89 (format "G1 X%.3f Y%.3f\n"
90 (float x2)
91 (float y2)))))))
93 (defn generate-gcode [pic]
94 (str (raster-preamble)
95 (str-join ""
96 (map
97 (fn [[index row]]
98 (row->gcode paramaters (even? index) row))
99 (indexed (make-rows pic))))
100 (raster-epilogue)))
120 (defn gather-row [row]
121 (let [base [[(first (first row)) (first (first row))]]]
122 ; (println base)
123 (reduce
124 (fn colapse [collection new-n]
126 (let [collection (apply vector collection)
127 prevoius (last (last collection))
128 range-start (first (last collection))]
129 ; (println new-n)
130 ; (println prevoius)
131 ; (println range-start)
132 (if (<= new-n (+ prevoius 1))
133 (do ;(println "join")
134 ;(println (butlast collection))
135 (conj (apply vector (butlast collection))
136 (vector range-start new-n)))
137 (conj collection (vector new-n new-n)))))
139 base
140 (map first row))))
145 (defn row->gmask [[x-dpi y-dpi] forward? row]
146 (let [start (float (* (/ x-dpi) (first (first
147 (if forward?
148 (reverse row) row)))))]
149 (let [preamble (if-not forward?
150 (format "0 0 0 %.3f\n" start)
151 (format "0 0 1 %.3f\n" start))
152 body
153 (for [[x y]
154 (if forward?
155 (reverse (gather-row row))
156 (gather-row row))]
157 (let [x (float (* x (/ x-dpi)))
158 y (float (* y (/ x-dpi)))]
159 ;; x (+ x 0.159)];; shift by a small margin.
160 (if-not forward?
161 (str (format "0 0 1 %.3f\n" x)
162 (format "0 1 1 %.3f\n" y))
164 (str (format "0 0 0 %.3f\n" y)
165 (format "0 1 0 %.3f\n" x)))))]
167 (str preamble (str-join "" body)))))
170 (defn generate-gmask [pic]
171 (str "1 0 0 0\n"
172 (str-join "" (map (fn [[index row]]
173 (row->gmask dpi (even? index) row))
174 (indexed (make-rows pic))))))
179 ;;;; testing
181 (defn generate-files [pic]
182 (println "made-image")
183 (spit "/home/r/kevin/out.ngc" (generate-gcode pic))
184 (println "/home/r/kevin/out.ngc")
185 (spit "/home/r/kevin/out.gmask" (generate-gmask pic))
186 (println "/home/r/kevin/out.gmask")
187 pic)
189 (defn update-state []
190 (def sing "/home/r/kevin/sing.png")
191 (def pic (frame-hash (rotate 180 (ImagePlus. sing))))
192 (def pic (b&w pic)))
194 (defn compare-gen-fn [n f cmp]
195 (let [theirs (re-split #"\n" (slurp cmp))
196 ours (re-split #"\n" (f pic))]
197 (println (format "%1$-25s%2$s" "OURS" "THEIRS"))
198 (println "_______________________________________")
199 (dorun (map (fn [[us them]] (println
200 (format "%1$-25s%2$s" us them)))
201 (take n (partition 2 (interleave ours theirs)))))))
203 (defn compare-gcode [n]
204 (compare-gen-fn n generate-gcode "/home/r/kevin/reference.ngc"))
205 (defn compare-gmask [n]
206 (compare-gen-fn n generate-gmask "/home/r/kevin/reference.gmask"))