summaryrefslogtreecommitdiffhomepage
path: root/qfunctionconfigurator
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2015-07-26 10:53:33 +0200
committerStanislaw Halik <sthalik@misaki.pl>2015-07-26 10:57:52 +0200
commitff290993484e6708ec57fbc4c4d4eeaaae379c4f (patch)
tree04e77efa8abca30792d92b69c77cf6030c3848b9 /qfunctionconfigurator
parentb34bb2dc35e2d11bd6473a75356a49303bbeb556 (diff)
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.
Diffstat (limited to 'qfunctionconfigurator')
-rw-r--r--qfunctionconfigurator/functionconfig.cpp62
-rw-r--r--qfunctionconfigurator/functionconfig.h6
2 files changed, 38 insertions, 30 deletions
diff --git a/qfunctionconfigurator/functionconfig.cpp b/qfunctionconfigurator/functionconfig.cpp
index b4806e19..31e3641d 100644
--- a/qfunctionconfigurator/functionconfig.cpp
+++ b/qfunctionconfigurator/functionconfig.cpp
@@ -44,13 +44,13 @@ bool Map::getLastPoint(QPointF& point ) {
Map::num Map::getValueInternal(int x) {
num sign = x < 0 ? -1 : 1;
- x = abs(x);
+ x = std::abs(x);
num ret;
int sz = cur.data.size();
if (sz == 0)
ret = 0;
else
- ret = cur.data[std::min<unsigned>(x, sz-1)];
+ ret = cur.data[std::min<unsigned>(x, sz-1)] * max_y / integral_max;
return ret * sign;
}
@@ -75,29 +75,29 @@ void Map::reload() {
QList<QPointF> input = cur.input;
auto& data = cur.data;
- data = std::vector<num>(value_count);
+ data = std::vector<integral>(value_count);
const int mult = precision();
const int sz = data.size();
for (int i = 0; i < sz; i++)
- data[i] = -1;
+ data[i] = integral_max;
if (input.size() == 1)
{
for (int k = 0; k < input[0].x() * mult; k++) {
if (k < sz)
- data[k] = input[0].y() * k / (input[0].x() * mult);
+ data[k] = input[0].y() * k / (input[0].x() * mult) / max_y * integral_max;
}
}
else if (input[0].x() > 1e-2)
input.prepend(QPointF(0, 0));
for (int i = 0; i < sz; i++) {
- QPointF p0 = ensureInBounds(input, i - 1);
- QPointF p1 = ensureInBounds(input, i);
- QPointF p2 = ensureInBounds(input, i + 1);
- QPointF p3 = ensureInBounds(input, i + 2);
+ const QPointF p0 = ensureInBounds(input, i - 1);
+ const QPointF p1 = ensureInBounds(input, i);
+ 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();
@@ -105,35 +105,39 @@ void Map::reload() {
// multiplier helps fill in all the x's needed
const int mult_ = mult * 20;
- int end = std::min<int>(sz, p2.x() * mult_);
- int start = p1.x() * mult;
+ const int end = std::min<int>(sz, p2.x() * mult_);
+ const int start = p1.x() * mult;
const n max = end - start;
for (int j = start; j < end; j++) {
- n t = (j - start) / max;
- n t2 = t*t;
- n t3 = t*t*t;
-
- 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;
+ const n t = (j - start) / max;
+ const n t2 = t*t;
+ const n t3 = t*t*t;
+
+ 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;
- 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;
+ 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<n>(max_y, std::max<n>(y, 0));
+
+ data[x] = y_ / max_y * integral_max;
}
}
num last = 0;
for (int i = 0; i < sz; i++)
{
- if (data[i] < 0)
+ if (data[i] == integral_max)
data[i] = last;
last = data[i];
}
@@ -235,6 +239,6 @@ void Map::saveSettings(QSettings& settings, const QString& title) {
int Map::precision() const {
if (cur.input.size())
- return value_count / std::max<num>(1.f, (cur.input[cur.input.size() - 1].x()));
+ return value_count / std::max<num>(1, (cur.input[cur.input.size() - 1].x()));
return 1;
}
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 <QSettings>
#include <QMutex>
#include <vector>
+#include <limits>
#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<integral>::max();
struct State {
QList<QPointF> input;
- std::vector<num> data;
+ std::vector<integral> data;
};
int precision() const;
@@ -45,6 +48,7 @@ public:
{
setMaxInput(maxx);
setMaxOutput(maxy);
+ reload();
}
num getValue(num x);