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 =========================================================================*/ 00048 #ifndef vtkFunctionParser_h 00049 #define vtkFunctionParser_h 00050 00051 #include "vtkCommonMiscModule.h" // For export macro 00052 #include "vtkObject.h" 00053 00054 #define VTK_PARSER_IMMEDIATE 1 00055 #define VTK_PARSER_UNARY_MINUS 2 00056 00057 // supported math functions 00058 #define VTK_PARSER_ADD 3 00059 #define VTK_PARSER_SUBTRACT 4 00060 #define VTK_PARSER_MULTIPLY 5 00061 #define VTK_PARSER_DIVIDE 6 00062 #define VTK_PARSER_POWER 7 00063 #define VTK_PARSER_ABSOLUTE_VALUE 8 00064 #define VTK_PARSER_EXPONENT 9 00065 #define VTK_PARSER_CEILING 10 00066 #define VTK_PARSER_FLOOR 11 00067 #define VTK_PARSER_LOGARITHM 12 00068 #define VTK_PARSER_LOGARITHME 13 00069 #define VTK_PARSER_LOGARITHM10 14 00070 #define VTK_PARSER_SQUARE_ROOT 15 00071 #define VTK_PARSER_SINE 16 00072 #define VTK_PARSER_COSINE 17 00073 #define VTK_PARSER_TANGENT 18 00074 #define VTK_PARSER_ARCSINE 19 00075 #define VTK_PARSER_ARCCOSINE 20 00076 #define VTK_PARSER_ARCTANGENT 21 00077 #define VTK_PARSER_HYPERBOLIC_SINE 22 00078 #define VTK_PARSER_HYPERBOLIC_COSINE 23 00079 #define VTK_PARSER_HYPERBOLIC_TANGENT 24 00080 #define VTK_PARSER_MIN 25 00081 #define VTK_PARSER_MAX 26 00082 #define VTK_PARSER_SIGN 28 00083 00084 // functions involving vectors 00085 #define VTK_PARSER_CROSS 27 00086 #define VTK_PARSER_VECTOR_UNARY_MINUS 29 00087 #define VTK_PARSER_DOT_PRODUCT 30 00088 #define VTK_PARSER_VECTOR_ADD 31 00089 #define VTK_PARSER_VECTOR_SUBTRACT 32 00090 #define VTK_PARSER_SCALAR_TIMES_VECTOR 33 00091 #define VTK_PARSER_VECTOR_TIMES_SCALAR 34 00092 #define VTK_PARSER_VECTOR_OVER_SCALAR 35 00093 #define VTK_PARSER_MAGNITUDE 36 00094 #define VTK_PARSER_NORMALIZE 37 00095 00096 // constants involving vectors 00097 #define VTK_PARSER_IHAT 38 00098 #define VTK_PARSER_JHAT 39 00099 #define VTK_PARSER_KHAT 40 00100 00101 // code for if(bool, trueval, falseval) resulting in a scalar 00102 #define VTK_PARSER_IF 41 00103 00104 // code for if(bool, truevec, falsevec) resulting in a vector 00105 #define VTK_PARSER_VECTOR_IF 42 00106 00107 // codes for boolean expressions 00108 #define VTK_PARSER_LESS_THAN 43 00109 00110 // codes for boolean expressions 00111 #define VTK_PARSER_GREATER_THAN 44 00112 00113 // codes for boolean expressions 00114 #define VTK_PARSER_EQUAL_TO 45 00115 00116 // codes for boolean expressions 00117 #define VTK_PARSER_AND 46 00118 00119 // codes for boolean expressions 00120 #define VTK_PARSER_OR 47 00121 00122 // codes for scalar variables come before those for vectors. Do not define 00123 // values for VTK_PARSER_BEGIN_VARIABLES+1, VTK_PARSER_BEGIN_VARIABLES+2, ..., 00124 // because they are used to look up variables numbered 1, 2, ... 00125 #define VTK_PARSER_BEGIN_VARIABLES 48 00126 00127 // the value that is retuned as a result if there is an error 00128 #define VTK_PARSER_ERROR_RESULT VTK_FLOAT_MAX 00129 00130 class VTKCOMMONMISC_EXPORT vtkFunctionParser : public vtkObject 00131 { 00132 public: 00133 static vtkFunctionParser *New(); 00134 vtkTypeMacro(vtkFunctionParser, vtkObject); 00135 void PrintSelf(ostream& os, vtkIndent indent); 00136 00138 unsigned long int GetMTime(); 00139 00141 00142 void SetFunction(const char *function); 00143 vtkGetStringMacro(Function); 00145 00148 int IsScalarResult(); 00149 00152 int IsVectorResult(); 00153 00155 double GetScalarResult(); 00156 00158 00159 double* GetVectorResult(); 00160 void GetVectorResult(double result[3]) { 00161 double *r = this->GetVectorResult(); 00162 result[0] = r[0]; result[1] = r[1]; result[2] = r[2]; }; 00164 00166 00170 void SetScalarVariableValue(const char* variableName, double value); 00171 void SetScalarVariableValue(int i, double value); 00173 00175 00176 double GetScalarVariableValue(const char* variableName); 00177 double GetScalarVariableValue(int i); 00179 00181 00185 void SetVectorVariableValue(const char* variableName, double xValue, 00186 double yValue, double zValue); 00187 void SetVectorVariableValue(const char* variableName, 00188 const double values[3]) { 00189 this->SetVectorVariableValue(variableName,values[0],values[1],values[2]);}; 00190 void SetVectorVariableValue(int i, double xValue, double yValue, 00191 double zValue); 00192 void SetVectorVariableValue(int i, const double values[3]) { 00193 this->SetVectorVariableValue(i,values[0],values[1],values[2]);}; 00195 00197 00198 double* GetVectorVariableValue(const char* variableName); 00199 void GetVectorVariableValue(const char* variableName, double value[3]) { 00200 double *r = this->GetVectorVariableValue(variableName); 00201 value[0] = r[0]; value[1] = r[1]; value[2] = r[2]; }; 00202 double* GetVectorVariableValue(int i); 00203 void GetVectorVariableValue(int i, double value[3]) { 00204 double *r = this->GetVectorVariableValue(i); 00205 value[0] = r[0]; value[1] = r[1]; value[2] = r[2]; }; 00207 00209 00210 vtkGetMacro(NumberOfScalarVariables,int); 00212 00214 00215 vtkGetMacro(NumberOfVectorVariables,int); 00217 00219 char* GetScalarVariableName(int i); 00220 00222 char* GetVectorVariableName(int i); 00223 00225 void RemoveAllVariables(); 00226 00228 void RemoveScalarVariables(); 00229 00231 void RemoveVectorVariables(); 00232 00234 00238 vtkSetMacro(ReplaceInvalidValues,int); 00239 vtkGetMacro(ReplaceInvalidValues,int); 00240 vtkBooleanMacro(ReplaceInvalidValues,int); 00241 vtkSetMacro(ReplacementValue,double); 00242 vtkGetMacro(ReplacementValue,double); 00244 00246 void CheckExpression(int &pos, char **error); 00247 00249 void InvalidateFunction(); 00250 00251 protected: 00252 vtkFunctionParser(); 00253 ~vtkFunctionParser(); 00254 00255 int Parse(); 00256 00258 bool Evaluate(); 00259 00260 int CheckSyntax(); 00261 00262 void CopyParseError(int &position, char **error); 00263 00264 void RemoveSpaces(); 00265 char* RemoveSpacesFrom(const char* variableName); 00266 int OperatorWithinVariable(int idx); 00267 00268 int BuildInternalFunctionStructure(); 00269 void BuildInternalSubstringStructure(int beginIndex, int endIndex); 00270 void AddInternalByte(unsigned char newByte); 00271 00272 int IsSubstringCompletelyEnclosed(int beginIndex, int endIndex); 00273 int FindEndOfMathFunction(int beginIndex); 00274 int FindEndOfMathConstant(int beginIndex); 00275 00276 int IsVariableName(int currentIndex); 00277 int IsElementaryOperator(int op); 00278 00279 int GetMathFunctionNumber(int currentIndex); 00280 int GetMathFunctionNumberByCheckingParenthesis( int currentIndex ); 00281 int GetMathFunctionStringLength(int mathFunctionNumber); 00282 int GetMathConstantNumber(int currentIndex); 00283 int GetMathConstantStringLength(int mathConstantNumber); 00284 unsigned char GetElementaryOperatorNumber(char op); 00285 unsigned char GetOperandNumber(int currentIndex); 00286 int GetVariableNameLength(int variableNumber); 00287 00288 int DisambiguateOperators(); 00289 00290 vtkSetStringMacro(ParseError); 00291 00292 int FindPositionInOriginalFunction(const int& pos); 00293 00294 char* Function; 00295 char* FunctionWithSpaces; 00296 00297 int FunctionLength; 00298 int NumberOfScalarVariables; 00299 int NumberOfVectorVariables; 00300 char** ScalarVariableNames; 00301 char** VectorVariableNames; 00302 double* ScalarVariableValues; 00303 double** VectorVariableValues; 00304 unsigned char *ByteCode; 00305 int ByteCodeSize; 00306 double *Immediates; 00307 int ImmediatesSize; 00308 double *Stack; 00309 int StackSize; 00310 int StackPointer; 00311 00312 vtkTimeStamp FunctionMTime; 00313 vtkTimeStamp ParseMTime; 00314 vtkTimeStamp VariableMTime; 00315 vtkTimeStamp EvaluateMTime; 00316 vtkTimeStamp CheckMTime; 00317 00318 int ReplaceInvalidValues; 00319 double ReplacementValue; 00320 00321 int ParseErrorPositon; 00322 char* ParseError; 00323 00324 private: 00325 vtkFunctionParser(const vtkFunctionParser&); // Not implemented. 00326 void operator=(const vtkFunctionParser&); // Not implemented. 00327 }; 00328 00329 #endif