Medical Imaging Interaction Toolkit  2018.4.99-3e3f1a6e
Medical Imaging Interaction Toolkit
mitk::FormulaParser Class Reference

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...
 

Detailed Description

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:

  • 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 everytime a variable's value changes since that is not done automatically.

Author
Sascha Diatschuk

Definition at line 64 of file mitkFormulaParser.h.

Member Typedef Documentation

◆ ValueType

Definition at line 67 of file mitkFormulaParser.h.

◆ VariableMapType

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

Definition at line 68 of file mitkFormulaParser.h.

Constructor & Destructor Documentation

◆ FormulaParser()

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

Construct the FormulaParser and initialized the variables with variables.

Parameters
[in]variablesA 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.

Definition at line 241 of file mitkFormulaParser.cpp.

Member Function Documentation

◆ lookupVariable()

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

Looks up the associated value of the given string var in the variables map.

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 given variable name cannot be found.

Definition at line 273 of file mitkFormulaParser.cpp.

References mitkThrowException.

Referenced by mitk::fresnelC().

◆ parse()

FormulaParser::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 comes across an unexpected character,
  • a variable in the input string could not be found in the look-up table or
  • the parser cannot apply the grammar to the string at all.

Definition at line 244 of file mitkFormulaParser.cpp.

References mitkThrowException.

Referenced by CalcSignalFromFunction(), and mitk::GenericParamModel::ComputeModelfunction().


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