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@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@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@197: m_normalizeColumns(false), Chris@357: m_normalizeVisibleArea(false), Chris@444: m_invertVertical(false), Chris@465: m_opaque(false), 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@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@0: connect(m_model, SIGNAL(modelChanged(size_t, size_t)), Chris@461: this, SLOT(modelChanged(size_t, size_t))); Chris@0: Chris@0: emit modelReplaced(); Chris@193: emit sliceableModelReplaced(oldModel, model); Chris@0: } Chris@0: Chris@0: void Chris@0: Colour3DPlotLayer::cacheInvalid() Chris@0: { Chris@0: delete m_cache; Chris@0: m_cache = 0; Chris@461: m_cacheValidStart = 0; Chris@461: m_cacheValidEnd = 0; Chris@0: } Chris@0: Chris@0: void Chris@461: Colour3DPlotLayer::cacheInvalid(size_t startFrame, size_t endFrame) Chris@0: { Chris@461: if (!m_cache) return; Chris@461: Chris@461: size_t modelResolution = m_model->getResolution(); Chris@461: size_t start = startFrame / modelResolution; Chris@461: size_t 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@461: Colour3DPlotLayer::modelChanged(size_t startFrame, size_t 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@357: list.push_back("Invert Vertical Scale"); Chris@465: list.push_back("Opaque"); 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@465: if (name == "Opaque") return tr("Always Opaque"); 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@346: return ""; Chris@346: } Chris@346: Chris@159: Layer::PropertyType Chris@159: Colour3DPlotLayer::getPropertyType(const PropertyName &name) const Chris@159: { 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@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@357: name == "Invert Vertical Scale" || Chris@197: name == "Colour Scale") return tr("Scale"); Chris@465: if (name == "Opaque" || 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@159: if (name == "Colour Scale") { Chris@159: Chris@159: *min = 0; Chris@197: *max = 2; 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@465: } else if (name == "Opaque") { Chris@465: Chris@465: *deflt = 0; Chris@465: val = (m_opaque ? 1 : 0); Chris@465: 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@159: } Chris@159: } Chris@159: return tr(""); Chris@159: } Chris@159: Chris@159: void Chris@159: Colour3DPlotLayer::setProperty(const PropertyName &name, int value) Chris@159: { Chris@159: 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@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@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@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@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@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@465: bool Chris@44: Colour3DPlotLayer::isLayerScrollable(const View *v) const Chris@25: { Chris@197: if (m_normalizeVisibleArea) return false; 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@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@445: // std::cerr << "Colour3DPlotLayer::setVerticalZoomStep(" <getStartFrame(); Chris@130: size_t 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@160: int sx0 = int((v->getFrameForX(x) / srRatio - long(modelStart)) / Chris@160: long(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@447: float binHeight = float(v->height()) / (symax - symin); Chris@447: int sy = int((v->height() - y) / binHeight) + symin; Chris@25: 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@159: // std::cerr << "bin value (" << sx0 << "," << sy << ") is " << value << std::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@248: Colour3DPlotLayer::getVerticalScaleWidth(View *, 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@160: for (size_t 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@44: Colour3DPlotLayer::paintVerticalScale(View *v, 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@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@465: std::cerr << "WARNING: Colour3DPlotLayer::paintVerticalScale: value " << value << ", mmin " << mmin << ", mmax " << mmax << " leads to invalid pixel " << pixel << std::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@456: float binHeight = float(v->height()) / (symax - symin); Chris@456: Chris@456: paint.save(); Chris@456: Chris@456: QFont tf = paint.font(); Chris@456: if (paint.fontMetrics().height() >= binHeight) { Chris@457: tf.setPixelSize(binHeight > 7 ? binHeight - 2 : 5); Chris@456: paint.setFont(tf); Chris@456: } Chris@456: Chris@98: int count = v->height() / paint.fontMetrics().height(); Chris@445: int step = (symax - symin) / count; Chris@98: if (step == 0) step = 1; Chris@25: Chris@445: for (size_t i = symin; i < symax; ++i) { Chris@25: Chris@357: size_t idx = i; Chris@357: if (m_invertVertical) idx = m_model->getHeight() - idx - 1; Chris@357: Chris@357: if ((idx % step) != 0) continue; Chris@98: Chris@445: int y0 = int(v->height() - ((i - symin) * binHeight) - 1); Chris@25: Chris@357: QString text = m_model->getBinName(idx); Chris@357: if (text == "") text = QString("[%1]").arg(idx + 1); Chris@25: Chris@159: paint.drawLine(cw, y0, w, y0); Chris@25: Chris@457: if (step > 1) { Chris@457: paint.drawLine(w - 1, y0 - (step * binHeight) + 1, Chris@457: w - 1, y0 - binHeight - 1); Chris@457: paint.drawLine(w - 2, y0 - (step * binHeight) + 1, Chris@457: w - 2, y0 - binHeight - 2); Chris@457: } Chris@457: Chris@248: int cy = int(y0 - (step * binHeight)/2); Chris@25: int ty = cy + paint.fontMetrics().ascent()/2; Chris@25: Chris@159: paint.drawText(cw + 5, ty, text); Chris@25: } Chris@456: Chris@456: paint.restore(); Chris@25: } Chris@25: Chris@467: DenseThreeDimensionalModel::Column Chris@467: Colour3DPlotLayer::getColumn(size_t col) const Chris@197: { Chris@467: DenseThreeDimensionalModel::Column values = m_model->getColumn(col); Chris@467: if (!m_normalizeColumns) 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@461: for (size_t 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@197: for (size_t 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@197: } Chris@197: } Chris@197: Chris@197: void Chris@197: Colour3DPlotLayer::fillCache(size_t firstBin, size_t lastBin) const Chris@197: { Chris@466: Profiler profiler("Colour3DPlotLayer::fillCache"); Chris@197: size_t modelStart = m_model->getStartFrame(); Chris@197: size_t modelEnd = m_model->getEndFrame(); Chris@197: size_t modelResolution = m_model->getResolution(); Chris@197: Chris@224: // std::cerr << "Colour3DPlotLayer::fillCache: " << firstBin << " -> " << lastBin << std::endl; Chris@197: Chris@461: size_t modelStartBin = modelStart / modelResolution; Chris@461: size_t modelEndBin = modelEnd / modelResolution; Chris@461: Chris@461: size_t cacheWidth = modelEndBin - modelStartBin + 1; Chris@461: size_t cacheHeight = m_model->getHeight(); Chris@461: Chris@461: if (m_cache && (m_cache->height() != int(cacheHeight))) { Chris@461: delete m_cache; Chris@461: m_cache = 0; Chris@461: } Chris@461: Chris@461: if (m_cache && (m_cache->width() != int(cacheWidth))) { Chris@461: QImage *newCache = new QImage(m_cache->copy(0, 0, cacheWidth, cacheHeight)); Chris@461: delete m_cache; Chris@461: m_cache = newCache; Chris@197: } Chris@197: Chris@461: if (!m_cache) { Chris@461: m_cache = new QImage(cacheWidth, cacheHeight, QImage::Format_Indexed8); Chris@461: m_cache->setNumColors(256); Chris@461: m_cacheValidStart = 0; Chris@461: m_cacheValidEnd = 0; Chris@197: } Chris@197: Chris@461: if (m_cacheValidStart <= firstBin && m_cacheValidEnd >= lastBin) { Chris@461: return; Chris@461: } Chris@461: Chris@461: size_t fillStart = firstBin; Chris@461: size_t 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@461: // the only valid area, ever, is the currently visible one Chris@461: Chris@461: m_cacheValidStart = fillStart; Chris@461: m_cacheValidEnd = fillEnd; Chris@461: } Chris@461: Chris@461: // std::cerr << "Cache size " << cacheWidth << "x" << cacheHeight << " will be valid from " << m_cacheValidStart << " to " << m_cacheValidEnd << std::endl; 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@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@197: m_cache->setColor(index, qRgb(colour.red(), colour.green(), colour.blue())); Chris@197: } Chris@197: Chris@461: // m_cache->fill(0); Chris@197: Chris@224: float visibleMax = 0.f, visibleMin = 0.f; Chris@197: Chris@461: if (normalizeVisible) { Chris@197: Chris@461: for (size_t 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@461: for (size_t 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@197: } Chris@197: } Chris@197: Chris@224: if (visibleMin == visibleMax) visibleMax = visibleMin + 1; Chris@224: Chris@461: for (size_t c = fillStart; c <= fillEnd; ++c) { Chris@197: Chris@467: values = getColumn(c); Chris@197: Chris@461: for (size_t y = 0; y < cacheHeight; ++y) { Chris@197: Chris@197: float value = min; Chris@197: if (y < values.size()) { Chris@197: value = values[y]; Chris@197: } Chris@224: Chris@224: if (m_colourScale == LogScale) { Chris@224: value = LogRange::map(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@197: Chris@357: if (m_invertVertical) { Chris@461: m_cache->setPixel(c, cacheHeight - y - 1, pixel); Chris@357: } else { Chris@461: m_cache->setPixel(c, y, pixel); Chris@357: } Chris@197: } Chris@197: } Chris@197: } Chris@197: Chris@197: void Chris@44: Colour3DPlotLayer::paint(View *v, QPainter &paint, QRect rect) const Chris@0: { Chris@443: /*!!! Chris@443: if (m_model) { Chris@443: std::cerr << "Colour3DPlotLayer::paint: model says shouldUseLogValueScale = " << m_model->shouldUseLogValueScale() << std::endl; Chris@443: } Chris@443: */ Chris@466: Profiler profiler("Colour3DPlotLayer::paint"); Chris@125: #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT Chris@125: std::cerr << "Colour3DPlotLayer::paint(): m_model is " << m_model << ", zoom level is " << v->getZoomLevel() << std::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@0: size_t modelStart = m_model->getStartFrame(); Chris@0: size_t modelEnd = m_model->getEndFrame(); Chris@130: size_t 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@197: int sx0 = int((v->getFrameForX(x0) / srRatio - long(modelStart)) Chris@197: / long(modelResolution)); Chris@197: int sx1 = int((v->getFrameForX(x1) / srRatio - long(modelStart)) Chris@197: / long(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@466: 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: #endif Chris@351: Chris@465: if (m_opaque || Chris@465: int(m_model->getHeight()) >= v->height() || Chris@466: // int(modelResolution * m_model->getSampleRate()) < v->getZoomLevel() / 2) { Chris@466: ((modelResolution * srRatio) / v->getZoomLevel()) < 2) { Chris@347: #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT Chris@351: std::cerr << "calling paintDense" << std::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@347: std::cerr << "Colour3DPlotLayer::paint: w " << x1-x0 << ", h " << h << ", sx0 " << sx0 << ", sx1 " << sx1 << ", sw " << sx1-sx0 << ", sh " << sh << std::endl; Chris@130: std::cerr << "Colour3DPlotLayer: sample rate is " << m_model->getSampleRate() << ", resolution " << m_model->getResolution() << std::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@130: int fx = sx * int(modelResolution); Chris@0: Chris@351: if (fx + int(modelResolution) <= int(modelStart) || Chris@0: fx > int(modelEnd)) continue; Chris@0: Chris@248: int rx0 = v->getXForFrame(int((fx + int(modelStart)) * srRatio)); Chris@248: int rx1 = v->getXForFrame(int((fx + int(modelStart) + int(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@444: int ry0 = h - ((sy - symin) * h) / (symax - symin) - 1; 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@444: QRect r(rx0, ry0 - h / (symax - symin) - 1, Chris@444: rw, h / (symax - symin) + 1); Chris@159: 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@159: std::cerr << "rect " << r.x() << "," << r.y() << " " Chris@159: << r.width() << "x" << r.height() << std::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@466: Profiler profiler("Colour3DPlotLayer::paintDense"); 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@159: float srRatio = Chris@159: float(v->getViewManager()->getMainModelSampleRate()) / Chris@159: float(m_model->getSampleRate()); Chris@159: Chris@98: int x0 = rect.left(); Chris@98: int x1 = rect.right() + 1; Chris@0: Chris@98: int w = x1 - x0; Chris@98: int h = v->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@466: int psy1i = -1; Chris@466: Chris@466: for (int y = 0; y < h; ++y) { Chris@466: Chris@466: float sy0 = symin + (float(h - y - 1) * (symax - symin)) / h; Chris@466: float sy1 = symin + (float(h - y) * (symax - symin)) / h; Chris@466: Chris@466: int sy0i = int(sy0 + 0.001); Chris@466: int sy1i = int(sy1); Chris@466: Chris@466: uchar *targetLine = img.scanLine(y); Chris@466: Chris@466: if (sy0i == sy1i && sy0i == psy1i) { // same scan line as just computed Chris@466: goto copy; Chris@105: } Chris@105: Chris@466: memset(peaks, 0, w); Chris@466: Chris@466: for (int sy = sy0i; sy <= sy1i; ++sy) { Chris@347: Chris@466: if (sy < 0 || sy >= m_cache->height()) continue; Chris@466: Chris@466: uchar *sourceLine = m_cache->scanLine(sy); Chris@98: Chris@466: long xf = -1, nxf = -1; Chris@98: Chris@466: for (int x = 0; x < w; ++x) { Chris@98: Chris@466: xf = nxf; Chris@466: nxf = v->getFrameForX(x + 1 + x0); Chris@466: Chris@466: if (xf < 0) { Chris@466: xf = v->getFrameForX(x + x0); Chris@466: } Chris@466: Chris@466: if (xf < 0) { Chris@466: peaks[x] = 0; Chris@466: continue; Chris@466: } Chris@466: Chris@466: float sx0 = (float(xf) / srRatio - modelStart) / modelResolution; Chris@466: float sx1 = (float(nxf) / srRatio - modelStart) / modelResolution; Chris@98: Chris@466: int sx0i = int(sx0 + 0.001); Chris@466: int sx1i = int(sx1); Chris@98: Chris@466: uchar peak = 0; Chris@466: for (int sx = sx0i; sx <= sx1i; ++sx) { Chris@466: if (sx < 0 || sx >= m_cache->width()) continue; Chris@466: if (sourceLine[sx] > peak) peak = sourceLine[sx]; Chris@98: } Chris@466: peaks[x] = peak; Chris@98: } Chris@466: } Chris@466: Chris@466: copy: Chris@466: for (int x = 0; x < w; ++x) { Chris@466: targetLine[x] = peaks[x]; Chris@98: } Chris@0: } Chris@0: Chris@98: paint.drawImage(x0, 0, img); Chris@0: } Chris@0: Chris@28: bool Chris@44: Colour3DPlotLayer::snapToFeatureFrame(View *v, int &frame, Chris@28: size_t &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@465: "opaque=\"%8\"") 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@465: .arg(m_opaque ? "true" : "false"); Chris@197: 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@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@465: setNormalizeVisibleArea(opaque); Chris@465: 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: