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  /// create the crowd object
75  void CreateCrowd(void);
76  /// create a new walker
77  bool AddWalker(ActorId id, carla::geom::Location from);
78  /// create a new vehicle in crowd to be avoided by walkers
79  bool AddOrUpdateVehicle(VehicleCollisionInfo &vehicle);
80  /// remove an agent
81  bool RemoveAgent(ActorId id);
82  /// add/update/delete vehicles in crowd
83  bool UpdateVehicles(std::vector<VehicleCollisionInfo> vehicles);
84  /// set new max speed
85  bool SetWalkerMaxSpeed(ActorId id, float max_speed);
86  /// set a new target point to go through a route with events
87  bool SetWalkerTarget(ActorId id, carla::geom::Location to);
88  // set a new target point to go directly without events
89  bool SetWalkerDirectTarget(ActorId id, carla::geom::Location to);
90  bool SetWalkerDirectTargetIndex(int index, carla::geom::Location to);
91  /// get the walker current transform
92  bool GetWalkerTransform(ActorId id, carla::geom::Transform &trans);
93  /// get the walker current location
94  bool GetWalkerPosition(ActorId id, carla::geom::Location &location);
95  /// get the walker current transform
96  float GetWalkerSpeed(ActorId id);
97  /// update all walkers in crowd
98  void UpdateCrowd(const client::detail::EpisodeState &state);
99  /// get a random location for navigation
100  bool GetRandomLocation(carla::geom::Location &location, dtQueryFilter * filter = nullptr) const;
101  /// set the probability that an agent could cross the roads in its path following
102  void SetPedestriansCrossFactor(float percentage);
103  /// set an agent as paused for the crowd
104  void PauseAgent(ActorId id, bool pause);
105  /// return if the agent has a vehicle near (as neighbour)
106  bool HasVehicleNear(ActorId id, float distance, carla::geom::Location direction);
107  /// make agent look at some location
108  bool SetWalkerLookAt(ActorId id, carla::geom::Location location);
109 
110  dtCrowd *GetCrowd() { return _crowd; };
111 
112  /// return the last delta seconds
113  double GetDeltaSeconds() { return _delta_seconds; };
114 
115  private:
116 
117  bool _ready { false };
118  std::vector<uint8_t> _binary_mesh;
119  double _delta_seconds { 0.0 };
120  /// meshes
121  dtNavMesh *_nav_mesh { nullptr };
122  dtNavMeshQuery *_nav_query { nullptr };
123  /// crowd
124  dtCrowd *_crowd { nullptr };
125  /// mapping Id
126  std::unordered_map<ActorId, int> _mapped_walkers_id;
127  std::unordered_map<ActorId, int> _mapped_vehicles_id;
128  // mapping by index also
129  std::unordered_map<int, ActorId> _mapped_by_index;
130  /// store walkers yaw angle from previous tick
131  std::unordered_map<ActorId, float> _yaw_walkers;
132  /// saves the position of each actor at intervals and check if any is blocked
133  std::unordered_map<int, carla::geom::Vector3D> _walkers_blocked_position;
134  double _time_to_unblock { 0.0 };
135 
136  /// walker manager for the route planning with events
138 
139  mutable std::mutex _mutex;
140 
141  float _probability_crossing { 0.0f };
142 
143  /// assign a filter index to an agent
144  void SetAgentFilter(int agent_index, int filter_index);
145  };
146 
147 } // namespace nav
148 } // namespace carla
std::unordered_map< ActorId, int > _mapped_vehicles_id
Definition: Navigation.h:127
std::vector< uint8_t > _binary_mesh
Definition: Navigation.h:118
std::unordered_map< int, ActorId > _mapped_by_index
Definition: Navigation.h:129
double GetDeltaSeconds()
return the last delta seconds
Definition: Navigation.h:113
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:126
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:99
Represents the state of all the actors of an episode at a given frame.
Definition: EpisodeState.h:26
dtCrowd * GetCrowd()
Definition: Navigation.h:110
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:137
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:131
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:133
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