Using Math Template

Volume Math Template

  1. In the Volume Maths process, scroll to the Formula section.
  2. Click the blue folder icon to view the Math Template menu.
    • Select Template: Open the Select a Math Template window.
    • Create Template: Open the Create a new Math Template window.
    • Copy and Edit Template's Formula: Shows the formula in the Formula text-box and allow editing to be done.
    • Edit Template: Open the Edit a Math Template window to edit and update the selected template.
    • Clear Template: Remove formula from the Formula text-box.

Create a Math Template

  1. Name: The names of math templates have a few requirements due to their use in math formulas:
    • Names must be unique. This is case insensitive, so "tree" and "Tree" will be considered clashing names.
    • Names can only use alphanumeric characters. This means that they cannot contain spaces or special characters.
    • Names cannot start with a number.
    • Names cannot be one of the reserved words for maths formulas. e.g. You would not be able to name a math template "e", "pi", or "sin".
  2. Description: The description can be used to explain the functionality and inputs of your template to other users.
  3. Formula: The remainder of this document describes all the things you can do in the Formula. It's case-insensitive (i.e. upper- and lower-case letters are equivalent).

Select a Math Template

Use the Select a Math Template window to find and select existing templates to use.

  1. Search a template by the Name, Owner, Description and/or Label.
  2. Template table: Select the preferred template from the table.
  3. Content panel: Shows the content of the selected template.
  4. Click the Select button to add the selected template.

This window will close and the formula will be added to the formula text-box.

Export/Import Math Template

  1. In the Volume Maths process, scroll to the Formula section.
  2. Select the blue folder icon and click Select Template.
  3. To Export a template
    1. In the table, select the preferred template.
    2. Click the Export button.
    3. Browse the location and type a name.
    4. Click Save.
  4. To Import a template
    1. Click the Import button
    2. Select the *.maths file to import.
    3. Click Open.
    4. If an existing template name exist, please type a new name in the pop-up window.
  5. Click Cancel to close the window.

Variables

Variables may have a value at a particular location, or they may be missing/invalid. Missing/invalid values are represented by 'NaN' (Not A Number). Volume Maths provides its own set of variables that you may use in your formula. See the help of the Volume Maths process to see the variables available within it.

Defining your own variables

If you are re-using the same value multiple times, or if you wish to organise your equations, defining your own variables can be very useful (and sometimes even more efficient!).

Variable Syntax:

  • The variable name appears on the left, followed by an equals sign (=), your expression, and finally a semi-colon (;).
  • Example:
        a = v1 * 2;
        res = a + sin(v1);
  • The semi-colon is very important. It must be there to separate the declaration of each variable.

Operators

Operator     Description

    +              Addition

    -               Subtraction

    *               Multiplication

    /               Division

   %              Modulo: A%B yields the remainder of A divided by B

    ^              Power: A^B raises A to the power of B

In the absence of any operator, multiplication is assumed. Thus, 5 a will be interpreted as 5 * a. For other operators, see also the Conditions sections for boolean operations, and the Bitwise Operators section below.

Bitwise Operators

Maths operations support a number of bitwise operators. These can be useful for setting flags in headers, but these are not boolean operations. Some care must be taken:

  • Only the integer part of a value is considered
  • Operations consider which bits are set in that integer value

As a consequence, these differ from conditional operations (if, && etc) which treat any non-zero value as one (true)

a xor b    Exclusive OR: sets bits in the output only where they differ in the inputs, according to the following table.

a & b    Bitwise AND: performs a logical AND on each bit in the inputs, and sets the corresponding output bit to the result.

a | b    Bitwise OR: performs a logical OR on each bit in the inputs, and sets the corresponding output bit to the result.

~a    1's complement: performs a bitwise NOT, inverting all bits in the value. Note that the integer representation is signed, so this is equivalent to (-a - 1).

* Simplified to show an 8 bit representation

Conditions

You can build complex expressions that result in different behaviour in different situations. Conditional statements take the form:

if  ( condition ,  true statement ,  false statement )

where the true statement is used if the condition is true, or vice-versa. Note that those statements can themselves be arbitrary expressions, such as another conditional statement.

Standard comparison operators may be used, which may be combined with standard boolean operators. You can preface any boolean expression with a ! to negate it:

<, >, <=, >= Comparison: Less Than, Greater Than, Less Than or Equal, Greater Than or Equal
!=, == Equality: Not Equal, Equal
&&, ||, ! Boolean: AND, OR and NOT.
isnan(...), isnull(...), ismissing(...) These functions will evaluate to "true" if the expression is NaN (missing/undefined value), otherwise "false"
isdefined(...), isvalid(...) These functions are the opposite of isnan/isnull/ismissing -- they will evaluate to true if the value is defined at this location (not NaN)

Note: Do not use != nan or == nan to check for invalid values in conditional statements. It won't do what you think! Use one of the isnan(), isnull(), isvalid(), etc. functions instead.

Conditional Examples:

if (a < 0 && b < 0, c, a + b)
Click to copy

If both a and b are less than zero, use the value of c; otherwise use the sum of a and b


if (a < 0, if (b < 0, c, a + b), a + b)
Click to copy

A more-complicated way of writing the same thing, using one conditional inside another.


if (isnan(a) || isnan(b), 1500, a + b)
Click to copy

If either a or b are NaN (invalid), use 1500; otherwise use the sum of a and b

Combining / Merging

The most natural-seeming way to merge two things together would be to "add" them, as in a + b + c. But this doesn't work, for two reasons.

First, in places where they overlap, that would give you the arithmetic sum of the values at that location -- which is probably not what you intend.

Second, missing values are generally represented by the value NaN (not-a-number). NaN plus any other value remains NaN, so this would have the effect of producing output only at locations where all inputs are defined. This is also probably not what you intend.

To simplify this, Insight provides several functions to make combining easy:

  • merge(a, b, c, ...) - combines any number of items, in order of precedence. i.e. where they overlap, the first available value in the list will be used.
  • min(a, b, c, ...) - combines any number of items, using the smallest available value
  • max(a, b, c, ...) - combines any number of items, using the largest available value
  • sum(a, b, c, ...) - arithmetic sum of any number of items, ignoring those that aren't defined at a location
  • avg(a, b, c, ...) - arithmetic mean of any number of items, ignoring those that aren't defined at a location
  • random(max) - generate a random number between 0 and max (inclusive)
  • random(min, max) - generate a random number between min and max (inclusive)
  • lerp(v0, v1, t) - linearly interpolates between two inputs (v0, v1) for a parameter (t) in the closed unit interval [0, 1]. Parameter t is clamped between 0 and 1.

Math Functions

Trigonometric
sin(x), cos(x), tan(x) sine, cosine, tangent of a value in radians
asin(x), acos(x), atan(x) arc sine, arc cosine, and arc tangent
atan2(y,x) arc tangent - returns the angle whose tangent is y/x
sec(x), cosec(x), cot(x) secant, cosecant, co-tangent
sinh(x), cosh(x), tanh(x), asinh(x), acosh(x), atanh(x) hyperbolic sine, cosine & tangent; inverse hyperbolic sine cosine & tangent
Logarithmic
ln(x) Natural logarithm
log(x) Log base 10
lg(x) Log base 2
exp(x) Exponential (e^x)
pow(a, b) or a^b Raise a to the power of b
Rounding
rint(x) Statistically round to the nearest integer (0.5 will always round towards the nearest even integer)
round(x) Arithmetically round to the nearest integer (0.5 will always round towards positive infinity)
floor(x) Floor (round down to the next integer)
ceil(x) Ceiling (round up to the next integer)
Miscellaneous
abs(x) Absolute value
rnd() Random number between 0 and 1
random(min, max) A random number between min and max
mod(x,y) The remainder after x is divided by y; this can also be expressed as x % y
sqrt(x) Square root
signum(x) Sign of a number (-1, 0, or 1)
toDegrees(x) Convert an angle in radians to degrees
toRadians(x) Convert an angle in degrees to radians

Adding comments to your formulas

You can use comments to explain your formulas to other users.

Comment Syntax:

  • Comments are started by a hash #, and extend to the end of that line. Anything written within a comment will be ignored in the formula.
  • Example:     

    # Comments can appear on their own line.     
    a = v1 * 2;     
    res = a + sin(v1); # Or at the end of other lines.

Examples

pimp^2 - 2 * simp^2  
Click to copy

Will create a Lambda-Rho volume (pimp squared minus 2 times simp squared)


if (!IsNaN(a), a, b)  
Click to copy

Will use the value of volume "a" for a sample if it exists, otherwise the value of volume "b".


if (d_m < h1, 1500, v1)
Click to copy

Will make a copy of volume "v1", with any sample shallower than "h1" (a horizon) set to 1500.