forked from miek/inspectrum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
traceplot.cpp
136 lines (117 loc) · 4.63 KB
/
traceplot.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
/*
* Copyright (C) 2016, Mike Walters <[email protected]>
*
* This file is part of inspectrum.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QPixmapCache>
#include <QTextStream>
#include <QtConcurrent>
#include "samplesource.h"
#include "traceplot.h"
TracePlot::TracePlot(std::shared_ptr<AbstractSampleSource> source) : Plot(source) {
connect(this, &TracePlot::imageReady, this, &TracePlot::handleImage);
}
void TracePlot::paintMid(QPainter &painter, QRect &rect, range_t<off_t> sampleRange)
{
int samplesPerColumn = sampleRange.length() / rect.width();
int samplesPerTile = tileWidth * samplesPerColumn;
off_t tileID = sampleRange.minimum / samplesPerTile;
off_t tileOffset = sampleRange.minimum % samplesPerTile; // Number of samples to skip from first image tile
int xOffset = tileOffset / samplesPerColumn; // Number of columns to skip from first image tile
// Paint first (possibly partial) tile
painter.drawPixmap(
QRect(rect.x(), rect.y(), tileWidth - xOffset, height()),
getTile(tileID++, samplesPerTile),
QRect(xOffset, 0, tileWidth - xOffset, height())
);
// Paint remaining tiles
for (int x = tileWidth - xOffset; x < rect.right(); x += tileWidth) {
painter.drawPixmap(
QRect(x, rect.y(), tileWidth, height()),
getTile(tileID++, samplesPerTile)
);
}
}
QPixmap TracePlot::getTile(off_t tileID, off_t sampleCount)
{
QPixmap pixmap(tileWidth, height());
QString key;
QTextStream(&key) << "traceplot_" << this << "_" << tileID << "_" << sampleCount;
if (QPixmapCache::find(key, &pixmap))
return pixmap;
if (!tasks.contains(key)) {
range_t<off_t> sampleRange{tileID * sampleCount, (tileID + 1) * sampleCount};
QtConcurrent::run(this, &TracePlot::drawTile, key, QRect(0, 0, tileWidth, height()), sampleRange);
tasks.insert(key);
}
pixmap.fill(Qt::transparent);
return pixmap;
}
void TracePlot::drawTile(QString key, const QRect &rect, range_t<off_t> sampleRange)
{
QImage image(rect.size(), QImage::Format_ARGB32);
image.fill(Qt::transparent);
QPainter painter(&image);
painter.setRenderHint(QPainter::Antialiasing, true);
auto firstSample = sampleRange.minimum;
auto length = sampleRange.length();
// Is it a 2-channel (complex) trace?
if (auto src = dynamic_cast<SampleSource<std::complex<float>>*>(sampleSource.get())) {
auto samples = src->getSamples(firstSample, length);
if (samples == nullptr)
return;
painter.setPen(Qt::red);
plotTrace(painter, rect, reinterpret_cast<float*>(samples.get()), length, 2);
painter.setPen(Qt::blue);
plotTrace(painter, rect, reinterpret_cast<float*>(samples.get())+1, length, 2);
// Otherwise is it single channel?
} else if (auto src = dynamic_cast<SampleSource<float>*>(sampleSource.get())) {
auto samples = src->getSamples(firstSample, length);
if (samples == nullptr)
return;
painter.setPen(Qt::green);
plotTrace(painter, rect, samples.get(), length, 1);
} else {
throw std::runtime_error("TracePlot::paintMid: Unsupported source type");
}
emit imageReady(key, image);
}
void TracePlot::handleImage(QString key, QImage image)
{
auto pixmap = QPixmap::fromImage(image);
QPixmapCache::insert(key, pixmap);
tasks.remove(key);
emit repaint();
}
void TracePlot::plotTrace(QPainter &painter, const QRect &rect, float *samples, off_t count, int step = 1)
{
QPainterPath path;
range_t<float> xRange{0, rect.width() - 2.f};
range_t<float> yRange{0, rect.height() - 2.f};
const float xStep = 1.0 / count * rect.width();
for (off_t i = 0; i < count; i++) {
float sample = samples[i*step];
float x = i * xStep;
float y = (1 - sample) * (rect.height() / 2);
x = xRange.clip(x) + rect.x();
y = yRange.clip(y) + rect.y();
if (i == 0)
path.moveTo(x, y);
else
path.lineTo(x, y);
}
painter.drawPath(path);
}