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 =========================================================================*/
45 #ifndef __vtkFunctionParser_h
46 #define __vtkFunctionParser_h
47 
48 #include "vtkObject.h"
49 
50 #define VTK_PARSER_IMMEDIATE 1
51 #define VTK_PARSER_UNARY_MINUS 2
52 
53 // supported math functions
54 #define VTK_PARSER_ADD 3
55 #define VTK_PARSER_SUBTRACT 4
56 #define VTK_PARSER_MULTIPLY 5
57 #define VTK_PARSER_DIVIDE 6
58 #define VTK_PARSER_POWER 7
59 #define VTK_PARSER_ABSOLUTE_VALUE 8
60 #define VTK_PARSER_EXPONENT 9
61 #define VTK_PARSER_CEILING 10
62 #define VTK_PARSER_FLOOR 11
63 #define VTK_PARSER_LOGARITHM 12
64 #define VTK_PARSER_LOGARITHME 13
65 #define VTK_PARSER_LOGARITHM10 14
66 #define VTK_PARSER_SQUARE_ROOT 15
67 #define VTK_PARSER_SINE 16
68 #define VTK_PARSER_COSINE 17
69 #define VTK_PARSER_TANGENT 18
70 #define VTK_PARSER_ARCSINE 19
71 #define VTK_PARSER_ARCCOSINE 20
72 #define VTK_PARSER_ARCTANGENT 21
73 #define VTK_PARSER_HYPERBOLIC_SINE 22
74 #define VTK_PARSER_HYPERBOLIC_COSINE 23
75 #define VTK_PARSER_HYPERBOLIC_TANGENT 24
76 #define VTK_PARSER_MIN 25
77 #define VTK_PARSER_MAX 26
78 #define VTK_PARSER_CROSS 27
79 #define VTK_PARSER_SIGN 28
80 
81 // functions involving vectors
82 #define VTK_PARSER_VECTOR_UNARY_MINUS 29
83 #define VTK_PARSER_DOT_PRODUCT 30
84 #define VTK_PARSER_VECTOR_ADD 31
85 #define VTK_PARSER_VECTOR_SUBTRACT 32
86 #define VTK_PARSER_SCALAR_TIMES_VECTOR 33
87 #define VTK_PARSER_VECTOR_TIMES_SCALAR 34
88 #define VTK_PARSER_VECTOR_OVER_SCALAR 35
89 #define VTK_PARSER_MAGNITUDE 36
90 #define VTK_PARSER_NORMALIZE 37
91 
92 // constants involving vectors
93 #define VTK_PARSER_IHAT 38
94 #define VTK_PARSER_JHAT 39
95 #define VTK_PARSER_KHAT 40
96 
97 // code for if(bool, trueval, falseval) resulting in a scalar
98 #define VTK_PARSER_IF 41
99 
100 // code for if(bool, truevec, falsevec) resulting in a vector
101 #define VTK_PARSER_VECTOR_IF 42
102 
103 // codes for boolean expressions
104 #define VTK_PARSER_LESS_THAN 43
105 
106 // codes for boolean expressions
107 #define VTK_PARSER_GREATER_THAN 44
108 
109 // codes for boolean expressions
110 #define VTK_PARSER_EQUAL_TO 45
111 
112 // codes for boolean expressions
113 #define VTK_PARSER_AND 46
114 
115 // codes for boolean expressions
116 #define VTK_PARSER_OR 47
117 
118 // codes for scalar variables come before those for vectors. Do not define
119 // values for VTK_PARSER_BEGIN_VARIABLES+1, VTK_PARSER_BEGIN_VARIABLES+2, ...,
120 // because they are used to look up variables numbered 1, 2, ...
121 #define VTK_PARSER_BEGIN_VARIABLES 48
122 
123 // the value that is retuned as a result if there is an error
124 #define VTK_PARSER_ERROR_RESULT VTK_LARGE_FLOAT
125 
127 {
128 public:
129  static vtkFunctionParser *New();
130  vtkTypeMacro(vtkFunctionParser, vtkObject);
131  void PrintSelf(ostream& os, vtkIndent indent);
132 
134 
135  void SetFunction(const char *function);
136  vtkGetStringMacro(Function);
138 
141  int IsScalarResult();
142 
145  int IsVectorResult();
146 
148  double GetScalarResult();
149 
151 
152  double* GetVectorResult();
153  void GetVectorResult(double result[3]) {
154  double *r = this->GetVectorResult();
155  result[0] = r[0]; result[1] = r[1]; result[2] = r[2]; };
157 
159 
163  void SetScalarVariableValue(const char* variableName, double value);
164  void SetScalarVariableValue(int i, double value);
166 
168 
169  double GetScalarVariableValue(const char* variableName);
170  double GetScalarVariableValue(int i);
172 
174 
178  void SetVectorVariableValue(const char* variableName, double xValue,
179  double yValue, double zValue);
180  void SetVectorVariableValue(const char* variableName,
181  const double values[3]) {
182  this->SetVectorVariableValue(variableName,values[0],values[1],values[2]);};
183  void SetVectorVariableValue(int i, double xValue, double yValue,
184  double zValue);
185  void SetVectorVariableValue(int i, const double values[3]) {
186  this->SetVectorVariableValue(i,values[0],values[1],values[2]);};
188 
190 
191  double* GetVectorVariableValue(const char* variableName);
192  void GetVectorVariableValue(const char* variableName, double value[3]) {
193  double *r = this->GetVectorVariableValue(variableName);
194  value[0] = r[0]; value[1] = r[1]; value[2] = r[2]; };
195  double* GetVectorVariableValue(int i);
196  void GetVectorVariableValue(int i, double value[3]) {
197  double *r = this->GetVectorVariableValue(i);
198  value[0] = r[0]; value[1] = r[1]; value[2] = r[2]; };
200 
202 
203  vtkGetMacro(NumberOfScalarVariables,int);
205 
207 
208  vtkGetMacro(NumberOfVectorVariables,int);
210 
212  char* GetScalarVariableName(int i);
213 
215  char* GetVectorVariableName(int i);
216 
218  void RemoveAllVariables();
219 
221  void RemoveScalarVariables();
222 
224  void RemoveVectorVariables();
225 
227 
231  vtkSetMacro(ReplaceInvalidValues,int);
232  vtkGetMacro(ReplaceInvalidValues,int);
233  vtkBooleanMacro(ReplaceInvalidValues,int);
234  vtkSetMacro(ReplacementValue,double);
235  vtkGetMacro(ReplacementValue,double);
237 
239  void CheckExpression(int &pos, char **error);
240 
241 protected:
244 
245  int Parse();
246 
248  bool Evaluate();
249 
250  int CheckSyntax();
251 
252  void CopyParseError(int &position, char **error);
253 
254  void RemoveSpaces();
255  char* RemoveSpacesFrom(const char* variableName);
256  int OperatorWithinVariable(int idx);
257 
258  int BuildInternalFunctionStructure();
259  void BuildInternalSubstringStructure(int beginIndex, int endIndex);
260  void AddInternalByte(unsigned char newByte);
261 
262  int IsSubstringCompletelyEnclosed(int beginIndex, int endIndex);
263  int FindEndOfMathFunction(int beginIndex);
264  int FindEndOfMathConstant(int beginIndex);
265 
266  int IsVariableName(int currentIndex);
267  int IsElementaryOperator(int op);
268 
269  int GetMathFunctionNumber(int currentIndex);
270  int GetMathFunctionNumberByCheckingParenthesis( int currentIndex );
271  int GetMathFunctionStringLength(int mathFunctionNumber);
272  int GetMathConstantNumber(int currentIndex);
273  int GetMathConstantStringLength(int mathConstantNumber);
274  unsigned char GetElementaryOperatorNumber(char op);
275  unsigned char GetOperandNumber(int currentIndex);
276  int GetVariableNameLength(int variableNumber);
277 
278  int DisambiguateOperators();
279 
280  vtkSetStringMacro(ParseError);
281 
282  int FindPositionInOriginalFunction(const int& pos);
283 
284  char* Function;
286 
294  unsigned char *ByteCode;
296  double *Immediates;
298  double *Stack;
301 
307 
310 
312  char* ParseError;
313 
314 private:
315  vtkFunctionParser(const vtkFunctionParser&); // Not implemented.
316  void operator=(const vtkFunctionParser&); // Not implemented.
317 };
318 
319 #endif