-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathqwt_scale_engine.cpp
More file actions
646 lines (512 loc) · 16.9 KB
/
qwt_scale_engine.cpp
File metadata and controls
646 lines (512 loc) · 16.9 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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
* Qwt Widget Library
* Copyright (C) 1997 Josef Wilgen
* Copyright (C) 2002 Uwe Rathmann
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the Qwt License, Version 1.0
*****************************************************************************/
#include "qwt_scale_engine.h"
#include "qwt_scale_map.h"
#include <qalgorithms.h>
#include <qmath.h>
static const double _eps = 1.0e-6;
/*!
Ceil a value, relative to an interval
\param value Value to ceil
\param intervalSize Interval size
\sa floorEps()
*/
double QwtScaleArithmetic::ceilEps( double value,
double intervalSize )
{
const double eps = _eps * intervalSize;
value = ( value - eps ) / intervalSize;
return qwtCeilF( value ) * intervalSize;
}
/*!
Floor a value, relative to an interval
\param value Value to floor
\param intervalSize Interval size
\sa floorEps()
*/
double QwtScaleArithmetic::floorEps( double value, double intervalSize )
{
const double eps = _eps * intervalSize;
value = ( value + eps ) / intervalSize;
return qwtFloorF( value ) * intervalSize;
}
/*!
\brief Divide an interval into steps
\f$stepSize = (intervalSize - intervalSize * 10e^{-6}) / numSteps\f$
\param intervalSize Interval size
\param numSteps Number of steps
\return Step size
*/
double QwtScaleArithmetic::divideEps( double intervalSize, double numSteps )
{
if ( numSteps == 0.0 || intervalSize == 0.0 )
return 0.0;
return ( intervalSize - ( _eps * intervalSize ) ) / numSteps;
}
/*!
Find the smallest value out of {1,2,5}*10^n with an integer number n
which is greater than or equal to x
\param x Input value
*/
double QwtScaleArithmetic::ceil125( double x )
{
if ( x == 0.0 )
return 0.0;
const double sign = ( x > 0 ) ? 1.0 : -1.0;
const double lx = ::log10( qFabs( x ) );
const double p10 = qwtFloorF( lx );
double fr = qPow( 10.0, lx - p10 );
if ( fr <= 1.0 )
fr = 1.0;
else if ( fr <= 2.0 )
fr = 2.0;
else if ( fr <= 5.0 )
fr = 5.0;
else
fr = 10.0;
return sign * fr * qPow( 10.0, p10 );
}
/*!
Calculate a step size for an interval size
\param intervalSize Interval size
\param numSteps Number of steps
\return Step size
*/
double QwtScaleEngine::divideInterval(
double intervalSize, int numSteps ) const
{
if ( numSteps <= 0 )
return 0.0;
double v = QwtScaleArithmetic::divideEps( intervalSize, numSteps );
return QwtScaleArithmetic::ceil125( v );
}
/*!
Check if an interval "contains" a value
\param interval Interval
\param value Value
\sa QwtScaleArithmetic::compareEps()
*/
bool QwtScaleEngine::contains(
const QwtInterval &interval, double value ) const
{
if ( !interval.isValid() )
return false;
if ( qwtFuzzyCompare( value, interval.minValue(), interval.width() ) < 0 )
return false;
if ( qwtFuzzyCompare( value, interval.maxValue(), interval.width() ) > 0 )
return false;
return true;
}
/*!
Remove ticks from a list, that are not inside an interval
\param ticks Tick list
\param interval Interval
\return Stripped tick list
*/
QList<double> QwtScaleEngine::strip( const QList<double>& ticks,
const QwtInterval &interval ) const
{
if ( !interval.isValid() || ticks.count() == 0 )
return QList<double>();
if ( contains( interval, ticks.first() )
&& contains( interval, ticks.last() ) )
{
return ticks;
}
QList<double> strippedTicks;
for ( int i = 0; i < ( int )ticks.count(); i++ )
{
if ( contains( interval, ticks[i] ) )
strippedTicks += ticks[i];
}
return strippedTicks;
}
/*!
\brief Build an interval for a value
In case of v == 0.0 the interval is [-0.5, 0.5],
otherwide it is [0.5 * v, 1.5 * v]
*/
QwtInterval QwtScaleEngine::buildInterval( double v ) const
{
const double delta = ( v == 0.0 ) ? 0.5 : fabs( 0.5 * v );
return QwtInterval( v - delta, v + delta );
}
/*!
Return a transformation, for linear scales
*/
QwtScaleTransformation *QwtLinearScaleEngine::transformation() const
{
return new QwtScaleTransformation( QwtScaleTransformation::Linear );
}
/*!
\brief Calculate a scale division
\param x1 First interval limit
\param x2 Second interval limit
\param maxMajSteps Maximum for the number of major steps
\param maxMinSteps Maximum number of minor steps
\param stepSize Step size. If stepSize == 0, the scaleEngine
calculates one.
\sa QwtScaleEngine::stepSize(), QwtScaleEngine::subDivide()
*/
QwtScaleDiv QwtLinearScaleEngine::divideScale( double x1, double x2,
int maxMajSteps, int maxMinSteps, double stepSize ) const
{
QwtInterval interval = QwtInterval( x1, x2 ).normalized();
if ( interval.width() <= 0 )
return QwtScaleDiv();
stepSize = fabs( stepSize );
if ( stepSize == 0.0 )
{
if ( maxMajSteps < 1 )
maxMajSteps = 1;
stepSize = divideInterval( interval.width(), maxMajSteps );
}
QwtScaleDiv scaleDiv;
if ( stepSize != 0.0 )
{
QList<double> ticks[QwtScaleDiv::NTickTypes];
buildTicks( interval, stepSize, maxMinSteps, ticks );
scaleDiv = QwtScaleDiv( interval, ticks );
}
if ( x1 > x2 )
scaleDiv.invert();
return scaleDiv;
}
/*!
\brief Calculate ticks for an interval
\param interval Interval
\param stepSize Step size
\param maxMinSteps Maximum number of minor steps
\param ticks Arrays to be filled with the calculated ticks
\sa buildMajorTicks(), buildMinorTicks
*/
void QwtLinearScaleEngine::buildTicks(
const QwtInterval& interval, double stepSize, int maxMinSteps,
QList<double> ticks[QwtScaleDiv::NTickTypes] ) const
{
const QwtInterval boundingInterval =
align( interval, stepSize );
ticks[QwtScaleDiv::MajorTick] =
buildMajorTicks( boundingInterval, stepSize );
if ( maxMinSteps > 0 )
{
buildMinorTicks( ticks[QwtScaleDiv::MajorTick], maxMinSteps, stepSize,
ticks[QwtScaleDiv::MinorTick], ticks[QwtScaleDiv::MediumTick] );
}
for ( int i = 0; i < QwtScaleDiv::NTickTypes; i++ )
{
ticks[i] = strip( ticks[i], interval );
// ticks very close to 0.0 are
// explicitely set to 0.0
for ( int j = 0; j < ( int )ticks[i].count(); j++ )
{
if ( qwtFuzzyCompare( ticks[i][j], 0.0, stepSize ) == 0 )
ticks[i][j] = 0.0;
}
}
}
/*!
\brief Calculate major ticks for an interval
\param interval Interval
\param stepSize Step size
\return Calculated ticks
*/
QList<double> QwtLinearScaleEngine::buildMajorTicks(
const QwtInterval &interval, double stepSize ) const
{
int numTicks = qRound( interval.width() / stepSize ) + 1;
if ( numTicks > 10000 )
numTicks = 10000;
QList<double> ticks;
ticks += interval.minValue();
for ( int i = 1; i < numTicks - 1; i++ )
ticks += interval.minValue() + i * stepSize;
ticks += interval.maxValue();
return ticks;
}
/*!
\brief Calculate minor/medium ticks for major ticks
\param majorTicks Major ticks
\param maxMinSteps Maximum number of minor steps
\param stepSize Step size
\param minorTicks Array to be filled with the calculated minor ticks
\param mediumTicks Array to be filled with the calculated medium ticks
*/
void QwtLinearScaleEngine::buildMinorTicks(
const QList<double>& majorTicks,
int maxMinSteps, double stepSize,
QList<double> &minorTicks,
QList<double> &mediumTicks ) const
{
double minStep = divideInterval( stepSize, maxMinSteps );
if ( minStep == 0.0 )
return;
// # ticks per interval
int numTicks = qCeil( fabs( stepSize / minStep ) ) - 1;
// Do the minor steps fit into the interval?
if ( qwtFuzzyCompare( ( numTicks + 1 ) * fabs( minStep ),
fabs( stepSize ), stepSize ) > 0 )
{
numTicks = 1;
minStep = stepSize * 0.5;
}
int medIndex = -1;
if ( numTicks % 2 )
medIndex = numTicks / 2;
// calculate minor ticks
for ( int i = 0; i < ( int )majorTicks.count(); i++ )
{
double val = majorTicks[i];
for ( int k = 0; k < numTicks; k++ )
{
val += minStep;
double alignedValue = val;
if ( qwtFuzzyCompare( val, 0.0, stepSize ) == 0 )
alignedValue = 0.0;
if ( k == medIndex )
mediumTicks += alignedValue;
else
minorTicks += alignedValue;
}
}
}
/*!
\brief Align an interval to a step size
The limits of an interval are aligned that both are integer
multiples of the step size.
\param interval Interval
\param stepSize Step size
\return Aligned interval
*/
QwtInterval QwtLinearScaleEngine::align(
const QwtInterval &interval, double stepSize ) const
{
double x1 = QwtScaleArithmetic::floorEps( interval.minValue(), stepSize );
if ( qwtFuzzyCompare( interval.minValue(), x1, stepSize ) == 0 )
x1 = interval.minValue();
double x2 = QwtScaleArithmetic::ceilEps( interval.maxValue(), stepSize );
if ( qwtFuzzyCompare( interval.maxValue(), x2, stepSize ) == 0 )
x2 = interval.maxValue();
return QwtInterval( x1, x2 );
}
/*!
Return a transformation, for logarithmic (base 10) scales
*/
QwtScaleTransformation *QwtLog10ScaleEngine::transformation() const
{
return new QwtScaleTransformation( QwtScaleTransformation::Log10 );
}
/*!
\brief Calculate a scale division
\param x1 First interval limit
\param x2 Second interval limit
\param maxMajSteps Maximum for the number of major steps
\param maxMinSteps Maximum number of minor steps
\param stepSize Step size. If stepSize == 0, the scaleEngine
calculates one.
\sa QwtScaleEngine::stepSize(), QwtLog10ScaleEngine::subDivide()
*/
QwtScaleDiv QwtLog10ScaleEngine::divideScale( double x1, double x2,
int maxMajSteps, int maxMinSteps, double stepSize ) const
{
QwtInterval interval = QwtInterval( x1, x2 ).normalized();
interval = interval.limited( LOG_MIN, LOG_MAX );
if ( interval.width() <= 0 )
return QwtScaleDiv();
if ( interval.maxValue() / interval.minValue() < 10.0 )
{
// scale width is less than one decade -> build linear scale
QwtLinearScaleEngine linearScaler;
if ( stepSize != 0.0 )
stepSize = qPow( 10.0, stepSize );
return linearScaler.divideScale( x1, x2,
maxMajSteps, maxMinSteps, stepSize );
}
stepSize = fabs( stepSize );
if ( stepSize == 0.0 )
{
if ( maxMajSteps < 1 )
maxMajSteps = 1;
stepSize = divideInterval( log10( interval ).width(), maxMajSteps );
if ( stepSize < 1.0 )
stepSize = 1.0; // major step must be >= 1 decade
}
QwtScaleDiv scaleDiv;
if ( stepSize != 0.0 )
{
QList<double> ticks[QwtScaleDiv::NTickTypes];
buildTicks( interval, stepSize, maxMinSteps, ticks );
scaleDiv = QwtScaleDiv( interval, ticks );
}
if ( x1 > x2 )
scaleDiv.invert();
return scaleDiv;
}
/*!
\brief Calculate ticks for an interval
\param interval Interval
\param maxMinSteps Maximum number of minor steps
\param stepSize Step size
\param ticks Arrays to be filled with the calculated ticks
\sa buildMajorTicks(), buildMinorTicks
*/
void QwtLog10ScaleEngine::buildTicks(
const QwtInterval& interval, double stepSize, int maxMinSteps,
QList<double> ticks[QwtScaleDiv::NTickTypes] ) const
{
const QwtInterval boundingInterval = align( interval, stepSize );
ticks[QwtScaleDiv::MajorTick] =
buildMajorTicks( boundingInterval, stepSize );
if ( maxMinSteps > 0 )
{
ticks[QwtScaleDiv::MinorTick] = buildMinorTicks(
ticks[QwtScaleDiv::MajorTick], maxMinSteps, stepSize );
}
for ( int i = 0; i < QwtScaleDiv::NTickTypes; i++ )
ticks[i] = strip( ticks[i], interval );
}
/*!
\brief Calculate major ticks for an interval
\param interval Interval
\param stepSize Step size
\return Calculated ticks
*/
QList<double> QwtLog10ScaleEngine::buildMajorTicks(
const QwtInterval &interval, double stepSize ) const
{
double width = log10( interval ).width();
int numTicks = qRound( width / stepSize ) + 1;
if ( numTicks > 10000 )
numTicks = 10000;
const double lxmin = ::log( interval.minValue() );
const double lxmax = ::log( interval.maxValue() );
const double lstep = ( lxmax - lxmin ) / double( numTicks - 1 );
QList<double> ticks;
ticks += interval.minValue();
for ( int i = 1; i < numTicks - 1; i++ )
ticks += qExp( lxmin + double( i ) * lstep );
ticks += interval.maxValue();
return ticks;
}
/*!
\brief Calculate minor/medium ticks for major ticks
\param majorTicks Major ticks
\param maxMinSteps Maximum number of minor steps
\param stepSize Step size
*/
QList<double> QwtLog10ScaleEngine::buildMinorTicks(
const QList<double> &majorTicks,
int maxMinSteps, double stepSize ) const
{
if ( stepSize < 1.1 ) // major step width is one decade
{
if ( maxMinSteps < 1 )
return QList<double>();
int k0, kstep, kmax;
if ( maxMinSteps >= 8 )
{
k0 = 2;
kmax = 9;
kstep = 1;
}
else if ( maxMinSteps >= 4 )
{
k0 = 2;
kmax = 8;
kstep = 2;
}
else if ( maxMinSteps >= 2 )
{
k0 = 2;
kmax = 5;
kstep = 3;
}
else
{
k0 = 5;
kmax = 5;
kstep = 1;
}
QList<double> minorTicks;
for ( int i = 0; i < ( int )majorTicks.count(); i++ )
{
const double v = majorTicks[i];
for ( int k = k0; k <= kmax; k += kstep )
minorTicks += v * double( k );
}
return minorTicks;
}
else // major step > one decade
{
double minStep = divideInterval( stepSize, maxMinSteps );
if ( minStep == 0.0 )
return QList<double>();
if ( minStep < 1.0 )
minStep = 1.0;
// # subticks per interval
int nMin = qRound( stepSize / minStep ) - 1;
// Do the minor steps fit into the interval?
if ( qwtFuzzyCompare( ( nMin + 1 ) * minStep,
fabs( stepSize ), stepSize ) > 0 )
{
nMin = 0;
}
if ( nMin < 1 )
return QList<double>(); // no subticks
// substep factor = 10^substeps
const qreal minFactor = qMax( qPow( 10.0, minStep ), qreal( 10.0 ) );
QList<double> minorTicks;
for ( int i = 0; i < ( int )majorTicks.count(); i++ )
{
double val = majorTicks[i];
for ( int k = 0; k < nMin; k++ )
{
val *= minFactor;
minorTicks += val;
}
}
return minorTicks;
}
}
/*!
\brief Align an interval to a step size
The limits of an interval are aligned that both are integer
multiples of the step size.
\param interval Interval
\param stepSize Step size
\return Aligned interval
*/
QwtInterval QwtLog10ScaleEngine::align(
const QwtInterval &interval, double stepSize ) const
{
const QwtInterval intv = log10( interval );
double x1 = QwtScaleArithmetic::floorEps( intv.minValue(), stepSize );
if ( qwtFuzzyCompare( interval.minValue(), x1, stepSize ) == 0 )
x1 = interval.minValue();
double x2 = QwtScaleArithmetic::ceilEps( intv.maxValue(), stepSize );
if ( qwtFuzzyCompare( interval.maxValue(), x2, stepSize ) == 0 )
x2 = interval.maxValue();
return pow10( QwtInterval( x1, x2 ) );
}
/*!
Return the interval [log10(interval.minValue(), log10(interval.maxValue]
*/
QwtInterval QwtLog10ScaleEngine::log10( const QwtInterval &interval ) const
{
return QwtInterval( ::log10( interval.minValue() ),
::log10( interval.maxValue() ) );
}
/*!
Return the interval [pow10(interval.minValue(), pow10(interval.maxValue]
*/
QwtInterval QwtLog10ScaleEngine::pow10( const QwtInterval &interval ) const
{
return QwtInterval( qPow( 10.0, interval.minValue() ),
qPow( 10.0, interval.maxValue() ) );
}