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, estimating linear and angular velocity based on the previous entry if the input
67 * relation does not have valid velocity data.
68 *
69 * If the history is full, it will also pop a pose out of the other side of the buffer.
70 *
71 * @return false if the timestamp is earlier than the most recent timestamp already recorded
72 *
73 * @public @memberof m_relation_history
74 */
75bool
77 struct xrt_space_relation const *in_relation,
78 int64_t timestamp);
79
80/*!
81 * Interpolates or extrapolates to the desired timestamp.
82 *
83 * Read-only operation - doesn't remove anything from the buffer or anything like that - you can call this as often
84 * as you want.
85 *
86 * @public @memberof m_relation_history
87 */
90 int64_t at_timestamp_ns,
91 struct xrt_space_relation *out_relation);
92
93/*!
94 * Get the latest report in the buffer, if any.
95 *
96 * @param rh self
97 * @param[out] out_time_ns Populated with the latest report time, if any
98 * @param[out] out_relation Populated with the latest relation, if any
99 *
100 * @return false if the history is empty.
101 *
102 * @public @memberof m_relation_history
103 */
104bool
106 int64_t *out_time_ns,
107 struct xrt_space_relation *out_relation);
108
109/*!
110 * Returns the number of items in the history.
111 *
112 * @public @memberof m_relation_history
113 */
114uint32_t
116
117/*!
118 * Clears the history from all of the items.
119 *
120 * @public @memberof m_relation_history
121 */
122void
124
125/*!
126 * Destroys an opaque relation_history object.
127 *
128 * @public @memberof m_relation_history
129 */
130void
132
133#ifdef __cplusplus
134}
135#endif
136
137
138#ifdef __cplusplus
139namespace xrt::auxiliary::math {
140
141/*!
142 * C++ interface for @ref m_relation_history, non-copyable/deletable.
143 *
144 * @ingroup aux_math
145 */
146class RelationHistory
147{
148public:
149 /*!
150 * @copydoc m_relation_history_result
151 */
152 typedef m_relation_history_result Result;
153
154
155private:
156 m_relation_history *mPtr{nullptr};
157
158
159public:
160 // clang-format off
161 RelationHistory() noexcept { m_relation_history_create(&mPtr); }
162 ~RelationHistory() { m_relation_history_destroy(&mPtr); }
163 // clang-format on
164
165 // Special non-copyable reference.
166 RelationHistory(RelationHistory const &) = delete;
167 RelationHistory(RelationHistory &&) = delete;
168 RelationHistory &
169 operator=(RelationHistory const &) = delete;
170 RelationHistory &
171 operator=(RelationHistory &&) = delete;
172
173
174 /*!
175 * @copydoc m_relation_history_push
176 */
177 bool
178 push(xrt_space_relation const &relation, int64_t ts) noexcept
179 {
180 return m_relation_history_push(mPtr, &relation, ts);
181 }
182
183 /*!
184 * @copydoc m_relation_history_get
185 */
186 Result
187 get(int64_t at_time_ns, xrt_space_relation *out_relation) const noexcept
188 {
189 return m_relation_history_get(mPtr, at_time_ns, out_relation);
190 }
191
192 /*!
193 * @copydoc m_relation_history_get_latest
194 */
195 bool
196 get_latest(int64_t *out_time_ns, xrt_space_relation *out_relation) const noexcept
197 {
198 return m_relation_history_get_latest(mPtr, out_time_ns, out_relation);
199 }
200
201 /*!
202 * @copydoc m_relation_history_get_size
203 */
204 size_t
205 size() const noexcept
206 {
207 return m_relation_history_get_size(mPtr);
208 }
209
210 /*!
211 * @copydoc m_relation_history_clear
212 */
213 void
214 clear() noexcept
215 {
216 return m_relation_history_clear(mPtr);
217 }
218};
219
220} // namespace xrt::auxiliary::math
221#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:87
@ 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:49
void m_relation_history_destroy(struct m_relation_history **rh)
Destroys an opaque relation_history object.
Definition m_relation_history.cpp:273
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:259
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:63
void m_relation_history_clear(struct m_relation_history *rh)
Clears the history from all of the items.
Definition m_relation_history.cpp:266
void m_relation_history_create(struct m_relation_history **rh)
Creates an opaque relation_history object.
Definition m_relation_history.cpp:56
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:226
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:245
A relation with two spaces, includes velocity and acceleration.
Definition xrt_defines.h:683
Common defines and enums for XRT.