Mercurial > lasercutter
changeset 19:3b255dcd6c50
getting closer
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Mon, 30 Aug 2010 00:09:06 -0400 |
parents | a769347618a1 |
children | 6bed8ceb51a9 |
files | graster/graster/.git/index src/laser/rasterize.clj |
diffstat | 2 files changed, 56 insertions(+), 85 deletions(-) [+] |
line wrap: on
line diff
1.1 Binary file graster/graster/.git/index has changed
2.1 --- a/src/laser/rasterize.clj Sun Aug 29 23:44:59 2010 -0400 2.2 +++ b/src/laser/rasterize.clj Mon Aug 30 00:09:06 2010 -0400 2.3 @@ -3,11 +3,9 @@ 2.4 image-utils 2.5 map-utils] 2.6 [clojure.contrib 2.7 - [str-utils :only [str-join re-gsub]] 2.8 + [str-utils :only [str-join re-gsub re-split]] 2.9 [seq :only [indexed]] 2.10 - [math] 2.11 - [def] 2.12 - ]) 2.13 + [math]]) 2.14 (:import [ij ImagePlus IJ])) 2.15 2.16 ;(import '(java.io File)) 2.17 @@ -18,8 +16,7 @@ 2.18 (set! *print-length* 20) 2.19 (def feed 120) 2.20 (def dpi [500, 500]) 2.21 - 2.22 -(def paramaters {:x-dpi 500 :y-dpi 500 :margin 0 :x-offset 0 :y-offset 0}) 2.23 +(def paramaters {:x-dpi 500 :y-dpi 500 :margin 0 :x-offset 1 :y-offset 1}) 2.24 2.25 ;;; this process is divided into two tasks, 2.26 ;;; creating the raster g-code, which sweeps back and forth 2.27 @@ -28,19 +25,12 @@ 2.28 2.29 ;;; we'll be using frame-hashes, which represent picutres as 2.30 ;;; a 3D vector field over 2D space, with the vectors representing 2.31 -;;; the rgb values at that particular point. 2.32 +;;; the rgb values at that particulat point. 2.33 2.34 (defn select-row 2.35 "returns a frame hash that is just a single line at the chosen y" 2.36 [y window] 2.37 - (reduce 2.38 - (fn [old-map number] 2.39 - (let [pixel (get window [number y] nil)] 2.40 - (if-not (nil? pixel) 2.41 - (into old-map {[number y] pixel}) 2.42 - old-map))) 2.43 - {} 2.44 - (range (width window)))) 2.45 + (filter-keys (comp (partial = y) last) window)) 2.46 2.47 (defn make-rows [pic] 2.48 (map (partial sort #(< (first %1) (first %2))) 2.49 @@ -48,80 +38,66 @@ 2.50 (sort (fn [[x1 y1][x2 y2]] (> y2 y1)) 2.51 (map first (filter-vals (partial = black) pic)))))) 2.52 2.53 + 2.54 ;;; generate rastering g-code 2.55 2.56 (defn raster-preamble [] 2.57 - (str-join \newline ["M63 P0\nG61" (str "F" feed) "M101" "M3 S1\n"])) 2.58 + (str-join \newline 2.59 + ["M63 P0\nG61" 2.60 + (str "F" feed) 2.61 + "M101" 2.62 + "M3 S1\n"])) 2.63 2.64 (defn raster-epilogue [] 2.65 - (str-join \newline ["M63 P0" "M5" "M2\n"])) 2.66 + (str-join \newline 2.67 + ["M63 P0" 2.68 + "M5" 2.69 + "M2\n"])) 2.70 2.71 -(defn raster-comment 2.72 - "wrap a statement in PARENTHENSIS to make it a comment in gcode. 2.73 - parenthesis themselves aren't allowed in comments. 2.74 - Oh the humanity!!" 2.75 - [string] 2.76 +(defn raster-comment [string] 2.77 (str "(" (re-gsub #"[()]" "" string) ")")) 2.78 2.79 -(defn rows 2.80 - "creates a sequence of one dimensional vector fields which 2.81 - represent the rows of a picture" 2.82 - [pic] 2.83 - (let [non-empty-rows (apply sorted-set (map (comp last first) pic))] 2.84 - (pmap (fn [n] (select-row n pic)) non-empty-rows))) 2.85 - 2.86 + 2.87 +;this is a sequence of rows 2.88 + 2.89 +;(defn span [row] 2.90 +; (let [sorted-row (sort #(< (first %1) (first %2)) row)] 2.91 +; (vector (first sorted-row) (last sorted-row)))) 2.92 + 2.93 + 2.94 + 2.95 +(defn row->gcode [{:keys [x-dpi y-dpi margin x-offset y-offset]} forward? row] 2.96 2.97 + (let [[x1 y1] (if forward? (last row) (first row)) 2.98 + [x2 y2] (if forward? (first row) (last row))] 2.99 2.100 + (let [x1 (* x1 (/ x-dpi)) 2.101 + x2 (* x2 (/ x-dpi)) 2.102 + y1 (* y1 (/ y-dpi)) 2.103 + y2 (* y2(/ y-dpi))] 2.104 2.105 - 2.106 -(defn row->gcode [{:keys [x-dpi y-dpi margin x-offset y-offset]} row] 2.107 - (let [pixels (keys row) 2.108 - x2 0 2.109 - [_ y2] (first pixels) 2.110 - [_ y1] (first pixels) 2.111 - x1 533] 2.112 - 2.113 - ;(let [ordered-row 2.114 -; (sort-map-by (fn [[x1 _] [x2 _]] (> x2 x1)) row)] 2.115 - 2.116 - (let [;[x1 y1] (last (keys ordered-row)) 2.117 - ;[x2 y2] (first (keys ordered-row)) 2.118 - [x1 y1 x2 y2] (if (odd? y1) [x2 y2 x1 y1] [x1 y1 x2 y2])] 2.119 - 2.120 + (let [x1 (+ x1 x-offset) 2.121 + x2 (+ x2 x-offset) 2.122 + y1 (+ y1 y-offset) 2.123 + y2 (+ y2 y-offset)] 2.124 + 2.125 + 2.126 (str (format "G0 X%.3f Y%.3f\n" 2.127 - (float (* x1 (/ x-dpi))) 2.128 - (float (* y1 (/ y-dpi)))) 2.129 + (float x1 ) 2.130 + (float y1)) 2.131 2.132 (format "G1 X%.3f Y%.3f\n" 2.133 - (float (* x2 (/ x-dpi))) 2.134 - (float (* y2 (/ y-dpi)))))))) 2.135 - 2.136 - 2.137 -(defn pic->gcode [paramaters pic] 2.138 - (reduce (fn [gcode current-height] 2.139 - (let [current-row (select-row current-height pic)] 2.140 - (if-not (empty? current-row) 2.141 - (let [new-code (row->gcode paramaters current-row)] 2.142 - (println new-code) 2.143 - (str gcode new-code)) 2.144 - gcode))) 2.145 - "" 2.146 - (range (height pic)))) 2.147 - 2.148 - 2.149 - 2.150 - 2.151 -(defn pic->gcode 2.152 - 2.153 - 2.154 - 2.155 -;(defn pic->gcode [paramaters pic] 2.156 + (float x2) 2.157 + (float y2))))))) 2.158 2.159 - 2.160 (defn generate-gcode [pic] 2.161 (str (raster-preamble) 2.162 - (row->gcode paramaters pic) 2.163 - (raster-epilogue))) 2.164 + (str-join "" 2.165 + (map 2.166 + (fn [[index row]] 2.167 + (row->gcode paramaters (even? index) row)) 2.168 + (indexed (make-rows pic)))) 2.169 + (raster-epilogue))) 2.170 2.171 2.172 2.173 @@ -212,27 +188,22 @@ 2.174 2.175 (defn update-state [] 2.176 (def sing "/home/r/kevin/sing.png") 2.177 - (def pic (frame-hash (ImagePlus. sing))) 2.178 - (def pic (b&w pic)) 2.179 - (def pic (filter-vals (partial = black) pic))) 2.180 + (def pic (frame-hash (rotate 180 (ImagePlus. sing)))) 2.181 + (def pic (b&w pic))) 2.182 2.183 -(defn compare-gen-fn 2.184 - ([n f cmp] 2.185 +(defn compare-gen-fn [n f cmp] 2.186 (let [theirs (re-split #"\n" (slurp cmp)) 2.187 ours (re-split #"\n" (f pic))] 2.188 (println (format "%1$-25s%2$s" "OURS" "THEIRS")) 2.189 (println "_______________________________________") 2.190 (dorun (map (fn [[us them]] (println 2.191 (format "%1$-25s%2$s" us them))) 2.192 - (take n (partition 2 (interleave ours theirs)))))))) 2.193 + (take n (partition 2 (interleave ours theirs))))))) 2.194 2.195 -(defn compare-gcode 2.196 - ([] (compare-gcode 25)) 2.197 - ([n] (compare-gen-fn n generate-gcode "/home/r/kevin/reference.ngc"))) 2.198 - 2.199 -(defn compare-gmask 2.200 - ([] compare-gmask 25) 2.201 - ([n] (compare-gen-fn n generate-gmask "/home/r/kevin/reference.gmask"))) 2.202 +(defn compare-gcode [n] 2.203 + (compare-gen-fn n generate-gcode "/home/r/kevin/reference.ngc")) 2.204 +(defn compare-gmask [n] 2.205 + (compare-gen-fn n generate-gmask "/home/r/kevin/reference.gmask")) 2.206 2.207 2.208