CARLA
 
载入中...
搜索中...
未找到
ActorBlueprint.cpp
浏览该文件的文档.
1// Copyright (c) 2017 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#include "carla/client/ActorBlueprint.h"//引入ActorBlueprint类相关头文件
8
9#include "carla/Exception.h"//引入异常处理头文件
10#include "carla/StringUtil.h"//引入字符串工具头文件,用于字符串操作
11
12#include <algorithm>//引入算法库,提供算法支持
13
14namespace carla {
15namespace client {
16 //函数:FillMap
17 //作用:将 source 容器中的元素移动到 destination map 中,并根据 item.id 作为 key
18 // 参数:
19 // destination - 目标 map 容器,用于存储按 id 映射的元素
20 // source - 源容器,包含待移动的元素
21 template <typename Map, typename Container>
22 static void FillMap(Map &destination, Container &source) {
23 //预先为 destination map 分配空间,提高性能
24 destination.reserve(source.size());
25 //遍历 source 容器中的每个元素,将元素按 id 作为键,移动到 destination map 中
26 for (auto &item : source) {
27 auto id = item.id; // 获取元素的 id
28 //将 item.id 作为 map 的键,并将 item 的所有权转移到 map 中去
29 destination.emplace(id, std::move(item));
30 }
31 }
32 // 构造函数:ActorBlueprint
33 // 作用:根据 rpc::ActorDefinition 初始化 ActorBlueprint 对象
34 // 参数:
35 // definition - rpc::ActorDefinition 类型的初始化定义,包含 ActorBlueprint 的初始数据
37 : _uid(definition.uid), //初始化成员变量 _uid 为传入的 uid
38 _id(std::move(definition.id)) { //将 definition 中的 id 移动赋值给 _id
39 StringUtil::Split(_tags, definition.tags, ","); //将 tags 按逗号分隔,并储存到 _tags容器中
40
41 FillMap(_attributes, definition.attributes); // 使用 FillMap 函数将 definition 中的 attributes 按 id 映射并存储到 _attributes 中
42
43 }
44 // 函数:MatchTags
45 // 作用:检查 _id 或 _tags 列表中的任意 tag 是否匹配指定的通配符模式
46 // 参数:
47 // wildcard_pattern - 用于匹配的通配符模式
48 // 返回值:
49 // 如果 _id 或 _tags 中任意 tag 匹配模式,则返回 true;否则返回 false
50 bool ActorBlueprint::MatchTags(const std::string &wildcard_pattern) const {
51 //返回结果为:_id 匹配模式或 _tags 列表中的任意一个 tag 匹配模式
52 return
53 StringUtil::Match(_id, wildcard_pattern) || // 检查 _id 是否匹配通配符模式
54
55 std::any_of(_tags.begin(), _tags.end(), [&](const auto &tag) {
56 // 遍历 _tags 列表,检查是否有任何 tag 匹配通配符模式
57 return StringUtil::Match(tag, wildcard_pattern);
58 });
59 }
60 // 函数:GetAttribute
61 // 作用:根据指定的属性 id 获取 ActorBlueprint 对象中的 ActorAttribute
62 // 参数:
63 // id - 要查询的属性的 id
64 // 返回值:
65 // 返回指定 id 对应的 ActorAttribute
66 // 异常:
67 // 如果指定 id 的属性不存在,抛出 std::out_of_range 异常
68 const ActorAttribute &ActorBlueprint::GetAttribute(const std::string &id) const {
69 //在_attributes映射中查找 id 对应的属性
70 auto it = _attributes.find(id);
71 if (it == _attributes.end()) {
72 // 如果未找到属性,抛出异常
73 using namespace std::string_literals;
74 throw_exception(std::out_of_range("attribute '"s + id + "' not found"));
75 }
76 return it->second; //返回找到的属性
77 }
78 // 函数:SetAttribute
79 // 作用:根据指定的属性 id 设置对应属性的值
80 // 参数:
81 // id - 要设置的属性的 id
82 // value - 要设置的新属性值
83 void ActorBlueprint::SetAttribute(const std::string &id, std::string value) {
84 const_cast<ActorAttribute &>(GetAttribute(id)).Set(std::move(value));
85 // 使用 const_cast 去除 const 限制,修改找到的属性值
86 }
87 // 函数:MakeActorDescription
88 // 作用:生成一个 rpc::ActorDescription 对象,用于描述当前的 ActorBlueprint
89 // 返回值:
90 // 返回生成的 rpc::ActorDescription 描述对象
92 rpc::ActorDescription description;
93 description.uid = _uid; //设置唯一标识符
94 description.id = _id; //设置 actor 的 id
95 //为属性容器预留空间,提高性能
96 description.attributes.reserve(_attributes.size());
97 //将所有属性添加到描述对象的 attributes 向量中
98 for (const auto &attribute : *this) {
99 description.attributes.push_back(attribute);
100 }
101 return description; //返回生成的描述对象
102 }
103
104} // namespace client
105} // namespace carla
地图类的前向声明,用于在LaneInvasionSensor类中可能的引用。
static bool Match(const char *str, const char *wildcard_pattern)
Match str with the Unix shell-style wildcard_pattern.
static void Split(Container &destination, const Range1T &str, const Range2T &separators)
Definition StringUtil.h:75
rpc::ActorDescription MakeActorDescription() const
std::unordered_map< std::string, ActorAttribute > _attributes
const ActorAttribute & GetAttribute(const std::string &id) const
std::unordered_set< std::string > _tags
ActorBlueprint(rpc::ActorDefinition actor_definition)
bool MatchTags(const std::string &wildcard_pattern) const
测试是否有标签满足 wildcard_pattern。
void SetAttribute(const std::string &id, std::string value)
Set the value of the attribute given by id.
static void FillMap(Map &destination, Container &source)
CARLA模拟器的主命名空间。
Definition Carla.cpp:139
void throw_exception(const std::exception &e)
Definition Carla.cpp:142
包含CARLA客户端相关类和函数的命名空间。