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()),
7  ui(new Ui::FirstDerivariveForm),
8  _dT(0.0)
9 {
10  ui->setupUi(_widget);
11  ui->lineEditCustom->setValidator( new QDoubleValidator(0.0001, 1000, 4, ui->lineEditCustom) );
12 
13  connect(ui->buttonCompute, &QPushButton::clicked,
15 
16  connect(ui->lineEditCustom, &QLineEdit::editingFinished,
17  this, [=](){
18  _dT = ui->lineEditCustom->text().toDouble();
19  emit parametersChanged();
20  } );
21 
22  connect(ui->radioActual, &QRadioButton::toggled,
23  this, [=](bool toggled){
24  if( toggled) {
25  _dT = 0.0;
26  emit parametersChanged();
27  }
28  } );
29 
30  connect(ui->radioCustom, &QRadioButton::toggled,
31  this, [=](bool toggled){
32  if( toggled) {
33  _dT = ui->lineEditCustom->text().toDouble();
34  emit parametersChanged();
35  }
36  } );
37 }
38 
40 {
41  delete ui;
42  delete _widget;
43 }
44 
47 {
48  if( index == 0)
49  {
50  return {};
51  }
52 
53  const auto& prev = dataSource()->at(index-1);
54  const auto& p = dataSource()->at(index);
55 
56  double dt = (_dT == 0.0) ? (p.x - prev.x) : _dT;
57 
58  if( dt <= 0 )
59  {
60  return {};
61  }
62 
63  double der = (p.y - prev.y) / dt;
64  PlotData::Point out = {prev.x, der};
65  return out;
66 }
67 
69 {
70  const size_t data_size = dataSource()->size();
71 
72  if(!dataSource() || data_size < 2)
73  {
74  _widget->setEnabled(false);
75  }
76  return _widget;
77 }
78 
79 bool FirstDerivative::xmlSaveState(QDomDocument &doc, QDomElement &parent_element) const
80 {
81  QDomElement widget_el = doc.createElement("options");
82 
83  if( ui->radioActual->isChecked() )
84  {
85  widget_el.setAttribute("radioChecked", "radioActual");
86  }
87  else{
88  widget_el.setAttribute("radioChecked", "radioCustom");
89  }
90  widget_el.setAttribute("lineEdit", ui->lineEditCustom->text() );
91 
92  parent_element.appendChild( widget_el );
93  return true;
94 }
95 
96 bool FirstDerivative::xmlLoadState(const QDomElement &parent_element)
97 {
98  QDomElement widget_el = parent_element.firstChildElement("options");
99 
100  ui->lineEditCustom->setText( widget_el.attribute("lineEdit") );
101 
102  if( widget_el.attribute("radioChecked") == "radioActual")
103  {
104  ui->radioActual->setChecked(true);
105  }
106  else{
107  ui->radioCustom->setChecked(true);
108  }
109  return true;
110 }
111 
113 {
114  if(!dataSource() || dataSource()->size() < 2)
115  {
116  return;
117  }
118 
119  const size_t data_size = dataSource()->size();
120 
121  // calculate automatic diff
122  std::vector<double> diff;
123  diff.reserve(data_size-1);
124  double prev_t = dataSource()->at(0).x;
125  for(size_t i=1; i<data_size; i++)
126  {
127  double t = dataSource()->at(i).x;
128  double delta = t - prev_t;
129  prev_t = t;
130  diff.push_back(delta);
131  }
132 
133  size_t first = 0;
134  size_t last = diff.size();
135  if( data_size > 10 )
136  {
137  std::sort(diff.begin(), diff.end());
138  first = last / 5;
139  last = (last*4)/5;
140  }
141  double total = 0;
142  for(size_t i=first; i<last; i++) {
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 }
const Point & at(size_t index) const
Definition: plotdata.h:97
Ui::FirstDerivariveForm * ui
bool xmlSaveState(QDomDocument &doc, QDomElement &parent_element) const override
~FirstDerivative() override
bool xmlLoadState(const QDomElement &parent_element) override
nonstd::optional< PlotData::Point > calculateNextPoint(size_t index) override
QWidget * optionsWidget() override
static int sort(lua_State *L)
Definition: ltablib.c:397
virtual size_t size() const
Definition: plotdata.h:92
const PlotData * dataSource() const
typename PlotDataBase< Value >::Point Point
Definition: plotdata.h:290


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Dec 6 2020 03:47:34