event-driven
vCodec.h
1 /*
2  * Copyright (C) 2017 Event-driven Perception for Robotics
3  * Author: arren.glover@iit.it
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef __VCODEC__
20 #define __VCODEC__
21 
22 #include "event-driven/vtsHelper.h"
23 #include <memory>
24 #include <deque>
25 #include <math.h>
26 #include <vector>
27 #include <iostream>
28 
29 namespace ev {
30 
31 #define IS_SKIN(x) x&0x01000000
32 #define IS_SAMPLE(x) x&0x00804000
33 #define IS_SSA(x) x&0x00004000
34 #define IS_SSV(x) x&0x00800000
35 #define IS_IMUSAMPLE(x) x&0x02000000
36 #define IS_AUDIO(x) x&0x04000000
37 
38 //macros
39 class vEvent;
40 
42 template<typename V = vEvent> using event = std::shared_ptr<V>;
44 template<typename V1, typename V2> inline event<V1> as_event(event<V2> orig_event) {
45  return std::dynamic_pointer_cast<V1>(orig_event);
46 }
48 template<typename V1, typename V2> inline event<V1> is_event(event<V2> orig_event) {
49  return std::static_pointer_cast<V1>(orig_event);
50 }
52 template<typename V> event<V> inline make_event(void) {
53  return std::make_shared<V>();
54 }
57 template<typename V> inline V* read_as(const event<> &orig_event) {
58  return (V *)orig_event.get();
59 }
62 template<typename V1, typename V2> event<V1> make_event(event<V2> orig_event) {
63  return std::make_shared<V1>(*(orig_event.get()));
64 }
66 using vQueue = std::deque< event<vEvent> >;
67 
69 void qsort(vQueue &q, bool respectWraps = false);
70 
72 event<> createEvent(const std::string &type);
73 
75 unsigned int packetSize(const std::string &type);
76 
78 enum { VLEFT = 0, VRIGHT = 1 } ;
79 
80 //event declarations
82 class vEvent
83 {
84 public:
85  static const std::string tag;
86  unsigned int stamp:31;
87 
88  vEvent();
89  virtual ~vEvent();
90 
91  virtual event<> clone();
92  virtual void encode(yarp::os::Bottle &b) const;
93  virtual void encode(std::vector<int32_t> &b, unsigned int &pos) const;
94  virtual bool decode(const yarp::os::Bottle &packet, size_t &pos);
95  virtual void decode(const int32_t *&data);
96  virtual yarp::os::Property getContent() const;
97  virtual std::string getType() const;
98  virtual int getChannel() const;
99  virtual void setChannel();
100 };
101 
103 class AddressEvent : public vEvent
104 {
105 public:
106  static const std::string tag;
107 
108  union
109  {
110  uint32_t _coded_data;
111  struct {
112  unsigned int polarity:1;
113  unsigned int x:9;
114  unsigned int _xfill:2;
115  unsigned int y:8;
116  unsigned int _yfill:2;
117  unsigned int channel:1;
118  unsigned int type:1;
119  unsigned int skin:1;
120  unsigned int _fill:7;
121  };
122  };
123 
124  AddressEvent();
125  AddressEvent(const vEvent &v);
126  AddressEvent(const AddressEvent &v);
127 
128  virtual event<> clone();
129  virtual void encode(yarp::os::Bottle &b) const;
130  virtual void encode(std::vector<int32_t> &b, unsigned int &pos) const;
131  virtual bool decode(const yarp::os::Bottle &packet, size_t &pos);
132  virtual void decode(const int32_t *&data);
133  virtual yarp::os::Property getContent() const;
134  virtual std::string getType() const;
135  virtual int getChannel() const;
136  virtual void setChannel(const int channel);
137 };
138 using AE = AddressEvent;
139 
141 class SkinEvent : public vEvent
142 {
143 public:
144  static const std::string tag;
145 
146  union
147  {
148  uint32_t _skei;
149  struct {
150  unsigned int polarity:1;
151  unsigned int taxel:10;
152  unsigned int _reserved1:2;
153  unsigned int cross_base:1;
154  unsigned int _sample:1;
155  unsigned int _error:1;
156  unsigned int body_part:3;
157  unsigned int _reserved2:3;
158  unsigned int side:1;
159  unsigned int type:1;
160  unsigned int skin:1;
161  };
162  };
163 
164  SkinEvent();
165  SkinEvent(const vEvent &v);
166  SkinEvent(const SkinEvent &v);
167 
168  virtual event<> clone();
169  virtual void encode(yarp::os::Bottle &b) const;
170  virtual void encode(std::vector<int32_t> &b, unsigned int &pos) const;
171  virtual bool decode(const yarp::os::Bottle &packet, size_t &pos);
172  virtual void decode(const int32_t *&data);
173  virtual yarp::os::Property getContent() const;
174  virtual std::string getType() const;
175 
176 };
177 
179 class SkinSample : public SkinEvent
180 {
181 public:
182  static const std::string tag;
183 
184  unsigned int _ts:31;
185  unsigned int value:16;
186 
187  SkinSample();
188  SkinSample(const vEvent &v);
189  SkinSample(const SkinSample &v);
190 
191  virtual event<> clone();
192  virtual void encode(yarp::os::Bottle &b) const;
193  virtual void encode(std::vector<int32_t> &b, unsigned int &pos) const;
194  virtual bool decode(const yarp::os::Bottle &packet, size_t &pos);
195  virtual void decode(const int32_t *&data);
196  virtual yarp::os::Property getContent() const;
197  virtual std::string getType() const;
198 
199 };
200 
202 class FlowEvent : public AddressEvent
203 {
204 public:
205  static const std::string tag;
206  union {
207  uint32_t _fei[2];
208  struct {
209  float vx;
210  float vy;
211  };
212  };
213 
214 
215  FlowEvent();
216  FlowEvent(const vEvent &v);
217  FlowEvent(const FlowEvent &v);
218 
219  virtual event<> clone();
220  virtual void encode(yarp::os::Bottle &b) const;
221  virtual void encode(std::vector<int32_t> &b, unsigned int &pos) const;
222  virtual bool decode(const yarp::os::Bottle &packet, size_t &pos);
223  virtual void decode(const int32_t *&data);
224  virtual yarp::os::Property getContent() const;
225  virtual std::string getType() const;
226 
227  int getDeath() const;
228 };
229 
231 class LabelledAE : public AddressEvent
232 {
233 public:
234  static const std::string tag;
235  int ID;
236 
237  LabelledAE();
238  LabelledAE(const vEvent &v);
239  LabelledAE(const LabelledAE &v);
240 
241  virtual event<> clone();
242  virtual void encode(yarp::os::Bottle &b) const;
243  virtual void encode(std::vector<int32_t> &b, unsigned int &pos) const;
244  virtual bool decode(const yarp::os::Bottle &packet, size_t &pos);
245  virtual void decode(const int32_t *&data);
246  virtual yarp::os::Property getContent() const;
247  virtual std::string getType() const;
248 };
249 
251 class GaussianAE : public LabelledAE
252 {
253 public:
254  static const std::string tag;
255  union {
256  uint32_t _gaei[3];
257  struct {
258  float sigx;
259  float sigy;
260  float sigxy;
261  };
262  };
263 
264  GaussianAE();
265  GaussianAE(const vEvent &v);
266  GaussianAE(const GaussianAE &v);
267 
268  virtual event<> clone();
269  virtual void encode(yarp::os::Bottle &b) const;
270  virtual void encode(std::vector<int32_t> &b, unsigned int &pos) const;
271  virtual bool decode(const yarp::os::Bottle &packet, size_t &pos);
272  virtual void decode(const int32_t *&data);
273  virtual yarp::os::Property getContent() const;
274  virtual std::string getType() const;
275 };
276 
278 class IMUevent : public vEvent
279 {
280 public:
281  static const std::string tag;
282 
283  union
284  {
285  uint32_t _coded_data;
286  struct {
287  int value:16;
288  unsigned int sensor:4;
289  unsigned int _r1:2;
290  unsigned int channel:1;
291  unsigned int type:1;
292  unsigned int _r2:8;
293  };
294  };
295 
296  const static unsigned int _max_value = 32768;
297 
298  IMUevent();
299  IMUevent(const vEvent &v);
300  IMUevent(const IMUevent &v);
301 
302  virtual event<> clone();
303  virtual void encode(yarp::os::Bottle &b) const;
304  virtual void encode(std::vector<int32_t> &b, unsigned int &pos) const;
305  virtual bool decode(const yarp::os::Bottle &packet, size_t &pos);
306  virtual void decode(const int32_t *&data);
307  virtual yarp::os::Property getContent() const;
308  virtual std::string getType() const;
309  virtual int getChannel() const { return channel;}
310  virtual void setChannel(const int channel) { this->channel = channel;}
311 };
312 
314 template <class T> size_t countEvents(const T &q) { return q.size(); }
315 //template <> size_t countEvents<vQueue>(const T &q) { return q.size(); }
316 
318 template <typename T> inline int countTime(const T &q, int &p_time)
319 {
320  if(!p_time) p_time = q.front().stamp;
321  int dt = q.back().stamp - q.front().stamp;
322  p_time = q.back().stamp;
323  if(dt < 0) dt += vtsHelper::max_stamp;
324  return dt;
325 }
326 
327 template <> inline int countTime<vQueue> (const vQueue &q, int &p_time)
328 {
329  if(!p_time) p_time = q.front()->stamp;
330  int dt = q.back()->stamp - q.front()->stamp;
331  p_time = q.back()->stamp;
332  if(dt < 0) dt += vtsHelper::max_stamp;
333  return dt;
334 }
335 
336 template <> inline int countTime< std::vector<int32_t> > (const std::vector<int32_t> &q, int &p_time)
337 {
338  if(!p_time) p_time = q[0] & vtsHelper::max_stamp;
339  int dt = (q[q.size()-2] & vtsHelper::max_stamp) - p_time;
340  p_time = q[q.size()-2] & vtsHelper::max_stamp;
341  if(dt < 0) dt += vtsHelper::max_stamp;
342  return dt;
343 }
344 
345 
346 
347 }
348 
349 #endif
350 
351 
Definition: vBottle.h:29
an AddressEvent with a velocity in visual space
Definition: vCodec.h:202
an event with a pixel location, camera number and polarity
Definition: vCodec.h:103
static unsigned int max_stamp
the maximum value of the timestamp before a wrap occurs
Definition: vtsHelper.h:39
an event with a taxel location, body-part and polarity
Definition: vCodec.h:179
base event class which defines the time the event occurs
Definition: vCodec.h:82
an AddressEvent with an ID or class label
Definition: vCodec.h:231
an event with a taxel location, body-part and polarity
Definition: vCodec.h:141
a LabelledAE with parameters that define a 2D gaussian
Definition: vCodec.h:251
an event with a pixel location, camera number and polarity
Definition: vCodec.h:278