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
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 <ostream>
17#include <string>
18
19namespace ev {
20constexpr bool POSITIVE = true;
21constexpr bool NEGATIVE = false;
22
23enum class Stereo : char {
24 LEFT = 'L',
25 RIGHT = 'R'
26};
27
28enum DistanceTypes : uint8_t {
29 DISTANCE_NORM_INF = cv::NORM_INF,
30 DISTANCE_NORM_L1 = cv::NORM_L1,
31 DISTANCE_NORM_L2 = cv::NORM_L2,
32 DISTANCE_NORM_L2SQR = cv::NORM_L2SQR,
33 DISTANCE_NORM_MANHATTAN = DISTANCE_NORM_L1,
34 DISTANCE_NORM_EUCLIDEAN = DISTANCE_NORM_L2,
35 DISTANCE_FLAG_SPATIAL = 0b00010000,
36 DISTANCE_FLAG_TEMPORAL = 0b00100000,
37 DISTANCE_FLAG_SPATIOTEMPORAL = 0b01000000,
38 DISTANCE_FLAG_3D = DISTANCE_FLAG_SPATIOTEMPORAL,
39 DISTANCE_FLAG_2D = DISTANCE_FLAG_SPATIAL,
40};
41
54template <typename T>
55class Event_ : public cv::Point_<T> {
56public:
57 double t;
58 bool p;
59
63 Event_() : cv::Point_<T>(), t{0}, p{POSITIVE} {};
64
66 ~Event_() = default;
68
72 Event_(const Event_<T> &) = default;
73
77 Event_(Event_<T> &&) noexcept = default;
78
84 Event_(const T x, const T y) : cv::Point_<T>(x, y), t{0}, p{POSITIVE} {};
85
90 explicit Event_(const cv::Point_<T> &pt) : cv::Point_<T>(pt), t{0}, p{POSITIVE} {};
91
99 Event_(const T x, const T y, const double t, const bool p) : cv::Point_<T>(x, y), t{t}, p{p} {};
100
107 Event_(const cv::Point_<T> &pt, const double t, const bool p) : cv::Point_<T>(pt), t{t}, p{p} {};
108
112 Event_<T> &operator=(const Event_<T> &) = default;
113
117 Event_<T> &operator=(const cv::Point_<T> &p) {
118 Event_<T>::x = p.x;
119 Event_<T>::y = p.y;
120 return *this;
121 }
122
126 Event_<T> &operator=(Event_<T> &&) noexcept = default;
127
131 [[nodiscard]] inline bool operator==(const Event_<T> &e) const {
132 return (Event_<T>::x == e.x) && (Event_<T>::y == e.y) && (t == e.t) && (p == e.p);
133 }
134
138 [[nodiscard]] inline bool operator==(const cv::Point_<T> &pt) const {
139 return (Event_<T>::x == pt.x) && (Event_<T>::y == pt.y);
140 }
141
145 [[nodiscard]] inline bool operator==(const cv::Point3_<T> &pt) const {
146 return (Event_<T>::x == pt.x) && (Event_<T>::y == pt.y) && (Event_<T>::t == pt.z);
147 }
148
152 [[nodiscard]] inline bool operator<(const Event_<T> &e) const {
153 return Event_<T>::t < e.t;
154 }
155
159 template <typename U>
160 [[nodiscard]] inline operator cv::Point_<U>() const {
161 return {static_cast<U>(Event_<T>::x), static_cast<U>(Event_<T>::y)};
162 }
163
167 template <typename U>
168 [[nodiscard]] inline operator cv::Point3_<U>() const {
169 return {static_cast<U>(Event_<T>::x), static_cast<U>(Event_<T>::y), static_cast<U>(Event_<T>::t)};
170 }
171
179 [[nodiscard]] inline double distance(const Event_<T> &e, const uint8_t type = DISTANCE_NORM_L2 | DISTANCE_FLAG_SPATIAL) const {
180 if(static_cast<bool>(type & DISTANCE_FLAG_SPATIOTEMPORAL)) {
181 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);
182 }
183 if(static_cast<bool>(type & DISTANCE_FLAG_SPATIAL) || !static_cast<bool>(type & 0xF0)) {
184 return cv::norm(cv::Matx<T, 2, 1>(Event_<T>::x - e.x, Event_<T>::y - e.y), type & 0x0F);
185 }
186 if(static_cast<bool>(type & DISTANCE_FLAG_TEMPORAL)) {
187 return Event_<T>::t - e.t;
188 }
189 logger::error("Bad distance option");
190 return 0.0;
191 }
192
199 [[nodiscard]] friend std::ostream &operator<<(std::ostream &os, const Event_<T> &e) {
200 os << std::string("(" + std::to_string(e.x) + "," + std::to_string(e.y) + ") " + std::to_string(e.t) + (e.p ? " [+]" : " [-]"));
201 return os;
202 }
203};
208using Event = Eventi;
209
222template <typename T>
223class AugmentedEvent_ : public Event_<T> {
224 using Event_<T>::Event_;
225
226public:
227 double weight{1};
228 double depth{0};
229 Stereo stereo{Stereo::LEFT};
230
237 [[nodiscard]] friend std::ostream &operator<<(std::ostream &os, const AugmentedEvent_<T> &e) {
238 os << std::string("(" + std::to_string(e.x) + "," + std::to_string(e.y) + ") " + std::to_string(e.t) + (e.p ? " [+]" : " [-]"));
239 os << " w=" + std::to_string(e.weight);
240 os << " d=" + std::to_string(e.depth);
241 os << " s=" + (e.stereo == Stereo::LEFT ? std::string("LEFT") : std::string("RIGHT"));
242 return os;
243 }
244};
250
264template <typename T>
265using Size2_ = cv::Size_<T>;
270using Size2 = Size2i;
271using Size = Size2;
272
285template <typename T>
286class Size3_ : public cv::Size_<T> {
287public:
289
293 Size3_() : cv::Size_<T>(), length{0} {};
294
301 Size3_(T w, T h, T l) : cv::Size_<T>(w, h), length{l} {};
302
307 [[nodiscard]] inline bool empty() const {
308 return cv::Size_<T>::width <= 0 || cv::Size_<T>::height <= 0 || length <= 0;
309 }
310
316 [[nodiscard]] inline T volume() const {
317 return cv::Size_<T>::width * cv::Size_<T>::height * length;
318 }
319};
324using Size3 = Size3i;
325
339template <typename T>
340using Rect2_ = cv::Rect_<T>;
345using Rect2 = Rect2i;
346using Rect = Rect2;
347
360template <typename T>
361class Rect3_ : public cv::Rect_<T> {
362public:
363 T t;
365
369 Rect3_() : cv::Rect_<T>(), t{0}, length{0} {};
370
380 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} {};
381
388 Rect3_(const Rect2_<T> &rect, const T t, const T l) : cv::Rect_<T>(rect), t{t}, length{l} {};
389
395 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} {};
396
402 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} {};
403
408 [[nodiscard]] inline bool empty() const {
409 return Rect3_<T>::width <= 0 || Rect3_<T>::height <= 0 || length <= 0;
410 }
411
418 template <typename Te>
419 [[nodiscard]] inline bool contains(const Event_<Te> &e) const {
420 return cv::Rect_<T>::contains(e) && e.t >= t && e.t < t + length;
421 }
422
427 [[nodiscard]] inline Size3_<T> size() const {
429 }
430
436 [[nodiscard]] inline T volume() const {
438 }
439};
444using Rect3 = Rect3i;
445
458template <typename T>
459struct Circ_ {
460 cv::Point_<T> center;
461 T radius;
462
466 Circ_() : center{0, 0}, radius{0} {};
467
473 Circ_(const cv::Point_<T> center, const T radius) : center{center}, radius{std::max(radius, static_cast<T>(0))} {}
474
479 [[nodiscard]] inline bool empty() const {
480 return radius <= 0;
481 }
482
489 template <typename Te>
490 [[nodiscard]] inline bool contains(const Event_<Te> &e) const {
491 return !empty() && pow(center.x - e.x, 2) + pow(center.y - e.y, 2) <= radius * radius;
492 }
493
494 [[nodiscard]] inline cv::Size size() const {
495 return {radius, radius};
496 }
497
498 [[nodiscard]] inline double area() const {
499 return M_PI * radius * radius;
500 }
501};
506using Circ = Circi;
507
508} // namespace ev
509
510#endif // OPENEV_CORE_TYPES_HPP
This class extends Event to include: weigth, depth, and stereo.
Definition types.hpp:223
Stereo stereo
Definition types.hpp:229
double depth
Definition types.hpp:228
friend std::ostream & operator<<(std::ostream &os, const AugmentedEvent_< T > &e)
Overload of << operator.
Definition types.hpp:237
double weight
Definition types.hpp:227
This class extends cv::Point_<T> for event data. For more information, please refer here.
Definition types.hpp:55
bool operator<(const Event_< T > &e) const
Definition types.hpp:152
Event_(const cv::Point_< T > &pt, const double t, const bool p)
Definition types.hpp:107
Event_< T > & operator=(const cv::Point_< T > &p)
Definition types.hpp:117
bool p
Definition types.hpp:58
bool operator==(const cv::Point3_< T > &pt) const
Definition types.hpp:145
Event_< T > & operator=(Event_< T > &&) noexcept=default
bool operator==(const cv::Point_< T > &pt) const
Definition types.hpp:138
Event_()
Definition types.hpp:63
Event_(const T x, const T y, const double t, const bool p)
Definition types.hpp:99
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:179
Event_(const Event_< T > &)=default
Event_(const cv::Point_< T > &pt)
Definition types.hpp:90
friend std::ostream & operator<<(std::ostream &os, const Event_< T > &e)
Overload of << operator.
Definition types.hpp:199
double t
Definition types.hpp:57
This class extends cv::Rect_<T> for event data. For more information, please refer here.
Definition types.hpp:361
Rect3_(const T x, const T y, const T t, const T w, const T h, const T l)
Definition types.hpp:380
Rect3_(const Rect2_< T > &rect, const T t, const T l)
Definition types.hpp:388
Size3_< T > size() const
Size of the rectangular cuboid.
Definition types.hpp:427
T volume() const
Compute the volume.
Definition types.hpp:436
bool empty() const
Check if empty.
Definition types.hpp:408
Rect3_(const cv::Point3_< T > &pt1, const cv::Point3_< T > &pt2)
Definition types.hpp:402
Rect3_(const cv::Point3_< T > &pt, const Size3_< T > sz)
Definition types.hpp:395
int t
Definition types.hpp:363
bool contains(const Event_< Te > &e) const
Check if the rectangular cuboid contains an event.
Definition types.hpp:419
Rect3_()
Definition types.hpp:369
int length
Definition types.hpp:364
This class extends cv::Size_<T> for event data. For more information, please refer here.
Definition types.hpp:286
bool empty() const
Check if empty.
Definition types.hpp:307
T volume() const
Compute the volume.
Definition types.hpp:316
Size3_(T w, T h, T l)
Definition types.hpp:301
int length
Definition types.hpp:288
Size3_()
Definition types.hpp:293
Logger utility.
void error(const char *message, const bool assert_condition=false)
Log message at error level.
Definition logger.hpp:38
This class defines a circle given its center and radius.
Definition types.hpp:459
Circ_()
Definition types.hpp:466
Circ_(const cv::Point_< T > center, const T radius)
Definition types.hpp:473
bool contains(const Event_< Te > &e) const
Check if the circle contains an event.
Definition types.hpp:490
bool empty() const
Check if empty.
Definition types.hpp:479
Event_< long > Eventl
Definition types.hpp:205
Rect2_< double > Rect2d
Definition types.hpp:344
Event_< int > Eventi
Definition types.hpp:204
constexpr bool POSITIVE
Definition types.hpp:20
Size2_< double > Size2d
Definition types.hpp:269
Size2i Size2
Definition types.hpp:270
Rect2i Rect2
Definition types.hpp:345
Rect2 Rect
Definition types.hpp:346
Event_< double > Eventd
Definition types.hpp:207
Size2_< int > Size2i
Definition types.hpp:266
Circ_< long > Circl
Definition types.hpp:503
Rect3_< int > Rect3i
Definition types.hpp:440
Size3i Size3
Definition types.hpp:324
Circ_< double > Circd
Definition types.hpp:505
Rect3i Rect3
Definition types.hpp:444
Circ_< int > Circi
Definition types.hpp:502
cv::Size_< T > Size2_
This class extends cv::Size_<T> for event data. For more information, please refer here.
Definition types.hpp:265
Rect3_< double > Rect3d
Definition types.hpp:443
Size2 Size
Definition types.hpp:271
cv::Rect_< T > Rect2_
This class extends cv::Rect_<T> for event data. For more information, please refer here.
Definition types.hpp:340
Circ_< float > Circf
Definition types.hpp:504
AugmentedEvent_< float > AugmentedEventf
Definition types.hpp:247
AugmentedEventi AugmentedEvent
Definition types.hpp:249
Rect2_< long > Rect2l
Definition types.hpp:342
Rect3_< float > Rect3f
Definition types.hpp:442
AugmentedEvent_< long > AugmentedEventl
Definition types.hpp:246
Size3_< float > Size3f
Definition types.hpp:322
Rect2_< int > Rect2i
Definition types.hpp:341
AugmentedEvent_< int > AugmentedEventi
Definition types.hpp:245
Size3_< double > Size3d
Definition types.hpp:323
constexpr bool NEGATIVE
Definition types.hpp:21
Event_< float > Eventf
Definition types.hpp:206
Size3_< long > Size3l
Definition types.hpp:321
Circi Circ
Definition types.hpp:506
AugmentedEvent_< double > AugmentedEventd
Definition types.hpp:248
Rect2_< float > Rect2f
Definition types.hpp:343
Size2_< long > Size2l
Definition types.hpp:267
Size2_< float > Size2f
Definition types.hpp:268
Eventi Event
Definition types.hpp:208
Size3_< int > Size3i
Definition types.hpp:320
Rect3_< long > Rect3l
Definition types.hpp:441