7#include "InlineCalculator.h"
20#include <boost/foreach.hpp>
28#include "IException.h"
29#include "InlineInfixToPostfix.h"
31#include "NaifStatus.h"
110 tokenOps = tokenOps.simplified();
113 QString error =
"Errors parsing inline equation[" +
equation +
"].";
121 while ( !tokenList.isEmpty() ) {
122 QString token = tokenList.takeFirst();
123 if ( !token.isEmpty() ) {
149 error =
"Equation element (" + token +
") invalid - token not recognized.";
167 return (nerrors == 0);
188 QVector<double> value;
197 "Calculation with variable pool failed.",
225 QString msg =
"Too many operands in the equation [" +
m_equation +
"].";
256 if (
scalar.isEmpty())
return (
false);
312 if (variablePool->exists(key)) {
313 QVector<double> values = variablePool->value(key);
319 QString error =
"Could not find variable [" + key +
"] in variable pool.";
346 QVector<double> y =
Pop();
347 QVector<double> x =
Pop();
348 QVector<double> result;
361 QVector<double> degree =
Pop();
362 QVector<double> result;
363 BOOST_FOREACH(
double d, degree) {
364 result.push_back(d * rpd_c());
377 QVector<double> result;
378 BOOST_FOREACH(
double r,
radians) {
379 result.push_back(r * dpr_c());
394 QVector<double> inputA =
Pop();
395 QVector<double> inputB =
Pop();
396 QVector<double> results;
397 if ( inputA.size() != inputB.size() ) {
398 QString msg =
"Failed performing logical or operation, "
399 "input vectors are of differnet lengths.";
402 for (
int i = 0; i < inputA.size(); i++) {
403 results.push_back( inputA[i] || inputB[i] );
418 QVector<double> inputA =
Pop();
419 QVector<double> inputB =
Pop();
420 QVector<double> results;
421 if ( inputA.size() != inputB.size() ) {
422 QString msg =
"Failed performing logical and operation, "
423 "input vectors are of differnet lengths.";
426 for (
int i = 0; i < inputA.size(); i++) {
427 results.push_back( inputA[i] && inputB[i] );
481 m_fxPool.insert(function->name(), function);
484 QString msg =
"Function operator [" + function->name() +
485 "] exists! Cannot replace existing functions in the pool :-(";
541 "Request for nonexistent variable pool.",
569 FxPoolType::iterator fx =
m_fxPool.find(fxname);
570 if (
m_fxPool.end() == fx )
return NULL;
692 const int &index)
const {
693 QString mess =
"No implementation in Calculator variable pool to provide "
694 " value for variable [" + variable +
"].";
712 QString mess =
"No implementation in Calculator variable pool to add "
713 " value for variable [" + key +
"].";
769 return (QVariant(
m_name));
void LessThan()
Pop two elements off the stack and compare them to see where one is less than the other,...
void Cosecant()
Pops one element and push the cosecant.
void Arctangent2()
Pops two elements and push the arctangent.
void Modulus()
Pops two elements, mods them, then pushes the result on the stack.
void Tangent()
Pops one element and push the tangent.
int StackSize()
Returns the current stack size.
void Arctangent()
Pops one element and push the arctangent.
void Divide()
Pops two, divides them, then pushes the quotient on the stack.
void GreaterThanOrEqual()
Pop two elements off the stack and compare them to see where one is greater than or equal to the othe...
void Push(double scalar)
Push a scalar onto the stack.
void Cotangent()
Pops one element and push the cotangent.
void Add()
Pops two elements, adds them, then pushes the sum on the stack.
void Multiply()
Pops two elements, multiplies them, then pushes the product on the stack.
void Negative()
Pops an element, negates it, then pushes the result.
void RightShift()
Pop the top element, then perform a right shift with zero fill.
void Or()
Pop two elements, OR them, then push the result on the stack.
void Sine()
Pops one element and push the sine.
virtual void Clear()
Clear out the stack.
void PerformOperation(QVector< double > &results, QVector< double >::iterator arg1Start, QVector< double >::iterator arg1End, double operation(double))
Performs the mathematical operations on each argument.
void Arcsine()
Pops one element and push the arcsine.
void LeftShift()
Pop the top element, then perform a left shift with zero fill.
void LessThanOrEqual()
Pop two elements off the stack and compare them to see where one is less than or equal to the other,...
void NotEqual()
Pop two elements off the stack and compare them to see where one is not equal to the other,...
void GreaterThan()
Pop two elements off the stack and compare them to see where one is greater than the other,...
void Arccosine()
Pops one element and push the arccosine.
void Log10()
Pop an element, compute its base 10 log, then push the result on the stack.
void MaximumPixel()
Pop two elements, then push the maximum on a pixel by pixel basis back on the stack.
void Secant()
Pops one element and push the secant.
void Log()
Pop an element, compute its log, then push the result on the stack.
void Subtract()
Pops two elements, subtracts them, then pushes the difference on the stack.
void Equal()
Pop two elements off the stack and compare them to see where one is equal to the other,...
QVector< double > Pop(bool keepSpecials=false)
Pop an element off the stack.
void AbsoluteValue()
Pop an element, compute its absolute value, then push the result on the stack.
void MinimumPixel()
Pop two elements, then push the minimum on a pixel by pixel basis back on the stack.
void Exponent()
Pops two elements, computes the power then pushes the result on the stack The exponent has to be a sc...
void And()
Pop two elements, AND them, then push the result on the stack.
void SquareRoot()
Pop an element, compute its square root, then push the root on the stack.
void Cosine()
Pops one element and push the cosine.
This is a simple class to model a Calculator Variable Pool.
CalculatorVariablePool()
Constructs a CalculatorVariablePool object.
virtual void add(const QString &key, QVector< double > &values)
Add a parameter to the variable pool.
virtual QVector< double > value(const QString &variable, const int &index=0) const
Return vector of doubles for Calculator functions.
virtual bool exists(const QString &variable) const
Returns true so the real error can be reported.
~CalculatorVariablePool()
Destroys the CalculatorVariablePool object.
This is the parent class to the various function classes.
QString m_name
Name of function.
void operator()()
Executes the function.
virtual ~FxBinder()
Destroys the FxBinder object.
void execute()
Executes the function.
FxBinder(const QString &name)
Constructs a function binder given a name.
virtual void dispatch()=0
This method defines how to execute this function.
virtual QVariant args()
Accesses the arguments for this function.
QString name() const
The name assigned to this function binder.
@ Unknown
A type of error that cannot be classified as any of the other error types.
@ User
A type of error that could only have occurred due to a mistake on the user's part (e....
@ Programmer
This error is for when a programmer made an API call that was illegal.
QString convert(const QString &infix)
This method converts infix to postfix.
Provides a calculator for inline equations.
virtual QString toPostfix(const QString &equation) const
Converts the given string from infix to postfix format.
void degrees()
Pops the top vector off the current stack and converts from radians to degrees.
FxTypePtr find(const QString &fxname)
Gets a pointer to the function from the current pool that corresponds to the given function name.
void initialize()
Adds the recognized functions to the function pool.
QVector< double > evaluate()
Evaluate compiled equation with existing variable pool.
bool isVariable(const QString &str)
Determines whether the given string is a variable.
void pushVariables(CalculatorVariablePool *variablePool)
Push the given variable pool onto the current variable pool list.
QList< CalculatorVariablePool * > m_variablePoolList
The list of variable pool pointers.
CalculatorVariablePool * variables()
Accesses the last variable pool in the current pool list.
void logicalOr()
Pops the top two vectors off the current stack and performs a logical or on each pair.
void scalar(const QVariant &scalar)
Pushes the given value onto the stack as a scalar.
virtual bool orphanTokenHandler(const QString &token)
Default token handler if it is undefined during parsing/compilation.
void floatModulus()
Pops the top two vectors off the current stack and performs the floatModulusOperator() on the corresp...
void destruct()
Discard of all the function pool and class resources.
void pi()
Pushes the PI constant onto the current stack.
void logicalAnd()
Pops the top two vectors off the current stack and performs a logical and on each pair.
void eConstant()
Pushes the Euler constant (e) onto the current stack.
virtual ~InlineCalculator()
Destroys the InlineCalculator object.
void variable(const QVariant &variable)
Pushes the given value onto the stack as a variable.
int size() const
Accesses the number of functions, operators, variables, and scalars to be executed.
void popVariables()
Removes the last variable pool in the current variable pool list.
void radians()
Pops the top vector off the current stack and converts from degrees to radians.
QString m_equation
The equation to be evaluated.
InlineCalculator()
Constructs an InlineCalculator object by initializing the operator lookup list.
bool compile(const QString &equation)
Compiles the given equation for evaluation.
bool fxExists(const QString &fxname) const
Determines whether the given function name exists in the current function pool.
QString equation() const
Accesses the string representation of the current equation, in postfix format.
FxTypePtr addFunction(FxTypePtr function)
Adds a function to the function pool.
FxPoolType m_fxPool
The map between function names and equation lists.
FxEqList m_functions
The list of pointers to function equations for the calculator.
bool isScalar(const QString &scalar)
Determines whether the given string contains a scalar value (i.e.
A parser for converting equation strings to postfix.
This class is used to bind function names with corresponding InlineCalculator functions that do not t...
calcOp m_func
The InlineCalculator operator that takes no parameters.
InlineCalculator * m_calc
The InlineCalculator used to evaluate this function.
InlineVoidFx(const QString &name, calcOp function, InlineCalculator *calculator)
Constructs an InlineVoid function from the given name, InlineCalculator operator, and InlineCalculato...
void dispatch()
Calls the function corresponding to this object using its stored InlineCalculator and InlineCalculato...
virtual ~InlineVoidFx()
Destroys the InlineVoidFx object.
This class is used to bind function names with corresponding Calculator functions that take a paramet...
InlineCalculator * m_calc
The InlineCalculator used to evaluate this function.
virtual ~ParameterFx()
Destroys the ParameterFx object.
calcOp m_func
The InlineCalculator operator that takes parameters.
void dispatch()
Calls the function corresponding to this object using its stored InlineCalculator,...
ParameterFx(const QString &name, calcOp function, InlineCalculator *calculator)
Constructs a Parameter function from the given name (containing the appropriate parameters),...
This class is used to bind function names with corresponding Calculator functions that do not take pa...
InlineCalculator * m_calc
The Calculator used to evaluate this function.
virtual ~VoidFx()
Destroys the VoidFx object.
void dispatch()
Calls the function corresponding to this object using its stored Calculator and Calculator operator.
calcOp m_func
The Calculator operator that takes no parameters.
VoidFx(const QString &name, calcOp function, InlineCalculator *calculator)
Constructs a Void function from the given name, Calculator operator, and Calculator.
const double E
Sets some basic constants for use in ISIS programming.
This is free and unencumbered software released into the public domain.
double floatModulusOperator(double a, double b)
Determines the remainder of the quotient a/b whose sign is the same as that of a.
double toDouble(const QString &string)
Global function to convert from a string to a double.
Namespace for the standard library.