OpenEV
Extending OpenCV to event-based vision
Loading...
Searching...
No Matches
types.hpp
Go to the documentation of this file.
1
6#ifndef OPENEV_CORE_TYPES_HPP
7#define OPENEV_CORE_TYPES_HPP
8
11#include <array>
12#include <cmath>
13#include <cstdint>
14#include <iosfwd>
15#include <opencv2/core/base.hpp>
16#include <opencv2/core/matx.hpp>
17#include <opencv2/core/types.hpp>
18#include <string>
19#include <vector>
20namespace ev {
21class UndistortMap;
22}
23
24namespace ev {
25constexpr bool POSITIVE = true;
26constexpr bool NEGATIVE = false;
28enum class Stereo : char {
29 LEFT = 'L',
30 RIGHT = 'R'
31};
32
33enum DistanceTypes : uint8_t {
34 DISTANCE_NORM_INF = cv::NORM_INF,
35 DISTANCE_NORM_L1 = cv::NORM_L1,
36 DISTANCE_NORM_L2 = cv::NORM_L2,
37 DISTANCE_NORM_L2SQR = cv::NORM_L2SQR,
38 DISTANCE_NORM_MANHATTAN = DISTANCE_NORM_L1,
39 DISTANCE_NORM_EUCLIDEAN = DISTANCE_NORM_L2,
40 DISTANCE_FLAG_SPATIAL = 0b00010000,
41 DISTANCE_FLAG_TEMPORAL = 0b00100000,
42 DISTANCE_FLAG_SPATIOTEMPORAL = 0b01000000,
43 DISTANCE_FLAG_3D = DISTANCE_FLAG_SPATIOTEMPORAL,
44 DISTANCE_FLAG_2D = DISTANCE_FLAG_SPATIAL,
45};
46
59template <typename T>
60class Event_ : public cv::Point_<T> {
61public:
62 double t;
63 bool p;
68 Event_() : cv::Point_<T>(), t{0}, p{POSITIVE} {};
69
71 ~Event_() = default;
77 Event_(const Event_<T> &) = default;
78
82 Event_(Event_<T> &&) noexcept = default;
83
89 Event_(const T x, const T y) : cv::Point_<T>(x, y), t{0}, p{POSITIVE} {};
90
95 explicit Event_(const cv::Point_<T> &pt) : cv::Point_<T>(pt), t{0}, p{POSITIVE} {};
96
104 Event_(const T x, const T y, const double t, const bool p) : cv::Point_<T>(x, y), t{t}, p{p} {};
105
112 Event_(const cv::Point_<T> &pt, const double t, const bool p) : cv::Point_<T>(pt), t{t}, p{p} {};
113
117 Event_<T> &operator=(const Event_<T> &) = default;
118
122 Event_<T> &operator=(const cv::Point_<T> &p) {
123 Event_<T>::x = p.x;
124 Event_<T>::y = p.y;
125 return *this;
126 }
127
131 Event_<T> &operator=(Event_<T> &&) noexcept = default;
132
136 [[nodiscard]] inline bool operator==(const Event_<T> &e) const {
137 return (Event_<T>::x == e.x) && (Event_<T>::y == e.y) && (t == e.t) && (p == e.p);
138 }
139
143 [[nodiscard]] inline bool operator==(const cv::Point_<T> &pt) const {
144 return (Event_<T>::x == pt.x) && (Event_<T>::y == pt.y);
145 }
146
150 [[nodiscard]] inline bool operator==(const cv::Point3_<T> &pt) const {
151 return (Event_<T>::x == pt.x) && (Event_<T>::y == pt.y) && (Event_<T>::t == pt.z);
152 }
153
157 [[nodiscard]] inline bool operator<(const Event_<T> &e) const {
158 return Event_<T>::t < e.t;
159 }
160
164 template <typename U>
165 [[nodiscard]] inline operator cv::Point_<U>() const {
166 return {static_cast<U>(Event_<T>::x), static_cast<U>(Event_<T>::y)};
167 }
168
172 template <typename U>
173 [[nodiscard]] inline operator cv::Point3_<U>() const {
174 return {static_cast<U>(Event_<T>::x), static_cast<U>(Event_<T>::y), static_cast<U>(Event_<T>::t)};
175 }
176
184 [[nodiscard]] inline double distance(const Event_<T> &e, const uint8_t type = DISTANCE_NORM_L2 | DISTANCE_FLAG_SPATIAL) const {
185 if(static_cast<bool>(type & DISTANCE_FLAG_SPATIOTEMPORAL)) {
186 return cv::norm(cv::Matx<T, 3, 1>(Event_<T>::x - e.x, Event_<T>::y - e.y, Event_<T>::t - e.t), type & 0x0F);
187 }
188 if(static_cast<bool>(type & DISTANCE_FLAG_SPATIAL) || !static_cast<bool>(type & 0xF0)) {
189 return cv::norm(cv::Matx<T, 2, 1>(Event_<T>::x - e.x, Event_<T>::y - e.y), type & 0x0F);
190 }
191 if(static_cast<bool>(type & DISTANCE_FLAG_TEMPORAL)) {
192 return Event_<T>::t - e.t;
193 }
194 logger::error("Bad distance option");
195 return 0.0;
196 }
197
204 inline bool undistort(const UndistortMap &map) {
205 return map(*this);
206 }
207
212 std::array<double, 4> bilinearVoting() const {
213 const double dx = this->x - static_cast<int>(this->x);
214 const double dy = this->y - static_cast<int>(this->y);
215 if(dx == 0 && dy == 0) {
216 return {1, 0, 0, 0};
217 }
218 if(dx == 0) {
219 return {(1 - dy), 0, dy, 0};
220 }
221 if(dy == 0) {
222 return {(1 - dx), dx, 0, 0};
223 }
224 return {(1 - dx) * (1 - dy), dx * (1 - dy), (1 - dx) * dy, dx * dy};
225 }
226
233 [[nodiscard]] friend std::ostream &operator<<(std::ostream &os, const Event_<T> &e) {
234 os << std::string("(" + std::to_string(e.x) + "," + std::to_string(e.y) + ") " + std::to_string(e.t) + (e.p ? " [+]" : " [-]"));
235 return os;
236 }
237};
242using Event = Eventi;
256template <typename T>
257class AugmentedEvent_ : public Event_<T> {
258 using Event_<T>::Event_;
259
260public:
261 double weight{1};
262 double depth{0};
263 Stereo stereo{Stereo::LEFT};
269 std::vector<AugmentedEvent_<int>> bilinearVoting() const {
270 const double dx = this->x - static_cast<int>(this->x);
271 const double dy = this->y - static_cast<int>(this->y);
272 if(dx == 0 && dy == 0) {
273 return {static_cast<AugmentedEvent_<int>>(*this)};
274 }
275 if(dx == 0) {
276 AugmentedEvent_<int> e1(static_cast<int>(this->x), static_cast<int>(this->y), this->t, this->p);
277 AugmentedEvent_<int> e3(static_cast<int>(this->x), 1 + static_cast<int>(this->y), this->t, this->p);
278 e1.weight = (1 - dy);
279 e3.weight = dy;
280 return {e1, e3};
281 }
282 if(dy == 0) {
283 AugmentedEvent_<int> e1(static_cast<int>(this->x), static_cast<int>(this->y), this->t, this->p);
284 AugmentedEvent_<int> e2(1 + static_cast<int>(this->x), static_cast<int>(this->y), this->t, this->p);
285 e1.weight = (1 - dx);
286 e2.weight = dx;
287 return {e1, e2};
288 }
289 AugmentedEvent_<int> e1(static_cast<int>(this->x), static_cast<int>(this->y), this->t, this->p);
290 AugmentedEvent_<int> e2(1 + static_cast<int>(this->x), static_cast<int>(this->y), this->t, this->p);
291 AugmentedEvent_<int> e3(static_cast<int>(this->x), 1 + static_cast<int>(this->y), this->t, this->p);
292 AugmentedEvent_<int> e4(1 + static_cast<int>(this->x), 1 + static_cast<int>(this->y), this->t, this->p);
293 e1.weight = (1 - dx) * (1 - dy);
294 e2.weight = dx * (1 - dy);
295 e3.weight = (1 - dx) * dy;
296 e4.weight = dx * dy;
297 return {e1, e2, e3, e4};
298 }
299
306 [[nodiscard]] friend std::ostream &operator<<(std::ostream &os, const AugmentedEvent_<T> &e) {
307 os << std::string("(" + std::to_string(e.x) + "," + std::to_string(e.y) + ") " + std::to_string(e.t) + (e.p ? " [+]" : " [-]"));
308 os << " w=" + std::to_string(e.weight);
309 os << " d=" + std::to_string(e.depth);
310 os << " s=" + (e.stereo == Stereo::LEFT ? std::string("LEFT") : std::string("RIGHT"));
311 return os;
312 }
313};
333template <typename T>
334using Size2_ = cv::Size_<T>;
339using Size2 = Size2i;
340using Size = Size2;
354template <typename T>
355class Size3_ : public cv::Size_<T> {
356public:
362 Size3_() : cv::Size_<T>(), length{0} {};
363
370 Size3_(T w, T h, T l) : cv::Size_<T>(w, h), length{l} {};
371
376 [[nodiscard]] inline bool empty() const {
377 return !(cv::Size_<T>::width || cv::Size_<T>::height || length);
378 }
379
385 [[nodiscard]] inline T volume() const {
386 return cv::Size_<T>::width * cv::Size_<T>::height * length;
387 }
388};
393using Size3 = Size3i;
408template <typename T>
409using Rect2_ = cv::Rect_<T>;
414using Rect2 = Rect2i;
415using Rect = Rect2;
429template <typename T>
430class Rect3_ : public cv::Rect_<T> {
431public:
432 T t;
438 Rect3_() : cv::Rect_<T>(), t{0}, length{0} {};
439
449 Rect3_(const T x, const T y, const T t, const T w, const T h, const T l) : cv::Rect_<T>(x, y, w, h), t{t}, length{l} {};
450
457 Rect3_(const Rect2_<T> &rect, const T t, const T l) : cv::Rect_<T>(rect), t{t}, length{l} {};
458
464 Rect3_(const cv::Point3_<T> &pt, const Size3_<T> sz) : cv::Rect_<T>(cv::Point_<T>(pt.x, pt.y), cv::Size_<T>(sz.width, sz.height)), t{pt.z}, length{sz.length} {};
465
471 Rect3_(const cv::Point3_<T> &pt1, const cv::Point3_<T> &pt2) : cv::Rect_<T>(cv::Point_<T>(pt1.x, pt1.y), cv::Size_<T>(pt2.x - pt1.x, pt2.y - pt1.y)), t{pt1.z}, length{pt2.z - pt1.z} {};
472
477 [[nodiscard]] inline bool empty() const {
479 }
480
487 template <typename Te>
488 [[nodiscard]] inline bool contains(const Event_<Te> &e) const {
489 return cv::Rect_<T>::contains(e) && e.t >= t && e.t < t + length;
490 }
491
496 [[nodiscard]] inline Size3_<T> size() const {
498 }
499
505 [[nodiscard]] inline T volume() const {
507 }
508};
513using Rect3 = Rect3i;
527template <typename T>
528struct Circ_ {
529 cv::Point_<T> center;
530 T radius;
531
535 Circ_() : center{0, 0}, radius{0} {};
536
542 Circ_(const cv::Point_<T> center, const T radius) : center{center}, radius{radius} {}
543
548 [[nodiscard]] inline bool empty() const {
549 return !radius;
550 }
551
558 template <typename Te>
559 [[nodiscard]] inline bool contains(const Event_<Te> &e) const {
560 return pow(center.x - e.x, 2) + pow(center.y - e.y, 2) <= radius * radius;
561 }
562
563 [[nodiscard]] inline cv::Size size() const {
564 return {radius, radius};
565 }
566
567 [[nodiscard]] inline double area() const {
568 return M_PI * radius * radius;
569 }
570};
575using Circ = Circi;
577} // namespace ev
578
579#endif // OPENEV_CORE_TYPES_HPP
This class extends Event to include: weigth, depth, and stereo.
Definition types.hpp:257
std::vector< AugmentedEvent_< int > > bilinearVoting() const
Bilinear voting.
Definition types.hpp:269
Stereo stereo
Definition types.hpp:263
double depth
Definition types.hpp:262
friend std::ostream & operator<<(std::ostream &os, const AugmentedEvent_< T > &e)
Overload of << operator.
Definition types.hpp:306
double weight
Definition types.hpp:261
This class extends cv::Point_<T> for event data. For more information, please refer here.
Definition types.hpp:60
bool operator<(const Event_< T > &e) const
Definition types.hpp:157
Event_(const cv::Point_< T > &pt, const double t, const bool p)
Definition types.hpp:112
Event_< T > & operator=(const cv::Point_< T > &p)
Definition types.hpp:122
bool p
Definition types.hpp:63
bool operator==(const cv::Point3_< T > &pt) const
Definition types.hpp:150
bool undistort(const UndistortMap &map)
Definition types.hpp:204
Event_< T > & operator=(Event_< T > &&) noexcept=default
std::array< double, 4 > bilinearVoting() const
Bilinear voting.
Definition types.hpp:212
bool operator==(const cv::Point_< T > &pt) const
Definition types.hpp:143
Event_()
Definition types.hpp:68
Event_(const T x, const T y, const double t, const bool p)
Definition types.hpp:104
Event_< T > & operator=(const Event_< T > &)=default
Event_(Event_< T > &&) noexcept=default
double distance(const Event_< T > &e, const uint8_t type=DISTANCE_NORM_L2|DISTANCE_FLAG_SPATIAL) const
Definition types.hpp:184
Event_(const Event_< T > &)=default
Event_(const cv::Point_< T > &pt)
Definition types.hpp:95
friend std::ostream & operator<<(std::ostream &os, const Event_< T > &e)
Overload of << operator.
Definition types.hpp:233
double t
Definition types.hpp:62
This class extends cv::Rect_<T> for event data. For more information, please refer here.
Definition types.hpp:430
Rect3_(const T x, const T y, const T t, const T w, const T h, const T l)
Definition types.hpp:449
Rect3_(const Rect2_< T > &rect, const T t, const T l)
Definition types.hpp:457
Size3_< T > size() const
Size of the rectangular cuboid.
Definition types.hpp:496
T volume() const
Compute the volume.
Definition types.hpp:505
bool empty() const
Check if empty.
Definition types.hpp:477
Rect3_(const cv::Point3_< T > &pt1, const cv::Point3_< T > &pt2)
Definition types.hpp:471
Rect3_(const cv::Point3_< T > &pt, const Size3_< T > sz)
Definition types.hpp:464
T t
Definition types.hpp:432
bool contains(const Event_< Te > &e) const
Check if the rectangular cuboid contains an event.
Definition types.hpp:488
Rect3_()
Definition types.hpp:438
T length
Definition types.hpp:433
This class extends cv::Size_<T> for event data. For more information, please refer here.
Definition types.hpp:355
bool empty() const
Check if empty.
Definition types.hpp:376
T volume() const
Compute the volume.
Definition types.hpp:385
Size3_(T w, T h, T l)
Definition types.hpp:370
T length
Definition types.hpp:357
Size3_()
Definition types.hpp:362
Definition undistortion.hpp:22
Logger utility.
This class defines a circle given its center and radius.
Definition types.hpp:528
Circ_()
Definition types.hpp:535
Circ_(const cv::Point_< T > center, const T radius)
Definition types.hpp:542
bool contains(const Event_< Te > &e) const
Check if the circle contains an event.
Definition types.hpp:559
bool empty() const
Check if empty.
Definition types.hpp:548
Rect2_< double > Rect2d
Definition types.hpp:413
constexpr bool POSITIVE
Definition types.hpp:25
Size2_< double > Size2d
Definition types.hpp:338
Size2i Size2
Definition types.hpp:339
Rect2i Rect2
Definition types.hpp:414
Rect2 Rect
Definition types.hpp:415
Size2_< int > Size2i
Definition types.hpp:335
cv::Size_< T > Size2_
This class extends cv::Size_<T> for event data. For more information, please refer here.
Definition types.hpp:334
Size2 Size
Definition types.hpp:340
cv::Rect_< T > Rect2_
This class extends cv::Rect_<T> for event data. For more information, please refer here.
Definition types.hpp:409
Rect2_< long > Rect2l
Definition types.hpp:411
Rect2_< int > Rect2i
Definition types.hpp:410
constexpr bool NEGATIVE
Definition types.hpp:26
Rect2_< float > Rect2f
Definition types.hpp:412
Size2_< long > Size2l
Definition types.hpp:336
Size2_< float > Size2f
Definition types.hpp:337
Undistortion utilities.