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
vtkExprTkFunctionParser::SetVectorVariableValue
void SetVectorVariableValue(int i, double values[3])
Set the value of a vector variable.
Definition: vtkExprTkFunctionParser.h:144
vtkExprTkFunctionParser::GetScalarVariableValue
double GetScalarVariableValue(int i)
Get the value of a scalar variable.
vtkExprTkFunctionParser::UsedScalarVariableNames
std::vector< std::string > UsedScalarVariableNames
Definition: vtkExprTkFunctionParser.h:337
vtkExprTkFunctionParser::SanitizeName
static std::string SanitizeName(const char *name)
Sanitize a label/name to remove spaces, delimiters etc.
vtkExprTkFunctionParser::GetVectorVariableNeeded
bool GetVectorVariableNeeded(const std::string &variableName)
Returns whether a vector variable is needed for the function evaluation.
vtkExprTkFunctionParser::Parse
int Parse(ParseMode mode)
Parses the given function, returning true on success, false on failure.
vtkX3D::value
@ value
Definition: vtkX3D.h:226
vtkExprTkFunctionParser::GetScalarVariableName
std::string GetScalarVariableName(int i)
Get the ith scalar variable name.
vtkExprTkFunctionParser::GetVectorVariableNeeded
bool GetVectorVariableNeeded(int i)
Returns whether a vector variable is needed for the function evaluation.
vtkExprTkFunctionParser::SetFunction
void SetFunction(const char *function)
Set/Get input string to evaluate.
vtkExprTkFunctionParser::VariableMTime
vtkTimeStamp VariableMTime
Definition: vtkExprTkFunctionParser.h:351
vtkExprTkFunctionParser::ParseMTime
vtkTimeStamp ParseMTime
Definition: vtkExprTkFunctionParser.h:350
vtkExprTkFunctionParser::GetVectorVariableValue
void GetVectorVariableValue(const std::string &variableName, double value[3])
Get the value of a vector variable.
Definition: vtkExprTkFunctionParser.h:155
vtkTimeStamp
record modification and/or execution time
Definition: vtkTimeStamp.h:52
vtkExprTkFunctionParser::ScalarVariableValues
std::vector< double * > ScalarVariableValues
Definition: vtkExprTkFunctionParser.h:344
vtkExprTkFunctionParser::ReplaceInvalidValues
vtkTypeBool ReplaceInvalidValues
Definition: vtkExprTkFunctionParser.h:354
vtkObject
abstract base class for most VTK objects
Definition: vtkObject.h:82
vtkExprTkFunctionParser::SetVectorVariableValue
void SetVectorVariableValue(int i, double xValue, double yValue, double zValue)
Set the value of a vector variable.
vtkExprTkFunctionParser::OriginalScalarVariableNames
std::vector< std::string > OriginalScalarVariableNames
Definition: vtkExprTkFunctionParser.h:336
vtkExprTkFunctionParser::VectorVariableValues
std::vector< vtkTuple< double, 3 > * > VectorVariableValues
Definition: vtkExprTkFunctionParser.h:345
vtkExprTkFunctionParser::GetScalarVariableValue
double GetScalarVariableValue(const std::string &variableName)
Get the value of a scalar variable.
vtkExprTkFunctionParser::GetScalarVariableNeeded
bool GetScalarVariableNeeded(const std::string &variableName)
Returns whether a scalar variable is needed for the function evaluation.
vtkExprTkFunctionParser::GetVectorVariableIndex
int GetVectorVariableIndex(const std::string &name)
Get scalar variable index or -1 if not found.
vtkExprTkFunctionParser::RemoveScalarVariables
void RemoveScalarVariables()
Remove all the scalar variables.
vtkExprTkFunctionParser::ScalarVariableNeeded
std::vector< bool > ScalarVariableNeeded
Definition: vtkExprTkFunctionParser.h:346
vtkExprTkFunctionParser::ResultType
int ResultType
Definition: vtkExprTkFunctionParser.h:359
vtkExprTkFunctionParser::RemoveAllVariables
void RemoveAllVariables()
Remove all the current variables.
vtkExprTkFunctionParser::CheckOldFormatOfDotProductUsage
bool CheckOldFormatOfDotProductUsage()
Check possible usage of old format of dot product, e.g.
vtkExprTkFunctionParser::vtkExprTkFunctionParser
vtkExprTkFunctionParser()
vtkExprTkFunctionParser::GetVectorVariableName
std::string GetVectorVariableName(int i)
Get the ith vector variable name.
vtkExprTkFunctionParser::New
static vtkExprTkFunctionParser * New()
vtkExprTkFunctionParser::Cross
@ Cross
Definition: vtkExprTkFunctionParser.h:299
VTK_SIZEHINT
#define VTK_SIZEHINT(...)
Definition: vtkWrappingHints.h:48
vtkExprTkFunctionParser::OriginalVectorVariableNames
std::vector< std::string > OriginalVectorVariableNames
Definition: vtkExprTkFunctionParser.h:338
vtkExprTkFunctionParser::UsedVectorVariableNames
std::vector< std::string > UsedVectorVariableNames
Definition: vtkExprTkFunctionParser.h:339
vtkExprTkFunctionParser::ReplacementValue
double ReplacementValue
Definition: vtkExprTkFunctionParser.h:355
vtkExprTkFunctionParser::GetVectorVariableValue
void GetVectorVariableValue(int i, double value[3])
Get the value of a vector variable.
Definition: vtkExprTkFunctionParser.h:163
vtkExprTkFunctionParser::GetScalarVariableIndex
int GetScalarVariableIndex(const std::string &name)
Get scalar variable index or -1 if not found.
vtkExprTkFunctionParser::IsScalarResult
int IsScalarResult()
Check whether the result is a scalar result.
vtkExprTkFunctionParser::PrintSelf
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
vtkExprTkFunctionParser::ExprTkTools
vtkExprTkTools * ExprTkTools
Definition: vtkExprTkFunctionParser.h:357
vtkIndent
a simple class to control print indentation
Definition: vtkIndent.h:113
vtkExprTkFunctionParser::GetVectorVariableValue
double * GetVectorVariableValue(const std::string &variableName)
Get the value of a vector variable.
vtkExprTkFunctionParser::FunctionMTime
vtkTimeStamp FunctionMTime
Definition: vtkExprTkFunctionParser.h:349
vtkExprTkFunctionParser::GetScalarResult
double GetScalarResult()
Get a scalar result from evaluating the input function.
vtkExprTkFunctionParser::RemoveVectorVariables
void RemoveVectorVariables()
Remove all the vector variables.
vtkExprTkFunctionParser::UpdateNeededVariables
void UpdateNeededVariables()
Collects meta-data about which variables are needed by the current function.
vtkExprTkFunctionParser::SetScalarVariableValue
void SetScalarVariableValue(int i, double value)
Set the value of a scalar variable.
vtkExprTkFunctionParser::IsVectorResult
int IsVectorResult()
Check whether the result is a vector result.
vtkExprTkFunctionParser::FunctionWithUsedVariableNames
std::string FunctionWithUsedVariableNames
Definition: vtkExprTkFunctionParser.h:331
vtkExprTkFunctionParser::DetectReturnType
@ DetectReturnType
Definition: vtkExprTkFunctionParser.h:283
vtkExprTkFunctionParser::ExpressionString
std::string ExpressionString
Definition: vtkExprTkFunctionParser.h:332
vtkExprTkFunctionParser::GetVectorVariableValue
double * GetVectorVariableValue(int i)
Get the value of a vector variable.
vtkExprTkFunctionParser::~vtkExprTkFunctionParser
~vtkExprTkFunctionParser() override
vtkExprTkFunctionParser
Parse and evaluate a mathematical expression.
Definition: vtkExprTkFunctionParser.h:47
vtkX3D::name
@ name
Definition: vtkX3D.h:225
vtkObject.h
vtkExprTkFunctionParser::FixVectorReturningFunctionOccurrences
std::string FixVectorReturningFunctionOccurrences(VectorReturningFunction vectorReturningFunction)
ExprTk does not support functions which return a vector.
vtkExprTkFunctionParser::InvalidateFunction
void InvalidateFunction()
Allow the user to force the function to be re-parsed.
vtkExprTkFunctionParser::SetVectorVariableValue
void SetVectorVariableValue(const std::string &variableName, double xValue, double yValue, double zValue)
Set the value of a vector variable.
vtkX3D::string
@ string
Definition: vtkX3D.h:496
vtkExprTkFunctionParser::GetNumberOfVectorVariables
int GetNumberOfVectorVariables()
Get the number of vector variables.
Definition: vtkExprTkFunctionParser.h:188
vtkExprTkFunctionParser::ParseMode
ParseMode
The first mode parses the function and uses a return statement to identify the ReturnType.
Definition: vtkExprTkFunctionParser.h:282
vtkExprTkFunctionParser::SetScalarVariableValue
void SetScalarVariableValue(const std::string &variableName, double value)
Set the value of a scalar variable.
vtkExprTkFunctionParser::GetScalarVariableNeeded
bool GetScalarVariableNeeded(int i)
Returns whether a scalar variable is needed for the function evaluation.
vtkTuple.h
vtkExprTkFunctionParser::GetNumberOfScalarVariables
int GetNumberOfScalarVariables()
Get the number of scalar variables.
Definition: vtkExprTkFunctionParser.h:175
vtkExprTkFunctionParser::VectorVariableNeeded
std::vector< bool > VectorVariableNeeded
Definition: vtkExprTkFunctionParser.h:347
vtkExprTkFunctionParser::EvaluateMTime
vtkTimeStamp EvaluateMTime
Definition: vtkExprTkFunctionParser.h:352
vtkX3D::mode
@ mode
Definition: vtkX3D.h:253
vtkExprTkFunctionParser::GetMTime
vtkMTimeType GetMTime() override
Return parser's MTime.
vtkTuple< double, 3 >
vtkExprTkFunctionParser::GetVectorResult
double * GetVectorResult()
Get a vector result from evaluating the input function.
vtkExprTkFunctionParser::GetFunction
const char * GetFunction()
Set/Get input string to evaluate.
Definition: vtkExprTkFunctionParser.h:63
vtkExprTkFunctionParser::VectorReturningFunction
VectorReturningFunction
Enum that defines the vector returning functions that are not supported by ExprTk.
Definition: vtkExprTkFunctionParser.h:298
vtkExprTkFunctionParser::SetVectorVariableValue
void SetVectorVariableValue(const std::string &variableName, double values[3])
Set the value of a vector variable.
Definition: vtkExprTkFunctionParser.h:139
vtkExprTkFunctionParser::Result
vtkTuple< double, 3 > Result
Definition: vtkExprTkFunctionParser.h:360
vtkExprTkFunctionParser::Function
std::string Function
Definition: vtkExprTkFunctionParser.h:330
vtkTypeBool
int vtkTypeBool
Definition: vtkABI.h:69
vtkExprTkFunctionParser::Evaluate
bool Evaluate()
Evaluate the function, returning true on success, false on failure.
vtkMTimeType
vtkTypeUInt32 vtkMTimeType
Definition: vtkType.h:287