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 allows you to 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"
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 
19 struct m_ff_vec3_f32;
20 
21 /*!
22  * Allocates a filter fifo tracking @p num samples and fills it with @p num
23  * samples at timepoint zero.
24  */
25 void
26 m_ff_vec3_f32_alloc(struct m_ff_vec3_f32 **ff_out, size_t num);
27 
28 /*!
29  * Frees the given filter fifo and all it's samples.
30  */
31 void
32 m_ff_vec3_f32_free(struct m_ff_vec3_f32 **ff_ptr);
33 
34 /*!
35  * Pushes a sample at the given timepoint, pushing samples out of order yields
36  * unspecified behaviour, so samples must be pushed in time order.
37  */
38 void
40  const struct xrt_vec3 *sample,
41  uint64_t timestamp_ns);
42 
43 /*!
44  * Return the sample at the index, zero means the last sample push, one second
45  * last and so on.
46  */
47 void
49  size_t num,
50  struct xrt_vec3 *out_sample,
51  uint64_t *out_timestamp_ns);
52 
53 /*!
54  * Averages all samples in the fifo between the two timepoints, returns number
55  * of samples sampled, if no samples was found between the timpoints returns 0
56  * and sets @p out_average to all zeros.
57  *
58  * @param ff Filter fifo to search in.
59  * @param start_ns Timepoint furthest in the past, to start searching for
60  * samples.
61  * @param stop_ns Timepoint closest in the past, or now, to stop searching
62  * for samples.
63  * @param out_average Average of all samples in the given timeframe.
64  */
65 size_t
67  uint64_t start_ns,
68  uint64_t stop_ns,
69  struct xrt_vec3 *out_average);
70 
71 
72 #ifdef __cplusplus
73 }
74 
75 /*!
76  * Helper class to wrap a C filter fifo.
77  */
78 class FilterFifo3F
79 {
80 private:
81  struct m_ff_vec3_f32 *ff;
82 
83 
84 public:
85  FilterFifo3F() = delete;
86 
87  FilterFifo3F(size_t size)
88  {
89  m_ff_vec3_f32_alloc(&ff, size);
90  }
91 
92  ~FilterFifo3F()
93  {
94  m_ff_vec3_f32_free(&ff);
95  }
96 
97  inline void
98  push(const xrt_vec3 &sample, uint64_t timestamp_ns)
99  {
100  m_ff_vec3_f32_push(ff, &sample, timestamp_ns);
101  }
102 
103  inline void
104  get(size_t num, xrt_vec3 *out_sample, uint64_t *out_timestamp_ns)
105  {
106  m_ff_vec3_f32_get(ff, num, out_sample, out_timestamp_ns);
107  }
108 
109  inline size_t
110  filter(uint64_t start_ns,
111  uint64_t stop_ns,
112  struct xrt_vec3 *out_average)
113  {
114  return m_ff_vec3_f32_filter(ff, start_ns, stop_ns, out_average);
115  }
116 };
117 #endif
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:112
void push(struct oxr_instance *inst, struct oxr_event *event)
Definition: oxr_event.c:62
A 3 element vector with single floats.
Definition: xrt_defines.h:133
void m_ff_vec3_f32_free(struct m_ff_vec3_f32 **ff_ptr)
Frees the given filter fifo and all it&#39;s samples.
Definition: m_filter_fifo.c:73
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:86
size_t num
Definition: m_filter_fifo.c:18
Definition: m_filter_fifo.c:16
Common defines and enums for XRT.
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:65
struct vive_imu_sample sample[3]
Definition: vive_protocol.h:210
void 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:101