CARLA
Navigation.h
Go to the documentation of this file.
1 // Copyright (c) 2019 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"
11 #include "carla/geom/BoundingBox.h"
12 #include "carla/geom/Location.h"
13 #include "carla/geom/Transform.h"
15 #include "carla/rpc/ActorId.h"
16 #include <recast/Recast.h>
17 #include <recast/DetourCrowd.h>
18 #include <recast/DetourNavMesh.h>
19 #include <recast/DetourNavMeshBuilder.h>
20 #include <recast/DetourNavMeshQuery.h>
21 #include <recast/DetourCommon.h>
22 
23 namespace carla {
24 namespace nav {
25 
26  enum NavAreas {
32  };
33 
35  {
41  CARLA_TYPE_ALL = 0xffff,
42 
44  };
45 
46  /// struct to send info about vehicles to the crowd
51  };
52 
53  /// Manage the pedestrians navigation, using the Recast & Detour library for low level calculations.
54  ///
55  /// This class gets the binary content of the map from the server, which is required for the path finding.
56  /// Then this class can add or remove pedestrians, and also set target points to walk for each one.
57  class Navigation : private NonCopyable {
58 
59  public:
60 
61  Navigation();
62  ~Navigation();
63 
64  /// load navigation data
65  bool Load(const std::string &filename);
66  /// load navigation data from memory
67  bool Load(std::vector<uint8_t> content);
68  /// return the path points to go from one position to another
69  bool GetPath(carla::geom::Location from, carla::geom::Location to, dtQueryFilter * filter,
70  std::vector<carla::geom::Location> &path, std::vector<unsigned char> &area);
71  bool GetAgentRoute(ActorId id, carla::geom::Location from, carla::geom::Location to,
72  std::vector<carla::geom::Location> &path, std::vector<unsigned char> &area);
73 
74  /// reference to the simulator to access API functions
75  void SetSimulator(std::weak_ptr<carla::client::detail::Simulator> simulator);
76  /// set the seed to use with random numbers
77  void SetSeed(unsigned int seed);
78  /// create the crowd object
79  void CreateCrowd(void);
80  /// create a new walker
81  bool AddWalker(ActorId id, carla::geom::Location from);
82  /// create a new vehicle in crowd to be avoided by walkers
83  bool AddOrUpdateVehicle(VehicleCollisionInfo &vehicle);
84  /// remove an agent
85  bool RemoveAgent(ActorId id);
86  /// add/update/delete vehicles in crowd
87  bool UpdateVehicles(std::vector<VehicleCollisionInfo> vehicles);
88  /// set new max speed
89  bool SetWalkerMaxSpeed(ActorId id, float max_speed);
90  /// set a new target point to go through a route with events
91  bool SetWalkerTarget(ActorId id, carla::geom::Location to);
92  // set a new target point to go directly without events
93  bool SetWalkerDirectTarget(ActorId id, carla::geom::Location to);
94  bool SetWalkerDirectTargetIndex(int index, carla::geom::Location to);
95  /// get the walker current transform
96  bool GetWalkerTransform(ActorId id, carla::geom::Transform &trans);
97  /// get the walker current location
98  bool GetWalkerPosition(ActorId id, carla::geom::Location &location);
99  /// get the walker current transform
100  float GetWalkerSpeed(ActorId id);
101  /// update all walkers in crowd
102  void UpdateCrowd(const client::detail::EpisodeState &state);
103  /// get a random location for navigation
104  bool GetRandomLocation(carla::geom::Location &location, dtQueryFilter * filter = nullptr) const;
105  /// set the probability that an agent could cross the roads in its path following
106  void SetPedestriansCrossFactor(float percentage);
107  /// set an agent as paused for the crowd
108  void PauseAgent(ActorId id, bool pause);
109  /// return if the agent has a vehicle near (as neighbour)
110  bool HasVehicleNear(ActorId id, float distance, carla::geom::Location direction);
111  /// make agent look at some location
112  bool SetWalkerLookAt(ActorId id, carla::geom::Location location);
113  /// return if the agent has been killed by a vehicle
114  bool IsWalkerAlive(ActorId id, bool &alive);
115 
116  dtCrowd *GetCrowd() { return _crowd; };
117 
118  /// return the last delta seconds
119  double GetDeltaSeconds() { return _delta_seconds; };
120 
121  private:
122 
123  bool _ready { false };
124  std::vector<uint8_t> _binary_mesh;
125  double _delta_seconds { 0.0 };
126  /// meshes
127  dtNavMesh *_nav_mesh { nullptr };
128  dtNavMeshQuery *_nav_query { nullptr };
129  /// crowd
130  dtCrowd *_crowd { nullptr };
131  /// mapping Id
132  std::unordered_map<ActorId, int> _mapped_walkers_id;
133  std::unordered_map<ActorId, int> _mapped_vehicles_id;
134  // mapping by index also
135  std::unordered_map<int, ActorId> _mapped_by_index;
136  /// store walkers yaw angle from previous tick
137  std::unordered_map<ActorId, float> _yaw_walkers;
138  /// saves the position of each actor at intervals and check if any is blocked
139  std::unordered_map<int, carla::geom::Vector3D> _walkers_blocked_position;
140  double _time_to_unblock { 0.0 };
141 
142  /// walker manager for the route planning with events
144 
145  std::weak_ptr<carla::client::detail::Simulator> _simulator;
146 
147  mutable std::mutex _mutex;
148 
149  float _probability_crossing { 0.0f };
150 
151  /// assign a filter index to an agent
152  void SetAgentFilter(int agent_index, int filter_index);
153  };
154 
155 } // namespace nav
156 } // namespace carla
std::unordered_map< ActorId, int > _mapped_vehicles_id
Definition: Navigation.h:133
std::vector< uint8_t > _binary_mesh
Definition: Navigation.h:124
std::unordered_map< int, ActorId > _mapped_by_index
Definition: Navigation.h:135
double GetDeltaSeconds()
return the last delta seconds
Definition: Navigation.h:119
rpc::ActorId ActorId
Definition: ActorId.h:18
carla::rpc::ActorId id
Definition: Navigation.h:48
std::unordered_map< ActorId, int > _mapped_walkers_id
mapping Id
Definition: Navigation.h:132
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
Represents the state of all the actors of an episode at a given frame.
Definition: EpisodeState.h:27
dtCrowd * GetCrowd()
Definition: Navigation.h:116
static EpisodeProxyPointerType::Shared Load(EpisodeProxyPointerType::Strong ptr)
carla::geom::BoundingBox bounding
Definition: Navigation.h:50
WalkerManager _walker_manager
walker manager for the route planning with events
Definition: Navigation.h:143
carla::geom::Transform transform
Definition: Navigation.h:49
std::unordered_map< ActorId, float > _yaw_walkers
store walkers yaw angle from previous tick
Definition: Navigation.h:137
std::weak_ptr< carla::client::detail::Simulator > _simulator
Definition: Navigation.h:145
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:139
uint32_t ActorId
Definition: ActorId.h:14
struct to send info about vehicles to the crowd
Definition: Navigation.h:47
Inherit (privately) to suppress copy/move construction and assignment.
Manage the pedestrians navigation, using the Recast & Detour library for low level calculations...
Definition: Navigation.h:57