annotate layer/Colour3DPlotLayer.cpp @ 531:0bd0b517e795

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