From fa7c187fb09787e70db8c5be10217544012fe14e Mon Sep 17 00:00:00 2001 From: Aaron Samaniego Date: Mon, 19 Feb 2024 09:35:28 +0100 Subject: [PATCH] Add initial code for commandlet --- .../SetProperPositionForWorldPartition.cpp | 97 +++++++++++++++++++ .../SetProperPositionForWorldPartition.h | 46 +++++++++ 2 files changed, 143 insertions(+) create mode 100644 Unreal/CarlaUE4/Plugins/CarlaTools/Source/CarlaTools/Private/Commandlet/SetProperPositionForWorldPartition.cpp create mode 100644 Unreal/CarlaUE4/Plugins/CarlaTools/Source/CarlaTools/Public/Commandlet/SetProperPositionForWorldPartition.h diff --git a/Unreal/CarlaUE4/Plugins/CarlaTools/Source/CarlaTools/Private/Commandlet/SetProperPositionForWorldPartition.cpp b/Unreal/CarlaUE4/Plugins/CarlaTools/Source/CarlaTools/Private/Commandlet/SetProperPositionForWorldPartition.cpp new file mode 100644 index 000000000..46b812ae6 --- /dev/null +++ b/Unreal/CarlaUE4/Plugins/CarlaTools/Source/CarlaTools/Private/Commandlet/SetProperPositionForWorldPartition.cpp @@ -0,0 +1,97 @@ +// Copyright (c) 2024 Computer Vision Center (CVC) at the Universitat Autonoma +// de Barcelona (UAB). +// +// This work is licensed under the terms of the MIT license. +// For a copy, see . + +#include "Commandlet/SetProperPositionForWorldPartition.h" + +#if WITH_EDITOR +#include "FileHelpers.h" +#endif +#include "UObject/ConstructorHelpers.h" +#include "EditorLevelLibrary.h" + +DEFINE_LOG_CATEGORY(LogCarlaToolsMapSetProperPositionForWorldPartitionCommandlet); + + +USetProperPositionForWorldPartition::USetProperPositionForWorldPartition() +{ +#if WITH_EDITOR + ConstructorHelpers::FClassFinder OpenDrivelassFinder(TEXT("/CarlaTools/OnroadMapGenerator/BP_OpenDriveToMap")); + OpenDriveClass = OpenDrivelassFinder.Class; +#endif +} + +USetProperPositionForWorldPartition::USetProperPositionForWorldPartition(const FObjectInitializer& Initializer) + : Super(Initializer) +{ +#if WITH_EDITOR + ConstructorHelpers::FClassFinder OpenDrivelassFinder(TEXT("/CarlaTools/OnroadMapGenerator/BP_OpenDriveToMap")); + OpenDriveClass = OpenDrivelassFinder.Class; +#endif +} + +#if WITH_EDITORONLY_DATA + +int32 USetProperPositionForWorldPartition::Main(const FString &Params) +{ + UE_LOG(LogCarlaToolsMapGenerateTileCommandlet, Log, TEXT("UGenerateTileCommandlet::Main Arguments %s"), *Params); + TArray Tokens; + TArray Switches; + TMap ParamsMap; + + ParseCommandLine(*Params, Tokens, Switches, ParamsMap ); + + OpenDriveMap = NewObject(this, OpenDriveClass); + FString FilePath = ParamsMap["FilePath"]; + FString BaseLevelName = ParamsMap["BaseLevelName"]; + FIntVector CurrentTilesInXY = FIntVector(FCString::Atof(*ParamsMap["CTileX"]),FCString::Atof(*ParamsMap["CTileY"]), 0); + + UEditorLevelLibrary::LoadLevel(*BaseLevelName); + UE_LOG(LogCarlaToolsMapGenerator, Warning, TEXT("Valid Map loaded")); + UE_LOG(LogCarlaToolsMapGenerator, Warning, TEXT("MapName %s"), *MapName); + + AActor* QueryActor = UGameplayStatics::GetActorOfClass( + GEditor->GetEditorWorldContext().World(), + ALargeMapManager::StaticClass()); + if (QueryActor != nullptr) { + ALargeMapManager* LmManager = Cast(QueryActor); + NumTilesInXY = LmManager->GetNumTilesInXY(); + TileSize = LmManager->GetTileSize(); + Tile0Offset = LmManager->GetTile0Offset(); + + FCarlaMapTile& CarlaTile = LmManager->GetCarlaMapTile(CurrentTilesInXY); + UEditorLevelLibrary::SaveCurrentLevel(); + + UE_LOG(LogCarlaToolsMapGenerator, Warning, TEXT("Current Tile is %s"), *(CurrentTilesInXY.ToString())); + UE_LOG(LogCarlaToolsMapGenerator, Warning, TEXT("NumTilesInXY is %s"), *(NumTilesInXY.ToString())); + UE_LOG(LogCarlaToolsMapGenerator, Warning, TEXT("TileSize is %f"), (TileSize)); + UE_LOG(LogCarlaToolsMapGenerator, Warning, TEXT("Tile0Offset is %s"), *(Tile0Offset.ToString())); + UE_LOG(LogCarlaToolsMapGenerator, Warning, TEXT("Tile Name is %s"), *(CarlaTile.Name)); + + UEditorLevelLibrary::LoadLevel(CarlaTile.Name); + + MinPosition = FVector(CurrentTilesInXY.X * TileSize, CurrentTilesInXY.Y * -TileSize, 0.0f); + MaxPosition = FVector((CurrentTilesInXY.X + 1.0f) * TileSize, (CurrentTilesInXY.Y + 1.0f) * -TileSize, 0.0f); + + + TArray FoundActors; + UGameplayStatics::GetAllActorsOfClass(GetWorld(), AActor::StaticClass(), FoundActors); + + for (AActor* CA : FoundActors) + { + CA->AddActorWorldOffset(MinPosition, false, nullptr, ETeleportType::ResetPhysics); + } + } + else { + UE_LOG(LogCarlaToolsMapGenerator, Error, TEXT("Largemapmanager not found ")); + } + + UEditorLoadingAndSavingUtils::SaveDirtyPackages(true, true); + UEditorLevelLibrary::SaveCurrentLevel(); + + return 0; +} + +#endif diff --git a/Unreal/CarlaUE4/Plugins/CarlaTools/Source/CarlaTools/Public/Commandlet/SetProperPositionForWorldPartition.h b/Unreal/CarlaUE4/Plugins/CarlaTools/Source/CarlaTools/Public/Commandlet/SetProperPositionForWorldPartition.h new file mode 100644 index 000000000..74c550d3f --- /dev/null +++ b/Unreal/CarlaUE4/Plugins/CarlaTools/Source/CarlaTools/Public/Commandlet/SetProperPositionForWorldPartition.h @@ -0,0 +1,46 @@ +// Copyright (c) 2024 Computer Vision Center (CVC) at the Universitat Autonoma +// de Barcelona (UAB). +// +// This work is licensed under the terms of the MIT license. +// For a copy, see . + +#pragma once + +#include "Runtime/Engine/Classes/Engine/ObjectLibrary.h" +#include "Commandlets/Commandlet.h" + +#include + +#include + +#include "OpenDriveToMap.h" + +#include "SetProperPositionForWorldPartition.generated.h" + +// Each commandlet should generate only 1 Tile + +DECLARE_LOG_CATEGORY_EXTERN(LogCarlaToolsMapSetProperPositionForWorldPartitionCommandlet, Log, All); + + +UCLASS() +class CARLATOOLS_API USetProperPositionForWorldPartition + : public UCommandlet +{ + GENERATED_BODY() + +public: + + /// Default constructor. + USetProperPositionForWorldPartition(); + USetProperPositionForWorldPartition(const FObjectInitializer &); + +#if WITH_EDITORONLY_DATA + + virtual int32 Main(const FString &Params) override; + +#endif // WITH_EDITORONLY_DATA + UPROPERTY() + UOpenDriveToMap* OpenDriveMap; + UPROPERTY() + UClass* OpenDriveClass; +};