CARLA
DVSEventArraySerializer.h
Go to the documentation of this file.
1 // Copyright (c) 2020 Robotics and Perception Group (GPR)
2 // University of Zurich and ETH Zurich
3 //
4 // This work is licensed under the terms of the MIT license.
5 // For a copy, see <https://opensource.org/licenses/MIT>.
6 
7 #pragma once
8 
9 #include "carla/Memory.h"
10 #include "carla/sensor/RawData.h"
12 
13 #include <cstdint>
14 #include <cstring>
15 
16 namespace carla {
17 namespace sensor {
18 
19  class SensorData;
20 
21 namespace s11n {
22 
23  /// Serializes events array generated by DVS camera sensors.
25  public:
26 
27 #pragma pack(push, 1)
28  struct DVSHeader {
29  uint32_t width;
30  uint32_t height;
31  float fov_angle;
32  };
33 #pragma pack(pop)
34 
35  constexpr static auto header_offset = sizeof(DVSHeader);
36  using DVSEventArray = std::vector<data::DVSEvent>;
37 
38  static const DVSHeader &DeserializeHeader(const RawData &data) {
39  return *reinterpret_cast<const DVSHeader *>(data.begin());
40  }
41 
42  template <typename Sensor>
43  static Buffer Serialize(const Sensor &sensor, const DVSEventArray &events, Buffer &&output);
44 
46  };
47 
48  template <typename Sensor>
49  inline Buffer DVSEventArraySerializer::Serialize(const Sensor &sensor, const DVSEventArray &events, Buffer &&output) {
50  DEBUG_ASSERT(events.size() > sizeof(DVSHeader));
51  DVSHeader header = {
52  sensor.GetImageWidth(),
53  sensor.GetImageHeight(),
54  sensor.GetFOVAngle(),
55  };
56 
57  /// Reset the output buffer
58  output.reset(sizeof(DVSHeader) + (events.size() * sizeof(data::DVSEvent)));
59 
60  /// Pointer to data in buffer
61  unsigned char *it = output.data();
62 
63  /// Copy the header into the output buffer
64  std::memcpy(it, reinterpret_cast<const void *>(&header), sizeof(header));
65  it += sizeof(DVSHeader);
66 
67  /// Copy the events into the output buffer
68  for (auto e : events) {
69  std::memcpy(it, reinterpret_cast<const void *>(&e), sizeof(data::DVSEvent));
70  it += sizeof(data::DVSEvent);
71  }
72  return std::move(output);
73  }
74 
75 } // namespace s11n
76 } // namespace sensor
77 } // namespace carla
boost::shared_ptr< T > SharedPtr
Use this SharedPtr (boost::shared_ptr) to keep compatibility with boost::python, but it would be nice...
Definition: Memory.h:20
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
auto begin() noexcept
Begin iterator to the data generated by the sensor.
Definition: RawData.h:52
#define DEBUG_ASSERT(predicate)
Definition: Debug.h:66
static const DVSHeader & DeserializeHeader(const RawData &data)
static SharedPtr< SensorData > Deserialize(RawData &&data)
static Buffer Serialize(const Sensor &sensor, const DVSEventArray &events, Buffer &&output)
A piece of raw data.
Definition: carla/Buffer.h:42
Wrapper around the raw data generated by a sensor plus some useful meta-information.
Definition: RawData.h:21
Serializes events array generated by DVS camera sensors.