|
Medical Imaging Interaction Toolkit
2026.06.00
Medical Imaging Interaction Toolkit
|
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... | |
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:
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.
Definition at line 63 of file mitkFormulaParser.h.
| using mitk::FormulaParser::ValueType = double |
Definition at line 66 of file mitkFormulaParser.h.
| using mitk::FormulaParser::VariableMapType = std::map<std::string, ValueType> |
Definition at line 67 of file mitkFormulaParser.h.
| mitk::FormulaParser::FormulaParser | ( | const VariableMapType * | variables | ) |
Constructs the FormulaParser and initializes the variables.
| [in] | variables | A map of variables with values. The map is delivered as a pointer so you can still change it from outside this class. |
| ValueType mitk::FormulaParser::lookupVariable | ( | const std::string | var | ) |
Looks up the associated value of the given variable name.
| [in] | var | The name of the variable whose value is to be returned. |
| FormulaParserException | If the variable map is empty or the variable name cannot be found. |
| ValueType mitk::FormulaParser::parse | ( | const std::string & | input | ) |
Evaluates the input string and returns the resulting value.
| [in] | input | The string to be evaluated. |
| FormulaParserException | If the parser encounters an unexpected character, a variable cannot be found in the look-up table, or the grammar cannot be applied. |