CARLA
SemanticLidarData.h
Go to the documentation of this file.
1 // Copyright (c) 2020 Computer Vision Center (CVC) at the Universitat Autonoma
2 // de Barcelona (UAB).
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/rpc/Location.h"
10 
11 #include <cstdint>
12 #include <vector>
13 #include <numeric>
14 
15 namespace carla {
16 
17 namespace ros2 {
18  class ROS2;
19 }
20 
21 namespace sensor {
22 
23 namespace s11n {
24  class SemanticLidarSerializer;
25  class SemanticLidarHeaderView;
26 }
27 
28 namespace data {
29 
30  /// Helper class to store and serialize the data generated by a RawLidar.
31  ///
32  /// The header of a Lidar measurement consists of an array of uint32_t's in
33  /// the following layout
34  ///
35  /// {
36  /// Horizontal angle (float),
37  /// Channel count,
38  /// Point count of channel 0,
39  /// ...
40  /// Point count of channel n,
41  /// }
42  ///
43  /// The points are stored in an array of detections, each detection consist in
44  /// four floats, the point detected and the angle between the casted ray and
45  /// the normal of the hitted object, and two unsigned integers, the index
46  /// and the semantic tag of the hitted object
47  ///
48  /// {
49  /// X0, Y0, Z0, Cos(TH0), idx_0, tag_0
50  /// ...
51  /// Xn, Yn, Zn, Cos(THn), idx_n, tag_n
52  /// }
53  ///
54 
55  #pragma pack(push, 1)
57  public:
58  geom::Location point{};
59  float cos_inc_angle{};
60  uint32_t object_idx{};
61  uint32_t object_tag{};
62 
63  SemanticLidarDetection() = default;
64 
65  SemanticLidarDetection(float x, float y, float z, float cosTh, uint32_t idx, uint32_t tag) :
66  point(x, y, z), cos_inc_angle{cosTh}, object_idx{idx}, object_tag{tag} { }
67  SemanticLidarDetection(geom::Location p, float cosTh, uint32_t idx, uint32_t tag) :
68  point(p), cos_inc_angle{cosTh}, object_idx{idx}, object_tag{tag} { }
69 
70  void WritePlyHeaderInfo(std::ostream& out) const{
71  out << "property float32 x\n" \
72  "property float32 y\n" \
73  "property float32 z\n" \
74  "property float32 CosAngle\n" \
75  "property uint32 ObjIdx\n" \
76  "property uint32 ObjTag";
77  }
78 
79  void WriteDetection(std::ostream& out) const{
80  out << point.x << ' ' << point.y << ' ' << point.z << ' ' \
81  << cos_inc_angle << ' ' << object_idx << ' ' << object_tag;
82  }
83  };
84  #pragma pack(pop)
85 
87  static_assert(sizeof(float) == sizeof(uint32_t), "Invalid float size");
88 
89  protected:
90  enum Index : size_t {
93  SIZE
94  };
95 
96  public:
97  explicit SemanticLidarData(uint32_t ChannelCount = 0u)
98  : _header(Index::SIZE + ChannelCount, 0u) {
99  _header[Index::ChannelCount] = ChannelCount;
100  }
101 
102  SemanticLidarData &operator=(SemanticLidarData &&) = default;
103 
104  virtual ~SemanticLidarData() {}
105 
106  float GetHorizontalAngle() const {
107  return reinterpret_cast<const float &>(_header[Index::HorizontalAngle]);
108  }
109 
110  void SetHorizontalAngle(float angle) {
111  std::memcpy(&_header[Index::HorizontalAngle], &angle, sizeof(uint32_t));
112  }
113 
114  uint32_t GetChannelCount() const {
115  return _header[Index::ChannelCount];
116  }
117 
118  virtual void ResetMemory(std::vector<uint32_t> points_per_channel) {
119  DEBUG_ASSERT(GetChannelCount() > points_per_channel.size());
120  std::memset(_header.data() + Index::SIZE, 0, sizeof(uint32_t) * GetChannelCount());
121 
122  uint32_t total_points = static_cast<uint32_t>(
123  std::accumulate(points_per_channel.begin(), points_per_channel.end(), 0));
124 
125  _ser_points.clear();
126  _ser_points.reserve(total_points);
127  }
128 
129  virtual void WriteChannelCount(std::vector<uint32_t> points_per_channel) {
130  for (auto idxChannel = 0u; idxChannel < GetChannelCount(); ++idxChannel)
131  _header[Index::SIZE + idxChannel] = points_per_channel[idxChannel];
132  }
133 
134  virtual void WritePointSync(SemanticLidarDetection &detection) {
135  _ser_points.emplace_back(detection);
136  }
137 
138  protected:
139  std::vector<uint32_t> _header;
141 
142  private:
143  std::vector<SemanticLidarDetection> _ser_points;
144 
147  friend class carla::ros2::ROS2;
148 
149  };
150 
151 } // namespace s11n
152 } // namespace sensor
153 } // namespace carla
SemanticLidarData(uint32_t ChannelCount=0u)
void WriteDetection(std::ostream &out) const
virtual void WriteChannelCount(std::vector< uint32_t > points_per_channel)
static const FString SIZE
Helper class to store and serialize the data generated by a RawLidar.
A view over the header of a Lidar measurement.
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
std::vector< SemanticLidarDetection > _ser_points
#define DEBUG_ASSERT(predicate)
Definition: Debug.h:66
Serializes the data generated by Lidar sensors.
SemanticLidarDetection(geom::Location p, float cosTh, uint32_t idx, uint32_t tag)
virtual void ResetMemory(std::vector< uint32_t > points_per_channel)
virtual void WritePointSync(SemanticLidarDetection &detection)
void WritePlyHeaderInfo(std::ostream &out) const
SemanticLidarDetection(float x, float y, float z, float cosTh, uint32_t idx, uint32_t tag)