OpenEV
Extending OpenCV to event-based vision
Loading...
Searching...
No Matches
grid.hpp
Go to the documentation of this file.
1
6#ifndef OPENEV_CONTAINERS_GRID_HPP
7#define OPENEV_CONTAINERS_GRID_HPP
8
10#include <cstddef>
11#include <opencv2/core/base.hpp>
12#include <opencv2/core/types.hpp>
13#include <type_traits>
14#include <utility>
15#include <vector>
16
17namespace ev {
18[[maybe_unused]] constexpr bool USING_GRID_HPP = true;
19
21template <typename Container, typename = void>
22struct has_push_back_ : std::false_type {};
23template <typename Container>
24struct has_push_back_<Container, std::void_t<decltype(std::declval<Container &>().push_back(std::declval<const typename Container::value_type &>()))>> : std::true_type {};
25
26template <typename Container, typename = void>
27struct has_push_ : std::false_type {};
28template <typename Container>
29struct has_push_<Container, std::void_t<decltype(std::declval<Container &>().push(std::declval<const typename Container::value_type &>()))>> : std::true_type {};
30
31template <typename Container, typename = void>
32struct has_clear_ : std::false_type {};
33template <typename Container>
34struct has_clear_<Container, std::void_t<decltype(std::declval<Container &>().clear())>> : std::true_type {};
36
48template <typename Container>
49class Grid_ {
50 static_assert(has_push_back_<Container>::value || has_push_<Container>::value, "ev::Grid_: the container must offer push_back() or push().");
51
52public:
53 using EventType = typename Container::value_type;
54 using T = typename EventType::value_type;
55
62 template <typename... Args>
63 explicit Grid_(const cv::Size sensor, const cv::Size cells, Args &&...args) : sensor_{sensor}, shape_{cells}, prototype_(std::forward<Args>(args)...) {
64 if(sensor.width <= 0 || sensor.height <= 0) {
65 CV_Error(cv::Error::StsBadArg, "ev::Grid_: the sensor size must be positive.");
66 }
67 if(cells.width <= 0 || cells.height <= 0 || cells.width > sensor.width || cells.height > sensor.height) {
68 CV_Error(cv::Error::StsBadArg, "ev::Grid_: the number of cells must be positive and not exceed the sensor size.");
69 }
70 cell_ = cv::Size((sensor.width + cells.width - 1) / cells.width, (sensor.height + cells.height - 1) / cells.height);
71 cells_.assign(static_cast<std::size_t>(cells.width) * static_cast<std::size_t>(cells.height), prototype_);
72 }
73
79 inline bool insert(const EventType &e) {
80 const cv::Point c = cell(e);
81 if(c.x < 0) {
82 return false;
83 }
84 push_(cells_[index_(c)], e);
85 return true;
86 }
87
93 [[nodiscard]] inline cv::Point cell(const EventType &e) const {
94 int x = 0;
95 int y = 0;
96 if constexpr(std::is_floating_point_v<T>) {
97 x = round_(e.x);
98 y = round_(e.y);
99 } else {
100 x = static_cast<int>(e.x);
101 y = static_cast<int>(e.y);
102 }
103 if(x < 0 || y < 0 || x >= sensor_.width || y >= sensor_.height) {
104 return {-1, -1};
105 }
106 return {x / cell_.width, y / cell_.height};
107 }
108
115 [[nodiscard]] inline Container &operator()(const int row, const int col) {
116 return cells_[index_({col, row})];
117 }
118
125 [[nodiscard]] inline const Container &operator()(const int row, const int col) const {
126 return cells_[index_({col, row})];
127 }
128
134 [[nodiscard]] inline Container &operator()(const cv::Point c) {
135 return cells_[index_(c)];
136 }
137
143 [[nodiscard]] inline const Container &operator()(const cv::Point c) const {
144 return cells_[index_(c)];
145 }
146
150 inline void clear() {
151 if constexpr(has_clear_<Container>::value) {
152 for(Container &cell : cells_) {
153 cell.clear();
154 }
155 } else {
156 cells_.assign(cells_.size(), prototype_);
157 }
158 }
159
164 [[nodiscard]] inline int rows() const {
165 return shape_.height;
166 }
167
172 [[nodiscard]] inline int cols() const {
173 return shape_.width;
174 }
175
180 [[nodiscard]] inline cv::Size size() const {
181 return shape_;
182 }
183
188 [[nodiscard]] inline cv::Size cellSize() const {
189 return cell_;
190 }
191
196 [[nodiscard]] inline cv::Size sensorSize() const {
197 return sensor_;
198 }
199
201 [[nodiscard]] inline auto begin() {
202 return cells_.begin();
203 }
204
205 [[nodiscard]] inline auto end() {
206 return cells_.end();
207 }
208
209 [[nodiscard]] inline auto begin() const {
210 return cells_.begin();
211 }
212
213 [[nodiscard]] inline auto end() const {
214 return cells_.end();
215 }
217
218private:
219 cv::Size sensor_;
220 cv::Size shape_;
221 cv::Size cell_;
222 Container prototype_;
223 std::vector<Container> cells_;
224
225 [[nodiscard]] inline std::size_t index_(const cv::Point c) const {
226 return (static_cast<std::size_t>(c.y) * static_cast<std::size_t>(shape_.width)) + static_cast<std::size_t>(c.x);
227 }
228
229 static inline void push_(Container &container, const EventType &e) {
230 if constexpr(has_push_<Container>::value) {
231 container.push(e);
232 } else {
233 container.push_back(e);
234 }
235 }
236};
237} // namespace ev
238
239#endif // OPENEV_CONTAINERS_GRID_HPP
const Container & operator()(const int row, const int col) const
Access a cell.
Definition grid.hpp:125
cv::Point cell(const EventType &e) const
Cell an event falls into.
Definition grid.hpp:93
bool insert(const EventType &e)
Insert an event into the cell its coordinates fall into.
Definition grid.hpp:79
cv::Size cellSize() const
Size of each cell in pixels.
Definition grid.hpp:188
cv::Size sensorSize() const
Size of the sensor in pixels.
Definition grid.hpp:196
Container & operator()(const int row, const int col)
Access a cell.
Definition grid.hpp:115
Grid_(const cv::Size sensor, const cv::Size cells, Args &&...args)
Construct a grid of cells covering the sensor.
Definition grid.hpp:63
typename Container::value_type EventType
Definition grid.hpp:53
void clear()
Empty every cell.
Definition grid.hpp:150
const Container & operator()(const cv::Point c) const
Access a cell.
Definition grid.hpp:143
typename EventType::value_type T
Definition grid.hpp:54
cv::Size size() const
Number of cells: width columns and height rows.
Definition grid.hpp:180
int rows() const
Number of rows of the grid.
Definition grid.hpp:164
Container & operator()(const cv::Point c)
Access a cell.
Definition grid.hpp:134
int cols() const
Number of columns of the grid.
Definition grid.hpp:172
Basic event-based vision structures based on OpenCV components.