VTK
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 =========================================================================*/
48 #ifndef vtkFunctionParser_h
49 #define vtkFunctionParser_h
50 
51 #include "vtkCommonMiscModule.h" // For export macro
52 #include "vtkObject.h"
53 
54 #define VTK_PARSER_IMMEDIATE 1
55 #define VTK_PARSER_UNARY_MINUS 2
56 
57 // supported math functions
58 #define VTK_PARSER_ADD 3
59 #define VTK_PARSER_SUBTRACT 4
60 #define VTK_PARSER_MULTIPLY 5
61 #define VTK_PARSER_DIVIDE 6
62 #define VTK_PARSER_POWER 7
63 #define VTK_PARSER_ABSOLUTE_VALUE 8
64 #define VTK_PARSER_EXPONENT 9
65 #define VTK_PARSER_CEILING 10
66 #define VTK_PARSER_FLOOR 11
67 #define VTK_PARSER_LOGARITHM 12
68 #define VTK_PARSER_LOGARITHME 13
69 #define VTK_PARSER_LOGARITHM10 14
70 #define VTK_PARSER_SQUARE_ROOT 15
71 #define VTK_PARSER_SINE 16
72 #define VTK_PARSER_COSINE 17
73 #define VTK_PARSER_TANGENT 18
74 #define VTK_PARSER_ARCSINE 19
75 #define VTK_PARSER_ARCCOSINE 20
76 #define VTK_PARSER_ARCTANGENT 21
77 #define VTK_PARSER_HYPERBOLIC_SINE 22
78 #define VTK_PARSER_HYPERBOLIC_COSINE 23
79 #define VTK_PARSER_HYPERBOLIC_TANGENT 24
80 #define VTK_PARSER_MIN 25
81 #define VTK_PARSER_MAX 26
82 #define VTK_PARSER_SIGN 28
83 
84 // functions involving vectors
85 #define VTK_PARSER_CROSS 27
86 #define VTK_PARSER_VECTOR_UNARY_MINUS 29
87 #define VTK_PARSER_DOT_PRODUCT 30
88 #define VTK_PARSER_VECTOR_ADD 31
89 #define VTK_PARSER_VECTOR_SUBTRACT 32
90 #define VTK_PARSER_SCALAR_TIMES_VECTOR 33
91 #define VTK_PARSER_VECTOR_TIMES_SCALAR 34
92 #define VTK_PARSER_VECTOR_OVER_SCALAR 35
93 #define VTK_PARSER_MAGNITUDE 36
94 #define VTK_PARSER_NORMALIZE 37
95 
96 // constants involving vectors
97 #define VTK_PARSER_IHAT 38
98 #define VTK_PARSER_JHAT 39
99 #define VTK_PARSER_KHAT 40
100 
101 // code for if(bool, trueval, falseval) resulting in a scalar
102 #define VTK_PARSER_IF 41
103 
104 // code for if(bool, truevec, falsevec) resulting in a vector
105 #define VTK_PARSER_VECTOR_IF 42
106 
107 // codes for boolean expressions
108 #define VTK_PARSER_LESS_THAN 43
109 
110 // codes for boolean expressions
111 #define VTK_PARSER_GREATER_THAN 44
112 
113 // codes for boolean expressions
114 #define VTK_PARSER_EQUAL_TO 45
115 
116 // codes for boolean expressions
117 #define VTK_PARSER_AND 46
118 
119 // codes for boolean expressions
120 #define VTK_PARSER_OR 47
121 
122 // codes for scalar variables come before those for vectors. Do not define
123 // values for VTK_PARSER_BEGIN_VARIABLES+1, VTK_PARSER_BEGIN_VARIABLES+2, ...,
124 // because they are used to look up variables numbered 1, 2, ...
125 #define VTK_PARSER_BEGIN_VARIABLES 48
126 
127 // the value that is retuned as a result if there is an error
128 #define VTK_PARSER_ERROR_RESULT VTK_FLOAT_MAX
129 
131 {
132 public:
133  static vtkFunctionParser *New();
134  vtkTypeMacro(vtkFunctionParser, vtkObject);
135  void PrintSelf(ostream& os, vtkIndent indent);
136 
138  unsigned long int GetMTime();
139 
141 
142  void SetFunction(const char *function);
143  vtkGetStringMacro(Function);
145 
148  int IsScalarResult();
149 
152  int IsVectorResult();
153 
155  double GetScalarResult();
156 
158 
159  double* GetVectorResult();
160  void GetVectorResult(double result[3]) {
161  double *r = this->GetVectorResult();
162  result[0] = r[0]; result[1] = r[1]; result[2] = r[2]; };
164 
166 
170  void SetScalarVariableValue(const char* variableName, double value);
171  void SetScalarVariableValue(int i, double value);
173 
175 
176  double GetScalarVariableValue(const char* variableName);
177  double GetScalarVariableValue(int i);
179 
181 
185  void SetVectorVariableValue(const char* variableName, double xValue,
186  double yValue, double zValue);
187  void SetVectorVariableValue(const char* variableName,
188  const double values[3]) {
189  this->SetVectorVariableValue(variableName,values[0],values[1],values[2]);};
190  void SetVectorVariableValue(int i, double xValue, double yValue,
191  double zValue);
192  void SetVectorVariableValue(int i, const double values[3]) {
193  this->SetVectorVariableValue(i,values[0],values[1],values[2]);};
195 
197 
198  double* GetVectorVariableValue(const char* variableName);
199  void GetVectorVariableValue(const char* variableName, double value[3]) {
200  double *r = this->GetVectorVariableValue(variableName);
201  value[0] = r[0]; value[1] = r[1]; value[2] = r[2]; };
202  double* GetVectorVariableValue(int i);
203  void GetVectorVariableValue(int i, double value[3]) {
204  double *r = this->GetVectorVariableValue(i);
205  value[0] = r[0]; value[1] = r[1]; value[2] = r[2]; };
207 
209 
210  vtkGetMacro(NumberOfScalarVariables,int);
212 
214 
215  vtkGetMacro(NumberOfVectorVariables,int);
217 
219  char* GetScalarVariableName(int i);
220 
222  char* GetVectorVariableName(int i);
223 
225  void RemoveAllVariables();
226 
228  void RemoveScalarVariables();
229 
231  void RemoveVectorVariables();
232 
234 
238  vtkSetMacro(ReplaceInvalidValues,int);
239  vtkGetMacro(ReplaceInvalidValues,int);
240  vtkBooleanMacro(ReplaceInvalidValues,int);
241  vtkSetMacro(ReplacementValue,double);
242  vtkGetMacro(ReplacementValue,double);
244 
246  void CheckExpression(int &pos, char **error);
247 
249  void InvalidateFunction();
250 
251 protected:
254 
255  int Parse();
256 
258  bool Evaluate();
259 
260  int CheckSyntax();
261 
262  void CopyParseError(int &position, char **error);
263 
264  void RemoveSpaces();
265  char* RemoveSpacesFrom(const char* variableName);
266  int OperatorWithinVariable(int idx);
267 
268  int BuildInternalFunctionStructure();
269  void BuildInternalSubstringStructure(int beginIndex, int endIndex);
270  void AddInternalByte(unsigned char newByte);
271 
272  int IsSubstringCompletelyEnclosed(int beginIndex, int endIndex);
273  int FindEndOfMathFunction(int beginIndex);
274  int FindEndOfMathConstant(int beginIndex);
275 
276  int IsVariableName(int currentIndex);
277  int IsElementaryOperator(int op);
278 
279  int GetMathFunctionNumber(int currentIndex);
280  int GetMathFunctionNumberByCheckingParenthesis( int currentIndex );
281  int GetMathFunctionStringLength(int mathFunctionNumber);
282  int GetMathConstantNumber(int currentIndex);
283  int GetMathConstantStringLength(int mathConstantNumber);
284  unsigned char GetElementaryOperatorNumber(char op);
285  unsigned char GetOperandNumber(int currentIndex);
286  int GetVariableNameLength(int variableNumber);
287 
288  int DisambiguateOperators();
289 
290  vtkSetStringMacro(ParseError);
291 
292  int FindPositionInOriginalFunction(const int& pos);
293 
294  char* Function;
296 
304  unsigned char *ByteCode;
306  double *Immediates;
308  double *Stack;
311 
317 
320 
322  char* ParseError;
323 
324 private:
325  vtkFunctionParser(const vtkFunctionParser&); // Not implemented.
326  void operator=(const vtkFunctionParser&); // Not implemented.
327 };
328 
329 #endif
vtkTimeStamp VariableMTime
void SetVectorVariableValue(int i, const double values[3])
abstract base class for most VTK objects
Definition: vtkObject.h:61
vtkTimeStamp CheckMTime
#define VTKCOMMONMISC_EXPORT
record modification and/or execution time
Definition: vtkTimeStamp.h:34
void SetVectorVariableValue(const char *variableName, const double values[3])
virtual void PrintSelf(ostream &os, vtkIndent indent)
vtkTimeStamp FunctionMTime
Parse and evaluate a mathematical expression.
virtual unsigned long GetMTime()
a simple class to control print indentation
Definition: vtkIndent.h:38
void GetVectorVariableValue(const char *variableName, double value[3])
vtkTimeStamp EvaluateMTime
vtkTimeStamp ParseMTime
double ** VectorVariableValues
void GetVectorResult(double result[3])
void GetVectorVariableValue(int i, double value[3])
static vtkObject * New()
unsigned char * ByteCode