CARLA
 
载入中...
搜索中...
未找到
SensorFactory.cpp
浏览该文件的文档.
1// Copyright (c) 2017 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.h"
9
12#include "Carla/Sensor/Sensor.h"
14
18
19#define LIBCARLA_SENSOR_REGISTRY_WITH_SENSOR_INCLUDES
21#undef LIBCARLA_SENSOR_REGISTRY_WITH_SENSOR_INCLUDES
22
23#include <type_traits>
24
25// =============================================================================
26// -- FSensorDefinitionGatherer ------------------------------------------------
27// =============================================================================
28
29/// Retrieve the definitions of all the sensors registered in the
30/// SensorRegistry by calling their static method
31/// SensorType::GetSensorDefinition().
32///
33/// @note To make this class ignore a given sensor, define a public member
34/// "not_spawnable" that defines a type. If so, the sensor won't be spawned by
35/// this factory.
37{
39
40public:
41
43 {
44 TArray<FActorDefinition> Definitions;
45 Definitions.Reserve(Registry::size());
46 AppendDefinitions(Definitions, std::make_index_sequence<Registry::size()>());
47 return Definitions;
48 }
49
50private:
51
52 // Type traits for detecting if a sensor is spawnable.
53
54 template<typename T>
55 struct void_type { typedef void type; };
56
57 template<typename T, typename = void>
58 struct is_spawnable : std::true_type {};
59
60 template<typename T>
61 struct is_spawnable<T, typename void_type<typename T::not_spawnable>::type > : std::false_type {};
62
63 // AppendDefinitions implementations.
64
65 template <typename SensorType>
66 static typename std::enable_if<is_spawnable<SensorType>::value, void>::type
67 AppendDefinitions(TArray<FActorDefinition> &Definitions)
68 {
69 auto Def = SensorType::GetSensorDefinition();
70 // Make sure the class matches the sensor type.
71 Def.Class = SensorType::StaticClass();
72 Definitions.Emplace(Def);
73 }
74
75 template <typename SensorType>
76 static typename std::enable_if<!is_spawnable<SensorType>::value, void>::type
77 AppendDefinitions(TArray<FActorDefinition> &) {}
78
79 template <size_t Index>
80 static void AppendDefinitions(TArray<FActorDefinition> &Definitions)
81 {
82 using SensorPtrType = typename Registry::get_by_index<Index>::key;
83 using SensorType = typename std::remove_pointer<SensorPtrType>::type;
84 AppendDefinitions<SensorType>(Definitions);
85 }
86
87 template <size_t... Is>
88 static void AppendDefinitions(
89 TArray<FActorDefinition> &Definitions,
90 std::index_sequence<Is...>)
91 {
92 std::initializer_list<int> ({(AppendDefinitions<Is>(Definitions), 0)...});
93 }
94};
95
96// =============================================================================
97// -- ASensorFactory -----------------------------------------------------------
98// =============================================================================
99
104
106 const FTransform &Transform,
107 const FActorDescription &Description)
108{
109 auto *World = GetWorld();
110 if (World == nullptr)
111 {
112 UE_LOG(LogCarla, Error, TEXT("ASensorFactory: cannot spawn sensor into an empty world."));
113 return {};
114 }
115
117 if (GameInstance == nullptr)
118 {
119 UE_LOG(LogCarla, Error, TEXT("ASensorFactory: cannot spawn sensor, incompatible game instance."));
120 return {};
121 }
122
123 auto *Sensor = World->SpawnActorDeferred<ASensor>(
124 Description.Class,
125 Transform,
126 this,
127 nullptr,
128 ESpawnActorCollisionHandlingMethod::AlwaysSpawn);
129 if (Sensor == nullptr)
130 {
131 UE_LOG(LogCarla, Error, TEXT("ASensorFactory: spawn sensor failed."));
132 }
133 else
134 {
135 auto *Episode = GameInstance->GetCarlaEpisode();
136 check(Episode != nullptr);
137
138 Sensor->SetEpisode(*Episode);
139 Sensor->Set(Description);
140 Sensor->SetDataStream(GameInstance->GetServer().OpenStream());
141 // ASceneCaptureSensor * SceneCaptureSensor = Cast<ASceneCaptureSensor>(Sensor);
142 // if(SceneCaptureSensor)
143 // {
144 // SceneCaptureSensor->CameraGBuffers.SceneColor.SetDataStream(GameInstance->GetServer().OpenStream());
145 // SceneCaptureSensor->CameraGBuffers.SceneDepth.SetDataStream(GameInstance->GetServer().OpenStream());
146 // SceneCaptureSensor->CameraGBuffers.SceneStencil.SetDataStream(GameInstance->GetServer().OpenStream());
147 // SceneCaptureSensor->CameraGBuffers.GBufferA.SetDataStream(GameInstance->GetServer().OpenStream());
148 // SceneCaptureSensor->CameraGBuffers.GBufferB.SetDataStream(GameInstance->GetServer().OpenStream());
149 // SceneCaptureSensor->CameraGBuffers.GBufferC.SetDataStream(GameInstance->GetServer().OpenStream());
150 // SceneCaptureSensor->CameraGBuffers.GBufferD.SetDataStream(GameInstance->GetServer().OpenStream());
151 // SceneCaptureSensor->CameraGBuffers.GBufferE.SetDataStream(GameInstance->GetServer().OpenStream());
152 // SceneCaptureSensor->CameraGBuffers.GBufferF.SetDataStream(GameInstance->GetServer().OpenStream());
153 // SceneCaptureSensor->CameraGBuffers.Velocity.SetDataStream(GameInstance->GetServer().OpenStream());
154 // SceneCaptureSensor->CameraGBuffers.SSAO.SetDataStream(GameInstance->GetServer().OpenStream());
155 // SceneCaptureSensor->CameraGBuffers.CustomDepth.SetDataStream(GameInstance->GetServer().OpenStream());
156 // SceneCaptureSensor->CameraGBuffers.CustomStencil.SetDataStream(GameInstance->GetServer().OpenStream());
157 // }
158 }
159 UGameplayStatics::FinishSpawningActor(Sensor, Transform);
160 return FActorSpawnResult{Sensor};
161}
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< ASemanticSegmentationCamera *, s11n::ImageSerializer >, std::pair< AInstanceSegmentationCamera *, s11n::ImageSerializer >, std::pair< FWorldObserver *, s11n::EpisodeStateSerializer >, std::pair< FCameraGBufferUint8 *, s11n::GBufferUint8Serializer >, std::pair< FCameraGBufferFloat *, s11n::GBufferFloatSerializer >, std::pair< AV2XSensor *, s11n::CAMDataSerializer >, std::pair< ACustomV2XSensor *, s11n::CustomV2XDataSerializer > > 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