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)
75  { return a.x < b.x; });
77  }
78  else
79  {
81  }
82  trimRange();
83  }
84 
85 private:
86  void trimRange()
87  {
88  if(_max_range_x < std::numeric_limits<double>::max() && !_points.empty())
89  {
90  auto const back_point_x = _points.back().x;
91  while (_points.size() > 2 && (back_point_x - _points.front().x) > _max_range_x)
92  {
93  this->popFront();
94  }
95  }
96  }
97 
98  static bool TimeCompare(const Point& a, const Point& b)
99  {
100  return a.x < b.x;
101  }
102 };
103 
104 //--------------------
105 
106 template <typename Value>
107 inline int TimeseriesBase<Value>::getIndexFromX(double x) const
108 {
109  if (_points.size() == 0)
110  {
111  return -1;
112  }
113  auto lower =
114  std::lower_bound(_points.begin(), _points.end(), Point(x, {}), TimeCompare);
115  auto index = std::distance(_points.begin(), lower);
116 
117  if (index >= _points.size())
118  {
119  return _points.size() - 1;
120  }
121  if (index < 0)
122  {
123  return 0;
124  }
125 
126  if (index > 0 && (abs(_points[index - 1].x - x) < abs(_points[index].x - x)))
127  {
128  index = index - 1;
129  }
130  return index;
131 }
132 
133 } // namespace PJ
134 
135 #endif
TimeseriesBase(const std::string &name, PlotGroup::Ptr group)
Definition: timeseries.h:25
std::shared_ptr< PlotGroup > Ptr
Definition: plotdatabase.h:82
virtual void insert(Iterator it, Point &&p)
Definition: plotdatabase.h:352
int getIndexFromX(double x) const
Definition: timeseries.h:107
Definition: lobject.h:49
void pushBack(Point &&p) override
Definition: timeseries.h:67
void setMaximumRangeX(double max_range)
Definition: timeseries.h:42
std::optional< Value > getYfromX(double x) const
Definition: timeseries.h:55
const T & move(const T &v)
Definition: backward.hpp:394
typename PlotDataBase< double, StringRef >::Point Point
Definition: timeseries.h:23
const PlotGroup::Ptr & group() const
Definition: plotdatabase.h:172
void pushBack(const Point &p) override
Definition: timeseries.h:61
TimeseriesBase & operator=(const TimeseriesBase &other)=delete
const Point & back() const
Definition: plotdatabase.h:249
static bool TimeCompare(const Point &a, const Point &b)
Definition: timeseries.h:98
virtual void pushBack(const Point &p)
Definition: plotdatabase.h:324
virtual bool isTimeseries() const override
Definition: timeseries.h:37
double maximumRangeX() const
Definition: timeseries.h:48


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Jun 19 2023 03:12:53