OpenEV
Extending OpenCV to event-based vision
Loading...
Searching...
No Matches
vector.hpp
1
6#ifndef OPENEV_CONTAINERS_VECTOR_HPP
7#define OPENEV_CONTAINERS_VECTOR_HPP
8
9#include "openev/containers/array.hpp"
10#include "openev/containers/queue.hpp"
11#include "openev/core/types.hpp"
12#include <cstddef>
13#include <numeric>
14#include <utility>
15#include <vector>
16
17namespace ev {
19#ifndef OPENEV_ARRAY_HPP
20template <typename T, std::size_t N>
21class Array_;
22#endif
23
24#ifndef OPENEV_QUEUE_HPP
25template <typename T>
26class Queue_;
27#endif
35template <typename T>
36class Vector_ : public std::vector<T> {
37 using std::vector<T>::vector;
38
39public:
41 inline void push_back(const T &e) {
42 std::vector<T>::push_back(e);
43 }
50 template <std::size_t N>
51 inline void push_back(const Array_<T, N> &array) {
52 std::vector<T>::reserve(std::vector<T>::size() + array.size());
53 std::vector<T>::insert(std::vector<T>::end(), array.begin(), array.end());
54 }
55
61 inline void push_back(Queue_<T> &queue, const bool keep_events_in_queue = false) {
62 std::vector<T>::reserve(std::vector<T>::size() + queue.size());
63 if(keep_events_in_queue) {
64 const std::size_t size = queue.size();
65 for(std::size_t i = 0; i < size; i++) {
66 std::vector<T>::emplace_back(std::move(queue.front()));
67 queue.emplace(queue.front());
68 queue.pop();
69 }
70 } else {
71 while(!queue.empty()) {
72 std::vector<T>::emplace_back(std::move(queue.front()));
73 queue.pop();
74 }
75 }
76 }
77
82 [[nodiscard]] inline double duration() const {
83 return (std::vector<T>::back()).t - (std::vector<T>::front()).t;
84 }
85
90 [[nodiscard]] inline double rate() const {
91 return std::vector<T>::size() / duration();
92 }
93
98 [[nodiscard]] Eventd mean() const {
99 double x = 0.0;
100 double y = 0.0;
101 double t = 0.0;
102 double p = 0.0;
103
104 for(const T &e : *this) {
105 x += static_cast<double>(e.x);
106 y += static_cast<double>(e.y);
107 t += static_cast<double>(e.t);
108 p += static_cast<double>(e.p);
109 }
110
111 const std::size_t size = std::vector<T>::size();
112 return {x / size, y / size, t / size, p / size >= 0.5};
113 }
114
119 [[nodiscard]] inline double meanTime() const {
120 return std::accumulate(std::vector<T>::begin(), std::vector<T>::end(), 0.0, [](double sum, const T &e) { return sum + e.t; }) / std::vector<T>::size();
121 }
122};
123using Vectori = Vector_<Eventi>;
124using Vectorl = Vector_<Eventl>;
125using Vectorf = Vector_<Eventf>;
126using Vectord = Vector_<Eventd>;
127using Vector = Vector_<Event>;
128using AugmentedVectori = Vector_<AugmentedEventi>;
129using AugmentedVectorl = Vector_<AugmentedEventl>;
130using AugmentedVectorf = Vector_<AugmentedEventf>;
131using AugmentedVectord = Vector_<AugmentedEventd>;
132using AugmentedVector = Vector_<AugmentedEvent>;
133} // namespace ev
134
135#endif // OPENEV_CONTAINERS_VECTOR_HPP
This class extends std::array to implement event arrays. For more information, please refer here.
Definition array.hpp:21
This class extends cv::Point_<T> for event data. For more information, please refer here.
Definition types.hpp:60
This class extends std::queue to implement event queues. For more information, please refer here.
Definition queue.hpp:35
This class extends std::vector to implement event vectors. For more information, please refer here.
Definition vector.hpp:36
double rate() const
Compute event rate as the ratio between the number of events and the time difference between the last...
Definition vector.hpp:90
double duration() const
Time difference between the last and the first event in the vector.
Definition vector.hpp:82
double meanTime() const
Compute the mean time of the events in the vector.
Definition vector.hpp:119
Eventd mean() const
Compute the mean of the events in the vector.
Definition vector.hpp:98
void push_back(const Array_< T, N > &array)
Push back elements from an array of events.
Definition vector.hpp:51
void push_back(Queue_< T > &queue, const bool keep_events_in_queue=false)
Push back elements from a queue of events.
Definition vector.hpp:61
Basic event-based vision structures based on OpenCV components.