point_series_xy.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 "point_series_xy.h"
8 #include <cmath>
9 #include <cstdlib>
10 
11 PointSeriesXY::PointSeriesXY(const PlotData* x_axis, const PlotData* y_axis)
13  , _x_axis(x_axis)
14  , _y_axis(y_axis)
15  , _cached_curve("", x_axis->group())
16 {
17  updateCache(true);
18 }
19 
20 size_t PointSeriesXY::size() const
21 {
22  return _cached_curve.size();
23 }
24 
25 std::optional<QPointF> PointSeriesXY::sampleFromTime(double t)
26 {
27  if (_cached_curve.size() == 0)
28  {
29  return {};
30  }
31 
32  int index = _y_axis->getIndexFromX(t);
33  if (index < 0)
34  {
35  return {};
36  }
37  const auto& p = _cached_curve.at(size_t(index));
38  return QPointF(p.x, p.y);
39 }
40 
42 {
43  return _cached_curve.rangeY();
44 }
45 
46 void PointSeriesXY::updateCache(bool reset_old_data)
47 {
49 
50  if (_x_axis == nullptr)
51  {
52  throw std::runtime_error("the X axis is null");
53  }
54 
55  const size_t data_size = std::min(_x_axis->size(), _y_axis->size());
56 
57  if (data_size == 0)
58  {
59  return;
60  }
61 
62  const double EPS = std::numeric_limits<double>::epsilon();
63 
64  for (size_t i = 0; i < data_size; i++)
65  {
66  if (std::abs(_x_axis->at(i).x - _y_axis->at(i).x) > EPS)
67  {
68  throw std::runtime_error("X and Y axis don't share the same time axis");
69  }
70 
71  const QPointF p(_x_axis->at(i).y, _y_axis->at(i).y);
72  _cached_curve.pushBack({ p.x(), p.y() });
73  }
74 }
75 
77 {
78  return _cached_curve.rangeX();
79 }
PJ::TimeseriesBase
Definition: timeseries.h:16
PointSeriesXY::getVisualizationRangeY
RangeOpt getVisualizationRangeY(Range range_X) override
Definition: point_series_xy.cpp:41
PointSeriesXY::_cached_curve
PlotDataXY _cached_curve
Definition: point_series_xy.h:50
PointSeriesXY::getVisualizationRangeX
RangeOpt getVisualizationRangeX() override
Definition: point_series_xy.cpp:76
PointSeriesXY::PointSeriesXY
PointSeriesXY(const PlotData *x_axis, const PlotData *y_axis)
Definition: point_series_xy.cpp:11
PointSeriesXY::size
size_t size() const override
Definition: point_series_xy.cpp:20
PJ::PlotDataBase::clear
virtual void clear()
Definition: plotdatabase.h:213
PJ::PlotDataBase::rangeX
virtual RangeOpt rangeX() const
Definition: plotdatabase.h:276
PJ::Range
Definition: plotdatabase.h:24
PJ::TimeseriesBase::getIndexFromX
int getIndexFromX(double x) const
Definition: timeseries.h:106
PointSeriesXY::updateCache
void updateCache(bool reset_old_data) override
Definition: point_series_xy.cpp:46
QwtTimeseries
Definition: timeseries_qwt.h:44
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
PointSeriesXY::_y_axis
const PlotData * _y_axis
Definition: point_series_xy.h:49
PJ::RangeOpt
std::optional< Range > RangeOpt
Definition: plotdatabase.h:36
PointSeriesXY::sampleFromTime
std::optional< QPointF > sampleFromTime(double t) override
Definition: point_series_xy.cpp:25
PJ::PlotDataBase::at
const Point & at(size_t index) const
Definition: plotdatabase.h:193
PointSeriesXY::_x_axis
const PlotData * _x_axis
Definition: point_series_xy.h:48
PJ::PlotDataBase::rangeY
virtual RangeOpt rangeY() const
Definition: plotdatabase.h:301
point_series_xy.h
nullptr
#define nullptr
Definition: backward.hpp:386


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