-
Notifications
You must be signed in to change notification settings - Fork 0
/
scatterdatamodifier.cpp
196 lines (170 loc) · 5.87 KB
/
scatterdatamodifier.cpp
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
#include "scatterdatamodifier.h"
#include <QtDataVisualization/qscatterdataproxy.h>
#include <QtDataVisualization/qvalue3daxis.h>
#include <QtDataVisualization/q3dscene.h>
#include <QtDataVisualization/q3dcamera.h>
#include <QtDataVisualization/qscatter3dseries.h>
#include <QtDataVisualization/q3dtheme.h>
#include <QtCore/qmath.h>
#include <QtCore/qrandom.h>
#include <QtWidgets/qcombobox.h>
using namespace Qt::StringLiterals;
// #define RANDOM_SCATTER // Uncomment this to switch to random scatter
const int numberOfItems = 10000;
const float curveDivider = 7.5f;
const int lowerNumberOfItems = 900;
const float lowerCurveDivider = 0.75f;
ScatterDataModifier::ScatterDataModifier(Q3DScatter *scatter, QObject *parent) :
QObject(parent),
m_graph(scatter),
m_itemCount(lowerNumberOfItems),
m_curveDivider(lowerCurveDivider),
//! [7]
m_inputHandler(new AxesInputHandler(scatter))
//! [7]
{
//! [0]
m_graph->activeTheme()->setType(Q3DTheme::ThemeStoneMoss);
m_graph->setShadowQuality(QAbstract3DGraph::ShadowQualitySoftHigh);
m_graph->scene()->activeCamera()->setCameraPreset(Q3DCamera::CameraPresetFront);
m_graph->scene()->activeCamera()->setZoomLevel(100.f);
//! [0]
//! [1]
auto *proxy = new QScatterDataProxy;
auto *series = new QScatter3DSeries(proxy);
series->setItemLabelFormat(u"@xTitle: @xLabel @yTitle: @yLabel @zTitle: @zLabel"_s);
series->setMeshSmooth(m_smooth);
m_graph->addSeries(series);
//! [1]
//! [8]
// Give ownership of the handler to the graph and make it the active handler
m_graph->setActiveInputHandler(m_inputHandler);
//! [8]
//! [9]
// Give our axes to the input handler
m_inputHandler->setAxes(m_graph->axisX(), m_graph->axisZ(), m_graph->axisY());
//! [9]
//! [2]
addData();
//! [2]
}
void ScatterDataModifier::addData()
{
// Configure the axes according to the data
//! [3]
m_graph->axisX()->setTitle("X");
m_graph->axisY()->setTitle("Y");
m_graph->axisZ()->setTitle("Z");
//! [3]
//! [4]
auto *dataArray = new QScatterDataArray;
dataArray->reserve(m_itemCount);
//! [4]
m_graph->seriesList().at(0)->dataProxy()->addItem(QScatterDataItem(randVector()));
/*
#ifdef RANDOM_SCATTER
for (int i = 0; i < m_itemCount; ++i)
dataArray->append(QScatterDataItem(randVector()));
#else
//! [5]
const float limit = qSqrt(m_itemCount) / 2.0f;
for (int i = -limit; i < limit; ++i) {
for (int j = -limit; j < limit; ++j) {
const float x = float(i) + 0.5f;
const float y = qCos(qDegreesToRadians(float(i * j) / m_curveDivider));
const float z = float(j) + 0.5f;
dataArray->append(QScatterDataItem({x, y, z}));
}
}
//! [5]
#endif
//! [6]
m_graph->seriesList().at(0)->dataProxy()->resetArray(dataArray);
//! [6]
*/
}
void ScatterDataModifier::changeStyle(int style)
{
QComboBox *comboBox = qobject_cast<QComboBox *>(sender());
if (comboBox) {
m_style = comboBox->itemData(style).value<QAbstract3DSeries::Mesh>();
if (!m_graph->seriesList().isEmpty())
m_graph->seriesList().at(0)->setMesh(m_style);
}
}
void ScatterDataModifier::setSmoothDots(int smooth)
{
m_smooth = bool(smooth);
QScatter3DSeries *series = m_graph->seriesList().at(0);
series->setMeshSmooth(m_smooth);
}
void ScatterDataModifier::changeTheme(int theme)
{
Q3DTheme *currentTheme = m_graph->activeTheme();
currentTheme->setType(Q3DTheme::Theme(theme));
emit backgroundEnabledChanged(currentTheme->isBackgroundEnabled());
emit gridEnabledChanged(currentTheme->isGridEnabled());
}
void ScatterDataModifier::changePresetCamera()
{
static int preset = Q3DCamera::CameraPresetFrontLow;
m_graph->scene()->activeCamera()->setCameraPreset((Q3DCamera::CameraPreset)preset);
if (++preset > Q3DCamera::CameraPresetDirectlyBelow)
preset = Q3DCamera::CameraPresetFrontLow;
}
void ScatterDataModifier::shadowQualityUpdatedByVisual(QAbstract3DGraph::ShadowQuality sq)
{
int quality = int(sq);
emit shadowQualityChanged(quality); // connected to a checkbox in scattergraph.cpp
}
void ScatterDataModifier::changeShadowQuality(int quality)
{
QAbstract3DGraph::ShadowQuality sq = QAbstract3DGraph::ShadowQuality(quality);
m_graph->setShadowQuality(sq);
}
void ScatterDataModifier::setBackgroundEnabled(int enabled)
{
m_graph->activeTheme()->setBackgroundEnabled(enabled == Qt::Checked);
}
void ScatterDataModifier::setGridEnabled(int enabled)
{
m_graph->activeTheme()->setGridEnabled(enabled == Qt::Checked);
}
void ScatterDataModifier::toggleItemCount()
{
if (m_itemCount == numberOfItems) {
m_itemCount = lowerNumberOfItems;
m_curveDivider = lowerCurveDivider;
} else {
m_itemCount = numberOfItems;
m_curveDivider = curveDivider;
}
m_graph->seriesList().at(0)->dataProxy()->resetArray(0);
addData();
}
void ScatterDataModifier::toggleRanges()
{
if (!m_autoAdjust) {
m_graph->axisX()->setAutoAdjustRange(true);
m_graph->axisZ()->setAutoAdjustRange(true);
m_inputHandler->setDragSpeedModifier(1.5f);
m_autoAdjust = true;
} else {
m_graph->axisX()->setRange(-10.0f, 10.0f);
m_graph->axisZ()->setRange(-10.0f, 10.0f);
m_inputHandler->setDragSpeedModifier(15.0f);
m_autoAdjust = false;
}
}
QVector3D ScatterDataModifier::randVector()
{
auto *generator = QRandomGenerator::global();
const auto x = float(generator->bounded(100)) / 2.0f - float(generator->bounded(100)) / 2.0f;
const auto y = float(generator->bounded(100)) / 100.0f - float(generator->bounded(100)) / 100.0f;
const auto z = float(generator->bounded(100)) / 2.0f - float(generator->bounded(100)) / 2.0f;
return {x, y, z};
}
void ScatterDataModifier::toggleTimeStamp(qreal time) {
qDebug() << "Toggling";
return;
}