Monado OpenXR Runtime
Loading...
Searching...
No Matches
t_constellation_tracker_internal.hpp
Go to the documentation of this file.
1// Copyright 2026, Beyley Cardellio
2// SPDX-License-Identifier: BSL-1.0
3/*!
4 * @file
5 * @brief Internal ures for the constellation tracker.
6 * @author Beyley Cardellio <ep1cm1n10n123@gmail.com>
7 * @ingroup tracking
8 */
9
10#pragma once
11
12#include "xrt/xrt_config_build.h"
13#include "xrt/xrt_frame.h"
14#include "xrt/xrt_tracking.h"
15
16#include "util/u_debug.h"
17#include "util/u_logging.h"
18#include "util/u_var.h"
19#include "util/u_threading.h"
20#include "util/u_weak_ptr.hpp"
21#include "util/u_sink.h"
22#include "util/u_frame.h"
24
26
27#include "math/m_api.h"
28
29#include <vector>
30#include <memory>
31#include <mutex>
32#include <shared_mutex>
33#include <optional>
34#include <stdexcept>
35#include <array>
36#include <fstream>
37
39#include "led_search_model.h"
40#include "pose_optimize.h"
41#include "pose_metrics.h"
43
44
45#define CT_TRACE(ct, ...) U_LOG_IFL_T(ct->log_level, __VA_ARGS__)
46#define CT_DEBUG(ct, ...) U_LOG_IFL_D(ct->log_level, __VA_ARGS__)
47#define CT_INFO(ct, ...) U_LOG_IFL_I(ct->log_level, __VA_ARGS__)
48#define CT_WARN(ct, ...) U_LOG_IFL_W(ct->log_level, __VA_ARGS__)
49#define CT_ERROR(ct, ...) U_LOG_IFL_E(ct->log_level, __VA_ARGS__)
50
51#define MIN_ROT_ERROR DEG_TO_RAD(30)
52#define MIN_POS_ERROR 0.10
53
54/*
55 *
56 * Forward declares for C callback functions
57 *
58 */
59
60extern "C" void
61constellation_tracker_camera_push_blobs(t_blob_sink *tbs, t_blob_observation *tbo);
62
63extern "C" void
64constellation_tracker_camera_destroy(t_blob_sink *tbs);
65
66extern "C" void *
67constellation_tracker_camera_slow_thread(void *ptr);
68
69extern "C" void *
70constellation_tracker_camera_fast_thread(void *ptr);
71
72extern "C" void
73constellation_tracker_node_break_apart(xrt_frame_node *node);
74
75extern "C" void
76constellation_tracker_node_destroy(xrt_frame_node *node);
77
78
79namespace xrt::tracking::constellation {
80
81namespace os = xrt::auxiliary::os;
82
83// Forward-declares
84struct Camera;
85struct CameraMosaic;
86struct ConstellationTracker;
87struct DataRecorder;
88struct Device;
89
91{
92 xrt_pose Tcv_cam_device XRT_POSE_IDENTITY;
93 float average_blob_brightness;
94};
95
97{
98 //! The ID of the device.
99 t_constellation_device_id_t device_id{XRT_CONSTELLATION_INVALID_DEVICE_ID};
100
101 //! The "predicted" pose, which is the pose the device expects itself to be at at the time of the blobservation.
102 std::optional<xrt_pose> Txr_world_device_prior{std::nullopt};
103
104 //! The final found pose of the device in this specific sample.
105 std::optional<FoundDevicePose> found_pose{std::nullopt};
106
107 //! Whether the device needs to have the slow processing thread run over it
109};
110
112{
113public: // Fields
114 t_blobwatch *source{nullptr};
115 uint64_t id{};
116 int64_t timestamp_ns{};
117 t_blob blobs[XRT_CONSTELLATION_MAX_BLOBS_PER_FRAME]{};
118 uint32_t blob_count{};
119
120 //! The camera's position in the constellation tracker's tracking origin
121 std::optional<xrt_pose> Txr_world_cam{std::nullopt};
122
123 std::array<DeviceState, XRT_CONSTELLATION_MAX_DEVICES> device_states{};
124 uint32_t device_count{};
125
126 uint32_t mosaic_index;
127 uint32_t camera_index;
128
129public: // Methods
130 std::optional<DeviceState *>
131 GetDeviceState(t_constellation_device_id_t device_id);
132
133 DeviceState &
134 PutDeviceState(t_constellation_device_id_t device_id);
135
136 CameraSample(t_blob_observation &blobservation, Camera *camera);
137
138 CameraSample() = default;
139
140 void
141 MarkMatchingBlobs(ConstellationTracker *ct,
143 t_constellation_device_id_t device_id,
144 pose_metrics_blob_match_info &blob_match_info);
145
147 ToBlobObservation() const &
148 {
149 t_blob_observation obs = {
150 .source = this->source,
151 .id = this->id,
152 .timestamp_ns = this->timestamp_ns,
153 .blobs = const_cast<t_blob *>(this->blobs),
154 .num_blobs = this->blob_count,
155 };
156
157 return obs;
158 }
159
161 ToBlobObservation() && = delete;
163 ToBlobObservation() const && = delete;
164};
165
167{
168public: // Fields
169 //! Whether to draw the blobs in the sample
170 bool draw_blobs{true};
171 //! Whether to draw the raw blob IDs
172 bool draw_blob_ids{false};
173 //! Whether to draw the device IDs on the blobs where possible
174 bool draw_blob_device_ids{true};
175 //! Whether to draw the LED IDs on the blobs where possible
176 bool draw_blob_led_ids{false};
177
178 //! Whether to draw the prior device pose in the sample, if it exists.
179 bool draw_prior{false};
180 //! Whether to draw the final device pose in the sample, if it exists.
181 bool draw_found{false};
182
183public: // Methods
184 void
185 SetupDebugTracking(void *root);
186};
187
188struct Camera
189{
190public: // Fields
191 t_blob_sink base = {
192 .push_blobs = constellation_tracker_camera_push_blobs,
193 .destroy = constellation_tracker_camera_destroy,
194 };
195
196 //! The owner tracker, so we can retrieve it from the blob sink callback
198 std::weak_ptr<CameraMosaic> mosaic;
199
200 t_camera_calibration calibration;
201
202 camera_model model;
203
204 CameraScribbleSettings scribble_settings{};
205
206 //! The index in the mosaic
207 size_t index;
208
209 //! Does "slow" processing for this camera when fast recovery paths fail.
210 os_thread_helper slow_processing_thread{};
211 struct
212 {
213 std::optional<CameraSample> sample{std::nullopt};
214
215 correspondence_search *cs{nullptr};
216
217 u_sink_debug debug_sink{};
218 } slow_processing_thread_data;
219
220 /*!
221 * Does "fast" processing for this camera, trying to recover a pose quickly. It's valid for this to happen at
222 * the same time as a slow process.
223 */
224 os_thread_helper fast_processing_thread{};
225 struct
226 {
227 std::optional<CameraSample> sample{std::nullopt};
228
229 correspondence_search *cs{nullptr};
230
231 u_sink_debug debug_sink{};
232 } fast_processing_thread_data;
233
234 //! Locks all processing data
235 mutable os::Mutex processing_lock;
236 //! All data protected by the processing lock
237 struct
238 {
239 xrt_pose Txr_origin_cam;
240 bool has_concrete_pose;
241 } locked_data;
242
243public: // Methods (t_constellation_tracker.cpp)
244 static Camera *
245 Get(t_blob_sink *tbs)
246 {
247 return container_of(tbs, Camera, base);
248 }
249
250 Camera(ConstellationTracker *tracker,
251 std::weak_ptr<CameraMosaic> mosaic,
252 const t_constellation_tracker_camera &camera_params,
253 enum u_logging_level *log_level_ptr,
254 size_t index);
255
256 ~Camera();
257
258 // Delete all copy/move ctors, since the pointers for `base` need to be stable
259 Camera(const Camera &) = delete;
260 Camera(Camera &&) = delete;
261 Camera &
262 operator=(const Camera &) = delete;
263 Camera &
264 operator=(Camera &&) = delete;
265
266 std::optional<xrt_pose>
267 GetWorldPose(timepoint_ns when_ns);
268
269 void
270 DeferSampleToSlowThread(CameraSample &sample);
271
272 //! Fast matching based on prior pose
273 bool
274 TryDevicePose(std::unique_ptr<Device> &device,
275 CameraSample &sample,
276 DeviceState &device_state,
277 xrt_pose &Tcv_cam_world,
278 std::optional<xrt_pose> &Tcv_world_device_prior,
279 xrt_pose &Tcv_world_device_candidate);
280
281 bool
282 TryDeviceBlobRecovery(std::unique_ptr<Device> &device,
283 CameraSample &sample,
284 DeviceState &device_state,
285 xrt_pose &Tcv_cam_world,
286 std::optional<xrt_pose> &Tcv_world_device_prior);
287
288 void
289 SlowSampleProcess(CameraSample &sample);
290
291 //! Returns whether a slow search is needed
292 bool
293 FastSampleProcess(CameraSample &sample);
294
295 void
296 PushPose(CameraSample &camera_sample,
297 DeviceState &device_state,
298 std::unique_ptr<Device> &device,
299 pose_metrics &score,
300 xrt_pose &Tcv_cam_device,
301 bool was_optimized);
302
303public: // Public (constellation_debug_scribble.cpp)
304 void
305 DebugScribbleSample(CameraSample &sample, bool fast);
306};
307
309{
310public: // Fields
311 std::vector<std::unique_ptr<Camera>> cameras;
312 //! The index of this mosaic in the tracker.
313 size_t index;
314
316
317public: // Methods
319 const t_constellation_tracker_camera_mosaic &mosaic_params,
320 size_t index);
321
322 ~CameraMosaic() = default;
323
324 std::optional<xrt_pose>
325 GetTrackingOriginPose(timepoint_ns when_ns);
326};
327
328struct Device
329{
330public: // Fields
333
334 t_constellation_device_id_t id;
335
336 // @todo remove when clang-format is updated in CI
337 // clang-format off
338 t_constellation_search_model *search_model{nullptr};
339
340 // @todo These need to be pulled from the device and put into the sample.
341 // Right now we just hardcode them since we don't have any real sensor fusion.
342 xrt_vec3 prior_pos_error{MIN_POS_ERROR, MIN_POS_ERROR, MIN_POS_ERROR};
343 xrt_vec3 prior_rot_error{MIN_ROT_ERROR, MIN_ROT_ERROR, MIN_ROT_ERROR};
344 float gravity_error_rad{MIN_ROT_ERROR}; /* Gravity vector uncertainty in radians 0..M_PI */
345
346 mutable os::Mutex data_lock;
347 struct
348 {
349 std::optional<xrt_pose> Txr_world_device_last_known;
350 } locked_data;
351 // clang-format on
352
353public: // Methods
356 t_constellation_device_id_t id);
357
358 ~Device();
359};
360
361// Separate base struct with our interface implementations so that `ConstellationTrackerBase` remains a standard layout
362// type and we can safely use `container_of` on it.
364{
365 xrt_frame_node node = {
366 .next = nullptr,
367 .break_apart = constellation_tracker_node_break_apart,
368 .destroy = constellation_tracker_node_destroy,
369 };
370 xrt_tracking_origin tracking_origin = {
371 .name = "Constellation Tracker",
373 .initial_offset = XRT_POSE_IDENTITY,
374 };
375};
376
378{
379public: // Fields
380 //! Whether the constellation tracker is running
381 bool running = true;
382
383 enum u_logging_level log_level = U_LOGGING_WARN;
384
386
387 bool single_threaded{false};
388
389 std::vector<std::shared_ptr<CameraMosaic>> mosaics;
390
391 std::shared_mutex device_lock;
392 std::vector<std::unique_ptr<Device>> devices;
393 t_constellation_device_id_t next_device_id{0};
394
395 std::unique_ptr<DataRecorder> data_recorder{};
396
397#ifdef XRT_FEATURE_RERUN
398 std::unique_ptr<struct RerunContext> rerun_stream{};
399#endif
400
401public: // Methods
402 static ConstellationTracker *
403 Get(xrt_frame_node *node)
404 {
405 return static_cast<ConstellationTracker *>(container_of(node, ConstellationTrackerBase, node));
406 }
407
408 static ConstellationTracker *
409 Get(t_constellation_tracker *tracker)
410 {
411 return reinterpret_cast<ConstellationTracker *>(tracker);
412 }
413
414 ConstellationTracker(t_constellation_tracker_params *params);
415
416 ~ConstellationTracker();
417
418 // Delete all copy/move ctors, since the pointers for `node` need to be stable
419 ConstellationTracker(const ConstellationTracker &) = delete;
420 ConstellationTracker(ConstellationTracker &&) = delete;
421 ConstellationTracker &
422 operator=(const ConstellationTracker &) = delete;
423 ConstellationTracker &
424 operator=(ConstellationTracker &&) = delete;
425
426 void
427 SetupVariableTracking();
428
429 t_constellation_device_id_t
431
432 void
433 RemoveDevice(t_constellation_device_id_t device_id);
434};
435
436}; // namespace xrt::tracking::constellation
Definition device.hpp:68
Ab-initio blob<->LED correspondence search.
u_logging_level
Logging level enum.
Definition u_logging.h:45
@ U_LOGGING_WARN
Warning messages: indicating a potential problem.
Definition u_logging.h:49
int64_t timepoint_ns
Integer timestamp type.
Definition u_time.h:77
#define container_of(ptr, type, field)
Get the holder from a pointer to a field.
Definition xrt_compiler.h:238
@ XRT_TRACKING_TYPE_CONSTELLATION
The device(s) are tracked through a constellation of lights as seen by cameras.
Definition xrt_tracking.h:63
LED search model management code.
C interface to math library.
Metrics for constellation tracking poses.
RANSAC PnP pose refinement.
Definition t_rift_blobwatch.c:88
Definition camera_model.h:19
Definition correspondence_search.h:92
All in one helper that handles locking, waiting for change and starting a thread.
Definition os_threading.h:499
Definition pose_metrics.h:89
Definition pose_metrics.h:63
Definition t_constellation.h:80
A generic interface to allow a tracking system to receive "snapshots" of seen t_blob in a frame.
Definition t_constellation.h:101
void(* push_blobs)(struct t_blob_sink *tbs, struct t_blob_observation *observation)
Push a set of blobs into the sink.
Definition t_constellation.h:109
A blob is a 2d position in a camera sensor's view that is being tracked.
Definition t_constellation.h:37
Definition t_constellation.h:152
Essential calibration data for a single camera, or single lens/sensor of a stereo camera.
Definition t_tracking.h:236
Definition led_search_model.h:40
A constellation tracker camera mosaic is a set of cameras that may or may not be physically attached,...
Definition t_constellation_tracker.h:60
A constellation tracker camera is a single camera that the constellation tracker will use to track de...
Definition t_constellation_tracker.h:34
Parameters for adding a device to the constellation tracker.
Definition t_constellation_tracker.h:79
A constellation tracker device is a device that the constellation tracker will attempt to track in 6d...
Definition t_constellation.h:339
The LED model is a series of points which define the real-world positions of all LEDs.
Definition t_constellation.h:266
Definition t_constellation_tracker.h:104
A constellation tracker tracking source is an arbitrary source of tracking data for the constellation...
Definition t_constellation.h:221
Allows more safely to debug sink inputs and outputs.
Definition u_sink.h:214
Definition t_constellation_tracker_internal.hpp:189
os::Mutex processing_lock
Locks all processing data.
Definition t_constellation_tracker_internal.hpp:235
ConstellationTracker * tracker
The owner tracker, so we can retrieve it from the blob sink callback.
Definition t_constellation_tracker_internal.hpp:197
size_t index
The index in the mosaic.
Definition t_constellation_tracker_internal.hpp:207
Definition t_constellation_tracker_internal.hpp:309
size_t index
The index of this mosaic in the tracker.
Definition t_constellation_tracker_internal.hpp:313
Definition t_constellation_tracker_internal.hpp:112
std::optional< xrt_pose > Txr_world_cam
The camera's position in the constellation tracker's tracking origin.
Definition t_constellation_tracker_internal.hpp:121
Definition t_constellation_tracker_internal.hpp:167
Definition t_constellation_tracker_internal.hpp:364
Definition t_constellation_tracker_internal.hpp:378
Definition t_constellation_tracker_internal.hpp:329
Definition t_constellation_tracker_internal.hpp:97
t_constellation_device_id_t device_id
The ID of the device.
Definition t_constellation_tracker_internal.hpp:99
std::optional< xrt_pose > Txr_world_device_prior
The "predicted" pose, which is the pose the device expects itself to be at at the time of the blobser...
Definition t_constellation_tracker_internal.hpp:102
std::optional< FoundDevicePose > found_pose
The final found pose of the device in this specific sample.
Definition t_constellation_tracker_internal.hpp:105
bool needs_slow_processing
Whether the device needs to have the slow processing thread run over it.
Definition t_constellation_tracker_internal.hpp:108
Definition t_constellation_tracker_internal.hpp:91
A interface object used for destroying a frame graph.
Definition xrt_frame.h:87
void(* break_apart)(struct xrt_frame_node *node)
Called first in when the graph is being destroyed, remove any references frames and other objects and...
Definition xrt_frame.h:94
A pose composed of a position and orientation.
Definition xrt_defines.h:492
A tracking system or device origin.
Definition xrt_tracking.h:78
char name[256]
For debugging.
Definition xrt_tracking.h:80
A 3 element vector with single floats.
Definition xrt_defines.h:289
Header defining the tracking system integration in Monado.
Header defining the constellation tracker parameters and functions.
Small debug helpers.
xrt_frame helpers.
xrt_frame helpers for scribbling onto frames.
Basic logging functionality.
xrt_frame_sink converters and other helpers.
Slightly higher level thread safe helpers.
Variable tracking code.
C++ helpers for weak pointers.
Data frame header.
Header defining the tracking system integration in Monado.