Monado OpenXR Runtime
t_tracking.h
Go to the documentation of this file.
1 // Copyright 2019-2020, Collabora, Ltd.
2 // SPDX-License-Identifier: BSL-1.0
3 /*!
4  * @file
5  * @brief Tracking API interface.
6  * @author Pete Black <pblack@collabora.com>
7  * @author Jakob Bornecrantz <jakob@collabora.com>
8  * @author Ryan Pavlik <ryan.pavlik@collabora.com>
9  * @ingroup aux_tracking
10  */
11 
12 #pragma once
13 
14 #include "xrt/xrt_frame.h"
15 #include "util/u_misc.h"
16 
17 #include <stdio.h>
18 
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 
25 /*!
26  * @defgroup aux_tracking Tracking
27  * @ingroup aux
28  * @brief Trackers, filters and associated helper code.
29  *
30  *
31  * ### Coordinate system
32  *
33  * Right now there is no specific convention on where a tracking systems
34  * coordinate system is centered, and is something we probably need to figure
35  * out. Right now the stereo based tracking system used by the PSVR and PSMV
36  * tracking system is centered on the camera that OpenCV decided is origin.
37  *
38  * To go a bit further on the PSVR/PSMV case. Think about a idealized start up
39  * case, the user is wearing the HMD headset and holding two PSMV controllers.
40  * The HMD's coordinate system axis are perfectly parallel with the user
41  * coordinate with the user's coordinate system. Where -Z is forward. The user
42  * holds the controllers with the ball pointing up and the buttons on the back
43  * pointing forward. Which if you read the documentation of @ref psmv_device
44  * will that the axis of the PSMV are also perfectly aligned with the users
45  * coordinate system. So everything "attached" to the user have it's coordinate
46  * system parallel to the user's.
47  *
48  * The camera on the other hand is looking directly at the user, it's Z-axis and
49  * X-axis is flipped in relation to the user's. So to compare what is sees to
50  * what the user sees, everything is rotated 180° around the Y-axis.
51  */
52 
53 /*!
54  * @dir auxiliary/tracking
55  * @ingroup aux
56  *
57  * @brief Trackers, filters and associated helper code.
58  */
59 
60 /*!
61  * @ingroup aux_tracking
62  * @{
63  */
64 
65 
66 /*
67  *
68  * Pre-declare
69  *
70  */
71 
72 struct xrt_tracked_psmv;
73 struct xrt_tracked_psvr;
74 
75 
76 /*
77  *
78  * Calibration data.
79  *
80  */
81 
82 //! Maximum size of rectilinear distortion coefficient array
83 #define XRT_DISTORTION_MAX_DIM (5)
84 
85 /*!
86  * @brief Essential calibration data for a single camera, or single lens/sensor
87  * of a stereo camera.
88  */
90 {
91  //! Source image size
93 
94  //! Camera intrinsics matrix
95  double intrinsics[3][3];
96 
97  //! Rectilinear distortion coefficients: k1, k2, p1, p2[, k3[, k4, k5,
98  //! k6[, s1, s2, s3, s4]]
100 
101  //! Fisheye camera distortion coefficients
103 
104  //! Is the camera fisheye?
106 };
107 
108 /*!
109  * Stereo camera calibration data to be given to trackers.
110  */
112 {
113  //! Ref counting
114  struct xrt_reference reference;
115 
116  //! Calibration of individual views/sensor
117  struct t_camera_calibration view[2];
118 
119  //! Translation from first to second in the stereo pair.
120  double camera_translation[3];
121  //! Rotation matrix from first to second in the stereo pair.
122  double camera_rotation[3][3];
123 
124  //! Essential matrix.
125  double camera_essential[3][3];
126  //! Fundamental matrix.
127  double camera_fundamental[3][3];
128 };
129 
130 /*!
131  * Allocates a new stereo calibration data, unreferences the old @p calib.
132  */
133 void
135 
136 /*!
137  * Only to be called by @p t_stereo_camera_calibration_reference.
138  */
139 void
141 
142 /*!
143  * Update the reference counts on a stereo calibration data(s).
144  *
145  * @param dst Pointer to a object reference, if the object reference is
146  * non-null will decrement it's counter. The reference that
147  * @p dst points to will be set to @p src.
148  * @param[in] src Object to be have it's refcount increased @p dst is set to
149  * this.
150  */
151 static inline void
152 t_stereo_camera_calibration_reference(struct t_stereo_camera_calibration **dst,
153  struct t_stereo_camera_calibration *src)
154 {
155  struct t_stereo_camera_calibration *old_dst = *dst;
156 
157  if (old_dst == src) {
158  return;
159  }
160 
161  if (src) {
162  xrt_reference_inc(&src->reference);
163  }
164 
165  *dst = src;
166 
167  if (old_dst) {
168  if (xrt_reference_dec(&old_dst->reference)) {
170  }
171  }
172 }
173 
174 /*!
175  * Load stereo calibration data from a given file.
176  */
177 bool
179  FILE *calib_file, struct t_stereo_camera_calibration **out_data);
180 
181 /*!
182  * Load a stereo calibration struct from a hardcoded place.
183  */
184 bool
186  struct t_stereo_camera_calibration **out_data);
187 
188 /*!
189  * Save raw calibration data to file, hack until prober has storage for such
190  * things.
191  */
192 bool
194 
195 
196 /*
197  *
198  * Conversion functions.
199  *
200  */
201 
203 {
204  uint8_t v[256][256][256][3];
205 };
206 
207 void
208 t_convert_fill_table(struct t_convert_table *t);
209 
210 void
211 t_convert_make_y8u8v8_to_r8g8b8(struct t_convert_table *t);
212 
213 void
214 t_convert_make_y8u8v8_to_h8s8v8(struct t_convert_table *t);
215 
216 void
217 t_convert_make_h8s8v8_to_r8g8b8(struct t_convert_table *t);
218 
219 void
220 t_convert_in_place_y8u8v8_to_r8g8b8(uint32_t width,
221  uint32_t height,
222  size_t stride,
223  void *data_ptr);
224 
225 void
226 t_convert_in_place_y8u8v8_to_h8s8v8(uint32_t width,
227  uint32_t height,
228  size_t stride,
229  void *data_ptr);
230 
231 void
232 t_convert_in_place_h8s8v8_to_r8g8b8(uint32_t width,
233  uint32_t height,
234  size_t stride,
235  void *data_ptr);
236 
237 
238 /*
239  *
240  * Filter functions.
241  *
242  */
243 
244 #define T_HSV_SIZE 32
245 #define T_HSV_STEP (256 / T_HSV_SIZE)
246 
247 #define T_HSV_DEFAULT_PARAMS() \
248  { \
249  { \
250  {165, 30, 160, 100}, \
251  {135, 30, 160, 100}, \
252  {95, 30, 160, 100}, \
253  }, \
254  {128, 80}, \
255  }
256 
258 {
259  uint8_t hue_min;
260  uint8_t hue_range;
261 
262  uint8_t s_min;
263 
264  uint8_t v_min;
265 };
266 
268 {
269  struct t_hsv_filter_color color[3];
270 
271  struct
272  {
273  uint8_t s_max;
274  uint8_t v_min;
275  } white;
276 };
277 
279 {
280  uint8_t v[256][256][256];
281 };
282 
284 {
285  uint8_t v[T_HSV_SIZE][T_HSV_SIZE][T_HSV_SIZE];
286 };
287 
288 void
289 t_hsv_build_convert_table(struct t_hsv_filter_params *params,
290  struct t_convert_table *t);
291 
292 void
293 t_hsv_build_large_table(struct t_hsv_filter_params *params,
294  struct t_hsv_filter_large_table *t);
295 
296 void
297 t_hsv_build_optimized_table(struct t_hsv_filter_params *params,
298  struct t_hsv_filter_optimized_table *t);
299 
300 static inline uint8_t
301 t_hsv_filter_sample(struct t_hsv_filter_optimized_table *t,
302  uint32_t y,
303  uint32_t u,
304  uint32_t v)
305 {
306  return t->v[y / T_HSV_STEP][u / T_HSV_STEP][v / T_HSV_STEP];
307 }
308 
309 int
310 t_hsv_filter_create(struct xrt_frame_context *xfctx,
311  struct t_hsv_filter_params *params,
312  struct xrt_frame_sink *sinks[4],
313  struct xrt_frame_sink **out_sink);
314 
315 
316 /*
317  *
318  * Tracker code.
319  *
320  */
321 
322 int
323 t_psmv_start(struct xrt_tracked_psmv *xtmv);
324 
325 int
326 t_psmv_create(struct xrt_frame_context *xfctx,
327  struct xrt_colour_rgb_f32 *rgb,
328  struct t_stereo_camera_calibration *data,
329  struct xrt_tracked_psmv **out_xtmv,
330  struct xrt_frame_sink **out_sink);
331 
332 int
333 t_psvr_start(struct xrt_tracked_psvr *xtvr);
334 
335 int
336 t_psvr_create(struct xrt_frame_context *xfctx,
337  struct t_stereo_camera_calibration *data,
338  struct xrt_tracked_psvr **out_xtvr,
339  struct xrt_frame_sink **out_sink);
340 
341 
342 /*
343  *
344  * Camera calibration
345  *
346  */
347 
348 /*!
349  * Board pattern type.
350  */
352 {
353  T_BOARD_CHECKERS,
354  T_BOARD_CIRCLES,
355  T_BOARD_ASYMMETRIC_CIRCLES,
356 };
357 
359 {
360  //! Is calibration finished?
361  bool finished;
362  //! Was the target found this frame?
363  bool found;
364  //! Number of frames collected
366  //! Number of moving frames before another capture
367  int cooldown;
368  //! Number of non-moving frames before capture.
370  //! Stereo calibration data that was produced.
372 };
373 
375 {
376  //! Should we use fisheye version of the calibration functions.
378  //! Is the camera a stereo sbs camera, mostly for image loading.
380  //! What type of pattern are we using for calibration.
381  enum t_board_pattern pattern;
382 
383  struct
384  {
385  int cols;
386  int rows;
387  float size_meters;
388 
389  bool subpixel_enable;
390  int subpixel_size;
391  } checkers;
392 
393  struct
394  {
395  int cols;
396  int rows;
397  float distance_meters;
398  } circles;
399 
400  struct
401  {
402  int cols;
403  int rows;
404  float diagonal_distance_meters;
405  } asymmetric_circles;
406 
407  struct
408  {
409  bool enabled;
410  int num_images;
411  } load;
412 
413  int num_cooldown_frames;
414  int num_wait_for;
415  int num_collect_total;
416  int num_collect_restart;
417 
418  /*!
419  * Should we mirror the RGB image?
420  *
421  * Before text is written out, has no effect on actual image capture.
422  */
424 
425  bool save_images;
426 };
427 
428 /*!
429  * Sets the calibration parameters to the their default values.
430  */
431 static inline void
432 t_calibration_params_default(struct t_calibration_params *p)
433 {
434  // Camera config.
435  p->use_fisheye = false;
436  p->stereo_sbs = true;
437 
438  // Which board should we calibrate against.
439  p->pattern = T_BOARD_CHECKERS;
440 
441  // Checker board.
442  p->checkers.cols = 9;
443  p->checkers.rows = 7;
444  p->checkers.size_meters = 0.025f;
445  p->checkers.subpixel_enable = true;
446  p->checkers.subpixel_size = 5;
447 
448  // Symmetrical circles.
449  p->circles.cols = 9;
450  p->circles.rows = 7;
451  p->circles.distance_meters = 0.025f;
452 
453  // Asymmetrical circles.
454  p->asymmetric_circles.cols = 5;
455  p->asymmetric_circles.rows = 17;
456  p->asymmetric_circles.diagonal_distance_meters = 0.02f;
457 
458  // Loading of images.
459  p->load.enabled = false;
460  p->load.num_images = 20;
461 
462  // Frame collection info.
463  p->num_cooldown_frames = 20;
464  p->num_wait_for = 5;
465  p->num_collect_total = 20;
466  p->num_collect_restart = 1;
467 
468  // Misc.
469  p->mirror_rgb_image = false;
470  p->save_images = true;
471 }
472 
473 /*!
474  * @brief Create the camera calibration frame sink.
475  *
476  * @param xfctx Context for frame transport.
477  * @param params Parameters to use during calibration. Values copied, pointer
478  * not retained.
479  * @param status Optional pointer to structure for status information. Pointer
480  * retained, and pointed-to struct modified.
481  * @param gui Frame sink
482  * @param out_sink Output: created frame sink.
483  */
484 int
486  const struct t_calibration_params *params,
487  struct t_calibration_status *status,
488  struct xrt_frame_sink *gui,
489  struct xrt_frame_sink **out_sink);
490 
491 
492 /*
493  *
494  * Sink creation functions.
495  *
496  */
497 
498 int
499 t_convert_yuv_or_yuyv_create(struct xrt_frame_sink *next,
500  struct xrt_frame_sink **out_sink);
501 
502 
503 
504 int
505 t_debug_hsv_picker_create(struct xrt_frame_context *xfctx,
506  struct xrt_frame_sink *passthrough,
507  struct xrt_frame_sink **out_sink);
508 
509 int
510 t_debug_hsv_viewer_create(struct xrt_frame_context *xfctx,
511  struct xrt_frame_sink *passthrough,
512  struct xrt_frame_sink **out_sink);
513 
514 int
515 t_debug_hsv_filter_create(struct xrt_frame_context *xfctx,
516  struct xrt_frame_sink *passthrough,
517  struct xrt_frame_sink **out_sink);
518 
519 /*!
520  * @}
521  */
522 
523 
524 #ifdef __cplusplus
525 }
526 #endif
struct t_stereo_camera_calibration * stereo_data
Stereo calibration data that was produced.
Definition: t_tracking.h:371
double distortion[XRT_DISTORTION_MAX_DIM]
Rectilinear distortion coefficients: k1, k2, p1, p2[, k3[, k4, k5, k6[, s1, s2, s3, s4]].
Definition: t_tracking.h:99
t_board_pattern
Board pattern type.
Definition: t_tracking.h:351
bool stereo_sbs
Is the camera a stereo sbs camera, mostly for image loading.
Definition: t_tracking.h:379
Essential calibration data for a single camera, or single lens/sensor of a stereo camera...
Definition: t_tracking.h:89
bool mirror_rgb_image
Should we mirror the RGB image?
Definition: t_tracking.h:423
int num_collected
Number of frames collected.
Definition: t_tracking.h:365
Definition: t_tracking.h:374
bool t_file_save_raw_data_hack(struct t_stereo_camera_calibration *data)
Save raw calibration data to file, hack until prober has storage for such things. ...
Definition: t_file.cpp:346
Definition: t_tracking.h:267
A object that is sent frames.
Definition: xrt_frame.h:51
Definition: t_tracking.h:278
double distortion_fisheye[4]
Fisheye camera distortion coefficients.
Definition: t_tracking.h:102
Definition: t_tracking.h:257
Stereo camera calibration data to be given to trackers.
Definition: t_tracking.h:111
Image size.
Definition: xrt_defines.h:216
void t_stereo_camera_calibration_destroy(struct t_stereo_camera_calibration *c)
Only to be called by t_stereo_camera_calibration_reference.
Definition: t_data_utils.c:25
#define XRT_DISTORTION_MAX_DIM
Maximum size of rectilinear distortion coefficient array.
Definition: t_tracking.h:83
bool use_fisheye
Should we use fisheye version of the calibration functions.
Definition: t_tracking.h:377
Definition: t_tracking.h:283
bool found
Was the target found this frame?
Definition: t_tracking.h:363
double intrinsics[3][3]
Camera intrinsics matrix.
Definition: t_tracking.h:95
Object used to track all sinks and frame producers in a graph.
Definition: xrt_frame.h:87
Definition: t_tracking.h:202
enum t_board_pattern pattern
What type of pattern are we using for calibration.
Definition: t_tracking.h:381
Definition: t_tracking.h:358
Data frame header.
bool use_fisheye
Is the camera fisheye?
Definition: t_tracking.h:105
int t_calibration_stereo_create(struct xrt_frame_context *xfctx, const struct t_calibration_params *params, struct t_calibration_status *status, struct xrt_frame_sink *gui, struct xrt_frame_sink **out_sink)
Create the camera calibration frame sink.
Definition: t_calibration.cpp:1222
A 3 element colour with floating point channels.
Definition: xrt_defines.h:191
bool t_stereo_camera_calibration_load_v1_hack(struct t_stereo_camera_calibration **out_data)
Load a stereo calibration struct from a hardcoded place.
Definition: t_file.cpp:322
A base class for reference counted objects.
Definition: xrt_defines.h:24
A tracked PSVR headset.
Definition: xrt_tracking.h:148
bool t_stereo_camera_calibration_load_v1(FILE *calib_file, struct t_stereo_camera_calibration **out_data)
Load stereo calibration data from a given file.
Definition: t_file.cpp:190
Very small misc utils.
A single tracked PS Move controller, camera and ball are not synced.
Definition: xrt_tracking.h:105
int cooldown
Number of moving frames before another capture.
Definition: t_tracking.h:367
int waits_remaining
Number of non-moving frames before capture.
Definition: t_tracking.h:369
void t_stereo_camera_calibration_alloc(struct t_stereo_camera_calibration **calib)
Allocates a new stereo calibration data, unreferences the old calib.
Definition: t_data_utils.c:17
struct xrt_size image_size_pixels
Source image size.
Definition: t_tracking.h:92
bool finished
Is calibration finished?
Definition: t_tracking.h:361
struct xrt_reference reference
Ref counting.
Definition: t_tracking.h:114