VTK  9.4.20250102
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
56#ifndef vtkFunctionParser_h
57#define vtkFunctionParser_h
58
59#include "vtkCommonMiscModule.h" // For export macro
60#include "vtkObject.h"
61#include "vtkTuple.h" // needed for vtkTuple
62#include <string> // needed for string.
63#include <vector> // needed for vector
64
65#define VTK_PARSER_IMMEDIATE 1
66#define VTK_PARSER_UNARY_MINUS 2
67#define VTK_PARSER_UNARY_PLUS 3
68
69// supported math functions
70#define VTK_PARSER_ADD 4
71#define VTK_PARSER_SUBTRACT 5
72#define VTK_PARSER_MULTIPLY 6
73#define VTK_PARSER_DIVIDE 7
74#define VTK_PARSER_POWER 8
75#define VTK_PARSER_ABSOLUTE_VALUE 9
76#define VTK_PARSER_EXPONENT 10
77#define VTK_PARSER_CEILING 11
78#define VTK_PARSER_FLOOR 12
79#define VTK_PARSER_LOGARITHM 13
80#define VTK_PARSER_LOGARITHME 14
81#define VTK_PARSER_LOGARITHM10 15
82#define VTK_PARSER_SQUARE_ROOT 16
83#define VTK_PARSER_SINE 17
84#define VTK_PARSER_COSINE 18
85#define VTK_PARSER_TANGENT 19
86#define VTK_PARSER_ARCSINE 20
87#define VTK_PARSER_ARCCOSINE 21
88#define VTK_PARSER_ARCTANGENT 22
89#define VTK_PARSER_HYPERBOLIC_SINE 23
90#define VTK_PARSER_HYPERBOLIC_COSINE 24
91#define VTK_PARSER_HYPERBOLIC_TANGENT 25
92#define VTK_PARSER_MIN 26
93#define VTK_PARSER_MAX 27
94#define VTK_PARSER_SIGN 29
95
96// functions involving vectors
97#define VTK_PARSER_CROSS 28
98#define VTK_PARSER_VECTOR_UNARY_MINUS 30
99#define VTK_PARSER_VECTOR_UNARY_PLUS 31
100#define VTK_PARSER_DOT_PRODUCT 32
101#define VTK_PARSER_VECTOR_ADD 33
102#define VTK_PARSER_VECTOR_SUBTRACT 34
103#define VTK_PARSER_SCALAR_TIMES_VECTOR 35
104#define VTK_PARSER_VECTOR_TIMES_SCALAR 36
105#define VTK_PARSER_VECTOR_OVER_SCALAR 37
106#define VTK_PARSER_MAGNITUDE 38
107#define VTK_PARSER_NORMALIZE 39
108
109// constants involving vectors
110#define VTK_PARSER_IHAT 40
111#define VTK_PARSER_JHAT 41
112#define VTK_PARSER_KHAT 42
113
114// code for if(bool, trueval, falseval) resulting in a scalar
115#define VTK_PARSER_IF 43
116
117// code for if(bool, truevec, falsevec) resulting in a vector
118#define VTK_PARSER_VECTOR_IF 44
119
120// codes for boolean expressions
121#define VTK_PARSER_LESS_THAN 45
122
123// codes for boolean expressions
124#define VTK_PARSER_GREATER_THAN 46
125
126// codes for boolean expressions
127#define VTK_PARSER_EQUAL_TO 47
128
129// codes for boolean expressions
130#define VTK_PARSER_AND 48
131
132// codes for boolean expressions
133#define VTK_PARSER_OR 49
134
135// codes for scalar variables come before those for vectors. Do not define
136// values for VTK_PARSER_BEGIN_VARIABLES+1, VTK_PARSER_BEGIN_VARIABLES+2, ...,
137// because they are used to look up variables numbered 1, 2, ...
138#define VTK_PARSER_BEGIN_VARIABLES 50
139
140// the value that is returned as a result if there is an error
141#define VTK_PARSER_ERROR_RESULT VTK_FLOAT_MAX
142
143VTK_ABI_NAMESPACE_BEGIN
144class VTKCOMMONMISC_EXPORT vtkFunctionParser : public vtkObject
145{
146public:
149 void PrintSelf(ostream& os, vtkIndent indent) override;
150
155
157
160 void SetFunction(const char* function);
161 vtkGetStringMacro(Function);
163
169
175
180
182
186 void GetVectorResult(double result[3])
187 {
188 double* r = this->GetVectorResult();
189 result[0] = r[0];
190 result[1] = r[1];
191 result[2] = r[2];
192 }
194
196
202 void SetScalarVariableValue(const char* variableName, double value);
203 void SetScalarVariableValue(const std::string& variableName, double value)
204 {
205 this->SetScalarVariableValue(variableName.c_str(), value);
206 }
207 void SetScalarVariableValue(int i, double value);
209
211
214 double GetScalarVariableValue(const char* variableName);
215 double GetScalarVariableValue(const std::string& variableName)
216 {
217 return this->GetScalarVariableValue(variableName.c_str());
218 }
221
223
230 const char* variableName, double xValue, double yValue, double zValue);
232 const std::string& variableName, double xValue, double yValue, double zValue)
233 {
234 this->SetVectorVariableValue(variableName.c_str(), xValue, yValue, zValue);
235 }
236 void SetVectorVariableValue(const char* variableName, const double values[3])
237 {
238 this->SetVectorVariableValue(variableName, values[0], values[1], values[2]);
239 }
240 void SetVectorVariableValue(const std::string& variableName, const double values[3])
241 {
242 this->SetVectorVariableValue(variableName.c_str(), values[0], values[1], values[2]);
243 }
244 void SetVectorVariableValue(int i, double xValue, double yValue, double zValue);
245 void SetVectorVariableValue(int i, const double values[3])
246 {
247 this->SetVectorVariableValue(i, values[0], values[1], values[2]);
248 }
250
252
255 double* GetVectorVariableValue(const char* variableName) VTK_SIZEHINT(3);
256 double* GetVectorVariableValue(const std::string& variableName) VTK_SIZEHINT(3)
257 {
258 return this->GetVectorVariableValue(variableName.c_str());
259 }
260 void GetVectorVariableValue(const char* variableName, double value[3])
261 {
262 double* r = this->GetVectorVariableValue(variableName);
263 value[0] = r[0];
264 value[1] = r[1];
265 value[2] = r[2];
266 }
267 void GetVectorVariableValue(const std::string& variableName, double value[3])
268 {
269 this->GetVectorVariableValue(variableName.c_str(), value);
270 }
272 void GetVectorVariableValue(int i, double value[3])
273 {
274 double* r = this->GetVectorVariableValue(i);
275 value[0] = r[0];
276 value[1] = r[1];
277 value[2] = r[2];
278 }
280
284 int GetNumberOfScalarVariables() { return static_cast<int>(this->ScalarVariableNames.size()); }
285
289 int GetScalarVariableIndex(const char* name);
290 int GetScalarVariableIndex(const std::string& name)
291 {
292 return this->GetScalarVariableIndex(name.c_str());
293 }
294
298 int GetNumberOfVectorVariables() { return static_cast<int>(this->VectorVariableNames.size()); }
299
303 int GetVectorVariableIndex(const char* name);
304 int GetVectorVariableIndex(const std::string& name)
305 {
306 return this->GetVectorVariableIndex(name.c_str());
307 }
308
312 const char* GetScalarVariableName(int i);
313
317 const char* GetVectorVariableName(int i);
318
320
326 bool GetScalarVariableNeeded(const char* variableName);
327 bool GetScalarVariableNeeded(const std::string& variableName)
328 {
329 return GetScalarVariableNeeded(variableName.c_str());
330 }
332
334
340 bool GetVectorVariableNeeded(const char* variableName);
341 bool GetVectorVariableNeeded(const std::string& variableName)
342 {
343 return this->GetVectorVariableNeeded(variableName.c_str());
344 }
346
351
356
361
363
369 vtkSetMacro(ReplaceInvalidValues, vtkTypeBool);
370 vtkGetMacro(ReplaceInvalidValues, vtkTypeBool);
371 vtkBooleanMacro(ReplaceInvalidValues, vtkTypeBool);
372 vtkSetMacro(ReplacementValue, double);
373 vtkGetMacro(ReplacementValue, double);
375
379 void CheckExpression(int& pos, char** error);
380
385
386protected:
389
390 int Parse();
391
395 bool Evaluate();
396
398
399 void CopyParseError(int& position, char** error);
400
402 char* RemoveSpacesFrom(const char* variableName);
404
406 void BuildInternalSubstringStructure(int beginIndex, int endIndex);
407 void AddInternalByte(unsigned int newByte);
408
409 int IsSubstringCompletelyEnclosed(int beginIndex, int endIndex);
410 int FindEndOfMathFunction(int beginIndex);
411 int FindEndOfMathConstant(int beginIndex);
412
413 int IsVariableName(int currentIndex);
415
416 int GetMathFunctionNumber(int currentIndex);
418 int GetMathFunctionStringLength(int mathFunctionNumber);
419 int GetMathConstantNumber(int currentIndex);
420 int GetMathConstantStringLength(int mathConstantNumber);
421 unsigned char GetElementaryOperatorNumber(char op);
422 unsigned int GetOperandNumber(int currentIndex);
423 int GetVariableNameLength(int variableNumber);
424
426
432
433 vtkSetStringMacro(ParseError);
434
436
437 char* Function;
439
441 std::vector<std::string> ScalarVariableNames;
442 std::vector<std::string> VectorVariableNames;
443 std::vector<double> ScalarVariableValues;
444 std::vector<vtkTuple<double, 3>> VectorVariableValues;
445 std::vector<bool> ScalarVariableNeeded;
446 std::vector<bool> VectorVariableNeeded;
447
448 std::vector<unsigned int> ByteCode;
450 double* Immediates;
452 double* Stack;
455
459
462
465
466private:
467 vtkFunctionParser(const vtkFunctionParser&) = delete;
468 void operator=(const vtkFunctionParser&) = delete;
469};
470
471VTK_ABI_NAMESPACE_END
472#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.
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
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:108
abstract base class for most VTK objects
Definition vtkObject.h:162
record modification and/or execution time
int vtkTypeBool
Definition vtkABI.h:64
vtkTypeUInt32 vtkMTimeType
Definition vtkType.h:270
#define VTK_SIZEHINT(...)