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 {
24  double val; // independent variable
25  void eval(const SeExprVarNode* /*node*/,SeVec3d& result)
26  {result[0]=val;}
27 };
28 
29 
31 class GrapherExpr:public SeExpression
32 {
33  const std::map<std::string,SimpleVar>& vars;
34 public:
36  GrapherExpr(const std::string& expr,const std::map<std::string,SimpleVar>& vars)
37  :SeExpression(expr),vars(vars)
38  {}
39 
41  void setX(double x_input)
42  {x.val=x_input;}
43 
44 private:
46  mutable SimpleVar x;
47 
49  SeExprVarRef* resolveVar(const std::string& name) const
50  {
51  // check my internal variable
52  if(name == "x") return &x;
53  // check external variable table
54  std::map<std::string,SimpleVar>::const_iterator i=vars.find(name);
55  if(i!=vars.end()) return const_cast<SimpleVar*>(&i->second);
56  // nothing found
57  return 0;
58  }
59 };
60 #endif
</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")
virtual void eval(ArgHandle args)
</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
</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