Function parser for OpenSCAD and parametric curve grapher

Function parser for OpenSCAD and parametric curve grapher

thingiverse

One of OpenSCAD's limitations is its inability to pass functions to functions or modules. This library lets you define mathematical functions in a string using all the OpenSCAD mathematical operators and more. The string is then compiled into an optimized representation that can be evaluated 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]])); This 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, it should be fairly fast. In my test, about 10,000 evals per second were achieved. 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 * 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 the following standard OpenSCAD language elements: + - * / % pow sqrt cos sin tan acos asin atan atan2 abs ceil exp floor ln log round sign PI cross norm max min concat let ?: != == >= <= < > ! && || 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: * 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 []

Download Model from thingiverse

With this file you will be able to print Function parser for OpenSCAD and parametric curve grapher with your 3D printer. Click on the button and save the file on your computer to work, edit or customize your design. You can also find more 3D designs for printers on Function parser for OpenSCAD and parametric curve grapher.