changeset 16:52f544d05414

I think I've made it worse....
author Robert McIntyre <rlm@mit.edu>
date Sun, 29 Aug 2010 23:40:39 -0400
parents 8ad629298649
children 962e223bab0d
files src/laser/rasterize.clj
diffstat 1 files changed, 76 insertions(+), 50 deletions(-) [+]
line wrap: on
line diff
     1.1 --- a/src/laser/rasterize.clj	Sun Aug 29 19:02:26 2010 -0400
     1.2 +++ b/src/laser/rasterize.clj	Sun Aug 29 23:40:39 2010 -0400
     1.3 @@ -5,7 +5,9 @@
     1.4  	[clojure.contrib
     1.5  	 [str-utils :only [str-join re-gsub]]
     1.6  	 [seq :only [indexed]]
     1.7 -	 [math]])
     1.8 +	 [math]
     1.9 +	 [def]
    1.10 +	 ])
    1.11    (:import [ij ImagePlus IJ]))
    1.12  
    1.13  ;(import '(java.io File))
    1.14 @@ -17,6 +19,7 @@
    1.15  (def feed 120)
    1.16  (def  dpi [500, 500])
    1.17  
    1.18 +(def paramaters {:x-dpi 500 :y-dpi 500 :margin 0 :x-offset 0 :y-offset 0})
    1.19  
    1.20  ;;; this process is divided into two tasks,
    1.21  ;;; creating the raster g-code, which sweeps back and forth
    1.22 @@ -25,12 +28,19 @@
    1.23  
    1.24  ;;; we'll be using frame-hashes, which represent picutres as
    1.25  ;;; a 3D vector field over 2D space, with the vectors representing
    1.26 -;;; the rgb values at that particulat point. 
    1.27 +;;; the rgb values at that particular point. 
    1.28  
    1.29  (defn select-row
    1.30    "returns a frame hash that is just a single line at the chosen y"
    1.31    [y window]
    1.32 -  (filter-keys (comp (partial = y) last) window))
    1.33 +  (reduce  
    1.34 +   (fn [old-map number]
    1.35 +     (let [pixel (get window [number y] nil)]
    1.36 +       (if-not (nil? pixel)
    1.37 +	 (into old-map {[number y] pixel})
    1.38 +	 old-map)))
    1.39 +   {}
    1.40 +   (range (width window))))
    1.41  
    1.42  (defn make-rows [pic]
    1.43    (map (partial sort #(< (first %1) (first %2)))
    1.44 @@ -38,63 +48,74 @@
    1.45  		     (sort (fn [[x1 y1][x2 y2]] (> y2 y1))
    1.46  			   (map first (filter-vals (partial = black) pic))))))
    1.47  
    1.48 -
    1.49  ;;; generate rastering g-code
    1.50  
    1.51  (defn raster-preamble []
    1.52 -  (str-join \newline
    1.53 -	    ["M63 P0\nG61"
    1.54 -	    (str "F" feed)
    1.55 -	    "M101"
    1.56 -	    "M3 S1\n"]))
    1.57 +  (str-join \newline ["M63 P0\nG61" (str "F" feed) "M101" "M3 S1\n"]))
    1.58  
    1.59  (defn raster-epilogue []
    1.60 -  (str-join \newline
    1.61 -	    ["M63 P0"
    1.62 -	     "M5"
    1.63 -	     "M2\n"]))
    1.64 +  (str-join \newline ["M63 P0" "M5" "M2\n"]))
    1.65  
    1.66 -(defn raster-comment [string]
    1.67 +(defn raster-comment
    1.68 +  "wrap a statement in PARENTHENSIS to make it a comment in gcode.
    1.69 +   parenthesis themselves aren't allowed in comments.
    1.70 +   Oh the humanity!!"
    1.71 +  [string]
    1.72    (str "(" (re-gsub #"[()]" "" string) ")"))
    1.73  
    1.74 +(defn rows
    1.75 +  "creates a sequence of one dimensional vector fields which
    1.76 +   represent the rows of a picture"
    1.77 +  [pic]
    1.78 +  (let [non-empty-rows (apply sorted-set (map (comp last first) pic))]
    1.79 +    (pmap (fn [n] (select-row n pic)) non-empty-rows)))
    1.80 +    
    1.81 +  
    1.82  
    1.83 -;this is a sequence of rows
    1.84  
    1.85 -;(defn span [row]
    1.86 -;  (let [sorted-row (sort #(< (first %1) (first %2)) row)]
    1.87 -;    (vector (first sorted-row) (last sorted-row))))
    1.88  
    1.89 +(defn row->gcode [{:keys [x-dpi y-dpi margin x-offset y-offset]} row]
    1.90 +  (let [pixels (keys row)
    1.91 +	x2 0
    1.92 +	[_ y2] (first pixels)
    1.93 +	[_ y1] (first pixels)
    1.94 +	x1 533]
    1.95  
    1.96 +					;(let [ordered-row
    1.97 +;	(sort-map-by (fn [[x1 _] [x2 _]] (> x2 x1)) row)]
    1.98 +    
    1.99 +    (let [;[x1 y1] (last (keys ordered-row))
   1.100 +	  ;[x2 y2] (first (keys ordered-row))
   1.101 +	  [x1 y1 x2 y2] (if (odd? y1) [x2 y2 x1 y1] [x1 y1 x2 y2])]
   1.102 +      
   1.103 +      (str (format "G0 X%.3f Y%.3f\n"
   1.104 +		   (float (* x1 (/ x-dpi)))
   1.105 +		   (float (* y1 (/ y-dpi))))
   1.106 +	   
   1.107 +	   (format "G1 X%.3f Y%.3f\n"
   1.108 +		   (float (* x2 (/ x-dpi)))
   1.109 +		   (float (* y2 (/ y-dpi))))))))
   1.110  
   1.111 -(defn row->gcode [[x-dpi y-dpi] forward? row]
   1.112 +
   1.113 +(defn pic->gcode [paramaters pic]
   1.114 +  (reduce (fn [gcode current-height]
   1.115 +	    (let [current-row (select-row current-height pic)]
   1.116 +	      (if-not (empty? current-row)
   1.117 +		(let [new-code (row->gcode paramaters current-row)] 
   1.118 +		  (println new-code)
   1.119 +		  (str gcode new-code))
   1.120 +		gcode)))
   1.121 +	  ""
   1.122 +	  (range (height pic))))
   1.123 +
   1.124 +
   1.125 +;(defn pic->gcode [paramaters pic]
   1.126    
   1.127 -  (let [[x1 y1] (if forward? (last row) (first row))
   1.128 -	[x2 y2] (if forward? (first row) (last row))]
   1.129 -    
   1.130 -
   1.131 - ; (let [[x1 y1] (first row)
   1.132 -;	[x2 y2] (last row)
   1.133 -;	x2 (+ x2 (* x-dpi 0.318))]
   1.134 -	  
   1.135 -
   1.136 -;    (println x2)
   1.137 -    (str (format "G0 X%.3f Y%.3f\n"
   1.138 -		 (float (* x1 (/ x-dpi)))
   1.139 -		 (float (* y1 (/ y-dpi))))
   1.140 -	 
   1.141 -	 (format "G1 X%.3f Y%.3f\n"
   1.142 -		 (float (* x2 (/ x-dpi)))
   1.143 -		 (float (* y2 (/ y-dpi)))))))
   1.144 -
   1.145  
   1.146  (defn generate-gcode [pic]
   1.147    (str (raster-preamble)
   1.148 -       (str-join ""
   1.149 -		 (map
   1.150 -		  (fn [[index row]]
   1.151 -		    (row->gcode dpi (even? index) row))
   1.152 -		    (indexed (make-rows pic))))
   1.153 -		 (raster-epilogue)))
   1.154 +       (row->gcode paramaters pic)
   1.155 +       (raster-epilogue)))
   1.156  
   1.157  
   1.158  
   1.159 @@ -186,21 +207,26 @@
   1.160  (defn update-state []
   1.161    (def sing "/home/r/kevin/sing.png")
   1.162    (def pic (frame-hash (ImagePlus. sing)))  
   1.163 -  (def pic (b&w pic)))
   1.164 +  (def pic (b&w pic))
   1.165 +  (def pic (filter-vals (partial = black) pic)))
   1.166  
   1.167 -(defn compare-gen-fn [n f cmp]
   1.168 +(defn compare-gen-fn
   1.169 +  ([n f cmp]
   1.170    (let [theirs (re-split #"\n" (slurp cmp))
   1.171  	ours   (re-split #"\n" (f pic))]
   1.172      (println (format "%1$-25s%2$s" "OURS" "THEIRS"))
   1.173      (println "_______________________________________")
   1.174      (dorun (map (fn [[us them]] (println
   1.175  			     (format "%1$-25s%2$s" us them)))
   1.176 -			     (take n (partition 2 (interleave  ours theirs)))))))
   1.177 +			     (take n (partition 2 (interleave  ours theirs))))))))
   1.178  
   1.179 -(defn compare-gcode [n]
   1.180 -  (compare-gen-fn n generate-gcode "/home/r/kevin/reference.ngc"))
   1.181 -(defn compare-gmask [n]
   1.182 -  (compare-gen-fn n generate-gmask "/home/r/kevin/reference.gmask"))
   1.183 +(defn compare-gcode
   1.184 +  ([] (compare-gcode 25))
   1.185 +  ([n] (compare-gen-fn n generate-gcode "/home/r/kevin/reference.ngc")))
   1.186 +
   1.187 +(defn compare-gmask
   1.188 +  ([] compare-gmask 25)
   1.189 +  ([n]  (compare-gen-fn n generate-gmask "/home/r/kevin/reference.gmask")))
   1.190    
   1.191  
   1.192