VTK  9.2.6
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 <string> // needed for string.
56 #include <vector> // needed for vector
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:
139  static vtkFunctionParser* New();
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 
160  int IsScalarResult();
161 
166  int IsVectorResult();
167 
171  double GetScalarResult();
172 
174 
177  double* GetVectorResult() VTK_SIZEHINT(3);
178  void GetVectorResult(double result[3])
179  {
180  double* r = this->GetVectorResult();
181  result[0] = r[0];
182  result[1] = r[1];
183  result[2] = r[2];
184  }
186 
188 
194  void SetScalarVariableValue(const char* variableName, double value);
195  void SetScalarVariableValue(const std::string& variableName, double value)
196  {
197  this->SetScalarVariableValue(variableName.c_str(), value);
198  }
199  void SetScalarVariableValue(int i, double value);
201 
203 
206  double GetScalarVariableValue(const char* variableName);
207  double GetScalarVariableValue(const std::string& variableName)
208  {
209  return this->GetScalarVariableValue(variableName.c_str());
210  }
211  double GetScalarVariableValue(int i);
213 
215 
221  void SetVectorVariableValue(
222  const char* variableName, double xValue, double yValue, double zValue);
224  const std::string& variableName, double xValue, double yValue, double zValue)
225  {
226  this->SetVectorVariableValue(variableName.c_str(), xValue, yValue, zValue);
227  }
228  void SetVectorVariableValue(const char* variableName, const double values[3])
229  {
230  this->SetVectorVariableValue(variableName, values[0], values[1], values[2]);
231  }
232  void SetVectorVariableValue(const std::string& variableName, const double values[3])
233  {
234  this->SetVectorVariableValue(variableName.c_str(), values[0], values[1], values[2]);
235  }
236  void SetVectorVariableValue(int i, double xValue, double yValue, double zValue);
237  void SetVectorVariableValue(int i, const double values[3])
238  {
239  this->SetVectorVariableValue(i, values[0], values[1], values[2]);
240  }
242 
244 
247  double* GetVectorVariableValue(const char* variableName) VTK_SIZEHINT(3);
248  double* GetVectorVariableValue(const std::string& variableName) VTK_SIZEHINT(3)
249  {
250  return this->GetVectorVariableValue(variableName.c_str());
251  }
252  void GetVectorVariableValue(const char* variableName, double value[3])
253  {
254  double* r = this->GetVectorVariableValue(variableName);
255  value[0] = r[0];
256  value[1] = r[1];
257  value[2] = r[2];
258  }
259  void GetVectorVariableValue(const std::string& variableName, double value[3])
260  {
261  this->GetVectorVariableValue(variableName.c_str(), value);
262  }
263  double* GetVectorVariableValue(int i) VTK_SIZEHINT(3);
264  void GetVectorVariableValue(int i, double value[3])
265  {
266  double* r = this->GetVectorVariableValue(i);
267  value[0] = r[0];
268  value[1] = r[1];
269  value[2] = r[2];
270  }
272 
276  int GetNumberOfScalarVariables() { return static_cast<int>(this->ScalarVariableNames.size()); }
277 
281  int GetScalarVariableIndex(const char* name);
283  {
284  return this->GetScalarVariableIndex(name.c_str());
285  }
286 
290  int GetNumberOfVectorVariables() { return static_cast<int>(this->VectorVariableNames.size()); }
291 
295  int GetVectorVariableIndex(const char* name);
297  {
298  return this->GetVectorVariableIndex(name.c_str());
299  }
300 
304  const char* GetScalarVariableName(int i);
305 
309  const char* GetVectorVariableName(int i);
310 
312 
317  bool GetScalarVariableNeeded(int i);
318  bool GetScalarVariableNeeded(const char* variableName);
319  bool GetScalarVariableNeeded(const std::string& variableName)
320  {
321  return GetScalarVariableNeeded(variableName.c_str());
322  }
324 
326 
331  bool GetVectorVariableNeeded(int i);
332  bool GetVectorVariableNeeded(const char* variableName);
333  bool GetVectorVariableNeeded(const std::string& variableName)
334  {
335  return this->GetVectorVariableNeeded(variableName.c_str());
336  }
338 
342  void RemoveAllVariables();
343 
347  void RemoveScalarVariables();
348 
352  void RemoveVectorVariables();
353 
355 
361  vtkSetMacro(ReplaceInvalidValues, vtkTypeBool);
362  vtkGetMacro(ReplaceInvalidValues, vtkTypeBool);
363  vtkBooleanMacro(ReplaceInvalidValues, vtkTypeBool);
364  vtkSetMacro(ReplacementValue, double);
365  vtkGetMacro(ReplacementValue, double);
367 
371  void CheckExpression(int& pos, char** error);
372 
376  void InvalidateFunction();
377 
378 protected:
380  ~vtkFunctionParser() override;
381 
382  int Parse();
383 
387  bool Evaluate();
388 
389  int CheckSyntax();
390 
391  void CopyParseError(int& position, char** error);
392 
393  void RemoveSpaces();
394  char* RemoveSpacesFrom(const char* variableName);
395  int OperatorWithinVariable(int idx);
396 
397  int BuildInternalFunctionStructure();
398  void BuildInternalSubstringStructure(int beginIndex, int endIndex);
399  void AddInternalByte(unsigned int newByte);
400 
401  int IsSubstringCompletelyEnclosed(int beginIndex, int endIndex);
402  int FindEndOfMathFunction(int beginIndex);
403  int FindEndOfMathConstant(int beginIndex);
404 
405  int IsVariableName(int currentIndex);
406  int IsElementaryOperator(int op);
407 
408  int GetMathFunctionNumber(int currentIndex);
409  int GetMathFunctionNumberByCheckingParenthesis(int currentIndex);
410  int GetMathFunctionStringLength(int mathFunctionNumber);
411  int GetMathConstantNumber(int currentIndex);
412  int GetMathConstantStringLength(int mathConstantNumber);
413  unsigned char GetElementaryOperatorNumber(char op);
414  unsigned int GetOperandNumber(int currentIndex);
415  int GetVariableNameLength(int variableNumber);
416 
417  int DisambiguateOperators();
418 
423  void UpdateNeededVariables();
424 
425  vtkSetStringMacro(ParseError);
426 
427  int FindPositionInOriginalFunction(const int& pos);
428 
429  char* Function;
431 
433  std::vector<std::string> ScalarVariableNames;
434  std::vector<std::string> VectorVariableNames;
435  std::vector<double> ScalarVariableValues;
436  std::vector<vtkTuple<double, 3>> VectorVariableValues;
437  std::vector<bool> ScalarVariableNeeded;
438  std::vector<bool> VectorVariableNeeded;
439 
440  std::vector<unsigned int> ByteCode;
442  double* Immediates;
444  double* Stack;
447 
451 
454 
456  char* ParseError;
457 
458 private:
459  vtkFunctionParser(const vtkFunctionParser&) = delete;
460  void operator=(const vtkFunctionParser&) = delete;
461 };
462 
463 #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:62
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:287
record modification and/or execution time
Definition: vtkTimeStamp.h:35
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:69
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:39
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.