Monado OpenXR Runtime
Loading...
Searching...
No Matches
m_relation_history.h
Go to the documentation of this file.
1// Copyright 2021, Collabora, Ltd.
2// SPDX-License-Identifier: BSL-1.0
3/*!
4 * @file
5 * @brief Small utility for keeping track of the history of an xrt_space_relation, ie. for knowing where a HMD or
6 * controller was in the past
7 * @author Moshi Turner <moshiturner@protonmail.com>
8 * @author Rylie Pavlik <rylie.pavlik@collabora.com>
9 * @author Korcan Hussein <korcan.hussein@collabora.com>
10 * @ingroup aux_util
11 */
12#pragma once
13
14#include "xrt/xrt_defines.h"
15
16#ifdef __cplusplus
17extern "C" {
18#endif
19
20/**
21 * @brief Opaque type for storing the history of a space relation in a ring buffer
22 *
23 * @note Unlike the bare C++ data structure @ref HistoryBuffer this wraps, **this is a thread safe interface**,
24 * and is safe for concurrent access from multiple threads.
25 * (It is using a simple mutex, not a reader/writer lock, but that is fine until proven to be a bottleneck.)
26 *
27 * @ingroup aux_util
28 */
29struct m_relation_history;
30
31/**
32 * @brief Describes how the resulting space relation for the desired time stamp was generated.
33 *
34 * @relates m_relation_history
35 */
37{
38 M_RELATION_HISTORY_RESULT_INVALID = 0, //!< The supplied timestamp was invalid (0) or buffer was empty
39 M_RELATION_HISTORY_RESULT_EXACT, //!< The exact desired timestamp was found
40 M_RELATION_HISTORY_RESULT_INTERPOLATED, //!< The desired timestamp was between two entries
41 M_RELATION_HISTORY_RESULT_PREDICTED, //!< The desired timestamp was newer than the most recent entry
42 M_RELATION_HISTORY_RESULT_REVERSE_PREDICTED, //!< The desired timestamp was older than the oldest entry
43};
44
45/*!
46 * Creates an opaque relation_history object.
47 *
48 * @public @memberof m_relation_history
49 */
50void
51m_relation_history_create(struct m_relation_history **rh);
52
53/*!
54 * Pushes a new pose to the history.
55 *
56 * If the history is full, it will also pop a pose out of the other side of the buffer.
57 *
58 * @return false if the timestamp is earlier than the most recent timestamp already recorded
59 *
60 * @public @memberof m_relation_history
61 */
62bool
63m_relation_history_push(struct m_relation_history *rh, struct xrt_space_relation const *in_relation, int64_t timestamp);
64
65/*!
66 * Pushes a new pose to the history, or updates an existing one.
67 *
68 * If the history is full, it will also pop a pose out of the other side of the buffer.
69 *
70 * @return false if the timestamp is not new or doesn't match an older timestamp
71 *
72 * @public @memberof m_relation_history
73 */
74bool
75m_relation_history_push_or_update(struct m_relation_history *rh,
76 struct xrt_space_relation const *in_relation,
77 int64_t timestamp);
78
79/*!
80 * Pushes a new pose to the history, estimating linear and angular velocity based on the previous entry if the input
81 * relation does not have valid velocity data.
82 *
83 * If the history is full, it will also pop a pose out of the other side of the buffer.
84 *
85 * @return false if the timestamp is earlier than the most recent timestamp already recorded
86 *
87 * @public @memberof m_relation_history
88 */
89bool
90m_relation_history_push_with_motion_estimation(struct m_relation_history *rh,
91 struct xrt_space_relation const *in_relation,
92 int64_t timestamp);
93
94/*!
95 * Interpolates or extrapolates to the desired timestamp.
96 *
97 * Read-only operation - doesn't remove anything from the buffer or anything like that - you can call this as often
98 * as you want.
99 *
100 * @public @memberof m_relation_history
101 */
103m_relation_history_get(const struct m_relation_history *rh,
104 int64_t at_timestamp_ns,
105 struct xrt_space_relation *out_relation);
106
107/*!
108 * Get the latest report in the buffer, if any.
109 *
110 * @param rh self
111 * @param[out] out_time_ns Populated with the latest report time, if any
112 * @param[out] out_relation Populated with the latest relation, if any
113 *
114 * @return false if the history is empty.
115 *
116 * @public @memberof m_relation_history
117 */
118bool
119m_relation_history_get_latest(const struct m_relation_history *rh,
120 int64_t *out_time_ns,
121 struct xrt_space_relation *out_relation);
122
123/*!
124 * Get the relation stored at exactly the given timestamp, if any.
125 *
126 * Unlike @ref m_relation_history_get, this never interpolates or extrapolates, and fails if there is no entry with
127 * precisely this timestamp.
128 *
129 * @param rh self
130 * @param at_timestamp_ns The exact timestamp to look up
131 * @param[out] out_relation Populated with the stored relation, if found
132 *
133 * @return false if there is no entry at that timestamp.
134 *
135 * @public @memberof m_relation_history
136 */
137bool
138m_relation_history_get_exact(const struct m_relation_history *rh,
139 int64_t at_timestamp_ns,
140 struct xrt_space_relation *out_relation);
141
142/*!
143 * Get the stored relation nearest to the given timestamp, if one is within @p max_slop_ns of it.
144 *
145 * Like @ref m_relation_history_get_exact, this never interpolates or extrapolates, but accepts entries that are up
146 * to @p max_slop_ns away (inclusive) from the requested timestamp. If two entries are equally close, the older one
147 * is returned.
148 *
149 * @param rh self
150 * @param at_timestamp_ns The timestamp to look up
151 * @param max_slop_ns Maximum allowed distance between @p at_timestamp_ns and the found entry, must be >= 0
152 * @param[out] out_time_ns Populated with the found entry's timestamp, if found
153 * @param[out] out_relation Populated with the stored relation, if found
154 *
155 * @return false if there is no entry within @p max_slop_ns of the timestamp.
156 *
157 * @public @memberof m_relation_history
158 */
159bool
160m_relation_history_get_nearest(const struct m_relation_history *rh,
161 int64_t at_timestamp_ns,
162 int64_t max_slop_ns,
163 int64_t *out_time_ns,
164 struct xrt_space_relation *out_relation);
165
166/*!
167 * Returns the number of items in the history.
168 *
169 * @public @memberof m_relation_history
170 */
171uint32_t
172m_relation_history_get_size(const struct m_relation_history *rh);
173
174/*!
175 * Clears the history from all of the items.
176 *
177 * @public @memberof m_relation_history
178 */
179void
180m_relation_history_clear(struct m_relation_history *rh);
181
182/*!
183 * Destroys an opaque relation_history object.
184 *
185 * @public @memberof m_relation_history
186 */
187void
188m_relation_history_destroy(struct m_relation_history **rh);
189
190#ifdef __cplusplus
191}
192#endif
m_relation_history_result
Describes how the resulting space relation for the desired time stamp was generated.
Definition m_relation_history.h:37
@ M_RELATION_HISTORY_RESULT_INTERPOLATED
The desired timestamp was between two entries.
Definition m_relation_history.h:40
@ M_RELATION_HISTORY_RESULT_PREDICTED
The desired timestamp was newer than the most recent entry.
Definition m_relation_history.h:41
@ M_RELATION_HISTORY_RESULT_REVERSE_PREDICTED
The desired timestamp was older than the oldest entry.
Definition m_relation_history.h:42
@ M_RELATION_HISTORY_RESULT_INVALID
The supplied timestamp was invalid (0) or buffer was empty.
Definition m_relation_history.h:38
@ M_RELATION_HISTORY_RESULT_EXACT
The exact desired timestamp was found.
Definition m_relation_history.h:39
A relation with two spaces, includes velocity and acceleration.
Definition xrt_defines.h:726
Common defines and enums for XRT.