VTK  9.7.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 
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:
42  void PrintSelf(ostream& os, vtkIndent indent) override;
43  static vtkExprTkFunctionParser* New();
44 
48  vtkMTimeType GetMTime() override;
49 
53  vtkGetMacro(ResultSize, int);
54 
56 
61  void SetFunction(const char* function);
62  const char* GetFunction() { return this->Function.c_str(); }
64 
69  int IsScalarResult();
70 
75  int IsVectorResult();
76 
83  double GetScalarResult();
84 
86 
92  double* GetVectorResult() VTK_SIZEHINT(this->GetResultSize());
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  }
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);
123  double GetScalarVariableValue(int i);
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  }
164  double* GetVectorVariableValue(int i) VTK_SIZEHINT(3);
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  }
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 
216  bool GetScalarVariableNeeded(int i);
217  bool GetScalarVariableNeeded(const std::string& variableName);
219 
221 
226  bool GetVectorVariableNeeded(int i);
227  bool GetVectorVariableNeeded(const std::string& variableName);
229 
233  void RemoveAllVariables();
234 
238  void RemoveScalarVariables();
239 
243  void RemoveVectorVariables();
244 
246 
252  vtkSetMacro(ReplaceInvalidValues, vtkTypeBool);
253  vtkGetMacro(ReplaceInvalidValues, vtkTypeBool);
254  vtkBooleanMacro(ReplaceInvalidValues, vtkTypeBool);
255  vtkSetMacro(ReplacementValue, double);
256  vtkGetMacro(ReplacementValue, double);
258 
262  void InvalidateFunction();
263 
271  static std::string SanitizeName(const char* name);
272 
273 protected:
275  ~vtkExprTkFunctionParser() override;
276 
284  {
286  SaveResultInVariable
287  };
288 
294  int Parse(ParseMode mode);
295 
300  {
302  Norm
303  };
304 
313  std::string FixVectorReturningFunctionOccurrences(
314  VectorReturningFunction vectorReturningFunction);
315 
319  bool CheckOldFormatOfDotProductUsage();
320 
324  bool Evaluate();
325 
330  void UpdateNeededVariables();
331 
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 
363 private:
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 
397 VTK_ABI_NAMESPACE_END
398 #endif
std::vector< double > Result
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:52
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:318
std::vector< std::vector< double > * > VectorVariableValues
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 SetVectorVariableValue(int i, double xValue, double yValue, double zValue)
Set the value of a vector variable.
void GetVectorVariableValue(int i, double value[3])
Get the value of a vector variable.
std::vector< std::string > OriginalScalarVariableNames
a simple class to control print indentation
Definition: vtkIndent.h:28
std::vector< std::string > UsedVectorVariableNames
std::vector< std::string > OriginalVectorVariableNames
void SetVectorVariableValue(const std::string &variableName, double xValue, double yValue, double zValue)
Set the value of a vector variable.
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.
std::vector< double > ParserErrorResult
Parse and evaluate a mathematical expression.
const char * GetFunction()
Set/Get input string to evaluate.