VTK  9.3.1
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
37 #ifndef vtkFunctionParser_h
38 #define vtkFunctionParser_h
39 
40 #include "vtkCommonMiscModule.h" // For export macro
41 #include "vtkObject.h"
42 #include "vtkTuple.h" // needed for vtkTuple
43 #include <string> // needed for string.
44 #include <vector> // needed for vector
45 
46 #define VTK_PARSER_IMMEDIATE 1
47 #define VTK_PARSER_UNARY_MINUS 2
48 #define VTK_PARSER_UNARY_PLUS 3
49 
50 // supported math functions
51 #define VTK_PARSER_ADD 4
52 #define VTK_PARSER_SUBTRACT 5
53 #define VTK_PARSER_MULTIPLY 6
54 #define VTK_PARSER_DIVIDE 7
55 #define VTK_PARSER_POWER 8
56 #define VTK_PARSER_ABSOLUTE_VALUE 9
57 #define VTK_PARSER_EXPONENT 10
58 #define VTK_PARSER_CEILING 11
59 #define VTK_PARSER_FLOOR 12
60 #define VTK_PARSER_LOGARITHM 13
61 #define VTK_PARSER_LOGARITHME 14
62 #define VTK_PARSER_LOGARITHM10 15
63 #define VTK_PARSER_SQUARE_ROOT 16
64 #define VTK_PARSER_SINE 17
65 #define VTK_PARSER_COSINE 18
66 #define VTK_PARSER_TANGENT 19
67 #define VTK_PARSER_ARCSINE 20
68 #define VTK_PARSER_ARCCOSINE 21
69 #define VTK_PARSER_ARCTANGENT 22
70 #define VTK_PARSER_HYPERBOLIC_SINE 23
71 #define VTK_PARSER_HYPERBOLIC_COSINE 24
72 #define VTK_PARSER_HYPERBOLIC_TANGENT 25
73 #define VTK_PARSER_MIN 26
74 #define VTK_PARSER_MAX 27
75 #define VTK_PARSER_SIGN 29
76 
77 // functions involving vectors
78 #define VTK_PARSER_CROSS 28
79 #define VTK_PARSER_VECTOR_UNARY_MINUS 30
80 #define VTK_PARSER_VECTOR_UNARY_PLUS 31
81 #define VTK_PARSER_DOT_PRODUCT 32
82 #define VTK_PARSER_VECTOR_ADD 33
83 #define VTK_PARSER_VECTOR_SUBTRACT 34
84 #define VTK_PARSER_SCALAR_TIMES_VECTOR 35
85 #define VTK_PARSER_VECTOR_TIMES_SCALAR 36
86 #define VTK_PARSER_VECTOR_OVER_SCALAR 37
87 #define VTK_PARSER_MAGNITUDE 38
88 #define VTK_PARSER_NORMALIZE 39
89 
90 // constants involving vectors
91 #define VTK_PARSER_IHAT 40
92 #define VTK_PARSER_JHAT 41
93 #define VTK_PARSER_KHAT 42
94 
95 // code for if(bool, trueval, falseval) resulting in a scalar
96 #define VTK_PARSER_IF 43
97 
98 // code for if(bool, truevec, falsevec) resulting in a vector
99 #define VTK_PARSER_VECTOR_IF 44
100 
101 // codes for boolean expressions
102 #define VTK_PARSER_LESS_THAN 45
103 
104 // codes for boolean expressions
105 #define VTK_PARSER_GREATER_THAN 46
106 
107 // codes for boolean expressions
108 #define VTK_PARSER_EQUAL_TO 47
109 
110 // codes for boolean expressions
111 #define VTK_PARSER_AND 48
112 
113 // codes for boolean expressions
114 #define VTK_PARSER_OR 49
115 
116 // codes for scalar variables come before those for vectors. Do not define
117 // values for VTK_PARSER_BEGIN_VARIABLES+1, VTK_PARSER_BEGIN_VARIABLES+2, ...,
118 // because they are used to look up variables numbered 1, 2, ...
119 #define VTK_PARSER_BEGIN_VARIABLES 50
120 
121 // the value that is returned as a result if there is an error
122 #define VTK_PARSER_ERROR_RESULT VTK_FLOAT_MAX
123 
124 VTK_ABI_NAMESPACE_BEGIN
125 class VTKCOMMONMISC_EXPORT vtkFunctionParser : public vtkObject
126 {
127 public:
128  static vtkFunctionParser* New();
129  vtkTypeMacro(vtkFunctionParser, vtkObject);
130  void PrintSelf(ostream& os, vtkIndent indent) override;
131 
135  vtkMTimeType GetMTime() override;
136 
138 
141  void SetFunction(const char* function);
142  vtkGetStringMacro(Function);
144 
149  int IsScalarResult();
150 
155  int IsVectorResult();
156 
160  double GetScalarResult();
161 
163 
166  double* GetVectorResult() VTK_SIZEHINT(3);
167  void GetVectorResult(double result[3])
168  {
169  double* r = this->GetVectorResult();
170  result[0] = r[0];
171  result[1] = r[1];
172  result[2] = r[2];
173  }
175 
177 
183  void SetScalarVariableValue(const char* variableName, double value);
184  void SetScalarVariableValue(const std::string& variableName, double value)
185  {
186  this->SetScalarVariableValue(variableName.c_str(), value);
187  }
188  void SetScalarVariableValue(int i, double value);
190 
192 
195  double GetScalarVariableValue(const char* variableName);
196  double GetScalarVariableValue(const std::string& variableName)
197  {
198  return this->GetScalarVariableValue(variableName.c_str());
199  }
200  double GetScalarVariableValue(int i);
202 
204 
210  void SetVectorVariableValue(
211  const char* variableName, double xValue, double yValue, double zValue);
213  const std::string& variableName, double xValue, double yValue, double zValue)
214  {
215  this->SetVectorVariableValue(variableName.c_str(), xValue, yValue, zValue);
216  }
217  void SetVectorVariableValue(const char* variableName, const double values[3])
218  {
219  this->SetVectorVariableValue(variableName, values[0], values[1], values[2]);
220  }
221  void SetVectorVariableValue(const std::string& variableName, const double values[3])
222  {
223  this->SetVectorVariableValue(variableName.c_str(), values[0], values[1], values[2]);
224  }
225  void SetVectorVariableValue(int i, double xValue, double yValue, double zValue);
226  void SetVectorVariableValue(int i, const double values[3])
227  {
228  this->SetVectorVariableValue(i, values[0], values[1], values[2]);
229  }
231 
233 
236  double* GetVectorVariableValue(const char* variableName) VTK_SIZEHINT(3);
237  double* GetVectorVariableValue(const std::string& variableName) VTK_SIZEHINT(3)
238  {
239  return this->GetVectorVariableValue(variableName.c_str());
240  }
241  void GetVectorVariableValue(const char* variableName, double value[3])
242  {
243  double* r = this->GetVectorVariableValue(variableName);
244  value[0] = r[0];
245  value[1] = r[1];
246  value[2] = r[2];
247  }
248  void GetVectorVariableValue(const std::string& variableName, double value[3])
249  {
250  this->GetVectorVariableValue(variableName.c_str(), value);
251  }
252  double* GetVectorVariableValue(int i) VTK_SIZEHINT(3);
253  void GetVectorVariableValue(int i, double value[3])
254  {
255  double* r = this->GetVectorVariableValue(i);
256  value[0] = r[0];
257  value[1] = r[1];
258  value[2] = r[2];
259  }
261 
265  int GetNumberOfScalarVariables() { return static_cast<int>(this->ScalarVariableNames.size()); }
266 
270  int GetScalarVariableIndex(const char* name);
272  {
273  return this->GetScalarVariableIndex(name.c_str());
274  }
275 
279  int GetNumberOfVectorVariables() { return static_cast<int>(this->VectorVariableNames.size()); }
280 
284  int GetVectorVariableIndex(const char* name);
286  {
287  return this->GetVectorVariableIndex(name.c_str());
288  }
289 
293  const char* GetScalarVariableName(int i);
294 
298  const char* GetVectorVariableName(int i);
299 
301 
306  bool GetScalarVariableNeeded(int i);
307  bool GetScalarVariableNeeded(const char* variableName);
308  bool GetScalarVariableNeeded(const std::string& variableName)
309  {
310  return GetScalarVariableNeeded(variableName.c_str());
311  }
313 
315 
320  bool GetVectorVariableNeeded(int i);
321  bool GetVectorVariableNeeded(const char* variableName);
322  bool GetVectorVariableNeeded(const std::string& variableName)
323  {
324  return this->GetVectorVariableNeeded(variableName.c_str());
325  }
327 
331  void RemoveAllVariables();
332 
336  void RemoveScalarVariables();
337 
341  void RemoveVectorVariables();
342 
344 
350  vtkSetMacro(ReplaceInvalidValues, vtkTypeBool);
351  vtkGetMacro(ReplaceInvalidValues, vtkTypeBool);
352  vtkBooleanMacro(ReplaceInvalidValues, vtkTypeBool);
353  vtkSetMacro(ReplacementValue, double);
354  vtkGetMacro(ReplacementValue, double);
356 
360  void CheckExpression(int& pos, char** error);
361 
365  void InvalidateFunction();
366 
367 protected:
369  ~vtkFunctionParser() override;
370 
371  int Parse();
372 
376  bool Evaluate();
377 
378  int CheckSyntax();
379 
380  void CopyParseError(int& position, char** error);
381 
382  void RemoveSpaces();
383  char* RemoveSpacesFrom(const char* variableName);
384  int OperatorWithinVariable(int idx);
385 
386  int BuildInternalFunctionStructure();
387  void BuildInternalSubstringStructure(int beginIndex, int endIndex);
388  void AddInternalByte(unsigned int newByte);
389 
390  int IsSubstringCompletelyEnclosed(int beginIndex, int endIndex);
391  int FindEndOfMathFunction(int beginIndex);
392  int FindEndOfMathConstant(int beginIndex);
393 
394  int IsVariableName(int currentIndex);
395  int IsElementaryOperator(int op);
396 
397  int GetMathFunctionNumber(int currentIndex);
398  int GetMathFunctionNumberByCheckingParenthesis(int currentIndex);
399  int GetMathFunctionStringLength(int mathFunctionNumber);
400  int GetMathConstantNumber(int currentIndex);
401  int GetMathConstantStringLength(int mathConstantNumber);
402  unsigned char GetElementaryOperatorNumber(char op);
403  unsigned int GetOperandNumber(int currentIndex);
404  int GetVariableNameLength(int variableNumber);
405 
406  int DisambiguateOperators();
407 
412  void UpdateNeededVariables();
413 
414  vtkSetStringMacro(ParseError);
415 
416  int FindPositionInOriginalFunction(const int& pos);
417 
418  char* Function;
420 
422  std::vector<std::string> ScalarVariableNames;
423  std::vector<std::string> VectorVariableNames;
424  std::vector<double> ScalarVariableValues;
425  std::vector<vtkTuple<double, 3>> VectorVariableValues;
426  std::vector<bool> ScalarVariableNeeded;
427  std::vector<bool> VectorVariableNeeded;
428 
429  std::vector<unsigned int> ByteCode;
431  double* Immediates;
433  double* Stack;
436 
440 
443 
445  char* ParseError;
446 
447 private:
448  vtkFunctionParser(const vtkFunctionParser&) = delete;
449  void operator=(const vtkFunctionParser&) = delete;
450 };
451 
452 VTK_ABI_NAMESPACE_END
453 #endif
void SetVectorVariableValue(int i, const double values[3])
Set the value of a vector variable.
vtkTypeBool ReplaceInvalidValues
void SetVectorVariableValue(const std::string &variableName, double xValue, double yValue, double zValue)
Set the value of a vector variable.
abstract base class for most VTK objects
Definition: vtkObject.h:51
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
vtkTimeStamp CheckMTime
vtkTypeUInt32 vtkMTimeType
Definition: vtkType.h:270
record modification and/or execution time
Definition: vtkTimeStamp.h:24
int GetVectorVariableIndex(const std::string &name)
std::vector< double > ScalarVariableValues
void SetVectorVariableValue(const char *variableName, const double values[3])
Set the value of a vector variable.
double GetScalarVariableValue(const std::string &variableName)
Get the value of a scalar variable.
bool GetVectorVariableNeeded(const std::string &variableName)
Returns whether a vector variable is needed for the function evaluation.
int vtkTypeBool
Definition: vtkABI.h:64
double * GetVectorVariableValue(const std::string &variableName)
Get the value of a vector variable.
vtkTimeStamp FunctionMTime
Parse and evaluate a mathematical expression.
a simple class to control print indentation
Definition: vtkIndent.h:28
void GetVectorVariableValue(const char *variableName, double value[3])
Get the value of a vector variable.
std::vector< vtkTuple< double, 3 > > VectorVariableValues
virtual vtkMTimeType GetMTime()
Return this object's modified time.
void SetVectorVariableValue(const std::string &variableName, const double values[3])
Set the value of a vector variable.
std::vector< bool > VectorVariableNeeded
std::vector< unsigned int > ByteCode
std::vector< std::string > VectorVariableNames
#define VTK_SIZEHINT(...)
std::vector< std::string > ScalarVariableNames
vtkTimeStamp ParseMTime
void GetVectorVariableValue(const std::string &variableName, double value[3])
Get the value of a vector variable.
void GetVectorVariableValue(int i, double value[3])
Get the value of a vector variable.
int GetScalarVariableIndex(const std::string &name)
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
bool GetScalarVariableNeeded(const std::string &variableName)
Returns whether a scalar variable is needed for the function evaluation.
std::vector< bool > ScalarVariableNeeded
int GetNumberOfScalarVariables()
Get the number of scalar variables.
void SetScalarVariableValue(const std::string &variableName, double value)
Set the value of a scalar variable.
int GetNumberOfVectorVariables()
Get the number of vector variables.