VTK  9.3.0
vtkFunctionParser.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
46 #ifndef vtkFunctionParser_h
47 #define vtkFunctionParser_h
48 
49 #include "vtkCommonMiscModule.h" // For export macro
50 #include "vtkObject.h"
51 #include "vtkTuple.h" // needed for vtkTuple
52 #include <string> // needed for string.
53 #include <vector> // needed for vector
54 
55 #define VTK_PARSER_IMMEDIATE 1
56 #define VTK_PARSER_UNARY_MINUS 2
57 #define VTK_PARSER_UNARY_PLUS 3
58 
59 // supported math functions
60 #define VTK_PARSER_ADD 4
61 #define VTK_PARSER_SUBTRACT 5
62 #define VTK_PARSER_MULTIPLY 6
63 #define VTK_PARSER_DIVIDE 7
64 #define VTK_PARSER_POWER 8
65 #define VTK_PARSER_ABSOLUTE_VALUE 9
66 #define VTK_PARSER_EXPONENT 10
67 #define VTK_PARSER_CEILING 11
68 #define VTK_PARSER_FLOOR 12
69 #define VTK_PARSER_LOGARITHM 13
70 #define VTK_PARSER_LOGARITHME 14
71 #define VTK_PARSER_LOGARITHM10 15
72 #define VTK_PARSER_SQUARE_ROOT 16
73 #define VTK_PARSER_SINE 17
74 #define VTK_PARSER_COSINE 18
75 #define VTK_PARSER_TANGENT 19
76 #define VTK_PARSER_ARCSINE 20
77 #define VTK_PARSER_ARCCOSINE 21
78 #define VTK_PARSER_ARCTANGENT 22
79 #define VTK_PARSER_HYPERBOLIC_SINE 23
80 #define VTK_PARSER_HYPERBOLIC_COSINE 24
81 #define VTK_PARSER_HYPERBOLIC_TANGENT 25
82 #define VTK_PARSER_MIN 26
83 #define VTK_PARSER_MAX 27
84 #define VTK_PARSER_SIGN 29
85 
86 // functions involving vectors
87 #define VTK_PARSER_CROSS 28
88 #define VTK_PARSER_VECTOR_UNARY_MINUS 30
89 #define VTK_PARSER_VECTOR_UNARY_PLUS 31
90 #define VTK_PARSER_DOT_PRODUCT 32
91 #define VTK_PARSER_VECTOR_ADD 33
92 #define VTK_PARSER_VECTOR_SUBTRACT 34
93 #define VTK_PARSER_SCALAR_TIMES_VECTOR 35
94 #define VTK_PARSER_VECTOR_TIMES_SCALAR 36
95 #define VTK_PARSER_VECTOR_OVER_SCALAR 37
96 #define VTK_PARSER_MAGNITUDE 38
97 #define VTK_PARSER_NORMALIZE 39
98 
99 // constants involving vectors
100 #define VTK_PARSER_IHAT 40
101 #define VTK_PARSER_JHAT 41
102 #define VTK_PARSER_KHAT 42
103 
104 // code for if(bool, trueval, falseval) resulting in a scalar
105 #define VTK_PARSER_IF 43
106 
107 // code for if(bool, truevec, falsevec) resulting in a vector
108 #define VTK_PARSER_VECTOR_IF 44
109 
110 // codes for boolean expressions
111 #define VTK_PARSER_LESS_THAN 45
112 
113 // codes for boolean expressions
114 #define VTK_PARSER_GREATER_THAN 46
115 
116 // codes for boolean expressions
117 #define VTK_PARSER_EQUAL_TO 47
118 
119 // codes for boolean expressions
120 #define VTK_PARSER_AND 48
121 
122 // codes for boolean expressions
123 #define VTK_PARSER_OR 49
124 
125 // codes for scalar variables come before those for vectors. Do not define
126 // values for VTK_PARSER_BEGIN_VARIABLES+1, VTK_PARSER_BEGIN_VARIABLES+2, ...,
127 // because they are used to look up variables numbered 1, 2, ...
128 #define VTK_PARSER_BEGIN_VARIABLES 50
129 
130 // the value that is returned as a result if there is an error
131 #define VTK_PARSER_ERROR_RESULT VTK_FLOAT_MAX
132 
133 VTK_ABI_NAMESPACE_BEGIN
134 class VTKCOMMONMISC_EXPORT vtkFunctionParser : public vtkObject
135 {
136 public:
138  vtkTypeMacro(vtkFunctionParser, vtkObject);
139  void PrintSelf(ostream& os, vtkIndent indent) override;
140 
144  vtkMTimeType GetMTime() override;
145 
147 
150  void SetFunction(const char* function);
151  vtkGetStringMacro(Function);
153 
159 
165 
169  double GetScalarResult();
170 
172 
176  void GetVectorResult(double result[3])
177  {
178  double* r = this->GetVectorResult();
179  result[0] = r[0];
180  result[1] = r[1];
181  result[2] = r[2];
182  }
184 
186 
192  void SetScalarVariableValue(const char* variableName, double value);
193  void SetScalarVariableValue(const std::string& variableName, double value)
194  {
195  this->SetScalarVariableValue(variableName.c_str(), value);
196  }
197  void SetScalarVariableValue(int i, double value);
199 
201 
204  double GetScalarVariableValue(const char* variableName);
205  double GetScalarVariableValue(const std::string& variableName)
206  {
207  return this->GetScalarVariableValue(variableName.c_str());
208  }
209  double GetScalarVariableValue(int i);
211 
213 
220  const char* variableName, double xValue, double yValue, double zValue);
222  const std::string& variableName, double xValue, double yValue, double zValue)
223  {
224  this->SetVectorVariableValue(variableName.c_str(), xValue, yValue, zValue);
225  }
226  void SetVectorVariableValue(const char* variableName, const double values[3])
227  {
228  this->SetVectorVariableValue(variableName, values[0], values[1], values[2]);
229  }
230  void SetVectorVariableValue(const std::string& variableName, const double values[3])
231  {
232  this->SetVectorVariableValue(variableName.c_str(), values[0], values[1], values[2]);
233  }
234  void SetVectorVariableValue(int i, double xValue, double yValue, double zValue);
235  void SetVectorVariableValue(int i, const double values[3])
236  {
237  this->SetVectorVariableValue(i, values[0], values[1], values[2]);
238  }
240 
242 
245  double* GetVectorVariableValue(const char* variableName) VTK_SIZEHINT(3);
246  double* GetVectorVariableValue(const std::string& variableName) VTK_SIZEHINT(3)
247  {
248  return this->GetVectorVariableValue(variableName.c_str());
249  }
250  void GetVectorVariableValue(const char* variableName, double value[3])
251  {
252  double* r = this->GetVectorVariableValue(variableName);
253  value[0] = r[0];
254  value[1] = r[1];
255  value[2] = r[2];
256  }
257  void GetVectorVariableValue(const std::string& variableName, double value[3])
258  {
259  this->GetVectorVariableValue(variableName.c_str(), value);
260  }
262  void GetVectorVariableValue(int i, double value[3])
263  {
264  double* r = this->GetVectorVariableValue(i);
265  value[0] = r[0];
266  value[1] = r[1];
267  value[2] = r[2];
268  }
270 
274  int GetNumberOfScalarVariables() { return static_cast<int>(this->ScalarVariableNames.size()); }
275 
279  int GetScalarVariableIndex(const char* name);
281  {
282  return this->GetScalarVariableIndex(name.c_str());
283  }
284 
288  int GetNumberOfVectorVariables() { return static_cast<int>(this->VectorVariableNames.size()); }
289 
293  int GetVectorVariableIndex(const char* name);
295  {
296  return this->GetVectorVariableIndex(name.c_str());
297  }
298 
302  const char* GetScalarVariableName(int i);
303 
307  const char* GetVectorVariableName(int i);
308 
310 
316  bool GetScalarVariableNeeded(const char* variableName);
317  bool GetScalarVariableNeeded(const std::string& variableName)
318  {
319  return GetScalarVariableNeeded(variableName.c_str());
320  }
322 
324 
330  bool GetVectorVariableNeeded(const char* variableName);
331  bool GetVectorVariableNeeded(const std::string& variableName)
332  {
333  return this->GetVectorVariableNeeded(variableName.c_str());
334  }
336 
341 
346 
351 
353 
359  vtkSetMacro(ReplaceInvalidValues, vtkTypeBool);
360  vtkGetMacro(ReplaceInvalidValues, vtkTypeBool);
361  vtkBooleanMacro(ReplaceInvalidValues, vtkTypeBool);
362  vtkSetMacro(ReplacementValue, double);
363  vtkGetMacro(ReplacementValue, double);
365 
369  void CheckExpression(int& pos, char** error);
370 
375 
376 protected:
378  ~vtkFunctionParser() override;
379 
380  int Parse();
381 
385  bool Evaluate();
386 
387  int CheckSyntax();
388 
389  void CopyParseError(int& position, char** error);
390 
391  void RemoveSpaces();
392  char* RemoveSpacesFrom(const char* variableName);
394 
396  void BuildInternalSubstringStructure(int beginIndex, int endIndex);
397  void AddInternalByte(unsigned int newByte);
398 
399  int IsSubstringCompletelyEnclosed(int beginIndex, int endIndex);
400  int FindEndOfMathFunction(int beginIndex);
401  int FindEndOfMathConstant(int beginIndex);
402 
403  int IsVariableName(int currentIndex);
404  int IsElementaryOperator(int op);
405 
406  int GetMathFunctionNumber(int currentIndex);
408  int GetMathFunctionStringLength(int mathFunctionNumber);
409  int GetMathConstantNumber(int currentIndex);
410  int GetMathConstantStringLength(int mathConstantNumber);
411  unsigned char GetElementaryOperatorNumber(char op);
412  unsigned int GetOperandNumber(int currentIndex);
413  int GetVariableNameLength(int variableNumber);
414 
416 
422 
423  vtkSetStringMacro(ParseError);
424 
425  int FindPositionInOriginalFunction(const int& pos);
426 
427  char* Function;
429 
431  std::vector<std::string> ScalarVariableNames;
432  std::vector<std::string> VectorVariableNames;
433  std::vector<double> ScalarVariableValues;
434  std::vector<vtkTuple<double, 3>> VectorVariableValues;
435  std::vector<bool> ScalarVariableNeeded;
436  std::vector<bool> VectorVariableNeeded;
437 
438  std::vector<unsigned int> ByteCode;
440  double* Immediates;
442  double* Stack;
445 
449 
452 
454  char* ParseError;
455 
456 private:
457  vtkFunctionParser(const vtkFunctionParser&) = delete;
458  void operator=(const vtkFunctionParser&) = delete;
459 };
460 
461 VTK_ABI_NAMESPACE_END
462 #endif
Parse and evaluate a mathematical expression.
double * GetVectorResult()
Get a vector result from evaluating the input function.
void SetScalarVariableValue(int i, double value)
Set the value of a scalar variable.
int BuildInternalFunctionStructure()
double GetScalarVariableValue(const std::string &variableName)
Get the value of a scalar variable.
int DisambiguateOperators()
std::vector< std::string > VectorVariableNames
~vtkFunctionParser() override
bool GetScalarVariableNeeded(const std::string &variableName)
Returns whether a scalar variable is needed for the function evaluation.
void SetVectorVariableValue(int i, const double values[3])
Set the value of a vector variable.
void SetFunction(const char *function)
Set/Get input string to evaluate.
vtkTimeStamp FunctionMTime
int IsScalarResult()
Check whether the result is a scalar result.
double * GetVectorVariableValue(const char *variableName)
Get the value of a vector variable.
int GetScalarVariableIndex(const char *name)
Get scalar variable index or -1 if not found.
int GetNumberOfVectorVariables()
Get the number of vector variables.
void RemoveVectorVariables()
Remove all the vector variables.
double * GetVectorVariableValue(int i)
Get the value of a vector variable.
void GetVectorVariableValue(const char *variableName, double value[3])
Get the value of a vector variable.
double GetScalarResult()
Get a scalar result from evaluating the input function.
int GetMathFunctionStringLength(int mathFunctionNumber)
int GetNumberOfScalarVariables()
Get the number of scalar variables.
int GetMathConstantNumber(int currentIndex)
int GetVectorVariableIndex(const char *name)
Get scalar variable index or -1 if not found.
void SetVectorVariableValue(const char *variableName, const double values[3])
Set the value of a vector variable.
int GetMathFunctionNumber(int currentIndex)
int IsElementaryOperator(int op)
int FindPositionInOriginalFunction(const int &pos)
unsigned int GetOperandNumber(int currentIndex)
void AddInternalByte(unsigned int newByte)
void SetVectorVariableValue(const std::string &variableName, const double values[3])
Set the value of a vector variable.
vtkMTimeType GetMTime() override
Return parser's MTime.
void UpdateNeededVariables()
Collects meta-data about which variables are needed by the current function.
bool GetVectorVariableNeeded(const std::string &variableName)
Returns whether a vector variable is needed for the function evaluation.
char * RemoveSpacesFrom(const char *variableName)
void GetVectorVariableValue(int i, double value[3])
Get the value of a vector variable.
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
void SetVectorVariableValue(int i, double xValue, double yValue, double zValue)
Set the value of a vector variable.
std::vector< bool > ScalarVariableNeeded
bool Evaluate()
Evaluate the function, returning true on success, false on failure.
std::vector< unsigned int > ByteCode
vtkTimeStamp CheckMTime
int GetMathFunctionNumberByCheckingParenthesis(int currentIndex)
double GetScalarVariableValue(const char *variableName)
Get the value of a scalar variable.
int IsVariableName(int currentIndex)
void CheckExpression(int &pos, char **error)
Check the validity of the function expression.
int FindEndOfMathConstant(int beginIndex)
double GetScalarVariableValue(int i)
Get the value of a scalar variable.
void SetVectorVariableValue(const std::string &variableName, double xValue, double yValue, double zValue)
Set the value of a vector variable.
std::vector< double > ScalarVariableValues
const char * GetVectorVariableName(int i)
Get the ith vector variable name.
vtkTimeStamp ParseMTime
bool GetScalarVariableNeeded(const char *variableName)
Returns whether a scalar variable is needed for the function evaluation.
bool GetScalarVariableNeeded(int i)
Returns whether a scalar variable is needed for the function evaluation.
unsigned char GetElementaryOperatorNumber(char op)
int GetVectorVariableIndex(const std::string &name)
const char * GetScalarVariableName(int i)
Get the ith scalar variable name.
double * GetVectorVariableValue(const std::string &variableName)
Get the value of a vector variable.
int GetVariableNameLength(int variableNumber)
std::vector< vtkTuple< double, 3 > > VectorVariableValues
void GetVectorVariableValue(const std::string &variableName, double value[3])
Get the value of a vector variable.
vtkTypeBool ReplaceInvalidValues
static vtkFunctionParser * New()
int IsVectorResult()
Check whether the result is a vector result.
void RemoveScalarVariables()
Remove all the scalar variables.
int IsSubstringCompletelyEnclosed(int beginIndex, int endIndex)
bool GetVectorVariableNeeded(int i)
Returns whether a vector variable is needed for the function evaluation.
void BuildInternalSubstringStructure(int beginIndex, int endIndex)
void RemoveAllVariables()
Remove all the current variables.
void CopyParseError(int &position, char **error)
void InvalidateFunction()
Allow the user to force the function to be re-parsed.
void SetScalarVariableValue(const std::string &variableName, double value)
Set the value of a scalar variable.
int OperatorWithinVariable(int idx)
bool GetVectorVariableNeeded(const char *variableName)
Returns whether a vector variable is needed for the function evaluation.
int GetMathConstantStringLength(int mathConstantNumber)
std::vector< bool > VectorVariableNeeded
std::vector< std::string > ScalarVariableNames
int GetScalarVariableIndex(const std::string &name)
void SetVectorVariableValue(const char *variableName, double xValue, double yValue, double zValue)
Set the value of a vector variable.
void SetScalarVariableValue(const char *variableName, double value)
Set the value of a scalar variable.
int FindEndOfMathFunction(int beginIndex)
a simple class to control print indentation
Definition: vtkIndent.h:38
abstract base class for most VTK objects
Definition: vtkObject.h:61
record modification and/or execution time
Definition: vtkTimeStamp.h:34
@ value
Definition: vtkX3D.h:220
@ name
Definition: vtkX3D.h:219
@ position
Definition: vtkX3D.h:261
@ string
Definition: vtkX3D.h:490
int vtkTypeBool
Definition: vtkABI.h:64
vtkTypeUInt32 vtkMTimeType
Definition: vtkType.h:270
#define VTK_SIZEHINT(...)