From 7eb9227aab7b18c9ab6fe31775f1d0b85cc5b7d0 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Wed, 7 Oct 2015 20:52:44 +0200 Subject: qfc: revert to floats as in rc15 Issue: #248 Issue: #231 --- qfunctionconfigurator/functionconfig.cpp | 71 ++++++++++++++------------------ qfunctionconfigurator/functionconfig.h | 11 ++--- 2 files changed, 36 insertions(+), 46 deletions(-) (limited to 'qfunctionconfigurator') diff --git a/qfunctionconfigurator/functionconfig.cpp b/qfunctionconfigurator/functionconfig.cpp index 8301c7f9..f776acf3 100644 --- a/qfunctionconfigurator/functionconfig.cpp +++ b/qfunctionconfigurator/functionconfig.cpp @@ -24,21 +24,21 @@ Map::Map() : { } -Map::num Map::getValue(Map::num x) { +float Map::getValue(float x) { QMutexLocker foo(&_mutex); if (lazy_reload) { lazy_reload = false; reload(); } - num q = x * precision(); - int xi = (int)q; - num yi = getValueInternal(xi); - num yiplus1 = getValueInternal(xi + (x < 0 ? -1 : 1)); - num f = (q-xi); - num ret = yiplus1 * f + yi * (1 - f); // at least do a linear interpolation. - last_input_value.setX(std::abs(x)); - last_input_value.setY(std::abs(ret)); + 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. + last_input_value.setX(x); + last_input_value.setY(ret); return ret; } @@ -48,15 +48,15 @@ bool Map::getLastPoint(QPointF& point ) { return activep; } -Map::num Map::getValueInternal(int x) { - num sign = x < 0 ? -1 : 1; +float Map::getValueInternal(int x) { + float sign = x < 0 ? -1 : 1; x = abs(x); - num ret; + float ret; int sz = cur.data.size(); if (sz == 0) ret = 0; else - ret = cur.data[std::min(x, sz-1)] * max_y / integral_max; + ret = cur.data[std::min(x, sz-1)]; return ret * sign; } @@ -81,19 +81,19 @@ void Map::reload() { QList input = cur.input; auto& data = cur.data; - data = std::vector(value_count); + data = std::vector(value_count); const int mult = precision(); const int sz = data.size(); for (int i = 0; i < sz; i++) - data[i] = integral_max; + data[i] = -1; if (input.size() == 1) { for (int k = 0; k < input[0].x() * mult; k++) { if (k < sz) - data[k] = input[0].y() * k * integral_max / (input[0].x() * mult) / max_y ; + data[k] = input[0].y() * k / (input[0].x() * mult); } } else if (input[0].x() > 1e-2) @@ -105,46 +105,39 @@ void Map::reload() { const QPointF p2 = ensureInBounds(input, i + 1); const QPointF p3 = ensureInBounds(input, i + 2); - 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(); + 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(); // multiplier helps fill in all the x's needed const int mult_ = mult * 20; const int end = std::min(sz, p2.x() * mult_); const int start = p1.x() * mult; - const n max = end - start; for (int j = start; j < end; j++) { - const n t = (j - start) / max; - const n t2 = t*t; - const n t3 = t*t*t; + const float t = (j - start) / (float) (end - start); + const float t2 = t*t; + const float t3 = t*t*t; - // XXX we could solve for t instead -sh 20150811 const int x = .5 * ((2. * p1_x) + (-p0_x + p2_x) * t + (2. * p0_x - 5. * p1_x + 4. * p2_x - p3_x) * t2 + (-p0_x + 3. * p1_x - 3. * p2_x + p3_x) * t3) - * mult; - - if (x < 0 || x >= sz || data[x] != integral_max) - continue; + * mult; - const n 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); - - const n y_ = std::min(max_y, std::max(y, 0)); - - data[x] = y_ * integral_max / max_y; + const 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); + + if (x >= 0 && x < sz) + data[x] = y; } } - integral last = 0; + float last = 0; for (int i = 0; i < sz; i++) { - if (data[i] == integral_max) + if (data[i] < 0) data[i] = last; last = data[i]; } @@ -249,6 +242,6 @@ void Map::saveSettings(QSettings& settings, const QString& title) { int Map::precision() const { if (cur.input.size()) - return value_count / std::max(1, (cur.input[cur.input.size() - 1].x())); + return value_count / std::max(1.f, (cur.input[cur.input.size() - 1].x())); return 1; } diff --git a/qfunctionconfigurator/functionconfig.h b/qfunctionconfigurator/functionconfig.h index 886ce06a..25e072d2 100644 --- a/qfunctionconfigurator/functionconfig.h +++ b/qfunctionconfigurator/functionconfig.h @@ -19,19 +19,16 @@ class Map { private: - static constexpr int value_count = 5000; - using num = double; - using integral = std::uint32_t; - static constexpr integral integral_max = std::numeric_limits::max(); + static constexpr int value_count = 10000; struct State { QList input; - std::vector data; + std::vector data; }; int precision() const; void reload(); - num getValueInternal(int x); + float getValueInternal(int x); MyMutex _mutex; QPointF last_input_value; @@ -52,7 +49,7 @@ public: lazy_reload = true; } - num getValue(num x); + float getValue(float x); bool getLastPoint(QPointF& point); void removePoint(int i); void removeAllPoints() { -- cgit v1.2.3 From 7233ba88c0cd55eeddc4453c612a7cce0a7d2da9 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Wed, 7 Oct 2015 21:25:16 +0200 Subject: qfc: try harder to fill all memoized buckets --- qfunctionconfigurator/functionconfig.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'qfunctionconfigurator') diff --git a/qfunctionconfigurator/functionconfig.cpp b/qfunctionconfigurator/functionconfig.cpp index f776acf3..f2e81b17 100644 --- a/qfunctionconfigurator/functionconfig.cpp +++ b/qfunctionconfigurator/functionconfig.cpp @@ -109,7 +109,7 @@ void Map::reload() { const float 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; + const int mult_ = mult * 30; const int end = std::min(sz, p2.x() * mult_); const int start = p1.x() * mult; -- cgit v1.2.3 From 790a9cff9a25def2370186e2a56ed30fbdbf0195 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Wed, 7 Oct 2015 21:28:00 +0200 Subject: qfc: no need to load lazily, it's received immediately --- qfunctionconfigurator/functionconfig.cpp | 20 +++++++------------- qfunctionconfigurator/functionconfig.h | 5 ++--- 2 files changed, 9 insertions(+), 16 deletions(-) (limited to 'qfunctionconfigurator') diff --git a/qfunctionconfigurator/functionconfig.cpp b/qfunctionconfigurator/functionconfig.cpp index f2e81b17..b93a4cbf 100644 --- a/qfunctionconfigurator/functionconfig.cpp +++ b/qfunctionconfigurator/functionconfig.cpp @@ -19,18 +19,12 @@ Map::Map() : _mutex(QMutex::Recursive), activep(false), max_x(0), - max_y(0), - lazy_reload(true) + max_y(0) { } float Map::getValue(float x) { QMutexLocker foo(&_mutex); - if (lazy_reload) - { - lazy_reload = false; - reload(); - } float q = x * precision(); int xi = (int)q; float yi = getValueInternal(xi); @@ -151,14 +145,14 @@ void Map::removePoint(int i) { if (i >= 0 && i < cur.input.size()) { cur.input.removeAt(i); - lazy_reload = true; + reload(); } } void Map::addPoint(QPointF pt) { QMutexLocker foo(&_mutex); cur.input.append(pt); - lazy_reload = true; + reload(); qStableSort(cur.input.begin(), cur.input.end(), sortFn); } @@ -167,8 +161,8 @@ void Map::movePoint(int idx, QPointF pt) { if (idx >= 0 && idx < cur.input.size()) { cur.input[idx] = pt; - lazy_reload = true; - // we don't allow points to be reodered, so no sort here + reload(); + // we don't allow points to be reordered, so no sort here } } @@ -181,7 +175,7 @@ void Map::invalidate_unsaved_settings() { QMutexLocker foo(&_mutex); cur = saved; - lazy_reload = true; + reload(); } void Map::loadSettings(QSettings& settings, const QString& title) { @@ -210,7 +204,7 @@ void Map::loadSettings(QSettings& settings, const QString& title) { points.append(QPointF(maxInput(), maxOutput())); cur.input = points; - lazy_reload = true; + reload(); saved = cur; } diff --git a/qfunctionconfigurator/functionconfig.h b/qfunctionconfigurator/functionconfig.h index 25e072d2..31aebdf6 100644 --- a/qfunctionconfigurator/functionconfig.h +++ b/qfunctionconfigurator/functionconfig.h @@ -37,7 +37,6 @@ private: double max_y; State cur, saved; - bool lazy_reload; public: double maxInput() const { return max_x; } double maxOutput() const { return max_y; } @@ -46,7 +45,7 @@ public: { setMaxInput(maxx); setMaxOutput(maxy); - lazy_reload = true; + reload(); } float getValue(float x); @@ -55,7 +54,7 @@ public: void removeAllPoints() { QMutexLocker foo(&_mutex); cur.input.clear(); - lazy_reload = true; + reload(); } void addPoint(QPointF pt); -- cgit v1.2.3 From 5441b2a60627406f3c235864c126a31e87234259 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Wed, 7 Oct 2015 21:34:08 +0200 Subject: qfc: move var to outer scope --- qfunctionconfigurator/functionconfig.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'qfunctionconfigurator') diff --git a/qfunctionconfigurator/functionconfig.cpp b/qfunctionconfigurator/functionconfig.cpp index b93a4cbf..594607ff 100644 --- a/qfunctionconfigurator/functionconfig.cpp +++ b/qfunctionconfigurator/functionconfig.cpp @@ -77,6 +77,7 @@ void Map::reload() { data = std::vector(value_count); const int mult = precision(); + const int mult_ = mult * 30; const int sz = data.size(); @@ -103,7 +104,6 @@ void Map::reload() { const float 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 * 30; const int end = std::min(sz, p2.x() * mult_); const int start = p1.x() * mult; -- cgit v1.2.3