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