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 =========================================================================*/
68 #ifndef vtkFunctionParser_h
69 #define vtkFunctionParser_h
70 
71 #include "vtkCommonMiscModule.h" // For export macro
72 #include "vtkObject.h"
73 #include "vtkTuple.h" // needed for vtkTuple
74 #include <string> // needed for string.
75 #include <vector> // needed for vector
76 
77 #define VTK_PARSER_IMMEDIATE 1
78 #define VTK_PARSER_UNARY_MINUS 2
79 #define VTK_PARSER_UNARY_PLUS 3
80 
81 // supported math functions
82 #define VTK_PARSER_ADD 4
83 #define VTK_PARSER_SUBTRACT 5
84 #define VTK_PARSER_MULTIPLY 6
85 #define VTK_PARSER_DIVIDE 7
86 #define VTK_PARSER_POWER 8
87 #define VTK_PARSER_ABSOLUTE_VALUE 9
88 #define VTK_PARSER_EXPONENT 10
89 #define VTK_PARSER_CEILING 11
90 #define VTK_PARSER_FLOOR 12
91 #define VTK_PARSER_LOGARITHM 13
92 #define VTK_PARSER_LOGARITHME 14
93 #define VTK_PARSER_LOGARITHM10 15
94 #define VTK_PARSER_SQUARE_ROOT 16
95 #define VTK_PARSER_SINE 17
96 #define VTK_PARSER_COSINE 18
97 #define VTK_PARSER_TANGENT 19
98 #define VTK_PARSER_ARCSINE 20
99 #define VTK_PARSER_ARCCOSINE 21
100 #define VTK_PARSER_ARCTANGENT 22
101 #define VTK_PARSER_HYPERBOLIC_SINE 23
102 #define VTK_PARSER_HYPERBOLIC_COSINE 24
103 #define VTK_PARSER_HYPERBOLIC_TANGENT 25
104 #define VTK_PARSER_MIN 26
105 #define VTK_PARSER_MAX 27
106 #define VTK_PARSER_SIGN 29
107 
108 // functions involving vectors
109 #define VTK_PARSER_CROSS 28
110 #define VTK_PARSER_VECTOR_UNARY_MINUS 30
111 #define VTK_PARSER_VECTOR_UNARY_PLUS 31
112 #define VTK_PARSER_DOT_PRODUCT 32
113 #define VTK_PARSER_VECTOR_ADD 33
114 #define VTK_PARSER_VECTOR_SUBTRACT 34
115 #define VTK_PARSER_SCALAR_TIMES_VECTOR 35
116 #define VTK_PARSER_VECTOR_TIMES_SCALAR 36
117 #define VTK_PARSER_VECTOR_OVER_SCALAR 37
118 #define VTK_PARSER_MAGNITUDE 38
119 #define VTK_PARSER_NORMALIZE 39
120 
121 // constants involving vectors
122 #define VTK_PARSER_IHAT 40
123 #define VTK_PARSER_JHAT 41
124 #define VTK_PARSER_KHAT 42
125 
126 // code for if(bool, trueval, falseval) resulting in a scalar
127 #define VTK_PARSER_IF 43
128 
129 // code for if(bool, truevec, falsevec) resulting in a vector
130 #define VTK_PARSER_VECTOR_IF 44
131 
132 // codes for boolean expressions
133 #define VTK_PARSER_LESS_THAN 45
134 
135 // codes for boolean expressions
136 #define VTK_PARSER_GREATER_THAN 46
137 
138 // codes for boolean expressions
139 #define VTK_PARSER_EQUAL_TO 47
140 
141 // codes for boolean expressions
142 #define VTK_PARSER_AND 48
143 
144 // codes for boolean expressions
145 #define VTK_PARSER_OR 49
146 
147 // codes for scalar variables come before those for vectors. Do not define
148 // values for VTK_PARSER_BEGIN_VARIABLES+1, VTK_PARSER_BEGIN_VARIABLES+2, ...,
149 // because they are used to look up variables numbered 1, 2, ...
150 #define VTK_PARSER_BEGIN_VARIABLES 50
151 
152 // the value that is returned as a result if there is an error
153 #define VTK_PARSER_ERROR_RESULT VTK_FLOAT_MAX
154 
155 class VTKCOMMONMISC_EXPORT vtkFunctionParser : public vtkObject
156 {
157 public:
159  vtkTypeMacro(vtkFunctionParser, vtkObject);
160  void PrintSelf(ostream& os, vtkIndent indent) override;
161 
165  vtkMTimeType GetMTime() override;
166 
168 
171  void SetFunction(const char* function);
172  vtkGetStringMacro(Function);
174 
180 
186 
190  double GetScalarResult();
191 
193 
197  void GetVectorResult(double result[3])
198  {
199  double* r = this->GetVectorResult();
200  result[0] = r[0];
201  result[1] = r[1];
202  result[2] = r[2];
203  }
205 
207 
213  void SetScalarVariableValue(const char* variableName, double value);
214  void SetScalarVariableValue(const std::string& variableName, double value)
215  {
216  this->SetScalarVariableValue(variableName.c_str(), value);
217  }
218  void SetScalarVariableValue(int i, double value);
220 
222 
225  double GetScalarVariableValue(const char* variableName);
226  double GetScalarVariableValue(const std::string& variableName)
227  {
228  return this->GetScalarVariableValue(variableName.c_str());
229  }
230  double GetScalarVariableValue(int i);
232 
234 
241  const char* variableName, double xValue, double yValue, double zValue);
243  const std::string& variableName, double xValue, double yValue, double zValue)
244  {
245  this->SetVectorVariableValue(variableName.c_str(), xValue, yValue, zValue);
246  }
247  void SetVectorVariableValue(const char* variableName, const double values[3])
248  {
249  this->SetVectorVariableValue(variableName, values[0], values[1], values[2]);
250  }
251  void SetVectorVariableValue(const std::string& variableName, const double values[3])
252  {
253  this->SetVectorVariableValue(variableName.c_str(), values[0], values[1], values[2]);
254  }
255  void SetVectorVariableValue(int i, double xValue, double yValue, double zValue);
256  void SetVectorVariableValue(int i, const double values[3])
257  {
258  this->SetVectorVariableValue(i, values[0], values[1], values[2]);
259  }
261 
263 
266  double* GetVectorVariableValue(const char* variableName) VTK_SIZEHINT(3);
267  double* GetVectorVariableValue(const std::string& variableName) VTK_SIZEHINT(3)
268  {
269  return this->GetVectorVariableValue(variableName.c_str());
270  }
271  void GetVectorVariableValue(const char* variableName, double value[3])
272  {
273  double* r = this->GetVectorVariableValue(variableName);
274  value[0] = r[0];
275  value[1] = r[1];
276  value[2] = r[2];
277  }
278  void GetVectorVariableValue(const std::string& variableName, double value[3])
279  {
280  this->GetVectorVariableValue(variableName.c_str(), value);
281  }
283  void GetVectorVariableValue(int i, double value[3])
284  {
285  double* r = this->GetVectorVariableValue(i);
286  value[0] = r[0];
287  value[1] = r[1];
288  value[2] = r[2];
289  }
291 
295  int GetNumberOfScalarVariables() { return static_cast<int>(this->ScalarVariableNames.size()); }
296 
300  int GetScalarVariableIndex(const char* name);
302  {
303  return this->GetScalarVariableIndex(name.c_str());
304  }
305 
309  int GetNumberOfVectorVariables() { return static_cast<int>(this->VectorVariableNames.size()); }
310 
314  int GetVectorVariableIndex(const char* name);
316  {
317  return this->GetVectorVariableIndex(name.c_str());
318  }
319 
323  const char* GetScalarVariableName(int i);
324 
328  const char* GetVectorVariableName(int i);
329 
331 
337  bool GetScalarVariableNeeded(const char* variableName);
338  bool GetScalarVariableNeeded(const std::string& variableName)
339  {
340  return GetScalarVariableNeeded(variableName.c_str());
341  }
343 
345 
351  bool GetVectorVariableNeeded(const char* variableName);
352  bool GetVectorVariableNeeded(const std::string& variableName)
353  {
354  return this->GetVectorVariableNeeded(variableName.c_str());
355  }
357 
362 
367 
372 
374 
380  vtkSetMacro(ReplaceInvalidValues, vtkTypeBool);
381  vtkGetMacro(ReplaceInvalidValues, vtkTypeBool);
382  vtkBooleanMacro(ReplaceInvalidValues, vtkTypeBool);
383  vtkSetMacro(ReplacementValue, double);
384  vtkGetMacro(ReplacementValue, double);
386 
390  void CheckExpression(int& pos, char** error);
391 
396 
397 protected:
399  ~vtkFunctionParser() override;
400 
401  int Parse();
402 
406  bool Evaluate();
407 
408  int CheckSyntax();
409 
410  void CopyParseError(int& position, char** error);
411 
412  void RemoveSpaces();
413  char* RemoveSpacesFrom(const char* variableName);
415 
417  void BuildInternalSubstringStructure(int beginIndex, int endIndex);
418  void AddInternalByte(unsigned int newByte);
419 
420  int IsSubstringCompletelyEnclosed(int beginIndex, int endIndex);
421  int FindEndOfMathFunction(int beginIndex);
422  int FindEndOfMathConstant(int beginIndex);
423 
424  int IsVariableName(int currentIndex);
425  int IsElementaryOperator(int op);
426 
427  int GetMathFunctionNumber(int currentIndex);
429  int GetMathFunctionStringLength(int mathFunctionNumber);
430  int GetMathConstantNumber(int currentIndex);
431  int GetMathConstantStringLength(int mathConstantNumber);
432  unsigned char GetElementaryOperatorNumber(char op);
433  unsigned int GetOperandNumber(int currentIndex);
434  int GetVariableNameLength(int variableNumber);
435 
437 
443 
444  vtkSetStringMacro(ParseError);
445 
446  int FindPositionInOriginalFunction(const int& pos);
447 
448  char* Function;
450 
452  std::vector<std::string> ScalarVariableNames;
453  std::vector<std::string> VectorVariableNames;
454  std::vector<double> ScalarVariableValues;
455  std::vector<vtkTuple<double, 3>> VectorVariableValues;
456  std::vector<bool> ScalarVariableNeeded;
457  std::vector<bool> VectorVariableNeeded;
458 
459  std::vector<unsigned int> ByteCode;
461  double* Immediates;
463  double* Stack;
466 
470 
473 
475  char* ParseError;
476 
477 private:
478  vtkFunctionParser(const vtkFunctionParser&) = delete;
479  void operator=(const vtkFunctionParser&) = delete;
480 };
481 
482 #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:119
abstract base class for most VTK objects
Definition: vtkObject.h:82
record modification and/or execution time
Definition: vtkTimeStamp.h:55
@ value
Definition: vtkX3D.h:226
@ name
Definition: vtkX3D.h:225
@ position
Definition: vtkX3D.h:267
@ string
Definition: vtkX3D.h:496
int vtkTypeBool
Definition: vtkABI.h:69
vtkTypeUInt32 vtkMTimeType
Definition: vtkType.h:287
#define VTK_SIZEHINT(...)