···11+// Copyright 2020, Collabora, Ltd.
22+// SPDX-License-Identifier: BSL-1.0
33+/*!
44+ * @file
55+ * @brief A fifo that also allows you to dynamically filter.
66+ * @author Jakob Bornecrantz <jakob@collabora.com>
77+ * @ingroup aux_math
88+ */
99+1010+#pragma once
1111+1212+#include "xrt/xrt_defines.h"
1313+1414+#ifdef __cplusplus
1515+extern "C" {
1616+#endif
1717+1818+1919+struct m_ff_vec3_f32;
2020+2121+/*!
2222+ * Allocates a filter fifo tracking @p num samples and fills it with @p num
2323+ * samples at timepoint zero.
2424+ */
2525+void
2626+m_ff_vec3_f32_alloc(struct m_ff_vec3_f32 **ff_out, size_t num);
2727+2828+/*!
2929+ * Frees the given filter fifo and all it's samples.
3030+ */
3131+void
3232+m_ff_vec3_f32_free(struct m_ff_vec3_f32 **ff_ptr);
3333+3434+/*!
3535+ * Pushes a sample at the given timepoint, pushing samples out of order yields
3636+ * unspecified behaviour, so samples must be pushed in time order.
3737+ */
3838+void
3939+m_ff_vec3_f32_push(struct m_ff_vec3_f32 *ff,
4040+ const struct xrt_vec3 *sample,
4141+ uint64_t timestamp_ns);
4242+4343+/*!
4444+ * Return the sample at the index, zero means the last sample push, one second
4545+ * last and so on.
4646+ */
4747+void
4848+m_ff_vec3_f32_get(struct m_ff_vec3_f32 *ff,
4949+ size_t num,
5050+ struct xrt_vec3 *out_sample,
5151+ uint64_t *out_timestamp_ns);
5252+5353+/*!
5454+ * Averages all samples in the fifo between the two timepoints, returns number
5555+ * of samples sampled, if no samples was found between the timpoints returns 0
5656+ * and sets @p out_average to all zeros.
5757+ *
5858+ * @param ff Filter fifo to search in.
5959+ * @param start_ns Timepoint furthest in the past, to start searching for
6060+ * samples.
6161+ * @param stop_ns Timepoint closest in the past, or now, to stop searching
6262+ * for samples.
6363+ * @param out_average Average of all samples in the given timeframe.
6464+ */
6565+size_t
6666+m_ff_vec3_f32_filter(struct m_ff_vec3_f32 *ff,
6767+ uint64_t start_ns,
6868+ uint64_t stop_ns,
6969+ struct xrt_vec3 *out_average);
7070+7171+7272+#ifdef __cplusplus
7373+}
7474+7575+/*!
7676+ * Helper class to wrap a C filter fifo.
7777+ */
7878+class FilterFifo3F
7979+{
8080+private:
8181+ struct m_ff_vec3_f32 *ff;
8282+8383+8484+public:
8585+ FilterFifo3F() = delete;
8686+8787+ FilterFifo3F(size_t size)
8888+ {
8989+ m_ff_vec3_f32_alloc(&ff, size);
9090+ }
9191+9292+ ~FilterFifo3F()
9393+ {
9494+ m_ff_vec3_f32_free(&ff);
9595+ }
9696+9797+ inline void
9898+ push(const xrt_vec3 &sample, uint64_t timestamp_ns)
9999+ {
100100+ m_ff_vec3_f32_push(ff, &sample, timestamp_ns);
101101+ }
102102+103103+ inline void
104104+ get(size_t num, xrt_vec3 *out_sample, uint64_t *out_timestamp_ns)
105105+ {
106106+ m_ff_vec3_f32_get(ff, num, out_sample, out_timestamp_ns);
107107+ }
108108+109109+ inline size_t
110110+ filter(uint64_t start_ns,
111111+ uint64_t stop_ns,
112112+ struct xrt_vec3 *out_average)
113113+ {
114114+ return m_ff_vec3_f32_filter(ff, start_ns, stop_ns, out_average);
115115+ }
116116+};
117117+#endif