-
-
Notifications
You must be signed in to change notification settings - Fork 288
/
Copy pathscene_impl.cxx
358 lines (311 loc) · 10.8 KB
/
scene_impl.cxx
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
#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 <optional>
#include <vtkCallbackCommand.h>
#include <vtkProgressBarRepresentation.h>
#include <vtkProgressBarWidget.h>
#include <vtkTimerLog.h>
#include <vtkVersion.h>
#include <vtksys/SystemTools.hxx>
#include <algorithm>
#include <numeric>
#include <vector>
namespace fs = std::filesystem;
namespace f3d::detail
{
class scene_impl::internals
{
public:
internals(const 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->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(const 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;
paths.reserve(filePathStrings.size());
for (const std::string& str : filePathStrings)
{
paths.emplace_back(str);
}
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");
}
// Recover the importer for the provided file path
f3d::reader* reader = f3d::factory::instance()->getReader(
filePath.string(), this->Internals->Options.scene.force_reader);
if (reader)
{
log::debug(
"Found a reader for \"" + filePath.string() + "\" : \"" + reader->getName() + "\"");
}
else
{
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 mesh_t& mesh)
{
// sanity checks
auto [valid, err] = mesh.isValid();
if (!valid)
{
throw scene::load_failure_exception(err);
}
vtkNew<vtkF3DMemoryMesh> vtkSource;
vtkSource->SetPoints(mesh.points);
vtkSource->SetNormals(mesh.normals);
vtkSource->SetTCoords(mesh.texture_coordinates);
vtkSource->SetFaces(mesh.face_sides, mesh.face_indices);
vtkSmartPointer<vtkF3DGenericImporter> importer = vtkSmartPointer<vtkF3DGenericImporter>::New();
importer->SetInternalReader(vtkSource);
log::debug("Loading 3D scene from memory");
this->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;
}
//----------------------------------------------------------------------------
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();
}
//----------------------------------------------------------------------------
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);
}
}