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 #define VTK_PARSER_UNARY_PLUS 3
57 
58 // supported math functions
59 #define VTK_PARSER_ADD 4
60 #define VTK_PARSER_SUBTRACT 5
61 #define VTK_PARSER_MULTIPLY 6
62 #define VTK_PARSER_DIVIDE 7
63 #define VTK_PARSER_POWER 8
64 #define VTK_PARSER_ABSOLUTE_VALUE 9
65 #define VTK_PARSER_EXPONENT 10
66 #define VTK_PARSER_CEILING 11
67 #define VTK_PARSER_FLOOR 12
68 #define VTK_PARSER_LOGARITHM 13
69 #define VTK_PARSER_LOGARITHME 14
70 #define VTK_PARSER_LOGARITHM10 15
71 #define VTK_PARSER_SQUARE_ROOT 16
72 #define VTK_PARSER_SINE 17
73 #define VTK_PARSER_COSINE 18
74 #define VTK_PARSER_TANGENT 19
75 #define VTK_PARSER_ARCSINE 20
76 #define VTK_PARSER_ARCCOSINE 21
77 #define VTK_PARSER_ARCTANGENT 22
78 #define VTK_PARSER_HYPERBOLIC_SINE 23
79 #define VTK_PARSER_HYPERBOLIC_COSINE 24
80 #define VTK_PARSER_HYPERBOLIC_TANGENT 25
81 #define VTK_PARSER_MIN 26
82 #define VTK_PARSER_MAX 27
83 #define VTK_PARSER_SIGN 29
84 
85 // functions involving vectors
86 #define VTK_PARSER_CROSS 28
87 #define VTK_PARSER_VECTOR_UNARY_MINUS 30
88 #define VTK_PARSER_VECTOR_UNARY_PLUS 31
89 #define VTK_PARSER_DOT_PRODUCT 32
90 #define VTK_PARSER_VECTOR_ADD 33
91 #define VTK_PARSER_VECTOR_SUBTRACT 34
92 #define VTK_PARSER_SCALAR_TIMES_VECTOR 35
93 #define VTK_PARSER_VECTOR_TIMES_SCALAR 36
94 #define VTK_PARSER_VECTOR_OVER_SCALAR 37
95 #define VTK_PARSER_MAGNITUDE 38
96 #define VTK_PARSER_NORMALIZE 39
97 
98 // constants involving vectors
99 #define VTK_PARSER_IHAT 40
100 #define VTK_PARSER_JHAT 41
101 #define VTK_PARSER_KHAT 42
102 
103 // code for if(bool, trueval, falseval) resulting in a scalar
104 #define VTK_PARSER_IF 43
105 
106 // code for if(bool, truevec, falsevec) resulting in a vector
107 #define VTK_PARSER_VECTOR_IF 44
108 
109 // codes for boolean expressions
110 #define VTK_PARSER_LESS_THAN 45
111 
112 // codes for boolean expressions
113 #define VTK_PARSER_GREATER_THAN 46
114 
115 // codes for boolean expressions
116 #define VTK_PARSER_EQUAL_TO 47
117 
118 // codes for boolean expressions
119 #define VTK_PARSER_AND 48
120 
121 // codes for boolean expressions
122 #define VTK_PARSER_OR 49
123 
124 // codes for scalar variables come before those for vectors. Do not define
125 // values for VTK_PARSER_BEGIN_VARIABLES+1, VTK_PARSER_BEGIN_VARIABLES+2, ...,
126 // because they are used to look up variables numbered 1, 2, ...
127 #define VTK_PARSER_BEGIN_VARIABLES 50
128 
129 // the value that is retuned as a result if there is an error
130 #define VTK_PARSER_ERROR_RESULT VTK_FLOAT_MAX
131 
133 {
134 public:
135  static vtkFunctionParser *New();
136  vtkTypeMacro(vtkFunctionParser, vtkObject);
137  void PrintSelf(ostream& os, vtkIndent indent);
138 
140  unsigned long int GetMTime();
141 
143 
144  void SetFunction(const char *function);
145  vtkGetStringMacro(Function);
147 
150  int IsScalarResult();
151 
154  int IsVectorResult();
155 
157  double GetScalarResult();
158 
160 
161  double* GetVectorResult();
162  void GetVectorResult(double result[3]) {
163  double *r = this->GetVectorResult();
164  result[0] = r[0]; result[1] = r[1]; result[2] = r[2]; };
166 
168 
172  void SetScalarVariableValue(const char* variableName, double value);
173  void SetScalarVariableValue(int i, double value);
175 
177 
178  double GetScalarVariableValue(const char* variableName);
179  double GetScalarVariableValue(int i);
181 
183 
187  void SetVectorVariableValue(const char* variableName, double xValue,
188  double yValue, double zValue);
189  void SetVectorVariableValue(const char* variableName,
190  const double values[3]) {
191  this->SetVectorVariableValue(variableName,values[0],values[1],values[2]);};
192  void SetVectorVariableValue(int i, double xValue, double yValue,
193  double zValue);
194  void SetVectorVariableValue(int i, const double values[3]) {
195  this->SetVectorVariableValue(i,values[0],values[1],values[2]);};
197 
199 
200  double* GetVectorVariableValue(const char* variableName);
201  void GetVectorVariableValue(const char* variableName, double value[3]) {
202  double *r = this->GetVectorVariableValue(variableName);
203  value[0] = r[0]; value[1] = r[1]; value[2] = r[2]; };
204  double* GetVectorVariableValue(int i);
205  void GetVectorVariableValue(int i, double value[3]) {
206  double *r = this->GetVectorVariableValue(i);
207  value[0] = r[0]; value[1] = r[1]; value[2] = r[2]; };
209 
211 
212  vtkGetMacro(NumberOfScalarVariables,int);
214 
216 
217  vtkGetMacro(NumberOfVectorVariables,int);
219 
221  char* GetScalarVariableName(int i);
222 
224  char* GetVectorVariableName(int i);
225 
227  void RemoveAllVariables();
228 
230  void RemoveScalarVariables();
231 
233  void RemoveVectorVariables();
234 
236 
240  vtkSetMacro(ReplaceInvalidValues,int);
241  vtkGetMacro(ReplaceInvalidValues,int);
242  vtkBooleanMacro(ReplaceInvalidValues,int);
243  vtkSetMacro(ReplacementValue,double);
244  vtkGetMacro(ReplacementValue,double);
246 
248  void CheckExpression(int &pos, char **error);
249 
251  void InvalidateFunction();
252 
253 protected:
256 
257  int Parse();
258 
260  bool Evaluate();
261 
262  int CheckSyntax();
263 
264  void CopyParseError(int &position, char **error);
265 
266  void RemoveSpaces();
267  char* RemoveSpacesFrom(const char* variableName);
268  int OperatorWithinVariable(int idx);
269 
270  int BuildInternalFunctionStructure();
271  void BuildInternalSubstringStructure(int beginIndex, int endIndex);
272  void AddInternalByte(unsigned char newByte);
273 
274  int IsSubstringCompletelyEnclosed(int beginIndex, int endIndex);
275  int FindEndOfMathFunction(int beginIndex);
276  int FindEndOfMathConstant(int beginIndex);
277 
278  int IsVariableName(int currentIndex);
279  int IsElementaryOperator(int op);
280 
281  int GetMathFunctionNumber(int currentIndex);
282  int GetMathFunctionNumberByCheckingParenthesis( int currentIndex );
283  int GetMathFunctionStringLength(int mathFunctionNumber);
284  int GetMathConstantNumber(int currentIndex);
285  int GetMathConstantStringLength(int mathConstantNumber);
286  unsigned char GetElementaryOperatorNumber(char op);
287  unsigned char GetOperandNumber(int currentIndex);
288  int GetVariableNameLength(int variableNumber);
289 
290  int DisambiguateOperators();
291 
292  vtkSetStringMacro(ParseError);
293 
294  int FindPositionInOriginalFunction(const int& pos);
295 
296  char* Function;
298 
306  unsigned char *ByteCode;
308  double *Immediates;
310  double *Stack;
313 
319 
322 
324  char* ParseError;
325 
326 private:
327  vtkFunctionParser(const vtkFunctionParser&); // Not implemented.
328  void operator=(const vtkFunctionParser&); // Not implemented.
329 };
330 
331 #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