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 POSITIVE = true;
22constexpr bool NEGATIVE = false;
23
24enum class Stereo : char {
25 LEFT = 'L',
26 RIGHT = 'R'
27};
28
29enum DistanceTypes : uint8_t {
30 DISTANCE_NORM_INF = static_cast<uint8_t>(cv::NORM_INF),
31 DISTANCE_NORM_L1 = static_cast<uint8_t>(cv::NORM_L1),
32 DISTANCE_NORM_L2 = static_cast<uint8_t>(cv::NORM_L2),
33 DISTANCE_NORM_L2SQR = static_cast<uint8_t>(cv::NORM_L2SQR),
34 DISTANCE_NORM_MANHATTAN = DISTANCE_NORM_L1,
35 DISTANCE_NORM_EUCLIDEAN = DISTANCE_NORM_L2,
36 DISTANCE_FLAG_SPATIAL = static_cast<uint8_t>(0b00010000),
37 DISTANCE_FLAG_TEMPORAL = static_cast<uint8_t>(0b00100000),
38 DISTANCE_FLAG_SPATIOTEMPORAL = static_cast<uint8_t>(0b01000000),
39 DISTANCE_FLAG_3D = DISTANCE_FLAG_SPATIOTEMPORAL,
40 DISTANCE_FLAG_2D = DISTANCE_FLAG_SPATIAL,
41};
42
55template <typename T>
56class Event_ : public cv::Point_<T> {
57public:
58 double t;
59 bool p;
60
64 Event_() : cv::Point_<T>(), t{0}, p{POSITIVE} {};
65
67 ~Event_() = default;
69
73 Event_(const Event_<T> &) = default;
74
78 Event_(Event_<T> &&) noexcept = default;
79
85 Event_(const T x, const T y) : cv::Point_<T>(x, y), t{0}, p{POSITIVE} {};
86
91 explicit Event_(const cv::Point_<T> &pt) : cv::Point_<T>(pt), t{0}, p{POSITIVE} {};
92
100 Event_(const T x, const T y, const double t, const bool p) : cv::Point_<T>(x, y), t{t}, p{p} {};
101
108 Event_(const cv::Point_<T> &pt, const double t, const bool p) : cv::Point_<T>(pt), t{t}, p{p} {};
109
113 Event_<T> &operator=(const Event_<T> &) = default;
114
118 template <typename U>
120 Event_<T>::x = static_cast<T>(e.x);
121 Event_<T>::y = static_cast<T>(e.y);
122 Event_<T>::t = static_cast<double>(e.t);
123 Event_<T>::p = static_cast<bool>(e.p);
124 return *this;
125 }
126
130 Event_<T> &operator=(const cv::Point3_<T> &p) {
131 Event_<T>::x = static_cast<T>(p.x);
132 Event_<T>::y = static_cast<T>(p.y);
133 Event_<T>::t = static_cast<double>(p.z);
134 return *this;
135 }
136
140 Event_<T> &operator=(const cv::Point_<T> &p) {
141 Event_<T>::x = static_cast<T>(p.x);
142 Event_<T>::y = static_cast<T>(p.y);
143 return *this;
144 }
145
149 Event_<T> &operator=(Event_<T> &&) noexcept = default;
150
154 [[nodiscard]] inline bool operator==(const Event_<T> &e) const {
155 return (Event_<T>::x == e.x) && (Event_<T>::y == e.y) && (t == e.t) && (p == e.p);
156 }
157
161 [[nodiscard]] inline bool operator==(const cv::Point_<T> &pt) const {
162 return (Event_<T>::x == pt.x) && (Event_<T>::y == pt.y);
163 }
164
168 [[nodiscard]] inline bool operator==(const cv::Point3_<T> &pt) const {
169 return (Event_<T>::x == pt.x) && (Event_<T>::y == pt.y) && (Event_<T>::t == pt.z);
170 }
171
175 [[nodiscard]] inline bool operator<(const Event_<T> &e) const {
176 return Event_<T>::t < e.t;
177 }
178
182 template <typename U>
183 [[nodiscard]] inline explicit operator cv::Point_<U>() const {
184 return {static_cast<U>(Event_<T>::x), static_cast<U>(Event_<T>::y)};
185 }
186
190 template <typename U>
191 [[nodiscard]] inline explicit operator cv::Point3_<U>() const {
192 return {static_cast<U>(Event_<T>::x), static_cast<U>(Event_<T>::y), static_cast<U>(Event_<T>::t)};
193 }
194
202 [[nodiscard]] inline double distance(const Event_<T> &e, const uint8_t type = DISTANCE_NORM_L2 | DISTANCE_FLAG_SPATIAL) const {
203 if(static_cast<bool>(type & DISTANCE_FLAG_SPATIOTEMPORAL)) {
204 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);
205 }
206 if(static_cast<bool>(type & DISTANCE_FLAG_SPATIAL) || !static_cast<bool>(type & 0xF0U)) {
207 return cv::norm(cv::Matx<T, 2, 1>(Event_<T>::x - e.x, Event_<T>::y - e.y), type & 0x0FU);
208 }
209 if(static_cast<bool>(type & DISTANCE_FLAG_TEMPORAL)) {
210 return Event_<T>::t - e.t;
211 }
212 CV_LOG_ERROR(nullptr, "Bad distance option");
213 return 0.0;
214 }
215
222 [[nodiscard]] friend std::ostream &operator<<(std::ostream &os, const Event_<T> &e) {
223 os << std::string("(" + std::to_string(e.x) + "," + std::to_string(e.y) + ") " + std::to_string(e.t) + (e.p ? " [+]" : " [-]"));
224 return os;
225 }
226};
231using Event = Eventi;
232
245template <typename T>
246class AugmentedEvent_ : public Event_<T> {
247 using Event_<T>::Event_;
248
249public:
250 double weight{1};
251 double depth{0};
252 Stereo stereo{Stereo::LEFT};
253
260 [[nodiscard]] friend std::ostream &operator<<(std::ostream &os, const AugmentedEvent_<T> &e) {
261 os << std::string("(" + std::to_string(e.x) + "," + std::to_string(e.y) + ") " + std::to_string(e.t) + (e.p ? " [+]" : " [-]"));
262 os << " w=" + std::to_string(e.weight);
263 os << " d=" + std::to_string(e.depth);
264 os << " s=" + (e.stereo == Stereo::LEFT ? std::string("LEFT") : std::string("RIGHT"));
265 return os;
266 }
267};
273
287template <typename T>
288using Size2_ = cv::Size_<T>;
293using Size2 = Size2i;
294using Size = Size2;
295
308template <typename T>
309class Size3_ : public cv::Size_<T> {
310public:
312
316 Size3_() : cv::Size_<T>(), length{0} {};
317
324 Size3_(T w, T h, T l) : cv::Size_<T>(w, h), length{l} {};
325
330 [[nodiscard]] inline bool empty() const {
331 return cv::Size_<T>::width <= 0 || cv::Size_<T>::height <= 0 || length <= 0;
332 }
333
339 [[nodiscard]] inline T volume() const {
340 return cv::Size_<T>::width * cv::Size_<T>::height * length;
341 }
342};
347using Size3 = Size3i;
348
362template <typename T>
363using Rect2_ = cv::Rect_<T>;
368using Rect2 = Rect2i;
369using Rect = Rect2;
370
383template <typename T>
384class Rect3_ : public cv::Rect_<T> {
385public:
386 T t;
388
392 Rect3_() : cv::Rect_<T>(), t{0}, length{0} {};
393
403 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} {};
404
411 Rect3_(const Rect2_<T> &rect, const T t, const T l) : cv::Rect_<T>(rect), t{t}, length{l} {};
412
418 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} {};
419
425 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} {};
426
431 [[nodiscard]] inline bool empty() const {
432 return Rect3_<T>::width <= 0 || Rect3_<T>::height <= 0 || length <= 0;
433 }
434
441 template <typename Te>
442 [[nodiscard]] inline bool contains(const Event_<Te> &e) const {
443 return cv::Rect_<T>::contains(e) && e.t >= t && e.t < t + length;
444 }
445
450 [[nodiscard]] inline Size3_<T> size() const {
452 }
453
459 [[nodiscard]] inline T volume() const {
461 }
462};
467using Rect3 = Rect3i;
468
481template <typename T>
482struct alignas(16) Circ_ {
483 cv::Point_<T> center;
484 T radius;
485
489 Circ_() : center{0, 0}, radius{0} {};
490
496 Circ_(const cv::Point_<T> center, const T radius) : center{center}, radius{std::max(radius, static_cast<T>(0))} {}
497
502 [[nodiscard]] inline bool empty() const {
503 return radius <= 0;
504 }
505
512 template <typename Te>
513 [[nodiscard]] inline bool contains(const Event_<Te> &e) const {
514 return !empty() && pow(center.x - e.x, 2) + pow(center.y - e.y, 2) <= radius * radius;
515 }
516
517 [[nodiscard]] inline cv::Size size() const {
518 return {radius, radius};
519 }
520
521 [[nodiscard]] inline double area() const {
522 return M_PI * radius * radius;
523 }
524};
529using Circ = Circi;
530
531} // namespace ev
532
533#endif // OPENEV_CORE_TYPES_HPP
This class extends Event to include: weigth, depth, and stereo.
Definition types.hpp:246
Stereo stereo
Definition types.hpp:252
double depth
Definition types.hpp:251
friend std::ostream & operator<<(std::ostream &os, const AugmentedEvent_< T > &e)
Overload of << operator.
Definition types.hpp:260
double weight
Definition types.hpp:250
This class extends cv::Point_<T> for event data. For more information, please refer here.
Definition types.hpp:56
bool operator<(const Event_< T > &e) const
Definition types.hpp:175
Event_(const cv::Point_< T > &pt, const double t, const bool p)
Definition types.hpp:108
Event_< T > & operator=(const cv::Point_< T > &p)
Definition types.hpp:140
bool p
Definition types.hpp:59
bool operator==(const cv::Point3_< T > &pt) const
Definition types.hpp:168
Event_< T > & operator=(Event_< T > &&) noexcept=default
Event_< T > & operator=(const Event_< U > &e)
Definition types.hpp:119
bool operator==(const cv::Point_< T > &pt) const
Definition types.hpp:161
Event_()
Definition types.hpp:64
Event_< T > & operator=(const cv::Point3_< T > &p)
Definition types.hpp:130
Event_(const T x, const T y, const double t, const bool p)
Definition types.hpp:100
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:202
Event_(const Event_< T > &)=default
Event_(const cv::Point_< T > &pt)
Definition types.hpp:91
friend std::ostream & operator<<(std::ostream &os, const Event_< T > &e)
Overload of << operator.
Definition types.hpp:222
double t
Definition types.hpp:58
This class extends cv::Rect_<T> for event data. For more information, please refer here.
Definition types.hpp:384
Rect3_(const T x, const T y, const T t, const T w, const T h, const T l)
Definition types.hpp:403
Rect3_(const Rect2_< T > &rect, const T t, const T l)
Definition types.hpp:411
Size3_< T > size() const
Size of the rectangular cuboid.
Definition types.hpp:450
T volume() const
Compute the volume.
Definition types.hpp:459
bool empty() const
Check if empty.
Definition types.hpp:431
Rect3_(const cv::Point3_< T > &pt1, const cv::Point3_< T > &pt2)
Definition types.hpp:425
Rect3_(const cv::Point3_< T > &pt, const Size3_< T > sz)
Definition types.hpp:418
int t
Definition types.hpp:386
bool contains(const Event_< Te > &e) const
Check if the rectangular cuboid contains an event.
Definition types.hpp:442
Rect3_()
Definition types.hpp:392
int length
Definition types.hpp:387
This class extends cv::Size_<T> for event data. For more information, please refer here.
Definition types.hpp:309
bool empty() const
Check if empty.
Definition types.hpp:330
T volume() const
Compute the volume.
Definition types.hpp:339
Size3_(T w, T h, T l)
Definition types.hpp:324
int length
Definition types.hpp:311
Size3_()
Definition types.hpp:316
This class defines a circle given its center and radius.
Definition types.hpp:482
Circ_()
Definition types.hpp:489
Circ_(const cv::Point_< T > center, const T radius)
Definition types.hpp:496
bool contains(const Event_< Te > &e) const
Check if the circle contains an event.
Definition types.hpp:513
bool empty() const
Check if empty.
Definition types.hpp:502
Event_< long > Eventl
Definition types.hpp:228
Rect2_< double > Rect2d
Definition types.hpp:367
Event_< int > Eventi
Definition types.hpp:227
constexpr bool POSITIVE
Definition types.hpp:21
Size2_< double > Size2d
Definition types.hpp:292
Size2i Size2
Definition types.hpp:293
Rect2i Rect2
Definition types.hpp:368
Rect2 Rect
Definition types.hpp:369
Event_< double > Eventd
Definition types.hpp:230
Size2_< int > Size2i
Definition types.hpp:289
Circ_< long > Circl
Definition types.hpp:526
Rect3_< int > Rect3i
Definition types.hpp:463
Size3i Size3
Definition types.hpp:347
Circ_< double > Circd
Definition types.hpp:528
Rect3i Rect3
Definition types.hpp:467
Circ_< int > Circi
Definition types.hpp:525
cv::Size_< T > Size2_
This class extends cv::Size_<T> for event data. For more information, please refer here.
Definition types.hpp:288
Rect3_< double > Rect3d
Definition types.hpp:466
Size2 Size
Definition types.hpp:294
cv::Rect_< T > Rect2_
This class extends cv::Rect_<T> for event data. For more information, please refer here.
Definition types.hpp:363
Circ_< float > Circf
Definition types.hpp:527
AugmentedEvent_< float > AugmentedEventf
Definition types.hpp:270
AugmentedEventi AugmentedEvent
Definition types.hpp:272
Rect2_< long > Rect2l
Definition types.hpp:365
Rect3_< float > Rect3f
Definition types.hpp:465
AugmentedEvent_< long > AugmentedEventl
Definition types.hpp:269
Size3_< float > Size3f
Definition types.hpp:345
Rect2_< int > Rect2i
Definition types.hpp:364
AugmentedEvent_< int > AugmentedEventi
Definition types.hpp:268
Size3_< double > Size3d
Definition types.hpp:346
constexpr bool NEGATIVE
Definition types.hpp:22
Event_< float > Eventf
Definition types.hpp:229
Size3_< long > Size3l
Definition types.hpp:344
Circi Circ
Definition types.hpp:529
AugmentedEvent_< double > AugmentedEventd
Definition types.hpp:271
Rect2_< float > Rect2f
Definition types.hpp:366
Size2_< long > Size2l
Definition types.hpp:290
Size2_< float > Size2f
Definition types.hpp:291
Eventi Event
Definition types.hpp:231
Size3_< int > Size3i
Definition types.hpp:343
Rect3_< long > Rect3l
Definition types.hpp:464