Extend and fix bugs in some gcode helpers

This commit is contained in:
arne 2023-08-05 15:54:59 +02:00
commit 8255d76580
3 changed files with 15 additions and 16 deletions

View file

@ -33,7 +33,7 @@
(throw (IllegalArgumentException. (throw (IllegalArgumentException.
(str ~(name coord) " should be in range [" ~min-v# ", " ~max-v# "] but is " ~coord)))))) (str ~(name coord) " should be in range [" ~min-v# ", " ~max-v# "] but is " ~coord))))))
(defn- send-command! [^Socket line-us ^String raw-cmd] (defn send-command! [^Socket line-us ^String raw-cmd]
;; this is basically taken from the Processing example code and the processing ;; this is basically taken from the Processing example code and the processing
;; "Client" class ;; "Client" class
(doto (io/output-stream line-us) (doto (io/output-stream line-us)
@ -42,7 +42,7 @@
;; wait for the response ;; wait for the response
(let [res (read-response line-us)] (let [res (read-response line-us)]
(if-not (re-find #"^(ok|hello)" res) (if-not (re-find #"^(ok|hello)" res)
(throw (Exception. res)) #dbg (throw (Exception. res))
res))) res)))
(defn send-movement! (defn send-movement!
@ -52,7 +52,7 @@
(validate-coord x) (validate-coord x)
(validate-coord y) (validate-coord y)
(validate-coord z) (validate-coord z)
(send-command! line-us (str "G01 X" x " Y" y " Z" z)) (send-command! line-us (format "G01 X%.0f Y%.0f Z%.0f" (double x) (double y) (double z)))
coords) coords)
(defn move-home! [^Socket line-us] (defn move-home! [^Socket line-us]

View file

@ -48,7 +48,7 @@
An optional parameter `{:res 10}` can be used to adjust the sampling resolution An optional parameter `{:res 10}` can be used to adjust the sampling resolution
for the shape, if the shape supports it." for the shape, if the shape supports it."
(comp gcode-verts->gcode-seq ->gcode-verts)) (comp #_gcode-verts->gcode-seq ->gcode-verts))
(defn- geom? [x] (defn- geom? [x]
(str/starts-with? (pr-str x) "#thi.ng.geom.")) (str/starts-with? (pr-str x) "#thi.ng.geom."))

View file

@ -2,31 +2,30 @@
(:require [thi.ng.geom.rect :as rect] (:require [thi.ng.geom.rect :as rect]
[thi.ng.math.core :as m])) [thi.ng.math.core :as m]))
(defn g01-bounds (defn coord-seq-bounds
"Returns a rect representing the drawing bounds of a sequence of g01 coords" "Returns a rect representing the drawing bounds of a sequence of g01 coords"
[g01] [coords]
(let [bounds [Double/MAX_VALUE Double/MIN_VALUE Double/MIN_VALUE Double/MAX_VALUE] (let [bounds [Double/MAX_VALUE Double/MIN_VALUE Double/MIN_VALUE Double/MAX_VALUE]
[top right bottom left] (reduce (fn [[top right bottom left] [x y _]] [top right bottom left] (reduce (fn [[top right bottom left] [x y _]]
[(min top y) (max right x) [(min top y) (max right x)
(max bottom y) (min left x)]) (max bottom y) (min left x)])
bounds bounds
g01)] coords)]
(rect/rect [left bottom] [right top]))) (rect/rect [left bottom] [right top])))
(defn rescale (defn rescale
"Returns a new g01 sequence that is proportionally scaled to fit into the "Returns a new g01 sequence that is proportionally scaled to fit into the
bounding box passed in as the second argument. Assumes that the top left bounding box passed in as the second argument. Assumes that the top left
in g01-seq is at [0 0]." in g01-seq is at [0 0]."
[g01-seq [top right bottom left]] [into-bounds g01-seq ]
(let [s-bounds (g01-bounds g01-seq) (let [src-bounds (coord-seq-bounds g01-seq)
t-bounds (rect/rect [left bottom] [right top])
;; we need to translate the bounding box ;; we need to translate the bounding box
[translate-x translate-y] (m/+ (:p s-bounds) (:p t-bounds)) [translate-x translate-y] (m/+ (:p src-bounds) (:p into-bounds))
;; and scale it ;; and scale it
[s-x s-y] (:p s-bounds) [s-x s-y] (:p src-bounds)
[t-x t-y] (:p t-bounds) [t-x t-y] (:p into-bounds)
[s-width s-height] (:size s-bounds) [s-width s-height] (:size src-bounds)
[t-width t-height] (:size t-bounds) [t-width t-height] (:size into-bounds)
factor (min (/ (+ t-x t-width) (+ s-x s-width)) (/ (+ t-y t-height) (+ s-y s-height)))] factor (min (/ (+ t-x t-width) (+ s-x s-width)) (/ (+ t-y t-height) (+ s-y s-height)))]
(map (fn [coord] (map (fn [coord]
(-> (update coord 0 #(+ translate-x (* % factor))) (-> (update coord 0 #(+ translate-x (* % factor)))