CARLA
 
载入中...
搜索中...
未找到
LidarSerializer.h
浏览该文件的文档.
1// Copyright (c) 2019 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
10#include "carla/Debug.h" // 引入carla项目中的Debug.h头文件
11#include "carla/Memory.h" // 引入carla项目中的Memory.h头文件
12#include "carla/sensor/RawData.h" // 引入carla项目中sensor模块下的RawData.h头文件
13#include "carla/sensor/data/LidarData.h" // 引入carla项目中sensor模块下data子模块中的LidarData.h头文件
14
15namespace carla {
16namespace sensor {
17
18 class SensorData;
19 // 前置声明SensorData类
20
21namespace s11n {
22
23 // ===========================================================================
24 // -- LidarHeaderView --------------------------------------------------------
25 // ===========================================================================
26
27 /// A view over the header of a Lidar measurement.
29 using Index = data::LidarData::Index;
30// 使用using关键字为data::LidarData::Index类型定义一个别名Index,方便后续代码中使用这个类型来表示相关的索引,使得代码更简洁易读
31
32 public:
33 float GetHorizontalAngle() const {
34// 定义一个常成员函数GetHorizontalAngle,用于获取激光雷达数据头部中的水平角度信息,返回值为float类型,表示角度大小
35 return reinterpret_cast<const float &>(_begin[Index::HorizontalAngle]);
36// 通过reinterpret_cast进行强制类型转换,将存储在_begin数组中按照Index::HorizontalAngle索引位置处的数据转换为const float类型的引用并返回,也就是获取水平角度的值
37 }
38
39 uint32_t GetChannelCount() const {
40// 定义一个常成员函数GetChannelCount,用于获取激光雷达数据头部中的通道数量信息,返回值为无符号32位整数类型(uint32_t)
41 return _begin[Index::ChannelCount];
42 // 从_begin数组中按照Index::ChannelCount索引位置获取对应的值,这个值代表了激光雷达数据的通道数量,同样假设_begin数组按规则存储了相关信息
43 }
44
45 uint32_t GetPointCount(size_t channel) const {
46 // 定义一个常成员函数GetPointCount,用于获取指定通道(参数channel表示通道编号)下的点数量信息,返回值为无符号32位整数类型(uint32_t)
47
48 DEBUG_ASSERT(channel < GetChannelCount());
49 // 使用DEBUG_ASSERT宏进行断言检查,确保传入的通道编号channel小于通过GetChannelCount函数获取到的总通道数量,即保证通道编号是合法有效的
50
51 return _begin[Index::SIZE + channel];
52 // 从_begin数组中按照Index::SIZE + channel这个索引位置获取对应的值
53 }
54
55 private:
56 friend class LidarSerializer;
57 // 将LidarSerializer类声明为友元类,,意味着LidarSerializer类可以访问LidarHeaderView类的私有成员变量和私有成员函数,方便两者之间进行协作,通常在有紧密关联的数据操作时会这样设计
58
59 explicit LidarHeaderView(const uint32_t *begin) : _begin(begin) {
60 // 定义私有构造函数,使用explicit关键字防止隐式类型转换,它接受一个指向无符号32位整数(uint32_t)的指针begin作为参数,用于初始化成员变量
61
62 DEBUG_ASSERT(_begin != nullptr);
63// 使用DEBUG_ASSERT宏进行断言检查,确保传入的指针_begin不为空指针,保证后续通过该指针访问数据的操作是安全的
64 }
65
66 const uint32_t *_begin;
67// 定义一个私有成员变量_begin,它是一个指向无符号32位整数(uint32_t)的指针,用于指向存储激光雷达数据头部信息的内存区域,通过这个指针结合相应的索引来访问头部中的各个数据成员
68 };
69
70 // ===========================================================================
71 // -- LidarSerializer --------------------------------------------------------
72 // ===========================================================================
73
74 /// Serializes the data generated by Lidar sensors.
76 public:
77
79 // 定义一个静态成员函数DeserializeHeader,用于从给定的RawData类型的对象(传感器原始数据)中反序列化出激光雷达数据的头部视图(LidarHeaderView类型)
80
81 return LidarHeaderView{reinterpret_cast<const uint32_t *>(data.begin())};
82 // 通过reinterpret_cast进行强制类型转换,将传感器原始数据(data)的起始地址(data.begin())转换为指向无符号32位整数(const uint32_t *)的指针,然后以此为参数构造一个LidarHeaderView对象并返回,也就是从原始数据中提取出头部信息对应的视图
83 }
84
85 static size_t GetHeaderOffset(const RawData &data) {
86 // 定义一个静态成员函数GetHeaderOffset,用于获取激光雷达数据头部在整个原始数据中的偏移量
87 auto View = DeserializeHeader(data);
88// 首先调用DeserializeHeader函数从给定的原始数据(data)中获取激光雷达数据的头部视图(LidarHeaderView类型),并将结果保存在View变量中
89 return sizeof(uint32_t) * (View.GetChannelCount() + data::LidarData::Index::SIZE);
90 // 根据获取到的头部视图View中的通道数量(通过View.GetChannelCount()获取)以及data::LidarData::Index::SIZE,计算出头部的偏移量
91 }
92
93 template <typename Sensor>
94 static Buffer Serialize(
95 const Sensor &sensor,
96 const data::LidarData &data,
97 Buffer &&output);
98// 定义一个函数模板Serialize,用于将激光雷达数据进行序列化并存储到给定的Buffer类型的对象中(参数output),这个函数模板可以适用于不同类型的Sensor
99
101 // 定义一个静态成员函数Deserialize,用于从右值引用类型的RawData对象(传感器原始数据)中反序列化出一个指向SensorData类的智能指针(SharedPtr<SensorData>),这里使用右值引用可以更高效地处理临时对象等情况
102 };
103
104 // ===========================================================================
105 // -- LidarSerializer implementation -----------------------------------------
106 // ===========================================================================
107
108 template <typename Sensor>
110 const Sensor &,
111 const data::LidarData &data,
112 Buffer &&output) {
113 // 这是LidarSerializer类中Serialize函数模板的具体实现部分,用于对给定的激光雷达数据(data::LidarData类型的data)进行序列化并存储到可移动的Buffer对象(output)中,适用于特定类型的Sensor(由模板参数决定)
114
115 std::array<boost::asio::const_buffer, 2u> seq = {
116 boost::asio::buffer(data._header),
117 boost::asio::buffer(data._points)};
118// 创建一个包含两个boost::asio::const_buffer类型元素的数组seq,通过调用boost::asio::buffer函数,分别将激光雷达数据中的头部信息(data._header)和点数据(data._points)转换为const_buffer类型,以便后续进行数据复制等操作,这里假设data._header和data._points是激光雷达数据中不同部分的数据成员,并且boost::asio相关的函数用于处理数据缓冲区相关的操作
119 output.copy_from(seq);
120// 调用output对象(Buffer类型,应该实现了copy_from之类的函数用于接收数据)的copy_from函数,将数组seq中的数据(即激光雷达数据的头部和点数据)复制到output所代表的缓冲区中,完成序列化后数据的存储
121 return std::move(output);
122// 通过std::move将output对象转换为右值引用返回
123 }
124
125} // namespace s11n
126} // namespace sensor
127} // namespace carla
auto begin() const noexcept
名称范围迭代支持
#define DEBUG_ASSERT(predicate)
Definition Debug.h:68
一块原始数据。 请注意,如果需要更多容量,则会分配一个新的内存块,并 删除旧的内存块。这意味着默认情况下,缓冲区只能增长。要释放内存,使用 clear 或 pop。
void copy_from(const T &source)
将 source复制到此缓冲区。如果需要,则分配内存。
包装一个传感器生成的原始数据以及一些有用的元信息。
Definition RawData.h:20
auto begin() noexcept
指向传感器生成的数据的开始迭代器。
Definition RawData.h:52
A view over the header of a Lidar measurement.
LidarHeaderView(const uint32_t *begin)
uint32_t GetPointCount(size_t channel) const
Serializes the data generated by Lidar sensors.
static SharedPtr< SensorData > Deserialize(RawData &&data)
static Buffer Serialize(const Sensor &sensor, const data::LidarData &data, Buffer &&output)
static LidarHeaderView DeserializeHeader(const RawData &data)
static size_t GetHeaderOffset(const RawData &data)
CARLA模拟器的主命名空间。
Definition Carla.cpp:139
boost::shared_ptr< T > SharedPtr
使用这个SharedPtr(boost::shared_ptr)以保持与boost::python的兼容性, 但未来如果可能的话,我们希望能为std::shared_ptr制作一个Python适配器。
Definition Memory.h:19