From e032ae53ada6592ec1dab5adc0d09b71a91f32ad Mon Sep 17 00:00:00 2001 From: bernatx Date: Thu, 21 Jul 2022 11:37:10 +0200 Subject: [PATCH] Return the merged result of all generated trigger boxes as the bounding box of the actor --- .../Source/Carla/Traffic/TrafficSignBase.cpp | 3 +- .../Carla/Util/BoundingBoxCalculator.cpp | 47 +++++++++++++++++++ .../Source/Carla/Util/BoundingBoxCalculator.h | 3 ++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficSignBase.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficSignBase.cpp index 7568fcaea..215a62870 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficSignBase.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficSignBase.cpp @@ -23,11 +23,12 @@ TArray ATrafficSignBase::GetTriggerVolumes() const GetComponents(Components, false); if (Components.Num()) { - USignComponent* SignComponent = Components.Top(); + USignComponent* SignComponent = Components[0]; return SignComponent->GetEffectTriggerVolume(); } else { + UE_LOG(LogCarla, Log, TEXT("returning GetTriggerVolume")); TArray TriggerVolumes; TriggerVolumes.Add(GetTriggerVolume()); return TriggerVolumes; diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Util/BoundingBoxCalculator.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Util/BoundingBoxCalculator.cpp index 5d195b209..511803e9d 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Util/BoundingBoxCalculator.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Util/BoundingBoxCalculator.cpp @@ -74,6 +74,16 @@ FBoundingBox UBoundingBoxCalculator::GetActorBoundingBox(const AActor *Actor, ui auto TrafficSign = Cast(Actor); if (TrafficSign != nullptr) { + // first return a merge of the generated trigger boxes, if any + auto TriggerVolumes = TrafficSign->GetTriggerVolumes(); + if (TriggerVolumes.Num() > 0) + { + FBoundingBox Box = UBoundingBoxCalculator::CombineBoxes(TriggerVolumes); + FTransform Transform = Actor->GetTransform(); + Box.Rotation = Transform.InverseTransformRotation(Box.Rotation.Quaternion()).Rotator(); + return Box; + } + // try to return the original bounding box auto TriggerVolume = TrafficSign->GetTriggerVolume(); if (TriggerVolume != nullptr) { @@ -610,6 +620,43 @@ FBoundingBox UBoundingBoxCalculator::CombineBBs(const TArray& BBsT return {Origin, Extent}; } +FBoundingBox UBoundingBoxCalculator::CombineBoxes(const TArray& BBsToCombine) +{ + FVector MaxVertex(TNumericLimits::Lowest()); + FVector MinVertex(TNumericLimits::Max()); + + for(const UBoxComponent *BB : BBsToCombine) { + auto BoxOrigin = BB->GetComponentLocation(); + auto BoxExtent = BB->GetScaledBoxExtent(); + + FVector MaxVertexOfBB = BoxOrigin + BoxExtent; + FVector MinVertexOfBB = BoxOrigin - BoxExtent; + + MaxVertex.X = (MaxVertexOfBB.X > MaxVertex.X) ? MaxVertexOfBB.X : MaxVertex.X; + MaxVertex.Y = (MaxVertexOfBB.Y > MaxVertex.Y) ? MaxVertexOfBB.Y : MaxVertex.Y; + MaxVertex.Z = (MaxVertexOfBB.Z > MaxVertex.Z) ? MaxVertexOfBB.Z : MaxVertex.Z; + MinVertex.X = (MinVertexOfBB.X < MinVertex.X) ? MinVertexOfBB.X : MinVertex.X; + MinVertex.Y = (MinVertexOfBB.Y < MinVertex.Y) ? MinVertexOfBB.Y : MinVertex.Y; + MinVertex.Z = (MinVertexOfBB.Z < MinVertex.Z) ? MinVertexOfBB.Z : MinVertex.Z; + } + + // Calculate box extent + FVector Extent ( + (MaxVertex.X - MinVertex.X) * 0.5f, + (MaxVertex.Y - MinVertex.Y) * 0.5f, + (MaxVertex.Z - MinVertex.Z) * 0.5f + ); + + // Calculate middle point + FVector Origin ( + (MinVertex.X + Extent.X), + (MinVertex.Y + Extent.Y), + (MinVertex.Z + Extent.Z) + ); + + return {Origin, Extent}; +} + void UBoundingBoxCalculator::GetMeshCompsFromActorBoundingBox( const AActor* Actor, const FBoundingBox& InBB, diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Util/BoundingBoxCalculator.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Util/BoundingBoxCalculator.h index 2bcb3875e..82d607054 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Util/BoundingBoxCalculator.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Util/BoundingBoxCalculator.h @@ -99,6 +99,9 @@ public: UFUNCTION(Category = "Carla Util", BlueprintCallable) static FBoundingBox CombineBBs(const TArray& BBsToCombine); + UFUNCTION(Category = "Carla Util", BlueprintCallable) + static FBoundingBox CombineBoxes(const TArray& BBsToCombine); + // Returns Static Mesh Components that generate the InBB of the Actor // ie: the SMComps that creates the BB of the TL light box UFUNCTION(Category = "Carla Actor", BlueprintCallable)