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 }
PointSeriesXY(const PlotData *x_axis, const PlotData *y_axis)
RangeOpt getVisualizationRangeX() override
#define nullptr
Definition: backward.hpp:386
int getIndexFromX(double x) const
Definition: timeseries.h:107
virtual RangeOpt rangeY() const
Definition: plotdatabase.h:300
PlotDataXY _cached_curve
size_t size() const override
std::optional< Range > RangeOpt
Definition: plotdatabase.h:35
virtual size_t size() const
Definition: plotdatabase.h:182
const PlotData * _y_axis
void updateCache(bool reset_old_data) override
virtual RangeOpt rangeX() const
Definition: plotdatabase.h:275
const Point & at(size_t index) const
Definition: plotdatabase.h:192
const PlotData * _x_axis
virtual void pushBack(const Point &p)
Definition: plotdatabase.h:324
virtual void clear()
Definition: plotdatabase.h:212
std::optional< QPointF > sampleFromTime(double t) override
RangeOpt getVisualizationRangeY(Range range_X) override


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Jun 19 2023 03:01:38