Medical Imaging Interaction Toolkit
2024.06.99-60d9b802
Medical Imaging Interaction Toolkit
|
This class offers the functionality to evaluate 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) | |
Construct the FormulaParser and initialized the variables with 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 string var in the variables map. More... | |
This class offers the functionality to evaluate 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:
a + b
, 4 - 3
, 2 * x
, 9 / 3
) +5
, -5
) 2 ^ 4
) 3 * (4 + 2)
) x
, myVar
, test2_var_
) 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 everytime a variable's value changes since that is not done automatically.
Definition at line 64 of file mitkFormulaParser.h.
using mitk::FormulaParser::ValueType = double |
Definition at line 67 of file mitkFormulaParser.h.
using mitk::FormulaParser::VariableMapType = std::map<std::string, ValueType> |
Definition at line 68 of file mitkFormulaParser.h.
mitk::FormulaParser::FormulaParser | ( | const VariableMapType * | variables | ) |
Construct the FormulaParser and initialized the variables with variables.
[in] | variables | A map of variables with values that will be assigned to the corresponding member variable. 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 string var in the variables map.
[in] | var | The name of the variable whose value is to be returned. |
FormulaParserException | If the variable map is empty or the given 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
|