-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathViewerWindow.cpp
943 lines (826 loc) · 34.1 KB
/
ViewerWindow.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
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
#ifdef __APPLE__
#define GL_SILENCE_DEPRECATION
#endif
#include <filesystem>
#include <fstream>
#include <regex>
#include <iomanip>
#include <imgui.h>
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>
#include <nfd.h>
#include <nlohmann/json.hpp>
#include "ViewerWindow.h"
#include "ROMParser.h"
#include "AppIcon.inc"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
namespace fs = std::filesystem;
double GetCurrentUnixTimestamp() {
auto now = std::chrono::system_clock::now();
auto duration = now.time_since_epoch();
return std::chrono::duration<double>(duration).count();
}
void ViewerWindow::SaveToolDefinition(const Tool &tool)
{
fs::path toolsDir("Tools");
if (!fs::exists(toolsDir))
{
fs::create_directories(toolsDir); // Create the Tools directory if it doesn't exist
}
// Construct the filename using the tool name
fs::path filePath = toolsDir / (tool.toolName + ".json");
nlohmann::json toolJson;
toolJson["numSpheres"] = tool.numSpheres;
toolJson["spherePositions"] = tool.spherePositions;
toolJson["sphereRadius"] = tool.sphereRadius;
toolJson["toolName"] = tool.toolName;
std::ofstream file(filePath);
if (file)
{
file << toolJson.dump(4); // Save the JSON with indentation for readability
}
else
{
std::cerr << "Failed to save tool definition: " << tool.toolName << std::endl;
}
}
bool ViewerWindow::LoadToolDefinition()
{
fs::path toolDir("Tools");
if (!fs::exists(toolDir) || !fs::is_directory(toolDir))
{
std::cerr << "Tool directory not found." << std::endl;
return false;
}
tools.clear(); // Clear existing tools
for (const auto &entry : fs::directory_iterator(toolDir))
{
if (entry.path().extension() == ".json")
{
std::ifstream file(entry.path());
if (!file)
{
std::cerr << "Failed to open " << entry.path() << std::endl;
continue;
}
nlohmann::json toolJson;
file >> toolJson;
Tool tool;
tool.numSpheres = toolJson["numSpheres"].get<int>();
tool.spherePositions = toolJson["spherePositions"].get<std::vector<float>>();
tool.sphereRadius = toolJson["sphereRadius"].get<float>();
tool.toolName = toolJson["toolName"].get<std::string>();
if (!file)
{
std::cerr << "Failed to read data from " << entry.path() << std::endl;
continue;
}
std::cout << "Loaded tool definition: " << tool.toolName << std::endl;
tools.push_back(std::move(tool));
numTools += 1;
}
}
if (tools.empty())
{
numTools = 1;
return false;
}
return true;
}
void ViewerWindow::Initialize(const std::string& file) {
recordedFile = file;
const int init_result = glfwInit();
if (init_result != GLFW_TRUE) {
std::cerr <<"glfwInit failed" << std::endl;
return;
}
// Decide GL+GLSL versions
#if defined(IMGUI_IMPL_OPENGL_ES2)
// GL ES 2.0 + GLSL 100
const char* glsl_version = "#version 100";
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
#elif defined(__APPLE__)
// GL 3.2 + GLSL 150
const char* glsl_version = "#version 150";
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // Required on Mac
#else
// GL 3.3 + GLSL 330
const char* glsl_version = "#version 330";
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
//glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only
//glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // 3.0+ only
#endif
window = glfwCreateWindow( 1060, 720, "RealSense Tool Tracker", nullptr, nullptr );
if (window == nullptr)
{
std::cerr <<"glfwCreateWindow failed" << std::endl;
return;
}
GLFWimage icon;
icon.pixels = stbi_load_from_memory(
AppIcon_png,
AppIcon_png_len,
&icon.width,
&icon.height,
nullptr,
4); // RGBA
glfwSetWindowIcon(window, 1, &icon);
free(icon.pixels);
glfwMakeContextCurrent( window );
glfwSwapInterval( 1 );
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGui::StyleColorsDark();
ImGui_ImplGlfw_InitForOpenGL( window, true );
ImGui_ImplOpenGL3_Init( glsl_version);
// Set window close callback
glfwSetWindowCloseCallback(window, [](GLFWwindow* window) {
glfwSetWindowShouldClose(window, GL_TRUE);
});
LoadToolDefinition();
tracker.RemoveAllToolDefinitions();
Terminated = false;
Render();
}
Eigen::Matrix4f ViewerWindow::TrackingDataToMatrix(const TrackingData& data)
{
Eigen::Quaternionf q(data.quaternion[3], data.quaternion[0], data.quaternion[1], data.quaternion[2]);
Eigen::Vector3f t(data.position[0], data.position[1], data.position[2]);
Eigen::Matrix4f matrix = Eigen::Matrix4f::Identity();
matrix.block<3, 3>(0, 0) = q.toRotationMatrix();
matrix.block<3, 1>(0, 3) = t;
return matrix;
}
bool ViewerWindow::Connect(NanoSocket& _socket, NanoAddress& address, const char *host, int port, bool& _connected) {
if (udpEnabled != multiEnabled)
{
if (nanosockets_initialize())
{
std::cerr << "Error initializing socket library" << std::endl;
return false;
}
}
_socket = nanosockets_create(1024, 1024);
if (socket < 0)
{
std::cerr << "Failed to create a socket." << std::endl;
return false;
}
address.port = static_cast<uint16_t>(port);
if (!host || std::strlen(host) == 0)
{
if (nanosockets_address_set_ip(&address, "127.0.0.1"))
{
std::cerr<<"Error setting default address"<<std::endl;
return false;
}
}
else
{
if (nanosockets_address_set_hostname(&address, host))
{
std::cerr << "Error setting hostname to " << host << std::endl;
return false;
}
}
_connected = true;
return true;
}
void ViewerWindow::Disconnect(NanoSocket& _socket, bool& _connected)
{
if (_connected)
{
nanosockets_destroy(&_socket);
if (!multiEnabled && !udpEnabled)
{
nanosockets_deinitialize();
}
_connected = false;
}
}
void ViewerWindow::UdpReceiveThreadFunction()
{
if (!Connect(receiveSocket, receiveAddress, "0.0.0.0", m_receiveport, m_receiveconnected))
{
std::cerr << "Failed to connect to server." << std::endl;
multiEnabled = false;
return;
}
receiveAddress.port = static_cast<uint16_t>(m_receiveport);
if (nanosockets_bind(receiveSocket, &receiveAddress))
{
std::cerr << "Failed to bind to port " << m_port << std::endl;
multiEnabled = false;
Disconnect(receiveSocket, m_receiveconnected);
return;
}
// Set the socket to non-blocking mode
if (nanosockets_set_nonblocking(receiveSocket, 1) != NANOSOCKETS_STATUS_OK) {
std::cerr << "Failed to set non-blocking mode" << std::endl;
Disconnect(receiveSocket, m_receiveconnected);
return;
}
while (multiEnabled)
{
NanoAddress sender;
std::vector<uint8_t> buffer(sizeof(TrackingData));
//toolTransforms.clear();
if (nanosockets_receive(receiveSocket, &sender, buffer.data(), buffer.size()) > 0)
{
TrackingData data;
std::memcpy(&data, buffer.data(), sizeof(TrackingData));
//std::cout << "Received tracking data for tool ID " << data.toolId << " from " << data.serialNumber <<std::endl;
//print received address
//char ip[16];
//nanosockets_address_get_ip(&sender, ip, sizeof(ip));
//std::cout << "Received from " << ip << ":" << sender.port << std::endl;
if (tracker.IsTrackingTools())
{
for (size_t id = 0; id < tools.size(); ++id)
{
if (data.toolId == static_cast<int>(id + 1))
{
// Always update extrinsics in case camera is moved
std::vector<float> tool_transform = tracker.GetToolTransform(tools[id].toolName);
if (!tool_transform.empty() && !std::isnan(tool_transform[0]) && tool_transform[7] != 0)
{
// tool_transform to matrix
Eigen::Matrix4f transform = Eigen::Matrix4f::Identity();
transform.block<3, 3>(0, 0) = Eigen::Quaternionf(tool_transform[6], tool_transform[3], tool_transform[4], tool_transform[5]).toRotationMatrix();
transform.block<3, 1>(0, 3) = Eigen::Vector3f(tool_transform[0], tool_transform[1], tool_transform[2]);
extrinsics[data.serialNumber] = transform * TrackingDataToMatrix(data).inverse();
}
// check if serial number is in extrinsics
if (extrinsics.find(data.serialNumber) == extrinsics.end())
{
continue;
}
Eigen::Matrix4f extrinsic = extrinsics[data.serialNumber];
Eigen::Matrix4f new_transform = extrinsic * TrackingDataToMatrix(data);
{
std::lock_guard<std::mutex> lock(secondaryDataMutex);
toolTransforms[id] = new_transform;
}
}
}
}
}
}
multiEnabled = false;
std::cout << "Exiting UDP receive thread" << std::endl;
Disconnect(receiveSocket, m_receiveconnected);
}
void ViewerWindow::UdpThreadFunction()
{
if (!Connect(socket, sendAddress, ipAddress, m_port, m_connected))
{
std::cerr << "Failed to connect to server." << std::endl;
udpEnabled = false;
return;
}
while (udpEnabled)
{
if (tracker.IsTrackingTools())
{
for (size_t id = 0; id < tools.size(); ++id)
{
const auto &tool = tools[id];
std::vector<float> tool_transform = tracker.GetToolTransform(tool.toolName);
bool validPrimaryData = !tool_transform.empty() && !std::isnan(tool_transform[0]) && tool_transform[7] != 0;
bool validSecondaryData = false;
if (!validPrimaryData && multiEnabled)
{
std::lock_guard<std::mutex> lock(secondaryDataMutex);
if (toolTransforms.find(id) != toolTransforms.end())
{
const Eigen::Matrix4f& secondaryData = toolTransforms[id];
Eigen::Quaternionf q(secondaryData.block<3, 3>(0, 0));
tool_transform = { secondaryData(0, 3), secondaryData(1, 3), secondaryData(2, 3),
q.x(), q.y(), q.z(), q.w() };
toolTransforms.clear();
validSecondaryData = true;
}
}
if (validPrimaryData || validSecondaryData)
{
TrackingData data;
std::copy(tool_transform.begin(), tool_transform.begin() + 3, data.position);
std::copy(tool_transform.begin() + 3, tool_transform.end(), data.quaternion);
data.toolId = static_cast<int>(id) + 1;
data.timestamp = GetCurrentUnixTimestamp();
data.serialNumber = serialNumber;
if (m_connected) {
// Serialize data
std::vector<uint8_t> buffer(sizeof(TrackingData));
std::memcpy(buffer.data(), &data, sizeof(TrackingData));
// Send the serialized data
if (nanosockets_send(socket, &sendAddress, buffer.data(), buffer.size()) < 0)
{
std::cerr << "Failed to send tracking data for tool ID " << data.toolId << std::endl;
}
}
}
}
}
int sleepDurationMs = 1000 / frequency; // Convert frequency to sleep duration in milliseconds
std::this_thread::sleep_for(std::chrono::milliseconds(sleepDurationMs));
}
Disconnect(socket, m_connected);
}
void ViewerWindow::WriteToCSV()
{
// Check if csvFileName exists, if so, add a number to the end of the filename
fs::path basePath(csvFileName);
std::string stem = basePath.stem().string();
std::string extension = basePath.extension().string();
int count = 1;
fs::path newFilePath = basePath;
while (fs::exists(newFilePath)) {
newFilePath = basePath.parent_path() / (stem + std::to_string(count) + extension);
count++;
}
std::cout << "Saving to " << newFilePath << std::endl;
std::ofstream file(newFilePath);
if (!file)
{
std::cerr << "Failed to open csv" << std::endl;
return;
}
file << "Serial Number,Tool ID,Timestamp,Position X,Position Y,Position Z,Quaternion X,Quaternion Y,Quaternion Z,Quaternion W\n";
std::chrono::steady_clock::time_point start;
bool timerStarted = false;
while (csvEnabled)
{
if (tracker.IsTrackingTools())
{
if (!timerStarted)
{
start = std::chrono::steady_clock::now(); // Start the timer
timerStarted = true;
}
else
{
auto now = std::chrono::steady_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::seconds>(now - start);
if (elapsed.count() >= duration) // Check if duration has been exceeded
{
break; // Exit loop
}
}
for (size_t id = 0; id < tools.size(); ++id)
{
const auto& tool = tools[id];
std::vector<float> tool_transform = tracker.GetToolTransform(tool.toolName);
if (!tool_transform.empty() && !std::isnan(tool_transform[0]) && tool_transform[7] != 0)
{
double unixTimestamp = GetCurrentUnixTimestamp();
file << std::fixed << std::setprecision(6) << serialNumber << "," << id + 1 << "," << unixTimestamp << ",";
for (size_t i = 0; i < tool_transform.size() - 1; ++i)
{
file << tool_transform[i];
if (i < tool_transform.size() - 2)
{
file << ",";
}
}
file << "\n";
}
}
}
int sleepDurationMs = 1000 / recordFrequency;
std::this_thread::sleep_for(std::chrono::milliseconds(sleepDurationMs));
}
file.close();
csvEnabled = false;
finishedRecord = true;
}
void ViewerWindow::GetSerialNumber()
{
std::regex re("\\b(\\d{12})\\b"); // Regular expression to match a 12-digit number
std::smatch match;
if (std::regex_search(currentDevice, match, re) && match.size() > 0) {
std::string numberStr = match.str(0); // The first match in the string
// Convert string to long long
std::istringstream iss(numberStr);
iss >> serialNumber;
}
}
void ViewerWindow::Render() {
// Generate textures
glGenTextures(1, &texture);
glGenTextures(1, &dtexture);
ImGuiWindowFlags overlayFlags = ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar |
ImGuiWindowFlags_NoResize | ImGuiWindowFlags_AlwaysAutoResize |
ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing |
ImGuiWindowFlags_NoNav | ImGuiWindowFlags_NoScrollbar;
tracker.queryDevices();
auto& deviceList = tracker.getDeviceList();
static int selectedDeviceIndex = deviceList.size() > 0 || recordedFile != "" ? 0: -1;
currentDevice = deviceList.size() > 0 ? deviceList[0] : "No Device Available";
GetSerialNumber();
float windowWidth = 350.0f;
// Main loop for GUI operations
while (!Terminated && !glfwWindowShouldClose(window)) {
glfwPollEvents();
glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
glClear( GL_COLOR_BUFFER_BIT );
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
// Create ImGui window
ImGui::SetNextWindowPos(ImVec2(20, 20), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowSize(ImVec2(windowWidth, 0.0f));
ImGui::Begin("Device Selection", nullptr, overlayFlags);
// Dropdown menu for devices
if (ImGui::BeginCombo("Devices", currentDevice.c_str())) {
for (int i = 0; i < static_cast<int>(deviceList.size()); i++) {
bool isSelected = (currentDevice == deviceList[i]);
if (ImGui::Selectable(deviceList[i].c_str(), isSelected)) {
currentDevice = deviceList[i];
selectedDeviceIndex = i;
std::cout << "Selected device: " << currentDevice << std::endl;
GetSerialNumber();
}
// Set the initial focus when opening the combo (scrolling + keyboard navigation focus)
if (isSelected) {
ImGui::SetItemDefaultFocus();
}
}
ImGui::EndCombo();
}
ImGui::SameLine();
if (ImGui::Button("Refresh")) {
tracker.queryDevices();
deviceList = tracker.getDeviceList();
if (deviceList.size() > 0) {
currentDevice = deviceList[0];
selectedDeviceIndex = 0;
GetSerialNumber();
}
else
{
currentDevice = "No Device Available";
selectedDeviceIndex = -1;
}
}
ImGui::End();
// Number of Tools Window
ImGui::SetNextWindowPos(ImVec2(20, 60), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowSize(ImVec2(windowWidth, 0.0f));
ImGui::Begin("Tools to Track", nullptr, overlayFlags);
ImGui::InputInt("Number of Tools", &numTools);
ImGui::End();
// Adjust the size of the tools vector based on numTools
numTools = std::max(numTools, 1);
if (static_cast<int>(tools.size()) != numTools)
{
tools.resize(numTools);
}
ImGui::SetNextWindowPos(ImVec2(20, 100), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowSize(ImVec2(windowWidth, 0.0f));
ImGui::Begin("Tool Definitions", nullptr, overlayFlags);
for (int toolIdx = 0; toolIdx < numTools; ++toolIdx)
{
tools[toolIdx].toolName = tools[toolIdx].toolName == "Tool" ? "Tool" + std::to_string(toolIdx + 1) : tools[toolIdx].toolName;
ImGui::SetNextItemOpen(true, ImGuiCond_Once);
if (ImGui::CollapsingHeader(("Tool " + std::to_string(toolIdx + 1)).c_str()))
{
ImGui::InputInt(("Number of Spheres##" + std::to_string(toolIdx)).c_str(), &tools[toolIdx].numSpheres);
ImGui::SetNextItemWidth(80);
ImGui::InputFloat(("Sphere Radius##" + std::to_string(toolIdx)).c_str(), &tools[toolIdx].sphereRadius);
ImGui::SameLine();
ImGui::SetNextItemWidth(80);
char tmpName[MAX_TOOL_NAME_LENGTH];
strncpy(tmpName, tools[toolIdx].toolName.c_str(), MAX_TOOL_NAME_LENGTH - 1);
tmpName[MAX_TOOL_NAME_LENGTH - 1] = '\0'; // Ensure null-termination
ImGui::InputText(("Tool Name##" + std::to_string(toolIdx)).c_str(), tmpName, MAX_TOOL_NAME_LENGTH);
tools[toolIdx].toolName = tmpName;
tools[toolIdx].numSpheres = std::max(tools[toolIdx].numSpheres, 4);
tools[toolIdx].spherePositions.resize(tools[toolIdx].numSpheres * 3);
for (int i = 0; i < tools[toolIdx].numSpheres; ++i)
{
std::ostringstream label;
label << "Sphere " << (i + 1) << " Position##" << toolIdx;
ImGui::InputFloat3(label.str().c_str(), &tools[toolIdx].spherePositions[i * 3]);
}
if (!tools[toolIdx].isAdded)
{
if (ImGui::Button(("Add Tool Definition##" + std::to_string(toolIdx)).c_str()))
{
tracker.AddToolDefinition(tools[toolIdx].numSpheres, tools[toolIdx].spherePositions, tools[toolIdx].sphereRadius, tools[toolIdx].toolName);
SaveToolDefinition(tools[toolIdx]);
tools[toolIdx].isAdded = true;
}
}
else
{
isToolAdded = true;
if (ImGui::Button(("Remove Tool Definition##" + std::to_string(toolIdx)).c_str()))
{
tracker.RemoveToolDefinition(tools[toolIdx].toolName);
tools[toolIdx].isAdded = false;
isToolAdded = false;
}
}
if (!tools[toolIdx].isAdded)
{
ImGui::SameLine();
if (ImGui::Button(("Calibrate Tool##" + std::to_string(toolIdx)).c_str()))
{
std::cout << "Calibrating tool " << toolIdx + 1 << std::endl;
if (recordedFile != "")
{
tracker.initializeFromFile(recordedFile);
}
else
{
tracker.initialize(selectedDeviceIndex, 848, 480);
}
tracker.StartToolCalibration();
calibrationInitiated = true;
toolId = toolIdx;
processingThread = std::make_shared<std::thread>([this]()
{ this->tracker.processStreams(); });
}
ImGui::SameLine();
if (ImGui::Button(("Load ROM##" + std::to_string(toolIdx)).c_str()))
{
NFD_Init();
nfdchar_t* path = nullptr;
nfdfilteritem_t filterItem[1] = { { "NDI rom file", "rom" }};
nfdresult_t result = NFD_OpenDialog(&path, filterItem, 1, NULL);
if (result == NFD_OKAY && path) {
ROMParser parser(path);
tools[toolIdx].numSpheres = parser.GetNumMarkers();
tools[toolIdx].spherePositions = parser.GetMarkerPositions();
NFD_FreePath(path);
}
NFD_Quit();
}
}
}
}
ImGui::End();
if (!tracker.IsCalibratingTool() && calibrationInitiated)
{
std::vector<float> tool_definition = tracker.GetToolDefinition();
if (!tool_definition.empty() && !std::isnan(tool_definition[0]) && tool_definition[0] != -1)
{
tools[toolId].numSpheres = tool_definition.size() / 3;
tools[toolId].spherePositions = tool_definition;
}
tracker.StopToolCalibration();
JoinThread(processingThread);
tracker.shutdown();
calibrationInitiated = false;
}
if (isToolAdded && selectedDeviceIndex != -1)
{
ImGui::SetNextWindowPos(ImVec2(400, 100), ImGuiCond_FirstUseEver);
ImGui::Begin("Tracking Control", nullptr, overlayFlags);
if (!tracker.IsTrackingTools()) {
if (ImGui::Button("Start Tracking")) {
if (recordedFile != "")
{
tracker.initializeFromFile(recordedFile);
}
else
{
tracker.initialize(selectedDeviceIndex, 848, 480);
tracker.getLaserPower(laserPower, minlasPower, maxlasPower);
}
tracker.StartToolTracking();
irThreshold = tracker.GetThreshold();
tracker.GetMinMaxSize(minPixelSize, maxPixelSize);
processingThread = std::make_shared<std::thread>([this]() {
this->tracker.processStreams();
});
}
} else {
if (ImGui::Button("Stop Tracking")) {
tracker.StopToolTracking();
JoinThread(processingThread);
tracker.shutdown();
}
}
ImGui::End();
}
// Add a slider for setting the IR threshold
ImGui::SetNextWindowPos(ImVec2(400, 20), ImGuiCond_FirstUseEver);
ImGui::Begin("IR Threshold Control", nullptr, overlayFlags);
// if not on Mac
#if !defined(__APPLE__)
{
if(ImGui::SliderInt("Laser Power", &laserPower, minlasPower, maxlasPower))
{
tracker.setLaserPower(laserPower);
}
}
#endif
if(ImGui::SliderInt("IR Threshold", &irThreshold, 0, 255))
{
tracker.SetThreshold(irThreshold);
}
ImGui::SetNextItemWidth(100);
if (ImGui::InputInt("Min Px", &minPixelSize))
{
tracker.SetMinMaxSize(minPixelSize, maxPixelSize);
}
ImGui::SetNextItemWidth(100);
ImGui::SameLine();
if (ImGui::InputInt("Max Px", &maxPixelSize))
{
tracker.SetMinMaxSize(minPixelSize, maxPixelSize);
}
ImGui::End();
// Checkbox for enabling UDP
ImGui::SetNextWindowSize(ImVec2(300, 0.0f));
ImGui::SetNextWindowPos(ImVec2(740, 20), ImGuiCond_FirstUseEver);
ImGui::Begin("UDP Settings", nullptr, overlayFlags);
ImGui::SetNextItemWidth(110);
ImGui::InputText("IP Address", ipAddress, sizeof(ipAddress));
ImGui::SameLine();
ImGui::SetNextItemWidth(50);
ImGui::InputInt("Port", &m_port, 0, 0, ImGuiInputTextFlags_CharsDecimal);
ImGui::SetNextItemWidth(110);
ImGui::InputInt("Frequency", &frequency);
ImGui::SameLine();
if (ImGui::Checkbox("UDP", &udpEnabled))
{
if (udpEnabled)
{
udpThread = std::make_shared<std::thread>(&ViewerWindow::UdpThreadFunction, this);
}
else
{
JoinThread(udpThread);
}
}
ImGui::End();
frequency = std::max(frequency, 1);
ImGui::SetNextWindowSize(ImVec2(300, 0.0f));
ImGui::SetNextWindowPos(ImVec2(740, 80), ImGuiCond_FirstUseEver);
ImGui::Begin("Multi-Camera Settings", nullptr, overlayFlags);
if (ImGui::Checkbox("Multi-Camera", &multiEnabled))
{
if (multiEnabled)
{
udpReceiveThread = std::make_shared<std::thread>(&ViewerWindow::UdpReceiveThreadFunction, this);
}
else
{
JoinThread(udpReceiveThread);
}
}
ImGui::SameLine();
ImGui::SetNextItemWidth(50);
ImGui::InputInt("Receive Port", &m_receiveport, 0, 0, ImGuiInputTextFlags_CharsDecimal);
ImGui::End();
// GUI for CSV recording
ImGui::SetNextWindowSize(ImVec2(300, 0.0f));
ImGui::SetNextWindowPos(ImVec2(740, 120), ImGuiCond_FirstUseEver);
ImGui::Begin("CSV Recording", nullptr, overlayFlags);
ImGui::SetNextItemWidth(70);
ImGui::InputInt("Frequency", &recordFrequency);
ImGui::SameLine();
ImGui::SetNextItemWidth(80);
ImGui::InputInt("Duration", &duration);
ImGui::SetNextItemWidth(110);
if (ImGui::Button("Save To"))
{
NFD_Init();
nfdchar_t* path = nullptr;
nfdfilteritem_t filterItem[1] = { { "CSV file", "csv" } };
nfdresult_t result = NFD_SaveDialog(&path, filterItem, 1, NULL, NULL);
if (result == NFD_OKAY && path) {
csvFileName = path;
NFD_FreePath(path);
}
NFD_Quit();
}
ImGui::SameLine();
if (ImGui::Checkbox("Record", &csvEnabled))
{
if (csvEnabled)
{
csvThread = std::make_shared<std::thread>(&ViewerWindow::WriteToCSV, this);
}
else
{
JoinThread(csvThread);
}
}
ImGui::End();
recordFrequency = std::min(std::max(recordFrequency, 1), 90);
duration = std::max(duration, 1);
if (finishedRecord)
{
JoinThread(csvThread);
finishedRecord = false;
}
cv::Mat frame = tracker.getNextIRFrame();
cv::Mat depth = tracker.getNextDepthFrame();
if (!depth.empty() && (tracker.IsTrackingTools() || calibrationInitiated))
{
int windowWidth, windowHeight;
glfwGetWindowSize(window, &windowWidth, &windowHeight);
ImGui::SetNextWindowPos(ImVec2(20, windowHeight - 300));
ImGui::Begin("IR Monitor", nullptr, overlayFlags);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, frame.cols, frame.rows, 0, GL_RGB, GL_UNSIGNED_BYTE, frame.data);
ImGui::Image(reinterpret_cast<void *>(static_cast<intptr_t>(texture)), ImVec2(424, 240));
ImGui::End();
ImGui::SetNextWindowPos(ImVec2(480, windowHeight - 300));
ImGui::Begin("Depth Monitor", nullptr, overlayFlags);
glBindTexture(GL_TEXTURE_2D, dtexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, depth.cols, depth.rows, 0, GL_RGB, GL_UNSIGNED_BYTE, depth.data);
ImGui::Image(reinterpret_cast<void *>(static_cast<intptr_t>(dtexture)), ImVec2(424, 240));
ImGui::End();
}
if (tracker.IsTrackingTools())
{
ImGui::SetNextWindowPos(ImVec2(400, 200), ImGuiCond_FirstUseEver);
ImGui::Begin("Tool Ttacking Results", nullptr, overlayFlags);
for (size_t i = 0; i < tools.size(); ++i)
{
const auto &tool = tools[i];
std::vector<float> tool_transform = tracker.GetToolTransform(tool.toolName);
bool validPrimaryData = !tool_transform.empty() && !std::isnan(tool_transform[0]) && tool_transform[7] != 0;
bool validSecondaryData = false;
if (!validPrimaryData && multiEnabled)
{
std::lock_guard<std::mutex> lock(secondaryDataMutex);
if (toolTransforms.find(i) != toolTransforms.end())
{
const Eigen::Matrix4f& secondaryData = toolTransforms[i];
Eigen::Quaternionf q(secondaryData.block<3, 3>(0, 0));
tool_transform = { secondaryData(0, 3), secondaryData(1, 3), secondaryData(2, 3),
q.x(), q.y(), q.z(), q.w() };
toolTransforms.clear();
validSecondaryData = true;
}
}
// Check if the tool transform is valid
if (validPrimaryData || validSecondaryData)
{
// Change text color based on the data source
if (validPrimaryData) {
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
}
else {
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1.0f, 1.0f, 1.0f, 0.5f));
}
ImGui::Text("Tool %zu: %s", i + 1, tool.toolName.c_str()); // Tool number and name
// Display the position (XYZ)
ImGui::Text(" Position: X: %.3f, Y: %.3f, Z: %.3f",
tool_transform[0], tool_transform[1], tool_transform[2]);
// Display the quaternion (XYZW)
ImGui::Text(" Quaternion: X: %.3f, Y: %.3f, Z: %.3f, W: %.3f",
tool_transform[3], tool_transform[4], tool_transform[5], tool_transform[6]);
ImGui::PopStyleColor();
ImGui::Separator(); // Separate the data for each tool
}
}
ImGui::End();
}
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData( ImGui::GetDrawData() );
glfwSwapBuffers( window );
}
StopRender();
Terminated = true;
}
void ViewerWindow::StopRender() {
glDeleteTextures(1, &texture);
glDeleteTextures(1, &dtexture);
ImGui_ImplGlfw_Shutdown();
ImGui_ImplOpenGL3_Shutdown();
ImGui::DestroyContext();
glfwDestroyWindow(window);
glfwTerminate();
}
void ViewerWindow::Shutdown() {
Terminated = true;
udpEnabled = false;
multiEnabled = false;
tracker.StopToolTracking();
JoinThread(processingThread);
JoinThread(udpThread);
JoinThread(udpReceiveThread);
JoinThread(csvThread);
if (tracker.IsTrackingTools() || tracker.IsCalibratingTool())
tracker.shutdown();
}