Function Parser For OpenSCAD And Parametric Curve Grapher 3D Printer Model

Author: @
License: CC BY
File formats: scad,stl
Download type: zip
Size:100.9KB

The file 'Function Parser For OpenSCAD And Parametric Curve Grapher 3D Printer Model' is (scad,stl) file type, size is 100.9KB.

Summary

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:2289738
    There may be some bugs in the optimization. If you run into problems, add the optional argument optimize=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 version

The 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 seconds
  • compileFunction() without optimization: 21 seconds
  • eval() applied to precompiled function: 0.8 seconds
  • evaluateFunction() 22 seconds

Note: 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:

  • July 8, 2020: fixed bug in pow
  • June 1, 2020: added interpolate(x,vector) function to the language: vector is a list of [x,value] pairs where the value can be a vector or a scalar
  • May 21, 2019: update to remove warnings for OpenSCAD 2019.05; will no longer work on earlier versions (use eval-pre2019.scad)
  • July 19, 2018: bug fixes for let
  • Jan 18, 2018: evaluateFunction(x) works when x is a scalar: it just returns x
  • May 6, 2017: No longer distinguish the "fast" variables x,y,z,t from the others. Remove dollar sign operator for variable values in compiled form now that I've discovered a fast check for whether something is a string.
  • May 16, 2017: Fix evaluator bug in some functions like exp(x) and floor(x).
  • May 17, 2017: 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()
  • May 18, 2017: PI supported
  • Apr 17, 2019: bugfix for []
eval-pre2019.scad 20.2KB
eval.scad 19.6KB
evaldemo-standalone.scad 20.5KB
evaldemo.stl 810.5KB