timeseries_qwt.cpp
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 #include "timeseries_qwt.h"
8 #include <limits>
9 #include <stdexcept>
10 #include <QMessageBox>
11 #include <QPushButton>
12 #include <QString>
13 
15 {
16  if (range_x.min <= std::numeric_limits<double>::lowest() &&
17  range_x.min <= std::numeric_limits<double>::max())
18  return _data->rangeY();
19 
20  double min_y = (std::numeric_limits<double>::max());
21  double max_y = (std::numeric_limits<double>::lowest());
22 
23  for (size_t i = 0; i < size(); i++)
24  {
25  const double Y = sample(i).y();
26  min_y = std::min(min_y, Y);
27  max_y = std::max(max_y, Y);
28  }
29  return Range{ min_y, max_y };
30 }
31 
33 {
34  int first_index = _ts_data->getIndexFromX(range_X.min + _time_offset);
35  int last_index = _ts_data->getIndexFromX(range_X.max + _time_offset);
36 
37  if (first_index > last_index || first_index < 0 || last_index < 0)
38  {
39  return {};
40  }
41 
42  if (first_index == 0 && last_index == plotData()->size() - 1)
43  {
44  return _ts_data->rangeY();
45  }
46 
47  double min_y = (std::numeric_limits<double>::max());
48  double max_y = (std::numeric_limits<double>::lowest());
49 
50  for (size_t i = first_index; i < last_index; i++)
51  {
52  const double Y = sample(i).y();
53  min_y = std::min(min_y, Y);
54  max_y = std::max(max_y, Y);
55  }
56  return Range{ min_y, max_y };
57 }
58 
59 std::optional<QPointF> QwtTimeseries::sampleFromTime(double t)
60 {
61  int index = _ts_data->getIndexFromX(t);
62  if (index < 0)
63  {
64  return {};
65  }
66  const auto& p = plotData()->at(size_t(index));
67  return QPointF(p.x, p.y);
68 }
69 
71  : QwtTimeseries(&_dst_data)
72  , _dst_data(source_data->plotName(), {})
73  , _src_data(source_data)
74 {
75 }
76 
78 {
79  return _transform;
80 }
81 
82 void TransformedTimeseries::setTransform(QString transform_ID)
83 {
84  if (transformName() == transform_ID)
85  {
86  return;
87  }
88  if (transform_ID.isEmpty())
89  {
90  _transform.reset();
91  }
92  else
93  {
94  _dst_data.clear();
95  _transform = TransformFactory::create(transform_ID.toStdString());
96  std::vector<PlotData*> dest = { &_dst_data };
97  _transform->setData(nullptr, { _src_data }, dest);
98  }
99 }
100 
101 void TransformedTimeseries::updateCache(bool reset_old_data)
102 {
103  if (_transform)
104  {
105  if (reset_old_data)
106  {
107  _dst_data.clear();
108  _transform->reset();
109  }
110  std::vector<PlotData*> dest = { &_dst_data };
111  _transform->calculate();
112  }
113  else
114  {
115  // TODO: optimize ??
116  _dst_data.clear();
117  for (size_t i = 0; i < _src_data->size(); i++)
118  {
120  }
121  }
122 }
123 
125 {
126  return (!_transform) ? QString() : _transform->name();
127 }
128 
130 {
131  return _alias;
132 }
133 
135 {
136  _alias = alias;
137 }
138 
140 {
141  if (size() == 0)
142  {
143  return {};
144  }
145  auto range_x = plotData()->rangeX().value();
146  auto range_y = plotData()->rangeY().value();
147 
148  QRectF box;
149  box.setLeft(range_x.min);
150  box.setRight(range_x.max);
151  box.setTop(range_y.max);
152  box.setBottom(range_y.min);
153  return box;
154 }
155 
157 {
158  if (size() == 0)
159  {
160  return {};
161  }
162  auto range_x = plotData()->rangeX().value();
163  auto range_y = plotData()->rangeY().value();
164 
165  QRectF box;
166  box.setLeft(range_x.min - _time_offset);
167  box.setRight(range_x.max - _time_offset);
168  box.setTop(range_y.max);
169  box.setBottom(range_y.min);
170  return box;
171 }
172 
173 QPointF QwtSeriesWrapper::sample(size_t i) const
174 {
175  const auto& p = _data->at(i);
176  return QPointF(p.x, p.y);
177 }
178 
179 QPointF QwtTimeseries::sample(size_t i) const
180 {
181  const auto& p = _ts_data->at(i);
182  return QPointF(p.x - _time_offset, p.y);
183 }
184 
186 {
187  return _data->size();
188 }
189 
190 void QwtTimeseries::setTimeOffset(double offset)
191 {
192  _time_offset = offset;
193 }
194 
196 {
197  if (this->size() < 2)
198  {
199  return {};
200  }
201  else
202  {
203  return _data->rangeX();
204  }
205 }
206 
208 {
209  if (this->size() < 2)
210  {
211  return {};
212  }
213  else
214  {
215  auto range = _ts_data->rangeX().value();
216  return RangeOpt({ range.min - _time_offset, range.max - _time_offset });
217  }
218 }
219 
221 {
222  return _data;
223 }
QwtSeriesWrapper::size
size_t size() const override
Definition: timeseries_qwt.cpp:185
PJ::TimeseriesBase
Definition: timeseries.h:16
QwtTimeseries::_time_offset
double _time_offset
Definition: timeseries_qwt.h:69
TransformedTimeseries::_transform
TransformFunction_SISO::Ptr _transform
Definition: timeseries_qwt.h:95
TransformedTimeseries::_dst_data
PlotData _dst_data
Definition: timeseries_qwt.h:93
PJ::TransformFunction::Ptr
std::shared_ptr< TransformFunction > Ptr
Definition: transform_function.h:27
TransformedTimeseries::alias
QString alias() const
Definition: timeseries_qwt.cpp:129
PJ::TimeseriesBase::pushBack
void pushBack(const Point &p) override
Definition: timeseries.h:61
TransformedTimeseries::transform
TransformFunction::Ptr transform()
Definition: timeseries_qwt.cpp:77
TransformedTimeseries::updateCache
virtual void updateCache(bool reset_old_data) override
Definition: timeseries_qwt.cpp:101
PJ::PlotDataBase::clear
virtual void clear()
Definition: plotdatabase.h:213
PJ::PlotDataBase::rangeX
virtual RangeOpt rangeX() const
Definition: plotdatabase.h:276
QwtTimeseries::sampleFromTime
virtual std::optional< QPointF > sampleFromTime(double t)
Definition: timeseries_qwt.cpp:59
QwtTimeseries::sample
QPointF sample(size_t i) const override
Definition: timeseries_qwt.cpp:179
timeseries_qwt.h
QwtSeriesWrapper::plotData
virtual const PlotDataXY * plotData() const
Definition: timeseries_qwt.cpp:220
TransformedTimeseries::_alias
QString _alias
Definition: timeseries_qwt.h:92
QwtSeriesWrapper::getVisualizationRangeX
virtual RangeOpt getVisualizationRangeX()
Definition: timeseries_qwt.cpp:195
TransformedTimeseries::_src_data
const PlotData * _src_data
Definition: timeseries_qwt.h:94
PJ::Range::max
double max
Definition: plotdatabase.h:27
TransformedTimeseries::transformName
QString transformName()
Definition: timeseries_qwt.cpp:124
TransformedTimeseries::setAlias
void setAlias(QString alias)
Definition: timeseries_qwt.cpp:134
QwtTimeseries::getVisualizationRangeX
virtual RangeOpt getVisualizationRangeX() override
Definition: timeseries_qwt.cpp:207
QwtTimeseries::boundingRect
QRectF boundingRect() const override
Definition: timeseries_qwt.cpp:156
PJ::Range
Definition: plotdatabase.h:24
PJ::TimeseriesBase::getIndexFromX
int getIndexFromX(double x) const
Definition: timeseries.h:106
QwtSeriesWrapper::boundingRect
QRectF boundingRect() const override
Definition: timeseries_qwt.cpp:139
QwtTimeseries
Definition: timeseries_qwt.h:44
dest
char * dest
Definition: lz4.h:765
sol::create
const new_table create
Definition: sol.hpp:7807
PJ::PlotDataBase::size
virtual size_t size() const
Definition: plotdatabase.h:183
QwtSeriesWrapper::getVisualizationRangeY
virtual RangeOpt getVisualizationRangeY(Range range_X)
Definition: timeseries_qwt.cpp:14
QwtTimeseries::setTimeOffset
void setTimeOffset(double offset)
Definition: timeseries_qwt.cpp:190
QwtSeriesWrapper::_data
const PlotDataXY * _data
Definition: timeseries_qwt.h:20
PJ::RangeOpt
std::optional< Range > RangeOpt
Definition: plotdatabase.h:36
QwtSeriesWrapper::sample
QPointF sample(size_t i) const override
Definition: timeseries_qwt.cpp:173
PJ::PlotDataBase::at
const Point & at(size_t index) const
Definition: plotdatabase.h:193
TransformedTimeseries::setTransform
void setTransform(QString transform_ID)
Definition: timeseries_qwt.cpp:82
PJ::PlotDataBase::rangeY
virtual RangeOpt rangeY() const
Definition: plotdatabase.h:301
PJ::PlotDataBase
Definition: plotdatabase.h:122
QwtTimeseries::getVisualizationRangeY
virtual RangeOpt getVisualizationRangeY(Range range_X) override
Definition: timeseries_qwt.cpp:32
QwtTimeseries::_ts_data
const PlotData * _ts_data
Definition: timeseries_qwt.h:68
TransformedTimeseries::TransformedTimeseries
TransformedTimeseries(const PlotData *source_data)
Definition: timeseries_qwt.cpp:70
PJ::Range::min
double min
Definition: plotdatabase.h:26


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