VTK  9.6.1
vtkFunctionParser.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2// SPDX-License-Identifier: BSD-3-Clause
36
37#ifndef vtkFunctionParser_h
38#define vtkFunctionParser_h
39
40#include "vtkCommonMiscModule.h" // For export macro
41#include "vtkObject.h"
42#include "vtkTuple.h" // needed for vtkTuple
43#include <string> // needed for string.
44#include <vector> // needed for vector
45
46#define VTK_PARSER_IMMEDIATE 1
47#define VTK_PARSER_UNARY_MINUS 2
48#define VTK_PARSER_UNARY_PLUS 3
49
50// supported math functions
51#define VTK_PARSER_ADD 4
52#define VTK_PARSER_SUBTRACT 5
53#define VTK_PARSER_MULTIPLY 6
54#define VTK_PARSER_DIVIDE 7
55#define VTK_PARSER_POWER 8
56#define VTK_PARSER_ABSOLUTE_VALUE 9
57#define VTK_PARSER_EXPONENT 10
58#define VTK_PARSER_CEILING 11
59#define VTK_PARSER_FLOOR 12
60#define VTK_PARSER_LOGARITHM 13
61#define VTK_PARSER_LOGARITHME 14
62#define VTK_PARSER_LOGARITHM10 15
63#define VTK_PARSER_SQUARE_ROOT 16
64#define VTK_PARSER_SINE 17
65#define VTK_PARSER_COSINE 18
66#define VTK_PARSER_TANGENT 19
67#define VTK_PARSER_ARCSINE 20
68#define VTK_PARSER_ARCCOSINE 21
69#define VTK_PARSER_ARCTANGENT 22
70#define VTK_PARSER_HYPERBOLIC_SINE 23
71#define VTK_PARSER_HYPERBOLIC_COSINE 24
72#define VTK_PARSER_HYPERBOLIC_TANGENT 25
73#define VTK_PARSER_MIN 26
74#define VTK_PARSER_MAX 27
75#define VTK_PARSER_SIGN 29
76
77// functions involving vectors
78#define VTK_PARSER_CROSS 28
79#define VTK_PARSER_VECTOR_UNARY_MINUS 30
80#define VTK_PARSER_VECTOR_UNARY_PLUS 31
81#define VTK_PARSER_DOT_PRODUCT 32
82#define VTK_PARSER_VECTOR_ADD 33
83#define VTK_PARSER_VECTOR_SUBTRACT 34
84#define VTK_PARSER_SCALAR_TIMES_VECTOR 35
85#define VTK_PARSER_VECTOR_TIMES_SCALAR 36
86#define VTK_PARSER_VECTOR_OVER_SCALAR 37
87#define VTK_PARSER_MAGNITUDE 38
88#define VTK_PARSER_NORMALIZE 39
89
90// constants involving vectors
91#define VTK_PARSER_IHAT 40
92#define VTK_PARSER_JHAT 41
93#define VTK_PARSER_KHAT 42
94
95// code for if(bool, trueval, falseval) resulting in a scalar
96#define VTK_PARSER_IF 43
97
98// code for if(bool, truevec, falsevec) resulting in a vector
99#define VTK_PARSER_VECTOR_IF 44
100
101// codes for boolean expressions
102#define VTK_PARSER_LESS_THAN 45
103
104// codes for boolean expressions
105#define VTK_PARSER_GREATER_THAN 46
106
107// codes for boolean expressions
108#define VTK_PARSER_EQUAL_TO 47
109
110// codes for boolean expressions
111#define VTK_PARSER_AND 48
112
113// codes for boolean expressions
114#define VTK_PARSER_OR 49
115
116// codes for scalar variables come before those for vectors. Do not define
117// values for VTK_PARSER_BEGIN_VARIABLES+1, VTK_PARSER_BEGIN_VARIABLES+2, ...,
118// because they are used to look up variables numbered 1, 2, ...
119#define VTK_PARSER_BEGIN_VARIABLES 50
120
121// the value that is returned as a result if there is an error
122#define VTK_PARSER_ERROR_RESULT VTK_FLOAT_MAX
123
124VTK_ABI_NAMESPACE_BEGIN
125class VTKCOMMONMISC_EXPORT vtkFunctionParser : public vtkObject
126{
127public:
130 void PrintSelf(ostream& os, vtkIndent indent) override;
131
136
137 int GetResultSize() { return 3; }
138
140
143 void SetFunction(const char* function);
144 vtkGetStringMacro(Function);
146
152
158
163
165
169 void GetVectorResult(double result[3])
170 {
171 double* r = this->GetVectorResult();
172 result[0] = r[0];
173 result[1] = r[1];
174 result[2] = r[2];
175 }
176
177
179
185 void SetScalarVariableValue(const char* variableName, double value);
186 void SetScalarVariableValue(const std::string& variableName, double value)
187 {
188 this->SetScalarVariableValue(variableName.c_str(), value);
189 }
190 void SetScalarVariableValue(int i, double value);
192
194
197 double GetScalarVariableValue(const char* variableName);
198 double GetScalarVariableValue(const std::string& variableName)
199 {
200 return this->GetScalarVariableValue(variableName.c_str());
201 }
204
206
213 const char* variableName, double xValue, double yValue, double zValue);
215 const std::string& variableName, double xValue, double yValue, double zValue)
216 {
217 this->SetVectorVariableValue(variableName.c_str(), xValue, yValue, zValue);
218 }
219 void SetVectorVariableValue(const char* variableName, const double* values, int vtkNotUsed(size))
220 {
221 this->SetVectorVariableValue(variableName, values[0], values[1], values[2]);
222 }
224 const std::string& variableName, const double* values, int vtkNotUsed(size))
225 {
226 this->SetVectorVariableValue(variableName.c_str(), values[0], values[1], values[2]);
227 }
228 void SetVectorVariableValue(int i, double xValue, double yValue, double zValue);
229 void SetVectorVariableValue(int i, const double* values, int vtkNotUsed(size))
230 {
231 this->SetVectorVariableValue(i, values[0], values[1], values[2]);
232 }
233
234
236
239 double* GetVectorVariableValue(const char* variableName) VTK_SIZEHINT(3);
240 double* GetVectorVariableValue(const std::string& variableName) VTK_SIZEHINT(3)
241 {
242 return this->GetVectorVariableValue(variableName.c_str());
243 }
244 void GetVectorVariableValue(const char* variableName, double value[3])
245 {
246 double* r = this->GetVectorVariableValue(variableName);
247 value[0] = r[0];
248 value[1] = r[1];
249 value[2] = r[2];
250 }
251 void GetVectorVariableValue(const std::string& variableName, double value[3])
252 {
253 this->GetVectorVariableValue(variableName.c_str(), value);
254 }
256 void GetVectorVariableValue(int i, double value[3])
257 {
258 double* r = this->GetVectorVariableValue(i);
259 value[0] = r[0];
260 value[1] = r[1];
261 value[2] = r[2];
262 }
263
264
268 int GetNumberOfScalarVariables() { return static_cast<int>(this->ScalarVariableNames.size()); }
269
273 int GetScalarVariableIndex(const char* name);
274 int GetScalarVariableIndex(const std::string& name)
275 {
276 return this->GetScalarVariableIndex(name.c_str());
277 }
278
282 int GetNumberOfVectorVariables() { return static_cast<int>(this->VectorVariableNames.size()); }
283
287 int GetVectorVariableIndex(const char* name);
288 int GetVectorVariableIndex(const std::string& name)
289 {
290 return this->GetVectorVariableIndex(name.c_str());
291 }
292
296 const char* GetScalarVariableName(int i);
297
301 const char* GetVectorVariableName(int i);
302
304
310 bool GetScalarVariableNeeded(const char* variableName);
311 bool GetScalarVariableNeeded(const std::string& variableName)
312 {
313 return GetScalarVariableNeeded(variableName.c_str());
314 }
315
316
318
324 bool GetVectorVariableNeeded(const char* variableName);
325 bool GetVectorVariableNeeded(const std::string& variableName)
326 {
327 return this->GetVectorVariableNeeded(variableName.c_str());
328 }
329
330
335
340
345
347
356 vtkSetMacro(ReplacementValue, double);
357 vtkGetMacro(ReplacementValue, double);
359
363 void CheckExpression(int& pos, char** error);
364
369
370protected:
373
374 int Parse();
375
379 bool Evaluate();
380
382
383 void CopyParseError(int& position, char** error);
384
386 char* RemoveSpacesFrom(const char* variableName);
388
390 void BuildInternalSubstringStructure(int beginIndex, int endIndex);
391 void AddInternalByte(unsigned int newByte);
392
393 int IsSubstringCompletelyEnclosed(int beginIndex, int endIndex);
394 int FindEndOfMathFunction(int beginIndex);
395 int FindEndOfMathConstant(int beginIndex);
396
397 int IsVariableName(int currentIndex);
399
400 int GetMathFunctionNumber(int currentIndex);
402 int GetMathFunctionStringLength(int mathFunctionNumber);
403 int GetMathConstantNumber(int currentIndex);
404 int GetMathConstantStringLength(int mathConstantNumber);
405 unsigned char GetElementaryOperatorNumber(char op);
406 unsigned int GetOperandNumber(int currentIndex);
407 int GetVariableNameLength(int variableNumber);
408
410
416
417 vtkSetStringMacro(ParseError);
418
420
421 char* Function;
423
425 std::vector<std::string> ScalarVariableNames;
426 std::vector<std::string> VectorVariableNames;
427 std::vector<double> ScalarVariableValues;
428 std::vector<vtkTuple<double, 3>> VectorVariableValues;
429 std::vector<bool> ScalarVariableNeeded;
430 std::vector<bool> VectorVariableNeeded;
431
432 std::vector<unsigned int> ByteCode;
434 double* Immediates;
436 double* Stack;
439
443
446
449
450private:
451 vtkFunctionParser(const vtkFunctionParser&) = delete;
452 void operator=(const vtkFunctionParser&) = delete;
453};
454
455VTK_ABI_NAMESPACE_END
456#endif
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, int size)
Set the value of a vector variable.
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 SetVectorVariableValue(const std::string &variableName, const double *values, int size)
Set the value of a vector variable.
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.
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)
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 SetVectorVariableValue(const char *variableName, const double *values, int size)
Set the value of a vector variable.
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
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
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)
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:29
record modification and/or execution time
int vtkTypeBool
Definition vtkABI.h:64
vtkTypeUInt32 vtkMTimeType
Definition vtkType.h:323
#define VTK_SIZEHINT(...)