SeExpr
GrapherExpr.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 #ifndef _GrapherExpr_
18 #define _GrapherExpr_
19 #include <SeExpression.h>
20 
22 struct SimpleVar : public SeExprScalarVarRef {
23  double val; // independent variable
24  void eval(const SeExprVarNode* /*node*/, SeVec3d& result) { result[0] = val; }
25 };
26 
28 class GrapherExpr : public SeExpression {
29  const std::map<std::string, SimpleVar>& vars;
30 
31  public:
33  GrapherExpr(const std::string& expr, const std::map<std::string, SimpleVar>& vars)
34  : SeExpression(expr), vars(vars) {}
35 
37  void setX(double x_input) { x.val = x_input; }
38 
39  private:
41  mutable SimpleVar x;
42 
44  SeExprVarRef* resolveVar(const std::string& name) const {
45  // check my internal variable
46  if (name == "x") return &x;
47  // check external variable table
48  std::map<std::string, SimpleVar>::const_iterator i = vars.find(name);
49  if (i != vars.end()) return const_cast<SimpleVar*>(&i->second);
50  // nothing found
51  return 0;
52  }
53 };
54 #endif
eval
virtual void eval(ArgHandle args)
Definition: ExprBuiltins.cpp:1028
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")
x
</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
Definition: tutorial.txt:108
resolveVar
</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