summaryrefslogtreecommitdiffhomepage
path: root/spline-widget/spline.hpp
blob: 5b0b3afcc0486ebeab2a88b637f700ca38495bd7 (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
/* 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.
 */

#pragma once

#include <QObject>
#include <QPointF>
#include <QString>
#include <QMutex>
#include <vector>
#include <limits>
#include <memory>
#include "compat/qcopyable-mutex.hpp"
#include "options/options.hpp"
using namespace options;

#ifdef BUILD_spline_widget
#   define SPLINE_WIDGET_EXPORT Q_DECL_EXPORT
#else
#   define SPLINE_WIDGET_EXPORT Q_DECL_IMPORT
#endif

class SPLINE_WIDGET_EXPORT spline final
{
private:
    struct settings
    {
        bundle b;
        value<QList<QPointF>> points;
        settings(bundle b) :
            b(b),
            points(b, "points", QList<QPointF>())
        {}
    };

    ptr<settings> s;

    std::vector<float> data;
    using interp_data_t = decltype(data);

    static constexpr int value_count = 10000;

    int precision(const QList<QPointF>& points) const;
    void update_interp_data();
    float getValueInternal(int x);
    void add_lone_point();
    static bool sort_fn(const QPointF& one, const QPointF& two);

    static QPointF ensure_in_bounds(const QList<QPointF>& points, int i);

    MyMutex _mutex;
    QPointF last_input_value;
    qreal max_x, max_y;
    volatile bool activep;

    template<typename t, typename u, typename w>
    static inline auto clamp(t val, u min, w max) -> decltype (val * min * max)
    {
        if (val > max)
            return max;
        if (val < min)
            return min;
        return val;
    }

public:
    void reload();
    void save(QSettings& s);
    void save();
    void set_bundle(bundle b);

    qreal maxInput() const;
    qreal maxOutput() const;
    spline();
    spline(qreal maxx, qreal maxy, const QString& name);

    float getValue(double x);
    bool getLastPoint(QPointF& point);
    void removePoint(int i);
    void removeAllPoints();

    void addPoint(QPointF pt);
    void movePoint(int idx, QPointF pt);
    QList<QPointF> getPoints() const;
    void setMaxInput(qreal MaxInput);
    void setMaxOutput(qreal MaxOutput);

    void setTrackingActive(bool blnActive);
    bundle get_bundle() { return s->b; }

    using points_t = decltype(s->points.get());
};