summaryrefslogtreecommitdiffhomepage
path: root/qfunctionconfigurator/functionconfig.cpp
blob: c563418cda2c4a7ec4301332fe6f03befdec79bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/* 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 <QCoreApplication>
#include <QPointF>
#include <QList>
#include "functionconfig.h"
#include <QtAlgorithms>
#include <QtAlgorithms>
#include <QSettings>
#include <math.h>
#include <QPixmap>
#include <QDebug>

//
// 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("Abbequerque Inc.", "FaceTrackNoIR");	// Registry settings (in HK_USER)
    QString currentFile = settings.value ( "SettingsFile", QCoreApplication::applicationDirPath() + "/Settings/default.ini" ).toString();
    QSettings iniFile( currentFile, QSettings::IniFormat );
    loadSettings(iniFile);
	reload();
}

//
// 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) {
    int x2 = (int) (std::min<float>(std::max<float>(x, -360), 360) * MEMOIZE_PRECISION);
    _mutex.lock();
    float ret = getValueInternal(x2);
	lastValueTracked.setX(x);
	lastValueTracked.setY(ret);
    _mutex.unlock();
	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 ) {

    _mutex.lock();
	point = lastValueTracked;
    _mutex.unlock();
	
	return _tracking_active;
}

float FunctionConfig::getValueInternal(int x) {
    float sign = x < 0 ? -1 : 1;
	x = x < 0 ? -x : 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;
}

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];
}

static bool sortFn(const QPointF& one, const QPointF& two) {
	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);
        }

       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;
            int start = p1.x() * MEMOIZE_PRECISION;

            for (int j = start; j < end && j < _size; j++) {
                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)
                        * 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;
            }
		}

       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;
}

//
// Remove a Point from the Function.
// Used by the Widget.
//
void FunctionConfig::removePoint(int i) {
    _mutex.lock();
    if (i >= 0 && i < _points.size())
    {
        _points.removeAt(i);
        reload();
    }
    _mutex.unlock();
}

//
// Add a Point to the Function.
// Used by the Widget and by loadSettings.
//
void FunctionConfig::addPoint(QPointF pt) {
    _mutex.lock();
	_points.append(pt);
	reload();
    _mutex.unlock();
}

//
// Move a Function Point.
// Used by the Widget.
//
void FunctionConfig::movePoint(int idx, QPointF pt) {
    _mutex.lock();
    if (idx >= 0 && idx < _points.size())
    {
        _points[idx] = pt;
        reload();
    }
    _mutex.unlock();
}

//
// Return the List of Points.
// Used by the Widget.
//
QList<QPointF> FunctionConfig::getPoints() {
	QList<QPointF> ret;
    _mutex.lock();
	for (int i = 0; i < _points.size(); i++) {
		ret.append(_points[i]);
	}
    _mutex.unlock();
	return ret;
}

//
// 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) {
    QPointF newPoint;

	QList<QPointF> points;
	settings.beginGroup(QString("Curves-%1").arg(_title));
	
    int max = settings.value("point-count", 0).toInt();

    qDebug() << _title << "count" << max;

	for (int i = 0; i < max; i++) {
        newPoint = QPointF(settings.value(QString("point-%1-x").arg(i), (i + 1) * _max_Input/2).toFloat(),
                           settings.value(QString("point-%1-y").arg(i), (i + 1) * _max_Output/2).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);
	}
    settings.endGroup();
    _mutex.lock();
	_points = points;
	reload();
    _mutex.unlock();
}

//
// 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) {
    _mutex.lock();
	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());
    }

    for (int i = max; true; i++)
    {
        QString x = QString("point-%1-x").arg(i);
        if (!settings.contains(x))
            break;
        settings.remove(x);
        settings.remove(QString("point-%1-y").arg(i));
    }
	settings.endGroup();
    _mutex.unlock();
}