VTK  9.3.1
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:
41  static vtkExprTkFunctionParser* New();
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 
62  int IsScalarResult();
63 
68  int IsVectorResult();
69 
76  double GetScalarResult();
77 
79 
85  double* GetVectorResult() VTK_SIZEHINT(3);
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 
129  void SetVectorVariableValue(
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  }
154  double* GetVectorVariableValue(int i) VTK_SIZEHINT(3);
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 
175  int GetScalarVariableIndex(const std::string& name);
176 
181  {
182  return static_cast<int>(this->UsedVectorVariableNames.size());
183  }
184 
188  int GetVectorVariableIndex(const std::string& name);
189 
193  std::string GetScalarVariableName(int i);
194 
198  std::string GetVectorVariableName(int i);
199 
201 
206  bool GetScalarVariableNeeded(int i);
207  bool GetScalarVariableNeeded(const std::string& variableName);
209 
211 
216  bool GetVectorVariableNeeded(int i);
217  bool GetVectorVariableNeeded(const std::string& variableName);
219 
223  void RemoveAllVariables();
224 
228  void RemoveScalarVariables();
229 
233  void RemoveVectorVariables();
234 
236 
242  vtkSetMacro(ReplaceInvalidValues, vtkTypeBool);
243  vtkGetMacro(ReplaceInvalidValues, vtkTypeBool);
244  vtkBooleanMacro(ReplaceInvalidValues, vtkTypeBool);
245  vtkSetMacro(ReplacementValue, double);
246  vtkGetMacro(ReplacementValue, double);
248 
252  void InvalidateFunction();
253 
261  static std::string SanitizeName(const char* name);
262 
263 protected:
265  ~vtkExprTkFunctionParser() override;
266 
274  {
276  SaveResultInVariable
277  };
278 
284  int Parse(ParseMode mode);
285 
290  {
292  Norm
293  };
294 
303  std::string FixVectorReturningFunctionOccurrences(
304  VectorReturningFunction vectorReturningFunction);
305 
309  bool CheckOldFormatOfDotProductUsage();
310 
314  bool Evaluate();
315 
320  void UpdateNeededVariables();
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
void GetVectorVariableValue(const std::string &variableName, double value[3])
Get the value of a vector variable.
abstract base class for most VTK objects
Definition: vtkObject.h:51
std::vector< std::string > UsedScalarVariableNames
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
vtkTypeUInt32 vtkMTimeType
Definition: vtkType.h:270
record modification and/or execution time
Definition: vtkTimeStamp.h:24
std::vector< bool > ScalarVariableNeeded
std::vector< double * > ScalarVariableValues
int vtkTypeBool
Definition: vtkABI.h:64
void GetVectorVariableValue(int i, double value[3])
Get the value of a vector variable.
std::vector< std::string > OriginalScalarVariableNames
std::vector< vtkTuple< double, 3 > * > VectorVariableValues
a simple class to control print indentation
Definition: vtkIndent.h:28
std::vector< std::string > UsedVectorVariableNames
std::vector< std::string > OriginalVectorVariableNames
virtual vtkMTimeType GetMTime()
Return this object's modified time.
int GetNumberOfScalarVariables()
Get the number of scalar variables.
#define VTK_SIZEHINT(...)
int GetNumberOfVectorVariables()
Get the number of vector variables.
ParseMode
The first mode parses the function and uses a return statement to identify the ReturnType.
std::vector< bool > VectorVariableNeeded
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
VectorReturningFunction
Enum that defines the vector returning functions that are not supported by ExprTk.
vtkTuple< double, 3 > Result
void SetVectorVariableValue(int i, double values[3])
Set the value of a vector variable.
Parse and evaluate a mathematical expression.
const char * GetFunction()
Set/Get input string to evaluate.
void SetVectorVariableValue(const std::string &variableName, double values[3])
Set the value of a vector variable.