diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2015-07-26 09:29:10 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2015-07-26 09:29:10 +0200 |
commit | 52f67dee52a484e8a2eed0c527777c1d341a2294 (patch) | |
tree | 4c8d7cdc7c960782752bd6166ed10250759dc61e | |
parent | 0f24550bcce54f7b1265605c005c382b5f80fa5f (diff) |
qfc: abstract away internally-used data type
Also, change value_type to more sensible value
-rw-r--r-- | qfunctionconfigurator/functionconfig.cpp | 49 | ||||
-rw-r--r-- | qfunctionconfigurator/functionconfig.h | 11 |
2 files changed, 31 insertions, 29 deletions
diff --git a/qfunctionconfigurator/functionconfig.cpp b/qfunctionconfigurator/functionconfig.cpp index dc2153a7..2a44cba0 100644 --- a/qfunctionconfigurator/functionconfig.cpp +++ b/qfunctionconfigurator/functionconfig.cpp @@ -23,14 +23,14 @@ Map::Map() : { } -float Map::getValue(float x) { +Map::num Map::getValue(Map::num x) { QMutexLocker foo(&_mutex); - float q = x * precision(); - int xi = (int)q; - float yi = getValueInternal(xi); - float yiplus1 = getValueInternal(xi+1); - float f = (q-xi); - float ret = yiplus1 * f + yi * (1.0f - f); // at least do a linear interpolation. + num q = x * precision(); + int xi = (int)q; + num yi = getValueInternal(xi); + num yiplus1 = getValueInternal(xi+1); + num f = (q-xi); + num ret = yiplus1 * f + yi * (1.0f - f); // at least do a linear interpolation. last_input_value.setX(x); last_input_value.setY(ret); return ret; @@ -42,10 +42,10 @@ bool Map::getLastPoint(QPointF& point ) { return activep; } -float Map::getValueInternal(int x) { - float sign = x < 0 ? -1 : 1; +Map::num Map::getValueInternal(int x) { + num sign = x < 0 ? -1 : 1; x = abs(x); - float ret; + num ret; int sz = cur.data.size(); if (sz == 0) ret = 0; @@ -75,7 +75,7 @@ void Map::reload() { QList<QPointF> input = cur.input; auto& data = cur.data; - data = std::vector<float>(value_count); + data = std::vector<num>(value_count); const int mult = precision(); const int sz = data.size(); @@ -99,8 +99,9 @@ void Map::reload() { QPointF p2 = ensureInBounds(input, i + 1); QPointF p3 = ensureInBounds(input, i + 2); - const float p0_x = p0.x(), p1_x = p1.x(), p2_x = p2.x(), p3_x = p3.x(); - const float p0_y = p0.y(), p1_y = p1.y(), p2_y = p2.y(), p3_y = p3.y(); + using n = double; + const n p0_x = p0.x(), p1_x = p1.x(), p2_x = p2.x(), p3_x = p3.x(); + const n p0_y = p0.y(), p1_y = p1.y(), p2_y = p2.y(), p3_y = p3.y(); // multiplier helps fill in all the x's needed const int mult_ = mult * 20; @@ -108,9 +109,9 @@ void Map::reload() { int start = p1.x() * mult; for (int j = start; j < end; j++) { - float t = (j - start) / (float) (end - start); - float t2 = t*t; - float t3 = t*t*t; + n t = (j - start) / (n) (end - start); + n t2 = t*t; + n t3 = t*t*t; int x = .5 * ((2. * p1_x) + (-p0_x + p2_x) * t + @@ -118,17 +119,17 @@ void Map::reload() { (-p0_x + 3. * p1_x - 3. * p2_x + p3_x) * t3) * mult; - float y = .5 * ((2. * p1_y) + - (-p0_y + p2_y) * t + - (2. * p0_y - 5. * p1_y + 4. * p2_y - p3_y) * t2 + - (-p0_y + 3. * p1_y - 3. * p2_y + p3_y) * t3); + num y = .5 * ((2. * p1_y) + + (-p0_y + p2_y) * t + + (2. * p0_y - 5. * p1_y + 4. * p2_y - p3_y) * t2 + + (-p0_y + 3. * p1_y - 3. * p2_y + p3_y) * t3); if (x >= 0 && x < sz) data[x] = y; } } - float last = 0; + num last = 0; for (int i = 0; i < sz; i++) { if (data[i] < 0) @@ -184,8 +185,8 @@ void Map::loadSettings(QSettings& settings, const QString& title) { int max = settings.value("point-count", 0).toInt(); for (int i = 0; i < max; i++) { - newPoint = QPointF(settings.value(QString("point-%1-x").arg(i), 0).toFloat(), - settings.value(QString("point-%1-y").arg(i), 0).toFloat()); + newPoint = QPointF(settings.value(QString("point-%1-x").arg(i), 0).toDouble(), + settings.value(QString("point-%1-y").arg(i), 0).toDouble()); if (newPoint.x() > max_x) { newPoint.setX(max_x); } @@ -233,6 +234,6 @@ void Map::saveSettings(QSettings& settings, const QString& title) { int Map::precision() const { if (cur.input.size()) - return value_count / std::max<float>(1.f, (cur.input[cur.input.size() - 1].x())); + return value_count / std::max<num>(1.f, (cur.input[cur.input.size() - 1].x())); return 1; } diff --git a/qfunctionconfigurator/functionconfig.h b/qfunctionconfigurator/functionconfig.h index a8b46597..446c73ac 100644 --- a/qfunctionconfigurator/functionconfig.h +++ b/qfunctionconfigurator/functionconfig.h @@ -18,16 +18,17 @@ class Map { private: + static constexpr int value_count = 5000; + using num = float; + struct State { QList<QPointF> input; - std::vector<float> data; + std::vector<num> data; }; - static constexpr int value_count = 9001; - int precision() const; void reload(); - float getValueInternal(int x); + num getValueInternal(int x); MyMutex _mutex; QPointF last_input_value; @@ -46,7 +47,7 @@ public: setMaxOutput(maxy); } - float getValue(float x); + num getValue(num x); bool getLastPoint(QPointF& point); void removePoint(int i); void removeAllPoints() { |