-
-
Notifications
You must be signed in to change notification settings - Fork 383
Expand file tree
/
Copy pathscene_impl.cxx
More file actions
497 lines (429 loc) · 15.8 KB
/
scene_impl.cxx
File metadata and controls
497 lines (429 loc) · 15.8 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
#include "scene_impl.h"
#include "animationManager.h"
#include "interactor_impl.h"
#include "log.h"
#include "options.h"
#include "scene.h"
#include "window_impl.h"
#include "factory.h"
#include "vtkF3DGenericImporter.h"
#include "vtkF3DMemoryMesh.h"
#include "vtkF3DMetaImporter.h"
#include "vtkF3DRenderer.h"
#include <optional>
#include <vtkCallbackCommand.h>
#include <vtkLightCollection.h>
#include <vtkProgressBarRepresentation.h>
#include <vtkProgressBarWidget.h>
#include <vtkTimerLog.h>
#include <vtkVersion.h>
#include <vtksys/SystemTools.hxx>
#include <vector>
namespace fs = std::filesystem;
namespace f3d::detail
{
class scene_impl::internals
{
public:
internals(options& options, window_impl& window)
: Options(options)
, Window(window)
, AnimationManager(options, window)
{
this->MetaImporter->SetRenderWindow(this->Window.GetRenderWindow());
this->Window.SetImporter(this->MetaImporter);
this->AnimationManager.SetImporter(this->MetaImporter);
}
struct ProgressDataStruct
{
vtkTimerLog* timer;
vtkProgressBarWidget* widget;
};
static void CreateProgressRepresentationAndCallback(
ProgressDataStruct* data, vtkImporter* importer, interactor_impl* interactor)
{
vtkNew<vtkCallbackCommand> progressCallback;
progressCallback->SetClientData(data);
progressCallback->SetCallback(
[](vtkObject*, unsigned long, void* clientData, void* callData)
{
auto progressData = static_cast<ProgressDataStruct*>(clientData);
progressData->timer->StopTimer();
vtkProgressBarWidget* widget = progressData->widget;
// Only show and render the progress bar if loading takes more than 0.15 seconds
if (progressData->timer->GetElapsedTime() > 0.15 ||
vtksys::SystemTools::HasEnv("CTEST_F3D_PROGRESS_BAR"))
{
widget->On();
vtkProgressBarRepresentation* rep =
vtkProgressBarRepresentation::SafeDownCast(widget->GetRepresentation());
rep->SetProgressRate(*static_cast<double*>(callData));
widget->Render();
}
});
importer->AddObserver(vtkCommand::ProgressEvent, progressCallback);
interactor->SetInteractorOn(data->widget);
vtkProgressBarRepresentation* progressRep =
vtkProgressBarRepresentation::SafeDownCast(data->widget->GetRepresentation());
progressRep->SetProgressRate(0.0);
progressRep->ProportionalResizeOff();
progressRep->SetPosition(0.0, 0.0);
progressRep->SetPosition2(1.0, 0.0);
progressRep->SetMinimumSize(0, 5);
progressRep->SetProgressBarColor(1, 1, 1);
progressRep->DrawBackgroundOff();
progressRep->DragableOff();
progressRep->SetShowBorderToOff();
progressRep->DrawFrameOff();
progressRep->SetPadding(0.0, 0.0);
data->timer->StartTimer();
}
void Load(const std::vector<vtkSmartPointer<vtkImporter>>& importers)
{
for (const vtkSmartPointer<vtkImporter>& importer : importers)
{
this->MetaImporter->AddImporter(importer);
}
// Initialize the UpVector on load
this->Window.InitializeUpVector();
// Reset temporary up to apply any config values
if (this->Interactor)
{
this->Interactor->ResetTemporaryUp();
}
if (this->Options.scene.camera.index.has_value())
{
this->MetaImporter->SetCameraIndex(this->Options.scene.camera.index.value());
}
// Manage progress bar
vtkNew<vtkProgressBarWidget> progressWidget;
vtkNew<vtkTimerLog> timer;
scene_impl::internals::ProgressDataStruct callbackData;
callbackData.timer = timer;
callbackData.widget = progressWidget;
if (this->Options.ui.loader_progress && this->Interactor)
{
scene_impl::internals::CreateProgressRepresentationAndCallback(
&callbackData, this->MetaImporter, this->Interactor);
}
// Update the meta importer, the will only update importers that have not been updated before
#if VTK_VERSION_NUMBER >= VTK_VERSION_CHECK(9, 3, 20240707)
if (!this->MetaImporter->Update())
{
this->MetaImporter->RemoveObservers(vtkCommand::ProgressEvent);
progressWidget->Off();
this->MetaImporter->Clear();
this->Window.Initialize();
throw scene::load_failure_exception("failed to load scene");
}
#else
this->MetaImporter->Update();
#endif
// Remove anything progress related if any
this->MetaImporter->RemoveObservers(vtkCommand::ProgressEvent);
progressWidget->Off();
// Initialize the animation using temporal information from the importer
this->AnimationManager.Initialize();
// Update all window options and reset camera to bounds if needed
this->Window.UpdateDynamicOptions();
if (!this->Options.scene.camera.index.has_value())
{
this->Window.getCamera().resetToBounds();
}
scene_impl::internals::DisplayAllInfo(this->MetaImporter, this->Window);
}
static void DisplayImporterDescription(log::VerboseLevel level, vtkImporter* importer)
{
vtkIdType availCameras = importer->GetNumberOfCameras();
if (availCameras <= 0)
{
log::print(level, "No camera available");
}
else
{
log::print(level, "Camera(s) available are:");
}
for (int i = 0; i < availCameras; i++)
{
log::print(level, i, ": ", importer->GetCameraName(i));
}
log::print(level, "");
log::print(level, importer->GetOutputsDescription(), "\n");
}
static void DisplayAllInfo(vtkImporter* importer, window_impl& window)
{
// Display output description
scene_impl::internals::DisplayImporterDescription(log::VerboseLevel::DEBUG, importer);
// Display coloring information
window.PrintColoringDescription(log::VerboseLevel::DEBUG);
log::debug("");
// Print scene description
window.PrintSceneDescription(log::VerboseLevel::DEBUG);
}
const options& Options;
window_impl& Window;
interactor_impl* Interactor = nullptr;
animationManager AnimationManager;
vtkNew<vtkF3DMetaImporter> MetaImporter;
};
//----------------------------------------------------------------------------
scene_impl::scene_impl(options& options, window_impl& window)
: Internals(std::make_unique<scene_impl::internals>(options, window))
{
}
//----------------------------------------------------------------------------
scene_impl::~scene_impl() = default;
//----------------------------------------------------------------------------
scene& scene_impl::add(const fs::path& filePath)
{
std::vector<fs::path> paths = { filePath };
return this->add(paths);
}
//----------------------------------------------------------------------------
scene& scene_impl::add(const std::vector<std::string>& filePathStrings)
{
std::vector<fs::path> paths(filePathStrings.size());
std::copy(filePathStrings.begin(), filePathStrings.end(), paths.begin());
return this->add(paths);
}
//----------------------------------------------------------------------------
scene& scene_impl::add(const std::vector<fs::path>& filePaths)
{
if (filePaths.empty())
{
log::debug("No file to load a full scene provided\n");
return *this;
}
std::vector<vtkSmartPointer<vtkImporter>> importers;
for (const fs::path& filePath : filePaths)
{
if (filePath.empty())
{
log::debug("An empty file to load was provided\n");
continue;
}
if (!vtksys::SystemTools::FileExists(filePath.string(), true))
{
throw scene::load_failure_exception(filePath.string() + " does not exists");
}
std::optional<std::string> forceReader = this->Internals->Options.scene.force_reader;
// Recover the importer for the provided file path
const f3d::reader* reader = f3d::factory::instance()->getReader(filePath.string(), forceReader);
if (reader)
{
if (forceReader)
{
log::debug("Forcing reader ", (*forceReader), " for ", filePath.string());
}
else
{
log::debug("Found a reader for \"", filePath.string(), "\" : \"", reader->getName(), "\"");
}
}
else
{
if (forceReader)
{
throw scene::load_failure_exception(*forceReader + " is not a valid force reader");
}
throw scene::load_failure_exception(
filePath.string() + " is not a file of a supported 3D scene file format");
}
vtkSmartPointer<vtkImporter> importer = reader->createSceneReader(filePath.string());
if (!importer)
{
// XXX: F3D Plugin CMake logic ensure there is either a scene reader or a geometry reader
auto vtkReader = reader->createGeometryReader(filePath.string());
assert(vtkReader);
vtkSmartPointer<vtkF3DGenericImporter> genericImporter =
vtkSmartPointer<vtkF3DGenericImporter>::New();
genericImporter->SetInternalReader(vtkReader);
importer = genericImporter;
}
importers.emplace_back(importer);
}
log::debug("\nLoading files: ");
if (filePaths.size() == 1)
{
log::debug(filePaths[0].string());
}
else
{
for (const fs::path& filePathStr : filePaths)
{
log::debug("- ", filePathStr.string());
}
}
log::debug("");
this->Internals->Load(importers);
return *this;
}
//----------------------------------------------------------------------------
scene& scene_impl::add(const double startTime, const double endTime, MeshCallback&& callback)
{
if (startTime > endTime)
{
throw scene::load_failure_exception("startTime must be less than or equal to endTime");
}
vtkNew<vtkF3DMemoryMesh> vtkSource;
auto wrappedCallback = [cb = std::move(callback)](double time, vtkF3DMemoryMesh* vtkMesh)
{
mesh_t mesh = cb(time);
vtkMesh->SetPoints(mesh.points);
vtkMesh->SetNormals(mesh.normals);
vtkMesh->SetTCoords(mesh.texture_coordinates);
vtkMesh->SetFaces(mesh.face_sides, mesh.face_indices);
};
vtkSource->SetAnimatedMesh(startTime, endTime, std::move(wrappedCallback));
auto importer = vtkSmartPointer<vtkF3DGenericImporter>::New();
importer->SetInternalReader(vtkSource);
log::debug("Loading animated 3D scene from memory");
this->Internals->Load({ importer });
return *this;
}
scene& scene_impl::add(const mesh_t& mesh)
{
// sanity checks
auto [valid, err] = mesh.isValid();
if (!valid)
{
throw scene::load_failure_exception(err);
}
vtkNew<vtkF3DMemoryMesh> vtkSource;
auto importer = vtkSmartPointer<vtkF3DGenericImporter>::New();
log::debug("Loading 3D scene from memory");
vtkSource->SetPoints(mesh.points);
vtkSource->SetNormals(mesh.normals);
vtkSource->SetTCoords(mesh.texture_coordinates);
vtkSource->SetFaces(mesh.face_sides, mesh.face_indices);
importer->SetInternalReader(vtkSource);
Internals->Load({ importer });
return *this;
}
//----------------------------------------------------------------------------
scene& scene_impl::clear()
{
// Clear the meta importer from all importers
this->Internals->MetaImporter->Clear();
// Clear the window of all actors
this->Internals->Window.Initialize();
return *this;
}
//----------------------------------------------------------------------------
int scene_impl::addLight(const light_state_t& lightState) const
{
vtkNew<vtkLight> newLight;
newLight->SetLightType(static_cast<int>(lightState.type));
newLight->SetPosition(lightState.position.data());
newLight->SetColor(lightState.color.data());
newLight->SetPositional(lightState.positionalLight);
newLight->SetFocalPoint(lightState.position[0] + lightState.direction[0],
lightState.position[1] + lightState.direction[1],
lightState.position[2] + lightState.direction[2]);
newLight->SetIntensity(lightState.intensity);
newLight->SetSwitch(lightState.switchState);
this->Internals->Window.GetRenderer()->AddLight(newLight);
return this->getLightCount() - 1;
}
//----------------------------------------------------------------------------
int scene_impl::getLightCount() const
{
vtkLightCollection* lc = this->Internals->Window.GetRenderer()->GetLights();
return lc->GetNumberOfItems();
}
//----------------------------------------------------------------------------
light_state_t scene_impl::getLight(int index) const
{
vtkLightCollection* lc = this->Internals->Window.GetRenderer()->GetLights();
vtkLight* light = vtkLight::SafeDownCast(lc->GetItemAsObject(index));
if (!light)
{
throw scene::light_exception("No light at index " + std::to_string(index) + " to get");
}
const double* position = light->GetPosition();
const double* color = light->GetDiffuseColor();
const double* focalPoint = light->GetFocalPoint();
light_state_t lightState;
lightState.type = static_cast<light_type>(light->GetLightType());
lightState.position = { position[0], position[1], position[2] };
lightState.color = { color[0], color[1], color[2] };
lightState.direction = { focalPoint[0] - position[0], focalPoint[1] - position[1],
focalPoint[2] - position[2] };
lightState.positionalLight = light->GetPositional();
lightState.intensity = light->GetIntensity();
lightState.switchState = light->GetSwitch();
return lightState;
}
//----------------------------------------------------------------------------
scene& scene_impl::updateLight(int index, const light_state_t& lightState)
{
vtkLightCollection* lc = this->Internals->Window.GetRenderer()->GetLights();
vtkLight* light = vtkLight::SafeDownCast(lc->GetItemAsObject(index));
if (!light)
{
throw scene::light_exception("No light at index " + std::to_string(index) + " to update");
}
light->SetLightType(static_cast<int>(lightState.type));
light->SetPosition(lightState.position.data());
light->SetColor(lightState.color.data());
light->SetPositional(lightState.positionalLight);
light->SetFocalPoint(lightState.position[0] + lightState.direction[0],
lightState.position[1] + lightState.direction[1],
lightState.position[2] + lightState.direction[2]);
light->SetIntensity(lightState.intensity);
light->SetSwitch(lightState.switchState);
return *this;
}
//----------------------------------------------------------------------------
scene& scene_impl::removeLight(int index)
{
vtkLightCollection* lc = this->Internals->Window.GetRenderer()->GetLights();
vtkLight* light = vtkLight::SafeDownCast(lc->GetItemAsObject(index));
if (!light)
{
throw scene::light_exception("No light at index " + std::to_string(index) + " to remove");
}
this->Internals->Window.GetRenderer()->RemoveLight(light);
return *this;
}
//----------------------------------------------------------------------------
scene& scene_impl::removeAllLights()
{
this->Internals->Window.GetRenderer()->RemoveAllLights();
return *this;
}
//----------------------------------------------------------------------------
bool scene_impl::supports(const fs::path& filePath)
{
return f3d::factory::instance()->getReader(
filePath.string(), this->Internals->Options.scene.force_reader) != nullptr;
}
//----------------------------------------------------------------------------
scene& scene_impl::loadAnimationTime(double timeValue)
{
this->Internals->AnimationManager.LoadAtTime(timeValue);
scene_impl::internals::DisplayAllInfo(this->Internals->MetaImporter, this->Internals->Window);
return *this;
}
//----------------------------------------------------------------------------
std::pair<double, double> scene_impl::animationTimeRange()
{
return this->Internals->AnimationManager.GetTimeRange();
}
//----------------------------------------------------------------------------
unsigned int scene_impl::availableAnimations() const
{
return this->Internals->AnimationManager.GetNumberOfAvailableAnimations();
}
//----------------------------------------------------------------------------
void scene_impl::SetInteractor(interactor_impl* interactor)
{
this->Internals->Interactor = interactor;
this->Internals->AnimationManager.SetInteractor(interactor);
this->Internals->Interactor->SetAnimationManager(&this->Internals->AnimationManager);
}
void scene_impl::PrintImporterDescription(log::VerboseLevel level)
{
scene_impl::internals::DisplayImporterDescription(level, this->Internals->MetaImporter);
}
}