29#include "GenericPlatform/GenericPlatformFile.h"
30#include "Serialization/JsonSerializer.h"
31#include "Serialization/JsonWriter.h"
32#include "Misc/DefaultValueHelper.h"
33#include "HAL/PlatformFileManager.h"
34#include "Misc/FileHelper.h"
36#include "Engine/GameViewportClient.h"
37#include "Engine/Engine.h"
38#include "Engine/World.h"
54 VehicleName =
"Forwarder";
58 VehicleName =
"Harvester";
62 VehicleName =
"Drone";
71 if (VehicleName.IsEmpty())
78 FString ValidatedFileName = FPaths::MakeValidFileName(FileName, TEXT(
'_'));
81 TSharedPtr<FJsonObject> JsonObject = MakeShareable(
new FJsonObject);
82 TArray<TSharedPtr<FJsonValue>> ObjectsArray;
84 TSharedPtr<FJsonObject> VehicleObject = MakeShareable(
new FJsonObject);
85 VehicleObject->SetStringField(
"type",
"vehicle");
86 VehicleObject->SetStringField(
"model", VehicleName);
87 VehicleObject->SetBoolField(
"teleportSpectator",
false);
88 VehicleObject->SetBoolField(
"followObject",
false);
89 VehicleObject->SetBoolField(
"destroyOverlappingObjects",
false);
90 VehicleObject->SetStringField(
"id",
Vehicle->GetActorID_Implementation());
92 TSharedPtr<FJsonObject> VehicleSpawnPoint = MakeShareable(
new FJsonObject);
94 FVector SpawnLocation;
95 FRotator SpawnRotation;
97 if (OverrideTransform)
104 SpawnLocation =
Vehicle->GetActorLocation();
105 SpawnRotation =
Vehicle->GetActorRotation();
118 TArray<TSharedPtr<FJsonValue>> SensorsArray;
120 USensorsManagerComponent* SensorManager =
Vehicle->GetSensorsManager();
124 SensorsArray.Append(
ParseLidars(SensorManager->GetLidars()));
125 SensorsArray.Append(
ParseCameras(SensorManager->GetCameras()));
129 SensorsArray.Append(
ParseRadars(SensorManager->GetRadars()));
133 if (SensorsArray.Num() > 0)
135 VehicleObject->SetArrayField(
"sensors", SensorsArray);
138 ObjectsArray.Add(MakeShareable(
new FJsonValueObject(VehicleObject)));
140 JsonObject->SetArrayField(
"objects", ObjectsArray);
143 TSharedRef<TJsonWriter<TCHAR>> JsonWriter = TJsonWriterFactory<>::Create(&JsonString);
144 FJsonSerializer::Serialize(JsonObject.ToSharedRef(), JsonWriter);
147 bool Saved = FFileHelper::SaveStringToFile(JsonString, *FilePath);
154 FString Msg = FString::Printf(TEXT(
"Exported vehicle JSON file to: %s"), *FilePath);
164 FString ValidatedFileName = FPaths::MakeValidFileName(FileName, TEXT(
'_'));
169 TSharedPtr<FJsonObject> ParametersObject = MakeShareable(
new FJsonObject);
170 FJsonObjectConverter::UStructToJsonObject(FWeatherParameters::StaticStruct(), &WeatherParameters, ParametersObject.ToSharedRef());
172 TSharedPtr<FJsonObject> RootObject = MakeShareable(
new FJsonObject);
173 TArray<TSharedPtr<FJsonValue>> ObjectsArray;
174 TSharedPtr<FJsonObject> WeatherObject = MakeShareable(
new FJsonObject);
175 WeatherObject->SetStringField(
"type",
"weather");
177 WeatherObject->SetObjectField(
"parameters", ParametersObject);
179 ObjectsArray.Add(MakeShareable(
new FJsonValueObject(WeatherObject)));
181 RootObject->SetArrayField(
"objects", ObjectsArray);
184 TSharedRef<TJsonWriter<>> JsonWriter = TJsonWriterFactory<>::Create(&JsonString);
185 FJsonSerializer::Serialize(RootObject.ToSharedRef(), JsonWriter);
187 bool Saved = FFileHelper::SaveStringToFile(JsonString, *FilePath);
194 FString Msg = FString::Printf(TEXT(
"Exported Weather JSON file to: %s"), *FilePath);
204 FString ValidatedFileName = FPaths::MakeValidFileName(FileName, TEXT(
'_'));
208 if (Actors.IsEmpty())
211 UE_LOG(LogTemp, Warning, TEXT(
"ROSJsonExporter.cpp: Actors TArray is empty."));
218 TSharedPtr<FJsonObject> JsonObject = MakeShareable(
new FJsonObject);
220 TArray<TSharedPtr<FJsonValue>> ObjectsArray;
228 FString type = Actor->GetTypeIDString();
229 FString model = Actor->GetModelIDString();
232 if (type ==
"none" || model ==
"none")
237 TSharedPtr<FJsonObject> ObjectEntry = MakeShareable(
new FJsonObject);
238 ObjectEntry->SetStringField(
"type", type);
239 ObjectEntry->SetStringField(
"model", model);
242 TSharedPtr<FJsonObject> SpawnPoint = MakeShareable(
new FJsonObject);
243 FVector SpawnLocation = Actor->GetActorLocation();
244 FRotator SpawnRotation = Actor->GetActorRotation();
245 FVector Scale = Actor->GetActorScale();
246 SetSpawnPoint(ObjectEntry, SpawnLocation, SpawnRotation, Scale);
247 ObjectsArray.Add(MakeShareable(
new FJsonValueObject(ObjectEntry)));
250 JsonObject->SetArrayField(
"objects", ObjectsArray);
253 TSharedRef<TJsonWriter<TCHAR>> JsonWriter = TJsonWriterFactory<>::Create(&JsonString);
254 FJsonSerializer::Serialize(JsonObject.ToSharedRef(), JsonWriter);
256 bool Saved = FFileHelper::SaveStringToFile(JsonString, *FilePath);
263 FString Msg = FString::Printf(TEXT(
"Exported Actors JSON file to: %s"), *FilePath);
279 FString ValidatedFileName = FPaths::MakeValidFileName(FileName, TEXT(
'_'));
284 TSharedPtr<FJsonObject> RootJsonObject = MakeShareable(
new FJsonObject);
287 TArray<TSharedPtr<FJsonValue>> ObjectsArray;
288 TSharedPtr<FJsonObject> ObjectEntry = MakeShareable(
new FJsonObject);
294 ObjectEntry->SetStringField(
"type",
"Walker");
296 ObjectEntry->SetStringField(
"id",
Walker->GetActorID_Implementation());
299 TSharedPtr<FJsonObject> ParametersObject = MakeShareable(
new FJsonObject);
300 FJsonObjectConverter::UStructToJsonObject(FWalkerParameters::StaticStruct(), &WalkerParameters, ParametersObject.ToSharedRef());
303 ParametersObject->RemoveField(TEXT(
"points"));
305 if (WalkerParameters.
Points.Num() >= 2)
309 TSharedPtr<FJsonObject> PointsObject = MakeShareable(
new FJsonObject);
310 for (int32 i = 0; i < WalkerParameters.
Points.Num(); ++i)
312 FString PointName = FString::Printf(TEXT(
"point%d"), i);
315 FVector Location =
Transform.GetLocation();
319 TSharedPtr<FJsonObject> PointData = MakeShareable(
new FJsonObject);
320 PointData->SetNumberField(TEXT(
"x"), Location.X);
321 PointData->SetNumberField(TEXT(
"y"), Location.Y);
322 PointData->SetNumberField(TEXT(
"z"), Location.Z);
323 PointData->SetNumberField(TEXT(
"roll"), Rotation.Roll);
324 PointData->SetNumberField(TEXT(
"pitch"), Rotation.Pitch);
325 PointData->SetNumberField(TEXT(
"yaw"), Rotation.Yaw);
328 PointsObject->SetObjectField(PointName, PointData);
332 ParametersObject->SetObjectField(TEXT(
"points"), PointsObject);
337 TSharedPtr<FJsonObject> SpawnPoint = MakeShareable(
new FJsonObject);
339 FVector SpawnLocation;
340 FRotator SpawnRotation;
341 if (WalkerParameters.
Points.IsEmpty())
345 SpawnLocation =
Walker->GetActorLocation();
346 SpawnRotation =
Walker->GetActorRotation();
360 ObjectEntry->SetObjectField(
"parameters", ParametersObject);
363 ObjectsArray.Add(MakeShareable(
new FJsonValueObject(ObjectEntry)));
366 RootJsonObject->SetArrayField(
"objects", ObjectsArray);
370 TSharedRef<TJsonWriter<TCHAR>> JsonWriter = TJsonWriterFactory<>::Create(&JsonString);
371 FJsonSerializer::Serialize(RootJsonObject.ToSharedRef(), JsonWriter);
374 bool Saved = FFileHelper::SaveStringToFile(JsonString, *FilePath);
382 FString Msg = FString::Printf(TEXT(
"Exported Actors JSON file to: %s"), *FilePath);
398 FString ValidatedFileName = FPaths::MakeValidFileName(FileName, TEXT(
'_'));
405 TSharedPtr<FJsonObject> ParametersObject = MakeShareable(
new FJsonObject);
407 bool Success =
false;
413 ALidar* LidarPtr = Cast<ALidar>(Sensor);
417 FJsonObjectConverter::UStructToJsonObject(FLidarParameters::StaticStruct(), &LidarParameters, ParametersObject.ToSharedRef());
427 ACamera* CameraPtr = Cast<ACamera>(Sensor);
439 if (ThermalCameraPtr)
442 FJsonObjectConverter::UStructToJsonObject(FThermalCameraParameters::StaticStruct(), &ThermalCameraParameters, ParametersObject.ToSharedRef());
450 ACamera* CameraPtr = Cast<ACamera>(Sensor);
453 ADepthCamera* DepthCameraPtr = Cast<ADepthCamera>(Sensor);
457 FJsonObjectConverter::UStructToJsonObject(FDepthCameraParameters::StaticStruct(), &DepthCameraParameters, ParametersObject.ToSharedRef());
466 ADVSCamera* DVSPtr = Cast<ADVSCamera>(Sensor);
470 FJsonObjectConverter::UStructToJsonObject(FDVSCameraParameters::StaticStruct(), &CameraParameters, ParametersObject.ToSharedRef());
478 ARadar* RadaPtr = Cast<ARadar>(Sensor);
482 FJsonObjectConverter::UStructToJsonObject(FRadarParameters::StaticStruct(), &RadarParameters, ParametersObject.ToSharedRef());
493 UE_LOG(LogTemp, Warning, TEXT(
"ROSJsonExporter.cpp: Exporting Collision/Transform or Overlap sensor is not supported."));
507 TSharedPtr<FJsonObject> RootJsonObject = MakeShareable(
new FJsonObject);
510 TArray<TSharedPtr<FJsonValue>> ObjectsArray;
511 TSharedPtr<FJsonObject> ObjectEntry = MakeShareable(
new FJsonObject);
514 ObjectEntry->SetStringField(
"type",
"Sensor");
515 ObjectEntry->SetStringField(
"model", SensorTypeString);
518 FVector SpawnLocation = Sensor->GetActorLocation();
519 FRotator SpawnRotation = Sensor->GetActorRotation();
520 SetSpawnPoint(ObjectEntry, SpawnLocation, SpawnRotation, Sensor->GetActorScale());
523 ObjectEntry->SetObjectField(
"parameters", ParametersObject);
526 ObjectsArray.Add(MakeShareable(
new FJsonValueObject(ObjectEntry)));
529 RootJsonObject->SetArrayField(
"objects", ObjectsArray);
533 TSharedRef<TJsonWriter<TCHAR>> JsonWriter = TJsonWriterFactory<>::Create(&JsonString);
534 FJsonSerializer::Serialize(RootJsonObject.ToSharedRef(), JsonWriter);
537 bool Saved = FFileHelper::SaveStringToFile(JsonString, *FilePath);
544 FString Msg = FString::Printf(TEXT(
"Exported Actors JSON file to: %s"), *FilePath);
552 TArray<TSharedPtr<FJsonValue>> LidarArray;
561 FTransform
Transform =
Lidar->GetRootComponent()->GetRelativeTransform();
564 TSharedPtr<FJsonObject> ParametersObject = MakeShareable(
new FJsonObject);
567 FJsonObjectConverter::UStructToJsonObject(FLidarParameters::StaticStruct(), &Params, ParametersObject.ToSharedRef());
569 SensorObject->SetObjectField(
"parameters", ParametersObject);
571 LidarArray.Add(MakeShareable(
new FJsonValueObject(SensorObject)));
579 TArray<TSharedPtr<FJsonValue>> CameraArray;
581 for (
ACamera* Camera : Cameras)
588 FTransform
Transform = Camera->GetRootComponent()->GetRelativeTransform();
595 SensorObject->SetObjectField(
"parameters", ParametersObject);
597 CameraArray.Add(MakeShareable(
new FJsonValueObject(SensorObject)));
605 TArray<TSharedPtr<FJsonValue>> CameraArray;
614 FTransform
Transform = Camera->GetRootComponent()->GetRelativeTransform();
617 TSharedPtr<FJsonObject> DepthCameraParametersObject = MakeShareable(
new FJsonObject);
620 FJsonObjectConverter::UStructToJsonObject(FDepthCameraParameters::StaticStruct(), &Params, DepthCameraParametersObject.ToSharedRef());
622 SensorObject->SetObjectField(
"parameters", DepthCameraParametersObject);
624 CameraArray.Add(MakeShareable(
new FJsonValueObject(SensorObject)));
632 TArray<TSharedPtr<FJsonValue>> CameraArray;
641 FTransform
Transform = Camera->GetRootComponent()->GetRelativeTransform();
644 TSharedPtr<FJsonObject> ThermalCameraParametersObject = MakeShareable(
new FJsonObject);
647 FJsonObjectConverter::UStructToJsonObject(FThermalCameraParameters::StaticStruct(), &Params, ThermalCameraParametersObject.ToSharedRef());
649 SensorObject->SetObjectField(
"parameters", ThermalCameraParametersObject);
651 CameraArray.Add(MakeShareable(
new FJsonValueObject(SensorObject)));
659 TArray<TSharedPtr<FJsonValue>> CameraArray;
668 FTransform
Transform = Camera->GetRootComponent()->GetRelativeTransform();
671 TSharedPtr<FJsonObject> DVSCameraParametersObject = MakeShareable(
new FJsonObject);
674 FJsonObjectConverter::UStructToJsonObject(FDVSCameraParameters::StaticStruct(), &Params, DVSCameraParametersObject.ToSharedRef());
676 SensorObject->SetObjectField(
"parameters", DVSCameraParametersObject);
678 CameraArray.Add(MakeShareable(
new FJsonValueObject(SensorObject)));
686 TArray<TSharedPtr<FJsonValue>> RadarArray;
695 FTransform
Transform =
Radar->GetRootComponent()->GetRelativeTransform();
698 TSharedPtr<FJsonObject> RadarParametersObject = MakeShareable(
new FJsonObject);
701 FJsonObjectConverter::UStructToJsonObject(FRadarParameters::StaticStruct(), &Params, RadarParametersObject.ToSharedRef());
703 SensorObject->SetObjectField(
"parameters", RadarParametersObject);
705 RadarArray.Add(MakeShareable(
new FJsonValueObject(SensorObject)));
713 TSharedPtr<FJsonObject> SensorObject = MakeShareable(
new FJsonObject);
718 UE_LOG(LogTemp, Warning, TEXT(
"ROSJsonExporter.cpp: SensorPtr is nullptr!"));
723 FJsonObject* SensorJsonObject = SensorObject.Get();
724 SensorJsonObject->SetStringField(
"type",
"sensor");
725 SensorJsonObject->SetStringField(
"model", SensorName);
726 SensorJsonObject->SetStringField(
"name", SensorPtr->
GetSensorName());
729 SensorJsonObject->SetStringField(
"attachedToBone", SensorPtr->
AttachedToBone.ToString());
731 FTransform
Transform = SensorPtr->GetRootComponent()->GetRelativeTransform();
732 FVector SensorLocation =
Transform.GetLocation();
733 FRotator SensorRotation =
Transform.Rotator();
741 TSharedPtr<FJsonObject> ParametersObject = MakeShareable(
new FJsonObject);
742 FJsonObjectConverter::UStructToJsonObject(FCameraBaseParameters::StaticStruct(), &Parameters, ParametersObject.ToSharedRef());
744 return ParametersObject;
749 TSharedPtr<FJsonObject> SpawnPoint = MakeShareable(
new FJsonObject);
752 SpawnPoint->SetNumberField(
"x", Location.X);
753 SpawnPoint->SetNumberField(
"y", Location.Y);
754 SpawnPoint->SetNumberField(
"z", Location.Z);
757 SpawnPoint->SetNumberField(
"roll", Rotation.Roll);
758 SpawnPoint->SetNumberField(
"pitch", Rotation.Pitch);
759 SpawnPoint->SetNumberField(
"yaw", Rotation.Yaw);
762 SpawnPoint->SetNumberField(
"scaleX", Scale.X);
763 SpawnPoint->SetNumberField(
"scaleY", Scale.Y);
764 SpawnPoint->SetNumberField(
"scaleZ", Scale.Z);
766 ObjectEntry->SetObjectField(
"spawnPoint", SpawnPoint);
771 APIDDrone* DronePtr = Cast<APIDDrone>(VehiclePtr);
772 if (!ObjectEntry.IsValid() || !DronePtr)
777 TSharedPtr<FJsonObject> VehicleParams = MakeShareable(
new FJsonObject);
781 FString DroneActionString = UEnum::GetDisplayValueAsText(DroneParams.
DroneAction).ToString();
782 VehicleParams->SetStringField(
"droneAction", DroneActionString);
784 FString DroneEndActionString = UEnum::GetDisplayValueAsText(DroneParams.
DroneEndAction).ToString();
785 VehicleParams->SetStringField(
"DroneEndAction", DroneEndActionString);
787 if (!DroneParams.
Points.IsEmpty())
790 TArray<TSharedPtr<FJsonValue>> PointsArray;
791 PointsArray.Reserve(DroneParams.
Points.Num());
795 TSharedPtr<FJsonObject> PointObject = MakeShareable(
new FJsonObject);
798 FVector Translation =
Transform.GetLocation();
799 PointObject->SetNumberField(
"X", Translation.X);
800 PointObject->SetNumberField(
"Y", Translation.Y);
801 PointObject->SetNumberField(
"Z", Translation.Z);
804 FRotator Rotation =
Transform.GetRotation().Rotator();
805 PointObject->SetNumberField(
"Pitch", Rotation.Pitch);
806 PointObject->SetNumberField(
"Yaw", Rotation.Yaw);
807 PointObject->SetNumberField(
"Roll", Rotation.Roll);
810 PointsArray.Add(MakeShareable(
new FJsonValueObject(PointObject)));
814 VehicleParams->SetArrayField(
"points", PointsArray);
818 ObjectEntry->SetObjectField(
"parameters", VehicleParams);
823 if (FileName.IsEmpty())
826 FileName =
"ExportedJsonFile";
830 FileName = FileName.TrimStartAndEnd();
833 FString InvalidChars = TEXT(
"\\/:*?\"<>|");
834 for (TCHAR InvalidChar : InvalidChars)
836 FileName.ReplaceCharInline(InvalidChar, TEXT(
'_'), ESearchCase::CaseSensitive);
840 FString BaseFileName = FileName;
849 FString IndexString = (FileIndex > 0) ? FString::Printf(TEXT(
"_%d"), FileIndex) : FString();
850 FinalPath = DataPathFolder / FString::Printf(TEXT(
"ExportedJsonFiles/%s%s.json"), *BaseFileName, *IndexString);
852 }
while (FPlatformFileManager::Get().GetPlatformFile().FileExists(*FinalPath));
@ SemanticSegmentationCamera
FCameraBaseParameters GetCameraParameters()
FDVSCameraParameters GetDVSCameraParameters() const
FDepthCameraParameters GetDepthCameraParameters()
FLidarParameters GetLidarParameters() const
FDroneParameters GetDroneParameters()
FRadarParameters GetRadarParameters()
FString GetSensorName() const
virtual ESensorTypes GetSensorType() const
virtual FString GetActorID_Implementation() const override
FString AttachedToComponent
FThermalCameraParameters GetThermalCameraParameters() const
static void Log(const FString &Message, bool LogToTextFile=true, bool LogToROS=true)
static FString GetDataFolder()
static FString ConvertSensorTypeToString(ESensorTypes Sensortype)
static FString ConvertWalkerTypeToString(EWalkerType WalkerType)
static FString ExportWalkerToJSON(const FString &FileName, AWalker *Walker)
static FString ExportWeatherToJSON(const FString &FileName, const FWeatherParameters &WeatherParameters)
static FString CreateUniqueFilePath(FString FileName)
static FString ExportVehicleAndSensorsToJSON(FString FileName, AVehicle *Vehicle, bool OverrideTransform=false, const FTransform &Transform=FTransform())
static FString ExportSensorToJSON(const FString &FileName, ASensor *Sensor)
static TArray< TSharedPtr< FJsonValue > > ParseDepthCameras(const TArray< ADepthCamera * > &DepthCameras)
static TArray< TSharedPtr< FJsonValue > > ParseDVSCameras(const TArray< ADVSCamera * > &DVSCameras)
static TArray< TSharedPtr< FJsonValue > > ParseLidars(const TArray< ALidar * > &Lidars)
static TArray< TSharedPtr< FJsonValue > > ParseThermalCameras(const TArray< AThermalCamera * > &ThermalCameras)
static TSharedPtr< FJsonObject > CreateSensorJSONObject(const ASensor *SensorPtr, FString SensorName)
static FString ExportInstancedActorsToJSON(FString FileName, const TArray< AInstancedActor * > &Actors)
static void SetSpawnPoint(TSharedPtr< FJsonObject > &ObjectEntry, const FVector &Location, const FRotator &Rotation, const FVector &Scale)
static void SetDroneParameters(TSharedPtr< FJsonObject > &ObjectEntry, AVehicle *VehiclePtr)
static TSharedPtr< FJsonObject > CameraParametersToJSONObject(const FCameraBaseParameters &Parameters)
static TArray< TSharedPtr< FJsonValue > > ParseCameras(const TArray< ACamera * > &Cameras)
static TArray< TSharedPtr< FJsonValue > > ParseRadars(const TArray< ARadar * > &Radars)
EDroneEndAction DroneEndAction
TArray< FTransform > Points
TArray< FTransform > Points