Improve wildcard filtering of actors and blueprints

This commit is contained in:
nsubiron 2018-10-18 21:09:54 +02:00
parent 3c1f23a3fa
commit a6dfef0cd6
6 changed files with 33 additions and 4 deletions

View File

@ -66,6 +66,7 @@
## `carla.ActorList` ## `carla.ActorList`
- `filter(wildcard_pattern)`
- `__getitem__(pos)` - `__getitem__(pos)`
- `__len__()` - `__len__()`
- `__iter__()` - `__iter__()`

View File

@ -30,9 +30,11 @@ namespace client {
} }
bool ActorBlueprint::MatchTags(const std::string &wildcard_pattern) const { bool ActorBlueprint::MatchTags(const std::string &wildcard_pattern) const {
return std::any_of(_tags.begin(), _tags.end(), [&](const auto &tag) { return
return StringUtil::Match(tag, wildcard_pattern); StringUtil::Match(_id, wildcard_pattern) ||
}); std::any_of(_tags.begin(), _tags.end(), [&](const auto &tag) {
return StringUtil::Match(tag, wildcard_pattern);
});
} }
rpc::ActorDescription ActorBlueprint::MakeActorDescription() const { rpc::ActorDescription ActorBlueprint::MakeActorDescription() const {

View File

@ -6,6 +6,7 @@
#include "carla/client/ActorList.h" #include "carla/client/ActorList.h"
#include "carla/StringUtil.h"
#include "carla/client/Actor.h" #include "carla/client/Actor.h"
#include "carla/client/detail/ActorFactory.h" #include "carla/client/detail/ActorFactory.h"
@ -14,12 +15,32 @@
namespace carla { namespace carla {
namespace client { namespace client {
struct GetTypeIdVisitor {
const std::string &operator()(const rpc::Actor &actor) const {
return actor.description.id;
}
const std::string &operator()(const SharedPtr<Actor> &actor) const {
return actor->GetTypeId();
}
};
ActorList::ActorList( ActorList::ActorList(
detail::EpisodeProxy episode, detail::EpisodeProxy episode,
std::vector<rpc::Actor> actors) std::vector<rpc::Actor> actors)
: _episode(std::move(episode)), : _episode(std::move(episode)),
_actors(std::make_move_iterator(actors.begin()), std::make_move_iterator(actors.end())) {} _actors(std::make_move_iterator(actors.begin()), std::make_move_iterator(actors.end())) {}
ActorList ActorList::Filter(const std::string &wildcard_pattern) const {
ActorList filtered{_episode, {}};
for (auto &&item : _actors) {
const auto &id = boost::apply_visitor(GetTypeIdVisitor(), item);
if (StringUtil::Match(id, wildcard_pattern)) {
filtered._actors.push_back(item);
}
}
return filtered;
}
SharedPtr<Actor> ActorList::RetrieveActor(value_type &value) const { SharedPtr<Actor> ActorList::RetrieveActor(value_type &value) const {
if (value.which() == 0u) { if (value.which() == 0u) {
value = detail::ActorFactory::MakeActor( value = detail::ActorFactory::MakeActor(

View File

@ -32,6 +32,9 @@ namespace client {
public: public:
/// Filters a list of Actor with type id matching @a wildcard_pattern.
ActorList Filter(const std::string &wildcard_pattern) const;
SharedPtr<Actor> operator[](size_t pos) const { SharedPtr<Actor> operator[](size_t pos) const {
return RetrieveActor(_actors[pos]); return RetrieveActor(_actors[pos]);
} }

View File

@ -40,7 +40,8 @@ namespace client {
BlueprintLibrary(BlueprintLibrary &&) = default; BlueprintLibrary(BlueprintLibrary &&) = default;
BlueprintLibrary &operator=(BlueprintLibrary &&) = default; BlueprintLibrary &operator=(BlueprintLibrary &&) = default;
/// Filters a list of ActorBlueprint with tags matching @a wildcard_pattern. /// Filters a list of ActorBlueprint with id or tags matching
/// @a wildcard_pattern.
SharedPtr<BlueprintLibrary> Filter(const std::string &wildcard_pattern) const; SharedPtr<BlueprintLibrary> Filter(const std::string &wildcard_pattern) const;
const_pointer Find(const std::string &key) const { const_pointer Find(const std::string &key) const {

View File

@ -33,6 +33,7 @@ void export_world() {
namespace cg = carla::geom; namespace cg = carla::geom;
class_<cc::ActorList>("ActorList", no_init) class_<cc::ActorList>("ActorList", no_init)
.def("filter", &cc::ActorList::Filter)
.def("__getitem__", &cc::ActorList::at) .def("__getitem__", &cc::ActorList::at)
.def("__len__", &cc::ActorList::size) .def("__len__", &cc::ActorList::size)
.def("__iter__", range(&cc::ActorList::begin, &cc::ActorList::end)) .def("__iter__", range(&cc::ActorList::begin, &cc::ActorList::end))