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 structures for the constellation tracker.
6 * @author Beyley Cardellio <ep1cm1n10n123@gmail.com>
7 * @ingroup aux_tracking
8 */
9
10#pragma once
11
12#include "xrt/xrt_frame.h"
13
14#include "util/u_debug.h"
15#include "util/u_logging.h"
16#include "util/u_var.h"
17#include "util/u_threading.h"
18#include "util/u_weak_ptr.hpp"
19
21
22#include "math/m_api.h"
23
24#include <vector>
25#include <memory>
26#include <mutex>
27#include <shared_mutex>
28#include <optional>
29#include <stdexcept>
30#include <array>
31
33#include "led_search_model.h"
34#include "pose_optimize.h"
35#include "pose_metrics.h"
37
38
39DEBUG_GET_ONCE_LOG_OPTION(constellation_tracker_log, "CONSTELLATION_TRACKER_LOG", U_LOGGING_WARN)
40
41#define CT_TRACE(ct, ...) U_LOG_IFL_T(ct->log_level, __VA_ARGS__)
42#define CT_DEBUG(ct, ...) U_LOG_IFL_D(ct->log_level, __VA_ARGS__)
43#define CT_INFO(ct, ...) U_LOG_IFL_I(ct->log_level, __VA_ARGS__)
44#define CT_WARN(ct, ...) U_LOG_IFL_W(ct->log_level, __VA_ARGS__)
45#define CT_ERROR(ct, ...) U_LOG_IFL_E(ct->log_level, __VA_ARGS__)
46
47#define MIN_ROT_ERROR DEG_TO_RAD(30)
48#define MIN_POS_ERROR 0.10
49
50/*
51 *
52 * Forward declares for C callback functions
53 *
54 */
55
56extern "C" void
57constellation_tracker_camera_push_blobs(struct t_blob_sink *tbs, struct t_blob_observation *tbo);
58
59extern "C" void
60constellation_tracker_camera_destroy(struct t_blob_sink *tbs);
61
62extern "C" void *
63constellation_tracker_camera_slow_thread(void *ptr);
64
65extern "C" void *
66constellation_tracker_camera_fast_thread(void *ptr);
67
68extern "C" void
69constellation_tracker_node_break_apart(struct xrt_frame_node *node);
70
71extern "C" void
72constellation_tracker_node_destroy(struct xrt_frame_node *node);
73
74
75namespace xrt::tracking::constellation {
76
77namespace os = xrt::auxiliary::os;
78
79// Forward-declares
80struct CameraMosaic;
81struct Device;
82
84{
85 bool has_sample;
86
87 struct t_blob blob_storage[XRT_CONSTELLATION_MAX_BLOBS_PER_FRAME];
88
90
91 std::array<t_constellation_device_id_t, XRT_CONSTELLATION_MAX_DEVICES> needs_slow_processing;
92 uint32_t num_devices_needing_slow_processing;
93
94 void
95 Take(std::optional<CameraSample> &maybe_sample);
96
97 void
99 std::array<t_constellation_device_id_t, XRT_CONSTELLATION_MAX_DEVICES> &devices_needing_slow_processing,
100 uint32_t num_devices_needing_slow_processing);
101
102 void
104};
105
106struct Camera
107{
108 struct t_blob_sink base = {
109 .push_blobs = constellation_tracker_camera_push_blobs,
110 .destroy = constellation_tracker_camera_destroy,
111 };
112
113 //! The owner tracker, so we can retrieve it from the blob sink callback
115 std::weak_ptr<CameraMosaic> mosaic;
116
117 struct t_camera_calibration calibration;
118
119 struct camera_model model;
120
121 //! Does "slow" processing for this camera when fast recovery paths fail.
123 struct
124 {
125 struct CameraSample sample;
126
127 struct correspondence_search *cs;
128 } slow_processing_thread_data;
129
130 /*!
131 * Does "fast" processing for this camera, trying to recover a pose quickly. It's valid for this to happen at
132 * the same time as a slow process.
133 */
135 struct
136 {
137 struct CameraSample sample;
138
139 struct correspondence_search *cs;
140 } fast_processing_thread_data;
141
142 //! Locks all processing data
143 mutable os::Mutex processing_lock;
144 //! All data protected by the processing lock
145 struct
146 {
147 struct xrt_pose Txr_origin_cam;
148 bool has_concrete_pose;
150
151 static Camera *
152 Get(struct t_blob_sink *tbs)
153 {
154 return container_of(tbs, Camera, base);
155 }
156
157 Camera(ConstellationTracker *tracker,
158 std::weak_ptr<CameraMosaic> mosaic,
159 const struct t_constellation_tracker_camera &camera_params,
160 enum u_logging_level *log_level_ptr);
161
162 ~Camera();
163
164 // Delete all copy/move ctors, since the pointers for `base` need to be stable
165 Camera(const Camera &) = delete;
166 Camera(Camera &&) = delete;
167 Camera &
168 operator=(const Camera &) = delete;
169 Camera &
170 operator=(Camera &&) = delete;
171
172 std::optional<struct xrt_pose>
173 GetWorldPose(timepoint_ns when_ns);
174
175 void
176 DeferSampleToSlowThread(CameraSample &sample);
177
178 //! Fast matching based on prior pose
179 bool
180 TryDevicePose(std::unique_ptr<Device> &device,
181 CameraSample &sample,
182 struct xrt_pose &Tcv_cam_world,
183 struct xrt_pose &Tcv_world_device_prior,
184 struct xrt_pose &Tcv_world_device_candidate);
185
186 bool
187 TryDeviceBlobRecovery(std::unique_ptr<Device> &device,
188 CameraSample &sample,
189 struct xrt_pose &Tcv_cam_world,
190 struct xrt_pose &Tcv_world_device_prior);
191
192 void
193 SlowSampleProcess(CameraSample &sample);
194
195 //! Returns whether a slow search is needed
196 bool
197 FastSampleProcess(CameraSample &sample);
198
199 void
200 PushPose(std::unique_ptr<Device> &device,
201 struct pose_metrics &score,
202 struct xrt_pose &Tcv_cam_device,
204 bool optimize);
205};
206
208{
209 std::vector<std::unique_ptr<Camera>> cameras;
210
211 struct t_constellation_tracker_tracking_source *tracking_origin;
212
213 CameraMosaic(ConstellationTracker *tracker, const struct t_constellation_tracker_camera_mosaic &mosaic_params);
214
215 ~CameraMosaic() = default;
216
217 std::optional<struct xrt_pose>
218 GetTrackingOriginPose(timepoint_ns when_ns);
219};
220
221struct Device
222{
224 struct t_constellation_tracker_device *device;
225
226 t_constellation_device_id_t id;
227
228 // @todo remove when clang-format is updated in CI
229 // clang-format off
230 struct t_constellation_search_model *search_model{nullptr};
231 struct xrt_vec3 prior_pos_error{MIN_POS_ERROR, MIN_POS_ERROR, MIN_POS_ERROR};
232 struct xrt_vec3 prior_rot_error{MIN_POS_ERROR, MIN_POS_ERROR, MIN_POS_ERROR};
233 float gravity_error_rad{MIN_ROT_ERROR}; /* Gravity vector uncertainty in radians 0..M_PI */
234
235 mutable os::Mutex data_lock;
236 struct
237 {
238 bool has_last_known;
239 struct xrt_pose Txr_world_device_last_known;
240 } locked_data;
241 // clang-format on
242
244 struct t_constellation_tracker_device *device,
245 t_constellation_device_id_t id);
246
247 ~Device();
248};
249
251{
252 struct xrt_frame_node node = {
253 .next = nullptr,
254 .break_apart = constellation_tracker_node_break_apart,
255 .destroy = constellation_tracker_node_destroy,
256 };
257
258 //! Whether the constellation tracker is running
259 bool running = true;
260
261 enum u_logging_level log_level = U_LOGGING_WARN;
262
263 struct t_constellation_tracker_params params;
264
265 std::vector<std::shared_ptr<CameraMosaic>> mosaics;
266
267 std::shared_mutex device_lock;
268 std::vector<std::unique_ptr<Device>> devices;
269 t_constellation_device_id_t next_device_id{0};
270
271 static ConstellationTracker *
272 Get(struct xrt_frame_node *node)
273 {
274 return container_of(node, ConstellationTracker, node);
275 }
276
277 ConstellationTracker(struct t_constellation_tracker_params *params);
278
279 ~ConstellationTracker();
280
281 // Delete all copy/move ctors, since the pointers for `node` need to be stable
282 ConstellationTracker(const ConstellationTracker &) = delete;
283 ConstellationTracker(ConstellationTracker &&) = delete;
284 ConstellationTracker &
285 operator=(const ConstellationTracker &) = delete;
286 ConstellationTracker &
287 operator=(ConstellationTracker &&) = delete;
288
289 t_constellation_device_id_t
290 AddDevice(struct t_constellation_tracker_device_params *params, struct t_constellation_tracker_device *device);
291
292 void
293 RemoveDevice(t_constellation_device_id_t device_id);
294
295 void
296 MarkMatchingBlobs(struct t_blob_observation &bwobs,
297 struct t_constellation_tracker_led_model &led_model,
298 t_constellation_device_id_t device_id,
299 struct pose_metrics_blob_match_info &blob_match_info);
300};
301
302}; // 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:226
LED search model management code.
C interface to math library.
Metrics for constellation tracking poses.
RANSAC PnP pose refinement.
Definition t_rift_blobwatch.c:82
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:85
Definition pose_metrics.h:61
Definition t_constellation.h:74
A generic interface to allow a tracking system to receive "snapshots" of seen t_blob in a frame.
Definition t_constellation.h:95
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:103
A blob is a 2d position in a camera sensor's view that is being tracked.
Definition t_constellation.h:37
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:58
A constellation tracker camera is a single camera that the constellation tracker will use to track de...
Definition t_constellation_tracker.h:32
Parameters for adding a device to the constellation tracker.
Definition t_constellation_tracker.h:77
A constellation tracker device is a device that the constellation tracker will attempt to track in 6d...
Definition t_constellation.h:315
The LED model is a series of points which define the real-world positions of all LEDs.
Definition t_constellation.h:260
Definition t_constellation_tracker.h:92
A constellation tracker tracking source is an arbitrary source of tracking data for the constellation...
Definition t_constellation.h:215
Definition t_constellation_tracker_internal.hpp:107
bool TryDevicePose(std::unique_ptr< Device > &device, CameraSample &sample, struct xrt_pose &Tcv_cam_world, struct xrt_pose &Tcv_world_device_prior, struct xrt_pose &Tcv_world_device_candidate)
Fast matching based on prior pose.
Definition t_constellation_tracker.cpp:185
os::Mutex processing_lock
Locks all processing data.
Definition t_constellation_tracker_internal.hpp:143
struct ConstellationTracker * tracker
The owner tracker, so we can retrieve it from the blob sink callback.
Definition t_constellation_tracker_internal.hpp:114
bool FastSampleProcess(CameraSample &sample)
Returns whether a slow search is needed.
Definition t_constellation_tracker.cpp:338
struct os_thread_helper slow_processing_thread
Does "slow" processing for this camera when fast recovery paths fail.
Definition t_constellation_tracker_internal.hpp:122
struct xrt::tracking::constellation::Camera::@322 locked_data
All data protected by the processing lock.
struct os_thread_helper fast_processing_thread
Does "fast" processing for this camera, trying to recover a pose quickly.
Definition t_constellation_tracker_internal.hpp:134
Definition t_constellation_tracker_internal.hpp:208
Definition t_constellation_tracker_internal.hpp:84
Definition t_constellation_tracker_internal.hpp:251
bool running
Whether the constellation tracker is running.
Definition t_constellation_tracker_internal.hpp:259
Definition t_constellation_tracker_internal.hpp:231
Definition t_constellation_tracker_internal.hpp:232
Definition t_constellation_tracker_internal.hpp:222
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 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.
Basic logging functionality.
Slightly higher level thread safe helpers.
Variable tracking code.
C++ helpers for weak pointers.
Data frame header.