# HG changeset patch # User Robert McIntyre # Date 1282301621 14400 # Node ID 5e167f275a9315b2b0f7ad43b962d11794b7ae12 # Parent 4ae2497acf7d0fb1c7d4e6e8934d6e612c40ea75 moving to rt, hopefully I can do stuff now diff -r 4ae2497acf7d -r 5e167f275a93 src/laser/rasterize.clj --- a/src/laser/rasterize.clj Fri Aug 20 03:18:05 2010 -0400 +++ b/src/laser/rasterize.clj Fri Aug 20 06:53:41 2010 -0400 @@ -172,3 +172,101 @@ +;this is a sequence of rows + +;(defn span [row] +; (let [sorted-row (sort #(< (first %1) (first %2)) row)] +; (vector (first sorted-row) (last sorted-row)))) + + +(defn row->gcode [[x-dpi y-dpi] row] + (let [[x1 y1] (first row) + [x2 y2] (last row)] + (str (format "GO X%.3f Y%.3f\n" + (float (* x1 (/ x-dpi))) + (float (* y1 (/ y-dpi)))) + + (format "G1 X%.3f Y%.3f\n" + (float (* x2 (/ x-dpi))) + (float (* y2 (/ y-dpi))))))) + +(defn gather-row [row] + (let [base [[(first (first row)) (first (first row))]]] + ; (println base) + (reduce + (fn colapse [collection new-n] + + (let [collection (apply vector collection) + prevoius (last (last collection)) + range-start (first (last collection))] + ; (println new-n) + ; (println prevoius) + ; (println range-start) + (if (<= new-n (+ prevoius 1)) + (do ;(println "join") + ;(println (butlast collection)) + (conj (apply vector (butlast collection)) (vector range-start new-n))) + (conj collection (vector new-n new-n))))) + + base + (map first row)))) + + + +(defn row->gmask [[x-dpi y-dpi] forward? row] + + (let [start (float (* (/ x-dpi) (first (first row))))] + + (let [preamble (if forward? + (str + (format "0 0 0 %.3f\n" start) + (format "0 0 1 %.3f\n" start))) + body + (for [[x y] (gather-row row)] + (let [x (float (* x (/ x-dpi))) + y (float (* y (/ x-dpi)))] + (if forward? + (str (format "0 0 1 %.3f\n" x) + (format "0 1 1 %.3f\n" y)) + + (str (format "0 0 0 %.3f\n" x) + (format "0 1 0 %.3f\n" y)))))] + + (str preamble (str-join "" body))))) + + + +(defn make-rows [pic] + + (map (partial sort #(< (first %1) (first %2))) + (partition-by last + (sort (fn [[x1 y1][x2 y2]] (> y2 y1)) + (map first (filter-vals (partial = black) pic)))))) + + + + +;sequence of numbers indicating width +(def mega + + ;sequence of rows + + (map (partial sort #(< (first %1) (first %2))) + (partition-by last + (sort (fn [[x1 y1][x2 y2]] (> y2 y1)) + (map first (filter-vals (partial = black) pic)))))) + + + + +(defn generate-gmask [pic] + + (str "1 0 0 0\n" + (str-join "" (map (fn [[index row]] (row->gmask dpi (even? index) row)) (indexed (make-rows pic)))))) + + +(defn generate-gcode [pic] + (str-join "" (map (partial row->gcode dpi) (make-rows pic)))) + + +