Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 69 additions & 12 deletions Source/CarlaDigitalTwinsTool/Private/Generation/OpenDriveToMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,8 @@ void UOpenDriveToMap::GenerateTile()
FFileHelper::LoadFileToString(FileContent, *FilePath);
std::string opendrive_xml = carla::rpc::FromLongFString(FileContent);
UE_LOG(LogCarlaDigitalTwinsTool, Warning, TEXT("UOpenDriveToMap::GenerateTile() Loading File..... "));
UE_LOG(LogCarlaDigitalTwinsTool, Warning, TEXT("Current tile: %i, %i"),CurrentTilesInXY.X, CurrentTilesInXY.Y );

CarlaMap = carla::opendrive::OpenDriveParser::Load(opendrive_xml);

if (!CarlaMap.has_value())
Expand Down Expand Up @@ -813,11 +815,13 @@ void UOpenDriveToMap::GenerateCurbSplinesFromRoadRenders()
MinPosition = FVector(CurrentTilesInXY.X * TileSize, CurrentTilesInXY.Y * -TileSize, 0.0f);
MaxPosition = FVector((CurrentTilesInXY.X + 1.0f) * TileSize, (CurrentTilesInXY.Y + 1.0f) * -TileSize, 0.0f);

RenderRoadToTexture(MinPosition, MaxPosition);
RenderRoadToTexture(MinPosition, MaxPosition, "Layer0");
RenderRoadToTexture(MinPosition, MaxPosition, "Layer1");

} while (GoNextTile());

GenerateCurbSplines();
GenerateCurbSplines("Layer0");
GenerateCurbSplines("Layer1");
}

TArray<AActor*> UOpenDriveToMap::GenerateMiscActors(float Offset, FVector MinLocation, FVector MaxLocation)
Expand Down Expand Up @@ -907,6 +911,7 @@ void UOpenDriveToMap::GenerateRoadMesh(
FVector MeshCentroid;
carla::road::Lane::LaneType LaneType;
int32 Index;
FString MeshLayer;
};

TArray<FPreparedMeshData> PreparedMeshes;
Expand All @@ -927,14 +932,18 @@ void UOpenDriveToMap::GenerateRoadMesh(

auto& Vertices = Mesh->GetVertices();

FString MeshLayer = "Layer0";

if (LaneType == carla::road::Lane::LaneType::Driving)
{
for (auto& Vertex : Vertices)
{
FVector FV = Vertex.ToFVector();
Vertex.z +=
GetHeight(Vertex.x * 100.0f, Vertex.y * 100.0f, DistanceToLaneBorder(ParamCarlaMap, FV) > 65.0f) /
100.0f;
if (Vertex.z > 0.0f){
MeshLayer = "Layer1";
}

Vertex.z += GetHeight(Vertex.x * 100.0f, Vertex.y * 100.0f, DistanceToLaneBorder(ParamCarlaMap, FV) > 65.0f) / 100.0f;
}
#if ENGINE_MAJOR_VERSION < 5
carla::geom::Simplification Simplify(0.15);
Expand Down Expand Up @@ -965,6 +974,7 @@ void UOpenDriveToMap::GenerateRoadMesh(
Data.MeshData = *Mesh;
Data.MeshCentroid = Centroid;
Data.LaneType = LaneType;
Data.MeshLayer = MeshLayer;

int32 AssignedIndex;
{
Expand All @@ -982,6 +992,7 @@ void UOpenDriveToMap::GenerateRoadMesh(
const FVector& Centroid = Entry.MeshCentroid;
const int32 Index = Entry.Index;
const carla::road::Lane::LaneType LaneType = Entry.LaneType;
const FString& MeshLayer = Entry.MeshLayer;

TArray<FProcMeshTangent> Tangents;
UKismetProceduralMeshLibrary::CalculateTangentsForMesh(
Expand All @@ -1006,7 +1017,7 @@ void UOpenDriveToMap::GenerateRoadMesh(

StaticMeshComponent->SetMaterial(0, DuplicatedRoadMaterial);
StaticMeshComponent->CastShadow = false;
TempActor->SetActorLabel(FString("SM_DrivingLane_") + FString::FromInt(Index));
TempActor->SetActorLabel(FString("SM_DrivingLane_") + MeshLayer + "_" + FString::FromInt(Index));
}

UStaticMesh* FinalMesh = nullptr;
Expand Down Expand Up @@ -1611,7 +1622,7 @@ void UOpenDriveToMap::UnloadWorldPartitionRegion(const FBox& RegionBox)
}
}

void UOpenDriveToMap::RenderRoadToTexture(FVector MinLocation, FVector MaxLocation)
void UOpenDriveToMap::RenderRoadToTexture(FVector MinLocation, FVector MaxLocation, FString MeshLayer)
{
UE_LOG(LogCarlaDigitalTwinsTool, Log, TEXT("Render road for curbs generation"));

Expand All @@ -1629,6 +1640,8 @@ void UOpenDriveToMap::RenderRoadToTexture(FVector MinLocation, FVector MaxLocati
}
#endif

RoadLabel += "_" + MeshLayer;

TArray<AActor*> HiddenActors;
{
TArray<AActor*> Actors;
Expand Down Expand Up @@ -1667,7 +1680,7 @@ void UOpenDriveToMap::RenderRoadToTexture(FVector MinLocation, FVector MaxLocati

FActorSpawnParameters ActorSpawnParameters;
ActorSpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
ActorSpawnParameters.Name = FName(*(TEXT("Camera") + GetStringForCurrentTile()));
ActorSpawnParameters.Name = FName(*(TEXT("Camera_" + MeshLayer) + GetStringForCurrentTile()));

auto Camera = World->SpawnActor<ASceneCapture2D>(ASceneCapture2D::StaticClass(), ActorSpawnParameters);

Expand Down Expand Up @@ -1709,7 +1722,7 @@ void UOpenDriveToMap::RenderRoadToTexture(FVector MinLocation, FVector MaxLocati
ImageTask->PixelData = MoveTemp(PixelData);

FString OutPath = UGenerationPathsHelper::GetPythonIntermediatePath(MapName);
FString ImagePath = OutPath / TEXT("road_render") + GetStringForCurrentTile() + TEXT(".png");
FString ImagePath = OutPath / TEXT("road_render") + "_" + MeshLayer + GetStringForCurrentTile() + TEXT(".png");

ImageTask->Filename = ImagePath;
ImageTask->Format = EImageFormat::PNG;
Expand All @@ -1729,7 +1742,7 @@ void UOpenDriveToMap::RenderRoadToTexture(FVector MinLocation, FVector MaxLocati
Task.Wait();
}

void UOpenDriveToMap::GenerateCurbSplines()
void UOpenDriveToMap::GenerateCurbSplines(FString MeshLayer)
{
UE_LOG(LogCarlaDigitalTwinsTool, Log, TEXT("Create splines for curbs generation"));

Expand All @@ -1741,7 +1754,7 @@ void UOpenDriveToMap::GenerateCurbSplines()

RunPythonRoadEdges();

auto JsonPath = OutPath / TEXT("contours.json");
auto JsonPath = OutPath / TEXT("contours_"+MeshLayer+".json");

MinPosition = FVector(0.0f, 0.0f, 0.0f);
MaxPosition = FVector(NumTilesInXY.X * TileSize, NumTilesInXY.Y * -TileSize, 0.0f);
Expand All @@ -1761,27 +1774,71 @@ void UOpenDriveToMap::GenerateCurbSplines()

UE_LOG(LogCarlaDigitalTwinsTool, Log, TEXT("Number of road splines: %i"), RoadSplines.Num());

// Project spline points to the floor

for (auto Spline : RoadSplines)
{
int32 NumPoints = Spline->GetNumberOfSplinePoints();
for (int32 i = 0; i < NumPoints; ++i)
{
// Firstly project level 0 spline points to the floor and level 1 to a higher height (maybe this is not required and can be cleaned up)
FVector Pos = Spline->GetLocationAtSplinePoint(i, ESplineCoordinateSpace::Local);

Pos.Z = GetHeight(Pos.X, Pos.Y, false);

if (MeshLayer=="Layer1"){
Pos.Z += OpenDriveGenParams.DefaultOSMLayerHeight;
}

Spline->SetLocationAtSplinePoint(i, Pos, ESplineCoordinateSpace::Local, false);

Spline->UpdateSpline();

// Improve spline placement with ray cast
UOpenDriveToMap::AlignSplineToRoadMesh(Spline, World, MeshLayer);

UE_LOG(LogCarlaDigitalTwinsTool, Log, TEXT("Spline updated"));
}
}

GeneratedSplines.Append(RoadSplines);
}

void UOpenDriveToMap::AlignSplineToRoadMesh(USplineComponent* Spline, UWorld* World, const FString& NamePrefix)
{
if (!Spline || !World) return;

FCollisionQueryParams QueryParams;
QueryParams.bTraceComplex = true;
QueryParams.AddIgnoredActor(Spline->GetOwner());

for (int32 i = 0; i < Spline->GetNumberOfSplinePoints(); i++)
{
FVector PointLocation = Spline->GetLocationAtSplinePoint(i, ESplineCoordinateSpace::World);

FVector Start = FVector(PointLocation.X, PointLocation.Y, PointLocation.Z + 10000.0f);
FVector End = FVector(PointLocation.X, PointLocation.Y, PointLocation.Z - 10000.0f);

FHitResult Hit;
if (World->LineTraceSingleByChannel(Hit, Start, End, ECC_Visibility, QueryParams))
{
UPrimitiveComponent* HitComp = Hit.GetComponent();
if (HitComp)
{
FString CompName = HitComp->GetName();
if (CompName.StartsWith(NamePrefix))
{
FVector NewLocation = PointLocation;
NewLocation.Z = Hit.Location.Z;

Spline->SetLocationAtSplinePoint(i, NewLocation, ESplineCoordinateSpace::World, true);
}
}
}
}

Spline->UpdateSpline();
}

void UOpenDriveToMap::RunPythonScript(FString ScriptPath, FString Args)
{
FString PythonExe = PythonBinPath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ class CARLADIGITALTWINSTOOL_API UOpenDriveToMap : public UEditorUtilityObject
protected:

UFUNCTION(BlueprintCallable, Category = "Assets Placement")
void RenderRoadToTexture(FVector MinLocation, FVector MaxLocation);
void RenderRoadToTexture(FVector MinLocation, FVector MaxLocation, FString MeshLayer);

UFUNCTION(BlueprintCallable, Category = "Assets Placement")
void RunPythonScript(FString ScriptPath, FString Args);
Expand All @@ -249,11 +249,14 @@ class CARLADIGITALTWINSTOOL_API UOpenDriveToMap : public UEditorUtilityObject
void RunPythonRoadEdges();

UFUNCTION(BlueprintCallable, Category = "Assets Placement")
void GenerateCurbSplines();
void GenerateCurbSplines(FString MeshLayer);

UFUNCTION(BlueprintCallable, Category = "Assets Placement")
void GenerateCurbSplinesFromRoadRenders();

UFUNCTION(BlueprintCallable, Category = "Assets Placement")
void AlignSplineToRoadMesh(USplineComponent* Spline, UWorld* World, const FString& NamePrefix);

UFUNCTION(BlueprintCallable, Category = "Mitsuba")
void ExportStaticMeshToOBJ(UStaticMesh *StaticMesh, const FString &OutputPath);

Expand Down