Module: Arithmetic ()
The Arithmetic module performs calculations on up to three input data objects according to a user-defined arithmetic expression. The result is stored in a new data object called Result. The calculations are triggered by the Apply button. The arithmetic expression is evaluated either on the grid of the first data object (in case of regular, tetrahedral, or hexahedral grids or surfaces) or on a regular 3D uniform grid for which the number of points can be set by the Resolution port.The module is able to process input fields with up to 6 different channels. Scalar input fields are referenced by the variables A, B, and C. The values of multi-component input fields are referenced for example by Ax, Ay, Az (if input A is a vector field) or Br, Bg, Bb, Ba (if input B is an RGBA color field).
In any case the expressions are evaluated per point, i.e., the result for a point (X,Y,Z) depends on input values at the same point only. If the resulting object is based on a regular grid, grid indices I, J, or K may also appear in the arithmetic expression. This means that for each grid point its associated I, J, or K index value will be substituted in the arithmetic expression on evaluation. This is useful in connection with comparison operators which produce a result of either zero or one. Computations may be confined to a specific sub-grid this way.
An expression consists of variables and mathematical and logical operators. The syntax is basically the same as for C expressions. The following variables are defined:
- A: The values of a scalar field at input A.
- B: The values of a scalar field at input B.
- C: The values of a scalar field at input C.
- Ar: The real part of a complex scalar field at input A.
- Ai: The imaginary part of a complex scalar field at input A.
- Ax: The x-component of a vector field at input A.
- Ay: The y-component of a vector field at input A.
- Az: The z-component of a vector field at input A.
- Ar: The red component of a color field at input A.
- Ag: The green component of a color field at input A.
- Ab: The blue component of a color field at input A.
- Aa: The alpha component of a color field at input A.
- Arx: Real part of the x-component of a complex vector field.
- Aix: Imaginary part of the x-component of a complex vector field.
- Ary: Real part of the y-component of a complex vector field.
- Aiy: Imaginary part of the y-component of a complex vector field.
- Arz: Real part of the z-component of a complex vector field.
- Aiz: Imaginary part of the z-component of a complex vector field.
- Aii: Index ii of a symmetric second order tensor.
- Aij: Index ij of a symmetric second order tensor.
- Aik: Index ik of a symmetric second order tensor.
- Ajj: Index jj of a symmetric second order tensor.
- Ajk: Index jk of a symmetric second order tensor.
- Akk: Index kk of a symmetric second order tensor.
- The same variables as above for fields at input B or C, but with A replaced by B or C.
- I: First index of a point (i,j,k) in a regular grid, or index of a point in an unstructured grid.
- J: Second index of a point (i,j,k) in a regular grid, undefined for unstructured grids.
- K: Third index of a point (i,j,k) in a regular grid, undefined for unstructured grids.
- X: The x-coordinate of the current point.
- Y: The y-coordinate of the current point.
- Z: The z-coordinate of the current point.
- R: The radius .
This is the list of available mathematical and logical operators:
- + - / * : The basic mathematical operators.
- ! : Unary negation.
- - : Unary minus.
- % : The modulo operator.
- > < <= >= != == : The comparison operators greater, less, less or equal, greater or equal, not equal, and equal. If the comparison is true the result is 1, otherwise it is 0.
- && || : The logical operators and and or. A non-zero operand is interpreted as true, while a zero operand is false. The result is either 1 (true) or 0 (false).
- & | ^ : The bitwise operations and, or, and xor. For bitwise and, the bits in the result are set to 1 if the corresponding bits in the two operands are both 1. For bitwise or, the bits in the result are set to 1 if at least one of the corresponding bits in the two operands is 1. For bitwise xor, the bits in the result are set to 1 if at exactly one of the corresponding bits in the two operands is 1.
There are also some built-in functions:
- pow(x,a) : Power function. Note that there is no power operator (the ^ operator exists but means bitwise xor).
- sin(x) cos(x) tan(x) : Trigonometric functions.
- asin(x) acos(x) atan(x) atan2(x,y) : Inverse trigonometric functions.
- sinh(x) cosh(x) tanh(x) : Hyperbolic trigonometric functions.
- asinh(x) acosh(x) atanh(x) : Hyperbolic inverse trigonometric functions.
- sqrt(x) : Square root.
- floor(x) ceil(x) : Next largest or smallest integer number.
- ln(x) log10(x) exp(x) : Logarithm and exponent.
- rand() : Pseudo-random variable uniformly distributed between 0 and 1.
- gauss() : Pseudo-random variable with Gaussian distribution (mean value 0, standard deviation 1).
- erf(x) erfc(x) : Error function.
- abs(x) fabs(x) : Absolute value.
- min(x,y) max(x,y) : Minimum or maximum values.
For better understanding some examples of how to use the Arithmetic module follow:
Expression: A
The result is a copy of input object A. If A is defined on a tetrahedral or hexahedral grid and the result type is set to regular together with an appropriate resolution, the expression leads to a conversion from an unstructured grid to a structured regular grid. The same trick can also be used to resample a regular field with stacked coordinates onto a uniform grid.Expression: A-B
The result is the difference between input objects A and B. This expression is sometimes useful in order to compare two different data sets.Expression: 255*(A>127)
Simple thresholding: For every value of A the result is set to 255 if the value is greater than 127. Otherwise the result is 0.Expression: A*(B>0)
Simple masking operation: If B is zero, the result is also set to zero. Otherwise, the result is set to A. For example, if A is a 3D image and B is a corresponding label image, the exterior parts of the object (where B is zero) are masked out by this expression.
InputA [required]
The input may either be a 3D data field with an arbitrary number of channels or a tetrahedral or hexahedral grid. The primitive data type of the result will be the same as this input, e.g., if input A contains bytes the result will also contain bytes.The input may also be a surface field or a surface itself. For example, you may want to capture the values of a 3D data field at all points of a surface in a surface field. However, note that data on surfaces can only be used as input if the output is defined on the same surface. Trying to evaluate a surface field for points of a 3D grid or for points of some other surface is not possible, even if these points lie exactly on the source surface.
InputB [optional]
Second input, can be any 3D data field or surface field.InputC [optional]
Third input, can be any 3D data field or surface field.
Result Type
With this radio box the grid type of the result can be set either to either the same as input A or to a regular grid with uniform coordinates.Result Location
Specify the location of the result. Produces either a value for every vertex of the grid (on Nodes), or for every grid cell (on Cell Center). This port is available if the first input connection is of type Surface, Tetra Grid or Hexa Grid.Resolution
If port Result Type is set to regular, the resolution of the regular field to be generated is set to the values given here.Min Box
Port to set the minimum x-, y-, z-coordinates of a bounding box around the output data object. If one or more input data objects are connected, the bounding box of the first input data object is also taken as the output bounding box.Max Box
Port to set maximum x-, y-, z-coordinates of the bounding box around the output data object.Options
Ignore errors: Tell the Arithmetic module to ignore errors. For example, this can be useful to force the computation of divisions if the data contains zeros.
Result Channels
Result Channels
This port determines the number of channels of the result. By default, the result has the same number of channels as input A. Alternatively, you can specify that the result should have 2 channels (complex scalar field), 3 channels (vector field), 4 channels (RGBA color field), or 6 channels (either a complex vector or a symmetric tensor field of second order).Expression
This port specifies the mathematical expression used to compute the result. If the result has more than one channel, more expression ports will be shown.