ESP  0.1
The Example-based Sensor Predictions (ESP) system tries to bring machine learning to the maker community.
plotter.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "ofMain.h"
4 #include "ofxGrt.h"
5 
6 using std::string;
7 
9  public:
12  uint32_t start;
13  uint32_t end;
14  void* data;
15  };
16 
19  uint32_t index;
20  void* data;
21  };
22 
25  void* data;
26  };
27 
29  : x_click_(0), x_release_(0), x_start_(0), x_end_(0),
30  is_tracking_mouse_(false) {
31  }
32 
33  typedef std::function<void(RangeSelectedCallbackArgs)>
35  typedef std::function<void(ValueHighlightedCallbackArgs)>
37 
39  void* data = nullptr) {
42  ofAddListener(ofEvents().mousePressed, this,
44  ofAddListener(ofEvents().mouseDragged, this,
46  ofAddListener(ofEvents().mouseReleased, this,
48  }
49 
50  template <typename T1, typename arg, class T>
51  void onRangeSelected(T1* owner, void (T::*listenerMethod)(arg),
52  void* data = nullptr) {
53  using namespace std::placeholders;
54  onRangeSelected(std::bind(listenerMethod, owner, _1), data);
55  }
56 
58  void* data = nullptr) {
61  ofAddListener(ofEvents().mouseMoved, this,
63  }
64 
65  template <typename T1, typename arg, class T>
66  void onValueHighlighted(T1* owner, void (T::*listenerMethod)(arg),
67  void* data = nullptr) {
68  using namespace std::placeholders;
69  onValueHighlighted(std::bind(listenerMethod, owner, _1), data);
70  }
71 
72  virtual MatrixDouble getData(uint32_t x_start_idx, uint32_t x_end_idx) = 0;
73  virtual vector<double> getData(uint32_t x_idx) = 0;
74 
75  std::pair<uint32_t, uint32_t> getSelection() {
76  return std::make_pair(mouseCoordinateToIndex(x_start_),
78  }
79 
80  void clearSelection() {
81  x_start_ = 0;
82  x_end_ = 0;
83  }
84 
85  protected:
86  virtual uint32_t mouseCoordinateToIndex(uint32_t x) = 0;
87 
88  bool contains(uint32_t x, uint32_t y) {
89  return (x_ <= x && x <= x_ + w_ && y_ <= y && y <= y_ + h_) ? true
90  : false;
91  }
92 
93  void mouseMoved(ofMouseEventArgs& arg) {
94  // Only tracks if point is inside
95  if (contains(arg.x, arg.y)) {
96  x_move_ = arg.x - x_;
97  if (value_highlighted_callback_ != nullptr) {
99  args.source = this;
103  }
104  }
105  }
106 
107  void startSelection(ofMouseEventArgs& arg) {
108  // Only tracks if point is inside
109  if (contains(arg.x, arg.y)) {
110  x_click_ = arg.x - x_;
111  is_tracking_mouse_ = true;
112  }
113  }
114 
115  void duringSelection(ofMouseEventArgs& arg) {
116  if (is_tracking_mouse_) {
117  if (contains(arg.x, arg.y)) {
118  x_release_ = arg.x - x_;
119  normalize();
120  }
121  }
122  }
123 
124  void normalize() {
125  pair<uint32_t, uint32_t> sel = std::minmax(x_click_, x_release_);
126  x_start_ = sel.first;
127  x_end_ = sel.second;
128  }
129 
130  void endSelection(ofMouseEventArgs& arg) {
131  if (is_tracking_mouse_) {
132  if (contains(arg.x, arg.y)) {
133  x_release_ = arg.x - x_;
134  normalize();
135  }
136 
137  if (range_selected_callback_ != nullptr) {
139  args.source = this;
144  }
145  }
146  is_tracking_mouse_ = false;
147  }
148 
149  // These need to be saved by the sub-class's draw() function so that the
150  // plot knows where it is (and can convert mouse coordinates to plot
151  // coordinates).
152  uint32_t x_;
153  uint32_t y_;
154  uint32_t w_;
155  uint32_t h_;
156 
157  uint32_t x_move_;
158 
159  // x_click_ and x_release_ keeps track of where the mouse is clicked and
160  // releasesd. Their values are normalized by subtracting x_.
161  uint32_t x_click_;
162  uint32_t x_release_;
163 
164  // x_start_ and x_end_ are values that are reported to users of this class
165  // for selections. When selection happens, x_start_ is always smaller than
166  // x_end_.
167  uint32_t x_start_;
168  uint32_t x_end_;
169 
170  // To avoid accidental tracking (such as out of range or when no data).
172 
175 
178 };
179 
180 // The Plotter class plots fixed length samples and allows for interaction
181 // using the InteractivePlot API.
182 class Plotter : public InteractivePlot {
183  public:
184  Plotter();
185 
186  bool setup(uint32_t num_dimensions, string title, string subtitle = "");
187  bool setData(const GRT::MatrixDouble& data);
188 
189  bool clearContentModifiedFlag();
190 
191  bool push_back(const vector<double>& data_point);
192 
193  virtual GRT::MatrixDouble& getData() {
194  return data_;
195  }
196 
197  virtual MatrixDouble getData(uint32_t x_start_idx, uint32_t x_end_idx) {
198  // dataBuffer is a "CircularBuffer< vector<float> >" inside
199  // ofxGrtTimeseriesPlot.
200  MatrixDouble selected_data;
201  for (uint32_t i = x_start_idx; i < x_end_idx && i < data_.getSize();
202  i++) {
203  vector<double> v_double(data_.getRowVector(i).begin(),
204  data_.getRowVector(i).end());
205  selected_data.push_back(v_double);
206  }
207  return selected_data;
208  }
209 
210  virtual vector<double> getData(uint32_t x_idx) {
211  return vector<double>(data_.getRowVector(x_idx).begin(),
212  data_.getRowVector(x_idx).end());
213  }
214 
215  bool setRanges(float minY, float maxY, bool lockRanges = false);
216  std::pair<float, float> getRanges();
217 
218  bool setColorPalette(const vector<ofColor>& colors);
219 
220  bool setTitle(const string& title);
221  void renameTitleStart();
222  void renameTitleDone();
223 
224  const string& getTitle() const;
225 
226  bool draw(uint32_t x, uint32_t y, uint32_t w, uint32_t h);
227 
228  bool reset();
229 
230  bool clearData();
231 
232  void setBackgroundColor(ofColor color) {
233  background_color_ = color;
234  }
235 
236  void setTextColor(ofColor color) {
237  text_color_ = color;
238  }
239 
240  void setGridColor(ofColor color) {
241  grid_color_ = color;
242  }
243 
244  protected:
245  virtual uint32_t mouseCoordinateToIndex(uint32_t x) {
246  float x_step = w_ * 1.0 / data_.getNumRows();
247  return std::min(std::max((uint32_t) 0, (uint32_t)(x / x_step)),
248  data_.getNumRows() - 1);
249  }
250 
251  private:
252  bool initialized_;
253  bool is_content_modified_;
254  bool is_in_renaming_;
255  uint32_t num_dimensions_;
256  vector<ofColor> colors_;
257  string title_;
258  string subtitle_;
259  bool lock_ranges_;
260  float minY_, default_minY_;
261  float maxY_, default_maxY_;
262  GRT::MatrixDouble data_;
263 
264  ofColor background_color_ = ofColor(0, 0, 0);
265  ofColor text_color_ = ofColor(0xFF, 0xFF, 0xFF);
266  ofColor grid_color_ = ofColor(0xFF, 0xFF, 0xFF);
267 };
268 
269 class InteractiveTimeSeriesPlot : public ofxGrtTimeseriesPlot,
270  public InteractivePlot {
271  public:
272  bool draw(uint32_t x, uint32_t y, uint32_t w, uint32_t h) {
273  x_ = x;
274  y_ = y;
275  w_ = w;
276  h_ = h;
277  ofxGrtTimeseriesPlot::draw(x, y, w, h);
278 
279  // Draw the selection.
280  if (x_start_ > 0 && x_end_ > x_start_) {
281  ofEnableAlphaBlending();
282  // Draw border
283  ofSetColor(0xFF, 0xFF, 0xFF, 0x7F);
284  ofDrawRectangle(x_ + x_start_, y_, x_end_ - x_start_, h);
285 
286  // Fill the rectangle
287  ofFill();
288  ofSetColor(0xFF, 0xFF, 0xFF, 0x2F);
289  ofDrawRectangle(x_ + x_start_, y_, x_end_ - x_start_, h);
290  }
291  return true;
292  }
293 
294  virtual MatrixDouble getData(uint32_t x_start_idx, uint32_t x_end_idx) {
295  // dataBuffer is a "CircularBuffer< vector<float> >" inside
296  // ofxGrtTimeseriesPlot.
297  MatrixDouble selected_data;
298  for (uint32_t i = x_start_idx;
299  i < x_end_idx && i < dataBuffer.getSize(); i++) {
300  vector<double> v_double(dataBuffer[i].begin(), dataBuffer[i].end());
301  selected_data.push_back(v_double);
302  }
303  return selected_data;
304  }
305 
306  virtual vector<double> getData(uint32_t x_idx) {
307  return vector<double>(dataBuffer[x_idx].begin(),
308  dataBuffer[x_idx].end());
309  }
310 
311  protected:
312  virtual uint32_t mouseCoordinateToIndex(uint32_t x) {
313  float x_step = w_ * 1.0 / timeseriesLength;
314  return std::min(std::max((uint32_t) 0, (uint32_t)(x / x_step)),
315  timeseriesLength - 1);
316  }
317 };
virtual uint32_t mouseCoordinateToIndex(uint32_t x)
Definition: plotter.h:312
onValueHighlightedCallback value_highlighted_callback_
Definition: plotter.h:176
void title(const std::string &titlestr)
Definition: matplotlibcpp.h:292
void duringSelection(ofMouseEventArgs &arg)
Definition: plotter.h:115
void * data
Definition: plotter.h:14
void startSelection(ofMouseEventArgs &arg)
Definition: plotter.h:107
Definition: plotter.h:182
uint32_t x_start_
Definition: plotter.h:167
InteractivePlot * source
Definition: plotter.h:24
uint32_t x_end_
Definition: plotter.h:168
uint32_t y_
Definition: plotter.h:153
void onRangeSelected(T1 *owner, void(T::*listenerMethod)(arg), void *data=nullptr)
Definition: plotter.h:51
virtual uint32_t mouseCoordinateToIndex(uint32_t x)
Definition: plotter.h:245
uint32_t w_
Definition: plotter.h:154
bool is_tracking_mouse_
Definition: plotter.h:171
void * value_highlighted_callback_data_
Definition: plotter.h:177
uint32_t start
Definition: plotter.h:12
void clearSelection()
Definition: plotter.h:80
void onValueHighlighted(T1 *owner, void(T::*listenerMethod)(arg), void *data=nullptr)
Definition: plotter.h:66
uint32_t h_
Definition: plotter.h:155
void * data
Definition: plotter.h:20
virtual uint32_t mouseCoordinateToIndex(uint32_t x)=0
virtual MatrixDouble getData(uint32_t x_start_idx, uint32_t x_end_idx)
Definition: plotter.h:294
void mouseMoved(ofMouseEventArgs &arg)
Definition: plotter.h:93
std::pair< uint32_t, uint32_t > getSelection()
Definition: plotter.h:75
void setTextColor(ofColor color)
Definition: plotter.h:236
uint32_t x_click_
Definition: plotter.h:161
Definition: plotter.h:269
std::function< void(RangeSelectedCallbackArgs)> onRangeSelectedCallback
Definition: plotter.h:34
Definition: plotter.h:8
InteractivePlot * source
Definition: plotter.h:18
bool contains(uint32_t x, uint32_t y)
Definition: plotter.h:88
void onValueHighlighted(const onValueHighlightedCallback &cb, void *data=nullptr)
Definition: plotter.h:57
void endSelection(ofMouseEventArgs &arg)
Definition: plotter.h:130
std::function< void(ValueHighlightedCallbackArgs)> onValueHighlightedCallback
Definition: plotter.h:36
void setGridColor(ofColor color)
Definition: plotter.h:240
bool draw(uint32_t x, uint32_t y, uint32_t w, uint32_t h)
Definition: plotter.h:272
void onRangeSelected(const onRangeSelectedCallback &cb, void *data=nullptr)
Definition: plotter.h:38
virtual vector< double > getData(uint32_t x_idx)
Definition: plotter.h:306
void * range_selected_callback_data_
Definition: plotter.h:174
uint32_t end
Definition: plotter.h:13
virtual MatrixDouble getData(uint32_t x_start_idx, uint32_t x_end_idx)=0
void setBackgroundColor(ofColor color)
Definition: plotter.h:232
virtual MatrixDouble getData(uint32_t x_start_idx, uint32_t x_end_idx)
Definition: plotter.h:197
uint32_t x_
Definition: plotter.h:152
uint32_t x_move_
Definition: plotter.h:157
void normalize()
Definition: plotter.h:124
virtual GRT::MatrixDouble & getData()
Definition: plotter.h:193
InteractivePlot * source
Definition: plotter.h:11
InteractivePlot()
Definition: plotter.h:28
uint32_t index
Definition: plotter.h:19
onRangeSelectedCallback range_selected_callback_
Definition: plotter.h:173
uint32_t x_release_
Definition: plotter.h:162
void setup()
Definition: user_accelerometer_gestures.cpp:111
virtual vector< double > getData(uint32_t x_idx)
Definition: plotter.h:210