# HG changeset patch # User Robert McIntyre # Date 1283139639 14400 # Node ID 52f544d054149405904f03dd615a31731d795c43 # Parent 8ad629298649073d1abd8e96459b7b7561d0ecbb I think I've made it worse.... diff -r 8ad629298649 -r 52f544d05414 src/laser/rasterize.clj --- a/src/laser/rasterize.clj Sun Aug 29 19:02:26 2010 -0400 +++ b/src/laser/rasterize.clj Sun Aug 29 23:40:39 2010 -0400 @@ -5,7 +5,9 @@ [clojure.contrib [str-utils :only [str-join re-gsub]] [seq :only [indexed]] - [math]]) + [math] + [def] + ]) (:import [ij ImagePlus IJ])) ;(import '(java.io File)) @@ -17,6 +19,7 @@ (def feed 120) (def dpi [500, 500]) +(def paramaters {:x-dpi 500 :y-dpi 500 :margin 0 :x-offset 0 :y-offset 0}) ;;; this process is divided into two tasks, ;;; creating the raster g-code, which sweeps back and forth @@ -25,12 +28,19 @@ ;;; we'll be using frame-hashes, which represent picutres as ;;; a 3D vector field over 2D space, with the vectors representing -;;; the rgb values at that particulat point. +;;; the rgb values at that particular point. (defn select-row "returns a frame hash that is just a single line at the chosen y" [y window] - (filter-keys (comp (partial = y) last) window)) + (reduce + (fn [old-map number] + (let [pixel (get window [number y] nil)] + (if-not (nil? pixel) + (into old-map {[number y] pixel}) + old-map))) + {} + (range (width window)))) (defn make-rows [pic] (map (partial sort #(< (first %1) (first %2))) @@ -38,63 +48,74 @@ (sort (fn [[x1 y1][x2 y2]] (> y2 y1)) (map first (filter-vals (partial = black) pic)))))) - ;;; generate rastering g-code (defn raster-preamble [] - (str-join \newline - ["M63 P0\nG61" - (str "F" feed) - "M101" - "M3 S1\n"])) + (str-join \newline ["M63 P0\nG61" (str "F" feed) "M101" "M3 S1\n"])) (defn raster-epilogue [] - (str-join \newline - ["M63 P0" - "M5" - "M2\n"])) + (str-join \newline ["M63 P0" "M5" "M2\n"])) -(defn raster-comment [string] +(defn raster-comment + "wrap a statement in PARENTHENSIS to make it a comment in gcode. + parenthesis themselves aren't allowed in comments. + Oh the humanity!!" + [string] (str "(" (re-gsub #"[()]" "" string) ")")) +(defn rows + "creates a sequence of one dimensional vector fields which + represent the rows of a picture" + [pic] + (let [non-empty-rows (apply sorted-set (map (comp last first) pic))] + (pmap (fn [n] (select-row n pic)) non-empty-rows))) + + -;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 [{:keys [x-dpi y-dpi margin x-offset y-offset]} row] + (let [pixels (keys row) + x2 0 + [_ y2] (first pixels) + [_ y1] (first pixels) + x1 533] + ;(let [ordered-row +; (sort-map-by (fn [[x1 _] [x2 _]] (> x2 x1)) row)] + + (let [;[x1 y1] (last (keys ordered-row)) + ;[x2 y2] (first (keys ordered-row)) + [x1 y1 x2 y2] (if (odd? y1) [x2 y2 x1 y1] [x1 y1 x2 y2])] + + (str (format "G0 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 row->gcode [[x-dpi y-dpi] forward? row] + +(defn pic->gcode [paramaters pic] + (reduce (fn [gcode current-height] + (let [current-row (select-row current-height pic)] + (if-not (empty? current-row) + (let [new-code (row->gcode paramaters current-row)] + (println new-code) + (str gcode new-code)) + gcode))) + "" + (range (height pic)))) + + +;(defn pic->gcode [paramaters pic] - (let [[x1 y1] (if forward? (last row) (first row)) - [x2 y2] (if forward? (first row) (last row))] - - - ; (let [[x1 y1] (first row) -; [x2 y2] (last row) -; x2 (+ x2 (* x-dpi 0.318))] - - -; (println x2) - (str (format "G0 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 generate-gcode [pic] (str (raster-preamble) - (str-join "" - (map - (fn [[index row]] - (row->gcode dpi (even? index) row)) - (indexed (make-rows pic)))) - (raster-epilogue))) + (row->gcode paramaters pic) + (raster-epilogue))) @@ -186,21 +207,26 @@ (defn update-state [] (def sing "/home/r/kevin/sing.png") (def pic (frame-hash (ImagePlus. sing))) - (def pic (b&w pic))) + (def pic (b&w pic)) + (def pic (filter-vals (partial = black) pic))) -(defn compare-gen-fn [n f cmp] +(defn compare-gen-fn + ([n f cmp] (let [theirs (re-split #"\n" (slurp cmp)) ours (re-split #"\n" (f pic))] (println (format "%1$-25s%2$s" "OURS" "THEIRS")) (println "_______________________________________") (dorun (map (fn [[us them]] (println (format "%1$-25s%2$s" us them))) - (take n (partition 2 (interleave ours theirs))))))) + (take n (partition 2 (interleave ours theirs)))))))) -(defn compare-gcode [n] - (compare-gen-fn n generate-gcode "/home/r/kevin/reference.ngc")) -(defn compare-gmask [n] - (compare-gen-fn n generate-gmask "/home/r/kevin/reference.gmask")) +(defn compare-gcode + ([] (compare-gcode 25)) + ([n] (compare-gen-fn n generate-gcode "/home/r/kevin/reference.ngc"))) + +(defn compare-gmask + ([] compare-gmask 25) + ([n] (compare-gen-fn n generate-gmask "/home/r/kevin/reference.gmask")))