annotate layer/Colour3DPlotLayer.cpp @ 1077:5144d7185fb5 spectrogram-minor-refactor

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