Debian Codegraph @

src/traceplot.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2016, Mike Walters <[email protected]>
3 *
4 * This file is part of inspectrum.
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <QPixmapCache>
21 #include <QTextStream>
22 #include <QtConcurrent>
23 #include <QPainterPath>
24 #include "samplesource.h"
25 #include "traceplot.h"
26
27 TracePlot::TracePlot(std::shared_ptr<AbstractSampleSource> source) : Plot(source) {
28 connect(this, &TracePlot::imageReady, this, &TracePlot::handleImage);
29 }
30
31 void TracePlot::paintMid(QPainter &painter, QRect &rect, range_t<size_t> sampleRange)
32 {
33 if (sampleRange.length() == 0) return;
34
35 int samplesPerColumn = std::max(1UL, sampleRange.length() / rect.width());
36 int samplesPerTile = tileWidth * samplesPerColumn;
37 size_t tileID = sampleRange.minimum / samplesPerTile;
38 size_t tileOffset = sampleRange.minimum % samplesPerTile; // Number of samples to skip from first image tile
39 int xOffset = tileOffset / samplesPerColumn; // Number of columns to skip from first image tile
40
41 // Paint first (possibly partial) tile
42 painter.drawPixmap(
43 QRect(rect.x(), rect.y(), tileWidth - xOffset, height()),
44 getTile(tileID++, samplesPerTile),
45 QRect(xOffset, 0, tileWidth - xOffset, height())
46 );
47
48 // Paint remaining tiles
49 for (int x = tileWidth - xOffset; x < rect.right(); x += tileWidth) {
50 painter.drawPixmap(
51 QRect(x, rect.y(), tileWidth, height()),
52 getTile(tileID++, samplesPerTile)
53 );
54 }
55 }
56
57 QPixmap TracePlot::getTile(size_t tileID, size_t sampleCount)
58 {
59 QPixmap pixmap(tileWidth, height());
60 QString key;
61 QTextStream(&key) << "traceplot_" << this << "_" << tileID << "_" << sampleCount;
62 if (QPixmapCache::find(key, &pixmap))
63 return pixmap;
64
65 if (!tasks.contains(key)) {
66 range_t<size_t> sampleRange{tileID * sampleCount, (tileID + 1) * sampleCount};
67 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
68 QtConcurrent::run(&TracePlot::drawTile, this, key, QRect(0, 0, tileWidth, height()), sampleRange);
69 #else
70 QtConcurrent::run(this, &TracePlot::drawTile, key, QRect(0, 0, tileWidth, height()), sampleRange);
71 #endif
72 tasks.insert(key);
73 }
74 pixmap.fill(Qt::transparent);
75 return pixmap;
76 }
77
78 void TracePlot::drawTile(QString key, const QRect &rect, range_t<size_t> sampleRange)
79 {
80 QImage image(rect.size(), QImage::Format_ARGB32);
81 image.fill(Qt::transparent);
82
83 QPainter painter(&image);
84 painter.setRenderHint(QPainter::Antialiasing, true);
85
86 auto firstSample = sampleRange.minimum;
87 auto length = sampleRange.length();
88
89 // Is it a 2-channel (complex) trace?
90 if (auto src = dynamic_cast<SampleSource<std::complex<float>>*>(sampleSource.get())) {
91 auto samples = src->getSamples(firstSample, length);
92 if (samples == nullptr)
93 return;
94
95 painter.setPen(Qt::red);
96 plotTrace(painter, rect, reinterpret_cast<float*>(samples.get()), length, 2);
97 painter.setPen(Qt::blue);
98 plotTrace(painter, rect, reinterpret_cast<float*>(samples.get())+1, length, 2);
99
100 // Otherwise is it single channel?
101 } else if (auto src = dynamic_cast<SampleSource<float>*>(sampleSource.get())) {
102 auto samples = src->getSamples(firstSample, length);
103 if (samples == nullptr)
104 return;
105
106 painter.setPen(Qt::green);
107 plotTrace(painter, rect, samples.get(), length, 1);
108 } else {
109 throw std::runtime_error("TracePlot::paintMid: Unsupported source type");
110 }
111
112 emit imageReady(key, image);
113 }
114
115 void TracePlot::handleImage(QString key, QImage image)
116 {
117 auto pixmap = QPixmap::fromImage(image);
118 QPixmapCache::insert(key, pixmap);
119 tasks.remove(key);
120 emit repaint();
121 }
122
123 void TracePlot::plotTrace(QPainter &painter, const QRect &rect, float *samples, size_t count, int step = 1)
124 {
125 QPainterPath path;
126 range_t<float> xRange{0, rect.width() - 2.f};
127 range_t<float> yRange{0, rect.height() - 2.f};
128 const float xStep = 1.0 / count * rect.width();
129 for (size_t i = 0; i < count; i++) {
130 float sample = samples[i*step];
131 float x = i * xStep;
132 float y = (1 - sample) * (rect.height() / 2);
133
134 x = xRange.clip(x) + rect.x();
135 y = yRange.clip(y) + rect.y();
136
137 if (i == 0)
138 path.moveTo(x, y);
139 else
140 path.lineTo(x, y);
141 }
142 painter.drawPath(path);
143 }