CARLA
RayTracer.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020 Computer Vision Center (CVC) at the Universitat Autonoma
2 // de Barcelona (UAB).
3 //
4 // This work is licensed under the terms of the MIT license.
5 // For a copy, see <https://opensource.org/licenses/MIT>.
6 
7 #include "Carla.h"
8 #include "Carla/Util/RayTracer.h"
9 
11 
12 
13 namespace crp = carla::rpc;
14 
15 std::vector<crp::LabelledPoint> URayTracer::CastRay(
16  FVector StartLocation, FVector EndLocation, UWorld * World)
17 {
18  TArray<FHitResult> OutHits;
19  World->LineTraceMultiByChannel(
20  OutHits,
21  StartLocation,
22  EndLocation,
23  ECC_GameTraceChannel3, // overlap channel
24  FCollisionQueryParams(),
25  FCollisionResponseParams()
26  );
27  std::vector<crp::LabelledPoint> result;
28  for (auto& Hit : OutHits)
29  {
30  UPrimitiveComponent* Component = Hit.GetComponent();
31  crp::CityObjectLabel ComponentTag =
33 
34  FVector UELocation = Hit.Location;
36  ALargeMapManager* LargeMap = GameMode->GetLMManager();
37  if (LargeMap)
38  {
39  UELocation = LargeMap->LocalToGlobalLocation(UELocation);
40  }
41  result.emplace_back(crp::LabelledPoint(UELocation, ComponentTag));
42  }
43  return result;
44 }
45 
46 std::pair<bool, crp::LabelledPoint> URayTracer::ProjectPoint(
47  FVector StartLocation, FVector Direction, float MaxDistance, UWorld * World)
48 {
49  FHitResult Hit;
50  bool bDidHit = World->LineTraceSingleByChannel(
51  Hit,
52  StartLocation,
53  StartLocation + Direction.GetSafeNormal() * MaxDistance,
54  ECC_GameTraceChannel2, // camera
55  FCollisionQueryParams(),
56  FCollisionResponseParams()
57  );
58  if (bDidHit)
59  {
60  UPrimitiveComponent* Component = Hit.GetComponent();
61  crp::CityObjectLabel ComponentTag =
63 
64  FVector UELocation = Hit.Location;
66  ALargeMapManager* LargeMap = GameMode->GetLMManager();
67  if (LargeMap)
68  {
69  UELocation = LargeMap->LocalToGlobalLocation(UELocation);
70  }
71  return std::make_pair(bDidHit, crp::LabelledPoint(UELocation, ComponentTag));
72  }
73  return std::make_pair(bDidHit, crp::LabelledPoint(FVector(0.0f,0.0f,0.0f), crp::CityObjectLabel::None));
74 }
static crp::CityObjectLabel GetTagOfTaggedComponent(const UPrimitiveComponent &Component)
Retrieve the tag of an already tagged component.
Definition: Tagger.h:52
static ACarlaGameModeBase * GetGameMode(const UObject *WorldContextObject)
Definition: CarlaStatics.h:58
ALargeMapManager * GetLMManager() const
static std::pair< bool, carla::rpc::LabelledPoint > ProjectPoint(FVector StartLocation, FVector Direction, float MaxDistance, UWorld *World)
Definition: RayTracer.cpp:46
Base class for the CARLA Game Mode.
FVector LocalToGlobalLocation(const FVector &InLocation) const
static std::vector< carla::rpc::LabelledPoint > CastRay(FVector StartLocation, FVector EndLocation, UWorld *World)
Definition: RayTracer.cpp:15