One of the OpenSCAD limitations is the inability to pass functions to functions or modules.
This library lets you define mathematical functions in a string, using all of the OpenSCAD mathematical operators (and a bit more). This string is then compiled to an optimized representation which can be evaluated fairly quickly. For instance:
use <eval.scad>;f = "pow(x,3)*y-x*cos(y)+y";fc = compileFunction(f);echo(eval(fc, [["x", 12], ["y", -5]]));
will evaluate the function at x=12 and y=-5.
Parsing the string is not fast, but if you evaluate the compiled function for different variable values, the evaluated function should be fairly fast (in my test, about 10,000 eval()
s per second).
There are three public functions provided:
compileFunction(f)
: takes a function string f
and returns an optimized compiled representation; the representation is described here: http://www.thingiverse.com/thing:2289738optimize=false
.eval(fc, variables)
: takes a compiled function fc
and evaluates it for the specified parameter values; variables
is a vector of [variableName, value]
pairs; for instance, [["x",1],["y",2]]
evaluateFunction(expression, variables)
: use this to evaluate an uncompiled function once; this is a convenience function equivalent to eval(compileFunction(expression,optimize=false),variables)
; if you are going to evaluate the function more than a couple of times, please compile it once first, and then call eval()
on the compiled versionThe string function representations should be able to use all of the following standard OpenSCAD language elements:
+ - * / % pow sqrt cos sin tan acos asin atan atan2 abs ceil exp floor ln log round signPIcross norm max min concatlet?: != == >= <= < > ! && ||
Vectors are supported, using [x,y,z]
style vector forming and a[i]
indexing.
Numbers can be specified in the usual way, and the true
and false
constants are available.
Additionally, the ^
infix operator is provided as a shorthand for pow
, and COS
, SIN
, TAN
, ACOS
, ASIN
, ATAN
and ATAN2
are provided which are radian-based trigonometric functions.
There are likely multiple bugs I have yet to catch, and there may be some subtle deviations from OpenSCAD order of operations.
The demo file is a 3D parametric curve grapher. You should be able to use the Customizer (unless it times out) to change the formula.
Benchmarks:
On my i5 based Windows 10 laptop, per 10,000 runs with x^3*y-x*y^3
:
compileFunction()
with optimization: 25 secondscompileFunction()
without optimization: 21 secondseval()
applied to precompiled function: 0.8 secondsevaluateFunction()
22 secondsNote: As I update eval.scad, I will upload new versions here. However, I will not be regularly updating the version inside the demo file. There is a repository here:
https://github.com/arpruss/miscellaneous-scad
Update notes:
pow
interpolate(x,vector)
function to the language: vector
is a list of [x,value]
pairs where the value
can be a vector or a scalarlet
exp(x)
and floor(x)
.compileFunction()
and evaluateFunction()
should now work if fed in an already-compiled function; in that case, compileFunction()
returns its input and evaluateFunction()
is the same as eval()
; this means that you can seamlessly support compiled and uncompiled function arguments in the same module; I also removed a warning when optimizing a function that uses norm()
PI
supported[]
eval-pre2019.scad | 20.2KB | |
eval.scad | 19.6KB | |
evaldemo-standalone.scad | 20.5KB | |
evaldemo.stl | 810.5KB |