CARLA
 
载入中...
搜索中...
未找到
RayTracer.cpp
浏览该文件的文档.
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"
9
11
12
13namespace crp = carla::rpc;
14
15// 发射一条射线,并返回射线与场景中物体的所有交点信息。
16// 该函数通过 `LineTraceMultiByChannel` 方法进行射线检测,收集所有与射线相交的物体信息。
17// 每个交点的信息包括交点位置和物体的标签(标签由 `ATagger::GetTagOfTaggedComponent` 获取)。
18std::vector<crp::LabelledPoint> URayTracer::CastRay(
19 FVector StartLocation, FVector EndLocation, UWorld * World)
20{
21 TArray<FHitResult> OutHits;
22 World->LineTraceMultiByChannel(
23 OutHits,
24 StartLocation,
25 EndLocation,
26 ECC_GameTraceChannel3, // overlap channel
27 FCollisionQueryParams(),
28 FCollisionResponseParams()
29 );
30 std::vector<crp::LabelledPoint> result;
31 for (auto& Hit : OutHits)
32 {
33 UPrimitiveComponent* Component = Hit.GetComponent();
34 crp::CityObjectLabel ComponentTag =
36
37 FVector UELocation = Hit.Location;
39 ALargeMapManager* LargeMap = GameMode->GetLMManager();
40 if (LargeMap)
41 {
42 UELocation = LargeMap->LocalToGlobalLocation(UELocation);
43 }
44 result.emplace_back(crp::LabelledPoint(UELocation, ComponentTag));
45 }
46 return result;
47}
48
49std::pair<bool, crp::LabelledPoint> URayTracer::ProjectPoint(
50 FVector StartLocation, FVector Direction, float MaxDistance, UWorld * World)
51{
52 FHitResult Hit;
53 bool bDidHit = World->LineTraceSingleByChannel(
54 Hit,
55 StartLocation,
56 StartLocation + Direction.GetSafeNormal() * MaxDistance,
57 ECC_GameTraceChannel2, // camera
58 FCollisionQueryParams(),
59 FCollisionResponseParams()
60 );
61 if (bDidHit)
62 {
63 UPrimitiveComponent* Component = Hit.GetComponent();
64 crp::CityObjectLabel ComponentTag =
66
67 FVector UELocation = Hit.Location;
69 ALargeMapManager* LargeMap = GameMode->GetLMManager();
70 if (LargeMap)
71 {
72 UELocation = LargeMap->LocalToGlobalLocation(UELocation);
73 }
74 return std::make_pair(bDidHit, crp::LabelledPoint(UELocation, ComponentTag));
75 }
76 return std::make_pair(bDidHit, crp::LabelledPoint(FVector(0.0f,0.0f,0.0f), crp::CityObjectLabel::None));
77}
TSharedPtr< const FActorInfo > carla::rpc::ActorState UWorld * World
CARLA Game Mode 的基类。
ALargeMapManager * GetLMManager() const
FVector LocalToGlobalLocation(const FVector &InLocation) const
static crp::CityObjectLabel GetTagOfTaggedComponent(const UPrimitiveComponent &Component)
检索已标记组件的标记。
Definition Tagger.h:52
static ACarlaGameModeBase * GetGameMode(const UObject *WorldContextObject)
static std::pair< bool, carla::rpc::LabelledPoint > ProjectPoint(FVector StartLocation, FVector Direction, float MaxDistance, UWorld *World)
Definition RayTracer.cpp:49
static std::vector< carla::rpc::LabelledPoint > CastRay(FVector StartLocation, FVector EndLocation, UWorld *World)
Definition RayTracer.cpp:18