-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathAlembicEntity.cpp
More file actions
303 lines (268 loc) · 8.7 KB
/
AlembicEntity.cpp
File metadata and controls
303 lines (268 loc) · 8.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include "AlembicEntity.hpp"
#include "IOThread.hpp"
#include "CameraLocatorEntity.hpp"
#include "PointCloudEntity.hpp"
#include "ObservationsEntity.hpp"
#include <Qt3DRender/QEffect>
#include <Qt3DRender/QTechnique>
#include <Qt3DRender/QRenderPass>
#include <Qt3DRender/QShaderProgram>
#include <Qt3DRender/QObjectPicker>
#include <Qt3DRender/QPickEvent>
#include <Qt3DExtras/QPerVertexColorMaterial>
#include <QFile>
using namespace Alembic::Abc;
using namespace Alembic::AbcGeom;
namespace abcentity
{
AlembicEntity::AlembicEntity(Qt3DCore::QNode* parent)
: Qt3DCore::QEntity(parent)
, _pointSizeParameter(new Qt3DRender::QParameter)
, _ioThread(new IOThread())
{
connect(_ioThread.get(), &IOThread::finished, this, &AlembicEntity::onIOThreadFinished);
createMaterials();
// trigger display repaint events of observations
connect(this, &AlembicEntity::viewIdChanged, this, &AlembicEntity::updateObservations);
connect(this, &AlembicEntity::displayObservationsChanged, this, &AlembicEntity::updateObservations);
connect(this, &AlembicEntity::viewer2DInfoChanged, this, &AlembicEntity::updateObservations);
}
void AlembicEntity::setSource(const QUrl& value)
{
if(_source == value)
return;
_source = value;
loadAbcArchive();
Q_EMIT sourceChanged();
}
void AlembicEntity::setPointSize(const float& value)
{
if(_pointSize == value)
return;
_pointSize = value;
_pointSizeParameter->setValue(value);
_cloudMaterial->setEnabled(_pointSize > 0.0f);
Q_EMIT pointSizeChanged();
}
void AlembicEntity::setLocatorScale(const float& value)
{
if(_locatorScale == value)
return;
_locatorScale = value;
scaleLocators();
Q_EMIT locatorScaleChanged();
}
void AlembicEntity::setViewId(const int& value)
{
if (_viewId == value)
return;
_viewId = value;
Q_EMIT viewIdChanged();
}
void AlembicEntity::setDisplayObservations(const bool& value)
{
if (_displayObservations == value)
return;
_displayObservations = value;
Q_EMIT displayObservationsChanged();
}
void AlembicEntity::setViewer2DInfo(const QVariantMap& value)
{
if (_viewer2DInfo == value)
return;
_viewer2DInfo = value;
Q_EMIT viewer2DInfoChanged();
}
void AlembicEntity::scaleLocators() const
{
for(auto* entity : _cameras)
{
for(auto* transform : entity->findChildren<Qt3DCore::QTransform*>())
transform->setScale(_locatorScale);
}
}
void AlembicEntity::updateObservations() const
{
for (auto* entity : _observations)
{
entity->update(static_cast<aliceVision::IndexT>(_viewId), _viewer2DInfo);
if (entity->isEnabled() != _displayObservations)
entity->setEnabled(_displayObservations);
}
}
// private
void AlembicEntity::createMaterials()
{
using namespace Qt3DRender;
using namespace Qt3DExtras;
_cloudMaterial = new QMaterial(this);
_cameraMaterial = new QPerVertexColorMaterial(this);
// configure cloud material
auto effect = new QEffect;
auto technique = new QTechnique;
auto renderPass = new QRenderPass;
auto shaderProgram = new QShaderProgram;
shaderProgram->setVertexShaderCode(R"(#version 130
in vec3 vertexPosition;
in vec3 vertexColor;
out vec3 color;
uniform mat4 mvp;
uniform mat4 projectionMatrix;
uniform mat4 viewportMatrix;
uniform float pointSize;
void main()
{
color = vertexColor;
gl_Position = mvp * vec4(vertexPosition, 1.0);
gl_PointSize = max(viewportMatrix[1][1] * projectionMatrix[1][1] * pointSize / gl_Position.w, 1.0);
}
)");
// set fragment shader
shaderProgram->setFragmentShaderCode(R"(#version 130
in vec3 color;
out vec4 fragColor;
void main(void)
{
fragColor = vec4(color, 1.0);
}
)");
// add a pointSize uniform
_pointSizeParameter->setName("pointSize");
_pointSizeParameter->setValue(_pointSize);
_cloudMaterial->addParameter(_pointSizeParameter);
// build the material
renderPass->setShaderProgram(shaderProgram);
technique->addRenderPass(renderPass);
effect->addTechnique(technique);
_cloudMaterial->setEffect(effect);
}
void AlembicEntity::clear()
{
// clear entity (remove direct children & all components)
auto entities = findChildren<QEntity*>(QString(), Qt::FindDirectChildrenOnly);
for(auto entity : entities)
{
entity->setParent((QNode*)nullptr);
entity->deleteLater();
}
for(auto& component : components())
removeComponent(component);
_cameras.clear();
_pointClouds.clear();
_observations.clear();
}
// private
void AlembicEntity::loadAbcArchive()
{
clear();
if(_source.isEmpty())
{
setStatus(AlembicEntity::None);
return;
}
setStatus(AlembicEntity::Loading);
_ioThread->read(_source);
}
void AlembicEntity::onIOThreadFinished()
{
const auto& archive = _ioThread->archive();
if(!archive.valid())
{
setStatus(AlembicEntity::Error);
return;
}
// visit the abc tree
try
{
visitAbcObject(archive.getTop(), this);
// store pointers to cameras and point clouds
_cameras = findChildren<CameraLocatorEntity*>();
_pointClouds = findChildren<PointCloudEntity*>();
_observations = findChildren<ObservationsEntity*>();
// perform initial locator scaling
scaleLocators();
updateObservations();
setStatus(AlembicEntity::Ready);
}
catch(...)
{
clear();
setStatus(AlembicEntity::Error);
}
_ioThread->clear();
Q_EMIT camerasChanged();
Q_EMIT pointCloudsChanged();
Q_EMIT observationsChanged();
}
// private
void AlembicEntity::visitAbcObject(const Alembic::Abc::IObject& iObj, QEntity* parent)
{
using namespace Alembic::Abc;
using namespace Alembic::AbcGeom;
const auto createEntities = [&](const IObject&) -> std::vector<BaseAlembicObject*> {
const MetaData& md = iObj.getMetaData();
if(IPoints::matches(md))
{
std::vector<BaseAlembicObject*> entities(2);
IPoints points(iObj, Alembic::Abc::kWrapExisting);
PointCloudEntity* entity0 = new PointCloudEntity(parent);
entity0->setData(iObj);
entity0->addComponent(_cloudMaterial);
entity0->fillArbProperties(points.getSchema().getArbGeomParams());
entity0->fillUserProperties(points.getSchema().getUserProperties());
entities[0] = entity0;
ObservationsEntity* entity1 = new ObservationsEntity(_source.toLocalFile().toStdString(), parent);
entity1->setData();
entity1->fillArbProperties(points.getSchema().getArbGeomParams());
entity1->fillUserProperties(points.getSchema().getUserProperties());
entities[1] = entity1;
return entities;
}
else if(IXform::matches(md))
{
IXform xform(iObj, kWrapExisting);
BaseAlembicObject* entity = new BaseAlembicObject(parent);
XformSample xs;
xform.getSchema().get(xs);
entity->setTransform(xs.getMatrix());
entity->fillArbProperties(xform.getSchema().getArbGeomParams());
entity->fillUserProperties(xform.getSchema().getUserProperties());
return {entity};
}
else if(ICamera::matches(md))
{
ICamera cam(iObj, Alembic::Abc::kWrapExisting);
CameraLocatorEntity* entity = new CameraLocatorEntity(parent);
entity->addComponent(_cameraMaterial);
entity->fillArbProperties(cam.getSchema().getArbGeomParams());
entity->fillUserProperties(cam.getSchema().getUserProperties());
return {entity};
}
else
{
// fallback: create empty object to preserve hierarchy
return {new BaseAlembicObject(parent)};
}
};
if(_skipHidden)
{
// Skip objects with visibilityProperty explicitly set to hidden
const auto& prop = iObj.getProperties();
if(prop.getPropertyHeader(kVisibilityPropertyName))
{
IVisibilityProperty visibilityProperty(prop, kVisibilityPropertyName);
if(ObjectVisibility(visibilityProperty.getValue()) == kVisibilityHidden)
return;
}
}
std::vector<BaseAlembicObject*> entities = createEntities(iObj);
for (size_t j = 0; j < entities.size(); j++)
{
auto entity = entities[j];
entity->setObjectName((iObj.getName() + std::to_string(j)).c_str());
// visit children
for (size_t i = 0; i < iObj.getNumChildren(); i++)
visitAbcObject(iObj.getChild(i), entity);
}
}
} // namespace