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