VTK
|
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 =========================================================================*/ 00038 #ifndef __vtkFunctionParser_h 00039 #define __vtkFunctionParser_h 00040 00041 #include "vtkObject.h" 00042 00043 #define VTK_PARSER_IMMEDIATE 1 00044 #define VTK_PARSER_UNARY_MINUS 2 00045 00046 // supported math functions 00047 #define VTK_PARSER_ADD 3 00048 #define VTK_PARSER_SUBTRACT 4 00049 #define VTK_PARSER_MULTIPLY 5 00050 #define VTK_PARSER_DIVIDE 6 00051 #define VTK_PARSER_POWER 7 00052 #define VTK_PARSER_ABSOLUTE_VALUE 8 00053 #define VTK_PARSER_EXPONENT 9 00054 #define VTK_PARSER_CEILING 10 00055 #define VTK_PARSER_FLOOR 11 00056 #define VTK_PARSER_LOGARITHM 12 00057 #define VTK_PARSER_LOGARITHME 13 00058 #define VTK_PARSER_LOGARITHM10 14 00059 #define VTK_PARSER_SQUARE_ROOT 15 00060 #define VTK_PARSER_SINE 16 00061 #define VTK_PARSER_COSINE 17 00062 #define VTK_PARSER_TANGENT 18 00063 #define VTK_PARSER_ARCSINE 19 00064 #define VTK_PARSER_ARCCOSINE 20 00065 #define VTK_PARSER_ARCTANGENT 21 00066 #define VTK_PARSER_HYPERBOLIC_SINE 22 00067 #define VTK_PARSER_HYPERBOLIC_COSINE 23 00068 #define VTK_PARSER_HYPERBOLIC_TANGENT 24 00069 #define VTK_PARSER_MIN 25 00070 #define VTK_PARSER_MAX 26 00071 #define VTK_PARSER_CROSS 27 00072 #define VTK_PARSER_SIGN 28 00073 00074 // functions involving vectors 00075 #define VTK_PARSER_VECTOR_UNARY_MINUS 29 00076 #define VTK_PARSER_DOT_PRODUCT 30 00077 #define VTK_PARSER_VECTOR_ADD 31 00078 #define VTK_PARSER_VECTOR_SUBTRACT 32 00079 #define VTK_PARSER_SCALAR_TIMES_VECTOR 33 00080 #define VTK_PARSER_VECTOR_TIMES_SCALAR 34 00081 #define VTK_PARSER_MAGNITUDE 35 00082 #define VTK_PARSER_NORMALIZE 36 00083 00084 // constants involving vectors 00085 #define VTK_PARSER_IHAT 37 00086 #define VTK_PARSER_JHAT 38 00087 #define VTK_PARSER_KHAT 39 00088 00089 // code for if(bool, trueval, falseval) resulting in a scalar 00090 #define VTK_PARSER_IF 40 00091 00092 // code for if(bool, truevec, falsevec) resulting in a vector 00093 #define VTK_PARSER_VECTOR_IF 41 00094 00095 // codes for boolean expressions 00096 #define VTK_PARSER_LESS_THAN 42 00097 00098 // codes for boolean expressions 00099 #define VTK_PARSER_GREATER_THAN 43 00100 00101 // codes for boolean expressions 00102 #define VTK_PARSER_EQUAL_TO 44 00103 00104 // codes for boolean expressions 00105 #define VTK_PARSER_AND 45 00106 00107 // codes for boolean expressions 00108 #define VTK_PARSER_OR 46 00109 00110 // codes for scalar variables come before those for vectors. Do not define 00111 // values for VTK_PARSER_BEGIN_VARIABLES+1, VTK_PARSER_BEGIN_VARIABLES+2, ..., 00112 // because they are used to look up variables numbered 1, 2, ... 00113 #define VTK_PARSER_BEGIN_VARIABLES 47 00114 00115 // the value that is retuned as a result if there is an error 00116 #define VTK_PARSER_ERROR_RESULT VTK_LARGE_FLOAT 00117 00118 class VTK_COMMON_EXPORT vtkFunctionParser : public vtkObject 00119 { 00120 public: 00121 static vtkFunctionParser *New(); 00122 vtkTypeMacro(vtkFunctionParser, vtkObject); 00123 void PrintSelf(ostream& os, vtkIndent indent); 00124 00126 00127 void SetFunction(const char *function); 00128 vtkGetStringMacro(Function); 00130 00133 int IsScalarResult(); 00134 00137 int IsVectorResult(); 00138 00140 double GetScalarResult(); 00141 00143 00144 double* GetVectorResult(); 00145 void GetVectorResult(double result[3]) { 00146 double *r = this->GetVectorResult(); 00147 result[0] = r[0]; result[1] = r[1]; result[2] = r[2]; }; 00149 00151 00155 void SetScalarVariableValue(const char* variableName, double value); 00156 void SetScalarVariableValue(int i, double value); 00158 00160 00161 double GetScalarVariableValue(const char* variableName); 00162 double GetScalarVariableValue(int i); 00164 00166 00170 void SetVectorVariableValue(const char* variableName, double xValue, 00171 double yValue, double zValue); 00172 void SetVectorVariableValue(const char* variableName, 00173 const double values[3]) { 00174 this->SetVectorVariableValue(variableName,values[0],values[1],values[2]);}; 00175 void SetVectorVariableValue(int i, double xValue, double yValue, 00176 double zValue); 00177 void SetVectorVariableValue(int i, const double values[3]) { 00178 this->SetVectorVariableValue(i,values[0],values[1],values[2]);}; 00180 00182 00183 double* GetVectorVariableValue(const char* variableName); 00184 void GetVectorVariableValue(const char* variableName, double value[3]) { 00185 double *r = this->GetVectorVariableValue(variableName); 00186 value[0] = r[0]; value[1] = r[1]; value[2] = r[2]; }; 00187 double* GetVectorVariableValue(int i); 00188 void GetVectorVariableValue(int i, double value[3]) { 00189 double *r = this->GetVectorVariableValue(i); 00190 value[0] = r[0]; value[1] = r[1]; value[2] = r[2]; }; 00192 00194 00195 vtkGetMacro(NumberOfScalarVariables,int); 00197 00199 00200 vtkGetMacro(NumberOfVectorVariables,int); 00202 00204 char* GetScalarVariableName(int i); 00205 00207 char* GetVectorVariableName(int i); 00208 00210 void RemoveAllVariables(); 00211 00213 void RemoveScalarVariables(); 00214 00216 void RemoveVectorVariables(); 00217 00219 00223 vtkSetMacro(ReplaceInvalidValues,int); 00224 vtkGetMacro(ReplaceInvalidValues,int); 00225 vtkBooleanMacro(ReplaceInvalidValues,int); 00226 vtkSetMacro(ReplacementValue,double); 00227 vtkGetMacro(ReplacementValue,double); 00229 00230 protected: 00231 vtkFunctionParser(); 00232 ~vtkFunctionParser(); 00233 00234 int Parse(); 00236 bool Evaluate(); 00237 00238 int CheckSyntax(); 00239 void RemoveSpaces(); 00240 char* RemoveSpacesFrom(const char* variableName); 00241 int OperatorWithinVariable(int idx); 00242 00243 int BuildInternalFunctionStructure(); 00244 void BuildInternalSubstringStructure(int beginIndex, int endIndex); 00245 void AddInternalByte(unsigned char newByte); 00246 00247 int IsSubstringCompletelyEnclosed(int beginIndex, int endIndex); 00248 int FindEndOfMathFunction(int beginIndex); 00249 int FindEndOfMathConstant(int beginIndex); 00250 00251 int IsVariableName(int currentIndex); 00252 int IsElementaryOperator(int op); 00253 00254 int GetMathFunctionNumber(int currentIndex); 00255 int GetMathFunctionNumberByCheckingParenthesis( int currentIndex ); 00256 int GetMathFunctionStringLength(int mathFunctionNumber); 00257 int GetMathConstantNumber(int currentIndex); 00258 int GetMathConstantStringLength(int mathConstantNumber); 00259 unsigned char GetElementaryOperatorNumber(char op); 00260 unsigned char GetOperandNumber(int currentIndex); 00261 int GetVariableNameLength(int variableNumber); 00262 00263 int DisambiguateOperators(); 00264 00265 char* Function; 00266 int FunctionLength; 00267 int NumberOfScalarVariables; 00268 int NumberOfVectorVariables; 00269 char** ScalarVariableNames; 00270 char** VectorVariableNames; 00271 double* ScalarVariableValues; 00272 double** VectorVariableValues; 00273 unsigned char *ByteCode; 00274 int ByteCodeSize; 00275 double *Immediates; 00276 int ImmediatesSize; 00277 double *Stack; 00278 int StackSize; 00279 int StackPointer; 00280 00281 vtkTimeStamp FunctionMTime; 00282 vtkTimeStamp ParseMTime; 00283 vtkTimeStamp VariableMTime; 00284 vtkTimeStamp EvaluateMTime; 00285 00286 int ReplaceInvalidValues; 00287 double ReplacementValue; 00288 00289 private: 00290 vtkFunctionParser(const vtkFunctionParser&); // Not implemented. 00291 void operator=(const vtkFunctionParser&); // Not implemented. 00292 }; 00293 00294 #endif