integral_transform.cpp
Go to the documentation of this file.
1 #include "integral_transform.h"
2 #include <QFormLayout>
3 #include <QDoubleValidator>
4 
6  : _widget(new QWidget()), ui(new Ui::IntegralTransform), _dT(0.0)
7 {
8  ui->setupUi(_widget);
9  ui->lineEditCustom->setValidator(
10  new QDoubleValidator(0.0001, 1000, 4, ui->lineEditCustom));
11 
12  connect(ui->buttonCompute, &QPushButton::clicked, this,
14 
15  connect(ui->lineEditCustom, &QLineEdit::editingFinished, this, [=]() {
16  _dT = ui->lineEditCustom->text().toDouble();
17  emit parametersChanged();
18  });
19 
20  connect(ui->radioActual, &QRadioButton::toggled, this, [=](bool toggled) {
21  if (toggled)
22  {
23  _dT = 0.0;
24  emit parametersChanged();
25  }
26  });
27 
28  connect(ui->radioCustom, &QRadioButton::toggled, this, [=](bool toggled) {
29  if (toggled)
30  {
31  _dT = ui->lineEditCustom->text().toDouble();
32  emit parametersChanged();
33  }
34  });
35 }
36 
38 {
39  delete ui;
40  delete _widget;
41 }
42 
43 std::optional<PlotData::Point> IntegralTransform::calculateNextPoint(size_t index)
44 {
45  if (index == 0)
46  {
47  return {};
48  }
49 
50  const auto& prev = dataSource()->at(index - 1);
51  const auto& p = dataSource()->at(index);
52 
53  double dt = (_dT == 0.0) ? (p.x - prev.x) : _dT;
54 
55  if (dt <= 0)
56  {
57  return {};
58  }
59 
60  double val = (p.y + prev.y) * dt / (2.0);
61  _accumulated_value += val;
62  PlotData::Point out = { p.x, _accumulated_value };
63  return out;
64 }
65 
67 {
68  const size_t data_size = dataSource()->size();
69 
70  if (!dataSource() || data_size < 2)
71  {
72  _widget->setEnabled(false);
73  }
74  return _widget;
75 }
76 
78 {
79  _accumulated_value = 0.0;
81 }
82 
83 bool IntegralTransform::xmlSaveState(QDomDocument& doc, QDomElement& parent_element) const
84 {
85  QDomElement widget_el = doc.createElement("options");
86 
87  if (ui->radioActual->isChecked())
88  {
89  widget_el.setAttribute("radioChecked", "radioActual");
90  }
91  else
92  {
93  widget_el.setAttribute("radioChecked", "radioCustom");
94  }
95  widget_el.setAttribute("lineEdit", ui->lineEditCustom->text());
96 
97  parent_element.appendChild(widget_el);
98  return true;
99 }
100 
101 bool IntegralTransform::xmlLoadState(const QDomElement& parent_element)
102 {
103  QDomElement widget_el = parent_element.firstChildElement("options");
104 
105  ui->lineEditCustom->setText(widget_el.attribute("lineEdit"));
106 
107  if (widget_el.attribute("radioChecked") == "radioActual")
108  {
109  ui->radioActual->setChecked(true);
110  }
111  else
112  {
113  ui->radioCustom->setChecked(true);
114  }
115  return true;
116 }
117 
119 {
120  if (!dataSource() || dataSource()->size() < 2)
121  {
122  return;
123  }
124 
125  const size_t data_size = dataSource()->size();
126 
127  // calculate automatic diff
128  std::vector<double> diff;
129  diff.reserve(data_size - 1);
130  double prev_t = dataSource()->at(0).x;
131  for (size_t i = 1; i < data_size; i++)
132  {
133  double t = dataSource()->at(i).x;
134  double delta = t - prev_t;
135  prev_t = t;
136  diff.push_back(delta);
137  }
138 
139  size_t first = 0;
140  size_t last = diff.size();
141  if (data_size > 10)
142  {
143  std::sort(diff.begin(), diff.end());
144  first = last / 5;
145  last = (last * 4) / 5;
146  }
147  double total = 0;
148  for (size_t i = first; i < last; i++)
149  {
150  total += diff[i];
151  }
152  double estimated_dt = total / static_cast<double>(last - first);
153  ui->lineEditCustom->setText(QString::number(estimated_dt, 'g', 4));
154 
155  if (ui->radioCustom->isChecked())
156  {
157  _dT = estimated_dt;
158  emit parametersChanged();
159  }
160 }
bool xmlLoadState(const QDomElement &parent_element) override
Override this method to load the status of the plugin from XML.
void reset() override
Ui::IntegralTransform * ui
virtual size_t size() const
Definition: plotdatabase.h:182
bool xmlSaveState(QDomDocument &doc, QDomElement &parent_element) const override
Override this method to save the status of the plugin to XML.
QWidget * optionsWidget() override
optionsWidget pointer to a persistent widget used to set the plugin options .
std::optional< PlotData::Point > calculateNextPoint(size_t index) override
static int sort(lua_State *L)
Definition: ltablib.c:398
const T & first(const T &value, const Tail &...)
Definition: compile.h:178
typename PlotDataBase< double, Value >::Point Point
Definition: timeseries.h:23
const Point & at(size_t index) const
Definition: plotdatabase.h:192
const PlotData * dataSource() const
span_constexpr std::size_t size(span< T, Extent > const &spn)
Definition: span.hpp:1485


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