VTK  9.1.0
vtkFunctionParser.h
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: vtkFunctionParser.h
5
6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 All rights reserved.
8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9
10 This software is distributed WITHOUT ANY WARRANTY; without even
11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the above copyright notice for more information.
13
14=========================================================================*/
65#ifndef vtkFunctionParser_h
66#define vtkFunctionParser_h
67
68#include "vtkCommonMiscModule.h" // For export macro
69#include "vtkObject.h"
70#include "vtkTuple.h" // needed for vtkTuple
71#include <string> // needed for string.
72#include <vector> // needed for vector
73
74#define VTK_PARSER_IMMEDIATE 1
75#define VTK_PARSER_UNARY_MINUS 2
76#define VTK_PARSER_UNARY_PLUS 3
77
78// supported math functions
79#define VTK_PARSER_ADD 4
80#define VTK_PARSER_SUBTRACT 5
81#define VTK_PARSER_MULTIPLY 6
82#define VTK_PARSER_DIVIDE 7
83#define VTK_PARSER_POWER 8
84#define VTK_PARSER_ABSOLUTE_VALUE 9
85#define VTK_PARSER_EXPONENT 10
86#define VTK_PARSER_CEILING 11
87#define VTK_PARSER_FLOOR 12
88#define VTK_PARSER_LOGARITHM 13
89#define VTK_PARSER_LOGARITHME 14
90#define VTK_PARSER_LOGARITHM10 15
91#define VTK_PARSER_SQUARE_ROOT 16
92#define VTK_PARSER_SINE 17
93#define VTK_PARSER_COSINE 18
94#define VTK_PARSER_TANGENT 19
95#define VTK_PARSER_ARCSINE 20
96#define VTK_PARSER_ARCCOSINE 21
97#define VTK_PARSER_ARCTANGENT 22
98#define VTK_PARSER_HYPERBOLIC_SINE 23
99#define VTK_PARSER_HYPERBOLIC_COSINE 24
100#define VTK_PARSER_HYPERBOLIC_TANGENT 25
101#define VTK_PARSER_MIN 26
102#define VTK_PARSER_MAX 27
103#define VTK_PARSER_SIGN 29
104
105// functions involving vectors
106#define VTK_PARSER_CROSS 28
107#define VTK_PARSER_VECTOR_UNARY_MINUS 30
108#define VTK_PARSER_VECTOR_UNARY_PLUS 31
109#define VTK_PARSER_DOT_PRODUCT 32
110#define VTK_PARSER_VECTOR_ADD 33
111#define VTK_PARSER_VECTOR_SUBTRACT 34
112#define VTK_PARSER_SCALAR_TIMES_VECTOR 35
113#define VTK_PARSER_VECTOR_TIMES_SCALAR 36
114#define VTK_PARSER_VECTOR_OVER_SCALAR 37
115#define VTK_PARSER_MAGNITUDE 38
116#define VTK_PARSER_NORMALIZE 39
117
118// constants involving vectors
119#define VTK_PARSER_IHAT 40
120#define VTK_PARSER_JHAT 41
121#define VTK_PARSER_KHAT 42
122
123// code for if(bool, trueval, falseval) resulting in a scalar
124#define VTK_PARSER_IF 43
125
126// code for if(bool, truevec, falsevec) resulting in a vector
127#define VTK_PARSER_VECTOR_IF 44
128
129// codes for boolean expressions
130#define VTK_PARSER_LESS_THAN 45
131
132// codes for boolean expressions
133#define VTK_PARSER_GREATER_THAN 46
134
135// codes for boolean expressions
136#define VTK_PARSER_EQUAL_TO 47
137
138// codes for boolean expressions
139#define VTK_PARSER_AND 48
140
141// codes for boolean expressions
142#define VTK_PARSER_OR 49
143
144// codes for scalar variables come before those for vectors. Do not define
145// values for VTK_PARSER_BEGIN_VARIABLES+1, VTK_PARSER_BEGIN_VARIABLES+2, ...,
146// because they are used to look up variables numbered 1, 2, ...
147#define VTK_PARSER_BEGIN_VARIABLES 50
148
149// the value that is returned as a result if there is an error
150#define VTK_PARSER_ERROR_RESULT VTK_FLOAT_MAX
151
152class VTKCOMMONMISC_EXPORT vtkFunctionParser : public vtkObject
153{
154public:
157 void PrintSelf(ostream& os, vtkIndent indent) override;
158
163
165
168 void SetFunction(const char* function);
169 vtkGetStringMacro(Function);
171
177
183
188
190
194 void GetVectorResult(double result[3])
195 {
196 double* r = this->GetVectorResult();
197 result[0] = r[0];
198 result[1] = r[1];
199 result[2] = r[2];
200 }
202
204
210 void SetScalarVariableValue(const char* variableName, double value);
211 void SetScalarVariableValue(const std::string& variableName, double value)
212 {
213 this->SetScalarVariableValue(variableName.c_str(), value);
214 }
215 void SetScalarVariableValue(int i, double value);
217
219
222 double GetScalarVariableValue(const char* variableName);
223 double GetScalarVariableValue(const std::string& variableName)
224 {
225 return this->GetScalarVariableValue(variableName.c_str());
226 }
229
231
238 const char* variableName, double xValue, double yValue, double zValue);
240 const std::string& variableName, double xValue, double yValue, double zValue)
241 {
242 this->SetVectorVariableValue(variableName.c_str(), xValue, yValue, zValue);
243 }
244 void SetVectorVariableValue(const char* variableName, const double values[3])
245 {
246 this->SetVectorVariableValue(variableName, values[0], values[1], values[2]);
247 }
248 void SetVectorVariableValue(const std::string& variableName, const double values[3])
249 {
250 this->SetVectorVariableValue(variableName.c_str(), values[0], values[1], values[2]);
251 }
252 void SetVectorVariableValue(int i, double xValue, double yValue, double zValue);
253 void SetVectorVariableValue(int i, const double values[3])
254 {
255 this->SetVectorVariableValue(i, values[0], values[1], values[2]);
256 }
258
260
263 double* GetVectorVariableValue(const char* variableName) VTK_SIZEHINT(3);
264 double* GetVectorVariableValue(const std::string& variableName) VTK_SIZEHINT(3)
265 {
266 return this->GetVectorVariableValue(variableName.c_str());
267 }
268 void GetVectorVariableValue(const char* variableName, double value[3])
269 {
270 double* r = this->GetVectorVariableValue(variableName);
271 value[0] = r[0];
272 value[1] = r[1];
273 value[2] = r[2];
274 }
275 void GetVectorVariableValue(const std::string& variableName, double value[3])
276 {
277 this->GetVectorVariableValue(variableName.c_str(), value);
278 }
280 void GetVectorVariableValue(int i, double value[3])
281 {
282 double* r = this->GetVectorVariableValue(i);
283 value[0] = r[0];
284 value[1] = r[1];
285 value[2] = r[2];
286 }
288
292 int GetNumberOfScalarVariables() { return static_cast<int>(this->ScalarVariableNames.size()); }
293
297 int GetScalarVariableIndex(const char* name);
298 int GetScalarVariableIndex(const std::string& name)
299 {
300 return this->GetScalarVariableIndex(name.c_str());
301 }
302
306 int GetNumberOfVectorVariables() { return static_cast<int>(this->VectorVariableNames.size()); }
307
311 int GetVectorVariableIndex(const char* name);
312 int GetVectorVariableIndex(const std::string& name)
313 {
314 return this->GetVectorVariableIndex(name.c_str());
315 }
316
320 const char* GetScalarVariableName(int i);
321
325 const char* GetVectorVariableName(int i);
326
328
334 bool GetScalarVariableNeeded(const char* variableName);
335 bool GetScalarVariableNeeded(const std::string& variableName)
336 {
337 return GetScalarVariableNeeded(variableName.c_str());
338 }
340
342
348 bool GetVectorVariableNeeded(const char* variableName);
349 bool GetVectorVariableNeeded(const std::string& variableName)
350 {
351 return this->GetVectorVariableNeeded(variableName.c_str());
352 }
354
359
364
369
371
377 vtkSetMacro(ReplaceInvalidValues, vtkTypeBool);
378 vtkGetMacro(ReplaceInvalidValues, vtkTypeBool);
379 vtkBooleanMacro(ReplaceInvalidValues, vtkTypeBool);
380 vtkSetMacro(ReplacementValue, double);
381 vtkGetMacro(ReplacementValue, double);
383
387 void CheckExpression(int& pos, char** error);
388
393
394protected:
397
398 int Parse();
399
403 bool Evaluate();
404
406
407 void CopyParseError(int& position, char** error);
408
410 char* RemoveSpacesFrom(const char* variableName);
412
414 void BuildInternalSubstringStructure(int beginIndex, int endIndex);
415 void AddInternalByte(unsigned int newByte);
416
417 int IsSubstringCompletelyEnclosed(int beginIndex, int endIndex);
418 int FindEndOfMathFunction(int beginIndex);
419 int FindEndOfMathConstant(int beginIndex);
420
421 int IsVariableName(int currentIndex);
423
424 int GetMathFunctionNumber(int currentIndex);
426 int GetMathFunctionStringLength(int mathFunctionNumber);
427 int GetMathConstantNumber(int currentIndex);
428 int GetMathConstantStringLength(int mathConstantNumber);
429 unsigned char GetElementaryOperatorNumber(char op);
430 unsigned int GetOperandNumber(int currentIndex);
431 int GetVariableNameLength(int variableNumber);
432
434
440
441 vtkSetStringMacro(ParseError);
442
444
445 char* Function;
447
449 std::vector<std::string> ScalarVariableNames;
450 std::vector<std::string> VectorVariableNames;
451 std::vector<double> ScalarVariableValues;
452 std::vector<vtkTuple<double, 3>> VectorVariableValues;
453 std::vector<bool> ScalarVariableNeeded;
454 std::vector<bool> VectorVariableNeeded;
455
456 std::vector<unsigned int> ByteCode;
458 double* Immediates;
460 double* Stack;
463
469
472
475
476private:
477 vtkFunctionParser(const vtkFunctionParser&) = delete;
478 void operator=(const vtkFunctionParser&) = delete;
479};
480
481#endif
Parse and evaluate a mathematical expression.
void SetScalarVariableValue(int i, double value)
Set the value of a scalar variable.
int BuildInternalFunctionStructure()
double GetScalarVariableValue(const std::string &variableName)
Get the value of a scalar variable.
int DisambiguateOperators()
std::vector< std::string > VectorVariableNames
~vtkFunctionParser() override
bool GetScalarVariableNeeded(const std::string &variableName)
Returns whether a scalar variable is needed for the function evaluation.
void SetVectorVariableValue(int i, const double values[3])
Set the value of a vector variable.
vtkTimeStamp EvaluateMTime
void SetFunction(const char *function)
Set/Get input string to evaluate.
double * GetVectorVariableValue(int i)
Get the value of a vector variable.
vtkTimeStamp FunctionMTime
int IsScalarResult()
Check whether the result is a scalar result.
double * GetVectorResult()
Get a vector result from evaluating the input function.
static vtkFunctionParser * New()
double * GetVectorVariableValue(const std::string &variableName)
Get the value of a vector variable.
int GetScalarVariableIndex(const char *name)
Get scalar variable index or -1 if not found.
int GetNumberOfVectorVariables()
Get the number of vector variables.
void RemoveVectorVariables()
Remove all the vector variables.
void GetVectorVariableValue(const char *variableName, double value[3])
Get the value of a vector variable.
double GetScalarResult()
Get a scalar result from evaluating the input function.
int GetMathFunctionStringLength(int mathFunctionNumber)
char * RemoveSpacesFrom(const char *variableName)
int GetNumberOfScalarVariables()
Get the number of scalar variables.
int GetMathConstantNumber(int currentIndex)
int GetVectorVariableIndex(const char *name)
Get scalar variable index or -1 if not found.
void SetVectorVariableValue(const char *variableName, const double values[3])
Set the value of a vector variable.
int GetMathFunctionNumber(int currentIndex)
int IsElementaryOperator(int op)
int FindPositionInOriginalFunction(const int &pos)
const char * GetVectorVariableName(int i)
Get the ith vector variable name.
unsigned int GetOperandNumber(int currentIndex)
void AddInternalByte(unsigned int newByte)
void SetVectorVariableValue(const std::string &variableName, const double values[3])
Set the value of a vector variable.
vtkMTimeType GetMTime() override
Return parser's MTime.
void UpdateNeededVariables()
Collects meta-data about which variables are needed by the current function.
bool GetVectorVariableNeeded(const std::string &variableName)
Returns whether a vector variable is needed for the function evaluation.
void GetVectorVariableValue(int i, double value[3])
Get the value of a vector variable.
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
void SetVectorVariableValue(int i, double xValue, double yValue, double zValue)
Set the value of a vector variable.
std::vector< bool > ScalarVariableNeeded
bool Evaluate()
Evaluate the function, returning true on success, false on failure.
const char * GetScalarVariableName(int i)
Get the ith scalar variable name.
std::vector< unsigned int > ByteCode
vtkTimeStamp CheckMTime
int GetMathFunctionNumberByCheckingParenthesis(int currentIndex)
double GetScalarVariableValue(const char *variableName)
Get the value of a scalar variable.
int IsVariableName(int currentIndex)
void CheckExpression(int &pos, char **error)
Check the validity of the function expression.
int FindEndOfMathConstant(int beginIndex)
double GetScalarVariableValue(int i)
Get the value of a scalar variable.
void SetVectorVariableValue(const std::string &variableName, double xValue, double yValue, double zValue)
Set the value of a vector variable.
std::vector< double > ScalarVariableValues
vtkTimeStamp ParseMTime
bool GetScalarVariableNeeded(const char *variableName)
Returns whether a scalar variable is needed for the function evaluation.
bool GetScalarVariableNeeded(int i)
Returns whether a scalar variable is needed for the function evaluation.
unsigned char GetElementaryOperatorNumber(char op)
int GetVectorVariableIndex(const std::string &name)
int GetVariableNameLength(int variableNumber)
double * GetVectorVariableValue(const char *variableName)
Get the value of a vector variable.
std::vector< vtkTuple< double, 3 > > VectorVariableValues
void GetVectorVariableValue(const std::string &variableName, double value[3])
Get the value of a vector variable.
vtkTypeBool ReplaceInvalidValues
int IsVectorResult()
Check whether the result is a vector result.
void RemoveScalarVariables()
Remove all the scalar variables.
int IsSubstringCompletelyEnclosed(int beginIndex, int endIndex)
bool GetVectorVariableNeeded(int i)
Returns whether a vector variable is needed for the function evaluation.
void BuildInternalSubstringStructure(int beginIndex, int endIndex)
vtkTimeStamp VariableMTime
void RemoveAllVariables()
Remove all the current variables.
void CopyParseError(int &position, char **error)
void InvalidateFunction()
Allow the user to force the function to be re-parsed.
void SetScalarVariableValue(const std::string &variableName, double value)
Set the value of a scalar variable.
int OperatorWithinVariable(int idx)
bool GetVectorVariableNeeded(const char *variableName)
Returns whether a vector variable is needed for the function evaluation.
int GetMathConstantStringLength(int mathConstantNumber)
std::vector< bool > VectorVariableNeeded
std::vector< std::string > ScalarVariableNames
int GetScalarVariableIndex(const std::string &name)
void SetVectorVariableValue(const char *variableName, double xValue, double yValue, double zValue)
Set the value of a vector variable.
void SetScalarVariableValue(const char *variableName, double value)
Set the value of a scalar variable.
int FindEndOfMathFunction(int beginIndex)
a simple class to control print indentation
Definition: vtkIndent.h:113
abstract base class for most VTK objects
Definition: vtkObject.h:82
record modification and/or execution time
Definition: vtkTimeStamp.h:52
int vtkTypeBool
Definition: vtkABI.h:69
vtkTypeUInt32 vtkMTimeType
Definition: vtkType.h:287
#define VTK_SIZEHINT(...)