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.
(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
;; "Client" class
(doto (io/output-stream line-us)
@ -42,7 +42,7 @@
;; wait for the response
(let [res (read-response line-us)]
(if-not (re-find #"^(ok|hello)" res)
(throw (Exception. res))
#dbg (throw (Exception. res))
res)))
(defn send-movement!
@ -52,7 +52,7 @@
(validate-coord x)
(validate-coord y)
(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)
(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
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]
(str/starts-with? (pr-str x) "#thi.ng.geom."))

View file

@ -2,31 +2,30 @@
(:require [thi.ng.geom.rect :as rect]
[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"
[g01]
[coords]
(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 _]]
[(min top y) (max right x)
(max bottom y) (min left x)])
bounds
g01)]
coords)]
(rect/rect [left bottom] [right top])))
(defn rescale
"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
in g01-seq is at [0 0]."
[g01-seq [top right bottom left]]
(let [s-bounds (g01-bounds g01-seq)
t-bounds (rect/rect [left bottom] [right top])
[into-bounds g01-seq ]
(let [src-bounds (coord-seq-bounds g01-seq)
;; 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
[s-x s-y] (:p s-bounds)
[t-x t-y] (:p t-bounds)
[s-width s-height] (:size s-bounds)
[t-width t-height] (:size t-bounds)
[s-x s-y] (:p src-bounds)
[t-x t-y] (:p into-bounds)
[s-width s-height] (:size src-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)))]
(map (fn [coord]
(-> (update coord 0 #(+ translate-x (* % factor)))