diff 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
line wrap: on
line diff
     1.1 --- a/src/laser/rasterize.clj	Fri Aug 20 03:18:05 2010 -0400
     1.2 +++ b/src/laser/rasterize.clj	Fri Aug 20 06:53:41 2010 -0400
     1.3 @@ -172,3 +172,101 @@
     1.4  
     1.5  
     1.6  
     1.7 +;this is a sequence of rows
     1.8 +
     1.9 +;(defn span [row]
    1.10 +;  (let [sorted-row (sort #(< (first %1) (first %2)) row)]
    1.11 +;    (vector (first sorted-row) (last sorted-row))))
    1.12 +
    1.13 +
    1.14 +(defn row->gcode [[x-dpi y-dpi] row]
    1.15 +  (let [[x1 y1] (first row)
    1.16 +	[x2 y2] (last row)]
    1.17 +    (str (format "GO X%.3f Y%.3f\n"
    1.18 +		 (float (* x1 (/ x-dpi)))
    1.19 +		 (float (* y1 (/ y-dpi))))
    1.20 +	 
    1.21 +	 (format "G1 X%.3f Y%.3f\n"
    1.22 +		 (float (* x2 (/ x-dpi)))
    1.23 +		 (float (* y2 (/ y-dpi)))))))
    1.24 +
    1.25 +(defn gather-row [row]
    1.26 +  (let [base    [[(first (first row)) (first (first row))]]]
    1.27 +					;  (println base)
    1.28 +    (reduce 
    1.29 +     (fn colapse [collection new-n]
    1.30 +       
    1.31 +       (let [collection (apply vector collection)
    1.32 +	     prevoius (last (last collection))
    1.33 +	     range-start (first (last collection))]
    1.34 +					;       (println new-n)
    1.35 +					;       (println prevoius)
    1.36 +					;       (println range-start)
    1.37 +	 (if (<= new-n (+ prevoius 1))
    1.38 +		     (do ;(println "join")
    1.39 +					;(println (butlast collection)) 
    1.40 +		       (conj (apply vector (butlast collection))  (vector range-start new-n)))
    1.41 +		     (conj collection  (vector new-n new-n)))))
    1.42 +     
    1.43 +     base
    1.44 +	       (map first row))))
    1.45 +
    1.46 +
    1.47 +
    1.48 +(defn row->gmask [[x-dpi y-dpi] forward? row]
    1.49 +
    1.50 +  (let [start (float (* (/ x-dpi) (first (first row))))]
    1.51 +
    1.52 +    (let [preamble (if forward?
    1.53 +		     (str 
    1.54 +		     (format "0 0 0 %.3f\n" start)
    1.55 +		     (format "0 0 1 %.3f\n" start)))
    1.56 +	  body
    1.57 +	  (for [[x y] (gather-row row)]
    1.58 +	    (let [x (float (* x (/ x-dpi)))
    1.59 +		  y (float (* y (/ x-dpi)))]
    1.60 +	      (if forward?
    1.61 +		(str (format "0 0 1 %.3f\n" x)
    1.62 +		     (format "0 1 1 %.3f\n" y))
    1.63 +		
    1.64 +		(str (format "0 0 0 %.3f\n" x)
    1.65 +		     (format "0 1 0 %.3f\n" y)))))]
    1.66 +
    1.67 +      (str preamble (str-join "" body)))))
    1.68 +
    1.69 +
    1.70 +
    1.71 +(defn make-rows [pic]
    1.72 +  
    1.73 +  (map (partial sort #(< (first %1) (first %2)))
    1.74 +       (partition-by last
    1.75 +		     (sort (fn [[x1 y1][x2 y2]] (> y2 y1))
    1.76 +			   (map first (filter-vals (partial = black) pic))))))
    1.77 +
    1.78 +
    1.79 +
    1.80 +
    1.81 +;sequence of numbers indicating width
    1.82 +(def mega 
    1.83 + 
    1.84 +     ;sequence of rows
    1.85 +      
    1.86 +      (map (partial sort #(< (first %1) (first %2)))
    1.87 +	   (partition-by last
    1.88 +			 (sort (fn [[x1 y1][x2 y2]] (> y2 y1))
    1.89 +			       (map first (filter-vals (partial = black) pic))))))
    1.90 +     
    1.91 +
    1.92 +
    1.93 +
    1.94 +(defn generate-gmask [pic]
    1.95 +
    1.96 +  (str "1 0 0 0\n"
    1.97 +       (str-join "" (map (fn [[index row]]  (row->gmask dpi (even? index) row)) (indexed (make-rows pic))))))
    1.98 +
    1.99 +
   1.100 +(defn generate-gcode [pic]
   1.101 +  (str-join "" (map (partial row->gcode dpi) (make-rows pic))))
   1.102 +
   1.103 +
   1.104 +