aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSamuel Fadel <samuelfadel@gmail.com>2015-12-16 14:00:08 +0100
committerSamuel Fadel <samuelfadel@gmail.com>2015-12-16 14:02:30 +0100
commita184000e2086c0d86d5aef20d715f5e884a63ff3 (patch)
tree836fcb3363edd0e3d247ed1e99fbdb2f0982909f
parentf55e5dcfa82b552a1eb6642b093ed42a48565a3a (diff)
Scale & LinearScale are now template classes.
-rw-r--r--historygraph.cpp4
-rw-r--r--scale.h37
-rw-r--r--scatterplot.cpp4
-rw-r--r--scatterplot.h2
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<float> sx(xy.col(0).min(), xy.col(0).max(), x, x + w);
+ LinearScale<float> 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 <algorithm>
+template<typename T>
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<typename T>
class LinearScale
- : public Scale
+ : public Scale<T>
{
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<T>(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<T>::m_domainMin) * m_transformSlope + Scale<T>::m_rangeMin;
}
protected:
virtual void valuesUpdated()
{
- m_transformSlope = (m_rangeMax - m_rangeMin) / (m_domainMax - m_domainMin);
+ m_transformSlope = (Scale<T>::m_rangeMax - Scale<T>::m_rangeMin)
+ / (Scale<T>::m_domainMax - Scale<T>::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<float> rx = m_sx;
+ LinearScale<float> 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<float> m_sx, m_sy;
enum InteractionState {
INTERACTION_NONE,