16#include "Components/CapsuleComponent.h"
17#include "GameFramework/Character.h"
18#include "Components/BoxComponent.h"
19#include "Rendering/SkeletalMeshRenderData.h"
20#include "Engine/SkeletalMeshSocket.h"
45 auto Vehicle = Cast<ACarlaWheeledVehicle>(Actor);
48 FVector Origin =
Vehicle->GetVehicleBoundingBoxTransform().GetTranslation();
49 FVector Extent =
Vehicle->GetVehicleBoundingBoxExtent();
50 return {Origin, Extent};
56 UActorComponent *ActorComp =
Character->GetComponentByClass(USkeletalMeshComponent::StaticClass());
57 USkeletalMeshComponent* ParentComp = Cast<USkeletalMeshComponent>(ActorComp);
59 if (ParentComp !=
nullptr)
69 const FTransform &MeshRelative = ParentComp->GetRelativeTransform();
72 if (
Character->GetName().Contains(
"_AB001_G3")
73 ||
Character->GetName().Contains(
"_AG001_G3"))
76 Box.Origin.Z -= Box.Extent.Z * (1.0f / 0.65f - 1.0f);
82 auto WalkerBase = Cast<AWalkerBase>(
Character);
83 if (WalkerBase && WalkerBase->bUsesWheelChair)
85 Box.Origin.X += 15.0f;
86 Box.Origin.Z -= 15.0f;
96 auto TrafficSign = Cast<ATrafficSignBase>(Actor);
97 if (TrafficSign !=
nullptr)
102 auto TriggerVolumes = TrafficSign->GetTriggerVolumes();
106 UBoxComponent* FirstTriggerVolume =
nullptr;
107 for (UBoxComponent* Volume : TriggerVolumes)
109 if (Volume !=
nullptr)
111 FirstTriggerVolume = Volume;
115 if (FirstTriggerVolume !=
nullptr)
117 FTransform
Transform = Actor->GetActorTransform();
120 TVWorld.
Origin = FirstTriggerVolume->GetComponentLocation();
121 TVWorld.
Extent = FirstTriggerVolume->GetScaledBoxExtent();
122 TVWorld.
Rotation = FirstTriggerVolume->GetComponentRotation();
145 UE_LOG(LogCarla, Warning, TEXT(
"Traffic sign missing trigger volume: %s"), *Actor->GetName());
153 FTransform
Transform = Actor->GetActorTransform();
172 UActorComponent *ActorComp =
Vehicle->GetComponentByClass(USkeletalMeshComponent::StaticClass());
173 USkeletalMeshComponent* ParentComp = Cast<USkeletalMeshComponent>(ActorComp);
177 if(FilterByTagEnabled && Tag != TagQueried)
return {};
179 USkeletalMesh* SkeletalMesh = ParentComp->GetSkeletalMeshAsset();
184 UE_LOG(LogCarla, Error, TEXT(
"%s has no SKM assigned"), *
Vehicle->GetName());
189 TArray<USkeletalMeshComponent*> SkeletalMeshComps;
190 Vehicle->GetComponents<USkeletalMeshComponent>(SkeletalMeshComps);
192 if(SkeletalMeshComps.Num() > 1)
194 FVector VehicleLocation =
Vehicle->GetActorLocation();
195 FRotator VehicleRotation =
Vehicle->GetActorRotation();
196 float MaxHeadLocation = TNumericLimits<float>::Lowest();
197 float MaxKneeLocation = TNumericLimits<float>::Lowest();
198 bool SocketFound =
false;
199 for(USkeletalMeshComponent* Comp : SkeletalMeshComps)
201 if(Comp == ParentComp)
continue;
204 FVector HeadLocation = Comp->GetSocketLocation(
"crl_Head__C") - VehicleLocation;
205 FVector KneeLocation = Comp->GetSocketLocation(
"crl_leg__L") - VehicleLocation;
207 HeadLocation = VehicleRotation.UnrotateVector(HeadLocation);
208 KneeLocation = VehicleRotation.UnrotateVector(KneeLocation);
210 MaxHeadLocation = (HeadLocation.Z > MaxHeadLocation) ? HeadLocation.Z : MaxHeadLocation;
211 MaxKneeLocation = (KneeLocation.Y > MaxKneeLocation) ? KneeLocation.Y : MaxKneeLocation;
221 Min = Min.ComponentMin(Max);
222 Max = Max.ComponentMin(Tmp);
224 MaxHeadLocation += 20.0f;
226 Max.Y = (MaxKneeLocation > Max.Y) ? MaxKneeLocation : Max.Y;
227 Min.Y = (-MaxKneeLocation < Min.Y) ? MaxKneeLocation : Min.Y;
228 Max.Z = (MaxHeadLocation > Max.Z) ? MaxHeadLocation : Max.Z;
230 BB.
Extent = (Max - Min) * 0.5f;
236 auto& CompToWorldTransform = ParentComp->GetComponentTransform();
251 UActorComponent *ActorComp =
Character->GetComponentByClass(USkeletalMeshComponent::StaticClass());
252 USkeletalMeshComponent* ParentComp = Cast<USkeletalMeshComponent>(ActorComp);
254 if (ParentComp && FilterByTag)
258 auto& CompToWorldTransform = ParentComp->GetComponentTransform();
270 TArray<FBoundingBox>& OutBB,
271 TArray<uint8>& OutTag,
276 TArray<FBoundingBox> BBsOfTL;
277 TArray<uint8> TagsOfTL;
281 const float DistanceThreshold = 100.0f * 100.0f;
287 check(BBsOfTL.Num() == TagsOfTL.Num());
291 for(
int i = 0; i < BBsOfTL.Num(); i++)
294 uint8 Tag = TagsOfTL[i];
296 if(FilterByTagEnabled && Tag != InTagQueried)
continue;
304 const USkeletalMeshComponent* SkeletalMeshComp
307 if (!SkeletalMeshComp || !SkeletalMeshComp->GetSkeletalMeshAsset())
312 TEXT(
"GetSkeletalMeshBoundingBoxFromComponent: missing SkeletalMeshComponent or SkeletalMesh"));
320 const FBoxSphereBounds LocalBounds =
321 SkeletalMeshComp->CalcBounds(FTransform::Identity);
323 return {LocalBounds.Origin, LocalBounds.BoxExtent};
331 UE_LOG(LogCarla, Error, TEXT(
"GetSkeletalMeshBoundingBox no SkeletalMesh"));
336 FSkeletalMeshRenderData* SkeletalMeshRenderData = SkeletalMesh->GetResourceForRendering();
337 FSkeletalMeshLODRenderData& LODRenderData = SkeletalMeshRenderData->LODRenderData[0];
338 FStaticMeshVertexBuffers& StaticMeshVertexBuffers = LODRenderData.StaticVertexBuffers;
339 FPositionVertexBuffer& FPositionVertexBuffer = StaticMeshVertexBuffers.PositionVertexBuffer;
340 uint32 NumVertices = FPositionVertexBuffer.GetNumVertices();
343 FVector Max(TNumericLimits<float>::Lowest());
344 FVector Min(TNumericLimits<float>::Max());
346 for(uint32 i = 0; i < NumVertices; i++)
348 auto Pos = FVector(FPositionVertexBuffer.VertexPosition(i));
349 Max = Max.ComponentMax(Pos);
350 Min = Min.ComponentMin(Pos);
353 auto Extent = (Max - Min) * 0.5f;
354 auto Origin = Min + Extent;
356 return {Origin, Extent};
367 FBox Box = StaticMesh->GetBoundingBox();
368 return {Box.GetCenter(), Box.GetExtent()};
373 UInstancedStaticMeshComponent*
ISMComp,
374 TArray<FBoundingBox>& OutBoundingBox)
378 UE_LOG(LogCarla, Error, TEXT(
"GetISMBoundingBox no ISMComp"));
382 const UStaticMesh *Mesh =
ISMComp->GetStaticMesh();
388 UE_LOG(LogCarla, Error, TEXT(
"Actor Name: %s Actor Labe: %s has no SM assigned to the ISM"), *
ISMComp->GetOwner()->GetName(), *
ISMComp->GetOwner()->GetActorLabel());
390 UE_LOG(LogCarla, Error, TEXT(
"Actor Name: %s has no SM assigned to the ISM"), *
ISMComp->GetOwner()->GetName());
395 const TArray<FInstancedStaticMeshInstanceData>& PerInstanceSMData =
ISMComp->PerInstanceSMData;
397 const FTransform ParentTransform =
ISMComp->GetComponentTransform();
399 for(
auto& InstSMIData : PerInstanceSMData)
401 const FTransform
Transform = FTransform(InstSMIData.Transform) * ParentTransform;
410 const TArray<UStaticMeshComponent*>& StaticMeshComps,
411 TArray<FBoundingBox>& OutBB,
412 TArray<uint8>& OutTag,
418 for(UStaticMeshComponent* Comp : StaticMeshComps)
421 bool isCrosswalk = Comp->GetOwner()->GetName().Contains(
"crosswalk");
424 if( (!Comp->IsVisible() && !isCrosswalk) || Cast<UInstancedStaticMeshComponent>(Comp))
431 if(FilterByTagEnabled && Tag != TagQueried)
continue;
433 UStaticMesh* StaticMesh = Comp->GetStaticMesh();
439 const FTransform& CompToWorldTransform = Comp->GetComponentTransform();
442 OutTag.Emplace(
static_cast<uint8
>(Tag));
448 const TArray<USkeletalMeshComponent*>& SkeletalMeshComps,
449 TArray<FBoundingBox>& OutBB,
450 TArray<uint8>& OutTag,
456 for(USkeletalMeshComponent* Comp : SkeletalMeshComps)
461 if(!Comp->IsVisible() || (FilterByTagEnabled && Tag != TagQueried))
continue;
463 USkeletalMesh* SkeletalMesh = Comp->GetSkeletalMeshAsset();
467 UE_LOG(LogCarla, Error, TEXT(
"%s has no SKM assigned"), *Comp->GetOwner()->GetName());
472 const FTransform& CompToWorldTransform = Comp->GetComponentTransform();
475 OutTag.Emplace(
static_cast<uint8
>(Tag));
481 const TArray<AActor*>& Actors,
484 TArray<FBoundingBox> Result;
485 for(
AActor* Actor : Actors)
487 TArray<FBoundingBox> BBs =
GetBBsOfActor(Actor, InTagQueried);
488 Result.Append(BBs.GetData(), BBs.Num());
498 TArray<FBoundingBox> Result;
503 FString ClassName = Actor->GetClass()->GetName();
509 if( ClassName.Contains(
"Procedural_Bulding") )
return Result;
544 TArray<UInstancedStaticMeshComponent *> ISMComps;
545 Actor->GetComponents<UInstancedStaticMeshComponent>(ISMComps);
546 for(UInstancedStaticMeshComponent* Comp: ISMComps)
550 if(FilterByTagEnabled && Tag != TagQueried)
continue;
556 TArray<UStaticMeshComponent*> StaticMeshComps;
558 Actor->GetComponents<UStaticMeshComponent>(StaticMeshComps);
562 TArray<USkeletalMeshComponent*> SkeletalMeshComps;
564 Actor->GetComponents<USkeletalMeshComponent>(SkeletalMeshComps);
572 TArray<FBoundingBox>& OutBB,
573 TArray<uint8>& OutTag,
574 const float DistanceThreshold,
579 TArray<FBoundingBox> BBsOfTL;
580 TArray<uint8> InternalOutTags;
581 TArray<UStaticMeshComponent*> StaticMeshComps;
582 Actor->GetComponents<UStaticMeshComponent>(StaticMeshComps);
585 bool IgnoreDistanceFilter = (DistanceThreshold <= 0.0f);
586 bool IgnoreTag = (TagToCombine == 0);
588 TSet<int> IndicesDiscarded;
589 for(
int i = 0; i < BBsOfTL.Num(); i++)
592 if(IndicesDiscarded.Contains(i))
continue;
594 TArray<FBoundingBox> BBsToCombine;
596 uint8 Tag1 = InternalOutTags[i];
598 for(
int j = i + 1; j < BBsOfTL.Num(); j++)
601 if(IndicesDiscarded.Contains(j))
continue;
604 uint8 Tag2 = InternalOutTags[j];
606 float Distance = FVector::DistSquared(BB1.
Origin, BB2.
Origin);
609 if( ((Distance <= DistanceThreshold) || IgnoreDistanceFilter) &&
610 ((Tag1 == Tag2) || IgnoreTag) )
612 BBsToCombine.Emplace(BB2);
613 IndicesDiscarded.Emplace(j);
616 if(BBsToCombine.Num() > 0)
618 BBsToCombine.Emplace(BB1);
619 IndicesDiscarded.Emplace(i);
622 OutBB.Emplace(MergedBB);
623 OutTag.Emplace(InternalOutTags[i]);
628 for(
int i = 0; i < BBsOfTL.Num(); i++)
631 if(IndicesDiscarded.Contains(i))
continue;
634 OutTag.Emplace(InternalOutTags[i]);
638template <
typename Collection,
typename ExtractAABB>
640 Collection&& collection,
641 ExtractAABB&& extract_aabb)
643 FVector Max(std::numeric_limits<float>::lowest());
644 FVector Min(std::numeric_limits<float>::max());
646 for (
auto& e : collection)
648 auto BB = extract_aabb(e);
649 auto LocalMax = BB.Origin + BB.Rotation.RotateVector(BB.Extent);
650 auto LocalMin = BB.Origin - BB.Rotation.RotateVector(BB.Extent);
652 LocalMin = LocalMin.ComponentMin(LocalMax);
653 LocalMax = LocalMax.ComponentMax(Tmp);
654 Max = Max.ComponentMax(LocalMax);
655 Min = Min.ComponentMin(LocalMin);
658 auto Extent = (Max - Min) * 0.5f;
659 auto Origin = Min + Extent;
661 return { Origin, Extent };
664template <
typename Collection>
667 using T = std::remove_reference_t<
decltype(collection[0])>;
682 e->GetComponentLocation(),
683 e->GetScaledBoxExtent(),
684 e->GetComponentRotation()
692 TArray<UStaticMeshComponent*>& OutStaticMeshComps)
697 const float DistanceThreshold = 100.0f * 100.0f;
699 TArray<UStaticMeshComponent*> SMComps;
700 Actor->GetComponents<UStaticMeshComponent>(SMComps);
702 for(UStaticMeshComponent* Comp : SMComps)
704 if(!Comp->IsVisible())
continue;
706 TArray<FBoundingBox> BBs;
707 TArray<uint8> InternalOutTags;
710 if(BBs.Num() == 0)
continue;
713 float Distance = FVector::DistSquared(InBB.
Origin, BB.
Origin);
714 if(Distance <= DistanceThreshold)
716 OutStaticMeshComps.Emplace(Comp);
static FBoundingBox ApplyTransformToBB(FBoundingBox InBB, const FTransform &Transform)
static FBoundingBox CombineBoundingBoxesGeneric(Collection &&collection, ExtractAABB &&extract_aabb)
Base class for CARLA wheeled vehicles.
static crp::CityObjectLabel GetTagOfTaggedComponent(const UPrimitiveComponent &Component)
Retrieve the tag of an already tagged component.
static void GetBBsOfSkeletalMeshComponents(const TArray< USkeletalMeshComponent * > &SkeletalMeshComps, TArray< FBoundingBox > &OutBB, TArray< uint8 > &OutTag, uint8 InTagQueried=0xFF)
static TArray< FBoundingBox > GetBBsOfActor(const AActor *Actor, uint8 InTagQueried=0xFF)
static FBoundingBox GetCharacterBoundingBox(const ACharacter *Character, uint8 InTagQueried=0xFF)
static void GetISMBoundingBox(UInstancedStaticMeshComponent *ISMComp, TArray< FBoundingBox > &OutBoundingBox)
static FBoundingBox GetSkeletalMeshBoundingBoxFromComponent(const USkeletalMeshComponent *SkeletalMeshComp)
static FBoundingBox GetSkeletalMeshBoundingBox(const USkeletalMesh *SkeletalMesh)
static FBoundingBox GetStaticMeshBoundingBox(const UStaticMesh *StaticMesh)
static void GetTrafficLightBoundingBox(const ATrafficLightBase *TrafficLight, TArray< FBoundingBox > &OutBB, TArray< uint8 > &OutTag, uint8 InTagQueried=0xFF)
static FBoundingBox CombineBBs(const TArray< FBoundingBox > &BBsToCombine)
static void GetBBsOfStaticMeshComponents(const TArray< UStaticMeshComponent * > &StaticMeshComps, TArray< FBoundingBox > &OutBB, TArray< uint8 > &OutTag, uint8 InTagQueried=0xFF)
static FBoundingBox GetVehicleBoundingBox(const ACarlaWheeledVehicle *Vehicle, uint8 InTagQueried=0xFF)
static void CombineBBsOfActor(const AActor *Actor, TArray< FBoundingBox > &OutBB, TArray< uint8 > &OutTag, const float DistanceThreshold=0.0f, uint8 TagToCombine=0xFF)
static FBoundingBox GetActorBoundingBox(const AActor *Actor, uint8 InTagQueried=0xFF)
Compute the bounding box of the given Carla actor.
static void GetMeshCompsFromActorBoundingBox(const AActor *Actor, const FBoundingBox &InBB, TArray< UStaticMeshComponent * > &OutStaticMeshComps)
static FBoundingBox CombineBoxes(const TArray< UBoxComponent * > &BBsToCombine)
static TArray< FBoundingBox > GetBoundingBoxOfActors(const TArray< AActor * > &Actors, uint8 InTagQueried=0xFF)
FRotator Rotation
Rotation of the bounding box.
FVector Origin
Origin of the bounding box relative to its owner.
FVector Extent
Radii extent of the bounding box.