1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkExprTkFunctionParser.h
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
/**
* @class vtkExprTkFunctionParser
* @brief Parse and evaluate a mathematical expression
*
* vtkExprTkFunctionParser is a wrapper class of the ExprTK library that takes
* in a mathematical expression as a char string, parses it, and evaluates it
* at the specified values of the variables in the input string.
*
* The detailed documentation of the supported functionality is described in
* https://github.com/ArashPartow/exprtk. In addition to the documented
* functionality, the following vector operations have been implemented:
* 1) cross(v1, v2), cross product of two vectors,
* 2) mag(v), magnitude of a vector,
* 3) norm(v), the normalized version of a vector.
*
* @par Thanks:
* Arash Partow for implementing the ExprTk library.
*/
#ifndef vtkExprTkFunctionParser_h
#define vtkExprTkFunctionParser_h
#include "vtkCommonMiscModule.h" // For export macro
#include "vtkObject.h"
#include "vtkTuple.h" // needed for vtkTuple
#include <string> // needed for string.
#include <vector> // needed for vector
// forward declarations for ExprTk tools
VTK_ABI_NAMESPACE_BEGIN
struct vtkExprTkTools;
class VTKCOMMONMISC_EXPORT vtkExprTkFunctionParser : public vtkObject
{
public:
static vtkExprTkFunctionParser* New();
vtkTypeMacro(vtkExprTkFunctionParser, vtkObject);
void PrintSelf(ostream& os, vtkIndent indent) override;
/**
* Return parser's MTime
*/
vtkMTimeType GetMTime() override;
///@{
/**
* Set/Get input string to evaluate.
*/
void SetFunction(const char* function);
const char* GetFunction() { return this->Function.c_str(); }
///@}
/**
* Check whether the result is a scalar result. If it isn't, then
* either the result is a vector or an error has occurred.
*/
int IsScalarResult();
/**
* Check whether the result is a vector result. If it isn't, then
* either the result is scalar or an error has occurred.
*/
int IsVectorResult();
/**
* Get a scalar result from evaluating the input function.
*
* If ReplaceInvalidValues is not set, then the error value
* that will be return is NaN.
*/
double GetScalarResult();
///@{
/**
* Get a vector result from evaluating the input function.
*
* If ReplaceInvalidValues is not set, then the error value
* that will be return is [NaN, NaN, NaN].
*/
double* GetVectorResult() VTK_SIZEHINT(3);
void GetVectorResult(double result[3])
{
double* r = this->GetVectorResult();
result[0] = r[0];
result[1] = r[1];
result[2] = r[2];
}
///@}
///@{
/**
* Set the value of a scalar variable. If a variable with this name
* exists, then its value will be set to the new value. If there is not
* already a variable with this name, variableName will be added to the
* list of variables, and its value will be set to the new value. If the
* variable name is not sanitized, it should be provided in quotes, and
* a valid unique string will be used as a replacement by the parser.
*
* @note A sanitized variable name is accepted by the following regex: ^[a-zA-Z][a-zA-Z_0-9]*.
*/
void SetScalarVariableValue(const std::string& variableName, double value);
void SetScalarVariableValue(int i, double value);
///@}
///@{
/**
* Get the value of a scalar variable.
*/
double GetScalarVariableValue(const std::string& variableName);
double GetScalarVariableValue(int i);
///@}
///@{
/**
* Set the value of a vector variable. If a variable with this name
* exists, then its value will be set to the new value. If there is not
* already a variable with this name, variableName will be added to the
* list of variables, and its value will be set to the new value. If the
* variable name is not sanitized, it should be provided in quotes, and
* a valid unique string will be used as a replacement by the parser.
*
* @note A sanitized variable name is accepted by the following regex: ^[a-zA-Z][a-zA-Z_0-9]*.
*/
void SetVectorVariableValue(
const std::string& variableName, double xValue, double yValue, double zValue);
void SetVectorVariableValue(const std::string& variableName, double values[3])
{
this->SetVectorVariableValue(variableName, values[0], values[1], values[2]);
}
void SetVectorVariableValue(int i, double xValue, double yValue, double zValue);
void SetVectorVariableValue(int i, double values[3])
{
this->SetVectorVariableValue(i, values[0], values[1], values[2]);
}
///@}
///@{
/**
* Get the value of a vector variable.
*/
double* GetVectorVariableValue(const std::string& variableName) VTK_SIZEHINT(3);
void GetVectorVariableValue(const std::string& variableName, double value[3])
{
double* r = this->GetVectorVariableValue(variableName);
value[0] = r[0];
value[1] = r[1];
value[2] = r[2];
}
double* GetVectorVariableValue(int i) VTK_SIZEHINT(3);
void GetVectorVariableValue(int i, double value[3])
{
double* r = this->GetVectorVariableValue(i);
value[0] = r[0];
value[1] = r[1];
value[2] = r[2];
}
///@}
/**
* Get the number of scalar variables.
*/
int GetNumberOfScalarVariables()
{
return static_cast<int>(this->UsedScalarVariableNames.size());
}
/**
* Get scalar variable index or -1 if not found
*/
int GetScalarVariableIndex(const std::string& name);
/**
* Get the number of vector variables.
*/
int GetNumberOfVectorVariables()
{
return static_cast<int>(this->UsedVectorVariableNames.size());
}
/**
* Get scalar variable index or -1 if not found
*/
int GetVectorVariableIndex(const std::string& name);
/**
* Get the ith scalar variable name.
*/
std::string GetScalarVariableName(int i);
/**
* Get the ith vector variable name.
*/
std::string GetVectorVariableName(int i);
///@{
/**
* Returns whether a scalar variable is needed for the function evaluation.
* This is only valid after a successful Parse(). Thus, call GetScalarResult()
* or IsScalarResult() or similar method before calling this.
*/
bool GetScalarVariableNeeded(int i);
bool GetScalarVariableNeeded(const std::string& variableName);
///@}
///@{
/**
* Returns whether a vector variable is needed for the function evaluation.
* This is only valid after a successful Parse(). Thus, call GetVectorResult()
* or IsVectorResult() or similar method before calling this.
*/
bool GetVectorVariableNeeded(int i);
bool GetVectorVariableNeeded(const std::string& variableName);
///@}
/**
* Remove all the current variables.
*/
void RemoveAllVariables();
/**
* Remove all the scalar variables.
*/
void RemoveScalarVariables();
/**
* Remove all the vector variables.
*/
void RemoveVectorVariables();
///@{
/**
* When ReplaceInvalidValues is on, all invalid values (such as
* sqrt(-2), note that function parser does not handle complex
* numbers) will be replaced by ReplacementValue. Otherwise an
* error will be reported
*/
vtkSetMacro(ReplaceInvalidValues, vtkTypeBool);
vtkGetMacro(ReplaceInvalidValues, vtkTypeBool);
vtkBooleanMacro(ReplaceInvalidValues, vtkTypeBool);
vtkSetMacro(ReplacementValue, double);
vtkGetMacro(ReplacementValue, double);
///@}
/**
* Allow the user to force the function to be re-parsed
*/
void InvalidateFunction();
/**
* Sanitize a label/name to remove spaces, delimiters etc.
* Once the label/name is sanitized is can be accepted by the
* following regex: ^[a-zA-Z][a-zA-Z_0-9]*.
*
* @note taken from vtkSMCoreUtilities
*/
static std::string SanitizeName(const char* name);
protected:
vtkExprTkFunctionParser();
~vtkExprTkFunctionParser() override;
/**
* The first mode parses the function and uses a return statement
* to identify the ReturnType. The second mode parses the function and uses a result
* vector to store the results of the function knowing its return type. The second mode
* is used because it's a lot faster than the first.
*/
enum ParseMode
{
DetectReturnType,
SaveResultInVariable
};
/**
* Parses the given function, returning true on success, false on failure.
* It has 2 modes (see ParseMode). Both modes need to be utilized to extract
* results of a function.
*/
int Parse(ParseMode mode);
/**
* Enum that defines the vector returning functions that are not supported by ExprTk.
*/
enum VectorReturningFunction
{
Cross,
Norm
};
/**
* ExprTk does not support functions which return a vector.
*
* All the cross(v1,v2) occurrences will be replaced with
* (iHat*crossX(v1,v2)+jHat*crossY(v1,v2)+kHat*crossZ(v1,v2)).
*
* All the norm(v) occurrences will be replaced with ((v)/mag(v)).
*/
std::string FixVectorReturningFunctionOccurrences(
VectorReturningFunction vectorReturningFunction);
/**
* Check possible usage of old format of dot product, e.g. v1.v2
*/
bool CheckOldFormatOfDotProductUsage();
/**
* Evaluate the function, returning true on success, false on failure.
*/
bool Evaluate();
/**
* Collects meta-data about which variables are needed by the current
* function. This is called only after a successful call to this->Parse().
*/
void UpdateNeededVariables();
std::string Function;
std::string FunctionWithUsedVariableNames;
std::string ExpressionString;
// original and used variables names are the same, except if the original
// ones are not valid.
std::vector<std::string> OriginalScalarVariableNames;
std::vector<std::string> UsedScalarVariableNames;
std::vector<std::string> OriginalVectorVariableNames;
std::vector<std::string> UsedVectorVariableNames;
// pointers for scalar and vector variables are used to enforce
// that their memory address will not change due to a possible
// resizing of their container (std::vector), ExprTk requires the
// memory address of the given variable to remain the same.
std::vector<double*> ScalarVariableValues;
std::vector<vtkTuple<double, 3>*> VectorVariableValues;
std::vector<bool> ScalarVariableNeeded;
std::vector<bool> VectorVariableNeeded;
vtkTimeStamp FunctionMTime;
vtkTimeStamp ParseMTime;
vtkTypeBool ReplaceInvalidValues;
double ReplacementValue;
vtkExprTkTools* ExprTkTools;
int ResultType;
vtkTuple<double, 3> Result;
private:
vtkExprTkFunctionParser(const vtkExprTkFunctionParser&) = delete;
void operator=(const vtkExprTkFunctionParser&) = delete;
};
VTK_ABI_NAMESPACE_END
#endif
|