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_MAGNITUDE 35
89 #define VTK_PARSER_NORMALIZE 36
90 
91 // constants involving vectors
92 #define VTK_PARSER_IHAT 37
93 #define VTK_PARSER_JHAT 38
94 #define VTK_PARSER_KHAT 39
95 
96 // code for if(bool, trueval, falseval) resulting in a scalar
97 #define VTK_PARSER_IF 40
98 
99 // code for if(bool, truevec, falsevec) resulting in a vector
100 #define VTK_PARSER_VECTOR_IF 41
101 
102 // codes for boolean expressions
103 #define VTK_PARSER_LESS_THAN 42
104 
105 // codes for boolean expressions
106 #define VTK_PARSER_GREATER_THAN 43
107 
108 // codes for boolean expressions
109 #define VTK_PARSER_EQUAL_TO 44
110 
111 // codes for boolean expressions
112 #define VTK_PARSER_AND 45
113 
114 // codes for boolean expressions
115 #define VTK_PARSER_OR 46
116 
117 // codes for scalar variables come before those for vectors. Do not define
118 // values for VTK_PARSER_BEGIN_VARIABLES+1, VTK_PARSER_BEGIN_VARIABLES+2, ...,
119 // because they are used to look up variables numbered 1, 2, ...
120 #define VTK_PARSER_BEGIN_VARIABLES 47
121 
122 // the value that is retuned as a result if there is an error
123 #define VTK_PARSER_ERROR_RESULT VTK_LARGE_FLOAT
124 
126 {
127 public:
128  static vtkFunctionParser *New();
129  vtkTypeMacro(vtkFunctionParser, vtkObject);
130  void PrintSelf(ostream& os, vtkIndent indent);
131 
133 
134  void SetFunction(const char *function);
135  vtkGetStringMacro(Function);
137 
140  int IsScalarResult();
141 
144  int IsVectorResult();
145 
147  double GetScalarResult();
148 
150 
151  double* GetVectorResult();
152  void GetVectorResult(double result[3]) {
153  double *r = this->GetVectorResult();
154  result[0] = r[0]; result[1] = r[1]; result[2] = r[2]; };
156 
158 
162  void SetScalarVariableValue(const char* variableName, double value);
163  void SetScalarVariableValue(int i, double value);
165 
167 
168  double GetScalarVariableValue(const char* variableName);
169  double GetScalarVariableValue(int i);
171 
173 
177  void SetVectorVariableValue(const char* variableName, double xValue,
178  double yValue, double zValue);
179  void SetVectorVariableValue(const char* variableName,
180  const double values[3]) {
181  this->SetVectorVariableValue(variableName,values[0],values[1],values[2]);};
182  void SetVectorVariableValue(int i, double xValue, double yValue,
183  double zValue);
184  void SetVectorVariableValue(int i, const double values[3]) {
185  this->SetVectorVariableValue(i,values[0],values[1],values[2]);};
187 
189 
190  double* GetVectorVariableValue(const char* variableName);
191  void GetVectorVariableValue(const char* variableName, double value[3]) {
192  double *r = this->GetVectorVariableValue(variableName);
193  value[0] = r[0]; value[1] = r[1]; value[2] = r[2]; };
194  double* GetVectorVariableValue(int i);
195  void GetVectorVariableValue(int i, double value[3]) {
196  double *r = this->GetVectorVariableValue(i);
197  value[0] = r[0]; value[1] = r[1]; value[2] = r[2]; };
199 
201 
202  vtkGetMacro(NumberOfScalarVariables,int);
204 
206 
207  vtkGetMacro(NumberOfVectorVariables,int);
209 
211  char* GetScalarVariableName(int i);
212 
214  char* GetVectorVariableName(int i);
215 
217  void RemoveAllVariables();
218 
220  void RemoveScalarVariables();
221 
223  void RemoveVectorVariables();
224 
226 
230  vtkSetMacro(ReplaceInvalidValues,int);
231  vtkGetMacro(ReplaceInvalidValues,int);
232  vtkBooleanMacro(ReplaceInvalidValues,int);
233  vtkSetMacro(ReplacementValue,double);
234  vtkGetMacro(ReplacementValue,double);
236 
238  void CheckExpression(int &pos, char **error);
239 
240 protected:
243 
244  int Parse();
245 
247  bool Evaluate();
248 
249  int CheckSyntax();
250 
251  void CopyParseError(int &position, char **error);
252 
253  void RemoveSpaces();
254  char* RemoveSpacesFrom(const char* variableName);
255  int OperatorWithinVariable(int idx);
256 
257  int BuildInternalFunctionStructure();
258  void BuildInternalSubstringStructure(int beginIndex, int endIndex);
259  void AddInternalByte(unsigned char newByte);
260 
261  int IsSubstringCompletelyEnclosed(int beginIndex, int endIndex);
262  int FindEndOfMathFunction(int beginIndex);
263  int FindEndOfMathConstant(int beginIndex);
264 
265  int IsVariableName(int currentIndex);
266  int IsElementaryOperator(int op);
267 
268  int GetMathFunctionNumber(int currentIndex);
269  int GetMathFunctionNumberByCheckingParenthesis( int currentIndex );
270  int GetMathFunctionStringLength(int mathFunctionNumber);
271  int GetMathConstantNumber(int currentIndex);
272  int GetMathConstantStringLength(int mathConstantNumber);
273  unsigned char GetElementaryOperatorNumber(char op);
274  unsigned char GetOperandNumber(int currentIndex);
275  int GetVariableNameLength(int variableNumber);
276 
277  int DisambiguateOperators();
278 
279  vtkSetStringMacro(ParseError);
280 
281  int FindPositionInOriginalFunction(const int& pos);
282 
283  char* Function;
285 
293  unsigned char *ByteCode;
295  double *Immediates;
297  double *Stack;
300 
306 
309 
311  char* ParseError;
312 
313 private:
314  vtkFunctionParser(const vtkFunctionParser&); // Not implemented.
315  void operator=(const vtkFunctionParser&); // Not implemented.
316 };
317 
318 #endif
vtkTimeStamp VariableMTime
void SetVectorVariableValue(int i, const double values[3])
abstract base class for most VTK objects
Definition: vtkObject.h:60
vtkTimeStamp CheckMTime
record modification and/or execution time
Definition: vtkTimeStamp.h:33
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:37
void GetVectorVariableValue(const char *variableName, double value[3])
#define VTK_COMMON_EXPORT
vtkTimeStamp EvaluateMTime
vtkTimeStamp ParseMTime
double ** VectorVariableValues
void GetVectorResult(double result[3])
void GetVectorVariableValue(int i, double value[3])
static vtkObject * New()
unsigned char * ByteCode