-
Notifications
You must be signed in to change notification settings - Fork 5k
Expand file tree
/
Copy pathviewer.cpp
More file actions
4553 lines (3888 loc) · 190 KB
/
Copy pathviewer.cpp
File metadata and controls
4553 lines (3888 loc) · 190 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
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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2017 RealSense, Inc. All Rights Reserved.
#ifdef _MSC_VER
#ifndef NOMINMAX
#define NOMINMAX
#endif
#endif
#include "viewer.h"
#include "os.h"
#include "udev-rules.h"
#include <opengl3.h>
#include <imgui_internal.h>
#include <realsense_imgui.h>
#define ARCBALL_CAMERA_IMPLEMENTATION
#include <third-party/arcball_camera.h>
#include <rsutils/accelerators/gpu.h>
#include <rsutils/os/special-folder.h>
#include <rsutils/string/trim-newlines.h>
#include <rsutils/string/split.h>
#include <common/utilities/imgui/wrap.h>
#include <common/labeled-point-cloud-utilities.h>
#include <common/utilities/com/center-of-mass.h>
#include <rsutils/easylogging/easyloggingpp.h>
#include <regex>
#include <algorithm>
#include <fstream>
namespace rs2
{
template <typename T>
T non_negative(const T& input)
{
return std::max(static_cast<T>(0), input);
}
// Allocates a frameset from points and texture frames
frameset_allocator::frameset_allocator(viewer_model* viewer) : owner(viewer),
filter([this](frame f, frame_source& s)
{
std::vector<rs2::frame> frame_vec;
auto tex = owner->get_last_texture()->get_last_frame(true);
if (tex)
{
frame_vec.push_back(tex);
frame_vec.push_back(f);
auto frame = s.allocate_composite_frame(frame_vec);
if (frame)
s.frame_ready(std::move(frame));
}
else
s.frame_ready(std::move(f));
}) {}
// Need out of class declaration to take reference
const rs2_option save_to_ply::OPTION_IGNORE_COLOR;
const rs2_option save_to_ply::OPTION_PLY_MESH;
const rs2_option save_to_ply::OPTION_PLY_BINARY;
const rs2_option save_to_ply::OPTION_PLY_NORMALS;
void viewer_model::set_export_popup(ImFont* large_font, ImFont* font, rect stream_rect, std::string& error_message, config_file& temp_cfg)
{
float font_size = (float)temp_cfg.get( configurations::window::font_size );
float w = 32.f * font_size;
float h = 20.f * font_size;
float x0 = stream_rect.x + stream_rect.w / 3;
float y0 = stream_rect.y + stream_rect.h / 3;
ImGui::SetNextWindowPos({ x0, y0 });
ImGui::SetNextWindowSize({ w, h });
auto flags = ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove |
ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoSavedSettings;
RsImGui_ScopePushFont(font);
ImGui::PushStyleColor(ImGuiCol_PopupBg, sensor_bg);
ImGui::PushStyleColor(ImGuiCol_TextSelectedBg, white);
ImGui::PushStyleColor(ImGuiCol_Text, light_grey);
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(15, 15));
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 1);
static export_type tab = export_type::ply;
if (ImGui::BeginPopupModal("Export", nullptr, flags))
{
ImGui::PushStyleColor(ImGuiCol_Button, sensor_bg);
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, sensor_bg);
ImGui::PushFont(large_font);
for (auto& exporter : exporters)
{
ImGui::PushStyleColor(ImGuiCol_Text, tab != exporter.first ? light_grey : light_blue);
ImGui::PushStyleColor(ImGuiCol_TextSelectedBg, tab != exporter.first ? light_grey : light_blue);
ImGui::SameLine();
if (ImGui::Button(exporter.second.name.c_str(), { w / exporters.size() - 50, 30 }))
{
config_file::instance().set(configurations::viewer::settings_tab, tab);
temp_cfg.set(configurations::viewer::settings_tab, tab);
tab = exporter.first;
}
ImGui::PopStyleColor(2);
}
ImGui::PopFont();
if (tab == export_type::ply)
{
bool mesh = temp_cfg.get(configurations::ply::mesh);
bool use_normals = temp_cfg.get(configurations::ply::use_normals);
if (!mesh) use_normals = false;
int encoding = temp_cfg.get(configurations::ply::encoding);
ImGui::PushStyleColor(ImGuiCol_Text, grey);
ImGui::Text("Polygon File Format defines a flexible systematic scheme for storing 3D data");
ImGui::PopStyleColor();
ImGui::NewLine();
ImGui::Separator();
if (ImGui::Checkbox("Meshing", &mesh))
{
temp_cfg.set(configurations::ply::mesh, mesh);
}
ImGui::PushStyleColor(ImGuiCol_Text, grey);
ImGui::Text(" Use faces for meshing by connecting each group of 3 adjacent points");
ImGui::PopStyleColor();
ImGui::Separator();
if (!mesh)
{
ImGui::PushStyleVar(ImGuiStyleVar_Alpha, ImGui::GetStyle().Alpha * 0.5f);
ImGui::PushStyleColor(ImGuiCol_FrameBgHovered, black);
ImGui::PushStyleColor(ImGuiCol_FrameBgActive, black);
}
if (ImGui::Checkbox("Normals", &use_normals))
{
if (!mesh)
use_normals = false;
else
temp_cfg.set(configurations::ply::use_normals, use_normals);
}
if (!mesh)
{
if (ImGui::IsItemHovered())
{
RsImGui::CustomTooltip("Enable meshing to allow vertex normals calculation");
}
ImGui::PopStyleColor(2);
ImGui::PopStyleVar();
}
ImGui::PushStyleColor(ImGuiCol_Text, grey);
ImGui::Text(" Calculate vertex normals and add them to the PLY");
ImGui::PopStyleColor();
ImGui::Separator();
ImGui::Text("Encoding:");
ImGui::PushStyleColor(ImGuiCol_Text, grey);
ImGui::Text("Save PLY as binary, or as a larger textual human-readable file");
ImGui::PopStyleColor();
if (ImGui::RadioButton("Textual", encoding == configurations::ply::textual))
{
encoding = configurations::ply::textual;
temp_cfg.set(configurations::ply::encoding, encoding);
}
if (ImGui::RadioButton("Binary", encoding == configurations::ply::binary))
{
encoding = configurations::ply::binary;
temp_cfg.set(configurations::ply::encoding, encoding);
}
auto curr_exporter = exporters.find(tab);
if (curr_exporter == exporters.end()) // every tab should have a corresponding exporter
error_message = "Exporter not implemented";
else
{
curr_exporter->second.options[rs2::save_to_ply::OPTION_PLY_MESH] = mesh;
curr_exporter->second.options[rs2::save_to_ply::OPTION_PLY_NORMALS] = use_normals;
curr_exporter->second.options[rs2::save_to_ply::OPTION_PLY_BINARY] = encoding;
}
}
ImGui::PopStyleColor(2); // button color
auto apply = [&]() {
update_configuration(&temp_cfg);
};
ImGui::PushStyleColor(ImGuiCol_Button, button_color);
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, button_color + 0.1f);
ImGui::PushStyleColor(ImGuiCol_ButtonActive, button_color + 0.1f);
ImGui::SetCursorScreenPos({ (float)(x0 + w / 2), (float)(y0 + h - 30) });
if( ImGui::Button( "Export", ImVec2( font_size * 8.f, 0 ) ) )
{
apply();
if (!last_points)
error_message = "No depth data available";
else
{
auto curr_exporter = exporters.find(tab);
if (curr_exporter == exporters.end()) // every tab should have a corresponding exporter
error_message = "Exporter not implemented";
else if (auto ret = file_dialog_open(save_file, curr_exporter->second.filters.data(), NULL, NULL))
{
auto model = ppf.get_points();
frame tex;
if (selected_tex_source_uid >= 0 && streams.find(selected_tex_source_uid) != streams.end())
{
tex = streams[selected_tex_source_uid].texture->get_last_frame(true);
if (tex) ppf.update_texture(tex);
}
std::string fname(ret);
if (!ends_with(rsutils::string::to_lower(fname), curr_exporter->second.extension)) fname += curr_exporter->second.extension;
std::unique_ptr<rs2::filter> exporter;
if (tab == export_type::ply)
exporter = std::unique_ptr<rs2::filter>(new rs2::save_to_ply(fname));
auto data = frameset_alloc.process(last_points);
for (auto& option : curr_exporter->second.options)
{
exporter->set_option(option.first, static_cast<float>(option.second));
}
export_frame(fname, std::move(exporter), *not_model, data);
}
}
ImGui::CloseCurrentPopup();
}
if (ImGui::IsItemHovered())
{
RsImGui::CustomTooltip("%s", "Save settings and export file");
}
ImGui::SameLine();
if( ImGui::Button( "Cancel", ImVec2( font_size * 8.f, 0 ) ) )
{
ImGui::CloseCurrentPopup();
}
if (ImGui::IsItemHovered())
{
RsImGui::CustomTooltip("%s", "Close window without saving any changes to the settings");
}
ImGui::PopStyleColor(3);
ImGui::EndPopup();
}
ImGui::PopStyleVar(2);
ImGui::PopStyleColor(3);
}
bool big_button(bool* status,
ux_window& win,
float x, float y,
const char* icon,
const char* label,
bool dropdown,
bool enabled,
const char* description,
ImVec4 text_color = light_grey
)
{
auto disabled = !enabled;
auto font = win.get_font();
auto large_font = win.get_large_font();
float font_size = (float)win.get_font_size();
bool hovered = false;
bool clicked = false;
if (!disabled)
{
if (*status)
{
ImGui::PushStyleColor(ImGuiCol_Text, light_blue);
ImGui::PushStyleColor(ImGuiCol_TextSelectedBg, light_blue);
}
else
{
ImGui::PushStyleColor(ImGuiCol_Text, text_color);
ImGui::PushStyleColor(ImGuiCol_TextSelectedBg, text_color);
}
}
else
{
ImGui::PushStyleColor(ImGuiCol_Text, header_color);
ImGui::PushStyleColor(ImGuiCol_TextSelectedBg, header_color);
}
float button_width = font_size * 3.8f;
ImGui::SetCursorPos( { x, y } );
ImGui::PushFont(large_font);
clicked = clicked || ImGui::Button( icon, { button_width, 50 } );
ImGui::PopFont();
hovered = hovered || ImGui::IsItemHovered();
ImGui::SetCursorPos( { x + button_width - font_size, y } );
ImGui::PushFont(font);
if (dropdown)
{
clicked = clicked || ImGui::Button(textual_icons::chevron_down, { font_size, 55 } );
hovered = hovered || ImGui::IsItemHovered();
}
ImGui::SetCursorPos( { x, y + 35 } );
clicked = clicked || ImGui::Button( label, { button_width, 20 } );
ImGui::PopFont();
hovered = hovered || ImGui::IsItemHovered();
if (hovered)
{
win.link_hovered();
RsImGui::CustomTooltip("%s", description);
}
if (clicked && !disabled)
{
*status = !(*status);
}
ImGui::PopStyleColor(2);
return clicked && !disabled;
}
// Get both font and large_font for the export pop-up
void viewer_model::show_3dviewer_header(ux_window& win, rs2::rect stream_rect, bool& paused, std::string& error_message)
{
auto font = win.get_font();
auto large_font = win.get_large_font();
// Draw pose header if pose stream exists
bool pose_render = false;
for (auto&& s : streams)
{
if (s.second.is_stream_visible() &&
s.second.profile.stream_type() == RS2_STREAM_POSE)
{
pose_render = true;
break;
}
}
// Initialize selected_labeled_points_source_uid for clearing it when needed
init_labeled_points_uid();
// Initialize and prepare depth and texture sources
int selected_depth_source = -1;
std::vector<std::string> depth_sources_str;
std::vector<int> depth_sources;
init_depth_uid(selected_depth_source, depth_sources_str, depth_sources);
int selected_tex_source = 0;
std::vector<std::string> tex_sources_str;
std::vector<int> tex_sources;
std::vector<rs2::stream_profile> tex_profiles;
int i = 0;
for (auto&& s : streams)
{
if (s.second.is_stream_visible() &&
(s.second.profile.stream_type() == RS2_STREAM_COLOR ||
s.second.profile.stream_type() == RS2_STREAM_INFRARED ||
s.second.profile.stream_type() == RS2_STREAM_CONFIDENCE ||
s.second.profile.stream_type() == RS2_STREAM_DEPTH ||
s.second.profile.stream_type() == RS2_STREAM_FISHEYE))
{
auto profile_unique_id = s.second.profile.unique_id();
auto stream_origin_iter = streams_origin.find(profile_unique_id);
auto profile_found = stream_origin_iter != streams_origin.end();
if( selected_tex_source_uid == -1 && selected_depth_source_uid != -1 )
{
if( profile_found && streams.find(stream_origin_iter->second ) != streams.end() )
{
selected_tex_source_uid = streams_origin[profile_unique_id];
}
}
if( ( profile_found && stream_origin_iter->second == selected_tex_source_uid ) )
{
selected_tex_source = i;
}
if( profile_found )
{
// The texture source shall always refer to the raw (original) streams
tex_sources.push_back(streams_origin[profile_unique_id]);
tex_profiles.push_back(s.second.profile);
auto dev_name = s.second.dev ? s.second.dev->dev.get_info(RS2_CAMERA_INFO_NAME) : "Unknown";
std::string stream_name = rs2_stream_to_string(s.second.profile.stream_type());
if (s.second.profile.stream_index())
stream_name += "_" + std::to_string(s.second.profile.stream_index());
tex_sources_str.push_back( rsutils::string::from() << dev_name << " " << stream_name );
i++;
}
}
}
for (int i = 0; i < tex_sources.size(); i++)
{
auto id = tex_sources[i];
auto it = std::find(begin(last_tex_sources), end(last_tex_sources), id);
if (it == last_tex_sources.end())
{
// Don't auto-switch to IR stream
if (tex_profiles[i].format() != RS2_FORMAT_Y8)
selected_tex_source_uid = id;
texture_update_time = glfwGetTime();
}
}
last_tex_sources = tex_sources;
const auto top_bar_height = 60.f;
ImGui::PushFont(font);
ImGui::PushStyleColor(ImGuiCol_Text, light_grey);
ImGui::PushStyleColor(ImGuiCol_TextSelectedBg, white);
ImGui::PushStyleColor(ImGuiCol_Button, header_window_bg);
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, header_window_bg);
ImGui::PushStyleColor(ImGuiCol_ButtonActive, header_window_bg);
std::string label = "header of 3dviewer";
ImGui::GetWindowDrawList()->AddRectFilled({ stream_rect.x, stream_rect.y },
{ stream_rect.x + stream_rect.w, stream_rect.y + top_bar_height }, ImColor(sensor_bg));
ImGui::SetCursorPos({ 0, 0 });
auto cursor = ImGui::GetCursorScreenPos();
float left = 5.f;
float button_width = win.get_font_size() * 3.8f;
const auto has_stream = tex_sources_str.size() && depth_sources_str.size();
// ------------ Pause Stream --------------
if (paused)
{
bool active = true;
if (big_button(&active, win, 5 + left, 0, textual_icons::play, "Resume", false, has_stream, "Resume streaming"))
{
for(auto&& s : streams)
if (s.second.dev) s.second.dev->resume();
paused = false;
}
}
else
{
bool active = false;
if (big_button(&active, win, 5 + left, 0, textual_icons::pause, "Pause", false, has_stream, "Pause streaming"))
{
for(auto&& s : streams)
if (s.second.dev) s.second.dev->pause();
paused = true;
}
}
left += button_width;
// ------------ Reset Viewport ---------------
bool default_view = (pos - float3{ 0.f, 0.f, -1.f }).length() < 0.001f &&
(target - float3{ 0.f, 0.f, 0.f }).length() < 0.001f;
bool active = false;
if (big_button(&active, win, 5 + left, 0, textual_icons::rotate, "Reset", false, !default_view, "Reset 3D viewport to initial state"))
{
reset_camera();
}
left += button_width;
// ------------ Lock Mode ---------------
if (synchronization_enable)
{
bool active = true;
if (big_button(&active, win, 5 + left, 0, textual_icons::lock, "Unlock", false,
support_non_syncronized_mode && has_stream, "Unlock texture data from pointcloud"))
{
synchronization_enable = false;
}
}
else
{
bool active = false;
if (big_button(&active, win, 5 + left, 0, textual_icons::unlock, "Lock", false,
support_non_syncronized_mode && has_stream, "Lock pointcloud and texture data together"))
{
synchronization_enable = true;
}
}
left += button_width + 10;
ImGui::GetWindowDrawList()->AddLine({ cursor.x + left - 1, cursor.y + 5 },
{ cursor.x + left - 1, cursor.y + top_bar_height - 5 }, ImColor(grey));
// ------------ Depth Selection --------------
const auto source_selection_popup = "Source Selection";
if (big_button(&select_3d_source, win, left, 0, textual_icons::cube,
"Source", true,
has_stream,
"List of available 3D data sources"))
{
ImGui::OpenPopup(source_selection_popup);
}
ImGui::PushStyleColor(ImGuiCol_Text, black);
ImGui::PushStyleColor(ImGuiCol_PopupBg, almost_white_bg);
ImGui::PushStyleColor(ImGuiCol_HeaderHovered, light_blue);
ImGui::PushStyleColor(ImGuiCol_TextSelectedBg, white);
ImGui::SetNextWindowPos({ cursor.x + left + 5, cursor.y + 60 });
if (ImGui::BeginPopup(source_selection_popup))
{
select_3d_source = true;
i = 0;
for (auto&& s : streams)
{
if (s.second.is_stream_visible() &&
s.second.texture->get_last_frame() &&
s.second.profile.stream_type() == RS2_STREAM_DEPTH)
{
std::string id = rsutils::string::from() << depth_sources_str[i] << "##DepthSource-" << i;
bool selected = i == selected_depth_source;
if (ImGui::MenuItem(id.c_str(), nullptr, &selected))
{
if (selected)
{
auto stream_origin_iter = streams_origin.find(s.second.profile.unique_id());
if( stream_origin_iter != streams_origin.end() )
selected_depth_source_uid = stream_origin_iter->second;
}
}
i++;
}
}
ImGui::EndPopup();
}
else
{
select_3d_source = false;
}
left += button_width + 20;
// ------------ Texture Selection --------------
auto t = single_wave(float(glfwGetTime() - texture_update_time) * 2);
ImVec4 text_color = light_grey * (1.f - t) + light_blue * t;
const auto tex_selection_popup = "Tex Selection";
if (big_button(&select_tex_source, win, left, 0, textual_icons::palette,
"Texture", true,
has_stream,
"List of available texture sources", text_color))
{
ImGui::OpenPopup(tex_selection_popup);
}
ImGui::SetNextWindowPos({ cursor.x + left + 5, cursor.y + 60 });
if (ImGui::BeginPopup(tex_selection_popup))
{
select_tex_source = true;
for (int i = 0; i < tex_sources_str.size(); i++)
{
std::string id = rsutils::string::from() << tex_sources_str[i] << "##TexSource-" << i;
bool selected = i == selected_tex_source;
if (ImGui::MenuItem(id.c_str(), nullptr, &selected))
{
if (selected)
{
selected_tex_source_uid = tex_sources[i];
}
}
}
ImGui::EndPopup();
}
else
{
select_tex_source = false;
}
left += button_width + 20;
// ------------ Shader Selection --------------
const auto shader_selection_popup = "Shading Selection";
if (big_button(&select_shader_source, win, left, 0, textual_icons::adjust,
"Shading", true, true,
"List of available shading modes"))
{
ImGui::OpenPopup(shader_selection_popup);
}
ImGui::SetNextWindowPos({ cursor.x + left + 5, cursor.y + 60 });
if (ImGui::BeginPopup(shader_selection_popup))
{
select_shader_source = true;
bool selected = selected_shader == shader_type::points;
if (ImGui::MenuItem("Raw Point-Cloud", nullptr, &selected))
{
if (selected) selected_shader = shader_type::points;
}
selected = selected_shader == shader_type::flat;
if (ImGui::MenuItem("Flat-Shaded Mesh", nullptr, &selected))
{
if (selected) selected_shader = shader_type::flat;
}
selected = selected_shader == shader_type::diffuse;
if (ImGui::MenuItem("With Diffuse Lighting", nullptr, &selected, glsl_available))
{
if (selected) selected_shader = shader_type::diffuse;
}
ImGui::EndPopup();
}
else
{
select_shader_source = false;
}
left += button_width + 20;
//-----------------------------
ImGui::PopStyleColor(4);
ImGui::GetWindowDrawList()->AddLine({ cursor.x + left - 1, cursor.y + 5 },
{ cursor.x + left - 1, cursor.y + top_bar_height - 5 }, ImColor(grey));
// -------------------- Measure ----------------
std::string measure_tooltip = "Measure distance between points\nHold shift to connect more than 2 points and measure area";
if (!glsl_available) measure_tooltip += "\nRequires GLSL acceleration! \nEnable 2 checkboxes in Settings - Performance: \n- Use GLSL for Rendering \n- Use GLSL for Processing ";
if (_measurements.is_enabled())
{
bool active = true;
if (big_button(&active, win, 5 + left, 0, textual_icons::ruler, "Measure", false, glsl_available, measure_tooltip.c_str()))
{
_measurements.disable();
}
}
else
{
bool active = false;
if (big_button(&active, win, 5 + left, 0, textual_icons::ruler, "Measure", false, glsl_available, measure_tooltip.c_str()))
{
_measurements.enable();
}
}
left += button_width;
// -------------------- Export ------------------
static config_file temp_cfg;
set_export_popup(large_font, font, stream_rect, error_message, temp_cfg);
active = false;
if (big_button(&active, win, 5 + left, 0, textual_icons::save, "Export", false, last_points, "Export 3D model to 3rd-party application"))
{
_measurements.disable();
temp_cfg = config_file::instance();
ImGui::OpenPopup("Export");
}
left += button_width;
//-----------------------------
// -------------------- LPC Settings ----------------
if (last_labeled_points)
{
ImGui::GetWindowDrawList()->AddLine({ cursor.x + left - 1, cursor.y + 5 },
{ cursor.x + left - 1, cursor.y + top_bar_height - 5 }, ImColor(grey));
left += 10;
// -------------------- LPC Points Size ----------------
const auto lpc_popup = "LPC Draw";
if (big_button(&select_lpc_point_size, win, left, 0, textual_icons::grid_6,
"Point Size", true, true,
"Labeled Point Cloud"))
{
ImGui::OpenPopup(lpc_popup);
}
ImGui::SetNextWindowPos({ cursor.x + left + 5, cursor.y + 60 });
if (ImGui::BeginPopup(lpc_popup))
{
select_lpc_point_size = true;
bool selected = selected_lpc_points_size == lpc_points_size::lpc_small;
if (ImGui::MenuItem("Small", nullptr, &selected))
{
if (selected) selected_lpc_points_size = lpc_points_size::lpc_small;
}
selected = selected_lpc_points_size == lpc_points_size::lpc_medium;
if (ImGui::MenuItem("Medium", nullptr, &selected))
{
if (selected) selected_lpc_points_size = lpc_points_size::lpc_medium;
}
selected = selected_lpc_points_size == lpc_points_size::lpc_large;
if (ImGui::MenuItem("Large", nullptr, &selected))
{
if (selected) selected_lpc_points_size = lpc_points_size::lpc_large;
}
config_file::instance().set(configurations::viewer::lpc_point_size, static_cast<int>(selected_lpc_points_size));
ImGui::EndPopup();
}
else
{
select_lpc_point_size = false;
}
left += 80;
if (show_safety_zones_3d)
{
bool active = true;
if (big_button(&active, win, left, 0, textual_icons::draw_polygon,
"S. Zones", false, true,
"Show/hide Safety Zones"))
{
show_safety_zones_3d = false;
config_file::instance().set(configurations::viewer::show_safety_zones_3d, show_safety_zones_3d);
}
}
else
{
bool active = false;
if (big_button(&active, win, left, 0, textual_icons::draw_polygon,
"S. Zones", false, true,
"Show/hide Safety Zones"))
{
show_safety_zones_3d = true;
config_file::instance().set(configurations::viewer::show_safety_zones_3d, show_safety_zones_3d);
}
}
}
ImGui::PopStyleColor(5);
ImGui::PopFont();
}
void viewer_model::check_permissions()
{
#ifdef __linux__
if (directory_exists("/etc/udev/rules.d") || directory_exists("/lib/udev/rules.d/"))
{
const std::string udev_rules_man("/etc/udev/rules.d/99-realsense-libusb.rules");
const std::string udev_rules_deb("/lib/udev/rules.d/60-librealsense2-udev-rules.rules");
std::ifstream f_man(udev_rules_man);
std::ifstream f_deb(udev_rules_deb);
std::string message = "UDEV-Rules permissions configuration \n for RealSense devices.`\n"
"Missing/outdated UDEV-Rules will cause 'Permissions Denied' errors\nunless the application is running under 'sudo' (not recommended)\n"
"In case of Debians use: \n"
"sudo apt-get upgrade/install librealsense2-udev-rules\n"
"To manually install UDEV-Rules in terminal run:\n"
"$ sudo cp ~/.99-realsense-libusb.rules /etc/udev/rules.d/99-realsense-libusb.rules && sudo udevadm control --reload-rules && udevadm trigger\n";
bool create_file = false;
if(!(f_man.good() || f_deb.good()))
{
message = "RealSense UDEV-Rules are missing!\n" + message;
auto n = not_model->add_notification({ message,
RS2_LOG_SEVERITY_WARN,
RS2_NOTIFICATION_CATEGORY_COUNT });
create_file = true;
n->enable_complex_dismiss = true;
n->delay_id = "missing-udev";
if (n->is_delayed()) n->dismiss(true);
}
else
{
std::ifstream f;
std::string udev_fname;
if(f_man.good())
{
if (f_deb.good())
{
std::string duplicates = "Multiple realsense udev-rules were found! :\n1:" + udev_rules_man
+ "\n2: " + udev_rules_deb+ "\nMake sure to remove redundancies!";
auto n = not_model->add_notification({ duplicates,
RS2_LOG_SEVERITY_WARN,
RS2_NOTIFICATION_CATEGORY_COUNT });
n->enable_complex_dismiss = true;
n->delay_id = "multiple-udev";
if (n->is_delayed()) n->dismiss(true);
}
f.swap(f_man);
udev_fname = udev_rules_man;
create_file = true;
}
else
{
f.swap(f_deb);
udev_fname = udev_rules_deb;
}
const std::string str((std::istreambuf_iterator<char>(f)),
std::istreambuf_iterator<char>());
// The generated array 'realsense_udev_rules' is not NUL-terminated...
std::string tmp = std::string(realsense_udev_rules, sizeof(realsense_udev_rules));
tmp.erase(tmp.find_last_of("\n") + 1);
const std::string udev = tmp;
float udev_file_ver{0}, built_in_file_ver{0};
// The udev-rules file shall start with version token expressed as ##Version=xx.yy##
std::regex udev_ver_regex("^##Version=(\\d+\\.\\d+)##");
std::smatch match;
if (std::regex_search(udev.begin(), udev.end(), match, udev_ver_regex))
built_in_file_ver = std::stof(std::string(match[1]));
if (std::regex_search(str.begin(), str.end(), match, udev_ver_regex))
udev_file_ver = std::stof(std::string(match[1]));
if (built_in_file_ver > udev_file_ver)
{
std::stringstream s;
s << "RealSense UDEV-Rules file:\n " << udev_fname <<"\n is not up-to date! Version " << built_in_file_ver << " can be applied\n";
auto n = not_model->add_notification({
s.str() + message,
RS2_LOG_SEVERITY_WARN,
RS2_NOTIFICATION_CATEGORY_COUNT });
n->enable_complex_dismiss = true;
n->delay_id = "udev-version";
if (n->is_delayed()) n->dismiss(true);
}
}
if (create_file)
{
std::string tmp_filename
= rsutils::os::get_special_folder( rsutils::os::special_folder::app_data ) // ~/.
+ "99-realsense-libusb.rules";
std::ofstream out(tmp_filename.c_str());
std::string tmp = std::string(realsense_udev_rules, sizeof(realsense_udev_rules));
out << tmp;
out.close();
}
}
// NVIDIA Jetson: hint the user when the viewer cannot benefit from CUDA acceleration.
// /etc/nv_tegra_release is the canonical L4T marker for Jetson platforms.
// Compile-time (RS2_USE_CUDA) and runtime (rs2_is_cuda_available) are kept separate:
// a CUDA-enabled build whose runtime fails to initialize (e.g. broken/mismatched CUDA
// stack) must not be told to "rebuild with CUDA" — the real problem is at runtime.
if (std::ifstream("/etc/nv_tegra_release").good())
{
#ifdef RS2_USE_CUDA
// Built with CUDA. If the runtime cannot enumerate a GPU, surface a runtime-stack hint.
if (!rsutils::rs2_is_cuda_available())
{
std::string message = "Running on NVIDIA Jetson and realsense-viewer was built with CUDA,\n"
"but the CUDA runtime failed to initialize (no GPU device reported).\n"
"Check the CUDA driver/library stack on this device;\n"
"see the SDK log for the cudaGetDeviceCount error.";
auto n = not_model->add_notification({ message,
RS2_LOG_SEVERITY_WARN,
RS2_NOTIFICATION_CATEGORY_COUNT });
n->enable_complex_dismiss = true;
n->delay_id = "jetson-cuda-runtime-init-failed";
n->width = 400; // wider than default 320 so the message is not truncated
if (n->is_delayed()) n->dismiss(true);
}
// else: built with CUDA + GPU available -> silent (case 4)
#else
// Built without CUDA: distinguish "runtime not installed" from "runtime installed but unused".
// /usr/local/cuda is the canonical L4T / JetPack install location for the CUDA runtime
// (normally a symlink to /usr/local/cuda-X.Y). A non-standard install will produce a
// false-positive "install runtime" popup — acceptable for a startup hint.
if (!directory_exists("/usr/local/cuda"))
{
std::string message = "Running on NVIDIA Jetson without the CUDA runtime installed.\n"
"For better performance, install the CUDA runtime via the NVIDIA JetPack SDK.";
auto n = not_model->add_notification({ message,
RS2_LOG_SEVERITY_WARN,
RS2_NOTIFICATION_CATEGORY_COUNT });
n->enable_complex_dismiss = true;
n->delay_id = "jetson-cuda-runtime-missing";
n->width = 400; // wider than default 320 so the message is not truncated
if (n->is_delayed()) n->dismiss(true);
}
else
{
std::string message = "Running on NVIDIA Jetson with the CUDA runtime installed,\n"
"but realsense-viewer is not using CUDA.\n"
"For better performance, rebuild librealsense with -DBUILD_WITH_CUDA=ON.";
auto n = not_model->add_notification({ message,
RS2_LOG_SEVERITY_INFO,
RS2_NOTIFICATION_CATEGORY_COUNT });
n->enable_complex_dismiss = true;
n->delay_id = "jetson-cuda-not-used";
n->width = 400; // wider than default 320 so the message is not truncated
if (n->is_delayed()) n->dismiss(true);
}
#endif
}
#endif
}
// Hide options from both the DQT and Viewer applications
void viewer_model::hide_common_options()
{
_hidden_options.emplace(RS2_OPTION_STREAM_FILTER);
_hidden_options.emplace(RS2_OPTION_STREAM_FORMAT_FILTER);
_hidden_options.emplace(RS2_OPTION_STREAM_INDEX_FILTER);
_hidden_options.emplace(RS2_OPTION_FRAMES_QUEUE_SIZE);
_hidden_options.emplace(RS2_OPTION_NOISE_ESTIMATION);
_hidden_options.emplace(RS2_OPTION_REGION_OF_INTEREST);
_hidden_options.emplace(RS2_OPTION_READOUT_SHAPING);
// Rendered as a "more" popup Selectable in device-model.cpp instead of a sensor
// control, so it doesn't need to appear in the sensor's Controls tree.
_hidden_options.emplace(RS2_OPTION_SENSORS_CONFIG_MODE);
}
void viewer_model::update_configuration(config_file* new_cfg)
{
if (new_cfg)
config_file::instance() = *new_cfg;
rs2_error* e = nullptr;
auto version = rs2_get_api_version(&e);
if (e) rs2::error::handle(e);
int saved_version = config_file::instance().get_or_default(
configurations::viewer::sdk_version, 0);
// Great the user once upon upgrading to a new version
if (version > saved_version)
{
auto n = std::make_shared<version_upgrade_model>(version);
not_model->add_notification(n);
config_file::instance().set(configurations::viewer::sdk_version, version);
}
continue_with_current_fw = config_file::instance().get_or_default(
configurations::viewer::continue_with_current_fw, false);
is_3d_view = config_file::instance().get_or_default(
configurations::viewer::is_3d_view, true);
if (bool measurement_enabled = config_file::instance().get_or_default(
configurations::viewer::is_measuring, false))
_measurements.enable();
_measurements.log_function = [this](std::string message) { not_model->add_log(message); };
_measurements.is_metric = [this]() { return metric_system; };
glsl_available = config_file::instance().get(
configurations::performance::glsl_for_rendering);
occlusion_invalidation = config_file::instance().get_or_default(
configurations::performance::occlusion_invalidation, true);
ground_truth_r = config_file::instance().get_or_default(
configurations::viewer::ground_truth_r, 1200);
selected_shader = (shader_type)config_file::instance().get_or_default(
configurations::viewer::shading_mode, 2);
#ifdef BUILD_EASYLOGGINGPP
auto min_severity = (rs2_log_severity)config_file::instance().get_or_default(
configurations::viewer::log_severity, 2);
if( ! _disable_log_to_console )
{
if( config_file::instance().get_or_default(
configurations::viewer::log_to_console, false ) )
{
rs2::log_to_console( min_severity );
}
else
{
rs2::log_to_console( RS2_LOG_SEVERITY_NONE );
}