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
|
#include "scatterplot.h"
#include <cmath>
static const qreal GLYPH_OPACITY = 0.3;
static const qreal GLYPH_OPACITY_SELECTED = 1.0;
static const QColor SELECTION_COLOR(QColor(128, 128, 128, 96));
static const int GLYPH_SIZE = 8;
static const float PADDING = 10;
static const float PI = 3.1415f;
Scatterplot::Scatterplot(QQuickItem *parent)
: QQuickItem(parent)
, m_colorScale{
QColor("#1f77b4"),
QColor("#ff7f0e"),
QColor("#2ca02c"),
QColor("#d62728"),
QColor("#9467bd"),
QColor("#8c564b"),
QColor("#e377c2"),
QColor("#17becf"),
QColor("#7f7f7f"),
}
{
setClip(true);
setFlag(QQuickItem::ItemHasContents);
}
Scatterplot::~Scatterplot()
{
}
void Scatterplot::setData(const arma::mat &data)
{
if (data.n_cols != 3)
return;
m_data = data;
m_xmin = data.col(0).min();
m_xmax = data.col(0).max();
m_ymin = data.col(1).min();
m_ymax = data.col(1).max();
m_colorScale.setExtents(m_data.col(2).min(), m_data.col(2).max());
m_selectedGlyphs.clear();
update();
}
int calculateCircleVertexCount(qreal radius)
{
// 10 * sqrt(r) \approx 2*pi / acos(1 - 1 / (4*r))
return (int) (10.0 * sqrt(radius));
}
void updateCircleGeometry(QSGGeometry *geometry, float size, float cx, float cy)
{
int vertexCount = geometry->vertexCount();
float theta = 2 * PI / float(vertexCount);
float c = cosf(theta);
float s = sinf(theta);
float x = size / 2;
float y = 0;
QSGGeometry::Point2D *vertexData = geometry->vertexDataAsPoint2D();
for (int i = 0; i < vertexCount; i++) {
vertexData[i].set(x + cx, y + cy);
float t = x;
x = c*x - s*y;
y = s*t + c*y;
}
}
void updateSquareGeometry(QSGGeometry *geometry, float size, float cx, float cy)
{
float r = size / 2;
QSGGeometry::Point2D *vertexData = geometry->vertexDataAsPoint2D();
vertexData[0].set(cx - r, cy - r);
vertexData[1].set(cx + r, cy - r);
vertexData[2].set(cx + r, cy + r);
vertexData[3].set(cx - r, cy + r);
}
float Scatterplot::fromDataXToScreenX(float x)
{
return PADDING + (x - m_xmin) / (m_xmax - m_xmin) * (width() - 2*PADDING);
}
float Scatterplot::fromDataYToScreenY(float y)
{
return PADDING + (y - m_ymin) / (m_ymax - m_ymin) * (height() - 2*PADDING);
}
QSGNode *Scatterplot::newGlyphNodeTree() {
QSGNode *node = new QSGNode;
int vertexCount = calculateCircleVertexCount(GLYPH_SIZE / 2);
for (arma::uword i = 0; i < m_data.n_rows; i++) {
QSGGeometryNode *glyphNode = new QSGGeometryNode;
QSGGeometry *geometry = new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), vertexCount);
geometry->setDrawingMode(GL_POLYGON);
glyphNode->setGeometry(geometry);
glyphNode->setFlag(QSGNode::OwnsGeometry);
QSGFlatColorMaterial *material = new QSGFlatColorMaterial;
material->setColor(m_colorScale.color(m_data(i, 2)));
glyphNode->setMaterial(material);
glyphNode->setFlag(QSGNode::OwnsMaterial);
// Place the glyph geometry node under a opacity node
QSGOpacityNode *glyphOpacityNode = new QSGOpacityNode;
glyphOpacityNode->appendChildNode(glyphNode);
node->appendChildNode(glyphOpacityNode);
}
return node;
}
QSGNode *Scatterplot::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *)
{
if (m_data.n_rows < 1)
return 0;
qreal x, y, xt, yt, moveTranslationF;
QSGNode *root = 0;
if (!oldNode) {
root = new QSGNode;
root->appendChildNode(newGlyphNodeTree());
} else {
root = oldNode;
}
if (m_currentState != INTERACTION_MOVING)
xt = yt = 0;
else {
xt = m_dragCurrentPos.x() - m_dragOriginPos.x();
yt = m_dragCurrentPos.y() - m_dragOriginPos.y();
}
QSGNode *node = root->firstChild()->firstChild();
for (arma::uword i = 0; i < m_data.n_rows; i++) {
arma::rowvec row = m_data.row(i);
bool isSelected = m_selectedGlyphs.contains(i);
QSGOpacityNode *glyphOpacityNode = static_cast<QSGOpacityNode *>(node);
glyphOpacityNode->setOpacity(isSelected ? GLYPH_OPACITY_SELECTED : GLYPH_OPACITY);
QSGGeometryNode *glyphNode = static_cast<QSGGeometryNode *>(node->firstChild());
QSGGeometry *geometry = glyphNode->geometry();
moveTranslationF = isSelected ? 1.0 : 0.0;
x = fromDataXToScreenX(row[0]) + xt * moveTranslationF;
y = fromDataYToScreenY(row[1]) + yt * moveTranslationF;
updateCircleGeometry(geometry, GLYPH_SIZE, x, y);
glyphNode->markDirty(QSGNode::DirtyGeometry);
node = node->nextSibling();
}
// Draw selection
if (m_currentState == INTERACTION_SELECTING) {
QSGSimpleRectNode *selectionNode = 0;
if (!root->firstChild()->nextSibling()) {
selectionNode = new QSGSimpleRectNode;
selectionNode->setColor(SELECTION_COLOR);
root->appendChildNode(selectionNode);
} else {
selectionNode = static_cast<QSGSimpleRectNode *>(root->firstChild()->nextSibling());
}
selectionNode->setRect(QRectF(m_dragOriginPos, m_dragCurrentPos));
selectionNode->markDirty(QSGNode::DirtyGeometry);
} else {
if (root->firstChild()->nextSibling()) {
root->firstChild()->nextSibling()->markDirty(QSGNode::DirtyGeometry);
root->removeChildNode(root->firstChild()->nextSibling());
}
}
return root;
}
void Scatterplot::mousePressEvent(QMouseEvent *event)
{
switch (m_currentState) {
case INTERACTION_NONE:
case INTERACTION_SELECTED:
m_currentState = (event->button() == Qt::MiddleButton) ? INTERACTION_MOVING
: INTERACTION_SELECTING;
m_dragOriginPos = event->localPos();
m_dragCurrentPos = m_dragOriginPos;
break;
case INTERACTION_SELECTING:
case INTERACTION_MOVING:
event->ignore();
return;
}
}
void Scatterplot::mouseMoveEvent(QMouseEvent *event)
{
switch (m_currentState) {
case INTERACTION_SELECTING:
m_dragCurrentPos = event->localPos();
update();
break;
case INTERACTION_MOVING:
m_dragCurrentPos = event->localPos();
updateData();
update();
m_dragOriginPos = m_dragCurrentPos;
break;
case INTERACTION_NONE:
case INTERACTION_SELECTED:
event->ignore();
return;
}
}
void Scatterplot::mouseReleaseEvent(QMouseEvent *event)
{
bool mergeSelection;
switch (m_currentState) {
case INTERACTION_SELECTING:
mergeSelection = (event->button() == Qt::RightButton);
m_currentState = selectGlyphs(mergeSelection) ? INTERACTION_SELECTED
: INTERACTION_NONE;
update();
break;
case INTERACTION_MOVING:
m_currentState = INTERACTION_SELECTED;
update();
break;
case INTERACTION_NONE:
case INTERACTION_SELECTED:
return; // should not be reached
}
}
bool Scatterplot::selectGlyphs(bool mergeSelection)
{
if (!mergeSelection)
m_selectedGlyphs.clear();
qreal x, y;
QRectF selectionRect(m_dragOriginPos, m_dragCurrentPos);
bool anySelected = false;
for (arma::uword i = 0; i < m_data.n_rows; i++) {
arma::rowvec row = m_data.row(i);
x = fromDataXToScreenX(row[0]);
y = fromDataYToScreenY(row[1]);
if (selectionRect.contains(x, y)) {
m_selectedGlyphs.insert(i);
if (!anySelected)
anySelected = true;
}
}
return anySelected;
}
void Scatterplot::updateData()
{
float xt = m_dragCurrentPos.x() - m_dragOriginPos.x();
float yt = m_dragCurrentPos.y() - m_dragOriginPos.y();
xt /= (width() - PADDING);
yt /= (height() - PADDING);
for (auto it = m_selectedGlyphs.cbegin(); it != m_selectedGlyphs.cend(); it++) {
arma::rowvec row = m_data.row(*it);
row[0] = ((row[0] - m_xmin) / (m_xmax - m_xmin) + xt) * (m_xmax - m_xmin) + m_xmin;
row[1] = ((row[1] - m_ymin) / (m_ymax - m_ymin) + yt) * (m_ymax - m_ymin) + m_ymin;
m_data.row(*it) = row;
}
// does not send last column (labels)
emit dataChanged(m_data.cols(0, m_data.n_cols - 2));
}
|