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
25#include "glue/g_blob_sink.hpp"
26#include "glue/g_frame_node.hpp"
27#include "glue/g_imu_sink.hpp"
28
30
31#include "math/m_api.h"
32
34#include "led_search_model.h"
35#include "pose_metrics.h"
40
41#include <array>
42#include <atomic>
43#include <ceres/context.h>
44#include <fstream>
45#include <memory>
46#include <mutex>
47#include <optional>
48#include <shared_mutex>
49#include <span>
50#include <stdexcept>
51#include <vector>
52
53
54#define CT_TRACE(ct, ...) U_LOG_IFL_T(ct->log_level, __VA_ARGS__)
55#define CT_DEBUG(ct, ...) U_LOG_IFL_D(ct->log_level, __VA_ARGS__)
56#define CT_INFO(ct, ...) U_LOG_IFL_I(ct->log_level, __VA_ARGS__)
57#define CT_WARN(ct, ...) U_LOG_IFL_W(ct->log_level, __VA_ARGS__)
58#define CT_ERROR(ct, ...) U_LOG_IFL_E(ct->log_level, __VA_ARGS__)
59
60#define MIN_ROT_ERROR DEG_TO_RAD(30)
61#define MIN_POS_ERROR 0.10
62
63/*
64 *
65 * Forward declares for C callback functions
66 *
67 */
68
69extern "C" void *
70constellation_tracker_camera_slow_thread(void *ptr);
71
72extern "C" void *
73constellation_tracker_camera_fast_thread(void *ptr);
74
75
76namespace xrt::tracking::constellation {
77
78using namespace xrt::tracking::constellation::optimizer;
79
80namespace os = xrt::auxiliary::os;
81
82// Forward-declares
83struct Camera;
84struct CameraMosaic;
86struct DataRecorder;
87struct Device;
88
90{
91 xrt_pose Tcv_cam_device XRT_POSE_IDENTITY;
92 float average_blob_brightness;
93
94 /*!
95 * Whether a found pose was already handled, this is present so that if a single sample is torn between some
96 * devices fast recovering and some devices slow recovering, we push the fast recovery poses the moment they're
97 * found rather than at the end of the slow recovery where the sample is "terminated".
98 */
99 bool handled{false};
100};
101
103{
104 //! The ID of the device.
105 t_constellation_device_id_t device_id{XRT_CONSTELLATION_INVALID_DEVICE_ID};
106
107 //! The predicted pose of the device in the world at the time of the sample.
108 std::optional<xrt_pose> Txr_world_device_prior{std::nullopt};
109 //! The predicted pose of the device relative to the camera at the time of the sample.
110 std::optional<xrt_space_relation> Tcv_cam_device_predicted{std::nullopt};
111
112 //! The final found pose of the device in this specific sample.
113 std::optional<FoundDevicePose> found_pose{std::nullopt};
114
115 //! Whether the device needs to have the slow processing thread run over it
117};
118
120{
121public: // Fields
122 t_blobwatch *source{nullptr};
123 uint64_t id{};
124 uint64_t sequence_id{};
125 int64_t timestamp_ns{};
126 t_blob blobs[XRT_CONSTELLATION_MAX_BLOBS_PER_FRAME]{};
127 uint32_t blob_count{};
128
129 //! The camera's position in the constellation tracker's tracking origin
130 std::optional<xrt_pose> Txr_world_cam{std::nullopt};
131
132 std::array<t_constellation_device_id_t, XRT_CONSTELLATION_MAX_DEVICES> device_ids_by_distance{};
133 std::array<DeviceState, XRT_CONSTELLATION_MAX_DEVICES> device_states{};
134 uint32_t device_count{};
135
136 uint32_t mosaic_index;
137 uint32_t camera_index;
138
139public: // Methods
140 std::optional<DeviceState *>
141 getDeviceState(t_constellation_device_id_t device_id);
142
144 putDeviceState(t_constellation_device_id_t device_id);
145
147
148 CameraSample() = default;
149
150 void
151 markMatchingBlobs(ConstellationTracker *ct,
153 t_constellation_device_id_t device_id,
154 pose_metrics_blob_match_info &blob_match_info);
155
157 toBlobObservation() const &
158 {
159 t_blob_observation obs = {
160 .source = this->source,
161 .id = this->id,
162 .sequence_id = this->sequence_id,
163 .timestamp_ns = this->timestamp_ns,
164 .blobs = const_cast<t_blob *>(this->blobs),
165 .num_blobs = this->blob_count,
166 .frame = nullptr,
167 };
168
169 return obs;
170 }
171
173 toBlobObservation() && = delete;
175 toBlobObservation() const && = delete;
176};
177
179{
180public: // Fields
181 //! Whether to draw the blobs in the sample
182 bool draw_blobs{true};
183 //! Whether to draw the raw blob IDs
184 bool draw_blob_ids{false};
185 //! Whether to draw the device IDs on the blobs where possible
186 bool draw_blob_device_ids{true};
187 //! Whether to draw the LED IDs on the blobs where possible
188 bool draw_blob_led_ids{false};
189
190 //! Whether to draw the prior device pose in the sample, if it exists.
191 bool draw_prior{false};
192 //! Whether to draw the final device pose in the sample, if it exists.
193 bool draw_found{false};
194
195public: // Methods
196 void
197 setupDebugTracking(void *root);
198};
199
201{
202public: // Fields
203 //! The owner tracker, so we can retrieve it from the blob sink callback
205 std::weak_ptr<CameraMosaic> mosaic;
206
207 t_camera_calibration calibration;
208
209 camera_model model;
210
211 CameraScribbleSettings scribble_settings{};
212
213 //! The index in the mosaic
214 uint32_t index;
215
216 //! Does "slow" processing for this camera when fast recovery paths fail.
217 os_thread_helper slow_processing_thread{};
218 struct
219 {
220 std::optional<CameraSample> sample{std::nullopt};
221
222 correspondence_search *cs{nullptr};
223
224 u_sink_debug debug_sink{};
225 } slow_processing_thread_data;
226
227 /*!
228 * Does "fast" processing for this camera, trying to recover a pose quickly. It's valid for this to happen at
229 * the same time as a slow process.
230 */
231 os_thread_helper fast_processing_thread{};
232 struct
233 {
234 std::optional<CameraSample> sample{std::nullopt};
235
236 correspondence_search *cs{nullptr};
237
238 u_sink_debug debug_sink{};
239 } fast_processing_thread_data;
240
241 //! Locks all processing data
242 mutable os::Mutex processing_lock;
243 //! All data protected by the processing lock
244 struct
245 {
246 xrt_pose Txr_origin_cam;
247 bool has_concrete_pose;
248 } locked_data;
249
250 std::atomic<uint32_t> shallow_searches_failed{0};
251
252public: // Methods (t_blob_sink, t_constellation_tracker.cpp)
253 void
254 pushBlobs(t_blob_observation &observation);
255
256 static void
257 destroyBlobSink(t_blob_sink *sink);
258
259public: // Methods (t_constellation_tracker.cpp)
261 std::weak_ptr<CameraMosaic> mosaic,
262 const t_constellation_tracker_camera &camera_params,
263 enum u_logging_level *log_level_ptr,
264 uint32_t index);
265
266 ~Camera();
267
268 // Delete all copy/move ctors, since the pointers we pass out need to be stable
269 Camera(const Camera &) = delete;
270 Camera(Camera &&) = delete;
271 Camera &
272 operator=(const Camera &) = delete;
273 Camera &
274 operator=(Camera &&) = delete;
275
276 std::optional<xrt_pose>
277 getWorldPose(timepoint_ns when_ns);
278
279 void
280 deferSampleToSlowThread(CameraSample &sample);
281
282 //! Fast matching based on prior pose
283 bool
284 tryDevicePose(Device *device,
285 CameraSample &sample,
286 DeviceState &device_state,
287 bool need_matching_ids,
288 const std::optional<xrt_space_relation> &Tcv_cam_device_prior,
289 const xrt_pose &Tcv_cam_device_candidate);
290
291 bool
292 tryDeviceBlobRecovery(Device *device,
293 CameraSample &sample,
294 DeviceState &device_state,
295 const std::optional<xrt_space_relation> &Tcv_cam_device_prior);
296
297 //! Handles a single step (fast,slow) being complete
298 void
299 handleStepComplete(CameraSample &sample);
300
301 //! Handles complete processing of a sample, hold the devices lock while you call this.
302 void
303 handleProcessingComplete(CameraSample &sample, const std::span<Device *> devices);
304
305 void
306 processSampleSlow(CameraSample &sample);
307
308 //! Returns whether a slow search is needed
309 bool
310 processSampleFast(CameraSample &sample);
311
312 void
313 pushPose(CameraSample &camera_sample,
314 DeviceState &device_state,
315 Device *device,
316 pose_metrics &score,
317 const xrt_pose &Tcv_cam_device,
318 bool was_optimized);
319
320public: // Methods (constellation_debug_scribble.cpp)
321 void
322 debugScribbleSample(CameraSample &sample, bool fast);
323};
324
326{
327public: // Fields
328 std::vector<std::unique_ptr<Camera>> cameras;
329 //! The index of this mosaic in the tracker.
330 uint32_t index;
331
333
334public: // Methods
336 const t_constellation_tracker_camera_mosaic &mosaic_params,
337 uint32_t index);
338
339 ~CameraMosaic() = default;
340
341 std::optional<xrt_pose>
342 getTrackingOriginPose(timepoint_ns when_ns);
343};
344
346{
347public: // Fields
348 xrt_pose Txr_world_device;
349 xrt_pose Tcv_cam_device;
350 timepoint_ns timestamp_ns;
351
352public: // Methods
353 DeviceLastPose(xrt_pose Txr_world_device, xrt_pose Tcv_cam_device, timepoint_ns timestamp_ns);
354};
355
356struct Device : public util::ImuSinkBase<Device>
357{
358public: // Fields
361
362 t_constellation_device_id_t id;
363
364 //! Devices are disabled by default, and the constellation tracker must choose to enable them.
365 std::atomic<bool> enabled{false};
366
367 //! The owner tracker, so we can retrieve it from the IMU sink callback
369
370 std::unique_ptr<sensor_fusion::SensorFusion> sensor_fusion{};
371
372 // @todo remove when clang-format is updated in CI
373 // clang-format off
374 t_constellation_search_model *search_model{nullptr};
375
376 // @todo These need to be pulled from the device and put into the sample.
377 // Right now we just hardcode them since we don't have any real sensor fusion.
378 xrt_vec3 prior_pos_error{MIN_POS_ERROR, MIN_POS_ERROR, MIN_POS_ERROR};
379 xrt_vec3 prior_rot_error{MIN_ROT_ERROR, MIN_ROT_ERROR, MIN_ROT_ERROR};
380 float gravity_error_rad{MIN_ROT_ERROR}; /* Gravity vector uncertainty in radians 0..M_PI */
381
382 mutable os::Mutex data_lock;
383 struct
384 {
385 std::optional<DeviceLastPose> last_known_pose;
386
387 struct
388 {
389 //! The latest sequence ID this device was seen at.
390 std::optional<int64_t> last_seen_sequence_id{std::nullopt};
391 } per_camera[XRT_CONSTELLATION_MAX_TRACKING_MOSAICS * XRT_TRACKING_MAX_FRAME_SOURCES];
392 } locked_data;
393 // clang-format on
394
395public: // Methods
399 t_constellation_device_id_t id);
400
401 ~Device();
402
403 // Delete move constructors
404 Device(const Device &) = delete;
405 Device(Device &&) = delete;
406 Device &
407 operator=(const Device &) = delete;
408 Device &
409 operator=(Device &&) = delete;
410
411 //! @note The interface expects non-const but we don't need to mutate it, it's source-compatible
412 void
413 pushImu(const xrt_imu_sample &sample);
414};
415
416struct ConstellationTracker : public util::FrameNodeBase<ConstellationTracker>
417{
418public: // Fields
419 xrt_tracking_origin tracking_origin = {};
420
421 //! Whether the constellation tracker is running
422 bool running = true;
423
424 enum u_logging_level log_level;
425
427
428 bool deterministic{false};
429
430 std::vector<std::shared_ptr<CameraMosaic>> mosaics;
431
432 // Put here to be destroyed before devices (reverse declaration order)
433 std::unique_ptr<ceres::Context> ceres_context{};
434
435 std::shared_mutex device_lock;
436 std::vector<std::unique_ptr<Device>> devices;
437 t_constellation_device_id_t next_device_id{0};
438
439 std::unique_ptr<DataRecorder> data_recorder{};
440
441 mutable os::Mutex rig_calibration_lock{};
442 os_thread_helper rig_calibration_thread{};
443 bool running_calibration{false};
444 std::unique_ptr<rig_calibration::RigCalibration> rig_calibration{};
445
446#ifdef XRT_FEATURE_RERUN
447 std::unique_ptr<struct RerunContext> rerun_stream{};
448#endif
449
450public: // Methods (xrt_frame_node)
451 void
452 breakApart();
453
454 static void
455 destroyFrameNode(xrt_frame_node *node);
456
457public: // Methods
458 static ConstellationTracker *
459 Get(t_constellation_tracker *tracker)
460 {
461 return reinterpret_cast<ConstellationTracker *>(tracker);
462 }
463
465
467
468 // Delete all copy/move ctors, since the pointers we return need to be stable
472 operator=(const ConstellationTracker &) = delete;
474 operator=(ConstellationTracker &&) = delete;
475
476 bool
477 needSensorCalibration();
478
479 bool
480 checkSensorCalibration();
481
482 void
483 setupVariableTracking();
484
485 t_constellation_device_id_t
487
488 void
489 removeDevice(t_constellation_device_id_t device_id);
490
491 void
492 getTrackedPose(t_constellation_device_id_t device_id, timepoint_ns when_ns, xrt_space_relation &out_relation);
493};
494
495}; // namespace xrt::tracking::constellation
Definition device.hpp:74
CRTP glue wrapper for t_blob_sink.
Definition g_blob_sink.hpp:38
CRTP glue wrapper for xrt_frame_node.
Definition g_frame_node.hpp:33
CRTP glue wrapper for xrt_imu_sink.
Definition g_imu_sink.hpp:34
Ab-initio blob<->LED correspondence search.
Header for glue classes to wrap constellation blob sink interfaces.
Header for glue classes to wrap xrt frame node interfaces.
Header for glue classes to wrap xrt IMU sink interfaces.
u_logging_level
Logging level enum.
Definition u_logging.h:45
int64_t timepoint_ns
Integer timestamp type.
Definition u_time.h:77
LED search model management code.
C interface to math library.
Metrics for constellation tracking poses.
RANSAC PnP pose refinement.
Constellation tracker rig calibration.
Constellation tracker sensor fusion.
Definition t_rift_blobwatch.c:85
Definition camera_model.h:21
Definition correspondence_search.h:95
Definition u_pacing_compositor.c:59
All in one helper that handles locking, waiting for change and starting a thread.
Definition os_threading.h:587
Definition pose_metrics.h:105
Definition pose_metrics.h:79
Definition t_constellation.h:104
A generic interface to allow a tracking system to receive "snapshots" of seen t_blob in a frame.
Definition t_constellation.h:136
A blob is a 2d position in a camera sensor's view that is being tracked.
Definition t_constellation.h:51
Definition t_constellation.h:187
Essential calibration data for a single camera, or single lens/sensor of a stereo camera.
Definition t_tracking.h:269
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:415
The LED model is a series of points which define the real-world positions of all LEDs.
Definition t_constellation.h:313
Definition t_constellation_tracker.h:108
A constellation tracker tracking source is an arbitrary source of tracking data for the constellation...
Definition t_constellation.h:256
Allows more safely to debug sink inputs and outputs.
Definition u_sink.h:214
Definition t_constellation_tracker_internal.hpp:201
os::Mutex processing_lock
Locks all processing data.
Definition t_constellation_tracker_internal.hpp:242
uint32_t index
The index in the mosaic.
Definition t_constellation_tracker_internal.hpp:214
ConstellationTracker * tracker
The owner tracker, so we can retrieve it from the blob sink callback.
Definition t_constellation_tracker_internal.hpp:204
Definition t_constellation_tracker_internal.hpp:326
uint32_t index
The index of this mosaic in the tracker.
Definition t_constellation_tracker_internal.hpp:330
Definition t_constellation_tracker_internal.hpp:120
std::optional< xrt_pose > Txr_world_cam
The camera's position in the constellation tracker's tracking origin.
Definition t_constellation_tracker_internal.hpp:130
Definition t_constellation_tracker_internal.hpp:179
Definition t_constellation_tracker_internal.hpp:417
Definition t_constellation_tracker_dataset.hpp:124
Definition t_constellation_tracker_internal.hpp:357
ConstellationTracker * tracker
The owner tracker, so we can retrieve it from the IMU sink callback.
Definition t_constellation_tracker_internal.hpp:368
Definition t_constellation_tracker_internal.hpp:346
Definition t_constellation_tracker_internal.hpp:103
std::optional< xrt_space_relation > Tcv_cam_device_predicted
The predicted pose of the device relative to the camera at the time of the sample.
Definition t_constellation_tracker_internal.hpp:110
t_constellation_device_id_t device_id
The ID of the device.
Definition t_constellation_tracker_internal.hpp:105
std::optional< xrt_pose > Txr_world_device_prior
The predicted pose of the device in the world at the time of the sample.
Definition t_constellation_tracker_internal.hpp:108
std::optional< FoundDevicePose > found_pose
The final found pose of the device in this specific sample.
Definition t_constellation_tracker_internal.hpp:113
bool needs_slow_processing
Whether the device needs to have the slow processing thread run over it.
Definition t_constellation_tracker_internal.hpp:116
Definition t_constellation_tracker_internal.hpp:90
bool handled
Whether a found pose was already handled, this is present so that if a single sample is torn between ...
Definition t_constellation_tracker_internal.hpp:99
A interface object used for destroying a frame graph.
Definition xrt_frame.h:87
IMU Sample.
Definition xrt_tracking.h:179
A pose composed of a position and orientation.
Definition xrt_defines.h:535
A relation with two spaces, includes velocity and acceleration.
Definition xrt_defines.h:726
A tracking system or device origin.
Definition xrt_tracking.h:115
A 3 element vector with single floats.
Definition xrt_defines.h:323
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.