VTK
dox/Common/vtkFunctionParser.h
Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Visualization Toolkit
00004   Module:    vtkFunctionParser.h
00005 
00006   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
00007   All rights reserved.
00008   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
00009 
00010      This software is distributed WITHOUT ANY WARRANTY; without even
00011      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00012      PURPOSE.  See the above copyright notice for more information.
00013 
00014 =========================================================================*/
00045 #ifndef __vtkFunctionParser_h
00046 #define __vtkFunctionParser_h
00047 
00048 #include "vtkObject.h"
00049 
00050 #define VTK_PARSER_IMMEDIATE 1
00051 #define VTK_PARSER_UNARY_MINUS 2
00052 
00053 // supported math functions
00054 #define VTK_PARSER_ADD 3
00055 #define VTK_PARSER_SUBTRACT 4
00056 #define VTK_PARSER_MULTIPLY 5
00057 #define VTK_PARSER_DIVIDE 6
00058 #define VTK_PARSER_POWER 7
00059 #define VTK_PARSER_ABSOLUTE_VALUE 8
00060 #define VTK_PARSER_EXPONENT 9
00061 #define VTK_PARSER_CEILING 10
00062 #define VTK_PARSER_FLOOR 11
00063 #define VTK_PARSER_LOGARITHM 12
00064 #define VTK_PARSER_LOGARITHME 13
00065 #define VTK_PARSER_LOGARITHM10 14
00066 #define VTK_PARSER_SQUARE_ROOT 15
00067 #define VTK_PARSER_SINE 16
00068 #define VTK_PARSER_COSINE 17
00069 #define VTK_PARSER_TANGENT 18
00070 #define VTK_PARSER_ARCSINE 19
00071 #define VTK_PARSER_ARCCOSINE 20
00072 #define VTK_PARSER_ARCTANGENT 21
00073 #define VTK_PARSER_HYPERBOLIC_SINE 22
00074 #define VTK_PARSER_HYPERBOLIC_COSINE 23
00075 #define VTK_PARSER_HYPERBOLIC_TANGENT 24
00076 #define VTK_PARSER_MIN 25
00077 #define VTK_PARSER_MAX 26
00078 #define VTK_PARSER_CROSS 27
00079 #define VTK_PARSER_SIGN 28
00080 
00081 // functions involving vectors
00082 #define VTK_PARSER_VECTOR_UNARY_MINUS 29
00083 #define VTK_PARSER_DOT_PRODUCT 30
00084 #define VTK_PARSER_VECTOR_ADD 31
00085 #define VTK_PARSER_VECTOR_SUBTRACT 32
00086 #define VTK_PARSER_SCALAR_TIMES_VECTOR 33
00087 #define VTK_PARSER_VECTOR_TIMES_SCALAR 34
00088 #define VTK_PARSER_MAGNITUDE 35
00089 #define VTK_PARSER_NORMALIZE 36
00090 
00091 // constants involving vectors
00092 #define VTK_PARSER_IHAT 37
00093 #define VTK_PARSER_JHAT 38
00094 #define VTK_PARSER_KHAT 39
00095 
00096 // code for if(bool, trueval, falseval) resulting in a scalar
00097 #define VTK_PARSER_IF 40
00098 
00099 // code for if(bool, truevec, falsevec) resulting in a vector
00100 #define VTK_PARSER_VECTOR_IF 41
00101 
00102 // codes for boolean expressions
00103 #define VTK_PARSER_LESS_THAN 42
00104 
00105 // codes for boolean expressions
00106 #define VTK_PARSER_GREATER_THAN 43
00107 
00108 // codes for boolean expressions
00109 #define VTK_PARSER_EQUAL_TO 44
00110 
00111 // codes for boolean expressions
00112 #define VTK_PARSER_AND 45
00113 
00114 // codes for boolean expressions
00115 #define VTK_PARSER_OR 46
00116 
00117 // codes for scalar variables come before those for vectors. Do not define
00118 // values for VTK_PARSER_BEGIN_VARIABLES+1, VTK_PARSER_BEGIN_VARIABLES+2, ...,
00119 // because they are used to look up variables numbered 1, 2, ...
00120 #define VTK_PARSER_BEGIN_VARIABLES 47
00121 
00122 // the value that is retuned as a result if there is an error
00123 #define VTK_PARSER_ERROR_RESULT VTK_LARGE_FLOAT
00124 
00125 class VTK_COMMON_EXPORT vtkFunctionParser : public vtkObject
00126 {
00127 public:
00128   static vtkFunctionParser *New();
00129   vtkTypeMacro(vtkFunctionParser, vtkObject);
00130   void PrintSelf(ostream& os, vtkIndent indent);
00131 
00133 
00134   void SetFunction(const char *function);
00135   vtkGetStringMacro(Function);
00137 
00140   int IsScalarResult();
00141 
00144   int IsVectorResult();
00145 
00147   double GetScalarResult();
00148 
00150 
00151   double* GetVectorResult();
00152   void GetVectorResult(double result[3]) {
00153     double *r = this->GetVectorResult();
00154     result[0] = r[0]; result[1] = r[1]; result[2] = r[2]; };
00156 
00158 
00162   void SetScalarVariableValue(const char* variableName, double value);
00163   void SetScalarVariableValue(int i, double value);
00165 
00167 
00168   double GetScalarVariableValue(const char* variableName);
00169   double GetScalarVariableValue(int i);
00171 
00173 
00177   void SetVectorVariableValue(const char* variableName, double xValue,
00178                               double yValue, double zValue);
00179   void SetVectorVariableValue(const char* variableName,
00180                               const double values[3]) {
00181     this->SetVectorVariableValue(variableName,values[0],values[1],values[2]);};
00182   void SetVectorVariableValue(int i, double xValue, double yValue,
00183                               double zValue);
00184   void SetVectorVariableValue(int i, const double values[3]) {
00185     this->SetVectorVariableValue(i,values[0],values[1],values[2]);};
00187 
00189 
00190   double* GetVectorVariableValue(const char* variableName);
00191   void GetVectorVariableValue(const char* variableName, double value[3]) {
00192     double *r = this->GetVectorVariableValue(variableName);
00193     value[0] = r[0]; value[1] = r[1]; value[2] = r[2]; };
00194   double* GetVectorVariableValue(int i);
00195   void GetVectorVariableValue(int i, double value[3]) {
00196     double *r = this->GetVectorVariableValue(i);
00197     value[0] = r[0]; value[1] = r[1]; value[2] = r[2]; };
00199 
00201 
00202   vtkGetMacro(NumberOfScalarVariables,int);
00204 
00206 
00207   vtkGetMacro(NumberOfVectorVariables,int);
00209 
00211   char* GetScalarVariableName(int i);
00212 
00214   char* GetVectorVariableName(int i);
00215 
00217   void RemoveAllVariables();
00218 
00220   void RemoveScalarVariables();
00221 
00223   void RemoveVectorVariables();
00224 
00226 
00230   vtkSetMacro(ReplaceInvalidValues,int);
00231   vtkGetMacro(ReplaceInvalidValues,int);
00232   vtkBooleanMacro(ReplaceInvalidValues,int);
00233   vtkSetMacro(ReplacementValue,double);
00234   vtkGetMacro(ReplacementValue,double);
00236 
00238   void CheckExpression(int &pos, char **error);
00239 
00240 protected:
00241   vtkFunctionParser();
00242   ~vtkFunctionParser();
00243 
00244   int Parse();
00245 
00247   bool Evaluate();
00248 
00249   int CheckSyntax();
00250 
00251   void CopyParseError(int &position, char **error);
00252 
00253   void RemoveSpaces();
00254   char* RemoveSpacesFrom(const char* variableName);
00255   int OperatorWithinVariable(int idx);
00256 
00257   int BuildInternalFunctionStructure();
00258   void BuildInternalSubstringStructure(int beginIndex, int endIndex);
00259   void AddInternalByte(unsigned char newByte);
00260 
00261   int IsSubstringCompletelyEnclosed(int beginIndex, int endIndex);
00262   int FindEndOfMathFunction(int beginIndex);
00263   int FindEndOfMathConstant(int beginIndex);
00264 
00265   int IsVariableName(int currentIndex);
00266   int IsElementaryOperator(int op);
00267 
00268   int GetMathFunctionNumber(int currentIndex);
00269   int GetMathFunctionNumberByCheckingParenthesis( int currentIndex );
00270   int GetMathFunctionStringLength(int mathFunctionNumber);
00271   int GetMathConstantNumber(int currentIndex);
00272   int GetMathConstantStringLength(int mathConstantNumber);
00273   unsigned char GetElementaryOperatorNumber(char op);
00274   unsigned char GetOperandNumber(int currentIndex);
00275   int GetVariableNameLength(int variableNumber);
00276 
00277   int DisambiguateOperators();
00278 
00279   vtkSetStringMacro(ParseError);
00280 
00281   int FindPositionInOriginalFunction(const int& pos);
00282 
00283   char* Function;
00284   char* FunctionWithSpaces;
00285 
00286   int FunctionLength;
00287   int NumberOfScalarVariables;
00288   int NumberOfVectorVariables;
00289   char** ScalarVariableNames;
00290   char** VectorVariableNames;
00291   double* ScalarVariableValues;
00292   double** VectorVariableValues;
00293   unsigned char *ByteCode;
00294   int ByteCodeSize;
00295   double *Immediates;
00296   int ImmediatesSize;
00297   double *Stack;
00298   int StackSize;
00299   int StackPointer;
00300 
00301   vtkTimeStamp FunctionMTime;
00302   vtkTimeStamp ParseMTime;
00303   vtkTimeStamp VariableMTime;
00304   vtkTimeStamp EvaluateMTime;
00305   vtkTimeStamp CheckMTime;
00306 
00307   int ReplaceInvalidValues;
00308   double ReplacementValue;
00309 
00310   int   ParseErrorPositon;
00311   char* ParseError;
00312 
00313 private:
00314   vtkFunctionParser(const vtkFunctionParser&);  // Not implemented.
00315   void operator=(const vtkFunctionParser&);  // Not implemented.
00316 };
00317 
00318 #endif