CARLA
BlueprintLibrary.cpp
Go to the documentation of this file.
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 
8 
9 #include "carla/Exception.h"
10 
11 #include <algorithm>
12 #include <iterator>
13 
14 namespace carla {
15 namespace client {
16 
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  }
23  }
24 
26  const std::string &wildcard_pattern) const {
27  map_type result;
28  for (auto &pair : _blueprints) {
29  if (pair.second.MatchTags(wildcard_pattern)) {
30  result.emplace(pair);
31  }
32  }
34  }
35 
37  const std::string &name, const std::string& value) const {
38  map_type result;
39 
40  for (auto &pair : _blueprints) {
41  if (!pair.second.ContainsAttribute(name))
42  continue;
43  const ActorAttribute &Attribute = pair.second.GetAttribute(name);
44  const std::vector<std::string> &Values = Attribute.GetRecommendedValues();
45  if (Values.empty())
46  {
47  const std::string &AttributeValue = Attribute.GetValue();
48  if (value == AttributeValue)
49  result.emplace(pair);
50  }
51  else
52  {
53  for (const std::string &Value : Values)
54  {
55  if (Value == value)
56  {
57  result.emplace(pair);
58  break;
59  }
60  }
61  }
62 
63  }
65  }
66 
68  auto it = _blueprints.find(key);
69  return it != _blueprints.end() ? &it->second : nullptr;
70  }
71 
73  auto it = _blueprints.find(key);
74  if (it == _blueprints.end()) {
75  using namespace std::string_literals;
76  throw_exception(std::out_of_range("blueprint '"s + key + "' not found"));
77  }
78  return it->second;
79  }
80 
82  if (pos >= size()) {
83  throw_exception(std::out_of_range("index out of range"));
84  }
85  return operator[](pos);
86  }
87 
88 } // namespace client
89 } // namespace carla
std::unordered_map< std::string, ActorBlueprint > map_type
const std::vector< std::string > & GetRecommendedValues() const
SharedPtr< BlueprintLibrary > Filter(const std::string &wildcard_pattern) const
Filters a list of ActorBlueprint with id or tags matching wildcard_pattern.
void throw_exception(const std::exception &e)
Definition: Carla.cpp:135
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
const value_type & const_reference
const_pointer Find(const std::string &key) const
virtual const std::string & GetValue() const override
const_reference at(const std::string &key) const
Contains all the necessary information for spawning an Actor.
BlueprintLibrary(const std::vector< rpc::ActorDefinition > &blueprints)
const_reference operator[](size_type pos) const
SharedPtr< BlueprintLibrary > FilterByAttribute(const std::string &name, const std::string &value) const