aboutsummaryrefslogtreecommitdiff
path: root/scatterplot.h
blob: b7982bfc339d9861f93d21404ef13b68086674d6 (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
#ifndef SCATTERPLOT_H
#define SCATTERPLOT_H

#include <memory>
#include <vector>

#include <armadillo>
#include <glad/gl.h>
#include <nod.hpp>

#include "colorscale.h"
#include "quadtree.h"
#include "geometry.h"
#include "scale.h"
#include "shader.h"

class Scatterplot
{
public:
    static const int PADDING = 20;

    Scatterplot();
    ~Scatterplot();

    arma::mat XY() const;
    void setColormap(GLuint texture);
    void setAutoScale(bool autoScale);

    size_t width() const { return m_width; }
    size_t height() const { return m_height; }
    void setSize(size_t, size_t);

    float glyphSize() const { return m_glyphSize; }
    void setGlyphSize(float glyphSize);

    void setDragEnabled(bool enabled) { m_dragEnabled = enabled; }
    bool isDragEnabled() const { return m_dragEnabled; }

    GLuint texture() const { return m_outTex; }

    nod::signal<void(const arma::mat &)> xyChanged, xyInteractivelyChanged;
    nod::signal<void(const arma::vec &)> colorDataChanged, opacityDataChanged;
    nod::signal<void(const std::vector<bool> &)> selectionChanged, selectionInteractivelyChanged;
    nod::signal<void(int)> itemBrushed, itemInteractivelyBrushed;
    nod::signal<void(const LinearScale<float> &, const LinearScale<float> &)> scaleChanged;
    nod::signal<void(float)> glyphSizeChanged;

    void setXY(const arma::mat &xy);
    void setColorData(const arma::vec &colorData);
    void setOpacityData(const arma::vec &opacityData);
    void setScale(const LinearScale<float> &sx, const LinearScale<float> &sy);
    void setSelection(const std::vector<bool> &selection);
    void brushItem(int item);
    void update();
    void draw();

protected:
    // QSGNode *updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *);
    // void mousePressEvent(QMouseEvent *event);
    // void mouseMoveEvent(QMouseEvent *event);
    // void mouseReleaseEvent(QMouseEvent *event);

    // void hoverEnterEvent(QHoverEvent *event);
    // void hoverMoveEvent(QHoverEvent *event);
    // void hoverLeaveEvent(QHoverEvent *event);

private:
    // QSGNode *newSceneGraph();
    // QSGNode *newGlyphTree();

    void applyManipulation();
    void updateGeometry();
    void updateMaterials();
    void updateTransform();
    // void updateGlyphs(QSGNode *node);
    // void updateBrush(QSGNode *node);

    // Data
    arma::mat m_xy;
    arma::vec m_colorData;
    arma::vec m_opacityData;

    // Visuals
    size_t m_width, m_height;
    float m_glyphSize;
    std::shared_ptr<const ColorScale> m_colorScale;

    void autoScale();
    bool m_autoScale;
    LinearScale<float> m_sx, m_sy;

    // Internal state
    void interactiveSelection(bool mergeSelection);
    std::vector<bool> m_selection;
    bool m_anySelected;
    int m_brushedItem;

    enum State {
        StateNone,
        StateBrushing,
        StateSelecting,
        StateSelected,
        StateMoving
    } m_interactionState;
    bool m_dragEnabled;

    std::unique_ptr<Shader> m_shader, m_shaderOutline;
    GLuint m_VAO, m_pointsVBO, m_valuesVBO;
    GLuint m_FBO, m_outFBO, m_colormapTex, m_depthTex, m_tex, m_outTex;
    GLfloat m_transform[4][4];
    std::vector<vec2> m_points;

    vec2 m_dragOriginPos, m_dragCurrentPos;
    bool m_redraw, m_shouldUpdateGeometry, m_shouldUpdateMaterials;

    std::unique_ptr<QuadTree> m_quadtree;
    void updateQuadTree();
};

#endif // SCATTERPLOT_H