diff options
Diffstat (limited to 'qfunctionconfigurator')
-rw-r--r-- | qfunctionconfigurator/functionconfig.cpp | 319 | ||||
-rw-r--r-- | qfunctionconfigurator/functionconfig.h | 90 | ||||
-rw-r--r-- | qfunctionconfigurator/qfunctionconfigurator.cpp | 267 | ||||
-rw-r--r-- | qfunctionconfigurator/qfunctionconfigurator.h | 97 |
4 files changed, 327 insertions, 446 deletions
diff --git a/qfunctionconfigurator/functionconfig.cpp b/qfunctionconfigurator/functionconfig.cpp index 97a6db24..a4d03ed8 100644 --- a/qfunctionconfigurator/functionconfig.cpp +++ b/qfunctionconfigurator/functionconfig.cpp @@ -1,11 +1,3 @@ -/* Copyright (c) 2012, 2013 Stanisław Halik <sthalik@misaki.pl> - - * Permission to use, copy, modify, and/or distribute this - * software for any purpose with or without fee is hereby granted, - * provided that the above copyright notice and this permission - * notice appear in all copies. - */ - #include <QMutexLocker> #include <QCoreApplication> #include <QPointF> @@ -14,259 +6,193 @@ #include <QtAlgorithms> #include <QtAlgorithms> #include <QSettings> -#include <math.h> #include <QPixmap> +#include <algorithm> -// -// Constructor with List of Points in argument. -// -FunctionConfig::FunctionConfig(QString title, int intMaxInput, int intMaxOutput) : - _mutex(QMutex::Recursive) -{ - _title = title; - _points = QList<QPointF>(); - _data = 0; - _size = 0; - lastValueTracked = QPointF(0,0); - _tracking_active = false; - _max_Input = intMaxInput; // Added WVR 20120805 - _max_Output = intMaxOutput; - QSettings settings("opentrack"); // Registry settings (in HK_USER) - QString currentFile = settings.value ( "SettingsFile", QCoreApplication::applicationDirPath() + "/settings/default.ini" ).toString(); - QSettings iniFile( currentFile, QSettings::IniFormat ); - loadSettings(iniFile); - reload(); -} - -void FunctionConfig::setTrackingActive(bool blnActive) +void Map::setTrackingActive(bool blnActive) { - _tracking_active = blnActive; + activep = blnActive; } -FunctionConfig::FunctionConfig() : +Map::Map() : _mutex(QMutex::Recursive), - _data(0), - _size(0), - _tracking_active(false), - _max_Input(0), - _max_Output(0) + activep(false), + max_x(0), + max_y(0) { } -// -// Calculate the value of the function, given the input 'x'. -// Used to draw the curve and, most importantly, to translate input to output. -// The return-value is also stored internally, so the Widget can show the current value, when the Tracker is running. -// -float FunctionConfig::getValue(float x) { +float Map::getValue(float x) { QMutexLocker foo(&_mutex); - int x2 = (int) (std::min<float>(std::max<float>(x, -360), 360) * MEMOIZE_PRECISION); + int x2 = x * (double) MEMOIZE_PRECISION; float ret = getValueInternal(x2); - lastValueTracked.setX(x); - lastValueTracked.setY(ret); - return ret; + last_input_value.setX(x); + last_input_value.setY(ret); + return ret; } -// -// The return-value is also stored internally, so the Widget can show the current value, when the Tracker is running. -// -bool FunctionConfig::getLastPoint(QPointF& point ) { +bool Map::getLastPoint(QPointF& point ) { QMutexLocker foo(&_mutex); - point = lastValueTracked; - return _tracking_active; + point = last_input_value; + return activep; } -float FunctionConfig::getValueInternal(int x) { +float Map::getValueInternal(int x) { float sign = x < 0 ? -1 : 1; - x = x < 0 ? -x : x; + x = std::abs(x); float ret; - if (!_data) - ret = 0; - else if (_size == 0) - ret = 0; - else if (x < 0) - ret = 0; - else if (x < _size && x >= 0) - ret = _data[x]; - else - ret = _data[_size - 1]; - return ret * sign; + int sz = cur.data.size(); + if (sz == 0) + ret = 0; + else + ret = cur.data[std::max(std::min(x, sz-1), 0)]; + return ret * sign; } static __inline QPointF ensureInBounds(QList<QPointF> points, int i) { - int siz = points.size(); - if (siz == 0 || i < 0) - return QPointF(0, 0); - if (siz > i) - return points[i]; - return points[siz - 1]; + int siz = points.size(); + if (siz == 0 || i < 0) + return QPointF(0, 0); + if (siz > i) + return points[i]; + return points[siz - 1]; } static bool sortFn(const QPointF& one, const QPointF& two) { - return one.x() < two.x(); + return one.x() < two.x(); } -void FunctionConfig::reload() { - _size = 0; - - if (_points.size()) - qStableSort(_points.begin(), _points.end(), sortFn); - - if (_data) - delete[] _data; - _data = NULL; - if (_points.size()) { - _data = new float[_size = MEMOIZE_PRECISION * _points[_points.size() - 1].x()]; - - for (int i = 0; i < _size; i++) - _data[i] = -1e6; - - for (int k = 0; k < _points[0].x() * MEMOIZE_PRECISION; k++) { - if (k < _size) - _data[k] = _points[0].y() * k / (_points[0].x() * MEMOIZE_PRECISION); +void Map::reload() { + if (cur.input.size()) + { + auto& input = cur.input; + auto& data = cur.data; + + qStableSort(input.begin(), input.end(), sortFn); + data = std::vector<float>(MEMOIZE_PRECISION * input[input.size() - 1].x()); + + 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); } - - for (int i = 0; i < _points.size(); i++) { - QPointF p0 = ensureInBounds(_points, i - 1); - QPointF p1 = ensureInBounds(_points, i); - QPointF p2 = ensureInBounds(_points, i + 1); - QPointF p3 = ensureInBounds(_points, i + 2); - - int end = p2.x() * MEMOIZE_PRECISION; + + 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); + + int end = std::min<int>(sz, p2.x() * MEMOIZE_PRECISION); int start = p1.x() * MEMOIZE_PRECISION; - - for (int j = start; j < end && j < _size; j++) { + + for (int j = start; j < end; j++) { double t = (j - start) / (double) (end - start); double t2 = t*t; double 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) * 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); - - if (x >= 0 && x < _size) - _data[x] = 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; } - } - - float last = 0; - - for (int i = 0; i < _size; i++) - { - if (_data[i] == -1e6) - _data[i] = last; - last = _data[i]; - } - } -} - -FunctionConfig::~FunctionConfig() { - if (_data) - delete[] _data; + } + + float last = 0; + for (int i = 0; i < sz; i++) + { + if (data[i] <= 0) + data[i] = last; + last = data[i]; + } + } + else + cur.data.clear(); } -// -// Remove a Point from the Function. -// Used by the Widget. -// -void FunctionConfig::removePoint(int i) { +void Map::removePoint(int i) { QMutexLocker foo(&_mutex); - if (i >= 0 && i < _points.size()) + if (i >= 0 && i < cur.input.size()) { - _points.removeAt(i); + cur.input.removeAt(i); reload(); } } -// -// Add a Point to the Function. -// Used by the Widget and by loadSettings. -// -void FunctionConfig::addPoint(QPointF pt) { +void Map::addPoint(QPointF pt) { QMutexLocker foo(&_mutex); - _points.append(pt); - reload(); + cur.input.append(pt); + reload(); } -// -// Move a Function Point. -// Used by the Widget. -// -void FunctionConfig::movePoint(int idx, QPointF pt) { +void Map::movePoint(int idx, QPointF pt) { QMutexLocker foo(&_mutex); - if (idx >= 0 && idx < _points.size()) + if (idx >= 0 && idx < cur.input.size()) { - _points[idx] = pt; + cur.input[idx] = pt; reload(); } } -// -// Return the List of Points. -// Used by the Widget. -// -QList<QPointF> FunctionConfig::getPoints() { - QList<QPointF> ret; +const QList<QPointF> Map::getPoints() { QMutexLocker foo(&_mutex); - for (int i = 0; i < _points.size(); i++) { - ret.append(_points[i]); - } - return ret; + return cur.input; +} + +void Map::invalidate_unsaved_settings() +{ + cur = saved; + reload(); } -// -// Load the Points for the Function from the INI-file designated by settings. -// Settings for a specific Curve are loaded from their own Group in the INI-file. -// -void FunctionConfig::loadSettings(QSettings& settings) { +void Map::loadSettings(QSettings& settings, const QString& title) { QMutexLocker foo(&_mutex); QPointF newPoint; + QList<QPointF> points; + settings.beginGroup(QString("Curves-%1").arg(title)); - QList<QPointF> points; - settings.beginGroup(QString("Curves-%1").arg(_title)); - int max = settings.value("point-count", 0).toInt(); - for (int i = 0; i < max; i++) { - newPoint = QPointF(settings.value(QString("point-%1-x").arg(i), 0).toFloat(), - settings.value(QString("point-%1-y").arg(i), 0).toFloat()); - // - // Make sure the new Point fits in the Function Range. - // Maybe this can be improved? - // - if (newPoint.x() > _max_Input) { - newPoint.setX(_max_Input); - } - if (newPoint.y() > _max_Output) { - newPoint.setY(_max_Output); - } - points.append(newPoint); - } + for (int i = 0; i < max; i++) { + newPoint = QPointF(settings.value(QString("point-%1-x").arg(i), 0).toFloat(), + settings.value(QString("point-%1-y").arg(i), 0).toFloat()); + if (newPoint.x() > max_x) { + newPoint.setX(max_x); + } + if (newPoint.y() > max_y) { + newPoint.setY(max_y); + } + points.append(newPoint); + } + settings.endGroup(); - _points = points; - reload(); + cur.input = points; + reload(); + saved = cur; } -// -// Save the Points for the Function to the INI-file designated by settings. -// Settings for a specific Curve are saved in their own Group in the INI-file. -// The number of Points is also saved, to make loading more convenient. -// -void FunctionConfig::saveSettings(QSettings& settings) { +void Map::saveSettings(QSettings& settings, const QString& title) { QMutexLocker foo(&_mutex); - settings.beginGroup(QString("Curves-%1").arg(_title)); - int max = _points.size(); - settings.setValue("point-count", max); - for (int i = 0; i < max; i++) { - settings.setValue(QString("point-%1-x").arg(i), _points[i].x()); - settings.setValue(QString("point-%1-y").arg(i), _points[i].y()); + settings.beginGroup(QString("Curves-%1").arg(title)); + int max = cur.input.size(); + settings.setValue("point-count", max); + + for (int i = 0; i < max; i++) { + settings.setValue(QString("point-%1-x").arg(i), cur.input[i].x()); + settings.setValue(QString("point-%1-y").arg(i), cur.input[i].y()); } for (int i = max; true; i++) @@ -277,5 +203,8 @@ void FunctionConfig::saveSettings(QSettings& settings) { settings.remove(x); settings.remove(QString("point-%1-y").arg(i)); } - settings.endGroup(); + + saved = cur; + + settings.endGroup(); } diff --git a/qfunctionconfigurator/functionconfig.h b/qfunctionconfigurator/functionconfig.h index 4d771dfd..e24f54cf 100644 --- a/qfunctionconfigurator/functionconfig.h +++ b/qfunctionconfigurator/functionconfig.h @@ -1,4 +1,4 @@ -/* Copyright (c) 2011-2012, Stanislaw Halik <sthalik@misaki.pl> +/* Copyright (c) 2011-2014, Stanislaw Halik <sthalik@misaki.pl> * Permission to use, copy, modify, and/or distribute this * software for any purpose with or without fee is hereby granted, @@ -13,63 +13,61 @@ #include <QString> #include <QSettings> #include <QMutex> -#include "ftnoir_tracker_base/ftnoir_tracker_base.h" +#include <vector> +#include "../facetracknoir/plugin-api.hpp" +#include "../facetracknoir/qcopyable-mutex.hpp" -#define MEMOIZE_PRECISION 100 -class FTNOIR_TRACKER_BASE_EXPORT FunctionConfig { +class OPENTRACK_EXPORT Map { private: - QMutex _mutex; - QList<QPointF> _points; - void reload(); - float* _data; - int _size; - QString _title; + struct State { + QList<QPointF> input; + std::vector<float> data; + }; + + static constexpr long MEMOIZE_PRECISION = 25; + void reload(); float getValueInternal(int x); - QPointF lastValueTracked; // The last input value requested by the Tracker, with it's output-value. - volatile bool _tracking_active; - int _max_Input; - int _max_Output; - FunctionConfig(const FunctionConfig&) = delete; + + MyMutex _mutex; + QPointF last_input_value; + volatile bool activep; + int max_x; + int max_y; + + State cur, saved; public: - int maxInput() const { return _max_Input; } - int maxOutput() const { return _max_Output; } - // - // Contructor(s) and destructor - // - FunctionConfig(); - FunctionConfig(QString title, int intMaxInput, int intMaxOutput); - ~FunctionConfig(); + int maxInput() const { return max_x; } + int maxOutput() const { return max_y; } + Map(); + Map(int maxx, int maxy) + { + setMaxInput(maxx); + setMaxOutput(maxy); + } float getValue(float x); - bool getLastPoint(QPointF& point); // Get the last Point that was requested. - - // - // Functions to manipulate the Function - // - void removePoint(int i); + bool getLastPoint(QPointF& point); + void removePoint(int i); void removeAllPoints() { QMutexLocker foo(&_mutex); - _points.clear(); + cur.input.clear(); reload(); } - void addPoint(QPointF pt); - void movePoint(int idx, QPointF pt); - QList<QPointF> getPoints(); - void setMaxInput(int MaxInput) { - _max_Input = MaxInput; - } - void setMaxOutput(int MaxOutput) { - _max_Output = MaxOutput; - } + void addPoint(QPointF pt); + void movePoint(int idx, QPointF pt); + const QList<QPointF> getPoints(); + void setMaxInput(int MaxInput) { + max_x = MaxInput; + } + void setMaxOutput(int MaxOutput) { + max_y = MaxOutput; + } - // - // Functions to load/save the Function-Points to an INI-file - // - void saveSettings(QSettings& settings); - void loadSettings(QSettings& settings); + void saveSettings(QSettings& settings, const QString& title); + void loadSettings(QSettings& settings, const QString& title); + void invalidate_unsaved_settings(); - void setTrackingActive(bool blnActive); - QString getTitle() { return _title; } + void setTrackingActive(bool blnActive); }; diff --git a/qfunctionconfigurator/qfunctionconfigurator.cpp b/qfunctionconfigurator/qfunctionconfigurator.cpp index 55d1e1bc..94a31be5 100644 --- a/qfunctionconfigurator/qfunctionconfigurator.cpp +++ b/qfunctionconfigurator/qfunctionconfigurator.cpp @@ -1,71 +1,47 @@ -/* Copyright (c) 2011-2012 Stanislaw Halik <sthalik@misaki.pl> - * Adapted to FaceTrackNoIR by Wim Vriend. - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - */ - #include "qfunctionconfigurator/qfunctionconfigurator.h" #include <QPainter> #include <QPaintEvent> -#include <QPainterPathStroker> -#include <QPainterPath> -#include <QBrush> -#include <QFileDialog> #include <QPen> -#include <QMessageBox> -#include <QImage> #include <QPixmap> #include <QTimer> -#include <QtDebug> #include <cmath> -#include <QTabWidget> -#include <QTabBar> -#include <QFontMetrics> +#include <algorithm> static const int pointSize = 5; -QFunctionConfigurator::QFunctionConfigurator(QWidget *parent) - : QWidget(parent) +QFunctionConfigurator::QFunctionConfigurator(QWidget *parent) : + QWidget(parent), + _config(nullptr), + moving_control_point_idx(-1), + _draw_function(true) { - movingPoint = -1; // Index of that same point - _config = 0; - _draw_background = true; - _draw_function = true; update_range(); setMouseTracking(true); } -void QFunctionConfigurator::setConfig(FunctionConfig* config) { - QSettings settings("opentrack"); // Registry settings (in HK_USER) +void QFunctionConfigurator::setConfig(Map* config, const QString& name) { + QSettings settings("opentrack"); QString currentFile = settings.value ( "SettingsFile", QCoreApplication::applicationDirPath() + "/settings/default.ini" ).toString(); - QSettings iniFile( currentFile, QSettings::IniFormat ); // Application settings (in INI-file) + QSettings iniFile( currentFile, QSettings::IniFormat ); - config->loadSettings(iniFile); + config->loadSettings(iniFile, name); _config = config; - _draw_function = _draw_background = true; + _draw_function = true; update_range(); update(); } -void QFunctionConfigurator::saveSettings(QString settingsFile) { - QSettings iniFile( settingsFile, QSettings::IniFormat ); // Application settings (in INI-file) - - if (_config) { - _config->saveSettings(iniFile); - } -} - void QFunctionConfigurator::drawBackground() { if (!_config) return; _background = QPixmap(width(), height()); + QPainter painter(&_background); painter.fillRect(rect(), QColor::fromRgb(204, 204, 204)); - painter.setRenderHint(QPainter::Antialiasing); + QColor bg_color(112, 154, 209); - painter.fillRect(range, bg_color); + painter.fillRect(pixel_bounds, bg_color); QFont font; font.setPointSize(8); @@ -79,61 +55,59 @@ void QFunctionConfigurator::drawBackground() const int maxy = _config->maxOutput(); // horizontal grid - for (int i = 0; i < maxy; i += xstep) { - double y = range.height() - i * c.y() + range.y(); + double y = pixel_bounds.height() - i * c.y() + pixel_bounds.y(); drawLine(&painter, - QPointF(range.x(), y), - QPointF(range.x() + range.width(), y), + QPointF(pixel_bounds.x(), y), + QPointF(pixel_bounds.x() + pixel_bounds.width(), y), pen); painter.drawText(QRectF(10, y - metrics.height()/2, - range.left(), + pixel_bounds.left(), metrics.height()), QString::number(i)); } { const int i = maxy; - double y = range.height() - i * c.y() + range.y(); + double y = pixel_bounds.height() - i * c.y() + pixel_bounds.y(); drawLine(&painter, - QPointF(range.x(), y), - QPointF(range.x() + range.width(), y), + QPointF(pixel_bounds.x(), y), + QPointF(pixel_bounds.x() + pixel_bounds.width(), y), pen); painter.drawText(QRectF(10, y - metrics.height()/2, - range.x() - 10, + pixel_bounds.x() - 10, metrics.height()), QString::number(i)); } // vertical grid - for (int i = 0; i < maxx; i += ystep) { - double x = range.x() + i * c.x(); + double x = pixel_bounds.x() + i * c.x(); drawLine(&painter, - QPointF(x, range.y()), - QPointF(x, range.y() + range.height()), + QPointF(x, pixel_bounds.y()), + QPointF(x, pixel_bounds.y() + pixel_bounds.height()), pen); const QString text = QString::number(i); painter.drawText(QRectF(x - metrics.width(text)/2, - range.height() + 10 + metrics.height(), + pixel_bounds.height() + 10 + metrics.height(), metrics.width(text), metrics.height()), text); } { const int i = maxx; - double x = range.x() + i * c.x(); + double x = pixel_bounds.x() + i * c.x(); drawLine(&painter, - QPointF(x, range.y()), - QPointF(x, range.y() + range.height()), + QPointF(x, pixel_bounds.y()), + QPointF(x, pixel_bounds.y() + pixel_bounds.height()), pen); const QString text = QString::number(i); painter.drawText(QRectF(x - metrics.width(text)/2, - range.height() + 10 + metrics.height(), + pixel_bounds.height() + 10 + metrics.height(), metrics.width(text), metrics.height()), text); @@ -144,102 +118,82 @@ void QFunctionConfigurator::drawFunction() { if (!_config) return; - int i; - QPointF prevPoint; - QPointF currentPoint; _function = QPixmap(_background); QPainter painter(&_function); - - painter.save(); painter.setRenderHint(QPainter::Antialiasing, true); QList<QPointF> points = _config->getPoints(); - for (i = 0; i < points.size(); i++) { - currentPoint = point_to_pixel( points[i] ); // Get the next point and convert it to Widget measures - drawPoint(&painter, currentPoint, QColor(200, 200, 210, 120)); - lastPoint = currentPoint; // Remember which point is the rightmost in the graph + for (int i = 0; i < points.size(); i++) { + drawPoint(&painter, + point_to_pixel(points[i]), + QColor(200, 200, 210, 120)); } + QPen pen(spline_color, 1.2, Qt::SolidLine); - QPen pen(colBezier, 1.2, Qt::SolidLine); - - prevPoint = point_to_pixel( QPointF(0,0) ); // Start at the Axis - double max = _config->maxInput(); + static constexpr double step = 1.02; + const double max = _config->maxInput(); + QPointF prev = point_to_pixel(QPointF(0, 0)); - const double step = 1.01; for (double i = 0; i < max; i += step) { double val = _config->getValue(i); QPointF cur = point_to_pixel(QPointF(i, val)); drawLine(&painter, prev, cur, pen); prev = cur; } - painter.restore(); } void QFunctionConfigurator::paintEvent(QPaintEvent *e) { - QPointF prevPoint; - QPointF currentPoint; - QPointF actualPos; - int i; - QPainter p(this); - p.setRenderHint(QPainter::Antialiasing); - if (_draw_background) { + if (_background.isNull()) drawBackground(); - _draw_background = false; - } - p.drawPixmap(e->rect(), _background); if (_draw_function) { - drawFunction(); // Draw the Function on a Pixmap _draw_function = false; + drawFunction(); } - p.drawPixmap(e->rect(), _function); // Always draw the background and the function + + p.drawPixmap(e->rect(), _function); if (_config) { QPen pen(Qt::white, 1, Qt::SolidLine); QList<QPointF> points = _config->getPoints(); - if (movingPoint >= 0 && movingPoint < points.size()) { - prevPoint = point_to_pixel( QPointF(0,0) ); // Start at the Axis - for (i = 0; i < points.size(); i++) { - currentPoint = point_to_pixel( points[i] ); // Get the next point and convert it to Widget measures - drawLine(&p, prevPoint, currentPoint, pen); - prevPoint = currentPoint; + if (points.size() && moving_control_point_idx >= 0 && moving_control_point_idx < points.size()) { + QPointF prev = points[0]; + for (int i = 1; i < points.size(); i++) { + auto tmp = point_to_pixel(points[i]); + drawLine(&p, prev, tmp, pen); + prev = tmp; } pen.setWidth(1); pen.setColor( Qt::white ); pen.setStyle( Qt::DashLine ); - actualPos = point_to_pixel(points[movingPoint]); - drawLine(&p, QPoint(range.left(), actualPos.y()), QPoint(actualPos.x(), actualPos.y()), pen); - drawLine(&p, QPoint(actualPos.x(), actualPos.y()), QPoint(actualPos.x(), range.height() + range.top()), pen); + QPointF pixel_pos = point_to_pixel(points[moving_control_point_idx]); + drawLine(&p, QPoint(pixel_bounds.left(), pixel_pos.y()), QPoint(pixel_pos.x(), pixel_pos.y()), pen); + drawLine(&p, QPoint(pixel_pos.x(), pixel_pos.y()), QPoint(pixel_pos.x(), pixel_bounds.height() + pixel_bounds.top()), pen); } - // // If the Tracker is active, the 'Last Point' it requested is recorded. // Show that point on the graph, with some lines to assist. // This new feature is very handy for tweaking the curves! - // - if (_config->getLastPoint( currentPoint )) { - actualPos = point_to_pixel( QPointF(fabs(currentPoint.x()), fabs(currentPoint.y())) ); - drawPoint(&p, actualPos, QColor(255, 0, 0, 120)); + QPointF last; + if (_config->getLastPoint(last)) { + QPointF pixel_pos = point_to_pixel( QPointF(fabs(last.x()), fabs(last.y())) ); + drawPoint(&p, pixel_pos, QColor(255, 0, 0, 120)); pen.setWidth(1); pen.setColor( Qt::black ); pen.setStyle( Qt::SolidLine ); - drawLine(&p, QPoint(range.left(), actualPos.y()), QPoint(actualPos.x(), actualPos.y()), pen); - drawLine(&p, QPoint(actualPos.x(), actualPos.y()), QPoint(actualPos.x(), range.width()), pen); + drawLine(&p, QPoint(pixel_bounds.left(), pixel_pos.y()), QPoint(pixel_pos.x(), pixel_pos.y()), pen); + drawLine(&p, QPoint(pixel_pos.x(), pixel_pos.y()), QPoint(pixel_pos.x(), pixel_bounds.width()), pen); } - } } -// -// Draw the handle, to move the Bezier-curve. -// void QFunctionConfigurator::drawPoint(QPainter *painter, const QPointF &pos, QColor colBG ) { painter->save(); @@ -251,7 +205,7 @@ void QFunctionConfigurator::drawPoint(QPainter *painter, const QPointF &pos, QCo painter->restore(); } -void QFunctionConfigurator::drawLine(QPainter *painter, const QPointF &start, const QPointF &end, QPen pen) +void QFunctionConfigurator::drawLine(QPainter *painter, const QPointF &start, const QPointF &end, QPen &pen) { painter->save(); painter->setPen(pen); @@ -267,19 +221,17 @@ void QFunctionConfigurator::mousePressEvent(QMouseEvent *e) QList<QPointF> points = _config->getPoints(); if (e->button() == Qt::LeftButton) { bool bTouchingPoint = false; - movingPoint = -1; + moving_control_point_idx = -1; if (_config) { for (int i = 0; i < points.size(); i++) { - if ( point_within_pixel(points[i], e->pos() ) ) { + if (point_within_pixel(points[i], e->pos())) { bTouchingPoint = true; - movingPoint = i; - timer.restart(); + moving_control_point_idx = i; break; } } if (!bTouchingPoint) { _config->addPoint(pixel_coord_to_point(e->pos())); - emit CurveChanged( true ); } } } @@ -288,7 +240,7 @@ void QFunctionConfigurator::mousePressEvent(QMouseEvent *e) if (_config) { int found_pt = -1; for (int i = 0; i < points.size(); i++) { - if ( point_within_pixel(points[i], e->pos() ) ) { + if (point_within_pixel(points[i], e->pos())) { found_pt = i; break; } @@ -296,9 +248,8 @@ void QFunctionConfigurator::mousePressEvent(QMouseEvent *e) if (found_pt != -1) { _config->removePoint(found_pt); - emit CurveChanged( true ); } - movingPoint = -1; + moving_control_point_idx = -1; } } _draw_function = true; @@ -309,29 +260,43 @@ void QFunctionConfigurator::mouseMoveEvent(QMouseEvent *e) { if (!_config) return; + + static constexpr int min_refresh_delay = 12; + + if (timer.isValid() && timer.elapsed() < min_refresh_delay) + return; + + static constexpr int refresh_delay = 17; QList<QPointF> points = _config->getPoints(); - const int refresh_delay = 50; - if (movingPoint >= 0 && movingPoint < points.size()) { + if (moving_control_point_idx != -1 && moving_control_point_idx < points.size()) { setCursor(Qt::ClosedHandCursor); - - if (timer.isValid() && timer.elapsed() > refresh_delay) + + bool overlap = false; + + QPointF new_pt = pixel_coord_to_point(e->pos()); + + 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(); + + if (overlap) + moving_control_point_idx = -1; + else if (timer.isValid() && timer.elapsed() > refresh_delay) { timer.restart(); - QPointF new_pt = pixel_coord_to_point(e->pos()); - points[movingPoint] = new_pt; - _config->movePoint(movingPoint, new_pt); + points[moving_control_point_idx] = new_pt; + _config->movePoint(moving_control_point_idx, new_pt); _draw_function = true; update(); } } else { bool bTouchingPoint = false; - if (_config) { - for (int i = 0; i < points.size(); i++) { - if ( point_within_pixel(points[i], e->pos() ) ) { - bTouchingPoint = true; - } + for (int i = 0; i < points.size(); i++) { + if ( point_within_pixel(points[i], e->pos() ) ) { + bTouchingPoint = true; } } @@ -348,38 +313,54 @@ void QFunctionConfigurator::mouseReleaseEvent(QMouseEvent *e) { if (!_config) return; - QList<QPointF> points = _config->getPoints(); if (e->button() == Qt::LeftButton) { - timer.invalidate(); - if (movingPoint >= 0 && movingPoint < points.size()) { - emit CurveChanged( true ); + QList<QPointF> points = _config->getPoints(); + if (moving_control_point_idx >= 0 && moving_control_point_idx < points.size()) { if (_config) { - _config->movePoint(movingPoint, pixel_coord_to_point(e->pos())); + _config->movePoint(moving_control_point_idx, pixel_coord_to_point(e->pos())); } } setCursor(Qt::ArrowCursor); - movingPoint = -1; + moving_control_point_idx = -1; + + _draw_function = true; + update(); } +} +void QFunctionConfigurator::update_range() +{ + if (!_config) + return; + + const double w = width(), h = height(); + const double mwl = 40, mhl = 20; + const double mwr = 15, mhr = 35; + + pixel_bounds = QRectF(mwl, mhl, (w - mwl - mwr), (h - mhl - mhr)); + c = QPointF(pixel_bounds.width() / _config->maxInput(), pixel_bounds.height() / _config->maxOutput()); _draw_function = true; - update(); + + _background = QPixmap(); + _function = QPixmap(); } -bool QFunctionConfigurator::point_within_pixel(QPointF pt, QPointF pixel) const +bool QFunctionConfigurator::point_within_pixel(const QPointF &pt, const QPointF &pixel) { - QPointF pixel2(range.x() + pt.x() * c.x(), (range.y() + range.height() - pt.y() * c.y())); + QPointF pixel2(pixel_bounds.x() + pt.x() * c.x(), + (pixel_bounds.y() + pixel_bounds.height() - pt.y() * c.y())); return pixel2.x() >= pixel.x() - pointSize && pixel2.x() < pixel.x() + pointSize && pixel2.y() >= pixel.y() - pointSize && pixel2.y() < pixel.y() + pointSize; } -QPointF QFunctionConfigurator::pixel_coord_to_point(QPointF point) const +QPointF QFunctionConfigurator::pixel_coord_to_point(const QPointF& point) { if (!_config) return QPointF(-1, -1); - double x = (point.x() - range.x()) / c.x(); - double y = (range.height() - point.y() + range.y()) / c.y(); + double x = (point.x() - pixel_bounds.x()) / c.x(); + double y = (pixel_bounds.height() - point.y() + pixel_bounds.y()) / c.y(); if (x < 0) x = 0; @@ -394,20 +375,14 @@ QPointF QFunctionConfigurator::pixel_coord_to_point(QPointF point) const return QPointF(x, y); } -QPointF QFunctionConfigurator::point_to_pixel(QPointF point) const -{ - return QPointF(range.x() + point.x() * c.x(), - range.y() + range.height() - point.y() * c.y()); -} - -void QFunctionConfigurator::setColorBezier(QColor color) +QPointF QFunctionConfigurator::point_to_pixel(const QPointF& point) { - colBezier = color; - update(); + return QPointF(pixel_bounds.x() + point.x() * c.x(), + pixel_bounds.y() + pixel_bounds.height() - point.y() * c.y()); } void QFunctionConfigurator::resizeEvent(QResizeEvent *) { update_range(); - repaint(); + update(); } diff --git a/qfunctionconfigurator/qfunctionconfigurator.h b/qfunctionconfigurator/qfunctionconfigurator.h index 1f6b4f78..229d9977 100644 --- a/qfunctionconfigurator/qfunctionconfigurator.h +++ b/qfunctionconfigurator/qfunctionconfigurator.h @@ -1,92 +1,71 @@ -/* Copyright (c) 2011-2012 Stanislaw Halik <sthalik@misaki.pl> - * Adapted to FaceTrackNoIR by Wim Vriend. +/* Copyright (c) 2011-2014 Stanislaw Halik <sthalik@misaki.pl> + * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. */ -#ifndef QFUNCTIONCONFIGURATOR_H -#define QFUNCTIONCONFIGURATOR_H + +// Adapted to FaceTrackNoIR by Wim Vriend. + +#pragma once #include <QWidget> #include <QtGui> #include <QPointF> #include <QElapsedTimer> #include "qfunctionconfigurator/functionconfig.h" -#include "ftnoir_tracker_base/ftnoir_tracker_base.h" - -// -// The FunctionConfigurator Widget is used to display and configure a function (curve). -// The Function is used by FaceTrackNoIR to 'translate' the actual head-pose to the virtual headpose. Every axis is configured by a separate Function. -// -// The Function is coded in a separate Class and can exists, without the Widget. When the widget is displayed (therefore 'created'), the Function can be attached to the -// Widget and the Widget used to change the Function. -// +#include "facetracknoir/plugin-api.hpp" -class FTNOIR_TRACKER_BASE_EXPORT QFunctionConfigurator : public QWidget +class OPENTRACK_EXPORT QFunctionConfigurator : public QWidget { Q_OBJECT Q_PROPERTY(QColor colorBezier READ colorBezier WRITE setColorBezier) +public: + QFunctionConfigurator(QWidget *parent = 0); + + Map* config(); + void setConfig(Map* config, const QString &name); + QColor colorBezier() const { - return colBezier; + return spline_color; + } + void setColorBezier(QColor color) + { + spline_color = color; + update(); } -public: - QFunctionConfigurator(QWidget *parent = 0); - FunctionConfig* config(); - - void setConfig(FunctionConfig* config); - void saveSettings(QString settingsFile); - -signals: - void CurveChanged(bool); - -public slots: - void setColorBezier(QColor); protected slots: void paintEvent(QPaintEvent *e); void mousePressEvent(QMouseEvent *e); void mouseMoveEvent(QMouseEvent *e); void mouseReleaseEvent(QMouseEvent *e); - -protected: +private: void drawBackground(); void drawFunction(); void drawPoint(QPainter *painter, const QPointF &pt, QColor colBG ); - void drawLine(QPainter *painter, const QPointF &start, const QPointF &end, QPen pen); - bool point_within_pixel(QPointF pt, QPointF pixel) const; - + void drawLine(QPainter *painter, const QPointF &start, const QPointF &end, QPen& pen); + bool point_within_pixel(const QPointF& pt, const QPointF& pixel); protected: - virtual void resizeEvent(QResizeEvent *); - + void resizeEvent(QResizeEvent *) override; private: - void update_range() { - if (!_config) - return; - double w = width(), h = height(); - const double mwl = 40, mhl = 20; - const double mwr = 15, mhr = 35; - range = QRectF(mwl, mhl, (w - mwl - mwr), (h - mhl - mhr)); - c = QPointF(range.width() / _config->maxInput(), range.height() / _config->maxOutput()); - _draw_function = _draw_background = true; - } + void update_range(); - QRectF range; // The actual rectangle for the Bezier-curve - QPointF lastPoint; // The right-most point of the Function - QPointF pixel_coord_to_point (QPointF point) const; // Convert the graphical Point to a real-life Point - QPointF point_to_pixel (QPointF point) const; // Convert the Point to a graphical Point + QPointF pixel_coord_to_point (const QPointF& point); + QPointF point_to_pixel (const QPointF& point); - int movingPoint; + Map* _config; + + // bounds of the rectangle user can interact with + QRectF pixel_bounds; + + int moving_control_point_idx; QElapsedTimer timer; QPointF c; - QColor colBezier; // Color of Bezier curve - - bool _draw_background; // Flag to determine if the background should be (re-)drawn on the QPixmap - QPixmap _background; // Image of the static parts (axis, lines, etc.) - bool _draw_function; // Flag to determine if the function should be (re-)drawn on the QPixmap - QPixmap _function; // Image of the function (static unless edited by the user) - - FunctionConfig* _config; + QColor spline_color; + + QPixmap _background; + QPixmap _function; + bool _draw_function; }; - -#endif // QFUNCTIONCONFIGURATOR_H |