CARLA
Loading...
Searching...
No Matches
mirroredActors.h
Go to the documentation of this file.
1// Copyright (c) 2026 Computer Vision Center (CVC) at the Universitat Autonoma de Barcelona (UAB).
2// This work is licensed under the terms of the MIT license.
3// For a copy, see <https://opensource.org/licenses/MIT>.
4
5#pragma once
6
7#include <cstdint>
8#include <optional>
9#include <unordered_map>
10
11namespace carla {
12namespace multigpu {
13
14 /// Primary actor id -> id of the local actor a secondary replayed it onto,
15 /// for the current episode only.
16 using MirroredActorMap = std::unordered_map<uint32_t, uint32_t>;
17
19 /// Local actor to reuse; if empty, spawn a new one.
20 std::optional<uint32_t> reuse;
21 /// Id to request for the new actor, 0 for a fresh one.
22 uint32_t spawn_id = 0u;
23 };
24
25 /// Where a secondary replays the add event of primary actor @a primary_id.
26 /// Local ids follow the secondary's own counter, so a local actor whose id
27 /// equals @a primary_id may be another actor, and is only reused if it is
28 /// a map actor that mirrors no other primary actor. @a local provides:
29 /// bool IsSameKind(id): the local actor exists with the replayed blueprint.
30 /// std::optional<uint32_t> FindMapActor(): the map-registered local actor
31 /// that is the replayed one (the spectator, a prop at its place), if any.
32 /// bool IsMapActorLike(id): a map-registered local actor matching the
33 /// replayed description.
34 /// bool Contains(id): the id is taken, dormant actors included.
35 template <typename LocalActors>
37 const MirroredActorMap &mirrors,
38 uint32_t primary_id,
39 const LocalActors &local) {
40 const auto mirror = mirrors.find(primary_id);
41 if ((mirror != mirrors.end()) && local.IsSameKind(mirror->second)) {
42 return {mirror->second, 0u};
43 }
44
45 const auto is_free_map_actor = [&](uint32_t local_id) {
46 for (const auto &item : mirrors) {
47 if (item.second == local_id) {
48 return false;
49 }
50 }
51 return local.IsMapActorLike(local_id);
52 };
53
54 const std::optional<uint32_t> map_actor = local.FindMapActor();
55 if (map_actor && is_free_map_actor(*map_actor)) {
56 return {map_actor, 0u};
57 }
58 if (is_free_map_actor(primary_id)) {
59 return {primary_id, 0u};
60 }
61 return {std::nullopt, local.Contains(primary_id) ? 0u : primary_id};
62 }
63
64} // namespace multigpu
65} // namespace carla
ReplayedActorPlan PlanReplayedActor(const MirroredActorMap &mirrors, uint32_t primary_id, const LocalActors &local)
Where a secondary replays the add event of primary actor primary_id.
std::unordered_map< uint32_t, uint32_t > MirroredActorMap
Primary actor id -> id of the local actor a secondary replayed it onto, for the current episode only.
This file contains definitions of common data structures used in traffic manager.
std::optional< uint32_t > reuse
Local actor to reuse; if empty, spawn a new one.
uint32_t spawn_id
Id to request for the new actor, 0 for a fresh one.