Math Expression Documentation
OpenVCAD uses math expression strings in several nodes, most notably the
FGrade and Function nodes. These expressions define how material
volume fractions or scalar fields vary across space.
Note
OpenVCAD uses the ExprTk library for parsing math strings. It follows syntax common on most graphing calculators and supports many advanced features. See the ExprTk documentation for the full specification.
Coordinate Systems
The following variables may be used in any math expression to grade based on spatial position. You can combine multiple coordinate systems in a single expression.
Name |
Form |
Variable Names |
|---|---|---|
Cartesian |
f(x, y, z) |
|
Cylindrical |
f(rho, phic, z) |
|
Spherical |
f(r, theta, phis) |
|
Signed Distance |
f(d) |
|
Note
phic corresponds to phi in cylindrical coordinates and phis to phi in
spherical coordinates.
Note
d is the signed distance to the surface of the object. Zero is the surface,
negative values are inside, and positive values are outside. This can be used
to create conformal gradients.
Arithmetic & Assignment Operators
Operator |
Definition |
|---|---|
|
Addition between x and y. (e.g., |
|
Subtraction between x and y. (e.g., |
|
Multiplication between x and y. (e.g., |
|
Division between x and y. (e.g., |
|
Modulus of x with respect to y. (e.g., |
|
x to the power of y. (e.g., |
|
Assign the value of x to y. (e.g., |
|
Increment x by the value of the expression on the right-hand side. |
|
Decrement x by the value of the expression on the right-hand side. |
|
Assign the multiplication of x by the value of the expression to x. |
|
Assign the division of x by the value of the expression to x. |
|
Assign x modulo the value of the expression on the right-hand side to x. |
Equalities & Inequalities
Operator |
Definition |
|---|---|
|
True only if x is strictly equal to y. (e.g., |
|
True only if x does not equal y. (e.g., |
|
True only if x is less than y. (e.g., |
|
True only if x is less than or equal to y. (e.g., |
|
True only if x is greater than y. (e.g., |
|
True only if x is greater than or equal to y. (e.g., |
Boolean Operations
Operator |
Definition |
|---|---|
|
True state or any value other than zero (typically 1). |
|
False state, value of exactly zero. |
|
Logical AND, True only if x and y are both true. (e.g., |
|
Logical OR, True if either x or y is true. (e.g., |
|
Logical NOT, negates the logical sense of the input. (e.g., |
|
Logical XOR, True only if the logical states of x and y differ. |
|
Logical NAND, True only if either x or y is false. |
|
Logical NOR, True only if both x and y are false. |
|
Multi-input logical AND, True only if all inputs are true. |
|
Multi-input logical OR, True if at least one input is true. |
General Purpose Functions
Function |
Definition |
|---|---|
|
Absolute value of x. (e.g., |
|
Average of all inputs. (e.g., |
|
Smallest integer greater than or equal to x. |
|
Largest integer less than or equal to x. |
|
Square root of x, where x >= 0. (e.g., |
|
Natural logarithm of x. (e.g., |
|
e to the power of x. (e.g., |
|
x to the power of y. (e.g., |
|
Largest value of all inputs. (e.g., |
|
Smallest value of all inputs. (e.g., |
|
Clamp x between low and high. (e.g., |
Trigonometry Functions
Function |
Definition |
|---|---|
|
Sine of x. (e.g., |
|
Cosine of x. (e.g., |
|
Tangent of x. (e.g., |
|
Arc sine of x, in radians. |
|
Arc cosine of x, in radians. |
|
Arc tangent of x, in radians. |
|
Convert x from degrees to radians. (e.g., |
|
Convert x from radians to degrees. (e.g., |
Constants
Constant |
Value |
|---|---|
|
3.14159… |
|
2.71828… |
Examples
Linear gradient along X
A bar that transitions from 100% red at x = -50 to 100% blue at x = 50:
root = pv.FGrade(
["x/100 + 0.5", "-x/100 + 0.5"],
[materials.id("red"), materials.id("blue")],
False
)
Radial gradient
A sphere that is one material at the center and another at the surface:
root = pv.FGrade(
["clamp(0, 1 - r/5, 1)", "clamp(0, r/5, 1)"],
[materials.id("red"), materials.id("blue")],
False
)
Angular gradient (cylindrical)
A gradient that varies with angle around the Z-axis:
root = pv.FGrade(
["(phic + pi) / (2*pi)", "1 - (phic + pi) / (2*pi)"],
[materials.id("red"), materials.id("blue")],
False
)
Conformal gradient (signed distance)
A gradient that follows the surface contour of the object:
root = pv.FGrade(
["clamp(0, -d/2 + 0.5, 1)", "clamp(0, d/2 + 0.5, 1)"],
[materials.id("red"), materials.id("blue")],
False
)
Important
All PDFs at any spatial location must sum to 1. If they don’t, OpenVCAD will normalize them, but this may produce unexpected results.