aboutsummaryrefslogtreecommitdiff
path: root/main.h
blob: 9e7ca525886aba6702a4ef7db7f9e94427c4fe1f (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
#ifndef MAIN_H
#define MAIN_H

#include <QObject>
#include <armadillo>
#include <memory>

#include "colorscale.h"
#include "continuouscolorscale.h"
#include "divergentcolorscale.h"
#include "projectionhistory.h"
#include "numericrange.h"
#include "barchart.h"
#include "colormap.h"
#include "lineplot.h"
#include "scatterplot.h"
#include "voronoisplat.h"

class Main : public QObject
{
    Q_OBJECT
    Q_ENUMS(ObserverType)
    Q_ENUMS(ColorScaleType)
public:
    static Main *instance() {
        // FIXME: Possibly dangerous
        static Main *m = 0;
        if (m == 0) {
            m = new Main();
        }

        return m;
    }

    Q_INVOKABLE bool saveData() const {
        bool ret = true;
        if (m_cp.n_elem > 0 && m_indicesSavePath.size() > 0) {
            ret = ret && m_cp.save(m_cpSavePath, arma::raw_ascii);
        }
        if (m_cpIndices.n_elem > 0 && m_cpSavePath.size() > 0) {
            ret = ret && m_cpIndices.save(m_indicesSavePath, arma::raw_ascii);
        }

        return ret;
    }

    Q_INVOKABLE bool loadDataset(const std::string &dataPath) {
        return m_X.load(dataPath, arma::raw_ascii);
    }

    Q_INVOKABLE void setIndicesSavePath(const std::string &path) {
        m_indicesSavePath = path;
    }
    Q_INVOKABLE void setIndicesSavePath(const QString &path) {
        setIndicesSavePath(path.toStdString());
    }
    Q_INVOKABLE void setCPSavePath(const std::string &path) {
        m_cpSavePath = path;
    }
    Q_INVOKABLE void setCPSavePath(const QString &path) {
        setCPSavePath(path.toStdString());
    }

    arma::mat X() const { return m_X; }

    Q_INVOKABLE void setSelectRPs() {
        cpPlot->setAcceptedMouseButtons(Qt::NoButton);
        cpPlot->setAcceptHoverEvents(false);

        rpPlot->setAcceptedMouseButtons(Qt::LeftButton | Qt::RightButton);
        rpPlot->setAcceptHoverEvents(true);
    }

    Q_INVOKABLE void setSelectCPs() {
        rpPlot->setAcceptedMouseButtons(Qt::NoButton);
        rpPlot->setAcceptHoverEvents(false);

        cpPlot->setAcceptedMouseButtons(Qt::LeftButton | Qt::RightButton);
        cpPlot->setAcceptHoverEvents(true);
    }

    enum ColorScaleType {
        ColorScaleCategorical,
        ColorScaleContinuous,
        ColorScaleDivergent,
        ColorScaleRainbow
    };

    Q_INVOKABLE void setCPColorScale(ColorScaleType colorScaleType) {
        ColorScale *ptr = colorScaleCPs.get();
        float min = ptr != nullptr ? ptr->min() : 0.0f;
        float max = ptr != nullptr ? ptr->max() : 1.0f;

        ptr = getColorScale(colorScaleType);
        ptr->setExtents(min, max);
        colorScaleCPs.reset(ptr);

        cpPlot->setColorScale(colorScaleCPs.get());
        cpBarChart->setColorScale(colorScaleCPs.get());
        cpColormap->setColorScale(colorScaleCPs.get());
        bundlePlot->setColorScale(colorScaleCPs.get());
    }

    Q_INVOKABLE void setRPColorScale(ColorScaleType colorScaleType) {
        ColorScale *ptr = colorScaleCPs.get();
        float min = 0.0f;
        float max = 1.0f;
        if (ptr) {
            min = ptr->min();
            max = ptr->max();
        }
        ptr = getColorScale(colorScaleType);
        ptr->setExtents(min, max);
        colorScaleRPs.reset(ptr);

        rpPlot->setColorScale(colorScaleRPs.get());
        splat->setColorScale(colorScaleRPs.get());
        rpBarChart->setColorScale(colorScaleRPs.get());
        rpColormap->setColorScale(colorScaleRPs.get());
    }

    // Pointers to visual components whose values are set in the main() function
    // after components are instantiated by the QtQuick engine
    BarChart *cpBarChart, *rpBarChart;
    Colormap *cpColormap, *rpColormap;
    Scatterplot *cpPlot, *rpPlot;
    VoronoiSplat *splat;
    LinePlot *bundlePlot;

    // Color scales in use
    std::unique_ptr<ColorScale> colorScaleCPs, colorScaleRPs;

    // Object that controls manipulation history
    ProjectionHistory *projectionHistory;

    Q_INVOKABLE void undoManipulation()  { projectionHistory->undo(); }
    Q_INVOKABLE void resetManipulation() { projectionHistory->reset(); }

    enum ObserverType {
        ObserverCurrent      = ProjectionHistory::ObserverCurrent,
        ObserverDiffPrevious = ProjectionHistory::ObserverDiffPrevious,
        ObserverDiffFirst    = ProjectionHistory::ObserverDiffFirst
    };

    Q_INVOKABLE bool setObserverType(ObserverType observerType) {
        switch (observerType) {
        case ObserverCurrent:
            return projectionHistory->setType(ProjectionHistory::ObserverCurrent);
        case ObserverDiffPrevious:
            return projectionHistory->setType(ProjectionHistory::ObserverDiffPrevious);
        case ObserverDiffFirst:
            return projectionHistory->setType(ProjectionHistory::ObserverDiffFirst);
        }

        return false;
    }

public slots:
    void setCPIndices(const arma::uvec &indices) {
        m_cpIndices = indices;

        m_rpIndices.set_size(m_X.n_rows - m_cpIndices.n_elem);
        NumericRange<arma::uword> allIndices(0, m_X.n_rows);
        std::set_symmetric_difference(allIndices.cbegin(), allIndices.cend(),
                m_cpIndices.cbegin(), m_cpIndices.cend(), m_rpIndices.begin());
    }

    void setCP(const arma::mat &cp) {
        if (cp.n_cols != 2
            || cp.n_rows != m_cpIndices.n_elem) {
            return;
        }

        m_cp = cp;
    }

    void updateMap(const arma::mat &Y) {
        cpPlot->setXY(Y.rows(m_cpIndices));

        const arma::mat &regularPoints = Y.rows(m_rpIndices);
        rpPlot->setXY(regularPoints);
        splat->setSites(regularPoints);
    }

private:
    Main(QObject *parent = 0)
        : QObject(parent)
        , cpBarChart(0)
        , rpBarChart(0)
        , cpColormap(0)
        , rpColormap(0)
        , cpPlot(0)
        , rpPlot(0)
        , splat(0)
        , bundlePlot(0)
        , projectionHistory(0)
    {
    }

    ~Main() {}

    ColorScale *getColorScale(ColorScaleType colorScaleType) {
        switch (colorScaleType) {
        case ColorScaleCategorical:
            return new ColorScale{
                    QColor("#1f77b4"), QColor("#ff7f0e"), QColor("#2ca02c"),
                    QColor("#d62728"), QColor("#9467bd"), QColor("#8c564b"),
                    QColor("#e377c2"), QColor("#17becf"), QColor("#7f7f7f"),
                  };
        case ColorScaleContinuous:
            return ContinuousColorScale::builtin(
                        ContinuousColorScale::HeatedObjects, nullptr);
        case ColorScaleDivergent:
            return DivergentColorScale::builtin(
                        DivergentColorScale::RedGrayBlue, nullptr);
        case ColorScaleRainbow:
            // fall-through
        default:
            return ContinuousColorScale::builtin(
                        ContinuousColorScale::Rainbow, nullptr);
        }
    }

    arma::mat m_X, m_cp;
    arma::uvec m_cpIndices, m_rpIndices;
    std::string m_indicesSavePath, m_cpSavePath;
};

#endif // MAIN_H