timeseries.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_TIMESERIES_H
8 #define PJ_TIMESERIES_H
9 
10 #include "plotdatabase.h"
11 #include <algorithm>
12 
13 namespace PJ
14 {
15 template <typename Value>
16 class TimeseriesBase : public PlotDataBase<double, Value>
17 {
18 protected:
19  double _max_range_x;
21 
22 public:
24 
25  TimeseriesBase(const std::string& name, PlotGroup::Ptr group)
26  : PlotDataBase<double, Value>(name, group)
27  , _max_range_x(std::numeric_limits<double>::max())
28  {
29  }
30 
31  TimeseriesBase(const TimeseriesBase& other) = delete;
32  TimeseriesBase(TimeseriesBase&& other) = default;
33 
34  TimeseriesBase& operator=(const TimeseriesBase& other) = delete;
35  TimeseriesBase& operator=(TimeseriesBase&& other) = default;
36 
37  virtual bool isTimeseries() const override
38  {
39  return true;
40  }
41 
42  void setMaximumRangeX(double max_range)
43  {
44  _max_range_x = max_range;
45  trimRange();
46  }
47 
48  double maximumRangeX() const
49  {
50  return _max_range_x;
51  }
52 
53  int getIndexFromX(double x) const;
54 
55  std::optional<Value> getYfromX(double x) const
56  {
57  int index = getIndexFromX(x);
58  return (index < 0) ? std::nullopt : std::optional(_points[index].y);
59  }
60 
61  void pushBack(const Point& p) override
62  {
63  auto temp = p;
64  pushBack(std::move(temp));
65  }
66 
67  void pushBack(Point&& p) override
68  {
69  bool need_sorting = (!_points.empty() && p.x < this->back().x);
70 
71  if (need_sorting)
72  {
73  auto it = std::upper_bound(_points.begin(), _points.end(), p,
74  [](const auto& a, const auto& b) { return a.x < b.x; });
76  }
77  else
78  {
80  }
81  trimRange();
82  }
83 
84 private:
85  void trimRange()
86  {
87  if (_max_range_x < std::numeric_limits<double>::max() && !_points.empty())
88  {
89  auto const back_point_x = _points.back().x;
90  while (_points.size() > 2 && (back_point_x - _points.front().x) > _max_range_x)
91  {
92  this->popFront();
93  }
94  }
95  }
96 
97  static bool TimeCompare(const Point& a, const Point& b)
98  {
99  return a.x < b.x;
100  }
101 };
102 
103 //--------------------
104 
105 template <typename Value>
106 inline int TimeseriesBase<Value>::getIndexFromX(double x) const
107 {
108  if (_points.size() == 0)
109  {
110  return -1;
111  }
112  auto lower =
113  std::lower_bound(_points.begin(), _points.end(), Point(x, {}), TimeCompare);
114  auto index = std::distance(_points.begin(), lower);
115 
116  if (index >= _points.size())
117  {
118  return _points.size() - 1;
119  }
120  if (index < 0)
121  {
122  return 0;
123  }
124 
125  if (index > 0 && (abs(_points[index - 1].x - x) < abs(_points[index].x - x)))
126  {
127  index = index - 1;
128  }
129  return index;
130 }
131 
132 } // namespace PJ
133 
134 #endif
PJ::TimeseriesBase
Definition: timeseries.h:16
PJ::TimeseriesBase::getYfromX
std::optional< Value > getYfromX(double x) const
Definition: timeseries.h:55
PJ::TimeseriesBase::pushBack
void pushBack(const Point &p) override
Definition: timeseries.h:61
PJ::TimeseriesBase::operator=
TimeseriesBase & operator=(const TimeseriesBase &other)=delete
mqtt_test_proto.x
x
Definition: mqtt_test_proto.py:34
PJ::PlotDataBase< double, Value >::group
const PlotGroup::Ptr & group() const
Definition: plotdatabase.h:173
mqtt_test_proto.y
y
Definition: mqtt_test_proto.py:35
PJ::PlotDataBase< double, Value >::back
const Point & back() const
Definition: plotdatabase.h:250
PJ::PlotGroup::Ptr
std::shared_ptr< PlotGroup > Ptr
Definition: plotdatabase.h:83
PJ::TimeseriesBase::trimRange
void trimRange()
Definition: timeseries.h:85
PJ::TimeseriesBase::_max_range_x
double _max_range_x
Definition: timeseries.h:19
backward::details::move
const T & move(const T &v)
Definition: backward.hpp:394
PJ::TimeseriesBase::TimeCompare
static bool TimeCompare(const Point &a, const Point &b)
Definition: timeseries.h:97
PJ::TimeseriesBase::setMaximumRangeX
void setMaximumRangeX(double max_range)
Definition: timeseries.h:42
PJ::TimeseriesBase::pushBack
void pushBack(Point &&p) override
Definition: timeseries.h:67
PJ::TimeseriesBase< StringRef >::Point
typename PlotDataBase< double, StringRef >::Point Point
Definition: timeseries.h:23
PJ::PlotDataBase< double, Value >::popFront
virtual void popFront()
Definition: plotdatabase.h:375
plotdatabase.h
PJ::TimeseriesBase::getIndexFromX
int getIndexFromX(double x) const
Definition: timeseries.h:106
PJ::PlotDataBase::insert
virtual void insert(Iterator it, Point &&p)
Definition: plotdatabase.h:353
PJ::PlotDataBase::pushBack
virtual void pushBack(const Point &p)
Definition: plotdatabase.h:325
PJ::TimeseriesBase::maximumRangeX
double maximumRangeX() const
Definition: timeseries.h:48
std
PJ
Definition: dataloader_base.h:16
PJ::PlotDataBase
Definition: plotdatabase.h:122
PJ::PlotDataBase< double, Value >::_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::TimeseriesBase::isTimeseries
virtual bool isTimeseries() const override
Definition: timeseries.h:37
PJ::TimeseriesBase::TimeseriesBase
TimeseriesBase(const std::string &name, PlotGroup::Ptr group)
Definition: timeseries.h:25


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Aug 11 2024 02:24:26