Agrarsense
Static Public Member Functions | Static Private Member Functions | Static Private Attributes | List of all members
USimulatorJsonParser Class Reference

#include <SimulatorJsonParser.h>

Inheritance diagram for USimulatorJsonParser:
Inheritance graph
[legend]
Collaboration diagram for USimulatorJsonParser:
Collaboration graph
[legend]

Static Public Member Functions

static void ParseAndOperateJSONFile (const FString &Path)
 

Static Private Member Functions

static void ParseVehicle (const TSharedPtr< FJsonObject > &JsonObject)
 
static void ParsePropOrFoliage (const TSharedPtr< FJsonObject > &JsonObject, const FString &ObjectType)
 
static void SpawnSensorToVehicle (AVehicle *Vehicle, const TSharedPtr< FJsonObject > &SensorObject, bool HideVehicleMeshForCameras=false)
 
static void SpawnSensorWorld (const TSharedPtr< FJsonObject > &SensorObject)
 
static void SpawnWalker (const TSharedPtr< FJsonObject > &JsonObject)
 
static void ParseWalkerSpawnPoints (const TSharedPtr< FJsonObject > &ParametersObject, FWalkerParameters &WalkerParams)
 
static void ChangeWeather (const TSharedPtr< FJsonObject > &JsonObject)
 
static void ParseAndSetDroneParameters (APIDDrone *DronePtr, const TSharedPtr< FJsonObject > &JsonObject)
 
static void ParseAndSetWheeledVehicleParameters (AVehicle *VehiclePtr, const TSharedPtr< FJsonObject > &JsonObject)
 
static void HandleSpectatorMovement (const TSharedPtr< FJsonObject > &JsonObject, AActor *Actor, const FTransform &Transform)
 
static void AttachActorToSpectatorCamera (AActor *ActorPtr)
 
static void ParseAndSetSemanticColors (const TSharedPtr< FJsonObject > &JsonObject)
 
static void ParseCommands (const TSharedPtr< FJsonObject > &JsonObject)
 
static void ParseVolumeDeletion (const TSharedPtr< FJsonObject > &JsonObject)
 
static void ParseDataCapture (const TSharedPtr< FJsonObject > &JsonObject)
 
static FTransform ParseTransform (const FString &FieldName, const TSharedPtr< FJsonObject > &Object)
 
static FVector4 ParseVector4 (const FString &FieldName, const TSharedPtr< FJsonObject > &Object)
 
static FThermalCameraParameters ParseThermalCameraParameters (const TSharedPtr< FJsonObject > &ParametersObject)
 
static int32 GetIntValueOrDefault (const TSharedPtr< FJsonObject > &JsonObject, const FString &FieldName, int32 DefaultValue)
 
template<typename T >
static T ParseParameters (const TSharedPtr< FJsonObject > &ParametersObject)
 

Static Private Attributes

static bool IsDataCapture = false
 
static TArray< ASensor * > SpawnedSensorsForDataCapture
 

Detailed Description

Static UBlueprintFunctionLibrary that reads and parses JSON file content. It processes the parsed data and attempts to spawn objects as defined within the JSON file.

Definition at line 34 of file SimulatorJsonParser.h.

Member Function Documentation

◆ AttachActorToSpectatorCamera()

void USimulatorJsonParser::AttachActorToSpectatorCamera ( AActor *  ActorPtr)
staticprivate

Definition at line 867 of file SimulatorJsonParser.cpp.

868{
869 if (!ActorPtr || !GEngine || !GEngine->GameViewport)
870 {
871 return;
872 }
873
874 UWorld* World = GEngine->GameViewport->GetWorld();
875 if (!World)
876 {
877 return;
878 }
879
881 APlayerController* PlayerController = UGameplayStatics::GetPlayerController(World, 0);
882 if (PlayerController && Spectator)
883 {
884 APlayerCameraManager* CameraManager = PlayerController->PlayerCameraManager;
885 if (CameraManager)
886 {
887 USceneComponent* CameraRootComponent = CameraManager->GetRootComponent();
888 if (CameraRootComponent)
889 {
890 // Set initial location and rotation to Spectator location
891 ActorPtr->SetActorLocation(Spectator->GetActorLocation());
892 ActorPtr->SetActorRotation(Spectator->GetActorRotation());
893
894 // Now attach ActorPtr to PlayerCameraManager CameraRootComponent so it follows the camera movement
895 ActorPtr->AttachToComponent(CameraRootComponent, FAttachmentTransformRules::SnapToTargetNotIncludingScale);
896
897 // Delete sensor model from the sensor after attaching to Spectator.
898 ASensor* SensorPtr = Cast<ASensor>(ActorPtr);
899 if (SensorPtr)
900 {
901 auto* SensorModelPtr = SensorPtr->GetSensorModel();
902 if (SensorModelPtr)
903 {
904 SensorModelPtr->Destroy();
905 }
906 }
907 }
908 }
909 }
910}
Definition: Sensor.h:45
ASensorModel * GetSensorModel() const
Definition: Sensor.h:181
static ASpectator * GetSpectator(const UObject *WorldContextObject)

References ASensor::GetSensorModel(), and UAgrarsenseStatics::GetSpectator().

Referenced by SpawnSensorWorld().

◆ ChangeWeather()

void USimulatorJsonParser::ChangeWeather ( const TSharedPtr< FJsonObject > &  JsonObject)
staticprivate

Definition at line 667 of file SimulatorJsonParser.cpp.

668{
669 if (!JsonObject.IsValid())
670 {
671 return;
672 }
673
674 TSharedPtr<FJsonObject> Parameters = JsonObject->GetObjectField(TEXT("parameters"));
675 if (Parameters.IsValid() && GEngine && GEngine->GameViewport)
676 {
677 UWorld* World = GEngine->GameViewport->GetWorld();
678 AWeather* WeatherActor = UAgrarsenseStatics::GetWeatherActor(World);
679 if (WeatherActor)
680 {
681 FWeatherParameters Params = WeatherActor->GetCurrentWeather();
682 FJsonObjectConverter::JsonObjectToUStruct(Parameters.ToSharedRef(), FWeatherParameters::StaticStruct(), &Params);
683 WeatherActor->UpdateWeather(Params, true);
684 }
685 }
686}
void UpdateWeather(const FWeatherParameters &WeatherParameters, bool updateToROS)
Definition: Weather.cpp:103
const FWeatherParameters & GetCurrentWeather() const
Definition: Weather.h:76
static AWeather * GetWeatherActor(const UObject *WorldContextObject)

References AWeather::GetCurrentWeather(), UAgrarsenseStatics::GetWeatherActor(), and AWeather::UpdateWeather().

Referenced by ParseAndOperateJSONFile().

◆ GetIntValueOrDefault()

int32 USimulatorJsonParser::GetIntValueOrDefault ( const TSharedPtr< FJsonObject > &  JsonObject,
const FString &  FieldName,
int32  DefaultValue 
)
staticprivate

Definition at line 1163 of file SimulatorJsonParser.cpp.

1164{
1165 int32 Value = DefaultValue;
1166
1167 if (JsonObject.IsValid() && JsonObject->HasField(FieldName))
1168 {
1169 JsonObject->TryGetNumberField(FieldName, Value);
1170 }
1171
1172 return Value;
1173}

Referenced by SpawnWalker().

◆ HandleSpectatorMovement()

void USimulatorJsonParser::HandleSpectatorMovement ( const TSharedPtr< FJsonObject > &  JsonObject,
AActor *  Actor,
const FTransform &  Transform 
)
staticprivate

Definition at line 819 of file SimulatorJsonParser.cpp.

820{
821 if (!JsonObject.IsValid() || !Actor)
822 {
823 return;
824 }
825
826 bool follow = false;
827 bool teleport = false;
828 JsonObject->TryGetBoolField(TEXT("followObject"), follow);
829 JsonObject->TryGetBoolField(TEXT("teleportSpectator"), teleport);
830
831 if (!follow && !teleport)
832 {
833 // If neither is set, return
834 return;
835 }
836
837 if (GEngine && GEngine->GameViewport)
838 {
839 UWorld* World = GEngine->GameViewport->GetWorld();
841 if (Spectator)
842 {
843 if (follow)
844 {
845 AVehicle* VehiclePtr = Cast<AVehicle>(Actor);
846 if (VehiclePtr)
847 {
849 if (SimulationLevelManager)
850 {
851 SimulationLevelManager->TakeManualControlOfVehicle(VehiclePtr);
852 }
853 }
854 /*else
855 {
856 Spectator->FollowActor(Actor, false, 150.0f, 50.0f);
857 }*/
858 }
859 else if (teleport)
860 {
861 Spectator->TeleportSpectator(Transform);
862 }
863 }
864 }
865}
bool TakeManualControlOfVehicle(AVehicle *Vehicle)
void TeleportSpectator(const FTransform &Transform)
Definition: Spectator.cpp:68
static ASimulationLevelManager * GetSimulationLevelManager(const UObject *WorldContextObject)

References UAgrarsenseStatics::GetSimulationLevelManager(), UAgrarsenseStatics::GetSpectator(), ASimulationLevelManager::TakeManualControlOfVehicle(), ASpectator::TeleportSpectator(), and Transform.

Referenced by ParseVehicle(), SpawnSensorWorld(), and SpawnWalker().

◆ ParseAndOperateJSONFile()

void USimulatorJsonParser::ParseAndOperateJSONFile ( const FString &  Path)
static

Try to load json file from Path and operate it based on the JSON content.

Parameters
Pathfull path to the JSON file, ex. C:\Agrarsense\Examples\ExampleJsonFiles\lidar.json

Definition at line 64 of file SimulatorJsonParser.cpp.

65{
67 {
68 SimulatorLog::Log("SimulatorJsonParser: Spawning objects in main menu is disabled.");
69 return;
70 }
71
72 if (!FPaths::FileExists(Path))
73 {
74 SimulatorLog::Log(FString::Printf(TEXT("SimulatorJsonParser: Path %s doesn't exist."), *Path));
75 return;
76 }
77
78 if (!FPaths::GetExtension(Path).Equals(TEXT("json"), ESearchCase::IgnoreCase))
79 {
80 SimulatorLog::Log("SimulatorJsonParser: Invalid file extension. Only .json files are supported.");
81 return;
82 }
83
84 FString JsonString;
85 if (!FFileHelper::LoadFileToString(JsonString, *Path))
86 {
87 SimulatorLog::Log("SimulatorJsonParser: Failed to read the json file.");
88 return;
89 }
90
91 TSharedPtr<FJsonObject> RootJsonObject = MakeShareable(new FJsonObject);
92 TSharedRef<TJsonReader<TCHAR>> Reader = TJsonReaderFactory<TCHAR>::Create(JsonString);
93 if (!FJsonSerializer::Deserialize(Reader, RootJsonObject))
94 {
95 SimulatorLog::Log("SimulatorJsonParser: Failed to deserialize json file.");
96 return;
97 }
98
99 const TArray<TSharedPtr<FJsonValue>>* JsonObjects;
100 if (!RootJsonObject->TryGetArrayField(TEXT("objects"), JsonObjects))
101 {
102 SimulatorLog::Log("json content doesn't contain 'objects' field. Make sure your json structure is correct.");
103 return;
104 }
105
106 // The key in the JSON object that we are searching for
107 FString FieldTypeString = "type";
108
109 // Pre-process incoming json objects
110 bool JsonContainsSensorType = false;
111 bool JsonContainsDataCaptureType = false;
112 TArray<TSharedPtr<FJsonObject>> FilteredJsonObjects;
113
114 for (const TSharedPtr<FJsonValue>& JsonObjectValue : *JsonObjects)
115 {
116 if (JsonObjectValue->Type != EJson::Object)
117 {
118 continue;
119 }
120
121 TSharedPtr<FJsonObject> JsonObject = JsonObjectValue->AsObject();
122 FString ObjectType;
123 if (!JsonObject->TryGetStringField(FieldTypeString, ObjectType))
124 {
125 continue;
126 }
127
128 // Convert ObjectType to lowercase to avoid case-sensitive checks below.
129 ObjectType.ToLowerInline();
130
131 FilteredJsonObjects.Add(JsonObject);
132
133 if (ObjectType == "sensor")
134 {
135 JsonContainsSensorType = true;
136 }
137 else if (ObjectType == "datacapture")
138 {
139 JsonContainsDataCaptureType = true;
140 }
141 }
142
143 // Check if both "sensor" and "datacapture" types are present in the JSON.
144 // If so, set IsDataCapture to true, indicating that spawned sensors should be tracked
145 // and added to the ADataCapture actor.
146 IsDataCapture = JsonContainsSensorType && JsonContainsDataCaptureType;
147
148 // Process json objects
149 for (const TSharedPtr<FJsonObject>& JsonObject : FilteredJsonObjects)
150 {
151 FString ObjectType;
152 if (!JsonObject->TryGetStringField(FieldTypeString, ObjectType))
153 {
154 continue;
155 }
156
157 ObjectType.ToLowerInline();
158
159 if (ObjectType == "vehicle")
160 {
161 ParseVehicle(JsonObject);
162 }
163 else if (ObjectType == "weather")
164 {
165 ChangeWeather(JsonObject);
166 }
167 else if (ObjectType == "walker")
168 {
169 SpawnWalker(JsonObject);
170 }
171 else if (ObjectType == "sensor")
172 {
173 SpawnSensorWorld(JsonObject);
174 }
175 else if (ObjectType == "prop")
176 {
177 ParsePropOrFoliage(JsonObject, "prop");
178 }
179 else if (ObjectType == "foliage")
180 {
181 ParsePropOrFoliage(JsonObject, "foliage");
182 }
183 else if (ObjectType == "semanticcolors")
184 {
185 ParseAndSetSemanticColors(JsonObject);
186 }
187 else if (ObjectType == "commands")
188 {
189 ParseCommands(JsonObject);
190 }
191 else if (ObjectType == "volumedeletion")
192 {
193 ParseVolumeDeletion(JsonObject);
194 }
195 else if (ObjectType == "datacapture")
196 {
197 ParseDataCapture(JsonObject);
198 }
199 }
200
201 // If IsDataCapture was true, clear array.
202 if (IsDataCapture)
203 {
205 IsDataCapture = false;
206 }
207}
static void Log(const FString &Message, bool LogToTextFile=true, bool LogToROS=true)
static bool IsPlayingInMainMenu()
static TArray< ASensor * > SpawnedSensorsForDataCapture
static void ParsePropOrFoliage(const TSharedPtr< FJsonObject > &JsonObject, const FString &ObjectType)
static void ParseVehicle(const TSharedPtr< FJsonObject > &JsonObject)
static void ParseVolumeDeletion(const TSharedPtr< FJsonObject > &JsonObject)
static void ParseDataCapture(const TSharedPtr< FJsonObject > &JsonObject)
static void ParseCommands(const TSharedPtr< FJsonObject > &JsonObject)
static void ParseAndSetSemanticColors(const TSharedPtr< FJsonObject > &JsonObject)
static void ChangeWeather(const TSharedPtr< FJsonObject > &JsonObject)
static void SpawnWalker(const TSharedPtr< FJsonObject > &JsonObject)
static void SpawnSensorWorld(const TSharedPtr< FJsonObject > &SensorObject)

References ChangeWeather(), IsDataCapture, UAgrarsenseSettings::IsPlayingInMainMenu(), SimulatorLog::Log(), ParseAndSetSemanticColors(), ParseCommands(), ParseDataCapture(), ParsePropOrFoliage(), ParseVehicle(), ParseVolumeDeletion(), SpawnedSensorsForDataCapture, SpawnSensorWorld(), and SpawnWalker().

Referenced by UROSCommands::HandleSpawnObjects(), and SDragAndDropWidget::OnDrop().

◆ ParseAndSetDroneParameters()

void USimulatorJsonParser::ParseAndSetDroneParameters ( APIDDrone DronePtr,
const TSharedPtr< FJsonObject > &  JsonObject 
)
staticprivate

Definition at line 707 of file SimulatorJsonParser.cpp.

708{
709 if (!DronePtr || !JsonObject.IsValid())
710 {
711 return;
712 }
713
714 TSharedPtr<FJsonObject> ParametersObject = JsonObject->GetObjectField(TEXT("parameters"));
715
716 if (ParametersObject.IsValid())
717 {
718 // Should use Z value as height from the ground instead of given Z value
719 bool ZIsHeightAboveGround = false;
720 ParametersObject->TryGetBoolField(TEXT("zIsHeightAboveGround"), ZIsHeightAboveGround);
721
722 FDroneParameters DroneParameters;
723
724 // Manual parsing due unreal automatic struct parsing doesn't work for enums
725
726 // Attempt to parse FDroneParameters from the json
727 FString DroneActionObject = ParametersObject->GetStringField(TEXT("droneAction"));
728 if (!DroneActionObject.IsEmpty())
729 {
730 EDroneAction droneAction = GetDroneAction(DroneActionObject.ToLower());
731 DroneParameters.DroneAction = droneAction;
732 }
733
734 FString DroneEndActionObject = ParametersObject->GetStringField(TEXT("droneEndAction"));
735 if (!DroneEndActionObject.IsEmpty())
736 {
737 EDroneEndAction droneEndAction = GetDroneEndAction(DroneEndActionObject);
738 DroneParameters.DroneEndAction = droneEndAction;
739 }
740
741 TSharedPtr<FJsonObject> PointsObject = ParametersObject->GetObjectField(TEXT("points"));
742 if (PointsObject.IsValid())
743 {
744 int PointIndex = 0;
745 while (true)
746 {
747 FString PointName = FString::Printf(TEXT("point%d"), PointIndex);
748 if (!PointsObject->HasField(PointName))
749 {
750 break;
751 }
752
753 TSharedPtr<FJsonObject> PointObject = PointsObject->GetObjectField(PointName);
754 if (PointObject.IsValid())
755 {
756 FTransform PointTransform = ParseTransform(PointName, PointsObject);
757
758 if (ZIsHeightAboveGround)
759 {
761 }
762
763 DroneParameters.Points.Add(PointTransform);
764 }
765
766 PointIndex++;
767 }
768 }
769
770 bool ShowForwardArrow = false;
771 ParametersObject->TryGetBoolField(TEXT("showForwardArrow"), ShowForwardArrow);
772 DroneParameters.ShowForwardArrow = ShowForwardArrow;
773
774 bool CollisionsEnabled = false;
775 ParametersObject->TryGetBoolField(TEXT("collisionsEnabled"), CollisionsEnabled);
776 DroneParameters.CollisionsEnabled = CollisionsEnabled;
777
778 bool CreateInnerOverlapSensor = false;
779 ParametersObject->TryGetBoolField(TEXT("createInnerOverlapSensor"), CreateInnerOverlapSensor);
780 DroneParameters.CreateInnerOverlapSensor = CreateInnerOverlapSensor;
781
782 float OverlapRadiusMeters = 25.0f;
783 ParametersObject->TryGetNumberField(TEXT("overlapRadiusMeters"), OverlapRadiusMeters);
784 DroneParameters.OverlapRadiusMeters = OverlapRadiusMeters;
785
786 bool VisualizeOverlap = false;
787 ParametersObject->TryGetBoolField(TEXT("visualizeOverlap"), VisualizeOverlap);
788 DroneParameters.VisualizeOverlap = VisualizeOverlap;
789
790 float InnerOverlapRadiusMeters = 2.0f;
791 ParametersObject->TryGetNumberField(TEXT("innerOverlapRadiusMeters"), InnerOverlapRadiusMeters);
792 DroneParameters.InnerOverlapRadiusMeters = InnerOverlapRadiusMeters;
793
794 bool DebugPointTransforms = false;
795 ParametersObject->TryGetBoolField(TEXT("debugPointTransforms"), DebugPointTransforms);
796
797 DronePtr->ChangeDroneParameters(DroneParameters);
798 DronePtr->SetShowForwardArrow(DroneParameters.ShowForwardArrow);
799 DronePtr->SetDrawDebugPoints(DebugPointTransforms);
800 }
801}
EDroneAction
Definition: DroneAction.h:15
EDroneEndAction
static EDroneEndAction GetDroneEndAction(FString endActionstring)
static EDroneAction GetDroneAction(FString actionstring)
void SetDrawDebugPoints(bool show)
Definition: PIDDrone.h:174
void ChangeDroneParameters(const FDroneParameters &newParameters)
Definition: PIDDrone.cpp:34
void SetShowForwardArrow(bool Show)
Definition: PIDDrone.h:169
static bool AlignTransformFromGroundInMeters(AActor *Actor, FTransform &InTransform)
static FTransform ParseTransform(const FString &FieldName, const TSharedPtr< FJsonObject > &Object)
float InnerOverlapRadiusMeters
EDroneEndAction DroneEndAction
EDroneAction DroneAction
TArray< FTransform > Points

References UPhysicsUtilities::AlignTransformFromGroundInMeters(), APIDDrone::ChangeDroneParameters(), FDroneParameters::CollisionsEnabled, FDroneParameters::CreateInnerOverlapSensor, FDroneParameters::DroneAction, FDroneParameters::DroneEndAction, GetDroneAction(), GetDroneEndAction(), FDroneParameters::InnerOverlapRadiusMeters, FDroneParameters::OverlapRadiusMeters, ParseTransform(), FDroneParameters::Points, APIDDrone::SetDrawDebugPoints(), APIDDrone::SetShowForwardArrow(), FDroneParameters::ShowForwardArrow, and FDroneParameters::VisualizeOverlap.

Referenced by ParseVehicle().

◆ ParseAndSetSemanticColors()

void USimulatorJsonParser::ParseAndSetSemanticColors ( const TSharedPtr< FJsonObject > &  JsonObject)
staticprivate

Definition at line 912 of file SimulatorJsonParser.cpp.

913{
914 UWorld* World = nullptr;
915 if (GEngine && GEngine->GameViewport)
916 {
917 World = GEngine->GameViewport->GetWorld();
918 }
919
920 if (!World)
921 {
922 return;
923 }
924
925 UMaterialParameterCollectionInstance* CollectionInstance = UMaterialParameterCollectionUtilities::LoadMaterialParameterCollectionInstance(World, "/Game/Agrarsense/Data/Semantic/MPC_SemanticCameraColors");
926 if (!CollectionInstance)
927 {
928 SimulatorLog::Log("ROSJsonParser.cpp: Failed to load MaterialParameterCollectionInstance!");
929 return;
930 }
931
932 // Access the "colors" object
933 const TSharedPtr<FJsonObject>* ColorsObjectPtr = nullptr;
934 if (JsonObject->TryGetObjectField(TEXT("colors"), ColorsObjectPtr) && ColorsObjectPtr != nullptr)
935 {
936 const TSharedPtr<FJsonObject>& ColorsObject = *ColorsObjectPtr;
937 for (const auto& Elem : ColorsObject->Values)
938 {
939 FName Type = FName(*Elem.Key);
940 FString VectorString = Elem.Value->AsString();
941
942 // Parse the FVector4 value from the string
943 FVector4 ParsedVector;
944 TArray<FString> ParsedValues;
945 VectorString.ParseIntoArray(ParsedValues, TEXT(","), true);
946 if (ParsedValues.Num() == 4)
947 {
948 ParsedVector.X = FCString::Atof(*ParsedValues[0]);
949 ParsedVector.Y = FCString::Atof(*ParsedValues[1]);
950 ParsedVector.Z = FCString::Atof(*ParsedValues[2]);
951 ParsedVector.W = FCString::Atof(*ParsedValues[3]);
952
953 // try to update the material collection vector value
954 bool success = UMaterialParameterCollectionUtilities::UpdateMaterialCollectionVectorValue(CollectionInstance, Type, ParsedVector);
955 }
956 }
957 }
958}
static bool UpdateMaterialCollectionVectorValue(UMaterialParameterCollectionInstance *MPCInstance, const FName ParameterName, const FVector4 Value)
static UMaterialParameterCollectionInstance * LoadMaterialParameterCollectionInstance(const UObject *WorldContextObject, const FString &Path)

References UMaterialParameterCollectionUtilities::LoadMaterialParameterCollectionInstance(), SimulatorLog::Log(), and UMaterialParameterCollectionUtilities::UpdateMaterialCollectionVectorValue().

Referenced by ParseAndOperateJSONFile().

◆ ParseAndSetWheeledVehicleParameters()

void USimulatorJsonParser::ParseAndSetWheeledVehicleParameters ( AVehicle VehiclePtr,
const TSharedPtr< FJsonObject > &  JsonObject 
)
staticprivate

Definition at line 803 of file SimulatorJsonParser.cpp.

804{
805 if (!VehiclePtr || !JsonObject.IsValid() || !JsonObject->HasField(TEXT("parameters")))
806 {
807 return;
808 }
809
810 // Get current vehicle parameters
811 FWheeledVehicleParameters Parameters = VehiclePtr->GetVehicleParameters();
812
813 // parse and set new parameters from json
814 TSharedPtr<FJsonObject> ParametersObject = JsonObject->GetObjectField(TEXT("parameters"));
815 Parameters = ParseParameters<FWheeledVehicleParameters>(ParametersObject);
816 VehiclePtr->SetVehicleParameters(Parameters);
817}
FWheeledVehicleParameters GetVehicleParameters() const
Definition: Vehicle.h:247
void SetVehicleParameters(FWheeledVehicleParameters NewParameters)
Definition: Vehicle.cpp:195

References AVehicle::GetVehicleParameters(), and AVehicle::SetVehicleParameters().

Referenced by ParseVehicle().

◆ ParseCommands()

void USimulatorJsonParser::ParseCommands ( const TSharedPtr< FJsonObject > &  JsonObject)
staticprivate

Definition at line 960 of file SimulatorJsonParser.cpp.

961{
962 // Parse json that contains "commands" array of Unreal console commands to execute
963
964 if (!GEngine || !GEngine->GameViewport)
965 {
966 return;
967 }
968
969 UWorld* World = GEngine->GameViewport->GetWorld();
970 if (World)
971 {
972 const TArray<TSharedPtr<FJsonValue>>* CommandsArray;
973 if (JsonObject->TryGetArrayField(TEXT("commands"), CommandsArray))
974 {
975 for (const TSharedPtr<FJsonValue>& CommandValue : *CommandsArray)
976 {
977 FString Command;
978 if (CommandValue->TryGetString(Command))
979 {
980 GEngine->Exec(World, *Command);
981 }
982 }
983 }
984 }
985}

Referenced by ParseAndOperateJSONFile().

◆ ParseDataCapture()

void USimulatorJsonParser::ParseDataCapture ( const TSharedPtr< FJsonObject > &  JsonObject)
staticprivate

Definition at line 1017 of file SimulatorJsonParser.cpp.

1018{
1019 if (!JsonObject.IsValid() || !GEngine || !GEngine->GameViewport)
1020 {
1021 return;
1022 }
1023
1024 FCaptureData CaptureData;
1025 TSharedPtr<FJsonObject> ParametersObject = JsonObject->GetObjectField(TEXT("parameters"));
1026
1027 if (ParametersObject.IsValid())
1028 {
1029 // Should use Z value as height from the ground instead of given Z value
1030 bool ZIsHeightAboveGround = false;
1031 ParametersObject->TryGetBoolField(TEXT("zIsHeightAboveGround"), ZIsHeightAboveGround);
1032
1033 bool UseGPS = false;
1034 UseGPS = ParametersObject->GetBoolField(TEXT("useGPSLocation"));
1035 CaptureData.UseGPSLocation = UseGPS;
1036 CaptureData.UseHeightAboveGround = ZIsHeightAboveGround;
1037
1038 CaptureData.CaptureRotatedViews = ParametersObject->GetBoolField(TEXT("captureRotatedViews"));
1039
1040 TSharedPtr<FJsonObject> PointsObject = ParametersObject->GetObjectField(TEXT("points"));
1041 if (PointsObject.IsValid())
1042 {
1043 int PointIndex = 0;
1044 while (true)
1045 {
1046 FString PointName = FString::Printf(TEXT("point%d"), PointIndex);
1047 if (!PointsObject->HasField(PointName))
1048 {
1049 break;
1050 }
1051
1052 TSharedPtr<FJsonObject> PointObject = PointsObject->GetObjectField(PointName);
1053 if (PointObject.IsValid())
1054 {
1055 FTransform PointTransform = ParseTransform(PointName, PointsObject);
1056
1057 if (ZIsHeightAboveGround && !UseGPS)
1058 {
1060 }
1061
1062 CaptureData.CapturePositions.Add(PointTransform);
1063 }
1064
1065 PointIndex++;
1066 }
1067 }
1068 }
1069
1070 UWorld* World = GEngine->GameViewport->GetWorld();
1071 if (CaptureData.CapturePositions.Num() != 0 && SpawnedSensorsForDataCapture.Num() != 0 && World)
1072 {
1073
1074 CaptureData.Sensors.Append(SpawnedSensorsForDataCapture);
1075 ADataCapture* DataCaptureActor = World->SpawnActor<ADataCapture>(ADataCapture::StaticClass(), CaptureData.CapturePositions[0]);
1076 DataCaptureActor->SetupDataCapture(CaptureData);
1077 }
1078}
void SetupDataCapture(FCaptureData NewCaptureData)
Definition: DataCapture.cpp:27
bool UseGPSLocation
Definition: DataCapture.h:44
bool UseHeightAboveGround
Definition: DataCapture.h:57
TArray< FTransform > CapturePositions
Definition: DataCapture.h:32
bool CaptureRotatedViews
Definition: DataCapture.h:38
TArray< ASensor * > Sensors
Definition: DataCapture.h:23

References UPhysicsUtilities::AlignTransformFromGroundInMeters(), FCaptureData::CapturePositions, FCaptureData::CaptureRotatedViews, ParseTransform(), FCaptureData::Sensors, ADataCapture::SetupDataCapture(), SpawnedSensorsForDataCapture, FCaptureData::UseGPSLocation, and FCaptureData::UseHeightAboveGround.

Referenced by ParseAndOperateJSONFile().

◆ ParseParameters()

template<typename T >
static T USimulatorJsonParser::ParseParameters ( const TSharedPtr< FJsonObject > &  ParametersObject)
inlinestaticprivate

Templated function to parse parameters from JSON object into specific struct. Requires the struct type T to have a StaticStruct method compatible with FJsonObjectConverter::JsonObjectToUStruct.

Parameters
SensorObjectThe JSON object from which to parse the parameters.
Returns
An instance of type T populated with data from the JSON object.

Definition at line 95 of file SimulatorJsonParser.h.

96 {
97 T Params;
98
99 if (ParametersObject.IsValid())
100 {
101 FJsonObjectConverter::JsonObjectToUStruct(ParametersObject.ToSharedRef(), T::StaticStruct(), &Params);
102 }
103
104 return Params;
105 }

◆ ParsePropOrFoliage()

void USimulatorJsonParser::ParsePropOrFoliage ( const TSharedPtr< FJsonObject > &  JsonObject,
const FString &  ObjectType 
)
staticprivate

Definition at line 282 of file SimulatorJsonParser.cpp.

283{
284 FString Model;
285 if (!JsonObject->TryGetStringField(TEXT("model"), Model))
286 {
287 // If json doesn't specifiy model, return
288 return;
289 }
290
291 bool RandomZRotation = false;
292 bool SnapToGround = false;
293 JsonObject->TryGetBoolField(TEXT("snap_to_ground"), SnapToGround);
294 JsonObject->TryGetBoolField(TEXT("random_z_rotation"), RandomZRotation);
295
296 FString Name, ID;
297 JsonObject->TryGetStringField(TEXT("name"), Name);
298 JsonObject->TryGetStringField(TEXT("id"), ID);
299
300 FTransform SpawnTransform = ParseTransform("spawnPoint", JsonObject);
301
302 if (ObjectType.Equals("prop", ESearchCase::IgnoreCase))
303 {
305 UAssetLibrary::SpawnProp(Type, SpawnTransform, Name, ID, RandomZRotation, SnapToGround);
306 }
307 else if (ObjectType.Equals("foliage", ESearchCase::IgnoreCase))
308 {
310 UAssetLibrary::SpawnFoliage(Type, SpawnTransform, Name, ID, RandomZRotation, SnapToGround);
311 }
312}
EFoliageTypes
Definition: FoliageTypes.h:15
EPropTypes
Definition: PropTypes.h:15
static AActor * SpawnProp(EPropTypes PropType, FTransform Transform, FString ActorName="", FString ActorID="", bool RandomZRotation=false, bool SnapToGround=false)
static AActor * SpawnFoliage(EFoliageTypes FoliageType, FTransform Transform, FString ActorName="", FString ActorID="", bool RandomZRotation=true, bool SnapToGround=true)
static EPropTypes ConvertStringToPropType(const FString &String)
static EFoliageTypes ConvertStringToFoliageType(const FString &String)

References UEnumUtilities::ConvertStringToFoliageType(), UEnumUtilities::ConvertStringToPropType(), ParseTransform(), UAssetLibrary::SpawnFoliage(), and UAssetLibrary::SpawnProp().

Referenced by ParseAndOperateJSONFile().

◆ ParseThermalCameraParameters()

FThermalCameraParameters USimulatorJsonParser::ParseThermalCameraParameters ( const TSharedPtr< FJsonObject > &  ParametersObject)
staticprivate

Definition at line 1139 of file SimulatorJsonParser.cpp.

1140{
1141 FThermalCameraParameters ThermalCameraParams;
1142
1143 if (ParametersObject.IsValid())
1144 {
1145 // Parse fields manually because FJsonObjectConverter::JsonObjectToUStruct couldn't parse these properly.
1146 ThermalCameraParams.WarmColor = ParseVector4(TEXT("warmColor"), ParametersObject);
1147 ThermalCameraParams.ColdColor = ParseVector4(TEXT("coldColor"), ParametersObject);
1148 ParametersObject->TryGetBoolField(TEXT("allowCustomNoiseResolution"), ThermalCameraParams.AllowCustomNoiseResolution);
1149 ParametersObject->TryGetNumberField(TEXT("widthResolutionNoise"), ThermalCameraParams.WidthResolutionNoise);
1150 ParametersObject->TryGetNumberField(TEXT("heightResolutionNoise"), ThermalCameraParams.HeightResolutionNoise);
1151
1152 // Parse FCameraBaseParameters
1153 TSharedPtr<FJsonObject> CameraParametersObject = ParametersObject->GetObjectField(TEXT("cameraParameters"));
1154 if (CameraParametersObject.IsValid())
1155 {
1156 ThermalCameraParams.CameraParameters = ParseParameters<FCameraBaseParameters>(CameraParametersObject);
1157 }
1158 }
1159
1160 return ThermalCameraParams;
1161}
static FVector4 ParseVector4(const FString &FieldName, const TSharedPtr< FJsonObject > &Object)
FCameraBaseParameters CameraParameters

References FThermalCameraParameters::AllowCustomNoiseResolution, FThermalCameraParameters::CameraParameters, FThermalCameraParameters::ColdColor, FThermalCameraParameters::HeightResolutionNoise, ParseVector4(), FThermalCameraParameters::WarmColor, and FThermalCameraParameters::WidthResolutionNoise.

Referenced by SpawnSensorWorld().

◆ ParseTransform()

FTransform USimulatorJsonParser::ParseTransform ( const FString &  FieldName,
const TSharedPtr< FJsonObject > &  Object 
)
staticprivate

Definition at line 1080 of file SimulatorJsonParser.cpp.

1081{
1082 FTransform SpawnTransform;
1083
1084 const TSharedPtr<FJsonObject>* SpawnPointObject = nullptr;
1085 if (!Object->TryGetObjectField(FieldName, SpawnPointObject))
1086 {
1087 return SpawnTransform;
1088 }
1089
1090 const auto& SPO = *SpawnPointObject->Get();
1091
1092 double LocationX = 0.0, LocationY = 0.0, LocationZ = 0.0;
1093 if (!SPO.TryGetNumberField(TEXT("x"), LocationX) ||
1094 !SPO.TryGetNumberField(TEXT("y"), LocationY) ||
1095 !SPO.TryGetNumberField(TEXT("z"), LocationZ))
1096 {
1097 return SpawnTransform;
1098 }
1099
1100 double Yaw = 0.0, Pitch = 0.0, Roll = 0.0;
1101 SPO.TryGetNumberField(TEXT("yaw"), Yaw);
1102 SPO.TryGetNumberField(TEXT("pitch"), Pitch);
1103 SPO.TryGetNumberField(TEXT("roll"), Roll);
1104
1105 const FVector Location(LocationX, LocationY, LocationZ);
1106 const FRotator Rotation(Pitch, Yaw, Roll);
1107 SpawnTransform.SetLocation(Location);
1108 SpawnTransform.SetRotation(Rotation.Quaternion());
1109
1110 double ScaleX = 1.0, ScaleY = 1.0, ScaleZ = 1.0;
1111 if (SPO.TryGetNumberField(TEXT("scaleX"), ScaleX) &&
1112 SPO.TryGetNumberField(TEXT("scaleY"), ScaleY) &&
1113 SPO.TryGetNumberField(TEXT("scaleZ"), ScaleZ))
1114 {
1115 SpawnTransform.SetScale3D(FVector(ScaleX, ScaleY, ScaleZ));
1116 }
1117
1118 return SpawnTransform;
1119}

Referenced by ParseAndSetDroneParameters(), ParseDataCapture(), ParsePropOrFoliage(), ParseVehicle(), ParseVolumeDeletion(), ParseWalkerSpawnPoints(), SpawnSensorToVehicle(), and SpawnSensorWorld().

◆ ParseVector4()

FVector4 USimulatorJsonParser::ParseVector4 ( const FString &  FieldName,
const TSharedPtr< FJsonObject > &  Object 
)
staticprivate

Definition at line 1121 of file SimulatorJsonParser.cpp.

1122{
1123 FVector4 Vector;
1124 const TSharedPtr<FJsonObject>* VectorObject;
1125
1126 if (Object->TryGetObjectField(FieldName, VectorObject))
1127 {
1128 double X, Y, Z, W;
1129 (*VectorObject)->TryGetNumberField(TEXT("x"), X);
1130 (*VectorObject)->TryGetNumberField(TEXT("y"), Y);
1131 (*VectorObject)->TryGetNumberField(TEXT("z"), Z);
1132 (*VectorObject)->TryGetNumberField(TEXT("w"), W);
1133 Vector = FVector4(X, Y, Z, W);
1134 }
1135
1136 return Vector;
1137}

Referenced by ParseThermalCameraParameters().

◆ ParseVehicle()

void USimulatorJsonParser::ParseVehicle ( const TSharedPtr< FJsonObject > &  JsonObject)
staticprivate

Definition at line 209 of file SimulatorJsonParser.cpp.

210{
211 FString Model;
212 if (!JsonObject->TryGetStringField(TEXT("model"), Model))
213 {
214 // If json doesn't specifiy model, return
215 return;
216 }
217
218 FString Name, ID;
219 JsonObject->TryGetStringField(TEXT("name"), Name);
220 JsonObject->TryGetStringField(TEXT("id"), ID);
221 if (ID.IsEmpty())
222 {
223 ID = Model;
224 }
225
227 bool IsDrone = VehicleType == EVehicleTypes::Drone;
228
229 FTransform SpawnTransform = ParseTransform("spawnPoint", JsonObject);
230
231 // Should use Z value as height from the ground instead of given Z value
232 bool ZIsHeightAboveGround = false;
233 JsonObject->TryGetBoolField(TEXT("zIsHeightAboveGround"), ZIsHeightAboveGround);
234 if (ZIsHeightAboveGround)
235 {
237 }
238
239 bool DestroyCollidedObjectsOnSpawn = false;
240 JsonObject->TryGetBoolField(TEXT("destroyOverlappingObjects"), DestroyCollidedObjectsOnSpawn);
241
242 AVehicle* VehiclePtr = UAssetLibrary::SpawnVehicle(VehicleType, SpawnTransform, Name, ID, false, 150.0f, DestroyCollidedObjectsOnSpawn);
243
244 if (VehiclePtr)
245 {
246 if (IsDrone)
247 {
248 ParseAndSetDroneParameters(Cast<APIDDrone>(VehiclePtr), JsonObject);
249 }
250 else
251 {
252 ParseAndSetWheeledVehicleParameters(VehiclePtr, JsonObject);
253 }
254
255 HandleSpectatorMovement(JsonObject, Cast<AActor>(VehiclePtr), SpawnTransform);
256 }
257 else
258 {
259 // If Vehicle is nullptr, return
260 return;
261 }
262
263
264 bool HideVehicleMeshForCameras = false;
265 JsonObject->TryGetBoolField(TEXT("hideVehicleMeshForCameras"), HideVehicleMeshForCameras);
266
267 // Parse possible sensors which will be attached to spawned vehicle
268 const TArray<TSharedPtr<FJsonValue>>* SensorJsonObjects;
269 if (JsonObject->TryGetArrayField(TEXT("sensors"), SensorJsonObjects))
270 {
271 for (const TSharedPtr<FJsonValue>& SensorValue : *SensorJsonObjects)
272 {
273 if (SensorValue->Type == EJson::Object)
274 {
275 TSharedPtr<FJsonObject> SensorJsonObject = SensorValue->AsObject();
276 SpawnSensorToVehicle(VehiclePtr, SensorJsonObject, HideVehicleMeshForCameras);
277 }
278 }
279 }
280}
EVehicleTypes
Definition: VehicleTypes.h:15
static AVehicle * SpawnVehicle(EVehicleTypes VehicleType, FTransform SpawnTransform, const FString &ActorName, const FString &ActorID, bool SnapAboveGround=false, float AboveOffset=150.0f, bool DestroyOverlappingActors=false)
static EVehicleTypes ConvertStringToVehicleType(const FString &String)
static void ParseAndSetDroneParameters(APIDDrone *DronePtr, const TSharedPtr< FJsonObject > &JsonObject)
static void ParseAndSetWheeledVehicleParameters(AVehicle *VehiclePtr, const TSharedPtr< FJsonObject > &JsonObject)
static void SpawnSensorToVehicle(AVehicle *Vehicle, const TSharedPtr< FJsonObject > &SensorObject, bool HideVehicleMeshForCameras=false)
static void HandleSpectatorMovement(const TSharedPtr< FJsonObject > &JsonObject, AActor *Actor, const FTransform &Transform)

References UPhysicsUtilities::AlignTransformFromGroundInMeters(), UEnumUtilities::ConvertStringToVehicleType(), Drone, HandleSpectatorMovement(), ParseAndSetDroneParameters(), ParseAndSetWheeledVehicleParameters(), ParseTransform(), SpawnSensorToVehicle(), and UAssetLibrary::SpawnVehicle().

Referenced by ParseAndOperateJSONFile().

◆ ParseVolumeDeletion()

void USimulatorJsonParser::ParseVolumeDeletion ( const TSharedPtr< FJsonObject > &  JsonObject)
staticprivate

Definition at line 987 of file SimulatorJsonParser.cpp.

988{
989 if (!GEngine || !GEngine->GameViewport || !JsonObject.IsValid())
990 {
991 return;
992 }
993
994 // Parse spawn location for this volume
995 FTransform SpawnTransform = ParseTransform("spawnPoint", JsonObject);
996
997 // We can use ParseTransform function to parse bounds as the json structure is similar.
998 FTransform TransformBounds = ParseTransform("bounds", JsonObject);
999 FVector Bounds = TransformBounds.GetLocation();
1000
1001 bool OnlyDestroyTrees = true; // default to only destroy trees
1002 JsonObject->TryGetBoolField(TEXT("onlyDestroyTrees"), OnlyDestroyTrees);
1003
1004 UWorld* World = GEngine->GameViewport->GetWorld();
1005
1006 if (World)
1007 {
1008 ADeletionVolume* Volume = World->SpawnActor<ADeletionVolume>(ADeletionVolume::StaticClass(), SpawnTransform);
1009 if (Volume)
1010 {
1011 Volume->ChangeOverlapBounds(Bounds);
1012 Volume->DestroyOverlappingActors(OnlyDestroyTrees);
1013 }
1014 }
1015}
void DestroyOverlappingActors(bool OnlyTrees)
void ChangeOverlapBounds(FVector Bounds)

References ADeletionVolume::ChangeOverlapBounds(), ADeletionVolume::DestroyOverlappingActors(), and ParseTransform().

Referenced by ParseAndOperateJSONFile().

◆ ParseWalkerSpawnPoints()

void USimulatorJsonParser::ParseWalkerSpawnPoints ( const TSharedPtr< FJsonObject > &  ParametersObject,
FWalkerParameters WalkerParams 
)
staticprivate

Definition at line 631 of file SimulatorJsonParser.cpp.

632{
633 if (ParametersObject->HasField(TEXT("points")))
634 {
635 // If the JSON has "points" field, parse those points into WalkerParams.Points
636 TSharedPtr<FJsonObject> PointsObject = ParametersObject->GetObjectField(TEXT("points"));
637 if (PointsObject.IsValid())
638 {
639 int PointIndex = 0;
640 while (true)
641 {
642 FString PointName = FString::Printf(TEXT("point%d"), PointIndex);
643 if (!PointsObject->HasField(PointName))
644 {
645 break;
646 }
647
648 TSharedPtr<FJsonObject> PointObject = PointsObject->GetObjectField(PointName);
649 if (PointObject.IsValid())
650 {
651 FTransform PointTransform = ParseTransform(PointName, PointsObject);
652 WalkerParams.Points.Add(PointTransform);
653 }
654
655 PointIndex++;
656 }
657 }
658 }
659 else if (ParametersObject->HasField(TEXT("spawnPoint")))
660 {
661 // If the JSON doesn't have "points" field but has "spawnPoint", parse that into WalkerParams.Points
662 FTransform PointTransform = ParseTransform("spawnPoint", ParametersObject);
663 WalkerParams.Points.Add(PointTransform);
664 }
665}
TArray< FTransform > Points

References ParseTransform(), and FWalkerParameters::Points.

Referenced by SpawnWalker().

◆ SpawnSensorToVehicle()

void USimulatorJsonParser::SpawnSensorToVehicle ( AVehicle Vehicle,
const TSharedPtr< FJsonObject > &  SensorObject,
bool  HideVehicleMeshForCameras = false 
)
staticprivate

Definition at line 314 of file SimulatorJsonParser.cpp.

315{
316 if (!Vehicle || !SensorObject.IsValid())
317 {
318 return;
319 }
320
321 USensorsManagerComponent* VehicleSensorManager = Vehicle->GetSensorsManager();
322 if (!VehicleSensorManager)
323 {
324 return;
325 }
326
327 TSharedPtr<FJsonObject> ParametersObject = SensorObject->GetObjectField(TEXT("parameters"));
328 if (!ParametersObject.IsValid())
329 {
330 return;
331 }
332
333 // Sensor spawn point (relative)
334 FTransform RelativeTransform = ParseTransform("spawnPoint", SensorObject);
335
336 // Parse ESensorTypes from string
337 FString SensorTypeString;
338 SensorObject->TryGetStringField(TEXT("model"), SensorTypeString);
339 ESensorTypes SensorType = UEnumUtilities::ConvertStringToSensorType(SensorTypeString);
340
341 // It's fine if these are empty,
342 // USensorsManagerComponent will just generate random name and identifier
343 FString SensorIdentifier, Name;
344 SensorObject->TryGetStringField(TEXT("name"), Name);
345 SensorObject->TryGetStringField(TEXT("id"), SensorIdentifier);
346
347 FString AttachedToComponent;
348 SensorObject->TryGetStringField(TEXT("attachedToComponent"), AttachedToComponent);
349
350 FString AttachedToBoneString;
351 FName AttachedToBone;
352 if (SensorObject->TryGetStringField(TEXT("attachedToBone"), AttachedToBoneString))
353 {
354 AttachedToBone = FName(*AttachedToBoneString);
355 }
356
357 ASensorModel* TempActor = nullptr;
358 ASensor* SensorPtr = nullptr;
359 USceneComponent* AttachToComponent = VehicleSensorManager->GetComponentByHierarchyName(AttachedToComponent);
360
361 // Here sensors are spawned through VehicleSensorManager Actor so it can keep track of the spawned sensors.
362 switch (SensorType)
363 {
365 SensorPtr = Cast<ASensor>(VehicleSensorManager->SpawnLidar(RelativeTransform, true, SensorIdentifier, Name, ParseParameters<FLidarParameters>(ParametersObject), true, TempActor, AttachToComponent, AttachedToBone));
366 break;
367
369 SensorPtr = Cast<ASensor>(VehicleSensorManager->SpawnCamera(RelativeTransform, true, SensorIdentifier, Name, ParseParameters<FCameraBaseParameters>(ParametersObject), true, TempActor, AttachToComponent, AttachedToBone));
370 break;
371
373 SensorPtr = Cast<ASensor>(VehicleSensorManager->SpawnThermalCamera(RelativeTransform, true, SensorIdentifier, Name, ParseParameters<FThermalCameraParameters>(ParametersObject), true, TempActor, AttachToComponent, AttachedToBone));
374 break;
375
377 SensorPtr = Cast<ASensor>(VehicleSensorManager->SpawnDepthCamera(RelativeTransform, true, SensorIdentifier, Name, ParseParameters<FDepthCameraParameters>(ParametersObject), true, TempActor, AttachToComponent, AttachedToBone));
378 break;
379
381 SensorPtr = Cast<ASensor>(VehicleSensorManager->SpawnRadar(RelativeTransform, true, SensorIdentifier, Name, ParseParameters<FRadarParameters>(ParametersObject), true, TempActor, AttachToComponent, AttachedToBone));
382 break;
383
385 SensorPtr = Cast<ASensor>(VehicleSensorManager->SpawnSegmentationCamera(RelativeTransform, true, SensorIdentifier, Name, ParseParameters<FCameraBaseParameters>(ParametersObject), true, TempActor, AttachToComponent, AttachedToBone));
386 break;
387
389 SensorPtr = Cast<ASensor>(VehicleSensorManager->SpawnInstanceSegmentationCamera(RelativeTransform, true, SensorIdentifier, Name, ParseParameters<FCameraBaseParameters>(ParametersObject), true, TempActor, AttachToComponent, AttachedToBone));
390 break;
391
393 SensorPtr = Cast<ASensor>(VehicleSensorManager->SpawnDVSCamera(RelativeTransform, true, SensorIdentifier, Name, ParseParameters<FDVSCameraParameters>(ParametersObject), true, TempActor, AttachToComponent, AttachedToBone));
394 break;
395
396 default:
397 break;
398 }
399
400 if (HideVehicleMeshForCameras)
401 {
402 if (ACamera* CameraPtr = Cast<ACamera>(SensorPtr))
403 {
404 CameraPtr->AddHiddenActor(Vehicle);
405 }
406 }
407
408 // If the vehicle we are attaching is drone,
409 // and the spawned sensor is camera
410 // check if user has given "useGimbal" parameter.
411 if(APIDDrone* DronePtr = Cast<APIDDrone>(Vehicle))
412 {
413 if (ACamera* CameraPtr = Cast<ACamera>(SensorPtr))
414 {
415 bool UseGimbal = false;
416 SensorObject->TryGetBoolField(TEXT("useGimbal"), UseGimbal);
417 CameraPtr->SetUseGimbal(UseGimbal);
418 }
419 }
420}
ESensorTypes
Definition: SensorTypes.h:15
@ SemanticSegmentationCamera
@ InstanceSegmentationCamera
Definition: Camera.h:53
static ESensorTypes ConvertStringToSensorType(const FString &String)

References UEnumUtilities::ConvertStringToSensorType(), DepthCamera, DVSCamera, InstanceSegmentationCamera, Lidar, ParseTransform(), Radar, RGBCamera, SemanticSegmentationCamera, ThermalCamera, and Vehicle.

Referenced by ParseVehicle().

◆ SpawnSensorWorld()

void USimulatorJsonParser::SpawnSensorWorld ( const TSharedPtr< FJsonObject > &  SensorObject)
staticprivate

Definition at line 422 of file SimulatorJsonParser.cpp.

423{
424 UWorld* World = nullptr;
425 if (GEngine && GEngine->GameViewport)
426 {
427 World = GEngine->GameViewport->GetWorld();
428 }
429
430 if (!World
431 || !SensorObject.IsValid()
432 || !SensorObject->HasField(TEXT("parameters"))
433 || !SensorObject->HasField(TEXT("model")))
434 {
435 return;
436 }
437
438 TSharedPtr<FJsonObject> ParametersObject = SensorObject->GetObjectField(TEXT("parameters"));
439 if (!ParametersObject.IsValid())
440 {
441 return;
442 }
443
444 FString SensorTypeString, SensorName, SensorIdentifier;
445 SensorObject->TryGetStringField(TEXT("model"), SensorTypeString);
446 SensorObject->TryGetStringField(TEXT("name"), SensorName);
447 SensorObject->TryGetStringField(TEXT("id"), SensorIdentifier);
448
449 FTransform SpawnTransform = ParseTransform("spawnPoint", SensorObject);
450
451 // Should use Z value as height from the ground instead of given Z value
452 bool ZIsHeightAboveGround = false;
453 SensorObject->TryGetBoolField(TEXT("zIsHeightAboveGround"), ZIsHeightAboveGround);
454 if (ZIsHeightAboveGround)
455 {
457 }
458
459 AActor* ActorPtr = nullptr;
460 ESensorTypes SensorType = UEnumUtilities::ConvertStringToSensorType(SensorTypeString);
461
463 if (!SimulationLevelManager)
464 {
465 SimulatorLog::Log("Failed to spawn Sensor. Reason: Coulnd't to find SimulationLevelManager Actor!");
466 return;
467 }
468
469 USensorsManagerComponent* WorldSensorManager = SimulationLevelManager->GetSensorsManager();
470 if (!WorldSensorManager)
471 {
472 SimulatorLog::Log("Failed to spawn Sensor. Reason: Coulnd't to find USensorsManagerComponent Component!");
473 return;
474 }
475
476 ASensorModel* TempActor = nullptr;
477
478 // Here sensors are spawned through WorldSensorManager Actor so it can keep track of the spawned sensors.
479 switch (SensorType)
480 {
482 ActorPtr = Cast<AActor>(WorldSensorManager->SpawnLidar(SpawnTransform, false, SensorIdentifier, SensorName, ParseParameters<FLidarParameters>(ParametersObject), true, TempActor, nullptr, NAME_None));
483 break;
484
486 ActorPtr = Cast<AActor>(WorldSensorManager->SpawnRadar(SpawnTransform, false, SensorIdentifier, SensorName, ParseParameters<FRadarParameters>(ParametersObject), true, TempActor, nullptr, NAME_None));
487 break;
488
490 ActorPtr = Cast<AActor>(WorldSensorManager->SpawnCamera(SpawnTransform, false, SensorIdentifier, SensorName, ParseParameters<FCameraBaseParameters>(ParametersObject), true, TempActor, nullptr, NAME_None));
491 break;
492
494 ActorPtr = Cast<AActor>(WorldSensorManager->SpawnThermalCamera(SpawnTransform, false, SensorIdentifier, SensorName, ParseThermalCameraParameters(ParametersObject), true, TempActor, nullptr, NAME_None));
495 break;
496
498 ActorPtr = Cast<AActor>(WorldSensorManager->SpawnDepthCamera(SpawnTransform, false, SensorIdentifier, SensorName, ParseParameters<FDepthCameraParameters>(ParametersObject), true, TempActor, nullptr, NAME_None));
499 break;
500
502 ActorPtr = Cast<AActor>(WorldSensorManager->SpawnSegmentationCamera(SpawnTransform, false, SensorIdentifier, SensorName, ParseParameters<FCameraBaseParameters>(ParametersObject), true, TempActor, nullptr, NAME_None));
503 break;
504
506 ActorPtr = Cast<AActor>(WorldSensorManager->SpawnInstanceSegmentationCamera(SpawnTransform, false, SensorIdentifier, SensorName, ParseParameters<FCameraBaseParameters>(ParametersObject), true, TempActor, nullptr, NAME_None));
507 break;
508
510 ActorPtr = Cast<AActor>(WorldSensorManager->SpawnDVSCamera(SpawnTransform, false, SensorIdentifier, SensorName, ParseParameters<FDVSCameraParameters>(ParametersObject), true, TempActor, nullptr, NAME_None));
511 break;
512
513 default:
514 break;
515 }
516
517 if (IsDataCapture)
518 {
519 ACamera* CameraPtr = Cast<ACamera>(ActorPtr);
520 if (CameraPtr)
521 {
522 SpawnedSensorsForDataCapture.Add(Cast<ASensor>(ActorPtr));
523 }
524
525 ALidar* LidarPtr = Cast<ALidar>(ActorPtr);
526 if (LidarPtr)
527 {
528 SpawnedSensorsForDataCapture.Add(Cast<ASensor>(ActorPtr));
529 }
530 }
531
532 bool AttachToSpectator = false;
533 SensorObject->TryGetBoolField(TEXT("attachToSpectator"), AttachToSpectator);
534
535 if (AttachToSpectator)
536 {
538 }
539 else
540 {
541 HandleSpectatorMovement(SensorObject, ActorPtr, SpawnTransform);
542 }
543}
Definition: Lidar.h:35
USensorsManagerComponent * GetSensorsManager() const
static FThermalCameraParameters ParseThermalCameraParameters(const TSharedPtr< FJsonObject > &ParametersObject)
static void AttachActorToSpectatorCamera(AActor *ActorPtr)

References UPhysicsUtilities::AlignTransformFromGroundInMeters(), AttachActorToSpectatorCamera(), UEnumUtilities::ConvertStringToSensorType(), DepthCamera, DVSCamera, ASimulationLevelManager::GetSensorsManager(), UAgrarsenseStatics::GetSimulationLevelManager(), HandleSpectatorMovement(), InstanceSegmentationCamera, IsDataCapture, Lidar, SimulatorLog::Log(), ParseThermalCameraParameters(), ParseTransform(), Radar, RGBCamera, SemanticSegmentationCamera, SpawnedSensorsForDataCapture, and ThermalCamera.

Referenced by ParseAndOperateJSONFile().

◆ SpawnWalker()

void USimulatorJsonParser::SpawnWalker ( const TSharedPtr< FJsonObject > &  JsonObject)
staticprivate

Definition at line 545 of file SimulatorJsonParser.cpp.

546{
547 if (!JsonObject.IsValid())
548 {
549 SimulatorLog::Log("SimulatorJsonParser failed to parse Walker json: Invalid JsonObject");
550 return;
551 }
552
553 FString WalkerTypeString;
554 if (!JsonObject->TryGetStringField(TEXT("model"), WalkerTypeString))
555 {
556 SimulatorLog::Log("SimulatorJsonParser failed to parse Walker json: Model field is missing");
557 return;
558 }
559
560 EWalkerType WalkerType = UEnumUtilities::ConvertStringToWalkerType(WalkerTypeString);
561 if (WalkerType == EWalkerType::NONE)
562 {
563 SimulatorLog::Log("SimulatorJsonParser failed to parse Walker json: Walker is None");
564 return;
565 }
566
567 TSharedPtr<FJsonObject> ParametersObject = JsonObject->GetObjectField(TEXT("parameters"));
568 if (!ParametersObject.IsValid())
569 {
570 SimulatorLog::Log("SimulatorJsonParser failed to parse Walker json: ParametersObject is invalid or missing");
571 return;
572 }
573
574 FString WalkerActionString;
575 if (!ParametersObject->TryGetStringField(TEXT("walkerAction"), WalkerActionString))
576 {
577 SimulatorLog::Log("SimulatorJsonParser failed to parse Walker json: walkerAction field is missing");
578 return;
579 }
580
581 EWalkerAction WalkerAction = UEnumUtilities::ConvertStringToWalkerActionType(WalkerActionString);
582 if (WalkerAction == EWalkerAction::Roaming && !ParametersObject->HasField(TEXT("spawnPoint")))
583 {
584 SimulatorLog::Log("SimulatorJsonParser failed to parse Walker json: Roaming Walker requires spawnPoint");
585 return;
586 }
587
588 // Retrieve the WalkerEndAction
589 FString WalkerEndActionString;
590 ParametersObject->TryGetStringField(TEXT("walkerEndAction"), WalkerEndActionString);
591 EWalkerEndAction WalkerEndAction = UEnumUtilities::ConvertStringToWalkerEndActionType(WalkerEndActionString);
592
593 // Create and populate WalkerParameters
594 FWalkerParameters WalkerParams;
595 WalkerParams.WalkerType = WalkerType;
596 WalkerParams.WalkerAction = WalkerAction;
597 WalkerParams.WalkerEndAction = WalkerEndAction;
598 WalkerParams.WalkerSpeed = GetIntValueOrDefault(ParametersObject, "walkerSpeed", 50);
599 WalkerParams.CreateTransformTopic = true;
600
601 bool CreateTransformTopic = false;
602 if (ParametersObject->TryGetBoolField(TEXT("createTransformTopic"), CreateTransformTopic))
603 {
604 UE_LOG(LogTemp, Warning, TEXT("The boolean value is %s"), (CreateTransformTopic ? TEXT("true") : TEXT("false")));
605 WalkerParams.CreateTransformTopic = CreateTransformTopic;
606 }
607
608 // Parse points or spawnPoint into WalkerParams.Points
609 ParseWalkerSpawnPoints(ParametersObject, WalkerParams);
610
611 // Retrieve optional name and id fields
612 FString Name, ID;
613 ParametersObject->TryGetStringField(TEXT("name"), Name);
614 ParametersObject->TryGetStringField(TEXT("id"), ID);
615
616 // Retrieve the startAutomatically field
617 bool StartAutomatically = true;
618 if (ParametersObject->HasField(TEXT("startAutomatically")))
619 {
620 ParametersObject->TryGetBoolField(TEXT("startAutomatically"), StartAutomatically);
621 }
622
623 // Finally spawn the Walker
624 AWalker* Walker = UAssetLibrary::SpawnWalker(WalkerParams, Name, ID, StartAutomatically);
625 if (Walker)
626 {
627 HandleSpectatorMovement(JsonObject, Cast<AActor>(Walker), WalkerParams.Points[0]);
628 }
629}
EWalkerAction
Definition: WalkerAction.h:15
EWalkerEndAction
EWalkerType
Definition: WalkerType.h:16
Definition: Walker.h:30
static AWalker * SpawnWalker(FWalkerParameters Parameters, const FString &ActorName="", const FString &ActorID="", bool StartAutomatically=true)
static EWalkerEndAction ConvertStringToWalkerEndActionType(const FString &String)
static EWalkerAction ConvertStringToWalkerActionType(const FString &String)
static EWalkerType ConvertStringToWalkerType(const FString &String)
static void ParseWalkerSpawnPoints(const TSharedPtr< FJsonObject > &ParametersObject, FWalkerParameters &WalkerParams)
static int32 GetIntValueOrDefault(const TSharedPtr< FJsonObject > &JsonObject, const FString &FieldName, int32 DefaultValue)
EWalkerAction WalkerAction
EWalkerType WalkerType
EWalkerEndAction WalkerEndAction

References UEnumUtilities::ConvertStringToWalkerActionType(), UEnumUtilities::ConvertStringToWalkerEndActionType(), UEnumUtilities::ConvertStringToWalkerType(), FWalkerParameters::CreateTransformTopic, GetIntValueOrDefault(), HandleSpectatorMovement(), SimulatorLog::Log(), NONE, ParseWalkerSpawnPoints(), FWalkerParameters::Points, Roaming, UAssetLibrary::SpawnWalker(), Walker, FWalkerParameters::WalkerAction, FWalkerParameters::WalkerEndAction, FWalkerParameters::WalkerSpeed, and FWalkerParameters::WalkerType.

Referenced by ParseAndOperateJSONFile().

Member Data Documentation

◆ IsDataCapture

bool USimulatorJsonParser::IsDataCapture = false
staticprivate

Definition at line 107 of file SimulatorJsonParser.h.

Referenced by ParseAndOperateJSONFile(), and SpawnSensorWorld().

◆ SpawnedSensorsForDataCapture

TArray< ASensor * > USimulatorJsonParser::SpawnedSensorsForDataCapture
staticprivate

The documentation for this class was generated from the following files: