VTK  9.1.0
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 =========================================================================*/
34 #ifndef vtkExprTkFunctionParser_h
35 #define vtkExprTkFunctionParser_h
36 
37 #include "vtkCommonMiscModule.h" // For export macro
38 #include "vtkObject.h"
39 #include "vtkTuple.h" // needed for vtkTuple
40 #include <string> // needed for string.
41 #include <vector> // needed for vector
42 
43 // forward declarations for ExprTk tools
44 struct vtkExprTkTools;
45 
46 class VTKCOMMONMISC_EXPORT vtkExprTkFunctionParser : public vtkObject
47 {
48 public:
51  void PrintSelf(ostream& os, vtkIndent indent) override;
52 
56  vtkMTimeType GetMTime() override;
57 
59 
62  void SetFunction(const char* function);
63  const char* GetFunction() { return this->Function.c_str(); }
65 
71 
77 
84  double GetScalarResult();
85 
87 
94  void GetVectorResult(double result[3])
95  {
96  double* r = this->GetVectorResult();
97  result[0] = r[0];
98  result[1] = r[1];
99  result[2] = r[2];
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  void SetVectorVariableValue(const std::string& variableName, double values[3])
140  {
141  this->SetVectorVariableValue(variableName, values[0], values[1], values[2]);
142  }
143  void SetVectorVariableValue(int i, double xValue, double yValue, double zValue);
144  void SetVectorVariableValue(int i, double values[3])
145  {
146  this->SetVectorVariableValue(i, values[0], values[1], values[2]);
147  }
149 
151 
154  double* GetVectorVariableValue(const std::string& variableName) VTK_SIZEHINT(3);
155  void GetVectorVariableValue(const std::string& variableName, double value[3])
156  {
157  double* r = this->GetVectorVariableValue(variableName);
158  value[0] = r[0];
159  value[1] = r[1];
160  value[2] = r[2];
161  }
163  void GetVectorVariableValue(int i, double value[3])
164  {
165  double* r = this->GetVectorVariableValue(i);
166  value[0] = r[0];
167  value[1] = r[1];
168  value[2] = r[2];
169  }
171 
176  {
177  return static_cast<int>(this->UsedScalarVariableNames.size());
178  }
179 
184 
189  {
190  return static_cast<int>(this->UsedVectorVariableNames.size());
191  }
192 
197 
202 
207 
209 
215  bool GetScalarVariableNeeded(const std::string& variableName);
217 
219 
225  bool GetVectorVariableNeeded(const std::string& variableName);
227 
232 
237 
242 
244 
250  vtkSetMacro(ReplaceInvalidValues, vtkTypeBool);
251  vtkGetMacro(ReplaceInvalidValues, vtkTypeBool);
252  vtkBooleanMacro(ReplaceInvalidValues, vtkTypeBool);
253  vtkSetMacro(ReplacementValue, double);
254  vtkGetMacro(ReplacementValue, double);
256 
261 
269  static std::string SanitizeName(const char* name);
270 
271 protected:
274 
282  {
284  SaveResultInVariable
285  };
286 
293 
298  {
300  Norm
301  };
302 
312  VectorReturningFunction vectorReturningFunction);
313 
318 
322  bool Evaluate();
323 
329 
333 
334  // original and used variables names are the same, except if the original
335  // ones are not valid.
336  std::vector<std::string> OriginalScalarVariableNames;
337  std::vector<std::string> UsedScalarVariableNames;
338  std::vector<std::string> OriginalVectorVariableNames;
339  std::vector<std::string> UsedVectorVariableNames;
340  // pointers for scalar and vector variables are used to enforce
341  // that their memory address will not change due to a possible
342  // resizing of their container (std::vector), ExprTk requires the
343  // memory address of the given variable to remain the same.
344  std::vector<double*> ScalarVariableValues;
345  std::vector<vtkTuple<double, 3>*> VectorVariableValues;
346  std::vector<bool> ScalarVariableNeeded;
347  std::vector<bool> VectorVariableNeeded;
348 
353 
356 
357  vtkExprTkTools* ExprTkTools;
358 
361 
362 private:
364  void operator=(const vtkExprTkFunctionParser&) = delete;
365 };
366 
367 #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:34
abstract base class for most VTK objects
Definition: vtkObject.h:63
record modification and/or execution time
Definition: vtkTimeStamp.h:33
@ mode
Definition: vtkX3D.h:253
@ value
Definition: vtkX3D.h:226
@ name
Definition: vtkX3D.h:225
@ string
Definition: vtkX3D.h:496
int vtkTypeBool
Definition: vtkABI.h:69
vtkTypeUInt32 vtkMTimeType
Definition: vtkType.h:287
#define VTK_SIZEHINT(...)