From ff290993484e6708ec57fbc4c4d4eeaaae379c4f Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 26 Jul 2015 10:53:33 +0200 Subject: qfc: use integers as storage, not floats Float mantissa has 23 bits. 9 bits get wasted per value. Instead, use uint16_t that doesn't lose much precision, but saves half the space. --- qfunctionconfigurator/functionconfig.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'qfunctionconfigurator/functionconfig.h') diff --git a/qfunctionconfigurator/functionconfig.h b/qfunctionconfigurator/functionconfig.h index 446c73ac..68ec2869 100644 --- a/qfunctionconfigurator/functionconfig.h +++ b/qfunctionconfigurator/functionconfig.h @@ -14,16 +14,19 @@ #include #include #include +#include #include "opentrack-compat/qcopyable-mutex.hpp" class Map { private: static constexpr int value_count = 5000; using num = float; + using integral = std::uint16_t; + static constexpr integral integral_max = std::numeric_limits::max(); struct State { QList input; - std::vector data; + std::vector data; }; int precision() const; @@ -45,6 +48,7 @@ public: { setMaxInput(maxx); setMaxOutput(maxy); + reload(); } num getValue(num x); -- cgit v1.2.3 From de900bb1159af9f85d6f874925648844566363e3 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 26 Jul 2015 11:26:13 +0200 Subject: qfc: reload spline data lazily --- qfunctionconfigurator/functionconfig.cpp | 22 +++++++++++++++------- qfunctionconfigurator/functionconfig.h | 5 +++-- 2 files changed, 18 insertions(+), 9 deletions(-) (limited to 'qfunctionconfigurator/functionconfig.h') diff --git a/qfunctionconfigurator/functionconfig.cpp b/qfunctionconfigurator/functionconfig.cpp index cf2516ca..dc50a4d1 100644 --- a/qfunctionconfigurator/functionconfig.cpp +++ b/qfunctionconfigurator/functionconfig.cpp @@ -19,12 +19,18 @@ Map::Map() : _mutex(QMutex::Recursive), activep(false), max_x(0), - max_y(0) + max_y(0), + lazy_reload(true) { } Map::num Map::getValue(Map::num x) { QMutexLocker foo(&_mutex); + if (lazy_reload) + { + lazy_reload = false; + reload(); + } num q = x * precision(); int xi = (int)q; num yi = getValueInternal(xi); @@ -87,7 +93,7 @@ void Map::reload() { { for (int k = 0; k < input[0].x() * mult; k++) { if (k < sz) - data[k] = input[0].y() * k / (input[0].x() * mult) / max_y * integral_max; + data[k] = input[0].y() * k * integral_max / (input[0].x() * mult) / max_y ; } } else if (input[0].x() > 1e-2) @@ -151,14 +157,15 @@ void Map::removePoint(int i) { if (i >= 0 && i < cur.input.size()) { cur.input.removeAt(i); - reload(); + lazy_reload = true; } } void Map::addPoint(QPointF pt) { QMutexLocker foo(&_mutex); cur.input.append(pt); - reload(); + lazy_reload = true; + qStableSort(cur.input.begin(), cur.input.end(), sortFn); } void Map::movePoint(int idx, QPointF pt) { @@ -166,7 +173,8 @@ void Map::movePoint(int idx, QPointF pt) { if (idx >= 0 && idx < cur.input.size()) { cur.input[idx] = pt; - reload(); + lazy_reload = true; + // we don't allow points to be reodered, so no sort here } } @@ -179,7 +187,7 @@ void Map::invalidate_unsaved_settings() { QMutexLocker foo(&_mutex); cur = saved; - reload(); + lazy_reload = true; } void Map::loadSettings(QSettings& settings, const QString& title) { @@ -208,7 +216,7 @@ void Map::loadSettings(QSettings& settings, const QString& title) { points.append(QPointF(maxInput(), maxOutput())); cur.input = points; - reload(); + lazy_reload = true; saved = cur; } diff --git a/qfunctionconfigurator/functionconfig.h b/qfunctionconfigurator/functionconfig.h index 68ec2869..74e20cf0 100644 --- a/qfunctionconfigurator/functionconfig.h +++ b/qfunctionconfigurator/functionconfig.h @@ -40,6 +40,7 @@ private: double max_y; State cur, saved; + bool lazy_reload; public: double maxInput() const { return max_x; } double maxOutput() const { return max_y; } @@ -48,7 +49,7 @@ public: { setMaxInput(maxx); setMaxOutput(maxy); - reload(); + lazy_reload = true; } num getValue(num x); @@ -57,7 +58,7 @@ public: void removeAllPoints() { QMutexLocker foo(&_mutex); cur.input.clear(); - reload(); + lazy_reload = true; } void addPoint(QPointF pt); -- cgit v1.2.3 From faced34f8133e3a2c0b1f571f0d7a9a64282b280 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 26 Jul 2015 11:27:28 +0200 Subject: qfc: use doubles for intermediate values We could lose precision when multiplying by integral_max with floats. For doubles, we can count on up to 2^56 - 1 to be expressible somewhat exactly. --- qfunctionconfigurator/functionconfig.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'qfunctionconfigurator/functionconfig.h') diff --git a/qfunctionconfigurator/functionconfig.h b/qfunctionconfigurator/functionconfig.h index 74e20cf0..d49a8f7b 100644 --- a/qfunctionconfigurator/functionconfig.h +++ b/qfunctionconfigurator/functionconfig.h @@ -20,7 +20,7 @@ class Map { private: static constexpr int value_count = 5000; - using num = float; + using num = double; using integral = std::uint16_t; static constexpr integral integral_max = std::numeric_limits::max(); -- cgit v1.2.3