-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathVisualizer.cpp
657 lines (547 loc) · 22.6 KB
/
Visualizer.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
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
#include <Logging.h>
#include <MeshManager.h>
#include <Shape.h>
#include <Utils/StudioUtils.h>
#include <Visualization/ColorMap.h>
#include <Visualization/ParticleColors.h>
#include <Visualization/Visualizer.h>
#include <vtkAppendPolyData.h>
#include <vtkLookupTable.h>
#include <vtkMath.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRendererCollection.h>
#include <vtkTransformPolyDataFilter.h>
#include <vtkWindowToImageFilter.h>
#include <QColor>
#include <QPixmap>
#include <QThread>
namespace shapeworks {
//-----------------------------------------------------------------------------
Visualizer::Visualizer(Preferences& prefs) : preferences_(prefs) {
QObject::connect(&preferences_, SIGNAL(glyph_properties_changed()), this, SLOT(update_viewer_properties()));
show_glyphs_ = true;
show_surface_ = true;
glyph_lut_ = vtkSmartPointer<vtkLookupTable>::New();
selected_point_one_ = -1;
selected_point_two_ = -1;
}
//-----------------------------------------------------------------------------
Visualizer::~Visualizer() {}
//-----------------------------------------------------------------------------
void Visualizer::set_lightbox(LightboxHandle lightbox) {
lightbox_ = lightbox;
lightbox_->set_visualizer(this);
}
//-----------------------------------------------------------------------------
void Visualizer::set_session(SessionHandle session) {
session_ = session;
lightbox_->set_session(session);
connect(session_.data(), &Session::feature_range_changed, this, &Visualizer::handle_feature_range_changed);
connect(session_.data(), &Session::landmarks_changed, this, &Visualizer::update_landmarks);
connect(session_.data(), &Session::planes_changed, this, &Visualizer::update_planes);
connect(session_.data(), &Session::image_slice_settings_changed, this,
&Visualizer::handle_image_slice_settings_changed);
connect(session_.data(), &Session::paint_mode_changed, this, &Visualizer::update_paint_mode);
connect(session_.data(), &Session::repaint, this, &Visualizer::redraw);
connect(session_.data(), &Session::annotations_changed, this, &Visualizer::update_annotations);
}
//-----------------------------------------------------------------------------
void Visualizer::set_center(bool center) { center_ = center; }
//-----------------------------------------------------------------------------
void Visualizer::display_samples() {
update_viewer_properties();
auto shapes = session_->get_shapes();
display_shapes(shapes);
}
//-----------------------------------------------------------------------------
void Visualizer::update_samples() {
Q_FOREACH (ViewerHandle viewer, lightbox_->get_viewers()) {
viewer->update_points();
}
lightbox_->redraw();
}
//-----------------------------------------------------------------------------
void Visualizer::update_landmarks() {
Q_FOREACH (ViewerHandle viewer, lightbox_->get_viewers()) {
viewer->update_landmarks();
}
lightbox_->redraw();
}
//-----------------------------------------------------------------------------
void Visualizer::update_planes() {
Q_FOREACH (ViewerHandle viewer, lightbox_->get_viewers()) {
viewer->update_planes();
}
lightbox_->redraw();
}
//-----------------------------------------------------------------------------
void Visualizer::update_paint_mode() {
Q_FOREACH (ViewerHandle viewer, lightbox_->get_viewers()) {
viewer->update_paint_mode();
}
lightbox_->redraw();
}
//-----------------------------------------------------------------------------
void Visualizer::display_shapes(ShapeList shapes) {
lightbox_->set_shapes(shapes);
lightbox_->redraw();
update_viewer_properties();
}
//-----------------------------------------------------------------------------
void Visualizer::display_shape(ShapeHandle shape) {
std::vector<ShapeHandle> shapes;
shapes.push_back(shape);
lightbox_->set_shapes(shapes);
update_viewer_properties();
// reset_camera();
lightbox_->redraw();
current_shape_ = shape->get_particles();
}
//-----------------------------------------------------------------------------
Particles Visualizer::get_current_shape() {
auto shapes = lightbox_->get_shapes();
if (shapes.size() > 0) {
return shapes[0]->get_particles();
}
Particles particles;
return particles;
}
//-----------------------------------------------------------------------------
vtkFloatArray* Visualizer::get_current_particle_scalars() {
auto viewers = lightbox_->get_viewers();
if (viewers.size() > 0) {
return viewers[0]->get_particle_scalars();
}
return nullptr;
}
//-----------------------------------------------------------------------------
vtkSmartPointer<vtkPolyData> Visualizer::get_current_particle_poly_data() {
auto viewers = lightbox_->get_viewers();
if (viewers.size() > 0) {
return viewers[0]->get_particle_poly_data();
}
return nullptr;
}
//-----------------------------------------------------------------------------
void Visualizer::handle_new_mesh() {
lightbox_->handle_new_mesh();
if (needs_camera_reset_) {
reset_camera();
}
}
//-----------------------------------------------------------------------------
vtkSmartPointer<vtkPolyData> Visualizer::get_current_mesh(int index) {
auto meshes = get_current_meshes_transformed(index);
if (meshes.empty()) {
return nullptr;
}
auto append = vtkSmartPointer<vtkAppendPolyData>::New();
for (int domain = 0; domain < meshes.size(); domain++) {
append->AddInputData(meshes[domain]);
}
append->Update();
vtkSmartPointer<vtkPolyData> combined = append->GetOutput();
return combined;
}
//-----------------------------------------------------------------------------
std::vector<vtkSmartPointer<vtkPolyData>> Visualizer::get_current_meshes_transformed(int index) {
std::vector<vtkSmartPointer<vtkPolyData>> list;
auto shapes = lightbox_->get_shapes();
if (shapes.size() > index) {
if (shapes[index]->get_meshes(session_->get_display_mode()).valid()) {
auto meshes = shapes[index]->get_meshes(session_->get_display_mode()).meshes();
for (int domain = 0; domain < meshes.size(); domain++) {
if (!meshes[domain]->get_poly_data()) {
return list;
}
// we have to transform each domain to its location in order to export an appended mesh
auto filter = vtkSmartPointer<vtkTransformPolyDataFilter>::New();
filter->SetTransform(get_transform(shapes[index], get_alignment_domain(), domain));
filter->SetInputData(meshes[domain]->get_poly_data());
filter->Update();
list.push_back(filter->GetOutput());
}
}
}
return list;
}
//-----------------------------------------------------------------------------
void Visualizer::display_sample(int i) {
update_viewer_properties();
std::vector<ShapeHandle> display_shapes;
std::vector<ShapeHandle> shapes = session_->get_shapes();
if (i < shapes.size()) {
display_shapes.push_back(shapes[i]);
}
lightbox_->set_shapes(display_shapes);
lightbox_->redraw();
update_viewer_properties();
}
//-----------------------------------------------------------------------------
void Visualizer::set_show_glyphs(bool show) { show_glyphs_ = show; }
//-----------------------------------------------------------------------------
void Visualizer::set_show_surface(bool show) { show_surface_ = show; }
//-----------------------------------------------------------------------------
void Visualizer::update_viewer_properties() {
double size = preferences_.get_glyph_size();
double quality = preferences_.get_glyph_quality();
quality = std::max<double>(quality, 3);
if (preferences_.get_glyph_auto_size()) {
size = session_->get_auto_glyph_size();
}
ColorMaps color_maps;
auto color_map = color_maps.get_color_map(preferences_.get_color_map());
color_map.set_discrete_mode(preferences_.get_discrete_color_mode());
color_map.set_reverse_mode(preferences_.get_reverse_color_map());
if (lightbox_) {
Q_FOREACH (ViewerHandle viewer, lightbox_->get_viewers()) {
viewer->set_glyph_size_and_quality(size, quality);
viewer->set_color_series(color_map);
viewer->set_show_glyphs(show_glyphs_);
viewer->set_show_surface(show_surface_);
viewer->set_scale_arrows(preferences_.get_glyph_scale_arrows());
viewer->set_color_scheme(preferences_.get_color_scheme());
}
lightbox_->set_orientation_marker(preferences_.get_orientation_marker_type(),
preferences_.get_orientation_marker_corner());
update_lut();
lightbox_->redraw();
}
current_glyph_size_ = size;
}
//-----------------------------------------------------------------------------
void Visualizer::handle_feature_range_changed() {
set_uniform_feature_range(session_->get_feature_uniform_scale());
feature_manual_range_[0] = session_->get_feature_range_min();
feature_manual_range_[1] = std::max<double>(feature_manual_range_[0], session_->get_feature_range_max());
lightbox_->update_feature_range();
lightbox_->redraw();
}
//-----------------------------------------------------------------------------
void Visualizer::handle_image_slice_settings_changed() {
lightbox_->update_interactor_style();
if (lightbox_) {
Q_FOREACH (ViewerHandle v, lightbox_->get_viewers()) {
v->update_image_volume(true);
v->update_actors();
v->reset_camera();
}
}
reset_camera();
lightbox_->redraw();
}
//-----------------------------------------------------------------------------
void Visualizer::update_lut() {
int num_points = session_->get_num_particles();
glyph_lut_->SetNumberOfTableValues(num_points + 1);
glyph_lut_->SetTableRange(0.0, (double)num_points + 1.0);
bool analysis_mode = session_->is_analysis_mode();
auto good_bad = session_->get_good_bad_particles();
bool good_bad_active = session_->get_show_good_bad_particles() && good_bad.size() == num_points && analysis_mode;
bool single_point_selected_mode = selected_point_one_ >= 0 && selected_point_two_ < 0;
bool dual_point_selected_mode = selected_point_one_ >= 0 && selected_point_two_ >= 0;
if (single_point_selected_mode) {
setup_single_selected_point_lut();
} else if (dual_point_selected_mode) {
// dual particle coloring mode
for (int i = 0; i < num_points; i++) {
glyph_lut_->SetTableValue(i, 0, 0, 0);
if (selected_point_one_ == i) {
glyph_lut_->SetTableValue(i, 1, 1, 1);
}
if (selected_point_two_ == i) {
glyph_lut_->SetTableValue(i, 1, 1, 0);
}
}
} else if (good_bad_active) {
for (int i = 0; i < num_points; i++) {
if (good_bad[i]) {
glyph_lut_->SetTableValue(i, 0, 1, 0);
} else {
glyph_lut_->SetTableValue(i, 1, 0, 0);
}
}
} else {
glyph_lut_->ForceBuild();
if (preferences_.get_particle_colors() == ParticleColors::ParticleColorsType::Distinct) {
auto lut = ParticleColors::construct_distinct();
glyph_lut_->ForceBuild();
// normal particle coloring mode
for (int i = 0; i < num_points; i++) {
glyph_lut_->SetTableValue(i, lut->GetTableValue(i % lut->GetNumberOfTableValues()));
}
}
}
glyph_lut_->Modified();
session_->set_glyph_lut(glyph_lut_);
lightbox_->set_glyph_lut(glyph_lut_);
}
//-----------------------------------------------------------------------------
void Visualizer::update_annotations() {
if (lightbox_) {
Q_FOREACH (ViewerHandle v, lightbox_->get_viewers()) {
v->update_annotations();
}
}
lightbox_->redraw();
}
//-----------------------------------------------------------------------------
void Visualizer::set_selected_point_one(int id) {
selected_point_one_ = id;
update_lut();
}
//-----------------------------------------------------------------------------
void Visualizer::set_selected_point_two(int id) {
selected_point_two_ = id;
update_lut();
}
//-----------------------------------------------------------------------------
void Visualizer::compute_measurements() {
if (selected_point_one_ >= 0 && selected_point_two_ >= 0) {
}
}
//-----------------------------------------------------------------------------
void Visualizer::setup_single_selected_point_lut() {
int num_points = session_->get_num_particles();
// color by distance from the selected point
int check = selected_point_one_ * 3 + 2;
if (check >= cached_mean_.size()) {
return;
}
double p1[3];
p1[0] = cached_mean_[selected_point_one_ * 3 + 0];
p1[1] = cached_mean_[selected_point_one_ * 3 + 1];
p1[2] = cached_mean_[selected_point_one_ * 3 + 2];
std::vector<double> distances;
double max_distance = 0;
for (int i = 0; i < cached_mean_.size() / 3.0; i++) {
double p2[3];
p2[0] = cached_mean_[i * 3 + 0];
p2[1] = cached_mean_[i * 3 + 1];
p2[2] = cached_mean_[i * 3 + 2];
double distance = sqrt(vtkMath::Distance2BetweenPoints(p1, p2));
distances.push_back(distance);
if (distance > max_distance) {
max_distance = distance;
}
}
vtkSmartPointer<vtkLookupTable> lut = vtkSmartPointer<vtkLookupTable>::New();
lut->SetTableRange(0, max_distance);
lut->SetHueRange(0, 1);
lut->SetSaturationRange(1, 1);
lut->SetValueRange(1, 1);
lut->Build();
for (int i = 0; i < num_points; i++) {
const unsigned char* color = lut->MapValue(max_distance - distances[i]);
// glyph_lut_->SetTableValue( i, distances[i] / max_distance, 0, 1 );
glyph_lut_->SetTableValue(i, color[0] / 255.0f, color[1] / 255.0f, color[2] / 255.0f);
if (selected_point_one_ == i) {
glyph_lut_->SetTableValue(i, 1, 1, 1);
}
if (selected_point_two_ == i) {
glyph_lut_->SetTableValue(i, 0, 0, 0);
}
}
}
//-----------------------------------------------------------------------------
void Visualizer::set_mean(const Eigen::VectorXd& mean) { cached_mean_ = mean; }
//-----------------------------------------------------------------------------
void Visualizer::reset_camera() {
needs_camera_reset_ = false;
if (lightbox_) {
auto trans = lightbox_->initPos();
Q_FOREACH (ViewerHandle v, lightbox_->get_viewers()) {
if (!v->is_viewer_ready()) {
needs_camera_reset_ = true;
}
v->reset_camera(trans);
}
}
update_viewer_properties();
}
//-----------------------------------------------------------------------------
const std::string& Visualizer::get_feature_map() const { return feature_map_; }
//-----------------------------------------------------------------------------
void Visualizer::set_feature_map(const std::string& feature_map) {
if (feature_map_ == feature_map) {
return;
}
feature_map_ = feature_map;
reset_feature_range();
}
//-----------------------------------------------------------------------------
bool Visualizer::get_center() { return center_; }
//-----------------------------------------------------------------------------
void Visualizer::set_alignment_domain(int domain) { alignment_domain_ = domain; }
//-----------------------------------------------------------------------------
int Visualizer::get_alignment_domain() { return alignment_domain_; }
//-----------------------------------------------------------------------------
void Visualizer::clear_viewers() {
std::vector<ShapeHandle> shapes;
lightbox_->set_shapes(shapes);
}
//-----------------------------------------------------------------------------
void Visualizer::reset_feature_range() { feature_range_valid_ = false; }
//-----------------------------------------------------------------------------
double* Visualizer::get_feature_range() {
if (session_->get_feature_auto_scale()) {
return feature_range_;
} else {
return feature_manual_range_;
}
}
//-----------------------------------------------------------------------------
double* Visualizer::get_feature_raw_range() { return feature_range_; }
//-----------------------------------------------------------------------------
bool Visualizer::get_feature_range_valid() { return feature_range_valid_; }
//-----------------------------------------------------------------------------
void Visualizer::update_feature_range(double* range) { update_feature_range(range[0], range[1]); }
//-----------------------------------------------------------------------------
void Visualizer::update_feature_range(double min, double max) {
if (!feature_range_valid_) {
feature_range_[0] = min;
feature_range_[1] = max;
}
feature_range_[0] = std::min(feature_range_[0], min);
feature_range_[1] = std::max(feature_range_[1], max);
feature_range_valid_ = true;
lightbox_->update_feature_range();
}
//-----------------------------------------------------------------------------
void Visualizer::set_uniform_feature_range(bool value) { feature_range_uniform_ = value; }
//-----------------------------------------------------------------------------
bool Visualizer::get_uniform_feature_range(void) { return feature_range_uniform_; }
//-----------------------------------------------------------------------------
vtkSmartPointer<vtkTransform> Visualizer::get_transform(std::shared_ptr<Shape> shape, int alignment_domain,
int domain) {
return get_transform(shape, session_->get_display_mode(), alignment_domain, domain);
}
//-----------------------------------------------------------------------------
vtkSmartPointer<vtkTransform> Visualizer::get_transform(std::shared_ptr<Shape> shape, DisplayMode display_mode,
int alignment_domain, int domain) {
auto transform = vtkSmartPointer<vtkTransform>::New();
if (display_mode == DisplayMode::Original) {
if (get_center()) {
transform = shape->get_transform(alignment_domain);
}
} else if (display_mode == DisplayMode::Groomed) {
if (get_center()) {
transform = shape->get_alignment(alignment_domain);
}
} else {
if (shape->is_subject()) {
if (get_center()) {
transform = shape->get_alignment(alignment_domain);
}
} else {
transform = shape->get_reconstruction_transform(domain);
}
}
return transform;
}
//-----------------------------------------------------------------------------
void Visualizer::set_opacities(std::vector<float> opacities) {
opacities_ = opacities;
if (lightbox_) {
Q_FOREACH (ViewerHandle viewer, lightbox_->get_viewers()) {
viewer->update_opacities();
}
}
lightbox_->redraw();
}
//-----------------------------------------------------------------------------
void Visualizer::set_domain_particle_visibilities(std::vector<bool> visibilities) {
domain_particle_visibilities_ = visibilities;
if (lightbox_) {
Q_FOREACH (ViewerHandle viewer, lightbox_->get_viewers()) {
viewer->update_points();
}
}
lightbox_->redraw();
}
//-----------------------------------------------------------------------------
std::vector<bool> Visualizer::get_domain_particle_visibilities() { return domain_particle_visibilities_; }
//-----------------------------------------------------------------------------
std::vector<float> Visualizer::get_opacities() { return opacities_; }
//-----------------------------------------------------------------------------
double Visualizer::get_current_glyph_size() { return current_glyph_size_; }
//-----------------------------------------------------------------------------
void Visualizer::handle_ctrl_click(PickResult result) { session_->handle_ctrl_click(result); }
//-----------------------------------------------------------------------------
void Visualizer::redraw() { lightbox_->redraw(); }
//-----------------------------------------------------------------------------
QPixmap Visualizer::export_to_pixmap(QSize size, bool transparent_background, bool show_orientation_marker,
bool show_color_scale, bool& ready) {
auto render_window = lightbox_->get_render_window();
ready = true;
Q_FOREACH (ViewerHandle viewer, lightbox_->get_viewers()) {
if (viewer->get_shape() && !viewer->is_ready()) {
ready = false;
}
};
auto window_to_image_filter = vtkSmartPointer<vtkWindowToImageFilter>::New();
int original_size[2];
original_size[0] = render_window->GetSize()[0];
original_size[1] = render_window->GetSize()[1];
auto off_render_window = vtkSmartPointer<vtkRenderWindow>::New();
off_render_window->OffScreenRenderingOn();
off_render_window->SetNumberOfLayers(render_window->GetNumberOfLayers());
off_render_window->SwapBuffersOff();
window_to_image_filter->SetInput(off_render_window);
window_to_image_filter->ReadFrontBufferOff(); // read from the back buffer
if (transparent_background) {
// If the background color is transparent then add alpha channel to output image.
off_render_window->SetAlphaBitPlanes(1); // Enable usage of alpha channel
window_to_image_filter->SetInputBufferTypeToRGBA();
}
if (!show_orientation_marker) {
lightbox_->set_orientation_marker(Preferences::OrientationMarkerType::none,
Preferences::OrientationMarkerCorner::upper_right);
}
if (!show_color_scale) {
Q_FOREACH (ViewerHandle viewer, lightbox_->get_viewers()) {
viewer->remove_scalar_bar();
}
}
vtkRendererCollection* collection = render_window->GetRenderers();
collection->InitTraversal();
vtkRenderer* renderer = collection->GetNextItem();
while (renderer) {
off_render_window->AddRenderer(renderer);
renderer->SetRenderWindow(off_render_window);
renderer->Modified();
renderer = collection->GetNextItem();
}
off_render_window->SetSize(size.width(), size.height());
off_render_window->Modified();
off_render_window->Render();
window_to_image_filter->Update();
auto qimage = StudioUtils::vtk_image_to_qimage(window_to_image_filter->GetOutput());
// set back to the original render window
collection->InitTraversal();
renderer = collection->GetNextItem();
while (renderer) {
off_render_window->RemoveRenderer(renderer);
renderer->SetRenderWindow(render_window);
renderer = collection->GetNextItem();
}
// restore changes
update_viewer_properties();
Q_FOREACH (ViewerHandle viewer, lightbox_->get_viewers()) {
viewer->update_actors();
}
return QPixmap::fromImage(qimage);
}
//-----------------------------------------------------------------------------
QSize Visualizer::get_render_size() {
auto render_window = lightbox_->get_render_window();
int* size = render_window->GetSize();
return QSize(size[0], size[1]);
}
//-----------------------------------------------------------------------------
void Visualizer::set_mean_shape(ShapeHandle mean_shape) { mean_shape_ = mean_shape; }
//-----------------------------------------------------------------------------
ShapeHandle Visualizer::get_mean_shape() { return mean_shape_; }
//-----------------------------------------------------------------------------
} // namespace shapeworks