-
Notifications
You must be signed in to change notification settings - Fork 1
/
dfm.clj
49 lines (39 loc) · 1.76 KB
/
dfm.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
;;; Utilities for design for manufacturability (DFM).
;;; In this case, it’s mainly design for additive manufacturing (DFAM),
;;; pre-empting 3D printer inaccuracies in the xy plane.
(ns scad-tarmi.dfm
(:require [scad-tarmi.maybe :as maybe]))
;;;;;;;;;;;;;;
;; INTERNAL ;;
;;;;;;;;;;;;;;
(defn- ratio [nominal error] (/ (- nominal error) nominal))
;;;;;;;;;;;;;;;;;;;;;;;;;
;; INTERFACE FUNCTIONS ;;
;;;;;;;;;;;;;;;;;;;;;;;;;
(defn error-fn
"Take an error: A measurement in mm of material added by a printer in the xy
plane. Return a compensator function that, when called with an intended
measurement, neutralizes the error for that measurement.
The two measurements (error and nominal) should be of a kind, whether it’s
radius or diameter. The convention is to use full size (diameter).
Called with one argument, a number, the closure supports the arithmetic
adjustment of an individual dimensional input. Using the function entirely
in this manner is typically difficult but saves on transformations in the
OpenSCAD output, thus improving rendering performance.
Called with more arguments, the closure implements scad-tarmi’s maybe/scale."
([] (error-fn (- 0.5)))
([error] (error-fn (/ error -2) error))
([positive-error negative-error]
(fn
([nominal] (- nominal negative-error))
([nominal options & block]
{:pre [(number? nominal)
(map? options)]}
(let [{:keys [negative x y z]
:or {negative true, x true, y true, z false}} options
n (ratio nominal (if negative negative-error positive-error))
factors (vec (map #(if % n 1) [x y z]))]
(apply (partial maybe/scale factors) block))))))
(def none
"An error function that leaves no trace in OpenSCAD code."
(error-fn 0))