CARLA
 
载入中...
搜索中...
未找到
BlueprintLibrary.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/BlueprintLibrary.h" // 引入Carla客户端库中的BlueprintLibrary头文件,该文件包含了与Carla模拟器交互所需的蓝图相关功能。
8
9#include "carla/Exception.h" // 引入Carla异常处理相关的头文件,这个头文件包含了Carla模拟器中可能抛出的异常类,方便进行错误处理。
10
11#include <algorithm> // 引入标准库中的算法功能,该头文件包含了各种常见的算法,如排序、查找等,可以在容器中使用。
12#include <iterator> // 引入标准库中的迭代器相关功能,该头文件定义了用于遍历容器的迭代器功能,例如 std::begin 和 std::end。
13
14namespace carla {
15namespace client {
16//构造函数:使用给定的蓝图列表初始化 BlueprintLibary
18 const std::vector<rpc::ActorDefinition> &blueprints) {
19 _blueprints.reserve(blueprints.size()); //为存储蓝图预留空间
20 for (auto &definition : blueprints) {
21 _blueprints.emplace(definition.id, ActorBlueprint{definition});
22 //将每个蓝图按其 ID 添加到映射中
23 }
24 }
25//根据通配符模式过滤蓝图,返回匹配的 BlueprintLibrary 对象
26 SharedPtr<BlueprintLibrary> BlueprintLibrary::Filter(
27 const std::string &wildcard_pattern) const {
28 map_type result; //用于存储过滤后的蓝图映射
29 for (auto &pair : _blueprints) {
30 if (pair.second.MatchTags(wildcard_pattern)) { //检查蓝图是否匹配通配符模式
31 result.emplace(pair); // 如果蓝图匹配通配符模式,则添加到结果中
32 }
33 }
35 // 返回过滤后的 BlueprintLibrary 对象
36 }
37//根据属性名称和值过滤蓝图,返回匹配的 BlueprintLibrary 对象
38 SharedPtr<BlueprintLibrary> BlueprintLibrary::FilterByAttribute(
39 const std::string &name, const std::string& value) const {
40 map_type result; //用于存储过滤后的蓝图映射
41
42 for (auto &pair : _blueprints) {
43 if (!pair.second.ContainsAttribute(name))
44 continue; //如果蓝图不包含指定的属性,则跳过
45 const ActorAttribute &Attribute = pair.second.GetAttribute(name); //获取指定的属性
46 const std::vector<std::string> &Values = Attribute.GetRecommendedValues();//获取推荐值
47 if (Values.empty()) // 如果没有推荐值,检查当前属性值
48 {
49 const std::string &AttributeValue = Attribute.GetValue(); //获取属性当前值
50 if (value == AttributeValue)
51 result.emplace(pair); // 如果属性值匹配,则添加到结果中
52 }
53 else
54 {
55 for (const std::string &Value : Values) // 遍历推荐值列表
56 {
57 if (Value == value)
58 {
59 result.emplace(pair); // 如果找到匹配值,添加到结果中
60 break; //找到匹配后退出循环
61 }
62 }
63 }
64
65 }
66 return SharedPtr<BlueprintLibrary>{new BlueprintLibrary(result)}; // 返回过滤后的 BlueprintLibrary 对象
67 }
68
69 // 查找并返回与给定键匹配的蓝图,如果未找到返回 nullptr
70 BlueprintLibrary::const_pointer BlueprintLibrary::Find(const std::string &key) const {
71 auto it = _blueprints.find(key); // 在 _blueprints 字典中查找给定的键
72 return it != _blueprints.end() ? &it->second : nullptr;
73 // 如果找到对应键,返回其值的指针,否则返回 nullptr
74 }
75
76 // 根据指定的键返回蓝图的常量引用,如果未找到键则抛出异常
77 BlueprintLibrary::const_reference BlueprintLibrary::at(const std::string &key) const {
78 auto it = _blueprints.find(key); // 在 _blueprints 字典中查找给定的键
79 if (it == _blueprints.end()) {
80 using namespace std::string_literals;
81 throw_exception(std::out_of_range("blueprint '"s + key + "' not found"));
82 } //如果未找到对应的键,抛出 std::out_of_range 异常
83 return it->second;//如果找到对应的键,则返回对应键的蓝图的常量引用
84 }
85
86 // 根据指定位置返回蓝图的常量引用,如果位置超出范围则抛出异常
87 BlueprintLibrary::const_reference BlueprintLibrary::at(size_type pos) const {
88 //检查 pos 是否超出 _blueprints 的大小范围
89 if (pos >= size()) {
90 throw_exception(std::out_of_range("index out of range"));
91 } //如果位置超出范围,抛出 std::out_of_range 异常
92 return operator[](pos); //返回指定位置的蓝图常量引用
93 }
94
95} // namespace client
96} // namespace carla
virtual const std::string & GetValue() const override
const std::vector< std::string > & GetRecommendedValues() const
包含生成参与者所需的所有必要信息。
std::unordered_map< std::string, ActorBlueprint > map_type
BlueprintLibrary(const std::vector< rpc::ActorDefinition > &blueprints)
CARLA模拟器的主命名空间。
Definition Carla.cpp:139
void throw_exception(const std::exception &e)
Definition Carla.cpp:142
boost::shared_ptr< T > SharedPtr
使用这个SharedPtr(boost::shared_ptr)以保持与boost::python的兼容性, 但未来如果可能的话,我们希望能为std::shared_ptr制作一个Python适配器。
Definition Memory.h:19
包含CARLA客户端相关类和函数的命名空间。