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