CARLA
Loading...
Searching...
No Matches
Navigation.h
Go to the documentation of this file.
1// Copyright (c) 2026 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#pragma once
8
9#include "carla/AtomicList.h"
12#include "carla/geom/Location.h"
15#include "carla/rpc/ActorId.h"
16#include <optional>
17
18#include <Recast.h>
19#include <DetourCrowd.h>
20#include <DetourNavMesh.h>
21#include <DetourNavMeshBuilder.h>
22#include <DetourNavMeshQuery.h>
23#include <DetourCommon.h>
24
25namespace carla {
26namespace nav {
27
35
47
48 /// struct to send info about vehicles to the crowd
54
55 /// Manage the pedestrians navigation, using the Recast & Detour library for low level calculations.
56 ///
57 /// This class gets the binary content of the map from the server, which is required for the path finding.
58 /// Then this class can add or remove pedestrians, and also set target points to walk for each one.
59 class Navigation : private NonCopyable {
60
61 public:
62
63 Navigation();
65
66 /// load navigation data
67 bool Load(const std::string &filename);
68 /// load navigation data from memory
69 bool Load(std::vector<uint8_t> content);
70 /// return the path points to go from one position to another
71 bool GetPath(carla::geom::Location from, carla::geom::Location to, dtQueryFilter * filter,
72 std::vector<carla::geom::Location> &path, std::vector<unsigned char> &area);
74 std::vector<carla::geom::Location> &path, std::vector<unsigned char> &area);
75
76 /// reference to the simulator to access API functions
77 void SetSimulator(std::weak_ptr<carla::client::detail::Simulator> simulator);
78 /// set the seed to use with random numbers
79 void SetSeed(unsigned int seed);
80 /// create the crowd object
81 void CreateCrowd(void);
82 /// create a new walker
84 /// create a new vehicle in crowd to be avoided by walkers
86 /// remove an agent
87 bool RemoveAgent(ActorId id);
88 /// add/update/delete vehicles in crowd
89 bool UpdateVehicles(std::vector<VehicleCollisionInfo> vehicles);
90 /// set new max speed
91 bool SetWalkerMaxSpeed(ActorId id, float max_speed);
92 /// set a new target point to go through a route with events
94 // set a new target point to go directly without events
97 /// get the walker current transform
99 /// get the walker current location
101 /// get the walker current transform
102 float GetWalkerSpeed(ActorId id);
103 /// update all walkers in crowd
104 void UpdateCrowd(const client::detail::EpisodeState &state);
105 /// get a random location for navigation
106 bool GetRandomLocation(carla::geom::Location &location, dtQueryFilter * filter = nullptr) const;
107 /// set the probability that an agent could cross the roads in its path following
108 void SetPedestriansCrossFactor(float percentage);
109 /// set an agent as paused for the crowd
110 void PauseAgent(ActorId id, bool pause);
111 /// return if the agent has a vehicle near (as neighbour)
112 bool HasVehicleNear(ActorId id, float distance, carla::geom::Location direction);
113 /// make agent look at some location
115 /// return if the agent has been killed by a vehicle
116 bool IsWalkerAlive(ActorId id, bool &alive);
117
118 dtCrowd *GetCrowd() { return _crowd; };
119
120 /// return the last delta seconds
121 double GetDeltaSeconds() { return _delta_seconds; };
122
123 private:
124
125 bool _ready { false };
126 std::vector<uint8_t> _binary_mesh;
127 double _delta_seconds { 0.0 };
128 /// Simulation time of the snapshot the crowd last advanced to; unset before the first update.
129 std::optional<double> _last_elapsed_seconds;
130 /// meshes
131 dtNavMesh *_nav_mesh { nullptr };
132 dtNavMeshQuery *_nav_query { nullptr };
133 /// crowd
134 dtCrowd *_crowd { nullptr };
135 /// mapping Id
136 std::unordered_map<ActorId, int> _mapped_walkers_id;
137 std::unordered_map<ActorId, int> _mapped_vehicles_id;
138 // mapping by index also
139 std::unordered_map<int, ActorId> _mapped_by_index;
140 /// store walkers yaw angle from previous tick
141 std::unordered_map<ActorId, float> _yaw_walkers;
142 /// saves the position of each actor at intervals and check if any is blocked
143 std::unordered_map<int, carla::geom::Vector3D> _walkers_blocked_position;
144 double _time_to_unblock { 0.0 };
145
146 /// walker manager for the route planning with events
148
149 std::weak_ptr<carla::client::detail::Simulator> _simulator;
150
151 mutable std::mutex _mutex;
152
153 float _probability_crossing { 0.0f };
154
155 /// assign a filter index to an agent
156 void SetAgentFilter(int agent_index, int filter_index);
157 };
158
159} // namespace nav
160} // namespace carla
NonCopyable()=default
Represents the state of all the actors of an episode at a given frame.
A rigid transform in CARLA's / Unreal's left-handed frame: x forward, y right, z up,...
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
Definition Navigation.h:137
std::unordered_map< ActorId, int > _mapped_walkers_id
mapping Id
Definition Navigation.h:136
bool RemoveAgent(ActorId id)
remove an agent
std::weak_ptr< carla::client::detail::Simulator > _simulator
Definition Navigation.h:149
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
Definition Navigation.h:131
bool UpdateVehicles(std::vector< VehicleCollisionInfo > vehicles)
add/update/delete vehicles in crowd
std::unordered_map< int, ActorId > _mapped_by_index
Definition Navigation.h:139
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
Definition Navigation.h:147
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
Definition Navigation.h:141
dtCrowd * _crowd
crowd
Definition Navigation.h:134
std::optional< double > _last_elapsed_seconds
Simulation time of the snapshot the crowd last advanced to; unset before the first update.
Definition Navigation.h:129
bool AddWalker(ActorId id, carla::geom::Location from)
create a new walker
std::vector< uint8_t > _binary_mesh
Definition Navigation.h:126
double GetDeltaSeconds()
return the last delta seconds
Definition Navigation.h:121
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
Definition Navigation.h:132
void CreateCrowd(void)
create the crowd object
bool AddOrUpdateVehicle(VehicleCollisionInfo &vehicle)
create a new vehicle in crowd to be avoided by walkers
std::unordered_map< int, carla::geom::Vector3D > _walkers_blocked_position
saves the position of each actor at intervals and check if any is blocked
Definition Navigation.h:143
@ CARLA_AREA_CROSSWALK
Definition Navigation.h:31
@ CARLA_AREA_SIDEWALK
Definition Navigation.h:30
@ CARLA_AREA_GRASS
Definition Navigation.h:33
@ CARLA_AREA_BLOCK
Definition Navigation.h:29
@ CARLA_AREA_ROAD
Definition Navigation.h:32
@ CARLA_TYPE_CROSSWALK
Definition Navigation.h:40
@ CARLA_TYPE_GRASS
Definition Navigation.h:42
@ CARLA_TYPE_WALKABLE
Definition Navigation.h:45
@ CARLA_TYPE_SIDEWALK
Definition Navigation.h:39
@ CARLA_TYPE_NONE
Definition Navigation.h:38
@ CARLA_TYPE_ROAD
Definition Navigation.h:41
uint32_t ActorId
Definition ActorId.h:14
This file contains definitions of common data structures used in traffic manager.
rpc::ActorId ActorId
Definition ActorId.h:18
struct to send info about vehicles to the crowd
Definition Navigation.h:49
carla::geom::BoundingBox bounding
Definition Navigation.h:52
carla::geom::Transform transform
Definition Navigation.h:51