-
Notifications
You must be signed in to change notification settings - Fork 2
/
qwtarrayplotitem.cpp
302 lines (248 loc) · 5.69 KB
/
qwtarrayplotitem.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include "QwtArrayPlotItem.h"
QwtArrayPlotItem::QwtArrayPlotItem(const QwtText &title):
QwtPlotItem(title),
m_dt(1.0),
m_size(0),
m_data(0),
m_plotColor(Qt::red)
{
setItemAttribute(QwtPlotItem::AutoScale, true);
setRenderHint(QwtPlotItem::RenderAntialiased,true);
}
QwtArrayPlotItem::~QwtArrayPlotItem()
{
}
void QwtArrayPlotItem::draw( QPainter *painter,
const QwtScaleMap &xMap, const QwtScaleMap &yMap,
const QRectF &canvasRect ) const
{
//some data checks first
if(!m_data)
return;
if(m_size<2)
return;
if(qFuzzyCompare(m_dt,0.0))
return;
//array that will be used to store calculated plot points in screen coordinates
QPointF* points=0;
//number of points in array (will be calculated later)
quint32 numberOfPlotPoints = 0;
//number of visible points for current zoom
quint32 realpoints = xMap.sDist()/m_dt;
if(realpoints>m_size) //wrong axes, just use standart value then
realpoints = m_size;
//number of pixels
int pixels = xMap.pDist();
if(pixels == 0)
return;
if(realpoints>2*pixels) //we have twice more points then screen pixels - need to use resample
{
/*
iterate through pixels - need to draw vertical line
corresponding to value range change for current pixel
*/
//only use points from visible range
double startPoint = xMap.s1()/m_dt;
if(startPoint>m_size)
startPoint = m_size;
else if(startPoint<0)
startPoint = 0;
double endPoint = xMap.s2()/m_dt;
if(endPoint>m_size)
endPoint = m_size;
else if(endPoint<0)
endPoint = 0;
double pointSize = endPoint - startPoint;
if ( pointSize <= 0.0 )
return;
//allocate memory
numberOfPlotPoints = pixels*2;
points = new QPointF[numberOfPlotPoints];
//iterate over pixels
int start = startPoint;
for(int pixel=0;pixel<pixels;++pixel)
{
int end = (((double)pixel+1.0)/pixels)*pointSize + startPoint;
if(end>endPoint)
end = endPoint;
//now find range [min;max] for current pixel
//using search algorithm for comparison optimization (3n/2 instead of 2n)
double min = 0.0;
double max = 0.0;
int minIndex = 0;
int maxIndex = 0;
if(m_data[start]<m_data[start+1])
{
min = m_data[start];
max = m_data[start+1];
minIndex = start;
maxIndex = start+1;
}
else
{
min = m_data[start+1];
max = m_data[start];
minIndex = start+1;
maxIndex = start;
}
//compare pairs
for(int k=start+2;k<end-2;k+=2)
{
if(m_data[k]>m_data[k+1])
{
if(m_data[k]>max)
{
max = m_data[k];
maxIndex = k;
}
if(m_data[k+1]<min)
{
min = m_data[k+1];
minIndex = k+1;
}
}
else
{
if(m_data[k+1]>max)
{
max = m_data[k+1];
maxIndex = k+1;
}
if(m_data[k]<min)
{
min = m_data[k];
minIndex = k;
}
}
}
//new start for next iteration
start = end;
double p1x = 0.0, p2x = 0.0, p1y = 0.0, p2y = 0.0;
if(minIndex<maxIndex)
{
//rising function, push points in direct order
p1x = xMap.transform(minIndex*m_dt);
p2x = xMap.transform(maxIndex*m_dt);
p1y = yMap.transform( min );
p2y = yMap.transform( max );
}
else
{
//falling function, push points in reverse order
p2x = xMap.transform(minIndex*m_dt);
p1x = xMap.transform(maxIndex*m_dt);
p2y = yMap.transform( min );
p1y = yMap.transform( max );
}
//add points to array
points[pixel*2+0].setX(p1x);
points[pixel*2+0].setY(p1y);
points[pixel*2+1].setX(p2x);
points[pixel*2+1].setY(p2y);
}
}
else //normal draw, not using resample
{
//only use points from visible range
quint32 startPoint = xMap.s1()/m_dt;
if(startPoint>m_size)
startPoint = m_size;
else if(startPoint<0)
startPoint = 0;
int endPoint = xMap.s2()/m_dt;
endPoint+=2;
if(endPoint>m_size)
endPoint = m_size;
int pointSize = endPoint - startPoint;
if ( pointSize <= 0 )
return;
//allocate array for points
numberOfPlotPoints = pointSize;
points = new QPointF[numberOfPlotPoints];
for ( int i = startPoint; i < endPoint; i++ )
{
double x = xMap.transform( i*m_dt );
double y = yMap.transform( m_data[i]);
points[i - startPoint].setX(x);
points[i - startPoint].setY(y);
}
}
//draw plot
painter->setPen(m_plotColor);
painter->drawPolyline(points, numberOfPlotPoints);
//free memory
delete points;
}
void QwtArrayPlotItem::setData(double* data, quint32 size, qreal dt)
{
if(!data)
{
qCritical()<<"QwtArrayPlotItem::setData: data == 0!";
return;
}
if(size<2)
{
qCritical()<<"QwtArrayPlotItem::setData: wrong size!";
return;
}
if(qFuzzyCompare(dt,0.0))
{
qCritical()<<"QwtArrayPlotItem::setData: dt == 0.0!";
return;
}
m_dt = dt;
m_size = size;
m_boundingRect = QRectF(0.0, 0.0, -1.0, -1.0);//set invalid to recalculate
m_data = data;
}
QRectF QwtArrayPlotItem::boundingRect() const
{
//if we have valid rect, return it
if( m_boundingRect.isValid())
return m_boundingRect;
//need to calculate
else if(m_data != 0 && m_size>1)
{
double min =0.0;
double max =0.0;
if(m_data[0]<m_data[1])
{
min = m_data[0];
max = m_data[1];
}
else
{
min = m_data[1];
max = m_data[0];
}
//compare pairs
for(int k=2;k<m_size-2;k+=2)
{
if(m_data[k]>m_data[k+1])
{
if(m_data[k]>max)
{
max = m_data[k];
}
if(m_data[k+1]<min)
{
min = m_data[k+1];
}
}
else
{
if(m_data[k+1]>max)
{
max = m_data[k+1];
}
if(m_data[k]<min)
{
min = m_data[k];
}
}
}
m_boundingRect = QRectF(0.0,(double)min, m_size*m_dt, (double)(max-min));
return m_boundingRect;
}
return QRectF( 1.0, 1.0, -2.0, -2.0 );
}