7#define _USE_MATH_DEFINES
52 return static_cast<float>(rand()) /
static_cast<float>(RAND_MAX);
89 std::istream_iterator<uint8_t> start(f), end;
92 f.open(filename, std::ios::binary);
96 std::vector<uint8_t> content(start, end);
100 return Load(std::move(content));
105 const int NAVMESHSET_MAGIC =
'M' << 24 |
'S' << 16 |
'E' << 8 |
'T';
106 const int NAVMESHSET_VERSION = 1;
109 struct NavMeshSetHeader {
113 dtNavMeshParams params;
115 struct NavMeshTileHeader {
122 if (content.size() <
sizeof(header)) {
123 logging::log(
"Nav: failed loading binary");
128 unsigned long pos = 0;
129 memcpy(&header, &content[pos],
sizeof(header));
130 pos +=
sizeof(header);
133 if (header.magic != NAVMESHSET_MAGIC || header.version != NAVMESHSET_VERSION) {
138 dtNavMesh *mesh = dtAllocNavMesh();
144 dtStatus status = mesh->init(&header.params);
145 if (dtStatusFailed(status)) {
150 for (
int i = 0; i < header.num_tiles; ++i) {
151 NavMeshTileHeader tile_header;
154 memcpy(&tile_header, &content[pos],
sizeof(tile_header));
155 pos +=
sizeof(tile_header);
156 if (pos >= content.size()) {
162 if (!tile_header.tile_ref || !tile_header.data_size) {
167 char *data =
static_cast<char *
>(dtAlloc(
static_cast<size_t>(tile_header.data_size), DT_ALLOC_PERM));
173 memcpy(data, &content[pos],
static_cast<size_t>(tile_header.data_size));
174 pos +=
static_cast<unsigned long>(tile_header.data_size);
175 if (pos > content.size()) {
182 mesh->addTile(
reinterpret_cast<unsigned char *
>(data), tile_header.data_size, DT_TILE_FREE_DATA,
183 tile_header.tile_ref, 0);
219 logging::log(
"Nav: failed to create crowd");
236 dtObstacleAvoidanceParams params;
238 memcpy(¶ms,
_crowd->getObstacleAvoidanceParams(0),
sizeof(dtObstacleAvoidanceParams));
241 params.velBias = 0.5f;
242 params.adaptiveDivs = 5;
243 params.adaptiveRings = 2;
244 params.adaptiveDepth = 1;
245 _crowd->setObstacleAvoidanceParams(0, ¶ms);
248 params.velBias = 0.5f;
249 params.adaptiveDivs = 5;
250 params.adaptiveRings = 2;
251 params.adaptiveDepth = 2;
252 _crowd->setObstacleAvoidanceParams(1, ¶ms);
255 params.velBias = 0.5f;
256 params.adaptiveDivs = 7;
257 params.adaptiveRings = 2;
258 params.adaptiveDepth = 3;
259 _crowd->setObstacleAvoidanceParams(2, ¶ms);
262 params.velBias = 0.5f;
263 params.adaptiveDivs = 7;
264 params.adaptiveRings = 3;
265 params.adaptiveDepth = 3;
267 _crowd->setObstacleAvoidanceParams(3, ¶ms);
273 dtQueryFilter * filter,
274 std::vector<carla::geom::Location> &path,
275 std::vector<unsigned char> &area) {
278 unsigned char straight_path_flags[
MAX_POLYS];
279 dtPolyRef straight_path_polys[
MAX_POLYS];
280 int num_straight_path;
281 int straight_path_options = DT_STRAIGHTPATH_AREA_CROSSINGS;
295 float poly_pick_ext[3];
296 poly_pick_ext[0] = 2;
297 poly_pick_ext[1] = 4;
298 poly_pick_ext[2] = 2;
301 dtQueryFilter filter2;
302 if (filter ==
nullptr) {
311 dtPolyRef start_ref = 0;
312 dtPolyRef end_ref = 0;
313 float start_pos[3] = { from.
x, from.
z, from.
y };
314 float end_pos[3] = { to.
x, to.
z, to.
y };
317 std::scoped_lock<std::mutex> lock(
_mutex);
318 _nav_query->findNearestPoly(start_pos, poly_pick_ext, filter, &start_ref, 0);
319 _nav_query->findNearestPoly(end_pos, poly_pick_ext, filter, &end_ref, 0);
321 if (!start_ref || !end_ref) {
327 std::scoped_lock<std::mutex> lock(
_mutex);
329 _nav_query->findPath(start_ref, end_ref, start_pos, end_pos, filter, polys, &num_polys,
MAX_POLYS);
333 num_straight_path = 0;
334 if (num_polys == 0) {
341 dtVcopy(end_pos2, end_pos);
342 if (polys[num_polys - 1] != end_ref) {
344 std::scoped_lock<std::mutex> lock(
_mutex);
345 _nav_query->closestPointOnPoly(polys[num_polys - 1], end_pos, end_pos2, 0);
351 std::scoped_lock<std::mutex> lock(
_mutex);
352 _nav_query->findStraightPath(start_pos, end_pos2, polys, num_polys,
353 straight_path, straight_path_flags,
354 straight_path_polys, &num_straight_path,
MAX_POLYS, straight_path_options);
359 path.reserve(
static_cast<unsigned long>(num_straight_path));
360 unsigned char area_type;
361 for (
int i = 0, j = 0; j < num_straight_path; i += 3, ++j) {
363 path.emplace_back(straight_path[i], straight_path[i + 2], straight_path[i + 1]);
367 std::scoped_lock<std::mutex> lock(
_mutex);
368 _nav_mesh->getPolyArea(straight_path_polys[j], &area_type);
370 area.emplace_back(area_type);
377 std::vector<carla::geom::Location> &path, std::vector<unsigned char> &area) {
380 unsigned char straight_path_flags[
MAX_POLYS];
381 dtPolyRef straight_path_polys[
MAX_POLYS];
382 int num_straight_path = 0;
383 int straight_path_options = DT_STRAIGHTPATH_AREA_CROSSINGS;
397 float poly_pick_ext[3] = {2,4,2};
404 const dtQueryFilter *filter;
407 std::scoped_lock<std::mutex> lock(
_mutex);
408 filter =
_crowd->getFilter(
_crowd->getAgent(it->second)->params.queryFilterType);
412 dtPolyRef start_ref = 0;
413 dtPolyRef end_ref = 0;
414 float start_pos[3] = { from.
x, from.
z, from.
y };
415 float end_pos[3] = { to.
x, to.
z, to.
y };
418 std::scoped_lock<std::mutex> lock(
_mutex);
419 _nav_query->findNearestPoly(start_pos, poly_pick_ext, filter, &start_ref, 0);
420 _nav_query->findNearestPoly(end_pos, poly_pick_ext, filter, &end_ref, 0);
422 if (!start_ref || !end_ref) {
429 std::scoped_lock<std::mutex> lock(
_mutex);
430 _nav_query->findPath(start_ref, end_ref, start_pos, end_pos, filter, polys, &num_polys,
MAX_POLYS);
434 if (num_polys == 0) {
441 dtVcopy(end_pos2, end_pos);
442 if (polys[num_polys - 1] != end_ref) {
444 std::scoped_lock<std::mutex> lock(
_mutex);
445 _nav_query->closestPointOnPoly(polys[num_polys - 1], end_pos, end_pos2, 0);
451 std::scoped_lock<std::mutex> lock(
_mutex);
452 _nav_query->findStraightPath(start_pos, end_pos2, polys, num_polys,
453 straight_path, straight_path_flags,
454 straight_path_polys, &num_straight_path,
MAX_POLYS, straight_path_options);
459 path.reserve(
static_cast<unsigned long>(num_straight_path));
460 unsigned char area_type;
461 for (
int i = 0, j = 0; j < num_straight_path; i += 3, ++j) {
463 path.emplace_back(straight_path[i], straight_path[i + 2], straight_path[i + 1]);
467 std::scoped_lock<std::mutex> lock(
_mutex);
468 _nav_mesh->getPolyArea(straight_path_polys[j], &area_type);
470 area.emplace_back(area_type);
478 dtCrowdAgentParams params;
488 memset(¶ms, 0,
sizeof(params));
491 params.maxAcceleration = 160.0f;
492 params.maxSpeed = 1.47f;
493 params.collisionQueryRange = 10;
494 params.obstacleAvoidanceType = 3;
495 params.separationWeight = 0.5f;
499 params.queryFilterType = 1;
501 params.queryFilterType = 0;
505 params.updateFlags = 0;
512 float point_from[3] = { from.
x, from.
z - (
AGENT_HEIGHT / 2.0f), from.
y };
517 std::scoped_lock<std::mutex> lock(
_mutex);
518 index =
_crowd->addAgent(point_from, ¶ms);
540 dtCrowdAgentParams params;
574 int index = it->second;
580 std::scoped_lock<std::mutex> lock(
_mutex);
581 agent =
_crowd->getEditableAgent(index);
589 agent->params.obb[0] = box_corner1.
x;
590 agent->params.obb[1] = box_corner1.
z;
591 agent->params.obb[2] = box_corner1.
y;
592 agent->params.obb[3] = box_corner2.
x;
593 agent->params.obb[4] = box_corner2.
z;
594 agent->params.obb[5] = box_corner2.
y;
595 agent->params.obb[6] = box_corner3.
x;
596 agent->params.obb[7] = box_corner3.
z;
597 agent->params.obb[8] = box_corner3.
y;
598 agent->params.obb[9] = box_corner4.
x;
599 agent->params.obb[10] = box_corner4.
z;
600 agent->params.obb[11] = box_corner4.
y;
607 memset(¶ms, 0,
sizeof(params));
610 params.maxAcceleration = 0.0f;
611 params.maxSpeed = 1.47f;
612 params.collisionQueryRange = 0;
613 params.obstacleAvoidanceType = 0;
614 params.separationWeight = 100.0f;
617 params.updateFlags = 0;
622 params.useObb =
true;
623 params.obb[0] = box_corner1.
x;
624 params.obb[1] = box_corner1.
z;
625 params.obb[2] = box_corner1.
y;
626 params.obb[3] = box_corner2.
x;
627 params.obb[4] = box_corner2.
z;
628 params.obb[5] = box_corner2.
y;
629 params.obb[6] = box_corner3.
x;
630 params.obb[7] = box_corner3.
z;
631 params.obb[8] = box_corner3.
y;
632 params.obb[9] = box_corner4.
x;
633 params.obb[10] = box_corner4.
z;
634 params.obb[11] = box_corner4.
y;
645 std::scoped_lock<std::mutex> lock(
_mutex);
646 index =
_crowd->addAgent(point_from, ¶ms);
648 logging::log(
"Vehicle agent not added to the crowd by some problem!");
653 dtCrowdAgent *agent =
_crowd->getEditableAgent(index);
655 agent->state = DT_CROWDAGENT_STATE_WALKING;
682 std::scoped_lock<std::mutex> lock(
_mutex);
683 _crowd->removeAgent(it->second);
699 std::scoped_lock<std::mutex> lock(
_mutex);
700 _crowd->removeAgent(it->second);
714 std::unordered_set<carla::rpc::ActorId> updated;
718 updated.insert(entry.first);
722 for (
auto &&entry : vehicles) {
726 updated.erase(entry.id);
730 for (
auto &&entry : updated) {
757 std::scoped_lock<std::mutex> lock(
_mutex);
758 dtCrowdAgent *agent =
_crowd->getEditableAgent(it->second);
760 agent->params.maxSpeed = max_speed;
818 float point_to[3] = { to.
x, to.
z, to.
y };
823 std::scoped_lock<std::mutex> lock(
_mutex);
824 const dtQueryFilter *filter =
_crowd->getFilter(0);
825 dtPolyRef target_ref;
826 _nav_query->findNearestPoly(point_to,
_crowd->getQueryHalfExtents(), filter, &target_ref, nearest);
831 res =
_crowd->requestMoveTarget(index, target_ref, point_to);
849 double dt = timestamp.delta_seconds;
861 for (
double remaining = dt; remaining > 0.0; remaining -=
MAX_NAV_STEP) {
863 std::scoped_lock<std::mutex> lock(
_mutex);
874 int total_unblocked = 0;
878 std::scoped_lock<std::mutex> lock(
_mutex);
879 total_agents =
_crowd->getAgentCount();
881 const dtCrowdAgent *ag;
882 for (
int i = 0; i < total_agents; ++i) {
885 std::scoped_lock<std::mutex> lock(
_mutex);
889 if (!ag->active || ag->paused || ag->dead) {
894 if (!ag->params.useObb && !ag->paused) {
895 bool reset_target_pos =
false;
896 bool use_same_filter =
false;
907 reset_target_pos =
true;
908 use_same_filter =
true;
914 if (reset_target_pos) {
916 if (!use_same_filter) {
955 int index = it->second;
961 const dtCrowdAgent *agent;
964 std::scoped_lock<std::mutex> lock(
_mutex);
965 agent =
_crowd->getAgent(index);
968 if (!agent->active) {
981 if (agent->vel[0] < -
min || agent->vel[0] >
min ||
982 agent->vel[2] < -
min || agent->vel[2] >
min) {
983 yaw = atan2f(agent->vel[2], agent->vel[0]) * (180.0f /
static_cast<float>(M_PI));
984 speed = sqrtf(agent->vel[0] * agent->vel[0] + agent->vel[1] * agent->vel[1] + agent->vel[2] * agent->vel[2]);
986 yaw = atan2f(agent->dvel[2], agent->dvel[0]) * (180.0f /
static_cast<float>(M_PI));
987 speed = sqrtf(agent->dvel[0] * agent->dvel[0] + agent->dvel[1] * agent->dvel[1] + agent->dvel[2] * agent->dvel[2]);
991 float shortest_angle = fmod(yaw -
_yaw_walkers[
id] + 540.0f, 360.0f) - 180.0f;
992 float per = (speed / 1.5f);
993 if (per > 1.0f) per = 1.0f;
994 float rotation_speed = per * 6.0f;
996 (shortest_angle * std::min(1.0f, rotation_speed *
static_cast<float>(
_delta_seconds)));
1019 int index = it->second;
1025 const dtCrowdAgent *agent;
1028 std::scoped_lock<std::mutex> lock(
_mutex);
1029 agent =
_crowd->getAgent(index);
1032 if (!agent->active) {
1037 location.
x = agent->npos[0];
1038 location.
y = agent->npos[2];
1039 location.
z = agent->npos[1];
1060 int index = it->second;
1066 const dtCrowdAgent *agent;
1069 std::scoped_lock<std::mutex> lock(
_mutex);
1070 agent =
_crowd->getAgent(index);
1073 return sqrt(agent->vel[0] * agent->vel[0] + agent->vel[1] * agent->vel[1] + agent->vel[2] *
1088 dtQueryFilter filter2;
1089 if (filter ==
nullptr) {
1096 dtPolyRef random_ref { 0 };
1097 float point[3] { 0.0f, 0.0f, 0.0f };
1102 std::scoped_lock<std::mutex> lock(
_mutex);
1104 status =
_nav_query->findRandomPoint(filter,
frand, &random_ref, point);
1106 if (status == DT_SUCCESS) {
1107 location.
x = point[0];
1108 location.
y = point[2];
1109 location.
z = point[1];
1112 }
while (status != DT_SUCCESS && rounds > 0);
1115 return (rounds > 0);
1122 dtCrowdAgent *agent;
1125 std::scoped_lock<std::mutex> lock(
_mutex);
1126 agent =
_crowd->getEditableAgent(agent_index);
1128 agent->params.queryFilterType =
static_cast<unsigned char>(filter_index);
1156 int index = it->second;
1162 dtCrowdAgent *agent;
1165 std::scoped_lock<std::mutex> lock(
_mutex);
1166 agent =
_crowd->getEditableAgent(index);
1170 agent->paused = pause;
1183 float dir[3] = { direction.
x, direction.
z, direction.
y };
1187 std::scoped_lock<std::mutex> lock(
_mutex);
1188 result =
_crowd->hasVehicleNear(it->second, distance * distance, dir,
false);
1204 dtCrowdAgent *agent;
1207 std::scoped_lock<std::mutex> lock(
_mutex);
1208 agent =
_crowd->getEditableAgent(it->second);
1212 float x = (location.
x - agent->npos[0]) * 0.0001f;
1213 float y = (location.
y - agent->npos[2]) * 0.0001f;
1214 float z = (location.
z - agent->npos[1]) * 0.0001f;
1245 int index = it->second;
1251 const dtCrowdAgent *agent;
1254 std::scoped_lock<std::mutex> lock(
_mutex);
1255 agent =
_crowd->getAgent(index);
1259 alive = !agent->dead;
#define DEBUG_ASSERT(predicate)
double min(double v1, double v2)
Represents the state of all the actors of an episode at a given frame.
const auto & GetTimestamp() const
Vector3D extent
Half the size of the BoundingBox in local space.
static constexpr T ToRadians(T deg)
static Vector3D RotatePointOnOrigin2D(Vector3D p, float angle)
bool SetWalkerMaxSpeed(ActorId id, float max_speed)
set new max speed
void UpdateCrowd(const client::detail::EpisodeState &state)
update all walkers in crowd
bool GetPath(carla::geom::Location from, carla::geom::Location to, dtQueryFilter *filter, std::vector< carla::geom::Location > &path, std::vector< unsigned char > &area)
return the path points to go from one position to another
void SetAgentFilter(int agent_index, int filter_index)
assign a filter index to an agent
std::unordered_map< ActorId, int > _mapped_vehicles_id
std::unordered_map< ActorId, int > _mapped_walkers_id
mapping Id
bool RemoveAgent(ActorId id)
remove an agent
std::weak_ptr< carla::client::detail::Simulator > _simulator
bool GetWalkerPosition(ActorId id, carla::geom::Location &location)
get the walker current location
float GetWalkerSpeed(ActorId id)
get the walker current transform
bool Load(const std::string &filename)
load navigation data
bool SetWalkerTarget(ActorId id, carla::geom::Location to)
set a new target point to go through a route with events
void SetSeed(unsigned int seed)
set the seed to use with random numbers
dtNavMesh * _nav_mesh
meshes
bool UpdateVehicles(std::vector< VehicleCollisionInfo > vehicles)
add/update/delete vehicles in crowd
std::unordered_map< int, ActorId > _mapped_by_index
bool SetWalkerDirectTarget(ActorId id, carla::geom::Location to)
bool SetWalkerLookAt(ActorId id, carla::geom::Location location)
make agent look at some location
void PauseAgent(ActorId id, bool pause)
set an agent as paused for the crowd
bool HasVehicleNear(ActorId id, float distance, carla::geom::Location direction)
return if the agent has a vehicle near (as neighbour)
bool GetWalkerTransform(ActorId id, carla::geom::Transform &trans)
get the walker current transform
bool GetRandomLocation(carla::geom::Location &location, dtQueryFilter *filter=nullptr) const
get a random location for navigation
bool IsWalkerAlive(ActorId id, bool &alive)
return if the agent has been killed by a vehicle
WalkerManager _walker_manager
walker manager for the route planning with events
bool GetAgentRoute(ActorId id, carla::geom::Location from, carla::geom::Location to, std::vector< carla::geom::Location > &path, std::vector< unsigned char > &area)
void SetPedestriansCrossFactor(float percentage)
set the probability that an agent could cross the roads in its path following
std::unordered_map< ActorId, float > _yaw_walkers
store walkers yaw angle from previous tick
std::optional< double > _last_elapsed_seconds
Simulation time of the snapshot the crowd last advanced to; unset before the first update.
bool AddWalker(ActorId id, carla::geom::Location from)
create a new walker
std::vector< uint8_t > _binary_mesh
bool SetWalkerDirectTargetIndex(int index, carla::geom::Location to)
void SetSimulator(std::weak_ptr< carla::client::detail::Simulator > simulator)
reference to the simulator to access API functions
dtNavMeshQuery * _nav_query
void CreateCrowd(void)
create the crowd object
bool AddOrUpdateVehicle(VehicleCollisionInfo &vehicle)
create a new vehicle in crowd to be avoided by walkers
float _probability_crossing
std::unordered_map< int, carla::geom::Vector3D > _walkers_blocked_position
saves the position of each actor at intervals and check if any is blocked
bool RemoveWalker(ActorId id)
remove a walker route
bool AddWalker(ActorId id)
create a new walker route
void SetSimulator(std::weak_ptr< carla::client::detail::Simulator > simulator)
reference to the simulator to access API functions
bool Update(double delta)
update all routes
void SetNav(Navigation *nav)
assign the navigation module
bool SetWalkerRoute(ActorId id)
set a new route from its current position
@ DT_CROWD_OBSTACLE_AVOIDANCE
@ DT_CROWD_ANTICIPATE_TURNS
static const float AREA_ROAD_COST
static const float AGENT_UNBLOCK_TIME
static const float AGENT_RADIUS
static const float AGENT_HEIGHT
static const float AGENT_UNBLOCK_DISTANCE_SQUARED
static const double MAX_NAV_STEP
static const int MAX_AGENTS
static const double MAX_NAV_CATCH_UP
static const int MAX_POLYS
static const int MAX_QUERY_SEARCH_NODES
static const float AGENT_UNBLOCK_DISTANCE
static const float AREA_GRASS_COST
This file contains definitions of common data structures used in traffic manager.
constexpr float SquaredLength() const
struct to send info about vehicles to the crowd
carla::geom::BoundingBox bounding
carla::geom::Transform transform