OpenVCAD Math Expression Reference
This document covers the operators, functions, and coordinate variables available in OpenVCAD math expression strings. These expressions are used by FloatAttribute, Vec4Attribute, and VolumeFractionsAttribute to define spatially varying fields.
OpenVCAD uses the Exprtk library for parsing math strings. The syntax follows conventions common on most graphing calculators, but Exprtk supports many advanced features. See their documentation for the full specification.
Coordinate Systems
The following variables are available in any math expression. You can freely mix variables from different coordinate systems in a single expression.
Coordinate System |
Form |
Variable Names |
|---|---|---|
Cartesian |
f(x, y, z) |
|
Cylindrical |
f(rho, phi, z) |
|
Spherical |
f(r, theta, phi) |
|
Signed Distance |
f(d) |
|
Note:
phicis the azimuthal angle in the cylindrical system;phisis the polar angle in the spherical system. Both are in radians.
Note:
dis 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 (surface-following) gradients.
Cylindrical Coordinate Definitions
rho= sqrt(x^2 + y^2) — radial distance from the Z axisphic= atan2(y, x) — azimuthal angle in the XY plane (radians, range -pi to +pi)z= z — same as the Cartesian Z coordinate
Spherical Coordinate Definitions
r= sqrt(x^2 + y^2 + z^2) — radial distance from the origintheta= atan2(y, x) — azimuthal angle in the XY plane (radians, range -pi to +pi)phis= acos(z / r) — polar angle from the +Z axis (radians, range 0 to pi)
Operators
Arithmetic & Assignment
Operator |
Definition |
|---|---|
|
Addition. ( |
|
Subtraction. ( |
|
Multiplication. ( |
|
Division. ( |
|
Modulus. ( |
|
Exponentiation. ( |
|
Assignment. ( |
|
Increment-assign. ( |
|
Decrement-assign. ( |
|
Multiply-assign. ( |
|
Divide-assign. ( |
|
Modulo-assign. ( |
Equalities & Inequalities
Operator |
Definition |
|---|---|
|
Equal to. ( |
|
Not equal to. ( |
|
Less than. ( |
|
Less than or equal. ( |
|
Greater than. ( |
|
Greater than or equal. ( |
Boolean Operations
Operator |
Definition |
|---|---|
|
True state (any non-zero value). |
|
False state (exactly zero). |
|
Logical AND. ( |
|
Logical OR. ( |
|
Logical NOT. ( |
|
Logical XOR. |
|
Logical NAND. |
|
Logical NOR. |
|
Multi-input AND. |
|
Multi-input OR. |
Functions
General Purpose
Function |
Definition |
|---|---|
|
Absolute value of x. |
|
Average of all inputs. |
|
Smallest integer >= x. |
|
Largest integer <= x. |
|
Square root (x >= 0). |
|
Natural logarithm. |
|
e raised to x. |
|
x to the power y. |
|
Largest of all inputs. |
|
Smallest of all inputs. |
|
Clamp x between lo and hi. |
Trigonometry
Function |
Definition |
|---|---|
|
Sine of x (radians). |
|
Cosine of x (radians). |
|
Tangent of x (radians). |
|
Arc sine (returns radians). |
|
Arc cosine (returns radians). |
|
Arc tangent (returns radians). |
|
Convert degrees to radians. |
|
Convert radians to degrees. |
Tips
Parentheses work as expected for grouping:
(x + y) * z.Constants: Use numeric literals directly:
3.14159,2.71828. Exprtk also providespias a built-in constant.Mixing coordinate systems: An expression like
rho * sin(phis) + zis perfectly valid — it uses cylindricalrho, sphericalphis, and Cartesianzsimultaneously.Clamping outputs: Use
clamp(0, expr, 1)to keep values in [0, 1], which is especially important for color channels and volume fractions.