summaryrefslogtreecommitdiffhomepage
path: root/qfunctionconfigurator
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2015-07-08 05:55:32 +0200
committerStanislaw Halik <sthalik@misaki.pl>2015-07-08 05:55:59 +0200
commit527eef2a2f0e68b286e1b782ba148ecdfafbb89c (patch)
tree2be0f0b96df712f7b12534291db3c36564be62ca /qfunctionconfigurator
parent3a54a111567370ecf903704d702d12736b693a8e (diff)
parentab47eac174db02710a7fa6c194e00c31cef755a4 (diff)
Merge branch 'unstable' into trackhat-ui
Diffstat (limited to 'qfunctionconfigurator')
-rw-r--r--qfunctionconfigurator/functionconfig.cpp65
-rw-r--r--qfunctionconfigurator/functionconfig.h10
-rw-r--r--qfunctionconfigurator/qfunctionconfigurator.cpp61
3 files changed, 87 insertions, 49 deletions
diff --git a/qfunctionconfigurator/functionconfig.cpp b/qfunctionconfigurator/functionconfig.cpp
index 70393246..7edbe0ef 100644
--- a/qfunctionconfigurator/functionconfig.cpp
+++ b/qfunctionconfigurator/functionconfig.cpp
@@ -24,7 +24,7 @@ Map::Map() :
float Map::getValue(float x) {
QMutexLocker foo(&_mutex);
- float q = x * MEMOIZE_PRECISION;
+ float q = x * precision();
int xi = (int)q;
float yi = getValueInternal(xi);
float yiplus1 = getValueInternal(xi+1);
@@ -69,46 +69,56 @@ static bool sortFn(const QPointF& one, const QPointF& two) {
void Map::reload() {
if (cur.input.size())
{
- auto& input = cur.input;
+ qStableSort(cur.input.begin(), cur.input.end(), sortFn);
+
+ QList<QPointF> input = cur.input;
auto& data = cur.data;
-
- qStableSort(input.begin(), input.end(), sortFn);
- data = std::vector<float>(MEMOIZE_PRECISION * input[input.size() - 1].x());
+
+ data = std::vector<float>(value_count);
+ const int mult = precision();
const int sz = data.size();
for (int i = 0; i < sz; i++)
data[i] = -1;
- for (int k = 0; k < input[0].x() * MEMOIZE_PRECISION; k++) {
- if (k < sz)
- data[k] = input[0].y() * k / (input[0].x() * MEMOIZE_PRECISION);
+ 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);
+ }
}
+ 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 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();
- int end = std::min<int>(sz, p2.x() * MEMOIZE_PRECISION);
- int start = p1.x() * MEMOIZE_PRECISION;
+ int end = std::min<int>(sz, p2.x() * mult);
+ int start = p1.x() * mult;
for (int j = start; j < end; j++) {
- double t = (j - start) / (double) (end - start);
- double t2 = t*t;
- double t3 = t*t*t;
+ float t = (j - start) / (float) (end - start);
+ float t2 = t*t;
+ float 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;
- 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)
- * MEMOIZE_PRECISION;
-
- 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);
+ 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;
@@ -118,7 +128,7 @@ void Map::reload() {
float last = 0;
for (int i = 0; i < sz; i++)
{
- if (data[i] <= 0)
+ if (data[i] < 0)
data[i] = last;
last = data[i];
}
@@ -216,3 +226,10 @@ void Map::saveSettings(QSettings& settings, const QString& title) {
settings.endGroup();
}
+
+
+int Map::precision() const {
+ if (cur.input.size())
+ return value_count / std::max<float>(1.f, (cur.input[cur.input.size() - 1].x()));
+ return 1;
+}
diff --git a/qfunctionconfigurator/functionconfig.h b/qfunctionconfigurator/functionconfig.h
index 4ce5efee..f7930cb2 100644
--- a/qfunctionconfigurator/functionconfig.h
+++ b/qfunctionconfigurator/functionconfig.h
@@ -14,7 +14,7 @@
#include <QSettings>
#include <QMutex>
#include <vector>
-#include "../opentrack/qcopyable-mutex.hpp"
+#include "opentrack/qcopyable-mutex.hpp"
class Map {
private:
@@ -22,8 +22,10 @@ private:
QList<QPointF> input;
std::vector<float> data;
};
-
- static constexpr long MEMOIZE_PRECISION = 25;
+
+ static constexpr int value_count = 9001;
+
+ int precision() const;
void reload();
float getValueInternal(int x);
@@ -32,7 +34,7 @@ private:
volatile bool activep;
int max_x;
int max_y;
-
+
State cur, saved;
public:
int maxInput() const { return max_x; }
diff --git a/qfunctionconfigurator/qfunctionconfigurator.cpp b/qfunctionconfigurator/qfunctionconfigurator.cpp
index e1c40396..3d86216e 100644
--- a/qfunctionconfigurator/qfunctionconfigurator.cpp
+++ b/qfunctionconfigurator/qfunctionconfigurator.cpp
@@ -1,4 +1,4 @@
-#include "../opentrack/options.hpp"
+#include "opentrack/options.hpp"
using namespace options;
#include "qfunctionconfigurator/qfunctionconfigurator.h"
#include <QPainter>
@@ -132,7 +132,7 @@ void QFunctionConfigurator::drawFunction()
QPen pen(spline_color, 1.2, Qt::SolidLine);
const double max = _config->maxInput();
- const double step = std::max(.1, max / 300.);
+ const double step = std::max(1e-2, max / 500.);
QPointF prev = point_to_pixel(QPointF(0, 0));
for (double i = 0; i < max; i += step) {
@@ -141,11 +141,6 @@ void QFunctionConfigurator::drawFunction()
drawLine(&painter, prev, cur, pen);
prev = cur;
}
- if (points.size())
- {
- auto last = point_to_pixel(points[points.size()-1]);
- drawLine(&painter, prev, last, pen);
- }
}
void QFunctionConfigurator::paintEvent(QPaintEvent *e)
@@ -259,16 +254,45 @@ void QFunctionConfigurator::mouseMoveEvent(QMouseEvent *e)
bool overlap = false;
- QPointF new_pt = pixel_coord_to_point(e->pos());
+ QPoint pix = e->pos();
+ QPointF new_pt = pixel_coord_to_point(pix);
- if (moving_control_point_idx + 1 < points.size())
- overlap |= new_pt.x() > points[moving_control_point_idx+1].x();
- if (moving_control_point_idx != 0)
- overlap |= new_pt.x() < points[moving_control_point_idx-1].x();
+ static constexpr int limit = 16;
- if (overlap)
- moving_control_point_idx = -1;
- else
+ for (int i = 0; i < 2; i++)
+ {
+ bool bad = false;
+ if (moving_control_point_idx + 1 < points.size())
+ {
+ auto other = points[moving_control_point_idx+1];
+ auto other_pix = point_to_pixel(other);
+ bad = pix.x() + limit > other_pix.x();
+ if (i == 0 && bad)
+ {
+ pix.setX(other_pix.x() - limit - 1);
+ new_pt = pixel_coord_to_point(pix);
+ }
+ else
+ overlap |= bad;
+ }
+ if (moving_control_point_idx != 0)
+ {
+ auto other = points[moving_control_point_idx-1];
+ auto other_pix = point_to_pixel(other);
+ bad = pix.x() - limit < other_pix.x();
+ if (i == 0 && bad)
+ {
+ pix.setX(other_pix.x() + limit + 1);
+ new_pt = pixel_coord_to_point(pix);
+ }
+ else
+ overlap |= bad;
+ }
+ if (!bad)
+ break;
+ }
+
+ if (!overlap)
{
points[moving_control_point_idx] = new_pt;
_config->movePoint(moving_control_point_idx, new_pt);
@@ -299,12 +323,7 @@ void QFunctionConfigurator::mouseReleaseEvent(QMouseEvent *e)
return;
if (e->button() == Qt::LeftButton) {
- QList<QPointF> points = _config->getPoints();
- if (moving_control_point_idx >= 0 && moving_control_point_idx < points.size()) {
- if (_config) {
- _config->movePoint(moving_control_point_idx, pixel_coord_to_point(e->pos()));
- }
- }
+ mouseMoveEvent(e);
setCursor(Qt::ArrowCursor);
moving_control_point_idx = -1;