summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2015-07-26 11:27:54 +0200
committerStanislaw Halik <sthalik@misaki.pl>2015-07-26 11:27:54 +0200
commitf4417361b0fd580855ec84d3c2159162c4426c43 (patch)
tree4faa9903e6e9b0e1a3ccb825405e19d85fcb6112
parenta0d668b148177f597f286ce7dfe01d06e48f97eb (diff)
parent058f823f432484594a816b2489ea2edb2b56d5a4 (diff)
Merge branch 'unstable' into trackhat-ui
-rw-r--r--qfunctionconfigurator/functionconfig.cpp91
-rw-r--r--qfunctionconfigurator/functionconfig.h11
-rw-r--r--qfunctionconfigurator/qfunctionconfigurator.cpp41
-rw-r--r--qfunctionconfigurator/qfunctionconfigurator.h1
4 files changed, 89 insertions, 55 deletions
diff --git a/qfunctionconfigurator/functionconfig.cpp b/qfunctionconfigurator/functionconfig.cpp
index 38a9e703..dc50a4d1 100644
--- a/qfunctionconfigurator/functionconfig.cpp
+++ b/qfunctionconfigurator/functionconfig.cpp
@@ -19,20 +19,26 @@ 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);
- num yiplus1 = getValueInternal(xi+1);
+ num yiplus1 = getValueInternal(xi + (x < 0 ? -1 : 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);
+ 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));
return ret;
}
@@ -44,13 +50,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 +81,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 * integral_max / (input[0].x() * mult) / max_y ;
}
}
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 +111,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];
}
@@ -147,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) {
@@ -162,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
}
}
@@ -173,8 +185,9 @@ const QList<QPointF> Map::getPoints() {
void Map::invalidate_unsaved_settings()
{
+ QMutexLocker foo(&_mutex);
cur = saved;
- reload();
+ lazy_reload = true;
}
void Map::loadSettings(QSettings& settings, const QString& title) {
@@ -203,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;
}
@@ -235,6 +248,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..d49a8f7b 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 num = double;
+ 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;
@@ -37,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; }
@@ -45,6 +49,7 @@ public:
{
setMaxInput(maxx);
setMaxOutput(maxy);
+ lazy_reload = true;
}
num getValue(num x);
@@ -53,7 +58,7 @@ public:
void removeAllPoints() {
QMutexLocker foo(&_mutex);
cur.input.clear();
- reload();
+ lazy_reload = true;
}
void addPoint(QPointF pt);
diff --git a/qfunctionconfigurator/qfunctionconfigurator.cpp b/qfunctionconfigurator/qfunctionconfigurator.cpp
index d058f905..a977af77 100644
--- a/qfunctionconfigurator/qfunctionconfigurator.cpp
+++ b/qfunctionconfigurator/qfunctionconfigurator.cpp
@@ -133,7 +133,7 @@ void QFunctionConfigurator::drawFunction()
QPen pen(spline_color, 1.2, Qt::SolidLine);
const double max = _config->maxInput();
- const double step = std::max(1e-2, max / 500.);
+ const double step = std::max(1e-2, max / 1000.);
QPointF prev = point_to_pixel(QPointF(0, 0));
for (double i = 0; i < max; i += step) {
@@ -175,7 +175,7 @@ void QFunctionConfigurator::paintEvent(QPaintEvent *e)
// This new feature is very handy for tweaking the curves!
QPointF last;
if (_config->getLastPoint(last)) {
- QPointF pixel_pos = point_to_pixel( QPointF(fabs(last.x()), fabs(last.y())) );
+ QPointF pixel_pos = point_to_pixel(last);
drawPoint(&p, pixel_pos, QColor(255, 0, 0, 120));
}
}
@@ -218,7 +218,22 @@ void QFunctionConfigurator::mousePressEvent(QMouseEvent *e)
}
}
if (!bTouchingPoint) {
- _config->addPoint(pixel_coord_to_point(e->pos()));
+ bool too_close = false;
+ const auto pos = e->pos();
+
+ for (int i = 0; i < points.size(); i++)
+ {
+ const QPointF pt = point_to_pixel(points[i]);
+ const auto x = pt.x() - pos.x();
+ if (point_closeness_limit * point_closeness_limit >= x * x)
+ {
+ too_close = true;
+ break;
+ }
+ }
+
+ if (!too_close)
+ _config->addPoint(pixel_coord_to_point(e->pos()));
}
}
}
@@ -258,8 +273,6 @@ void QFunctionConfigurator::mouseMoveEvent(QMouseEvent *e)
QPoint pix = e->pos();
QPointF new_pt = pixel_coord_to_point(pix);
- static constexpr int limit = 16;
-
for (int i = 0; i < 2; i++)
{
bool bad = false;
@@ -267,10 +280,10 @@ void QFunctionConfigurator::mouseMoveEvent(QMouseEvent *e)
{
auto other = points[moving_control_point_idx+1];
auto other_pix = point_to_pixel(other);
- bad = pix.x() + limit > other_pix.x();
+ bad = pix.x() + point_closeness_limit > other_pix.x();
if (i == 0 && bad)
{
- pix.setX(other_pix.x() - limit - 1);
+ pix.setX(other_pix.x() - point_closeness_limit - 1);
new_pt = pixel_coord_to_point(pix);
}
else
@@ -280,10 +293,10 @@ void QFunctionConfigurator::mouseMoveEvent(QMouseEvent *e)
{
auto other = points[moving_control_point_idx-1];
auto other_pix = point_to_pixel(other);
- bad = pix.x() - limit < other_pix.x();
+ bad = pix.x() - point_closeness_limit < other_pix.x();
if (i == 0 && bad)
{
- pix.setX(other_pix.x() + limit + 1);
+ pix.setX(other_pix.x() + point_closeness_limit + 1);
new_pt = pixel_coord_to_point(pix);
}
else
@@ -302,14 +315,16 @@ void QFunctionConfigurator::mouseMoveEvent(QMouseEvent *e)
}
}
else {
- bool bTouchingPoint = false;
+ bool is_on_point = false;
for (int i = 0; i < points.size(); i++) {
- if ( point_within_pixel(points[i], e->pos() ) ) {
- bTouchingPoint = true;
+ const QPoint pos = e->pos();
+ if (point_within_pixel(points[i], pos)) {
+ is_on_point = true;
+ break;
}
}
- if ( bTouchingPoint ) {
+ if (is_on_point) {
setCursor(Qt::OpenHandCursor);
}
else {
diff --git a/qfunctionconfigurator/qfunctionconfigurator.h b/qfunctionconfigurator/qfunctionconfigurator.h
index 5b75d84d..59c8ffeb 100644
--- a/qfunctionconfigurator/qfunctionconfigurator.h
+++ b/qfunctionconfigurator/qfunctionconfigurator.h
@@ -49,6 +49,7 @@ protected:
void resizeEvent(QResizeEvent *) override;
private:
void update_range();
+ static constexpr int point_closeness_limit = 12;
QPointF pixel_coord_to_point (const QPointF& point);
QPointF point_to_pixel (const QPointF& point);