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#include <type_traits>
20
21namespace ev {
22constexpr bool USING_TYPES_HPP = true;
23
24using TimeType = double;
25using PolarityType = bool;
26using WeightType = float;
27using DepthType = float;
28using CounterType = int16_t;
29
30constexpr PolarityType POSITIVE = static_cast<PolarityType>(true);
31constexpr PolarityType NEGATIVE = static_cast<PolarityType>(false);
32
36enum class Stereo : char {
37 LEFT = 'L',
38 RIGHT = 'R'
39};
40
51enum DistanceTypes : uint8_t {
52 DISTANCE_NORM_INF = static_cast<uint8_t>(cv::NORM_INF),
53 DISTANCE_NORM_L1 = static_cast<uint8_t>(cv::NORM_L1),
54 DISTANCE_NORM_L2 = static_cast<uint8_t>(cv::NORM_L2),
55 DISTANCE_NORM_L2SQR = static_cast<uint8_t>(cv::NORM_L2SQR),
56 DISTANCE_NORM_MANHATTAN = DISTANCE_NORM_L1,
57 DISTANCE_NORM_EUCLIDEAN = DISTANCE_NORM_L2,
58 DISTANCE_FLAG_SPATIAL = static_cast<uint8_t>(0b00010000),
59 DISTANCE_FLAG_TEMPORAL = static_cast<uint8_t>(0b00100000),
60 DISTANCE_FLAG_SPATIOTEMPORAL = static_cast<uint8_t>(0b01000000),
63};
64
77template <typename T>
78class Event_ : public cv::Point_<T> {
79 static_assert(std::is_arithmetic_v<T>, "ev::Event_: the coordinate type must be arithmetic.");
80
81public:
82 TimeType t;
83 PolarityType p;
84
88 Event_() : cv::Point_<T>(), t{0}, p{POSITIVE} {};
89
91 ~Event_() = default;
93
97 Event_(const Event_<T> &) = default;
98
102 Event_(Event_<T> &&) noexcept = default;
103
109 Event_(const T x, const T y) : cv::Point_<T>(x, y), t{0}, p{POSITIVE} {};
110
115 explicit Event_(const cv::Point_<T> &pt) : cv::Point_<T>(pt), t{0}, p{POSITIVE} {};
116
124 Event_(const T x, const T y, const TimeType t, const PolarityType p) : cv::Point_<T>(x, y), t{t}, p{p} {};
125
132 Event_(const cv::Point_<T> &pt, const TimeType t, const PolarityType p) : cv::Point_<T>(pt), t{t}, p{p} {};
133
137 Event_<T> &operator=(const Event_<T> &) = default;
138
142 template <typename U>
144 Event_<T>::x = static_cast<T>(e.x);
145 Event_<T>::y = static_cast<T>(e.y);
146 Event_<T>::t = static_cast<TimeType>(e.t);
147 Event_<T>::p = static_cast<PolarityType>(e.p);
148 return *this;
149 }
150
154 Event_<T> &operator=(const cv::Point3_<T> &p) {
155 Event_<T>::x = static_cast<T>(p.x);
156 Event_<T>::y = static_cast<T>(p.y);
157 Event_<T>::t = static_cast<TimeType>(p.z);
158 return *this;
159 }
160
164 Event_<T> &operator=(const cv::Point_<T> &p) {
165 Event_<T>::x = static_cast<T>(p.x);
166 Event_<T>::y = static_cast<T>(p.y);
167 return *this;
168 }
169
173 Event_<T> &operator=(Event_<T> &&) noexcept = default;
174
178 [[nodiscard]] inline bool operator==(const Event_<T> &e) const {
179 return (Event_<T>::x == e.x) && (Event_<T>::y == e.y) && (t == e.t) && (p == e.p);
180 }
181
185 [[nodiscard]] inline bool operator==(const cv::Point_<T> &pt) const {
186 return (Event_<T>::x == pt.x) && (Event_<T>::y == pt.y);
187 }
188
192 [[nodiscard]] inline bool operator==(const cv::Point3_<T> &pt) const {
193 return (Event_<T>::x == pt.x) && (Event_<T>::y == pt.y) && (Event_<T>::t == pt.z);
194 }
195
199 [[nodiscard]] inline bool operator<(const Event_<T> &e) const {
200 return Event_<T>::t < e.t;
201 }
202
206 template <typename U>
207 [[nodiscard]] inline explicit operator cv::Point_<U>() const {
208 return {static_cast<U>(Event_<T>::x), static_cast<U>(Event_<T>::y)};
209 }
210
214 template <typename U>
215 [[nodiscard]] inline explicit operator cv::Point3_<U>() const {
216 return {static_cast<U>(Event_<T>::x), static_cast<U>(Event_<T>::y), static_cast<U>(Event_<T>::t)};
217 }
218
226 [[nodiscard]] inline double distance(const Event_<T> &e, const uint8_t type = DISTANCE_NORM_L2 | DISTANCE_FLAG_SPATIAL) const {
227 if(static_cast<bool>(type & DISTANCE_FLAG_SPATIOTEMPORAL)) {
228 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);
229 }
230 if(static_cast<bool>(type & DISTANCE_FLAG_SPATIAL) || !static_cast<bool>(type & 0xF0U)) {
231 return cv::norm(cv::Matx<T, 2, 1>(Event_<T>::x - e.x, Event_<T>::y - e.y), type & 0x0FU);
232 }
233 if(static_cast<bool>(type & DISTANCE_FLAG_TEMPORAL)) {
234 return Event_<T>::t - e.t;
235 }
236 CV_Error(cv::Error::StsBadArg, "ev::Event_::distance: Bad distance option.");
237 }
238
245 [[nodiscard]] friend std::ostream &operator<<(std::ostream &os, const Event_<T> &e) {
246 os << std::string("(" + std::to_string(e.x) + "," + std::to_string(e.y) + ") " + std::to_string(e.t) + (e.p ? " [+]" : " [-]"));
247 return os;
248 }
249};
254using Event = Eventi;
255
268template <typename T>
269class AugmentedEvent_ : public Event_<T> {
270 using Event_<T>::Event_;
271
272public:
273 WeightType weight{1};
274 DepthType depth{0};
276
283 [[nodiscard]] friend std::ostream &operator<<(std::ostream &os, const AugmentedEvent_<T> &e) {
284 os << std::string("(" + std::to_string(e.x) + "," + std::to_string(e.y) + ") " + std::to_string(e.t) + (e.p ? " [+]" : " [-]"));
285 os << " w=" + std::to_string(e.weight);
286 os << " d=" + std::to_string(e.depth);
287 os << " s=" + (e.stereo == Stereo::LEFT ? std::string("LEFT") : std::string("RIGHT"));
288 return os;
289 }
290};
296
310template <typename T>
311using Size2_ = cv::Size_<T>;
316using Size2 = Size2i;
317using Size = Size2;
318
331template <typename T>
332class Size3_ : public cv::Size_<T> {
333public:
335
339 Size3_() : cv::Size_<T>(), length{0} {};
340
347 Size3_(T w, T h, T l) : cv::Size_<T>(w, h), length{l} {};
348
353 [[nodiscard]] inline bool empty() const {
354 return cv::Size_<T>::width <= 0 || cv::Size_<T>::height <= 0 || length <= 0;
355 }
356
362 [[nodiscard]] inline T volume() const {
363 return cv::Size_<T>::width * cv::Size_<T>::height * length;
364 }
365};
370using Size3 = Size3i;
371
385template <typename T>
386using Rect2_ = cv::Rect_<T>;
391using Rect2 = Rect2i;
392using Rect = Rect2;
393
406template <typename T>
407class Rect3_ : public cv::Rect_<T> {
408public:
409 T t;
411
415 Rect3_() : cv::Rect_<T>(), t{0}, length{0} {};
416
426 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} {};
427
434 Rect3_(const Rect2_<T> &rect, const T t, const T l) : cv::Rect_<T>(rect), t{t}, length{l} {};
435
441 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} {};
442
448 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} {};
449
454 [[nodiscard]] inline bool empty() const {
455 return Rect3_<T>::width <= 0 || Rect3_<T>::height <= 0 || length <= 0;
456 }
457
464 template <typename Te>
465 [[nodiscard]] inline bool contains(const Event_<Te> &e) const {
466 return cv::Rect_<T>::contains(e) && e.t >= t && e.t < t + length;
467 }
468
473 [[nodiscard]] inline Size3_<T> size() const {
475 }
476
482 [[nodiscard]] inline T volume() const {
484 }
485};
490using Rect3 = Rect3i;
491
504template <typename T>
505struct alignas(16) Circ_ {
506 cv::Point_<T> center;
507 T radius;
508
512 Circ_() : center{0, 0}, radius{0} {};
513
519 Circ_(const cv::Point_<T> center, const T radius) : center{center}, radius{std::max(radius, static_cast<T>(0))} {}
520
525 [[nodiscard]] inline bool empty() const {
526 return radius <= 0;
527 }
528
535 template <typename Te>
536 [[nodiscard]] inline bool contains(const Event_<Te> &e) const {
537 return !empty() && pow(center.x - e.x, 2) + pow(center.y - e.y, 2) <= radius * radius;
538 }
539
544 [[nodiscard]] inline cv::Size size() const {
545 return {radius, radius};
546 }
547
553 [[nodiscard]] inline double area() const {
554 return M_PI * radius * radius;
555 }
556};
561using Circ = Circi;
562
563} // namespace ev
564
565#endif // OPENEV_CORE_TYPES_HPP
This class extends Event to include: weigth, depth, and stereo.
Definition types.hpp:269
DepthType depth
Definition types.hpp:274
WeightType weight
Definition types.hpp:273
Stereo stereo
Definition types.hpp:275
friend std::ostream & operator<<(std::ostream &os, const AugmentedEvent_< T > &e)
Overload of << operator.
Definition types.hpp:283
This class extends cv::Point_<T> for event data. For more information, please refer here.
Definition types.hpp:78
PolarityType p
Definition types.hpp:83
bool operator<(const Event_< T > &e) const
Definition types.hpp:199
Event_< T > & operator=(const cv::Point_< T > &p)
Definition types.hpp:164
Event_(const T x, const T y, const TimeType t, const PolarityType p)
Definition types.hpp:124
bool operator==(const cv::Point3_< T > &pt) const
Definition types.hpp:192
Event_(const cv::Point_< T > &pt, const TimeType t, const PolarityType p)
Definition types.hpp:132
Event_< T > & operator=(Event_< T > &&) noexcept=default
Event_< T > & operator=(const Event_< U > &e)
Definition types.hpp:143
bool operator==(const cv::Point_< T > &pt) const
Definition types.hpp:185
TimeType t
Definition types.hpp:82
Event_()
Definition types.hpp:88
Event_< T > & operator=(const cv::Point3_< T > &p)
Definition types.hpp:154
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:226
Event_(const Event_< T > &)=default
Event_(const cv::Point_< T > &pt)
Definition types.hpp:115
friend std::ostream & operator<<(std::ostream &os, const Event_< T > &e)
Overload of << operator.
Definition types.hpp:245
This class extends cv::Rect_<T> for event data. For more information, please refer here.
Definition types.hpp:407
Rect3_(const T x, const T y, const T t, const T w, const T h, const T l)
Definition types.hpp:426
Rect3_(const Rect2_< T > &rect, const T t, const T l)
Definition types.hpp:434
Size3_< T > size() const
Size of the rectangular cuboid.
Definition types.hpp:473
T volume() const
Compute the volume.
Definition types.hpp:482
bool empty() const
Check if empty.
Definition types.hpp:454
Rect3_(const cv::Point3_< T > &pt1, const cv::Point3_< T > &pt2)
Definition types.hpp:448
Rect3_(const cv::Point3_< T > &pt, const Size3_< T > sz)
Definition types.hpp:441
int t
Definition types.hpp:409
bool contains(const Event_< Te > &e) const
Check if the rectangular cuboid contains an event.
Definition types.hpp:465
Rect3_()
Definition types.hpp:415
int length
Definition types.hpp:410
This class extends cv::Size_<T> for event data. For more information, please refer here.
Definition types.hpp:332
bool empty() const
Check if empty.
Definition types.hpp:353
T volume() const
Compute the volume.
Definition types.hpp:362
Size3_(T w, T h, T l)
Definition types.hpp:347
int length
Definition types.hpp:334
Size3_()
Definition types.hpp:339
This class defines a circle given its center and radius.
Definition types.hpp:505
Circ_()
Definition types.hpp:512
double area() const
Compute the area of the circle.
Definition types.hpp:553
Circ_(const cv::Point_< T > center, const T radius)
Definition types.hpp:519
cv::Size size() const
Bounding square of the circle as a cv::Size (radius x radius).
Definition types.hpp:544
bool contains(const Event_< Te > &e) const
Check if the circle contains an event.
Definition types.hpp:536
bool empty() const
Check if empty.
Definition types.hpp:525
Event_< long > Eventl
Definition types.hpp:251
Rect2_< double > Rect2d
Definition types.hpp:390
constexpr PolarityType POSITIVE
Definition types.hpp:30
Event_< int > Eventi
Definition types.hpp:250
Size2_< double > Size2d
Definition types.hpp:315
Size2i Size2
Definition types.hpp:316
Rect2i Rect2
Definition types.hpp:391
Rect2 Rect
Definition types.hpp:392
Event_< double > Eventd
Definition types.hpp:253
Size2_< int > Size2i
Definition types.hpp:312
Circ_< long > Circl
Definition types.hpp:558
Rect3_< int > Rect3i
Definition types.hpp:486
Size3i Size3
Definition types.hpp:370
Circ_< double > Circd
Definition types.hpp:560
Rect3i Rect3
Definition types.hpp:490
Circ_< int > Circi
Definition types.hpp:557
cv::Size_< T > Size2_
Type alias for cv::Size_<T> providing a named 2D size for event data. For more information,...
Definition types.hpp:311
Rect3_< double > Rect3d
Definition types.hpp:489
Size2 Size
Definition types.hpp:317
DistanceTypes
Norm types and dimensional flags for Event_::distance().
Definition types.hpp:51
@ DISTANCE_FLAG_SPATIOTEMPORAL
Definition types.hpp:60
@ DISTANCE_FLAG_TEMPORAL
Definition types.hpp:59
@ DISTANCE_FLAG_2D
Definition types.hpp:62
@ DISTANCE_FLAG_SPATIAL
Definition types.hpp:58
@ DISTANCE_FLAG_3D
Definition types.hpp:61
cv::Rect_< T > Rect2_
Type alias for cv::Rect_<T> providing a named 2D rectangle for event data. For more information,...
Definition types.hpp:386
Circ_< float > Circf
Definition types.hpp:559
AugmentedEvent_< float > AugmentedEventf
Definition types.hpp:293
AugmentedEventi AugmentedEvent
Definition types.hpp:295
Rect2_< long > Rect2l
Definition types.hpp:388
Rect3_< float > Rect3f
Definition types.hpp:488
AugmentedEvent_< long > AugmentedEventl
Definition types.hpp:292
Size3_< float > Size3f
Definition types.hpp:368
Rect2_< int > Rect2i
Definition types.hpp:387
Stereo
Stereo camera side identifier.
Definition types.hpp:36
@ RIGHT
Definition types.hpp:38
@ LEFT
Definition types.hpp:37
AugmentedEvent_< int > AugmentedEventi
Definition types.hpp:291
Size3_< double > Size3d
Definition types.hpp:369
Event_< float > Eventf
Definition types.hpp:252
Size3_< long > Size3l
Definition types.hpp:367
Circi Circ
Definition types.hpp:561
AugmentedEvent_< double > AugmentedEventd
Definition types.hpp:294
Rect2_< float > Rect2f
Definition types.hpp:389
Size2_< long > Size2l
Definition types.hpp:313
Size2_< float > Size2f
Definition types.hpp:314
Eventi Event
Definition types.hpp:254
Size3_< int > Size3i
Definition types.hpp:366
Rect3_< long > Rect3l
Definition types.hpp:487
constexpr PolarityType NEGATIVE
Definition types.hpp:31