From a184000e2086c0d86d5aef20d715f5e884a63ff3 Mon Sep 17 00:00:00 2001 From: Samuel Fadel Date: Wed, 16 Dec 2015 14:00:08 +0100 Subject: Scale & LinearScale are now template classes. --- historygraph.cpp | 4 ++-- scale.h | 37 ++++++++++++++++++++----------------- scatterplot.cpp | 4 ++-- scatterplot.h | 2 +- 4 files changed, 25 insertions(+), 22 deletions(-) diff --git a/historygraph.cpp b/historygraph.cpp index 0f05642..8ed5be4 100644 --- a/historygraph.cpp +++ b/historygraph.cpp @@ -122,8 +122,8 @@ void HistoryGraph::addScatterplot(QSGNode *node, const HistoryGraph::HistoryItem const arma::mat &xy = historyItemNode->item(); int vertexCount = calculateCircleVertexCount(GLYPH_SIZE / 2); - LinearScale sx(xy.col(0).min(), xy.col(0).max(), x, x + w); - LinearScale sy(xy.col(1).min(), xy.col(1).max(), y + h, y); // reverse on purpose + LinearScale sx(xy.col(0).min(), xy.col(0).max(), x, x + w); + LinearScale sy(xy.col(1).min(), xy.col(1).max(), y + h, y); // reverse on purpose for (arma::uword i = 0; i < xy.n_rows; i++) { const arma::rowvec &row = xy.row(i); diff --git a/scale.h b/scale.h index ee0bde8..ccf23c5 100644 --- a/scale.h +++ b/scale.h @@ -3,10 +3,11 @@ #include +template class Scale { public: - Scale(float domainMin, float domainMax, float rangeMin, float rangeMax) + Scale(const T &domainMin, const T &domainMax, const T &rangeMin, const T &rangeMax) : m_domainMin(domainMin) , m_domainMax(domainMax) , m_rangeMin(rangeMin) @@ -17,19 +18,19 @@ public: virtual ~Scale() {} - void setRangeMin(float rangeMin) { m_rangeMin = rangeMin; valuesUpdated(); } - void setRangeMax(float rangeMax) { m_rangeMax = rangeMax; valuesUpdated(); } - void setDomainMin(float domainMin) { m_domainMin = domainMin; valuesUpdated(); } - void setDomainMax(float domainMax) { m_domainMax = domainMax; valuesUpdated(); } + void setRangeMin(T rangeMin) { m_rangeMin = rangeMin; valuesUpdated(); } + void setRangeMax(T rangeMax) { m_rangeMax = rangeMax; valuesUpdated(); } + void setDomainMin(T domainMin) { m_domainMin = domainMin; valuesUpdated(); } + void setDomainMax(T domainMax) { m_domainMax = domainMax; valuesUpdated(); } - void setRange(float rangeMin, float rangeMax) + void setRange(const T &rangeMin, const T &rangeMax) { m_rangeMin = rangeMin; m_rangeMax = rangeMax; valuesUpdated(); } - void setDomain(float domainMin, float domainMax) + void setDomain(const T &domainMin, const T &domainMax) { m_domainMin = domainMin; m_domainMax = domainMax; @@ -43,39 +44,41 @@ public: valuesUpdated(); } - virtual float operator()(float value) const = 0; + virtual T operator()(const T &value) const = 0; protected: // Called when internal values change virtual void valuesUpdated() {} - float m_domainMin, m_domainMax; - float m_rangeMin, m_rangeMax; + T m_domainMin, m_domainMax; + T m_rangeMin, m_rangeMax; }; +template class LinearScale - : public Scale + : public Scale { public: - LinearScale(float domainMin, float domainMax, float rangeMin, float rangeMax) - : Scale(domainMin, domainMax, rangeMin, rangeMax) + LinearScale(const T &domainMin, const T &domainMax, const T &rangeMin, const T &rangeMax) + : Scale(domainMin, domainMax, rangeMin, rangeMax) { valuesUpdated(); } - virtual float operator()(float value) const + virtual T operator()(const T &value) const { - return (value - m_domainMin) * m_transformSlope + m_rangeMin; + return (value - Scale::m_domainMin) * m_transformSlope + Scale::m_rangeMin; } protected: virtual void valuesUpdated() { - m_transformSlope = (m_rangeMax - m_rangeMin) / (m_domainMax - m_domainMin); + m_transformSlope = (Scale::m_rangeMax - Scale::m_rangeMin) + / (Scale::m_domainMax - Scale::m_domainMin); } private: - float m_transformSlope; + T m_transformSlope; }; #endif // SCALE_H diff --git a/scatterplot.cpp b/scatterplot.cpp index d8a7b5e..dd1f266 100644 --- a/scatterplot.cpp +++ b/scatterplot.cpp @@ -381,8 +381,8 @@ void Scatterplot::applyManipulation() { m_sx.inverse(); m_sy.inverse(); - LinearScale rx = m_sx; - LinearScale ry = m_sy; + LinearScale rx = m_sx; + LinearScale ry = m_sy; m_sy.inverse(); m_sx.inverse(); diff --git a/scatterplot.h b/scatterplot.h index e909b6a..255649a 100644 --- a/scatterplot.h +++ b/scatterplot.h @@ -51,7 +51,7 @@ private: void animationTick(); arma::mat m_oldXY, m_xy; - LinearScale m_sx, m_sy; + LinearScale m_sx, m_sy; enum InteractionState { INTERACTION_NONE, -- cgit v1.2.3