plotdatabase.h
Go to the documentation of this file.
1 /*
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
5  */
6 
7 #ifndef PJ_PLOTDATA_BASE_H
8 #define PJ_PLOTDATA_BASE_H
9 
10 #include <vector>
11 #include <memory>
12 #include <string>
13 #include <map>
14 #include <mutex>
15 #include <deque>
16 #include <type_traits>
17 #include <iostream>
18 #include <cmath>
19 #include <cstdlib>
20 #include <unordered_map>
21 #include <set>
22 #include <string_view>
23 #include <any>
24 #include <optional>
25 #include <QVariant>
26 
27 namespace PJ
28 {
29 struct Range
30 {
31  double min;
32  double max;
33 };
34 
35 typedef std::optional<Range> RangeOpt;
36 
37 // Attributes supported by the GUI.
39 {
40  // color to be displayed on the curve list.
41  // Type: QColor
43 
44  // font style to be displayed on the curve list.
45  // Type: boolean. Default: false
47 
48  // Tooltip to be displayed on the curve list.
49  // Type: QString
51 
52  // Color of the curve in the plot.
53  // Type: QColor
55 };
56 
57 using Attributes = std::unordered_map<PlotAttribute, QVariant>;
58 
59 inline bool CheckType(PlotAttribute attr, const QVariant& value)
60 {
61  switch (attr)
62  {
63  case TEXT_COLOR:
64  case COLOR_HINT:
65  return value.type() == QVariant::Color;
66  case ITALIC_FONTS:
67  return value.type() == QVariant::Bool;
68  case TOOL_TIP:
69  return value.type() == QVariant::String;
70  }
71  return false;
72 }
73 
79 class PlotGroup
80 {
81 public:
82  using Ptr = std::shared_ptr<PlotGroup>;
83 
84  PlotGroup(const std::string& name) : _name(name)
85  {
86  }
87 
88  const std::string& name() const
89  {
90  return _name;
91  }
92 
93  const Attributes& attributes() const
94  {
95  return _attributes;
96  }
97 
99  {
100  return _attributes;
101  }
102 
103  void setAttribute(const PlotAttribute& id, const QVariant& value)
104  {
105  _attributes[id] = value;
106  }
107 
108  QVariant attribute(const PlotAttribute& id) const
109  {
110  auto it = _attributes.find(id);
111  return (it == _attributes.end()) ? QVariant() : it->second;
112  }
113 
114 private:
115  const std::string _name;
117 };
118 
119 // A Generic series of points
120 template <typename TypeX, typename Value>
122 {
123 public:
124  class Point
125  {
126  public:
127  TypeX x;
129  Point(TypeX _x, Value _y) : x(_x), y(_y)
130  {
131  }
132  Point() = default;
133  };
134 
135  enum
136  {
137  MAX_CAPACITY = 1024 * 1024,
138  ASYNC_BUFFER_CAPACITY = 1024
139  };
140 
141  typedef typename std::deque<Point>::iterator Iterator;
142  typedef typename std::deque<Point>::const_iterator ConstIterator;
143  typedef Value ValueT;
144 
145  PlotDataBase(const std::string& name, PlotGroup::Ptr group)
146  : _name(name), _range_x_dirty(true), _range_y_dirty(true), _group(group)
147  {
148  }
149 
150  PlotDataBase(const PlotDataBase& other) = delete;
151  PlotDataBase(PlotDataBase&& other) = default;
152 
153  PlotDataBase& operator=(const PlotDataBase& other) = delete;
154  PlotDataBase& operator=(PlotDataBase&& other) = default;
155 
156  void clonePoints(const PlotDataBase& other)
157  {
158  _points = other._points;
159  _range_x = other._range_x;
160  _range_y = other._range_y;
161  _range_x_dirty = other._range_x_dirty;
162  _range_y_dirty = other._range_y_dirty;
163  }
164 
165  virtual ~PlotDataBase() = default;
166 
167  const std::string& plotName() const
168  {
169  return _name;
170  }
171 
172  const PlotGroup::Ptr& group() const
173  {
174  return _group;
175  }
176 
178  {
179  _group = group;
180  }
181 
182  virtual size_t size() const
183  {
184  return _points.size();
185  }
186 
187  virtual bool isTimeseries() const
188  {
189  return false;
190  }
191 
192  const Point& at(size_t index) const
193  {
194  return _points[index];
195  }
196 
197  Point& at(size_t index)
198  {
199  return _points[index];
200  }
201 
202  const Point& operator[](size_t index) const
203  {
204  return at(index);
205  }
206 
207  Point& operator[](size_t index)
208  {
209  return at(index);
210  }
211 
212  virtual void clear()
213  {
214  _points.clear();
215  _range_x_dirty = true;
216  _range_y_dirty = true;
217  }
218 
219  const Attributes& attributes() const
220  {
221  return _attributes;
222  }
223 
225  {
226  return _attributes;
227  }
228 
229  void setAttribute(PlotAttribute id, const QVariant& value)
230  {
231  _attributes[id] = value;
232  if (!CheckType(id, value))
233  {
234  throw std::runtime_error("PlotDataBase::setAttribute : wrong type");
235  }
236  }
237 
238  QVariant attribute(PlotAttribute id) const
239  {
240  auto it = _attributes.find(id);
241  return (it == _attributes.end()) ? QVariant() : it->second;
242  }
243 
244  const Point& front() const
245  {
246  return _points.front();
247  }
248 
249  const Point& back() const
250  {
251  return _points.back();
252  }
253 
254  ConstIterator begin() const
255  {
256  return _points.begin();
257  }
258 
259  ConstIterator end() const
260  {
261  return _points.end();
262  }
263 
264  Iterator begin()
265  {
266  return _points.begin();
267  }
268 
269  Iterator end()
270  {
271  return _points.end();
272  }
273 
274  // template specialization for types that support compare operator
275  virtual RangeOpt rangeX() const
276  {
277  if constexpr (std::is_arithmetic_v<TypeX>)
278  {
279  if (_points.empty())
280  {
281  return std::nullopt;
282  }
283  if (_range_x_dirty)
284  {
285  _range_x.min = front().x;
286  _range_x.max = _range_x.min;
287  for (const auto& p : _points)
288  {
289  _range_x.min = std::min(_range_x.min, p.x);
290  _range_x.max = std::max(_range_x.max, p.x);
291  }
292  _range_x_dirty = false;
293  }
294  return _range_x;
295  }
296  return std::nullopt;
297  }
298 
299  // template specialization for types that support compare operator
300  virtual RangeOpt rangeY() const
301  {
302  if constexpr (std::is_arithmetic_v<Value>)
303  {
304  if (_points.empty())
305  {
306  return std::nullopt;
307  }
308  if (_range_y_dirty)
309  {
310  _range_y.min = front().y;
311  _range_y.max = _range_y.min;
312  for (const auto& p : _points)
313  {
314  _range_y.min = std::min(_range_y.min, p.y);
315  _range_y.max = std::max(_range_y.max, p.y);
316  }
317  _range_y_dirty = false;
318  }
319  return _range_y;
320  }
321  return std::nullopt;
322  }
323 
324  virtual void pushBack(const Point& p)
325  {
326  auto temp = p;
327  pushBack(std::move(temp));
328  }
329 
330  virtual void pushBack(Point&& p)
331  {
332  if constexpr (std::is_arithmetic_v<TypeX>)
333  {
334  if (std::isinf(p.x) || std::isnan(p.x))
335  {
336  return; // skip
337  }
338  pushUpdateRangeX(p);
339  }
340  if constexpr (std::is_arithmetic_v<Value>)
341  {
342  if (std::isinf(p.y) || std::isnan(p.y))
343  {
344  return; // skip
345  }
346  pushUpdateRangeY(p);
347  }
348 
349  _points.emplace_back(p);
350  }
351 
352  virtual void insert(Iterator it, Point&& p)
353  {
354  if constexpr (std::is_arithmetic_v<TypeX>)
355  {
356  if (std::isinf(p.x) || std::isnan(p.x))
357  {
358  return; // skip
359  }
360  pushUpdateRangeX(p);
361  }
362  if constexpr (std::is_arithmetic_v<Value>)
363  {
364  if (std::isinf(p.y) || std::isnan(p.y))
365  {
366  return; // skip
367  }
368  pushUpdateRangeY(p);
369  }
370 
371  _points.insert(it, p);
372  }
373 
374  virtual void popFront()
375  {
376  const auto& p = _points.front();
377 
378  if constexpr (std::is_arithmetic_v<TypeX>)
379  {
380  if (!_range_x_dirty && (p.x == _range_x.max || p.x == _range_x.min))
381  {
382  _range_x_dirty = true;
383  }
384  }
385 
386  if constexpr (std::is_arithmetic_v<Value>)
387  {
388  if (!_range_y_dirty && (p.y == _range_y.max || p.y == _range_y.min))
389  {
390  _range_y_dirty = true;
391  }
392  }
393  _points.pop_front();
394  }
395 
396 protected:
397  std::string _name;
399  std::deque<Point> _points;
400 
401  mutable Range _range_x;
402  mutable Range _range_y;
403  mutable bool _range_x_dirty;
404  mutable bool _range_y_dirty;
405  mutable std::shared_ptr<PlotGroup> _group;
406 
407  // template specialization for types that support compare operator
408  virtual void pushUpdateRangeX(const Point& p)
409  {
410  if constexpr (std::is_arithmetic_v<TypeX>)
411  {
412  if (_points.empty())
413  {
414  _range_x_dirty = false;
415  _range_x.min = p.x;
416  _range_x.max = p.x;
417  }
418  if (!_range_x_dirty)
419  {
420  if (p.x > _range_x.max)
421  {
422  _range_x.max = p.x;
423  }
424  else if (p.x < _range_x.min)
425  {
426  _range_x.min = p.x;
427  }
428  else
429  {
430  _range_x_dirty = true;
431  }
432  }
433  }
434  }
435 
436  // template specialization for types that support compare operator
437  virtual void pushUpdateRangeY(const Point& p)
438  {
439  if constexpr (std::is_arithmetic_v<Value>)
440  {
441  if (!_range_y_dirty)
442  {
443  if (p.y > _range_y.max)
444  {
445  _range_y.max = p.y;
446  }
447  else if (p.y < _range_y.min)
448  {
449  _range_y.min = p.y;
450  }
451  else
452  {
453  _range_y_dirty = true;
454  }
455  }
456  }
457  }
458 };
459 
460 } // namespace PJ
461 
462 #endif
Attributes _attributes
Definition: plotdatabase.h:116
double max
Definition: plotdatabase.h:32
std::shared_ptr< PlotGroup > Ptr
Definition: plotdatabase.h:82
bool isnan(T)
Definition: chrono.h:787
PlotData may or may not have a group. Think of PlotGroup as a way to say that certain set of series a...
Definition: plotdatabase.h:79
std::shared_ptr< PlotGroup > _group
Definition: plotdatabase.h:405
virtual void insert(Iterator it, Point &&p)
Definition: plotdatabase.h:352
ConstIterator end() const
Definition: plotdatabase.h:259
bool CheckType(PlotAttribute attr, const QVariant &value)
Definition: plotdatabase.h:59
virtual bool isTimeseries() const
Definition: plotdatabase.h:187
const std::string _name
Definition: plotdatabase.h:115
virtual RangeOpt rangeY() const
Definition: plotdatabase.h:300
PlotGroup(const std::string &name)
Definition: plotdatabase.h:84
Definition: lobject.h:49
virtual void pushUpdateRangeX(const Point &p)
Definition: plotdatabase.h:408
virtual void pushUpdateRangeY(const Point &p)
Definition: plotdatabase.h:437
std::deque< Point >::iterator Iterator
Definition: plotdatabase.h:141
std::deque< Point >::const_iterator ConstIterator
Definition: plotdatabase.h:142
Iterator begin()
Definition: plotdatabase.h:264
std::optional< Range > RangeOpt
Definition: plotdatabase.h:35
virtual size_t size() const
Definition: plotdatabase.h:182
const Point & operator[](size_t index) const
Definition: plotdatabase.h:202
virtual void popFront()
Definition: plotdatabase.h:374
const Point & front() const
Definition: plotdatabase.h:244
Point(TypeX _x, Value _y)
Definition: plotdatabase.h:129
Point & at(size_t index)
Definition: plotdatabase.h:197
QVariant attribute(PlotAttribute id) const
Definition: plotdatabase.h:238
const Attributes & attributes() const
Definition: plotdatabase.h:93
std::string _name
Definition: plotdatabase.h:397
virtual RangeOpt rangeX() const
Definition: plotdatabase.h:275
Point & operator[](size_t index)
Definition: plotdatabase.h:207
std::deque< Point > _points
Definition: plotdatabase.h:399
PlotAttribute
Definition: plotdatabase.h:38
const T & move(const T &v)
Definition: backward.hpp:394
QVariant attribute(const PlotAttribute &id) const
Definition: plotdatabase.h:108
const PlotGroup::Ptr & group() const
Definition: plotdatabase.h:172
const Point & at(size_t index) const
Definition: plotdatabase.h:192
const Attributes & attributes() const
Definition: plotdatabase.h:219
void setAttribute(PlotAttribute id, const QVariant &value)
Definition: plotdatabase.h:229
virtual void pushBack(Point &&p)
Definition: plotdatabase.h:330
const std::string & name() const
Definition: plotdatabase.h:88
const std::string & plotName() const
Definition: plotdatabase.h:167
double min
Definition: plotdatabase.h:31
const Point & back() const
Definition: plotdatabase.h:249
Definition: core.h:1131
Iterator end()
Definition: plotdatabase.h:269
void clonePoints(const PlotDataBase &other)
Definition: plotdatabase.h:156
Attributes & attributes()
Definition: plotdatabase.h:224
void setAttribute(const PlotAttribute &id, const QVariant &value)
Definition: plotdatabase.h:103
virtual void pushBack(const Point &p)
Definition: plotdatabase.h:324
Attributes _attributes
Definition: plotdatabase.h:398
std::unordered_map< PlotAttribute, QVariant > Attributes
Definition: plotdatabase.h:57
virtual void clear()
Definition: plotdatabase.h:212
PlotDataBase(const std::string &name, PlotGroup::Ptr group)
Definition: plotdatabase.h:145
ConstIterator begin() const
Definition: plotdatabase.h:254
Attributes & attributes()
Definition: plotdatabase.h:98
void changeGroup(PlotGroup::Ptr group)
Definition: plotdatabase.h:177


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Jun 19 2023 03:01:38