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


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:44