annotate layer/Colour3DPlotRenderer.cpp @ 1499:e4c9d38d304d

Debug concerns
author Chris Cannam
date Tue, 10 Sep 2019 16:35:28 +0100
parents 78d9282519b0
children ac5a82e57d23
rev   line source
Chris@1071 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@1071 2
Chris@1071 3 /*
Chris@1071 4 Sonic Visualiser
Chris@1071 5 An audio file viewer and annotation editor.
Chris@1071 6 Centre for Digital Music, Queen Mary, University of London.
Chris@1071 7 This file copyright 2006-2016 Chris Cannam and QMUL.
Chris@1071 8
Chris@1071 9 This program is free software; you can redistribute it and/or
Chris@1071 10 modify it under the terms of the GNU General Public License as
Chris@1071 11 published by the Free Software Foundation; either version 2 of the
Chris@1071 12 License, or (at your option) any later version. See the file
Chris@1071 13 COPYING included with this distribution for more information.
Chris@1071 14 */
Chris@1071 15
Chris@1071 16 #include "Colour3DPlotRenderer.h"
Chris@1074 17 #include "RenderTimer.h"
Chris@1071 18
Chris@1117 19 #include "base/Profiler.h"
Chris@1214 20 #include "base/HitCount.h"
Chris@1117 21
Chris@1075 22 #include "data/model/DenseThreeDimensionalModel.h"
Chris@1075 23 #include "data/model/Dense3DModelPeakCache.h"
Chris@1075 24 #include "data/model/FFTModel.h"
Chris@1075 25
Chris@1077 26 #include "LayerGeometryProvider.h"
Chris@1082 27 #include "VerticalBinLayer.h"
Chris@1109 28 #include "PaintAssistant.h"
Chris@1139 29 #include "ImageRegionFinder.h"
Chris@1109 30
Chris@1109 31 #include "view/ViewManager.h" // for main model sample rate. Pity
Chris@1075 32
Chris@1079 33 #include <vector>
Chris@1079 34
Chris@1325 35 #include <utility>
Chris@1325 36 using namespace std::rel_ops;
Chris@1325 37
Chris@1362 38 //#define DEBUG_COLOUR_PLOT_REPAINT 1
Chris@1094 39
Chris@1079 40 using namespace std;
Chris@1079 41
Chris@1073 42 Colour3DPlotRenderer::RenderResult
Chris@1113 43 Colour3DPlotRenderer::render(const LayerGeometryProvider *v, QPainter &paint, QRect rect)
Chris@1076 44 {
Chris@1090 45 return render(v, paint, rect, false);
Chris@1076 46 }
Chris@1076 47
Chris@1076 48 Colour3DPlotRenderer::RenderResult
Chris@1113 49 Colour3DPlotRenderer::renderTimeConstrained(const LayerGeometryProvider *v,
Chris@1090 50 QPainter &paint, QRect rect)
Chris@1076 51 {
Chris@1090 52 return render(v, paint, rect, true);
Chris@1076 53 }
Chris@1076 54
Chris@1096 55 QRect
Chris@1121 56 Colour3DPlotRenderer::getLargestUncachedRect(const LayerGeometryProvider *v)
Chris@1096 57 {
Chris@1121 58 RenderType renderType = decideRenderType(v);
Chris@1121 59
Chris@1121 60 if (renderType == DirectTranslucent) {
Chris@1121 61 return QRect(); // never cached
Chris@1121 62 }
Chris@1121 63
Chris@1096 64 int h = m_cache.getSize().height();
Chris@1096 65
Chris@1096 66 QRect areaLeft(0, 0, m_cache.getValidLeft(), h);
Chris@1096 67 QRect areaRight(m_cache.getValidRight(), 0,
Chris@1096 68 m_cache.getSize().width() - m_cache.getValidRight(), h);
Chris@1096 69
Chris@1096 70 if (areaRight.width() > areaLeft.width()) {
Chris@1096 71 return areaRight;
Chris@1096 72 } else {
Chris@1096 73 return areaLeft;
Chris@1096 74 }
Chris@1096 75 }
Chris@1096 76
Chris@1122 77 bool
Chris@1122 78 Colour3DPlotRenderer::geometryChanged(const LayerGeometryProvider *v)
Chris@1122 79 {
Chris@1122 80 RenderType renderType = decideRenderType(v);
Chris@1122 81
Chris@1122 82 if (renderType == DirectTranslucent) {
Chris@1122 83 return true; // never cached
Chris@1122 84 }
Chris@1122 85
Chris@1122 86 if (m_cache.getSize() == v->getPaintSize() &&
Chris@1122 87 m_cache.getZoomLevel() == v->getZoomLevel() &&
Chris@1122 88 m_cache.getStartFrame() == v->getStartFrame()) {
Chris@1122 89 return false;
Chris@1122 90 } else {
Chris@1122 91 return true;
Chris@1122 92 }
Chris@1122 93 }
Chris@1122 94
Chris@1076 95 Colour3DPlotRenderer::RenderResult
Chris@1113 96 Colour3DPlotRenderer::render(const LayerGeometryProvider *v,
Chris@1090 97 QPainter &paint, QRect rect, bool timeConstrained)
Chris@1073 98 {
Chris@1109 99 RenderType renderType = decideRenderType(v);
Chris@1109 100
Chris@1221 101 if (timeConstrained) {
Chris@1221 102 if (renderType != DrawBufferPixelResolution) {
Chris@1221 103 // Rendering should be fast in bin-resolution and direct
Chris@1221 104 // draw cases because we are quite well zoomed-in, and the
Chris@1221 105 // sums are easier this way. Calculating boundaries later
Chris@1221 106 // will be fiddly for partial paints otherwise.
Chris@1221 107 timeConstrained = false;
Chris@1221 108
Chris@1221 109 } else if (m_secondsPerXPixelValid) {
Chris@1221 110 double predicted = m_secondsPerXPixel * rect.width();
Chris@1236 111 #ifdef DEBUG_COLOUR_PLOT_REPAINT
Chris@1499 112 SVDEBUG << "render " << m_sources.source
Chris@1499 113 << ": Predicted time for width " << rect.width() << " = "
Chris@1236 114 << predicted << " (" << m_secondsPerXPixel << " x "
Chris@1236 115 << rect.width() << ")" << endl;
Chris@1221 116 #endif
Chris@1450 117 if (predicted < 0.175) {
Chris@1221 118 #ifdef DEBUG_COLOUR_PLOT_REPAINT
Chris@1499 119 SVDEBUG << "render " << m_sources.source
Chris@1499 120 << ": Predicted time looks fast enough: no partial renders"
Chris@1221 121 << endl;
Chris@1221 122 #endif
Chris@1221 123 timeConstrained = false;
Chris@1221 124 }
Chris@1221 125 }
Chris@1109 126 }
Chris@1221 127
Chris@1079 128 int x0 = v->getXForViewX(rect.x());
Chris@1079 129 int x1 = v->getXForViewX(rect.x() + rect.width());
Chris@1079 130 if (x0 < 0) x0 = 0;
Chris@1079 131 if (x1 > v->getPaintWidth()) x1 = v->getPaintWidth();
Chris@1079 132
Chris@1120 133 sv_frame_t startFrame = v->getStartFrame();
Chris@1451 134
Chris@1451 135 bool justInvalidated =
Chris@1451 136 (m_cache.getSize() != v->getPaintSize() ||
Chris@1451 137 m_cache.getZoomLevel() != v->getZoomLevel());
Chris@1120 138
Chris@1079 139 m_cache.resize(v->getPaintSize());
Chris@1079 140 m_cache.setZoomLevel(v->getZoomLevel());
Chris@1079 141
Chris@1119 142 m_magCache.resize(v->getPaintSize().width());
Chris@1119 143 m_magCache.setZoomLevel(v->getZoomLevel());
Chris@1119 144
Chris@1120 145 if (renderType == DirectTranslucent) {
Chris@1121 146 MagnitudeRange range = renderDirectTranslucent(v, paint, rect);
Chris@1121 147 return { rect, range };
Chris@1120 148 }
Chris@1120 149
Chris@1123 150 #ifdef DEBUG_COLOUR_PLOT_REPAINT
Chris@1499 151 SVDEBUG << "render " << m_sources.source
Chris@1499 152 << ": cache start " << m_cache.getStartFrame()
Chris@1499 153 << " valid left " << m_cache.getValidLeft()
Chris@1499 154 << " valid right " << m_cache.getValidRight()
Chris@1499 155 << endl;
Chris@1214 156 SVDEBUG << " view start " << startFrame
Chris@1499 157 << " x0 " << x0
Chris@1499 158 << " x1 " << x1
Chris@1499 159 << endl;
Chris@1123 160 #endif
Chris@1214 161
Chris@1214 162 static HitCount count("Colour3DPlotRenderer: image cache");
Chris@1451 163
Chris@1079 164 if (m_cache.isValid()) { // some part of the cache is valid
Chris@1079 165
Chris@1079 166 if (v->getXForFrame(m_cache.getStartFrame()) ==
Chris@1079 167 v->getXForFrame(startFrame) &&
Chris@1079 168 m_cache.getValidLeft() <= x0 &&
Chris@1079 169 m_cache.getValidRight() >= x1) {
Chris@1090 170
Chris@1123 171 #ifdef DEBUG_COLOUR_PLOT_REPAINT
Chris@1214 172 SVDEBUG << "cache hit" << endl;
Chris@1123 173 #endif
Chris@1214 174 count.hit();
Chris@1090 175
Chris@1079 176 // cache is valid for the complete requested area
Chris@1079 177 paint.drawImage(rect, m_cache.getImage(), rect);
Chris@1119 178
Chris@1122 179 MagnitudeRange range = m_magCache.getRange(x0, x1 - x0);
Chris@1119 180
Chris@1119 181 return { rect, range };
Chris@1079 182
Chris@1079 183 } else {
Chris@1123 184 #ifdef DEBUG_COLOUR_PLOT_REPAINT
Chris@1214 185 SVDEBUG << "cache partial hit" << endl;
Chris@1123 186 #endif
Chris@1214 187 count.partial();
Chris@1090 188
Chris@1079 189 // cache doesn't begin at the right frame or doesn't
Chris@1079 190 // contain the complete view, but might be scrollable or
Chris@1079 191 // partially usable
Chris@1090 192 m_cache.scrollTo(v, startFrame);
Chris@1119 193 m_magCache.scrollTo(v, startFrame);
Chris@1079 194
Chris@1079 195 // if we are not time-constrained, then we want to paint
Chris@1081 196 // the whole area in one go; we don't return a partial
Chris@1081 197 // paint. To avoid providing the more complex logic to
Chris@1081 198 // handle painting discontiguous areas, if the only valid
Chris@1079 199 // part of cache is in the middle, just make the whole
Chris@1079 200 // thing invalid and start again.
Chris@1079 201 if (!timeConstrained) {
Chris@1079 202 if (m_cache.getValidLeft() > x0 &&
Chris@1079 203 m_cache.getValidRight() < x1) {
Chris@1079 204 m_cache.invalidate();
Chris@1079 205 }
Chris@1079 206 }
Chris@1079 207 }
Chris@1090 208 } else {
Chris@1118 209 // cache is completely invalid
Chris@1214 210 count.miss();
Chris@1090 211 m_cache.setStartFrame(startFrame);
Chris@1119 212 m_magCache.setStartFrame(startFrame);
Chris@1075 213 }
Chris@1075 214
Chris@1079 215 bool rightToLeft = false;
Chris@1079 216
Chris@1122 217 int reqx0 = x0;
Chris@1122 218 int reqx1 = x1;
Chris@1122 219
Chris@1079 220 if (!m_cache.isValid() && timeConstrained) {
Chris@1079 221 if (x0 == 0 && x1 == v->getPaintWidth()) {
Chris@1237 222
Chris@1237 223 // When rendering the whole area, in a context where we
Chris@1237 224 // might not be able to complete the work, start from
Chris@1237 225 // somewhere near the middle so that the region of
Chris@1237 226 // interest appears first.
Chris@1237 227 //
Chris@1237 228 // This is very useful if we actually are slow to render,
Chris@1237 229 // but if we're not sure how fast we'll be, we should
Chris@1237 230 // prefer not to because it can be distracting to render
Chris@1237 231 // fast from the middle and then jump back to fill in the
Chris@1237 232 // start. That is:
Chris@1237 233 //
Chris@1237 234 // - if our seconds-per-x-pixel count is invalid, then we
Chris@1237 235 // don't do this: we've probably only just been created
Chris@1237 236 // and don't know how fast we'll be yet (this happens
Chris@1237 237 // often while zooming rapidly in and out). The exception
Chris@1237 238 // to the exception is if we're displaying peak
Chris@1237 239 // frequencies; this we can assume to be slow. (Note that
Chris@1237 240 // if the seconds-per-x-pixel is valid and we know we're
Chris@1237 241 // fast, then we've already set timeConstrained false
Chris@1237 242 // above so this doesn't apply)
Chris@1237 243 //
Chris@1237 244 // - if we're using a peak cache, we don't do this;
Chris@1237 245 // drawing from peak cache is often (even if not always)
Chris@1237 246 // fast.
Chris@1237 247
Chris@1237 248 bool drawFromTheMiddle = true;
Chris@1237 249
Chris@1237 250 if (!m_secondsPerXPixelValid &&
Chris@1237 251 (m_params.binDisplay != BinDisplay::PeakFrequencies)) {
Chris@1237 252 drawFromTheMiddle = false;
Chris@1237 253 } else {
Chris@1237 254 int peakCacheIndex = -1, binsPerPeak = -1;
Chris@1237 255 getPreferredPeakCache(v, peakCacheIndex, binsPerPeak);
Chris@1237 256 if (peakCacheIndex >= 0) { // have a peak cache
Chris@1237 257 drawFromTheMiddle = false;
Chris@1237 258 }
Chris@1237 259 }
Chris@1237 260
Chris@1237 261 if (drawFromTheMiddle) {
Chris@1222 262 double offset = 0.5 * (double(rand()) / double(RAND_MAX));
Chris@1222 263 x0 = int(x1 * offset);
Chris@1213 264 }
Chris@1079 265 }
Chris@1079 266 }
Chris@1079 267
Chris@1079 268 if (m_cache.isValid()) {
Chris@1090 269
Chris@1079 270 // When rendering only a part of the cache, we need to make
Chris@1079 271 // sure that the part we're rendering is adjacent to (or
Chris@1079 272 // overlapping) a valid area of cache, if we have one. The
Chris@1079 273 // alternative is to ditch the valid area of cache and render
Chris@1079 274 // only the requested area, but that's risky because this can
Chris@1079 275 // happen when just waving the pointer over a small part of
Chris@1079 276 // the view -- if we lose the partly-built cache every time
Chris@1079 277 // the user does that, we'll never finish building it.
Chris@1079 278 int left = x0;
Chris@1079 279 int width = x1 - x0;
Chris@1079 280 bool isLeftOfValidArea = false;
Chris@1079 281 m_cache.adjustToTouchValidArea(left, width, isLeftOfValidArea);
Chris@1079 282 x0 = left;
Chris@1079 283 x1 = x0 + width;
Chris@1079 284
Chris@1079 285 // That call also told us whether we should be painting
Chris@1079 286 // sub-regions of our target region in right-to-left order in
Chris@1079 287 // order to ensure contiguity
Chris@1079 288 rightToLeft = isLeftOfValidArea;
Chris@1079 289 }
Chris@1075 290
Chris@1109 291 // Note, we always paint the full height to cache. We want to
Chris@1109 292 // ensure the cache is coherent without having to worry about
Chris@1109 293 // vertical matching of required and valid areas as well as
Chris@1109 294 // horizontal.
Chris@1094 295
Chris@1109 296 if (renderType == DrawBufferBinResolution) {
Chris@1109 297
Chris@1094 298 renderToCacheBinResolution(v, x0, x1 - x0);
Chris@1109 299
Chris@1109 300 } else { // must be DrawBufferPixelResolution, handled DirectTranslucent earlier
Chris@1109 301
Chris@1451 302 if (timeConstrained && justInvalidated) {
Chris@1499 303 SVDEBUG << "render " << m_sources.source << ": just invalidated cache in time-constrained context, that's all we're doing for now - wait for next update to start filling" << endl;
Chris@1451 304 } else {
Chris@1451 305 renderToCachePixelResolution(v, x0, x1 - x0, rightToLeft, timeConstrained);
Chris@1451 306 }
Chris@1094 307 }
Chris@1079 308
Chris@1079 309 QRect pr = rect & m_cache.getValidArea();
Chris@1079 310 paint.drawImage(pr.x(), pr.y(), m_cache.getImage(),
Chris@1079 311 pr.x(), pr.y(), pr.width(), pr.height());
Chris@1079 312
Chris@1079 313 if (!timeConstrained && (pr != rect)) {
Chris@1494 314 QRect cva = m_cache.getValidArea();
Chris@1265 315 SVCERR << "WARNING: failed to render entire requested rect "
Chris@1451 316 << "even when not time-constrained: wanted "
Chris@1450 317 << rect.x() << "," << rect.y() << " "
Chris@1451 318 << rect.width() << "x" << rect.height() << ", got "
Chris@1450 319 << pr.x() << "," << pr.y() << " "
Chris@1450 320 << pr.width() << "x" << pr.height()
Chris@1451 321 << ", after request of width " << (x1 - x0)
Chris@1494 322 << endl
Chris@1494 323 << "(cache valid area is "
Chris@1494 324 << cva.x() << "," << cva.y() << " "
Chris@1494 325 << cva.width() << "x" << cva.height() << ")"
Chris@1450 326 << endl;
Chris@1079 327 }
Chris@1120 328
Chris@1122 329 MagnitudeRange range = m_magCache.getRange(reqx0, reqx1 - reqx0);
Chris@1412 330
Chris@1412 331 #ifdef DEBUG_COLOUR_PLOT_REPAINT
Chris@1499 332 SVDEBUG << "render " << m_sources.source
Chris@1499 333 << ": returning rect rendered as " << pr.x() << "," << pr.y()
Chris@1412 334 << " " << pr.width() << "x" << pr.height() << endl;
Chris@1499 335 SVDEBUG << "render " << m_sources.source
Chris@1499 336 << ": mag range from cache in x-range " << reqx0
Chris@1412 337 << " to " << reqx1 << " is " << range.getMin() << " -> "
Chris@1412 338 << range.getMax() << endl;
Chris@1412 339 #endif
Chris@1120 340
Chris@1120 341 return { pr, range };
Chris@1073 342 }
Chris@1073 343
Chris@1109 344 Colour3DPlotRenderer::RenderType
Chris@1113 345 Colour3DPlotRenderer::decideRenderType(const LayerGeometryProvider *v) const
Chris@1094 346 {
Chris@1469 347 auto model = ModelById::getAs<DenseThreeDimensionalModel>(m_sources.source);
Chris@1109 348 if (!model || !v || !(v->getViewManager())) {
Chris@1109 349 return DrawBufferPixelResolution; // or anything
Chris@1109 350 }
Chris@1109 351
Chris@1094 352 int binResolution = model->getResolution();
Chris@1325 353 ZoomLevel zoomLevel = v->getZoomLevel();
Chris@1109 354 sv_samplerate_t modelRate = model->getSampleRate();
Chris@1109 355
Chris@1109 356 double rateRatio = v->getViewManager()->getMainModelSampleRate() / modelRate;
Chris@1109 357 double relativeBinResolution = binResolution * rateRatio;
Chris@1109 358
Chris@1109 359 if (m_params.binDisplay == BinDisplay::PeakFrequencies) {
Chris@1109 360 // no alternative works here
Chris@1109 361 return DrawBufferPixelResolution;
Chris@1109 362 }
Chris@1109 363
Chris@1109 364 if (!m_params.alwaysOpaque && !m_params.interpolate) {
Chris@1109 365
Chris@1109 366 // consider translucent option -- only if not smoothing & not
Chris@1109 367 // explicitly requested opaque & sufficiently zoomed-in
Chris@1109 368
Chris@1117 369 if (model->getHeight() * 3 < v->getPaintHeight() &&
Chris@1325 370 zoomLevel < ZoomLevel(ZoomLevel::FramesPerPixel,
Chris@1325 371 int(round(relativeBinResolution / 3)))) {
Chris@1109 372 return DirectTranslucent;
Chris@1109 373 }
Chris@1109 374 }
Chris@1109 375
Chris@1325 376 if (ZoomLevel(ZoomLevel::FramesPerPixel,
Chris@1325 377 int(round(relativeBinResolution))) > zoomLevel) {
Chris@1109 378 return DrawBufferBinResolution;
Chris@1109 379 } else {
Chris@1109 380 return DrawBufferPixelResolution;
Chris@1109 381 }
Chris@1109 382 }
Chris@1109 383
Chris@1138 384 ColumnOp::Column
Chris@1161 385 Colour3DPlotRenderer::getColumn(int sx, int minbin, int nbins,
Chris@1212 386 int peakCacheIndex) const
Chris@1138 387 {
Chris@1138 388 // order:
Chris@1138 389 // get column -> scale -> normalise -> record extents ->
Chris@1138 390 // peak pick -> distribute/interpolate -> apply display gain
Chris@1138 391
Chris@1138 392 // we do the first bit here:
Chris@1138 393 // get column -> scale -> normalise
Chris@1138 394
Chris@1138 395 ColumnOp::Column column;
Chris@1364 396
Chris@1364 397 if (m_params.showDerivative && sx > 0) {
Chris@1364 398
Chris@1364 399 auto prev = getColumnRaw(sx - 1, minbin, nbins, peakCacheIndex);
Chris@1364 400 column = getColumnRaw(sx, minbin, nbins, peakCacheIndex);
Chris@1364 401
Chris@1364 402 for (int i = 0; i < nbins; ++i) {
Chris@1364 403 column[i] -= prev[i];
Chris@1364 404 }
Chris@1364 405
Chris@1364 406 } else {
Chris@1364 407 column = getColumnRaw(sx, minbin, nbins, peakCacheIndex);
Chris@1364 408 }
Chris@1364 409
Chris@1364 410 if (m_params.colourScale.getScale() == ColourScaleType::Phase &&
Chris@1469 411 !m_sources.fft.isNone()) {
Chris@1364 412 return column;
Chris@1364 413 } else {
Chris@1364 414 column = ColumnOp::applyGain(column, m_params.scaleFactor);
Chris@1364 415 column = ColumnOp::normalize(column, m_params.normalization);
Chris@1364 416 return column;
Chris@1364 417 }
Chris@1364 418 }
Chris@1364 419
Chris@1364 420 ColumnOp::Column
Chris@1364 421 Colour3DPlotRenderer::getColumnRaw(int sx, int minbin, int nbins,
Chris@1364 422 int peakCacheIndex) const
Chris@1364 423 {
Chris@1364 424 Profiler profiler("Colour3DPlotRenderer::getColumn");
Chris@1364 425
Chris@1364 426 ColumnOp::Column column;
Chris@1469 427 ColumnOp::Column fullColumn;
Chris@1364 428
Chris@1469 429 if (m_params.colourScale.getScale() == ColourScaleType::Phase) {
Chris@1469 430 auto fftModel = ModelById::getAs<FFTModel>(m_sources.fft);
Chris@1469 431 if (fftModel) {
Chris@1469 432 fullColumn = fftModel->getPhases(sx);
Chris@1469 433 }
Chris@1138 434 }
Chris@1138 435
Chris@1469 436 if (fullColumn.empty()) {
Chris@1469 437
Chris@1469 438 if (peakCacheIndex >= 0) {
Chris@1473 439 auto peakCache = ModelById::getAs<Dense3DModelPeakCache>
Chris@1473 440 (m_sources.peakCaches[peakCacheIndex]);
Chris@1473 441 if (!peakCache) {
Chris@1473 442 return vector<float>(nbins, 0.f);
Chris@1473 443 }
Chris@1473 444 fullColumn = peakCache->getColumn(sx);
Chris@1469 445 } else {
Chris@1469 446 auto model = ModelById::getAs<DenseThreeDimensionalModel>
Chris@1469 447 (m_sources.source);
Chris@1469 448 if (!model) {
Chris@1469 449 return vector<float>(nbins, 0.f);
Chris@1469 450 }
Chris@1469 451 fullColumn = model->getColumn(sx);
Chris@1469 452 }
Chris@1469 453 }
Chris@1469 454
Chris@1469 455 column = vector<float>(fullColumn.data() + minbin,
Chris@1469 456 fullColumn.data() + minbin + nbins);
Chris@1138 457 return column;
Chris@1138 458 }
Chris@1138 459
Chris@1121 460 MagnitudeRange
Chris@1113 461 Colour3DPlotRenderer::renderDirectTranslucent(const LayerGeometryProvider *v,
Chris@1109 462 QPainter &paint,
Chris@1109 463 QRect rect)
Chris@1109 464 {
Chris@1117 465 Profiler profiler("Colour3DPlotRenderer::renderDirectTranslucent");
Chris@1117 466
Chris@1121 467 MagnitudeRange magRange;
Chris@1121 468
Chris@1115 469 QPoint illuminatePos;
Chris@1115 470 bool illuminate = v->shouldIlluminateLocalFeatures
Chris@1115 471 (m_sources.verticalBinLayer, illuminatePos);
Chris@1109 472
Chris@1469 473 auto model = ModelById::getAs<DenseThreeDimensionalModel>(m_sources.source);
Chris@1469 474 if (!model) return magRange;
Chris@1109 475
Chris@1109 476 int x0 = rect.left();
Chris@1109 477 int x1 = rect.right() + 1;
Chris@1109 478
Chris@1109 479 int h = v->getPaintHeight();
Chris@1109 480
Chris@1109 481 sv_frame_t modelStart = model->getStartFrame();
Chris@1109 482 sv_frame_t modelEnd = model->getEndFrame();
Chris@1109 483 int modelResolution = model->getResolution();
Chris@1109 484
Chris@1109 485 double rateRatio =
Chris@1109 486 v->getViewManager()->getMainModelSampleRate() / model->getSampleRate();
Chris@1109 487
Chris@1109 488 // the s-prefix values are source, i.e. model, column and bin numbers
Chris@1109 489 int sx0 = int((double(v->getFrameForX(x0)) / rateRatio - double(modelStart))
Chris@1109 490 / modelResolution);
Chris@1109 491 int sx1 = int((double(v->getFrameForX(x1)) / rateRatio - double(modelStart))
Chris@1109 492 / modelResolution);
Chris@1109 493
Chris@1109 494 int sh = model->getHeight();
Chris@1109 495
Chris@1109 496 const int buflen = 40;
Chris@1109 497 char labelbuf[buflen];
Chris@1109 498
Chris@1133 499 int minbin = m_sources.verticalBinLayer->getIBinForY(v, h);
Chris@1135 500 if (minbin >= sh) minbin = sh - 1;
Chris@1135 501 if (minbin < 0) minbin = 0;
Chris@1135 502
Chris@1135 503 int nbins = m_sources.verticalBinLayer->getIBinForY(v, 0) - minbin + 1;
Chris@1135 504 if (minbin + nbins > sh) nbins = sh - minbin;
Chris@1133 505
Chris@1109 506 int psx = -1;
Chris@1109 507
Chris@1109 508 vector<float> preparedColumn;
Chris@1109 509
Chris@1109 510 int modelWidth = model->getWidth();
Chris@1109 511
Chris@1109 512 for (int sx = sx0; sx <= sx1; ++sx) {
Chris@1109 513
Chris@1109 514 if (sx < 0 || sx >= modelWidth) {
Chris@1109 515 continue;
Chris@1109 516 }
Chris@1109 517
Chris@1109 518 if (sx != psx) {
Chris@1109 519
Chris@1138 520 // order:
Chris@1138 521 // get column -> scale -> normalise -> record extents ->
Chris@1138 522 // peak pick -> distribute/interpolate -> apply display gain
Chris@1109 523
Chris@1138 524 // this does the first three:
Chris@1219 525 preparedColumn = getColumn(sx, minbin, nbins, -1);
Chris@1131 526
Chris@1131 527 magRange.sample(preparedColumn);
Chris@1109 528
Chris@1109 529 if (m_params.binDisplay == BinDisplay::PeakBins) {
Chris@1115 530 preparedColumn = ColumnOp::peakPick(preparedColumn);
Chris@1109 531 }
Chris@1109 532
Chris@1124 533 // Display gain belongs to the colour scale and is
Chris@1124 534 // applied by the colour scale object when mapping it
Chris@1124 535
Chris@1109 536 psx = sx;
Chris@1109 537 }
Chris@1109 538
Chris@1266 539 sv_frame_t fx = sx * modelResolution + modelStart;
Chris@1109 540
Chris@1266 541 if (fx + modelResolution <= modelStart || fx > modelEnd) continue;
Chris@1109 542
Chris@1109 543 int rx0 = v->getXForFrame(int(double(fx) * rateRatio));
Chris@1266 544 int rx1 = v->getXForFrame(int(double(fx + modelResolution + 1) * rateRatio));
Chris@1109 545
Chris@1266 546 int rw = rx1 - rx0;
Chris@1266 547 if (rw < 1) rw = 1;
Chris@1109 548
Chris@1471 549 // Qt 5.13 deprecates QFontMetrics::width(), but its suggested
Chris@1471 550 // replacement (horizontalAdvance) was only added in Qt 5.11
Chris@1471 551 // which is too new for us
Chris@1471 552 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
Chris@1471 553
Chris@1266 554 bool showLabel = (rw > 10 &&
Chris@1266 555 paint.fontMetrics().width("0.000000") < rw - 3 &&
Chris@1266 556 paint.fontMetrics().height() < (h / sh));
Chris@1109 557
Chris@1266 558 for (int sy = minbin; sy < minbin + nbins; ++sy) {
Chris@1109 559
Chris@1109 560 int ry0 = m_sources.verticalBinLayer->getIYForBin(v, sy);
Chris@1109 561 int ry1 = m_sources.verticalBinLayer->getIYForBin(v, sy + 1);
Chris@1116 562
Chris@1116 563 if (m_params.invertVertical) {
Chris@1116 564 ry0 = h - ry0 - 1;
Chris@1116 565 ry1 = h - ry1 - 1;
Chris@1116 566 }
Chris@1116 567
Chris@1109 568 QRect r(rx0, ry1, rw, ry0 - ry1);
Chris@1109 569
Chris@1109 570 float value = preparedColumn[sy - minbin];
Chris@1112 571 QColor colour = m_params.colourScale.getColour(value,
Chris@1112 572 m_params.colourRotation);
Chris@1109 573
Chris@1109 574 if (rw == 1) {
Chris@1109 575 paint.setPen(colour);
Chris@1109 576 paint.setBrush(Qt::NoBrush);
Chris@1109 577 paint.drawLine(r.x(), r.y(), r.x(), r.y() + r.height() - 1);
Chris@1109 578 continue;
Chris@1109 579 }
Chris@1109 580
Chris@1266 581 QColor pen(255, 255, 255, 80);
Chris@1266 582 QColor brush(colour);
Chris@1109 583
Chris@1109 584 if (rw > 3 && r.height() > 3) {
Chris@1109 585 brush.setAlpha(160);
Chris@1109 586 }
Chris@1109 587
Chris@1266 588 paint.setPen(Qt::NoPen);
Chris@1266 589 paint.setBrush(brush);
Chris@1109 590
Chris@1266 591 if (illuminate) {
Chris@1266 592 if (r.contains(illuminatePos)) {
Chris@1266 593 paint.setPen(v->getForeground());
Chris@1266 594 }
Chris@1266 595 }
Chris@1109 596
Chris@1214 597 #ifdef DEBUG_COLOUR_PLOT_REPAINT
Chris@1214 598 // SVDEBUG << "rect " << r.x() << "," << r.y() << " "
Chris@1109 599 // << r.width() << "x" << r.height() << endl;
Chris@1109 600 #endif
Chris@1109 601
Chris@1266 602 paint.drawRect(r);
Chris@1109 603
Chris@1266 604 if (showLabel) {
Chris@1109 605 double value = model->getValueAt(sx, sy);
Chris@1109 606 snprintf(labelbuf, buflen, "%06f", value);
Chris@1109 607 QString text(labelbuf);
Chris@1109 608 PaintAssistant::drawVisibleText
Chris@1109 609 (v,
Chris@1109 610 paint,
Chris@1109 611 rx0 + 2,
Chris@1109 612 ry0 - h / sh - 1 + 2 + paint.fontMetrics().ascent(),
Chris@1109 613 text,
Chris@1109 614 PaintAssistant::OutlinedText);
Chris@1266 615 }
Chris@1266 616 }
Chris@1109 617 }
Chris@1121 618
Chris@1121 619 return magRange;
Chris@1094 620 }
Chris@1094 621
Chris@1080 622 void
Chris@1213 623 Colour3DPlotRenderer::getPreferredPeakCache(const LayerGeometryProvider *v,
Chris@1213 624 int &peakCacheIndex,
Chris@1213 625 int &binsPerPeak) const
Chris@1213 626 {
Chris@1213 627 peakCacheIndex = -1;
Chris@1213 628 binsPerPeak = -1;
Chris@1213 629
Chris@1469 630 auto model = ModelById::getAs<DenseThreeDimensionalModel>(m_sources.source);
Chris@1213 631 if (!model) return;
Chris@1217 632 if (m_params.binDisplay == BinDisplay::PeakFrequencies) return;
Chris@1217 633 if (m_params.colourScale.getScale() == ColourScaleType::Phase) return;
Chris@1213 634
Chris@1325 635 ZoomLevel zoomLevel = v->getZoomLevel();
Chris@1213 636 int binResolution = model->getResolution();
Chris@1213 637
Chris@1213 638 for (int ix = 0; in_range_for(m_sources.peakCaches, ix); ++ix) {
Chris@1473 639 auto peakCache = ModelById::getAs<Dense3DModelPeakCache>
Chris@1473 640 (m_sources.peakCaches[ix]);
Chris@1473 641 if (!peakCache) continue;
Chris@1473 642 int bpp = peakCache->getColumnsPerPeak();
Chris@1325 643 ZoomLevel equivZoom(ZoomLevel::FramesPerPixel, binResolution * bpp);
Chris@1450 644 #ifdef DEBUG_COLOUR_PLOT_REPAINT
Chris@1450 645 SVDEBUG << "getPreferredPeakCache: zoomLevel = " << zoomLevel
Chris@1450 646 << ", cache " << ix << " has bpp = " << bpp
Chris@1450 647 << " for equivZoom = " << equivZoom << endl;
Chris@1450 648 #endif
Chris@1213 649 if (zoomLevel >= equivZoom) {
Chris@1213 650 // this peak cache would work, though it might not be best
Chris@1213 651 if (bpp > binsPerPeak) {
Chris@1213 652 // ok, it's better than the best one we've found so far
Chris@1213 653 peakCacheIndex = ix;
Chris@1213 654 binsPerPeak = bpp;
Chris@1213 655 }
Chris@1213 656 }
Chris@1213 657 }
Chris@1213 658
Chris@1214 659 #ifdef DEBUG_COLOUR_PLOT_REPAINT
Chris@1499 660 SVDEBUG << "render " << m_sources.source
Chris@1499 661 << ": getPreferredPeakCache: zoomLevel = " << zoomLevel
Chris@1213 662 << ", binResolution " << binResolution
Chris@1213 663 << ", peakCaches " << m_sources.peakCaches.size()
Chris@1450 664 << ": preferring peakCacheIndex " << peakCacheIndex
Chris@1450 665 << " for binsPerPeak " << binsPerPeak
Chris@1213 666 << endl;
Chris@1214 667 #endif
Chris@1213 668 }
Chris@1213 669
Chris@1213 670 void
Chris@1113 671 Colour3DPlotRenderer::renderToCachePixelResolution(const LayerGeometryProvider *v,
Chris@1094 672 int x0, int repaintWidth,
Chris@1094 673 bool rightToLeft,
Chris@1094 674 bool timeConstrained)
Chris@1079 675 {
Chris@1117 676 Profiler profiler("Colour3DPlotRenderer::renderToCachePixelResolution");
Chris@1143 677 #ifdef DEBUG_COLOUR_PLOT_REPAINT
Chris@1499 678 SVDEBUG << "render " << m_sources.source
Chris@1499 679 << ": renderToCachePixelResolution" << endl;
Chris@1143 680 #endif
Chris@1094 681
Chris@1094 682 // Draw to the draw buffer, and then copy from there. The draw
Chris@1094 683 // buffer is at the same resolution as the target in the cache, so
Chris@1094 684 // no extra scaling needed.
Chris@1079 685
Chris@1469 686 auto model = ModelById::getAs<DenseThreeDimensionalModel>(m_sources.source);
Chris@1469 687 if (!model) return;
Chris@1079 688
Chris@1079 689 int h = v->getPaintHeight();
Chris@1079 690
Chris@1094 691 clearDrawBuffer(repaintWidth, h);
Chris@1079 692
Chris@1094 693 vector<int> binforx(repaintWidth);
Chris@1079 694 vector<double> binfory(h);
Chris@1079 695
Chris@1094 696 int binResolution = model->getResolution();
Chris@1079 697
Chris@1094 698 for (int x = 0; x < repaintWidth; ++x) {
Chris@1094 699 sv_frame_t f0 = v->getFrameForX(x0 + x);
Chris@1094 700 double s0 = double(f0 - model->getStartFrame()) / binResolution;
Chris@1094 701 binforx[x] = int(s0 + 0.0001);
Chris@1094 702 }
Chris@1080 703
Chris@1212 704 int peakCacheIndex = -1;
Chris@1212 705 int binsPerPeak = -1;
Chris@1212 706
Chris@1217 707 getPreferredPeakCache(v, peakCacheIndex, binsPerPeak);
Chris@1094 708
Chris@1080 709 for (int y = 0; y < h; ++y) {
Chris@1090 710 binfory[y] = m_sources.verticalBinLayer->getBinForY(v, h - y - 1);
Chris@1080 711 }
Chris@1079 712
Chris@1097 713 int attainedWidth;
Chris@1097 714
Chris@1103 715 if (m_params.binDisplay == BinDisplay::PeakFrequencies) {
Chris@1097 716 attainedWidth = renderDrawBufferPeakFrequencies(v,
Chris@1097 717 repaintWidth,
Chris@1097 718 h,
Chris@1097 719 binforx,
Chris@1097 720 binfory,
Chris@1097 721 rightToLeft,
Chris@1097 722 timeConstrained);
Chris@1097 723
Chris@1097 724 } else {
Chris@1097 725 attainedWidth = renderDrawBuffer(repaintWidth,
Chris@1080 726 h,
Chris@1080 727 binforx,
Chris@1080 728 binfory,
Chris@1212 729 peakCacheIndex,
Chris@1080 730 rightToLeft,
Chris@1080 731 timeConstrained);
Chris@1097 732 }
Chris@1083 733
Chris@1094 734 if (attainedWidth == 0) return;
Chris@1084 735
Chris@1094 736 // draw buffer is pixel resolution, no scaling factors or padding involved
Chris@1084 737
Chris@1084 738 int paintedLeft = x0;
Chris@1084 739 if (rightToLeft) {
Chris@1084 740 paintedLeft += (repaintWidth - attainedWidth);
Chris@1084 741 }
Chris@1084 742
Chris@1094 743 m_cache.drawImage(paintedLeft, attainedWidth,
Chris@1094 744 m_drawBuffer,
Chris@1094 745 paintedLeft - x0, attainedWidth);
Chris@1121 746
Chris@1121 747 for (int i = 0; in_range_for(m_magRanges, i); ++i) {
Chris@1121 748 m_magCache.sampleColumn(i, m_magRanges.at(i));
Chris@1121 749 }
Chris@1094 750 }
Chris@1084 751
Chris@1167 752 QImage
Chris@1167 753 Colour3DPlotRenderer::scaleDrawBufferImage(QImage image,
Chris@1167 754 int targetWidth,
Chris@1167 755 int targetHeight) const
Chris@1167 756 {
Chris@1167 757 int sourceWidth = image.width();
Chris@1167 758 int sourceHeight = image.height();
Chris@1167 759
Chris@1167 760 // We can only do this if we're making the image larger --
Chris@1167 761 // otherwise peaks may be lost. So this should be called only when
Chris@1167 762 // rendering in DrawBufferBinResolution mode. Whenever the bin
Chris@1167 763 // size is smaller than the pixel size, in either x or y axis, we
Chris@1167 764 // should be using DrawBufferPixelResolution mode instead
Chris@1167 765
Chris@1167 766 if (targetWidth < sourceWidth || targetHeight < sourceHeight) {
Chris@1167 767 throw std::logic_error("Colour3DPlotRenderer::scaleDrawBufferImage: Can only use this function when making the image larger; should be rendering DrawBufferPixelResolution instead");
Chris@1167 768 }
Chris@1167 769
Chris@1167 770 if (sourceWidth <= 0 || sourceHeight <= 0) {
Chris@1167 771 throw std::logic_error("Colour3DPlotRenderer::scaleDrawBufferImage: Source image is empty");
Chris@1167 772 }
Chris@1167 773
Chris@1167 774 if (targetWidth <= 0 || targetHeight <= 0) {
Chris@1167 775 throw std::logic_error("Colour3DPlotRenderer::scaleDrawBufferImage: Target image is empty");
Chris@1167 776 }
Chris@1167 777
Chris@1167 778 // This function exists because of some unpredictable behaviour
Chris@1167 779 // from Qt when scaling images with FastTransformation mode. We
Chris@1167 780 // continue to use Qt's scaler for SmoothTransformation but let's
Chris@1167 781 // bring the non-interpolated version "in-house" so we know what
Chris@1167 782 // it's really doing.
Chris@1167 783
Chris@1167 784 if (m_params.interpolate) {
Chris@1167 785 return image.scaled(targetWidth, targetHeight,
Chris@1167 786 Qt::IgnoreAspectRatio,
Chris@1167 787 Qt::SmoothTransformation);
Chris@1167 788 }
Chris@1167 789
Chris@1167 790 // Same format as the target cache
Chris@1167 791 QImage target(targetWidth, targetHeight, QImage::Format_ARGB32_Premultiplied);
Chris@1167 792
Chris@1167 793 for (int y = 0; y < targetHeight; ++y) {
Chris@1167 794
Chris@1167 795 QRgb *targetLine = reinterpret_cast<QRgb *>(target.scanLine(y));
Chris@1167 796
Chris@1167 797 int sy = int((uint64_t(y) * sourceHeight) / targetHeight);
Chris@1167 798 if (sy == sourceHeight) --sy;
Chris@1167 799
Chris@1167 800 for (int x = 0; x < targetWidth; ++x) {
Chris@1167 801
Chris@1167 802 int sx = int((uint64_t(x) * sourceWidth) / targetWidth);
Chris@1167 803 if (sx == sourceWidth) --sx;
Chris@1167 804
Chris@1167 805 targetLine[x] = image.pixel(sx, sy);
Chris@1167 806 }
Chris@1167 807 }
Chris@1167 808
Chris@1167 809 return target;
Chris@1167 810 }
Chris@1167 811
Chris@1094 812 void
Chris@1113 813 Colour3DPlotRenderer::renderToCacheBinResolution(const LayerGeometryProvider *v,
Chris@1094 814 int x0, int repaintWidth)
Chris@1094 815 {
Chris@1117 816 Profiler profiler("Colour3DPlotRenderer::renderToCacheBinResolution");
Chris@1143 817 #ifdef DEBUG_COLOUR_PLOT_REPAINT
Chris@1499 818 SVDEBUG << "render " << m_sources.source
Chris@1499 819 << ": renderToCacheBinResolution" << endl;
Chris@1143 820 #endif
Chris@1094 821
Chris@1094 822 // Draw to the draw buffer, and then scale-copy from there. Draw
Chris@1094 823 // buffer is at bin resolution, i.e. buffer x == source column
Chris@1094 824 // number. We use toolkit smooth scaling for interpolation.
Chris@1084 825
Chris@1469 826 auto model = ModelById::getAs<DenseThreeDimensionalModel>(m_sources.source);
Chris@1469 827 if (!model) return;
Chris@1094 828
Chris@1094 829 // The draw buffer will contain a fragment at bin resolution. We
Chris@1094 830 // need to ensure that it starts and ends at points where a
Chris@1094 831 // time-bin boundary occurs at an exact pixel boundary, and with a
Chris@1094 832 // certain amount of overlap across existing pixels so that we can
Chris@1094 833 // scale and draw from it without smoothing errors at the edges.
Chris@1094 834
Chris@1094 835 // If (getFrameForX(x) / increment) * increment ==
Chris@1094 836 // getFrameForX(x), then x is a time-bin boundary. We want two
Chris@1094 837 // such boundaries at either side of the draw buffer -- one which
Chris@1094 838 // we draw up to, and one which we subsequently crop at.
Chris@1094 839
Chris@1094 840 sv_frame_t leftBoundaryFrame = -1, leftCropFrame = -1;
Chris@1094 841 sv_frame_t rightBoundaryFrame = -1, rightCropFrame = -1;
Chris@1094 842
Chris@1094 843 int drawBufferWidth;
Chris@1094 844 int binResolution = model->getResolution();
Chris@1094 845
Chris@1330 846 // These loops should eventually terminate provided that
Chris@1330 847 // getFrameForX always returns a multiple of the zoom level,
Chris@1330 848 // i.e. there is some x for which getFrameForX(x) == 0 and
Chris@1330 849 // subsequent return values are equally spaced
Chris@1329 850
Chris@1330 851 for (int x = x0; ; --x) {
Chris@1094 852 sv_frame_t f = v->getFrameForX(x);
Chris@1330 853 if ((f / binResolution) * binResolution == f) {
Chris@1094 854 if (leftCropFrame == -1) leftCropFrame = f;
Chris@1094 855 else if (x < x0 - 2) {
Chris@1094 856 leftBoundaryFrame = f;
Chris@1094 857 break;
Chris@1094 858 }
Chris@1094 859 }
Chris@1094 860 }
Chris@1329 861
Chris@1330 862 for (int x = x0 + repaintWidth; ; ++x) {
Chris@1094 863 sv_frame_t f = v->getFrameForX(x);
Chris@1330 864 if ((f / binResolution) * binResolution == f) {
Chris@1494 865 if (v->getXForFrame(f) < x0 + repaintWidth) {
Chris@1494 866 continue;
Chris@1494 867 }
Chris@1094 868 if (rightCropFrame == -1) rightCropFrame = f;
Chris@1094 869 else if (x > x0 + repaintWidth + 2) {
Chris@1094 870 rightBoundaryFrame = f;
Chris@1094 871 break;
Chris@1094 872 }
Chris@1094 873 }
Chris@1094 874 }
Chris@1329 875
Chris@1094 876 drawBufferWidth = int
Chris@1094 877 ((rightBoundaryFrame - leftBoundaryFrame) / binResolution);
Chris@1094 878
Chris@1094 879 int h = v->getPaintHeight();
Chris@1094 880
Chris@1095 881 // For our purposes here, the draw buffer needs to be exactly our
Chris@1095 882 // target size (so we recreate always rather than just clear it)
Chris@1095 883
Chris@1095 884 recreateDrawBuffer(drawBufferWidth, h);
Chris@1094 885
Chris@1094 886 vector<int> binforx(drawBufferWidth);
Chris@1094 887 vector<double> binfory(h);
Chris@1094 888
Chris@1094 889 for (int x = 0; x < drawBufferWidth; ++x) {
Chris@1094 890 binforx[x] = int(leftBoundaryFrame / binResolution) + x;
Chris@1094 891 }
Chris@1094 892
Chris@1214 893 #ifdef DEBUG_COLOUR_PLOT_REPAINT
Chris@1167 894 SVDEBUG << "[BIN] binResolution " << binResolution << endl;
Chris@1214 895 #endif
Chris@1094 896
Chris@1094 897 for (int y = 0; y < h; ++y) {
Chris@1094 898 binfory[y] = m_sources.verticalBinLayer->getBinForY(v, h - y - 1);
Chris@1094 899 }
Chris@1094 900
Chris@1094 901 int attainedWidth = renderDrawBuffer(drawBufferWidth,
Chris@1094 902 h,
Chris@1094 903 binforx,
Chris@1094 904 binfory,
Chris@1212 905 -1,
Chris@1094 906 false,
Chris@1094 907 false);
Chris@1094 908
Chris@1094 909 if (attainedWidth == 0) return;
Chris@1094 910
Chris@1094 911 int scaledLeft = v->getXForFrame(leftBoundaryFrame);
Chris@1094 912 int scaledRight = v->getXForFrame(rightBoundaryFrame);
Chris@1095 913
Chris@1143 914 #ifdef DEBUG_COLOUR_PLOT_REPAINT
Chris@1212 915 SVDEBUG << "scaling draw buffer from width " << m_drawBuffer.width()
Chris@1494 916 << " to " << (scaledRight - scaledLeft)
Chris@1494 917 << " (nb drawBufferWidth = "
Chris@1494 918 << drawBufferWidth << ", attainedWidth = "
Chris@1494 919 << attainedWidth << ")" << endl;
Chris@1143 920 #endif
Chris@1167 921
Chris@1167 922 QImage scaled = scaleDrawBufferImage
Chris@1167 923 (m_drawBuffer, scaledRight - scaledLeft, h);
Chris@1084 924
Chris@1094 925 int scaledLeftCrop = v->getXForFrame(leftCropFrame);
Chris@1094 926 int scaledRightCrop = v->getXForFrame(rightCropFrame);
Chris@1094 927
Chris@1094 928 int targetLeft = scaledLeftCrop;
Chris@1094 929 if (targetLeft < 0) {
Chris@1094 930 targetLeft = 0;
Chris@1094 931 }
Chris@1094 932
Chris@1094 933 int targetWidth = scaledRightCrop - targetLeft;
Chris@1094 934 if (targetLeft + targetWidth > m_cache.getSize().width()) {
Chris@1094 935 targetWidth = m_cache.getSize().width() - targetLeft;
Chris@1094 936 }
Chris@1094 937
Chris@1094 938 int sourceLeft = targetLeft - scaledLeft;
Chris@1094 939 if (sourceLeft < 0) {
Chris@1094 940 sourceLeft = 0;
Chris@1094 941 }
Chris@1494 942
Chris@1494 943 #ifdef DEBUG_COLOUR_PLOT_REPAINT
Chris@1494 944 SVDEBUG << "leftBoundaryFrame = " << leftBoundaryFrame
Chris@1494 945 << ", leftCropFrame = " << leftCropFrame
Chris@1494 946 << ", scaledLeft = " << scaledLeft
Chris@1494 947 << ", scaledLeftCrop = " << scaledLeftCrop
Chris@1494 948 << endl;
Chris@1494 949 SVDEBUG << "rightBoundaryFrame = " << rightBoundaryFrame
Chris@1494 950 << ", rightCropFrame = " << rightCropFrame
Chris@1494 951 << ", scaledRight = " << scaledRight
Chris@1494 952 << ", scaledRightCrop = " << scaledRightCrop
Chris@1494 953 << endl;
Chris@1494 954 #endif
Chris@1094 955
Chris@1143 956 #ifdef DEBUG_COLOUR_PLOT_REPAINT
Chris@1494 957 SVDEBUG << "x0 = " << x0
Chris@1494 958 << ", repaintWidth = " << repaintWidth
Chris@1494 959 << ", targetLeft = " << targetLeft
Chris@1214 960 << ", targetWidth = " << targetWidth << endl;
Chris@1143 961 #endif
Chris@1094 962
Chris@1094 963 if (targetWidth > 0) {
Chris@1136 964 // we are copying from an image that has already been scaled,
Chris@1136 965 // hence using the same width in both geometries
Chris@1094 966 m_cache.drawImage(targetLeft, targetWidth,
Chris@1094 967 scaled,
Chris@1136 968 sourceLeft, targetWidth);
Chris@1084 969 }
Chris@1121 970
Chris@1121 971 for (int i = 0; i < targetWidth; ++i) {
Chris@1136 972 // but the mag range vector has not been scaled
Chris@1136 973 int sourceIx = int((double(i + sourceLeft) / scaled.width())
Chris@1136 974 * int(m_magRanges.size()));
Chris@1121 975 if (in_range_for(m_magRanges, sourceIx)) {
Chris@1121 976 m_magCache.sampleColumn(i, m_magRanges.at(sourceIx));
Chris@1121 977 }
Chris@1121 978 }
Chris@1079 979 }
Chris@1083 980
Chris@1083 981 int
Chris@1083 982 Colour3DPlotRenderer::renderDrawBuffer(int w, int h,
Chris@1083 983 const vector<int> &binforx,
Chris@1083 984 const vector<double> &binfory,
Chris@1212 985 int peakCacheIndex,
Chris@1083 986 bool rightToLeft,
Chris@1083 987 bool timeConstrained)
Chris@1083 988 {
Chris@1083 989 // Callers must have checked that the appropriate subset of
Chris@1083 990 // Sources data members are set for the supplied flags (e.g. that
Chris@1212 991 // peakCache corresponding to peakCacheIndex exists)
Chris@1083 992
Chris@1083 993 RenderTimer timer(timeConstrained ?
Chris@1083 994 RenderTimer::FastRender :
Chris@1083 995 RenderTimer::NoTimeout);
Chris@1083 996
Chris@1164 997 Profiler profiler("Colour3DPlotRenderer::renderDrawBuffer");
Chris@1164 998
Chris@1083 999 int divisor = 1;
Chris@1473 1000
Chris@1473 1001 std::shared_ptr<DenseThreeDimensionalModel> sourceModel;
Chris@1473 1002
Chris@1473 1003 if (peakCacheIndex >= 0) {
Chris@1473 1004 auto peakCache = ModelById::getAs<Dense3DModelPeakCache>
Chris@1473 1005 (m_sources.peakCaches[peakCacheIndex]);
Chris@1473 1006 if (peakCache) {
Chris@1473 1007 divisor = peakCache->getColumnsPerPeak();
Chris@1473 1008 sourceModel = peakCache;
Chris@1473 1009 }
Chris@1473 1010 }
Chris@1473 1011
Chris@1473 1012 if (!sourceModel) {
Chris@1473 1013 sourceModel = ModelById::getAs<DenseThreeDimensionalModel>
Chris@1473 1014 (m_sources.source);
Chris@1473 1015 }
Chris@1469 1016
Chris@1473 1017 if (!sourceModel) return 0;
Chris@1083 1018
Chris@1214 1019 #ifdef DEBUG_COLOUR_PLOT_REPAINT
cannam@1171 1020 SVDEBUG << "renderDrawBuffer: w = " << w << ", h = " << h
Chris@1212 1021 << ", peakCacheIndex = " << peakCacheIndex << " (divisor = "
cannam@1171 1022 << divisor << "), rightToLeft = " << rightToLeft
cannam@1171 1023 << ", timeConstrained = " << timeConstrained << endl;
cannam@1171 1024 SVDEBUG << "renderDrawBuffer: normalization = " << int(m_params.normalization)
cannam@1171 1025 << ", binDisplay = " << int(m_params.binDisplay)
cannam@1171 1026 << ", binScale = " << int(m_params.binScale)
cannam@1171 1027 << ", alwaysOpaque = " << m_params.alwaysOpaque
cannam@1171 1028 << ", interpolate = " << m_params.interpolate << endl;
Chris@1214 1029 #endif
cannam@1171 1030
Chris@1135 1031 int sh = sourceModel->getHeight();
Chris@1135 1032
Chris@1135 1033 int minbin = int(binfory[0] + 0.0001);
Chris@1135 1034 if (minbin >= sh) minbin = sh - 1;
Chris@1135 1035 if (minbin < 0) minbin = 0;
Chris@1135 1036
Chris@1170 1037 int nbins = int(binfory[h-1] + 0.0001) - minbin + 1;
Chris@1135 1038 if (minbin + nbins > sh) nbins = sh - minbin;
Chris@1162 1039
Chris@1162 1040 #ifdef DEBUG_COLOUR_PLOT_REPAINT
Chris@1214 1041 SVDEBUG << "minbin = " << minbin << ", nbins = " << nbins << ", last binfory = "
Chris@1170 1042 << binfory[h-1] << " (rounds to " << int(binfory[h-1]) << ") (model height " << sh << ")" << endl;
Chris@1162 1043 #endif
Chris@1135 1044
Chris@1083 1045 int psx = -1;
Chris@1083 1046
Chris@1083 1047 int start = 0;
Chris@1083 1048 int finish = w;
Chris@1083 1049 int step = 1;
Chris@1083 1050
Chris@1083 1051 if (rightToLeft) {
Chris@1083 1052 start = w-1;
Chris@1083 1053 finish = -1;
Chris@1083 1054 step = -1;
Chris@1083 1055 }
Chris@1083 1056
Chris@1221 1057 int xPixelCount = 0;
Chris@1083 1058
Chris@1083 1059 vector<float> preparedColumn;
Chris@1094 1060
Chris@1094 1061 int modelWidth = sourceModel->getWidth();
Chris@1121 1062
Chris@1143 1063 #ifdef DEBUG_COLOUR_PLOT_REPAINT
Chris@1214 1064 SVDEBUG << "modelWidth " << modelWidth << ", divisor " << divisor << endl;
Chris@1451 1065 SVDEBUG << "start = " << start << ", finish = " << finish << ", step = " << step << endl;
Chris@1143 1066 #endif
Chris@1143 1067
Chris@1083 1068 for (int x = start; x != finish; x += step) {
Chris@1083 1069
Chris@1083 1070 // x is the on-canvas pixel coord; sx (later) will be the
Chris@1083 1071 // source column index
Chris@1083 1072
Chris@1221 1073 ++xPixelCount;
Chris@1083 1074
Chris@1083 1075 if (binforx[x] < 0) continue;
Chris@1083 1076
Chris@1083 1077 int sx0 = binforx[x] / divisor;
Chris@1083 1078 int sx1 = sx0;
Chris@1083 1079 if (x+1 < w) sx1 = binforx[x+1] / divisor;
Chris@1083 1080 if (sx0 < 0) sx0 = sx1 - 1;
Chris@1083 1081 if (sx0 < 0) continue;
Chris@1083 1082 if (sx1 <= sx0) sx1 = sx0 + 1;
Chris@1083 1083
Chris@1161 1084 #ifdef DEBUG_COLOUR_PLOT_REPAINT
Chris@1214 1085 // SVDEBUG << "x = " << x << ", binforx[x] = " << binforx[x] << ", sx range " << sx0 << " -> " << sx1 << endl;
Chris@1161 1086 #endif
Chris@1161 1087
Chris@1083 1088 vector<float> pixelPeakColumn;
Chris@1121 1089 MagnitudeRange magRange;
Chris@1083 1090
Chris@1083 1091 for (int sx = sx0; sx < sx1; ++sx) {
Chris@1083 1092
Chris@1094 1093 if (sx < 0 || sx >= modelWidth) {
Chris@1083 1094 continue;
Chris@1083 1095 }
Chris@1083 1096
Chris@1083 1097 if (sx != psx) {
Chris@1138 1098
Chris@1138 1099 // order:
Chris@1138 1100 // get column -> scale -> normalise -> record extents ->
Chris@1138 1101 // peak pick -> distribute/interpolate -> apply display gain
Chris@1083 1102
Chris@1138 1103 // this does the first three:
Chris@1161 1104 ColumnOp::Column column = getColumn(sx, minbin, nbins,
Chris@1212 1105 peakCacheIndex);
Chris@1083 1106
Chris@1131 1107 magRange.sample(column);
Chris@1162 1108
Chris@1103 1109 if (m_params.binDisplay == BinDisplay::PeakBins) {
Chris@1083 1110 column = ColumnOp::peakPick(column);
Chris@1083 1111 }
Chris@1083 1112
Chris@1083 1113 preparedColumn =
Chris@1124 1114 ColumnOp::distribute(column,
Chris@1083 1115 h,
Chris@1083 1116 binfory,
Chris@1083 1117 minbin,
Chris@1083 1118 m_params.interpolate);
Chris@1124 1119
Chris@1124 1120 // Display gain belongs to the colour scale and is
Chris@1124 1121 // applied by the colour scale object when mapping it
Chris@1083 1122
Chris@1083 1123 psx = sx;
Chris@1083 1124 }
Chris@1083 1125
Chris@1083 1126 if (sx == sx0) {
Chris@1083 1127 pixelPeakColumn = preparedColumn;
Chris@1083 1128 } else {
Chris@1083 1129 for (int i = 0; in_range_for(pixelPeakColumn, i); ++i) {
Chris@1083 1130 pixelPeakColumn[i] = std::max(pixelPeakColumn[i],
Chris@1083 1131 preparedColumn[i]);
Chris@1083 1132 }
Chris@1083 1133 }
Chris@1083 1134 }
Chris@1083 1135
Chris@1083 1136 if (!pixelPeakColumn.empty()) {
Chris@1121 1137
Chris@1083 1138 for (int y = 0; y < h; ++y) {
Chris@1116 1139 int py;
Chris@1116 1140 if (m_params.invertVertical) {
Chris@1116 1141 py = y;
Chris@1116 1142 } else {
Chris@1116 1143 py = h - y - 1;
Chris@1116 1144 }
Chris@1083 1145 m_drawBuffer.setPixel
Chris@1083 1146 (x,
Chris@1116 1147 py,
Chris@1083 1148 m_params.colourScale.getPixel(pixelPeakColumn[y]));
Chris@1083 1149 }
Chris@1121 1150
Chris@1121 1151 m_magRanges.push_back(magRange);
Chris@1083 1152 }
Chris@1083 1153
Chris@1221 1154 double fractionComplete = double(xPixelCount) / double(w);
Chris@1083 1155 if (timer.outOfTime(fractionComplete)) {
Chris@1214 1156 #ifdef DEBUG_COLOUR_PLOT_REPAINT
Chris@1451 1157 SVDEBUG << "out of time with xPixelCount = " << xPixelCount << endl;
Chris@1214 1158 #endif
Chris@1221 1159 updateTimings(timer, xPixelCount);
Chris@1221 1160 return xPixelCount;
Chris@1083 1161 }
Chris@1083 1162 }
Chris@1083 1163
Chris@1221 1164 updateTimings(timer, xPixelCount);
Chris@1451 1165
Chris@1451 1166 #ifdef DEBUG_COLOUR_PLOT_REPAINT
Chris@1451 1167 SVDEBUG << "completed with xPixelCount = " << xPixelCount << endl;
Chris@1451 1168 #endif
Chris@1221 1169 return xPixelCount;
Chris@1083 1170 }
Chris@1083 1171
Chris@1097 1172 int
Chris@1113 1173 Colour3DPlotRenderer::renderDrawBufferPeakFrequencies(const LayerGeometryProvider *v,
Chris@1097 1174 int w, int h,
Chris@1097 1175 const vector<int> &binforx,
Chris@1097 1176 const vector<double> &binfory,
Chris@1097 1177 bool rightToLeft,
Chris@1097 1178 bool timeConstrained)
Chris@1097 1179 {
Chris@1097 1180 // Callers must have checked that the appropriate subset of
Chris@1097 1181 // Sources data members are set for the supplied flags (e.g. that
Chris@1097 1182 // fft model exists)
Chris@1097 1183
Chris@1097 1184 RenderTimer timer(timeConstrained ?
Chris@1221 1185 RenderTimer::SlowRender :
Chris@1097 1186 RenderTimer::NoTimeout);
Chris@1097 1187
Chris@1469 1188 auto fft = ModelById::getAs<FFTModel>(m_sources.fft);
Chris@1469 1189 if (!fft) return 0;
Chris@1135 1190
Chris@1135 1191 int sh = fft->getHeight();
Chris@1135 1192
Chris@1097 1193 int minbin = int(binfory[0] + 0.0001);
Chris@1135 1194 if (minbin >= sh) minbin = sh - 1;
Chris@1097 1195 if (minbin < 0) minbin = 0;
Chris@1097 1196
Chris@1135 1197 int nbins = int(binfory[h-1]) - minbin + 1;
Chris@1135 1198 if (minbin + nbins > sh) nbins = sh - minbin;
Chris@1097 1199
Chris@1097 1200 FFTModel::PeakSet peakfreqs;
Chris@1097 1201
Chris@1097 1202 int psx = -1;
Chris@1097 1203
Chris@1097 1204 int start = 0;
Chris@1097 1205 int finish = w;
Chris@1097 1206 int step = 1;
Chris@1097 1207
Chris@1097 1208 if (rightToLeft) {
Chris@1097 1209 start = w-1;
Chris@1097 1210 finish = -1;
Chris@1097 1211 step = -1;
Chris@1097 1212 }
Chris@1097 1213
Chris@1221 1214 int xPixelCount = 0;
Chris@1097 1215
Chris@1097 1216 vector<float> preparedColumn;
Chris@1097 1217
Chris@1097 1218 int modelWidth = fft->getWidth();
Chris@1143 1219 #ifdef DEBUG_COLOUR_PLOT_REPAINT
Chris@1214 1220 SVDEBUG << "modelWidth " << modelWidth << endl;
Chris@1143 1221 #endif
Chris@1143 1222
Chris@1135 1223 double minFreq =
Chris@1135 1224 (double(minbin) * fft->getSampleRate()) / fft->getFFTSize();
Chris@1135 1225 double maxFreq =
Chris@1135 1226 (double(minbin + nbins - 1) * fft->getSampleRate()) / fft->getFFTSize();
Chris@1097 1227
Chris@1103 1228 bool logarithmic = (m_params.binScale == BinScale::Log);
Chris@1217 1229
Chris@1218 1230 #ifdef DEBUG_COLOUR_PLOT_REPAINT
Chris@1218 1231 SVDEBUG << "start = " << start << ", finish = " << finish
Chris@1218 1232 << ", step = " << step << endl;
Chris@1218 1233 #endif
Chris@1218 1234
Chris@1097 1235 for (int x = start; x != finish; x += step) {
Chris@1097 1236
Chris@1097 1237 // x is the on-canvas pixel coord; sx (later) will be the
Chris@1097 1238 // source column index
Chris@1097 1239
Chris@1221 1240 ++xPixelCount;
Chris@1097 1241
Chris@1097 1242 if (binforx[x] < 0) continue;
Chris@1097 1243
Chris@1097 1244 int sx0 = binforx[x];
Chris@1097 1245 int sx1 = sx0;
Chris@1097 1246 if (x+1 < w) sx1 = binforx[x+1];
Chris@1097 1247 if (sx0 < 0) sx0 = sx1 - 1;
Chris@1097 1248 if (sx0 < 0) continue;
Chris@1097 1249 if (sx1 <= sx0) sx1 = sx0 + 1;
Chris@1097 1250
Chris@1097 1251 vector<float> pixelPeakColumn;
Chris@1121 1252 MagnitudeRange magRange;
Chris@1097 1253
Chris@1097 1254 for (int sx = sx0; sx < sx1; ++sx) {
Chris@1097 1255
Chris@1097 1256 if (sx < 0 || sx >= modelWidth) {
Chris@1097 1257 continue;
Chris@1097 1258 }
Chris@1097 1259
Chris@1097 1260 if (sx != psx) {
Chris@1219 1261 preparedColumn = getColumn(sx, minbin, nbins, -1);
Chris@1131 1262 magRange.sample(preparedColumn);
Chris@1097 1263 psx = sx;
Chris@1097 1264 }
Chris@1097 1265
Chris@1097 1266 if (sx == sx0) {
Chris@1097 1267 pixelPeakColumn = preparedColumn;
Chris@1097 1268 peakfreqs = fft->getPeakFrequencies(FFTModel::AllPeaks, sx,
Chris@1135 1269 minbin, minbin + nbins - 1);
Chris@1097 1270 } else {
Chris@1097 1271 for (int i = 0; in_range_for(pixelPeakColumn, i); ++i) {
Chris@1097 1272 pixelPeakColumn[i] = std::max(pixelPeakColumn[i],
Chris@1097 1273 preparedColumn[i]);
Chris@1097 1274 }
Chris@1097 1275 }
Chris@1097 1276 }
Chris@1097 1277
Chris@1097 1278 if (!pixelPeakColumn.empty()) {
Chris@1164 1279
Chris@1218 1280 #ifdef DEBUG_COLOUR_PLOT_REPAINT
Chris@1221 1281 // SVDEBUG << "found " << peakfreqs.size() << " peak freqs at column "
Chris@1221 1282 // << sx0 << endl;
Chris@1218 1283 #endif
Chris@1218 1284
Chris@1097 1285 for (FFTModel::PeakSet::const_iterator pi = peakfreqs.begin();
Chris@1097 1286 pi != peakfreqs.end(); ++pi) {
Chris@1097 1287
Chris@1097 1288 int bin = pi->first;
Chris@1097 1289 double freq = pi->second;
Chris@1097 1290
Chris@1097 1291 if (bin < minbin) continue;
Chris@1135 1292 if (bin >= minbin + nbins) break;
Chris@1097 1293
Chris@1097 1294 double value = pixelPeakColumn[bin - minbin];
Chris@1097 1295
Chris@1097 1296 double y = v->getYForFrequency
Chris@1097 1297 (freq, minFreq, maxFreq, logarithmic);
Chris@1097 1298
Chris@1097 1299 int iy = int(y + 0.5);
Chris@1097 1300 if (iy < 0 || iy >= h) continue;
Chris@1097 1301
Chris@1219 1302 auto pixel = m_params.colourScale.getPixel(value);
Chris@1219 1303
Chris@1219 1304 #ifdef DEBUG_COLOUR_PLOT_REPAINT
Chris@1219 1305 // SVDEBUG << "frequency " << freq << " for bin " << bin
Chris@1219 1306 // << " -> y = " << y << ", iy = " << iy << ", value = "
Chris@1219 1307 // << value << ", pixel " << pixel << "\n";
Chris@1219 1308 #endif
Chris@1219 1309
Chris@1219 1310 m_drawBuffer.setPixel(x, iy, pixel);
Chris@1097 1311 }
Chris@1121 1312
Chris@1121 1313 m_magRanges.push_back(magRange);
Chris@1218 1314
Chris@1218 1315 } else {
Chris@1218 1316 #ifdef DEBUG_COLOUR_PLOT_REPAINT
Chris@1218 1317 SVDEBUG << "pixel peak column for range " << sx0 << " to " << sx1
Chris@1218 1318 << " is empty" << endl;
Chris@1218 1319 #endif
Chris@1097 1320 }
Chris@1097 1321
Chris@1221 1322 double fractionComplete = double(xPixelCount) / double(w);
Chris@1097 1323 if (timer.outOfTime(fractionComplete)) {
Chris@1218 1324 #ifdef DEBUG_COLOUR_PLOT_REPAINT
Chris@1218 1325 SVDEBUG << "out of time" << endl;
Chris@1218 1326 #endif
Chris@1221 1327 updateTimings(timer, xPixelCount);
Chris@1221 1328 return xPixelCount;
Chris@1097 1329 }
Chris@1097 1330 }
Chris@1097 1331
Chris@1221 1332 updateTimings(timer, xPixelCount);
Chris@1221 1333 return xPixelCount;
Chris@1221 1334 }
Chris@1221 1335
Chris@1221 1336 void
Chris@1221 1337 Colour3DPlotRenderer::updateTimings(const RenderTimer &timer, int xPixelCount)
Chris@1221 1338 {
Chris@1237 1339 double secondsPerXPixel = timer.secondsPerItem(xPixelCount);
Chris@1221 1340
Chris@1237 1341 // valid if we have enough data points, or if the overall time is
Chris@1237 1342 // massively slow anyway (as we definitely need to warn about that)
Chris@1237 1343 bool valid = (xPixelCount > 20 || secondsPerXPixel > 0.01);
Chris@1237 1344
Chris@1237 1345 if (valid) {
Chris@1237 1346 m_secondsPerXPixel = secondsPerXPixel;
Chris@1237 1347 m_secondsPerXPixelValid = true;
Chris@1237 1348
Chris@1221 1349 #ifdef DEBUG_COLOUR_PLOT_REPAINT
Chris@1236 1350 SVDEBUG << "across " << xPixelCount << " x-pixels, seconds per x-pixel = "
Chris@1450 1351 << m_secondsPerXPixel << " (total = "
Chris@1499 1352 << (xPixelCount * m_secondsPerXPixel) << ")" << endl;
Chris@1221 1353 #endif
Chris@1237 1354 }
Chris@1097 1355 }
Chris@1097 1356
Chris@1079 1357 void
Chris@1095 1358 Colour3DPlotRenderer::recreateDrawBuffer(int w, int h)
Chris@1079 1359 {
Chris@1095 1360 m_drawBuffer = QImage(w, h, QImage::Format_Indexed8);
Chris@1079 1361
Chris@1095 1362 for (int pixel = 0; pixel < 256; ++pixel) {
Chris@1095 1363 m_drawBuffer.setColor
Chris@1095 1364 ((unsigned char)pixel,
Chris@1112 1365 m_params.colourScale.getColourForPixel
Chris@1112 1366 (pixel, m_params.colourRotation).rgb());
Chris@1079 1367 }
Chris@1079 1368
Chris@1079 1369 m_drawBuffer.fill(0);
Chris@1121 1370 m_magRanges.clear();
Chris@1079 1371 }
Chris@1079 1372
Chris@1095 1373 void
Chris@1095 1374 Colour3DPlotRenderer::clearDrawBuffer(int w, int h)
Chris@1095 1375 {
Chris@1095 1376 if (m_drawBuffer.width() < w || m_drawBuffer.height() != h) {
Chris@1095 1377 recreateDrawBuffer(w, h);
Chris@1095 1378 } else {
Chris@1095 1379 m_drawBuffer.fill(0);
Chris@1121 1380 m_magRanges.clear();
Chris@1095 1381 }
Chris@1095 1382 }
Chris@1079 1383
Chris@1139 1384 QRect
Chris@1139 1385 Colour3DPlotRenderer::findSimilarRegionExtents(QPoint p) const
Chris@1139 1386 {
Chris@1139 1387 QImage image = m_cache.getImage();
Chris@1139 1388 ImageRegionFinder finder;
Chris@1139 1389 QRect rect = finder.findRegionExtents(&image, p);
Chris@1139 1390 return rect;
Chris@1139 1391 }