annotate layer/Colour3DPlotLayer.cpp @ 1106:8abdefce36a6 spectrogram-minor-refactor

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