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 {
22[[maybe_unused]] constexpr bool USING_TYPES_HPP = true;
23
25template <typename T>
26[[nodiscard]] inline int round_(const T x) {
27 const double value = x;
28 return static_cast<int>(value + std::copysign(0.5, value));
29}
31
32using TimeType = double;
33using PolarityType = bool;
34using WeightType = float;
35using DepthType = float;
36using CounterType = int16_t;
37
38constexpr PolarityType POSITIVE = static_cast<PolarityType>(true);
39constexpr PolarityType NEGATIVE = static_cast<PolarityType>(false);
40
44enum class Stereo : char {
45 LEFT = 'L',
46 RIGHT = 'R'
47};
48
59enum DistanceTypes : uint8_t {
60 DISTANCE_NORM_INF = static_cast<uint8_t>(cv::NORM_INF),
61 DISTANCE_NORM_L1 = static_cast<uint8_t>(cv::NORM_L1),
62 DISTANCE_NORM_L2 = static_cast<uint8_t>(cv::NORM_L2),
63 DISTANCE_NORM_L2SQR = static_cast<uint8_t>(cv::NORM_L2SQR),
64 DISTANCE_NORM_MANHATTAN = DISTANCE_NORM_L1,
65 DISTANCE_NORM_EUCLIDEAN = DISTANCE_NORM_L2,
66 DISTANCE_FLAG_SPATIAL = static_cast<uint8_t>(0b00010000),
67 DISTANCE_FLAG_TEMPORAL = static_cast<uint8_t>(0b00100000),
68 DISTANCE_FLAG_SPATIOTEMPORAL = static_cast<uint8_t>(0b01000000),
71};
72
85template <typename T>
86class Event_ : public cv::Point_<T> {
87 static_assert(std::is_arithmetic_v<T>, "ev::Event_: the coordinate type must be arithmetic.");
88
89public:
90 TimeType t;
91 PolarityType p;
92
96 Event_() : cv::Point_<T>(), t{0}, p{POSITIVE} {};
97
99 ~Event_() = default;
101
105 Event_(const Event_<T> &) = default;
106
110 Event_(Event_<T> &&) noexcept = default;
111
117 Event_(const T x, const T y) : cv::Point_<T>(x, y), t{0}, p{POSITIVE} {};
118
123 explicit Event_(const cv::Point_<T> &pt) : cv::Point_<T>(pt), t{0}, p{POSITIVE} {};
124
132 Event_(const T x, const T y, const TimeType t, const PolarityType p) : cv::Point_<T>(x, y), t{t}, p{p} {};
133
140 Event_(const cv::Point_<T> &pt, const TimeType t, const PolarityType p) : cv::Point_<T>(pt), t{t}, p{p} {};
141
145 Event_<T> &operator=(const Event_<T> &) = default;
146
150 template <typename U>
152 Event_<T>::x = static_cast<T>(e.x);
153 Event_<T>::y = static_cast<T>(e.y);
154 Event_<T>::t = static_cast<TimeType>(e.t);
155 Event_<T>::p = static_cast<PolarityType>(e.p);
156 return *this;
157 }
158
162 Event_<T> &operator=(const cv::Point3_<T> &p) {
163 Event_<T>::x = static_cast<T>(p.x);
164 Event_<T>::y = static_cast<T>(p.y);
165 Event_<T>::t = static_cast<TimeType>(p.z);
166 return *this;
167 }
168
172 Event_<T> &operator=(const cv::Point_<T> &p) {
173 Event_<T>::x = static_cast<T>(p.x);
174 Event_<T>::y = static_cast<T>(p.y);
175 return *this;
176 }
177
181 Event_<T> &operator=(Event_<T> &&) noexcept = default;
182
186 [[nodiscard]] inline bool operator==(const Event_<T> &e) const {
187 return (Event_<T>::x == e.x) && (Event_<T>::y == e.y) && (t == e.t) && (p == e.p);
188 }
189
193 [[nodiscard]] inline bool operator==(const cv::Point_<T> &pt) const {
194 return (Event_<T>::x == pt.x) && (Event_<T>::y == pt.y);
195 }
196
200 [[nodiscard]] inline bool operator==(const cv::Point3_<T> &pt) const {
201 return (Event_<T>::x == pt.x) && (Event_<T>::y == pt.y) && (Event_<T>::t == pt.z);
202 }
203
207 [[nodiscard]] inline bool operator<(const Event_<T> &e) const {
208 return Event_<T>::t < e.t;
209 }
210
214 template <typename U>
215 [[nodiscard]] inline explicit operator cv::Point_<U>() const {
216 return {static_cast<U>(Event_<T>::x), static_cast<U>(Event_<T>::y)};
217 }
218
222 template <typename U>
223 [[nodiscard]] inline explicit operator cv::Point3_<U>() const {
224 return {static_cast<U>(Event_<T>::x), static_cast<U>(Event_<T>::y), static_cast<U>(Event_<T>::t)};
225 }
226
234 [[nodiscard]] inline double distance(const Event_<T> &e, const uint8_t type = DISTANCE_NORM_L2 | DISTANCE_FLAG_SPATIAL) const {
235 if(static_cast<bool>(type & DISTANCE_FLAG_SPATIOTEMPORAL)) {
236 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);
237 }
238 if(static_cast<bool>(type & DISTANCE_FLAG_SPATIAL) || !static_cast<bool>(type & 0xF0U)) {
239 return cv::norm(cv::Matx<T, 2, 1>(Event_<T>::x - e.x, Event_<T>::y - e.y), type & 0x0FU);
240 }
241 if(static_cast<bool>(type & DISTANCE_FLAG_TEMPORAL)) {
242 return Event_<T>::t - e.t;
243 }
244 CV_Error(cv::Error::StsBadArg, "ev::Event_::distance: Bad distance option.");
245 }
246
253 [[nodiscard]] friend std::ostream &operator<<(std::ostream &os, const Event_<T> &e) {
254 os << std::string("(" + std::to_string(e.x) + "," + std::to_string(e.y) + ") " + std::to_string(e.t) + (e.p ? " [+]" : " [-]"));
255 return os;
256 }
257};
262using Event = Eventi;
263
276template <typename T>
277class AugmentedEvent_ : public Event_<T> {
278 using Event_<T>::Event_;
279
280public:
281 WeightType weight{1};
282 DepthType depth{0};
284
291 [[nodiscard]] friend std::ostream &operator<<(std::ostream &os, const AugmentedEvent_<T> &e) {
292 os << std::string("(" + std::to_string(e.x) + "," + std::to_string(e.y) + ") " + std::to_string(e.t) + (e.p ? " [+]" : " [-]"));
293 os << " w=" + std::to_string(e.weight);
294 os << " d=" + std::to_string(e.depth);
295 os << " s=" + (e.stereo == Stereo::LEFT ? std::string("LEFT") : std::string("RIGHT"));
296 return os;
297 }
298};
304
318template <typename T>
319using Size2_ = cv::Size_<T>;
324using Size2 = Size2i;
325using Size = Size2;
326
339template <typename T>
340class Size3_ : public cv::Size_<T> {
341public:
343
347 Size3_() : cv::Size_<T>(), length{0} {};
348
355 Size3_(T w, T h, T l) : cv::Size_<T>(w, h), length{l} {};
356
361 [[nodiscard]] inline bool empty() const {
362 return cv::Size_<T>::width <= 0 || cv::Size_<T>::height <= 0 || length <= 0;
363 }
364
370 [[nodiscard]] inline T volume() const {
371 return cv::Size_<T>::width * cv::Size_<T>::height * length;
372 }
373};
378using Size3 = Size3i;
379
393template <typename T>
394using Rect2_ = cv::Rect_<T>;
399using Rect2 = Rect2i;
400using Rect = Rect2;
401
414template <typename T>
415class Rect3_ : public cv::Rect_<T> {
416public:
417 T t;
419
423 Rect3_() : cv::Rect_<T>(), t{0}, length{0} {};
424
434 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} {};
435
442 Rect3_(const Rect2_<T> &rect, const T t, const T l) : cv::Rect_<T>(rect), t{t}, length{l} {};
443
449 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} {};
450
456 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} {};
457
462 [[nodiscard]] inline bool empty() const {
463 return Rect3_<T>::width <= 0 || Rect3_<T>::height <= 0 || length <= 0;
464 }
465
472 template <typename Te>
473 [[nodiscard]] inline bool contains(const Event_<Te> &e) const {
474 return cv::Rect_<T>::contains(e) && e.t >= t && e.t < t + length;
475 }
476
481 [[nodiscard]] inline Size3_<T> size() const {
483 }
484
490 [[nodiscard]] inline T volume() const {
492 }
493};
498using Rect3 = Rect3i;
499
512template <typename T>
513struct alignas(16) Circ_ {
514 cv::Point_<T> center;
515 T radius;
516
520 Circ_() : center{0, 0}, radius{0} {};
521
527 Circ_(const cv::Point_<T> center, const T radius) : center{center}, radius{std::max(radius, static_cast<T>(0))} {}
528
533 [[nodiscard]] inline bool empty() const {
534 return radius <= 0;
535 }
536
543 template <typename Te>
544 [[nodiscard]] inline bool contains(const Event_<Te> &e) const {
545 return !empty() && pow(center.x - e.x, 2) + pow(center.y - e.y, 2) <= radius * radius;
546 }
547
552 [[nodiscard]] inline cv::Size size() const {
553 return {radius, radius};
554 }
555
561 [[nodiscard]] inline double area() const {
562 return M_PI * radius * radius;
563 }
564};
569using Circ = Circi;
570
571} // namespace ev
572
573#endif // OPENEV_CORE_TYPES_HPP
This class extends Event to include: weigth, depth, and stereo.
Definition types.hpp:277
DepthType depth
Definition types.hpp:282
WeightType weight
Definition types.hpp:281
Stereo stereo
Definition types.hpp:283
friend std::ostream & operator<<(std::ostream &os, const AugmentedEvent_< T > &e)
Overload of << operator.
Definition types.hpp:291
This class extends cv::Point_<T> for event data. For more information, please refer here.
Definition types.hpp:86
PolarityType p
Definition types.hpp:91
bool operator<(const Event_< T > &e) const
Definition types.hpp:207
Event_< T > & operator=(const cv::Point_< T > &p)
Definition types.hpp:172
Event_(const T x, const T y, const TimeType t, const PolarityType p)
Definition types.hpp:132
bool operator==(const cv::Point3_< T > &pt) const
Definition types.hpp:200
Event_(const cv::Point_< T > &pt, const TimeType t, const PolarityType p)
Definition types.hpp:140
Event_< T > & operator=(Event_< T > &&) noexcept=default
Event_< T > & operator=(const Event_< U > &e)
Definition types.hpp:151
bool operator==(const cv::Point_< T > &pt) const
Definition types.hpp:193
TimeType t
Definition types.hpp:90
Event_()
Definition types.hpp:96
Event_< T > & operator=(const cv::Point3_< T > &p)
Definition types.hpp:162
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:234
Event_(const Event_< T > &)=default
Event_(const cv::Point_< T > &pt)
Definition types.hpp:123
friend std::ostream & operator<<(std::ostream &os, const Event_< T > &e)
Overload of << operator.
Definition types.hpp:253
This class extends cv::Rect_<T> for event data. For more information, please refer here.
Definition types.hpp:415
Rect3_(const T x, const T y, const T t, const T w, const T h, const T l)
Definition types.hpp:434
Rect3_(const Rect2_< T > &rect, const T t, const T l)
Definition types.hpp:442
Size3_< T > size() const
Size of the rectangular cuboid.
Definition types.hpp:481
T volume() const
Compute the volume.
Definition types.hpp:490
bool empty() const
Check if empty.
Definition types.hpp:462
Rect3_(const cv::Point3_< T > &pt1, const cv::Point3_< T > &pt2)
Definition types.hpp:456
Rect3_(const cv::Point3_< T > &pt, const Size3_< T > sz)
Definition types.hpp:449
int t
Definition types.hpp:417
bool contains(const Event_< Te > &e) const
Check if the rectangular cuboid contains an event.
Definition types.hpp:473
Rect3_()
Definition types.hpp:423
int length
Definition types.hpp:418
This class extends cv::Size_<T> for event data. For more information, please refer here.
Definition types.hpp:340
bool empty() const
Check if empty.
Definition types.hpp:361
T volume() const
Compute the volume.
Definition types.hpp:370
Size3_(T w, T h, T l)
Definition types.hpp:355
int length
Definition types.hpp:342
Size3_()
Definition types.hpp:347
This class defines a circle given its center and radius.
Definition types.hpp:513
Circ_()
Definition types.hpp:520
double area() const
Compute the area of the circle.
Definition types.hpp:561
Circ_(const cv::Point_< T > center, const T radius)
Definition types.hpp:527
cv::Size size() const
Bounding square of the circle as a cv::Size (radius x radius).
Definition types.hpp:552
bool contains(const Event_< Te > &e) const
Check if the circle contains an event.
Definition types.hpp:544
bool empty() const
Check if empty.
Definition types.hpp:533
Event_< long > Eventl
Definition types.hpp:259
Rect2_< double > Rect2d
Definition types.hpp:398
constexpr PolarityType POSITIVE
Definition types.hpp:38
Event_< int > Eventi
Definition types.hpp:258
Size2_< double > Size2d
Definition types.hpp:323
Size2i Size2
Definition types.hpp:324
Rect2i Rect2
Definition types.hpp:399
Rect2 Rect
Definition types.hpp:400
Event_< double > Eventd
Definition types.hpp:261
Size2_< int > Size2i
Definition types.hpp:320
Circ_< long > Circl
Definition types.hpp:566
Rect3_< int > Rect3i
Definition types.hpp:494
Size3i Size3
Definition types.hpp:378
Circ_< double > Circd
Definition types.hpp:568
Rect3i Rect3
Definition types.hpp:498
Circ_< int > Circi
Definition types.hpp:565
cv::Size_< T > Size2_
Type alias for cv::Size_<T> providing a named 2D size for event data. For more information,...
Definition types.hpp:319
Rect3_< double > Rect3d
Definition types.hpp:497
Size2 Size
Definition types.hpp:325
DistanceTypes
Norm types and dimensional flags for Event_::distance().
Definition types.hpp:59
@ DISTANCE_FLAG_SPATIOTEMPORAL
Definition types.hpp:68
@ DISTANCE_FLAG_TEMPORAL
Definition types.hpp:67
@ DISTANCE_FLAG_2D
Definition types.hpp:70
@ DISTANCE_FLAG_SPATIAL
Definition types.hpp:66
@ DISTANCE_FLAG_3D
Definition types.hpp:69
cv::Rect_< T > Rect2_
Type alias for cv::Rect_<T> providing a named 2D rectangle for event data. For more information,...
Definition types.hpp:394
Circ_< float > Circf
Definition types.hpp:567
AugmentedEvent_< float > AugmentedEventf
Definition types.hpp:301
AugmentedEventi AugmentedEvent
Definition types.hpp:303
Rect2_< long > Rect2l
Definition types.hpp:396
Rect3_< float > Rect3f
Definition types.hpp:496
AugmentedEvent_< long > AugmentedEventl
Definition types.hpp:300
Size3_< float > Size3f
Definition types.hpp:376
Rect2_< int > Rect2i
Definition types.hpp:395
Stereo
Stereo camera side identifier.
Definition types.hpp:44
@ RIGHT
Definition types.hpp:46
@ LEFT
Definition types.hpp:45
AugmentedEvent_< int > AugmentedEventi
Definition types.hpp:299
Size3_< double > Size3d
Definition types.hpp:377
Event_< float > Eventf
Definition types.hpp:260
Size3_< long > Size3l
Definition types.hpp:375
Circi Circ
Definition types.hpp:569
AugmentedEvent_< double > AugmentedEventd
Definition types.hpp:302
Rect2_< float > Rect2f
Definition types.hpp:397
Size2_< long > Size2l
Definition types.hpp:321
Size2_< float > Size2f
Definition types.hpp:322
Eventi Event
Definition types.hpp:262
Size3_< int > Size3i
Definition types.hpp:374
Rect3_< long > Rect3l
Definition types.hpp:495
constexpr PolarityType NEGATIVE
Definition types.hpp:39