VTK  9.5.20251214
vtkExprTkFunctionParser.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
24
25#ifndef vtkExprTkFunctionParser_h
26#define vtkExprTkFunctionParser_h
27
28#include "vtkCommonMiscModule.h" // For export macro
29#include "vtkObject.h"
30
31#include <string> // needed for string.
32#include <vector> // needed for vector
33
34// forward declarations for ExprTk tools
35VTK_ABI_NAMESPACE_BEGIN
36struct vtkExprTkTools;
37
38class VTKCOMMONMISC_EXPORT vtkExprTkFunctionParser : public vtkObject
39{
40public:
42 void PrintSelf(ostream& os, vtkIndent indent) override;
44
49
53 vtkGetMacro(ResultSize, int);
54
56
61 void SetFunction(const char* function);
62 const char* GetFunction() { return this->Function.c_str(); }
64
70
76
84
86
93 void GetVectorResult(double* result)
94 {
95 double* r = this->GetVectorResult();
96 for (int i = 0; i < this->ResultSize; ++i)
97 {
98 result[i] = r[i];
99 }
100 }
101
102
104
114 void SetScalarVariableValue(const std::string& variableName, double value);
115 void SetScalarVariableValue(int i, double value);
117
119
122 double GetScalarVariableValue(const std::string& variableName);
125
127
138 const std::string& variableName, double xValue, double yValue, double zValue)
139 {
140 double values[3] = { xValue, yValue, zValue };
141 this->SetVectorVariableValue(variableName, values, 3);
142 }
143 void SetVectorVariableValue(const std::string& variableName, double* values, int size = 3);
144 void SetVectorVariableValue(int i, double xValue, double yValue, double zValue)
145 {
146 double values[3] = { xValue, yValue, zValue };
147 this->SetVectorVariableValue(i, values, 3);
148 }
149 void SetVectorVariableValue(int i, double values[3], int size = 3);
151
153
156 double* GetVectorVariableValue(const std::string& variableName) VTK_SIZEHINT(3);
157 void GetVectorVariableValue(const std::string& variableName, double value[3])
158 {
159 double* r = this->GetVectorVariableValue(variableName);
160 value[0] = r[0];
161 value[1] = r[1];
162 value[2] = r[2];
163 }
165 void GetVectorVariableValue(int i, double value[3])
166 {
167 double* r = this->GetVectorVariableValue(i);
168 value[0] = r[0];
169 value[1] = r[1];
170 value[2] = r[2];
171 }
172
173
178 {
179 return static_cast<int>(this->UsedScalarVariableNames.size());
180 }
181
185 int GetScalarVariableIndex(const std::string& name);
186
191 {
192 return static_cast<int>(this->UsedVectorVariableNames.size());
193 }
194
198 int GetVectorVariableIndex(const std::string& name);
199
203 std::string GetScalarVariableName(int i);
204
208 std::string GetVectorVariableName(int i);
209
211
217 bool GetScalarVariableNeeded(const std::string& variableName);
219
221
227 bool GetVectorVariableNeeded(const std::string& variableName);
229
234
239
244
246
255 vtkSetMacro(ReplacementValue, double);
256 vtkGetMacro(ReplacementValue, double);
258
263
271 static std::string SanitizeName(const char* name);
272
273protected:
276
288
294 int Parse(ParseMode mode);
295
304
314 VectorReturningFunction vectorReturningFunction);
315
320
324 bool Evaluate();
325
331
332 std::string Function;
334 std::string ExpressionString;
335
336 // original and used variables names are the same, except if the original
337 // ones are not valid.
338 std::vector<std::string> OriginalScalarVariableNames;
339 std::vector<std::string> UsedScalarVariableNames;
340 std::vector<std::string> OriginalVectorVariableNames;
341 std::vector<std::string> UsedVectorVariableNames;
342 // pointers for scalar and vector variables are used to enforce
343 // that their memory address will not change due to a possible
344 // resizing of their container (std::vector), ExprTk requires the
345 // memory address of the given variable to remain the same.
346 std::vector<double*> ScalarVariableValues;
347 std::vector<std::vector<double>*> VectorVariableValues;
348 std::vector<bool> ScalarVariableNeeded;
349 std::vector<bool> VectorVariableNeeded;
350
353
356
357 vtkExprTkTools* ExprTkTools;
358
360 std::vector<double> Result;
361 std::vector<double> ParserErrorResult;
362
363private:
365 void operator=(const vtkExprTkFunctionParser&) = delete;
366
367 enum SizeModes
368 {
369 AutoDetected = 0,
370 VectorDefined = 1
371 };
372
373 int SizeMode;
374 int ResultSize;
375
379 static SizeModes DetermineSizeMode(const std::string& function);
380
394 vtkGetMacro(SizeMode, int);
395};
396
397VTK_ABI_NAMESPACE_END
398#endif
bool CheckOldFormatOfDotProductUsage()
Check possible usage of old format of dot product, e.g.
std::vector< std::vector< double > * > VectorVariableValues
const char * GetFunction()
Set/Get input string to evaluate.
void GetVectorVariableValue(int i, double value[3])
Get the value of a vector variable.
void RemoveScalarVariables()
Remove all the scalar variables.
int GetNumberOfScalarVariables()
Get the number of scalar variables.
double * GetVectorVariableValue(const std::string &variableName)
Get the value of a vector variable.
void UpdateNeededVariables()
Collects meta-data about which variables are needed by the current function.
void GetVectorVariableValue(const std::string &variableName, double value[3])
Get the value of a vector variable.
int GetNumberOfVectorVariables()
Get the number of vector variables.
std::vector< std::string > OriginalScalarVariableNames
std::vector< bool > ScalarVariableNeeded
std::vector< std::string > OriginalVectorVariableNames
void SetScalarVariableValue(const std::string &variableName, double value)
Set the value of a scalar variable.
void RemoveVectorVariables()
Remove all the vector variables.
void RemoveAllVariables()
Remove all the current variables.
double GetScalarVariableValue(const std::string &variableName)
Get the value of a scalar variable.
int GetVectorVariableIndex(const std::string &name)
Get scalar variable index or -1 if not found.
double * GetVectorVariableValue(int i)
Get the value of a vector variable.
void InvalidateFunction()
Allow the user to force the function to be re-parsed.
int GetScalarVariableIndex(const std::string &name)
Get scalar variable index or -1 if not found.
std::vector< std::string > UsedVectorVariableNames
double GetScalarResult()
Get a scalar result from evaluating the input function.
void SetVectorVariableValue(int i, double values[3], int size=3)
Set the value of a vector variable.
ParseMode
The first mode parses the function and uses a return statement to identify the ReturnType.
int Parse(ParseMode mode)
Parses the given function, returning true on success, false on failure.
std::string FixVectorReturningFunctionOccurrences(VectorReturningFunction vectorReturningFunction)
ExprTk does not support functions which return a vector.
bool GetScalarVariableNeeded(const std::string &variableName)
Returns whether a scalar variable is needed for the function evaluation.
void SetVectorVariableValue(const std::string &variableName, double xValue, double yValue, double zValue)
Set the value of a vector variable.
void SetFunction(const char *function)
Set/Get input string to evaluate.
void SetVectorVariableValue(int i, double xValue, double yValue, double zValue)
Set the value of a vector variable.
double * GetVectorResult()
Get a vector result from evaluating the input function.
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
int IsScalarResult()
Check whether the result is a scalar result.
std::string GetScalarVariableName(int i)
Get the ith scalar variable name.
std::vector< std::string > UsedScalarVariableNames
bool GetVectorVariableNeeded(int i)
Returns whether a vector variable is needed for the function evaluation.
bool GetScalarVariableNeeded(int i)
Returns whether a scalar variable is needed for the function evaluation.
double GetScalarVariableValue(int i)
Get the value of a scalar variable.
static std::string SanitizeName(const char *name)
Sanitize a label/name to remove spaces, delimiters etc.
std::vector< bool > VectorVariableNeeded
virtual int GetResultSize()
Get the size of the result.
vtkMTimeType GetMTime() override
Return parser's MTime.
void SetVectorVariableValue(const std::string &variableName, double *values, int size=3)
Set the value of a vector variable.
~vtkExprTkFunctionParser() override
bool Evaluate()
Evaluate the function, returning true on success, false on failure.
std::string GetVectorVariableName(int i)
Get the ith vector variable name.
VectorReturningFunction
Enum that defines the vector returning functions that are not supported by ExprTk.
bool GetVectorVariableNeeded(const std::string &variableName)
Returns whether a vector variable is needed for the function evaluation.
static vtkExprTkFunctionParser * New()
std::vector< double * > ScalarVariableValues
int IsVectorResult()
Check whether the result is a vector result.
void SetScalarVariableValue(int i, double value)
Set the value of a scalar variable.
std::vector< double > ParserErrorResult
a simple class to control print indentation
Definition vtkIndent.h:108
record modification and/or execution time
int vtkTypeBool
Definition vtkABI.h:64
vtkTypeUInt32 vtkMTimeType
Definition vtkType.h:322
#define VTK_SIZEHINT(...)