Math expressions

Math expressions can be used in scene files. Values that should be parsed as math expressions should be enclosed in parentheses.

Sample XML scene file
 1<%set res = 1920 %>
 2<%set spp = 128 %>
 3<%set mix = 0.5 %>
 4
 5<sensor type="perspective" id="camera">
 6   <integer name="samples" value="$spp" />
 7
 8   <film type="array" >
 9      <integer name="width" value="$res" />
10      <integer name="height" value="(ROUND($res*9/16))" />
11   </film>
12</sensor>
13
14<texture function="((1-$mix) * tex1 + $mix * tex2)">
15   <texture name="tex1" type="checkerboard">
16      <spectrum name="colour1" constant="($colour1*2)" />
17      <spectrum name="colour2" constant="($colour2*2)" />
18   </texture>
19   <texture name="tex2" type="bitmap">
20      <path value="..." />
21   </texture>
22</texture>

In the example above, a function texture blends between a couple of textures using a user-supplied function.

The exprtk library is used for mathematical expressions parsing. It is a powerful feature-rich library, with support for a variety of built-in functions, see its documentations. Parsed expressions are compiled and statically evaluated, as much as possible. For example, the “(1-$mix)” part of the function in the example above (line 14) can be evaluated statically, while the rest of the expression needs to be evaluated dynamically for each texture lookup.

Complex numbers in scene files are parsed as expressions, for example 1+2i. Complex-valued math expression parsing is done only for some of the data fields, where a complex value is expected.