Monado OpenXR Runtime
m_filter_fifo.h
Go to the documentation of this file.
1// Copyright 2020, Collabora, Ltd.
2// SPDX-License-Identifier: BSL-1.0
3/*!
4 * @file
5 * @brief A fifo that also lets you dynamically filter.
6 * @author Jakob Bornecrantz <jakob@collabora.com>
7 * @ingroup aux_math
8 */
9
10#pragma once
11
12#include "xrt/xrt_defines.h" // IWYU pragma: keep
13
14#ifdef __cplusplus
15extern "C" {
16#endif
17
18
19struct m_ff_f64;
20struct m_ff_vec3_f32;
21
22/*!
23 * Allocates a filter fifo tracking @p num samples and fills it with @p num
24 * samples at timepoint zero.
25 */
26void
27m_ff_vec3_f32_alloc(struct m_ff_vec3_f32 **ff_out, size_t num);
28
29/*!
30 * Frees the given filter fifo and all its samples.
31 */
32void
33m_ff_vec3_f32_free(struct m_ff_vec3_f32 **ff_ptr);
34
35/*!
36 * Return the number of samples that can fill the fifo.
37 */
38size_t
40
41/*!
42 * Pushes a sample at the given timepoint, pushing samples out of order yields
43 * unspecified behaviour, so samples must be pushed in time order.
44 */
45void
46m_ff_vec3_f32_push(struct m_ff_vec3_f32 *ff, const struct xrt_vec3 *sample, uint64_t timestamp_ns);
47
48/*!
49 * Return the sample at the index, zero means the last sample push, one second
50 * last and so on.
51 */
52bool
53m_ff_vec3_f32_get(struct m_ff_vec3_f32 *ff, size_t num, struct xrt_vec3 *out_sample, uint64_t *out_timestamp_ns);
54
55/*!
56 * Return the timestamp of the sample at the index, zero means the last sample push, one second
57 * last and so on.
58 */
59bool
60m_ff_vec3_f32_get_timestamp(struct m_ff_vec3_f32 *ff, size_t num, uint64_t *out_timestamp_ns);
61
62/*!
63 * Averages all samples in the fifo between the two timepoints, returns number
64 * of samples sampled, if no samples was found between the timpoints returns 0
65 * and sets @p out_average to all zeros.
66 *
67 * @param ff Filter fifo to search in.
68 * @param start_ns Timepoint furthest in the past, to start searching for
69 * samples.
70 * @param stop_ns Timepoint closest in the past, or now, to stop searching
71 * for samples.
72 * @param out_average Average of all samples in the given timeframe.
73 */
74size_t
75m_ff_vec3_f32_filter(struct m_ff_vec3_f32 *ff, uint64_t start_ns, uint64_t stop_ns, struct xrt_vec3 *out_average);
76
77/*!
78 * Allocates a filter fifo tracking @p num samples and fills it with @p num
79 * samples at timepoint zero.
80 */
81void
82m_ff_f64_alloc(struct m_ff_f64 **ff_out, size_t num);
83
84/*!
85 * Frees the given filter fifo and all its samples.
86 */
87void
88m_ff_f64_free(struct m_ff_f64 **ff_ptr);
89
90/*!
91 * Return the number of samples that can fill the fifo.
92 */
93size_t
94m_ff_f64_get_num(struct m_ff_f64 *ff);
95
96/*!
97 * Pushes a sample at the given timepoint, pushing samples out of order yields
98 * unspecified behaviour, so samples must be pushed in time order.
99 */
100void
101m_ff_f64_push(struct m_ff_f64 *ff, const double *sample, uint64_t timestamp_ns);
102
103/*!
104 * Return the sample at the index, 0 means the last sample push, 1 second-to-last, etc.
105 */
106bool
107m_ff_f64_get(struct m_ff_f64 *ff, size_t num, double *out_sample, uint64_t *out_timestamp_ns);
108
109/*!
110 * Averages all samples in the fifo between the two timepoints, returns number
111 * of samples sampled, if no samples was found between the timpoints returns 0
112 * and sets @p out_average to all zeros.
113 *
114 * @param ff Filter fifo to search in.
115 * @param start_ns Timepoint furthest in the past, to start searching for
116 * samples.
117 * @param stop_ns Timepoint closest in the past, or now, to stop searching
118 * for samples.
119 * @param out_average Average of all samples in the given timeframe.
120 */
121size_t
122m_ff_f64_filter(struct m_ff_f64 *ff, uint64_t start_ns, uint64_t stop_ns, double *out_average);
123
124
125#ifdef __cplusplus
126}
127
128/*!
129 * Helper class to wrap a C filter fifo (@ref m_ff_vec3_f32).
130 */
131class FilterFifo3F
132{
133private:
134 m_ff_vec3_f32 *mFifoPtr;
135
136
137public:
138 FilterFifo3F() = delete;
139
140 FilterFifo3F(size_t size)
141 {
142 m_ff_vec3_f32_alloc(&mFifoPtr, size);
143 }
144
145 ~FilterFifo3F()
146 {
147 m_ff_vec3_f32_free(&mFifoPtr);
148 }
149
150 /*!
151 * Get the pointer to the C filter fifo, ownership is not passed.
152 */
153 inline m_ff_vec3_f32 *
154 unsafeGetFilterFifo()
155 {
156 return mFifoPtr;
157 }
158
159 /*!
160 * @copydoc m_ff_vec3_f32_push
161 *
162 * Wrapper for @ref m_ff_vec3_f32_push.
163 */
164 inline void
165 push(const xrt_vec3 &sample, uint64_t timestamp_ns)
166 {
167 m_ff_vec3_f32_push(mFifoPtr, &sample, timestamp_ns);
168 }
169
170 /*!
171 * @copydoc m_ff_vec3_f32_get
172 *
173 * Wrapper for @ref m_ff_vec3_f32_get.
174 */
175 inline bool
176 get(size_t num, xrt_vec3 *out_sample, uint64_t *out_timestamp_ns)
177 {
178 return m_ff_vec3_f32_get(mFifoPtr, num, out_sample, out_timestamp_ns);
179 }
180
181 /*!
182 * @copydoc m_ff_vec3_f32_filter
183 *
184 * Wrapper for @ref m_ff_vec3_f32_filter.
185 */
186 inline size_t
187 filter(uint64_t start_ns, uint64_t stop_ns, struct xrt_vec3 *out_average)
188 {
189 return m_ff_vec3_f32_filter(mFifoPtr, start_ns, stop_ns, out_average);
190 }
191};
192#endif
void m_ff_f64_push(struct m_ff_f64 *ff, const double *sample, uint64_t timestamp_ns)
Pushes a sample at the given timepoint, pushing samples out of order yields unspecified behaviour,...
Definition: m_filter_fifo.c:273
bool m_ff_vec3_f32_get_timestamp(struct m_ff_vec3_f32 *ff, size_t num, uint64_t *out_timestamp_ns)
Return the timestamp of the sample at the index, zero means the last sample push, one second last and...
Definition: m_filter_fifo.c:113
bool m_ff_f64_get(struct m_ff_f64 *ff, size_t num, double *out_sample, uint64_t *out_timestamp_ns)
Return the sample at the index, 0 means the last sample push, 1 second-to-last, etc.
Definition: m_filter_fifo.c:286
void m_ff_f64_alloc(struct m_ff_f64 **ff_out, size_t num)
Allocates a filter fifo tracking num samples and fills it with num samples at timepoint zero.
Definition: m_filter_fifo.c:246
void m_ff_vec3_f32_free(struct m_ff_vec3_f32 **ff_ptr)
Frees the given filter fifo and all its samples.
Definition: m_filter_fifo.c:81
void m_ff_vec3_f32_push(struct m_ff_vec3_f32 *ff, const struct xrt_vec3 *sample, uint64_t timestamp_ns)
Pushes a sample at the given timepoint, pushing samples out of order yields unspecified behaviour,...
Definition: m_filter_fifo.c:100
void m_ff_vec3_f32_alloc(struct m_ff_vec3_f32 **ff_out, size_t num)
Allocates a filter fifo tracking num samples and fills it with num samples at timepoint zero.
Definition: m_filter_fifo.c:73
size_t m_ff_vec3_f32_filter(struct m_ff_vec3_f32 *ff, uint64_t start_ns, uint64_t stop_ns, struct xrt_vec3 *out_average)
Averages all samples in the fifo between the two timepoints, returns number of samples sampled,...
Definition: m_filter_fifo.c:140
bool m_ff_vec3_f32_get(struct m_ff_vec3_f32 *ff, size_t num, struct xrt_vec3 *out_sample, uint64_t *out_timestamp_ns)
Return the sample at the index, zero means the last sample push, one second last and so on.
Definition: m_filter_fifo.c:126
void m_ff_f64_free(struct m_ff_f64 **ff_ptr)
Frees the given filter fifo and all its samples.
Definition: m_filter_fifo.c:254
size_t m_ff_f64_filter(struct m_ff_f64 *ff, uint64_t start_ns, uint64_t stop_ns, double *out_average)
Averages all samples in the fifo between the two timepoints, returns number of samples sampled,...
Definition: m_filter_fifo.c:300
size_t m_ff_vec3_f32_get_num(struct m_ff_vec3_f32 *ff)
Return the number of samples that can fill the fifo.
Definition: m_filter_fifo.c:94
size_t m_ff_f64_get_num(struct m_ff_f64 *ff)
Return the number of samples that can fill the fifo.
Definition: m_filter_fifo.c:267
Definition: m_filter_fifo.c:198
Definition: m_filter_fifo.c:23
A 3 element vector with single floats.
Definition: xrt_defines.h:289
static const cJSON * get(const cJSON *json, const char *f)
Less typing.
Definition: u_json.c:36
Common defines and enums for XRT.