VTK  9.2.6
vtkExprTkFunctionParser.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkExprTkFunctionParser.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
37 #ifndef vtkExprTkFunctionParser_h
38 #define vtkExprTkFunctionParser_h
39 
40 #include "vtkCommonMiscModule.h" // For export macro
41 #include "vtkObject.h"
42 #include "vtkTuple.h" // needed for vtkTuple
43 #include <string> // needed for string.
44 #include <vector> // needed for vector
45 
46 // forward declarations for ExprTk tools
47 struct vtkExprTkTools;
48 
49 class VTKCOMMONMISC_EXPORT vtkExprTkFunctionParser : public vtkObject
50 {
51 public:
52  static vtkExprTkFunctionParser* New();
54  void PrintSelf(ostream& os, vtkIndent indent) override;
55 
59  vtkMTimeType GetMTime() override;
60 
62 
65  void SetFunction(const char* function);
66  const char* GetFunction() { return this->Function.c_str(); }
68 
73  int IsScalarResult();
74 
79  int IsVectorResult();
80 
87  double GetScalarResult();
88 
90 
96  double* GetVectorResult() VTK_SIZEHINT(3);
97  void GetVectorResult(double result[3])
98  {
99  double* r = this->GetVectorResult();
100  result[0] = r[0];
101  result[1] = r[1];
102  result[2] = r[2];
103  }
105 
107 
117  void SetScalarVariableValue(const std::string& variableName, double value);
118  void SetScalarVariableValue(int i, double value);
120 
122 
125  double GetScalarVariableValue(const std::string& variableName);
126  double GetScalarVariableValue(int i);
128 
130 
140  void SetVectorVariableValue(
141  const std::string& variableName, double xValue, double yValue, double zValue);
142  void SetVectorVariableValue(const std::string& variableName, double values[3])
143  {
144  this->SetVectorVariableValue(variableName, values[0], values[1], values[2]);
145  }
146  void SetVectorVariableValue(int i, double xValue, double yValue, double zValue);
147  void SetVectorVariableValue(int i, double values[3])
148  {
149  this->SetVectorVariableValue(i, values[0], values[1], values[2]);
150  }
152 
154 
157  double* GetVectorVariableValue(const std::string& variableName) VTK_SIZEHINT(3);
158  void GetVectorVariableValue(const std::string& variableName, double value[3])
159  {
160  double* r = this->GetVectorVariableValue(variableName);
161  value[0] = r[0];
162  value[1] = r[1];
163  value[2] = r[2];
164  }
165  double* GetVectorVariableValue(int i) VTK_SIZEHINT(3);
166  void GetVectorVariableValue(int i, double value[3])
167  {
168  double* r = this->GetVectorVariableValue(i);
169  value[0] = r[0];
170  value[1] = r[1];
171  value[2] = r[2];
172  }
174 
179  {
180  return static_cast<int>(this->UsedScalarVariableNames.size());
181  }
182 
186  int GetScalarVariableIndex(const std::string& name);
187 
192  {
193  return static_cast<int>(this->UsedVectorVariableNames.size());
194  }
195 
199  int GetVectorVariableIndex(const std::string& name);
200 
204  std::string GetScalarVariableName(int i);
205 
209  std::string GetVectorVariableName(int i);
210 
212 
217  bool GetScalarVariableNeeded(int i);
218  bool GetScalarVariableNeeded(const std::string& variableName);
220 
222 
227  bool GetVectorVariableNeeded(int i);
228  bool GetVectorVariableNeeded(const std::string& variableName);
230 
234  void RemoveAllVariables();
235 
239  void RemoveScalarVariables();
240 
244  void RemoveVectorVariables();
245 
247 
253  vtkSetMacro(ReplaceInvalidValues, vtkTypeBool);
254  vtkGetMacro(ReplaceInvalidValues, vtkTypeBool);
255  vtkBooleanMacro(ReplaceInvalidValues, vtkTypeBool);
256  vtkSetMacro(ReplacementValue, double);
257  vtkGetMacro(ReplacementValue, double);
259 
263  void InvalidateFunction();
264 
272  static std::string SanitizeName(const char* name);
273 
274 protected:
276  ~vtkExprTkFunctionParser() override;
277 
285  {
287  SaveResultInVariable
288  };
289 
295  int Parse(ParseMode mode);
296 
301  {
303  Norm
304  };
305 
314  std::string FixVectorReturningFunctionOccurrences(
315  VectorReturningFunction vectorReturningFunction);
316 
320  bool CheckOldFormatOfDotProductUsage();
321 
325  bool Evaluate();
326 
331  void UpdateNeededVariables();
332 
336 
337  // original and used variables names are the same, except if the original
338  // ones are not valid.
339  std::vector<std::string> OriginalScalarVariableNames;
340  std::vector<std::string> UsedScalarVariableNames;
341  std::vector<std::string> OriginalVectorVariableNames;
342  std::vector<std::string> UsedVectorVariableNames;
343  // pointers for scalar and vector variables are used to enforce
344  // that their memory address will not change due to a possible
345  // resizing of their container (std::vector), ExprTk requires the
346  // memory address of the given variable to remain the same.
347  std::vector<double*> ScalarVariableValues;
348  std::vector<vtkTuple<double, 3>*> VectorVariableValues;
349  std::vector<bool> ScalarVariableNeeded;
350  std::vector<bool> VectorVariableNeeded;
351 
354 
357 
358  vtkExprTkTools* ExprTkTools;
359 
362 
363 private:
365  void operator=(const vtkExprTkFunctionParser&) = delete;
366 };
367 
368 #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:62
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:287
record modification and/or execution time
Definition: vtkTimeStamp.h:35
std::vector< bool > ScalarVariableNeeded
std::vector< double * > ScalarVariableValues
int vtkTypeBool
Definition: vtkABI.h:69
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:39
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.