annotate layer/Colour3DPlotLayer.cpp @ 970:1c37aa13bfd8

Add half-wave rectification option to Colour 3D Plot layer
author Chris Cannam
date Fri, 22 May 2015 09:17:52 +0100
parents 4a578a360011
children 1011ffb1b6d5 08f20a23fb81
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@475 533 Colour3DPlotLayer::setLayerDormant(const View *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@44 557 Colour3DPlotLayer::isLayerScrollable(const View *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@805 616 Colour3DPlotLayer::getYScaleValue(const View *, 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@904 672 Colour3DPlotLayer::getYForBin(View *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@904 678 double h = v->height();
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@903 690 Colour3DPlotLayer::getIYForBin(View *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@904 696 Colour3DPlotLayer::getBinForY(View *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@904 702 double h = v->height();
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@903 714 Colour3DPlotLayer::getIBinForY(View *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@44 720 Colour3DPlotLayer::getFeatureDescription(View *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@904 751 // double binHeight = double(v->height()) / (symax - symin);
Chris@534 752 // int sy = int((v->height() - 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@248 782 Colour3DPlotLayer::getColourScaleWidth(QPainter &) const
Chris@159 783 {
Chris@159 784 int cw = 20;
Chris@159 785 return cw;
Chris@159 786 }
Chris@159 787
Chris@159 788 int
Chris@607 789 Colour3DPlotLayer::getVerticalScaleWidth(View *, bool, QPainter &paint) const
Chris@25 790 {
Chris@25 791 if (!m_model) return 0;
Chris@25 792
Chris@160 793 QString sampleText = QString("[%1]").arg(m_model->getHeight());
Chris@25 794 int tw = paint.fontMetrics().width(sampleText);
Chris@98 795 bool another = false;
Chris@25 796
Chris@805 797 for (int i = 0; i < m_model->getHeight(); ++i) {
Chris@25 798 if (m_model->getBinName(i).length() > sampleText.length()) {
Chris@25 799 sampleText = m_model->getBinName(i);
Chris@98 800 another = true;
Chris@25 801 }
Chris@25 802 }
Chris@98 803 if (another) {
Chris@25 804 tw = std::max(tw, paint.fontMetrics().width(sampleText));
Chris@25 805 }
Chris@25 806
Chris@159 807 return tw + 13 + getColourScaleWidth(paint);
Chris@25 808 }
Chris@25 809
Chris@25 810 void
Chris@607 811 Colour3DPlotLayer::paintVerticalScale(View *v, bool, QPainter &paint, QRect rect) const
Chris@25 812 {
Chris@25 813 if (!m_model) return;
Chris@25 814
Chris@25 815 int h = rect.height(), w = rect.width();
Chris@25 816
Chris@159 817 int cw = getColourScaleWidth(paint);
Chris@159 818
Chris@159 819 int ch = h - 20;
Chris@159 820 if (ch > 20 && m_cache) {
Chris@159 821
Chris@904 822 double min = m_model->getMinimumLevel();
Chris@904 823 double max = m_model->getMaximumLevel();
Chris@447 824
Chris@904 825 double mmin = min;
Chris@904 826 double mmax = max;
Chris@447 827
Chris@447 828 if (m_colourScale == LogScale) {
Chris@447 829 LogRange::mapRange(mmin, mmax);
Chris@447 830 } else if (m_colourScale == PlusMinusOneScale) {
Chris@447 831 mmin = -1.f;
Chris@447 832 mmax = 1.f;
Chris@509 833 } else if (m_colourScale == AbsoluteScale) {
Chris@509 834 if (mmin < 0) {
Chris@904 835 if (fabs(mmin) > fabs(mmax)) mmax = fabs(mmin);
Chris@904 836 else mmax = fabs(mmax);
Chris@509 837 mmin = 0;
Chris@509 838 } else {
Chris@904 839 mmin = fabs(mmin);
Chris@904 840 mmax = fabs(mmax);
Chris@509 841 }
Chris@447 842 }
Chris@447 843
Chris@902 844 if (max == min) max = min + 1.f;
Chris@902 845 if (mmax == mmin) mmax = mmin + 1.f;
Chris@447 846
Chris@287 847 paint.setPen(v->getForeground());
Chris@447 848 paint.drawRect(4, 10, cw - 8, ch+1);
Chris@159 849
Chris@446 850 for (int y = 0; y < ch; ++y) {
Chris@904 851 double value = ((max - min) * (double(ch-y) - 1.0)) / double(ch) + min;
Chris@447 852 if (m_colourScale == LogScale) {
Chris@447 853 value = LogRange::map(value);
Chris@447 854 }
Chris@447 855 int pixel = int(((value - mmin) * 256) / (mmax - mmin));
Chris@465 856 if (pixel >= 0 && pixel < 256) {
Chris@465 857 QRgb c = m_cache->color(pixel);
Chris@465 858 paint.setPen(QColor(qRed(c), qGreen(c), qBlue(c)));
Chris@465 859 paint.drawLine(5, 11 + y, cw - 5, 11 + y);
Chris@465 860 } else {
Chris@682 861 cerr << "WARNING: Colour3DPlotLayer::paintVerticalScale: value " << value << ", mmin " << mmin << ", mmax " << mmax << " leads to invalid pixel " << pixel << endl;
Chris@465 862 }
Chris@159 863 }
Chris@446 864
Chris@446 865 QString minstr = QString("%1").arg(min);
Chris@446 866 QString maxstr = QString("%1").arg(max);
Chris@446 867
Chris@446 868 paint.save();
Chris@446 869
Chris@446 870 QFont font = paint.font();
Chris@446 871 font.setPixelSize(10);
Chris@446 872 paint.setFont(font);
Chris@446 873
Chris@446 874 int msw = paint.fontMetrics().width(maxstr);
Chris@446 875
Chris@446 876 QMatrix m;
Chris@446 877 m.translate(cw - 6, ch + 10);
Chris@446 878 m.rotate(-90);
Chris@446 879
Chris@446 880 paint.setWorldMatrix(m);
Chris@446 881
Chris@446 882 v->drawVisibleText(paint, 2, 0, minstr, View::OutlinedText);
Chris@446 883
Chris@446 884 m.translate(ch - msw - 2, 0);
Chris@446 885 paint.setWorldMatrix(m);
Chris@446 886
Chris@446 887 v->drawVisibleText(paint, 0, 0, maxstr, View::OutlinedText);
Chris@446 888
Chris@446 889 paint.restore();
Chris@159 890 }
Chris@159 891
Chris@287 892 paint.setPen(v->getForeground());
Chris@159 893
Chris@445 894 int sh = m_model->getHeight();
Chris@445 895
Chris@445 896 int symin = m_miny;
Chris@445 897 int symax = m_maxy;
Chris@445 898 if (symax <= symin) {
Chris@445 899 symin = 0;
Chris@445 900 symax = sh;
Chris@445 901 }
Chris@445 902 if (symin < 0) symin = 0;
Chris@445 903 if (symax > sh) symax = sh;
Chris@445 904
Chris@532 905 paint.save();
Chris@456 906
Chris@533 907 int py = h;
Chris@25 908
Chris@805 909 for (int i = symin; i <= symax; ++i) {
Chris@98 910
Chris@532 911 int y0;
Chris@534 912
Chris@903 913 y0 = getIYForBin(v, i);
Chris@532 914 int h = py - y0;
Chris@532 915
Chris@532 916 if (i > symin) {
Chris@532 917 if (paint.fontMetrics().height() >= h) {
Chris@533 918 if (h >= 8) {
Chris@532 919 QFont tf = paint.font();
Chris@533 920 tf.setPixelSize(h-2);
Chris@532 921 paint.setFont(tf);
Chris@532 922 } else {
Chris@532 923 continue;
Chris@532 924 }
Chris@532 925 }
Chris@532 926 }
Chris@25 927
Chris@532 928 py = y0;
Chris@532 929
Chris@534 930 if (i < symax) {
Chris@534 931 paint.drawLine(cw, y0, w, y0);
Chris@534 932 }
Chris@25 933
Chris@532 934 if (i > symin) {
Chris@534 935
Chris@805 936 int idx = i - 1;
Chris@534 937 if (m_invertVertical) idx = m_model->getHeight() - idx - 1;
Chris@534 938
Chris@534 939 QString text = m_model->getBinName(idx);
Chris@534 940 if (text == "") text = QString("[%1]").arg(idx + 1);
Chris@534 941
Chris@534 942 int ty = y0 + (h/2) - (paint.fontMetrics().height()/2) +
Chris@534 943 paint.fontMetrics().ascent() + 1;
Chris@534 944
Chris@534 945 paint.drawText(cw + 5, ty, text);
Chris@457 946 }
Chris@25 947 }
Chris@456 948
Chris@456 949 paint.restore();
Chris@25 950 }
Chris@25 951
Chris@467 952 DenseThreeDimensionalModel::Column
Chris@805 953 Colour3DPlotLayer::getColumn(int col) const
Chris@197 954 {
Chris@812 955 Profiler profiler("Colour3DPlotLayer::getColumn");
Chris@812 956
Chris@970 957 DenseThreeDimensionalModel::Column prev;
Chris@970 958 if (m_rectified && (col > m_model->getStartFrame())) {
Chris@970 959 prev = m_model->getColumn(col - 1);
Chris@970 960 }
Chris@970 961
Chris@467 962 DenseThreeDimensionalModel::Column values = m_model->getColumn(col);
Chris@970 963
Chris@970 964 if (m_rectified && !prev.empty()) {
Chris@970 965 for (int y = 0; y < values.size(); ++y) {
Chris@970 966 if (values[y] < prev[y]) values[y] = 0;
Chris@970 967 else values[y] -= prev[y];
Chris@970 968 }
Chris@970 969 }
Chris@970 970
Chris@468 971 while (values.size() < m_model->getHeight()) values.push_back(0.f);
Chris@719 972 if (!m_normalizeColumns && !m_normalizeHybrid) return values;
Chris@461 973
Chris@904 974 double colMax = 0.f, colMin = 0.f;
Chris@904 975 double min = 0.f, max = 0.f;
Chris@197 976
Chris@461 977 min = m_model->getMinimumLevel();
Chris@461 978 max = m_model->getMaximumLevel();
Chris@461 979
Chris@805 980 for (int y = 0; y < values.size(); ++y) {
Chris@467 981 if (y == 0 || values.at(y) > colMax) colMax = values.at(y);
Chris@467 982 if (y == 0 || values.at(y) < colMin) colMin = values.at(y);
Chris@197 983 }
Chris@461 984 if (colMin == colMax) colMax = colMin + 1;
Chris@197 985
Chris@805 986 for (int y = 0; y < values.size(); ++y) {
Chris@461 987
Chris@904 988 double value = values.at(y);
Chris@904 989 double norm = (value - colMin) / (colMax - colMin);
Chris@904 990 double newvalue = min + (max - min) * norm;
Chris@197 991
Chris@904 992 if (value != newvalue) values[y] = float(newvalue);
Chris@468 993 }
Chris@468 994
Chris@719 995 if (m_normalizeHybrid && (colMax > 0.0)) {
Chris@904 996 double logmax = log10(colMax);
Chris@805 997 for (int y = 0; y < values.size(); ++y) {
Chris@904 998 values[y] = float(values[y] * logmax);
Chris@719 999 }
Chris@719 1000 }
Chris@719 1001
Chris@468 1002 return values;
Chris@197 1003 }
Chris@197 1004
Chris@197 1005 void
Chris@805 1006 Colour3DPlotLayer::fillCache(int firstBin, int lastBin) const
Chris@197 1007 {
Chris@812 1008 Profiler profiler("Colour3DPlotLayer::fillCache", true);
Chris@476 1009
Chris@902 1010 sv_frame_t modelStart = m_model->getStartFrame();
Chris@902 1011 sv_frame_t modelEnd = m_model->getEndFrame();
Chris@805 1012 int modelResolution = m_model->getResolution();
Chris@197 1013
Chris@902 1014 int modelStartBin = int(modelStart / modelResolution);
Chris@902 1015 int modelEndBin = int(modelEnd / modelResolution);
Chris@461 1016
Chris@812 1017 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@812 1018 cerr << "Colour3DPlotLayer::fillCache: range " << firstBin << " -> " << lastBin << " of model range " << modelStartBin << " -> " << modelEndBin << " (model resolution " << modelResolution << ")" << endl;
Chris@812 1019 #endif
Chris@812 1020
Chris@805 1021 int cacheWidth = modelEndBin - modelStartBin + 1;
Chris@471 1022 if (lastBin > modelEndBin) cacheWidth = lastBin - modelStartBin + 1;
Chris@805 1023 int cacheHeight = m_model->getHeight();
Chris@461 1024
Chris@812 1025 if (m_cache && m_cache->height() != cacheHeight) {
Chris@742 1026 // height has changed: delete everything rather than resizing
Chris@812 1027 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@812 1028 cerr << "Colour3DPlotLayer::fillCache: Cache height has changed, recreating" << endl;
Chris@812 1029 #endif
Chris@461 1030 delete m_cache;
Chris@469 1031 delete m_peaksCache;
Chris@461 1032 m_cache = 0;
Chris@469 1033 m_peaksCache = 0;
Chris@461 1034 }
Chris@461 1035
Chris@812 1036 if (m_cache && m_cache->width() != cacheWidth) {
Chris@742 1037 // width has changed and we have an existing cache: resize it
Chris@812 1038 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@812 1039 cerr << "Colour3DPlotLayer::fillCache: Cache width has changed, resizing existing cache" << endl;
Chris@812 1040 #endif
Chris@469 1041 QImage *newCache =
Chris@469 1042 new QImage(m_cache->copy(0, 0, cacheWidth, cacheHeight));
Chris@461 1043 delete m_cache;
Chris@461 1044 m_cache = newCache;
Chris@469 1045 if (m_peaksCache) {
Chris@469 1046 QImage *newPeaksCache =
Chris@469 1047 new QImage(m_peaksCache->copy
Chris@742 1048 (0, 0, cacheWidth / m_peakResolution + 1, cacheHeight));
Chris@469 1049 delete m_peaksCache;
Chris@469 1050 m_peaksCache = newPeaksCache;
Chris@469 1051 }
Chris@197 1052 }
Chris@197 1053
Chris@461 1054 if (!m_cache) {
Chris@812 1055 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@812 1056 cerr << "Colour3DPlotLayer::fillCache: Have no cache, making one" << endl;
Chris@812 1057 #endif
Chris@469 1058 m_cache = new QImage
Chris@469 1059 (cacheWidth, cacheHeight, QImage::Format_Indexed8);
Chris@812 1060 m_cache->setColorCount(256);
Chris@514 1061 m_cache->fill(0);
Chris@474 1062 if (!m_normalizeVisibleArea) {
Chris@469 1063 m_peaksCache = new QImage
Chris@469 1064 (cacheWidth / m_peakResolution + 1, cacheHeight,
Chris@469 1065 QImage::Format_Indexed8);
Chris@812 1066 m_peaksCache->setColorCount(256);
Chris@514 1067 m_peaksCache->fill(0);
Chris@469 1068 } else if (m_peaksCache) {
Chris@469 1069 delete m_peaksCache;
Chris@469 1070 m_peaksCache = 0;
Chris@469 1071 }
Chris@461 1072 m_cacheValidStart = 0;
Chris@461 1073 m_cacheValidEnd = 0;
Chris@197 1074 }
Chris@197 1075
Chris@812 1076 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@812 1077 cerr << "cache size = " << m_cache->width() << "x" << m_cache->height()
Chris@812 1078 << " peaks cache size = " << m_peaksCache->width() << "x" << m_peaksCache->height() << endl;
Chris@812 1079 #endif
Chris@742 1080
Chris@461 1081 if (m_cacheValidStart <= firstBin && m_cacheValidEnd >= lastBin) {
Chris@519 1082 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@682 1083 cerr << "Cache is valid in this region already" << endl;
Chris@519 1084 #endif
Chris@461 1085 return;
Chris@461 1086 }
Chris@461 1087
Chris@805 1088 int fillStart = firstBin;
Chris@805 1089 int fillEnd = lastBin;
Chris@197 1090
Chris@461 1091 if (fillStart < modelStartBin) fillStart = modelStartBin;
Chris@461 1092 if (fillStart > modelEndBin) fillStart = modelEndBin;
Chris@461 1093 if (fillEnd < modelStartBin) fillEnd = modelStartBin;
Chris@461 1094 if (fillEnd > modelEndBin) fillEnd = modelEndBin;
Chris@197 1095
Chris@461 1096 bool normalizeVisible = (m_normalizeVisibleArea && !m_normalizeColumns);
Chris@197 1097
Chris@461 1098 if (!normalizeVisible && (m_cacheValidStart < m_cacheValidEnd)) {
Chris@461 1099
Chris@461 1100 if (m_cacheValidEnd < fillStart) {
Chris@461 1101 fillStart = m_cacheValidEnd + 1;
Chris@461 1102 }
Chris@461 1103 if (m_cacheValidStart > fillEnd) {
Chris@461 1104 fillEnd = m_cacheValidStart - 1;
Chris@461 1105 }
Chris@461 1106
Chris@461 1107 m_cacheValidStart = std::min(fillStart, m_cacheValidStart);
Chris@461 1108 m_cacheValidEnd = std::max(fillEnd, m_cacheValidEnd);
Chris@461 1109
Chris@461 1110 } else {
Chris@461 1111
Chris@812 1112 // when normalising the visible area, the only valid area,
Chris@812 1113 // ever, is the currently visible one
Chris@461 1114
Chris@461 1115 m_cacheValidStart = fillStart;
Chris@461 1116 m_cacheValidEnd = fillEnd;
Chris@461 1117 }
Chris@461 1118
Chris@519 1119 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@812 1120 cerr << "Cache size " << cacheWidth << "x" << cacheHeight << " will be valid from " << m_cacheValidStart << " to " << m_cacheValidEnd << " (fillStart = " << fillStart << ", fillEnd = " << fillEnd << ")" << endl;
Chris@519 1121 #endif
Chris@461 1122
Chris@197 1123 DenseThreeDimensionalModel::Column values;
Chris@197 1124
Chris@904 1125 double min = m_model->getMinimumLevel();
Chris@904 1126 double max = m_model->getMaximumLevel();
Chris@197 1127
Chris@197 1128 if (m_colourScale == LogScale) {
Chris@197 1129 LogRange::mapRange(min, max);
Chris@197 1130 } else if (m_colourScale == PlusMinusOneScale) {
Chris@197 1131 min = -1.f;
Chris@197 1132 max = 1.f;
Chris@509 1133 } else if (m_colourScale == AbsoluteScale) {
Chris@509 1134 if (min < 0) {
Chris@904 1135 if (fabs(min) > fabs(max)) max = fabs(min);
Chris@904 1136 else max = fabs(max);
Chris@509 1137 min = 0;
Chris@509 1138 } else {
Chris@904 1139 min = fabs(min);
Chris@904 1140 max = fabs(max);
Chris@509 1141 }
Chris@197 1142 }
Chris@197 1143
Chris@902 1144 if (max == min) max = min + 1.f;
Chris@197 1145
Chris@197 1146 ColourMapper mapper(m_colourMap, 0.f, 255.f);
Chris@197 1147
Chris@197 1148 for (int index = 0; index < 256; ++index) {
Chris@197 1149 QColor colour = mapper.map(index);
Chris@469 1150 m_cache->setColor
Chris@469 1151 (index, qRgb(colour.red(), colour.green(), colour.blue()));
Chris@469 1152 if (m_peaksCache) {
Chris@469 1153 m_peaksCache->setColor
Chris@469 1154 (index, qRgb(colour.red(), colour.green(), colour.blue()));
Chris@469 1155 }
Chris@197 1156 }
Chris@197 1157
Chris@904 1158 double visibleMax = 0.f, visibleMin = 0.f;
Chris@197 1159
Chris@461 1160 if (normalizeVisible) {
Chris@197 1161
Chris@805 1162 for (int c = fillStart; c <= fillEnd; ++c) {
Chris@197 1163
Chris@467 1164 values = getColumn(c);
Chris@197 1165
Chris@904 1166 double colMax = 0.f, colMin = 0.f;
Chris@197 1167
Chris@805 1168 for (int y = 0; y < cacheHeight; ++y) {
Chris@197 1169 if (y >= values.size()) break;
Chris@197 1170 if (y == 0 || values[y] > colMax) colMax = values[y];
Chris@224 1171 if (y == 0 || values[y] < colMin) colMin = values[y];
Chris@197 1172 }
Chris@197 1173
Chris@461 1174 if (c == fillStart || colMax > visibleMax) visibleMax = colMax;
Chris@461 1175 if (c == fillStart || colMin < visibleMin) visibleMin = colMin;
Chris@461 1176 }
Chris@461 1177
Chris@461 1178 if (m_colourScale == LogScale) {
Chris@461 1179 visibleMin = LogRange::map(visibleMin);
Chris@461 1180 visibleMax = LogRange::map(visibleMax);
Chris@461 1181 if (visibleMin > visibleMax) std::swap(visibleMin, visibleMax);
Chris@509 1182 } else if (m_colourScale == AbsoluteScale) {
Chris@509 1183 if (visibleMin < 0) {
Chris@904 1184 if (fabs(visibleMin) > fabs(visibleMax)) visibleMax = fabs(visibleMin);
Chris@904 1185 else visibleMax = fabs(visibleMax);
Chris@509 1186 visibleMin = 0;
Chris@509 1187 } else {
Chris@904 1188 visibleMin = fabs(visibleMin);
Chris@904 1189 visibleMax = fabs(visibleMax);
Chris@509 1190 }
Chris@197 1191 }
Chris@197 1192 }
Chris@197 1193
Chris@224 1194 if (visibleMin == visibleMax) visibleMax = visibleMin + 1;
Chris@224 1195
Chris@469 1196 int *peaks = 0;
Chris@469 1197 if (m_peaksCache) {
Chris@469 1198 peaks = new int[cacheHeight];
Chris@469 1199 for (int y = 0; y < cacheHeight; ++y) {
Chris@469 1200 peaks[y] = 0;
Chris@469 1201 }
Chris@469 1202 }
Chris@469 1203
Chris@812 1204 Profiler profiler2("Colour3DPlotLayer::fillCache: filling", true);
Chris@812 1205
Chris@805 1206 for (int c = fillStart; c <= fillEnd; ++c) {
Chris@197 1207
Chris@467 1208 values = getColumn(c);
Chris@197 1209
Chris@742 1210 if (c >= m_cache->width()) {
Chris@742 1211 cerr << "ERROR: column " << c << " >= cache width "
Chris@742 1212 << m_cache->width() << endl;
Chris@742 1213 continue;
Chris@742 1214 }
Chris@742 1215
Chris@805 1216 for (int y = 0; y < cacheHeight; ++y) {
Chris@197 1217
Chris@904 1218 double value = min;
Chris@197 1219 if (y < values.size()) {
Chris@469 1220 value = values.at(y);
Chris@197 1221 }
Chris@224 1222
Chris@534 1223 value = value * m_gain;
Chris@534 1224
Chris@224 1225 if (m_colourScale == LogScale) {
Chris@224 1226 value = LogRange::map(value);
Chris@509 1227 } else if (m_colourScale == AbsoluteScale) {
Chris@904 1228 value = fabs(value);
Chris@197 1229 }
Chris@461 1230
Chris@461 1231 if (normalizeVisible) {
Chris@904 1232 double norm = (value - visibleMin) / (visibleMax - visibleMin);
Chris@461 1233 value = min + (max - min) * norm;
Chris@461 1234 }
Chris@197 1235
Chris@197 1236 int pixel = int(((value - min) * 256) / (max - min));
Chris@197 1237 if (pixel < 0) pixel = 0;
Chris@197 1238 if (pixel > 255) pixel = 255;
Chris@469 1239 if (peaks && (pixel > peaks[y])) peaks[y] = pixel;
Chris@197 1240
Chris@357 1241 if (m_invertVertical) {
Chris@461 1242 m_cache->setPixel(c, cacheHeight - y - 1, pixel);
Chris@357 1243 } else {
Chris@742 1244 if (y >= m_cache->height()) {
Chris@742 1245 cerr << "ERROR: row " << y << " >= cache height " << m_cache->height() << endl;
Chris@742 1246 } else {
Chris@742 1247 m_cache->setPixel(c, y, pixel);
Chris@742 1248 }
Chris@357 1249 }
Chris@197 1250 }
Chris@469 1251
Chris@469 1252 if (peaks) {
Chris@805 1253 int notch = (c % m_peakResolution);
Chris@469 1254 if (notch == m_peakResolution-1 || c == fillEnd) {
Chris@805 1255 int pc = c / m_peakResolution;
Chris@742 1256 if (pc >= m_peaksCache->width()) {
Chris@742 1257 cerr << "ERROR: peak column " << pc
Chris@742 1258 << " (from col " << c << ") >= peaks cache width "
Chris@742 1259 << m_peaksCache->width() << endl;
Chris@742 1260 continue;
Chris@742 1261 }
Chris@805 1262 for (int y = 0; y < cacheHeight; ++y) {
Chris@469 1263 if (m_invertVertical) {
Chris@469 1264 m_peaksCache->setPixel(pc, cacheHeight - y - 1, peaks[y]);
Chris@469 1265 } else {
Chris@742 1266 if (y >= m_peaksCache->height()) {
Chris@742 1267 cerr << "ERROR: row " << y
Chris@742 1268 << " >= peaks cache height "
Chris@742 1269 << m_peaksCache->height() << endl;
Chris@742 1270 } else {
Chris@742 1271 m_peaksCache->setPixel(pc, y, peaks[y]);
Chris@742 1272 }
Chris@469 1273 }
Chris@469 1274 }
Chris@469 1275 for (int y = 0; y < cacheHeight; ++y) {
Chris@469 1276 peaks[y] = 0;
Chris@469 1277 }
Chris@469 1278 }
Chris@469 1279 }
Chris@197 1280 }
Chris@469 1281
Chris@469 1282 delete[] peaks;
Chris@197 1283 }
Chris@197 1284
Chris@812 1285 bool
Chris@812 1286 Colour3DPlotLayer::shouldPaintDenseIn(const View *v) const
Chris@812 1287 {
Chris@812 1288 if (!m_model || !v || !(v->getViewManager())) {
Chris@812 1289 return false;
Chris@812 1290 }
Chris@902 1291 double srRatio =
Chris@902 1292 v->getViewManager()->getMainModelSampleRate() / m_model->getSampleRate();
Chris@812 1293 if (m_opaque ||
Chris@812 1294 m_smooth ||
Chris@812 1295 m_model->getHeight() >= v->height() ||
Chris@812 1296 ((m_model->getResolution() * srRatio) / v->getZoomLevel()) < 2) {
Chris@812 1297 return true;
Chris@812 1298 }
Chris@812 1299 return false;
Chris@812 1300 }
Chris@812 1301
Chris@197 1302 void
Chris@44 1303 Colour3DPlotLayer::paint(View *v, QPainter &paint, QRect rect) const
Chris@0 1304 {
Chris@514 1305 /*
Chris@443 1306 if (m_model) {
Chris@587 1307 SVDEBUG << "Colour3DPlotLayer::paint: model says shouldUseLogValueScale = " << m_model->shouldUseLogValueScale() << endl;
Chris@443 1308 }
Chris@514 1309 */
Chris@466 1310 Profiler profiler("Colour3DPlotLayer::paint");
Chris@125 1311 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@812 1312 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 1313 #endif
Chris@0 1314
Chris@0 1315 int completion = 0;
Chris@0 1316 if (!m_model || !m_model->isOK() || !m_model->isReady(&completion)) {
Chris@0 1317 if (completion > 0) {
Chris@44 1318 paint.fillRect(0, 10, v->width() * completion / 100,
Chris@0 1319 10, QColor(120, 120, 120));
Chris@0 1320 }
Chris@0 1321 return;
Chris@0 1322 }
Chris@0 1323
Chris@197 1324 if (m_normalizeVisibleArea && !m_normalizeColumns) rect = v->rect();
Chris@197 1325
Chris@902 1326 sv_frame_t modelStart = m_model->getStartFrame();
Chris@902 1327 sv_frame_t modelEnd = m_model->getEndFrame();
Chris@805 1328 int modelResolution = m_model->getResolution();
Chris@0 1329
Chris@197 1330 // The cache is from the model's start frame to the model's end
Chris@197 1331 // frame at the model's window increment frames per pixel. We
Chris@197 1332 // want to draw from our start frame + x0 * zoomLevel to our start
Chris@197 1333 // frame + x1 * zoomLevel at zoomLevel frames per pixel.
Chris@12 1334
Chris@197 1335 // We have quite different paint mechanisms for rendering "large"
Chris@197 1336 // bins (more than one bin per pixel in both directions) and
Chris@197 1337 // "small". This is "large"; see paintDense below for "small".
Chris@12 1338
Chris@197 1339 int x0 = rect.left();
Chris@197 1340 int x1 = rect.right() + 1;
Chris@12 1341
Chris@197 1342 int h = v->height();
Chris@0 1343
Chris@902 1344 double srRatio =
Chris@902 1345 v->getViewManager()->getMainModelSampleRate() / m_model->getSampleRate();
Chris@0 1346
Chris@902 1347 int sx0 = int((double(v->getFrameForX(x0)) / srRatio - double(modelStart))
Chris@812 1348 / modelResolution);
Chris@902 1349 int sx1 = int((double(v->getFrameForX(x1)) / srRatio - double(modelStart))
Chris@812 1350 / modelResolution);
Chris@197 1351 int sh = m_model->getHeight();
Chris@159 1352
Chris@444 1353 int symin = m_miny;
Chris@444 1354 int symax = m_maxy;
Chris@444 1355 if (symax <= symin) {
Chris@444 1356 symin = 0;
Chris@444 1357 symax = sh;
Chris@444 1358 }
Chris@444 1359 if (symin < 0) symin = 0;
Chris@444 1360 if (symax > sh) symax = sh;
Chris@444 1361
Chris@197 1362 if (sx0 > 0) --sx0;
Chris@197 1363 fillCache(sx0 < 0 ? 0 : sx0,
Chris@197 1364 sx1 < 0 ? 0 : sx1);
Chris@0 1365
Chris@351 1366 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@812 1367 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 1368 #endif
Chris@351 1369
Chris@812 1370 if (shouldPaintDenseIn(v)) {
Chris@347 1371 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@812 1372 cerr << "calling paintDense" << endl;
Chris@347 1373 #endif
Chris@98 1374 paintDense(v, paint, rect);
Chris@98 1375 return;
Chris@98 1376 }
Chris@98 1377
Chris@125 1378 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@812 1379 cerr << "Colour3DPlotLayer::paint: w " << x1-x0 << ", h " << h << ", sx0 " << sx0 << ", sx1 " << sx1 << ", sw " << sx1-sx0 << ", sh " << sh << endl;
Chris@682 1380 cerr << "Colour3DPlotLayer: sample rate is " << m_model->getSampleRate() << ", resolution " << m_model->getResolution() << endl;
Chris@125 1381 #endif
Chris@0 1382
Chris@25 1383 QPoint illuminatePos;
Chris@44 1384 bool illuminate = v->shouldIlluminateLocalFeatures(this, illuminatePos);
Chris@864 1385
Chris@864 1386 const int buflen = 40;
Chris@864 1387 char labelbuf[buflen];
Chris@25 1388
Chris@197 1389 for (int sx = sx0; sx <= sx1; ++sx) {
Chris@0 1390
Chris@902 1391 sv_frame_t fx = sx * modelResolution;
Chris@0 1392
Chris@812 1393 if (fx + modelResolution <= modelStart || fx > modelEnd) continue;
Chris@0 1394
Chris@902 1395 int rx0 = v->getXForFrame(int(double(fx + modelStart) * srRatio));
Chris@902 1396 int rx1 = v->getXForFrame(int(double(fx + modelStart + modelResolution + 1) * srRatio));
Chris@54 1397
Chris@159 1398 int rw = rx1 - rx0;
Chris@159 1399 if (rw < 1) rw = 1;
Chris@54 1400
Chris@159 1401 bool showLabel = (rw > 10 &&
Chris@159 1402 paint.fontMetrics().width("0.000000") < rw - 3 &&
Chris@54 1403 paint.fontMetrics().height() < (h / sh));
Chris@98 1404
Chris@444 1405 for (int sy = symin; sy < symax; ++sy) {
Chris@0 1406
Chris@903 1407 int ry0 = getIYForBin(v, sy);
Chris@903 1408 int ry1 = getIYForBin(v, sy + 1);
Chris@534 1409 QRect r(rx0, ry1, rw, ry0 - ry1);
Chris@534 1410
Chris@0 1411 QRgb pixel = qRgb(255, 255, 255);
Chris@461 1412 if (sx >= 0 && sx < m_cache->width() &&
Chris@0 1413 sy >= 0 && sy < m_cache->height()) {
Chris@461 1414 pixel = m_cache->pixel(sx, sy);
Chris@0 1415 }
Chris@0 1416
Chris@159 1417 if (rw == 1) {
Chris@159 1418 paint.setPen(pixel);
Chris@159 1419 paint.setBrush(Qt::NoBrush);
Chris@159 1420 paint.drawLine(r.x(), r.y(), r.x(), r.y() + r.height() - 1);
Chris@159 1421 continue;
Chris@159 1422 }
Chris@159 1423
Chris@0 1424 QColor pen(255, 255, 255, 80);
Chris@0 1425 QColor brush(pixel);
Chris@159 1426
Chris@159 1427 if (rw > 3 && r.height() > 3) {
Chris@159 1428 brush.setAlpha(160);
Chris@159 1429 }
Chris@159 1430
Chris@0 1431 paint.setPen(Qt::NoPen);
Chris@0 1432 paint.setBrush(brush);
Chris@0 1433
Chris@25 1434 if (illuminate) {
Chris@25 1435 if (r.contains(illuminatePos)) {
Chris@287 1436 paint.setPen(v->getForeground());
Chris@25 1437 }
Chris@25 1438 }
Chris@76 1439
Chris@125 1440 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@682 1441 // cerr << "rect " << r.x() << "," << r.y() << " "
Chris@682 1442 // << r.width() << "x" << r.height() << endl;
Chris@125 1443 #endif
Chris@25 1444
Chris@25 1445 paint.drawRect(r);
Chris@0 1446
Chris@54 1447 if (showLabel) {
Chris@461 1448 if (sx >= 0 && sx < m_cache->width() &&
Chris@54 1449 sy >= 0 && sy < m_cache->height()) {
Chris@904 1450 double value = m_model->getValueAt(sx, sy);
Chris@864 1451 snprintf(labelbuf, buflen, "%06f", value);
Chris@54 1452 QString text(labelbuf);
Chris@287 1453 paint.setPen(v->getBackground());
Chris@54 1454 paint.drawText(rx0 + 2,
Chris@54 1455 ry0 - h / sh - 1 + 2 + paint.fontMetrics().ascent(),
Chris@54 1456 text);
Chris@0 1457 }
Chris@0 1458 }
Chris@0 1459 }
Chris@0 1460 }
Chris@98 1461 }
Chris@0 1462
Chris@98 1463 void
Chris@98 1464 Colour3DPlotLayer::paintDense(View *v, QPainter &paint, QRect rect) const
Chris@98 1465 {
Chris@812 1466 Profiler profiler("Colour3DPlotLayer::paintDense", true);
Chris@463 1467 if (!m_cache) return;
Chris@463 1468
Chris@903 1469 double modelStart = double(m_model->getStartFrame());
Chris@903 1470 double modelResolution = double(m_model->getResolution());
Chris@0 1471
Chris@903 1472 sv_samplerate_t mmsr = v->getViewManager()->getMainModelSampleRate();
Chris@903 1473 sv_samplerate_t msr = m_model->getSampleRate();
Chris@903 1474 double srRatio = mmsr / msr;
Chris@159 1475
Chris@98 1476 int x0 = rect.left();
Chris@98 1477 int x1 = rect.right() + 1;
Chris@0 1478
Chris@470 1479 const int w = x1 - x0; // const so it can be used as array size below
Chris@471 1480 int h = v->height(); // we always paint full height
Chris@160 1481 int sh = m_model->getHeight();
Chris@98 1482
Chris@444 1483 int symin = m_miny;
Chris@444 1484 int symax = m_maxy;
Chris@444 1485 if (symax <= symin) {
Chris@444 1486 symin = 0;
Chris@444 1487 symax = sh;
Chris@444 1488 }
Chris@444 1489 if (symin < 0) symin = 0;
Chris@444 1490 if (symax > sh) symax = sh;
Chris@444 1491
Chris@466 1492 QImage img(w, h, QImage::Format_Indexed8);
Chris@466 1493 img.setColorTable(m_cache->colorTable());
Chris@98 1494
Chris@466 1495 uchar *peaks = new uchar[w];
Chris@466 1496 memset(peaks, 0, w);
Chris@98 1497
Chris@469 1498 int zoomLevel = v->getZoomLevel();
Chris@469 1499
Chris@469 1500 QImage *source = m_cache;
Chris@812 1501
Chris@812 1502 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@812 1503 cerr << "modelResolution " << modelResolution << ", srRatio "
Chris@812 1504 << srRatio << ", m_peakResolution " << m_peakResolution
Chris@812 1505 << ", zoomLevel " << zoomLevel << ", result "
Chris@812 1506 << ((modelResolution * srRatio * m_peakResolution) / zoomLevel)
Chris@812 1507 << endl;
Chris@812 1508 #endif
Chris@474 1509
Chris@474 1510 if (m_peaksCache) {
Chris@474 1511 if (((modelResolution * srRatio * m_peakResolution) / zoomLevel) < 1) {
Chris@812 1512 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@812 1513 cerr << "using peaks cache" << endl;
Chris@812 1514 #endif
Chris@474 1515 source = m_peaksCache;
Chris@474 1516 modelResolution *= m_peakResolution;
Chris@474 1517 } else {
Chris@812 1518 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@812 1519 cerr << "not using peaks cache" << endl;
Chris@812 1520 #endif
Chris@474 1521 }
Chris@469 1522 } else {
Chris@812 1523 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@812 1524 cerr << "have no peaks cache" << endl;
Chris@812 1525 #endif
Chris@469 1526 }
Chris@469 1527
Chris@469 1528 int sw = source->width();
Chris@470 1529
Chris@903 1530 sv_frame_t xf = -1;
Chris@903 1531 sv_frame_t nxf = v->getFrameForX(x0);
Chris@470 1532
Chris@903 1533 double epsilon = 0.000001;
Chris@535 1534
Chris@903 1535 vector<double> sxa(w*2);
Chris@903 1536
Chris@470 1537 for (int x = 0; x < w; ++x) {
Chris@470 1538
Chris@470 1539 xf = nxf;
Chris@470 1540 nxf = xf + zoomLevel;
Chris@470 1541
Chris@903 1542 double sx0 = (double(xf) / srRatio - modelStart) / modelResolution;
Chris@903 1543 double sx1 = (double(nxf) / srRatio - modelStart) / modelResolution;
Chris@470 1544
Chris@535 1545 sxa[x*2] = sx0;
Chris@535 1546 sxa[x*2 + 1] = sx1;
Chris@470 1547 }
Chris@466 1548
Chris@904 1549 double logmin = symin+1, logmax = symax+1;
Chris@532 1550 LogRange::mapRange(logmin, logmax);
Chris@532 1551
Chris@812 1552 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@812 1553 cerr << "m_smooth = " << m_smooth << ", w = " << w << ", h = " << h << endl;
Chris@812 1554 #endif
Chris@812 1555
Chris@535 1556 if (m_smooth) {
Chris@535 1557
Chris@535 1558 for (int y = 0; y < h; ++y) {
Chris@466 1559
Chris@904 1560 double sy = getBinForY(v, y) - 0.5;
Chris@535 1561 int syi = int(sy + epsilon);
Chris@535 1562 if (syi < 0 || syi >= source->height()) continue;
Chris@531 1563
Chris@535 1564 uchar *targetLine = img.scanLine(y);
Chris@535 1565 uchar *sourceLine = source->scanLine(syi);
Chris@535 1566 uchar *nextSource;
Chris@535 1567 if (syi + 1 < source->height()) {
Chris@535 1568 nextSource = source->scanLine(syi + 1);
Chris@535 1569 } else {
Chris@535 1570 nextSource = sourceLine;
Chris@535 1571 }
Chris@466 1572
Chris@466 1573 for (int x = 0; x < w; ++x) {
Chris@98 1574
Chris@535 1575 targetLine[x] = 0;
Chris@474 1576
Chris@903 1577 double sx0 = sxa[x*2];
Chris@537 1578 if (sx0 < 0) continue;
Chris@535 1579 int sx0i = int(sx0 + epsilon);
Chris@474 1580 if (sx0i >= sw) break;
Chris@98 1581
Chris@903 1582 double a = sourceLine[sx0i];
Chris@903 1583 double b = a;
Chris@903 1584 double value;
Chris@535 1585
Chris@903 1586 double sx1 = sxa[x*2+1];
Chris@535 1587 if (sx1 > sx0 + 1.f) {
Chris@535 1588 int sx1i = int(sx1);
Chris@535 1589 bool have = false;
Chris@535 1590 for (int sx = sx0i; sx <= sx1i; ++sx) {
Chris@535 1591 if (sx < 0 || sx >= sw) continue;
Chris@535 1592 if (!have) {
Chris@903 1593 a = sourceLine[sx];
Chris@903 1594 b = nextSource[sx];
Chris@535 1595 have = true;
Chris@535 1596 } else {
Chris@903 1597 a = std::max(a, double(sourceLine[sx]));
Chris@903 1598 b = std::max(b, double(nextSource[sx]));
Chris@535 1599 }
Chris@535 1600 }
Chris@903 1601 double yprop = sy - syi;
Chris@535 1602 value = (a * (1.f - yprop) + b * yprop);
Chris@535 1603 } else {
Chris@903 1604 a = sourceLine[sx0i];
Chris@903 1605 b = nextSource[sx0i];
Chris@903 1606 double yprop = sy - syi;
Chris@535 1607 value = (a * (1.f - yprop) + b * yprop);
Chris@535 1608 int oi = sx0i + 1;
Chris@903 1609 double xprop = sx0 - sx0i;
Chris@535 1610 xprop -= 0.5;
Chris@535 1611 if (xprop < 0) {
Chris@535 1612 oi = sx0i - 1;
Chris@535 1613 xprop = -xprop;
Chris@535 1614 }
Chris@535 1615 if (oi < 0 || oi >= sw) oi = sx0i;
Chris@903 1616 a = sourceLine[oi];
Chris@903 1617 b = nextSource[oi];
Chris@535 1618 value = (value * (1.f - xprop) +
Chris@535 1619 (a * (1.f - yprop) + b * yprop) * xprop);
Chris@98 1620 }
Chris@535 1621
Chris@903 1622 int vi = int(lrint(value));
Chris@535 1623 if (vi > 255) vi = 255;
Chris@535 1624 if (vi < 0) vi = 0;
Chris@535 1625 targetLine[x] = uchar(vi);
Chris@98 1626 }
Chris@466 1627 }
Chris@535 1628 } else {
Chris@535 1629
Chris@904 1630 double sy0 = getBinForY(v, 0);
Chris@812 1631
Chris@812 1632 int psy0i = -1, psy1i = -1;
Chris@812 1633
Chris@535 1634 for (int y = 0; y < h; ++y) {
Chris@535 1635
Chris@904 1636 double sy1 = sy0;
Chris@904 1637 sy0 = getBinForY(v, double(y + 1));
Chris@535 1638
Chris@535 1639 int sy0i = int(sy0 + epsilon);
Chris@535 1640 int sy1i = int(sy1);
Chris@535 1641
Chris@535 1642 uchar *targetLine = img.scanLine(y);
Chris@535 1643
Chris@812 1644 if (sy0i == psy0i && sy1i == psy1i) {
Chris@812 1645 // same source scan line as just computed
Chris@535 1646 goto copy;
Chris@535 1647 }
Chris@535 1648
Chris@812 1649 psy0i = sy0i;
Chris@812 1650 psy1i = sy1i;
Chris@812 1651
Chris@535 1652 for (int x = 0; x < w; ++x) {
Chris@535 1653 peaks[x] = 0;
Chris@535 1654 }
Chris@466 1655
Chris@535 1656 for (int sy = sy0i; sy <= sy1i; ++sy) {
Chris@535 1657
Chris@535 1658 if (sy < 0 || sy >= source->height()) continue;
Chris@535 1659
Chris@535 1660 uchar *sourceLine = source->scanLine(sy);
Chris@535 1661
Chris@535 1662 for (int x = 0; x < w; ++x) {
Chris@535 1663
Chris@903 1664 double sx1 = sxa[x*2 + 1];
Chris@537 1665 if (sx1 < 0) continue;
Chris@537 1666 int sx1i = int(sx1);
Chris@535 1667
Chris@903 1668 double sx0 = sxa[x*2];
Chris@537 1669 if (sx0 < 0) continue;
Chris@537 1670 int sx0i = int(sx0 + epsilon);
Chris@535 1671 if (sx0i >= sw) break;
Chris@535 1672
Chris@535 1673 uchar peak = 0;
Chris@535 1674 for (int sx = sx0i; sx <= sx1i; ++sx) {
Chris@535 1675 if (sx < 0 || sx >= sw) continue;
Chris@535 1676 if (sourceLine[sx] > peak) peak = sourceLine[sx];
Chris@535 1677 }
Chris@535 1678 peaks[x] = peak;
Chris@535 1679 }
Chris@535 1680 }
Chris@535 1681
Chris@535 1682 copy:
Chris@535 1683 for (int x = 0; x < w; ++x) {
Chris@535 1684 targetLine[x] = peaks[x];
Chris@535 1685 }
Chris@98 1686 }
Chris@0 1687 }
Chris@0 1688
Chris@469 1689 delete[] peaks;
Chris@469 1690
Chris@98 1691 paint.drawImage(x0, 0, img);
Chris@0 1692 }
Chris@0 1693
Chris@28 1694 bool
Chris@904 1695 Colour3DPlotLayer::snapToFeatureFrame(View *v, sv_frame_t &frame,
Chris@805 1696 int &resolution,
Chris@28 1697 SnapType snap) const
Chris@24 1698 {
Chris@24 1699 if (!m_model) {
Chris@44 1700 return Layer::snapToFeatureFrame(v, frame, resolution, snap);
Chris@24 1701 }
Chris@24 1702
Chris@130 1703 resolution = m_model->getResolution();
Chris@904 1704 sv_frame_t left = (frame / resolution) * resolution;
Chris@904 1705 sv_frame_t right = left + resolution;
Chris@28 1706
Chris@28 1707 switch (snap) {
Chris@28 1708 case SnapLeft: frame = left; break;
Chris@28 1709 case SnapRight: frame = right; break;
Chris@28 1710 case SnapNearest:
Chris@28 1711 case SnapNeighbouring:
Chris@28 1712 if (frame - left > right - frame) frame = right;
Chris@28 1713 else frame = left;
Chris@28 1714 break;
Chris@28 1715 }
Chris@24 1716
Chris@28 1717 return true;
Chris@24 1718 }
Chris@24 1719
Chris@316 1720 void
Chris@316 1721 Colour3DPlotLayer::toXml(QTextStream &stream,
Chris@316 1722 QString indent, QString extraAttributes) const
Chris@197 1723 {
Chris@316 1724 QString s = QString("scale=\"%1\" "
Chris@316 1725 "colourScheme=\"%2\" "
Chris@316 1726 "normalizeColumns=\"%3\" "
Chris@445 1727 "normalizeVisibleArea=\"%4\" "
Chris@445 1728 "minY=\"%5\" "
Chris@465 1729 "maxY=\"%6\" "
Chris@465 1730 "invertVertical=\"%7\" "
Chris@535 1731 "opaque=\"%8\" %9")
Chris@197 1732 .arg((int)m_colourScale)
Chris@197 1733 .arg(m_colourMap)
Chris@197 1734 .arg(m_normalizeColumns ? "true" : "false")
Chris@445 1735 .arg(m_normalizeVisibleArea ? "true" : "false")
Chris@445 1736 .arg(m_miny)
Chris@465 1737 .arg(m_maxy)
Chris@465 1738 .arg(m_invertVertical ? "true" : "false")
Chris@534 1739 .arg(m_opaque ? "true" : "false")
Chris@536 1740 .arg(QString("binScale=\"%1\" smooth=\"%2\" gain=\"%3\" ")
Chris@535 1741 .arg((int)m_binScale)
Chris@536 1742 .arg(m_smooth ? "true" : "false")
Chris@536 1743 .arg(m_gain));
Chris@535 1744
Chris@316 1745 Layer::toXml(stream, indent, extraAttributes + " " + s);
Chris@197 1746 }
Chris@197 1747
Chris@197 1748 void
Chris@197 1749 Colour3DPlotLayer::setProperties(const QXmlAttributes &attributes)
Chris@197 1750 {
Chris@445 1751 bool ok = false, alsoOk = false;
Chris@197 1752
Chris@197 1753 ColourScale scale = (ColourScale)attributes.value("scale").toInt(&ok);
Chris@197 1754 if (ok) setColourScale(scale);
Chris@197 1755
Chris@197 1756 int colourMap = attributes.value("colourScheme").toInt(&ok);
Chris@197 1757 if (ok) setColourMap(colourMap);
Chris@197 1758
Chris@534 1759 BinScale binscale = (BinScale)attributes.value("binScale").toInt(&ok);
Chris@534 1760 if (ok) setBinScale(binscale);
Chris@534 1761
Chris@197 1762 bool normalizeColumns =
Chris@197 1763 (attributes.value("normalizeColumns").trimmed() == "true");
Chris@197 1764 setNormalizeColumns(normalizeColumns);
Chris@197 1765
Chris@197 1766 bool normalizeVisibleArea =
Chris@197 1767 (attributes.value("normalizeVisibleArea").trimmed() == "true");
Chris@197 1768 setNormalizeVisibleArea(normalizeVisibleArea);
Chris@445 1769
Chris@465 1770 bool invertVertical =
Chris@465 1771 (attributes.value("invertVertical").trimmed() == "true");
Chris@465 1772 setInvertVertical(invertVertical);
Chris@465 1773
Chris@465 1774 bool opaque =
Chris@465 1775 (attributes.value("opaque").trimmed() == "true");
Chris@535 1776 setOpaque(opaque);
Chris@535 1777
Chris@535 1778 bool smooth =
Chris@535 1779 (attributes.value("smooth").trimmed() == "true");
Chris@535 1780 setSmooth(smooth);
Chris@465 1781
Chris@536 1782 float gain = attributes.value("gain").toFloat(&ok);
Chris@536 1783 if (ok) setGain(gain);
Chris@536 1784
Chris@445 1785 float min = attributes.value("minY").toFloat(&ok);
Chris@445 1786 float max = attributes.value("maxY").toFloat(&alsoOk);
Chris@445 1787 if (ok && alsoOk) setDisplayExtents(min, max);
Chris@197 1788 }
Chris@197 1789