VTK
vtkFunctionParser.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkFunctionParser.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
49 #ifndef vtkFunctionParser_h
50 #define vtkFunctionParser_h
51 
52 #include "vtkCommonMiscModule.h" // For export macro
53 #include "vtkObject.h"
54 #include "vtkTuple.h" // needed for vtkTuple
55 #include <vector> // needed for vector
56 #include <string> // needed for string.
57 
58 #define VTK_PARSER_IMMEDIATE 1
59 #define VTK_PARSER_UNARY_MINUS 2
60 #define VTK_PARSER_UNARY_PLUS 3
61 
62 // supported math functions
63 #define VTK_PARSER_ADD 4
64 #define VTK_PARSER_SUBTRACT 5
65 #define VTK_PARSER_MULTIPLY 6
66 #define VTK_PARSER_DIVIDE 7
67 #define VTK_PARSER_POWER 8
68 #define VTK_PARSER_ABSOLUTE_VALUE 9
69 #define VTK_PARSER_EXPONENT 10
70 #define VTK_PARSER_CEILING 11
71 #define VTK_PARSER_FLOOR 12
72 #define VTK_PARSER_LOGARITHM 13
73 #define VTK_PARSER_LOGARITHME 14
74 #define VTK_PARSER_LOGARITHM10 15
75 #define VTK_PARSER_SQUARE_ROOT 16
76 #define VTK_PARSER_SINE 17
77 #define VTK_PARSER_COSINE 18
78 #define VTK_PARSER_TANGENT 19
79 #define VTK_PARSER_ARCSINE 20
80 #define VTK_PARSER_ARCCOSINE 21
81 #define VTK_PARSER_ARCTANGENT 22
82 #define VTK_PARSER_HYPERBOLIC_SINE 23
83 #define VTK_PARSER_HYPERBOLIC_COSINE 24
84 #define VTK_PARSER_HYPERBOLIC_TANGENT 25
85 #define VTK_PARSER_MIN 26
86 #define VTK_PARSER_MAX 27
87 #define VTK_PARSER_SIGN 29
88 
89 // functions involving vectors
90 #define VTK_PARSER_CROSS 28
91 #define VTK_PARSER_VECTOR_UNARY_MINUS 30
92 #define VTK_PARSER_VECTOR_UNARY_PLUS 31
93 #define VTK_PARSER_DOT_PRODUCT 32
94 #define VTK_PARSER_VECTOR_ADD 33
95 #define VTK_PARSER_VECTOR_SUBTRACT 34
96 #define VTK_PARSER_SCALAR_TIMES_VECTOR 35
97 #define VTK_PARSER_VECTOR_TIMES_SCALAR 36
98 #define VTK_PARSER_VECTOR_OVER_SCALAR 37
99 #define VTK_PARSER_MAGNITUDE 38
100 #define VTK_PARSER_NORMALIZE 39
101 
102 // constants involving vectors
103 #define VTK_PARSER_IHAT 40
104 #define VTK_PARSER_JHAT 41
105 #define VTK_PARSER_KHAT 42
106 
107 // code for if(bool, trueval, falseval) resulting in a scalar
108 #define VTK_PARSER_IF 43
109 
110 // code for if(bool, truevec, falsevec) resulting in a vector
111 #define VTK_PARSER_VECTOR_IF 44
112 
113 // codes for boolean expressions
114 #define VTK_PARSER_LESS_THAN 45
115 
116 // codes for boolean expressions
117 #define VTK_PARSER_GREATER_THAN 46
118 
119 // codes for boolean expressions
120 #define VTK_PARSER_EQUAL_TO 47
121 
122 // codes for boolean expressions
123 #define VTK_PARSER_AND 48
124 
125 // codes for boolean expressions
126 #define VTK_PARSER_OR 49
127 
128 // codes for scalar variables come before those for vectors. Do not define
129 // values for VTK_PARSER_BEGIN_VARIABLES+1, VTK_PARSER_BEGIN_VARIABLES+2, ...,
130 // because they are used to look up variables numbered 1, 2, ...
131 #define VTK_PARSER_BEGIN_VARIABLES 50
132 
133 // the value that is returned as a result if there is an error
134 #define VTK_PARSER_ERROR_RESULT VTK_FLOAT_MAX
135 
136 class VTKCOMMONMISC_EXPORT vtkFunctionParser : public vtkObject
137 {
138 public:
140  vtkTypeMacro(vtkFunctionParser, vtkObject);
141  void PrintSelf(ostream& os, vtkIndent indent) override;
142 
146  vtkMTimeType GetMTime() override;
147 
149 
152  void SetFunction(const char *function);
153  vtkGetStringMacro(Function);
155 
161 
167 
171  double GetScalarResult();
172 
174 
178  void GetVectorResult(double result[3]) {
179  double *r = this->GetVectorResult();
180  result[0] = r[0]; result[1] = r[1]; result[2] = r[2]; };
182 
184 
190  void SetScalarVariableValue(const char* variableName, double value);
191  void SetScalarVariableValue(int i, double value);
193 
195 
198  double GetScalarVariableValue(const char* variableName);
199  double GetScalarVariableValue(int i);
201 
203 
209  void SetVectorVariableValue(const char* variableName, double xValue,
210  double yValue, double zValue);
211  void SetVectorVariableValue(const char* variableName,
212  const double values[3]) {
213  this->SetVectorVariableValue(variableName,values[0],values[1],values[2]);};
214  void SetVectorVariableValue(int i, double xValue, double yValue,
215  double zValue);
216  void SetVectorVariableValue(int i, const double values[3]) {
217  this->SetVectorVariableValue(i,values[0],values[1],values[2]);};
219 
221 
224  double* GetVectorVariableValue(const char* variableName) VTK_SIZEHINT(3);
225  void GetVectorVariableValue(const char* variableName, double value[3]) {
226  double *r = this->GetVectorVariableValue(variableName);
227  value[0] = r[0]; value[1] = r[1]; value[2] = r[2]; };
229  void GetVectorVariableValue(int i, double value[3]) {
230  double *r = this->GetVectorVariableValue(i);
231  value[0] = r[0]; value[1] = r[1]; value[2] = r[2]; };
233 
238  { return static_cast<int>(this->ScalarVariableNames.size()); }
239 
243  int GetScalarVariableIndex(const char *name);
244 
249  { return static_cast<int>(this->VectorVariableNames.size()); }
250 
254  int GetVectorVariableIndex(const char *name);
255 
259  const char* GetScalarVariableName(int i);
260 
264  const char* GetVectorVariableName(int i);
265 
267 
273  bool GetScalarVariableNeeded(const char* variableName);
275 
277 
283  bool GetVectorVariableNeeded(const char* variableName);
285 
290 
295 
300 
302 
308  vtkSetMacro(ReplaceInvalidValues,vtkTypeBool);
309  vtkGetMacro(ReplaceInvalidValues,vtkTypeBool);
310  vtkBooleanMacro(ReplaceInvalidValues,vtkTypeBool);
311  vtkSetMacro(ReplacementValue,double);
312  vtkGetMacro(ReplacementValue,double);
314 
318  void CheckExpression(int &pos, char **error);
319 
324 
325 protected:
327  ~vtkFunctionParser() override;
328 
329  int Parse();
330 
334  bool Evaluate();
335 
336  int CheckSyntax();
337 
338  void CopyParseError(int &position, char **error);
339 
340  void RemoveSpaces();
341  char* RemoveSpacesFrom(const char* variableName);
343 
345  void BuildInternalSubstringStructure(int beginIndex, int endIndex);
346  void AddInternalByte(unsigned char newByte);
347 
348  int IsSubstringCompletelyEnclosed(int beginIndex, int endIndex);
349  int FindEndOfMathFunction(int beginIndex);
350  int FindEndOfMathConstant(int beginIndex);
351 
352  int IsVariableName(int currentIndex);
353  int IsElementaryOperator(int op);
354 
355  int GetMathFunctionNumber(int currentIndex);
357  int GetMathFunctionStringLength(int mathFunctionNumber);
358  int GetMathConstantNumber(int currentIndex);
359  int GetMathConstantStringLength(int mathConstantNumber);
360  unsigned char GetElementaryOperatorNumber(char op);
361  unsigned int GetOperandNumber(int currentIndex);
362  int GetVariableNameLength(int variableNumber);
363 
365 
371 
372  vtkSetStringMacro(ParseError);
373 
374  int FindPositionInOriginalFunction(const int& pos);
375 
376  char* Function;
378 
380  std::vector<std::string> ScalarVariableNames;
381  std::vector<std::string> VectorVariableNames;
382  std::vector<double> ScalarVariableValues;
383  std::vector<vtkTuple<double, 3> > VectorVariableValues;
384  std::vector<bool> ScalarVariableNeeded;
385  std::vector<bool> VectorVariableNeeded;
386 
387  unsigned char *ByteCode;
389  double *Immediates;
391  double *Stack;
394 
400 
403 
405  char* ParseError;
406 
407 private:
408  vtkFunctionParser(const vtkFunctionParser&) = delete;
409  void operator=(const vtkFunctionParser&) = delete;
410 };
411 
412 #endif
vtkFunctionParser::GetMathConstantNumber
int GetMathConstantNumber(int currentIndex)
vtkFunctionParser::GetScalarVariableValue
double GetScalarVariableValue(int i)
vtkFunctionParser::ParseErrorPositon
int ParseErrorPositon
Definition: vtkFunctionParser.h:404
vtkFunctionParser::GetVectorVariableNeeded
bool GetVectorVariableNeeded(const char *variableName)
vtkFunctionParser::GetMathFunctionStringLength
int GetMathFunctionStringLength(int mathFunctionNumber)
vtkFunctionParser::GetVectorResult
double * GetVectorResult()
Get a vector result from evaluating the input function.
vtkFunctionParser::IsVectorResult
int IsVectorResult()
Check whether the result is a vector result.
vtkFunctionParser::DisambiguateOperators
int DisambiguateOperators()
vtkFunctionParser::GetVectorVariableName
const char * GetVectorVariableName(int i)
Get the ith vector variable name.
vtkX3D::value
@ value
Definition: vtkX3D.h:220
vtkFunctionParser::RemoveSpaces
void RemoveSpaces()
vtkFunctionParser::IsScalarResult
int IsScalarResult()
Check whether the result is a scalar result.
vtkFunctionParser::GetMathConstantStringLength
int GetMathConstantStringLength(int mathConstantNumber)
vtkFunctionParser::SetVectorVariableValue
void SetVectorVariableValue(int i, const double values[3])
Definition: vtkFunctionParser.h:216
vtkFunctionParser::IsElementaryOperator
int IsElementaryOperator(int op)
vtkFunctionParser::vtkFunctionParser
vtkFunctionParser()
vtkTimeStamp
record modification and/or execution time
Definition: vtkTimeStamp.h:36
vtkFunctionParser::Evaluate
bool Evaluate()
Evaluate the function, returning true on success, false on failure.
vtkFunctionParser::GetMathFunctionNumberByCheckingParenthesis
int GetMathFunctionNumberByCheckingParenthesis(int currentIndex)
vtkFunctionParser::InvalidateFunction
void InvalidateFunction()
Allow the user to force the function to be re-parsed.
vtkFunctionParser::FunctionLength
int FunctionLength
Definition: vtkFunctionParser.h:379
vtkObject
abstract base class for most VTK objects
Definition: vtkObject.h:60
vtkFunctionParser::SetVectorVariableValue
void SetVectorVariableValue(int i, double xValue, double yValue, double zValue)
vtkFunctionParser::UpdateNeededVariables
void UpdateNeededVariables()
Collects meta-data about which variables are needed by the current function.
vtkFunctionParser::GetScalarVariableNeeded
bool GetScalarVariableNeeded(int i)
Returns whether a scalar variable is needed for the function evaluation.
vtkFunctionParser::GetScalarVariableIndex
int GetScalarVariableIndex(const char *name)
Get scalar variable index or -1 if not found.
vtkFunctionParser::GetOperandNumber
unsigned int GetOperandNumber(int currentIndex)
vtkFunctionParser::RemoveScalarVariables
void RemoveScalarVariables()
Remove all the scalar variables.
vtkFunctionParser::FunctionMTime
vtkTimeStamp FunctionMTime
Definition: vtkFunctionParser.h:395
vtkFunctionParser::ScalarVariableValues
std::vector< double > ScalarVariableValues
Definition: vtkFunctionParser.h:382
vtkFunctionParser::IsVariableName
int IsVariableName(int currentIndex)
vtkFunctionParser::StackSize
int StackSize
Definition: vtkFunctionParser.h:392
vtkFunctionParser::SetScalarVariableValue
void SetScalarVariableValue(int i, double value)
vtkFunctionParser::New
static vtkFunctionParser * New()
vtkFunctionParser::GetScalarVariableNeeded
bool GetScalarVariableNeeded(const char *variableName)
vtkFunctionParser::RemoveSpacesFrom
char * RemoveSpacesFrom(const char *variableName)
vtkFunctionParser::GetVariableNameLength
int GetVariableNameLength(int variableNumber)
vtkFunctionParser::ParseError
char * ParseError
Definition: vtkFunctionParser.h:405
vtkFunctionParser::ImmediatesSize
int ImmediatesSize
Definition: vtkFunctionParser.h:390
vtkX3D::position
@ position
Definition: vtkX3D.h:261
vtkFunctionParser::VectorVariableNeeded
std::vector< bool > VectorVariableNeeded
Definition: vtkFunctionParser.h:385
vtkFunctionParser::SetVectorVariableValue
void SetVectorVariableValue(const char *variableName, const double values[3])
Definition: vtkFunctionParser.h:211
VTK_SIZEHINT
#define VTK_SIZEHINT(...)
Definition: vtkWrappingHints.h:42
vtkFunctionParser::GetVectorVariableValue
void GetVectorVariableValue(const char *variableName, double value[3])
Definition: vtkFunctionParser.h:225
vtkFunctionParser::OperatorWithinVariable
int OperatorWithinVariable(int idx)
vtkFunctionParser::VectorVariableNames
std::vector< std::string > VectorVariableNames
Definition: vtkFunctionParser.h:381
vtkFunctionParser::ByteCodeSize
int ByteCodeSize
Definition: vtkFunctionParser.h:388
vtkFunctionParser::CheckExpression
void CheckExpression(int &pos, char **error)
Check the validity of the function expression.
vtkFunctionParser::EvaluateMTime
vtkTimeStamp EvaluateMTime
Definition: vtkFunctionParser.h:398
vtkFunctionParser::StackPointer
int StackPointer
Definition: vtkFunctionParser.h:393
vtkFunctionParser::GetVectorVariableIndex
int GetVectorVariableIndex(const char *name)
Get scalar variable index or -1 if not found.
vtkIndent
a simple class to control print indentation
Definition: vtkIndent.h:40
vtkFunctionParser::SetScalarVariableValue
void SetScalarVariableValue(const char *variableName, double value)
Set the value of a scalar variable.
vtkFunctionParser::GetElementaryOperatorNumber
unsigned char GetElementaryOperatorNumber(char op)
vtkFunctionParser::BuildInternalSubstringStructure
void BuildInternalSubstringStructure(int beginIndex, int endIndex)
vtkFunctionParser::ScalarVariableNames
std::vector< std::string > ScalarVariableNames
Definition: vtkFunctionParser.h:380
vtkFunctionParser::Stack
double * Stack
Definition: vtkFunctionParser.h:391
vtkFunctionParser::GetVectorVariableNeeded
bool GetVectorVariableNeeded(int i)
Returns whether a vector variable is needed for the function evaluation.
vtkFunctionParser::GetScalarResult
double GetScalarResult()
Get a scalar result from evaluating the input function.
vtkFunctionParser::GetScalarVariableValue
double GetScalarVariableValue(const char *variableName)
Get the value of a scalar variable.
vtkX3D::name
@ name
Definition: vtkX3D.h:219
vtkObject.h
vtkFunctionParser::FindPositionInOriginalFunction
int FindPositionInOriginalFunction(const int &pos)
vtkFunctionParser::ReplacementValue
double ReplacementValue
Definition: vtkFunctionParser.h:402
vtkFunctionParser::VectorVariableValues
std::vector< vtkTuple< double, 3 > > VectorVariableValues
Definition: vtkFunctionParser.h:383
vtkFunctionParser::GetMTime
vtkMTimeType GetMTime() override
Return parser's MTime.
vtkFunctionParser::FindEndOfMathConstant
int FindEndOfMathConstant(int beginIndex)
vtkFunctionParser::FunctionWithSpaces
char * FunctionWithSpaces
Definition: vtkFunctionParser.h:377
vtkTuple.h
vtkFunctionParser::CheckSyntax
int CheckSyntax()
vtkFunctionParser::~vtkFunctionParser
~vtkFunctionParser() override
vtkFunctionParser::GetVectorVariableValue
void GetVectorVariableValue(int i, double value[3])
Definition: vtkFunctionParser.h:229
vtkFunctionParser::GetVectorVariableValue
double * GetVectorVariableValue(int i)
vtkFunctionParser::FindEndOfMathFunction
int FindEndOfMathFunction(int beginIndex)
vtkFunctionParser
Parse and evaluate a mathematical expression.
Definition: vtkFunctionParser.h:137
vtkFunctionParser::ParseMTime
vtkTimeStamp ParseMTime
Definition: vtkFunctionParser.h:396
vtkFunctionParser::GetScalarVariableName
const char * GetScalarVariableName(int i)
Get the ith scalar variable name.
vtkFunctionParser::Parse
int Parse()
vtkFunctionParser::AddInternalByte
void AddInternalByte(unsigned char newByte)
vtkFunctionParser::CopyParseError
void CopyParseError(int &position, char **error)
vtkFunctionParser::RemoveVectorVariables
void RemoveVectorVariables()
Remove all the vector variables.
vtkFunctionParser::ByteCode
unsigned char * ByteCode
Definition: vtkFunctionParser.h:387
vtkFunctionParser::VariableMTime
vtkTimeStamp VariableMTime
Definition: vtkFunctionParser.h:397
vtkFunctionParser::GetNumberOfScalarVariables
int GetNumberOfScalarVariables()
Get the number of scalar variables.
Definition: vtkFunctionParser.h:237
vtkFunctionParser::ReplaceInvalidValues
vtkTypeBool ReplaceInvalidValues
Definition: vtkFunctionParser.h:401
vtkFunctionParser::Immediates
double * Immediates
Definition: vtkFunctionParser.h:389
vtkFunctionParser::PrintSelf
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
vtkFunctionParser::GetNumberOfVectorVariables
int GetNumberOfVectorVariables()
Get the number of vector variables.
Definition: vtkFunctionParser.h:248
vtkFunctionParser::SetFunction
void SetFunction(const char *function)
Set/Get input string to evaluate.
vtkFunctionParser::SetVectorVariableValue
void SetVectorVariableValue(const char *variableName, double xValue, double yValue, double zValue)
Set the value of a vector variable.
vtkFunctionParser::GetVectorVariableValue
double * GetVectorVariableValue(const char *variableName)
Get the value of a vector variable.
vtkFunctionParser::ScalarVariableNeeded
std::vector< bool > ScalarVariableNeeded
Definition: vtkFunctionParser.h:384
vtkFunctionParser::BuildInternalFunctionStructure
int BuildInternalFunctionStructure()
vtkTypeBool
int vtkTypeBool
Definition: vtkABI.h:69
vtkFunctionParser::Function
char * Function
Definition: vtkFunctionParser.h:376
vtkFunctionParser::IsSubstringCompletelyEnclosed
int IsSubstringCompletelyEnclosed(int beginIndex, int endIndex)
vtkFunctionParser::GetMathFunctionNumber
int GetMathFunctionNumber(int currentIndex)
vtkFunctionParser::RemoveAllVariables
void RemoveAllVariables()
Remove all the current variables.
vtkMTimeType
vtkTypeUInt32 vtkMTimeType
Definition: vtkType.h:302
vtkFunctionParser::CheckMTime
vtkTimeStamp CheckMTime
Definition: vtkFunctionParser.h:399