Chris@58: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@0: Chris@0: /* Chris@59: Sonic Visualiser Chris@59: An audio file viewer and annotation editor. Chris@59: Centre for Digital Music, Queen Mary, University of London. Chris@182: This file copyright 2006 Chris Cannam and QMUL. Chris@0: Chris@59: This program is free software; you can redistribute it and/or Chris@59: modify it under the terms of the GNU General Public License as Chris@59: published by the Free Software Foundation; either version 2 of the Chris@59: License, or (at your option) any later version. See the file Chris@59: COPYING included with this distribution for more information. Chris@0: */ Chris@0: Chris@0: #include "Colour3DPlotLayer.h" Chris@0: Chris@128: #include "view/View.h" Chris@0: #include "base/Profiler.h" Chris@197: #include "base/LogRange.h" Chris@444: #include "base/RangeMapper.h" Chris@376: #include "ColourMapper.h" Chris@0: Chris@0: #include Chris@0: #include Chris@0: #include Chris@316: #include Chris@0: Chris@0: #include Chris@0: Chris@0: #include Chris@0: Chris@545: #ifndef __GNUC__ Chris@545: #include Chris@545: #endif Chris@545: Chris@353: //#define DEBUG_COLOUR_3D_PLOT_LAYER_PAINT 1 Chris@125: Chris@0: Chris@44: Colour3DPlotLayer::Colour3DPlotLayer() : Chris@0: m_model(0), Chris@159: m_cache(0), Chris@469: m_peaksCache(0), Chris@461: m_cacheValidStart(0), Chris@461: m_cacheValidEnd(0), Chris@197: m_colourScale(LinearScale), Chris@461: m_colourScaleSet(false), Chris@197: m_colourMap(0), Chris@534: m_gain(1.0), Chris@531: m_binScale(LinearBinScale), Chris@197: m_normalizeColumns(false), Chris@357: m_normalizeVisibleArea(false), Chris@719: m_normalizeHybrid(false), Chris@444: m_invertVertical(false), Chris@465: m_opaque(false), Chris@535: m_smooth(false), Chris@805: m_peakResolution(256), Chris@444: m_miny(0), Chris@444: m_maxy(0) Chris@0: { Chris@44: Chris@0: } Chris@0: Chris@0: Colour3DPlotLayer::~Colour3DPlotLayer() Chris@0: { Chris@461: delete m_cache; Chris@469: delete m_peaksCache; Chris@0: } Chris@0: Chris@0: void Chris@0: Colour3DPlotLayer::setModel(const DenseThreeDimensionalModel *model) Chris@0: { Chris@193: if (m_model == model) return; Chris@193: const DenseThreeDimensionalModel *oldModel = m_model; Chris@0: m_model = model; Chris@0: if (!m_model || !m_model->isOK()) return; Chris@0: Chris@320: connectSignals(m_model); Chris@0: Chris@461: connect(m_model, SIGNAL(modelChanged()), this, SLOT(modelChanged())); Chris@823: connect(m_model, SIGNAL(modelChangedWithin(int, int)), Chris@823: this, SLOT(modelChangedWithin(int, int))); Chris@0: Chris@474: m_peakResolution = 256; Chris@474: if (model->getResolution() > 512) { Chris@474: m_peakResolution = 16; Chris@474: } else if (model->getResolution() > 128) { Chris@474: m_peakResolution = 64; Chris@474: } else if (model->getResolution() > 2) { Chris@474: m_peakResolution = 128; Chris@474: } Chris@474: cacheInvalid(); Chris@474: Chris@0: emit modelReplaced(); Chris@193: emit sliceableModelReplaced(oldModel, model); Chris@0: } Chris@0: Chris@0: void Chris@0: Colour3DPlotLayer::cacheInvalid() Chris@0: { Chris@469: delete m_cache; Chris@469: delete m_peaksCache; Chris@0: m_cache = 0; Chris@469: m_peaksCache = 0; Chris@461: m_cacheValidStart = 0; Chris@461: m_cacheValidEnd = 0; Chris@0: } Chris@0: Chris@0: void Chris@805: Colour3DPlotLayer::cacheInvalid(int startFrame, int endFrame) Chris@0: { Chris@843: if (!m_cache || !m_model) return; Chris@461: Chris@805: int modelResolution = m_model->getResolution(); Chris@805: int start = startFrame / modelResolution; Chris@805: int end = endFrame / modelResolution + 1; Chris@461: if (m_cacheValidStart < end) m_cacheValidStart = end; Chris@461: if (m_cacheValidEnd > start) m_cacheValidEnd = start; Chris@461: if (m_cacheValidStart > m_cacheValidEnd) m_cacheValidEnd = m_cacheValidStart; Chris@461: } Chris@461: Chris@461: void Chris@461: Colour3DPlotLayer::modelChanged() Chris@461: { Chris@461: if (!m_colourScaleSet && m_colourScale == LinearScale) { Chris@461: if (m_model) { Chris@461: if (m_model->shouldUseLogValueScale()) { Chris@461: setColourScale(LogScale); Chris@461: } else { Chris@461: m_colourScaleSet = true; Chris@461: } Chris@461: } Chris@461: } Chris@0: cacheInvalid(); Chris@0: } Chris@0: Chris@461: void Chris@823: Colour3DPlotLayer::modelChangedWithin(int startFrame, int endFrame) Chris@461: { Chris@461: if (!m_colourScaleSet && m_colourScale == LinearScale) { Chris@461: if (m_model && m_model->getWidth() > 50) { Chris@461: if (m_model->shouldUseLogValueScale()) { Chris@461: setColourScale(LogScale); Chris@461: } else { Chris@461: m_colourScaleSet = true; Chris@461: } Chris@461: } Chris@461: } Chris@461: cacheInvalid(startFrame, endFrame); Chris@461: } Chris@461: Chris@159: Layer::PropertyList Chris@159: Colour3DPlotLayer::getProperties() const Chris@159: { Chris@159: PropertyList list; Chris@197: list.push_back("Colour"); Chris@159: list.push_back("Colour Scale"); Chris@197: list.push_back("Normalize Columns"); Chris@197: list.push_back("Normalize Visible Area"); Chris@534: list.push_back("Gain"); Chris@531: list.push_back("Bin Scale"); Chris@357: list.push_back("Invert Vertical Scale"); Chris@465: list.push_back("Opaque"); Chris@535: list.push_back("Smooth"); Chris@159: return list; Chris@159: } Chris@159: Chris@159: QString Chris@159: Colour3DPlotLayer::getPropertyLabel(const PropertyName &name) const Chris@159: { Chris@197: if (name == "Colour") return tr("Colour"); Chris@197: if (name == "Colour Scale") return tr("Scale"); Chris@197: if (name == "Normalize Columns") return tr("Normalize Columns"); Chris@197: if (name == "Normalize Visible Area") return tr("Normalize Visible Area"); Chris@357: if (name == "Invert Vertical Scale") return tr("Invert Vertical Scale"); Chris@534: if (name == "Gain") return tr("Gain"); Chris@465: if (name == "Opaque") return tr("Always Opaque"); Chris@535: if (name == "Smooth") return tr("Smooth"); Chris@531: if (name == "Bin Scale") return tr("Bin Scale"); Chris@159: return ""; Chris@159: } Chris@159: Chris@346: QString Chris@346: Colour3DPlotLayer::getPropertyIconName(const PropertyName &name) const Chris@346: { Chris@346: if (name == "Normalize Columns") return "normalise-columns"; Chris@346: if (name == "Normalize Visible Area") return "normalise"; Chris@357: if (name == "Invert Vertical Scale") return "invert-vertical"; Chris@465: if (name == "Opaque") return "opaque"; Chris@535: if (name == "Smooth") return "smooth"; Chris@346: return ""; Chris@346: } Chris@346: Chris@159: Layer::PropertyType Chris@159: Colour3DPlotLayer::getPropertyType(const PropertyName &name) const Chris@159: { Chris@534: if (name == "Gain") return RangeProperty; Chris@197: if (name == "Normalize Columns") return ToggleProperty; Chris@197: if (name == "Normalize Visible Area") return ToggleProperty; Chris@357: if (name == "Invert Vertical Scale") return ToggleProperty; Chris@465: if (name == "Opaque") return ToggleProperty; Chris@535: if (name == "Smooth") return ToggleProperty; Chris@159: return ValueProperty; Chris@159: } Chris@159: Chris@159: QString Chris@159: Colour3DPlotLayer::getPropertyGroupName(const PropertyName &name) const Chris@159: { Chris@197: if (name == "Normalize Columns" || Chris@197: name == "Normalize Visible Area" || Chris@534: name == "Colour Scale" || Chris@534: name == "Gain") return tr("Scale"); Chris@531: if (name == "Bin Scale" || Chris@531: name == "Invert Vertical Scale") return tr("Bins"); Chris@465: if (name == "Opaque" || Chris@535: name == "Smooth" || Chris@465: name == "Colour") return tr("Colour"); Chris@159: return QString(); Chris@159: } Chris@159: Chris@159: int Chris@159: Colour3DPlotLayer::getPropertyRangeAndValue(const PropertyName &name, Chris@216: int *min, int *max, int *deflt) const Chris@159: { Chris@216: int val = 0; Chris@159: Chris@216: int garbage0, garbage1, garbage2; Chris@159: if (!min) min = &garbage0; Chris@159: if (!max) max = &garbage1; Chris@216: if (!deflt) deflt = &garbage2; Chris@159: Chris@534: if (name == "Gain") { Chris@534: Chris@534: *min = -50; Chris@534: *max = 50; Chris@534: Chris@534: *deflt = lrintf(log10(1.f) * 20.0);; Chris@534: if (*deflt < *min) *deflt = *min; Chris@534: if (*deflt > *max) *deflt = *max; Chris@534: Chris@534: val = lrintf(log10(m_gain) * 20.0); Chris@534: if (val < *min) val = *min; Chris@534: if (val > *max) val = *max; Chris@534: Chris@534: } else if (name == "Colour Scale") { Chris@159: Chris@159: *min = 0; Chris@509: *max = 3; Chris@216: *deflt = (int)LinearScale; Chris@159: Chris@216: val = (int)m_colourScale; Chris@159: Chris@197: } else if (name == "Colour") { Chris@197: Chris@197: *min = 0; Chris@197: *max = ColourMapper::getColourMapCount() - 1; Chris@216: *deflt = 0; Chris@197: Chris@216: val = m_colourMap; Chris@197: Chris@197: } else if (name == "Normalize Columns") { Chris@197: Chris@216: *deflt = 0; Chris@216: val = (m_normalizeColumns ? 1 : 0); Chris@197: Chris@197: } else if (name == "Normalize Visible Area") { Chris@197: Chris@216: *deflt = 0; Chris@216: val = (m_normalizeVisibleArea ? 1 : 0); Chris@197: Chris@357: } else if (name == "Invert Vertical Scale") { Chris@357: Chris@357: *deflt = 0; Chris@357: val = (m_invertVertical ? 1 : 0); Chris@357: Chris@531: } else if (name == "Bin Scale") { Chris@531: Chris@531: *min = 0; Chris@531: *max = 1; Chris@531: *deflt = int(LinearBinScale); Chris@531: val = (int)m_binScale; Chris@531: Chris@465: } else if (name == "Opaque") { Chris@465: Chris@465: *deflt = 0; Chris@465: val = (m_opaque ? 1 : 0); Chris@465: Chris@535: } else if (name == "Smooth") { Chris@535: Chris@535: *deflt = 0; Chris@535: val = (m_smooth ? 1 : 0); Chris@535: Chris@159: } else { Chris@216: val = Layer::getPropertyRangeAndValue(name, min, max, deflt); Chris@159: } Chris@159: Chris@216: return val; Chris@159: } Chris@159: Chris@159: QString Chris@159: Colour3DPlotLayer::getPropertyValueLabel(const PropertyName &name, Chris@159: int value) const Chris@159: { Chris@197: if (name == "Colour") { Chris@197: return ColourMapper::getColourMapName(value); Chris@197: } Chris@159: if (name == "Colour Scale") { Chris@159: switch (value) { Chris@159: default: Chris@198: case 0: return tr("Linear"); Chris@198: case 1: return tr("Log"); Chris@198: case 2: return tr("+/-1"); Chris@509: case 3: return tr("Absolute"); Chris@159: } Chris@159: } Chris@531: if (name == "Bin Scale") { Chris@531: switch (value) { Chris@531: default: Chris@531: case 0: return tr("Linear"); Chris@531: case 1: return tr("Log"); Chris@531: } Chris@531: } Chris@159: return tr(""); Chris@159: } Chris@159: Chris@534: RangeMapper * Chris@534: Colour3DPlotLayer::getNewPropertyRangeMapper(const PropertyName &name) const Chris@534: { Chris@534: if (name == "Gain") { Chris@534: return new LinearRangeMapper(-50, 50, -25, 25, tr("dB")); Chris@534: } Chris@534: return 0; Chris@534: } Chris@534: Chris@159: void Chris@159: Colour3DPlotLayer::setProperty(const PropertyName &name, int value) Chris@159: { Chris@534: if (name == "Gain") { Chris@534: setGain(pow(10, float(value)/20.0)); Chris@534: } else if (name == "Colour Scale") { Chris@159: switch (value) { Chris@159: default: Chris@159: case 0: setColourScale(LinearScale); break; Chris@197: case 1: setColourScale(LogScale); break; Chris@197: case 2: setColourScale(PlusMinusOneScale); break; Chris@509: case 3: setColourScale(AbsoluteScale); break; Chris@159: } Chris@197: } else if (name == "Colour") { Chris@197: setColourMap(value); Chris@197: } else if (name == "Normalize Columns") { Chris@197: setNormalizeColumns(value ? true : false); Chris@197: } else if (name == "Normalize Visible Area") { Chris@197: setNormalizeVisibleArea(value ? true : false); Chris@357: } else if (name == "Invert Vertical Scale") { Chris@357: setInvertVertical(value ? true : false); Chris@465: } else if (name == "Opaque") { Chris@465: setOpaque(value ? true : false); Chris@535: } else if (name == "Smooth") { Chris@535: setSmooth(value ? true : false); Chris@531: } else if (name == "Bin Scale") { Chris@531: switch (value) { Chris@531: default: Chris@531: case 0: setBinScale(LinearBinScale); break; Chris@531: case 1: setBinScale(LogBinScale); break; Chris@531: } Chris@159: } Chris@159: } Chris@159: Chris@159: void Chris@159: Colour3DPlotLayer::setColourScale(ColourScale scale) Chris@159: { Chris@159: if (m_colourScale == scale) return; Chris@159: m_colourScale = scale; Chris@461: m_colourScaleSet = true; Chris@159: cacheInvalid(); Chris@159: emit layerParametersChanged(); Chris@159: } Chris@159: Chris@197: void Chris@197: Colour3DPlotLayer::setColourMap(int map) Chris@197: { Chris@197: if (m_colourMap == map) return; Chris@197: m_colourMap = map; Chris@197: cacheInvalid(); Chris@197: emit layerParametersChanged(); Chris@197: } Chris@197: Chris@197: void Chris@534: Colour3DPlotLayer::setGain(float gain) Chris@534: { Chris@534: if (m_gain == gain) return; Chris@534: m_gain = gain; Chris@534: cacheInvalid(); Chris@534: emit layerParametersChanged(); Chris@534: } Chris@534: Chris@534: float Chris@534: Colour3DPlotLayer::getGain() const Chris@534: { Chris@534: return m_gain; Chris@534: } Chris@534: Chris@534: void Chris@531: Colour3DPlotLayer::setBinScale(BinScale binScale) Chris@531: { Chris@531: if (m_binScale == binScale) return; Chris@531: m_binScale = binScale; Chris@531: cacheInvalid(); Chris@531: emit layerParametersChanged(); Chris@531: } Chris@531: Chris@531: Colour3DPlotLayer::BinScale Chris@531: Colour3DPlotLayer::getBinScale() const Chris@531: { Chris@531: return m_binScale; Chris@531: } Chris@531: Chris@531: void Chris@197: Colour3DPlotLayer::setNormalizeColumns(bool n) Chris@197: { Chris@197: if (m_normalizeColumns == n) return; Chris@197: m_normalizeColumns = n; Chris@197: cacheInvalid(); Chris@197: emit layerParametersChanged(); Chris@197: } Chris@197: Chris@197: bool Chris@197: Colour3DPlotLayer::getNormalizeColumns() const Chris@197: { Chris@197: return m_normalizeColumns; Chris@197: } Chris@197: Chris@197: void Chris@719: Colour3DPlotLayer::setNormalizeHybrid(bool n) Chris@719: { Chris@719: if (m_normalizeHybrid == n) return; Chris@719: m_normalizeHybrid = n; Chris@719: cacheInvalid(); Chris@719: emit layerParametersChanged(); Chris@719: } Chris@719: Chris@719: bool Chris@719: Colour3DPlotLayer::getNormalizeHybrid() const Chris@719: { Chris@719: return m_normalizeHybrid; Chris@719: } Chris@719: Chris@719: void Chris@197: Colour3DPlotLayer::setNormalizeVisibleArea(bool n) Chris@197: { Chris@197: if (m_normalizeVisibleArea == n) return; Chris@197: m_normalizeVisibleArea = n; Chris@197: cacheInvalid(); Chris@197: emit layerParametersChanged(); Chris@197: } Chris@197: Chris@197: bool Chris@197: Colour3DPlotLayer::getNormalizeVisibleArea() const Chris@197: { Chris@197: return m_normalizeVisibleArea; Chris@197: } Chris@197: Chris@357: void Chris@357: Colour3DPlotLayer::setInvertVertical(bool n) Chris@357: { Chris@357: if (m_invertVertical == n) return; Chris@357: m_invertVertical = n; Chris@357: cacheInvalid(); Chris@357: emit layerParametersChanged(); Chris@357: } Chris@357: Chris@465: void Chris@465: Colour3DPlotLayer::setOpaque(bool n) Chris@465: { Chris@465: if (m_opaque == n) return; Chris@465: m_opaque = n; Chris@465: emit layerParametersChanged(); Chris@465: } Chris@465: Chris@535: void Chris@535: Colour3DPlotLayer::setSmooth(bool n) Chris@535: { Chris@535: if (m_smooth == n) return; Chris@535: m_smooth = n; Chris@535: emit layerParametersChanged(); Chris@535: } Chris@535: Chris@357: bool Chris@357: Colour3DPlotLayer::getInvertVertical() const Chris@357: { Chris@357: return m_invertVertical; Chris@357: } Chris@357: Chris@25: bool Chris@465: Colour3DPlotLayer::getOpaque() const Chris@465: { Chris@465: return m_opaque; Chris@465: } Chris@465: Chris@535: bool Chris@535: Colour3DPlotLayer::getSmooth() const Chris@535: { Chris@535: return m_smooth; Chris@535: } Chris@535: Chris@475: void Chris@475: Colour3DPlotLayer::setLayerDormant(const View *v, bool dormant) Chris@475: { Chris@475: if (dormant) { Chris@475: Chris@475: #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT Chris@812: cerr << "Colour3DPlotLayer::setLayerDormant(" << dormant << ")" Chris@585: << endl; Chris@475: #endif Chris@475: Chris@475: if (isLayerDormant(v)) { Chris@475: return; Chris@475: } Chris@475: Chris@475: Layer::setLayerDormant(v, true); Chris@475: Chris@475: cacheInvalid(); Chris@475: Chris@475: } else { Chris@475: Chris@475: Layer::setLayerDormant(v, false); Chris@475: } Chris@475: } Chris@475: Chris@465: bool Chris@44: Colour3DPlotLayer::isLayerScrollable(const View *v) const Chris@25: { Chris@812: if (m_normalizeVisibleArea) { Chris@812: return false; Chris@812: } Chris@812: if (shouldPaintDenseIn(v)) { Chris@812: return true; Chris@812: } Chris@25: QPoint discard; Chris@44: return !v->shouldIlluminateLocalFeatures(this, discard); Chris@25: } Chris@25: Chris@444: bool Chris@444: Colour3DPlotLayer::getValueExtents(float &min, float &max, Chris@444: bool &logarithmic, QString &unit) const Chris@444: { Chris@444: if (!m_model) return false; Chris@444: Chris@444: min = 0; Chris@444: max = m_model->getHeight(); Chris@444: Chris@444: logarithmic = false; Chris@444: unit = ""; Chris@444: Chris@444: return true; Chris@444: } Chris@444: Chris@444: bool Chris@444: Colour3DPlotLayer::getDisplayExtents(float &min, float &max) const Chris@444: { Chris@444: if (!m_model) return false; Chris@444: Chris@444: min = m_miny; Chris@444: max = m_maxy; Chris@444: if (max <= min) { Chris@444: min = 0; Chris@444: max = m_model->getHeight(); Chris@444: } Chris@444: if (min < 0) min = 0; Chris@444: if (max > m_model->getHeight()) max = m_model->getHeight(); Chris@444: Chris@444: return true; Chris@444: } Chris@444: Chris@444: bool Chris@444: Colour3DPlotLayer::setDisplayExtents(float min, float max) Chris@444: { Chris@444: if (!m_model) return false; Chris@444: Chris@444: m_miny = lrintf(min); Chris@444: m_maxy = lrintf(max); Chris@444: Chris@444: emit layerParametersChanged(); Chris@444: return true; Chris@444: } Chris@444: Chris@725: bool Chris@805: Colour3DPlotLayer::getYScaleValue(const View *, int, Chris@805: float &, QString &) const Chris@725: { Chris@725: return false;//!!! Chris@725: } Chris@725: Chris@444: int Chris@444: Colour3DPlotLayer::getVerticalZoomSteps(int &defaultStep) const Chris@444: { Chris@444: if (!m_model) return 0; Chris@444: Chris@444: defaultStep = 0; Chris@444: int h = m_model->getHeight(); Chris@444: return h; Chris@444: } Chris@444: Chris@444: int Chris@444: Colour3DPlotLayer::getCurrentVerticalZoomStep() const Chris@444: { Chris@444: if (!m_model) return 0; Chris@444: Chris@444: float min, max; Chris@444: getDisplayExtents(min, max); Chris@444: return m_model->getHeight() - lrintf(max - min); Chris@444: } Chris@444: Chris@444: void Chris@444: Colour3DPlotLayer::setVerticalZoomStep(int step) Chris@444: { Chris@444: if (!m_model) return; Chris@444: Chris@587: // SVDEBUG << "Colour3DPlotLayer::setVerticalZoomStep(" <getHeight(); Chris@532: getDisplayExtents(mn, mx); Chris@532: float h = v->height(); Chris@532: if (m_binScale == LinearBinScale) { Chris@532: y = h - (((bin - mn) * h) / (mx - mn)); Chris@532: } else { Chris@532: float logmin = mn + 1, logmax = mx + 1; Chris@532: LogRange::mapRange(logmin, logmax); Chris@532: y = h - (((LogRange::map(bin + 1) - logmin) * h) / (logmax - logmin)); Chris@532: } Chris@532: return y; Chris@532: } Chris@532: Chris@532: float Chris@532: Colour3DPlotLayer::getBinForY(View *v, float y) const Chris@532: { Chris@532: float bin = y; Chris@532: if (!m_model) return bin; Chris@532: float mn = 0, mx = m_model->getHeight(); Chris@532: getDisplayExtents(mn, mx); Chris@532: float h = v->height(); Chris@532: if (m_binScale == LinearBinScale) { Chris@532: bin = mn + ((h - y) * (mx - mn)) / h; Chris@532: } else { Chris@532: float logmin = mn + 1, logmax = mx + 1; Chris@532: LogRange::mapRange(logmin, logmax); Chris@532: bin = LogRange::unmap(logmin + ((h - y) * (logmax - logmin)) / h) - 1; Chris@532: } Chris@532: return bin; Chris@532: } Chris@532: Chris@25: QString Chris@44: Colour3DPlotLayer::getFeatureDescription(View *v, QPoint &pos) const Chris@25: { Chris@25: if (!m_model) return ""; Chris@25: Chris@25: int x = pos.x(); Chris@25: int y = pos.y(); Chris@25: Chris@805: int modelStart = m_model->getStartFrame(); Chris@805: int modelResolution = m_model->getResolution(); Chris@25: Chris@159: float srRatio = Chris@159: float(v->getViewManager()->getMainModelSampleRate()) / Chris@159: float(m_model->getSampleRate()); Chris@159: Chris@812: int sx0 = int((v->getFrameForX(x) / srRatio - modelStart) / Chris@812: modelResolution); Chris@25: Chris@160: int f0 = sx0 * modelResolution; Chris@160: int f1 = f0 + modelResolution; Chris@160: Chris@447: int sh = m_model->getHeight(); Chris@447: Chris@447: int symin = m_miny; Chris@447: int symax = m_maxy; Chris@447: if (symax <= symin) { Chris@447: symin = 0; Chris@447: symax = sh; Chris@447: } Chris@447: if (symin < 0) symin = 0; Chris@447: if (symax > sh) symax = sh; Chris@447: Chris@534: // float binHeight = float(v->height()) / (symax - symin); Chris@534: // int sy = int((v->height() - y) / binHeight) + symin; Chris@534: Chris@534: int sy = getBinForY(v, y); Chris@25: Chris@812: if (sy < 0 || sy >= m_model->getHeight()) { Chris@812: return ""; Chris@812: } Chris@812: Chris@357: if (m_invertVertical) sy = m_model->getHeight() - sy - 1; Chris@357: Chris@160: float value = m_model->getValueAt(sx0, sy); Chris@159: Chris@682: // cerr << "bin value (" << sx0 << "," << sy << ") is " << value << endl; Chris@25: Chris@25: QString binName = m_model->getBinName(sy); Chris@25: if (binName == "") binName = QString("[%1]").arg(sy + 1); Chris@25: else binName = QString("%1 [%2]").arg(binName).arg(sy + 1); Chris@25: Chris@25: QString text = tr("Time:\t%1 - %2\nBin:\t%3\nValue:\t%4") Chris@160: .arg(RealTime::frame2RealTime(f0, m_model->getSampleRate()) Chris@25: .toText(true).c_str()) Chris@160: .arg(RealTime::frame2RealTime(f1, m_model->getSampleRate()) Chris@25: .toText(true).c_str()) Chris@25: .arg(binName) Chris@25: .arg(value); Chris@25: Chris@25: return text; Chris@25: } Chris@25: Chris@25: int Chris@248: Colour3DPlotLayer::getColourScaleWidth(QPainter &) const Chris@159: { Chris@159: int cw = 20; Chris@159: return cw; Chris@159: } Chris@159: Chris@159: int Chris@607: Colour3DPlotLayer::getVerticalScaleWidth(View *, bool, QPainter &paint) const Chris@25: { Chris@25: if (!m_model) return 0; Chris@25: Chris@160: QString sampleText = QString("[%1]").arg(m_model->getHeight()); Chris@25: int tw = paint.fontMetrics().width(sampleText); Chris@98: bool another = false; Chris@25: Chris@805: for (int i = 0; i < m_model->getHeight(); ++i) { Chris@25: if (m_model->getBinName(i).length() > sampleText.length()) { Chris@25: sampleText = m_model->getBinName(i); Chris@98: another = true; Chris@25: } Chris@25: } Chris@98: if (another) { Chris@25: tw = std::max(tw, paint.fontMetrics().width(sampleText)); Chris@25: } Chris@25: Chris@159: return tw + 13 + getColourScaleWidth(paint); Chris@25: } Chris@25: Chris@25: void Chris@607: Colour3DPlotLayer::paintVerticalScale(View *v, bool, QPainter &paint, QRect rect) const Chris@25: { Chris@25: if (!m_model) return; Chris@25: Chris@25: int h = rect.height(), w = rect.width(); Chris@25: Chris@159: int cw = getColourScaleWidth(paint); Chris@159: Chris@159: int ch = h - 20; Chris@159: if (ch > 20 && m_cache) { Chris@159: Chris@447: float min = m_model->getMinimumLevel(); Chris@447: float max = m_model->getMaximumLevel(); Chris@447: Chris@447: float mmin = min; Chris@447: float mmax = max; Chris@447: Chris@447: if (m_colourScale == LogScale) { Chris@447: LogRange::mapRange(mmin, mmax); Chris@447: } else if (m_colourScale == PlusMinusOneScale) { Chris@447: mmin = -1.f; Chris@447: mmax = 1.f; Chris@509: } else if (m_colourScale == AbsoluteScale) { Chris@509: if (mmin < 0) { Chris@509: if (fabsf(mmin) > fabsf(mmax)) mmax = fabsf(mmin); Chris@509: else mmax = fabsf(mmax); Chris@509: mmin = 0; Chris@509: } else { Chris@509: mmin = fabsf(mmin); Chris@509: mmax = fabsf(mmax); Chris@509: } Chris@447: } Chris@447: Chris@447: if (max == min) max = min + 1.0; Chris@465: if (mmax == mmin) mmax = mmin + 1.0; Chris@447: Chris@287: paint.setPen(v->getForeground()); Chris@447: paint.drawRect(4, 10, cw - 8, ch+1); Chris@159: Chris@446: for (int y = 0; y < ch; ++y) { Chris@447: float value = ((max - min) * (ch - y - 1)) / ch + min; Chris@447: if (m_colourScale == LogScale) { Chris@447: value = LogRange::map(value); Chris@447: } Chris@447: int pixel = int(((value - mmin) * 256) / (mmax - mmin)); Chris@465: if (pixel >= 0 && pixel < 256) { Chris@465: QRgb c = m_cache->color(pixel); Chris@465: paint.setPen(QColor(qRed(c), qGreen(c), qBlue(c))); Chris@465: paint.drawLine(5, 11 + y, cw - 5, 11 + y); Chris@465: } else { Chris@682: cerr << "WARNING: Colour3DPlotLayer::paintVerticalScale: value " << value << ", mmin " << mmin << ", mmax " << mmax << " leads to invalid pixel " << pixel << endl; Chris@465: } Chris@159: } Chris@446: Chris@446: QString minstr = QString("%1").arg(min); Chris@446: QString maxstr = QString("%1").arg(max); Chris@446: Chris@446: paint.save(); Chris@446: Chris@446: QFont font = paint.font(); Chris@446: font.setPixelSize(10); Chris@446: paint.setFont(font); Chris@446: Chris@446: int msw = paint.fontMetrics().width(maxstr); Chris@446: Chris@446: QMatrix m; Chris@446: m.translate(cw - 6, ch + 10); Chris@446: m.rotate(-90); Chris@446: Chris@446: paint.setWorldMatrix(m); Chris@446: Chris@446: v->drawVisibleText(paint, 2, 0, minstr, View::OutlinedText); Chris@446: Chris@446: m.translate(ch - msw - 2, 0); Chris@446: paint.setWorldMatrix(m); Chris@446: Chris@446: v->drawVisibleText(paint, 0, 0, maxstr, View::OutlinedText); Chris@446: Chris@446: paint.restore(); Chris@159: } Chris@159: Chris@287: paint.setPen(v->getForeground()); Chris@159: Chris@445: int sh = m_model->getHeight(); Chris@445: Chris@445: int symin = m_miny; Chris@445: int symax = m_maxy; Chris@445: if (symax <= symin) { Chris@445: symin = 0; Chris@445: symax = sh; Chris@445: } Chris@445: if (symin < 0) symin = 0; Chris@445: if (symax > sh) symax = sh; Chris@445: Chris@532: paint.save(); Chris@456: Chris@533: int py = h; Chris@25: Chris@805: for (int i = symin; i <= symax; ++i) { Chris@98: Chris@532: int y0; Chris@534: Chris@532: y0 = lrintf(getYForBin(v, i)); Chris@532: int h = py - y0; Chris@532: Chris@532: if (i > symin) { Chris@532: if (paint.fontMetrics().height() >= h) { Chris@533: if (h >= 8) { Chris@532: QFont tf = paint.font(); Chris@533: tf.setPixelSize(h-2); Chris@532: paint.setFont(tf); Chris@532: } else { Chris@532: continue; Chris@532: } Chris@532: } Chris@532: } Chris@25: Chris@532: py = y0; Chris@532: Chris@534: if (i < symax) { Chris@534: paint.drawLine(cw, y0, w, y0); Chris@534: } Chris@25: Chris@532: if (i > symin) { Chris@534: Chris@805: int idx = i - 1; Chris@534: if (m_invertVertical) idx = m_model->getHeight() - idx - 1; Chris@534: Chris@534: QString text = m_model->getBinName(idx); Chris@534: if (text == "") text = QString("[%1]").arg(idx + 1); Chris@534: Chris@534: int ty = y0 + (h/2) - (paint.fontMetrics().height()/2) + Chris@534: paint.fontMetrics().ascent() + 1; Chris@534: Chris@534: paint.drawText(cw + 5, ty, text); Chris@457: } Chris@25: } Chris@456: Chris@456: paint.restore(); Chris@25: } Chris@25: Chris@467: DenseThreeDimensionalModel::Column Chris@805: Colour3DPlotLayer::getColumn(int col) const Chris@197: { Chris@812: Profiler profiler("Colour3DPlotLayer::getColumn"); Chris@812: Chris@467: DenseThreeDimensionalModel::Column values = m_model->getColumn(col); Chris@468: while (values.size() < m_model->getHeight()) values.push_back(0.f); Chris@719: if (!m_normalizeColumns && !m_normalizeHybrid) return values; Chris@461: Chris@224: float colMax = 0.f, colMin = 0.f; Chris@461: float min = 0.f, max = 0.f; Chris@197: Chris@461: min = m_model->getMinimumLevel(); Chris@461: max = m_model->getMaximumLevel(); Chris@461: Chris@805: for (int y = 0; y < values.size(); ++y) { Chris@467: if (y == 0 || values.at(y) > colMax) colMax = values.at(y); Chris@467: if (y == 0 || values.at(y) < colMin) colMin = values.at(y); Chris@197: } Chris@461: if (colMin == colMax) colMax = colMin + 1; Chris@197: Chris@805: for (int y = 0; y < values.size(); ++y) { Chris@461: Chris@467: float value = values.at(y); Chris@461: float norm = (value - colMin) / (colMax - colMin); Chris@467: float newvalue = min + (max - min) * norm; Chris@197: Chris@467: if (value != newvalue) values[y] = newvalue; Chris@468: } Chris@468: Chris@719: if (m_normalizeHybrid && (colMax > 0.0)) { Chris@719: float logmax = log10(colMax); Chris@805: for (int y = 0; y < values.size(); ++y) { Chris@719: values[y] *= logmax; Chris@719: } Chris@719: } Chris@719: Chris@468: return values; Chris@197: } Chris@197: Chris@197: void Chris@805: Colour3DPlotLayer::fillCache(int firstBin, int lastBin) const Chris@197: { Chris@812: Profiler profiler("Colour3DPlotLayer::fillCache", true); Chris@476: Chris@805: int modelStart = m_model->getStartFrame(); Chris@805: int modelEnd = m_model->getEndFrame(); Chris@805: int modelResolution = m_model->getResolution(); Chris@197: Chris@805: int modelStartBin = modelStart / modelResolution; Chris@805: int modelEndBin = modelEnd / modelResolution; Chris@461: Chris@812: #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT Chris@812: cerr << "Colour3DPlotLayer::fillCache: range " << firstBin << " -> " << lastBin << " of model range " << modelStartBin << " -> " << modelEndBin << " (model resolution " << modelResolution << ")" << endl; Chris@812: #endif Chris@812: Chris@805: int cacheWidth = modelEndBin - modelStartBin + 1; Chris@471: if (lastBin > modelEndBin) cacheWidth = lastBin - modelStartBin + 1; Chris@805: int cacheHeight = m_model->getHeight(); Chris@461: Chris@812: if (m_cache && m_cache->height() != cacheHeight) { Chris@742: // height has changed: delete everything rather than resizing Chris@812: #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT Chris@812: cerr << "Colour3DPlotLayer::fillCache: Cache height has changed, recreating" << endl; Chris@812: #endif Chris@461: delete m_cache; Chris@469: delete m_peaksCache; Chris@461: m_cache = 0; Chris@469: m_peaksCache = 0; Chris@461: } Chris@461: Chris@812: if (m_cache && m_cache->width() != cacheWidth) { Chris@742: // width has changed and we have an existing cache: resize it Chris@812: #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT Chris@812: cerr << "Colour3DPlotLayer::fillCache: Cache width has changed, resizing existing cache" << endl; Chris@812: #endif Chris@469: QImage *newCache = Chris@469: new QImage(m_cache->copy(0, 0, cacheWidth, cacheHeight)); Chris@461: delete m_cache; Chris@461: m_cache = newCache; Chris@469: if (m_peaksCache) { Chris@469: QImage *newPeaksCache = Chris@469: new QImage(m_peaksCache->copy Chris@742: (0, 0, cacheWidth / m_peakResolution + 1, cacheHeight)); Chris@469: delete m_peaksCache; Chris@469: m_peaksCache = newPeaksCache; Chris@469: } Chris@197: } Chris@197: Chris@461: if (!m_cache) { Chris@812: #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT Chris@812: cerr << "Colour3DPlotLayer::fillCache: Have no cache, making one" << endl; Chris@812: #endif Chris@469: m_cache = new QImage Chris@469: (cacheWidth, cacheHeight, QImage::Format_Indexed8); Chris@812: m_cache->setColorCount(256); Chris@514: m_cache->fill(0); Chris@474: if (!m_normalizeVisibleArea) { Chris@469: m_peaksCache = new QImage Chris@469: (cacheWidth / m_peakResolution + 1, cacheHeight, Chris@469: QImage::Format_Indexed8); Chris@812: m_peaksCache->setColorCount(256); Chris@514: m_peaksCache->fill(0); Chris@469: } else if (m_peaksCache) { Chris@469: delete m_peaksCache; Chris@469: m_peaksCache = 0; Chris@469: } Chris@461: m_cacheValidStart = 0; Chris@461: m_cacheValidEnd = 0; Chris@197: } Chris@197: Chris@812: #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT Chris@812: cerr << "cache size = " << m_cache->width() << "x" << m_cache->height() Chris@812: << " peaks cache size = " << m_peaksCache->width() << "x" << m_peaksCache->height() << endl; Chris@812: #endif Chris@742: Chris@461: if (m_cacheValidStart <= firstBin && m_cacheValidEnd >= lastBin) { Chris@519: #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT Chris@682: cerr << "Cache is valid in this region already" << endl; Chris@519: #endif Chris@461: return; Chris@461: } Chris@461: Chris@805: int fillStart = firstBin; Chris@805: int fillEnd = lastBin; Chris@197: Chris@461: if (fillStart < modelStartBin) fillStart = modelStartBin; Chris@461: if (fillStart > modelEndBin) fillStart = modelEndBin; Chris@461: if (fillEnd < modelStartBin) fillEnd = modelStartBin; Chris@461: if (fillEnd > modelEndBin) fillEnd = modelEndBin; Chris@197: Chris@461: bool normalizeVisible = (m_normalizeVisibleArea && !m_normalizeColumns); Chris@197: Chris@461: if (!normalizeVisible && (m_cacheValidStart < m_cacheValidEnd)) { Chris@461: Chris@461: if (m_cacheValidEnd < fillStart) { Chris@461: fillStart = m_cacheValidEnd + 1; Chris@461: } Chris@461: if (m_cacheValidStart > fillEnd) { Chris@461: fillEnd = m_cacheValidStart - 1; Chris@461: } Chris@461: Chris@461: m_cacheValidStart = std::min(fillStart, m_cacheValidStart); Chris@461: m_cacheValidEnd = std::max(fillEnd, m_cacheValidEnd); Chris@461: Chris@461: } else { Chris@461: Chris@812: // when normalising the visible area, the only valid area, Chris@812: // ever, is the currently visible one Chris@461: Chris@461: m_cacheValidStart = fillStart; Chris@461: m_cacheValidEnd = fillEnd; Chris@461: } Chris@461: Chris@519: #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT Chris@812: cerr << "Cache size " << cacheWidth << "x" << cacheHeight << " will be valid from " << m_cacheValidStart << " to " << m_cacheValidEnd << " (fillStart = " << fillStart << ", fillEnd = " << fillEnd << ")" << endl; Chris@519: #endif Chris@461: Chris@197: DenseThreeDimensionalModel::Column values; Chris@197: Chris@197: float min = m_model->getMinimumLevel(); Chris@197: float max = m_model->getMaximumLevel(); Chris@197: Chris@197: if (m_colourScale == LogScale) { Chris@197: LogRange::mapRange(min, max); Chris@197: } else if (m_colourScale == PlusMinusOneScale) { Chris@197: min = -1.f; Chris@197: max = 1.f; Chris@509: } else if (m_colourScale == AbsoluteScale) { Chris@509: if (min < 0) { Chris@509: if (fabsf(min) > fabsf(max)) max = fabsf(min); Chris@509: else max = fabsf(max); Chris@509: min = 0; Chris@509: } else { Chris@509: min = fabsf(min); Chris@509: max = fabsf(max); Chris@509: } Chris@197: } Chris@197: Chris@197: if (max == min) max = min + 1.0; Chris@197: Chris@197: ColourMapper mapper(m_colourMap, 0.f, 255.f); Chris@197: Chris@197: for (int index = 0; index < 256; ++index) { Chris@197: QColor colour = mapper.map(index); Chris@469: m_cache->setColor Chris@469: (index, qRgb(colour.red(), colour.green(), colour.blue())); Chris@469: if (m_peaksCache) { Chris@469: m_peaksCache->setColor Chris@469: (index, qRgb(colour.red(), colour.green(), colour.blue())); Chris@469: } Chris@197: } Chris@197: Chris@224: float visibleMax = 0.f, visibleMin = 0.f; Chris@197: Chris@461: if (normalizeVisible) { Chris@197: Chris@805: for (int c = fillStart; c <= fillEnd; ++c) { Chris@197: Chris@467: values = getColumn(c); Chris@197: Chris@224: float colMax = 0.f, colMin = 0.f; Chris@197: Chris@805: for (int y = 0; y < cacheHeight; ++y) { Chris@197: if (y >= values.size()) break; Chris@197: if (y == 0 || values[y] > colMax) colMax = values[y]; Chris@224: if (y == 0 || values[y] < colMin) colMin = values[y]; Chris@197: } Chris@197: Chris@461: if (c == fillStart || colMax > visibleMax) visibleMax = colMax; Chris@461: if (c == fillStart || colMin < visibleMin) visibleMin = colMin; Chris@461: } Chris@461: Chris@461: if (m_colourScale == LogScale) { Chris@461: visibleMin = LogRange::map(visibleMin); Chris@461: visibleMax = LogRange::map(visibleMax); Chris@461: if (visibleMin > visibleMax) std::swap(visibleMin, visibleMax); Chris@509: } else if (m_colourScale == AbsoluteScale) { Chris@509: if (visibleMin < 0) { Chris@509: if (fabsf(visibleMin) > fabsf(visibleMax)) visibleMax = fabsf(visibleMin); Chris@509: else visibleMax = fabsf(visibleMax); Chris@509: visibleMin = 0; Chris@509: } else { Chris@509: visibleMin = fabsf(visibleMin); Chris@509: visibleMax = fabsf(visibleMax); Chris@509: } Chris@197: } Chris@197: } Chris@197: Chris@224: if (visibleMin == visibleMax) visibleMax = visibleMin + 1; Chris@224: Chris@469: int *peaks = 0; Chris@469: if (m_peaksCache) { Chris@469: peaks = new int[cacheHeight]; Chris@469: for (int y = 0; y < cacheHeight; ++y) { Chris@469: peaks[y] = 0; Chris@469: } Chris@469: } Chris@469: Chris@812: Profiler profiler2("Colour3DPlotLayer::fillCache: filling", true); Chris@812: Chris@805: for (int c = fillStart; c <= fillEnd; ++c) { Chris@197: Chris@467: values = getColumn(c); Chris@197: Chris@742: if (c >= m_cache->width()) { Chris@742: cerr << "ERROR: column " << c << " >= cache width " Chris@742: << m_cache->width() << endl; Chris@742: continue; Chris@742: } Chris@742: Chris@805: for (int y = 0; y < cacheHeight; ++y) { Chris@197: Chris@197: float value = min; Chris@197: if (y < values.size()) { Chris@469: value = values.at(y); Chris@197: } Chris@224: Chris@534: value = value * m_gain; Chris@534: Chris@224: if (m_colourScale == LogScale) { Chris@224: value = LogRange::map(value); Chris@509: } else if (m_colourScale == AbsoluteScale) { Chris@509: value = fabsf(value); Chris@197: } Chris@461: Chris@461: if (normalizeVisible) { Chris@461: float norm = (value - visibleMin) / (visibleMax - visibleMin); Chris@461: value = min + (max - min) * norm; Chris@461: } Chris@197: Chris@197: int pixel = int(((value - min) * 256) / (max - min)); Chris@197: if (pixel < 0) pixel = 0; Chris@197: if (pixel > 255) pixel = 255; Chris@469: if (peaks && (pixel > peaks[y])) peaks[y] = pixel; Chris@197: Chris@357: if (m_invertVertical) { Chris@461: m_cache->setPixel(c, cacheHeight - y - 1, pixel); Chris@357: } else { Chris@742: if (y >= m_cache->height()) { Chris@742: cerr << "ERROR: row " << y << " >= cache height " << m_cache->height() << endl; Chris@742: } else { Chris@742: m_cache->setPixel(c, y, pixel); Chris@742: } Chris@357: } Chris@197: } Chris@469: Chris@469: if (peaks) { Chris@805: int notch = (c % m_peakResolution); Chris@469: if (notch == m_peakResolution-1 || c == fillEnd) { Chris@805: int pc = c / m_peakResolution; Chris@742: if (pc >= m_peaksCache->width()) { Chris@742: cerr << "ERROR: peak column " << pc Chris@742: << " (from col " << c << ") >= peaks cache width " Chris@742: << m_peaksCache->width() << endl; Chris@742: continue; Chris@742: } Chris@805: for (int y = 0; y < cacheHeight; ++y) { Chris@469: if (m_invertVertical) { Chris@469: m_peaksCache->setPixel(pc, cacheHeight - y - 1, peaks[y]); Chris@469: } else { Chris@742: if (y >= m_peaksCache->height()) { Chris@742: cerr << "ERROR: row " << y Chris@742: << " >= peaks cache height " Chris@742: << m_peaksCache->height() << endl; Chris@742: } else { Chris@742: m_peaksCache->setPixel(pc, y, peaks[y]); Chris@742: } Chris@469: } Chris@469: } Chris@469: for (int y = 0; y < cacheHeight; ++y) { Chris@469: peaks[y] = 0; Chris@469: } Chris@469: } Chris@469: } Chris@197: } Chris@469: Chris@469: delete[] peaks; Chris@197: } Chris@197: Chris@812: bool Chris@812: Colour3DPlotLayer::shouldPaintDenseIn(const View *v) const Chris@812: { Chris@812: if (!m_model || !v || !(v->getViewManager())) { Chris@812: return false; Chris@812: } Chris@812: float srRatio = Chris@812: float(v->getViewManager()->getMainModelSampleRate()) / Chris@812: float(m_model->getSampleRate()); Chris@812: if (m_opaque || Chris@812: m_smooth || Chris@812: m_model->getHeight() >= v->height() || Chris@812: ((m_model->getResolution() * srRatio) / v->getZoomLevel()) < 2) { Chris@812: return true; Chris@812: } Chris@812: return false; Chris@812: } Chris@812: Chris@197: void Chris@44: Colour3DPlotLayer::paint(View *v, QPainter &paint, QRect rect) const Chris@0: { Chris@514: /* Chris@443: if (m_model) { Chris@587: SVDEBUG << "Colour3DPlotLayer::paint: model says shouldUseLogValueScale = " << m_model->shouldUseLogValueScale() << endl; Chris@443: } Chris@514: */ Chris@466: Profiler profiler("Colour3DPlotLayer::paint"); Chris@125: #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT Chris@812: 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: #endif Chris@0: Chris@0: int completion = 0; Chris@0: if (!m_model || !m_model->isOK() || !m_model->isReady(&completion)) { Chris@0: if (completion > 0) { Chris@44: paint.fillRect(0, 10, v->width() * completion / 100, Chris@0: 10, QColor(120, 120, 120)); Chris@0: } Chris@0: return; Chris@0: } Chris@0: Chris@197: if (m_normalizeVisibleArea && !m_normalizeColumns) rect = v->rect(); Chris@197: Chris@805: int modelStart = m_model->getStartFrame(); Chris@805: int modelEnd = m_model->getEndFrame(); Chris@805: int modelResolution = m_model->getResolution(); Chris@0: Chris@197: // The cache is from the model's start frame to the model's end Chris@197: // frame at the model's window increment frames per pixel. We Chris@197: // want to draw from our start frame + x0 * zoomLevel to our start Chris@197: // frame + x1 * zoomLevel at zoomLevel frames per pixel. Chris@12: Chris@197: // We have quite different paint mechanisms for rendering "large" Chris@197: // bins (more than one bin per pixel in both directions) and Chris@197: // "small". This is "large"; see paintDense below for "small". Chris@12: Chris@197: int x0 = rect.left(); Chris@197: int x1 = rect.right() + 1; Chris@12: Chris@197: int h = v->height(); Chris@0: Chris@197: float srRatio = Chris@197: float(v->getViewManager()->getMainModelSampleRate()) / Chris@197: float(m_model->getSampleRate()); Chris@0: Chris@812: int sx0 = int((v->getFrameForX(x0) / srRatio - modelStart) Chris@812: / modelResolution); Chris@812: int sx1 = int((v->getFrameForX(x1) / srRatio - modelStart) Chris@812: / modelResolution); Chris@197: int sh = m_model->getHeight(); Chris@159: Chris@444: int symin = m_miny; Chris@444: int symax = m_maxy; Chris@444: if (symax <= symin) { Chris@444: symin = 0; Chris@444: symax = sh; Chris@444: } Chris@444: if (symin < 0) symin = 0; Chris@444: if (symax > sh) symax = sh; Chris@444: Chris@197: if (sx0 > 0) --sx0; Chris@197: fillCache(sx0 < 0 ? 0 : sx0, Chris@197: sx1 < 0 ? 0 : sx1); Chris@0: Chris@351: #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT Chris@812: 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: #endif Chris@351: Chris@812: if (shouldPaintDenseIn(v)) { Chris@347: #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT Chris@812: cerr << "calling paintDense" << endl; Chris@347: #endif Chris@98: paintDense(v, paint, rect); Chris@98: return; Chris@98: } Chris@98: Chris@125: #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT Chris@812: cerr << "Colour3DPlotLayer::paint: w " << x1-x0 << ", h " << h << ", sx0 " << sx0 << ", sx1 " << sx1 << ", sw " << sx1-sx0 << ", sh " << sh << endl; Chris@682: cerr << "Colour3DPlotLayer: sample rate is " << m_model->getSampleRate() << ", resolution " << m_model->getResolution() << endl; Chris@125: #endif Chris@0: Chris@25: QPoint illuminatePos; Chris@44: bool illuminate = v->shouldIlluminateLocalFeatures(this, illuminatePos); Chris@54: char labelbuf[10]; Chris@25: Chris@197: for (int sx = sx0; sx <= sx1; ++sx) { Chris@0: Chris@812: int fx = sx * modelResolution; Chris@0: Chris@812: if (fx + modelResolution <= modelStart || fx > modelEnd) continue; Chris@0: Chris@812: int rx0 = v->getXForFrame(int((fx + modelStart) * srRatio)); Chris@812: int rx1 = v->getXForFrame(int((fx + modelStart + modelResolution + 1) * srRatio)); Chris@54: Chris@159: int rw = rx1 - rx0; Chris@159: if (rw < 1) rw = 1; Chris@54: Chris@159: bool showLabel = (rw > 10 && Chris@159: paint.fontMetrics().width("0.000000") < rw - 3 && Chris@54: paint.fontMetrics().height() < (h / sh)); Chris@98: Chris@444: for (int sy = symin; sy < symax; ++sy) { Chris@0: Chris@534: int ry0 = getYForBin(v, sy); Chris@534: int ry1 = getYForBin(v, sy + 1); Chris@534: QRect r(rx0, ry1, rw, ry0 - ry1); Chris@534: Chris@0: QRgb pixel = qRgb(255, 255, 255); Chris@461: if (sx >= 0 && sx < m_cache->width() && Chris@0: sy >= 0 && sy < m_cache->height()) { Chris@461: pixel = m_cache->pixel(sx, sy); Chris@0: } Chris@0: Chris@159: if (rw == 1) { Chris@159: paint.setPen(pixel); Chris@159: paint.setBrush(Qt::NoBrush); Chris@159: paint.drawLine(r.x(), r.y(), r.x(), r.y() + r.height() - 1); Chris@159: continue; Chris@159: } Chris@159: Chris@0: QColor pen(255, 255, 255, 80); Chris@0: QColor brush(pixel); Chris@159: Chris@159: if (rw > 3 && r.height() > 3) { Chris@159: brush.setAlpha(160); Chris@159: } Chris@159: Chris@0: paint.setPen(Qt::NoPen); Chris@0: paint.setBrush(brush); Chris@0: Chris@25: if (illuminate) { Chris@25: if (r.contains(illuminatePos)) { Chris@287: paint.setPen(v->getForeground()); Chris@25: } Chris@25: } Chris@76: Chris@125: #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT Chris@682: // cerr << "rect " << r.x() << "," << r.y() << " " Chris@682: // << r.width() << "x" << r.height() << endl; Chris@125: #endif Chris@25: Chris@25: paint.drawRect(r); Chris@0: Chris@54: if (showLabel) { Chris@461: if (sx >= 0 && sx < m_cache->width() && Chris@54: sy >= 0 && sy < m_cache->height()) { Chris@461: float value = m_model->getValueAt(sx, sy); Chris@54: sprintf(labelbuf, "%06f", value); Chris@54: QString text(labelbuf); Chris@287: paint.setPen(v->getBackground()); Chris@54: paint.drawText(rx0 + 2, Chris@54: ry0 - h / sh - 1 + 2 + paint.fontMetrics().ascent(), Chris@54: text); Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@98: } Chris@0: Chris@98: void Chris@98: Colour3DPlotLayer::paintDense(View *v, QPainter &paint, QRect rect) const Chris@98: { Chris@812: Profiler profiler("Colour3DPlotLayer::paintDense", true); Chris@463: if (!m_cache) return; Chris@463: Chris@466: float modelStart = m_model->getStartFrame(); Chris@466: float modelResolution = m_model->getResolution(); Chris@0: Chris@469: int mmsr = v->getViewManager()->getMainModelSampleRate(); Chris@469: int msr = m_model->getSampleRate(); Chris@469: float srRatio = float(mmsr) / float(msr); Chris@159: Chris@98: int x0 = rect.left(); Chris@98: int x1 = rect.right() + 1; Chris@0: Chris@470: const int w = x1 - x0; // const so it can be used as array size below Chris@471: int h = v->height(); // we always paint full height Chris@160: int sh = m_model->getHeight(); Chris@98: Chris@444: int symin = m_miny; Chris@444: int symax = m_maxy; Chris@444: if (symax <= symin) { Chris@444: symin = 0; Chris@444: symax = sh; Chris@444: } Chris@444: if (symin < 0) symin = 0; Chris@444: if (symax > sh) symax = sh; Chris@444: Chris@466: QImage img(w, h, QImage::Format_Indexed8); Chris@466: img.setColorTable(m_cache->colorTable()); Chris@98: Chris@466: uchar *peaks = new uchar[w]; Chris@466: memset(peaks, 0, w); Chris@98: Chris@469: int zoomLevel = v->getZoomLevel(); Chris@469: Chris@469: QImage *source = m_cache; Chris@812: Chris@812: #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT Chris@812: cerr << "modelResolution " << modelResolution << ", srRatio " Chris@812: << srRatio << ", m_peakResolution " << m_peakResolution Chris@812: << ", zoomLevel " << zoomLevel << ", result " Chris@812: << ((modelResolution * srRatio * m_peakResolution) / zoomLevel) Chris@812: << endl; Chris@812: #endif Chris@474: Chris@474: if (m_peaksCache) { Chris@474: if (((modelResolution * srRatio * m_peakResolution) / zoomLevel) < 1) { Chris@812: #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT Chris@812: cerr << "using peaks cache" << endl; Chris@812: #endif Chris@474: source = m_peaksCache; Chris@474: modelResolution *= m_peakResolution; Chris@474: } else { Chris@812: #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT Chris@812: cerr << "not using peaks cache" << endl; Chris@812: #endif Chris@474: } Chris@469: } else { Chris@812: #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT Chris@812: cerr << "have no peaks cache" << endl; Chris@812: #endif Chris@469: } Chris@469: Chris@469: int sw = source->width(); Chris@470: Chris@812: int xf = -1; Chris@812: int nxf = v->getFrameForX(x0); Chris@470: Chris@535: float epsilon = 0.000001; Chris@535: Chris@545: #ifdef __GNUC__ Chris@535: float sxa[w * 2]; Chris@545: #else Chris@545: float *sxa = (float *)alloca(w * 2 * sizeof(float)); Chris@545: #endif Chris@470: for (int x = 0; x < w; ++x) { Chris@470: Chris@470: xf = nxf; Chris@470: nxf = xf + zoomLevel; Chris@470: Chris@470: float sx0 = (float(xf) / srRatio - modelStart) / modelResolution; Chris@470: float sx1 = (float(nxf) / srRatio - modelStart) / modelResolution; Chris@470: Chris@535: sxa[x*2] = sx0; Chris@535: sxa[x*2 + 1] = sx1; Chris@470: } Chris@466: Chris@532: float logmin = symin+1, logmax = symax+1; Chris@532: LogRange::mapRange(logmin, logmax); Chris@532: Chris@812: #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT Chris@812: cerr << "m_smooth = " << m_smooth << ", w = " << w << ", h = " << h << endl; Chris@812: #endif Chris@812: Chris@535: if (m_smooth) { Chris@535: Chris@535: for (int y = 0; y < h; ++y) { Chris@466: Chris@535: float sy = getBinForY(v, y) - 0.5; Chris@535: int syi = int(sy + epsilon); Chris@535: if (syi < 0 || syi >= source->height()) continue; Chris@531: Chris@535: uchar *targetLine = img.scanLine(y); Chris@535: uchar *sourceLine = source->scanLine(syi); Chris@535: uchar *nextSource; Chris@535: if (syi + 1 < source->height()) { Chris@535: nextSource = source->scanLine(syi + 1); Chris@535: } else { Chris@535: nextSource = sourceLine; Chris@535: } Chris@466: Chris@466: for (int x = 0; x < w; ++x) { Chris@98: Chris@535: targetLine[x] = 0; Chris@474: Chris@535: float sx0 = sxa[x*2]; Chris@537: if (sx0 < 0) continue; Chris@535: int sx0i = int(sx0 + epsilon); Chris@474: if (sx0i >= sw) break; Chris@98: Chris@843: float a = float(sourceLine[sx0i]); Chris@843: float b = a; Chris@843: float value; Chris@535: Chris@535: float sx1 = sxa[x*2+1]; Chris@535: if (sx1 > sx0 + 1.f) { Chris@535: int sx1i = int(sx1); Chris@535: bool have = false; Chris@535: for (int sx = sx0i; sx <= sx1i; ++sx) { Chris@535: if (sx < 0 || sx >= sw) continue; Chris@535: if (!have) { Chris@535: a = float(sourceLine[sx]); Chris@535: b = float(nextSource[sx]); Chris@535: have = true; Chris@535: } else { Chris@535: a = std::max(a, float(sourceLine[sx])); Chris@535: b = std::max(b, float(nextSource[sx])); Chris@535: } Chris@535: } Chris@535: float yprop = sy - syi; Chris@535: value = (a * (1.f - yprop) + b * yprop); Chris@535: } else { Chris@535: a = float(sourceLine[sx0i]); Chris@535: b = float(nextSource[sx0i]); Chris@535: float yprop = sy - syi; Chris@535: value = (a * (1.f - yprop) + b * yprop); Chris@535: int oi = sx0i + 1; Chris@535: float xprop = sx0 - sx0i; Chris@535: xprop -= 0.5; Chris@535: if (xprop < 0) { Chris@535: oi = sx0i - 1; Chris@535: xprop = -xprop; Chris@535: } Chris@535: if (oi < 0 || oi >= sw) oi = sx0i; Chris@535: a = float(sourceLine[oi]); Chris@535: b = float(nextSource[oi]); Chris@535: value = (value * (1.f - xprop) + Chris@535: (a * (1.f - yprop) + b * yprop) * xprop); Chris@98: } Chris@535: Chris@535: int vi = lrintf(value); Chris@535: if (vi > 255) vi = 255; Chris@535: if (vi < 0) vi = 0; Chris@535: targetLine[x] = uchar(vi); Chris@98: } Chris@466: } Chris@535: } else { Chris@535: Chris@812: float sy0 = getBinForY(v, 0); Chris@812: Chris@812: int psy0i = -1, psy1i = -1; Chris@812: Chris@535: for (int y = 0; y < h; ++y) { Chris@535: Chris@812: float sy1 = sy0; Chris@535: sy0 = getBinForY(v, y + 1); Chris@535: Chris@535: int sy0i = int(sy0 + epsilon); Chris@535: int sy1i = int(sy1); Chris@535: Chris@535: uchar *targetLine = img.scanLine(y); Chris@535: Chris@812: if (sy0i == psy0i && sy1i == psy1i) { Chris@812: // same source scan line as just computed Chris@535: goto copy; Chris@535: } Chris@535: Chris@812: psy0i = sy0i; Chris@812: psy1i = sy1i; Chris@812: Chris@535: for (int x = 0; x < w; ++x) { Chris@535: peaks[x] = 0; Chris@535: } Chris@466: Chris@535: for (int sy = sy0i; sy <= sy1i; ++sy) { Chris@535: Chris@535: if (sy < 0 || sy >= source->height()) continue; Chris@535: Chris@535: uchar *sourceLine = source->scanLine(sy); Chris@535: Chris@535: for (int x = 0; x < w; ++x) { Chris@535: Chris@537: float sx1 = sxa[x*2 + 1]; Chris@537: if (sx1 < 0) continue; Chris@537: int sx1i = int(sx1); Chris@535: Chris@537: float sx0 = sxa[x*2]; Chris@537: if (sx0 < 0) continue; Chris@537: int sx0i = int(sx0 + epsilon); Chris@535: if (sx0i >= sw) break; Chris@535: Chris@535: uchar peak = 0; Chris@535: for (int sx = sx0i; sx <= sx1i; ++sx) { Chris@535: if (sx < 0 || sx >= sw) continue; Chris@535: if (sourceLine[sx] > peak) peak = sourceLine[sx]; Chris@535: } Chris@535: peaks[x] = peak; Chris@535: } Chris@535: } Chris@535: Chris@535: copy: Chris@535: for (int x = 0; x < w; ++x) { Chris@535: targetLine[x] = peaks[x]; Chris@535: } Chris@98: } Chris@0: } Chris@0: Chris@469: delete[] peaks; Chris@469: Chris@98: paint.drawImage(x0, 0, img); Chris@0: } Chris@0: Chris@28: bool Chris@44: Colour3DPlotLayer::snapToFeatureFrame(View *v, int &frame, Chris@805: int &resolution, Chris@28: SnapType snap) const Chris@24: { Chris@24: if (!m_model) { Chris@44: return Layer::snapToFeatureFrame(v, frame, resolution, snap); Chris@24: } Chris@24: Chris@130: resolution = m_model->getResolution(); Chris@28: int left = (frame / resolution) * resolution; Chris@28: int right = left + resolution; Chris@28: Chris@28: switch (snap) { Chris@28: case SnapLeft: frame = left; break; Chris@28: case SnapRight: frame = right; break; Chris@28: case SnapNearest: Chris@28: case SnapNeighbouring: Chris@28: if (frame - left > right - frame) frame = right; Chris@28: else frame = left; Chris@28: break; Chris@28: } Chris@24: Chris@28: return true; Chris@24: } Chris@24: Chris@316: void Chris@316: Colour3DPlotLayer::toXml(QTextStream &stream, Chris@316: QString indent, QString extraAttributes) const Chris@197: { Chris@316: QString s = QString("scale=\"%1\" " Chris@316: "colourScheme=\"%2\" " Chris@316: "normalizeColumns=\"%3\" " Chris@445: "normalizeVisibleArea=\"%4\" " Chris@445: "minY=\"%5\" " Chris@465: "maxY=\"%6\" " Chris@465: "invertVertical=\"%7\" " Chris@535: "opaque=\"%8\" %9") Chris@197: .arg((int)m_colourScale) Chris@197: .arg(m_colourMap) Chris@197: .arg(m_normalizeColumns ? "true" : "false") Chris@445: .arg(m_normalizeVisibleArea ? "true" : "false") Chris@445: .arg(m_miny) Chris@465: .arg(m_maxy) Chris@465: .arg(m_invertVertical ? "true" : "false") Chris@534: .arg(m_opaque ? "true" : "false") Chris@536: .arg(QString("binScale=\"%1\" smooth=\"%2\" gain=\"%3\" ") Chris@535: .arg((int)m_binScale) Chris@536: .arg(m_smooth ? "true" : "false") Chris@536: .arg(m_gain)); Chris@535: Chris@316: Layer::toXml(stream, indent, extraAttributes + " " + s); Chris@197: } Chris@197: Chris@197: void Chris@197: Colour3DPlotLayer::setProperties(const QXmlAttributes &attributes) Chris@197: { Chris@445: bool ok = false, alsoOk = false; Chris@197: Chris@197: ColourScale scale = (ColourScale)attributes.value("scale").toInt(&ok); Chris@197: if (ok) setColourScale(scale); Chris@197: Chris@197: int colourMap = attributes.value("colourScheme").toInt(&ok); Chris@197: if (ok) setColourMap(colourMap); Chris@197: Chris@534: BinScale binscale = (BinScale)attributes.value("binScale").toInt(&ok); Chris@534: if (ok) setBinScale(binscale); Chris@534: Chris@197: bool normalizeColumns = Chris@197: (attributes.value("normalizeColumns").trimmed() == "true"); Chris@197: setNormalizeColumns(normalizeColumns); Chris@197: Chris@197: bool normalizeVisibleArea = Chris@197: (attributes.value("normalizeVisibleArea").trimmed() == "true"); Chris@197: setNormalizeVisibleArea(normalizeVisibleArea); Chris@445: Chris@465: bool invertVertical = Chris@465: (attributes.value("invertVertical").trimmed() == "true"); Chris@465: setInvertVertical(invertVertical); Chris@465: Chris@465: bool opaque = Chris@465: (attributes.value("opaque").trimmed() == "true"); Chris@535: setOpaque(opaque); Chris@535: Chris@535: bool smooth = Chris@535: (attributes.value("smooth").trimmed() == "true"); Chris@535: setSmooth(smooth); Chris@465: Chris@536: float gain = attributes.value("gain").toFloat(&ok); Chris@536: if (ok) setGain(gain); Chris@536: Chris@445: float min = attributes.value("minY").toFloat(&ok); Chris@445: float max = attributes.value("maxY").toFloat(&alsoOk); Chris@445: if (ok && alsoOk) setDisplayExtents(min, max); Chris@197: } Chris@197: