Minsky: 3.17.0
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 // override EcoLab's default CLASSDESC_ACCESS macro
23 #include "classdesc_access.h"
24 
25 #include "item.h"
26 #include "slider.h"
27 
28 #include <vector>
29 #include <cairo/cairo.h>
30 
31 #include <arrays.h>
32 
33 #include "polyBase.h"
34 #include "polyPackBase.h"
35 #include <pack_base.h>
36 #include "operationBase.h"
37 #include "itemT.h"
38 
39 namespace minsky
40 {
41  template <minsky::OperationType::Type T>
42  class Operation: public ItemT<Operation<T>, OperationBase>,
43  public classdesc::PolyPack<Operation<T> >
44  {
46  public:
48  Type type() const override {return T;}
49  void iconDraw(cairo_t *) const override;
50  std::size_t numPorts() const override
51  {return OperationTypeInfo::numArguments<T>()+1;}
53  this->addPorts();
54  // custom arg defaults
55  switch (T) {
57  this->arg=-1;
58  break;
59  default:
60  break;
61  }
62  }
63  Operation(const Operation& x): Super(x) {this->addPorts();}
64  Operation(Operation&& x): Super(x) {this->addPorts();}
67  this->addPorts();
68  return *this;
69  }
72  this->addPorts();
73  return *this;
74  }
75  std::string classType() const override {return "Operation:"+OperationType::typeName(T);}
76  };
77 
78  class Time: public Operation<OperationType::time>
79  {
80  public:
81  Units units(bool) const override;
82  };
83 
84  class Derivative: public Operation<OperationType::differentiate>
85  {
86  public:
87  Units units(bool) const override;
88  };
89 
90  class Copy: public Operation<OperationType::copy>
91  {
92  public:
93  Units units(bool check) const override {return m_ports[1]->units(check);}
94  };
95 
97  class NamedOp
98  {
99  protected:
100  std::string m_description;
101  virtual void updateBB()=0;
103  public:
105  virtual std::string description() const;
106  virtual std::string description(const std::string&);
108 
109  };
110 
112  struct DrawBinOp
113  {
114  cairo_t *cairo;
115  double zoomFactor;
116  DrawBinOp(cairo_t *cairo, double z=1): cairo(cairo), zoomFactor(z) {}
117 
118  void drawPlus() const
119  {
120  cairo_move_to(cairo,0,-5);
121  cairo_line_to(cairo,0,5);
122  cairo_move_to(cairo,-5,0);
123  cairo_line_to(cairo,5,0);
124  cairo_stroke(cairo);
125  }
126 
127  void drawMinus() const
128  {
129  cairo_move_to(cairo,-5,0);
130  cairo_line_to(cairo,5,0);
131  cairo_stroke(cairo);
132  }
133 
134  void drawMultiply() const
135  {
136  cairo_move_to(cairo,-5,-5);
137  cairo_line_to(cairo,5,5);
138  cairo_move_to(cairo,-5,5);
139  cairo_line_to(cairo,5,-5);
140  cairo_stroke(cairo);
141  }
142 
143  void drawDivide() const
144  {
145  cairo_move_to(cairo,-5,0);
146  cairo_line_to(cairo,5,0);
147  cairo_new_sub_path(cairo);
148  cairo_arc(cairo,0,3,1,0,2*M_PI);
149  cairo_new_sub_path(cairo);
150  cairo_arc(cairo,0,-3,1,0,2*M_PI);
151  cairo_stroke(cairo);
152  }
153 
154  void drawSymbol(const char* s) const
155  {
156  cairo_scale(cairo,zoomFactor,zoomFactor);
157  cairo_move_to(cairo,-5,0);
158  cairo_show_text(cairo,s);
159  }
160 
161  // puts a small symbol to identify port
162  // x, y = position of symbol
163  template <class F>
164  void drawPort(F f, float x, float y, float rotation) const
165  {
166  const ecolab::cairo::CairoSave cs(cairo);
167 
168  const double angle=rotation * M_PI / 180.0;
169  if (minsky::flipped(rotation))
170  y=-y;
171  cairo_rotate(cairo, angle);
172 
173  cairo_translate(cairo,0.7*x,0.6*y);
174  cairo_scale(cairo,0.5,0.5);
175 
176  // and counter-rotate
177  cairo_rotate(cairo, -angle);
178  f();
179  }
180  };
181 }
182 
183 #include "operation.cd"
184 #include "operation.xcd"
185 #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:143
std::string classType() const override
Definition: operation.h:75
CLASSDESC_ACCESS(NamedOp)
Units units(bool) const override
compute the dimensional units
Definition: operation.cc:531
ItemT< Operation< T >, OperationBase > Super
Definition: operation.h:45
std::size_t numPorts() const override
Definition: operation.h:50
DrawBinOp(cairo_t *cairo, double z=1)
Definition: operation.h:116
Operation(const Operation &x)
Definition: operation.h:63
void drawPort(F f, float x, float y, float rotation) const
Definition: operation.h:164
double arg
operation argument. For example, the offset used in a difference operator, or binsize in a binning op...
Definition: operationBase.h:87
virtual float x() const
Definition: item.cc:107
Operation & operator=(Operation &&x)
Definition: operation.h:70
Units units(bool) const override
compute the dimensional units
Definition: operation.cc:532
void drawMultiply() const
Definition: operation.h:134
base class for operations that have names
Definition: operation.h:97
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:47
void drawSymbol(const char *s) const
Definition: operation.h:154
Type type() const override
Definition: operation.h:48
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:154
Units units(bool check) const override
compute the dimensional units
Definition: operation.h:93
virtual void addPorts()
Definition: operation.cc:99
void drawPlus() const
Definition: operation.h:118
std::string m_description
Definition: operation.h:100
Operation & operator=(const Operation &x)
Definition: operation.h:65
void drawMinus() const
Definition: operation.h:127
helper class to draw port label symbols
Definition: operation.h:112
cairo_t * cairo
Definition: operation.h:114
ItemT & operator=(const ItemT &)=default
Operation(Operation &&x)
Definition: operation.h:64
void iconDraw(cairo_t *) const override
visual representation of operation on the canvas