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
9#include <algorithm>
10#include <cmath>
11#include <cstdint>
12#include <iosfwd>
13#include <opencv2/core/base.hpp>
14#include <opencv2/core/matx.hpp>
15#include <opencv2/core/types.hpp>
16#include <opencv2/core/utils/logger.hpp>
17#include <ostream>
18#include <string>
19
20namespace ev {
21constexpr bool USING_TYPES_HPP = true;
22
23using TimeType = float;
24using PolarityType = bool;
25using WeightType = float;
26using DepthType = float;
27using CounterType = int16_t;
28
29constexpr PolarityType POSITIVE = static_cast<PolarityType>(true);
30constexpr PolarityType NEGATIVE = static_cast<PolarityType>(false);
31
35enum class Stereo : char {
36 LEFT = 'L',
37 RIGHT = 'R'
38};
39
50enum DistanceTypes : uint8_t {
51 DISTANCE_NORM_INF = static_cast<uint8_t>(cv::NORM_INF),
52 DISTANCE_NORM_L1 = static_cast<uint8_t>(cv::NORM_L1),
53 DISTANCE_NORM_L2 = static_cast<uint8_t>(cv::NORM_L2),
54 DISTANCE_NORM_L2SQR = static_cast<uint8_t>(cv::NORM_L2SQR),
55 DISTANCE_NORM_MANHATTAN = DISTANCE_NORM_L1,
56 DISTANCE_NORM_EUCLIDEAN = DISTANCE_NORM_L2,
57 DISTANCE_FLAG_SPATIAL = static_cast<uint8_t>(0b00010000),
58 DISTANCE_FLAG_TEMPORAL = static_cast<uint8_t>(0b00100000),
59 DISTANCE_FLAG_SPATIOTEMPORAL = static_cast<uint8_t>(0b01000000),
62};
63
76template <typename T>
77class Event_ : public cv::Point_<T> {
78public:
79 TimeType t;
80 PolarityType p;
81
85 Event_() : cv::Point_<T>(), t{0}, p{POSITIVE} {};
86
88 ~Event_() = default;
90
94 Event_(const Event_<T> &) = default;
95
99 Event_(Event_<T> &&) noexcept = default;
100
106 Event_(const T x, const T y) : cv::Point_<T>(x, y), t{0}, p{POSITIVE} {};
107
112 explicit Event_(const cv::Point_<T> &pt) : cv::Point_<T>(pt), t{0}, p{POSITIVE} {};
113
121 Event_(const T x, const T y, const TimeType t, const PolarityType p) : cv::Point_<T>(x, y), t{t}, p{p} {};
122
129 Event_(const cv::Point_<T> &pt, const TimeType t, const PolarityType p) : cv::Point_<T>(pt), t{t}, p{p} {};
130
134 Event_<T> &operator=(const Event_<T> &) = default;
135
139 template <typename U>
141 Event_<T>::x = static_cast<T>(e.x);
142 Event_<T>::y = static_cast<T>(e.y);
143 Event_<T>::t = static_cast<TimeType>(e.t);
144 Event_<T>::p = static_cast<PolarityType>(e.p);
145 return *this;
146 }
147
151 Event_<T> &operator=(const cv::Point3_<T> &p) {
152 Event_<T>::x = static_cast<T>(p.x);
153 Event_<T>::y = static_cast<T>(p.y);
154 Event_<T>::t = static_cast<TimeType>(p.z);
155 return *this;
156 }
157
161 Event_<T> &operator=(const cv::Point_<T> &p) {
162 Event_<T>::x = static_cast<T>(p.x);
163 Event_<T>::y = static_cast<T>(p.y);
164 return *this;
165 }
166
170 Event_<T> &operator=(Event_<T> &&) noexcept = default;
171
175 [[nodiscard]] inline bool operator==(const Event_<T> &e) const {
176 return (Event_<T>::x == e.x) && (Event_<T>::y == e.y) && (t == e.t) && (p == e.p);
177 }
178
182 [[nodiscard]] inline bool operator==(const cv::Point_<T> &pt) const {
183 return (Event_<T>::x == pt.x) && (Event_<T>::y == pt.y);
184 }
185
189 [[nodiscard]] inline bool operator==(const cv::Point3_<T> &pt) const {
190 return (Event_<T>::x == pt.x) && (Event_<T>::y == pt.y) && (Event_<T>::t == pt.z);
191 }
192
196 [[nodiscard]] inline bool operator<(const Event_<T> &e) const {
197 return Event_<T>::t < e.t;
198 }
199
203 template <typename U>
204 [[nodiscard]] inline explicit operator cv::Point_<U>() const {
205 return {static_cast<U>(Event_<T>::x), static_cast<U>(Event_<T>::y)};
206 }
207
211 template <typename U>
212 [[nodiscard]] inline explicit operator cv::Point3_<U>() const {
213 return {static_cast<U>(Event_<T>::x), static_cast<U>(Event_<T>::y), static_cast<U>(Event_<T>::t)};
214 }
215
223 [[nodiscard]] inline double distance(const Event_<T> &e, const uint8_t type = DISTANCE_NORM_L2 | DISTANCE_FLAG_SPATIAL) const {
224 if(static_cast<bool>(type & DISTANCE_FLAG_SPATIOTEMPORAL)) {
225 return cv::norm(cv::Matx<T, 3, 1>(Event_<T>::x - e.x, Event_<T>::y - e.y, Event_<T>::t - e.t), type & 0x0FU);
226 }
227 if(static_cast<bool>(type & DISTANCE_FLAG_SPATIAL) || !static_cast<bool>(type & 0xF0U)) {
228 return cv::norm(cv::Matx<T, 2, 1>(Event_<T>::x - e.x, Event_<T>::y - e.y), type & 0x0FU);
229 }
230 if(static_cast<bool>(type & DISTANCE_FLAG_TEMPORAL)) {
231 return Event_<T>::t - e.t;
232 }
233 CV_LOG_ERROR(nullptr, "Bad distance option");
234 return 0.0;
235 }
236
243 [[nodiscard]] friend std::ostream &operator<<(std::ostream &os, const Event_<T> &e) {
244 os << std::string("(" + std::to_string(e.x) + "," + std::to_string(e.y) + ") " + std::to_string(e.t) + (e.p ? " [+]" : " [-]"));
245 return os;
246 }
247};
252using Event = Eventi;
253
266template <typename T>
267class AugmentedEvent_ : public Event_<T> {
268 using Event_<T>::Event_;
269
270public:
271 WeightType weight{1};
272 DepthType depth{0};
274
281 [[nodiscard]] friend std::ostream &operator<<(std::ostream &os, const AugmentedEvent_<T> &e) {
282 os << std::string("(" + std::to_string(e.x) + "," + std::to_string(e.y) + ") " + std::to_string(e.t) + (e.p ? " [+]" : " [-]"));
283 os << " w=" + std::to_string(e.weight);
284 os << " d=" + std::to_string(e.depth);
285 os << " s=" + (e.stereo == Stereo::LEFT ? std::string("LEFT") : std::string("RIGHT"));
286 return os;
287 }
288};
294
308template <typename T>
309using Size2_ = cv::Size_<T>;
314using Size2 = Size2i;
315using Size = Size2;
316
329template <typename T>
330class Size3_ : public cv::Size_<T> {
331public:
333
337 Size3_() : cv::Size_<T>(), length{0} {};
338
345 Size3_(T w, T h, T l) : cv::Size_<T>(w, h), length{l} {};
346
351 [[nodiscard]] inline bool empty() const {
352 return cv::Size_<T>::width <= 0 || cv::Size_<T>::height <= 0 || length <= 0;
353 }
354
360 [[nodiscard]] inline T volume() const {
361 return cv::Size_<T>::width * cv::Size_<T>::height * length;
362 }
363};
368using Size3 = Size3i;
369
383template <typename T>
384using Rect2_ = cv::Rect_<T>;
389using Rect2 = Rect2i;
390using Rect = Rect2;
391
404template <typename T>
405class Rect3_ : public cv::Rect_<T> {
406public:
407 T t;
409
413 Rect3_() : cv::Rect_<T>(), t{0}, length{0} {};
414
424 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} {};
425
432 Rect3_(const Rect2_<T> &rect, const T t, const T l) : cv::Rect_<T>(rect), t{t}, length{l} {};
433
439 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} {};
440
446 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} {};
447
452 [[nodiscard]] inline bool empty() const {
453 return Rect3_<T>::width <= 0 || Rect3_<T>::height <= 0 || length <= 0;
454 }
455
462 template <typename Te>
463 [[nodiscard]] inline bool contains(const Event_<Te> &e) const {
464 return cv::Rect_<T>::contains(e) && e.t >= t && e.t < t + length;
465 }
466
471 [[nodiscard]] inline Size3_<T> size() const {
473 }
474
480 [[nodiscard]] inline T volume() const {
482 }
483};
488using Rect3 = Rect3i;
489
502template <typename T>
503struct alignas(16) Circ_ {
504 cv::Point_<T> center;
505 T radius;
506
510 Circ_() : center{0, 0}, radius{0} {};
511
517 Circ_(const cv::Point_<T> center, const T radius) : center{center}, radius{std::max(radius, static_cast<T>(0))} {}
518
523 [[nodiscard]] inline bool empty() const {
524 return radius <= 0;
525 }
526
533 template <typename Te>
534 [[nodiscard]] inline bool contains(const Event_<Te> &e) const {
535 return !empty() && pow(center.x - e.x, 2) + pow(center.y - e.y, 2) <= radius * radius;
536 }
537
542 [[nodiscard]] inline cv::Size size() const {
543 return {radius, radius};
544 }
545
551 [[nodiscard]] inline double area() const {
552 return M_PI * radius * radius;
553 }
554};
559using Circ = Circi;
560
561} // namespace ev
562
563#endif // OPENEV_CORE_TYPES_HPP
This class extends Event to include: weigth, depth, and stereo.
Definition types.hpp:267
DepthType depth
Definition types.hpp:272
WeightType weight
Definition types.hpp:271
Stereo stereo
Definition types.hpp:273
friend std::ostream & operator<<(std::ostream &os, const AugmentedEvent_< T > &e)
Overload of << operator.
Definition types.hpp:281
This class extends cv::Point_<T> for event data. For more information, please refer here.
Definition types.hpp:77
PolarityType p
Definition types.hpp:80
bool operator<(const Event_< T > &e) const
Definition types.hpp:196
Event_< T > & operator=(const cv::Point_< T > &p)
Definition types.hpp:161
Event_(const T x, const T y, const TimeType t, const PolarityType p)
Definition types.hpp:121
bool operator==(const cv::Point3_< T > &pt) const
Definition types.hpp:189
Event_(const cv::Point_< T > &pt, const TimeType t, const PolarityType p)
Definition types.hpp:129
Event_< T > & operator=(Event_< T > &&) noexcept=default
Event_< T > & operator=(const Event_< U > &e)
Definition types.hpp:140
bool operator==(const cv::Point_< T > &pt) const
Definition types.hpp:182
TimeType t
Definition types.hpp:79
Event_()
Definition types.hpp:85
Event_< T > & operator=(const cv::Point3_< T > &p)
Definition types.hpp:151
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:223
Event_(const Event_< T > &)=default
Event_(const cv::Point_< T > &pt)
Definition types.hpp:112
friend std::ostream & operator<<(std::ostream &os, const Event_< T > &e)
Overload of << operator.
Definition types.hpp:243
This class extends cv::Rect_<T> for event data. For more information, please refer here.
Definition types.hpp:405
Rect3_(const T x, const T y, const T t, const T w, const T h, const T l)
Definition types.hpp:424
Rect3_(const Rect2_< T > &rect, const T t, const T l)
Definition types.hpp:432
Size3_< T > size() const
Size of the rectangular cuboid.
Definition types.hpp:471
T volume() const
Compute the volume.
Definition types.hpp:480
bool empty() const
Check if empty.
Definition types.hpp:452
Rect3_(const cv::Point3_< T > &pt1, const cv::Point3_< T > &pt2)
Definition types.hpp:446
Rect3_(const cv::Point3_< T > &pt, const Size3_< T > sz)
Definition types.hpp:439
int t
Definition types.hpp:407
bool contains(const Event_< Te > &e) const
Check if the rectangular cuboid contains an event.
Definition types.hpp:463
Rect3_()
Definition types.hpp:413
int length
Definition types.hpp:408
This class extends cv::Size_<T> for event data. For more information, please refer here.
Definition types.hpp:330
bool empty() const
Check if empty.
Definition types.hpp:351
T volume() const
Compute the volume.
Definition types.hpp:360
Size3_(T w, T h, T l)
Definition types.hpp:345
int length
Definition types.hpp:332
Size3_()
Definition types.hpp:337
This class defines a circle given its center and radius.
Definition types.hpp:503
Circ_()
Definition types.hpp:510
double area() const
Compute the area of the circle.
Definition types.hpp:551
Circ_(const cv::Point_< T > center, const T radius)
Definition types.hpp:517
cv::Size size() const
Bounding square of the circle as a cv::Size (radius × radius).
Definition types.hpp:542
bool contains(const Event_< Te > &e) const
Check if the circle contains an event.
Definition types.hpp:534
bool empty() const
Check if empty.
Definition types.hpp:523
Event_< long > Eventl
Definition types.hpp:249
Rect2_< double > Rect2d
Definition types.hpp:388
constexpr PolarityType POSITIVE
Definition types.hpp:29
Event_< int > Eventi
Definition types.hpp:248
Size2_< double > Size2d
Definition types.hpp:313
Size2i Size2
Definition types.hpp:314
Rect2i Rect2
Definition types.hpp:389
Rect2 Rect
Definition types.hpp:390
Event_< double > Eventd
Definition types.hpp:251
Size2_< int > Size2i
Definition types.hpp:310
Circ_< long > Circl
Definition types.hpp:556
Rect3_< int > Rect3i
Definition types.hpp:484
Size3i Size3
Definition types.hpp:368
Circ_< double > Circd
Definition types.hpp:558
Rect3i Rect3
Definition types.hpp:488
Circ_< int > Circi
Definition types.hpp:555
cv::Size_< T > Size2_
Type alias for cv::Size_<T> providing a named 2D size for event data. For more information,...
Definition types.hpp:309
Rect3_< double > Rect3d
Definition types.hpp:487
Size2 Size
Definition types.hpp:315
DistanceTypes
Norm types and dimensional flags for Event_::distance().
Definition types.hpp:50
@ DISTANCE_FLAG_SPATIOTEMPORAL
Definition types.hpp:59
@ DISTANCE_FLAG_TEMPORAL
Definition types.hpp:58
@ DISTANCE_FLAG_2D
Definition types.hpp:61
@ DISTANCE_FLAG_SPATIAL
Definition types.hpp:57
@ DISTANCE_FLAG_3D
Definition types.hpp:60
cv::Rect_< T > Rect2_
Type alias for cv::Rect_<T> providing a named 2D rectangle for event data. For more information,...
Definition types.hpp:384
Circ_< float > Circf
Definition types.hpp:557
AugmentedEvent_< float > AugmentedEventf
Definition types.hpp:291
AugmentedEventi AugmentedEvent
Definition types.hpp:293
Rect2_< long > Rect2l
Definition types.hpp:386
Rect3_< float > Rect3f
Definition types.hpp:486
AugmentedEvent_< long > AugmentedEventl
Definition types.hpp:290
Size3_< float > Size3f
Definition types.hpp:366
Rect2_< int > Rect2i
Definition types.hpp:385
Stereo
Stereo camera side identifier.
Definition types.hpp:35
@ RIGHT
Definition types.hpp:37
@ LEFT
Definition types.hpp:36
AugmentedEvent_< int > AugmentedEventi
Definition types.hpp:289
Size3_< double > Size3d
Definition types.hpp:367
Event_< float > Eventf
Definition types.hpp:250
Size3_< long > Size3l
Definition types.hpp:365
Circi Circ
Definition types.hpp:559
AugmentedEvent_< double > AugmentedEventd
Definition types.hpp:292
Rect2_< float > Rect2f
Definition types.hpp:387
Size2_< long > Size2l
Definition types.hpp:311
Size2_< float > Size2f
Definition types.hpp:312
Eventi Event
Definition types.hpp:252
Size3_< int > Size3i
Definition types.hpp:364
Rect3_< long > Rect3l
Definition types.hpp:485
constexpr PolarityType NEGATIVE
Definition types.hpp:30