# HG changeset patch # User Robert McIntyre # Date 1283141346 14400 # Node ID 3b255dcd6c500b2f3212ae7e695ee17e789a33e5 # Parent a769347618a17a721820e21b1d93221259113360 getting closer diff -r a769347618a1 -r 3b255dcd6c50 graster/graster/.git/index Binary file graster/graster/.git/index has changed diff -r a769347618a1 -r 3b255dcd6c50 src/laser/rasterize.clj --- a/src/laser/rasterize.clj Sun Aug 29 23:44:59 2010 -0400 +++ b/src/laser/rasterize.clj Mon Aug 30 00:09:06 2010 -0400 @@ -3,11 +3,9 @@ image-utils map-utils] [clojure.contrib - [str-utils :only [str-join re-gsub]] + [str-utils :only [str-join re-gsub re-split]] [seq :only [indexed]] - [math] - [def] - ]) + [math]]) (:import [ij ImagePlus IJ])) ;(import '(java.io File)) @@ -18,8 +16,7 @@ (set! *print-length* 20) (def feed 120) (def dpi [500, 500]) - -(def paramaters {:x-dpi 500 :y-dpi 500 :margin 0 :x-offset 0 :y-offset 0}) +(def paramaters {:x-dpi 500 :y-dpi 500 :margin 0 :x-offset 1 :y-offset 1}) ;;; this process is divided into two tasks, ;;; creating the raster g-code, which sweeps back and forth @@ -28,19 +25,12 @@ ;;; 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 particular point. +;;; the rgb values at that particulat point. (defn select-row "returns a frame hash that is just a single line at the chosen y" [y 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)))) + (filter-keys (comp (partial = y) last) window)) (defn make-rows [pic] (map (partial sort #(< (first %1) (first %2))) @@ -48,80 +38,66 @@ (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 - "wrap a statement in PARENTHENSIS to make it a comment in gcode. - parenthesis themselves aren't allowed in comments. - Oh the humanity!!" - [string] +(defn raster-comment [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]} forward? row] + (let [[x1 y1] (if forward? (last row) (first row)) + [x2 y2] (if forward? (first row) (last row))] + (let [x1 (* x1 (/ x-dpi)) + x2 (* x2 (/ x-dpi)) + y1 (* y1 (/ y-dpi)) + y2 (* y2(/ y-dpi))] - -(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])] - + (let [x1 (+ x1 x-offset) + x2 (+ x2 x-offset) + y1 (+ y1 y-offset) + y2 (+ y2 y-offset)] + + (str (format "G0 X%.3f Y%.3f\n" - (float (* x1 (/ x-dpi))) - (float (* y1 (/ y-dpi)))) + (float x1 ) + (float y1)) (format "G1 X%.3f Y%.3f\n" - (float (* x2 (/ x-dpi))) - (float (* y2 (/ y-dpi)))))))) - - -(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 - - - -;(defn pic->gcode [paramaters pic] + (float x2) + (float y2))))))) - (defn generate-gcode [pic] (str (raster-preamble) - (row->gcode paramaters pic) - (raster-epilogue))) + (str-join "" + (map + (fn [[index row]] + (row->gcode paramaters (even? index) row)) + (indexed (make-rows pic)))) + (raster-epilogue))) @@ -212,27 +188,22 @@ (defn update-state [] (def sing "/home/r/kevin/sing.png") - (def pic (frame-hash (ImagePlus. sing))) - (def pic (b&w pic)) - (def pic (filter-vals (partial = black) pic))) + (def pic (frame-hash (rotate 180 (ImagePlus. sing)))) + (def pic (b&w 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 - ([] (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"))) +(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"))