VTK  9.0.1
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 =========================================================================*/
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 class VTKCOMMONMISC_EXPORT vtkFunctionParser : public vtkObject
134 {
135 public:
136  static vtkFunctionParser* New();
137  vtkTypeMacro(vtkFunctionParser, vtkObject);
138  void PrintSelf(ostream& os, vtkIndent indent) override;
139 
143  vtkMTimeType GetMTime() override;
144 
146 
149  void SetFunction(const char* function);
150  vtkGetStringMacro(Function);
152 
157  int IsScalarResult();
158 
163  int IsVectorResult();
164 
168  double GetScalarResult();
169 
171 
174  double* GetVectorResult() VTK_SIZEHINT(3);
175  void GetVectorResult(double result[3])
176  {
177  double* r = this->GetVectorResult();
178  result[0] = r[0];
179  result[1] = r[1];
180  result[2] = r[2];
181  }
183 
185 
191  void SetScalarVariableValue(const char* variableName, double value);
192  void SetScalarVariableValue(int i, double value);
194 
196 
199  double GetScalarVariableValue(const char* variableName);
200  double GetScalarVariableValue(int i);
202 
204 
210  void SetVectorVariableValue(
211  const char* variableName, double xValue, double yValue, double zValue);
212  void SetVectorVariableValue(const char* variableName, const double values[3])
213  {
214  this->SetVectorVariableValue(variableName, values[0], values[1], values[2]);
215  }
216  void SetVectorVariableValue(int i, double xValue, double yValue, double zValue);
217  void SetVectorVariableValue(int i, const double values[3])
218  {
219  this->SetVectorVariableValue(i, values[0], values[1], values[2]);
220  }
222 
224 
227  double* GetVectorVariableValue(const char* variableName) VTK_SIZEHINT(3);
228  void GetVectorVariableValue(const char* variableName, double value[3])
229  {
230  double* r = this->GetVectorVariableValue(variableName);
231  value[0] = r[0];
232  value[1] = r[1];
233  value[2] = r[2];
234  }
235  double* GetVectorVariableValue(int i) VTK_SIZEHINT(3);
236  void GetVectorVariableValue(int i, double value[3])
237  {
238  double* r = this->GetVectorVariableValue(i);
239  value[0] = r[0];
240  value[1] = r[1];
241  value[2] = r[2];
242  }
244 
248  int GetNumberOfScalarVariables() { return static_cast<int>(this->ScalarVariableNames.size()); }
249 
253  int GetScalarVariableIndex(const char* name);
254 
258  int GetNumberOfVectorVariables() { return static_cast<int>(this->VectorVariableNames.size()); }
259 
263  int GetVectorVariableIndex(const char* name);
264 
268  const char* GetScalarVariableName(int i);
269 
273  const char* GetVectorVariableName(int i);
274 
276 
281  bool GetScalarVariableNeeded(int i);
282  bool GetScalarVariableNeeded(const char* variableName);
284 
286 
291  bool GetVectorVariableNeeded(int i);
292  bool GetVectorVariableNeeded(const char* variableName);
294 
298  void RemoveAllVariables();
299 
303  void RemoveScalarVariables();
304 
308  void RemoveVectorVariables();
309 
311 
317  vtkSetMacro(ReplaceInvalidValues, vtkTypeBool);
318  vtkGetMacro(ReplaceInvalidValues, vtkTypeBool);
319  vtkBooleanMacro(ReplaceInvalidValues, vtkTypeBool);
320  vtkSetMacro(ReplacementValue, double);
321  vtkGetMacro(ReplacementValue, double);
323 
327  void CheckExpression(int& pos, char** error);
328 
332  void InvalidateFunction();
333 
334 protected:
336  ~vtkFunctionParser() override;
337 
338  int Parse();
339 
343  bool Evaluate();
344 
345  int CheckSyntax();
346 
347  void CopyParseError(int& position, char** error);
348 
349  void RemoveSpaces();
350  char* RemoveSpacesFrom(const char* variableName);
351  int OperatorWithinVariable(int idx);
352 
353  int BuildInternalFunctionStructure();
354  void BuildInternalSubstringStructure(int beginIndex, int endIndex);
355  void AddInternalByte(unsigned int newByte);
356 
357  int IsSubstringCompletelyEnclosed(int beginIndex, int endIndex);
358  int FindEndOfMathFunction(int beginIndex);
359  int FindEndOfMathConstant(int beginIndex);
360 
361  int IsVariableName(int currentIndex);
362  int IsElementaryOperator(int op);
363 
364  int GetMathFunctionNumber(int currentIndex);
365  int GetMathFunctionNumberByCheckingParenthesis(int currentIndex);
366  int GetMathFunctionStringLength(int mathFunctionNumber);
367  int GetMathConstantNumber(int currentIndex);
368  int GetMathConstantStringLength(int mathConstantNumber);
369  unsigned char GetElementaryOperatorNumber(char op);
370  unsigned int GetOperandNumber(int currentIndex);
371  int GetVariableNameLength(int variableNumber);
372 
373  int DisambiguateOperators();
374 
379  void UpdateNeededVariables();
380 
381  vtkSetStringMacro(ParseError);
382 
383  int FindPositionInOriginalFunction(const int& pos);
384 
385  char* Function;
387 
389  std::vector<std::string> ScalarVariableNames;
390  std::vector<std::string> VectorVariableNames;
391  std::vector<double> ScalarVariableValues;
392  std::vector<vtkTuple<double, 3> > VectorVariableValues;
393  std::vector<bool> ScalarVariableNeeded;
394  std::vector<bool> VectorVariableNeeded;
395 
396  std::vector<unsigned int> ByteCode;
398  double* Immediates;
400  double* Stack;
403 
409 
412 
414  char* ParseError;
415 
416 private:
417  vtkFunctionParser(const vtkFunctionParser&) = delete;
418  void operator=(const vtkFunctionParser&) = delete;
419 };
420 
421 #endif
vtkTimeStamp VariableMTime
void SetVectorVariableValue(int i, const double values[3])
Set the value of a vector variable.
vtkTypeBool ReplaceInvalidValues
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:293
record modification and/or execution time
Definition: vtkTimeStamp.h:32
std::vector< double > ScalarVariableValues
void SetVectorVariableValue(const char *variableName, const double values[3])
Set the value of a vector variable.
int vtkTypeBool
Definition: vtkABI.h:69
vtkTimeStamp FunctionMTime
Parse and evaluate a mathematical expression.
a simple class to control print indentation
Definition: vtkIndent.h:33
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&#39;s modified time.
std::vector< bool > VectorVariableNeeded
std::vector< unsigned int > ByteCode
std::vector< std::string > VectorVariableNames
#define VTK_SIZEHINT(...)
std::vector< std::string > ScalarVariableNames
vtkTimeStamp EvaluateMTime
vtkTimeStamp ParseMTime
void GetVectorVariableValue(int i, double value[3])
Get the value of a vector variable.
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
std::vector< bool > ScalarVariableNeeded
int GetNumberOfScalarVariables()
Get the number of scalar variables.
int GetNumberOfVectorVariables()
Get the number of vector variables.