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
22
23// The ROS 2 interface receives a camera's type as its SensorRegistry position
24// and reads it as carla::ros2::ESensors: keep the two in lock-step.
25#define CARLA_CHECK_ROS2_SENSOR_TYPE(SensorT, Enum) \
26 static_assert(carla::sensor::SensorRegistry::get<SensorT *>::index == carla::ros2::ESensors::Enum, \
27 "carla::ros2::ESensors::" #Enum " does not match the SensorRegistry position of " #SensorT)
60 "carla::ros2::ESensors needs one entry per SensorRegistry item");
61#undef CARLA_CHECK_ROS2_SENSOR_TYPE
62
63
64#include <type_traits>
65
66// =============================================================================
67// -- FSensorDefinitionGatherer ------------------------------------------------
68// =============================================================================
69
70/// Retrieve the definitions of all the sensors registered in the
71/// SensorRegistry by calling their static method
72/// SensorType::GetSensorDefinition().
73///
74/// @note To make this class ignore a given sensor, define a public member
75/// "not_spawnable" that defines a type. If so, the sensor won't be spawned by
76/// this factory.
78{
80
81public:
82
84 {
85 TArray<FActorDefinition> Definitions;
86 Definitions.Reserve(Registry::size());
87 AppendDefinitions(Definitions, std::make_index_sequence<Registry::size()>());
88 return Definitions;
89 }
90
91private:
92
93 // Type traits for detecting if a sensor is spawnable.
94
95 template<typename T>
96 struct void_type { typedef void type; };
97
98 template<typename T, typename = void>
99 struct is_spawnable : std::true_type {};
100
101 template<typename T>
102 struct is_spawnable<T, typename void_type<typename T::not_spawnable>::type > : std::false_type {};
103
104 // AppendDefinitions implementations.
105
106 template <typename SensorType>
107 static typename std::enable_if<is_spawnable<SensorType>::value, void>::type
108 AppendDefinitions(TArray<FActorDefinition> &Definitions)
109 {
110 auto Def = SensorType::GetSensorDefinition();
111 // Make sure the class matches the sensor type.
112 Def.Class = SensorType::StaticClass();
113 Definitions.Emplace(Def);
114 }
115
116 template <typename SensorType>
117 static typename std::enable_if<!is_spawnable<SensorType>::value, void>::type
118 AppendDefinitions(TArray<FActorDefinition> &) {}
119
120 template <size_t Index>
121 static void AppendDefinitions(TArray<FActorDefinition> &Definitions)
122 {
123 using SensorPtrType = typename Registry::get_by_index<Index>::key;
124 using SensorType = typename std::remove_pointer<SensorPtrType>::type;
126 }
127
128 template <size_t... Is>
129 static void AppendDefinitions(
130 TArray<FActorDefinition> &Definitions,
131 std::index_sequence<Is...>)
132 {
133 std::initializer_list<int> ({(AppendDefinitions<Is>(Definitions), 0)...});
134 }
135};
136
137// =============================================================================
138// -- ASensorFactory -----------------------------------------------------------
139// =============================================================================
140
145
147 const FTransform &Transform,
148 const FActorDescription &Description)
149{
150 auto *World = GetWorld();
151 if (World == nullptr)
152 {
153 UE_LOG(LogCarla, Error, TEXT("ASensorFactory: cannot spawn sensor into an empty world."));
154 return {};
155 }
156
158 if (GameInstance == nullptr)
159 {
160 UE_LOG(LogCarla, Error, TEXT("ASensorFactory: cannot spawn sensor, incompatible game instance."));
161 return {};
162 }
163
164 auto *Sensor = World->SpawnActorDeferred<ASensor>(
165 Description.Class,
166 Transform,
167 this,
168 nullptr,
169 ESpawnActorCollisionHandlingMethod::AlwaysSpawn);
170 if (Sensor == nullptr)
171 {
172 UE_LOG(LogCarla, Error, TEXT("ASensorFactory: spawn sensor failed."));
173 }
174 else
175 {
176 auto *Episode = GameInstance->GetCarlaEpisode();
177 check(Episode != nullptr);
178
179 Sensor->SetEpisode(*Episode);
180 Sensor->Set(Description);
181 Sensor->SetDataStream(GameInstance->GetServer().OpenStream());
182 // ASceneCaptureSensor * SceneCaptureSensor = Cast<ASceneCaptureSensor>(Sensor);
183 // if(SceneCaptureSensor)
184 // {
185 // SceneCaptureSensor->CameraGBuffers.SceneColor.SetDataStream(GameInstance->GetServer().OpenStream());
186 // SceneCaptureSensor->CameraGBuffers.SceneDepth.SetDataStream(GameInstance->GetServer().OpenStream());
187 // SceneCaptureSensor->CameraGBuffers.SceneStencil.SetDataStream(GameInstance->GetServer().OpenStream());
188 // SceneCaptureSensor->CameraGBuffers.GBufferA.SetDataStream(GameInstance->GetServer().OpenStream());
189 // SceneCaptureSensor->CameraGBuffers.GBufferB.SetDataStream(GameInstance->GetServer().OpenStream());
190 // SceneCaptureSensor->CameraGBuffers.GBufferC.SetDataStream(GameInstance->GetServer().OpenStream());
191 // SceneCaptureSensor->CameraGBuffers.GBufferD.SetDataStream(GameInstance->GetServer().OpenStream());
192 // SceneCaptureSensor->CameraGBuffers.GBufferE.SetDataStream(GameInstance->GetServer().OpenStream());
193 // SceneCaptureSensor->CameraGBuffers.GBufferF.SetDataStream(GameInstance->GetServer().OpenStream());
194 // SceneCaptureSensor->CameraGBuffers.Velocity.SetDataStream(GameInstance->GetServer().OpenStream());
195 // SceneCaptureSensor->CameraGBuffers.SSAO.SetDataStream(GameInstance->GetServer().OpenStream());
196 // SceneCaptureSensor->CameraGBuffers.CustomDepth.SetDataStream(GameInstance->GetServer().OpenStream());
197 // SceneCaptureSensor->CameraGBuffers.CustomStencil.SetDataStream(GameInstance->GetServer().OpenStream());
198 // }
199 }
200 UGameplayStatics::FinishSpawningActor(Sensor, Transform);
201 return FActorSpawnResult{Sensor};
202}
#define CARLA_CHECK_ROS2_SENSOR_TYPE(SensorT, Enum)
Autoware GNSS sensor ("sensor.other.autoware_gnss"): streams GeoLocation to the client like the regul...
A sensor to register collisions.
Sensor that produce Dynamic Vision Events.
Definition DVSCamera.h:47
Sensor that produces "depth" images.
Sensor that produces "depth" images.
Definition DepthCamera.h:16
Gnss sensor representation The actual position calculation is done one server side.
Definition GnssSensor.h:24
A fixed-sweep ray-cast Lidar approximating a Hybrid Solid-State LiDAR (e.g.
Definition HSSLidar.h:28
Sensor that produces "Instance segmentation" images.
Sensor that produces "Instance segmentation" images.
LaneInvasion sensor representation The actual position calculation is done one client side.
Sensor that produces "normals" images.
A sensor to register collisions.
Sensor that produces "optical flow" images.
A ray-cast based Radar sensor.
Definition Radar.h:21
A ray-cast based Lidar sensor.
A ray-cast based Lidar sensor.
RSS sensor representation The actual calculation is done one client side.
A path-traced distance sensor ("sensor.camera.rt_lens_distance"): the geometric twin of sensor....
A path-traced instance/semantic sensor ("sensor.camera.rt_lens_instance"): the labelling twin of sens...
A path-traced camera sensor ("sensor.camera.rt_lens") that renders through a configurable lens model ...
A sensor that captures images from the scene.
A sensor that captures images from the scene.
Sensor that produces "semantic segmentation" images.
Sensor that produces "semantic segmentation" images.
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.
Vehicle status sensor ("sensor.other.vehicle_status"): attached to a vehicle, streams a packed kinema...
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... >)
carla::sensor::SensorRegistry Registry
static std::enable_if< is_spawnable< SensorType >::value, void >::type AppendDefinitions(TArray< FActorDefinition > &Definitions)
Serializes and sends all the actors in the current UCarlaEpisode.
A rigid transform in CARLA's / Unreal's left-handed frame: x forward, y right, z up,...
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)
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