summaryrefslogtreecommitdiffhomepage
path: root/spline-widget/spline.cpp
blob: 8e5ef31b894df6fdf25308018e5291e1c4a81afb (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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/* Copyright (c) 2012-2016, 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.
 */

#include "spline.hpp"
#include <QMutexLocker>
#include <QCoreApplication>
#include <QPointF>
#include <QSettings>
#include <QString>
#include <algorithm>
#include <cmath>

#include <QDebug>

constexpr int spline::value_count;

void spline::setTrackingActive(bool blnActive)
{
    activep = blnActive;
}

spline::spline() : spline(0, 0, "")
{
}

void spline::removeAllPoints()
{
    QMutexLocker l(&_mutex);
    s->points = points_t();
    update_interp_data();
}

void spline::setMaxInput(qreal max_input)
{
    QMutexLocker l(&_mutex);
    max_x = max_input;
}

void spline::setMaxOutput(qreal max_output)
{
    QMutexLocker l(&_mutex);
    max_y = max_output;
}

qreal spline::maxInput() const
{
    QMutexLocker l(&_mutex);
    return max_x;
}

qreal spline::maxOutput() const
{
    QMutexLocker l(&_mutex);
    return max_y;
}

// todo try non-recursive mtx
spline::spline(qreal maxx, qreal maxy, const QString& name) :
    data(value_count, -1.f),
    _mutex(QMutex::Recursive),
    max_x(maxx),
    max_y(maxy),
    activep(false)
{
    set_bundle(options::make_bundle(name));
}

float spline::getValue(double x)
{
    QMutexLocker foo(&_mutex);
    float q  = float(x * precision(s->points));
    int    xi = (int)q;
    float  yi = getValueInternal(xi);
    float  yiplus1 = getValueInternal(xi+1);
    float  f = (q-xi);
    float  ret = yiplus1 * f + yi * (1.0f - f); // at least do a linear interpolation.
    last_input_value.setX(std::fabs(x));
    last_input_value.setY(double(std::fabs(ret)));
    return ret;
}

bool spline::getLastPoint(QPointF& point )
{
    QMutexLocker foo(&_mutex);
    point = last_input_value;
    return activep;
}

float spline::getValueInternal(int x)
{
    float sign = x < 0 ? -1 : 1;
    x = abs(x);
    float ret;
        ret = data[std::min(unsigned(x), unsigned(value_count)-1u)];
    return ret * sign;
}

void spline::add_lone_point()
{
    points_t points;
    points.push_back(QPointF(max_x, max_y));

    s->points = points;
}

QPointF spline::ensure_in_bounds(const QList<QPointF>& points, int i)
{
    const int sz = points.size();

    if (i < 0 || sz == 0)
        return QPointF(0, 0);
    if (i < sz)
        return points[i];
    return points[sz - 1];
}

bool spline::sort_fn(const QPointF& one, const QPointF& two)
{
    return one.x() < two.x();
}

void spline::update_interp_data()
{
    points_t points = s->points;

    if (points.size() == 0)
        points.append(QPointF(max_x, max_y));

    std::stable_sort(points.begin(), points.end(), sort_fn);

    const double mult = precision(points);
    const double mult_ = mult * 30;

    for (unsigned i = 0; i < value_count; i++)
        data[i] = -1;

    if (points.size() == 1)
    {
        const double x = points[0].x();
        const double y = points[0].y();
        const int max = clamp(int(x * precision(points)), 0, value_count-1);
        for (int k = 0; k <= max; k++)
        {
            if (k < value_count)
                data[unsigned(k)] = float(y * k / max);
        }
    }
    else
    {
        if (points[0].x() > 1e-2)
            points.push_front(QPointF(0, 0));

        for (int i = 0; i < points.size(); i++)
        {
            const QPointF p0 = ensure_in_bounds(points, i - 1);
            const QPointF p1 = ensure_in_bounds(points, i);
            const QPointF p2 = ensure_in_bounds(points, i + 1);
            const QPointF p3 = ensure_in_bounds(points, i + 2);

            const double p0_x = p0.x(), p1_x = p1.x(), p2_x = p2.x(), p3_x = p3.x();
            const double p0_y = p0.y(), p1_y = p1.y(), p2_y = p2.y(), p3_y = p3.y();

            // multiplier helps fill in all the x's needed
            const unsigned end = std::min(unsigned(value_count), unsigned(p2_x * mult_));
            const unsigned start = std::max(0u, unsigned(p1_x * mult));

            for (unsigned j = start; j < end; j++)
            {
                const double t = (j - start) / (double) (end - start);
                const double t2 = t*t;
                const double t3 = t*t*t;

                const int x = int(.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 float y = float(.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 < value_count)
                    data[unsigned(x)] = y;
            }
        }
    }

    float last = 0;
    for (unsigned i = 0; i < unsigned(value_count); i++)
    {
        if (data[i] < 0)
            data[i] = last;
        last = data[i];
    }
}

void spline::removePoint(int i)
{
    QMutexLocker foo(&_mutex);

    points_t points = s->points;

    if (i >= 0 && i < points.size())
    {
        points.erase(points.begin() + i);
        s->points = points;
        update_interp_data();
    }
}

void spline::addPoint(QPointF pt)
{
    QMutexLocker foo(&_mutex);

    points_t points = s->points;
    points.push_back(pt);
    std::stable_sort(points.begin(), points.end(), sort_fn);
    s->points = points;
    update_interp_data();
}

void spline::movePoint(int idx, QPointF pt)
{
    QMutexLocker foo(&_mutex);

    points_t points = s->points;

    if (idx >= 0 && idx < points.size())
    {
        points[idx] = pt;
        // we don't allow points to be reordered, but sort due to possible caller logic error
        std::stable_sort(points.begin(), points.end(), sort_fn);
        s->points = points;
        update_interp_data();
    }
}

QList<QPointF> spline::getPoints() const
{
    QMutexLocker foo(&_mutex);
    return s->points;
}

void spline::reload()
{
    if (s && s->b)
        s->b->reload();
}

void spline::save(QSettings& settings)
{
    if (s && s->b)
        s->b->save_deferred(settings);
}

void spline::save()
{
    save(*group::ini_file());
}

void spline::set_bundle(bundle b)
{
    QMutexLocker foo(&_mutex);

    if (b && (!s || s->b != b))
        s = ::make_unique<settings>(b);
    else if (!b)
        s = ::make_unique<settings>(nullptr);

    QList<QPointF> list = s->points;

    const int sz = list.size();

    QList<QPointF> ret_list;
    ret_list.reserve(sz);

    for (int i = 0; i < sz; i++)
    {
        QPointF& pt(list[i]);

        pt = QPointF(clamp(pt.x(), 0, max_x),
                     clamp(pt.y(), 0, max_y));

        const bool overlap = progn(
                                for (int j = 0; j < i; j++)
                                {
                                    QPointF& pt2(list[j]);
                                    const double dist_sq = (pt.x() - pt2.x())*(pt.x() - pt2.x());
                                    if (dist_sq < .33)
                                    {
                                        return true;
                                    }
                                }
                                return false;
                             );
        if (!overlap)
            ret_list.push_back(pt);
    }

    s->points = ret_list;

    last_input_value = QPointF(0, 0);
    activep = false;

    update_interp_data();
}

int spline::precision(const QList<QPointF>& points) const
{
    if (points.size())
        return int(value_count / clamp(points[points.size() - 1].x(), 1, max_x));

    return value_count / clamp(int(max_x), 1, value_count);
}