Return the merged result of all generated trigger boxes as the bounding box of the actor

This commit is contained in:
bernatx 2022-07-21 11:37:10 +02:00 committed by bernat
parent 9bb8f41f3e
commit e032ae53ad
3 changed files with 52 additions and 1 deletions

View File

@ -23,11 +23,12 @@ TArray<UBoxComponent*> ATrafficSignBase::GetTriggerVolumes() const
GetComponents<USignComponent>(Components, false); GetComponents<USignComponent>(Components, false);
if (Components.Num()) if (Components.Num())
{ {
USignComponent* SignComponent = Components.Top(); USignComponent* SignComponent = Components[0];
return SignComponent->GetEffectTriggerVolume(); return SignComponent->GetEffectTriggerVolume();
} }
else else
{ {
UE_LOG(LogCarla, Log, TEXT("returning GetTriggerVolume"));
TArray<UBoxComponent*> TriggerVolumes; TArray<UBoxComponent*> TriggerVolumes;
TriggerVolumes.Add(GetTriggerVolume()); TriggerVolumes.Add(GetTriggerVolume());
return TriggerVolumes; return TriggerVolumes;

View File

@ -74,6 +74,16 @@ FBoundingBox UBoundingBoxCalculator::GetActorBoundingBox(const AActor *Actor, ui
auto TrafficSign = Cast<ATrafficSignBase>(Actor); auto TrafficSign = Cast<ATrafficSignBase>(Actor);
if (TrafficSign != nullptr) 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(); auto TriggerVolume = TrafficSign->GetTriggerVolume();
if (TriggerVolume != nullptr) if (TriggerVolume != nullptr)
{ {
@ -610,6 +620,43 @@ FBoundingBox UBoundingBoxCalculator::CombineBBs(const TArray<FBoundingBox>& BBsT
return {Origin, Extent}; return {Origin, Extent};
} }
FBoundingBox UBoundingBoxCalculator::CombineBoxes(const TArray<UBoxComponent *>& BBsToCombine)
{
FVector MaxVertex(TNumericLimits<float>::Lowest());
FVector MinVertex(TNumericLimits<float>::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( void UBoundingBoxCalculator::GetMeshCompsFromActorBoundingBox(
const AActor* Actor, const AActor* Actor,
const FBoundingBox& InBB, const FBoundingBox& InBB,

View File

@ -99,6 +99,9 @@ public:
UFUNCTION(Category = "Carla Util", BlueprintCallable) UFUNCTION(Category = "Carla Util", BlueprintCallable)
static FBoundingBox CombineBBs(const TArray<FBoundingBox>& BBsToCombine); static FBoundingBox CombineBBs(const TArray<FBoundingBox>& BBsToCombine);
UFUNCTION(Category = "Carla Util", BlueprintCallable)
static FBoundingBox CombineBoxes(const TArray<UBoxComponent *>& BBsToCombine);
// Returns Static Mesh Components that generate the InBB of the Actor // Returns Static Mesh Components that generate the InBB of the Actor
// ie: the SMComps that creates the BB of the TL light box // ie: the SMComps that creates the BB of the TL light box
UFUNCTION(Category = "Carla Actor", BlueprintCallable) UFUNCTION(Category = "Carla Actor", BlueprintCallable)