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