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 871 of file SimulatorJsonParser.cpp.

872{
873 if (!ActorPtr || !GEngine || !GEngine->GameViewport)
874 {
875 return;
876 }
877
878 UWorld* World = GEngine->GameViewport->GetWorld();
879 if (!World)
880 {
881 return;
882 }
883
885 APlayerController* PlayerController = UGameplayStatics::GetPlayerController(World, 0);
886 if (PlayerController && Spectator)
887 {
888 APlayerCameraManager* CameraManager = PlayerController->PlayerCameraManager;
889 if (CameraManager)
890 {
891 USceneComponent* CameraRootComponent = CameraManager->GetRootComponent();
892 if (CameraRootComponent)
893 {
894 // Set initial location and rotation to Spectator location
895 ActorPtr->SetActorLocation(Spectator->GetActorLocation());
896 ActorPtr->SetActorRotation(Spectator->GetActorRotation());
897
898 // Now attach ActorPtr to PlayerCameraManager CameraRootComponent so it follows the camera movement
899 ActorPtr->AttachToComponent(CameraRootComponent, FAttachmentTransformRules::SnapToTargetNotIncludingScale);
900
901 // Delete sensor model from the sensor after attaching to Spectator.
902 ASensor* SensorPtr = Cast<ASensor>(ActorPtr);
903 if (SensorPtr)
904 {
905 auto* SensorModelPtr = SensorPtr->GetSensorModel();
906 if (SensorModelPtr)
907 {
908 SensorModelPtr->Destroy();
909 }
910 }
911 }
912 }
913 }
914}
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 1167 of file SimulatorJsonParser.cpp.

1168{
1169 int32 Value = DefaultValue;
1170
1171 if (JsonObject.IsValid() && JsonObject->HasField(FieldName))
1172 {
1173 JsonObject->TryGetNumberField(FieldName, Value);
1174 }
1175
1176 return Value;
1177}

Referenced by SpawnWalker().

◆ HandleSpectatorMovement()

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

Definition at line 823 of file SimulatorJsonParser.cpp.

824{
825 if (!JsonObject.IsValid() || !Actor)
826 {
827 return;
828 }
829
830 bool follow = false;
831 bool teleport = false;
832 JsonObject->TryGetBoolField(TEXT("followObject"), follow);
833 JsonObject->TryGetBoolField(TEXT("teleportSpectator"), teleport);
834
835 if (!follow && !teleport)
836 {
837 // If neither is set, return
838 return;
839 }
840
841 if (GEngine && GEngine->GameViewport)
842 {
843 UWorld* World = GEngine->GameViewport->GetWorld();
845 if (Spectator)
846 {
847 if (follow)
848 {
849 AVehicle* VehiclePtr = Cast<AVehicle>(Actor);
850 if (VehiclePtr)
851 {
853 if (SimulationLevelManager)
854 {
855 SimulationLevelManager->TakeManualControlOfVehicle(VehiclePtr);
856 }
857 }
858 /*else
859 {
860 Spectator->FollowActor(Actor, false, 150.0f, 50.0f);
861 }*/
862 }
863 else if (teleport)
864 {
865 Spectator->TeleportSpectator(Transform);
866 }
867 }
868 }
869}
bool TakeManualControlOfVehicle(AVehicle *Vehicle)
void TeleportSpectator(const FTransform &Transform)
Definition: Spectator.cpp:75
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 int32 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 float DroneSlowToPointModifier = 1.0f;
795 ParametersObject->TryGetNumberField(TEXT("droneSlowToPointModifier"), DroneSlowToPointModifier);
796 DroneParameters.DroneSlowToPointModifier = DroneSlowToPointModifier;
797
798 bool DebugPointTransforms = false;
799 ParametersObject->TryGetBoolField(TEXT("debugPointTransforms"), DebugPointTransforms);
800
801 DronePtr->ChangeDroneParameters(DroneParameters);
802 DronePtr->SetShowForwardArrow(DroneParameters.ShowForwardArrow);
803 DronePtr->SetDrawDebugPoints(DebugPointTransforms);
804 }
805}
EDroneAction
Definition: DroneAction.h:15
EDroneEndAction
static EDroneEndAction GetDroneEndAction(FString endActionstring)
static EDroneAction GetDroneAction(FString actionstring)
void SetDrawDebugPoints(bool show)
Definition: PIDDrone.h:177
void ChangeDroneParameters(const FDroneParameters &newParameters)
Definition: PIDDrone.cpp:36
void SetShowForwardArrow(bool Show)
Definition: PIDDrone.h:172
static bool AlignTransformFromGroundInMeters(AActor *Actor, FTransform &InTransform)
static FTransform ParseTransform(const FString &FieldName, const TSharedPtr< FJsonObject > &Object)
float InnerOverlapRadiusMeters
EDroneEndAction DroneEndAction
EDroneAction DroneAction
float DroneSlowToPointModifier
TArray< FTransform > Points

References UPhysicsUtilities::AlignTransformFromGroundInMeters(), APIDDrone::ChangeDroneParameters(), FDroneParameters::CollisionsEnabled, FDroneParameters::CreateInnerOverlapSensor, FDroneParameters::DroneAction, FDroneParameters::DroneEndAction, FDroneParameters::DroneSlowToPointModifier, 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 916 of file SimulatorJsonParser.cpp.

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

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

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

Referenced by ParseVehicle().

◆ ParseCommands()

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

Definition at line 964 of file SimulatorJsonParser.cpp.

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

Referenced by ParseAndOperateJSONFile().

◆ ParseDataCapture()

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

Definition at line 1021 of file SimulatorJsonParser.cpp.

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

1144{
1145 FThermalCameraParameters ThermalCameraParams;
1146
1147 if (ParametersObject.IsValid())
1148 {
1149 // Parse fields manually because FJsonObjectConverter::JsonObjectToUStruct couldn't parse these properly.
1150 ThermalCameraParams.WarmColor = ParseVector4(TEXT("warmColor"), ParametersObject);
1151 ThermalCameraParams.ColdColor = ParseVector4(TEXT("coldColor"), ParametersObject);
1152 ParametersObject->TryGetBoolField(TEXT("allowCustomNoiseResolution"), ThermalCameraParams.AllowCustomNoiseResolution);
1153 ParametersObject->TryGetNumberField(TEXT("widthResolutionNoise"), ThermalCameraParams.WidthResolutionNoise);
1154 ParametersObject->TryGetNumberField(TEXT("heightResolutionNoise"), ThermalCameraParams.HeightResolutionNoise);
1155
1156 // Parse FCameraBaseParameters
1157 TSharedPtr<FJsonObject> CameraParametersObject = ParametersObject->GetObjectField(TEXT("cameraParameters"));
1158 if (CameraParametersObject.IsValid())
1159 {
1160 ThermalCameraParams.CameraParameters = ParseParameters<FCameraBaseParameters>(CameraParametersObject);
1161 }
1162 }
1163
1164 return ThermalCameraParams;
1165}
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 1084 of file SimulatorJsonParser.cpp.

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

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 1125 of file SimulatorJsonParser.cpp.

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

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 991 of file SimulatorJsonParser.cpp.

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