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  if (widget_el.isNull())
98  {
99  return false;
100  }
101 
102  ui->lineEditCustom->setText(widget_el.attribute("lineEdit"));
103 
104  if (widget_el.attribute("radioChecked") == "radioActual")
105  {
106  ui->radioActual->setChecked(true);
107  }
108  else
109  {
110  ui->radioCustom->setChecked(true);
111  }
112  return true;
113 }
114 
116 {
117  if (!dataSource() || dataSource()->size() < 2)
118  {
119  return;
120  }
121 
122  const size_t data_size = dataSource()->size();
123 
124  // calculate automatic diff
125  std::vector<double> diff;
126  diff.reserve(data_size - 1);
127  double prev_t = dataSource()->at(0).x;
128  for (size_t i = 1; i < data_size; i++)
129  {
130  double t = dataSource()->at(i).x;
131  double delta = t - prev_t;
132  prev_t = t;
133  diff.push_back(delta);
134  }
135 
136  size_t first = 0;
137  size_t last = diff.size();
138  if (data_size > 10)
139  {
140  std::sort(diff.begin(), diff.end());
141  first = last / 5;
142  last = (last * 4) / 5;
143  }
144  double total = 0;
145  for (size_t i = first; i < last; i++)
146  {
147  total += diff[i];
148  }
149  double estimated_dt = total / static_cast<double>(last - first);
150  ui->lineEditCustom->setText(QString::number(estimated_dt, 'g', 4));
151 
152  if (ui->radioCustom->isChecked())
153  {
154  _dT = estimated_dt;
155  emit parametersChanged();
156  }
157 }
detail::first
auto first(const T &value, const Tail &...) -> const T &
Definition: compile.h:60
FirstDerivative::optionsWidget
QWidget * optionsWidget() override
optionsWidget pointer to a persistent widget used to set the plugin options .
Definition: first_derivative.cpp:65
FirstDerivative::_dT
double _dT
Definition: first_derivative.h:40
FirstDerivative::xmlLoadState
bool xmlLoadState(const QDomElement &parent_element) override
Override this method to load the status of the plugin from XML.
Definition: first_derivative.cpp:94
FirstDerivative::on_buttonCompute_clicked
void on_buttonCompute_clicked()
Definition: first_derivative.cpp:115
FirstDerivative::calculateNextPoint
std::optional< PlotData::Point > calculateNextPoint(size_t index) override
Definition: first_derivative.cpp:43
nonstd::span_lite::size
span_constexpr std::size_t size(span< T, Extent > const &spn)
Definition: span.hpp:1554
FirstDerivative::FirstDerivative
FirstDerivative()
Definition: first_derivative.cpp:5
Ui
Definition: cheatsheet_dialog.h:6
FirstDerivative::_widget
QWidget * _widget
Definition: first_derivative.h:38
FirstDerivative::~FirstDerivative
~FirstDerivative() override
Definition: first_derivative.cpp:37
PJ::TimeseriesBase::Point
typename PlotDataBase< double, Value >::Point Point
Definition: timeseries.h:23
sort
static int sort(lua_State *L)
Definition: ltablib.c:398
FirstDerivative::ui
Ui::FirstDerivariveForm * ui
Definition: first_derivative.h:39
PJ::PlotDataBase::size
virtual size_t size() const
Definition: plotdatabase.h:183
PJ::TransformFunction::parametersChanged
void parametersChanged()
first_derivative.h
PJ::TransformFunction_SISO::dataSource
const PlotData * dataSource() const
Definition: transform_function.cpp:113
PJ::PlotDataBase::at
const Point & at(size_t index) const
Definition: plotdatabase.h:193
FirstDerivative::xmlSaveState
bool xmlSaveState(QDomDocument &doc, QDomElement &parent_element) const override
Override this method to save the status of the plugin to XML.
Definition: first_derivative.cpp:76


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Aug 11 2024 02:24:22