CARLA
Loading...
Searching...
No Matches
SensorFactory.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
8#include "Carla.h"
11#include "Carla/Sensor/Sensor.h"
13
14#include <util/ue-header-guard-begin.h>
16#define LIBCARLA_SENSOR_REGISTRY_WITH_SENSOR_INCLUDES
18#undef LIBCARLA_SENSOR_REGISTRY_WITH_SENSOR_INCLUDES
19#include <util/ue-header-guard-end.h>
20
21#include <type_traits>
22
23// =============================================================================
24// -- FSensorDefinitionGatherer ------------------------------------------------
25// =============================================================================
26
27/// Retrieve the definitions of all the sensors registered in the
28/// SensorRegistry by calling their static method
29/// SensorType::GetSensorDefinition().
30///
31/// @note To make this class ignore a given sensor, define a public member
32/// "not_spawnable" that defines a type. If so, the sensor won't be spawned by
33/// this factory.
35{
37
38public:
39
41 {
42 TArray<FActorDefinition> Definitions;
43 Definitions.Reserve(Registry::size());
44 AppendDefinitions(Definitions, std::make_index_sequence<Registry::size()>());
45 return Definitions;
46 }
47
48private:
49
50 // Type traits for detecting if a sensor is spawnable.
51
52 template<typename T>
53 struct void_type { typedef void type; };
54
55 template<typename T, typename = void>
56 struct is_spawnable : std::true_type {};
57
58 template<typename T>
59 struct is_spawnable<T, typename void_type<typename T::not_spawnable>::type > : std::false_type {};
60
61 // AppendDefinitions implementations.
62
63 template <typename SensorType>
64 static typename std::enable_if<is_spawnable<SensorType>::value, void>::type
65 AppendDefinitions(TArray<FActorDefinition> &Definitions)
66 {
67 auto Def = SensorType::GetSensorDefinition();
68 // Make sure the class matches the sensor type.
69 Def.Class = SensorType::StaticClass();
70 Definitions.Emplace(Def);
71 }
72
73 template <typename SensorType>
74 static typename std::enable_if<!is_spawnable<SensorType>::value, void>::type
75 AppendDefinitions(TArray<FActorDefinition> &) {}
76
77 template <size_t Index>
78 static void AppendDefinitions(TArray<FActorDefinition> &Definitions)
79 {
80 using SensorPtrType = typename Registry::get_by_index<Index>::key;
81 using SensorType = typename std::remove_pointer<SensorPtrType>::type;
82 AppendDefinitions<SensorType>(Definitions);
83 }
84
85 template <size_t... Is>
86 static void AppendDefinitions(
87 TArray<FActorDefinition> &Definitions,
88 std::index_sequence<Is...>)
89 {
90 std::initializer_list<int> ({(AppendDefinitions<Is>(Definitions), 0)...});
91 }
92};
93
94// =============================================================================
95// -- ASensorFactory -----------------------------------------------------------
96// =============================================================================
97
102
104 const FTransform &Transform,
105 const FActorDescription &Description)
106{
107 auto *World = GetWorld();
108 if (World == nullptr)
109 {
110 UE_LOG(LogCarla, Error, TEXT("ASensorFactory: cannot spawn sensor into an empty world."));
111 return {};
112 }
113
115 if (GameInstance == nullptr)
116 {
117 UE_LOG(LogCarla, Error, TEXT("ASensorFactory: cannot spawn sensor, incompatible game instance."));
118 return {};
119 }
120
121 auto *Sensor = World->SpawnActorDeferred<ASensor>(
122 Description.Class,
123 Transform,
124 this,
125 nullptr,
126 ESpawnActorCollisionHandlingMethod::AlwaysSpawn);
127 if (Sensor == nullptr)
128 {
129 UE_LOG(LogCarla, Error, TEXT("ASensorFactory: spawn sensor failed."));
130 }
131 else
132 {
133 auto *Episode = GameInstance->GetCarlaEpisode();
134 check(Episode != nullptr);
135
136 Sensor->SetEpisode(*Episode);
137 Sensor->Set(Description);
138 Sensor->SetDataStream(GameInstance->GetServer().OpenStream());
139 // ASceneCaptureSensor * SceneCaptureSensor = Cast<ASceneCaptureSensor>(Sensor);
140 // if(SceneCaptureSensor)
141 // {
142 // SceneCaptureSensor->CameraGBuffers.SceneColor.SetDataStream(GameInstance->GetServer().OpenStream());
143 // SceneCaptureSensor->CameraGBuffers.SceneDepth.SetDataStream(GameInstance->GetServer().OpenStream());
144 // SceneCaptureSensor->CameraGBuffers.SceneStencil.SetDataStream(GameInstance->GetServer().OpenStream());
145 // SceneCaptureSensor->CameraGBuffers.GBufferA.SetDataStream(GameInstance->GetServer().OpenStream());
146 // SceneCaptureSensor->CameraGBuffers.GBufferB.SetDataStream(GameInstance->GetServer().OpenStream());
147 // SceneCaptureSensor->CameraGBuffers.GBufferC.SetDataStream(GameInstance->GetServer().OpenStream());
148 // SceneCaptureSensor->CameraGBuffers.GBufferD.SetDataStream(GameInstance->GetServer().OpenStream());
149 // SceneCaptureSensor->CameraGBuffers.GBufferE.SetDataStream(GameInstance->GetServer().OpenStream());
150 // SceneCaptureSensor->CameraGBuffers.GBufferF.SetDataStream(GameInstance->GetServer().OpenStream());
151 // SceneCaptureSensor->CameraGBuffers.Velocity.SetDataStream(GameInstance->GetServer().OpenStream());
152 // SceneCaptureSensor->CameraGBuffers.SSAO.SetDataStream(GameInstance->GetServer().OpenStream());
153 // SceneCaptureSensor->CameraGBuffers.CustomDepth.SetDataStream(GameInstance->GetServer().OpenStream());
154 // SceneCaptureSensor->CameraGBuffers.CustomStencil.SetDataStream(GameInstance->GetServer().OpenStream());
155 // }
156 }
157 UGameplayStatics::FinishSpawningActor(Sensor, Transform);
158 return FActorSpawnResult{Sensor};
159}
FActorSpawnResult SpawnActor(const FTransform &SpawnAtTransform, const FActorDescription &ActorDescription) final
Spawn an actor based on ActorDescription and Transform.
TArray< FActorDefinition > GetDefinitions() final
Retrieve the definitions of all the sensors registered in the SensorRegistry.
FDataStream OpenStream() const
Retrieve the definitions of all the sensors registered in the SensorRegistry by calling their static ...
static auto GetSensorDefinitions()
static std::enable_if<!is_spawnable< SensorType >::value, void >::type AppendDefinitions(TArray< FActorDefinition > &)
static void AppendDefinitions(TArray< FActorDefinition > &Definitions)
static void AppendDefinitions(TArray< FActorDefinition > &Definitions, std::index_sequence< Is... >)
static std::enable_if< is_spawnable< SensorType >::value, void >::type AppendDefinitions(TArray< FActorDefinition > &Definitions)
The game instance contains elements that must be kept alive in between levels.
const FCarlaServer & GetServer() const
UCarlaEpisode * GetCarlaEpisode()
static UCarlaGameInstance * GetGameInstance(const UObject *WorldContextObject)
Compile-time map for mapping sensor objects to serializers.
CompositeSerializer< std::pair< ACollisionSensor *, s11n::CollisionEventSerializer >, std::pair< ADepthCamera *, s11n::ImageSerializer >, std::pair< ANormalsCamera *, s11n::NormalsImageSerializer >, std::pair< ADVSCamera *, s11n::DVSEventArraySerializer >, std::pair< AGnssSensor *, s11n::GnssSerializer >, std::pair< AInertialMeasurementUnit *, s11n::IMUSerializer >, std::pair< ALaneInvasionSensor *, s11n::NoopSerializer >, std::pair< AObstacleDetectionSensor *, s11n::ObstacleDetectionEventSerializer >, std::pair< AOpticalFlowCamera *, s11n::OpticalFlowImageSerializer >, std::pair< ARadar *, s11n::RadarSerializer >, std::pair< ARayCastSemanticLidar *, s11n::SemanticLidarSerializer >, std::pair< ARayCastLidar *, s11n::LidarSerializer >, std::pair< ARssSensor *, s11n::NoopSerializer >, std::pair< ASceneCaptureCamera *, s11n::ImageSerializer >, std::pair< ASceneCaptureCamera_RayTracedLens *, s11n::ImageSerializer >, std::pair< ASemanticSegmentationCamera *, s11n::ImageSerializer >, std::pair< AInstanceSegmentationCamera *, s11n::ImageSerializer >, std::pair< FWorldObserver *, s11n::EpisodeStateSerializer >, std::pair< ASceneCaptureCamera_WideAngleLens *, s11n::ImageSerializer >, std::pair< ADepthCamera_WideAngleLens *, s11n::ImageSerializer >, std::pair< AInstanceSegmentationCamera_WideAngleLens *, s11n::ImageSerializer >, std::pair< ASemanticSegmentationCamera_WideAngleLens *, s11n::ImageSerializer >, std::pair< FCameraGBufferUint8 *, s11n::GBufferUint8Serializer >, std::pair< FCameraGBufferFloat *, s11n::GBufferFloatSerializer >, std::pair< AHSSLidar *, s11n::LidarSerializer >, std::pair< AV2XSensor *, s11n::CAMDataSerializer >, std::pair< ACustomV2XSensor *, s11n::CustomV2XDataSerializer >, std::pair< AVehicleStatusSensor *, s11n::VehicleStatusSerializer >, std::pair< AAutowareGnssSensor *, s11n::GnssSerializer >, std::pair< ASceneCaptureCamera_RayTracedLensDistance *, s11n::DistanceImageSerializer >, std::pair< ASceneCaptureCamera_RayTracedLensInstance *, s11n::ImageSerializer > > SensorRegistry
Contains a registry of all the sensors available and allows serializing and deserializing sensor data...
A description of a Carla Actor with all its variation.
TSubclassOf< AActor > Class
Class of the actor to be spawned.
Result of an actor spawn function.
typename detail::CompileTimeTypeMapImpl< sizeof...(Items), Items... >::template get_by_index< Index > get_by_index