Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions include/IRToolTrack.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ class IRToolTracker
std::atomic<bool> m_bShouldStop{false};

std::vector<IRTrackedTool> m_Tools;
// Guards m_Tools, m_ToolIndexMapping and the per-tool cur_transform/timestamp
// against concurrent access (GUI thread add/remove vs. UDP/CSV/GUI readers via
// GetToolTransform vs. the tracking thread writing results in UnionSegmentation).
std::mutex m_ToolsMutex;

std::unique_ptr<AHATFrame> m_CurrentFrame;
std::mutex m_MutexCurFrame;
Expand Down Expand Up @@ -108,6 +112,9 @@ class IRToolTracker

float m_fCalibrationSphereRadius = 6.5f;
const int MAX_CALIBRATION_FRAMES = 100;
// Hard upper bound on how many spheres a calibration may resolve. A tool with
// more than this is rejected as unsuccessful rather than fed downstream.
static constexpr int MAX_CALIBRATION_SPHERES = 6;
int NUM_CALIBRATION_SPHERES = 4;

std::vector<float> markerPositions;
Expand Down
3 changes: 3 additions & 0 deletions include/IRToolTracking.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ class IRToolTracking {
bool playFromFile = false;

bool intrinsics_found = false;
// True only between a successful pipeline.start() and the matching shutdown().
// Guards against rs2::pipeline::stop() throwing when start() never succeeded.
bool pipeline_started = false;

std::vector<std::string> deviceNames;

Expand Down
31 changes: 29 additions & 2 deletions include/ViewerWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class ViewerWindow {
public:
void Initialize(const std::string& file);
void Shutdown();
// Make destruction safe regardless of how Render()/main() unwound: stop every
// worker and join it, so no joinable std::thread is ever destroyed (which would
// call std::terminate()).
~ViewerWindow() { Shutdown(); }

bool IsTerminated() const
{
Expand All @@ -43,10 +47,24 @@ class ViewerWindow {

bool Connect(NanoSocket& _socket, NanoAddress& address, const char* host, int port, bool& _connected);
void Disconnect(NanoSocket& _socket, bool& _connected);
// Reference-counted nanosockets init/deinit so the library is initialized exactly
// once while one or more channels (send/receive) are active.
bool EnsureSocketInit();
void ReleaseSocketInit();

// Thread-safe snapshot of the current tool names, so worker threads never index the
// GUI-owned `tools` vector while it is being resized/edited on the GUI thread.
std::vector<std::string> SnapshotToolNames();

void GetSerialNumber();
Eigen::Matrix4f TrackingDataToMatrix(const TrackingData &data);

// Validation bounds.
static constexpr int MAX_TOOLS = 32; // upper bound for "Number of Tools"
static constexpr int MIN_SPHERES = 4; // lower bound for spheres per tool
static constexpr int MAX_SPHERES = 20; // safety cap for manual entry / ROM
static constexpr int MAX_CALIB_SPHERES = 6; // a calibration must not exceed this
Comment on lines +62 to +66


std::atomic<bool> Terminated = ATOMIC_VAR_INIT(false);
std::shared_ptr<std::thread> Thread;
Expand All @@ -66,9 +84,15 @@ class ViewerWindow {
GLFWwindow* window = nullptr;
GLuint texture = 0, dtexture = 0;
std::vector<Tool> tools;
// Guards `tools` (resized/edited by the GUI thread, read by UDP/CSV worker threads).
std::mutex toolsMutex;
std::map<int, Eigen::Matrix4f> toolTransforms;
std::mutex secondaryDataMutex;

// Set when a calibration produced an invalid result (too many spheres / NaN /
// no data); triggers the "Tool calibration unsuccessful" popup on the next frame.
bool showCalibrationError = false;

int numTools = 0; // Default number of tools
std::string toolName = "Tool1"; // Default tool name
int toolId = 0; // Default tool id
Expand Down Expand Up @@ -96,8 +120,11 @@ class ViewerWindow {
NanoSocket receiveSocket;
NanoAddress sendAddress = {};
NanoAddress receiveAddress = {};
bool m_connected;
bool m_receiveconnected;
bool m_connected = false;
bool m_receiveconnected = false;
// Reference count + guard for nanosockets_initialize()/deinitialize().
std::mutex socketInitMutex;
int socketInitCount = 0;
char ipAddress[16] = "127.0.0.1";
int m_port = 12345;
int m_receiveport = 12345;
Expand Down
Loading
Loading