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 <ostream>
19#include <string>
20#include <vector>
21
22namespace ev {
23class UndistortMap;
24}
25
26namespace ev {
27constexpr bool POSITIVE = true;
28constexpr bool NEGATIVE = false;
29
30enum class Stereo : char {
31 LEFT = 'L',
32 RIGHT = 'R'
33};
34
35enum DistanceTypes : uint8_t {
36 DISTANCE_NORM_INF = cv::NORM_INF,
37 DISTANCE_NORM_L1 = cv::NORM_L1,
38 DISTANCE_NORM_L2 = cv::NORM_L2,
39 DISTANCE_NORM_L2SQR = cv::NORM_L2SQR,
40 DISTANCE_NORM_MANHATTAN = DISTANCE_NORM_L1,
41 DISTANCE_NORM_EUCLIDEAN = DISTANCE_NORM_L2,
42 DISTANCE_FLAG_SPATIAL = 0b00010000,
43 DISTANCE_FLAG_TEMPORAL = 0b00100000,
44 DISTANCE_FLAG_SPATIOTEMPORAL = 0b01000000,
45 DISTANCE_FLAG_3D = DISTANCE_FLAG_SPATIOTEMPORAL,
46 DISTANCE_FLAG_2D = DISTANCE_FLAG_SPATIAL,
47};
48
61template <typename T>
62class Event_ : public cv::Point_<T> {
63public:
64 double t;
65 bool p;
66
70 Event_() : cv::Point_<T>(), t{0}, p{POSITIVE} {};
71
73 ~Event_() = default;
75
79 Event_(const Event_<T> &) = default;
80
84 Event_(Event_<T> &&) noexcept = default;
85
91 Event_(const T x, const T y) : cv::Point_<T>(x, y), t{0}, p{POSITIVE} {};
92
97 explicit Event_(const cv::Point_<T> &pt) : cv::Point_<T>(pt), t{0}, p{POSITIVE} {};
98
106 Event_(const T x, const T y, const double t, const bool p) : cv::Point_<T>(x, y), t{t}, p{p} {};
107
114 Event_(const cv::Point_<T> &pt, const double t, const bool p) : cv::Point_<T>(pt), t{t}, p{p} {};
115
119 Event_<T> &operator=(const Event_<T> &) = default;
120
124 Event_<T> &operator=(const cv::Point_<T> &p) {
125 Event_<T>::x = p.x;
126 Event_<T>::y = p.y;
127 return *this;
128 }
129
133 Event_<T> &operator=(Event_<T> &&) noexcept = default;
134
138 [[nodiscard]] inline bool operator==(const Event_<T> &e) const {
139 return (Event_<T>::x == e.x) && (Event_<T>::y == e.y) && (t == e.t) && (p == e.p);
140 }
141
145 [[nodiscard]] inline bool operator==(const cv::Point_<T> &pt) const {
146 return (Event_<T>::x == pt.x) && (Event_<T>::y == pt.y);
147 }
148
152 [[nodiscard]] inline bool operator==(const cv::Point3_<T> &pt) const {
153 return (Event_<T>::x == pt.x) && (Event_<T>::y == pt.y) && (Event_<T>::t == pt.z);
154 }
155
159 [[nodiscard]] inline bool operator<(const Event_<T> &e) const {
160 return Event_<T>::t < e.t;
161 }
162
166 template <typename U>
167 [[nodiscard]] inline operator cv::Point_<U>() const {
168 return {static_cast<U>(Event_<T>::x), static_cast<U>(Event_<T>::y)};
169 }
170
174 template <typename U>
175 [[nodiscard]] inline operator cv::Point3_<U>() const {
176 return {static_cast<U>(Event_<T>::x), static_cast<U>(Event_<T>::y), static_cast<U>(Event_<T>::t)};
177 }
178
186 [[nodiscard]] inline double distance(const Event_<T> &e, const uint8_t type = DISTANCE_NORM_L2 | DISTANCE_FLAG_SPATIAL) const {
187 if(static_cast<bool>(type & DISTANCE_FLAG_SPATIOTEMPORAL)) {
188 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);
189 }
190 if(static_cast<bool>(type & DISTANCE_FLAG_SPATIAL) || !static_cast<bool>(type & 0xF0)) {
191 return cv::norm(cv::Matx<T, 2, 1>(Event_<T>::x - e.x, Event_<T>::y - e.y), type & 0x0F);
192 }
193 if(static_cast<bool>(type & DISTANCE_FLAG_TEMPORAL)) {
194 return Event_<T>::t - e.t;
195 }
196 logger::error("Bad distance option");
197 return 0.0;
198 }
199
206 inline bool undistort(const UndistortMap &map) {
207 return map(*this);
208 }
209
214 std::array<double, 4> bilinearVoting() const {
215 const double dx = this->x - static_cast<int>(this->x);
216 const double dy = this->y - static_cast<int>(this->y);
217 if(dx == 0 && dy == 0) {
218 return {1, 0, 0, 0};
219 }
220 if(dx == 0) {
221 return {(1 - dy), 0, dy, 0};
222 }
223 if(dy == 0) {
224 return {(1 - dx), dx, 0, 0};
225 }
226 return {(1 - dx) * (1 - dy), dx * (1 - dy), (1 - dx) * dy, dx * dy};
227 }
228
235 [[nodiscard]] friend std::ostream &operator<<(std::ostream &os, const Event_<T> &e) {
236 os << std::string("(" + std::to_string(e.x) + "," + std::to_string(e.y) + ") " + std::to_string(e.t) + (e.p ? " [+]" : " [-]"));
237 return os;
238 }
239};
244using Event = Eventi;
245
258template <typename T>
259class AugmentedEvent_ : public Event_<T> {
260 using Event_<T>::Event_;
261
262public:
263 double weight{1};
264 double depth{0};
265 Stereo stereo{Stereo::LEFT};
266
271 std::vector<AugmentedEvent_<int>> bilinearVoting() const {
272 const double dx = this->x - static_cast<int>(this->x);
273 const double dy = this->y - static_cast<int>(this->y);
274 if(dx == 0 && dy == 0) {
275 return {static_cast<AugmentedEvent_<int>>(*this)};
276 }
277 if(dx == 0) {
278 AugmentedEvent_<int> e1(static_cast<int>(this->x), static_cast<int>(this->y), this->t, this->p);
279 AugmentedEvent_<int> e3(static_cast<int>(this->x), 1 + static_cast<int>(this->y), this->t, this->p);
280 e1.weight = (1 - dy);
281 e3.weight = dy;
282 return {e1, e3};
283 }
284 if(dy == 0) {
285 AugmentedEvent_<int> e1(static_cast<int>(this->x), static_cast<int>(this->y), this->t, this->p);
286 AugmentedEvent_<int> e2(1 + static_cast<int>(this->x), static_cast<int>(this->y), this->t, this->p);
287 e1.weight = (1 - dx);
288 e2.weight = dx;
289 return {e1, e2};
290 }
291 AugmentedEvent_<int> e1(static_cast<int>(this->x), static_cast<int>(this->y), this->t, this->p);
292 AugmentedEvent_<int> e2(1 + static_cast<int>(this->x), static_cast<int>(this->y), this->t, this->p);
293 AugmentedEvent_<int> e3(static_cast<int>(this->x), 1 + static_cast<int>(this->y), this->t, this->p);
294 AugmentedEvent_<int> e4(1 + static_cast<int>(this->x), 1 + static_cast<int>(this->y), this->t, this->p);
295 e1.weight = (1 - dx) * (1 - dy);
296 e2.weight = dx * (1 - dy);
297 e3.weight = (1 - dx) * dy;
298 e4.weight = dx * dy;
299 return {e1, e2, e3, e4};
300 }
301
308 [[nodiscard]] friend std::ostream &operator<<(std::ostream &os, const AugmentedEvent_<T> &e) {
309 os << std::string("(" + std::to_string(e.x) + "," + std::to_string(e.y) + ") " + std::to_string(e.t) + (e.p ? " [+]" : " [-]"));
310 os << " w=" + std::to_string(e.weight);
311 os << " d=" + std::to_string(e.depth);
312 os << " s=" + (e.stereo == Stereo::LEFT ? std::string("LEFT") : std::string("RIGHT"));
313 return os;
314 }
315};
321
335template <typename T>
336using Size2_ = cv::Size_<T>;
341using Size2 = Size2i;
342using Size = Size2;
343
356template <typename T>
357class Size3_ : public cv::Size_<T> {
358public:
360
364 Size3_() : cv::Size_<T>(), length{0} {};
365
372 Size3_(T w, T h, T l) : cv::Size_<T>(w, h), length{l} {};
373
378 [[nodiscard]] inline bool empty() const {
379 return cv::Size_<T>::width <= 0 || cv::Size_<T>::height <= 0 || length <= 0;
380 }
381
387 [[nodiscard]] inline T volume() const {
388 return cv::Size_<T>::width * cv::Size_<T>::height * length;
389 }
390};
395using Size3 = Size3i;
396
410template <typename T>
411using Rect2_ = cv::Rect_<T>;
416using Rect2 = Rect2i;
417using Rect = Rect2;
418
431template <typename T>
432class Rect3_ : public cv::Rect_<T> {
433public:
434 T t;
436
440 Rect3_() : cv::Rect_<T>(), t{0}, length{0} {};
441
451 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} {};
452
459 Rect3_(const Rect2_<T> &rect, const T t, const T l) : cv::Rect_<T>(rect), t{t}, length{l} {};
460
466 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} {};
467
473 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} {};
474
479 [[nodiscard]] inline bool empty() const {
480 return Rect3_<T>::width <= 0 || Rect3_<T>::height <= 0 || length <= 0;
481 }
482
489 template <typename Te>
490 [[nodiscard]] inline bool contains(const Event_<Te> &e) const {
491 return cv::Rect_<T>::contains(e) && e.t >= t && e.t < t + length;
492 }
493
498 [[nodiscard]] inline Size3_<T> size() const {
500 }
501
507 [[nodiscard]] inline T volume() const {
509 }
510};
515using Rect3 = Rect3i;
516
529template <typename T>
530struct Circ_ {
531 cv::Point_<T> center;
532 T radius;
533
537 Circ_() : center{0, 0}, radius{0} {};
538
544 Circ_(const cv::Point_<T> center, const T radius) : center{center}, radius{radius} {}
545
550 [[nodiscard]] inline bool empty() const {
551 return radius <= 0;
552 }
553
560 template <typename Te>
561 [[nodiscard]] inline bool contains(const Event_<Te> &e) const {
562 return pow(center.x - e.x, 2) + pow(center.y - e.y, 2) <= radius * radius;
563 }
564
565 [[nodiscard]] inline cv::Size size() const {
566 return {radius, radius};
567 }
568
569 [[nodiscard]] inline double area() const {
570 return M_PI * radius * radius;
571 }
572};
577using Circ = Circi;
578
579} // namespace ev
580
581#endif // OPENEV_CORE_TYPES_HPP
This class extends Event to include: weigth, depth, and stereo.
Definition types.hpp:259
std::vector< AugmentedEvent_< int > > bilinearVoting() const
Bilinear voting.
Definition types.hpp:271
Stereo stereo
Definition types.hpp:265
double depth
Definition types.hpp:264
friend std::ostream & operator<<(std::ostream &os, const AugmentedEvent_< T > &e)
Overload of << operator.
Definition types.hpp:308
double weight
Definition types.hpp:263
This class extends cv::Point_<T> for event data. For more information, please refer here.
Definition types.hpp:62
bool operator<(const Event_< T > &e) const
Definition types.hpp:159
Event_(const cv::Point_< T > &pt, const double t, const bool p)
Definition types.hpp:114
Event_< T > & operator=(const cv::Point_< T > &p)
Definition types.hpp:124
bool p
Definition types.hpp:65
bool operator==(const cv::Point3_< T > &pt) const
Definition types.hpp:152
bool undistort(const UndistortMap &map)
Definition types.hpp:206
Event_< T > & operator=(Event_< T > &&) noexcept=default
std::array< double, 4 > bilinearVoting() const
Bilinear voting.
Definition types.hpp:214
bool operator==(const cv::Point_< T > &pt) const
Definition types.hpp:145
Event_()
Definition types.hpp:70
Event_(const T x, const T y, const double t, const bool p)
Definition types.hpp:106
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:186
Event_(const Event_< T > &)=default
Event_(const cv::Point_< T > &pt)
Definition types.hpp:97
friend std::ostream & operator<<(std::ostream &os, const Event_< T > &e)
Overload of << operator.
Definition types.hpp:235
double t
Definition types.hpp:64
This class extends cv::Rect_<T> for event data. For more information, please refer here.
Definition types.hpp:432
Rect3_(const T x, const T y, const T t, const T w, const T h, const T l)
Definition types.hpp:451
Rect3_(const Rect2_< T > &rect, const T t, const T l)
Definition types.hpp:459
Size3_< T > size() const
Size of the rectangular cuboid.
Definition types.hpp:498
T volume() const
Compute the volume.
Definition types.hpp:507
bool empty() const
Check if empty.
Definition types.hpp:479
Rect3_(const cv::Point3_< T > &pt1, const cv::Point3_< T > &pt2)
Definition types.hpp:473
Rect3_(const cv::Point3_< T > &pt, const Size3_< T > sz)
Definition types.hpp:466
int t
Definition types.hpp:434
bool contains(const Event_< Te > &e) const
Check if the rectangular cuboid contains an event.
Definition types.hpp:490
Rect3_()
Definition types.hpp:440
int length
Definition types.hpp:435
This class extends cv::Size_<T> for event data. For more information, please refer here.
Definition types.hpp:357
bool empty() const
Check if empty.
Definition types.hpp:378
T volume() const
Compute the volume.
Definition types.hpp:387
Size3_(T w, T h, T l)
Definition types.hpp:372
int length
Definition types.hpp:359
Size3_()
Definition types.hpp:364
Definition undistortion.hpp:25
Logger utility.
void error(const char *message, const bool assert_condition=false)
Log message at error level.
Definition logger.hpp:39
This class defines a circle given its center and radius.
Definition types.hpp:530
Circ_()
Definition types.hpp:537
Circ_(const cv::Point_< T > center, const T radius)
Definition types.hpp:544
bool contains(const Event_< Te > &e) const
Check if the circle contains an event.
Definition types.hpp:561
bool empty() const
Check if empty.
Definition types.hpp:550
Event_< long > Eventl
Definition types.hpp:241
Rect2_< double > Rect2d
Definition types.hpp:415
Event_< int > Eventi
Definition types.hpp:240
constexpr bool POSITIVE
Definition types.hpp:27
Size2_< double > Size2d
Definition types.hpp:340
Size2i Size2
Definition types.hpp:341
Rect2i Rect2
Definition types.hpp:416
Rect2 Rect
Definition types.hpp:417
Event_< double > Eventd
Definition types.hpp:243
Size2_< int > Size2i
Definition types.hpp:337
Circ_< long > Circl
Definition types.hpp:574
Rect3_< int > Rect3i
Definition types.hpp:511
Size3i Size3
Definition types.hpp:395
Circ_< double > Circd
Definition types.hpp:576
Rect3i Rect3
Definition types.hpp:515
Circ_< int > Circi
Definition types.hpp:573
cv::Size_< T > Size2_
This class extends cv::Size_<T> for event data. For more information, please refer here.
Definition types.hpp:336
Rect3_< double > Rect3d
Definition types.hpp:514
Size2 Size
Definition types.hpp:342
cv::Rect_< T > Rect2_
This class extends cv::Rect_<T> for event data. For more information, please refer here.
Definition types.hpp:411
Circ_< float > Circf
Definition types.hpp:575
AugmentedEvent_< float > AugmentedEventf
Definition types.hpp:318
AugmentedEventi AugmentedEvent
Definition types.hpp:320
Rect2_< long > Rect2l
Definition types.hpp:413
Rect3_< float > Rect3f
Definition types.hpp:513
AugmentedEvent_< long > AugmentedEventl
Definition types.hpp:317
Size3_< float > Size3f
Definition types.hpp:393
Rect2_< int > Rect2i
Definition types.hpp:412
AugmentedEvent_< int > AugmentedEventi
Definition types.hpp:316
Size3_< double > Size3d
Definition types.hpp:394
constexpr bool NEGATIVE
Definition types.hpp:28
Event_< float > Eventf
Definition types.hpp:242
Size3_< long > Size3l
Definition types.hpp:392
Circi Circ
Definition types.hpp:577
AugmentedEvent_< double > AugmentedEventd
Definition types.hpp:319
Rect2_< float > Rect2f
Definition types.hpp:414
Size2_< long > Size2l
Definition types.hpp:338
Size2_< float > Size2f
Definition types.hpp:339
Eventi Event
Definition types.hpp:244
Size3_< int > Size3i
Definition types.hpp:391
Rect3_< long > Rect3l
Definition types.hpp:512
Undistortion utilities.