Medical Imaging Interaction Toolkit  2026.06.00
Medical Imaging Interaction Toolkit
mitk::FormulaParser Class Reference

Evaluates simple mathematical formula strings (e.g. "3.5 + 4 * x * sin(x) - 1 / 2"). More...

#include <mitkFormulaParser.h>

Public Types

using ValueType = double
 
using VariableMapType = std::map< std::string, ValueType >
 

Public Member Functions

 FormulaParser (const VariableMapType *variables)
 Constructs the FormulaParser and initializes the variables. More...
 
ValueType parse (const std::string &input)
 Evaluates the input string and returns the resulting value. More...
 
ValueType lookupVariable (const std::string var)
 Looks up the associated value of the given variable name. More...
 

Detailed Description

Evaluates simple mathematical formula strings (e.g. "3.5 + 4 * x * sin(x) - 1 / 2").

Internally it utilizes the boost::spirit framework with the Grammar structure to parse the input string into a valid result number. Function strings (e.g. "sin", "tan" or "abs") are translated to actual function calls and variables (e.g. "x", "myVariable", "amount_") are replaced by their currently assigned value via a look-up table.

The parser is able to recognize:

  • sums, differences, products and divisions (a + b, 4 - 3, 2 * x, 9 / 3)
  • algebraic signs (+5, -5)
  • exponentiation (2 ^ 4)
  • parentheses (3 * (4 + 2))
  • variables (x, myVar, test2_var_)
  • the following unary functions: abs, exp, sin, cos, tan, sind (sine in degrees), cosd (cosine in degrees), tand (tangent in degrees)

In order to use the FormulaParser you just have to initialize it with a map of variables (i.e. a look-up table where "x" is assigned to 5 for example and "y" is assigned to 13 and so on) and then call the FormulaParser::parse function with the string that should be evaluated. Be sure to update the look-up table every time a variable's value changes since that is not done automatically.

See also
GenericParamModel

Definition at line 63 of file mitkFormulaParser.h.

Member Typedef Documentation

◆ ValueType

Definition at line 66 of file mitkFormulaParser.h.

◆ VariableMapType

using mitk::FormulaParser::VariableMapType = std::map<std::string, ValueType>

Definition at line 67 of file mitkFormulaParser.h.

Constructor & Destructor Documentation

◆ FormulaParser()

mitk::FormulaParser::FormulaParser ( const VariableMapType *  variables)

Constructs the FormulaParser and initializes the variables.

Parameters
[in]variablesA map of variables with values. The map is delivered as a pointer so you can still change it from outside this class.

Member Function Documentation

◆ lookupVariable()

ValueType mitk::FormulaParser::lookupVariable ( const std::string  var)

Looks up the associated value of the given variable name.

Parameters
[in]varThe name of the variable whose value is to be returned.
Returns
The associated value of the given variable name.
Exceptions
FormulaParserExceptionIf the variable map is empty or the variable name cannot be found.

◆ parse()

ValueType mitk::FormulaParser::parse ( const std::string &  input)

Evaluates the input string and returns the resulting value.

Parameters
[in]inputThe string to be evaluated.
Returns
The number that results from the evaluated string.
Exceptions
FormulaParserExceptionIf the parser encounters an unexpected character, a variable cannot be found in the look-up table, or the grammar cannot be applied.

The documentation for this class was generated from the following file: