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 */
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
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
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
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 */
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
120 int64_t *out_time_ns,
121 struct xrt_space_relation *out_relation);
122
123/*!
124 * Returns the number of items in the history.
125 *
126 * @public @memberof m_relation_history
127 */
128uint32_t
130
131/*!
132 * Clears the history from all of the items.
133 *
134 * @public @memberof m_relation_history
135 */
136void
138
139/*!
140 * Destroys an opaque relation_history object.
141 *
142 * @public @memberof m_relation_history
143 */
144void
146
147#ifdef __cplusplus
148}
149#endif
150
151
152#ifdef __cplusplus
153namespace xrt::auxiliary::math {
154
155/*!
156 * C++ interface for @ref m_relation_history, non-copyable/deletable.
157 *
158 * @ingroup aux_math
159 */
160class RelationHistory
161{
162public:
163 /*!
164 * @copydoc m_relation_history_result
165 */
166 typedef m_relation_history_result Result;
167
168
169private:
170 m_relation_history *mPtr{nullptr};
171
172
173public:
174 // clang-format off
175 RelationHistory() noexcept { m_relation_history_create(&mPtr); }
176 ~RelationHistory() { m_relation_history_destroy(&mPtr); }
177 // clang-format on
178
179 // Special non-copyable reference.
180 RelationHistory(RelationHistory const &) = delete;
181 RelationHistory(RelationHistory &&) = delete;
182 RelationHistory &
183 operator=(RelationHistory const &) = delete;
184 RelationHistory &
185 operator=(RelationHistory &&) = delete;
186
187
188 /*!
189 * @copydoc m_relation_history_push
190 */
191 bool
192 push(xrt_space_relation const &relation, int64_t ts) noexcept
193 {
194 return m_relation_history_push(mPtr, &relation, ts);
195 }
196
197 /*!
198 * @copydoc m_relation_history_get
199 */
200 Result
201 get(int64_t at_time_ns, xrt_space_relation *out_relation) const noexcept
202 {
203 return m_relation_history_get(mPtr, at_time_ns, out_relation);
204 }
205
206 /*!
207 * @copydoc m_relation_history_get_latest
208 */
209 bool
210 get_latest(int64_t *out_time_ns, xrt_space_relation *out_relation) const noexcept
211 {
212 return m_relation_history_get_latest(mPtr, out_time_ns, out_relation);
213 }
214
215 /*!
216 * @copydoc m_relation_history_get_size
217 */
218 size_t
219 size() const noexcept
220 {
221 return m_relation_history_get_size(mPtr);
222 }
223
224 /*!
225 * @copydoc m_relation_history_clear
226 */
227 void
228 clear() noexcept
229 {
230 return m_relation_history_clear(mPtr);
231 }
232};
233
234} // namespace xrt::auxiliary::math
235#endif
enum m_relation_history_result m_relation_history_get(const struct m_relation_history *rh, int64_t at_timestamp_ns, struct xrt_space_relation *out_relation)
Definition m_relation_history.cpp:180
@ 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
C++-only functionality in the Math helper library.
Definition m_documentation.hpp:15
Definition m_relation_history.cpp:50
bool m_relation_history_push_or_update(struct m_relation_history *rh, struct xrt_space_relation const *in_relation, int64_t timestamp)
Pushes a new pose to the history, or updates an existing one.
Definition m_relation_history.cpp:162
void m_relation_history_destroy(struct m_relation_history **rh)
Destroys an opaque relation_history object.
Definition m_relation_history.cpp:327
m_relation_history_result
Describes how the resulting space relation for the desired time stamp was generated.
Definition m_relation_history.h:37
uint32_t m_relation_history_get_size(const struct m_relation_history *rh)
Returns the number of items in the history.
Definition m_relation_history.cpp:313
bool m_relation_history_push(struct m_relation_history *rh, struct xrt_space_relation const *in_relation, int64_t timestamp)
Pushes a new pose to the history.
Definition m_relation_history.cpp:146
void m_relation_history_clear(struct m_relation_history *rh)
Clears the history from all of the items.
Definition m_relation_history.cpp:320
void m_relation_history_create(struct m_relation_history **rh)
Creates an opaque relation_history object.
Definition m_relation_history.cpp:139
bool m_relation_history_push_with_motion_estimation(struct m_relation_history *rh, struct xrt_space_relation const *in_relation, int64_t timestamp)
Pushes a new pose to the history, estimating linear and angular velocity based on the previous entry ...
Definition m_relation_history.cpp:280
bool m_relation_history_get_latest(const struct m_relation_history *rh, int64_t *out_time_ns, struct xrt_space_relation *out_relation)
Get the latest report in the buffer, if any.
Definition m_relation_history.cpp:299
A relation with two spaces, includes velocity and acceleration.
Definition xrt_defines.h:713
Common defines and enums for XRT.