forked from OpenModelica/OpenModelica
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qwt_point_mapper.cpp
969 lines (793 loc) · 24.7 KB
/
qwt_point_mapper.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
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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
/******************************************************************************
* 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_point_mapper.h"
#include "qwt_scale_map.h"
#include "qwt_pixel_matrix.h"
#include "qwt_series_data.h"
#include "qwt_math.h"
#include <qpolygon.h>
#include <qimage.h>
#include <qpen.h>
#include <qpainter.h>
#include <qthread.h>
#include <qfuture.h>
#include <qtconcurrentrun.h>
#if !defined( QT_NO_QFUTURE )
#define QWT_USE_THREADS 1
#endif
static QRectF qwtInvalidRect( 0.0, 0.0, -1.0, -1.0 );
static inline int qwtRoundValue( double value )
{
return qRound( value );
}
static inline double qwtRoundValueF( double value )
{
#if 1
// MS Windows and at least IRIX does not have C99's nearbyint() function
return ( value >= 0.0 ) ? std::floor( value + 0.5 ) : std::ceil( value - 0.5 );
#else
return nearbyint( value );
#endif
}
static Qt::Orientation qwtProbeOrientation(
const QwtSeriesData< QPointF >* series, int from, int to )
{
if ( to - from < 20 )
{
// not enough points to "have an orientation"
return Qt::Horizontal;
}
const double x0 = series->sample( from ).x();
const double xn = series->sample( to ).x();
if ( x0 == xn )
return Qt::Vertical;
const int step = ( to - from ) / 10;
const bool isIncreasing = xn > x0;
double x1 = x0;
for ( int i = from + step; i < to; i += step )
{
const double x2 = series->sample( i ).x();
if ( x2 != x1 )
{
if ( ( x2 > x1 ) != isIncreasing )
return Qt::Vertical;
}
x1 = x2;
}
return Qt::Horizontal;
}
namespace
{
template< class Polygon, class Point >
class QwtPolygonQuadrupelX
{
public:
inline void start( int x, int y )
{
x0 = x;
y1 = yMin = yMax = y2 = y;
}
inline bool append( int x, int y )
{
if ( x0 != x )
return false;
if ( y < yMin )
yMin = y;
else if ( y > yMax )
yMax = y;
y2 = y;
return true;
}
inline void flush( Polygon& polyline )
{
appendTo( y1, polyline );
if ( y2 > y1 )
qSwap( yMin, yMax );
if ( yMax != y1 )
appendTo( yMax, polyline );
if ( yMin != yMax )
appendTo( yMin, polyline );
if ( y2 != yMin )
appendTo( y2, polyline );
}
private:
inline void appendTo( int y, Polygon& polyline )
{
polyline += Point( x0, y );
}
private:
int x0, y1, yMin, yMax, y2;
};
template< class Polygon, class Point >
class QwtPolygonQuadrupelY
{
public:
inline void start( int x, int y )
{
y0 = y;
x1 = xMin = xMax = x2 = x;
}
inline bool append( int x, int y )
{
if ( y0 != y )
return false;
if ( x < xMin )
xMin = x;
else if ( x > xMax )
xMax = x;
x2 = x;
return true;
}
inline void flush( Polygon& polyline )
{
appendTo( x1, polyline );
if ( x2 > x1 )
qSwap( xMin, xMax );
if ( xMax != x1 )
appendTo( xMax, polyline );
if ( xMin != xMax )
appendTo( xMin, polyline );
if ( x2 != xMin )
appendTo( x2, polyline );
}
private:
inline void appendTo( int x, Polygon& polyline )
{
polyline += Point( x, y0 );
}
int y0, x1, xMin, xMax, x2;
};
}
template< class Polygon, class Point, class PolygonQuadrupel >
static Polygon qwtMapPointsQuad( const QwtScaleMap& xMap, const QwtScaleMap& yMap,
const QwtSeriesData< QPointF >* series, int from, int to )
{
const QPointF sample0 = series->sample( from );
PolygonQuadrupel q;
q.start( qwtRoundValue( xMap.transform( sample0.x() ) ),
qwtRoundValue( yMap.transform( sample0.y() ) ) );
Polygon polyline;
for ( int i = from; i <= to; i++ )
{
const QPointF sample = series->sample( i );
const int x = qwtRoundValue( xMap.transform( sample.x() ) );
const int y = qwtRoundValue( yMap.transform( sample.y() ) );
if ( !q.append( x, y ) )
{
q.flush( polyline );
q.start( x, y );
}
}
q.flush( polyline );
return polyline;
}
template< class Polygon, class Point, class PolygonQuadrupel >
static Polygon qwtMapPointsQuad( const Polygon& polyline )
{
const int numPoints = polyline.size();
if ( numPoints < 3 )
return polyline;
const Point* points = polyline.constData();
Polygon polylineXY;
PolygonQuadrupel q;
q.start( points[0].x(), points[0].y() );
for ( int i = 0; i < numPoints; i++ )
{
const int x = points[i].x();
const int y = points[i].y();
if ( !q.append( x, y ) )
{
q.flush( polylineXY );
q.start( x, y );
}
}
q.flush( polylineXY );
return polylineXY;
}
template< class Polygon, class Point >
static Polygon qwtMapPointsQuad( const QwtScaleMap& xMap, const QwtScaleMap& yMap,
const QwtSeriesData< QPointF >* series, int from, int to )
{
Polygon polyline;
if ( from > to )
return polyline;
/*
probing some values, to decide if it is better
to start with x or y coordinates
*/
const Qt::Orientation orientation = qwtProbeOrientation( series, from, to );
if ( orientation == Qt::Horizontal )
{
polyline = qwtMapPointsQuad< Polygon, Point,
QwtPolygonQuadrupelY< Polygon, Point > >( xMap, yMap, series, from, to );
polyline = qwtMapPointsQuad< Polygon, Point,
QwtPolygonQuadrupelX< Polygon, Point > >( polyline );
}
else
{
polyline = qwtMapPointsQuad< Polygon, Point,
QwtPolygonQuadrupelX< Polygon, Point > >( xMap, yMap, series, from, to );
polyline = qwtMapPointsQuad< Polygon, Point,
QwtPolygonQuadrupelY< Polygon, Point > >( polyline );
}
return polyline;
}
// Helper class to work around the 5 parameters
// limitation of QtConcurrent::run()
class QwtDotsCommand
{
public:
const QwtSeriesData< QPointF >* series;
int from;
int to;
QRgb rgb;
};
static void qwtRenderDots(
const QwtScaleMap& xMap, const QwtScaleMap& yMap,
const QwtDotsCommand& command, const QPoint& pos, QImage* image )
{
const QRgb rgb = command.rgb;
QRgb* bits = reinterpret_cast< QRgb* >( image->bits() );
const int w = image->width();
const int h = image->height();
const int x0 = pos.x();
const int y0 = pos.y();
for ( int i = command.from; i <= command.to; i++ )
{
const QPointF sample = command.series->sample( i );
const int x = static_cast< int >( xMap.transform( sample.x() ) + 0.5 ) - x0;
const int y = static_cast< int >( yMap.transform( sample.y() ) + 0.5 ) - y0;
if ( x >= 0 && x < w && y >= 0 && y < h )
bits[ y * w + x ] = rgb;
}
}
// some functors, so that the compile can inline
struct QwtRoundI
{
inline int operator()( double value ) const
{
return qwtRoundValue( value );
}
};
struct QwtRoundF
{
inline double operator()( double value ) const
{
return qwtRoundValueF( value );
}
};
struct QwtNoRoundF
{
inline double operator()( double value ) const
{
return value;
}
};
// mapping points without any filtering - beside checking
// the bounding rectangle
template< class Polygon, class Point, class Round >
static inline Polygon qwtToPoints(
const QRectF& boundingRect,
const QwtScaleMap& xMap, const QwtScaleMap& yMap,
const QwtSeriesData< QPointF >* series,
int from, int to, Round round )
{
Polygon polyline( to - from + 1 );
Point* points = polyline.data();
int numPoints = 0;
if ( boundingRect.isValid() )
{
// iterating over all values
// filtering out all points outside of
// the bounding rectangle
for ( int i = from; i <= to; i++ )
{
const QPointF sample = series->sample( i );
const double x = xMap.transform( sample.x() );
const double y = yMap.transform( sample.y() );
if ( boundingRect.contains( x, y ) )
{
points[ numPoints ].rx() = round( x );
points[ numPoints ].ry() = round( y );
numPoints++;
}
}
polyline.resize( numPoints );
}
else
{
// simply iterating over all values
// without any filtering
for ( int i = from; i <= to; i++ )
{
const QPointF sample = series->sample( i );
const double x = xMap.transform( sample.x() );
const double y = yMap.transform( sample.y() );
points[ numPoints ].rx() = round( x );
points[ numPoints ].ry() = round( y );
numPoints++;
}
}
return polyline;
}
static inline QPolygon qwtToPointsI(
const QRectF& boundingRect,
const QwtScaleMap& xMap, const QwtScaleMap& yMap,
const QwtSeriesData< QPointF >* series,
int from, int to )
{
return qwtToPoints< QPolygon, QPoint >(
boundingRect, xMap, yMap, series, from, to, QwtRoundI() );
}
template< class Round >
static inline QPolygonF qwtToPointsF(
const QRectF& boundingRect,
const QwtScaleMap& xMap, const QwtScaleMap& yMap,
const QwtSeriesData< QPointF >* series,
int from, int to, Round round )
{
return qwtToPoints< QPolygonF, QPointF >(
boundingRect, xMap, yMap, series, from, to, round );
}
// Mapping points with filtering out consecutive
// points mapped to the same position
template< class Polygon, class Point, class Round >
static inline Polygon qwtToPolylineFiltered(
const QwtScaleMap& xMap, const QwtScaleMap& yMap,
const QwtSeriesData< QPointF >* series,
int from, int to, Round round )
{
// in curves with many points consecutive points
// are often mapped to the same position. As this might
// result in empty lines ( or symbols hidden by others )
// we try to filter them out
Polygon polyline( to - from + 1 );
Point* points = polyline.data();
const QPointF sample0 = series->sample( from );
points[0].rx() = round( xMap.transform( sample0.x() ) );
points[0].ry() = round( yMap.transform( sample0.y() ) );
int pos = 0;
for ( int i = from + 1; i <= to; i++ )
{
const QPointF sample = series->sample( i );
const Point p( round( xMap.transform( sample.x() ) ),
round( yMap.transform( sample.y() ) ) );
if ( points[pos] != p )
points[++pos] = p;
}
polyline.resize( pos + 1 );
return polyline;
}
static inline QPolygon qwtToPolylineFilteredI(
const QwtScaleMap& xMap, const QwtScaleMap& yMap,
const QwtSeriesData< QPointF >* series,
int from, int to )
{
return qwtToPolylineFiltered< QPolygon, QPoint >(
xMap, yMap, series, from, to, QwtRoundI() );
}
template< class Round >
static inline QPolygonF qwtToPolylineFilteredF(
const QwtScaleMap& xMap, const QwtScaleMap& yMap,
const QwtSeriesData< QPointF >* series,
int from, int to, Round round )
{
return qwtToPolylineFiltered< QPolygonF, QPointF >(
xMap, yMap, series, from, to, round );
}
template< class Polygon, class Point >
static inline Polygon qwtToPointsFiltered(
const QRectF& boundingRect,
const QwtScaleMap& xMap, const QwtScaleMap& yMap,
const QwtSeriesData< QPointF >* series, int from, int to )
{
// F.e. in scatter plots ( no connecting lines ) we
// can sort out all duplicates ( not only consecutive points )
Polygon polygon( to - from + 1 );
Point* points = polygon.data();
QwtPixelMatrix pixelMatrix( boundingRect.toAlignedRect() );
int numPoints = 0;
for ( int i = from; i <= to; i++ )
{
const QPointF sample = series->sample( i );
const int x = qwtRoundValue( xMap.transform( sample.x() ) );
const int y = qwtRoundValue( yMap.transform( sample.y() ) );
if ( pixelMatrix.testAndSetPixel( x, y, true ) == false )
{
points[ numPoints ].rx() = x;
points[ numPoints ].ry() = y;
numPoints++;
}
}
polygon.resize( numPoints );
return polygon;
}
static inline QPolygon qwtToPointsFilteredI(
const QRectF& boundingRect,
const QwtScaleMap& xMap, const QwtScaleMap& yMap,
const QwtSeriesData< QPointF >* series, int from, int to )
{
return qwtToPointsFiltered< QPolygon, QPoint >(
boundingRect, xMap, yMap, series, from, to );
}
static inline QPolygonF qwtToPointsFilteredF(
const QRectF& boundingRect,
const QwtScaleMap& xMap, const QwtScaleMap& yMap,
const QwtSeriesData< QPointF >* series, int from, int to )
{
return qwtToPointsFiltered< QPolygonF, QPointF >(
boundingRect, xMap, yMap, series, from, to );
}
class QwtPointMapper::PrivateData
{
public:
PrivateData()
: boundingRect( qwtInvalidRect )
{
}
QRectF boundingRect;
QwtPointMapper::TransformationFlags flags;
};
//! Constructor
QwtPointMapper::QwtPointMapper()
{
m_data = new PrivateData();
}
//! Destructor
QwtPointMapper::~QwtPointMapper()
{
delete m_data;
}
/*!
Set the flags affecting the transformation process
\param flags Flags
\sa flags(), setFlag()
*/
void QwtPointMapper::setFlags( TransformationFlags flags )
{
m_data->flags = flags;
}
/*!
\return Flags affecting the transformation process
\sa setFlags(), setFlag()
*/
QwtPointMapper::TransformationFlags QwtPointMapper::flags() const
{
return m_data->flags;
}
/*!
Modify a flag affecting the transformation process
\param flag Flag type
\param on Value
\sa flag(), setFlags()
*/
void QwtPointMapper::setFlag( TransformationFlag flag, bool on )
{
if ( on )
m_data->flags |= flag;
else
m_data->flags &= ~flag;
}
/*!
\return True, when the flag is set
\param flag Flag type
\sa setFlag(), setFlags()
*/
bool QwtPointMapper::testFlag( TransformationFlag flag ) const
{
return m_data->flags & flag;
}
/*!
Set a bounding rectangle for the point mapping algorithm
A valid bounding rectangle can be used for optimizations
\param rect Bounding rectangle
\sa boundingRect()
*/
void QwtPointMapper::setBoundingRect( const QRectF& rect )
{
m_data->boundingRect = rect;
}
/*!
\return Bounding rectangle
\sa setBoundingRect()
*/
QRectF QwtPointMapper::boundingRect() const
{
return m_data->boundingRect;
}
/*!
\brief Translate a series of points into a QPolygonF
When the WeedOutPoints flag is enabled consecutive points,
that are mapped to the same position will be one point.
When RoundPoints is set all points are rounded to integers
but returned as PolygonF - what only makes sense
when the further processing of the values need a QPolygonF.
When RoundPoints & WeedOutIntermediatePoints is enabled an even more
aggressive weeding algorithm is enabled.
\param xMap x map
\param yMap y map
\param series Series of points to be mapped
\param from Index of the first point to be painted
\param to Index of the last point to be painted
\return Translated polygon
*/
QPolygonF QwtPointMapper::toPolygonF(
const QwtScaleMap& xMap, const QwtScaleMap& yMap,
const QwtSeriesData< QPointF >* series, int from, int to ) const
{
QPolygonF polyline;
if ( m_data->flags & RoundPoints )
{
if ( m_data->flags & WeedOutIntermediatePoints )
{
polyline = qwtMapPointsQuad< QPolygonF, QPointF >(
xMap, yMap, series, from, to );
}
else if ( m_data->flags & WeedOutPoints )
{
polyline = qwtToPolylineFilteredF(
xMap, yMap, series, from, to, QwtRoundF() );
}
else
{
polyline = qwtToPointsF( qwtInvalidRect,
xMap, yMap, series, from, to, QwtRoundF() );
}
}
else
{
if ( m_data->flags & WeedOutPoints )
{
polyline = qwtToPolylineFilteredF(
xMap, yMap, series, from, to, QwtNoRoundF() );
}
else
{
polyline = qwtToPointsF( qwtInvalidRect,
xMap, yMap, series, from, to, QwtNoRoundF() );
}
}
return polyline;
}
/*!
\brief Translate a series of points into a QPolygon
When the WeedOutPoints flag is enabled consecutive points,
that are mapped to the same position will be one point.
\param xMap x map
\param yMap y map
\param series Series of points to be mapped
\param from Index of the first point to be painted
\param to Index of the last point to be painted
\return Translated polygon
*/
QPolygon QwtPointMapper::toPolygon(
const QwtScaleMap& xMap, const QwtScaleMap& yMap,
const QwtSeriesData< QPointF >* series, int from, int to ) const
{
QPolygon polyline;
if ( m_data->flags & WeedOutIntermediatePoints )
{
// TODO WeedOutIntermediatePointsY ...
polyline = qwtMapPointsQuad< QPolygon, QPoint >(
xMap, yMap, series, from, to );
}
else if ( m_data->flags & WeedOutPoints )
{
polyline = qwtToPolylineFilteredI(
xMap, yMap, series, from, to );
}
else
{
polyline = qwtToPointsI(
qwtInvalidRect, xMap, yMap, series, from, to );
}
return polyline;
}
/*!
\brief Translate a series into a QPolygonF
- WeedOutPoints & RoundPoints & boundingRect().isValid()
All points that are mapped to the same position
will be one point. Points outside of the bounding
rectangle are ignored.
- WeedOutPoints & RoundPoints & !boundingRect().isValid()
All consecutive points that are mapped to the same position
will one point
- WeedOutPoints & !RoundPoints
All consecutive points that are mapped to the same position
will one point
- !WeedOutPoints & boundingRect().isValid()
Points outside of the bounding rectangle are ignored.
When RoundPoints is set all points are rounded to integers
but returned as PolygonF - what only makes sense
when the further processing of the values need a QPolygonF.
\param xMap x map
\param yMap y map
\param series Series of points to be mapped
\param from Index of the first point to be painted
\param to Index of the last point to be painted
\return Translated polygon
*/
QPolygonF QwtPointMapper::toPointsF(
const QwtScaleMap& xMap, const QwtScaleMap& yMap,
const QwtSeriesData< QPointF >* series, int from, int to ) const
{
QPolygonF points;
if ( m_data->flags & WeedOutPoints )
{
if ( m_data->flags & RoundPoints )
{
if ( m_data->boundingRect.isValid() )
{
points = qwtToPointsFilteredF( m_data->boundingRect,
xMap, yMap, series, from, to );
}
else
{
// without a bounding rectangle all we can
// do is to filter out duplicates of
// consecutive points
points = qwtToPolylineFilteredF(
xMap, yMap, series, from, to, QwtRoundF() );
}
}
else
{
// when rounding is not allowed we can't use
// qwtToPointsFilteredF
points = qwtToPolylineFilteredF(
xMap, yMap, series, from, to, QwtNoRoundF() );
}
}
else
{
if ( m_data->flags & RoundPoints )
{
points = qwtToPointsF( m_data->boundingRect,
xMap, yMap, series, from, to, QwtRoundF() );
}
else
{
points = qwtToPointsF( m_data->boundingRect,
xMap, yMap, series, from, to, QwtNoRoundF() );
}
}
return points;
}
/*!
\brief Translate a series of points into a QPolygon
- WeedOutPoints & boundingRect().isValid()
All points that are mapped to the same position
will be one point. Points outside of the bounding
rectangle are ignored.
- WeedOutPoints & !boundingRect().isValid()
All consecutive points that are mapped to the same position
will one point
- !WeedOutPoints & boundingRect().isValid()
Points outside of the bounding rectangle are ignored.
\param xMap x map
\param yMap y map
\param series Series of points to be mapped
\param from Index of the first point to be painted
\param to Index of the last point to be painted
\return Translated polygon
*/
QPolygon QwtPointMapper::toPoints(
const QwtScaleMap& xMap, const QwtScaleMap& yMap,
const QwtSeriesData< QPointF >* series, int from, int to ) const
{
QPolygon points;
if ( m_data->flags & WeedOutPoints )
{
if ( m_data->boundingRect.isValid() )
{
points = qwtToPointsFilteredI( m_data->boundingRect,
xMap, yMap, series, from, to );
}
else
{
// when we don't have the bounding rectangle all
// we can do is to filter out consecutive duplicates
points = qwtToPolylineFilteredI(
xMap, yMap, series, from, to );
}
}
else
{
points = qwtToPointsI(
m_data->boundingRect, xMap, yMap, series, from, to );
}
return points;
}
/*!
\brief Translate a series into a QImage
\param xMap x map
\param yMap y map
\param series Series of points to be mapped
\param from Index of the first point to be painted
\param to Index of the last point to be painted
\param pen Pen used for drawing a point
of the image, where a point is mapped to
\param antialiased True, when the dots should be displayed
antialiased
\param numThreads Number of threads to be used for rendering.
If numThreads is set to 0, the system specific
ideal thread count is used.
\return Image displaying the series
*/
QImage QwtPointMapper::toImage(
const QwtScaleMap& xMap, const QwtScaleMap& yMap,
const QwtSeriesData< QPointF >* series, int from, int to,
const QPen& pen, bool antialiased, uint numThreads ) const
{
Q_UNUSED( antialiased )
#if QWT_USE_THREADS
if ( numThreads == 0 )
numThreads = QThread::idealThreadCount();
if ( numThreads <= 0 )
numThreads = 1;
#else
Q_UNUSED( numThreads )
#endif
// a very special optimization for scatter plots
// where every sample is mapped to one pixel only.
const QRect rect = m_data->boundingRect.toAlignedRect();
QImage image( rect.size(), QImage::Format_ARGB32 );
image.fill( Qt::transparent );
if ( pen.width() <= 1 && pen.color().alpha() == 255 )
{
QwtDotsCommand command;
command.series = series;
command.rgb = pen.color().rgba();
#if QWT_USE_THREADS
const int numPoints = ( to - from + 1 ) / numThreads;
QList< QFuture< void > > futures;
for ( uint i = 0; i < numThreads; i++ )
{
const QPoint pos = rect.topLeft();
const int index0 = from + i * numPoints;
if ( i == numThreads - 1 )
{
command.from = index0;
command.to = to;
qwtRenderDots( xMap, yMap, command, pos, &image );
}
else
{
command.from = index0;
command.to = index0 + numPoints - 1;
futures += QtConcurrent::run( &qwtRenderDots,
xMap, yMap, command, pos, &image );
}
}
for ( int i = 0; i < futures.size(); i++ )
futures[i].waitForFinished();
#else
command.from = from;
command.to = to;
qwtRenderDots( xMap, yMap, command, rect.topLeft(), &image );
#endif
}
else
{
// fallback implementation: to be replaced later by
// setting the pixels of the image like above, TODO ...
QPainter painter( &image );
painter.setPen( pen );
painter.setRenderHint( QPainter::Antialiasing, antialiased );
const int chunkSize = 1000;
for ( int i = from; i <= to; i += chunkSize )
{
const int indexTo = qMin( i + chunkSize - 1, to );
const QPolygon points = toPoints(
xMap, yMap, series, i, indexTo );
painter.drawPoints( points );
}
}
return image;
}