VTK  9.3.0
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
25 #ifndef vtkExprTkFunctionParser_h
26 #define vtkExprTkFunctionParser_h
27 
28 #include "vtkCommonMiscModule.h" // For export macro
29 #include "vtkObject.h"
30 #include "vtkTuple.h" // needed for vtkTuple
31 #include <string> // needed for string.
32 #include <vector> // needed for vector
33 
34 // forward declarations for ExprTk tools
35 VTK_ABI_NAMESPACE_BEGIN
36 struct vtkExprTkTools;
37 
38 class VTKCOMMONMISC_EXPORT vtkExprTkFunctionParser : public vtkObject
39 {
40 public:
43  void PrintSelf(ostream& os, vtkIndent indent) override;
44 
48  vtkMTimeType GetMTime() override;
49 
51 
54  void SetFunction(const char* function);
55  const char* GetFunction() { return this->Function.c_str(); }
57 
63 
69 
76  double GetScalarResult();
77 
79 
86  void GetVectorResult(double result[3])
87  {
88  double* r = this->GetVectorResult();
89  result[0] = r[0];
90  result[1] = r[1];
91  result[2] = r[2];
92  }
94 
96 
106  void SetScalarVariableValue(const std::string& variableName, double value);
107  void SetScalarVariableValue(int i, double value);
109 
111 
114  double GetScalarVariableValue(const std::string& variableName);
115  double GetScalarVariableValue(int i);
117 
119 
130  const std::string& variableName, double xValue, double yValue, double zValue);
131  void SetVectorVariableValue(const std::string& variableName, double values[3])
132  {
133  this->SetVectorVariableValue(variableName, values[0], values[1], values[2]);
134  }
135  void SetVectorVariableValue(int i, double xValue, double yValue, double zValue);
136  void SetVectorVariableValue(int i, double values[3])
137  {
138  this->SetVectorVariableValue(i, values[0], values[1], values[2]);
139  }
141 
143 
146  double* GetVectorVariableValue(const std::string& variableName) VTK_SIZEHINT(3);
147  void GetVectorVariableValue(const std::string& variableName, double value[3])
148  {
149  double* r = this->GetVectorVariableValue(variableName);
150  value[0] = r[0];
151  value[1] = r[1];
152  value[2] = r[2];
153  }
155  void GetVectorVariableValue(int i, double value[3])
156  {
157  double* r = this->GetVectorVariableValue(i);
158  value[0] = r[0];
159  value[1] = r[1];
160  value[2] = r[2];
161  }
163 
168  {
169  return static_cast<int>(this->UsedScalarVariableNames.size());
170  }
171 
176 
181  {
182  return static_cast<int>(this->UsedVectorVariableNames.size());
183  }
184 
189 
194 
199 
201 
207  bool GetScalarVariableNeeded(const std::string& variableName);
209 
211 
217  bool GetVectorVariableNeeded(const std::string& variableName);
219 
224 
229 
234 
236 
242  vtkSetMacro(ReplaceInvalidValues, vtkTypeBool);
243  vtkGetMacro(ReplaceInvalidValues, vtkTypeBool);
244  vtkBooleanMacro(ReplaceInvalidValues, vtkTypeBool);
245  vtkSetMacro(ReplacementValue, double);
246  vtkGetMacro(ReplacementValue, double);
248 
253 
261  static std::string SanitizeName(const char* name);
262 
263 protected:
266 
274  {
276  SaveResultInVariable
277  };
278 
285 
290  {
292  Norm
293  };
294 
304  VectorReturningFunction vectorReturningFunction);
305 
310 
314  bool Evaluate();
315 
321 
325 
326  // original and used variables names are the same, except if the original
327  // ones are not valid.
328  std::vector<std::string> OriginalScalarVariableNames;
329  std::vector<std::string> UsedScalarVariableNames;
330  std::vector<std::string> OriginalVectorVariableNames;
331  std::vector<std::string> UsedVectorVariableNames;
332  // pointers for scalar and vector variables are used to enforce
333  // that their memory address will not change due to a possible
334  // resizing of their container (std::vector), ExprTk requires the
335  // memory address of the given variable to remain the same.
336  std::vector<double*> ScalarVariableValues;
337  std::vector<vtkTuple<double, 3>*> VectorVariableValues;
338  std::vector<bool> ScalarVariableNeeded;
339  std::vector<bool> VectorVariableNeeded;
340 
343 
346 
347  vtkExprTkTools* ExprTkTools;
348 
351 
352 private:
354  void operator=(const vtkExprTkFunctionParser&) = delete;
355 };
356 
357 VTK_ABI_NAMESPACE_END
358 #endif
Parse and evaluate a mathematical expression.
static vtkExprTkFunctionParser * New()
bool CheckOldFormatOfDotProductUsage()
Check possible usage of old format of dot product, e.g.
void GetVectorVariableValue(int i, double value[3])
Get the value of a vector variable.
void RemoveScalarVariables()
Remove all the scalar variables.
double * GetVectorResult()
Get a vector result from evaluating the input function.
int GetNumberOfScalarVariables()
Get the number of scalar variables.
std::vector< vtkTuple< double, 3 > * > VectorVariableValues
void UpdateNeededVariables()
Collects meta-data about which variables are needed by the current function.
double * GetVectorVariableValue(const std::string &variableName)
Get the value of a vector variable.
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 SetVectorVariableValue(int i, double values[3])
Set the value of a vector variable.
void RemoveAllVariables()
Remove all the current variables.
double * GetVectorVariableValue(int i)
Get the value of a vector variable.
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.
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.
void SetVectorVariableValue(const std::string &variableName, double values[3])
Set the value of a vector variable.
std::vector< std::string > UsedVectorVariableNames
double GetScalarResult()
Get a scalar result from evaluating the input function.
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.
vtkTuple< double, 3 > Result
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.
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.
const char * GetFunction()
Set/Get input string to evaluate.
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
vtkMTimeType GetMTime() override
Return parser's MTime.
~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.
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.
a simple class to control print indentation
Definition: vtkIndent.h:38
abstract base class for most VTK objects
Definition: vtkObject.h:61
record modification and/or execution time
Definition: vtkTimeStamp.h:34
@ mode
Definition: vtkX3D.h:247
@ value
Definition: vtkX3D.h:220
@ name
Definition: vtkX3D.h:219
@ string
Definition: vtkX3D.h:490
int vtkTypeBool
Definition: vtkABI.h:64
vtkTypeUInt32 vtkMTimeType
Definition: vtkType.h:270
#define VTK_SIZEHINT(...)