Changing events for walkers

This commit is contained in:
bernatx 2019-10-25 11:48:27 +02:00 committed by bernat
parent 4929f06ef7
commit 065295c7f0
4 changed files with 55 additions and 29 deletions

View File

@ -12,27 +12,29 @@
namespace carla {
namespace nav {
bool WalkerEventVisitor::operator()(WalkerEventIgnore &, double, WalkerManager &) {
return true;
EventResult WalkerEventVisitor::operator()(WalkerEventIgnore &) {
return EventResult::End;
}
bool WalkerEventVisitor::operator()(WalkerEventWait &event, double delta, WalkerManager &) {
EventResult WalkerEventVisitor::operator()(WalkerEventWait &event) {
// refresh time and check
event.time -= delta;
return (event.time <= 0.0);
event.time -= _delta;
if (event.time <= 0.0)
return EventResult::End;
else
return EventResult::Continue;
}
bool WalkerEventVisitor::operator()(WalkerEventStopAndCheck &event, double delta, WalkerManager &manager) {
event.time -= delta;
logging::log("time = ", event.time);
EventResult WalkerEventVisitor::operator()(WalkerEventStopAndCheck &event) {
event.time -= _delta;
if (event.time <= 0.0) {
// assign a new route
logging::log("event time out");
manager.SetWalkerRoute(event.id);
return true;
return EventResult::TimeOut;
} else {
// check if the agent has any vehicle around
return (!event.nav->hasVehicleNear(event.id));
if (_manager && !(_manager->GetNavigation()->hasVehicleNear(_id)))
return EventResult::End;
else
return EventResult::Continue;
}
}

View File

@ -16,6 +16,12 @@ namespace nav {
class Navigation;
class WalkerManager;
enum class EventResult : uint8_t {
Continue,
End,
TimeOut
};
struct WalkerEventIgnore {
};
@ -25,18 +31,23 @@ namespace nav {
};
struct WalkerEventStopAndCheck {
ActorId id;
Navigation *nav;
double time;
WalkerEventStopAndCheck(ActorId id, Navigation *nav, double duration) : id(id), nav(nav), time(duration) {};
WalkerEventStopAndCheck(double duration) : time(duration) {};
};
using WalkerEvent = boost::variant<WalkerEventIgnore, WalkerEventWait, WalkerEventStopAndCheck>;
struct WalkerEventVisitor {
bool operator()(WalkerEventIgnore &event, double delta, WalkerManager &manager);
bool operator()(WalkerEventWait &event, double delta, WalkerManager &manager);
bool operator()(WalkerEventStopAndCheck &event, double delta, WalkerManager &manager);
// visitor class
class WalkerEventVisitor {
public:
WalkerEventVisitor(WalkerManager *manager, ActorId id, double delta) : _manager(manager), _id(id), _delta(delta) {};
EventResult operator()(WalkerEventIgnore &event);
EventResult operator()(WalkerEventWait &event);
EventResult operator()(WalkerEventStopAndCheck &event);
private:
WalkerManager *_manager { nullptr };
double _delta { 0 };
ActorId _id { 0 };
};
} // namespace nav

View File

@ -70,8 +70,17 @@ namespace nav {
break;
case WALKER_IN_EVENT:
if (ExecuteEvent(it.second, delta)) {
SetWalkerNextPoint(it.first);
switch (ExecuteEvent(it.first, it.second, delta)) {
case EventResult::Continue:
break;
case EventResult::End:
// next point in route
SetWalkerNextPoint(it.first);
break;
case EventResult::TimeOut:
// unblock changing the route
SetWalkerRoute(it.first);
break;
}
break;
@ -95,7 +104,7 @@ namespace nav {
_nav->GetRandomLocation(location, 1, nullptr);
// set the route
SetWalkerRoute(id, location);
return SetWalkerRoute(id, location);
}
/// set a new route from its current position
@ -137,7 +146,7 @@ namespace nav {
// stop and check
case SAMPLE_POLYAREA_ROAD:
case SAMPLE_POLYAREA_CROSS:
info.route.emplace_back(WalkerEventStopAndCheck(id, _nav, 3), std::move(path[i]));
info.route.emplace_back(WalkerEventStopAndCheck(5), std::move(path[i]));
break;
default:
@ -185,13 +194,14 @@ namespace nav {
return true;
}
bool WalkerManager::ExecuteEvent(WalkerInfo &info, double delta) {
EventResult WalkerManager::ExecuteEvent(ActorId id, WalkerInfo &info, double delta) {
// go to the event
WalkerRoutePoint &rp = info.route[info.currentIndex];
// we need to add 'delta' as second parameter, and 'this' as third
auto bound_visitor = std::bind(WalkerEventVisitor(), std::placeholders::_1, delta, *this);
return boost::apply_visitor(bound_visitor, rp.event);
// build the visitor structure
WalkerEventVisitor visitor(this, id, delta);
// run the event
return boost::apply_visitor(visitor, rp.event);
}

View File

@ -73,9 +73,12 @@ namespace nav {
/// set the next point in the route
bool SetWalkerNextPoint(ActorId id);
/// return the navigation object
Navigation *GetNavigation() { return _nav; };
private:
bool ExecuteEvent(WalkerInfo &info, double delta);
EventResult ExecuteEvent(ActorId id, WalkerInfo &info, double delta);
std::unordered_map<ActorId, WalkerInfo> _walkers;
Navigation *_nav { nullptr };