Minsky
operation.h
Go to the documentation of this file.
1 /*
2  @copyright Steve Keen 2012
3  @author Russell Standish
4  This file is part of Minsky.
5 
6  Minsky is free software: you can redistribute it and/or modify it
7  under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Minsky is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with Minsky. If not, see <http://www.gnu.org/licenses/>.
18 */
19 #ifndef OPERATION_H
20 #define OPERATION_H
21 
22 #include <ecolab.h>
23 #include <xml_pack_base.h>
24 #include <xml_unpack_base.h>
25 
26 // override EcoLab's default CLASSDESC_ACCESS macro
27 #include "classdesc_access.h"
28 
29 #include "item.h"
30 #include "slider.h"
31 
32 #include <vector>
33 #include <cairo/cairo.h>
34 
35 #include <arrays.h>
36 
37 #include "polyBase.h"
38 #include "polyPackBase.h"
39 #include <pack_base.h>
40 #include "operationBase.h"
41 #include "itemT.h"
42 
43 namespace minsky
44 {
45  template <minsky::OperationType::Type T>
46  class Operation: public ItemT<Operation<T>, OperationBase>,
47  public classdesc::PolyPack<Operation<T> >
48  {
50  public:
52  Type type() const override {return T;}
53  void iconDraw(cairo_t *) const override;
54  std::size_t numPorts() const override
55  {return OperationTypeInfo::numArguments<T>()+1;}
57  this->addPorts();
58  // custom arg defaults
59  switch (T) {
61  this->arg=-1;
62  break;
63  default:
64  break;
65  }
66  }
67  Operation(const Operation& x): Super(x) {this->addPorts();}
68  Operation(Operation&& x): Super(x) {this->addPorts();}
71  this->addPorts();
72  return *this;
73  }
76  this->addPorts();
77  return *this;
78  }
79  std::string classType() const override {return "Operation:"+OperationType::typeName(T);}
80  };
81 
82  class Time: public Operation<OperationType::time>
83  {
84  public:
85  Units units(bool) const override;
86  };
87 
88  class Derivative: public Operation<OperationType::differentiate>
89  {
90  public:
91  Units units(bool) const override;
92  };
93 
94  class Copy: public Operation<OperationType::copy>
95  {
96  public:
97  Units units(bool check) const override {return m_ports[1]->units(check);}
98  };
99 
101  class NamedOp: public ecolab::TCLAccessor<NamedOp,std::string>
102  {
103  protected:
104  std::string m_description;
105  virtual void updateBB()=0;
107  public:
108  NamedOp(): ecolab::TCLAccessor<NamedOp,std::string>
109  ("description",(ecolab::TCLAccessor<NamedOp,std::string>::Getter)&NamedOp::description,
110  (ecolab::TCLAccessor<NamedOp,std::string>::Setter)&NamedOp::description)
111  {}
113  virtual std::string description() const;
114  virtual std::string description(const std::string&);
116 
117  };
118 
120  struct DrawBinOp
121  {
122  cairo_t *cairo;
123  double zoomFactor;
124  DrawBinOp(cairo_t *cairo, double z=1): cairo(cairo), zoomFactor(z) {}
125 
126  void drawPlus() const
127  {
128  cairo_move_to(cairo,0,-5);
129  cairo_line_to(cairo,0,5);
130  cairo_move_to(cairo,-5,0);
131  cairo_line_to(cairo,5,0);
132  cairo_stroke(cairo);
133  }
134 
135  void drawMinus() const
136  {
137  cairo_move_to(cairo,-5,0);
138  cairo_line_to(cairo,5,0);
139  cairo_stroke(cairo);
140  }
141 
142  void drawMultiply() const
143  {
144  cairo_move_to(cairo,-5,-5);
145  cairo_line_to(cairo,5,5);
146  cairo_move_to(cairo,-5,5);
147  cairo_line_to(cairo,5,-5);
148  cairo_stroke(cairo);
149  }
150 
151  void drawDivide() const
152  {
153  cairo_move_to(cairo,-5,0);
154  cairo_line_to(cairo,5,0);
155  cairo_new_sub_path(cairo);
156  cairo_arc(cairo,0,3,1,0,2*M_PI);
157  cairo_new_sub_path(cairo);
158  cairo_arc(cairo,0,-3,1,0,2*M_PI);
159  cairo_stroke(cairo);
160  }
161 
162  void drawSymbol(const char* s) const
163  {
164  cairo_scale(cairo,zoomFactor,zoomFactor);
165  cairo_move_to(cairo,-5,0);
166  cairo_show_text(cairo,s);
167  }
168 
169  // puts a small symbol to identify port
170  // x, y = position of symbol
171  template <class F>
172  void drawPort(F f, float x, float y, float rotation) const
173  {
174  const ecolab::cairo::CairoSave cs(cairo);
175 
176  const double angle=rotation * M_PI / 180.0;
177  if (minsky::flipped(rotation))
178  y=-y;
179  cairo_rotate(cairo, angle);
180 
181  cairo_translate(cairo,0.7*x,0.6*y);
182  cairo_scale(cairo,0.5,0.5);
183 
184  // and counter-rotate
185  cairo_rotate(cairo, -angle);
186  f();
187  }
188  };
189 }
190 
191 #include "operation.cd"
192 #include "operation.xcd"
193 #endif
#define M_PI
some useful geometry types, defined from boost::geometry
Definition: geometry.h:29
function f
Definition: canvas.m:1
void drawDivide() const
Definition: operation.h:151
std::string classType() const override
Definition: operation.h:79
CLASSDESC_ACCESS(NamedOp)
Units units(bool) const override
compute the dimensional units
Definition: operation.cc:531
ItemT< Operation< T >, OperationBase > Super
Definition: operation.h:49
std::size_t numPorts() const override
Definition: operation.h:54
DrawBinOp(cairo_t *cairo, double z=1)
Definition: operation.h:124
Operation(const Operation &x)
Definition: operation.h:67
void drawPort(F f, float x, float y, float rotation) const
Definition: operation.h:172
double arg
operation argument. For example, the offset used in a difference operator, or binsize in a binning op...
Definition: operationBase.h:92
virtual float x() const
Definition: item.cc:107
Operation & operator=(Operation &&x)
Definition: operation.h:74
STL namespace.
Units units(bool) const override
compute the dimensional units
Definition: operation.cc:532
void drawMultiply() const
Definition: operation.h:142
base class for operations that have names
Definition: operation.h:101
bool flipped(double rotation)
returns if the angle (in degrees) is in the second or third quadrant
Definition: geometry.h:102
OperationType::Type Type
Definition: operation.h:51
Creation and access to the minskyTCL_obj object, which has code to record whenever Minsky&#39;s state cha...
Definition: constMap.h:22
void drawSymbol(const char *s) const
Definition: operation.h:162
Type type() const override
Definition: operation.h:52
virtual void updateBB()=0
represents the units (in sense of dimensional analysis) of a variable.
Definition: units.h:34
virtual std::string description() const
name of the associated data operation
Definition: operation.cc:573
static std::string typeName(int type)
return the symbolic name of type
ItemPortVector m_ports
Definition: item.h:156
Units units(bool check) const override
compute the dimensional units
Definition: operation.h:97
virtual void addPorts()
Definition: operation.cc:99
void drawPlus() const
Definition: operation.h:126
std::string m_description
Definition: operation.h:104
Operation & operator=(const Operation &x)
Definition: operation.h:69
void drawMinus() const
Definition: operation.h:135
helper class to draw port label symbols
Definition: operation.h:120
cairo_t * cairo
Definition: operation.h:122
ItemT & operator=(const ItemT &)=default
Operation(Operation &&x)
Definition: operation.h:68
void iconDraw(cairo_t *) const override
visual representation of operation on the canvas