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


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:45