19#include <SeExpression.h>
22struct SimpleVar :
public SeExprScalarVarRef {
24 void eval(
const SeExprVarNode* , SeVec3d& result) { result[0] = val; }
28class GrapherExpr :
public SeExpression {
29 const std::map<std::string, SimpleVar>& vars;
33 GrapherExpr(
const std::string&
expr,
const std::map<std::string, SimpleVar>& vars)
34 : SeExpression(
expr), vars(vars) {}
37 void setX(
double x_input) {
x.val = x_input; }
44 SeExprVarRef*
resolveVar(
const std::string& name)
const {
46 if (name ==
"x")
return &
x;
48 std::map<std::string, SimpleVar>::const_iterator i = vars.find(name);
49 if (i != vars.end())
return const_cast<SimpleVar*
>(&i->second);
virtual void eval(ArgHandle args)
</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")
</pre > Once we have this we need an instance to store our variable and provide a reference to that We make it because resolveVar() is const . One does not need to store a variable reference in a given expression. In fact
</pre >< h3 > A simple variable reference</h3 > This is not a very interesting subclass of expression until we add some additional variables Variables on some applications may be very dynamic In this we only need x