VTK  9.1.0
vtkFunctionParser.h
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: vtkFunctionParser.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=========================================================================*/
55#ifndef vtkFunctionParser_h
56#define vtkFunctionParser_h
57
58#include "vtkCommonMiscModule.h" // For export macro
59#include "vtkObject.h"
60#include "vtkTuple.h" // needed for vtkTuple
61#include <string> // needed for string.
62#include <vector> // needed for vector
63
64#define VTK_PARSER_IMMEDIATE 1
65#define VTK_PARSER_UNARY_MINUS 2
66#define VTK_PARSER_UNARY_PLUS 3
67
68// supported math functions
69#define VTK_PARSER_ADD 4
70#define VTK_PARSER_SUBTRACT 5
71#define VTK_PARSER_MULTIPLY 6
72#define VTK_PARSER_DIVIDE 7
73#define VTK_PARSER_POWER 8
74#define VTK_PARSER_ABSOLUTE_VALUE 9
75#define VTK_PARSER_EXPONENT 10
76#define VTK_PARSER_CEILING 11
77#define VTK_PARSER_FLOOR 12
78#define VTK_PARSER_LOGARITHM 13
79#define VTK_PARSER_LOGARITHME 14
80#define VTK_PARSER_LOGARITHM10 15
81#define VTK_PARSER_SQUARE_ROOT 16
82#define VTK_PARSER_SINE 17
83#define VTK_PARSER_COSINE 18
84#define VTK_PARSER_TANGENT 19
85#define VTK_PARSER_ARCSINE 20
86#define VTK_PARSER_ARCCOSINE 21
87#define VTK_PARSER_ARCTANGENT 22
88#define VTK_PARSER_HYPERBOLIC_SINE 23
89#define VTK_PARSER_HYPERBOLIC_COSINE 24
90#define VTK_PARSER_HYPERBOLIC_TANGENT 25
91#define VTK_PARSER_MIN 26
92#define VTK_PARSER_MAX 27
93#define VTK_PARSER_SIGN 29
94
95// functions involving vectors
96#define VTK_PARSER_CROSS 28
97#define VTK_PARSER_VECTOR_UNARY_MINUS 30
98#define VTK_PARSER_VECTOR_UNARY_PLUS 31
99#define VTK_PARSER_DOT_PRODUCT 32
100#define VTK_PARSER_VECTOR_ADD 33
101#define VTK_PARSER_VECTOR_SUBTRACT 34
102#define VTK_PARSER_SCALAR_TIMES_VECTOR 35
103#define VTK_PARSER_VECTOR_TIMES_SCALAR 36
104#define VTK_PARSER_VECTOR_OVER_SCALAR 37
105#define VTK_PARSER_MAGNITUDE 38
106#define VTK_PARSER_NORMALIZE 39
107
108// constants involving vectors
109#define VTK_PARSER_IHAT 40
110#define VTK_PARSER_JHAT 41
111#define VTK_PARSER_KHAT 42
112
113// code for if(bool, trueval, falseval) resulting in a scalar
114#define VTK_PARSER_IF 43
115
116// code for if(bool, truevec, falsevec) resulting in a vector
117#define VTK_PARSER_VECTOR_IF 44
118
119// codes for boolean expressions
120#define VTK_PARSER_LESS_THAN 45
121
122// codes for boolean expressions
123#define VTK_PARSER_GREATER_THAN 46
124
125// codes for boolean expressions
126#define VTK_PARSER_EQUAL_TO 47
127
128// codes for boolean expressions
129#define VTK_PARSER_AND 48
130
131// codes for boolean expressions
132#define VTK_PARSER_OR 49
133
134// codes for scalar variables come before those for vectors. Do not define
135// values for VTK_PARSER_BEGIN_VARIABLES+1, VTK_PARSER_BEGIN_VARIABLES+2, ...,
136// because they are used to look up variables numbered 1, 2, ...
137#define VTK_PARSER_BEGIN_VARIABLES 50
138
139// the value that is returned as a result if there is an error
140#define VTK_PARSER_ERROR_RESULT VTK_FLOAT_MAX
141
142class VTKCOMMONMISC_EXPORT vtkFunctionParser : public vtkObject
143{
144public:
147 void PrintSelf(ostream& os, vtkIndent indent) override;
148
153
155
158 void SetFunction(const char* function);
159 vtkGetStringMacro(Function);
161
167
173
178
180
184 void GetVectorResult(double result[3])
185 {
186 double* r = this->GetVectorResult();
187 result[0] = r[0];
188 result[1] = r[1];
189 result[2] = r[2];
190 }
192
194
200 void SetScalarVariableValue(const char* variableName, double value);
201 void SetScalarVariableValue(const std::string& variableName, double value)
202 {
203 this->SetScalarVariableValue(variableName.c_str(), value);
204 }
205 void SetScalarVariableValue(int i, double value);
207
209
212 double GetScalarVariableValue(const char* variableName);
213 double GetScalarVariableValue(const std::string& variableName)
214 {
215 return this->GetScalarVariableValue(variableName.c_str());
216 }
219
221
228 const char* variableName, double xValue, double yValue, double zValue);
230 const std::string& variableName, double xValue, double yValue, double zValue)
231 {
232 this->SetVectorVariableValue(variableName.c_str(), xValue, yValue, zValue);
233 }
234 void SetVectorVariableValue(const char* variableName, const double values[3])
235 {
236 this->SetVectorVariableValue(variableName, values[0], values[1], values[2]);
237 }
238 void SetVectorVariableValue(const std::string& variableName, const double values[3])
239 {
240 this->SetVectorVariableValue(variableName.c_str(), values[0], values[1], values[2]);
241 }
242 void SetVectorVariableValue(int i, double xValue, double yValue, double zValue);
243 void SetVectorVariableValue(int i, const double values[3])
244 {
245 this->SetVectorVariableValue(i, values[0], values[1], values[2]);
246 }
248
250
253 double* GetVectorVariableValue(const char* variableName) VTK_SIZEHINT(3);
254 double* GetVectorVariableValue(const std::string& variableName) VTK_SIZEHINT(3)
255 {
256 return this->GetVectorVariableValue(variableName.c_str());
257 }
258 void GetVectorVariableValue(const char* variableName, double value[3])
259 {
260 double* r = this->GetVectorVariableValue(variableName);
261 value[0] = r[0];
262 value[1] = r[1];
263 value[2] = r[2];
264 }
265 void GetVectorVariableValue(const std::string& variableName, double value[3])
266 {
267 this->GetVectorVariableValue(variableName.c_str(), value);
268 }
270 void GetVectorVariableValue(int i, double value[3])
271 {
272 double* r = this->GetVectorVariableValue(i);
273 value[0] = r[0];
274 value[1] = r[1];
275 value[2] = r[2];
276 }
278
282 int GetNumberOfScalarVariables() { return static_cast<int>(this->ScalarVariableNames.size()); }
283
287 int GetScalarVariableIndex(const char* name);
289 {
290 return this->GetScalarVariableIndex(name.c_str());
291 }
292
296 int GetNumberOfVectorVariables() { return static_cast<int>(this->VectorVariableNames.size()); }
297
301 int GetVectorVariableIndex(const char* name);
303 {
304 return this->GetVectorVariableIndex(name.c_str());
305 }
306
310 const char* GetScalarVariableName(int i);
311
315 const char* GetVectorVariableName(int i);
316
318
324 bool GetScalarVariableNeeded(const char* variableName);
325 bool GetScalarVariableNeeded(const std::string& variableName)
326 {
327 return GetScalarVariableNeeded(variableName.c_str());
328 }
330
332
338 bool GetVectorVariableNeeded(const char* variableName);
339 bool GetVectorVariableNeeded(const std::string& variableName)
340 {
341 return this->GetVectorVariableNeeded(variableName.c_str());
342 }
344
349
354
359
361
367 vtkSetMacro(ReplaceInvalidValues, vtkTypeBool);
368 vtkGetMacro(ReplaceInvalidValues, vtkTypeBool);
369 vtkBooleanMacro(ReplaceInvalidValues, vtkTypeBool);
370 vtkSetMacro(ReplacementValue, double);
371 vtkGetMacro(ReplacementValue, double);
373
377 void CheckExpression(int& pos, char** error);
378
383
384protected:
387
388 int Parse();
389
393 bool Evaluate();
394
396
397 void CopyParseError(int& position, char** error);
398
400 char* RemoveSpacesFrom(const char* variableName);
402
404 void BuildInternalSubstringStructure(int beginIndex, int endIndex);
405 void AddInternalByte(unsigned int newByte);
406
407 int IsSubstringCompletelyEnclosed(int beginIndex, int endIndex);
408 int FindEndOfMathFunction(int beginIndex);
409 int FindEndOfMathConstant(int beginIndex);
410
411 int IsVariableName(int currentIndex);
413
414 int GetMathFunctionNumber(int currentIndex);
416 int GetMathFunctionStringLength(int mathFunctionNumber);
417 int GetMathConstantNumber(int currentIndex);
418 int GetMathConstantStringLength(int mathConstantNumber);
419 unsigned char GetElementaryOperatorNumber(char op);
420 unsigned int GetOperandNumber(int currentIndex);
421 int GetVariableNameLength(int variableNumber);
422
424
430
431 vtkSetStringMacro(ParseError);
432
434
435 char* Function;
437
439 std::vector<std::string> ScalarVariableNames;
440 std::vector<std::string> VectorVariableNames;
441 std::vector<double> ScalarVariableValues;
442 std::vector<vtkTuple<double, 3>> VectorVariableValues;
443 std::vector<bool> ScalarVariableNeeded;
444 std::vector<bool> VectorVariableNeeded;
445
446 std::vector<unsigned int> ByteCode;
448 double* Immediates;
450 double* Stack;
453
459
462
465
466private:
467 vtkFunctionParser(const vtkFunctionParser&) = delete;
468 void operator=(const vtkFunctionParser&) = delete;
469};
470
471#endif
Parse and evaluate a mathematical expression.
void SetScalarVariableValue(int i, double value)
Set the value of a scalar variable.
int BuildInternalFunctionStructure()
double GetScalarVariableValue(const std::string &variableName)
Get the value of a scalar variable.
int DisambiguateOperators()
std::vector< std::string > VectorVariableNames
~vtkFunctionParser() override
bool GetScalarVariableNeeded(const std::string &variableName)
Returns whether a scalar variable is needed for the function evaluation.
void SetVectorVariableValue(int i, const double values[3])
Set the value of a vector variable.
vtkTimeStamp EvaluateMTime
void SetFunction(const char *function)
Set/Get input string to evaluate.
double * GetVectorVariableValue(int i)
Get the value of a vector variable.
vtkTimeStamp FunctionMTime
int IsScalarResult()
Check whether the result is a scalar result.
double * GetVectorResult()
Get a vector result from evaluating the input function.
static vtkFunctionParser * New()
double * GetVectorVariableValue(const std::string &variableName)
Get the value of a vector variable.
int GetScalarVariableIndex(const char *name)
Get scalar variable index or -1 if not found.
int GetNumberOfVectorVariables()
Get the number of vector variables.
void RemoveVectorVariables()
Remove all the vector variables.
void GetVectorVariableValue(const char *variableName, double value[3])
Get the value of a vector variable.
double GetScalarResult()
Get a scalar result from evaluating the input function.
int GetMathFunctionStringLength(int mathFunctionNumber)
char * RemoveSpacesFrom(const char *variableName)
int GetNumberOfScalarVariables()
Get the number of scalar variables.
int GetMathConstantNumber(int currentIndex)
int GetVectorVariableIndex(const char *name)
Get scalar variable index or -1 if not found.
void SetVectorVariableValue(const char *variableName, const double values[3])
Set the value of a vector variable.
int GetMathFunctionNumber(int currentIndex)
int IsElementaryOperator(int op)
int FindPositionInOriginalFunction(const int &pos)
const char * GetVectorVariableName(int i)
Get the ith vector variable name.
unsigned int GetOperandNumber(int currentIndex)
void AddInternalByte(unsigned int newByte)
void SetVectorVariableValue(const std::string &variableName, const double values[3])
Set the value of a vector variable.
vtkMTimeType GetMTime() override
Return parser's MTime.
void UpdateNeededVariables()
Collects meta-data about which variables are needed by the current function.
bool GetVectorVariableNeeded(const std::string &variableName)
Returns whether a vector variable is needed for the function evaluation.
void GetVectorVariableValue(int i, double value[3])
Get 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.
void SetVectorVariableValue(int i, double xValue, double yValue, double zValue)
Set the value of a vector variable.
std::vector< bool > ScalarVariableNeeded
bool Evaluate()
Evaluate the function, returning true on success, false on failure.
const char * GetScalarVariableName(int i)
Get the ith scalar variable name.
std::vector< unsigned int > ByteCode
vtkTimeStamp CheckMTime
int GetMathFunctionNumberByCheckingParenthesis(int currentIndex)
double GetScalarVariableValue(const char *variableName)
Get the value of a scalar variable.
int IsVariableName(int currentIndex)
void CheckExpression(int &pos, char **error)
Check the validity of the function expression.
int FindEndOfMathConstant(int beginIndex)
double GetScalarVariableValue(int i)
Get the value of a scalar variable.
void SetVectorVariableValue(const std::string &variableName, double xValue, double yValue, double zValue)
Set the value of a vector variable.
std::vector< double > ScalarVariableValues
vtkTimeStamp ParseMTime
bool GetScalarVariableNeeded(const char *variableName)
Returns whether a scalar variable is needed for the function evaluation.
bool GetScalarVariableNeeded(int i)
Returns whether a scalar variable is needed for the function evaluation.
unsigned char GetElementaryOperatorNumber(char op)
int GetVectorVariableIndex(const std::string &name)
int GetVariableNameLength(int variableNumber)
double * GetVectorVariableValue(const char *variableName)
Get the value of a vector variable.
std::vector< vtkTuple< double, 3 > > VectorVariableValues
void GetVectorVariableValue(const std::string &variableName, double value[3])
Get the value of a vector variable.
vtkTypeBool ReplaceInvalidValues
int IsVectorResult()
Check whether the result is a vector result.
void RemoveScalarVariables()
Remove all the scalar variables.
int IsSubstringCompletelyEnclosed(int beginIndex, int endIndex)
bool GetVectorVariableNeeded(int i)
Returns whether a vector variable is needed for the function evaluation.
void BuildInternalSubstringStructure(int beginIndex, int endIndex)
vtkTimeStamp VariableMTime
void RemoveAllVariables()
Remove all the current variables.
void CopyParseError(int &position, char **error)
void InvalidateFunction()
Allow the user to force the function to be re-parsed.
void SetScalarVariableValue(const std::string &variableName, double value)
Set the value of a scalar variable.
int OperatorWithinVariable(int idx)
bool GetVectorVariableNeeded(const char *variableName)
Returns whether a vector variable is needed for the function evaluation.
int GetMathConstantStringLength(int mathConstantNumber)
std::vector< bool > VectorVariableNeeded
std::vector< std::string > ScalarVariableNames
int GetScalarVariableIndex(const std::string &name)
void SetVectorVariableValue(const char *variableName, double xValue, double yValue, double zValue)
Set the value of a vector variable.
void SetScalarVariableValue(const char *variableName, double value)
Set the value of a scalar variable.
int FindEndOfMathFunction(int beginIndex)
a simple class to control print indentation
Definition: vtkIndent.h:43
abstract base class for most VTK objects
Definition: vtkObject.h:63
record modification and/or execution time
Definition: vtkTimeStamp.h:42
@ function
Definition: vtkX3D.h:255
@ value
Definition: vtkX3D.h:226
@ name
Definition: vtkX3D.h:225
@ position
Definition: vtkX3D.h:267
@ string
Definition: vtkX3D.h:496
int vtkTypeBool
Definition: vtkABI.h:69
vtkTypeUInt32 vtkMTimeType
Definition: vtkType.h:287
#define VTK_SIZEHINT(...)