first_derivative.cpp
Go to the documentation of this file.
1 #include "first_derivative.h"
2 #include <QFormLayout>
3 #include <QDoubleValidator>
4 
6  : _widget(new QWidget()), ui(new Ui::FirstDerivariveForm), _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> FirstDerivative::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 der = (p.y - prev.y) / dt;
61  PlotData::Point out = { prev.x, der };
62  return out;
63 }
64 
66 {
67  const size_t data_size = dataSource()->size();
68 
69  if (!dataSource() || data_size < 2)
70  {
71  _widget->setEnabled(false);
72  }
73  return _widget;
74 }
75 
76 bool FirstDerivative::xmlSaveState(QDomDocument& doc, QDomElement& parent_element) const
77 {
78  QDomElement widget_el = doc.createElement("options");
79 
80  if (ui->radioActual->isChecked())
81  {
82  widget_el.setAttribute("radioChecked", "radioActual");
83  }
84  else
85  {
86  widget_el.setAttribute("radioChecked", "radioCustom");
87  }
88  widget_el.setAttribute("lineEdit", ui->lineEditCustom->text());
89 
90  parent_element.appendChild(widget_el);
91  return true;
92 }
93 
94 bool FirstDerivative::xmlLoadState(const QDomElement& parent_element)
95 {
96  QDomElement widget_el = parent_element.firstChildElement("options");
97 
98  ui->lineEditCustom->setText(widget_el.attribute("lineEdit"));
99 
100  if (widget_el.attribute("radioChecked") == "radioActual")
101  {
102  ui->radioActual->setChecked(true);
103  }
104  else
105  {
106  ui->radioCustom->setChecked(true);
107  }
108  return true;
109 }
110 
112 {
113  if (!dataSource() || dataSource()->size() < 2)
114  {
115  return;
116  }
117 
118  const size_t data_size = dataSource()->size();
119 
120  // calculate automatic diff
121  std::vector<double> diff;
122  diff.reserve(data_size - 1);
123  double prev_t = dataSource()->at(0).x;
124  for (size_t i = 1; i < data_size; i++)
125  {
126  double t = dataSource()->at(i).x;
127  double delta = t - prev_t;
128  prev_t = t;
129  diff.push_back(delta);
130  }
131 
132  size_t first = 0;
133  size_t last = diff.size();
134  if (data_size > 10)
135  {
136  std::sort(diff.begin(), diff.end());
137  first = last / 5;
138  last = (last * 4) / 5;
139  }
140  double total = 0;
141  for (size_t i = first; i < last; i++)
142  {
143  total += diff[i];
144  }
145  double estimated_dt = total / static_cast<double>(last - first);
146  ui->lineEditCustom->setText(QString::number(estimated_dt, 'g', 4));
147 
148  if (ui->radioCustom->isChecked())
149  {
150  _dT = estimated_dt;
151  emit parametersChanged();
152  }
153 }
std::optional< PlotData::Point > calculateNextPoint(size_t index) override
Ui::FirstDerivariveForm * ui
bool xmlSaveState(QDomDocument &doc, QDomElement &parent_element) const override
Override this method to save the status of the plugin to XML.
~FirstDerivative() override
bool xmlLoadState(const QDomElement &parent_element) override
Override this method to load the status of the plugin from XML.
virtual size_t size() const
Definition: plotdatabase.h:182
QWidget * optionsWidget() override
optionsWidget pointer to a persistent widget used to set the plugin options .
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