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_VECTOR_OVER_SCALAR 35
00089 #define VTK_PARSER_MAGNITUDE 36
00090 #define VTK_PARSER_NORMALIZE 37
00091 
00092 // constants involving vectors
00093 #define VTK_PARSER_IHAT 38
00094 #define VTK_PARSER_JHAT 39
00095 #define VTK_PARSER_KHAT 40
00096 
00097 // code for if(bool, trueval, falseval) resulting in a scalar
00098 #define VTK_PARSER_IF 41
00099 
00100 // code for if(bool, truevec, falsevec) resulting in a vector
00101 #define VTK_PARSER_VECTOR_IF 42
00102 
00103 // codes for boolean expressions
00104 #define VTK_PARSER_LESS_THAN 43
00105 
00106 // codes for boolean expressions
00107 #define VTK_PARSER_GREATER_THAN 44
00108 
00109 // codes for boolean expressions
00110 #define VTK_PARSER_EQUAL_TO 45
00111 
00112 // codes for boolean expressions
00113 #define VTK_PARSER_AND 46
00114 
00115 // codes for boolean expressions
00116 #define VTK_PARSER_OR 47
00117 
00118 // codes for scalar variables come before those for vectors. Do not define
00119 // values for VTK_PARSER_BEGIN_VARIABLES+1, VTK_PARSER_BEGIN_VARIABLES+2, ...,
00120 // because they are used to look up variables numbered 1, 2, ...
00121 #define VTK_PARSER_BEGIN_VARIABLES 48
00122 
00123 // the value that is retuned as a result if there is an error
00124 #define VTK_PARSER_ERROR_RESULT VTK_LARGE_FLOAT
00125 
00126 class VTK_COMMON_EXPORT vtkFunctionParser : public vtkObject
00127 {
00128 public:
00129   static vtkFunctionParser *New();
00130   vtkTypeMacro(vtkFunctionParser, vtkObject);
00131   void PrintSelf(ostream& os, vtkIndent indent);
00132 
00134 
00135   void SetFunction(const char *function);
00136   vtkGetStringMacro(Function);
00138 
00141   int IsScalarResult();
00142 
00145   int IsVectorResult();
00146 
00148   double GetScalarResult();
00149 
00151 
00152   double* GetVectorResult();
00153   void GetVectorResult(double result[3]) {
00154     double *r = this->GetVectorResult();
00155     result[0] = r[0]; result[1] = r[1]; result[2] = r[2]; };
00157 
00159 
00163   void SetScalarVariableValue(const char* variableName, double value);
00164   void SetScalarVariableValue(int i, double value);
00166 
00168 
00169   double GetScalarVariableValue(const char* variableName);
00170   double GetScalarVariableValue(int i);
00172 
00174 
00178   void SetVectorVariableValue(const char* variableName, double xValue,
00179                               double yValue, double zValue);
00180   void SetVectorVariableValue(const char* variableName,
00181                               const double values[3]) {
00182     this->SetVectorVariableValue(variableName,values[0],values[1],values[2]);};
00183   void SetVectorVariableValue(int i, double xValue, double yValue,
00184                               double zValue);
00185   void SetVectorVariableValue(int i, const double values[3]) {
00186     this->SetVectorVariableValue(i,values[0],values[1],values[2]);};
00188 
00190 
00191   double* GetVectorVariableValue(const char* variableName);
00192   void GetVectorVariableValue(const char* variableName, double value[3]) {
00193     double *r = this->GetVectorVariableValue(variableName);
00194     value[0] = r[0]; value[1] = r[1]; value[2] = r[2]; };
00195   double* GetVectorVariableValue(int i);
00196   void GetVectorVariableValue(int i, double value[3]) {
00197     double *r = this->GetVectorVariableValue(i);
00198     value[0] = r[0]; value[1] = r[1]; value[2] = r[2]; };
00200 
00202 
00203   vtkGetMacro(NumberOfScalarVariables,int);
00205 
00207 
00208   vtkGetMacro(NumberOfVectorVariables,int);
00210 
00212   char* GetScalarVariableName(int i);
00213 
00215   char* GetVectorVariableName(int i);
00216 
00218   void RemoveAllVariables();
00219 
00221   void RemoveScalarVariables();
00222 
00224   void RemoveVectorVariables();
00225 
00227 
00231   vtkSetMacro(ReplaceInvalidValues,int);
00232   vtkGetMacro(ReplaceInvalidValues,int);
00233   vtkBooleanMacro(ReplaceInvalidValues,int);
00234   vtkSetMacro(ReplacementValue,double);
00235   vtkGetMacro(ReplacementValue,double);
00237 
00239   void CheckExpression(int &pos, char **error);
00240 
00241 protected:
00242   vtkFunctionParser();
00243   ~vtkFunctionParser();
00244 
00245   int Parse();
00246 
00248   bool Evaluate();
00249 
00250   int CheckSyntax();
00251 
00252   void CopyParseError(int &position, char **error);
00253 
00254   void RemoveSpaces();
00255   char* RemoveSpacesFrom(const char* variableName);
00256   int OperatorWithinVariable(int idx);
00257 
00258   int BuildInternalFunctionStructure();
00259   void BuildInternalSubstringStructure(int beginIndex, int endIndex);
00260   void AddInternalByte(unsigned char newByte);
00261 
00262   int IsSubstringCompletelyEnclosed(int beginIndex, int endIndex);
00263   int FindEndOfMathFunction(int beginIndex);
00264   int FindEndOfMathConstant(int beginIndex);
00265 
00266   int IsVariableName(int currentIndex);
00267   int IsElementaryOperator(int op);
00268 
00269   int GetMathFunctionNumber(int currentIndex);
00270   int GetMathFunctionNumberByCheckingParenthesis( int currentIndex );
00271   int GetMathFunctionStringLength(int mathFunctionNumber);
00272   int GetMathConstantNumber(int currentIndex);
00273   int GetMathConstantStringLength(int mathConstantNumber);
00274   unsigned char GetElementaryOperatorNumber(char op);
00275   unsigned char GetOperandNumber(int currentIndex);
00276   int GetVariableNameLength(int variableNumber);
00277 
00278   int DisambiguateOperators();
00279 
00280   vtkSetStringMacro(ParseError);
00281 
00282   int FindPositionInOriginalFunction(const int& pos);
00283 
00284   char* Function;
00285   char* FunctionWithSpaces;
00286 
00287   int FunctionLength;
00288   int NumberOfScalarVariables;
00289   int NumberOfVectorVariables;
00290   char** ScalarVariableNames;
00291   char** VectorVariableNames;
00292   double* ScalarVariableValues;
00293   double** VectorVariableValues;
00294   unsigned char *ByteCode;
00295   int ByteCodeSize;
00296   double *Immediates;
00297   int ImmediatesSize;
00298   double *Stack;
00299   int StackSize;
00300   int StackPointer;
00301 
00302   vtkTimeStamp FunctionMTime;
00303   vtkTimeStamp ParseMTime;
00304   vtkTimeStamp VariableMTime;
00305   vtkTimeStamp EvaluateMTime;
00306   vtkTimeStamp CheckMTime;
00307 
00308   int ReplaceInvalidValues;
00309   double ReplacementValue;
00310 
00311   int   ParseErrorPositon;
00312   char* ParseError;
00313 
00314 private:
00315   vtkFunctionParser(const vtkFunctionParser&);  // Not implemented.
00316   void operator=(const vtkFunctionParser&);  // Not implemented.
00317 };
00318 
00319 #endif