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
15 extern "C" {
16 #endif
17 
18 
19 struct m_ff_f64;
20 struct 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  */
26 void
27 m_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  */
32 void
33 m_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  */
38 size_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  */
45 void
46 m_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  */
52 bool
53 m_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  * Averages all samples in the fifo between the two timepoints, returns number
57  * of samples sampled, if no samples was found between the timpoints returns 0
58  * and sets @p out_average to all zeros.
59  *
60  * @param ff Filter fifo to search in.
61  * @param start_ns Timepoint furthest in the past, to start searching for
62  * samples.
63  * @param stop_ns Timepoint closest in the past, or now, to stop searching
64  * for samples.
65  * @param out_average Average of all samples in the given timeframe.
66  */
67 size_t
68 m_ff_vec3_f32_filter(struct m_ff_vec3_f32 *ff, uint64_t start_ns, uint64_t stop_ns, struct xrt_vec3 *out_average);
69 
70 /*!
71  * Allocates a filter fifo tracking @p num samples and fills it with @p num
72  * samples at timepoint zero.
73  */
74 void
75 m_ff_f64_alloc(struct m_ff_f64 **ff_out, size_t num);
76 
77 /*!
78  * Frees the given filter fifo and all its samples.
79  */
80 void
81 m_ff_f64_free(struct m_ff_f64 **ff_ptr);
82 
83 /*!
84  * Return the number of samples that can fill the fifo.
85  */
86 size_t
87 m_ff_f64_get_num(struct m_ff_f64 *ff);
88 
89 /*!
90  * Pushes a sample at the given timepoint, pushing samples out of order yields
91  * unspecified behaviour, so samples must be pushed in time order.
92  */
93 void
94 m_ff_f64_push(struct m_ff_f64 *ff, const double *sample, uint64_t timestamp_ns);
95 
96 /*!
97  * Return the sample at the index, 0 means the last sample push, 1 second-to-last, etc.
98  */
99 bool
100 m_ff_f64_get(struct m_ff_f64 *ff, size_t num, double *out_sample, uint64_t *out_timestamp_ns);
101 
102 /*!
103  * Averages all samples in the fifo between the two timepoints, returns number
104  * of samples sampled, if no samples was found between the timpoints returns 0
105  * and sets @p out_average to all zeros.
106  *
107  * @param ff Filter fifo to search in.
108  * @param start_ns Timepoint furthest in the past, to start searching for
109  * samples.
110  * @param stop_ns Timepoint closest in the past, or now, to stop searching
111  * for samples.
112  * @param out_average Average of all samples in the given timeframe.
113  */
114 size_t
115 m_ff_f64_filter(struct m_ff_f64 *ff, uint64_t start_ns, uint64_t stop_ns, double *out_average);
116 
117 
118 #ifdef __cplusplus
119 }
120 
121 /*!
122  * Helper class to wrap a C filter fifo (@ref m_ff_vec3_f32).
123  */
124 class FilterFifo3F
125 {
126 private:
127  m_ff_vec3_f32 *mFifoPtr;
128 
129 
130 public:
131  FilterFifo3F() = delete;
132 
133  FilterFifo3F(size_t size)
134  {
135  m_ff_vec3_f32_alloc(&mFifoPtr, size);
136  }
137 
138  ~FilterFifo3F()
139  {
140  m_ff_vec3_f32_free(&mFifoPtr);
141  }
142 
143  /*!
144  * Get the pointer to the C filter fifo, ownership is not passed.
145  */
146  inline m_ff_vec3_f32 *
147  unsafeGetFilterFifo()
148  {
149  return mFifoPtr;
150  }
151 
152  /*!
153  * @copydoc m_ff_vec3_f32_push
154  *
155  * Wrapper for @ref m_ff_vec3_f32_push.
156  */
157  inline void
158  push(const xrt_vec3 &sample, uint64_t timestamp_ns)
159  {
160  m_ff_vec3_f32_push(mFifoPtr, &sample, timestamp_ns);
161  }
162 
163  /*!
164  * @copydoc m_ff_vec3_f32_get
165  *
166  * Wrapper for @ref m_ff_vec3_f32_get.
167  */
168  inline bool
169  get(size_t num, xrt_vec3 *out_sample, uint64_t *out_timestamp_ns)
170  {
171  return m_ff_vec3_f32_get(mFifoPtr, num, out_sample, out_timestamp_ns);
172  }
173 
174  /*!
175  * @copydoc m_ff_vec3_f32_filter
176  *
177  * Wrapper for @ref m_ff_vec3_f32_filter.
178  */
179  inline size_t
180  filter(uint64_t start_ns, uint64_t stop_ns, struct xrt_vec3 *out_average)
181  {
182  return m_ff_vec3_f32_filter(mFifoPtr, start_ns, stop_ns, out_average);
183  }
184 };
185 #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:258
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:271
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:231
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:79
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:98
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:71
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:125
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:111
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:239
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:285
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:92
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:252
Definition: m_filter_fifo.c:183
Definition: m_filter_fifo.c:23
A 3 element vector with single floats.
Definition: xrt_defines.h:271
static const cJSON * get(const cJSON *json, const char *f)
Less typing.
Definition: u_json.c:36
Common defines and enums for XRT.