annotate layer/Colour3DPlotLayer.cpp @ 1103:d84a0033b305 spectrogram-minor-refactor

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