annotate layer/Colour3DPlotLayer.cpp @ 978:64c2b3a4435a 3.0-integration

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