CARLA
Toggle main menu visibility
Loading...
Searching...
No Matches
LibCarla
source
carla
client
detail
WalkerNavigation.cpp
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
#include "
carla/client/detail/WalkerNavigation.h
"
8
9
#include "
carla/client/detail/Client.h
"
10
#include "
carla/client/detail/Episode.h
"
11
#include "
carla/client/detail/EpisodeState.h
"
12
#include "
carla/client/detail/Simulator.h
"
13
#include "
carla/nav/Navigation.h
"
14
#include "
carla/rpc/Command.h
"
15
#include "
carla/rpc/DebugShape.h
"
16
#include "
carla/rpc/WalkerControl.h
"
17
18
#include <sstream>
19
20
namespace
carla
{
21
namespace
client
{
22
namespace
detail
{
23
24
WalkerNavigation::WalkerNavigation
(std::weak_ptr<Simulator> simulator,
bool
server_side)
25
:
_simulator
(simulator),
26
_next_check_index
(0),
27
_server_side
(server_side) {
28
_nav
.
SetSimulator
(simulator);
29
if
(
_server_side
) {
30
// The server hosts the navigation (World-Partitioned navmesh): no .bin
31
// navmesh is downloaded and the client-side Detour crowd stays empty.
32
return
;
33
}
34
// Here call the server to retrieve the navmesh data.
35
auto
files =
_simulator
.lock()->GetRequiredFiles(
"Nav"
);
36
if
(!files.empty()) {
37
_nav.Load(_simulator.lock()->GetCacheFile(files[0], true));
38
}
39
}
40
41
void
WalkerNavigation::Tick
(std::shared_ptr<Episode> episode) {
42
if
(
_server_side
) {
43
// Walkers are driven by the server's crowd; pushing client-side
44
// WalkerControl states here would fight the server controller.
45
return
;
46
}
47
auto
walkers =
_walkers
.Load();
48
if
(walkers->empty()) {
49
return
;
50
}
51
52
// get current state
53
std::shared_ptr<const EpisodeState> state = episode->GetState();
54
55
// purge all possible dead walkers
56
CheckIfWalkerExist
(*walkers, *state);
57
58
// add/update/delete all vehicles in crowd
59
UpdateVehiclesInCrowd
(episode,
false
);
60
61
// update crowd in navigation module
62
_nav
.
UpdateCrowd
(*state);
63
64
carla::geom::Transform
trans;
65
using
Cmd =
rpc::Command
;
66
std::vector<Cmd> commands;
67
commands.reserve(walkers->size());
68
for
(
auto
handle : *walkers) {
69
// get the transform of the walker
70
if
(
_nav
.
GetWalkerTransform
(handle.walker, trans)) {
71
float
speed =
_nav
.
GetWalkerSpeed
(handle.walker);
72
commands.emplace_back(Cmd::ApplyWalkerState{ handle.walker, trans, speed });
73
}
74
}
75
_simulator
.lock()->ApplyBatchSync(std::move(commands),
false
);
76
77
// check if any agent has been killed
78
bool
alive;
79
for
(
auto
handle : *walkers) {
80
// get the agent state
81
if
(
_nav
.
IsWalkerAlive
(handle.walker, alive)) {
82
if
(!alive) {
83
_simulator
.lock()->SetActorDead(handle.walker);
84
// remove from the crowd
85
_nav
.
RemoveAgent
(handle.walker);
86
// destroy the controller
87
_simulator
.lock()->DestroyActor(handle.controller);
88
// unregister from list
89
UnregisterWalker
(handle.walker, handle.controller);
90
}
91
}
92
}
93
}
94
95
void
WalkerNavigation::CheckIfWalkerExist
(std::vector<WalkerHandle> walkers,
const
EpisodeState
&state) {
96
97
// check with total
98
if
(
_next_check_index
>= walkers.size())
99
_next_check_index
= 0;
100
101
// check the existence
102
if
(!state.
ContainsActorSnapshot
(walkers[
_next_check_index
].walker)) {
103
// remove from the crowd
104
_nav
.
RemoveAgent
(walkers[
_next_check_index
].walker);
105
// destroy the controller
106
_simulator
.lock()->DestroyActor(walkers[
_next_check_index
].controller);
107
// unregister from list
108
UnregisterWalker
(walkers[
_next_check_index
].walker, walkers[
_next_check_index
].controller);
109
}
110
111
++
_next_check_index
;
112
113
}
114
115
// add/update/delete all vehicles in crowd
116
void
WalkerNavigation::UpdateVehiclesInCrowd
(std::shared_ptr<Episode> episode,
bool
show_debug) {
117
std::vector<carla::nav::VehicleCollisionInfo> vehicles;
118
119
// get current state
120
std::shared_ptr<const EpisodeState> state = episode->GetState();
121
122
// get all vehicles from episode
123
for
(
auto
&&actor : episode->GetActors()) {
124
// only vehicles
125
if
(actor.description.id.rfind(
"vehicle."
, 0) == 0) {
126
// get the snapshot
127
ActorSnapshot
snapshot = state->GetActorSnapshot(actor.id);
128
// add to the vector
129
vehicles.emplace_back(
carla::nav::VehicleCollisionInfo
{actor.id, snapshot.
transform
, actor.bounding_box});
130
}
131
}
132
133
// update the vehicles found
134
_nav
.
UpdateVehicles
(vehicles);
135
136
// optional debug info
137
if
(show_debug) {
138
if
(
_nav
.
GetCrowd
() ==
nullptr
)
return
;
139
140
// draw bounding boxes for debug
141
for
(
int
i = 0; i <
_nav
.
GetCrowd
()->getAgentCount(); ++i) {
142
// get the agent
143
const
dtCrowdAgent *agent =
_nav
.
GetCrowd
()->getAgent(i);
144
if
(agent && agent->params.useObb) {
145
// draw for debug
146
carla::geom::Location
p1, p2, p3, p4;
147
p1.
x
= agent->params.obb[0];
148
p1.
z
= agent->params.obb[1];
149
p1.
y
= agent->params.obb[2];
150
p2.
x
= agent->params.obb[3];
151
p2.
z
= agent->params.obb[4];
152
p2.
y
= agent->params.obb[5];
153
p3.
x
= agent->params.obb[6];
154
p3.
z
= agent->params.obb[7];
155
p3.
y
= agent->params.obb[8];
156
p4.
x
= agent->params.obb[9];
157
p4.
z
= agent->params.obb[10];
158
p4.
y
= agent->params.obb[11];
159
carla::rpc::DebugShape
line1;
160
line1.
life_time
= 0.01f;
161
line1.
persistent_lines
=
false
;
162
// line 1
163
line1.
primitive
=
carla::rpc::DebugShape::Line
{p1, p2, 0.2f};
164
line1.
color
= { 0, 255, 0 };
165
_simulator
.lock()->DrawDebugShape(line1);
166
// line 2
167
line1.
primitive
=
carla::rpc::DebugShape::Line
{p2, p3, 0.2f};
168
line1.
color
= { 255, 0, 0 };
169
_simulator
.lock()->DrawDebugShape(line1);
170
// line 3
171
line1.
primitive
=
carla::rpc::DebugShape::Line
{p3, p4, 0.2f};
172
line1.
color
= { 0, 0, 255 };
173
_simulator
.lock()->DrawDebugShape(line1);
174
// line 4
175
line1.
primitive
=
carla::rpc::DebugShape::Line
{p4, p1, 0.2f};
176
line1.
color
= { 255, 255, 0 };
177
_simulator
.lock()->DrawDebugShape(line1);
178
}
179
}
180
181
// draw some text for debug
182
for
(
int
i = 0; i <
_nav
.
GetCrowd
()->getAgentCount(); ++i) {
183
// get the agent
184
const
dtCrowdAgent *agent =
_nav
.
GetCrowd
()->getAgent(i);
185
if
(agent) {
186
// draw for debug
187
carla::geom::Location
p1(agent->npos[0], agent->npos[2], agent->npos[1] + 1);
188
if
(agent->params.userData) {
189
std::ostringstream out;
190
out << *(reinterpret_cast<const float *>(agent->params.userData));
191
carla::rpc::DebugShape
text;
192
text.
life_time
= 0.01f;
193
text.
persistent_lines
=
false
;
194
text.
primitive
=
carla::rpc::DebugShape::String
{p1, out.str(),
false
};
195
text.
color
= { 0, 255, 0 };
196
_simulator
.lock()->DrawDebugShape(text);
197
}
198
}
199
}
200
}
201
}
202
203
}
// namespace detail
204
}
// namespace client
205
}
// namespace carla
Command.h
DebugShape.h
EpisodeState.h
Episode.h
WalkerControl.h
Navigation.h
Simulator.h
WalkerNavigation.h
carla::client::detail::EpisodeState
Represents the state of all the actors of an episode at a given frame.
Definition
EpisodeState.h:29
carla::client::detail::EpisodeState::ContainsActorSnapshot
bool ContainsActorSnapshot(ActorId actor_id) const
Definition
EpisodeState.h:63
carla::client::detail::WalkerNavigation::_next_check_index
unsigned long _next_check_index
Definition
WalkerNavigation.h:102
carla::client::detail::WalkerNavigation::UpdateVehiclesInCrowd
void UpdateVehiclesInCrowd(std::shared_ptr< Episode > episode, bool show_debug=false)
add/update/delete all vehicles in crowd
Definition
WalkerNavigation.cpp:116
carla::client::detail::WalkerNavigation::_simulator
std::weak_ptr< Simulator > _simulator
Definition
WalkerNavigation.h:100
carla::client::detail::WalkerNavigation::Tick
void Tick(std::shared_ptr< Episode > episode)
Definition
WalkerNavigation.cpp:41
carla::client::detail::WalkerNavigation::CheckIfWalkerExist
void CheckIfWalkerExist(std::vector< WalkerHandle > walkers, const EpisodeState &state)
check a few walkers and if they don't exist then remove from the crowd
Definition
WalkerNavigation.cpp:95
carla::client::detail::WalkerNavigation::WalkerNavigation
WalkerNavigation(std::weak_ptr< Simulator > simulator, bool server_side=false)
Definition
WalkerNavigation.cpp:24
carla::client::detail::WalkerNavigation::UnregisterWalker
void UnregisterWalker(ActorId walker_id, ActorId controller_id)
Definition
WalkerNavigation.h:44
carla::client::detail::WalkerNavigation::_nav
carla::nav::Navigation _nav
Definition
WalkerNavigation.h:106
carla::client::detail::WalkerNavigation::_walkers
AtomicList< WalkerHandle > _walkers
Definition
WalkerNavigation.h:113
carla::client::detail::WalkerNavigation::_server_side
const bool _server_side
Definition
WalkerNavigation.h:104
carla::geom::Transform
A rigid transform in CARLA's / Unreal's left-handed frame: x forward, y right, z up,...
Definition
geom/Transform.h:32
carla::nav::Navigation::UpdateCrowd
void UpdateCrowd(const client::detail::EpisodeState &state)
update all walkers in crowd
Definition
Navigation.cpp:838
carla::nav::Navigation::RemoveAgent
bool RemoveAgent(ActorId id)
remove an agent
Definition
Navigation.cpp:667
carla::nav::Navigation::GetWalkerSpeed
float GetWalkerSpeed(ActorId id)
get the walker current transform
Definition
Navigation.cpp:1044
carla::nav::Navigation::UpdateVehicles
bool UpdateVehicles(std::vector< VehicleCollisionInfo > vehicles)
add/update/delete vehicles in crowd
Definition
Navigation.cpp:713
carla::nav::Navigation::GetWalkerTransform
bool GetWalkerTransform(ActorId id, carla::geom::Transform &trans)
get the walker current transform
Definition
Navigation.cpp:939
carla::nav::Navigation::IsWalkerAlive
bool IsWalkerAlive(ActorId id, bool &alive)
return if the agent has been killed by a vehicle
Definition
Navigation.cpp:1230
carla::nav::Navigation::SetSimulator
void SetSimulator(std::weak_ptr< carla::client::detail::Simulator > simulator)
reference to the simulator to access API functions
Definition
Navigation.cpp:75
carla::nav::Navigation::GetCrowd
dtCrowd * GetCrowd()
Definition
Navigation.h:118
carla::rpc::Command
Definition
Command.h:41
carla::rpc::DebugShape
Definition
DebugShape.h:29
carla::rpc::DebugShape::primitive
std::variant< Point, Line, Arrow, Box, String > primitive
Definition
DebugShape.h:65
carla::rpc::DebugShape::life_time
float life_time
Definition
DebugShape.h:69
carla::rpc::DebugShape::persistent_lines
bool persistent_lines
Definition
DebugShape.h:71
carla::rpc::DebugShape::color
Color color
Definition
DebugShape.h:67
Client.h
carla::client::detail
Definition
AtomicList.h:18
carla::client
Definition
BasicAgent.h:24
carla
This file contains definitions of common data structures used in traffic manager.
Definition
SignComponent.h:21
carla::client::ActorSnapshot
Definition
ActorSnapshot.h:18
carla::client::ActorSnapshot::transform
geom::Transform transform
Definition
ActorSnapshot.h:22
carla::geom::Location
Definition
geom/Location.h:23
carla::geom::Vector3D::y
float y
Definition
geom/Vector3D.h:27
carla::geom::Vector3D::x
float x
Definition
geom/Vector3D.h:25
carla::geom::Vector3D::z
float z
Definition
geom/Vector3D.h:29
carla::nav::VehicleCollisionInfo
struct to send info about vehicles to the crowd
Definition
Navigation.h:49
carla::rpc::DebugShape::Line
Definition
DebugShape.h:38
carla::rpc::DebugShape::String
Definition
DebugShape.h:58
Generated by
1.18.0