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

#include <QObject>
#include <armadillo>

#include "colorscale.h"
#include "continuouscolorscale.h"
#include "projectionhistory.h"
#include "numericrange.h"
#include "barchart.h"
#include "colormap.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 &path) { return m_dataset.load(path, 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_dataset.cols(0, m_dataset.n_cols - 2); }
    arma::vec labels() const { return m_dataset.col(m_dataset.n_cols - 1); }

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

    // No need to be static: this class is a singleton
    ColorScale COLOR_SCALE_CATEGORICAL;
    ColorScale COLOR_SCALE_CONTINUOUS;
    ColorScale COLOR_SCALE_DIVERGENT;
    ColorScale COLOR_SCALE_RAINBOW;

    Q_INVOKABLE void setCPColorScale(ColorScaleType colorScaleType) {
        ColorScale &scale = getColorScale(colorScaleType);

        cpPlot->setColorScale(scale);
        cpBarChart->setColorScale(scale);
        cpColormap->setColorScale(scale);
    }

    Q_INVOKABLE void setRPColorScale(ColorScaleType colorScaleType) {
        ColorScale &scale = getColorScale(colorScaleType);

        rpPlot->setColorScale(scale);
        splat->setColorScale(scale);
        rpBarChart->setColorScale(scale);
        rpColormap->setColorScale(scale);
    }

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

    // 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_dataset.n_rows - m_cpIndices.n_elem);
        NumericRange<arma::uword> allIndices(0, m_dataset.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)
        , COLOR_SCALE_CATEGORICAL{{
            QColor("#1f77b4"), QColor("#ff7f0e"), QColor("#2ca02c"),
            QColor("#d62728"), QColor("#9467bd"), QColor("#8c564b"),
            QColor("#e377c2"), QColor("#17becf"), QColor("#7f7f7f"),
          }}
        , COLOR_SCALE_CONTINUOUS{ContinuousColorScale::builtin(ContinuousColorScale::HeatedObjects)}
        , COLOR_SCALE_DIVERGENT{ContinuousColorScale::builtin(ContinuousColorScale::RedGrayBlue)}
        , COLOR_SCALE_RAINBOW{ContinuousColorScale::builtin(ContinuousColorScale::Rainbow)}
        , cpBarChart(0)
        , rpBarChart(0)
        , cpColormap(0)
        , rpColormap(0)
        , cpPlot(0)
        , rpPlot(0)
        , splat(0)
        , projectionHistory(0)
    {
    }

    ~Main() {}

    ColorScale &getColorScale(ColorScaleType colorScaleType) {
        switch (colorScaleType) {
        case ColorScaleCategorical:
            return COLOR_SCALE_CATEGORICAL;
        case ColorScaleContinuous:
            return COLOR_SCALE_CONTINUOUS;
        case ColorScaleDivergent:
            return COLOR_SCALE_DIVERGENT;
        case ColorScaleRainbow:
            // fall-through
        default:
            return COLOR_SCALE_RAINBOW;
        }
    }

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

#endif // MAIN_H