SeExpr
BasicExpression.h
Go to the documentation of this file.
1 /*
2 * Copyright Disney Enterprises, Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License
6 * and the following modification to it: Section 6 Trademarks.
7 * deleted and replaced with:
8 *
9 * 6. Trademarks. This License does not grant permission to use the
10 * trade names, trademarks, service marks, or product names of the
11 * Licensor and its affiliates, except as required for reproducing
12 * the content of the NOTICE file.
13 *
14 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * @file BasicExpression.h
18 * @brief A basic expression context for the expression previewer
19 * @author aselle
20 */
21 
22 #ifndef BasicExpression_h
23 #define BasicExpression_h
24 
25 #include <map>
26 #include <SeExpr2/Expression.h>
27 #include <SeExpr2/ExprFunc.h>
28 #include <SeExpr2/ExprNode.h>
29 
31  public:
32  struct ScalarRef : public SeExpr2::ExprVarRef {
33  double value;
34  ScalarRef() : SeExpr2::ExprVarRef(SeExpr2::ExprType().FP(1).Varying()), value(0.0) {}
35  void eval(double* result) { result[0] = value; }
36  void eval(const char** result) { assert(false); }
37  };
38 
39  struct VectorRef : public SeExpr2::ExprVarRef {
41  VectorRef() : SeExpr2::ExprVarRef(SeExpr2::ExprType().FP(3).Varying()), value(0.0) {}
42  void eval(double* result) {
43  for (int k = 0; k < 3; k++) result[k] = value[k];
44  };
45  void eval(const char** result) {
46  assert(false);
47  };
48  };
49 
52  virtual ~DummyFuncX() {}
53 
55  bool scalarWanted,
56  SeExpr2::ExprVarEnvBuilder& envBuilder) const {
57  bool valid = true;
58  int nargs = node->numChildren();
59  for (int i = 0; i < nargs; i++)
60  valid &= node->checkArg(i, SeExpr2::ExprType().FP(3).Constant(), envBuilder);
61  return valid ? SeExpr2::ExprType().FP(3).Varying() : SeExpr2::ExprType().Error();
62  }
63 
65  return new SeExpr2::ExprFuncNode::Data();
66  }
67 
68  virtual void eval(ArgHandle args) {
69  double* out = &args.outFp;
70  for (int i = 0; i < 3; i++) out[i] = 0.0;
71  }
72  } dummyFuncX;
74 
75  mutable ScalarRef u;
76  mutable ScalarRef v;
77  mutable VectorRef P;
78 
79  typedef std::map<std::string, VectorRef*> VARMAP;
80  mutable VARMAP varmap;
81  typedef std::map<std::string, bool> FUNCMAP;
82  mutable FUNCMAP funcmap;
83 
84  BasicExpression(const std::string& expr, const SeExpr2::ExprType& type = SeExpr2::ExprType().FP(3));
85  virtual ~BasicExpression();
86 
87  SeExpr2::ExprVarRef* resolveVar(const std::string& name) const;
88  SeExpr2::ExprFunc* resolveFunc(const std::string& name) const;
89  void setExpr(const std::string& str);
90  void clearVars();
91 };
92 
93 #endif
BasicExpression::VARMAP
std::map< std::string, VectorRef * > VARMAP
Definition: BasicExpression.h:79
BasicExpression::DummyFuncX::evalConstant
virtual SeExpr2::ExprFuncNode::Data * evalConstant(const SeExpr2::ExprFuncNode *node, ArgHandle args) const
Definition: BasicExpression.h:64
BasicExpression::FUNCMAP
std::map< std::string, bool > FUNCMAP
Definition: BasicExpression.h:81
BasicExpression::ScalarRef::ScalarRef
ScalarRef()
Definition: BasicExpression.h:34
SeExpr2::ExprType::FP
ExprType & FP(int d)
Mutate this into a floating point type of dimension d.
Definition: ExprType.h:90
BasicExpression::setExpr
void setExpr(const std::string &str)
Definition: BasicExpression.cpp:40
SeExpr2::ExprNode::numChildren
int numChildren() const
Number of children.
Definition: ExprNode.h:114
SeExpr2::Vec< double, 3, false >
BasicExpression::resolveFunc
SeExpr2::ExprFunc * resolveFunc(const std::string &name) const
Definition: BasicExpression.cpp:64
BasicExpression::DummyFuncX::eval
virtual void eval(ArgHandle args)
Definition: BasicExpression.h:68
BasicExpression::DummyFuncX::DummyFuncX
DummyFuncX()
Definition: BasicExpression.h:51
SeExpr2::ExprType::Varying
ExprType & Varying()
Mutate this into a varying lifetime.
Definition: ExprType.h:122
SeExpr2::ExprFuncSimple
Definition: ExprFuncX.h:72
SeExpr2::ExprFuncSimple::ArgHandle
Definition: ExprFuncX.h:76
BasicExpression::VectorRef
Definition: BasicExpression.h:39
SeExpr2::ExprType::Constant
ExprType & Constant()
Mutate this into a constant lifetime.
Definition: ExprType.h:112
ExprFunc.h
SeExpr2::ExprType
Definition: ExprType.h:39
SeExpr2::ExprFuncNode
Node that calls a function.
Definition: ExprNode.h:517
SeExpr2::ExprFunc
Function Definition, used in parse tree and func table.
Definition: ExprFunc.h:44
BasicExpression::VectorRef::value
SeExpr2::Vec3d value
Definition: BasicExpression.h:40
SeExpr2
Definition: Context.h:22
BasicExpression::ScalarRef::value
double value
Definition: BasicExpression.h:33
ExprNode.h
SeExpr2::ExprFuncSimple::ArgHandle::outFp
double & outFp
Definition: ExprFuncX.h:97
BasicExpression::dummyFunc
SeExpr2::ExprFunc dummyFunc
Definition: BasicExpression.h:73
BasicExpression::P
VectorRef P
Definition: BasicExpression.h:77
BasicExpression::varmap
VARMAP varmap
Definition: BasicExpression.h:80
SeExpr2::ExprType::Error
ExprType & Error()
Mutate this into an error type.
Definition: ExprType.h:102
BasicExpression::ScalarRef::eval
void eval(const char **result)
Definition: BasicExpression.h:36
BasicExpression::VectorRef::VectorRef
VectorRef()
Definition: BasicExpression.h:41
SeExpr2::ExprVarEnvBuilder
Variable scope builder is used by the type checking and code gen to track visiblity of variables and ...
Definition: ExprEnv.h:148
SeExpr2::ExprFuncNode::checkArg
bool checkArg(int argIndex, ExprType type, ExprVarEnvBuilder &envBuilder)
Definition: ExprNode.cpp:578
SeExpr2::ExprFuncSimple::ExprFuncSimple
ExprFuncSimple(const bool threadSafe)
Definition: ExprFuncX.h:74
SeExpr2::Expression
main expression class
Definition: Expression.h:76
BasicExpression::DummyFuncX::~DummyFuncX
virtual ~DummyFuncX()
Definition: BasicExpression.h:52
BasicExpression::~BasicExpression
virtual ~BasicExpression()
Definition: BasicExpression.cpp:27
BasicExpression
Definition: BasicExpression.h:30
BasicExpression::VectorRef::eval
void eval(const char **result)
Definition: BasicExpression.h:45
SeExpr2::ExprVarRef::ExprVarRef
ExprVarRef()
Definition: Expression.h:46
BasicExpression::dummyFuncX
BasicExpression::DummyFuncX dummyFuncX
BasicExpression::resolveVar
SeExpr2::ExprVarRef * resolveVar(const std::string &name) const
Definition: BasicExpression.cpp:45
BasicExpression::VectorRef::eval
void eval(double *result)
returns this variable's value by setting result
Definition: BasicExpression.h:42
BasicExpression::DummyFuncX::prep
virtual SeExpr2::ExprType prep(SeExpr2::ExprFuncNode *node, bool scalarWanted, SeExpr2::ExprVarEnvBuilder &envBuilder) const
Definition: BasicExpression.h:54
BasicExpression::v
ScalarRef v
Definition: BasicExpression.h:76
SeExpr2::ExprFuncNode::Data
base class for custom instance data
Definition: ExprNode.h:570
BasicExpression::clearVars
void clearVars()
Definition: BasicExpression.cpp:35
BasicExpression::ScalarRef::eval
void eval(double *result)
returns this variable's value by setting result
Definition: BasicExpression.h:35
BasicExpression::DummyFuncX
Definition: BasicExpression.h:50
BasicExpression::u
ScalarRef u
Definition: BasicExpression.h:75
BasicExpression::ScalarRef
Definition: BasicExpression.h:32
SeExpr2::ExprVarRef
abstract class for implementing variable references
Definition: Expression.h:45
BasicExpression::funcmap
FUNCMAP funcmap
Definition: BasicExpression.h:82
Expression.h
expr
</pre >< h3 > Binding our variable reference</h3 > If we now tried to use the variable would still not be found by our expressions To make it bindable we need to override the resolveVar() function as follows</pre >< h3 > Variable setting</h3 > Next we need to make a way of setting the variable As the controlling code will use the expression it will repeatedly alternate between setting the independent variables that are used and calling evaluate(). What it has to do depends very much on the application. In this case we only need to set the independent variable x as</pre >< h2 > Evaluating expressions</h2 > Evaluating an expression is pretty easy But before we can do that we need to make an instance< pre > GrapherExpr expr("x+x^2")
BasicExpression::BasicExpression
BasicExpression(const std::string &expr, const SeExpr2::ExprType &type=SeExpr2::ExprType().FP(3))
Definition: BasicExpression.cpp:24