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