{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscenegraph.cpp
More file actions
288 lines (253 loc) · 12.7 KB
/
Copy pathscenegraph.cpp
File metadata and controls
288 lines (253 loc) · 12.7 KB
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
287
288
#include "entities/axis.h"
#include "material.h"
#include "widget.h"
#include <Qt3DExtras/QCuboidMesh>
#include <Qt3DRender/QPointLight>
using namespace Mildred;
//! Creates the scene graph for the display.
/*!
* The Qt3D scene graph is created with the following layout.
*
* [RootEntity] Top-level root entity
* |
* sceneRootEntity_
* | |
* sceneRootTransform_ | Translates to distant rotation point and applies 3D view rotation
* |
* sceneObjectsEntity_ Contains all viewable objects for plotting
* | | |
* sceneObjectsTransform_ | | Places scene objects so that global 0,0,0 is lower left corner to the viewer
* | |
* axesEntity | Contains the individual AxesEntities
* | |
* xAxis_,yAxis_... | Individual axis entities
* |
* dataEntity Parent entity for all displayed data series
* | |
* dataOriginTransform_ * Sets the coordinate system so that 0,0,0 is at the axes origin
*/
void MildredWidget::createSceneGraph()
{
auto *lightEntity = new Qt3DCore::QEntity(rootEntity_.data());
auto *light = new Qt3DRender::QPointLight(lightEntity);
light->setColor("white");
light->setIntensity(1);
lightEntity->addComponent(light);
auto *lightTransform = new Qt3DCore::QTransform(lightEntity);
lightTransform->setTranslation(lightPosition_);
lightEntity->addComponent(lightTransform);
sceneRootEntity_ = new Qt3DCore::QEntity(rootEntity_.data());
sceneRootTransform_ = new Qt3DCore::QTransform(sceneRootEntity_);
sceneRootEntity_->addComponent(sceneRootTransform_);
auto *sceneObjectsEntity_ = new Qt3DCore::QEntity(sceneRootEntity_);
sceneObjectsTransform_ = new Qt3DCore::QTransform(sceneObjectsEntity_);
sceneObjectsEntity_->addComponent(sceneObjectsTransform_);
// Debug
sceneBoundingCuboidEntity_ = new Qt3DCore::QEntity(sceneRootEntity_);
sceneBoundingCuboidEntity_->setEnabled(false);
auto *cuboidMesh = new Qt3DExtras::QCuboidMesh(sceneBoundingCuboidEntity_);
sceneBoundingCuboidEntity_->addComponent(cuboidMesh);
sceneBoundingCuboidTransform_ = new Qt3DCore::QTransform(sceneBoundingCuboidEntity_);
sceneBoundingCuboidEntity_->addComponent(sceneBoundingCuboidTransform_);
auto *cuboidMaterial =
createMaterial(sceneBoundingCuboidEntity_, RenderableMaterial::VertexShaderType::Unclipped,
RenderableMaterial::GeometryShaderType::LineTesselator, RenderableMaterial::FragmentShaderType::Phong);
cuboidMaterial->setAmbient(QColor(255, 0, 0, 255));
sceneBoundingCuboidEntity_->addComponent(cuboidMaterial);
/*
* Axes Leaf
*/
auto *axesEntity = new Qt3DCore::QEntity(sceneObjectsEntity_);
auto *xAxisBarMaterial = createMaterial(axesEntity, RenderableMaterial::VertexShaderType::Unclipped,
RenderableMaterial::GeometryShaderType::LineTesselator,
RenderableMaterial::FragmentShaderType::Monochrome);
xAxisBarMaterial->setAmbient(QColor(0, 0, 0, 255));
auto *xAxisLabelMaterial =
createMaterial(axesEntity, RenderableMaterial::VertexShaderType::Unclipped,
RenderableMaterial::GeometryShaderType::None, RenderableMaterial::FragmentShaderType::Monochrome);
xAxisLabelMaterial->setAmbient(QColor(0, 0, 0, 255));
xAxis_ = new AxisEntity(axesEntity, AxisEntity::AxisType::Horizontal, metrics_, xAxisBarMaterial, xAxisLabelMaterial);
xAxis_->setTitleText("X");
connect(xAxis_, SIGNAL(enabledChanged(bool)), this, SLOT(updateMetrics()));
connect(xAxis_, SIGNAL(rangeChanged()), this, SLOT(updateMetrics()));
auto *yAxisBarMaterial = createMaterial(axesEntity, RenderableMaterial::VertexShaderType::Unclipped,
RenderableMaterial::GeometryShaderType::LineTesselator,
RenderableMaterial::FragmentShaderType::Monochrome);
yAxisBarMaterial->setAmbient(QColor(0, 0, 0, 255));
auto *yAxisLabelMaterial =
createMaterial(axesEntity, RenderableMaterial::VertexShaderType::Unclipped,
RenderableMaterial::GeometryShaderType::None, RenderableMaterial::FragmentShaderType::Monochrome);
yAxisLabelMaterial->setAmbient(QColor(0, 0, 0, 255));
yAxis_ = new AxisEntity(axesEntity, AxisEntity::AxisType::Vertical, metrics_, yAxisBarMaterial, yAxisLabelMaterial);
yAxis_->setTitleText("Y");
connect(yAxis_, SIGNAL(enabledChanged(bool)), this, SLOT(updateMetrics()));
connect(yAxis_, SIGNAL(rangeChanged()), this, SLOT(updateMetrics()));
auto *zAxisBarMaterial = createMaterial(axesEntity, RenderableMaterial::VertexShaderType::Unclipped,
RenderableMaterial::GeometryShaderType::LineTesselator,
RenderableMaterial::FragmentShaderType::Monochrome);
zAxisBarMaterial->setAmbient(QColor(0, 0, 0, 255));
auto *zAxisLabelMaterial =
createMaterial(axesEntity, RenderableMaterial::VertexShaderType::Unclipped,
RenderableMaterial::GeometryShaderType::None, RenderableMaterial::FragmentShaderType::Monochrome);
zAxisLabelMaterial->setAmbient(QColor(0, 0, 0, 255));
zAxis_ = new AxisEntity(axesEntity, AxisEntity::AxisType::Depth, metrics_, zAxisBarMaterial, zAxisLabelMaterial);
zAxis_->setTitleText("Z");
connect(zAxis_, SIGNAL(enabledChanged(bool)), this, SLOT(updateMetrics()));
connect(zAxis_, SIGNAL(rangeChanged()), this, SLOT(updateMetrics()));
zAxis_->setEnabled(false);
/*
* Data Space Leaf
*/
dataRootEntity_ = new Qt3DCore::QEntity(sceneObjectsEntity_);
dataEntityParent_ = new Qt3DCore::QEntity(dataRootEntity_);
dataOriginTransform_ = new Qt3DCore::QTransform(dataEntityParent_);
dataEntityParent_->addComponent(dataOriginTransform_);
// Create mouse coord entity
auto *mouseCoordLabelMaterial =
createMaterial(sceneObjectsEntity_, RenderableMaterial::VertexShaderType::Unclipped,
RenderableMaterial::GeometryShaderType::None, RenderableMaterial::FragmentShaderType::Monochrome);
mouseCoordLabelMaterial->setAmbient(QColor(0, 0, 0, 255));
mouseCoordEntity_ = new TextEntity(sceneObjectsEntity_, QString("0.0, 0.0"));
mouseCoordEntity_->setMaterial(mouseCoordLabelMaterial);
mouseCoordEntity_->setFont(QFont("monospace", 10.0));
mouseCoordEntity_->setAnchorPoint(MildredMetrics::AnchorPoint::BottomLeft);
mouseCoordEntity_->setEnabled(false);
}
//! Convert widget position to 2D (flat) coordinates
QPointF MildredWidget::toAxes2D(QPoint pos) const
{
return {xAxis_->fromScaled((pos.x() - width() / 2.0) - sceneObjectsTransform_->translation().x()),
yAxis_->fromScaled((pos.y() - height() / 2.0) - sceneObjectsTransform_->translation().y())};
}
//! Return screen coordinates at centre of 2D view
QPoint MildredWidget::screen2DCentre() const
{
return {int(width() / 2 + sceneObjectsTransform_->translation().x() + metrics_.displayVolumeExtent().x() / 2.0),
int(height() / 2 + sceneObjectsTransform_->translation().y() + metrics_.displayVolumeExtent().y() / 2.0)};
}
//! Return x axis entity
AxisEntity *MildredWidget::xAxis() { return xAxis_; }
//! Return y axis entity
AxisEntity *MildredWidget::yAxis() { return yAxis_; }
//! Return z axis entity
AxisEntity *MildredWidget::zAxis() { return zAxis_; }
//! Update transforms from metrics
/*!
* The metrics defined in the @class MildredMetrics object contains the offsets in 3D space for the axes origin and general
* graphing volume. This slot is connected internally to signals in the @class MildredMetrics object to ensure correct update of
* the view when the metrics have been recalculated.
*/
void MildredWidget::updateTransforms()
{
if (sceneObjectsTransform_)
sceneObjectsTransform_->setTranslation(metrics_.displayVolumeOrigin() -
QVector3D(width() / 2.0, height() / 2.0, -width() / 2.0));
if (dataOriginTransform_)
dataOriginTransform_->setTranslation(
-(xAxis_->toScaled(xAxis_->minimum()) + yAxis_->toScaled(yAxis_->minimum()) + zAxis_->toScaled(zAxis_->minimum())));
}
//! Update shader parameters
/*!
* Update shader parameters from current view information.
*/
void MildredWidget::updateShaderParameters()
{
sceneDataAxesParameter_->setValue(QMatrix4x4(xAxis_->direction().x(), xAxis_->direction().y(), xAxis_->direction().z(), 0.0,
yAxis_->direction().x(), yAxis_->direction().y(), yAxis_->direction().z(), 0.0,
zAxis_->direction().x(), zAxis_->direction().y(), zAxis_->direction().z(), 0.0,
0.0, 0.0, 0.0, 1.0));
sceneDataAxesExtentsParameter_->setValue(metrics_.displayVolumeExtent());
sceneDataAxesOriginParameter_->setValue(xAxis_->toScaled(xAxis_->minimum()) + yAxis_->toScaled(yAxis_->minimum()) +
zAxis_->toScaled(zAxis_->minimum()));
sceneDataTransformInverseParameter_->setValue(
(sceneRootTransform_->matrix() * sceneObjectsTransform_->matrix() * dataOriginTransform_->matrix()).inverted());
viewportSizeParameter_->setValue(QVector2D(width(), height()));
}
//! Reset view
/*!
* Resets the main scene view rotation to the identity matrix
*/
void MildredWidget::resetView()
{
assert(sceneRootTransform_);
viewRotationMatrix_ = QQuaternion();
sceneRootTransform_->setRotation(viewRotationMatrix_);
updateShaderParameters();
}
//! Show all data in view
/*!
* Reset axis limits to show all visible data
*/
void MildredWidget::showAllData()
{
Cuboid extrema, logarithmicExtrema;
for (auto &[name, entity] : dataEntities_)
{
// Don't adjust extents for hidden data
if (!entity->isEnabled())
continue;
extrema.expand(entity->extrema());
logarithmicExtrema.expand(entity->logarithmicExtrema());
}
// Overwrite linear with log extents for any log axes we have
if (xAxis_->isLogarithmic() && logarithmicExtrema.validXExtent())
extrema.setXExtent(pow(10.0, logarithmicExtrema.lowerLeftBack().x()),
pow(10.0, logarithmicExtrema.upperRightFront().x()));
if (yAxis_->isLogarithmic() && logarithmicExtrema.validYExtent())
extrema.setYExtent(pow(10.0, logarithmicExtrema.lowerLeftBack().y()),
pow(10.0, logarithmicExtrema.upperRightFront().y()));
if (zAxis_->isLogarithmic() && logarithmicExtrema.validZExtent())
extrema.setZExtent(pow(10.0, logarithmicExtrema.lowerLeftBack().z()),
pow(10.0, logarithmicExtrema.upperRightFront().z()));
// Don't allow zero range on any axis
// TODO Checking on absolute value is going to be unsuited to all eventualities
if (!extrema.validXExtent() || extrema.xExtent() < 1.0e-8)
extrema.expandX(1.0);
if (!extrema.validYExtent() || extrema.yExtent() < 1.0e-8)
extrema.expandY(1.0);
if (!extrema.validZExtent() || extrema.zExtent() < 1.0e-8)
extrema.expandZ(1.0);
xAxis_->setLimits(extrema.lowerLeftBack().x(), extrema.upperRightFront().x());
yAxis_->setLimits(extrema.lowerLeftBack().y(), extrema.upperRightFront().y());
zAxis_->setLimits(extrema.lowerLeftBack().z(), extrema.upperRightFront().z());
updateMetrics();
}
//! Change the text of the x-axis title.
/*!
* Set the displayed text of the x-axis title to @param title. Metrics are updated after the change.
*/
void MildredWidget::setXAxisTitle(const QString &title)
{
assert(xAxis_);
xAxis_->setTitleText(title);
updateMetrics();
}
//! Change the text of the y-axis title.
/*!
* Set the displayed text of the y-axis title to @param title. Metrics are updated after the change.
*/
void MildredWidget::setYAxisTitle(const QString &title)
{
assert(yAxis_);
yAxis_->setTitleText(title);
updateMetrics();
}
//! Change the text of the z-axis title.
/*!
* Set the displayed text of the z-axis title to @param title. Metrics are updated after the change.
*/
void MildredWidget::setZAxisTitle(const QString &title)
{
assert(zAxis_);
zAxis_->setTitleText(title);
updateMetrics();
}
//! Change the visibility of the scene debug cuboid.
/*!
* Sets whether the scene debug cuboid is enabled (visible), and which illustrates the current view volume extent.
*/
void MildredWidget::setSceneCuboidEnabled(bool enabled)
{
assert(sceneBoundingCuboidEntity_);
sceneBoundingCuboidEntity_->setEnabled(enabled);
}
You can’t perform that action at this time.
