annotate layer/Colour3DPlotLayer.cpp @ 1100:102f986ec032 spectrogram-minor-refactor

Create, but don't yet use, a renderer in the colour 3d plot layer
author Chris Cannam
date Wed, 13 Jul 2016 13:06:28 +0100
parents 5c6271734790
children 1364cbf4453d
rev   line source
Chris@58 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 2
Chris@0 3 /*
Chris@59 4 Sonic Visualiser
Chris@59 5 An audio file viewer and annotation editor.
Chris@59 6 Centre for Digital Music, Queen Mary, University of London.
Chris@182 7 This file copyright 2006 Chris Cannam and QMUL.
Chris@0 8
Chris@59 9 This program is free software; you can redistribute it and/or
Chris@59 10 modify it under the terms of the GNU General Public License as
Chris@59 11 published by the Free Software Foundation; either version 2 of the
Chris@59 12 License, or (at your option) any later version. See the file
Chris@59 13 COPYING included with this distribution for more information.
Chris@0 14 */
Chris@0 15
Chris@0 16 #include "Colour3DPlotLayer.h"
Chris@0 17
Chris@0 18 #include "base/Profiler.h"
Chris@197 19 #include "base/LogRange.h"
Chris@444 20 #include "base/RangeMapper.h"
Chris@1077 21
Chris@376 22 #include "ColourMapper.h"
Chris@1077 23 #include "LayerGeometryProvider.h"
Chris@1078 24 #include "PaintAssistant.h"
Chris@1077 25
Chris@1100 26 #include "data/model/Dense3DModelPeakCache.h"
Chris@1100 27
Chris@1077 28 #include "view/ViewManager.h"
Chris@0 29
Chris@0 30 #include <QPainter>
Chris@0 31 #include <QImage>
Chris@0 32 #include <QRect>
Chris@316 33 #include <QTextStream>
Chris@1018 34 #include <QSettings>
Chris@0 35
Chris@0 36 #include <iostream>
Chris@0 37
Chris@0 38 #include <cassert>
Chris@0 39
Chris@545 40 #ifndef __GNUC__
Chris@545 41 #include <alloca.h>
Chris@545 42 #endif
Chris@545 43
Chris@903 44 using std::vector;
Chris@903 45
Chris@353 46 //#define DEBUG_COLOUR_3D_PLOT_LAYER_PAINT 1
Chris@125 47
Chris@0 48
Chris@44 49 Colour3DPlotLayer::Colour3DPlotLayer() :
Chris@0 50 m_model(0),
Chris@159 51 m_cache(0),
Chris@469 52 m_peaksCache(0),
Chris@461 53 m_cacheValidStart(0),
Chris@461 54 m_cacheValidEnd(0),
Chris@1099 55 m_colourScale(ColourScale::LinearColourScale),
Chris@461 56 m_colourScaleSet(false),
Chris@197 57 m_colourMap(0),
Chris@534 58 m_gain(1.0),
Chris@1099 59 m_binScale(Colour3DPlotRenderer::LinearBinScale),
Chris@1099 60 m_normalization(ColumnOp::NoNormalization),
Chris@444 61 m_invertVertical(false),
Chris@465 62 m_opaque(false),
Chris@535 63 m_smooth(false),
Chris@805 64 m_peakResolution(256),
Chris@444 65 m_miny(0),
Chris@1100 66 m_maxy(0),
Chris@1100 67 m_peakCache(0),
Chris@1100 68 m_peakCacheDivisor(8)
Chris@0 69 {
Chris@1018 70 QSettings settings;
Chris@1018 71 settings.beginGroup("Preferences");
Chris@1018 72 setColourMap(settings.value("colour-3d-plot-colour", ColourMapper::Green).toInt());
Chris@1018 73 settings.endGroup();
Chris@0 74 }
Chris@0 75
Chris@0 76 Colour3DPlotLayer::~Colour3DPlotLayer()
Chris@0 77 {
Chris@461 78 delete m_cache;
Chris@1100 79 delete m_peaksCache; //!!! this one is to go...
Chris@1100 80 delete m_peakCache;
Chris@0 81 }
Chris@0 82
Chris@0 83 void
Chris@0 84 Colour3DPlotLayer::setModel(const DenseThreeDimensionalModel *model)
Chris@0 85 {
Chris@193 86 if (m_model == model) return;
Chris@193 87 const DenseThreeDimensionalModel *oldModel = m_model;
Chris@0 88 m_model = model;
Chris@0 89 if (!m_model || !m_model->isOK()) return;
Chris@0 90
Chris@320 91 connectSignals(m_model);
Chris@0 92
Chris@461 93 connect(m_model, SIGNAL(modelChanged()), this, SLOT(modelChanged()));
Chris@908 94 connect(m_model, SIGNAL(modelChangedWithin(sv_frame_t, sv_frame_t)),
Chris@908 95 this, SLOT(modelChangedWithin(sv_frame_t, sv_frame_t)));
Chris@0 96
Chris@474 97 m_peakResolution = 256;
Chris@474 98 if (model->getResolution() > 512) {
Chris@474 99 m_peakResolution = 16;
Chris@474 100 } else if (model->getResolution() > 128) {
Chris@474 101 m_peakResolution = 64;
Chris@474 102 } else if (model->getResolution() > 2) {
Chris@474 103 m_peakResolution = 128;
Chris@474 104 }
Chris@1100 105
Chris@1100 106 delete m_peakCache;
Chris@1100 107 m_peakCache = 0;
Chris@1100 108
Chris@474 109 cacheInvalid();
Chris@474 110
Chris@0 111 emit modelReplaced();
Chris@193 112 emit sliceableModelReplaced(oldModel, model);
Chris@0 113 }
Chris@0 114
Chris@0 115 void
Chris@0 116 Colour3DPlotLayer::cacheInvalid()
Chris@0 117 {
Chris@1100 118 //!!! to go
Chris@1100 119
Chris@469 120 delete m_cache;
Chris@469 121 delete m_peaksCache;
Chris@0 122 m_cache = 0;
Chris@469 123 m_peaksCache = 0;
Chris@461 124 m_cacheValidStart = 0;
Chris@461 125 m_cacheValidEnd = 0;
Chris@0 126 }
Chris@0 127
Chris@0 128 void
Chris@908 129 Colour3DPlotLayer::cacheInvalid(sv_frame_t startFrame, sv_frame_t endFrame)
Chris@0 130 {
Chris@1100 131 //!!! to go
Chris@1100 132
Chris@843 133 if (!m_cache || !m_model) return;
Chris@461 134
Chris@805 135 int modelResolution = m_model->getResolution();
Chris@908 136 int start = int(startFrame / modelResolution);
Chris@908 137 int end = int(endFrame / modelResolution + 1);
Chris@461 138 if (m_cacheValidStart < end) m_cacheValidStart = end;
Chris@461 139 if (m_cacheValidEnd > start) m_cacheValidEnd = start;
Chris@461 140 if (m_cacheValidStart > m_cacheValidEnd) m_cacheValidEnd = m_cacheValidStart;
Chris@461 141 }
Chris@461 142
Chris@1100 143 Dense3DModelPeakCache *
Chris@1100 144 Colour3DPlotLayer::getPeakCache() const
Chris@1100 145 {
Chris@1100 146 if (!m_peakCache) {
Chris@1100 147 m_peakCache = new Dense3DModelPeakCache(m_model, m_peakCacheDivisor);
Chris@1100 148 }
Chris@1100 149 return m_peakCache;
Chris@1100 150 }
Chris@1100 151
Chris@461 152 void
Chris@461 153 Colour3DPlotLayer::modelChanged()
Chris@461 154 {
Chris@1099 155 if (!m_colourScaleSet && m_colourScale == ColourScale::LinearColourScale) {
Chris@461 156 if (m_model) {
Chris@461 157 if (m_model->shouldUseLogValueScale()) {
Chris@1099 158 setColourScale(ColourScale::LogColourScale);
Chris@461 159 } else {
Chris@461 160 m_colourScaleSet = true;
Chris@461 161 }
Chris@461 162 }
Chris@461 163 }
Chris@0 164 cacheInvalid();
Chris@0 165 }
Chris@0 166
Chris@461 167 void
Chris@908 168 Colour3DPlotLayer::modelChangedWithin(sv_frame_t startFrame, sv_frame_t endFrame)
Chris@461 169 {
Chris@1099 170 if (!m_colourScaleSet && m_colourScale == ColourScale::LinearColourScale) {
Chris@461 171 if (m_model && m_model->getWidth() > 50) {
Chris@461 172 if (m_model->shouldUseLogValueScale()) {
Chris@1099 173 setColourScale(ColourScale::LogColourScale);
Chris@461 174 } else {
Chris@461 175 m_colourScaleSet = true;
Chris@461 176 }
Chris@461 177 }
Chris@461 178 }
Chris@461 179 cacheInvalid(startFrame, endFrame);
Chris@461 180 }
Chris@461 181
Chris@159 182 Layer::PropertyList
Chris@159 183 Colour3DPlotLayer::getProperties() const
Chris@159 184 {
Chris@159 185 PropertyList list;
Chris@197 186 list.push_back("Colour");
Chris@159 187 list.push_back("Colour Scale");
Chris@197 188 list.push_back("Normalize Columns");
Chris@197 189 list.push_back("Normalize Visible Area");
Chris@534 190 list.push_back("Gain");
Chris@531 191 list.push_back("Bin Scale");
Chris@357 192 list.push_back("Invert Vertical Scale");
Chris@465 193 list.push_back("Opaque");
Chris@535 194 list.push_back("Smooth");
Chris@159 195 return list;
Chris@159 196 }
Chris@159 197
Chris@159 198 QString
Chris@159 199 Colour3DPlotLayer::getPropertyLabel(const PropertyName &name) const
Chris@159 200 {
Chris@197 201 if (name == "Colour") return tr("Colour");
Chris@197 202 if (name == "Colour Scale") return tr("Scale");
Chris@197 203 if (name == "Normalize Columns") return tr("Normalize Columns");
Chris@197 204 if (name == "Normalize Visible Area") return tr("Normalize Visible Area");
Chris@357 205 if (name == "Invert Vertical Scale") return tr("Invert Vertical Scale");
Chris@534 206 if (name == "Gain") return tr("Gain");
Chris@465 207 if (name == "Opaque") return tr("Always Opaque");
Chris@535 208 if (name == "Smooth") return tr("Smooth");
Chris@531 209 if (name == "Bin Scale") return tr("Bin Scale");
Chris@159 210 return "";
Chris@159 211 }
Chris@159 212
Chris@346 213 QString
Chris@346 214 Colour3DPlotLayer::getPropertyIconName(const PropertyName &name) const
Chris@346 215 {
Chris@346 216 if (name == "Normalize Columns") return "normalise-columns";
Chris@346 217 if (name == "Normalize Visible Area") return "normalise";
Chris@357 218 if (name == "Invert Vertical Scale") return "invert-vertical";
Chris@465 219 if (name == "Opaque") return "opaque";
Chris@535 220 if (name == "Smooth") return "smooth";
Chris@346 221 return "";
Chris@346 222 }
Chris@346 223
Chris@159 224 Layer::PropertyType
Chris@159 225 Colour3DPlotLayer::getPropertyType(const PropertyName &name) const
Chris@159 226 {
Chris@534 227 if (name == "Gain") return RangeProperty;
Chris@197 228 if (name == "Normalize Columns") return ToggleProperty;
Chris@197 229 if (name == "Normalize Visible Area") return ToggleProperty;
Chris@357 230 if (name == "Invert Vertical Scale") return ToggleProperty;
Chris@465 231 if (name == "Opaque") return ToggleProperty;
Chris@535 232 if (name == "Smooth") return ToggleProperty;
Chris@159 233 return ValueProperty;
Chris@159 234 }
Chris@159 235
Chris@159 236 QString
Chris@159 237 Colour3DPlotLayer::getPropertyGroupName(const PropertyName &name) const
Chris@159 238 {
Chris@197 239 if (name == "Normalize Columns" ||
Chris@197 240 name == "Normalize Visible Area" ||
Chris@534 241 name == "Colour Scale" ||
Chris@534 242 name == "Gain") return tr("Scale");
Chris@531 243 if (name == "Bin Scale" ||
Chris@531 244 name == "Invert Vertical Scale") return tr("Bins");
Chris@465 245 if (name == "Opaque" ||
Chris@535 246 name == "Smooth" ||
Chris@465 247 name == "Colour") return tr("Colour");
Chris@159 248 return QString();
Chris@159 249 }
Chris@159 250
Chris@159 251 int
Chris@159 252 Colour3DPlotLayer::getPropertyRangeAndValue(const PropertyName &name,
Chris@216 253 int *min, int *max, int *deflt) const
Chris@159 254 {
Chris@216 255 int val = 0;
Chris@159 256
Chris@216 257 int garbage0, garbage1, garbage2;
Chris@159 258 if (!min) min = &garbage0;
Chris@159 259 if (!max) max = &garbage1;
Chris@216 260 if (!deflt) deflt = &garbage2;
Chris@159 261
Chris@534 262 if (name == "Gain") {
Chris@534 263
Chris@534 264 *min = -50;
Chris@534 265 *max = 50;
Chris@534 266
Chris@902 267 *deflt = int(lrint(log10(1.0) * 20.0));
Chris@534 268 if (*deflt < *min) *deflt = *min;
Chris@534 269 if (*deflt > *max) *deflt = *max;
Chris@534 270
Chris@902 271 val = int(lrint(log10(m_gain) * 20.0));
Chris@534 272 if (val < *min) val = *min;
Chris@534 273 if (val > *max) val = *max;
Chris@534 274
Chris@534 275 } else if (name == "Colour Scale") {
Chris@159 276
Chris@1099 277 // linear, log, +/-1, abs
Chris@159 278 *min = 0;
Chris@509 279 *max = 3;
Chris@1099 280 *deflt = 0;
Chris@159 281
Chris@216 282 val = (int)m_colourScale;
Chris@159 283
Chris@197 284 } else if (name == "Colour") {
Chris@197 285
Chris@197 286 *min = 0;
Chris@197 287 *max = ColourMapper::getColourMapCount() - 1;
Chris@216 288 *deflt = 0;
Chris@197 289
Chris@216 290 val = m_colourMap;
Chris@197 291
Chris@1099 292 } else if (name == "Normalization") {
Chris@197 293
Chris@1099 294 *min = 0;
Chris@1099 295 *max = 3;
Chris@1099 296 *deflt = int(ColumnOp::NoNormalization);
Chris@1099 297 val = (int)m_normalization;
Chris@197 298
Chris@357 299 } else if (name == "Invert Vertical Scale") {
Chris@357 300
Chris@357 301 *deflt = 0;
Chris@357 302 val = (m_invertVertical ? 1 : 0);
Chris@357 303
Chris@531 304 } else if (name == "Bin Scale") {
Chris@531 305
Chris@531 306 *min = 0;
Chris@531 307 *max = 1;
Chris@1099 308 *deflt = int(Colour3DPlotRenderer::LinearBinScale);
Chris@531 309 val = (int)m_binScale;
Chris@531 310
Chris@465 311 } else if (name == "Opaque") {
Chris@465 312
Chris@465 313 *deflt = 0;
Chris@465 314 val = (m_opaque ? 1 : 0);
Chris@465 315
Chris@535 316 } else if (name == "Smooth") {
Chris@535 317
Chris@535 318 *deflt = 0;
Chris@535 319 val = (m_smooth ? 1 : 0);
Chris@535 320
Chris@159 321 } else {
Chris@216 322 val = Layer::getPropertyRangeAndValue(name, min, max, deflt);
Chris@159 323 }
Chris@159 324
Chris@216 325 return val;
Chris@159 326 }
Chris@159 327
Chris@159 328 QString
Chris@159 329 Colour3DPlotLayer::getPropertyValueLabel(const PropertyName &name,
Chris@159 330 int value) const
Chris@159 331 {
Chris@197 332 if (name == "Colour") {
Chris@197 333 return ColourMapper::getColourMapName(value);
Chris@197 334 }
Chris@159 335 if (name == "Colour Scale") {
Chris@159 336 switch (value) {
Chris@159 337 default:
Chris@1099 338 //!!! yuck
Chris@198 339 case 0: return tr("Linear");
Chris@198 340 case 1: return tr("Log");
Chris@198 341 case 2: return tr("+/-1");
Chris@509 342 case 3: return tr("Absolute");
Chris@159 343 }
Chris@159 344 }
Chris@1099 345 if (name == "Normalization") {
Chris@1099 346 return ""; // icon only
Chris@1099 347 }
Chris@531 348 if (name == "Bin Scale") {
Chris@531 349 switch (value) {
Chris@531 350 default:
Chris@531 351 case 0: return tr("Linear");
Chris@531 352 case 1: return tr("Log");
Chris@531 353 }
Chris@531 354 }
Chris@159 355 return tr("<unknown>");
Chris@159 356 }
Chris@159 357
Chris@1099 358 QString
Chris@1099 359 Colour3DPlotLayer::getPropertyValueIconName(const PropertyName &name,
Chris@1099 360 int value) const
Chris@1099 361 {
Chris@1099 362 if (name == "Normalization") {
Chris@1099 363 switch(value) {
Chris@1099 364 default:
Chris@1099 365 case 0: return "normalise-none";
Chris@1099 366 case 1: return "normalise-columns";
Chris@1099 367 case 2: return "normalise";
Chris@1099 368 case 3: return "normalise-hybrid";
Chris@1099 369 }
Chris@1099 370 }
Chris@1099 371 return "";
Chris@1099 372 }
Chris@1099 373
Chris@534 374 RangeMapper *
Chris@534 375 Colour3DPlotLayer::getNewPropertyRangeMapper(const PropertyName &name) const
Chris@534 376 {
Chris@534 377 if (name == "Gain") {
Chris@534 378 return new LinearRangeMapper(-50, 50, -25, 25, tr("dB"));
Chris@534 379 }
Chris@534 380 return 0;
Chris@534 381 }
Chris@534 382
Chris@159 383 void
Chris@159 384 Colour3DPlotLayer::setProperty(const PropertyName &name, int value)
Chris@159 385 {
Chris@534 386 if (name == "Gain") {
Chris@902 387 setGain(float(pow(10, value/20.0)));
Chris@534 388 } else if (name == "Colour Scale") {
Chris@159 389 switch (value) {
Chris@159 390 default:
Chris@1099 391 case 0: setColourScale(ColourScale::LinearColourScale); break;
Chris@1099 392 case 1: setColourScale(ColourScale::LogColourScale); break;
Chris@1099 393 case 2: setColourScale(ColourScale::PlusMinusOneScale); break;
Chris@1099 394 case 3: setColourScale(ColourScale::AbsoluteScale); break;
Chris@159 395 }
Chris@197 396 } else if (name == "Colour") {
Chris@197 397 setColourMap(value);
Chris@357 398 } else if (name == "Invert Vertical Scale") {
Chris@357 399 setInvertVertical(value ? true : false);
Chris@465 400 } else if (name == "Opaque") {
Chris@465 401 setOpaque(value ? true : false);
Chris@535 402 } else if (name == "Smooth") {
Chris@535 403 setSmooth(value ? true : false);
Chris@531 404 } else if (name == "Bin Scale") {
Chris@531 405 switch (value) {
Chris@531 406 default:
Chris@1099 407 case 0: setBinScale(Colour3DPlotRenderer::LinearBinScale); break;
Chris@1099 408 case 1: setBinScale(Colour3DPlotRenderer::LogBinScale); break;
Chris@531 409 }
Chris@1099 410 } else if (name == "Normalization") {
Chris@1099 411 switch (value) {
Chris@1099 412 default:
Chris@1099 413 case 0: setNormalization(ColumnOp::NoNormalization); break;
Chris@1099 414 case 1: setNormalization(ColumnOp::NormalizeColumns); break;
Chris@1099 415 case 2: setNormalization(ColumnOp::NormalizeVisibleArea); break;
Chris@1099 416 case 3: setNormalization(ColumnOp::NormalizeHybrid); break;
Chris@1099 417 }
Chris@159 418 }
Chris@159 419 }
Chris@159 420
Chris@159 421 void
Chris@1099 422 Colour3DPlotLayer::setColourScale(ColourScale::Scale scale)
Chris@159 423 {
Chris@159 424 if (m_colourScale == scale) return;
Chris@159 425 m_colourScale = scale;
Chris@461 426 m_colourScaleSet = true;
Chris@159 427 cacheInvalid();
Chris@159 428 emit layerParametersChanged();
Chris@159 429 }
Chris@159 430
Chris@197 431 void
Chris@197 432 Colour3DPlotLayer::setColourMap(int map)
Chris@197 433 {
Chris@197 434 if (m_colourMap == map) return;
Chris@197 435 m_colourMap = map;
Chris@197 436 cacheInvalid();
Chris@197 437 emit layerParametersChanged();
Chris@197 438 }
Chris@197 439
Chris@197 440 void
Chris@534 441 Colour3DPlotLayer::setGain(float gain)
Chris@534 442 {
Chris@534 443 if (m_gain == gain) return;
Chris@534 444 m_gain = gain;
Chris@534 445 cacheInvalid();
Chris@534 446 emit layerParametersChanged();
Chris@534 447 }
Chris@534 448
Chris@534 449 float
Chris@534 450 Colour3DPlotLayer::getGain() const
Chris@534 451 {
Chris@534 452 return m_gain;
Chris@534 453 }
Chris@534 454
Chris@534 455 void
Chris@1099 456 Colour3DPlotLayer::setBinScale(Colour3DPlotRenderer::BinScale binScale)
Chris@531 457 {
Chris@531 458 if (m_binScale == binScale) return;
Chris@531 459 m_binScale = binScale;
Chris@531 460 cacheInvalid();
Chris@531 461 emit layerParametersChanged();
Chris@531 462 }
Chris@531 463
Chris@1099 464 Colour3DPlotRenderer::BinScale
Chris@531 465 Colour3DPlotLayer::getBinScale() const
Chris@531 466 {
Chris@531 467 return m_binScale;
Chris@531 468 }
Chris@531 469
Chris@531 470 void
Chris@1099 471 Colour3DPlotLayer::setNormalization(ColumnOp::Normalization n)
Chris@197 472 {
Chris@1099 473 if (m_normalization == n) return;
Chris@1099 474
Chris@1099 475 m_normalization = n;
Chris@197 476 cacheInvalid();
Chris@1099 477
Chris@197 478 emit layerParametersChanged();
Chris@197 479 }
Chris@197 480
Chris@1099 481 ColumnOp::Normalization
Chris@1099 482 Colour3DPlotLayer::getNormalization() const
Chris@197 483 {
Chris@1099 484 return m_normalization;
Chris@197 485 }
Chris@197 486
Chris@357 487 void
Chris@357 488 Colour3DPlotLayer::setInvertVertical(bool n)
Chris@357 489 {
Chris@357 490 if (m_invertVertical == n) return;
Chris@357 491 m_invertVertical = n;
Chris@357 492 cacheInvalid();
Chris@357 493 emit layerParametersChanged();
Chris@357 494 }
Chris@357 495
Chris@465 496 void
Chris@465 497 Colour3DPlotLayer::setOpaque(bool n)
Chris@465 498 {
Chris@465 499 if (m_opaque == n) return;
Chris@465 500 m_opaque = n;
Chris@465 501 emit layerParametersChanged();
Chris@465 502 }
Chris@465 503
Chris@535 504 void
Chris@535 505 Colour3DPlotLayer::setSmooth(bool n)
Chris@535 506 {
Chris@535 507 if (m_smooth == n) return;
Chris@535 508 m_smooth = n;
Chris@535 509 emit layerParametersChanged();
Chris@535 510 }
Chris@535 511
Chris@357 512 bool
Chris@357 513 Colour3DPlotLayer::getInvertVertical() const
Chris@357 514 {
Chris@357 515 return m_invertVertical;
Chris@357 516 }
Chris@357 517
Chris@25 518 bool
Chris@465 519 Colour3DPlotLayer::getOpaque() const
Chris@465 520 {
Chris@465 521 return m_opaque;
Chris@465 522 }
Chris@465 523
Chris@535 524 bool
Chris@535 525 Colour3DPlotLayer::getSmooth() const
Chris@535 526 {
Chris@535 527 return m_smooth;
Chris@535 528 }
Chris@535 529
Chris@475 530 void
Chris@916 531 Colour3DPlotLayer::setLayerDormant(const LayerGeometryProvider *v, bool dormant)
Chris@475 532 {
Chris@475 533 if (dormant) {
Chris@475 534
Chris@475 535 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@812 536 cerr << "Colour3DPlotLayer::setLayerDormant(" << dormant << ")"
Chris@585 537 << endl;
Chris@475 538 #endif
Chris@475 539
Chris@475 540 if (isLayerDormant(v)) {
Chris@475 541 return;
Chris@475 542 }
Chris@475 543
Chris@475 544 Layer::setLayerDormant(v, true);
Chris@475 545
Chris@475 546 cacheInvalid();
Chris@475 547
Chris@475 548 } else {
Chris@475 549
Chris@475 550 Layer::setLayerDormant(v, false);
Chris@475 551 }
Chris@475 552 }
Chris@475 553
Chris@465 554 bool
Chris@916 555 Colour3DPlotLayer::isLayerScrollable(const LayerGeometryProvider *v) const
Chris@25 556 {
Chris@1099 557 if (m_normalization == ColumnOp::NormalizeVisibleArea) {
Chris@812 558 return false;
Chris@812 559 }
Chris@812 560 if (shouldPaintDenseIn(v)) {
Chris@812 561 return true;
Chris@812 562 }
Chris@25 563 QPoint discard;
Chris@44 564 return !v->shouldIlluminateLocalFeatures(this, discard);
Chris@25 565 }
Chris@25 566
Chris@444 567 bool
Chris@904 568 Colour3DPlotLayer::getValueExtents(double &min, double &max,
Chris@444 569 bool &logarithmic, QString &unit) const
Chris@444 570 {
Chris@444 571 if (!m_model) return false;
Chris@444 572
Chris@444 573 min = 0;
Chris@904 574 max = double(m_model->getHeight());
Chris@444 575
Chris@444 576 logarithmic = false;
Chris@444 577 unit = "";
Chris@444 578
Chris@444 579 return true;
Chris@444 580 }
Chris@444 581
Chris@444 582 bool
Chris@904 583 Colour3DPlotLayer::getDisplayExtents(double &min, double &max) const
Chris@444 584 {
Chris@444 585 if (!m_model) return false;
Chris@444 586
Chris@904 587 double hmax = double(m_model->getHeight());
Chris@902 588
Chris@904 589 min = m_miny;
Chris@904 590 max = m_maxy;
Chris@444 591 if (max <= min) {
Chris@444 592 min = 0;
Chris@902 593 max = hmax;
Chris@444 594 }
Chris@444 595 if (min < 0) min = 0;
Chris@902 596 if (max > hmax) max = hmax;
Chris@444 597
Chris@444 598 return true;
Chris@444 599 }
Chris@444 600
Chris@444 601 bool
Chris@904 602 Colour3DPlotLayer::setDisplayExtents(double min, double max)
Chris@444 603 {
Chris@444 604 if (!m_model) return false;
Chris@444 605
Chris@904 606 m_miny = int(lrint(min));
Chris@904 607 m_maxy = int(lrint(max));
Chris@444 608
Chris@444 609 emit layerParametersChanged();
Chris@444 610 return true;
Chris@444 611 }
Chris@444 612
Chris@725 613 bool
Chris@916 614 Colour3DPlotLayer::getYScaleValue(const LayerGeometryProvider *, int,
Chris@904 615 double &, QString &) const
Chris@725 616 {
Chris@725 617 return false;//!!!
Chris@725 618 }
Chris@725 619
Chris@444 620 int
Chris@444 621 Colour3DPlotLayer::getVerticalZoomSteps(int &defaultStep) const
Chris@444 622 {
Chris@444 623 if (!m_model) return 0;
Chris@444 624
Chris@444 625 defaultStep = 0;
Chris@444 626 int h = m_model->getHeight();
Chris@444 627 return h;
Chris@444 628 }
Chris@444 629
Chris@444 630 int
Chris@444 631 Colour3DPlotLayer::getCurrentVerticalZoomStep() const
Chris@444 632 {
Chris@444 633 if (!m_model) return 0;
Chris@444 634
Chris@904 635 double min, max;
Chris@444 636 getDisplayExtents(min, max);
Chris@904 637 return m_model->getHeight() - int(lrint(max - min));
Chris@444 638 }
Chris@444 639
Chris@444 640 void
Chris@444 641 Colour3DPlotLayer::setVerticalZoomStep(int step)
Chris@444 642 {
Chris@444 643 if (!m_model) return;
Chris@444 644
Chris@587 645 // SVDEBUG << "Colour3DPlotLayer::setVerticalZoomStep(" <<step <<"): before: miny = " << m_miny << ", maxy = " << m_maxy << endl;
Chris@444 646
Chris@444 647 int dist = m_model->getHeight() - step;
Chris@444 648 if (dist < 1) dist = 1;
Chris@904 649 double centre = m_miny + (m_maxy - m_miny) / 2.0;
Chris@904 650 m_miny = int(lrint(centre - dist/2.0));
Chris@444 651 if (m_miny < 0) m_miny = 0;
Chris@444 652 m_maxy = m_miny + dist;
Chris@444 653 if (m_maxy > m_model->getHeight()) m_maxy = m_model->getHeight();
Chris@444 654
Chris@587 655 // SVDEBUG << "Colour3DPlotLayer::setVerticalZoomStep(" <<step <<"): after: miny = " << m_miny << ", maxy = " << m_maxy << endl;
Chris@444 656
Chris@444 657 emit layerParametersChanged();
Chris@444 658 }
Chris@444 659
Chris@444 660 RangeMapper *
Chris@444 661 Colour3DPlotLayer::getNewVerticalZoomRangeMapper() const
Chris@444 662 {
Chris@444 663 if (!m_model) return 0;
Chris@444 664
Chris@444 665 return new LinearRangeMapper(0, m_model->getHeight(),
Chris@444 666 0, m_model->getHeight(), "");
Chris@444 667 }
Chris@444 668
Chris@904 669 double
Chris@916 670 Colour3DPlotLayer::getYForBin(LayerGeometryProvider *v, double bin) const
Chris@532 671 {
Chris@904 672 double y = bin;
Chris@532 673 if (!m_model) return y;
Chris@904 674 double mn = 0, mx = m_model->getHeight();
Chris@532 675 getDisplayExtents(mn, mx);
Chris@916 676 double h = v->getPaintHeight();
Chris@1099 677 if (m_binScale == Colour3DPlotRenderer::LinearBinScale) {
Chris@532 678 y = h - (((bin - mn) * h) / (mx - mn));
Chris@532 679 } else {
Chris@904 680 double logmin = mn + 1, logmax = mx + 1;
Chris@532 681 LogRange::mapRange(logmin, logmax);
Chris@532 682 y = h - (((LogRange::map(bin + 1) - logmin) * h) / (logmax - logmin));
Chris@532 683 }
Chris@532 684 return y;
Chris@532 685 }
Chris@532 686
Chris@904 687 double
Chris@916 688 Colour3DPlotLayer::getBinForY(LayerGeometryProvider *v, double y) const
Chris@532 689 {
Chris@904 690 double bin = y;
Chris@532 691 if (!m_model) return bin;
Chris@904 692 double mn = 0, mx = m_model->getHeight();
Chris@532 693 getDisplayExtents(mn, mx);
Chris@916 694 double h = v->getPaintHeight();
Chris@1099 695 if (m_binScale == Colour3DPlotRenderer::LinearBinScale) {
Chris@532 696 bin = mn + ((h - y) * (mx - mn)) / h;
Chris@532 697 } else {
Chris@904 698 double logmin = mn + 1, logmax = mx + 1;
Chris@532 699 LogRange::mapRange(logmin, logmax);
Chris@532 700 bin = LogRange::unmap(logmin + ((h - y) * (logmax - logmin)) / h) - 1;
Chris@532 701 }
Chris@532 702 return bin;
Chris@532 703 }
Chris@532 704
Chris@25 705 QString
Chris@916 706 Colour3DPlotLayer::getFeatureDescription(LayerGeometryProvider *v, QPoint &pos) const
Chris@25 707 {
Chris@25 708 if (!m_model) return "";
Chris@25 709
Chris@25 710 int x = pos.x();
Chris@25 711 int y = pos.y();
Chris@25 712
Chris@902 713 sv_frame_t modelStart = m_model->getStartFrame();
Chris@805 714 int modelResolution = m_model->getResolution();
Chris@25 715
Chris@902 716 double srRatio =
Chris@902 717 v->getViewManager()->getMainModelSampleRate() /
Chris@902 718 m_model->getSampleRate();
Chris@159 719
Chris@902 720 int sx0 = int((double(v->getFrameForX(x)) / srRatio - double(modelStart)) /
Chris@812 721 modelResolution);
Chris@25 722
Chris@160 723 int f0 = sx0 * modelResolution;
Chris@160 724 int f1 = f0 + modelResolution;
Chris@160 725
Chris@447 726 int sh = m_model->getHeight();
Chris@447 727
Chris@447 728 int symin = m_miny;
Chris@447 729 int symax = m_maxy;
Chris@447 730 if (symax <= symin) {
Chris@447 731 symin = 0;
Chris@447 732 symax = sh;
Chris@447 733 }
Chris@447 734 if (symin < 0) symin = 0;
Chris@447 735 if (symax > sh) symax = sh;
Chris@447 736
Chris@916 737 // double binHeight = double(v->getPaintHeight()) / (symax - symin);
Chris@916 738 // int sy = int((v->getPaintHeight() - y) / binHeight) + symin;
Chris@534 739
Chris@903 740 int sy = getIBinForY(v, y);
Chris@25 741
Chris@812 742 if (sy < 0 || sy >= m_model->getHeight()) {
Chris@812 743 return "";
Chris@812 744 }
Chris@812 745
Chris@357 746 if (m_invertVertical) sy = m_model->getHeight() - sy - 1;
Chris@357 747
Chris@160 748 float value = m_model->getValueAt(sx0, sy);
Chris@159 749
Chris@682 750 // cerr << "bin value (" << sx0 << "," << sy << ") is " << value << endl;
Chris@25 751
Chris@25 752 QString binName = m_model->getBinName(sy);
Chris@25 753 if (binName == "") binName = QString("[%1]").arg(sy + 1);
Chris@25 754 else binName = QString("%1 [%2]").arg(binName).arg(sy + 1);
Chris@25 755
Chris@25 756 QString text = tr("Time:\t%1 - %2\nBin:\t%3\nValue:\t%4")
Chris@160 757 .arg(RealTime::frame2RealTime(f0, m_model->getSampleRate())
Chris@25 758 .toText(true).c_str())
Chris@160 759 .arg(RealTime::frame2RealTime(f1, m_model->getSampleRate())
Chris@25 760 .toText(true).c_str())
Chris@25 761 .arg(binName)
Chris@25 762 .arg(value);
Chris@25 763
Chris@25 764 return text;
Chris@25 765 }
Chris@25 766
Chris@25 767 int
Chris@969 768 Colour3DPlotLayer::getColourScaleWidth(QPainter &p) const
Chris@159 769 {
Chris@969 770 // Font is rotated
Chris@969 771 int cw = p.fontMetrics().height();
Chris@159 772 return cw;
Chris@159 773 }
Chris@159 774
Chris@159 775 int
Chris@916 776 Colour3DPlotLayer::getVerticalScaleWidth(LayerGeometryProvider *, bool, QPainter &paint) const
Chris@25 777 {
Chris@25 778 if (!m_model) return 0;
Chris@25 779
Chris@160 780 QString sampleText = QString("[%1]").arg(m_model->getHeight());
Chris@25 781 int tw = paint.fontMetrics().width(sampleText);
Chris@98 782 bool another = false;
Chris@25 783
Chris@805 784 for (int i = 0; i < m_model->getHeight(); ++i) {
Chris@25 785 if (m_model->getBinName(i).length() > sampleText.length()) {
Chris@25 786 sampleText = m_model->getBinName(i);
Chris@98 787 another = true;
Chris@25 788 }
Chris@25 789 }
Chris@98 790 if (another) {
Chris@25 791 tw = std::max(tw, paint.fontMetrics().width(sampleText));
Chris@25 792 }
Chris@25 793
Chris@159 794 return tw + 13 + getColourScaleWidth(paint);
Chris@25 795 }
Chris@25 796
Chris@25 797 void
Chris@916 798 Colour3DPlotLayer::paintVerticalScale(LayerGeometryProvider *v, bool, QPainter &paint, QRect rect) const
Chris@25 799 {
Chris@25 800 if (!m_model) return;
Chris@25 801
Chris@25 802 int h = rect.height(), w = rect.width();
Chris@25 803
Chris@159 804 int cw = getColourScaleWidth(paint);
Chris@159 805
Chris@159 806 int ch = h - 20;
Chris@159 807 if (ch > 20 && m_cache) {
Chris@159 808
Chris@904 809 double min = m_model->getMinimumLevel();
Chris@904 810 double max = m_model->getMaximumLevel();
Chris@447 811
Chris@904 812 double mmin = min;
Chris@904 813 double mmax = max;
Chris@447 814
Chris@1099 815 if (m_colourScale == ColourScale::LogColourScale) {
Chris@447 816 LogRange::mapRange(mmin, mmax);
Chris@1099 817 } else if (m_colourScale == ColourScale::PlusMinusOneScale) {
Chris@447 818 mmin = -1.f;
Chris@447 819 mmax = 1.f;
Chris@1099 820 } else if (m_colourScale == ColourScale::AbsoluteScale) {
Chris@509 821 if (mmin < 0) {
Chris@904 822 if (fabs(mmin) > fabs(mmax)) mmax = fabs(mmin);
Chris@904 823 else mmax = fabs(mmax);
Chris@509 824 mmin = 0;
Chris@509 825 } else {
Chris@904 826 mmin = fabs(mmin);
Chris@904 827 mmax = fabs(mmax);
Chris@509 828 }
Chris@447 829 }
Chris@447 830
Chris@902 831 if (max == min) max = min + 1.f;
Chris@902 832 if (mmax == mmin) mmax = mmin + 1.f;
Chris@447 833
Chris@287 834 paint.setPen(v->getForeground());
Chris@447 835 paint.drawRect(4, 10, cw - 8, ch+1);
Chris@159 836
Chris@446 837 for (int y = 0; y < ch; ++y) {
Chris@904 838 double value = ((max - min) * (double(ch-y) - 1.0)) / double(ch) + min;
Chris@1099 839 if (m_colourScale == ColourScale::LogColourScale) {
Chris@447 840 value = LogRange::map(value);
Chris@447 841 }
Chris@447 842 int pixel = int(((value - mmin) * 256) / (mmax - mmin));
Chris@465 843 if (pixel >= 0 && pixel < 256) {
Chris@465 844 QRgb c = m_cache->color(pixel);
Chris@465 845 paint.setPen(QColor(qRed(c), qGreen(c), qBlue(c)));
Chris@465 846 paint.drawLine(5, 11 + y, cw - 5, 11 + y);
Chris@465 847 } else {
Chris@682 848 cerr << "WARNING: Colour3DPlotLayer::paintVerticalScale: value " << value << ", mmin " << mmin << ", mmax " << mmax << " leads to invalid pixel " << pixel << endl;
Chris@465 849 }
Chris@159 850 }
Chris@446 851
Chris@446 852 QString minstr = QString("%1").arg(min);
Chris@446 853 QString maxstr = QString("%1").arg(max);
Chris@446 854
Chris@446 855 paint.save();
Chris@446 856
Chris@446 857 QFont font = paint.font();
Chris@1053 858 if (font.pixelSize() > 0) {
Chris@1053 859 int newSize = int(font.pixelSize() * 0.65);
Chris@1053 860 if (newSize < 6) newSize = 6;
Chris@1053 861 font.setPixelSize(newSize);
Chris@1053 862 paint.setFont(font);
Chris@1053 863 }
Chris@446 864
Chris@446 865 int msw = paint.fontMetrics().width(maxstr);
Chris@446 866
Chris@446 867 QMatrix m;
Chris@446 868 m.translate(cw - 6, ch + 10);
Chris@446 869 m.rotate(-90);
Chris@446 870
Chris@446 871 paint.setWorldMatrix(m);
Chris@446 872
Chris@1078 873 PaintAssistant::drawVisibleText(v, paint, 2, 0, minstr, PaintAssistant::OutlinedText);
Chris@446 874
Chris@446 875 m.translate(ch - msw - 2, 0);
Chris@446 876 paint.setWorldMatrix(m);
Chris@446 877
Chris@1078 878 PaintAssistant::drawVisibleText(v, paint, 0, 0, maxstr, PaintAssistant::OutlinedText);
Chris@446 879
Chris@446 880 paint.restore();
Chris@159 881 }
Chris@159 882
Chris@287 883 paint.setPen(v->getForeground());
Chris@159 884
Chris@445 885 int sh = m_model->getHeight();
Chris@445 886
Chris@445 887 int symin = m_miny;
Chris@445 888 int symax = m_maxy;
Chris@445 889 if (symax <= symin) {
Chris@445 890 symin = 0;
Chris@445 891 symax = sh;
Chris@445 892 }
Chris@445 893 if (symin < 0) symin = 0;
Chris@445 894 if (symax > sh) symax = sh;
Chris@445 895
Chris@532 896 paint.save();
Chris@456 897
Chris@533 898 int py = h;
Chris@25 899
Chris@969 900 int defaultFontHeight = paint.fontMetrics().height();
Chris@969 901
Chris@805 902 for (int i = symin; i <= symax; ++i) {
Chris@98 903
Chris@532 904 int y0;
Chris@534 905
Chris@903 906 y0 = getIYForBin(v, i);
Chris@532 907 int h = py - y0;
Chris@532 908
Chris@532 909 if (i > symin) {
Chris@532 910 if (paint.fontMetrics().height() >= h) {
Chris@969 911 if (h >= defaultFontHeight * 0.8) {
Chris@532 912 QFont tf = paint.font();
Chris@973 913 tf.setPixelSize(int(h * 0.8));
Chris@532 914 paint.setFont(tf);
Chris@532 915 } else {
Chris@532 916 continue;
Chris@532 917 }
Chris@532 918 }
Chris@532 919 }
Chris@25 920
Chris@532 921 py = y0;
Chris@532 922
Chris@534 923 if (i < symax) {
Chris@534 924 paint.drawLine(cw, y0, w, y0);
Chris@534 925 }
Chris@25 926
Chris@532 927 if (i > symin) {
Chris@534 928
Chris@805 929 int idx = i - 1;
Chris@534 930 if (m_invertVertical) idx = m_model->getHeight() - idx - 1;
Chris@534 931
Chris@534 932 QString text = m_model->getBinName(idx);
Chris@534 933 if (text == "") text = QString("[%1]").arg(idx + 1);
Chris@534 934
Chris@534 935 int ty = y0 + (h/2) - (paint.fontMetrics().height()/2) +
Chris@534 936 paint.fontMetrics().ascent() + 1;
Chris@534 937
Chris@534 938 paint.drawText(cw + 5, ty, text);
Chris@457 939 }
Chris@25 940 }
Chris@456 941
Chris@456 942 paint.restore();
Chris@25 943 }
Chris@25 944
Chris@467 945 DenseThreeDimensionalModel::Column
Chris@805 946 Colour3DPlotLayer::getColumn(int col) const
Chris@197 947 {
Chris@812 948 Profiler profiler("Colour3DPlotLayer::getColumn");
Chris@812 949
Chris@467 950 DenseThreeDimensionalModel::Column values = m_model->getColumn(col);
Chris@1019 951 values.resize(m_model->getHeight(), 0.f);
Chris@1099 952 if (m_normalization != ColumnOp::NormalizeColumns &&
Chris@1099 953 m_normalization != ColumnOp::NormalizeHybrid) {
Chris@1099 954 return values;
Chris@1099 955 }
Chris@461 956
Chris@904 957 double colMax = 0.f, colMin = 0.f;
Chris@904 958 double min = 0.f, max = 0.f;
Chris@197 959
Chris@1019 960 int nv = int(values.size());
Chris@1019 961
Chris@461 962 min = m_model->getMinimumLevel();
Chris@461 963 max = m_model->getMaximumLevel();
Chris@461 964
Chris@1019 965 for (int y = 0; y < nv; ++y) {
Chris@467 966 if (y == 0 || values.at(y) > colMax) colMax = values.at(y);
Chris@467 967 if (y == 0 || values.at(y) < colMin) colMin = values.at(y);
Chris@197 968 }
Chris@461 969 if (colMin == colMax) colMax = colMin + 1;
Chris@197 970
Chris@1019 971 for (int y = 0; y < nv; ++y) {
Chris@461 972
Chris@904 973 double value = values.at(y);
Chris@904 974 double norm = (value - colMin) / (colMax - colMin);
Chris@904 975 double newvalue = min + (max - min) * norm;
Chris@197 976
Chris@904 977 if (value != newvalue) values[y] = float(newvalue);
Chris@468 978 }
Chris@468 979
Chris@1099 980 if (m_normalization == ColumnOp::NormalizeHybrid
Chris@1099 981 && (colMax > 0.0)) {
Chris@904 982 double logmax = log10(colMax);
Chris@1019 983 for (int y = 0; y < nv; ++y) {
Chris@904 984 values[y] = float(values[y] * logmax);
Chris@719 985 }
Chris@719 986 }
Chris@719 987
Chris@468 988 return values;
Chris@197 989 }
Chris@197 990
Chris@197 991 void
Chris@1046 992 Colour3DPlotLayer::fillCache(int firstColumn, int lastColumn) const
Chris@197 993 {
Chris@1046 994 // This call requests a (perhaps partial) fill of the cache
Chris@1046 995 // between model columns firstColumn and lastColumn inclusive.
Chris@1046 996 // The cache itself always has size sufficient to contain the
Chris@1046 997 // whole model, but its validity may be less, depending on which
Chris@1046 998 // regions have been requested via calls to this function. Note
Chris@1046 999 // that firstColumn and lastColumn are *model* column numbers. If
Chris@1046 1000 // the model starts at a frame > 0, a firstColumn of zero still
Chris@1046 1001 // corresponds to the first column in the model, not the first
Chris@1046 1002 // column on the resulting rendered layer.
Chris@1046 1003
Chris@812 1004 Profiler profiler("Colour3DPlotLayer::fillCache", true);
Chris@476 1005
Chris@1046 1006 int cacheWidth = m_model->getWidth();
Chris@1046 1007 int cacheHeight = m_model->getHeight();
Chris@461 1008
Chris@812 1009 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@1046 1010 cerr << "Colour3DPlotLayer::fillCache: range " << firstColumn << " -> " << lastColumn << " (cache size will be " << cacheWidth << " x " << cacheHeight << ")" << endl;
Chris@812 1011 #endif
Chris@812 1012
Chris@812 1013 if (m_cache && m_cache->height() != cacheHeight) {
Chris@742 1014 // height has changed: delete everything rather than resizing
Chris@812 1015 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@812 1016 cerr << "Colour3DPlotLayer::fillCache: Cache height has changed, recreating" << endl;
Chris@812 1017 #endif
Chris@461 1018 delete m_cache;
Chris@469 1019 delete m_peaksCache;
Chris@461 1020 m_cache = 0;
Chris@469 1021 m_peaksCache = 0;
Chris@461 1022 }
Chris@461 1023
Chris@812 1024 if (m_cache && m_cache->width() != cacheWidth) {
Chris@742 1025 // width has changed and we have an existing cache: resize it
Chris@812 1026 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@812 1027 cerr << "Colour3DPlotLayer::fillCache: Cache width has changed, resizing existing cache" << endl;
Chris@812 1028 #endif
Chris@469 1029 QImage *newCache =
Chris@469 1030 new QImage(m_cache->copy(0, 0, cacheWidth, cacheHeight));
Chris@461 1031 delete m_cache;
Chris@461 1032 m_cache = newCache;
Chris@469 1033 if (m_peaksCache) {
Chris@469 1034 QImage *newPeaksCache =
Chris@469 1035 new QImage(m_peaksCache->copy
Chris@742 1036 (0, 0, cacheWidth / m_peakResolution + 1, cacheHeight));
Chris@469 1037 delete m_peaksCache;
Chris@469 1038 m_peaksCache = newPeaksCache;
Chris@469 1039 }
Chris@197 1040 }
Chris@197 1041
Chris@461 1042 if (!m_cache) {
Chris@812 1043 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@812 1044 cerr << "Colour3DPlotLayer::fillCache: Have no cache, making one" << endl;
Chris@812 1045 #endif
Chris@1046 1046 m_cache = new QImage(cacheWidth, cacheHeight, QImage::Format_Indexed8);
Chris@812 1047 m_cache->setColorCount(256);
Chris@514 1048 m_cache->fill(0);
Chris@1099 1049 if (m_normalization != ColumnOp::NormalizeVisibleArea) {
Chris@469 1050 m_peaksCache = new QImage
Chris@469 1051 (cacheWidth / m_peakResolution + 1, cacheHeight,
Chris@469 1052 QImage::Format_Indexed8);
Chris@812 1053 m_peaksCache->setColorCount(256);
Chris@514 1054 m_peaksCache->fill(0);
Chris@469 1055 } else if (m_peaksCache) {
Chris@469 1056 delete m_peaksCache;
Chris@469 1057 m_peaksCache = 0;
Chris@469 1058 }
Chris@461 1059 m_cacheValidStart = 0;
Chris@461 1060 m_cacheValidEnd = 0;
Chris@197 1061 }
Chris@197 1062
Chris@812 1063 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@812 1064 cerr << "cache size = " << m_cache->width() << "x" << m_cache->height()
Chris@812 1065 << " peaks cache size = " << m_peaksCache->width() << "x" << m_peaksCache->height() << endl;
Chris@812 1066 #endif
Chris@742 1067
Chris@1046 1068 if (m_cacheValidStart <= firstColumn && m_cacheValidEnd >= lastColumn) {
Chris@519 1069 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@682 1070 cerr << "Cache is valid in this region already" << endl;
Chris@519 1071 #endif
Chris@461 1072 return;
Chris@461 1073 }
Chris@461 1074
Chris@1046 1075 int fillStart = firstColumn;
Chris@1046 1076 int fillEnd = lastColumn;
Chris@197 1077
Chris@1046 1078 if (fillStart >= cacheWidth) fillStart = cacheWidth-1;
Chris@1046 1079 if (fillStart < 0) fillStart = 0;
Chris@1046 1080 if (fillEnd >= cacheWidth) fillEnd = cacheWidth-1;
Chris@1046 1081 if (fillEnd < 0) fillEnd = 0;
Chris@1046 1082 if (fillEnd < fillStart) fillEnd = fillStart;
Chris@197 1083
Chris@1099 1084 bool normalizeVisible = (m_normalization == ColumnOp::NormalizeVisibleArea);
Chris@197 1085
Chris@461 1086 if (!normalizeVisible && (m_cacheValidStart < m_cacheValidEnd)) {
Chris@461 1087
Chris@461 1088 if (m_cacheValidEnd < fillStart) {
Chris@461 1089 fillStart = m_cacheValidEnd + 1;
Chris@461 1090 }
Chris@461 1091 if (m_cacheValidStart > fillEnd) {
Chris@461 1092 fillEnd = m_cacheValidStart - 1;
Chris@461 1093 }
Chris@461 1094
Chris@461 1095 m_cacheValidStart = std::min(fillStart, m_cacheValidStart);
Chris@461 1096 m_cacheValidEnd = std::max(fillEnd, m_cacheValidEnd);
Chris@461 1097
Chris@461 1098 } else {
Chris@461 1099
Chris@812 1100 // when normalising the visible area, the only valid area,
Chris@812 1101 // ever, is the currently visible one
Chris@461 1102
Chris@461 1103 m_cacheValidStart = fillStart;
Chris@461 1104 m_cacheValidEnd = fillEnd;
Chris@461 1105 }
Chris@461 1106
Chris@519 1107 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@812 1108 cerr << "Cache size " << cacheWidth << "x" << cacheHeight << " will be valid from " << m_cacheValidStart << " to " << m_cacheValidEnd << " (fillStart = " << fillStart << ", fillEnd = " << fillEnd << ")" << endl;
Chris@519 1109 #endif
Chris@461 1110
Chris@197 1111 DenseThreeDimensionalModel::Column values;
Chris@197 1112
Chris@904 1113 double min = m_model->getMinimumLevel();
Chris@904 1114 double max = m_model->getMaximumLevel();
Chris@197 1115
Chris@1099 1116 if (m_colourScale == ColourScale::LogColourScale) {
Chris@197 1117 LogRange::mapRange(min, max);
Chris@1099 1118 } else if (m_colourScale == ColourScale::PlusMinusOneScale) {
Chris@197 1119 min = -1.f;
Chris@197 1120 max = 1.f;
Chris@1099 1121 } else if (m_colourScale == ColourScale::AbsoluteScale) {
Chris@509 1122 if (min < 0) {
Chris@904 1123 if (fabs(min) > fabs(max)) max = fabs(min);
Chris@904 1124 else max = fabs(max);
Chris@509 1125 min = 0;
Chris@509 1126 } else {
Chris@904 1127 min = fabs(min);
Chris@904 1128 max = fabs(max);
Chris@509 1129 }
Chris@197 1130 }
Chris@197 1131
Chris@902 1132 if (max == min) max = min + 1.f;
Chris@197 1133
Chris@197 1134 ColourMapper mapper(m_colourMap, 0.f, 255.f);
Chris@197 1135
Chris@197 1136 for (int index = 0; index < 256; ++index) {
Chris@197 1137 QColor colour = mapper.map(index);
Chris@469 1138 m_cache->setColor
Chris@469 1139 (index, qRgb(colour.red(), colour.green(), colour.blue()));
Chris@469 1140 if (m_peaksCache) {
Chris@469 1141 m_peaksCache->setColor
Chris@469 1142 (index, qRgb(colour.red(), colour.green(), colour.blue()));
Chris@469 1143 }
Chris@197 1144 }
Chris@197 1145
Chris@904 1146 double visibleMax = 0.f, visibleMin = 0.f;
Chris@197 1147
Chris@461 1148 if (normalizeVisible) {
Chris@197 1149
Chris@805 1150 for (int c = fillStart; c <= fillEnd; ++c) {
Chris@197 1151
Chris@467 1152 values = getColumn(c);
Chris@197 1153
Chris@904 1154 double colMax = 0.f, colMin = 0.f;
Chris@197 1155
Chris@805 1156 for (int y = 0; y < cacheHeight; ++y) {
Chris@1019 1157 if (!in_range_for(values, y)) break;
Chris@197 1158 if (y == 0 || values[y] > colMax) colMax = values[y];
Chris@224 1159 if (y == 0 || values[y] < colMin) colMin = values[y];
Chris@197 1160 }
Chris@197 1161
Chris@461 1162 if (c == fillStart || colMax > visibleMax) visibleMax = colMax;
Chris@461 1163 if (c == fillStart || colMin < visibleMin) visibleMin = colMin;
Chris@461 1164 }
Chris@461 1165
Chris@1099 1166 if (m_colourScale == ColourScale::LogColourScale) {
Chris@461 1167 visibleMin = LogRange::map(visibleMin);
Chris@461 1168 visibleMax = LogRange::map(visibleMax);
Chris@461 1169 if (visibleMin > visibleMax) std::swap(visibleMin, visibleMax);
Chris@1099 1170 } else if (m_colourScale == ColourScale::AbsoluteScale) {
Chris@509 1171 if (visibleMin < 0) {
Chris@904 1172 if (fabs(visibleMin) > fabs(visibleMax)) visibleMax = fabs(visibleMin);
Chris@904 1173 else visibleMax = fabs(visibleMax);
Chris@509 1174 visibleMin = 0;
Chris@509 1175 } else {
Chris@904 1176 visibleMin = fabs(visibleMin);
Chris@904 1177 visibleMax = fabs(visibleMax);
Chris@509 1178 }
Chris@197 1179 }
Chris@197 1180 }
Chris@197 1181
Chris@224 1182 if (visibleMin == visibleMax) visibleMax = visibleMin + 1;
Chris@224 1183
Chris@469 1184 int *peaks = 0;
Chris@469 1185 if (m_peaksCache) {
Chris@469 1186 peaks = new int[cacheHeight];
Chris@469 1187 for (int y = 0; y < cacheHeight; ++y) {
Chris@469 1188 peaks[y] = 0;
Chris@469 1189 }
Chris@469 1190 }
Chris@469 1191
Chris@812 1192 Profiler profiler2("Colour3DPlotLayer::fillCache: filling", true);
Chris@812 1193
Chris@805 1194 for (int c = fillStart; c <= fillEnd; ++c) {
Chris@197 1195
Chris@467 1196 values = getColumn(c);
Chris@197 1197
Chris@742 1198 if (c >= m_cache->width()) {
Chris@742 1199 cerr << "ERROR: column " << c << " >= cache width "
Chris@742 1200 << m_cache->width() << endl;
Chris@742 1201 continue;
Chris@742 1202 }
Chris@742 1203
Chris@805 1204 for (int y = 0; y < cacheHeight; ++y) {
Chris@197 1205
Chris@904 1206 double value = min;
Chris@1019 1207 if (in_range_for(values, y)) {
Chris@469 1208 value = values.at(y);
Chris@197 1209 }
Chris@224 1210
Chris@534 1211 value = value * m_gain;
Chris@534 1212
Chris@1099 1213 if (m_colourScale == ColourScale::LogColourScale) {
Chris@224 1214 value = LogRange::map(value);
Chris@1099 1215 } else if (m_colourScale == ColourScale::AbsoluteScale) {
Chris@904 1216 value = fabs(value);
Chris@197 1217 }
Chris@461 1218
Chris@461 1219 if (normalizeVisible) {
Chris@904 1220 double norm = (value - visibleMin) / (visibleMax - visibleMin);
Chris@461 1221 value = min + (max - min) * norm;
Chris@461 1222 }
Chris@197 1223
Chris@197 1224 int pixel = int(((value - min) * 256) / (max - min));
Chris@197 1225 if (pixel < 0) pixel = 0;
Chris@197 1226 if (pixel > 255) pixel = 255;
Chris@469 1227 if (peaks && (pixel > peaks[y])) peaks[y] = pixel;
Chris@197 1228
Chris@357 1229 if (m_invertVertical) {
Chris@461 1230 m_cache->setPixel(c, cacheHeight - y - 1, pixel);
Chris@357 1231 } else {
Chris@742 1232 if (y >= m_cache->height()) {
Chris@742 1233 cerr << "ERROR: row " << y << " >= cache height " << m_cache->height() << endl;
Chris@742 1234 } else {
Chris@742 1235 m_cache->setPixel(c, y, pixel);
Chris@742 1236 }
Chris@357 1237 }
Chris@197 1238 }
Chris@469 1239
Chris@469 1240 if (peaks) {
Chris@805 1241 int notch = (c % m_peakResolution);
Chris@469 1242 if (notch == m_peakResolution-1 || c == fillEnd) {
Chris@805 1243 int pc = c / m_peakResolution;
Chris@742 1244 if (pc >= m_peaksCache->width()) {
Chris@742 1245 cerr << "ERROR: peak column " << pc
Chris@742 1246 << " (from col " << c << ") >= peaks cache width "
Chris@742 1247 << m_peaksCache->width() << endl;
Chris@742 1248 continue;
Chris@742 1249 }
Chris@805 1250 for (int y = 0; y < cacheHeight; ++y) {
Chris@469 1251 if (m_invertVertical) {
Chris@469 1252 m_peaksCache->setPixel(pc, cacheHeight - y - 1, peaks[y]);
Chris@469 1253 } else {
Chris@742 1254 if (y >= m_peaksCache->height()) {
Chris@742 1255 cerr << "ERROR: row " << y
Chris@742 1256 << " >= peaks cache height "
Chris@742 1257 << m_peaksCache->height() << endl;
Chris@742 1258 } else {
Chris@742 1259 m_peaksCache->setPixel(pc, y, peaks[y]);
Chris@742 1260 }
Chris@469 1261 }
Chris@469 1262 }
Chris@469 1263 for (int y = 0; y < cacheHeight; ++y) {
Chris@469 1264 peaks[y] = 0;
Chris@469 1265 }
Chris@469 1266 }
Chris@469 1267 }
Chris@197 1268 }
Chris@469 1269
Chris@469 1270 delete[] peaks;
Chris@197 1271 }
Chris@197 1272
Chris@812 1273 bool
Chris@916 1274 Colour3DPlotLayer::shouldPaintDenseIn(const LayerGeometryProvider *v) const
Chris@812 1275 {
Chris@812 1276 if (!m_model || !v || !(v->getViewManager())) {
Chris@812 1277 return false;
Chris@812 1278 }
Chris@902 1279 double srRatio =
Chris@902 1280 v->getViewManager()->getMainModelSampleRate() / m_model->getSampleRate();
Chris@812 1281 if (m_opaque ||
Chris@812 1282 m_smooth ||
Chris@916 1283 m_model->getHeight() >= v->getPaintHeight() ||
Chris@812 1284 ((m_model->getResolution() * srRatio) / v->getZoomLevel()) < 2) {
Chris@812 1285 return true;
Chris@812 1286 }
Chris@812 1287 return false;
Chris@812 1288 }
Chris@812 1289
Chris@1100 1290 Colour3DPlotRenderer *
Chris@1100 1291 Colour3DPlotLayer::getRenderer(LayerGeometryProvider *v) const
Chris@1100 1292 {
Chris@1100 1293 if (m_renderers.find(v->getId()) == m_renderers.end()) {
Chris@1100 1294
Chris@1100 1295 Colour3DPlotRenderer::Sources sources;
Chris@1100 1296 sources.verticalBinLayer = this;
Chris@1100 1297 sources.fft = 0;
Chris@1100 1298 sources.source = m_model;
Chris@1100 1299 sources.peaks = getPeakCache();
Chris@1100 1300
Chris@1100 1301 ColourScale::Parameters cparams;
Chris@1100 1302 cparams.colourMap = m_colourMap;
Chris@1100 1303 cparams.scale = m_colourScale;
Chris@1100 1304 cparams.gain = m_gain;
Chris@1100 1305
Chris@1100 1306 Colour3DPlotRenderer::Parameters params;
Chris@1100 1307 params.colourScale = ColourScale(cparams);
Chris@1100 1308 params.normalization = m_normalization;
Chris@1100 1309 params.binScale = m_binScale;
Chris@1100 1310 params.alwaysOpaque = m_opaque;
Chris@1100 1311 params.invertVertical = m_invertVertical;
Chris@1100 1312 params.interpolate = m_smooth;
Chris@1100 1313
Chris@1100 1314 m_renderers[v->getId()] = new Colour3DPlotRenderer(sources, params);
Chris@1100 1315 }
Chris@1100 1316
Chris@1100 1317 return m_renderers[v->getId()];
Chris@1100 1318 }
Chris@1100 1319
Chris@197 1320 void
Chris@916 1321 Colour3DPlotLayer::paint(LayerGeometryProvider *v, QPainter &paint, QRect rect) const
Chris@0 1322 {
Chris@514 1323 /*
Chris@443 1324 if (m_model) {
Chris@587 1325 SVDEBUG << "Colour3DPlotLayer::paint: model says shouldUseLogValueScale = " << m_model->shouldUseLogValueScale() << endl;
Chris@443 1326 }
Chris@514 1327 */
Chris@466 1328 Profiler profiler("Colour3DPlotLayer::paint");
Chris@125 1329 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@812 1330 cerr << "Colour3DPlotLayer::paint(): m_model is " << m_model << ", zoom level is " << v->getZoomLevel() << ", rect is (" << rect.x() << "," << rect.y() << ") " << rect.width() << "x" << rect.height() << endl;
Chris@125 1331 #endif
Chris@0 1332
Chris@0 1333 int completion = 0;
Chris@0 1334 if (!m_model || !m_model->isOK() || !m_model->isReady(&completion)) {
Chris@0 1335 if (completion > 0) {
Chris@916 1336 paint.fillRect(0, 10, v->getPaintWidth() * completion / 100,
Chris@0 1337 10, QColor(120, 120, 120));
Chris@0 1338 }
Chris@0 1339 return;
Chris@0 1340 }
Chris@0 1341
Chris@1053 1342 if (m_model->getWidth() == 0) {
Chris@1053 1343 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@1053 1344 cerr << "Colour3DPlotLayer::paint(): model width == 0, "
Chris@1053 1345 << "nothing to paint (yet)" << endl;
Chris@1053 1346 #endif
Chris@1053 1347 return;
Chris@1053 1348 }
Chris@1053 1349
Chris@1099 1350 if (m_normalization == ColumnOp::NormalizeVisibleArea) {
Chris@1099 1351 rect = v->getPaintRect();
Chris@1099 1352 }
Chris@197 1353
Chris@902 1354 sv_frame_t modelStart = m_model->getStartFrame();
Chris@902 1355 sv_frame_t modelEnd = m_model->getEndFrame();
Chris@805 1356 int modelResolution = m_model->getResolution();
Chris@0 1357
Chris@197 1358 // The cache is from the model's start frame to the model's end
Chris@197 1359 // frame at the model's window increment frames per pixel. We
Chris@197 1360 // want to draw from our start frame + x0 * zoomLevel to our start
Chris@197 1361 // frame + x1 * zoomLevel at zoomLevel frames per pixel.
Chris@12 1362
Chris@197 1363 // We have quite different paint mechanisms for rendering "large"
Chris@197 1364 // bins (more than one bin per pixel in both directions) and
Chris@197 1365 // "small". This is "large"; see paintDense below for "small".
Chris@12 1366
Chris@197 1367 int x0 = rect.left();
Chris@197 1368 int x1 = rect.right() + 1;
Chris@12 1369
Chris@916 1370 int h = v->getPaintHeight();
Chris@0 1371
Chris@902 1372 double srRatio =
Chris@902 1373 v->getViewManager()->getMainModelSampleRate() / m_model->getSampleRate();
Chris@0 1374
Chris@1047 1375 // the s-prefix values are source, i.e. model, column and bin numbers
Chris@902 1376 int sx0 = int((double(v->getFrameForX(x0)) / srRatio - double(modelStart))
Chris@812 1377 / modelResolution);
Chris@902 1378 int sx1 = int((double(v->getFrameForX(x1)) / srRatio - double(modelStart))
Chris@812 1379 / modelResolution);
Chris@197 1380 int sh = m_model->getHeight();
Chris@159 1381
Chris@444 1382 int symin = m_miny;
Chris@444 1383 int symax = m_maxy;
Chris@444 1384 if (symax <= symin) {
Chris@444 1385 symin = 0;
Chris@444 1386 symax = sh;
Chris@444 1387 }
Chris@444 1388 if (symin < 0) symin = 0;
Chris@444 1389 if (symax > sh) symax = sh;
Chris@444 1390
Chris@197 1391 if (sx0 > 0) --sx0;
Chris@197 1392 fillCache(sx0 < 0 ? 0 : sx0,
Chris@197 1393 sx1 < 0 ? 0 : sx1);
Chris@0 1394
Chris@351 1395 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@812 1396 cerr << "Colour3DPlotLayer::paint: height = "<< m_model->getHeight() << ", modelStart = " << modelStart << ", resolution = " << modelResolution << ", model rate = " << m_model->getSampleRate() << " (zoom level = " << v->getZoomLevel() << ", srRatio = " << srRatio << ")" << endl;
Chris@351 1397 #endif
Chris@351 1398
Chris@812 1399 if (shouldPaintDenseIn(v)) {
Chris@347 1400 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@812 1401 cerr << "calling paintDense" << endl;
Chris@347 1402 #endif
Chris@98 1403 paintDense(v, paint, rect);
Chris@98 1404 return;
Chris@98 1405 }
Chris@98 1406
Chris@125 1407 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@812 1408 cerr << "Colour3DPlotLayer::paint: w " << x1-x0 << ", h " << h << ", sx0 " << sx0 << ", sx1 " << sx1 << ", sw " << sx1-sx0 << ", sh " << sh << endl;
Chris@682 1409 cerr << "Colour3DPlotLayer: sample rate is " << m_model->getSampleRate() << ", resolution " << m_model->getResolution() << endl;
Chris@125 1410 #endif
Chris@0 1411
Chris@25 1412 QPoint illuminatePos;
Chris@44 1413 bool illuminate = v->shouldIlluminateLocalFeatures(this, illuminatePos);
Chris@864 1414
Chris@864 1415 const int buflen = 40;
Chris@864 1416 char labelbuf[buflen];
Chris@25 1417
Chris@197 1418 for (int sx = sx0; sx <= sx1; ++sx) {
Chris@0 1419
Chris@1047 1420 sv_frame_t fx = sx * modelResolution + modelStart;
Chris@0 1421
Chris@812 1422 if (fx + modelResolution <= modelStart || fx > modelEnd) continue;
Chris@0 1423
Chris@1047 1424 int rx0 = v->getXForFrame(int(double(fx) * srRatio));
Chris@1047 1425 int rx1 = v->getXForFrame(int(double(fx + modelResolution + 1) * srRatio));
Chris@54 1426
Chris@159 1427 int rw = rx1 - rx0;
Chris@159 1428 if (rw < 1) rw = 1;
Chris@54 1429
Chris@159 1430 bool showLabel = (rw > 10 &&
Chris@159 1431 paint.fontMetrics().width("0.000000") < rw - 3 &&
Chris@54 1432 paint.fontMetrics().height() < (h / sh));
Chris@98 1433
Chris@444 1434 for (int sy = symin; sy < symax; ++sy) {
Chris@0 1435
Chris@903 1436 int ry0 = getIYForBin(v, sy);
Chris@903 1437 int ry1 = getIYForBin(v, sy + 1);
Chris@534 1438 QRect r(rx0, ry1, rw, ry0 - ry1);
Chris@534 1439
Chris@0 1440 QRgb pixel = qRgb(255, 255, 255);
Chris@461 1441 if (sx >= 0 && sx < m_cache->width() &&
Chris@0 1442 sy >= 0 && sy < m_cache->height()) {
Chris@461 1443 pixel = m_cache->pixel(sx, sy);
Chris@0 1444 }
Chris@0 1445
Chris@159 1446 if (rw == 1) {
Chris@159 1447 paint.setPen(pixel);
Chris@159 1448 paint.setBrush(Qt::NoBrush);
Chris@159 1449 paint.drawLine(r.x(), r.y(), r.x(), r.y() + r.height() - 1);
Chris@159 1450 continue;
Chris@159 1451 }
Chris@159 1452
Chris@0 1453 QColor pen(255, 255, 255, 80);
Chris@0 1454 QColor brush(pixel);
Chris@159 1455
Chris@159 1456 if (rw > 3 && r.height() > 3) {
Chris@159 1457 brush.setAlpha(160);
Chris@159 1458 }
Chris@159 1459
Chris@0 1460 paint.setPen(Qt::NoPen);
Chris@0 1461 paint.setBrush(brush);
Chris@0 1462
Chris@25 1463 if (illuminate) {
Chris@25 1464 if (r.contains(illuminatePos)) {
Chris@287 1465 paint.setPen(v->getForeground());
Chris@25 1466 }
Chris@25 1467 }
Chris@76 1468
Chris@125 1469 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@682 1470 // cerr << "rect " << r.x() << "," << r.y() << " "
Chris@682 1471 // << r.width() << "x" << r.height() << endl;
Chris@125 1472 #endif
Chris@25 1473
Chris@25 1474 paint.drawRect(r);
Chris@0 1475
Chris@54 1476 if (showLabel) {
Chris@461 1477 if (sx >= 0 && sx < m_cache->width() &&
Chris@54 1478 sy >= 0 && sy < m_cache->height()) {
Chris@904 1479 double value = m_model->getValueAt(sx, sy);
Chris@864 1480 snprintf(labelbuf, buflen, "%06f", value);
Chris@54 1481 QString text(labelbuf);
Chris@1078 1482 PaintAssistant::drawVisibleText
Chris@1078 1483 (v,
Chris@1078 1484 paint,
Chris@1048 1485 rx0 + 2,
Chris@1048 1486 ry0 - h / sh - 1 + 2 + paint.fontMetrics().ascent(),
Chris@1048 1487 text,
Chris@1078 1488 PaintAssistant::OutlinedText);
Chris@0 1489 }
Chris@0 1490 }
Chris@0 1491 }
Chris@0 1492 }
Chris@98 1493 }
Chris@0 1494
Chris@98 1495 void
Chris@916 1496 Colour3DPlotLayer::paintDense(LayerGeometryProvider *v, QPainter &paint, QRect rect) const
Chris@98 1497 {
Chris@812 1498 Profiler profiler("Colour3DPlotLayer::paintDense", true);
Chris@463 1499 if (!m_cache) return;
Chris@463 1500
Chris@903 1501 double modelStart = double(m_model->getStartFrame());
Chris@903 1502 double modelResolution = double(m_model->getResolution());
Chris@0 1503
Chris@903 1504 sv_samplerate_t mmsr = v->getViewManager()->getMainModelSampleRate();
Chris@903 1505 sv_samplerate_t msr = m_model->getSampleRate();
Chris@903 1506 double srRatio = mmsr / msr;
Chris@159 1507
Chris@98 1508 int x0 = rect.left();
Chris@98 1509 int x1 = rect.right() + 1;
Chris@0 1510
Chris@470 1511 const int w = x1 - x0; // const so it can be used as array size below
Chris@916 1512 int h = v->getPaintHeight(); // we always paint full height
Chris@160 1513 int sh = m_model->getHeight();
Chris@98 1514
Chris@444 1515 int symin = m_miny;
Chris@444 1516 int symax = m_maxy;
Chris@444 1517 if (symax <= symin) {
Chris@444 1518 symin = 0;
Chris@444 1519 symax = sh;
Chris@444 1520 }
Chris@444 1521 if (symin < 0) symin = 0;
Chris@444 1522 if (symax > sh) symax = sh;
Chris@444 1523
Chris@466 1524 QImage img(w, h, QImage::Format_Indexed8);
Chris@466 1525 img.setColorTable(m_cache->colorTable());
Chris@98 1526
Chris@466 1527 uchar *peaks = new uchar[w];
Chris@466 1528 memset(peaks, 0, w);
Chris@98 1529
Chris@469 1530 int zoomLevel = v->getZoomLevel();
Chris@469 1531
Chris@469 1532 QImage *source = m_cache;
Chris@812 1533
Chris@812 1534 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@812 1535 cerr << "modelResolution " << modelResolution << ", srRatio "
Chris@812 1536 << srRatio << ", m_peakResolution " << m_peakResolution
Chris@812 1537 << ", zoomLevel " << zoomLevel << ", result "
Chris@812 1538 << ((modelResolution * srRatio * m_peakResolution) / zoomLevel)
Chris@812 1539 << endl;
Chris@812 1540 #endif
Chris@474 1541
Chris@474 1542 if (m_peaksCache) {
Chris@474 1543 if (((modelResolution * srRatio * m_peakResolution) / zoomLevel) < 1) {
Chris@812 1544 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@812 1545 cerr << "using peaks cache" << endl;
Chris@812 1546 #endif
Chris@474 1547 source = m_peaksCache;
Chris@474 1548 modelResolution *= m_peakResolution;
Chris@474 1549 } else {
Chris@812 1550 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@812 1551 cerr << "not using peaks cache" << endl;
Chris@812 1552 #endif
Chris@474 1553 }
Chris@469 1554 } else {
Chris@812 1555 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@812 1556 cerr << "have no peaks cache" << endl;
Chris@812 1557 #endif
Chris@469 1558 }
Chris@469 1559
Chris@469 1560 int sw = source->width();
Chris@470 1561
Chris@903 1562 sv_frame_t xf = -1;
Chris@903 1563 sv_frame_t nxf = v->getFrameForX(x0);
Chris@470 1564
Chris@903 1565 double epsilon = 0.000001;
Chris@535 1566
Chris@903 1567 vector<double> sxa(w*2);
Chris@903 1568
Chris@470 1569 for (int x = 0; x < w; ++x) {
Chris@470 1570
Chris@470 1571 xf = nxf;
Chris@470 1572 nxf = xf + zoomLevel;
Chris@470 1573
Chris@903 1574 double sx0 = (double(xf) / srRatio - modelStart) / modelResolution;
Chris@903 1575 double sx1 = (double(nxf) / srRatio - modelStart) / modelResolution;
Chris@470 1576
Chris@535 1577 sxa[x*2] = sx0;
Chris@535 1578 sxa[x*2 + 1] = sx1;
Chris@470 1579 }
Chris@466 1580
Chris@904 1581 double logmin = symin+1, logmax = symax+1;
Chris@532 1582 LogRange::mapRange(logmin, logmax);
Chris@532 1583
Chris@812 1584 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@812 1585 cerr << "m_smooth = " << m_smooth << ", w = " << w << ", h = " << h << endl;
Chris@812 1586 #endif
Chris@812 1587
Chris@535 1588 if (m_smooth) {
Chris@535 1589
Chris@535 1590 for (int y = 0; y < h; ++y) {
Chris@466 1591
Chris@904 1592 double sy = getBinForY(v, y) - 0.5;
Chris@535 1593 int syi = int(sy + epsilon);
Chris@535 1594 if (syi < 0 || syi >= source->height()) continue;
Chris@531 1595
Chris@535 1596 uchar *targetLine = img.scanLine(y);
Chris@535 1597 uchar *sourceLine = source->scanLine(syi);
Chris@535 1598 uchar *nextSource;
Chris@535 1599 if (syi + 1 < source->height()) {
Chris@535 1600 nextSource = source->scanLine(syi + 1);
Chris@535 1601 } else {
Chris@535 1602 nextSource = sourceLine;
Chris@535 1603 }
Chris@466 1604
Chris@466 1605 for (int x = 0; x < w; ++x) {
Chris@98 1606
Chris@535 1607 targetLine[x] = 0;
Chris@474 1608
Chris@903 1609 double sx0 = sxa[x*2];
Chris@537 1610 if (sx0 < 0) continue;
Chris@535 1611 int sx0i = int(sx0 + epsilon);
Chris@474 1612 if (sx0i >= sw) break;
Chris@98 1613
Chris@903 1614 double a = sourceLine[sx0i];
Chris@903 1615 double b = a;
Chris@903 1616 double value;
Chris@535 1617
Chris@903 1618 double sx1 = sxa[x*2+1];
Chris@535 1619 if (sx1 > sx0 + 1.f) {
Chris@535 1620 int sx1i = int(sx1);
Chris@535 1621 bool have = false;
Chris@535 1622 for (int sx = sx0i; sx <= sx1i; ++sx) {
Chris@535 1623 if (sx < 0 || sx >= sw) continue;
Chris@535 1624 if (!have) {
Chris@903 1625 a = sourceLine[sx];
Chris@903 1626 b = nextSource[sx];
Chris@535 1627 have = true;
Chris@535 1628 } else {
Chris@903 1629 a = std::max(a, double(sourceLine[sx]));
Chris@903 1630 b = std::max(b, double(nextSource[sx]));
Chris@535 1631 }
Chris@535 1632 }
Chris@903 1633 double yprop = sy - syi;
Chris@535 1634 value = (a * (1.f - yprop) + b * yprop);
Chris@535 1635 } else {
Chris@903 1636 a = sourceLine[sx0i];
Chris@903 1637 b = nextSource[sx0i];
Chris@903 1638 double yprop = sy - syi;
Chris@535 1639 value = (a * (1.f - yprop) + b * yprop);
Chris@535 1640 int oi = sx0i + 1;
Chris@903 1641 double xprop = sx0 - sx0i;
Chris@535 1642 xprop -= 0.5;
Chris@535 1643 if (xprop < 0) {
Chris@535 1644 oi = sx0i - 1;
Chris@535 1645 xprop = -xprop;
Chris@535 1646 }
Chris@535 1647 if (oi < 0 || oi >= sw) oi = sx0i;
Chris@903 1648 a = sourceLine[oi];
Chris@903 1649 b = nextSource[oi];
Chris@535 1650 value = (value * (1.f - xprop) +
Chris@535 1651 (a * (1.f - yprop) + b * yprop) * xprop);
Chris@98 1652 }
Chris@535 1653
Chris@903 1654 int vi = int(lrint(value));
Chris@535 1655 if (vi > 255) vi = 255;
Chris@535 1656 if (vi < 0) vi = 0;
Chris@535 1657 targetLine[x] = uchar(vi);
Chris@98 1658 }
Chris@466 1659 }
Chris@535 1660 } else {
Chris@535 1661
Chris@904 1662 double sy0 = getBinForY(v, 0);
Chris@812 1663
Chris@812 1664 int psy0i = -1, psy1i = -1;
Chris@812 1665
Chris@535 1666 for (int y = 0; y < h; ++y) {
Chris@535 1667
Chris@904 1668 double sy1 = sy0;
Chris@904 1669 sy0 = getBinForY(v, double(y + 1));
Chris@535 1670
Chris@535 1671 int sy0i = int(sy0 + epsilon);
Chris@535 1672 int sy1i = int(sy1);
Chris@535 1673
Chris@535 1674 uchar *targetLine = img.scanLine(y);
Chris@535 1675
Chris@812 1676 if (sy0i == psy0i && sy1i == psy1i) {
Chris@812 1677 // same source scan line as just computed
Chris@535 1678 goto copy;
Chris@535 1679 }
Chris@535 1680
Chris@812 1681 psy0i = sy0i;
Chris@812 1682 psy1i = sy1i;
Chris@812 1683
Chris@535 1684 for (int x = 0; x < w; ++x) {
Chris@535 1685 peaks[x] = 0;
Chris@535 1686 }
Chris@466 1687
Chris@535 1688 for (int sy = sy0i; sy <= sy1i; ++sy) {
Chris@535 1689
Chris@535 1690 if (sy < 0 || sy >= source->height()) continue;
Chris@535 1691
Chris@535 1692 uchar *sourceLine = source->scanLine(sy);
Chris@535 1693
Chris@535 1694 for (int x = 0; x < w; ++x) {
Chris@535 1695
Chris@903 1696 double sx1 = sxa[x*2 + 1];
Chris@537 1697 if (sx1 < 0) continue;
Chris@537 1698 int sx1i = int(sx1);
Chris@535 1699
Chris@903 1700 double sx0 = sxa[x*2];
Chris@537 1701 if (sx0 < 0) continue;
Chris@537 1702 int sx0i = int(sx0 + epsilon);
Chris@535 1703 if (sx0i >= sw) break;
Chris@535 1704
Chris@535 1705 uchar peak = 0;
Chris@535 1706 for (int sx = sx0i; sx <= sx1i; ++sx) {
Chris@535 1707 if (sx < 0 || sx >= sw) continue;
Chris@535 1708 if (sourceLine[sx] > peak) peak = sourceLine[sx];
Chris@535 1709 }
Chris@535 1710 peaks[x] = peak;
Chris@535 1711 }
Chris@535 1712 }
Chris@535 1713
Chris@535 1714 copy:
Chris@535 1715 for (int x = 0; x < w; ++x) {
Chris@535 1716 targetLine[x] = peaks[x];
Chris@535 1717 }
Chris@98 1718 }
Chris@0 1719 }
Chris@0 1720
Chris@469 1721 delete[] peaks;
Chris@469 1722
Chris@98 1723 paint.drawImage(x0, 0, img);
Chris@0 1724 }
Chris@0 1725
Chris@28 1726 bool
Chris@916 1727 Colour3DPlotLayer::snapToFeatureFrame(LayerGeometryProvider *v, sv_frame_t &frame,
Chris@805 1728 int &resolution,
Chris@28 1729 SnapType snap) const
Chris@24 1730 {
Chris@24 1731 if (!m_model) {
Chris@44 1732 return Layer::snapToFeatureFrame(v, frame, resolution, snap);
Chris@24 1733 }
Chris@24 1734
Chris@130 1735 resolution = m_model->getResolution();
Chris@904 1736 sv_frame_t left = (frame / resolution) * resolution;
Chris@904 1737 sv_frame_t right = left + resolution;
Chris@28 1738
Chris@28 1739 switch (snap) {
Chris@28 1740 case SnapLeft: frame = left; break;
Chris@28 1741 case SnapRight: frame = right; break;
Chris@28 1742 case SnapNearest:
Chris@28 1743 case SnapNeighbouring:
Chris@28 1744 if (frame - left > right - frame) frame = right;
Chris@28 1745 else frame = left;
Chris@28 1746 break;
Chris@28 1747 }
Chris@24 1748
Chris@28 1749 return true;
Chris@24 1750 }
Chris@24 1751
Chris@1099 1752 static ColourScale::Scale
Chris@1099 1753 convertInColourScale(int fileScale)
Chris@1099 1754 {
Chris@1099 1755 //!!! this is very badly handled, switching the enum directly for
Chris@1099 1756 //!!! the ColourScale one did not work out well!
Chris@1099 1757
Chris@1099 1758 switch (fileScale) {
Chris@1099 1759 default:
Chris@1099 1760 case 0: return ColourScale::LinearColourScale;
Chris@1099 1761 case 1: return ColourScale::LogColourScale;
Chris@1099 1762 case 2: return ColourScale::PlusMinusOneScale;
Chris@1099 1763 case 3: return ColourScale::AbsoluteScale;
Chris@1099 1764 }
Chris@1099 1765 }
Chris@1099 1766
Chris@1099 1767 static int
Chris@1099 1768 convertOutColourScale(ColourScale::Scale scale)
Chris@1099 1769 {
Chris@1099 1770 switch (scale) {
Chris@1099 1771 case ColourScale::LinearColourScale: return 0;
Chris@1099 1772 case ColourScale::LogColourScale: return 1;
Chris@1099 1773 case ColourScale::PlusMinusOneScale: return 2;
Chris@1099 1774 case ColourScale::AbsoluteScale: return 3;
Chris@1099 1775
Chris@1099 1776 case ColourScale::MeterColourScale:
Chris@1099 1777 case ColourScale::PhaseColourScale:
Chris@1099 1778 default: return 0;
Chris@1099 1779 }
Chris@1099 1780 }
Chris@1099 1781
Chris@316 1782 void
Chris@316 1783 Colour3DPlotLayer::toXml(QTextStream &stream,
Chris@316 1784 QString indent, QString extraAttributes) const
Chris@197 1785 {
Chris@316 1786 QString s = QString("scale=\"%1\" "
Chris@316 1787 "colourScheme=\"%2\" "
Chris@1099 1788 "minY=\"%3\" "
Chris@1099 1789 "maxY=\"%4\" "
Chris@1099 1790 "invertVertical=\"%5\" "
Chris@1099 1791 "opaque=\"%6\" %7")
Chris@1099 1792 .arg(convertOutColourScale(m_colourScale))
Chris@197 1793 .arg(m_colourMap)
Chris@445 1794 .arg(m_miny)
Chris@465 1795 .arg(m_maxy)
Chris@465 1796 .arg(m_invertVertical ? "true" : "false")
Chris@534 1797 .arg(m_opaque ? "true" : "false")
Chris@536 1798 .arg(QString("binScale=\"%1\" smooth=\"%2\" gain=\"%3\" ")
Chris@535 1799 .arg((int)m_binScale)
Chris@536 1800 .arg(m_smooth ? "true" : "false")
Chris@536 1801 .arg(m_gain));
Chris@535 1802
Chris@1099 1803 // New-style normalization attributes, allowing for more types of
Chris@1099 1804 // normalization in future: write out the column normalization
Chris@1099 1805 // type separately, and then whether we are normalizing visible
Chris@1099 1806 // area as well afterwards
Chris@1099 1807
Chris@1099 1808 s += QString("columnNormalization=\"%1\" ")
Chris@1099 1809 .arg(m_normalization == ColumnOp::NormalizeColumns ? "peak" :
Chris@1099 1810 m_normalization == ColumnOp::NormalizeHybrid ? "hybrid" : "none");
Chris@1099 1811
Chris@1099 1812 // Old-style normalization attribute, for backward compatibility
Chris@1099 1813
Chris@1099 1814 s += QString("normalizeColumns=\"%1\" ")
Chris@1099 1815 .arg(m_normalization == ColumnOp::NormalizeColumns ? "true" : "false");
Chris@1099 1816
Chris@1099 1817 // And this applies to both old- and new-style attributes
Chris@1099 1818
Chris@1099 1819 s += QString("normalizeVisibleArea=\"%1\" ")
Chris@1099 1820 .arg(m_normalization == ColumnOp::NormalizeVisibleArea ? "true" : "false");
Chris@1099 1821
Chris@316 1822 Layer::toXml(stream, indent, extraAttributes + " " + s);
Chris@197 1823 }
Chris@197 1824
Chris@197 1825 void
Chris@197 1826 Colour3DPlotLayer::setProperties(const QXmlAttributes &attributes)
Chris@197 1827 {
Chris@445 1828 bool ok = false, alsoOk = false;
Chris@197 1829
Chris@1099 1830 ColourScale::Scale colourScale = convertInColourScale
Chris@1099 1831 (attributes.value("colourScale").toInt(&ok));
Chris@1099 1832 if (ok) setColourScale(colourScale);
Chris@197 1833
Chris@197 1834 int colourMap = attributes.value("colourScheme").toInt(&ok);
Chris@197 1835 if (ok) setColourMap(colourMap);
Chris@197 1836
Chris@1099 1837 Colour3DPlotRenderer::BinScale binScale = (Colour3DPlotRenderer::BinScale)
Chris@1099 1838 attributes.value("binScale").toInt(&ok);
Chris@1099 1839 if (ok) setBinScale(binScale);
Chris@445 1840
Chris@465 1841 bool invertVertical =
Chris@465 1842 (attributes.value("invertVertical").trimmed() == "true");
Chris@465 1843 setInvertVertical(invertVertical);
Chris@465 1844
Chris@465 1845 bool opaque =
Chris@465 1846 (attributes.value("opaque").trimmed() == "true");
Chris@535 1847 setOpaque(opaque);
Chris@535 1848
Chris@535 1849 bool smooth =
Chris@535 1850 (attributes.value("smooth").trimmed() == "true");
Chris@535 1851 setSmooth(smooth);
Chris@465 1852
Chris@536 1853 float gain = attributes.value("gain").toFloat(&ok);
Chris@536 1854 if (ok) setGain(gain);
Chris@536 1855
Chris@445 1856 float min = attributes.value("minY").toFloat(&ok);
Chris@445 1857 float max = attributes.value("maxY").toFloat(&alsoOk);
Chris@445 1858 if (ok && alsoOk) setDisplayExtents(min, max);
Chris@1099 1859
Chris@1099 1860 bool haveNewStyleNormalization = false;
Chris@1099 1861
Chris@1099 1862 QString columnNormalization = attributes.value("columnNormalization");
Chris@1099 1863
Chris@1099 1864 if (columnNormalization != "") {
Chris@1099 1865
Chris@1099 1866 haveNewStyleNormalization = true;
Chris@1099 1867
Chris@1099 1868 if (columnNormalization == "peak") {
Chris@1099 1869 setNormalization(ColumnOp::NormalizeColumns);
Chris@1099 1870 } else if (columnNormalization == "hybrid") {
Chris@1099 1871 setNormalization(ColumnOp::NormalizeHybrid);
Chris@1099 1872 } else if (columnNormalization == "none") {
Chris@1099 1873 // do nothing
Chris@1099 1874 } else {
Chris@1099 1875 cerr << "NOTE: Unknown or unsupported columnNormalization attribute \""
Chris@1099 1876 << columnNormalization << "\"" << endl;
Chris@1099 1877 }
Chris@1099 1878 }
Chris@1099 1879
Chris@1099 1880 if (!haveNewStyleNormalization) {
Chris@1099 1881
Chris@1099 1882 bool normalizeColumns =
Chris@1099 1883 (attributes.value("normalizeColumns").trimmed() == "true");
Chris@1099 1884 if (normalizeColumns) {
Chris@1099 1885 setNormalization(ColumnOp::NormalizeColumns);
Chris@1099 1886 }
Chris@1099 1887
Chris@1099 1888 bool normalizeHybrid =
Chris@1099 1889 (attributes.value("normalizeHybrid").trimmed() == "true");
Chris@1099 1890 if (normalizeHybrid) {
Chris@1099 1891 setNormalization(ColumnOp::NormalizeHybrid);
Chris@1099 1892 }
Chris@1099 1893 }
Chris@1099 1894
Chris@1099 1895 bool normalizeVisibleArea =
Chris@1099 1896 (attributes.value("normalizeVisibleArea").trimmed() == "true");
Chris@1099 1897 if (normalizeVisibleArea) {
Chris@1099 1898 setNormalization(ColumnOp::NormalizeVisibleArea);
Chris@1099 1899 }
Chris@1099 1900
Chris@1099 1901 //!!! todo: check save/reload scaling, compare with
Chris@1099 1902 //!!! SpectrogramLayer, compare with prior SV versions, compare
Chris@1099 1903 //!!! with Tony v1 and v2 and their save files
Chris@197 1904 }
Chris@197 1905