forked from facontidavide/PlotJuggler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeseries_qwt.cpp
249 lines (216 loc) · 7.16 KB
/
timeseries_qwt.cpp
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include "timeseries_qwt.h"
#include <limits>
#include <stdexcept>
#include <QMessageBox>
#include <QString>
TimeseriesQwt::TimeseriesQwt(PlotDataPtr base):
_plot_data(base),
_subsample(1),
_transform( noTransform ),
_time_offset(0)
{
updateData();
}
QPointF TimeseriesQwt::sample(size_t i) const
{
if(_transform == noTransform)
{
auto p = _plot_data->at(i);
return QPointF(p.x - _time_offset, p.y);
}
return _cached_transformed_curve[i];
}
QRectF TimeseriesQwt::boundingRect() const
{
return _bounding_box;
}
size_t TimeseriesQwt::size() const
{
if(_transform == noTransform){
return _plot_data->size();
}
return _cached_transformed_curve.size();
}
void TimeseriesQwt::setSubsampleFactor()
{
// _subsample = (_plot_data->size() / 2000) + 1;
}
void TimeseriesQwt::updateData()
{
if(_plot_data->size() == 0) return;
double min_y =( std::numeric_limits<double>::max() );
double max_y =(-std::numeric_limits<double>::max() );
double min_x =( std::numeric_limits<double>::max() );
double max_x =(-std::numeric_limits<double>::max() );
//if(updated || force_transform)
{
if(_transform == noTransform)
{
_cached_transformed_curve.resize( 0 );
_cached_transformed_curve.shrink_to_fit();
for (size_t i=0; i< _plot_data->size(); i++ )
{
auto p = _plot_data->at( i );
p.x -= _time_offset;
if(min_y > p.y) min_y =(p.y);
else if(max_y < p.y) max_y =(p.y);
if(min_x > p.x) min_x =(p.x);
else if(max_x < p.x) max_x =(p.x);
}
}
else if(_transform == firstDerivative)
{
if( _plot_data->size() < 1){
_cached_transformed_curve.clear();
}
else{
_cached_transformed_curve.resize( _plot_data->size() - 1 );
}
for (size_t i=0; i< _plot_data->size() -1; i++ )
{
const auto& p0 = _plot_data->at( i );
const auto& p1 = _plot_data->at( i+1 );
const auto delta = p1.x - p0.x;
const auto vel = (p1.y - p0.y) /delta;
QPointF p( (p1.x + p0.x)*0.5, vel);
p.setX( p.x() - _time_offset);
_cached_transformed_curve[i] = p;
if(min_y > p.y()) min_y =(p.y());
else if(max_y < p.y()) max_y =(p.y());
if(min_x > p.x()) min_x =(p.x());
else if(max_x < p.x()) max_x =(p.x());
}
}
else if(_transform == secondDerivative)
{
if( _plot_data->size() < 2){
_cached_transformed_curve.clear();
}
else{
_cached_transformed_curve.resize( _plot_data->size() - 2 );
}
for (size_t i=0; i< _cached_transformed_curve.size(); i++ )
{
const auto& p0 = _plot_data->at( i );
const auto& p1 = _plot_data->at( i+1 );
const auto& p2 = _plot_data->at( i+2 );
const auto delta = (p2.x - p0.x) *0.5;
const auto acc = ( p2.y - 2.0* p1.y + p0.y)/(delta*delta);
QPointF p( (p2.x + p0.x)*0.5, acc );
p.setX( p.x() - _time_offset);
_cached_transformed_curve[i] = p;
if(min_y > p.y()) min_y =(p.y());
else if(max_y < p.y()) max_y =(p.y());
if(min_x > p.x()) min_x =(p.x());
else if(max_x < p.x()) max_x =(p.x());
}
}
else if( _transform == XYPlot && _alternative_X_axis)
{
bool failed = false;
const size_t N = _alternative_X_axis->size();
if( _plot_data->size() != N ){
failed = true ;
}
for (size_t i=0; i<N && !failed; i++ )
{
if( _alternative_X_axis->at(i).x != _plot_data->at(i).x ){
failed = true ;
break;
}
}
if( failed){
QMessageBox::warning(0, QString("Warning"),
QString("The creation of the XY plot failed because at least two "
"timeseries don't share the same time axis.") );
return;
}
else{
_cached_transformed_curve.resize(N);
for (size_t i=0; i<N; i++ )
{
const QPointF p(_alternative_X_axis->at(i).y, _plot_data->at(i).y );
_cached_transformed_curve[i] = p;
if(min_y > p.y()) min_y =(p.y());
else if(max_y < p.y()) max_y =(p.y());
if(min_x > p.x()) min_x =(p.x());
else if(max_x < p.x()) max_x =(p.x());
}
}
}
}
_bounding_box.setBottom(min_y);
_bounding_box.setTop(max_y);
_bounding_box.setLeft(min_x);
_bounding_box.setRight(max_x);
}
PlotData::RangeTimeOpt TimeseriesQwt::getVisualizationRangeX()
{
// std::lock_guard<std::mutex> lock(_mutex);
if( this->size() < 2 )
return PlotData::RangeTimeOpt();
else{
return PlotData::RangeTimeOpt( { _bounding_box.left(), _bounding_box.right() } );
}
}
PlotData::RangeValueOpt TimeseriesQwt::getVisualizationRangeY(int first_index, int last_index)
{
if( first_index < 0 || last_index < 0 || first_index > last_index)
{
return PlotData::RangeValueOpt();
}
if( (_transform == XYPlot && _alternative_X_axis) ||
( first_index==0 && last_index == size() -1) )
{
return PlotData::RangeValueOpt( { _bounding_box.bottom(), _bounding_box.top() } );
}
const double first_Y = sample(first_index).y();
double y_min = first_Y;
double y_max = first_Y;
for (int i = first_index+1; i < last_index; i++)
{
const double Y = sample(i).y();
if( Y < y_min ) y_min = Y;
else if( Y > y_max ) y_max = Y;
}
return PlotData::RangeValueOpt( { y_min, y_max } );
}
void TimeseriesQwt::setAlternativeAxisX(PlotDataPtr new_x_data)
{
_alternative_X_axis = new_x_data;
}
nonstd::optional<QPointF> TimeseriesQwt::sampleFromTime(double t)
{
if( _transform == XYPlot && _alternative_X_axis)
{
auto res1 = _alternative_X_axis->getYfromX( t );
if( res1)
{
auto res2 = _plot_data->getYfromX( t );
if( res2 ){
return nonstd::optional<QPointF>(
QPointF(res1.value(), res2.value() ) ) ;
}
}
}
else{
auto res = _plot_data->getYfromX( t );
if(res){
return nonstd::optional<QPointF>( QPointF(t, res.value() ) ) ;
}
}
return nonstd::optional<QPointF>();
}
void TimeseriesQwt::setTransform(TimeseriesQwt::Transform trans)
{
if(trans != _transform)
{
_transform = trans;
updateData();
}
}
void TimeseriesQwt::setTimeOffset(double offset)
{
_time_offset = offset;
updateData();
}