-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfitcontroller.cpp
More file actions
188 lines (153 loc) · 4.88 KB
/
Copy pathfitcontroller.cpp
File metadata and controls
188 lines (153 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "fitcontroller.h"
#include "workerprocessing.h"
FitController::FitController(QObject *parent) : QObject(parent) {
_workerThread = new QThread(this);
_worker = new workerprocessing;
_worker->moveToThread(_workerThread);
connect(this, &FitController::requestRun, _worker, &workerprocessing::run);
connect(_worker, &workerprocessing::finished, this, &FitController::onWorkerFinished);
connect(_worker, &workerprocessing::canceled, this, &FitController::onWorkerCanceled);
connect(_worker, &workerprocessing::error, this, &FitController::onWorkerError);
_debounce = new QTimer(this);
_debounce->setInterval(1000);
_debounce->setSingleShot(true);
connect(_debounce, &QTimer::timeout, this, &FitController::fire);
connect(this, &FitController::plotTypeChanged, this, &FitController::onInputChanged);
connect(this, &FitController::useFractionsChanged, this, &FitController::onInputChanged);
_workerThread->start();
}
FitController::~FitController() {
_worker->requestCancellation();
_workerThread->quit();
_workerThread->wait();
delete _worker;
}
PointData *FitController::source() const {
return _source;
}
void FitController::setSource(PointData *source) {
if (_source == source) return;
if (_source)
disconnect(_source, &PointData::pointsChanged,
this, &FitController::onInputChanged);
_source = source;
if (_source)
connect(_source, &PointData::pointsChanged,
this, &FitController::onInputChanged);
emit sourceChanged();
if (_source) onInputChanged();
}
PlotTypes::PlotType FitController::plotType() const {
return _plotType;
}
void FitController::setPlotType(const PlotTypes::PlotType plotType) {
if (_plotType == plotType) return;
_plotType = plotType;
emit plotTypeChanged();
}
bool FitController::useFractions() const {
return _useFractions;
}
void FitController::setUseFractions(const bool useFractions) {
if (_useFractions == useFractions) return;
_useFractions = useFractions;
emit useFractionsChanged();
}
FitController::Progress FitController::progress() const {
return _progress;
}
QString FitController::error() const {
return _error;
}
QString FitController::resultEquation() const {
return _resultEquation;
}
SidebarResult FitController::resultMatrices() const {
return _sdRes;
}
PlotTypes::PlotType FitController::resolvedFitType() const {
return _resolvedFitType;
}
double FitController::evaluateAt(const double x) const {
if (_progress != READY) return qQNaN();
switch (_resolvedFitType) {
case PlotTypes::LINEAL:
return _bA + _bB * x;
case PlotTypes::CUADRATIC:
return _bA + _bB * x + _bC * x * x;
case PlotTypes::EXPONENTIAL: {
const double expo = _bB * x;
if (expo > 700.0 || expo < -700.0) return qQNaN();
return _bA * std::exp(expo);
}
case PlotTypes::AUTOMATIC_FIT:
return qQNaN();
}
return qQNaN();
}
double FitController::residualFor(const QPointF &point) const {
return point.y() - evaluateAt(point.x());
}
double FitController::residualFor(const int idx) const {
if (idx >= _source->pointCount() || idx < 0) return {};
const auto point = _source->pointAt(idx);
return point.y() - evaluateAt(point.x());
}
void FitController::onInputChanged() {
if (!_source || _source->pointCount() < 2) {
_debounce->stop();
setProgress(NOTFIRED);
return;
}
if (_progress == PROCESSING) {
_pendingRestart = true;
_worker->requestCancellation();
return;
}
scheduleRun();
}
void FitController::onWorkerFinished(const Result &result) {
if (_pendingRestart) {
_pendingRestart = false;
_debounce->start();
return;
}
if (_debounce->isActive()) return;
_resultEquation = result.eqRes;
_bA = result.betaA;
_bB = result.betaB;
_bC = result.betaC;
_sdRes = result.sr;
_resolvedFitType = _plotType == PlotTypes::AUTOMATIC_FIT
? result.selectedPlotType
: _plotType;
setProgress(READY);
emit errorChanged();
emit resultChanged();
}
void FitController::onWorkerError(const QString &err) {
_error = err;
setProgress(ERROR);
}
void FitController::onWorkerCanceled() {
if (_pendingRestart || _debounce->isActive()) {
_pendingRestart = false;
_debounce->start();
return;
}
setProgress(CANCELED);
}
void FitController::fire() {
_pendingRestart = false;
setProgress(PROCESSING);
emit requestRun(_source->allPoints(), _plotType, _useFractions);
}
void FitController::scheduleRun() {
setProgress(PROCESSING);
_debounce->start();
}
void FitController::setProgress(const Progress p) {
if (_progress == p) return;
_progress = p;
emit progressChanged();
}